/
fipstest.c
2100 lines (1695 loc) · 88.7 KB
/
fipstest.c
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PKCS #11 FIPS Power-Up Self Test.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "softoken.h" /* Required for RC2-ECB, RC2-CBC, RC4, DES-ECB, */
/* DES-CBC, DES3-ECB, DES3-CBC, RSA */
/* and DSA. */
#include "seccomon.h" /* Required for RSA and DSA. */
#include "lowkeyi.h" /* Required for RSA and DSA. */
#include "pkcs11.h" /* Required for PKCS #11. */
#include "secerr.h"
#ifndef NSS_DISABLE_ECC
#include "ec.h" /* Required for ECDSA */
#endif
/* FIPS preprocessor directives for RC2-ECB and RC2-CBC. */
#define FIPS_RC2_KEY_LENGTH 5 /* 40-bits */
#define FIPS_RC2_ENCRYPT_LENGTH 8 /* 64-bits */
#define FIPS_RC2_DECRYPT_LENGTH 8 /* 64-bits */
/* FIPS preprocessor directives for RC4. */
#define FIPS_RC4_KEY_LENGTH 5 /* 40-bits */
#define FIPS_RC4_ENCRYPT_LENGTH 8 /* 64-bits */
#define FIPS_RC4_DECRYPT_LENGTH 8 /* 64-bits */
/* FIPS preprocessor directives for DES-ECB and DES-CBC. */
#define FIPS_DES_ENCRYPT_LENGTH 8 /* 64-bits */
#define FIPS_DES_DECRYPT_LENGTH 8 /* 64-bits */
/* FIPS preprocessor directives for DES3-CBC and DES3-ECB. */
#define FIPS_DES3_ENCRYPT_LENGTH 8 /* 64-bits */
#define FIPS_DES3_DECRYPT_LENGTH 8 /* 64-bits */
/* FIPS preprocessor directives for AES-ECB and AES-CBC. */
#define FIPS_AES_BLOCK_SIZE 16 /* 128-bits */
#define FIPS_AES_ENCRYPT_LENGTH 16 /* 128-bits */
#define FIPS_AES_DECRYPT_LENGTH 16 /* 128-bits */
#define FIPS_AES_128_KEY_SIZE 16 /* 128-bits */
#define FIPS_AES_192_KEY_SIZE 24 /* 192-bits */
#define FIPS_AES_256_KEY_SIZE 32 /* 256-bits */
/* FIPS preprocessor directives for message digests */
#define FIPS_KNOWN_HASH_MESSAGE_LENGTH 64 /* 512-bits */
/* FIPS preprocessor directives for RSA. */
#define FIPS_RSA_TYPE siBuffer
#define FIPS_RSA_PUBLIC_EXPONENT_LENGTH 3 /* 24-bits */
#define FIPS_RSA_PRIVATE_VERSION_LENGTH 1 /* 8-bits */
#define FIPS_RSA_MESSAGE_LENGTH 256 /* 2048-bits */
#define FIPS_RSA_COEFFICIENT_LENGTH 128 /* 1024-bits */
#define FIPS_RSA_PRIME0_LENGTH 128 /* 1024-bits */
#define FIPS_RSA_PRIME1_LENGTH 128 /* 1024-bits */
#define FIPS_RSA_EXPONENT0_LENGTH 128 /* 1024-bits */
#define FIPS_RSA_EXPONENT1_LENGTH 128 /* 1024-bits */
#define FIPS_RSA_PRIVATE_EXPONENT_LENGTH 256 /* 2048-bits */
#define FIPS_RSA_ENCRYPT_LENGTH 256 /* 2048-bits */
#define FIPS_RSA_DECRYPT_LENGTH 256 /* 2048-bits */
#define FIPS_RSA_SIGNATURE_LENGTH 256 /* 2048-bits */
#define FIPS_RSA_MODULUS_LENGTH 256 /* 2048-bits */
/* FIPS preprocessor directives for DSA. */
#define FIPS_DSA_TYPE siBuffer
#define FIPS_DSA_DIGEST_LENGTH 20 /* 160-bits */
#define FIPS_DSA_SUBPRIME_LENGTH 20 /* 160-bits */
#define FIPS_DSA_SIGNATURE_LENGTH 40 /* 320-bits */
#define FIPS_DSA_PRIME_LENGTH 128 /* 1024-bits */
#define FIPS_DSA_BASE_LENGTH 128 /* 1024-bits */
/* FIPS preprocessor directives for RNG. */
#define FIPS_RNG_XKEY_LENGTH 32 /* 256-bits */
static CK_RV
sftk_fips_RC2_PowerUpSelfTest( void )
{
/* RC2 Known Key (40-bits). */
static const PRUint8 rc2_known_key[] = { "RSARC" };
/* RC2-CBC Known Initialization Vector (64-bits). */
static const PRUint8 rc2_cbc_known_initialization_vector[] = {"Security"};
/* RC2 Known Plaintext (64-bits). */
static const PRUint8 rc2_ecb_known_plaintext[] = {"Netscape"};
static const PRUint8 rc2_cbc_known_plaintext[] = {"Netscape"};
/* RC2 Known Ciphertext (64-bits). */
static const PRUint8 rc2_ecb_known_ciphertext[] = {
0x1a,0x71,0x33,0x54,0x8d,0x5c,0xd2,0x30};
static const PRUint8 rc2_cbc_known_ciphertext[] = {
0xff,0x41,0xdb,0x94,0x8a,0x4c,0x33,0xb3};
/* RC2 variables. */
PRUint8 rc2_computed_ciphertext[FIPS_RC2_ENCRYPT_LENGTH];
PRUint8 rc2_computed_plaintext[FIPS_RC2_DECRYPT_LENGTH];
RC2Context * rc2_context;
unsigned int rc2_bytes_encrypted;
unsigned int rc2_bytes_decrypted;
SECStatus rc2_status;
/******************************************************/
/* RC2-ECB Single-Round Known Answer Encryption Test: */
/******************************************************/
rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH,
NULL, NSS_RC2,
FIPS_RC2_KEY_LENGTH );
if( rc2_context == NULL )
return( CKR_HOST_MEMORY );
rc2_status = RC2_Encrypt( rc2_context, rc2_computed_ciphertext,
&rc2_bytes_encrypted, FIPS_RC2_ENCRYPT_LENGTH,
rc2_ecb_known_plaintext,
FIPS_RC2_DECRYPT_LENGTH );
RC2_DestroyContext( rc2_context, PR_TRUE );
if( ( rc2_status != SECSuccess ) ||
( rc2_bytes_encrypted != FIPS_RC2_ENCRYPT_LENGTH ) ||
( PORT_Memcmp( rc2_computed_ciphertext, rc2_ecb_known_ciphertext,
FIPS_RC2_ENCRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/******************************************************/
/* RC2-ECB Single-Round Known Answer Decryption Test: */
/******************************************************/
rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH,
NULL, NSS_RC2,
FIPS_RC2_KEY_LENGTH );
if( rc2_context == NULL )
return( CKR_HOST_MEMORY );
rc2_status = RC2_Decrypt( rc2_context, rc2_computed_plaintext,
&rc2_bytes_decrypted, FIPS_RC2_DECRYPT_LENGTH,
rc2_ecb_known_ciphertext,
FIPS_RC2_ENCRYPT_LENGTH );
RC2_DestroyContext( rc2_context, PR_TRUE );
if( ( rc2_status != SECSuccess ) ||
( rc2_bytes_decrypted != FIPS_RC2_DECRYPT_LENGTH ) ||
( PORT_Memcmp( rc2_computed_plaintext, rc2_ecb_known_plaintext,
FIPS_RC2_DECRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/******************************************************/
/* RC2-CBC Single-Round Known Answer Encryption Test: */
/******************************************************/
rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH,
rc2_cbc_known_initialization_vector,
NSS_RC2_CBC, FIPS_RC2_KEY_LENGTH );
if( rc2_context == NULL )
return( CKR_HOST_MEMORY );
rc2_status = RC2_Encrypt( rc2_context, rc2_computed_ciphertext,
&rc2_bytes_encrypted, FIPS_RC2_ENCRYPT_LENGTH,
rc2_cbc_known_plaintext,
FIPS_RC2_DECRYPT_LENGTH );
RC2_DestroyContext( rc2_context, PR_TRUE );
if( ( rc2_status != SECSuccess ) ||
( rc2_bytes_encrypted != FIPS_RC2_ENCRYPT_LENGTH ) ||
( PORT_Memcmp( rc2_computed_ciphertext, rc2_cbc_known_ciphertext,
FIPS_RC2_ENCRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/******************************************************/
/* RC2-CBC Single-Round Known Answer Decryption Test: */
/******************************************************/
rc2_context = RC2_CreateContext( rc2_known_key, FIPS_RC2_KEY_LENGTH,
rc2_cbc_known_initialization_vector,
NSS_RC2_CBC, FIPS_RC2_KEY_LENGTH );
if( rc2_context == NULL )
return( CKR_HOST_MEMORY );
rc2_status = RC2_Decrypt( rc2_context, rc2_computed_plaintext,
&rc2_bytes_decrypted, FIPS_RC2_DECRYPT_LENGTH,
rc2_cbc_known_ciphertext,
FIPS_RC2_ENCRYPT_LENGTH );
RC2_DestroyContext( rc2_context, PR_TRUE );
if( ( rc2_status != SECSuccess ) ||
( rc2_bytes_decrypted != FIPS_RC2_DECRYPT_LENGTH ) ||
( PORT_Memcmp( rc2_computed_plaintext, rc2_ecb_known_plaintext,
FIPS_RC2_DECRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
return( CKR_OK );
}
static CK_RV
sftk_fips_RC4_PowerUpSelfTest( void )
{
/* RC4 Known Key (40-bits). */
static const PRUint8 rc4_known_key[] = { "RSARC" };
/* RC4 Known Plaintext (64-bits). */
static const PRUint8 rc4_known_plaintext[] = { "Netscape" };
/* RC4 Known Ciphertext (64-bits). */
static const PRUint8 rc4_known_ciphertext[] = {
0x29,0x33,0xc7,0x9a,0x9d,0x6c,0x09,0xdd};
/* RC4 variables. */
PRUint8 rc4_computed_ciphertext[FIPS_RC4_ENCRYPT_LENGTH];
PRUint8 rc4_computed_plaintext[FIPS_RC4_DECRYPT_LENGTH];
RC4Context * rc4_context;
unsigned int rc4_bytes_encrypted;
unsigned int rc4_bytes_decrypted;
SECStatus rc4_status;
/**************************************************/
/* RC4 Single-Round Known Answer Encryption Test: */
/**************************************************/
rc4_context = RC4_CreateContext( rc4_known_key, FIPS_RC4_KEY_LENGTH );
if( rc4_context == NULL )
return( CKR_HOST_MEMORY );
rc4_status = RC4_Encrypt( rc4_context, rc4_computed_ciphertext,
&rc4_bytes_encrypted, FIPS_RC4_ENCRYPT_LENGTH,
rc4_known_plaintext, FIPS_RC4_DECRYPT_LENGTH );
RC4_DestroyContext( rc4_context, PR_TRUE );
if( ( rc4_status != SECSuccess ) ||
( rc4_bytes_encrypted != FIPS_RC4_ENCRYPT_LENGTH ) ||
( PORT_Memcmp( rc4_computed_ciphertext, rc4_known_ciphertext,
FIPS_RC4_ENCRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/**************************************************/
/* RC4 Single-Round Known Answer Decryption Test: */
/**************************************************/
rc4_context = RC4_CreateContext( rc4_known_key, FIPS_RC4_KEY_LENGTH );
if( rc4_context == NULL )
return( CKR_HOST_MEMORY );
rc4_status = RC4_Decrypt( rc4_context, rc4_computed_plaintext,
&rc4_bytes_decrypted, FIPS_RC4_DECRYPT_LENGTH,
rc4_known_ciphertext, FIPS_RC4_ENCRYPT_LENGTH );
RC4_DestroyContext( rc4_context, PR_TRUE );
if( ( rc4_status != SECSuccess ) ||
( rc4_bytes_decrypted != FIPS_RC4_DECRYPT_LENGTH ) ||
( PORT_Memcmp( rc4_computed_plaintext, rc4_known_plaintext,
FIPS_RC4_DECRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
return( CKR_OK );
}
static CK_RV
sftk_fips_DES_PowerUpSelfTest( void )
{
/* DES Known Key (56-bits). */
static const PRUint8 des_known_key[] = { "ANSI DES" };
/* DES-CBC Known Initialization Vector (64-bits). */
static const PRUint8 des_cbc_known_initialization_vector[] = { "Security" };
/* DES Known Plaintext (64-bits). */
static const PRUint8 des_ecb_known_plaintext[] = { "Netscape" };
static const PRUint8 des_cbc_known_plaintext[] = { "Netscape" };
/* DES Known Ciphertext (64-bits). */
static const PRUint8 des_ecb_known_ciphertext[] = {
0x26,0x14,0xe9,0xc3,0x28,0x80,0x50,0xb0};
static const PRUint8 des_cbc_known_ciphertext[] = {
0x5e,0x95,0x94,0x5d,0x76,0xa2,0xd3,0x7d};
/* DES variables. */
PRUint8 des_computed_ciphertext[FIPS_DES_ENCRYPT_LENGTH];
PRUint8 des_computed_plaintext[FIPS_DES_DECRYPT_LENGTH];
DESContext * des_context;
unsigned int des_bytes_encrypted;
unsigned int des_bytes_decrypted;
SECStatus des_status;
/******************************************************/
/* DES-ECB Single-Round Known Answer Encryption Test: */
/******************************************************/
des_context = DES_CreateContext( des_known_key, NULL, NSS_DES, PR_TRUE );
if( des_context == NULL )
return( CKR_HOST_MEMORY );
des_status = DES_Encrypt( des_context, des_computed_ciphertext,
&des_bytes_encrypted, FIPS_DES_ENCRYPT_LENGTH,
des_ecb_known_plaintext,
FIPS_DES_DECRYPT_LENGTH );
DES_DestroyContext( des_context, PR_TRUE );
if( ( des_status != SECSuccess ) ||
( des_bytes_encrypted != FIPS_DES_ENCRYPT_LENGTH ) ||
( PORT_Memcmp( des_computed_ciphertext, des_ecb_known_ciphertext,
FIPS_DES_ENCRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/******************************************************/
/* DES-ECB Single-Round Known Answer Decryption Test: */
/******************************************************/
des_context = DES_CreateContext( des_known_key, NULL, NSS_DES, PR_FALSE );
if( des_context == NULL )
return( CKR_HOST_MEMORY );
des_status = DES_Decrypt( des_context, des_computed_plaintext,
&des_bytes_decrypted, FIPS_DES_DECRYPT_LENGTH,
des_ecb_known_ciphertext,
FIPS_DES_ENCRYPT_LENGTH );
DES_DestroyContext( des_context, PR_TRUE );
if( ( des_status != SECSuccess ) ||
( des_bytes_decrypted != FIPS_DES_DECRYPT_LENGTH ) ||
( PORT_Memcmp( des_computed_plaintext, des_ecb_known_plaintext,
FIPS_DES_DECRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/******************************************************/
/* DES-CBC Single-Round Known Answer Encryption Test. */
/******************************************************/
des_context = DES_CreateContext( des_known_key,
des_cbc_known_initialization_vector,
NSS_DES_CBC, PR_TRUE );
if( des_context == NULL )
return( CKR_HOST_MEMORY );
des_status = DES_Encrypt( des_context, des_computed_ciphertext,
&des_bytes_encrypted, FIPS_DES_ENCRYPT_LENGTH,
des_cbc_known_plaintext,
FIPS_DES_DECRYPT_LENGTH );
DES_DestroyContext( des_context, PR_TRUE );
if( ( des_status != SECSuccess ) ||
( des_bytes_encrypted != FIPS_DES_ENCRYPT_LENGTH ) ||
( PORT_Memcmp( des_computed_ciphertext, des_cbc_known_ciphertext,
FIPS_DES_ENCRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/******************************************************/
/* DES-CBC Single-Round Known Answer Decryption Test. */
/******************************************************/
des_context = DES_CreateContext( des_known_key,
des_cbc_known_initialization_vector,
NSS_DES_CBC, PR_FALSE );
if( des_context == NULL )
return( CKR_HOST_MEMORY );
des_status = DES_Decrypt( des_context, des_computed_plaintext,
&des_bytes_decrypted, FIPS_DES_DECRYPT_LENGTH,
des_cbc_known_ciphertext,
FIPS_DES_ENCRYPT_LENGTH );
DES_DestroyContext( des_context, PR_TRUE );
if( ( des_status != SECSuccess ) ||
( des_bytes_decrypted != FIPS_DES_DECRYPT_LENGTH ) ||
( PORT_Memcmp( des_computed_plaintext, des_cbc_known_plaintext,
FIPS_DES_DECRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
return( CKR_OK );
}
static CK_RV
sftk_fips_DES3_PowerUpSelfTest( void )
{
/* DES3 Known Key (56-bits). */
static const PRUint8 des3_known_key[] = { "ANSI Triple-DES Key Data" };
/* DES3-CBC Known Initialization Vector (64-bits). */
static const PRUint8 des3_cbc_known_initialization_vector[] = { "Security" };
/* DES3 Known Plaintext (64-bits). */
static const PRUint8 des3_ecb_known_plaintext[] = { "Netscape" };
static const PRUint8 des3_cbc_known_plaintext[] = { "Netscape" };
/* DES3 Known Ciphertext (64-bits). */
static const PRUint8 des3_ecb_known_ciphertext[] = {
0x55,0x8e,0xad,0x3c,0xee,0x49,0x69,0xbe};
static const PRUint8 des3_cbc_known_ciphertext[] = {
0x43,0xdc,0x6a,0xc1,0xaf,0xa6,0x32,0xf5};
/* DES3 variables. */
PRUint8 des3_computed_ciphertext[FIPS_DES3_ENCRYPT_LENGTH];
PRUint8 des3_computed_plaintext[FIPS_DES3_DECRYPT_LENGTH];
DESContext * des3_context;
unsigned int des3_bytes_encrypted;
unsigned int des3_bytes_decrypted;
SECStatus des3_status;
/*******************************************************/
/* DES3-ECB Single-Round Known Answer Encryption Test. */
/*******************************************************/
des3_context = DES_CreateContext( des3_known_key, NULL,
NSS_DES_EDE3, PR_TRUE );
if( des3_context == NULL )
return( CKR_HOST_MEMORY );
des3_status = DES_Encrypt( des3_context, des3_computed_ciphertext,
&des3_bytes_encrypted, FIPS_DES3_ENCRYPT_LENGTH,
des3_ecb_known_plaintext,
FIPS_DES3_DECRYPT_LENGTH );
DES_DestroyContext( des3_context, PR_TRUE );
if( ( des3_status != SECSuccess ) ||
( des3_bytes_encrypted != FIPS_DES3_ENCRYPT_LENGTH ) ||
( PORT_Memcmp( des3_computed_ciphertext, des3_ecb_known_ciphertext,
FIPS_DES3_ENCRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/*******************************************************/
/* DES3-ECB Single-Round Known Answer Decryption Test. */
/*******************************************************/
des3_context = DES_CreateContext( des3_known_key, NULL,
NSS_DES_EDE3, PR_FALSE );
if( des3_context == NULL )
return( CKR_HOST_MEMORY );
des3_status = DES_Decrypt( des3_context, des3_computed_plaintext,
&des3_bytes_decrypted, FIPS_DES3_DECRYPT_LENGTH,
des3_ecb_known_ciphertext,
FIPS_DES3_ENCRYPT_LENGTH );
DES_DestroyContext( des3_context, PR_TRUE );
if( ( des3_status != SECSuccess ) ||
( des3_bytes_decrypted != FIPS_DES3_DECRYPT_LENGTH ) ||
( PORT_Memcmp( des3_computed_plaintext, des3_ecb_known_plaintext,
FIPS_DES3_DECRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/*******************************************************/
/* DES3-CBC Single-Round Known Answer Encryption Test. */
/*******************************************************/
des3_context = DES_CreateContext( des3_known_key,
des3_cbc_known_initialization_vector,
NSS_DES_EDE3_CBC, PR_TRUE );
if( des3_context == NULL )
return( CKR_HOST_MEMORY );
des3_status = DES_Encrypt( des3_context, des3_computed_ciphertext,
&des3_bytes_encrypted, FIPS_DES3_ENCRYPT_LENGTH,
des3_cbc_known_plaintext,
FIPS_DES3_DECRYPT_LENGTH );
DES_DestroyContext( des3_context, PR_TRUE );
if( ( des3_status != SECSuccess ) ||
( des3_bytes_encrypted != FIPS_DES3_ENCRYPT_LENGTH ) ||
( PORT_Memcmp( des3_computed_ciphertext, des3_cbc_known_ciphertext,
FIPS_DES3_ENCRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/*******************************************************/
/* DES3-CBC Single-Round Known Answer Decryption Test. */
/*******************************************************/
des3_context = DES_CreateContext( des3_known_key,
des3_cbc_known_initialization_vector,
NSS_DES_EDE3_CBC, PR_FALSE );
if( des3_context == NULL )
return( CKR_HOST_MEMORY );
des3_status = DES_Decrypt( des3_context, des3_computed_plaintext,
&des3_bytes_decrypted, FIPS_DES3_DECRYPT_LENGTH,
des3_cbc_known_ciphertext,
FIPS_DES3_ENCRYPT_LENGTH );
DES_DestroyContext( des3_context, PR_TRUE );
if( ( des3_status != SECSuccess ) ||
( des3_bytes_decrypted != FIPS_DES3_DECRYPT_LENGTH ) ||
( PORT_Memcmp( des3_computed_plaintext, des3_cbc_known_plaintext,
FIPS_DES3_DECRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
return( CKR_OK );
}
/* AES self-test for 128-bit, 192-bit, or 256-bit key sizes*/
static CK_RV
sftk_fips_AES_PowerUpSelfTest( int aes_key_size )
{
/* AES Known Key (up to 256-bits). */
static const PRUint8 aes_known_key[] =
{ "AES-128 RIJNDAELLEADNJIR 821-SEA" };
/* AES-CBC Known Initialization Vector (128-bits). */
static const PRUint8 aes_cbc_known_initialization_vector[] =
{ "SecurityytiruceS" };
/* AES Known Plaintext (128-bits). (blocksize is 128-bits) */
static const PRUint8 aes_known_plaintext[] = { "NetscapeepacsteN" };
/* AES Known Ciphertext (128-bit key). */
static const PRUint8 aes_ecb128_known_ciphertext[] = {
0x3c,0xa5,0x96,0xf3,0x34,0x6a,0x96,0xc1,
0x03,0x88,0x16,0x7b,0x20,0xbf,0x35,0x47 };
static const PRUint8 aes_cbc128_known_ciphertext[] = {
0xcf,0x15,0x1d,0x4f,0x96,0xe4,0x4f,0x63,
0x15,0x54,0x14,0x1d,0x4e,0xd8,0xd5,0xea };
/* AES Known Ciphertext (192-bit key). */
static const PRUint8 aes_ecb192_known_ciphertext[] = {
0xa0,0x18,0x62,0xed,0x88,0x19,0xcb,0x62,
0x88,0x1d,0x4d,0xfe,0x84,0x02,0x89,0x0e };
static const PRUint8 aes_cbc192_known_ciphertext[] = {
0x83,0xf7,0xa4,0x76,0xd1,0x6f,0x07,0xbe,
0x07,0xbc,0x43,0x2f,0x6d,0xad,0x29,0xe1 };
/* AES Known Ciphertext (256-bit key). */
static const PRUint8 aes_ecb256_known_ciphertext[] = {
0xdb,0xa6,0x52,0x01,0x8a,0x70,0xae,0x66,
0x3a,0x99,0xd8,0x95,0x7f,0xfb,0x01,0x67 };
static const PRUint8 aes_cbc256_known_ciphertext[] = {
0x37,0xea,0x07,0x06,0x31,0x1c,0x59,0x27,
0xc5,0xc5,0x68,0x71,0x6e,0x34,0x40,0x16 };
const PRUint8 *aes_ecb_known_ciphertext =
( aes_key_size == FIPS_AES_128_KEY_SIZE) ? aes_ecb128_known_ciphertext :
( aes_key_size == FIPS_AES_192_KEY_SIZE) ? aes_ecb192_known_ciphertext :
aes_ecb256_known_ciphertext;
const PRUint8 *aes_cbc_known_ciphertext =
( aes_key_size == FIPS_AES_128_KEY_SIZE) ? aes_cbc128_known_ciphertext :
( aes_key_size == FIPS_AES_192_KEY_SIZE) ? aes_cbc192_known_ciphertext :
aes_cbc256_known_ciphertext;
/* AES variables. */
PRUint8 aes_computed_ciphertext[FIPS_AES_ENCRYPT_LENGTH];
PRUint8 aes_computed_plaintext[FIPS_AES_DECRYPT_LENGTH];
AESContext * aes_context;
unsigned int aes_bytes_encrypted;
unsigned int aes_bytes_decrypted;
SECStatus aes_status;
/*check if aes_key_size is 128, 192, or 256 bits */
if ((aes_key_size != FIPS_AES_128_KEY_SIZE) &&
(aes_key_size != FIPS_AES_192_KEY_SIZE) &&
(aes_key_size != FIPS_AES_256_KEY_SIZE))
return( CKR_DEVICE_ERROR );
/******************************************************/
/* AES-ECB Single-Round Known Answer Encryption Test: */
/******************************************************/
aes_context = AES_CreateContext( aes_known_key, NULL, NSS_AES, PR_TRUE,
aes_key_size, FIPS_AES_BLOCK_SIZE );
if( aes_context == NULL )
return( CKR_HOST_MEMORY );
aes_status = AES_Encrypt( aes_context, aes_computed_ciphertext,
&aes_bytes_encrypted, FIPS_AES_ENCRYPT_LENGTH,
aes_known_plaintext,
FIPS_AES_DECRYPT_LENGTH );
AES_DestroyContext( aes_context, PR_TRUE );
if( ( aes_status != SECSuccess ) ||
( aes_bytes_encrypted != FIPS_AES_ENCRYPT_LENGTH ) ||
( PORT_Memcmp( aes_computed_ciphertext, aes_ecb_known_ciphertext,
FIPS_AES_ENCRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/******************************************************/
/* AES-ECB Single-Round Known Answer Decryption Test: */
/******************************************************/
aes_context = AES_CreateContext( aes_known_key, NULL, NSS_AES, PR_FALSE,
aes_key_size, FIPS_AES_BLOCK_SIZE );
if( aes_context == NULL )
return( CKR_HOST_MEMORY );
aes_status = AES_Decrypt( aes_context, aes_computed_plaintext,
&aes_bytes_decrypted, FIPS_AES_DECRYPT_LENGTH,
aes_ecb_known_ciphertext,
FIPS_AES_ENCRYPT_LENGTH );
AES_DestroyContext( aes_context, PR_TRUE );
if( ( aes_status != SECSuccess ) ||
( aes_bytes_decrypted != FIPS_AES_DECRYPT_LENGTH ) ||
( PORT_Memcmp( aes_computed_plaintext, aes_known_plaintext,
FIPS_AES_DECRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/******************************************************/
/* AES-CBC Single-Round Known Answer Encryption Test. */
/******************************************************/
aes_context = AES_CreateContext( aes_known_key,
aes_cbc_known_initialization_vector,
NSS_AES_CBC, PR_TRUE, aes_key_size,
FIPS_AES_BLOCK_SIZE );
if( aes_context == NULL )
return( CKR_HOST_MEMORY );
aes_status = AES_Encrypt( aes_context, aes_computed_ciphertext,
&aes_bytes_encrypted, FIPS_AES_ENCRYPT_LENGTH,
aes_known_plaintext,
FIPS_AES_DECRYPT_LENGTH );
AES_DestroyContext( aes_context, PR_TRUE );
if( ( aes_status != SECSuccess ) ||
( aes_bytes_encrypted != FIPS_AES_ENCRYPT_LENGTH ) ||
( PORT_Memcmp( aes_computed_ciphertext, aes_cbc_known_ciphertext,
FIPS_AES_ENCRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/******************************************************/
/* AES-CBC Single-Round Known Answer Decryption Test. */
/******************************************************/
aes_context = AES_CreateContext( aes_known_key,
aes_cbc_known_initialization_vector,
NSS_AES_CBC, PR_FALSE, aes_key_size,
FIPS_AES_BLOCK_SIZE );
if( aes_context == NULL )
return( CKR_HOST_MEMORY );
aes_status = AES_Decrypt( aes_context, aes_computed_plaintext,
&aes_bytes_decrypted, FIPS_AES_DECRYPT_LENGTH,
aes_cbc_known_ciphertext,
FIPS_AES_ENCRYPT_LENGTH );
AES_DestroyContext( aes_context, PR_TRUE );
if( ( aes_status != SECSuccess ) ||
( aes_bytes_decrypted != FIPS_AES_DECRYPT_LENGTH ) ||
( PORT_Memcmp( aes_computed_plaintext, aes_known_plaintext,
FIPS_AES_DECRYPT_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
return( CKR_OK );
}
/* Known Hash Message (512-bits). Used for all hashes (incl. SHA-N [N>1]). */
static const PRUint8 known_hash_message[] = {
"The test message for the MD2, MD5, and SHA-1 hashing algorithms." };
static CK_RV
sftk_fips_MD2_PowerUpSelfTest( void )
{
/* MD2 Known Digest Message (128-bits). */
static const PRUint8 md2_known_digest[] = {
0x41,0x5a,0x12,0xb2,0x3f,0x28,0x97,0x17,
0x0c,0x71,0x4e,0xcc,0x40,0xc8,0x1d,0x1b};
/* MD2 variables. */
MD2Context * md2_context;
unsigned int md2_bytes_hashed;
PRUint8 md2_computed_digest[MD2_LENGTH];
/***********************************************/
/* MD2 Single-Round Known Answer Hashing Test. */
/***********************************************/
md2_context = MD2_NewContext();
if( md2_context == NULL )
return( CKR_HOST_MEMORY );
MD2_Begin( md2_context );
MD2_Update( md2_context, known_hash_message,
FIPS_KNOWN_HASH_MESSAGE_LENGTH );
MD2_End( md2_context, md2_computed_digest, &md2_bytes_hashed, MD2_LENGTH );
MD2_DestroyContext( md2_context , PR_TRUE );
if( ( md2_bytes_hashed != MD2_LENGTH ) ||
( PORT_Memcmp( md2_computed_digest, md2_known_digest,
MD2_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
return( CKR_OK );
}
static CK_RV
sftk_fips_MD5_PowerUpSelfTest( void )
{
/* MD5 Known Digest Message (128-bits). */
static const PRUint8 md5_known_digest[] = {
0x25,0xc8,0xc0,0x10,0xc5,0x6e,0x68,0x28,
0x28,0xa4,0xa5,0xd2,0x98,0x9a,0xea,0x2d};
/* MD5 variables. */
PRUint8 md5_computed_digest[MD5_LENGTH];
SECStatus md5_status;
/***********************************************/
/* MD5 Single-Round Known Answer Hashing Test. */
/***********************************************/
md5_status = MD5_HashBuf( md5_computed_digest, known_hash_message,
FIPS_KNOWN_HASH_MESSAGE_LENGTH );
if( ( md5_status != SECSuccess ) ||
( PORT_Memcmp( md5_computed_digest, md5_known_digest,
MD5_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
return( CKR_OK );
}
/****************************************************/
/* Single Round HMAC SHA-X test */
/****************************************************/
static SECStatus
sftk_fips_HMAC(unsigned char *hmac_computed,
const PRUint8 *secret_key,
unsigned int secret_key_length,
const PRUint8 *message,
unsigned int message_length,
HASH_HashType hashAlg )
{
SECStatus hmac_status = SECFailure;
HMACContext *cx = NULL;
SECHashObject *hashObj = NULL;
unsigned int bytes_hashed = 0;
hashObj = (SECHashObject *) HASH_GetRawHashObject(hashAlg);
if (!hashObj)
return( SECFailure );
cx = HMAC_Create(hashObj, secret_key,
secret_key_length,
PR_TRUE); /* PR_TRUE for in FIPS mode */
if (cx == NULL)
return( SECFailure );
HMAC_Begin(cx);
HMAC_Update(cx, message, message_length);
hmac_status = HMAC_Finish(cx, hmac_computed, &bytes_hashed,
hashObj->length);
HMAC_Destroy(cx, PR_TRUE);
return( hmac_status );
}
static CK_RV
sftk_fips_HMAC_PowerUpSelfTest( void )
{
static const PRUint8 HMAC_known_secret_key[] = {
"Firefox and ThunderBird are awesome!"};
static const PRUint8 HMAC_known_secret_key_length
= sizeof HMAC_known_secret_key;
/* known SHA1 hmac (20 bytes) */
static const PRUint8 known_SHA1_hmac[] = {
0xd5, 0x85, 0xf6, 0x5b, 0x39, 0xfa, 0xb9, 0x05,
0x3b, 0x57, 0x1d, 0x61, 0xe7, 0xb8, 0x84, 0x1e,
0x5d, 0x0e, 0x1e, 0x11};
/* known SHA224 hmac (28 bytes) */
static const PRUint8 known_SHA224_hmac[] = {
0x1c, 0xc3, 0x06, 0x8e, 0xce, 0x37, 0x68, 0xfb,
0x1a, 0x82, 0x4a, 0xbe, 0x2b, 0x00, 0x51, 0xf8,
0x9d, 0xb6, 0xe0, 0x90, 0x0d, 0x00, 0xc9, 0x64,
0x9a, 0xb8, 0x98, 0x4e};
/* known SHA256 hmac (32 bytes) */
static const PRUint8 known_SHA256_hmac[] = {
0x05, 0x75, 0x9a, 0x9e, 0x70, 0x5e, 0xe7, 0x44,
0xe2, 0x46, 0x4b, 0x92, 0x22, 0x14, 0x22, 0xe0,
0x1b, 0x92, 0x8a, 0x0c, 0xfe, 0xf5, 0x49, 0xe9,
0xa7, 0x1b, 0x56, 0x7d, 0x1d, 0x29, 0x40, 0x48};
/* known SHA384 hmac (48 bytes) */
static const PRUint8 known_SHA384_hmac[] = {
0xcd, 0x56, 0x14, 0xec, 0x05, 0x53, 0x06, 0x2b,
0x7e, 0x9c, 0x8a, 0x18, 0x5e, 0xea, 0xf3, 0x91,
0x33, 0xfb, 0x64, 0xf6, 0xe3, 0x9f, 0x89, 0x0b,
0xaf, 0xbe, 0x83, 0x4d, 0x3f, 0x3c, 0x43, 0x4d,
0x4a, 0x0c, 0x56, 0x98, 0xf8, 0xca, 0xb4, 0xaa,
0x9a, 0xf4, 0x0a, 0xaf, 0x4f, 0x69, 0xca, 0x87};
/* known SHA512 hmac (64 bytes) */
static const PRUint8 known_SHA512_hmac[] = {
0xf6, 0x0e, 0x97, 0x12, 0x00, 0x67, 0x6e, 0xb9,
0x0c, 0xb2, 0x63, 0xf0, 0x60, 0xac, 0x75, 0x62,
0x70, 0x95, 0x2a, 0x52, 0x22, 0xee, 0xdd, 0xd2,
0x71, 0xb1, 0xe8, 0x26, 0x33, 0xd3, 0x13, 0x27,
0xcb, 0xff, 0x44, 0xef, 0x87, 0x97, 0x16, 0xfb,
0xd3, 0x0b, 0x48, 0xbe, 0x12, 0x4e, 0xda, 0xb1,
0x89, 0x90, 0xfb, 0x06, 0x0c, 0xbe, 0xe5, 0xc4,
0xff, 0x24, 0x37, 0x3d, 0xc7, 0xe4, 0xe4, 0x37};
SECStatus hmac_status;
PRUint8 hmac_computed[HASH_LENGTH_MAX];
/***************************************************/
/* HMAC SHA-1 Single-Round Known Answer HMAC Test. */
/***************************************************/
hmac_status = sftk_fips_HMAC(hmac_computed,
HMAC_known_secret_key,
HMAC_known_secret_key_length,
known_hash_message,
FIPS_KNOWN_HASH_MESSAGE_LENGTH,
HASH_AlgSHA1);
if( ( hmac_status != SECSuccess ) ||
( PORT_Memcmp( hmac_computed, known_SHA1_hmac,
SHA1_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/***************************************************/
/* HMAC SHA-224 Single-Round Known Answer Test. */
/***************************************************/
hmac_status = sftk_fips_HMAC(hmac_computed,
HMAC_known_secret_key,
HMAC_known_secret_key_length,
known_hash_message,
FIPS_KNOWN_HASH_MESSAGE_LENGTH,
HASH_AlgSHA224);
if( ( hmac_status != SECSuccess ) ||
( PORT_Memcmp( hmac_computed, known_SHA224_hmac,
SHA224_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/***************************************************/
/* HMAC SHA-256 Single-Round Known Answer Test. */
/***************************************************/
hmac_status = sftk_fips_HMAC(hmac_computed,
HMAC_known_secret_key,
HMAC_known_secret_key_length,
known_hash_message,
FIPS_KNOWN_HASH_MESSAGE_LENGTH,
HASH_AlgSHA256);
if( ( hmac_status != SECSuccess ) ||
( PORT_Memcmp( hmac_computed, known_SHA256_hmac,
SHA256_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/***************************************************/
/* HMAC SHA-384 Single-Round Known Answer Test. */
/***************************************************/
hmac_status = sftk_fips_HMAC(hmac_computed,
HMAC_known_secret_key,
HMAC_known_secret_key_length,
known_hash_message,
FIPS_KNOWN_HASH_MESSAGE_LENGTH,
HASH_AlgSHA384);
if( ( hmac_status != SECSuccess ) ||
( PORT_Memcmp( hmac_computed, known_SHA384_hmac,
SHA384_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
/***************************************************/
/* HMAC SHA-512 Single-Round Known Answer Test. */
/***************************************************/
hmac_status = sftk_fips_HMAC(hmac_computed,
HMAC_known_secret_key,
HMAC_known_secret_key_length,
known_hash_message,
FIPS_KNOWN_HASH_MESSAGE_LENGTH,
HASH_AlgSHA512);
if( ( hmac_status != SECSuccess ) ||
( PORT_Memcmp( hmac_computed, known_SHA512_hmac,
SHA512_LENGTH ) != 0 ) )
return( CKR_DEVICE_ERROR );
return( CKR_OK );
}
static CK_RV
sftk_fips_SHA_PowerUpSelfTest( void )
{
/* SHA-1 Known Digest Message (160-bits). */
static const PRUint8 sha1_known_digest[] = {
0x0a,0x6d,0x07,0xba,0x1e,0xbd,0x8a,0x1b,
0x72,0xf6,0xc7,0x22,0xf1,0x27,0x9f,0xf0,
0xe0,0x68,0x47,0x7a};
/* SHA-224 Known Digest Message (224-bits). */
static const PRUint8 sha224_known_digest[] = {
0x89,0x5e,0x7f,0xfd,0x0e,0xd8,0x35,0x6f,
0x64,0x6d,0xf2,0xde,0x5e,0xed,0xa6,0x7f,
0x29,0xd1,0x12,0x73,0x42,0x84,0x95,0x4f,
0x8e,0x08,0xe5,0xcb};
/* SHA-256 Known Digest Message (256-bits). */
static const PRUint8 sha256_known_digest[] = {
0x38,0xa9,0xc1,0xf0,0x35,0xf6,0x5d,0x61,
0x11,0xd4,0x0b,0xdc,0xce,0x35,0x14,0x8d,
0xf2,0xdd,0xaf,0xaf,0xcf,0xb7,0x87,0xe9,
0x96,0xa5,0xd2,0x83,0x62,0x46,0x56,0x79};
/* SHA-384 Known Digest Message (384-bits). */
static const PRUint8 sha384_known_digest[] = {
0x11,0xfe,0x1c,0x00,0x89,0x48,0xde,0xb3,
0x99,0xee,0x1c,0x18,0xb4,0x10,0xfb,0xfe,
0xe3,0xa8,0x2c,0xf3,0x04,0xb0,0x2f,0xc8,
0xa3,0xc4,0x5e,0xea,0x7e,0x60,0x48,0x7b,
0xce,0x2c,0x62,0xf7,0xbc,0xa7,0xe8,0xa3,
0xcf,0x24,0xce,0x9c,0xe2,0x8b,0x09,0x72};
/* SHA-512 Known Digest Message (512-bits). */
static const PRUint8 sha512_known_digest[] = {
0xc8,0xb3,0x27,0xf9,0x0b,0x24,0xc8,0xbf,
0x4c,0xba,0x33,0x54,0xf2,0x31,0xbf,0xdb,
0xab,0xfd,0xb3,0x15,0xd7,0xfa,0x48,0x99,
0x07,0x60,0x0f,0x57,0x41,0x1a,0xdd,0x28,
0x12,0x55,0x25,0xac,0xba,0x3a,0x99,0x12,
0x2c,0x7a,0x8f,0x75,0x3a,0xe1,0x06,0x6f,
0x30,0x31,0xc9,0x33,0xc6,0x1b,0x90,0x1a,
0x6c,0x98,0x9a,0x87,0xd0,0xb2,0xf8,0x07};
/* SHA-X variables. */
PRUint8 sha_computed_digest[HASH_LENGTH_MAX];
SECStatus sha_status;