1 /* ====================================================================
2 * Copyright (c) 2003 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 #define OPENSSL_FIPSAPI
52 #include <openssl/rand.h>
53 #include <openssl/fips_rand.h>
54 #include <openssl/err.h>
55 #include <openssl/bio.h>
56 #include <openssl/hmac.h>
57 #include <openssl/rsa.h>
58 #include <openssl/dsa.h>
61 #include "fips_locl.h"
65 #include <openssl/fips.h>
71 static int fips_selftest_fail;
73 static const void *fips_rand_check;
75 static void fips_set_mode(int onoff)
77 int owning_thread = fips_is_owning_thread();
79 if (fips_is_started())
81 if (!owning_thread) fips_w_lock();
83 if (!owning_thread) fips_w_unlock();
87 static void fips_set_rand_check(const void *rand_check)
89 int owning_thread = fips_is_owning_thread();
91 if (fips_is_started())
93 if (!owning_thread) fips_w_lock();
94 fips_rand_check = rand_check;
95 if (!owning_thread) fips_w_unlock();
102 int owning_thread = fips_is_owning_thread();
104 if (fips_is_started())
106 if (!owning_thread) fips_r_lock();
108 if (!owning_thread) fips_r_unlock();
113 const void *FIPS_rand_check(void)
116 int owning_thread = fips_is_owning_thread();
118 if (fips_is_started())
120 if (!owning_thread) fips_r_lock();
121 ret = fips_rand_check;
122 if (!owning_thread) fips_r_unlock();
127 int FIPS_selftest_failed(void)
130 if (fips_is_started())
132 int owning_thread = fips_is_owning_thread();
134 if (!owning_thread) fips_r_lock();
135 ret = fips_selftest_fail;
136 if (!owning_thread) fips_r_unlock();
141 /* Selftest failure fatal exit routine. This will be called
142 * during *any* cryptographic operation. It has the minimum
143 * overhead possible to avoid too big a performance hit.
146 void FIPS_selftest_check(void)
148 if (fips_selftest_fail)
150 OpenSSLDie(__FILE__,__LINE__, "FATAL FIPS SELFTEST FAILURE");
154 void fips_set_selftest_fail(void)
156 fips_selftest_fail = 1;
159 int FIPS_selftest(void)
162 return FIPS_selftest_sha1()
163 && FIPS_selftest_hmac()
164 && FIPS_selftest_aes()
165 && FIPS_selftest_des()
166 && FIPS_selftest_rsa()
167 && FIPS_selftest_dsa();
170 extern const void *FIPS_text_start(), *FIPS_text_end();
171 extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
172 unsigned char FIPS_signature [20] = { 0 };
173 static const char FIPS_hmac_key[]="etaonrishdlcupfm";
175 unsigned int FIPS_incore_fingerprint(unsigned char *sig,unsigned int len)
177 const unsigned char *p1 = FIPS_text_start();
178 const unsigned char *p2 = FIPS_text_end();
179 const unsigned char *p3 = FIPS_rodata_start;
180 const unsigned char *p4 = FIPS_rodata_end;
184 HMAC_Init(&c,FIPS_hmac_key,strlen(FIPS_hmac_key),EVP_sha1());
186 /* detect overlapping regions */
187 if (p1<=p3 && p2>=p3)
188 p3=p1, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
189 else if (p3<=p1 && p4>=p1)
190 p3=p3, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
193 HMAC_Update(&c,p1,(size_t)p2-(size_t)p1);
195 if (FIPS_signature>=p3 && FIPS_signature<p4)
198 HMAC_Update(&c,p3,(size_t)FIPS_signature-(size_t)p3);
199 p3 = FIPS_signature+sizeof(FIPS_signature);
201 HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
204 HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
206 HMAC_Final(&c,sig,&len);
207 HMAC_CTX_cleanup(&c);
212 int FIPS_check_incore_fingerprint(void)
214 unsigned char sig[EVP_MAX_MD_SIZE];
216 #if defined(__sgi) && (defined(__mips) || defined(mips))
217 extern int __dso_displacement[];
219 extern int OPENSSL_NONPIC_relocated;
222 if (FIPS_text_start()==NULL)
224 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_UNSUPPORTED_PLATFORM);
228 len=FIPS_incore_fingerprint (sig,sizeof(sig));
230 if (len!=sizeof(FIPS_signature) ||
231 memcmp(FIPS_signature,sig,sizeof(FIPS_signature)))
233 if (FIPS_signature>=FIPS_rodata_start && FIPS_signature<FIPS_rodata_end)
234 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_SEGMENT_ALIASING);
235 #if defined(__sgi) && (defined(__mips) || defined(mips))
236 else if (__dso_displacement!=NULL)
238 else if (OPENSSL_NONPIC_relocated)
240 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_NONPIC_RELOCATED);
242 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
243 #ifdef OPENSSL_FIPS_DEBUGGER
252 int FIPS_mode_set(int onoff)
254 int fips_set_owning_thread();
255 int fips_clear_owning_thread();
260 fips_set_owning_thread();
264 unsigned char buf[48];
266 fips_selftest_fail = 0;
268 /* Don't go into FIPS mode twice, just so we can do automagic
272 FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
273 fips_selftest_fail = 1;
278 #ifdef OPENSSL_IA32_SSE2
279 if ((OPENSSL_ia32cap & (1<<25|1<<26)) != (1<<25|1<<26))
281 FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_UNSUPPORTED_PLATFORM);
282 fips_selftest_fail = 1;
288 if(fips_signature_witness() != FIPS_signature)
290 FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_CONTRADICTING_EVIDENCE);
291 fips_selftest_fail = 1;
296 if(!FIPS_check_incore_fingerprint())
298 fips_selftest_fail = 1;
303 /* Perform RNG KAT before seeding */
304 if (!FIPS_selftest_rng())
306 fips_selftest_fail = 1;
311 /* automagically seed PRNG if not already seeded */
312 if(!FIPS_rand_status())
314 if(RAND_bytes(buf,sizeof buf) <= 0)
316 fips_selftest_fail = 1;
320 FIPS_rand_set_key(buf,32);
321 FIPS_rand_seed(buf+32,16);
324 /* now switch into FIPS mode */
325 fips_set_rand_check(FIPS_rand_method());
326 RAND_set_rand_method(FIPS_rand_method());
331 fips_selftest_fail = 1;
339 fips_selftest_fail = 0;
342 fips_clear_owning_thread();
347 void fips_w_lock(void) { CRYPTO_w_lock(CRYPTO_LOCK_FIPS); }
348 void fips_w_unlock(void) { CRYPTO_w_unlock(CRYPTO_LOCK_FIPS); }
349 void fips_r_lock(void) { CRYPTO_r_lock(CRYPTO_LOCK_FIPS); }
350 void fips_r_unlock(void) { CRYPTO_r_unlock(CRYPTO_LOCK_FIPS); }
352 static int fips_started = 0;
353 static CRYPTO_THREADID fips_thread;
354 static int fips_thread_set = 0;
356 void fips_set_started(void)
361 int fips_is_started(void)
366 int fips_is_owning_thread(void)
370 if (fips_is_started())
372 CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
376 CRYPTO_THREADID_current(&cur);
377 if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
380 CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
385 int fips_set_owning_thread(void)
389 if (fips_is_started())
391 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
392 if (!fips_thread_set)
394 CRYPTO_THREADID_current(&fips_thread);
397 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
402 int fips_clear_owning_thread(void)
406 if (fips_is_started())
408 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
412 CRYPTO_THREADID_current(&cur);
413 if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
416 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
421 unsigned char *fips_signature_witness(void)
423 extern unsigned char FIPS_signature[];
424 return FIPS_signature;
427 /* Generalized public key test routine. Signs and verifies the data
428 * supplied in tbs using mesage digest md and setting RSA padding mode
429 * pad_mode. If the 'kat' parameter is not NULL it will
430 * additionally check the signature matches it: a known answer test
431 * The string "fail_str" is used for identification purposes in case
435 int fips_pkey_signature_test(EVP_PKEY *pkey,
436 const unsigned char *tbs, int tbslen,
437 const unsigned char *kat, unsigned int katlen,
438 const EVP_MD *digest, int pad_mode,
439 const char *fail_str)
442 unsigned char sigtmp[256], *sig = sigtmp;
444 DSA_SIG *dsig = NULL;
446 EVP_MD_CTX_init(&mctx);
448 if ((pkey->type == EVP_PKEY_RSA)
449 && ((size_t)RSA_size(pkey->pkey.rsa) > sizeof(sigtmp)))
451 sig = OPENSSL_malloc(RSA_size(pkey->pkey.rsa));
454 FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,ERR_R_MALLOC_FAILURE);
460 tbslen = strlen((char *)tbs);
462 if (!EVP_DigestInit_ex(&mctx, digest, NULL))
464 if (!EVP_DigestUpdate(&mctx, tbs, tbslen))
466 if (pkey->type == EVP_PKEY_RSA)
468 if (!FIPS_rsa_sign_ctx(pkey->pkey.rsa, &mctx,
469 pad_mode, 0, NULL, sig, &siglen))
472 else if (pkey->type == EVP_PKEY_DSA)
474 dsig = FIPS_dsa_sign_ctx(pkey->pkey.dsa, &mctx);
479 else if (!EVP_SignFinal(&mctx, sig, &siglen, pkey))
483 if (kat && ((siglen != katlen) || memcmp(kat, sig, katlen)))
486 if (!EVP_DigestInit_ex(&mctx, digest, NULL))
488 if (!EVP_DigestUpdate(&mctx, tbs, tbslen))
490 if (pkey->type == EVP_PKEY_RSA)
492 ret = FIPS_rsa_verify_ctx(pkey->pkey.rsa, &mctx,
493 pad_mode, 0, NULL, sig, siglen);
495 else if (pkey->type == EVP_PKEY_DSA)
497 ret = FIPS_dsa_verify_ctx(pkey->pkey.dsa, &mctx, dsig);
501 ret = EVP_VerifyFinal(&mctx, sig, siglen, pkey);
509 EVP_MD_CTX_cleanup(&mctx);
512 FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,FIPS_R_TEST_FAILURE);
514 ERR_add_error_data(2, "Type=", fail_str);
520 /* Generalized symmetric cipher test routine. Encrypt data, verify result
521 * against known answer, decrypt and compare with original plaintext.
524 int fips_cipher_test(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
525 const unsigned char *key,
526 const unsigned char *iv,
527 const unsigned char *plaintext,
528 const unsigned char *ciphertext,
531 unsigned char pltmp[FIPS_MAX_CIPHER_TEST_SIZE];
532 unsigned char citmp[FIPS_MAX_CIPHER_TEST_SIZE];
533 OPENSSL_assert(len <= FIPS_MAX_CIPHER_TEST_SIZE);
534 if (EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 1) <= 0)
536 EVP_Cipher(ctx, citmp, plaintext, len);
537 if (memcmp(citmp, ciphertext, len))
539 if (EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 0) <= 0)
541 EVP_Cipher(ctx, pltmp, citmp, len);
542 if (memcmp(pltmp, plaintext, len))
548 /* The purpose of this is to ensure the error code exists and the function
549 * name is to keep the error checking script quiet
551 void hash_final(void)
553 FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_NON_FIPS_METHOD);