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.
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>
30 int main(int argc
, char *argv
[])
32 printf("No FIPS support\n");
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];
49 if (AES_set_encrypt_key( userkey
, 128, &key
))
51 AES_encrypt( plaintext
, ciphertext
, &key
);
52 if (AES_set_decrypt_key( userkey
, 128, &dkey
))
54 AES_decrypt( ciphertext
, buf
, &dkey
);
55 if (memcmp(buf
, plaintext
, sizeof(buf
)))
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' };
68 DES_cblock ciphertext
;
72 if (DES_set_key(&userkey
, &key
) < 0)
74 DES_ecb_encrypt( &plaintext
, &ciphertext
, &key
, 1);
75 DES_ecb_encrypt( &ciphertext
, &buf
, &key
, 0);
76 if (memcmp(buf
, plaintext
, sizeof(buf
)))
81 /* DSA: generate key and sign a known digest, then verify the signature
84 static int FIPS_dsa_test()
87 unsigned char dgst
[] = "etaonrishdlc";
88 unsigned char sig
[256];
92 dsa
= DSA_generate_parameters(512,NULL
,0,NULL
,NULL
,NULL
,NULL
);
95 if (!DSA_generate_key(dsa
))
97 if ( DSA_sign(0,dgst
,sizeof(dgst
) - 1,sig
,&siglen
,dsa
) != 1 )
99 if ( DSA_verify(0,dgst
,sizeof(dgst
) - 1,sig
,siglen
,dsa
) != 1 )
105 /* RSA: generate keys and encrypt and decrypt known plaintext, verify result
106 * matches the original plaintext
108 static int FIPS_rsa_test()
111 unsigned char input_ptext
[] = "etaonrishdlc";
112 unsigned char ctext
[256];
113 unsigned char ptext
[256];
117 key
= RSA_generate_key(1024,65537,NULL
,NULL
);
121 n
= RSA_public_encrypt(sizeof(input_ptext
) - 1,input_ptext
,ctext
,key
,RSA_PKCS1_PADDING
);
124 n
= RSA_private_decrypt(n
,ctext
,ptext
,key
,RSA_PKCS1_PADDING
);
128 if (memcmp(input_ptext
,ptext
,sizeof(input_ptext
) - 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
];
145 if (!SHA1(str
,sizeof(str
) - 1,md
)) return 0;
146 if (memcmp(md
,digest
,sizeof(md
)))
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
];
163 if (!MD5(str
,sizeof(str
) - 1,md
))
165 if (memcmp(md
,digest
,sizeof(md
)))
170 /* DH: generate shared parameters
177 dh
= DH_generate_parameters(256, 2, NULL
, NULL
);
188 unsigned char userkey
[16] =
189 { 0x48, 0x50, 0xf0, 0xa3, 0x3a, 0xed, 0xd3, 0xaf, 0x6e, 0x47, 0x7f, 0x83, 0x02, 0xb1, 0x09, 0x68 };
192 key
= RSA_generate_key(1024,65537,NULL
,NULL
);
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
]);
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
]);
213 const char * Fail(const char *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);
228 /* Corrupted KAT tests */
229 if (!strcmp(argv
[1], "aes")) {
231 printf("3. AES encryption/decryption with corrupted KAT...\n");
232 } else if (!strcmp(argv
[1], "des")) {
234 printf("5. DES-ECB encryption/decryption with corrupted KAT...\n");
235 } else if (!strcmp(argv
[1], "dsa")) {
237 printf("6. DSA key generation and signature validation with corrupted KAT...\n");
238 } else if (!strcmp(argv
[1], "rsa")) {
240 printf("4. RSA key generation and encryption/decryption with corrupted KAT...\n");
241 } else if (!strcmp(argv
[1], "sha1")) {
243 printf("7. SHA-1 hash with corrupted KAT...\n");
245 printf("Bad argument \"%s\"\n", argv
[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");
255 printf("Power-up self test successful\n");
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
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"));
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") );
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") );
335 printf("9. Zero-ization...\n");
338 printf("\nAll tests completed with %d errors\n", Error
);