Import OpenSSL-0.9.8i.
[dragonfly.git] / crypto / openssl-0.9.7e / fips / fips_test_suite.c
blob60ee8d856b3054ed78c21d12128bfede4b2e9443
1 /* ====================================================================
2 * Copyright (c) 2003 The OpenSSL Project. All rights reserved.
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
11 * Contributed by Steve Marquess.
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/sha.h>
24 #include <openssl/md5.h>
25 #include <openssl/err.h>
26 #include <openssl/fips.h>
27 #include <openssl/bn.h>
28 #include <openssl/rand.h>
29 #ifndef OPENSSL_FIPS
30 int main(int argc, char *argv[])
32 printf("No FIPS support\n");
33 return(0);
35 #else
37 /* AES: encrypt and decrypt known plaintext, verify result matches original plaintext
39 static int FIPS_aes_test()
41 unsigned char userkey[16] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xf0, 0x0d };
42 unsigned char plaintext[16] = "etaonrishdlcu";
43 unsigned char ciphertext[16];
44 unsigned char buf[16];
45 AES_KEY key;
46 AES_KEY dkey;
48 ERR_clear_error();
49 if (AES_set_encrypt_key( userkey, 128, &key ))
50 return 0;
51 AES_encrypt( plaintext, ciphertext, &key);
52 if (AES_set_decrypt_key( userkey, 128, &dkey ))
53 return 0;
54 AES_decrypt( ciphertext, buf, &dkey);
55 if (memcmp(buf, plaintext, sizeof(buf)))
56 return 0;
57 return 1;
60 /* DES: encrypt and decrypt known plaintext, verify result matches original plaintext
62 static int FIPS_des_test()
64 DES_cblock userkey = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xf0, 0x0d };
65 DES_cblock plaintext = { 'e', 't', 'a', 'o', 'n', 'r', 'i', 's' };
67 DES_key_schedule key;
68 DES_cblock ciphertext;
69 DES_cblock buf;
71 ERR_clear_error();
72 if (DES_set_key(&userkey, &key) < 0)
73 return 0;
74 DES_ecb_encrypt( &plaintext, &ciphertext, &key, 1);
75 DES_ecb_encrypt( &ciphertext, &buf, &key, 0);
76 if (memcmp(buf, plaintext, sizeof(buf)))
77 return 0;
78 return 1;
81 /* DSA: generate key and sign a known digest, then verify the signature
82 * against the digest
84 static int FIPS_dsa_test()
86 DSA *dsa = NULL;
87 unsigned char dgst[] = "etaonrishdlc";
88 unsigned char sig[256];
89 unsigned int siglen;
91 ERR_clear_error();
92 dsa = DSA_generate_parameters(512,NULL,0,NULL,NULL,NULL,NULL);
93 if (!dsa)
94 return 0;
95 if (!DSA_generate_key(dsa))
96 return 0;
97 if ( DSA_sign(0,dgst,sizeof(dgst) - 1,sig,&siglen,dsa) != 1 )
98 return 0;
99 if ( DSA_verify(0,dgst,sizeof(dgst) - 1,sig,siglen,dsa) != 1 )
100 return 0;
101 DSA_free(dsa);
102 return 1;
105 /* RSA: generate keys and encrypt and decrypt known plaintext, verify result
106 * matches the original plaintext
108 static int FIPS_rsa_test()
110 RSA *key;
111 unsigned char input_ptext[] = "etaonrishdlc";
112 unsigned char ctext[256];
113 unsigned char ptext[256];
114 int n;
116 ERR_clear_error();
117 key = RSA_generate_key(1024,65537,NULL,NULL);
118 if (!key)
119 return 0;
120 n = RSA_size(key);
121 n = RSA_public_encrypt(sizeof(input_ptext) - 1,input_ptext,ctext,key,RSA_PKCS1_PADDING);
122 if (n < 0)
123 return 0;
124 n = RSA_private_decrypt(n,ctext,ptext,key,RSA_PKCS1_PADDING);
125 if (n < 0)
126 return 0;
127 RSA_free(key);
128 if (memcmp(input_ptext,ptext,sizeof(input_ptext) - 1))
129 return 0;
130 return 1;
133 /* SHA1: generate hash of known digest value and compare to known
134 precomputed correct hash
136 static int FIPS_sha1_test()
138 unsigned char digest[SHA_DIGEST_LENGTH] =
139 { 0x11, 0xf1, 0x9a, 0x3a, 0xec, 0x1a, 0x1e, 0x8e, 0x65, 0xd4, 0x9a, 0x38, 0x0c, 0x8b, 0x1e, 0x2c, 0xe8, 0xb3, 0xc5, 0x18 };
140 unsigned char str[] = "etaonrishd";
142 unsigned char md[SHA_DIGEST_LENGTH];
144 ERR_clear_error();
145 if (!SHA1(str,sizeof(str) - 1,md)) return 0;
146 if (memcmp(md,digest,sizeof(md)))
147 return 0;
148 return 1;
151 /* MD5: generate hash of known digest value and compare to known
152 precomputed correct hash
154 static int md5_test()
156 unsigned char digest[MD5_DIGEST_LENGTH] =
157 { 0x48, 0x50, 0xf0, 0xa3, 0x3a, 0xed, 0xd3, 0xaf, 0x6e, 0x47, 0x7f, 0x83, 0x02, 0xb1, 0x09, 0x68 };
158 unsigned char str[] = "etaonrishd";
160 unsigned char md[MD5_DIGEST_LENGTH];
162 ERR_clear_error();
163 if (!MD5(str,sizeof(str) - 1,md))
164 return 0;
165 if (memcmp(md,digest,sizeof(md)))
166 return 0;
167 return 1;
170 /* DH: generate shared parameters
172 static int dh_test()
174 DH *dh;
176 ERR_clear_error();
177 dh = DH_generate_parameters(256, 2, NULL, NULL);
178 if (dh)
179 return 1;
180 return 0;
183 /* Zeroize
185 static int Zeroize()
187 RSA *key;
188 unsigned char userkey[16] =
189 { 0x48, 0x50, 0xf0, 0xa3, 0x3a, 0xed, 0xd3, 0xaf, 0x6e, 0x47, 0x7f, 0x83, 0x02, 0xb1, 0x09, 0x68 };
190 int i, n;
192 key = RSA_generate_key(1024,65537,NULL,NULL);
193 if (!key)
194 return 0;
195 n = BN_num_bytes(key->d);
196 printf(" Generated %d byte RSA private key\n", n);
197 printf("\tBN key before overwriting:\n%s\n", BN_bn2hex(key->d));
198 BN_rand(key->d,n*8,-1,0);
199 printf("\tBN key after overwriting:\n%s\n", BN_bn2hex(key->d));
201 printf("\tchar buffer key before overwriting: \n\t\t");
202 for(i = 0; i < sizeof(userkey); i++) printf("%02x", userkey[i]);
203 printf("\n");
204 RAND_bytes(userkey, sizeof userkey);
205 printf("\tchar buffer key after overwriting: \n\t\t");
206 for(i = 0; i < sizeof(userkey); i++) printf("%02x", userkey[i]);
207 printf("\n");
209 return 1;
212 static int Error;
213 const char * Fail(const char *msg)
215 Error++;
216 return msg;
219 int main(int argc,char **argv)
222 printf("\tFIPS-mode test application\n\n");
224 /* Load entropy from external file, if any */
225 RAND_load_file(".rnd", 1024);
227 if (argv[1]) {
228 /* Corrupted KAT tests */
229 if (!strcmp(argv[1], "aes")) {
230 FIPS_corrupt_aes();
231 printf("3. AES encryption/decryption with corrupted KAT...\n");
232 } else if (!strcmp(argv[1], "des")) {
233 FIPS_corrupt_des();
234 printf("5. DES-ECB encryption/decryption with corrupted KAT...\n");
235 } else if (!strcmp(argv[1], "dsa")) {
236 FIPS_corrupt_dsa();
237 printf("6. DSA key generation and signature validation with corrupted KAT...\n");
238 } else if (!strcmp(argv[1], "rsa")) {
239 FIPS_corrupt_rsa();
240 printf("4. RSA key generation and encryption/decryption with corrupted KAT...\n");
241 } else if (!strcmp(argv[1], "sha1")) {
242 FIPS_corrupt_sha1();
243 printf("7. SHA-1 hash with corrupted KAT...\n");
244 } else {
245 printf("Bad argument \"%s\"\n", argv[1]);
246 exit(1);
248 if (!FIPS_mode_set(1,argv[0]))
250 ERR_load_crypto_strings();
251 ERR_print_errors(BIO_new_fp(stderr,BIO_NOCLOSE));
252 printf("Power-up self test failed\n");
253 exit(1);
255 printf("Power-up self test successful\n");
256 exit(0);
259 /* Non-Approved cryptographic operation
261 printf("0. Non-Approved cryptographic operation test...\n");
262 printf("\ta. Excluded algorithm (MD5)...");
263 printf( md5_test() ? "successful\n" : Fail("FAILED!\n") );
264 printf("\tb. Included algorithm (D-H)...");
265 printf( dh_test() ? "successful\n" : Fail("FAILED!\n") );
267 /* Power-up self test failure
269 printf("1. Automatic power-up self test...");
270 printf( FIPS_mode_set(1,"/dev/null") ? Fail("passed INCORRECTLY!\n") : "failed as expected\n" );
272 /* Algorithm call when uninitialized failure
274 printf("\ta. AES API failure on failed power-up self test...");
275 printf( FIPS_aes_test() ? Fail("passed INCORRECTLY!\n") :"failed as expected\n" );
276 printf("\tb. RSA API failure on failed power-up self test...");
277 printf( FIPS_rsa_test() ? Fail("passed INCORRECTLY!\n") : "failed as expected\n" );
278 printf("\tc. DES API failure on failed power-up self test...");
279 printf( FIPS_des_test() ? Fail("passed INCORRECTLY!\n") : "failed as expected\n" );
280 printf("\td. DSA API failure on failed power-up self test...");
281 printf( FIPS_dsa_test() ? Fail("passed INCORRECTLY!\n") : "failed as expected\n" );
282 printf("\te. SHA1 API failure on failed power-up self test...");
283 printf( FIPS_sha1_test() ? Fail("passed INCORRECTLY!\n") : "failed as expected\n" );
285 /* Power-up self test retry
287 ERR_clear_error();
288 printf("2. Automatic power-up self test retry...");
289 if (!FIPS_mode_set(1,argv[0]))
291 ERR_load_crypto_strings();
292 ERR_print_errors(BIO_new_fp(stderr,BIO_NOCLOSE));
293 printf(Fail("FAILED!\n"));
294 exit(1);
296 printf("successful\n");
298 /* AES encryption/decryption
300 printf("3. AES encryption/decryption...");
301 printf( FIPS_aes_test() ? "successful\n" : Fail("FAILED!\n") );
303 /* RSA key generation and encryption/decryption
305 printf("4. RSA key generation and encryption/decryption...");
306 printf( FIPS_rsa_test() ? "successful\n" : Fail("FAILED!\n") );
308 /* DES-CBC encryption/decryption
310 printf("5. DES-ECB encryption/decryption...");
311 printf( FIPS_des_test() ? "successful\n" : Fail("FAILED!\n") );
313 /* DSA key generation and signature validation
315 printf("6. DSA key generation and signature validation...");
316 printf( FIPS_dsa_test() ? "successful\n" : Fail("FAILED!\n") );
318 /* SHA-1 hash
320 printf("7. SHA-1 hash...");
321 printf( FIPS_sha1_test() ? "successful\n" : Fail("FAILED!\n") );
323 /* Non-Approved cryptographic operation
325 printf("8. Non-Approved cryptographic operation test...\n");
326 printf("\ta. Excluded algorithm (MD5)...");
327 printf( md5_test() ? Fail("passed INCORRECTLY!\n")
328 : "failed as expected\n" );
329 printf("\tb. Included algorithm (D-H)...");
330 printf( dh_test() ? "successful as expected\n"
331 : Fail("failed INCORRECTLY!\n") );
333 /* Zeroization
335 printf("9. Zero-ization...\n");
336 Zeroize();
338 printf("\nAll tests completed with %d errors\n", Error);
339 return 0;
341 #endif