blob: 84a0b9c5347ed2fc092e7eecd3583815dcf02198 (
plain)
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
|
/*******************************************************************************
* 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
*/
/******************************************************************************/
/** \file hc32f460_aes.c
**
** A detailed description is available at
** @link AesGroup Aes description @endlink
**
** - 2018-10-20 CDT First version for Device Driver Library of Aes.
**
******************************************************************************/
/*******************************************************************************
* Include files
******************************************************************************/
#include "hc32f460_aes.h"
#include "hc32f460_utility.h"
/**
*******************************************************************************
** \addtogroup AesGroup
******************************************************************************/
//@{
/*******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
/*******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/* AES block length in bytes is 16. */
#define AES_BLOCK_LEN ((uint8_t)16)
/* Each encryption operation takes 440 system clock cycles. */
#define AES_ENCRYPT_TIMEOUT (440u)
/* Each decryption operation takes 580 system clock cycles. */
#define AES_DECRYPT_TIMEOUT (580u)
/*******************************************************************************
* 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(uint8_t *pu8Dest);
static void AES_WriteKey(const uint8_t *pu8Key);
/*******************************************************************************
* Local variable definitions ('static')
******************************************************************************/
/*******************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/
/**
*******************************************************************************
** \brief AES128 encryption(ECB mode).
**
** \param [in] pu8Plaintext Pointer to 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 [out] pu8Ciphertext The destination address to store the result of the encryption.
**
** \retval Ok No error occurred.
** \retval ErrorTimeout AES works timeout.
** \retval ErrorInvalidParameter Parameter error.
**
******************************************************************************/
en_result_t AES_Encrypt(const uint8_t *pu8Plaintext,
uint32_t u32PlaintextSize,
const uint8_t *pu8Key,
uint8_t *pu8Ciphertext)
{
en_result_t enRet = ErrorInvalidParameter;
uint32_t u32BlockOffset;
uint32_t u32Index;
__IO uint32_t u32TimeCount;
if ((NULL != pu8Plaintext) &&
(0u != u32PlaintextSize) &&
(NULL != pu8Key) &&
(NULL != pu8Ciphertext) &&
(0u == (u32PlaintextSize & 0xFu)) && /* u32PlaintextSize % AES_BLOCK_LEN */
(0u == ((uint32_t)pu8Plaintext & 0x3u)) && /* (uint32_t)pu8Ciphertext % 4u */
(0u == ((uint32_t)pu8Key & 0x3u)) && /* (uint32_t)pu8Key % 4u */
(0u == ((uint32_t)pu8Ciphertext & 0x3u))) /* (uint32_t)pu8Plaintext % 4u */
{
/* Write the key to the register. */
AES_WriteKey(pu8Key);
u32BlockOffset = 0u;
while (0u != u32PlaintextSize)
{
/* Stop AES calculating. */
bM4_AES_CR_START = 0u;
/* Write data. */
u32Index = u32BlockOffset * AES_BLOCK_LEN;
AES_WriteData(&pu8Plaintext[u32Index]);
/* Set AES encrypt. */
bM4_AES_CR_MODE = 0u;
/* Start AES calculating. */
bM4_AES_CR_START = 1u;
enRet = ErrorTimeout;
u32TimeCount = 0u;
while (u32TimeCount < AES_ENCRYPT_TIMEOUT)
{
if (bM4_AES_CR_START == 0u)
{
enRet = Ok;
break;
}
u32TimeCount++;
}
if (enRet == ErrorTimeout)
{
break;
}
AES_ReadData(&pu8Ciphertext[u32Index]);
u32PlaintextSize -= AES_BLOCK_LEN;
u32BlockOffset++;
}
/* Stop AES calculating. */
bM4_AES_CR_START = 0u;
}
return enRet;
}
/**
*******************************************************************************
** \brief AES128 decryption(ECB mode).
**
** \param [in] pu8Ciphertext Pointer to 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 [out] pu8Plaintext The destination address to store the result of the decryption.
**
** \retval Ok No error occurred.
** \retval ErrorTimeout AES works timeout.
** \retval ErrorInvalidParameter Parameter error.
**
******************************************************************************/
en_result_t AES_Decrypt(const uint8_t *pu8Ciphertext,
uint32_t u32CiphertextSize,
const uint8_t *pu8Key,
uint8_t *pu8Plaintext)
{
en_result_t enRet = ErrorInvalidParameter;
uint32_t u32BlockOffset;
uint32_t u32Index;
__IO uint32_t u32TimeCount;
if ((NULL != pu8Ciphertext) &&
(0u != u32CiphertextSize) &&
(NULL != pu8Key) &&
(NULL != pu8Plaintext) &&
(0u == (u32CiphertextSize & 0xFu)) && /* u32CiphertextSize % AES_BLOCK_LEN */
(0u == ((uint32_t)pu8Ciphertext & 0x3u)) && /* (uint32_t)pu8Ciphertext % 4u */
(0u == ((uint32_t)pu8Key & 0x3u)) && /* (uint32_t)pu8Key % 4u */
(0u == ((uint32_t)pu8Plaintext & 0x3u))) /* (uint32_t)pu8Plaintext % 4u */
{
/* Write the key to the register. */
AES_WriteKey(pu8Key);
u32BlockOffset = 0u;
while (0u != u32CiphertextSize)
{
/* Stop AES calculating. */
bM4_AES_CR_START = 0u;
/* Write data. */
u32Index = u32BlockOffset * AES_BLOCK_LEN;
AES_WriteData(&pu8Ciphertext[u32Index]);
/* Set AES decrypt. */
bM4_AES_CR_MODE = 1u;
/* Start AES calculating. */
bM4_AES_CR_START = 1u;
enRet = ErrorTimeout;
u32TimeCount = 0u;
while (u32TimeCount < AES_DECRYPT_TIMEOUT)
{
if (bM4_AES_CR_START == 0u)
{
enRet = Ok;
break;
}
u32TimeCount++;
}
if (enRet == ErrorTimeout)
{
break;
}
AES_ReadData(&pu8Plaintext[u32Index]);
u32CiphertextSize -= AES_BLOCK_LEN;
u32BlockOffset++;
}
/* Stop AES calculating. */
bM4_AES_CR_START = 0u;
}
return enRet;
}
/*******************************************************************************
* Function implementation - local ('static')
******************************************************************************/
/**
*******************************************************************************
** \brief Writes the input buffer in data register.
**
** \param [in] pu8SrcData Pointer to source data buffer.
**
** \retval None.
**
******************************************************************************/
static void AES_WriteData(const uint8_t *pu8SrcData)
{
uint8_t i;
uint32_t u32SrcAddr = (uint32_t)pu8SrcData;
uint32_t u32DrAddr = (uint32_t)&(M4_AES->DR0);
for (i = 0u; i < 4u; i++)
{
*(__IO uint32_t *)u32DrAddr = *(uint32_t*)u32SrcAddr;
u32SrcAddr += 4u;
u32DrAddr += 4u;
}
}
/**
*******************************************************************************
** \brief Reads the from data register.
**
** \param [out] pu8Dest Pointer to the destination buffer.
**
** \retval None.
**
******************************************************************************/
static void AES_ReadData(uint8_t *pu8Dest)
{
uint8_t i;
uint32_t u32DestAddr = (uint32_t)pu8Dest;
uint32_t u32DrAddr = (uint32_t)&(M4_AES->DR0);
for (i = 0u; i < 4u; i++)
{
*(uint32_t*)u32DestAddr = *(__IO uint32_t *)u32DrAddr;
u32DestAddr += 4u;
u32DrAddr += 4u;
}
}
/**
*******************************************************************************
** \brief Writes the input buffer in key register.
**
** \param [in] pu8Key Pointer to AES key.
**
** \retval None.
**
******************************************************************************/
static void AES_WriteKey(const uint8_t *pu8Key)
{
uint8_t i;
uint32_t u32SrcKeyAddr = (uint32_t)pu8Key;
uint32_t u32KeyAddr = (uint32_t)&(M4_AES->KR0);
for (i = 0u; i < 4u; i++)
{
*(__IO uint32_t *)u32KeyAddr = *(uint32_t*)u32SrcKeyAddr;
u32SrcKeyAddr += 4u;
u32KeyAddr += 4u;
}
}
//@} // AesGroup
/*******************************************************************************
* EOF (not truncated)
******************************************************************************/
|