Bourne shell portability fix.
[oweals/openssl.git] / fips-1.0 / fips_test_suite.c
1 /* ====================================================================
2  * Copyright (c) 2003 The OpenSSL Project.  All rights reserved.
3  *
4  *
5  * This command is intended as a test driver for the FIPS-140 testing
6  * lab performing FIPS-140 validation.  It demonstrates the use of the
7  * OpenSSL library ito perform a variety of common cryptographic
8  * functions.  A power-up self test is demonstrated by deliberately
9  * pointing to an invalid executable hash
10  *
11  * Contributed by Steve Marquess.
12  *
13  */
14 #include <stdio.h>
15 #include <assert.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <openssl/aes.h>
20 #include <openssl/des.h>
21 #include <openssl/rsa.h>
22 #include <openssl/dsa.h>
23 #include <openssl/hmac.h>
24 #include <openssl/err.h>
25 #include <openssl/fips.h>
26 #include <openssl/bn.h>
27 #include <openssl/rand.h>
28 #include <openssl/sha.h>
29
30
31 #ifndef OPENSSL_FIPS
32 int main(int argc, char *argv[])
33     {
34     printf("No FIPS support\n");
35     return(0);
36     }
37 #else
38
39 #include "fips_utl.h"
40
41 /* AES: encrypt and decrypt known plaintext, verify result matches original plaintext
42 */
43 static int FIPS_aes_test()
44     {
45     unsigned char userkey[16] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xf0, 0x0d };
46     unsigned char plaintext[16] = "etaonrishdlcu";
47     unsigned char ciphertext[16];
48     unsigned char buf[16];
49     AES_KEY key;
50     AES_KEY dkey;
51
52     ERR_clear_error();
53     if (AES_set_encrypt_key( userkey, 128, &key ))
54         return 0;
55     AES_encrypt( plaintext, ciphertext, &key);
56     if (AES_set_decrypt_key( userkey, 128, &dkey ))
57         return 0;
58     AES_decrypt( ciphertext, buf, &dkey);
59     if (memcmp(buf, plaintext, sizeof(buf)))
60         return 0;
61     return 1;
62     }
63
64 /* DES: encrypt and decrypt known plaintext, verify result matches original plaintext
65 */
66 static int FIPS_des_test()
67     {
68     DES_cblock userkey = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xf0, 0x0d };
69     DES_cblock plaintext = { 'e', 't', 'a', 'o', 'n', 'r', 'i', 's' };
70
71     DES_key_schedule key;
72     DES_cblock ciphertext;
73     DES_cblock buf;
74
75     ERR_clear_error();
76     if (DES_set_key(&userkey, &key) < 0)
77         return 0;
78     DES_ecb_encrypt( &plaintext, &ciphertext, &key, 1);
79     DES_ecb_encrypt( &ciphertext, &buf, &key, 0);
80     if (memcmp(buf, plaintext, sizeof(buf)))
81         return 0;
82     return 1;
83     }
84
85 /*
86  * DSA: generate keys and sign, verify input plaintext.
87  */
88 static int FIPS_dsa_test()
89     {
90     DSA *dsa = NULL;
91     EVP_PKEY pk;
92     unsigned char dgst[] = "etaonrishdlc";
93     unsigned char buf[60];
94     unsigned int slen;
95     int r = 0;
96     EVP_MD_CTX mctx;
97
98     ERR_clear_error();
99     EVP_MD_CTX_init(&mctx);
100     dsa = FIPS_dsa_new();
101     if (!dsa)
102         goto end;
103     if (!DSA_generate_parameters_ex(dsa, 512,NULL,0,NULL,NULL,NULL))
104         goto end;
105     if (!DSA_generate_key(dsa))
106         goto end;
107
108     pk.type = EVP_PKEY_DSA;
109     pk.pkey.dsa = dsa;
110
111     if (!EVP_SignInit_ex(&mctx, EVP_dss1(), NULL))
112         goto end;
113     if (!EVP_SignUpdate(&mctx, dgst, sizeof(dgst) - 1))
114         goto end;
115     if (!EVP_SignFinal(&mctx, buf, &slen, &pk))
116         goto end;
117
118     if (!EVP_VerifyInit_ex(&mctx, EVP_dss1(), NULL))
119         goto end;
120     if (!EVP_VerifyUpdate(&mctx, dgst, sizeof(dgst) - 1))
121         goto end;
122     r = EVP_VerifyFinal(&mctx, buf, slen, &pk);
123     end:
124     EVP_MD_CTX_cleanup(&mctx);
125     if (dsa)
126           FIPS_dsa_free(dsa);
127     if (r != 1)
128         return 0;
129     return 1;
130     }
131
132 /*
133  * RSA: generate keys and sign, verify input plaintext.
134  */
135 static int FIPS_rsa_test()
136     {
137     RSA *key;
138     unsigned char input_ptext[] = "etaonrishdlc";
139     unsigned char buf[256];
140     unsigned int slen;
141     BIGNUM *bn;
142     EVP_MD_CTX mctx;
143     EVP_PKEY pk;
144     int r;
145
146     ERR_clear_error();
147     EVP_MD_CTX_init(&mctx);
148     key = FIPS_rsa_new();
149     bn = BN_new();
150     if (!key || !bn)
151         return 0;
152     BN_set_word(bn, 65537);
153     if (!RSA_generate_key_ex(key, 1024,bn,NULL))
154         return 0;
155     BN_free(bn);
156
157     pk.type = EVP_PKEY_RSA;
158     pk.pkey.rsa = key;
159
160     if (!EVP_SignInit_ex(&mctx, EVP_sha1(), NULL))
161         goto end;
162     if (!EVP_SignUpdate(&mctx, input_ptext, sizeof(input_ptext) - 1))
163         goto end;
164     if (!EVP_SignFinal(&mctx, buf, &slen, &pk))
165         goto end;
166
167     if (!EVP_VerifyInit_ex(&mctx, EVP_sha1(), NULL))
168         goto end;
169     if (!EVP_VerifyUpdate(&mctx, input_ptext, sizeof(input_ptext) - 1))
170         goto end;
171     r = EVP_VerifyFinal(&mctx, buf, slen, &pk);
172     end:
173     EVP_MD_CTX_cleanup(&mctx);
174     if (key)
175           FIPS_rsa_free(key);
176     if (r != 1)
177         return 0;
178     return 1;
179     }
180
181 /* SHA1: generate hash of known digest value and compare to known
182    precomputed correct hash
183 */
184 static int FIPS_sha1_test()
185     {
186     unsigned char digest[SHA_DIGEST_LENGTH] =
187         { 0x11, 0xf1, 0x9a, 0x3a, 0xec, 0x1a, 0x1e, 0x8e, 0x65, 0xd4, 0x9a, 0x38, 0x0c, 0x8b, 0x1e, 0x2c, 0xe8, 0xb3, 0xc5, 0x18 };
188     unsigned char str[] = "etaonrishd";
189
190     unsigned char md[SHA_DIGEST_LENGTH];
191
192     ERR_clear_error();
193     if (!EVP_Digest(str,sizeof(str) - 1,md, NULL, EVP_sha1(), NULL)) return 0;
194     if (memcmp(md,digest,sizeof(md)))
195         return 0;
196     return 1;
197     }
198
199 /* SHA256: generate hash of known digest value and compare to known
200    precomputed correct hash
201 */
202 static int FIPS_sha256_test()
203     {
204     unsigned char digest[SHA256_DIGEST_LENGTH] =
205         {0xf5, 0x53, 0xcd, 0xb8, 0xcf, 0x1, 0xee, 0x17, 0x9b, 0x93, 0xc9, 0x68, 0xc0, 0xea, 0x40, 0x91,
206          0x6, 0xec, 0x8e, 0x11, 0x96, 0xc8, 0x5d, 0x1c, 0xaf, 0x64, 0x22, 0xe6, 0x50, 0x4f, 0x47, 0x57};
207     unsigned char str[] = "etaonrishd";
208
209     unsigned char md[SHA256_DIGEST_LENGTH];
210
211     ERR_clear_error();
212     if (!SHA256(str,sizeof(str) - 1,md)) return 0;
213     if (memcmp(md,digest,sizeof(md)))
214         return 0;
215     return 1;
216     }
217
218 /* SHA512: generate hash of known digest value and compare to known
219    precomputed correct hash
220 */
221 static int FIPS_sha512_test()
222     {
223     unsigned char digest[SHA512_DIGEST_LENGTH] =
224         {0x99, 0xc9, 0xe9, 0x5b, 0x88, 0xd4, 0x78, 0x88, 0xdf, 0x88, 0x5f, 0x94, 0x71, 0x64, 0x28, 0xca,
225          0x16, 0x1f, 0x3d, 0xf4, 0x1f, 0xf3, 0x0f, 0xc5, 0x03, 0x99, 0xb2, 0xd0, 0xe7, 0x0b, 0x94, 0x4a,
226          0x45, 0xd2, 0x6c, 0x4f, 0x20, 0x06, 0xef, 0x71, 0xa9, 0x25, 0x7f, 0x24, 0xb1, 0xd9, 0x40, 0x22,
227          0x49, 0x54, 0x10, 0xc2, 0x22, 0x9d, 0x27, 0xfe, 0xbd, 0xd6, 0xd6, 0xeb, 0x2d, 0x42, 0x1d, 0xa3};
228     unsigned char str[] = "etaonrishd";
229
230     unsigned char md[SHA512_DIGEST_LENGTH];
231
232     ERR_clear_error();
233     if (!SHA512(str,sizeof(str) - 1,md)) return 0;
234     if (memcmp(md,digest,sizeof(md)))
235         return 0;
236     return 1;
237     }
238
239 /* HMAC-SHA1: generate hash of known digest value and compare to known
240    precomputed correct hash
241 */
242 static int FIPS_hmac_sha1_test()
243     {
244     unsigned char key[] = "etaonrishd";
245     unsigned char iv[] = "Sample text";
246     unsigned char kaval[EVP_MAX_MD_SIZE] =
247         {0x73, 0xf7, 0xa0, 0x48, 0xf8, 0x94, 0xed, 0xdd, 0x0a, 0xea, 0xea, 0x56, 0x1b, 0x61, 0x2e, 0x70,
248          0xb2, 0xfb, 0xec, 0xc6};
249
250     unsigned char out[EVP_MAX_MD_SIZE];
251     unsigned int outlen;
252
253     ERR_clear_error();
254     if (!HMAC(EVP_sha1(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
255     if (memcmp(out,kaval,outlen))
256         return 0;
257     return 1;
258     }
259
260 /* HMAC-SHA224: generate hash of known digest value and compare to known
261    precomputed correct hash
262 */
263 static int FIPS_hmac_sha224_test()
264     {
265     unsigned char key[] = "etaonrishd";
266     unsigned char iv[] = "Sample text";
267     unsigned char kaval[EVP_MAX_MD_SIZE] =
268         {0x75, 0x58, 0xd5, 0xbd, 0x55, 0x6d, 0x87, 0x0f, 0x75, 0xff, 0xbe, 0x1c, 0xb2, 0xf0, 0x20, 0x35,
269          0xe5, 0x62, 0x49, 0xb6, 0x94, 0xb9, 0xfc, 0x65, 0x34, 0x33, 0x3a, 0x19};
270
271     unsigned char out[EVP_MAX_MD_SIZE];
272     unsigned int outlen;
273
274     ERR_clear_error();
275     if (!HMAC(EVP_sha224(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
276     if (memcmp(out,kaval,outlen))
277         return 0;
278     return 1;
279     }
280
281 /* HMAC-SHA256: generate hash of known digest value and compare to known
282    precomputed correct hash
283 */
284 static int FIPS_hmac_sha256_test()
285     {
286     unsigned char key[] = "etaonrishd";
287     unsigned char iv[] = "Sample text";
288     unsigned char kaval[EVP_MAX_MD_SIZE] =
289         {0xe9, 0x17, 0xc1, 0x7b, 0x4c, 0x6b, 0x77, 0xda, 0xd2, 0x30, 0x36, 0x02, 0xf5, 0x72, 0x33, 0x87,
290          0x9f, 0xc6, 0x6e, 0x7b, 0x7e, 0xa8, 0xea, 0xaa, 0x9f, 0xba, 0xee, 0x51, 0xff, 0xda, 0x24, 0xf4};
291
292     unsigned char out[EVP_MAX_MD_SIZE];
293     unsigned int outlen;
294
295     ERR_clear_error();
296     if (!HMAC(EVP_sha256(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
297     if (memcmp(out,kaval,outlen))
298         return 0;
299     return 1;
300     }
301
302 /* HMAC-SHA384: generate hash of known digest value and compare to known
303    precomputed correct hash
304 */
305 static int FIPS_hmac_sha384_test()
306     {
307     unsigned char key[] = "etaonrishd";
308     unsigned char iv[] = "Sample text";
309     unsigned char kaval[EVP_MAX_MD_SIZE] =
310         {0xb2, 0x9d, 0x40, 0x58, 0x32, 0xc4, 0xe3, 0x31, 0xb6, 0x63, 0x08, 0x26, 0x99, 0xef, 0x3b, 0x10,
311          0xe2, 0xdf, 0xf8, 0xff, 0xc6, 0xe1, 0x03, 0x29, 0x81, 0x2a, 0x1b, 0xac, 0xb0, 0x07, 0x39, 0x08,
312          0xf3, 0x91, 0x35, 0x11, 0x76, 0xd6, 0x4c, 0x20, 0xfb, 0x4d, 0xc3, 0xf3, 0xb8, 0x9b, 0x88, 0x1c};
313
314     unsigned char out[EVP_MAX_MD_SIZE];
315     unsigned int outlen;
316
317     ERR_clear_error();
318     if (!HMAC(EVP_sha384(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
319     if (memcmp(out,kaval,outlen))
320         return 0;
321     return 1;
322     }
323
324 /* HMAC-SHA512: generate hash of known digest value and compare to known
325    precomputed correct hash
326 */
327 static int FIPS_hmac_sha512_test()
328     {
329     unsigned char key[] = "etaonrishd";
330     unsigned char iv[] = "Sample text";
331     unsigned char kaval[EVP_MAX_MD_SIZE] =
332         {0xcd, 0x3e, 0xb9, 0x51, 0xb8, 0xbc, 0x7f, 0x9a, 0x23, 0xaf, 0xf3, 0x77, 0x59, 0x85, 0xa9, 0xe6,
333          0xf7, 0xd1, 0x51, 0x96, 0x17, 0xe0, 0x92, 0xd8, 0xa6, 0x3b, 0xc1, 0xad, 0x7e, 0x24, 0xca, 0xb1,
334          0xd7, 0x79, 0x0a, 0xa5, 0xea, 0x2c, 0x02, 0x58, 0x0b, 0xa6, 0x52, 0x6b, 0x61, 0x7f, 0xeb, 0x9c,
335          0x47, 0x86, 0x5d, 0x74, 0x2b, 0x88, 0xdf, 0xee, 0x46, 0x69, 0x96, 0x3d, 0xa6, 0xd9, 0x2a, 0x53};
336
337     unsigned char out[EVP_MAX_MD_SIZE];
338     unsigned int outlen;
339
340     ERR_clear_error();
341     if (!HMAC(EVP_sha512(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
342     if (memcmp(out,kaval,outlen))
343         return 0;
344     return 1;
345     }
346
347
348 /* DH: generate shared parameters
349 */
350 static int dh_test()
351     {
352     DH *dh;
353     ERR_clear_error();
354     dh = FIPS_dh_new();
355     if (!dh)
356         return 0;
357     if (!DH_generate_parameters_ex(dh, 256, 2, NULL))
358         return 0;
359     FIPS_dh_free(dh);
360     return 1;
361     }
362
363 /* Zeroize
364 */
365 static int Zeroize()
366     {
367     RSA *key;
368     BIGNUM *bn;
369     unsigned char userkey[16] = 
370         { 0x48, 0x50, 0xf0, 0xa3, 0x3a, 0xed, 0xd3, 0xaf, 0x6e, 0x47, 0x7f, 0x83, 0x02, 0xb1, 0x09, 0x68 };
371     int i, n;
372
373     key = FIPS_rsa_new();
374     bn = BN_new();
375     if (!key || !bn)
376         return 0;
377     BN_set_word(bn, 65537);
378     if (!RSA_generate_key_ex(key, 1024,bn,NULL))
379         return 0;
380     BN_free(bn);
381     
382     n = BN_num_bytes(key->d);
383     printf(" Generated %d byte RSA private key\n", n);
384     printf("\tBN key before overwriting:\n");
385     do_bn_print(stdout, key->d);
386     BN_rand(key->d,n*8,-1,0);
387     printf("\tBN key after overwriting:\n");
388     do_bn_print(stdout, key->d);
389
390     printf("\tchar buffer key before overwriting: \n\t\t");
391     for(i = 0; i < sizeof(userkey); i++) printf("%02x", userkey[i]);
392         printf("\n");
393     RAND_bytes(userkey, sizeof userkey);
394     printf("\tchar buffer key after overwriting: \n\t\t");
395     for(i = 0; i < sizeof(userkey); i++) printf("%02x", userkey[i]);
396         printf("\n");
397
398     return 1;
399     }
400
401 static int Error;
402 const char * Fail(const char *msg)
403     {
404     Error++;
405     return msg; 
406     }
407
408 int main(int argc,char **argv)
409     {
410
411     printf("\tFIPS-mode test application\n\n");
412
413     /* Load entropy from external file, if any */
414     RAND_load_file(".rnd", 1024);
415
416     if (argv[1]) {
417         /* Corrupted KAT tests */
418         if (!strcmp(argv[1], "aes")) {
419             FIPS_corrupt_aes();
420             printf("AES encryption/decryption with corrupted KAT...\n");
421         } else if (!strcmp(argv[1], "des")) {
422             FIPS_corrupt_des();
423             printf("DES-ECB encryption/decryption with corrupted KAT...\n");
424         } else if (!strcmp(argv[1], "dsa")) {
425             FIPS_corrupt_dsa();
426             printf("DSA key generation and signature validation with corrupted KAT...\n");
427         } else if (!strcmp(argv[1], "rsa")) {
428             FIPS_corrupt_rsa();
429             printf("RSA key generation and encryption/decryption with corrupted KAT...\n");
430         } else if (!strcmp(argv[1], "sha1")) {
431             FIPS_corrupt_sha1();
432             printf("SHA-1 hash with corrupted KAT...\n");
433         } else if (!strcmp(argv[1], "rng")) {
434             FIPS_corrupt_rng();
435             printf("RNG test with corrupted KAT...\n");
436         } else {
437             printf("Bad argument \"%s\"\n", argv[1]);
438             exit(1);
439         }
440         if (!FIPS_mode_set(1))
441             {
442             do_print_errors();
443             printf("Power-up self test failed\n");
444             exit(1);
445         }
446         printf("Power-up self test successful\n");
447         exit(0);
448     }
449
450     /* Non-Approved cryptographic operation
451     */
452     printf("1. Non-Approved cryptographic operation test...\n");
453     printf("\ta. Included algorithm (D-H)...");
454     printf( dh_test() ? "successful\n" :  Fail("FAILED!\n") );
455
456     /* Power-up self test
457     */
458     ERR_clear_error();
459     printf("2. Automatic power-up self test...");
460     if (!FIPS_mode_set(1))
461         {
462         do_print_errors();
463         printf(Fail("FAILED!\n"));
464         exit(1);
465         }
466     printf("successful\n");
467
468     /* AES encryption/decryption
469     */
470     printf("3. AES encryption/decryption...");
471     printf( FIPS_aes_test() ? "successful\n" :  Fail("FAILED!\n") );
472
473     /* RSA key generation and encryption/decryption
474     */
475     printf("4. RSA key generation and encryption/decryption...");
476     printf( FIPS_rsa_test() ? "successful\n" :  Fail("FAILED!\n") );
477
478     /* DES-CBC encryption/decryption
479     */
480     printf("5. DES-ECB encryption/decryption...");
481     printf( FIPS_des_test() ? "successful\n" :  Fail("FAILED!\n") );
482
483     /* DSA key generation and signature validation
484     */
485     printf("6. DSA key generation and signature validation...");
486     printf( FIPS_dsa_test() ? "successful\n" :  Fail("FAILED!\n") );
487
488     /* SHA-1 hash
489     */
490     printf("7a. SHA-1 hash...");
491     printf( FIPS_sha1_test() ? "successful\n" :  Fail("FAILED!\n") );
492
493     /* SHA-256 hash
494     */
495     printf("7b. SHA-256 hash...");
496     printf( FIPS_sha256_test() ? "successful\n" :  Fail("FAILED!\n") );
497
498     /* SHA-512 hash
499     */
500     printf("7c. SHA-512 hash...");
501     printf( FIPS_sha512_test() ? "successful\n" :  Fail("FAILED!\n") );
502
503     /* HMAC-SHA-1 hash
504     */
505     printf("7d. HMAC-SHA-1 hash...");
506     printf( FIPS_hmac_sha1_test() ? "successful\n" :  Fail("FAILED!\n") );
507
508     /* HMAC-SHA-224 hash
509     */
510     printf("7e. HMAC-SHA-224 hash...");
511     printf( FIPS_hmac_sha224_test() ? "successful\n" :  Fail("FAILED!\n") );
512
513     /* HMAC-SHA-256 hash
514     */
515     printf("7f. HMAC-SHA-256 hash...");
516     printf( FIPS_hmac_sha256_test() ? "successful\n" :  Fail("FAILED!\n") );
517
518     /* HMAC-SHA-384 hash
519     */
520     printf("7g. HMAC-SHA-384 hash...");
521     printf( FIPS_hmac_sha384_test() ? "successful\n" :  Fail("FAILED!\n") );
522
523     /* HMAC-SHA-512 hash
524     */
525     printf("7h. HMAC-SHA-512 hash...");
526     printf( FIPS_hmac_sha512_test() ? "successful\n" :  Fail("FAILED!\n") );
527
528     /* Non-Approved cryptographic operation
529     */
530     printf("8. Non-Approved cryptographic operation test...\n");
531     printf("\ta. Included algorithm (D-H)...");
532     printf( dh_test() ? "successful as expected\n"
533             : Fail("failed INCORRECTLY!\n") );
534
535     /* Zeroization
536     */
537     printf("9. Zero-ization...\n");
538     printf( Zeroize() ? "\tsuccessful as expected\n"
539             : Fail("\tfailed INCORRECTLY!\n") );
540
541     printf("\nAll tests completed with %d errors\n", Error);
542     return Error ? 1 : 0;
543     }
544
545 #endif