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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/**************************************************************************//**
* @file crypto.c
* @version V1.10
* $Revision: 3 $
* $Date: 15/06/12 9:42a $
* @brief Cryptographic Accelerator driver source file
*
* @note
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2015 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include "N9H30.h"
#include "nu_crypto.h"
/** @addtogroup N9H30_Device_Driver N9H30 Device Driver
@{
*/
/** @addtogroup N9H30_CRYPTO_Driver CRYPTO Driver
@{
*/
/** @addtogroup N9H30_CRYPTO_EXPORTED_FUNCTIONS CRYPTO Exported Functions
@{
*/
/// @cond HIDDEN_SYMBOLS
static uint32_t g_AES_CTL[4];
static uint32_t g_TDES_CTL[4];
/// @endcond HIDDEN_SYMBOLS
/**
* @brief Open PRNG function
* @param[in] u32KeySize is PRNG key size, including:
* - \ref PRNG_KEY_SIZE_64
* - \ref PRNG_KEY_SIZE_128
* - \ref PRNG_KEY_SIZE_192
* - \ref PRNG_KEY_SIZE_256
* @param[in] u32SeedReload is PRNG seed reload or not, including:
* - \ref PRNG_SEED_CONT
* - \ref PRNG_SEED_RELOAD
* @param[in] u32Seed The new seed. Only valid when u32SeedReload is PRNG_SEED_RELOAD.
* @return None
*/
void PRNG_Open(uint32_t u32KeySize, uint32_t u32SeedReload, uint32_t u32Seed)
{
if (u32SeedReload)
CRPT->PRNG_SEED = u32Seed;
CRPT->PRNG_CTL = (u32KeySize << CRPT_PRNG_CTL_KEYSZ_Pos) |
(u32SeedReload << CRPT_PRNG_CTL_SEEDRLD_Pos);
}
/**
* @brief Start to generate one PRNG key.
* @return None
*/
void PRNG_Start(void)
{
CRPT->PRNG_CTL |= CRPT_PRNG_CTL_START_Msk;
}
/**
* @brief Read the PRNG key.
* @param[out] u32RandKey The key buffer to store newly generated PRNG key.
* @return None
*/
void PRNG_Read(uint32_t u32RandKey[])
{
uint32_t i, wcnt;
wcnt = (((CRPT->PRNG_CTL & CRPT_PRNG_CTL_KEYSZ_Msk) >> CRPT_PRNG_CTL_KEYSZ_Pos) + 1U) * 2U;
for (i = 0U; i < wcnt; i++)
{
u32RandKey[i] = CRPT->PRNG_KEY[i];
}
CRPT->PRNG_CTL &= ~CRPT_PRNG_CTL_SEEDRLD_Msk;
}
/**
* @brief Open AES encrypt/decrypt function.
* @param[in] u32Channel AES channel. Must be 0~3.
* @param[in] u32EncDec 1: AES encode; 0: AES decode
* @param[in] u32OpMode AES operation mode, including:
* - \ref AES_MODE_ECB
* - \ref AES_MODE_CBC
* - \ref AES_MODE_CFB
* - \ref AES_MODE_OFB
* - \ref AES_MODE_CTR
* - \ref AES_MODE_CBC_CS1
* - \ref AES_MODE_CBC_CS2
* - \ref AES_MODE_CBC_CS3
* @param[in] u32KeySize is AES key size, including:
* - \ref AES_KEY_SIZE_128
* - \ref AES_KEY_SIZE_192
* - \ref AES_KEY_SIZE_256
* @param[in] u32SwapType is AES input/output data swap control, including:
* - \ref AES_NO_SWAP
* - \ref AES_OUT_SWAP
* - \ref AES_IN_SWAP
* - \ref AES_IN_OUT_SWAP
* @return None
*/
void AES_Open(uint32_t u32Channel, uint32_t u32EncDec,
uint32_t u32OpMode, uint32_t u32KeySize, uint32_t u32SwapType)
{
CRPT->AES_CTL = (u32Channel << CRPT_AES_CTL_CHANNEL_Pos) |
(u32EncDec << CRPT_AES_CTL_ENCRPT_Pos) |
(u32OpMode << CRPT_AES_CTL_OPMODE_Pos) |
(u32KeySize << CRPT_AES_CTL_KEYSZ_Pos) |
(u32SwapType << CRPT_AES_CTL_OUTSWAP_Pos);
g_AES_CTL[u32Channel] = CRPT->AES_CTL;
}
/**
* @brief Start AES encrypt/decrypt
* @param[in] u32Channel AES channel. Must be 0~3.
* @param[in] u32DMAMode AES DMA control, including:
* - \ref CRYPTO_DMA_ONE_SHOT One shop AES encrypt/decrypt.
* - \ref CRYPTO_DMA_CONTINUE Continuous AES encrypt/decrypt.
* - \ref CRYPTO_DMA_LAST Last AES encrypt/decrypt of a series of AES_Start.
* @return None
*/
void AES_Start(int32_t u32Channel, uint32_t u32DMAMode)
{
CRPT->AES_CTL = g_AES_CTL[u32Channel];
CRPT->AES_CTL |= CRPT_AES_CTL_START_Msk | (u32DMAMode << CRPT_AES_CTL_DMALAST_Pos);
}
/**
* @brief Set AES keys
* @param[in] u32Channel AES channel. Must be 0~3.
* @param[in] au32Keys An word array contains AES keys.
* @param[in] u32KeySize is AES key size, including:
* - \ref AES_KEY_SIZE_128
* - \ref AES_KEY_SIZE_192
* - \ref AES_KEY_SIZE_256
* @return None
*/
void AES_SetKey(uint32_t u32Channel, uint32_t au32Keys[], uint32_t u32KeySize)
{
int i, wcnt;
uint32_t *key_ptr;
key_ptr = (uint32_t *)((uint32_t)&CRPT->AES0_KEY0 + (u32Channel * 0x3C));
wcnt = 4 + u32KeySize * 2;
for (i = 0; i < wcnt; i++, key_ptr++)
*key_ptr = au32Keys[i];
}
/**
* @brief Set AES initial vectors
* @param[in] u32Channel AES channel. Must be 0~3.
* @param[in] au32IV A four entry word array contains AES initial vectors.
* @return None
*/
void AES_SetInitVect(uint32_t u32Channel, uint32_t au32IV[])
{
int i;
uint32_t *key_ptr;
key_ptr = (uint32_t *)((uint32_t)&CRPT->AES0_IV0 + (u32Channel * 0x3C));
for (i = 0; i < 4; i++, key_ptr++)
*key_ptr = au32IV[i];
}
/**
* @brief Set AES DMA transfer configuration.
* @param[in] u32Channel AES channel. Must be 0~3.
* @param[in] u32SrcAddr AES DMA source address
* @param[in] u32DstAddr AES DMA destination address
* @param[in] u32TransCnt AES DMA transfer byte count
* @return None
*/
void AES_SetDMATransfer(uint32_t u32Channel, uint32_t u32SrcAddr,
uint32_t u32DstAddr, uint32_t u32TransCnt)
{
*(uint32_t *)((uint32_t)&CRPT->AES0_SADDR + (u32Channel * 0x3C)) = u32SrcAddr;
*(uint32_t *)((uint32_t)&CRPT->AES0_DADDR + (u32Channel * 0x3C)) = u32DstAddr;
*(uint32_t *)((uint32_t)&CRPT->AES0_CNT + (u32Channel * 0x3C)) = u32TransCnt;
}
/**
* @brief Open TDES encrypt/decrypt function.
* @param[in] u32Channel TDES channel. Must be 0~3.
* @param[in] u32EncDec 1: TDES encode; 0: TDES decode
* @param[in] u32OpMode TDES operation mode, including:
* - \ref TDES_MODE_ECB
* - \ref TDES_MODE_CBC
* - \ref TDES_MODE_CFB
* - \ref TDES_MODE_OFB
* - \ref TDES_MODE_CTR
* @param[in] u32SwapType is TDES input/output data swap control and word swap control, including:
* - \ref TDES_NO_SWAP
* - \ref TDES_WHL_SWAP
* - \ref TDES_OUT_SWAP
* - \ref TDES_OUT_WHL_SWAP
* - \ref TDES_IN_SWAP
* - \ref TDES_IN_WHL_SWAP
* - \ref TDES_IN_OUT_SWAP
* - \ref TDES_IN_OUT_WHL_SWAP
* @return None
*/
void TDES_Open(uint32_t u32Channel, uint32_t u32EncDec, int32_t Is3DES, int32_t Is3Key,
uint32_t u32OpMode, uint32_t u32SwapType)
{
g_TDES_CTL[u32Channel] = (u32Channel << CRPT_TDES_CTL_CHANNEL_Pos) |
(u32EncDec << CRPT_TDES_CTL_ENCRPT_Pos) |
u32OpMode | (u32SwapType << CRPT_TDES_CTL_BLKSWAP_Pos);
if (Is3DES)
{
g_TDES_CTL[u32Channel] |= CRPT_TDES_CTL_TMODE_Msk;
}
if (Is3Key)
{
g_TDES_CTL[u32Channel] |= CRPT_TDES_CTL_3KEYS_Msk;
}
}
/**
* @brief Start TDES encrypt/decrypt
* @param[in] u32Channel TDES channel. Must be 0~3.
* @param[in] u32DMAMode TDES DMA control, including:
* - \ref CRYPTO_DMA_ONE_SHOT One shop TDES encrypt/decrypt.
* - \ref CRYPTO_DMA_CONTINUE Continuous TDES encrypt/decrypt.
* - \ref CRYPTO_DMA_LAST Last TDES encrypt/decrypt of a series of TDES_Start.
* @return None
*/
void TDES_Start(int32_t u32Channel, uint32_t u32DMAMode)
{
g_TDES_CTL[u32Channel] |= CRPT_TDES_CTL_START_Msk | (u32DMAMode << CRPT_TDES_CTL_DMALAST_Pos);
CRPT->TDES_CTL = g_TDES_CTL[u32Channel];
}
/**
* @brief Set TDES keys
* @param[in] u32Channel TDES channel. Must be 0~3.
* @param[in] au8Keys The TDES keys.
* @return None
*/
void TDES_SetKey(uint32_t u32Channel, uint32_t au32Keys[3][2])
{
int i;
uint32_t *pu32TKey;
pu32TKey = (uint32_t *)((uint32_t)&CRPT->TDES0_KEY1H + (0x40 * u32Channel));
for (i = 0; i < 3; i++)
{
*pu32TKey = au32Keys[i][0]; /* TDESn_KEYxH */
pu32TKey++;
*pu32TKey = au32Keys[i][1]; /* TDESn_KEYxL */
pu32TKey++;
}
}
/**
* @brief Set TDES initial vectors
* @param[in] u32Channel TDES channel. Must be 0~3.
* @param[in] u32IVH TDES initial vector high word.
* @param[in] u32IVL TDES initial vector low word.
* @return None
*/
void TDES_SetInitVect(uint32_t u32Channel, uint32_t u32IVH, uint32_t u32IVL)
{
*(uint32_t *)((uint32_t)&CRPT->TDES0_IVH + 0x40 * u32Channel) = u32IVH;
*(uint32_t *)((uint32_t)&CRPT->TDES0_IVL + 0x40 * u32Channel) = u32IVL;
}
/**
* @brief Set TDES DMA transfer configuration.
* @param[in] u32Channel TDES channel. Must be 0~3.
* @param[in] u32SrcAddr TDES DMA source address
* @param[in] u32DstAddr TDES DMA destination address
* @param[in] u32TransCnt TDES DMA transfer byte count
* @return None
*/
void TDES_SetDMATransfer(uint32_t u32Channel, uint32_t u32SrcAddr,
uint32_t u32DstAddr, uint32_t u32TransCnt)
{
*(uint32_t *)((uint32_t)&CRPT->TDES0_SADDR + (u32Channel * 0x40)) = u32SrcAddr;
*(uint32_t *)((uint32_t)&CRPT->TDES0_DADDR + (u32Channel * 0x40)) = u32DstAddr;
*(uint32_t *)((uint32_t)&CRPT->TDES0_CNT + (u32Channel * 0x40)) = u32TransCnt;
}
/**
* @brief Open SHA encrypt function.
* @param[in] u32OpMode SHA operation mode, including:
* - \ref SHA_MODE_SHA1
* - \ref SHA_MODE_SHA224
* - \ref SHA_MODE_SHA256
* - \ref SHA_MODE_SHA384
* - \ref SHA_MODE_SHA512
* @param[in] u32SwapType is SHA input/output data swap control, including:
* - \ref SHA_NO_SWAP
* - \ref SHA_OUT_SWAP
* - \ref SHA_IN_SWAP
* - \ref SHA_IN_OUT_SWAP
* @param[in] hmac_key_len The length of HMAC key if HMAC is employed.
* If HMAC is not used, just give hmac_key_len a zero value.
* @return None
*/
void SHA_Open(uint32_t u32OpMode, uint32_t u32SwapType, int hmac_key_len)
{
CRPT->HMAC_CTL = (u32OpMode << CRPT_HMAC_CTL_OPMODE_Pos) |
(u32SwapType << CRPT_HMAC_CTL_OUTSWAP_Pos);
if (hmac_key_len > 0)
{
CRPT->HMAC_KEYCNT = hmac_key_len;
CRPT->HMAC_CTL |= CRPT_HMAC_CTL_HMACEN_Msk;
}
}
/**
* @brief Start SHA encrypt
* @param[in] u32DMAMode TDES DMA control, including:
* - \ref CRYPTO_DMA_ONE_SHOT One shop SHA encrypt.
* - \ref CRYPTO_DMA_CONTINUE Continuous SHA encrypt.
* - \ref CRYPTO_DMA_LAST Last SHA encrypt of a series of SHA_Start.
* @return None
*/
void SHA_Start(uint32_t u32DMAMode)
{
CRPT->HMAC_CTL &= ~(0x7 << CRPT_HMAC_CTL_DMALAST_Pos);
CRPT->HMAC_CTL |= CRPT_HMAC_CTL_START_Msk | (u32DMAMode << CRPT_HMAC_CTL_DMALAST_Pos);
}
/**
* @brief Set SHA DMA transfer
* @param[in] u32SrcAddr SHA DMA source address
* @param[in] u32TransCnt SHA DMA transfer byte count
* @return None
*/
void SHA_SetDMATransfer(uint32_t u32SrcAddr, uint32_t u32TransCnt)
{
CRPT->HMAC_SADDR = u32SrcAddr;
CRPT->HMAC_DMACNT = u32TransCnt;
}
/**
* @brief Read the SHA digest.
* @param[out] u32Digest The SHA encrypt output digest.
* @return None
*/
void SHA_Read(uint32_t u32Digest[])
{
uint32_t i, wcnt;
i = (CRPT->HMAC_CTL & CRPT_HMAC_CTL_OPMODE_Msk) >> CRPT_HMAC_CTL_OPMODE_Pos;
if (i == SHA_MODE_SHA1)
{
wcnt = 5UL;
}
else if (i == SHA_MODE_SHA224)
{
wcnt = 7UL;
}
else if (i == SHA_MODE_SHA256)
{
wcnt = 8UL;
}
else if (i == SHA_MODE_SHA384)
{
wcnt = 12UL;
}
else
{
/* SHA_MODE_SHA512 */
wcnt = 16UL;
}
for (i = 0; i < wcnt; i++)
u32Digest[i] = *(uint32_t *)((uint32_t) & (CRPT->HMAC_DGST0) + (i * 4));
}
/*@}*/ /* end of group N9H30_CRYPTO_EXPORTED_FUNCTIONS */
/*@}*/ /* end of group N9H30_CRYPTO_Driver */
/*@}*/ /* end of group N9H30_Device_Driver */
/*** (C) COPYRIGHT 2015 Nuvoton Technology Corp. ***/
|