1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/**
*******************************************************************************
* @file hc32f4a0_aes.c
* @brief This file provides firmware functions to manage the Advanced Encryption
* Standard(AES).
@verbatim
Change Logs:
Date Author Notes
2020-06-12 Heqb First version
@endverbatim
*******************************************************************************
* Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
*
* This software component is licensed by HDSC under BSD 3-Clause license
* (the "License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
*******************************************************************************
*/
/*******************************************************************************
* Include files
******************************************************************************/
#include "hc32f4a0_aes.h"
#include "hc32f4a0_utility.h"
/**
* @addtogroup HC32F4A0_DDL_Driver
* @{
*/
/**
* @defgroup DDL_AES AES
* @brief AES Driver Library
* @{
*/
#if (DDL_AES_ENABLE == DDL_ON)
/*******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
/*******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/**
* @defgroup AES_Local_Macros AES Local Macros
* @{
*/
/* Delay count for timeout */
#define AES_TIMEOUT (30000UL)
/**
* @defgroup AES_KEY_LENGTH AES key length in bytes
* @{
*/
#define AES_KEY_LEN_128BIT (16U)
#define AES_KEY_LEN_192BIT (24U)
#define AES_KEY_LEN_256BIT (32U)
/**
* @}
*/
/**
* @defgroup AES_Check_Parameters_Validity AES Check Parameters Validity
* @{
*/
#define IS_AES_KEYLENGTH(x) \
( ((x) == AES_KEY_LEN_128BIT) || \
((x) == AES_KEY_LEN_192BIT) || \
((x) == AES_KEY_LEN_256BIT))
/**
* @}
*/
/**
* @}
*/
/*******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
/*******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
static void AES_WriteData(const uint8_t *pu8Srcdata);
static void AES_ReadData(const uint8_t *pu8Result);
static void AES_WriteKey(const uint8_t *pu8Key, uint8_t u8KeyLength);
/*******************************************************************************
* Local variable definitions ('static')
******************************************************************************/
/*******************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/
/**
* @defgroup AES_Global_Functions AES Global Functions
* @{
*/
/**
* @brief AES encryption.
* @param [in] au8Plaintext Buffer of the plaintext(the source data which will be encrypted).
* @param [in] u32PlaintextSize Length of plaintext in bytes.
* @param [in] pu8Key Pointer to the AES key.
* @param [in] u8KeyLength Buffer of the key in bytes.
* @param [out] au8Ciphertext Buffer of the ciphertext.
* @retval An en_result_t enumeration value:
* Ok: Encryption successfully.
* ErrorInvalidParameter: Invalid parameter
* ErrorTimeout: Encryption error timeout
*/
en_result_t AES_Encrypt(uint8_t au8Plaintext[],
uint32_t u32PlaintextSize,
const uint8_t *pu8Key,
uint8_t u8KeyLength,
uint8_t au8Ciphertext[])
{
en_result_t enRet = ErrorInvalidParameter;
uint32_t u32TimeCount = 0UL;
uint32_t u32Index = 0UL;
uint8_t au8FillBuffer[16U] = {0U};
if((au8Plaintext != NULL) && (u32PlaintextSize != 0UL) \
&& (pu8Key != NULL) && (u8KeyLength != 0U) \
&& (au8Ciphertext != NULL))
{
DDL_ASSERT(IS_AES_KEYLENGTH(u8KeyLength));
enRet = Ok;
while(u32PlaintextSize > 0UL)
{
/* Wait for AES to stop */
while(READ_REG32_BIT(M4_AES->CR, AES_CR_START) == 1U)
{
if(u32TimeCount++ >= AES_TIMEOUT)
{
enRet = ErrorTimeout;
break;
}
}
if (enRet == Ok)
{
if (u32PlaintextSize >= AES_BLOCK_LEN)
{
AES_WriteData(&au8Plaintext[u32Index]);
}
else
{
(void)memcpy(au8FillBuffer, &au8Plaintext[u32Index], u32PlaintextSize);
AES_WriteData(&au8FillBuffer[0U]);
}
AES_WriteKey(pu8Key, u8KeyLength);
switch (u8KeyLength)
{
case AES_KEY_LEN_128BIT:
MODIFY_REG32(M4_AES->CR, AES_CR_KEYSIZE, AES_KEY_SIZE_128BIT);
break;
case AES_KEY_LEN_192BIT:
MODIFY_REG32(M4_AES->CR, AES_CR_KEYSIZE, AES_KEY_SIZE_192BIT);
break;
case AES_KEY_LEN_256BIT:
MODIFY_REG32(M4_AES->CR, AES_CR_KEYSIZE, AES_KEY_SIZE_256BIT);
break;
default:
break;
}
/* Set AES encrypt. */
CLEAR_REG32_BIT(M4_AES->CR, AES_CR_MODE);
/* Start AES calculating. */
SET_REG32_BIT(M4_AES->CR, AES_CR_START);
/* Wait for AES to stop */
u32TimeCount = 0UL;
while(READ_REG32_BIT(M4_AES->CR, AES_CR_START) == 1U)
{
if(u32TimeCount++ > AES_TIMEOUT)
{
enRet = ErrorTimeout;
break;
}
}
if (enRet == Ok)
{
AES_ReadData(&au8Ciphertext[u32Index]);
if (u32PlaintextSize < AES_BLOCK_LEN)
{
u32PlaintextSize = 0UL;
}
else
{
u32PlaintextSize -= AES_BLOCK_LEN;
}
u32Index += AES_BLOCK_LEN;
}
}
}
}
return enRet;
}
/**
* @brief AES decryption.
* @param [in] au8Ciphertext Buffer of the Ciphertext(the source data which will be decrypted).
* @param [in] u32CiphertextSize Length of ciphertext in bytes.
* @param [in] pu8Key Pointer to the AES key.
* @param [in] u8KeyLength Length of key in bytes.
* @param [out] au8Plaintext Buffer of the plaintext.
* @retval An en_result_t enumeration value:
* Ok: Decryption successfully.
* ErrorInvalidParameter: Invalid parameter
* ErrorTimeout: Decryption error timeout
*/
en_result_t AES_Decrypt(uint8_t au8Ciphertext[],
uint32_t u32CiphertextSize,
const uint8_t *pu8Key,
uint8_t u8KeyLength,
uint8_t au8Plaintext[])
{
en_result_t enRet = Ok;
uint32_t u32TimeCount = 0UL;
uint32_t u32Index = 0UL;
if((au8Plaintext != NULL) && (u32CiphertextSize != 0UL) \
&& (pu8Key != NULL) && (u8KeyLength != 0U) \
&& (au8Ciphertext != NULL))
{
DDL_ASSERT(IS_AES_KEYLENGTH(u8KeyLength));
enRet = Ok;
while(u32CiphertextSize > 0UL)
{
/* Wait for AES to stop */
while(READ_REG32_BIT(M4_AES->CR, AES_CR_START) == 1U)
{
if(u32TimeCount++ > AES_TIMEOUT)
{
enRet = ErrorTimeout;
break;
}
}
if (enRet == Ok)
{
AES_WriteData(&au8Ciphertext[u32Index]);
AES_WriteKey(pu8Key, u8KeyLength);
switch (u8KeyLength)
{
case AES_KEY_LEN_128BIT:
MODIFY_REG32(M4_AES->CR, AES_CR_KEYSIZE, AES_KEY_SIZE_128BIT);
break;
case AES_KEY_LEN_192BIT:
MODIFY_REG32(M4_AES->CR, AES_CR_KEYSIZE, AES_KEY_SIZE_192BIT);
break;
case AES_KEY_LEN_256BIT:
MODIFY_REG32(M4_AES->CR, AES_CR_KEYSIZE, AES_KEY_SIZE_256BIT);
break;
default:
break;
}
/* Set AES decrypt. */
SET_REG32_BIT(M4_AES->CR, AES_CR_MODE);
/* Start AES calculating. */
SET_REG32_BIT(M4_AES->CR, AES_CR_START);
/* Wait for AES to stop */
u32TimeCount = 0UL;
while(READ_REG32_BIT(M4_AES->CR, AES_CR_START) == 1UL)
{
if(u32TimeCount++ >= AES_TIMEOUT)
{
enRet = ErrorTimeout;
break;
}
}
if (enRet == Ok)
{
AES_ReadData(&au8Plaintext[u32Index]);
u32CiphertextSize -= AES_BLOCK_LEN;
u32Index += AES_BLOCK_LEN;
}
}
}
}
return enRet;
}
/**
* @brief Write the input buffer in data register.
* @param [in] pu8Srcdata Point to the source data buffer.
* @retval None
*/
static void AES_WriteData(const uint8_t *pu8Srcdata)
{
uint8_t i;
uint32_t u32DrAddr = (uint32_t)&(M4_AES->DR0);
uint32_t u32SrcAddr = (uint32_t)pu8Srcdata;
for(i = 0U; i < 4U; i++)
{
RW_MEM32(u32DrAddr) = RW_MEM32(u32SrcAddr);
u32SrcAddr += 4UL;
u32DrAddr += 4UL;
}
}
/**
* @brief Read the from data register.
* @param [out] pu8Result Point to the result buffer.
* @retval None
*/
static void AES_ReadData(const uint8_t *pu8Result)
{
uint8_t i;
uint32_t u32DrAddr = (uint32_t)&(M4_AES->DR0);
uint32_t u32ResultAddr = (uint32_t)pu8Result;
for(i = 0U; i < 4U; i++)
{
RW_MEM32(u32ResultAddr) = RW_MEM32(u32DrAddr);
u32DrAddr += 4U;
u32ResultAddr += 4UL;
}
}
/**
* @brief Write the input buffer in key register.
* @param [in] pu8Key Pointer to the kry buffer.
* @param [in] u8KeyLength Length of key in bytes.
* @retval None
*/
static void AES_WriteKey(const uint8_t *pu8Key, uint8_t u8KeyLength)
{
uint8_t i;
uint8_t Length = 0U;
uint32_t u32KeyAddr = (uint32_t)&(M4_AES->KR0);
uint32_t u32DataAddr = (uint32_t)pu8Key;
switch (u8KeyLength)
{
case AES_KEY_LEN_128BIT:
Length = 4U;
break;
case AES_KEY_LEN_192BIT:
Length = 6U;
break;
case AES_KEY_LEN_256BIT:
Length = 8U;
break;
default:
break;
}
for(i = 0U; i < Length; i++)
{
RW_MEM32(u32KeyAddr) = RW_MEM32(u32DataAddr);
u32DataAddr += 4UL;
u32KeyAddr += 4UL;
}
}
/**
* @}
*/
#endif /* DDL_AES_ENABLE */
/**
* @}
*/
/**
* @}
*/
/*******************************************************************************
* EOF (not truncated)
******************************************************************************/
|