| NUC472_NUC442_BSP V3.04.000 The Board Support Package for NUC472/NUC442

crypto.c
Go to the documentation of this file.
1/**************************************************************************/
13#include <string.h>
14#include "NUC472_442.h"
15
30
31static uint32_t g_AES_CTL[4];
32static uint32_t g_TDES_CTL[4];
33
35
49 void PRNG_Open(uint32_t u32KeySize, uint32_t u32SeedReload, uint32_t u32Seed)
50{
51 if (u32SeedReload)
52 CRPT->PRNG_SEED = u32Seed;
53
54 CRPT->PRNG_CTL = (u32KeySize << CRPT_PRNG_CTL_KEYSZ_Pos) |
55 (u32SeedReload << CRPT_PRNG_CTL_SEEDRLD_Pos);
56}
57
63 void PRNG_Start(void)
64{
65 CRPT->PRNG_CTL |= CRPT_PRNG_CTL_START_Msk;
66}
67
73 void PRNG_Read(uint32_t u32RandKey[])
74{
75 int i, wcnt;
76
77 wcnt = (((CRPT->PRNG_CTL & CRPT_PRNG_CTL_KEYSZ_Msk)>>CRPT_PRNG_CTL_KEYSZ_Pos)+1)*2;
78 for (i = 0; i < wcnt; i++)
79 u32RandKey[i] = *(uint32_t *)((uint32_t)&(CRPT->PRNG_KEY0) + (i * 4));
80}
81
82
107 void AES_Open(uint32_t u32Channel, uint32_t u32EncDec,
108 uint32_t u32OpMode, uint32_t u32KeySize, uint32_t u32SwapType)
109{
110 CRPT->AES_CTL = (u32Channel << CRPT_AES_CTL_CHANNEL_Pos) |
111 (u32EncDec << CRPT_AES_CTL_ENCRPT_Pos) |
112 (u32OpMode << CRPT_AES_CTL_OPMODE_Pos) |
113 (u32KeySize << CRPT_AES_CTL_KEYSZ_Pos) |
114 (u32SwapType << CRPT_AES_CTL_OUTSWAP_Pos);
115 g_AES_CTL[u32Channel] = CRPT->AES_CTL;
116}
117
127 void AES_Start(int32_t u32Channel, uint32_t u32DMAMode)
128{
129 CRPT->AES_CTL = g_AES_CTL[u32Channel];
130 CRPT->AES_CTL |= CRPT_AES_CTL_START_Msk | (u32DMAMode << CRPT_AES_CTL_DMALAST_Pos);
131}
132
143 void AES_SetKey(uint32_t u32Channel, uint32_t au32Keys[], uint32_t u32KeySize)
144{
145 uint32_t i, wcnt, key_reg_addr;
146
147 key_reg_addr = (uint32_t)&CRPT->AES0_KEY0 + (u32Channel * 0x3CUL);
148 wcnt = 4UL + u32KeySize*2UL;
149
150 for (i = 0U; i < wcnt; i++)
151 {
152 outpw(key_reg_addr, au32Keys[i]);
153 key_reg_addr += 4UL;
154 }
155}
156
163 void AES_SetInitVect(uint32_t u32Channel, uint32_t au32IV[])
164{
165 uint32_t i, key_reg_addr;
166
167 key_reg_addr = (uint32_t)&CRPT->AES0_IV0 + (u32Channel * 0x3CUL);
168
169 for (i = 0U; i < 4U; i++)
170 {
171 outpw(key_reg_addr, au32IV[i]);
172 key_reg_addr += 4UL;
173 }
174}
175
184 void AES_SetDMATransfer(uint32_t u32Channel, uint32_t u32SrcAddr,
185 uint32_t u32DstAddr, uint32_t u32TransCnt)
186{
187 uint32_t reg_addr;
188
189 reg_addr = (uint32_t)&CRPT->AES0_SADDR + (u32Channel * 0x3CUL);
190 outpw(reg_addr, u32SrcAddr);
191
192 reg_addr = (uint32_t)&CRPT->AES0_DADDR + (u32Channel * 0x3CUL);
193 outpw(reg_addr, u32DstAddr);
194
195 reg_addr = (uint32_t)&CRPT->AES0_CNT + (u32Channel * 0x3CUL);
196 outpw(reg_addr, u32TransCnt);
197}
198
222 void TDES_Open(uint32_t u32Channel, uint32_t u32EncDec, int32_t Is3DES, int32_t Is3Key,
223 uint32_t u32OpMode, uint32_t u32SwapType)
224{
225 g_TDES_CTL[u32Channel] = (u32Channel << CRPT_TDES_CTL_CHANNEL_Pos) |
226 (u32EncDec << CRPT_TDES_CTL_ENCRPT_Pos) |
227 u32OpMode | (u32SwapType << CRPT_TDES_CTL_BLKSWAP_Pos);
228 if (Is3DES)
229 {
230 g_TDES_CTL[u32Channel] |= CRPT_TDES_CTL_TMODE_Msk;
231 }
232 if (Is3Key)
233 {
234 g_TDES_CTL[u32Channel] |= CRPT_TDES_CTL_3KEYS_Msk;
235 }
236}
237
247 void TDES_Start(int32_t u32Channel, uint32_t u32DMAMode)
248{
249 g_TDES_CTL[u32Channel] |= CRPT_TDES_CTL_START_Msk | (u32DMAMode << CRPT_TDES_CTL_DMALAST_Pos);
250 CRPT->TDES_CTL = g_TDES_CTL[u32Channel];
251}
252
259 void TDES_SetKey(uint32_t u32Channel, uint32_t au32Keys[3][2])
260{
261 uint32_t i, reg_addr;
262
263 reg_addr = (uint32_t)&CRPT->TDES0_KEY1H + (0x40UL * u32Channel);
264
265 for (i = 0U; i < 3U; i++)
266 {
267 outpw(reg_addr, au32Keys[i][0]); /* TDESn_KEYxH */
268 reg_addr += 4UL;
269 outpw(reg_addr, au32Keys[i][1]); /* TDESn_KEYxL */
270 reg_addr += 4UL;
271 }
272}
273
281 void TDES_SetInitVect(uint32_t u32Channel, uint32_t u32IVH, uint32_t u32IVL)
282{
283 uint32_t reg_addr;
284
285 reg_addr = (uint32_t)&CRPT->TDES0_IVH + (u32Channel * 0x40UL);
286 outpw(reg_addr, u32IVH);
287
288 reg_addr = (uint32_t)&CRPT->TDES0_IVL + (u32Channel * 0x40UL);
289 outpw(reg_addr, u32IVL);
290}
291
300 void TDES_SetDMATransfer(uint32_t u32Channel, uint32_t u32SrcAddr,
301 uint32_t u32DstAddr, uint32_t u32TransCnt)
302{
303 uint32_t reg_addr;
304
305 reg_addr = (uint32_t)&CRPT->TDES0_SADDR + (u32Channel * 0x40UL);
306 outpw(reg_addr, u32SrcAddr);
307
308 reg_addr = (uint32_t)&CRPT->TDES0_DADDR + (u32Channel * 0x40UL);
309 outpw(reg_addr, u32DstAddr);
310
311 reg_addr = (uint32_t)&CRPT->TDES0_CNT + (u32Channel * 0x40UL);
312 outpw(reg_addr, u32TransCnt);
313}
314
328 void SHA_Open(uint32_t u32OpMode, uint32_t u32SwapType)
329{
330 CRPT->SHA_CTL = (u32OpMode << CRPT_SHA_CTL_OPMODE_Pos) |
331 (u32SwapType << CRPT_SHA_CTL_OUTSWAP_Pos);
332}
333
342 void SHA_Start(uint32_t u32DMAMode)
343{
344 CRPT->SHA_CTL &= ~(0x7 << CRPT_SHA_CTL_DMALAST_Pos);
345 CRPT->SHA_CTL |= CRPT_SHA_CTL_START_Msk | (u32DMAMode << CRPT_SHA_CTL_DMALAST_Pos);
346}
347
354 void SHA_SetDMATransfer(uint32_t u32SrcAddr, uint32_t u32TransCnt)
355{
356 CRPT->SHA_SADDR = u32SrcAddr;
357 CRPT->SHA_DMACNT = u32TransCnt;
358}
359
365 void SHA_Read(uint32_t u32Digest[])
366{
367 uint32_t i, wcnt;
368
369 i = (CRPT->SHA_CTL & CRPT_SHA_CTL_OPMODE_Msk) >> CRPT_SHA_CTL_OPMODE_Pos;
370 if (i == SHA_MODE_SHA1)
371 wcnt = 5;
372 else if (i == SHA_MODE_SHA224)
373 wcnt = 7;
374 else
375 wcnt = 8;
376
377 for (i = 0; i < wcnt; i++)
378 u32Digest[i] = *(uint32_t *)((uint32_t)&(CRPT->SHA_DGST0) + (i * 4));
379}
380
381 /* end of group NUC472_442_CRYPTO_EXPORTED_FUNCTIONS */
383 /* end of group NUC472_442_CRYPTO_Driver */
385 /* end of group NUC472_442_Device_Driver */
387
388/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
389
NUC472_442.h
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
CRPT_PRNG_CTL_KEYSZ_Msk
#define CRPT_PRNG_CTL_KEYSZ_Msk
Definition: NUC472_442.h:6829
CRPT_SHA_CTL_OPMODE_Pos
#define CRPT_SHA_CTL_OPMODE_Pos
Definition: NUC472_442.h:7350
CRPT_AES_CTL_DMALAST_Pos
#define CRPT_AES_CTL_DMALAST_Pos
Definition: NUC472_442.h:6888
CRPT_TDES_CTL_ENCRPT_Pos
#define CRPT_TDES_CTL_ENCRPT_Pos
Definition: NUC472_442.h:7155
CRPT_PRNG_CTL_KEYSZ_Pos
#define CRPT_PRNG_CTL_KEYSZ_Pos
Definition: NUC472_442.h:6828
CRPT_TDES_CTL_TMODE_Msk
#define CRPT_TDES_CTL_TMODE_Msk
Definition: NUC472_442.h:7138
CRPT_SHA_CTL_OUTSWAP_Pos
#define CRPT_SHA_CTL_OUTSWAP_Pos
Definition: NUC472_442.h:7353
CRPT_AES_CTL_ENCRPT_Pos
#define CRPT_AES_CTL_ENCRPT_Pos
Definition: NUC472_442.h:6900
CRPT_AES_CTL_START_Msk
#define CRPT_AES_CTL_START_Msk
Definition: NUC472_442.h:6880
CRPT_AES_CTL_OPMODE_Pos
#define CRPT_AES_CTL_OPMODE_Pos
Definition: NUC472_442.h:6897
CRPT_TDES_CTL_CHANNEL_Pos
#define CRPT_TDES_CTL_CHANNEL_Pos
Definition: NUC472_442.h:7167
CRPT_TDES_CTL_DMALAST_Pos
#define CRPT_TDES_CTL_DMALAST_Pos
Definition: NUC472_442.h:7143
CRPT_TDES_CTL_BLKSWAP_Pos
#define CRPT_TDES_CTL_BLKSWAP_Pos
Definition: NUC472_442.h:7158
CRPT_TDES_CTL_START_Msk
#define CRPT_TDES_CTL_START_Msk
Definition: NUC472_442.h:7132
CRPT_TDES_CTL_3KEYS_Msk
#define CRPT_TDES_CTL_3KEYS_Msk
Definition: NUC472_442.h:7141
CRPT_AES_CTL_OUTSWAP_Pos
#define CRPT_AES_CTL_OUTSWAP_Pos
Definition: NUC472_442.h:6903
CRPT_PRNG_CTL_SEEDRLD_Pos
#define CRPT_PRNG_CTL_SEEDRLD_Pos
Definition: NUC472_442.h:6825
CRPT_AES_CTL_CHANNEL_Pos
#define CRPT_AES_CTL_CHANNEL_Pos
Definition: NUC472_442.h:6909
CRPT_SHA_CTL_START_Msk
#define CRPT_SHA_CTL_START_Msk
Definition: NUC472_442.h:7339
CRPT_SHA_CTL_DMALAST_Pos
#define CRPT_SHA_CTL_DMALAST_Pos
Definition: NUC472_442.h:7344
CRPT_PRNG_CTL_START_Msk
#define CRPT_PRNG_CTL_START_Msk
Definition: NUC472_442.h:6823
CRPT_SHA_CTL_OPMODE_Msk
#define CRPT_SHA_CTL_OPMODE_Msk
Definition: NUC472_442.h:7351
CRPT_AES_CTL_KEYSZ_Pos
#define CRPT_AES_CTL_KEYSZ_Pos
Definition: NUC472_442.h:6885
SHA_MODE_SHA1
#define SHA_MODE_SHA1
Definition: crypto.h:79
SHA_MODE_SHA224
#define SHA_MODE_SHA224
Definition: crypto.h:80
TDES_Open
void TDES_Open(uint32_t u32Channel, uint32_t u32EncDec, int32_t Is3DES, int32_t Is3Key, uint32_t u32OpMode, uint32_t u32SwapType)
Open TDES encrypt/decrypt function.
Definition: crypto.c:222
SHA_Open
void SHA_Open(uint32_t u32OpMode, uint32_t u32SwapType)
Open SHA encrypt function.
Definition: crypto.c:328
TDES_SetDMATransfer
void TDES_SetDMATransfer(uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt)
Set TDES DMA transfer configuration.
Definition: crypto.c:300
AES_Open
void AES_Open(uint32_t u32Channel, uint32_t u32EncDec, uint32_t u32OpMode, uint32_t u32KeySize, uint32_t u32SwapType)
Open AES encrypt/decrypt function.
Definition: crypto.c:107
PRNG_Open
void PRNG_Open(uint32_t u32KeySize, uint32_t u32SeedReload, uint32_t u32Seed)
Open PRNG function.
Definition: crypto.c:49
SHA_Read
void SHA_Read(uint32_t u32Digest[])
Read the SHA digest.
Definition: crypto.c:365
PRNG_Read
void PRNG_Read(uint32_t u32RandKey[])
Read the PRNG key.
Definition: crypto.c:73
AES_SetInitVect
void AES_SetInitVect(uint32_t u32Channel, uint32_t au32IV[])
Set AES initial vectors.
Definition: crypto.c:163
PRNG_Start
void PRNG_Start(void)
Start to generate one PRNG key.
Definition: crypto.c:63
AES_SetDMATransfer
void AES_SetDMATransfer(uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt)
Set AES DMA transfer configuration.
Definition: crypto.c:184
TDES_SetKey
void TDES_SetKey(uint32_t u32Channel, uint32_t au32Keys[3][2])
Set TDES keys.
Definition: crypto.c:259
TDES_Start
void TDES_Start(int32_t u32Channel, uint32_t u32DMAMode)
Start TDES encrypt/decrypt.
Definition: crypto.c:247
AES_SetKey
void AES_SetKey(uint32_t u32Channel, uint32_t au32Keys[], uint32_t u32KeySize)
Set AES keys.
Definition: crypto.c:143
AES_Start
void AES_Start(int32_t u32Channel, uint32_t u32DMAMode)
Start AES encrypt/decrypt.
Definition: crypto.c:127
SHA_Start
void SHA_Start(uint32_t u32DMAMode)
Start SHA encrypt.
Definition: crypto.c:342
SHA_SetDMATransfer
void SHA_SetDMATransfer(uint32_t u32SrcAddr, uint32_t u32TransCnt)
Set SHA DMA transfer.
Definition: crypto.c:354
TDES_SetInitVect
void TDES_SetInitVect(uint32_t u32Channel, uint32_t u32IVH, uint32_t u32IVL)
Set TDES initial vectors.
Definition: crypto.c:281
outpw
#define outpw(port, value)
Set a 32-bit unsigned value to specified I/O port.
Definition: NUC472_442.h:28915
CRPT
#define CRPT
Definition: NUC472_442.h:28873