M2351_crypto.c
15.6 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/**********************************************************
*
* @file : M2351_crypto.h
* @author : HaewonSeo
*
* @note : M2351 Cryptographic Accelerator
**********************************************************/
#include "M2351_crypto.h"
#include "nsc.h"
uint32_t sessionKey[8] =
{
0x30303030, 0x30303030, 0x30303030, 0x30303030,
0x30303030, 0x30303030, 0x30303030, 0x30303030
};
uint32_t sessionIV[4] =
{
0x30303030, 0x30303030, 0x30303030, 0x30303030
};
char priKey[49] = {0};
char pubKey1[49] = {0}, pubKey2[49] = {0};
char R[49] = {0}, S[49] = {0};
#define KEY_LENGTH 192 /* Select ECC P-192 curve, 192-bits key length */
static char hex_char_tbl[] = "0123456789abcdef";
static char get_Nth_nibble_char(uint32_t val32, uint32_t idx)
{
return hex_char_tbl[(val32 >> (idx * 4U)) & 0xfU ];
}
static void Reg2Hex(int32_t count, uint32_t volatile reg[], char output[])
{
int32_t idx, ri;
uint32_t i;
output[count] = 0U;
idx = count - 1;
for(ri = 0; idx >= 0; ri++)
{
for(i = 0UL; (i < 8UL) && (idx >= 0); i++)
{
output[idx] = get_Nth_nibble_char(reg[ri], i);
idx--;
}
}
}
static void DumpBuffHex(uint8_t *pucBuff, int nBytes)
{
int32_t i32Idx, i;
i32Idx = 0;
while(nBytes > 0)
{
printf("0x%04X ", i32Idx);
for(i = 0; i < 16; i++)
printf("%02x ", pucBuff[i32Idx + i]);
printf(" ");
for(i = 0; i < 16; i++)
{
if((pucBuff[i32Idx + i] >= 0x20) && (pucBuff[i32Idx + i] < 127))
printf("%c", pucBuff[i32Idx + i]);
else
printf(".");
nBytes--;
}
i32Idx += 16;
printf("\n");
}
printf("\n");
}
void CRPT_IRQHandler()
{
if(AES_GET_INT_FLAG(CRPT))
{
g_AES_done = 1;
AES_CLR_INT_FLAG(CRPT);
}
ECC_DriverISR(CRPT);
}
void M2351_Crypto_Init(uint8_t channel, uint8_t modeAES)
{
if(modeAES != ENCRYPT && modeAES != DECRYPT)
printf("Error mode , mode must be ENCRYPT or DECRYPT value\n");
NVIC_EnableIRQ(CRPT_IRQn);
AES_ENABLE_INT(CRPT);
AES_Open(CRPT, channel, modeAES, AES_MODE_CBC, AES_KEY_SIZE_128, AES_IN_OUT_SWAP);
}
void M2351_Crypto_UseMasterKey()
{
CRPT_T *crpt = CRPT;
uint32_t u32Channel = 1;
/* Load Key */
uint32_t key_reg_addr;
// 0x3CUL : channel size
key_reg_addr = (uint32_t)&crpt->AES0_KEY[0] + (u32Channel * 0x3CUL);
/* Enable FMC ISP function */
FMC_Open();
outpw(key_reg_addr, FMC_ReadUID(0));
outpw(key_reg_addr+4UL, FMC_ReadUID(1));
outpw(key_reg_addr+8UL, FMC_ReadUID(2));
outpw(key_reg_addr+12UL, FMC_ReadUID(0));
//printf("\nmasterKey = %08x%08x%08x%08x\n",FMC_ReadUID(0),FMC_ReadUID(1),FMC_ReadUID(2),FMC_ReadUID(0));
/* Disable FMC ISP function */
FMC_Close();
//outpw(key_reg_addr, 0x2b7e1516);
//outpw(key_reg_addr+4UL, 0x28aed2a6);
//outpw(key_reg_addr+8UL, 0xabf71588);
//outpw(key_reg_addr+12UL, 0x09cf4f3c);
/* Load IV */
uint32_t iv_reg_addr;
iv_reg_addr = (uint32_t)&crpt->AES0_IV[0] + (u32Channel * 0x3CUL);
outpw(iv_reg_addr, 0x00000000);
outpw(iv_reg_addr+4UL, 0x00000000);
outpw(iv_reg_addr+8UL, 0x00000000);
outpw(iv_reg_addr+12UL, 0x00000000);
}
void M2351_Crypto_UseSessionKey(uint8_t channel)
{
if (DEMO)
printf("| Secure is running ... Use sessionKey |\n");
uint32_t tmp_sk[4];
uint32_t tmp_si[4];
for (uint8_t z = 0; z < 4; z++)
{
tmp_sk[z] = sessionKey[z];
tmp_si[z] = sessionIV[z];
}
AES_SetKey(CRPT, channel, tmp_sk, AES_KEY_SIZE_128);
AES_SetInitVect(CRPT, channel, tmp_si);
}
/* Function M2351_encrypt_data is same as Nuvoton_M2351_decrypt_data */
void M2351_Encrypt_Data(uint8_t channel, uint8_t InputData[], uint8_t OutputData[], uint32_t bytes)
{
//printf("AES ECB encrypt start.\n");
//printf("&inputData = %p\n",InputData);
//printf("&outputData = %p\n",OutputData);
AES_SetDMATransfer(CRPT, channel, (uint32_t)InputData, (uint32_t)OutputData, bytes);
/*
printf("before start AES : \n"
"CRPT->AES0_CNT : %d\n"
"CRPT->AES0_DADDR : %d\n"
"CRPT->AES0_IV : %d\n"
"CRPT->AES0_KEY : %d\n"
"CRPT->AES0_SADDR : %d\n", CRPT->AES0_CNT,CRPT->AES0_DADDR,CRPT->AES0_IV, CRPT->AES0_KEY, CRPT->AES0_SADDR);
*/
g_AES_done = 0;
/* Start AES encrypt */
AES_Start(CRPT, channel, CRYPTO_DMA_ONE_SHOT);
/* Waiting for AES calculation */
while(!g_AES_done);
//printf("AES encrypt done.\n\n");
//DumpBuffHex((uint8_t*)OutputData, sizeof((uint8_t*)InputData));
}
/* Function M2351_decrypt_data is same as Nuvoton_M2351_encrypt_data */
void M2351_Decrypt_Data(uint8_t channel, uint8_t InputData[], uint8_t OutputData[], uint32_t bytes)
{
//printf("AES ECB decrypt start.\n");
/*
printf("&inputData = %p\n",InputData);
printf("&outputData = %p\n",OutputData);
*/
AES_SetDMATransfer(CRPT, channel, (uint32_t)InputData, (uint32_t)OutputData, bytes);
/*
printf("before start AES : \n"
"CRPT->AES0_CNT : %d\n"
"CRPT->AES0_DADDR : %d\n"
"CRPT->AES0_IV : %d\n"
"CRPT->AES0_KEY : %d\n"
"CRPT->AES0_SADDR : %d\n", CRPT->AES0_CNT,CRPT->AES0_DADDR,CRPT->AES0_IV, CRPT->AES0_KEY, CRPT->AES0_SADDR);
*/
g_AES_done = 0;
/* Start AES decrypt */
AES_Start(CRPT, channel, CRYPTO_DMA_ONE_SHOT);
/* Waiting for AES calculation */
while(!g_AES_done);
//printf("AES decrypt done.\n\n");
//DumpBuffHex((uint8_t*)OutputData, sizeof((uint8_t*)InputData));
}
// OTPn : 64 bit
void M2351_FMC_Read_Key(uint32_t num, uint32_t len, char *priKey)
{
uint32_t u32OtpHw, u32OtpLw;
uint32_t i;
uint32_t otpPrivateKey[6]= {0};
printf("+---------------------------------------------+\n");
printf("| FMC Read OTP Private Key |\n");
printf("+---------------------------------------------+\n");
if(num >= FMC_OTP_ENTRY_CNT)
{
printf("Invalid OTP number.\n");
return ;
}
SYS_UnlockReg(); /* Unlock protected registers */
FMC_Open(); /* Enable FMC ISP function */
for (num = 0; num < len; num++)
{
if(FMC_Read_OTP(num, &u32OtpLw, &u32OtpHw) != 0)
{
printf("Read OTP%d failed!\n", num);
goto lexit;
}
otpPrivateKey[2*num + 0] = u32OtpLw;
otpPrivateKey[2*num + 1] = u32OtpHw;
}
for (num = 0; num < len; num++)
printf("Read OTP%d : 0x%08x-0x%08x.\n", num, otpPrivateKey[2*num + 0], otpPrivateKey[2*num + 1]);
Reg2Hex(48, otpPrivateKey, priKey);
lexit:
FMC_Close(); /* Disable FMC ISP function */
SYS_LockReg(); /* Lock protected registers */
}
void M2351_FMC_Write_Key()
{
uint32_t u32i, u32OtpHw, u32OtpLw;
SYS_UnlockReg(); /* Unlock protected registers */
FMC_Open(); /* Enable FMC ISP function */
for(u32i = 0; u32i < FMC_OTP_ENTRY_CNT; u32i++)
{
if(FMC_Read_OTP(u32i, &u32OtpLw, &u32OtpHw) != 0)
{
printf("Read OTP%d failed!\n", u32i);
goto lexit;
}
if((u32OtpLw == 0xFFFFFFFF) && (u32OtpHw == 0xFFFFFFFF))
{
printf("OTP%d is 0xFFFFFFFF-0xFFFFFFFF. It should be a free entry.\n", u32i);
break;
}
}
if(u32i == FMC_OTP_ENTRY_CNT)
{
printf("All OTP entries are used.\n");
goto lexit;
}
printf("Program OTP%d with 0x%x-0x%x...\n", u32i, 0x5A5A0000 | u32i, 0x00005A5A | u32i);
if(FMC_Write_OTP(u32i, 0x5A5A0000 | u32i, 0x00005A5A | u32i) != 0)
{
printf("Failed to program OTP%d!\n", u32i);
goto lexit;
}
if(FMC_Read_OTP(u32i, &u32OtpLw, &u32OtpHw) != 0)
{
printf("Read OTP%d failed after programmed!\n", u32i);
goto lexit;
}
printf("Read back OTP%d: 0x%x-0x%x.\n", u32i, u32OtpLw, u32OtpHw);
if((u32OtpLw != (0x5A5A0000 | u32i)) || (u32OtpHw != (0x00005A5A | u32i)))
{
printf("OTP%d value is not matched with programmed value!\n", u32i);
goto lexit;
}
printf("Lock OTP%d...\n", u32i);
if(FMC_Lock_OTP(u32i) != 0)
{
printf("Failed to lock OTP%d!\n", u32i);
goto lexit;
}
if(FMC_Read_OTP(u32i, &u32OtpLw, &u32OtpHw) != 0)
{
printf("Read OTP%d failed after programmed!\n", u32i);
goto lexit;
}
printf("Read OTP%d after locked: 0x%x-0x%x.\n", u32i, u32OtpLw, u32OtpHw);
if((u32OtpLw != (0x5A5A0000 | u32i)) || (u32OtpHw != (0x00005A5A | u32i)))
{
printf("OTP%d value is incorrect after locked!\n", u32i);
goto lexit;
}
printf("OTP Write done.\n");
lexit:
FMC_Close(); /* Disable FMC ISP function */
SYS_LockReg(); /* Lock protected registers */
}
void M2351_ECC_GenerateKey(char *priKey, char *gKey1, char *gKey2)
{
//char gKey1[168], gKey2[168]; /* temporary buffer used to keep output public keys */
char Qx[] = "1da18eaaa64fdd781bf699feb935744f18f1fa0efac1b255"; /* expected answer: public key 1 */
char Qy[] = "0bbeb7ba3ba9d4e6b8aa5738ddef4cd15ce400611094d319"; /* expected answer: public key 2 */
int32_t i;
//SYS_UnlockReg();
printf("+---------------------------------------------+\n");
printf("| Crypto ECC Public Key Generation |\n");
printf("+---------------------------------------------+\n");
/* Enable ECC interrupt */
NVIC_EnableIRQ(CRPT_IRQn);
ECC_ENABLE_INT(CRPT);
/* Generate public key from private key d */
if(ECC_GeneratePublicKey(CRPT, CURVE_P_192, priKey, gKey1, gKey2) < 0)
{
printf("ECC key generation failed!!\n");
while(1);
}
/* Verify public key 1 */
/*
if(memcmp(Qx, gKey1, KEY_LENGTH / 8))
{
printf("Public key 1 [%s] is not matched with expected [%s]!\n", gKey1, Qx);
if(memcmp(Qx, gKey1, KEY_LENGTH / 8) == 0)
printf("PASS.\n");
else
printf("Error !!\n");
for(i = 0; i < KEY_LENGTH / 8; i++)
{
if(Qx[i] != gKey1[i])
printf("\n%d - 0x%x 0x%x\n", i, Qx[i], gKey1[i]);
}
while(1);
}
*/
printf("ECC key generated OK.\n");
//printf("pubKey1 : %s\npubKey2 : %s\n", gKey1, gKey2);
}
static int32_t SHAHash(uint32_t u32Mode, uint32_t *pu32Addr, int32_t size, uint32_t digest[])
{
int32_t i;
int32_t n;
/* Enable CRYPTO */
CLK->AHBCLK |= CLK_AHBCLK_CRPTCKEN_Msk;
/* Init SHA */
CRPT->HMAC_CTL = (u32Mode << CRPT_HMAC_CTL_OPMODE_Pos) | CRPT_HMAC_CTL_INSWAP_Msk;
CRPT->HMAC_DMACNT = size;
/* Calculate SHA */
while(size > 0)
{
if(size <= 4)
{
CRPT->HMAC_CTL |= CRPT_HMAC_CTL_DMALAST_Msk;
}
/* Trigger to start SHA processing */
CRPT->HMAC_CTL |= CRPT_HMAC_CTL_START_Msk;
/* Waiting for SHA data input ready */
while((CRPT->HMAC_STS & CRPT_HMAC_STS_DATINREQ_Msk) == 0);
/* Input new SHA date */
CRPT->HMAC_DATIN = *pu32Addr;
pu32Addr++;
size -= 4;
}
/* Waiting for calculation done */
while(CRPT->HMAC_STS & CRPT_HMAC_STS_BUSY_Msk);
/* return SHA results */
if(u32Mode == SHA_MODE_SHA1)
n = 5;
else if(u32Mode == SHA_MODE_SHA224)
n = 7;
else if(u32Mode == SHA_MODE_SHA256)
n = 8;
else if(u32Mode == SHA_MODE_SHA384)
n = 12;
for(i = 0; i < n; i++)
digest[i] = CRPT->HMAC_DGST[i];
return 0;
}
void M2351_SHA_Hash(uint8_t *msg, uint8_t *hash_msg)
{
int32_t i;
uint32_t u32_hash[5];
printf("+---------------------------------------------+\n");
printf("| M2351 Crypto SHA-1 Hash |\n");
printf("+---------------------------------------------+\n");
NVIC_EnableIRQ(CRPT_IRQn);
printf("Input data:\n");
for(i=0;i<16;i++)
{
printf("%02x", msg[i]);
}
printf("\n");
SHAHash(SHA_MODE_SHA1, (uint32_t *)msg, 16, u32_hash);
//printf("\nOutput SHA1 Hash:\n");
//for(i=0; i<5; i++)
// printf("%08x", u32_hash[i]);
/*
printf("\n");
SHAHash(SHA_MODE_SHA224, (uint32_t *)g_au8Test, 32, hash);
printf("\nOutput SHA224 Hash:\n");
for(i=0;i<7;i++)
printf("%08x",hash[i]);
printf("\n");
SHAHash(SHA_MODE_SHA256, (uint32_t *)g_au8Test, 32, hash);
printf("\nOutput SHA256 Hash:\n");
for(i=0;i<8;i++)
printf("%08x",hash[i]);
printf("\n");
SHAHash(SHA_MODE_SHA384, (uint32_t *)g_au8Test, 32, hash);
printf("\nOutput SHA384 Hash:\n");
for(i=0;i<12;i++)
printf("%08x",hash[i]);
*/
Reg2Hex(40, u32_hash, (char *)hash_msg);
printf("\nOutput SHA1 Hash:\n%s\n", hash_msg);
//while(1);
}
void M2351_ECDSA_GenerateSignature(uint8_t *msg, char *priKey, char* R, char *S)
{
//char g_SHA_msg[] = "608079423f12421de616b7493ebe551cf4d65b92"; /* SHA-1 hash */
char hash_msg[41] = {0};
//char gD[] = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3"; /* private key */
char gK[] = "cb0abc7043a10783684556fb12c4154d57bc31a289685f25"; /* random integer k form [1, n-1] */
//char gR1[] = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e"; /* Expected answer: R of (R,S) digital signature */
//char gS1[] = "9cbd7f157288b914a844d941bcdf46ae2355f993d040fbed"; /* Expected answer: S of (R,S) digital signature */
//char gR[168], gS[168]; /* temporary buffer used to keep digital signature (R,S) pair */
NVIC_EnableIRQ(CRPT_IRQn);
ECC_ENABLE_INT(CRPT);
printf("\n");
printf("+---------------------------------------------+\n");
printf("| Crypto ECDSA Singnature Generation |\n");
printf("+---------------------------------------------+\n");
M2351_SHA_Hash(msg, (uint8_t *)hash_msg);
/* Calculate ECC signature */
if(ECC_GenerateSignature(CRPT, CURVE_P_192, hash_msg, priKey, gK, R, S) < 0)
{
printf("ECC signature generation failed!!\n");
while(1);
}
/* Verify the signature (R,S) */
/*
if(memcmp(gR, gR1, sizeof(gR)))
{
printf("Signature R [%s] is not matched with expected [%s]!\n", gR, gR1);
while(1);
}
if(memcmp(gS, gS1, sizeof(gS)))
{
printf("Signature S [%s] is not matched with expected [%s]!\n", gS, gS1);
while(1);
}
*/
printf("\nECC digital signature generated OK.\n");
printf("Signature R : %s\nSignature S : %s\n", R, S);
}
void M2351_ECDSA_VerificationSignature(char *hash_msg, char *pubKey1, char* pubKey2, char* R, char *S)
{
//char sha_msg[] = "f621926efff296c8b7b5041577237d09d994b481"; /* SHA-1 hash */
//char Qx[] = "1da18eaaa64fdd781bf699feb935744f18f1fa0efac1b255"; /* expected answer: public key 1 */
//char Qy[] = "0bbeb7ba3ba9d4e6b8aa5738ddef4cd15ce400611094d319"; /* expected answer: public key 2 */
//char R[] = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e"; /* Expected answer: R of (R,S) digital signature */
//char S[] = "ba2de074a517000325d5dbebef8acfb3943eab222ae56f46"; /* Expected answer: S of (R,S) digital signature */
/* Enable crypto interrupt */
NVIC_EnableIRQ(CRPT_IRQn);
ECC_ENABLE_INT(CRPT);
printf("+---------------------------------------------+\n");
printf("| Crypto ECDSA Singnature Verification |\n");
printf("+---------------------------------------------+\n");
/* Verify the signature */
if(ECC_VerifySignature(CRPT, CURVE_P_192, hash_msg, pubKey1, pubKey2, R, S) < 0)
{
printf("ECC signature verification failed!!\n");
while(1);
}
printf("ECC digital signature verification OK.\n");
}