signverify.c
16.1 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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#include <PR/bcp.h>
#include <PR/R4300.h>
#include <PR/bbsim.h>
#include <PR/bbskapi.h>
#include <PR/os.h>
#include <PR/os_bbfs.h>
#include <PR/os_bbatb.h>
#include <PR/os_bbexec.h>
#include <PR/os_bbsa.h>
#include <sha1.h>
#include "signverify.h"
#include "nu64sys.h"
#include "graph.h"
u32 gMiSecModeRegEntryVal;
/*
* buffer for FS must be dcache aligned. use macro below.
*/
#define ALIGN_DCACHE __attribute__((aligned(DCACHE_LINESIZE)))
/*
* debug print support
*/
#undef PRINTF
#ifdef DEBUG
#define PRINTF osSyncPrintf
#else
#define PRINTF(format, args...)
#endif
/*
* Thread and stack structures
*/
char bootStack[STACKSIZE] __attribute__ ((aligned (8)));
static OSThread idleThread;
static char idleThreadStack[STACKSIZE] __attribute__ ((aligned (8)));
static OSThread mainThread;
static char mainThreadStack[STACKSIZE] __attribute__ ((aligned (8)));
/*
* FS specific definitions
*/
static void idleproc(char *);
static void mainproc(char *);
static OSBbFs gFs;
/* XXX: hard-coded size requires more consideration */
#define BB_SYSAPP_NUM_DIR_ENTRIES 256
static OSBbDirEnt gFsDirs[BB_SYSAPP_NUM_DIR_ENTRIES];
static const char gSignFname[] = "sig.sys";
static const char gIdFname[] = "cert.sys";
static const char gSaRlFname[] = "crl.sys";
static const char gEccFname[] = "ecc.sys";
static const char bbChainFname[] = "ida.sys";
#define BB_SYSAPP_RL_BUF_SIZE (16*1024*4)
static u8 gRlBuf[BB_SYSAPP_RL_BUF_SIZE] ALIGN_DCACHE;
#define BB_SYSAPP_SERIAL_HEAP_SIZE (16*1024*8)
static u8 gSerialHeap[BB_SYSAPP_SERIAL_HEAP_SIZE] ALIGN_DCACHE;
#define BB_SIG_HEAP_SIZE (16*1024*8)
static u8 gSigHeap[BB_SIG_HEAP_SIZE] ALIGN_DCACHE;
#define BB_ECC_HEAP_SIZE (16*1024)
static u8 gEccHeap[BB_ECC_HEAP_SIZE] ALIGN_DCACHE;
#define DMA_QUEUE_SIZE 200
static OSMesg PiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue PiMessageQ;
static OSMesg SiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue SiMessageQ;
/*
* simple graphics
*/
static OSMesgQueue retraceMessageQ;
static OSMesg dummyMessage, retraceMessageBuf;
static u16 cfb[SCREEN_WD*SCREEN_HT] __attribute__((aligned(64)));
static void clear(u16 bg) {
int i;
for (i = 0; i < SCREEN_WD*SCREEN_HT; ++i) {
cfb[i] = bg;
}
}
static void
delay(void) {
int k;
for(k = 0; k < 1000000; k++)
;
}
void __osBbVideoPllInit(u32);
void boot(u32 miSecModeRegVal)
{
gMiSecModeRegEntryVal = miSecModeRegVal;
/* Init the video PLL */
__osBbVideoPllInit(OS_TV_NTSC);
osInitialize();
osCreateThread(&idleThread, 1, (void(*)(void *))idleproc, (void *)0,
idleThreadStack+STACKSIZE, 8);
osStartThread(&idleThread);
}
static void
idleproc(char *argv) /* priority 8 */
{
osCreateViManager(OS_PRIORITY_VIMGR);
#if SCREEN_LOW
osViSetMode(&osViModeTable[OS_VI_NTSC_LPN1]);
#else
osViSetMode(&osViModeTable[OS_VI_NTSC_HPF1]);
#endif
/*
* Start PI Mgr for access to cartridge - start before the debugger
*/
osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
DMA_QUEUE_SIZE);
osCreateMesgQueue(&SiMessageQ, SiMessages, DMA_QUEUE_SIZE);
osSetEventMesg(OS_EVENT_SI, &SiMessageQ, (OSMesg)DMA_QUEUE_SIZE);
/*
* The main thread's priority must be the same or lower than the original
* idle's thread priority. This allows the idle thread to change its
* priority to 0 before the main thread starts execution.
*/
osCreateThread(&mainThread, 3, (void(*)(void *))mainproc, argv,
mainThreadStack+STACKSIZE/8, (OSPri)7);
osStartThread(&mainThread);
osSetThreadPri(0, OS_PRIORITY_IDLE);
for(;;) {
#if 0
osBbSetErrorLed(1);
delay();
osBbSetErrorLed(0);
delay();
#endif
}
}
/* TODO: error handling in place of "goto exit" lines in code below. */
BbShaHash hash_data;
BbShaHash hash_copy;
SHA1Context sha;
BbGenericSig sign;
#define STATE_SIZE 1024
#define SIGN_SIZE 256 /* short cutting here, for rsa sign */
void readInCrl(BbAppLaunchCrls *apprls){
/* read entire rl file into serialized heap */
#ifdef DEBUG
PRINTF("create bundle\n");
#endif
if(osBbSaRlInit() != BB_SYSAPP_PASS){
#ifdef DEBUG
PRINTF("crl reading error\n");
#endif
}
if(osBbSaRlBundle(apprls)!=BB_SYSAPP_PASS){
#ifdef DEBUG
PRINTF("bundle creation error\n");
#endif
}
}
void mainproc(char *argv)
{
int i;
s32 fsret, fd, signfd;
OSBbStatBuf fsStat;
u8 *certHeap;
BbCertBase *certChain[BB_CERT_CHAIN_MAXLEN];
u8 statedata[STATE_SIZE];
u8 filename[512];
u32 *signdata;
BbEccSig sig;
BbGenericSig signature;
BbAppLaunchCrls appRls;
int expected;
char outstring[512];
int iter;
int numiter = 50;
u32 starttime;
u32 endtime;
u32 first, second, third, fourth;
u32 ret;
/* setup text output to screen */
osCreateMesgQueue(&retraceMessageQ, &retraceMessageBuf, 1);
osViSetEvent(&retraceMessageQ, dummyMessage, 1);
osViBlack(1);
osViSwapBuffer(cfb);
clear(0);
osViBlack(0);
osWritebackDCacheAll();
osViSwapBuffer(cfb);
printstr(white, 3, 1, "Test Sign/Verify");
osWritebackDCacheAll();
delay();
/* pointer tests first ! */
for(iter=0; iter< 8; iter++){
if(iter ==0){
/* starts at end */
first = (K0BASE+0x800000);
second = (u32 ) sig;
expected = SK_API_FAIL;
}
else if(iter ==1){
/* crosses over end */
first =( K0BASE+0x800000) - sizeof(BbShaHash)+4;
second = (u32 ) sig;
expected = SK_API_FAIL;
}
else if(iter ==2){
/* crosses over beginning */
first = K0BASE -4;
second = (u32 )sig;
expected = SK_API_FAIL;
}
else if(iter ==3){
/* alignment */
first = K0BASE + 3;
second = (u32 )sig;
expected = SK_API_FAIL;
}
/* repeat for sig pointer */
else if(iter ==4){
first = (u32)hash_data;
second = (K0BASE+0x800000);
expected = SK_API_FAIL;
}
else if(iter ==5){
first = (u32)hash_data;
second = ( K0BASE+0x800000) - sizeof(BbShaHash)+4;
expected = SK_API_FAIL;
}
else if(iter ==6){
first = (u32)hash_data;
second = K0BASE -4;
expected = SK_API_FAIL;
}
else if(iter ==7){
first = (u32)hash_data;
second = K0BASE +3;
expected = SK_API_FAIL;
}
if(skSignHash((u32 *)first, (u32 *)second) == expected){
#ifdef DEBUG
PRINTF("Pointer check pass \n");
#endif
printstr(white, 3, 2, "Pointer Check PASS");
osWritebackDCacheAll();
}
else{
#ifdef DEBUG
PRINTF("Pointer check fail \n");
#endif
printstr(white, 3, 3, "Pointer Check FAIL");
osWritebackDCacheAll();
}
}
/* pointer checks on verify */
for(iter =0; iter < 7; iter++){
if(iter ==0){
/* starts at end */
first = K0BASE + (0x800000);
second = (u32 ) sig;
third = 0;
fourth = 0;
expected = SK_API_FAIL;
}
else if(iter ==1){
first = (u32)hash_data;
second = ( K0BASE+0x800000) - sizeof(BbShaHash)+4;
third = 0;
fourth = 0;
expected = SK_API_FAIL;
}
else if(iter ==2){
first = (u32)hash_data;
second = ( K0BASE+0x800000) - sizeof(BbShaHash)+4;
third = 0;
fourth = 0;
expected = SK_API_FAIL;
}
else if(iter ==3){
first = (u32) hash_data;
second = (u32) sig;
third = K0BASE - 4;
fourth = 0;
expected = SK_API_FAIL;
}
else if(iter ==4){
first = (u32) hash_data;
second = (u32) sig;
third = K0BASE;
appRls.carl.head = (BbCrlHead *)(K0BASE+0x800000);
fourth = (u32) &appRls;
}
else if(iter ==5){
first = (u32) hash_data;
second = (u32) sig;
third = K0BASE;
appRls.carl.list = (BbServerSuffix *) (K0BASE-4);
fourth = (u32) &appRls;
}
else if(iter ==6){
/* check for cert chain pointer error */
first = (u32) hash_data;
second = (u32) sig;
third = K0BASE;
appRls.carl.certChain[0] = (BbCertBase *)(K0BASE-4);
fourth = (u32) &appRls;
}
if(skVerifyHash((u32 *)first, (BbGenericSig *) second, (BbCertBase **)third,(BbAppLaunchCrls *)fourth)==expected){
#ifdef DEBUG
PRINTF("Pointer check verify PASS iter = %d\n", iter);
#endif
sprintf(outstring, "Verify ptr PASS %d", iter);
printstr(white, 3, 2, outstring);
osWritebackDCacheAll();
}
else{
#ifdef DEBUG
PRINTF("Pointer check verify FAIL iter = %d\n", iter);
#endif
sprintf(outstring, "Verify ptr FAIL %d", iter);
printstr(white, 3, 3, outstring);
osWritebackDCacheAll();
}
}
if(osBbFInit(&gFs)<0){
#ifdef DEBUG
PRINTF("ERROR osBbFInit()\n");
#endif
goto exit;
}
fsret = osBbFReadDir(gFsDirs, BB_SYSAPP_NUM_DIR_ENTRIES);
if(fsret<0){
#ifdef DEBUG
PRINTF("ERROR osBbFReadDir()\n");
#endif
goto exit;
}
for(i=0; i< STATE_SIZE; i++){
statedata[i] = (u8 )i;
}
SHA1Reset(&sha);
SHA1Input(&sha, (u8 *) statedata, STATE_SIZE);
SHA1Result(&sha, (u8 *)hash_data);
memcpy(hash_copy, hash_data, sizeof(BbShaHash));
for(iter = 0; iter < 10;iter++ ){
sprintf(outstring, "Iteration = %d", iter);
printstr(white, 3, 4, outstring);
osWritebackDCacheAll();
starttime = osGetCount();
hash_data[0] = hash_data[0] ^ iter;
if(skSignHash(hash_data, sig) == SK_API_SUCCESS){
#ifdef DEBUG
PRINTF("Signed data \n");
#endif
printstr(white, 3, 5, "Sign PASS");
osWritebackDCacheAll();
}
else{
#ifdef DEBUG
PRINTF("Signed data \n");
#endif
printstr(white, 3, 5, "After sign done, FAIL inside");
osWritebackDCacheAll();
}
endtime = osGetCount();
sprintf(outstring, "Time taken sign = %llu",OS_CYCLES_TO_USEC(endtime - starttime));
printstr(white, 3, 12, outstring);
osWritebackDCacheAll();
for(i=0; i< sizeof(BbEccSig)/sizeof(u32); i++){
signature.ecc[i] = sig[i];
sprintf(outstring, "%08x\n", (unsigned int )sig[i]);
#ifdef DEBUG
PRINTF(outstring);
#endif
}
/* check verify with cert chain */
if(iter == 0){
fd = osBbFOpen(bbChainFname,"r");
if (fd < 0){
#ifdef DEBUG
PRINTF("open failed");
#endif
}
fsret = osBbFStat(fd, &fsStat, NULL, 0);
#ifdef DEBUG
PRINTF("%s contains %d bytes\n",gIdFname,fsStat.size);
#endif
if(fsret !=0){
#ifdef DEBUG
PRINTF("stat error");
#endif
}
certHeap = (u8 *)gSerialHeap;
/* read entire cert file into serialized heap */
if(osBbFRead(fd, 0, certHeap, fsStat.size)<0){
osBbFClose(fd);
#ifdef DEBUG
PRINTF("reading into heap error\n");
#endif
}
if(osBbFClose(fd)<0){
#ifdef DEBUG
PRINTF("error closing file\n");
#endif
}
certChain[0] = certHeap + 4;
certChain[1] = ((u32) (certChain[0])) + sizeof(BbEccCert);
certChain[2] = ((u32) (certChain[1])) + sizeof(BbRsaCert);
memset((void *)&appRls,0,sizeof(BbAppLaunchCrls));
expected = SK_API_SUCCESS;
if(skVerifyHash(hash_data, &signature, certChain, &appRls) == expected){
#ifdef DEBUG
PRINTF("BB CERT TEST PASS iter = %d\n", iter);
#endif
sprintf(outstring, "Verify PASS %d", iter);
printstr(white, 3, 8, outstring);
osWritebackDCacheAll();
}
else{
#ifdef DEBUG
PRINTF("BB CERT TEST FAIL = %d\n", iter);
#endif
sprintf(outstring, "Verify FAIL %d", iter);
printstr(white, 3, 9, outstring);
osWritebackDCacheAll();
}
}
else{
starttime = osGetCount();
if(skVerifyHash(hash_data, &signature, 0x0, 0x0) == SK_API_SUCCESS){
#ifdef DEBUG
PRINTF("ECC sign verification succeeded\n");
#endif
sprintf(outstring, "Verify PASS %d", iter);
printstr(white, 3, 7, outstring);
osWritebackDCacheAll();
}
else{
#ifdef DEBUG
PRINTF("ECC sign verify failed\n");
#endif
sprintf(outstring, "After sign: FAIL %d", iter);
printstr(white, 3, 9, outstring);
osWritebackDCacheAll();
}
endtime = osGetCount();
sprintf(outstring, "Time taken verify = %llu",OS_CYCLES_TO_USEC(endtime - starttime));
printstr(white, 3, 14, outstring);
osWritebackDCacheAll();
}
}
/*
* load certs
*/
fd = osBbFOpen(gIdFname,"r");
if (fd < 0){
#ifdef DEBUG
PRINTF("open failed");
#endif
}
fsret = osBbFStat(fd, &fsStat, NULL, 0);
#ifdef DEBUG
PRINTF("%s contains %d bytes\n",gIdFname,fsStat.size);
#endif
if(fsret !=0){
#ifdef DEBUG
PRINTF("stat error");
#endif
}
certHeap = (u8 *)gSerialHeap;
/* read entire cert file into serialized heap */
if(osBbFRead(fd, 0, certHeap, fsStat.size)<0){
osBbFClose(fd);
#ifdef DEBUG
PRINTF("reading into heap error\n");
#endif
}
if(osBbFClose(fd)<0){
#ifdef DEBUG
PRINTF("error closing file\n");
#endif
}
/* initialize cert parser */
if(osBbSaCertInit(certHeap)!=BB_SYSAPP_PASS){
#ifdef DEBUG
PRINTF("error initialising parser\n");
#endif
}
if(osBbSaCertCreateChain("Root-XSCA00000002-XS00000001",
certChain)<0){
#ifdef DEBUG
PRINTF("saCertCreateChain() failure for cert chain.\n");
#endif
}
/*
* open sign file
*/
signfd = osBbFOpen(gSignFname, "r");
if(signfd < 0){
#ifdef DEBUG
PRINTF("open failed");
#endif
}
fsret = osBbFStat(signfd, &fsStat, NULL, 0);
if(osBbFRead(signfd, 0, gSigHeap, sizeof(BbRsaSig2048)*numiter)<0){
osBbFClose(signfd);
#ifdef DEBUG
PRINTF("reading into sign data error\n");
#endif
}
memcpy(hash_data, hash_copy, sizeof(BbShaHash));
for(iter=0; iter < numiter; iter++){
/* read part of sign file into serialized heap */
signdata = (u32 *)(gSigHeap + sizeof(BbRsaSig2048)*iter);
for(i=0; i< sizeof(BbRsaSig2048)/sizeof(u32); i++){
sign.rsa2048[i] = signdata[i];
}
/* do CRL tests simultaneously.
*/
if(iter ==0){
/* crla.sys revokes nothing being used */
fsret = osBbFRename("crla.sys", "crl.sys");
#ifdef DEBUG
if(fsret < 0){
PRINTF("Rename did not happen!\n");
}
#endif
readInCrl(&appRls);
/*reset virage */
skResetWindow();
skResetWindow();
expected = SK_API_SUCCESS;
}
else if(iter == 1){
/* check if it fails because internal number is upgraded and
* no crl is passed
*/
memset((void *)&appRls,0,sizeof(BbAppLaunchCrls));
expected = SK_API_FAIL;
}
if(iter ==2){
fsret = osBbFRename("crlb.sys", "crl.sys");
#ifdef DEBUG
if(fsret < 0){
PRINTF("Rename did not happen!\n");
}
#endif
readInCrl(&appRls);
/* crlb.sys revokes the ca */
/*reset virage */
skResetWindow();
skResetWindow();
expected = SK_API_REVOKED_SERVER;
}
else if (iter ==3){
/* crlc.sys revokes the XS */
osBbFRename("crlc.sys", "crl.sys");
readInCrl(&appRls);
/*reset virage */
skResetWindow();
skResetWindow();
expected = SK_API_REVOKED_SERVER;
}
else if (iter ==4){
/* crld.sys revokes the XS from a cpcrl */
osBbFRename("crld.sys", "crl.sys");
readInCrl(&appRls);
/*reset virage */
skResetWindow();
skResetWindow();
expected = SK_API_REVOKED_SERVER;
}
else if (iter ==5){
/* negative test */
skResetWindow();
skResetWindow();
memset((void *)&appRls,0,sizeof(BbAppLaunchCrls));
sign.rsa2048[0] = sign.rsa2048[0] ^ iter;
expected = SK_API_FAIL;
}
else{ /* no CRL */
skResetWindow();
skResetWindow();
memset((void *)&appRls,0,sizeof(BbAppLaunchCrls));
expected = SK_API_SUCCESS;
}
starttime = osGetCount();
ret = skVerifyHash(hash_data, &sign, certChain, &appRls);
if(ret == expected){
#ifdef DEBUG
PRINTF("RSA sign verify TEST PASS iter = %d\n", iter);
#endif
sprintf(outstring, "RSA PASS iter = %d\n", iter);
printstr(white, 3, 8, outstring);
osWritebackDCacheAll();
}
else{
#ifdef DEBUG
PRINTF("RSA sign verify TEST FAIL = %d\n", iter);
#endif
sprintf(outstring, "RSA FAIL iter = %d\n", iter);
printstr(white, 3, 9, outstring);
osWritebackDCacheAll();
}
endtime = osGetCount();
if(iter > 0){
skResetWindow();
skResetWindow();
}
if(iter ==0){
osBbFRename("crl.sys", "crla.sys");
}
else if(iter == 2){
osBbFRename("crl.sys", "crlb.sys");
}
else if(iter == 3){
osBbFRename("crl.sys", "crlc.sys");
}
else if(iter == 4){
osBbFRename("crl.sys", "crld.sys");
}
}
if(osBbFClose(signfd)<0){
#ifdef DEBUG
PRINTF("error closing file\n");
#endif
}
exit:
return;
}