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 #include <openssl/fips.h>
51 #include <openssl/rand.h>
52 #include <openssl/fips_rand.h>
53 #include <openssl/err.h>
54 #include <openssl/bio.h>
55 #include <openssl/hmac.h>
56 #include <openssl/rsa.h>
59 #include "fips_locl.h"
67 static int fips_selftest_fail;
69 static const void *fips_rand_check;
71 static void fips_set_mode(int onoff)
73 int owning_thread = fips_is_owning_thread();
75 if (fips_is_started())
77 if (!owning_thread) fips_w_lock();
79 if (!owning_thread) fips_w_unlock();
83 static void fips_set_rand_check(const void *rand_check)
85 int owning_thread = fips_is_owning_thread();
87 if (fips_is_started())
89 if (!owning_thread) fips_w_lock();
90 fips_rand_check = rand_check;
91 if (!owning_thread) fips_w_unlock();
98 int owning_thread = fips_is_owning_thread();
100 if (fips_is_started())
102 if (!owning_thread) fips_r_lock();
104 if (!owning_thread) fips_r_unlock();
109 const void *FIPS_rand_check(void)
112 int owning_thread = fips_is_owning_thread();
114 if (fips_is_started())
116 if (!owning_thread) fips_r_lock();
117 ret = fips_rand_check;
118 if (!owning_thread) fips_r_unlock();
123 int FIPS_selftest_failed(void)
126 if (fips_is_started())
128 int owning_thread = fips_is_owning_thread();
130 if (!owning_thread) fips_r_lock();
131 ret = fips_selftest_fail;
132 if (!owning_thread) fips_r_unlock();
137 /* Selftest failure fatal exit routine. This will be called
138 * during *any* cryptographic operation. It has the minimum
139 * overhead possible to avoid too big a performance hit.
142 void FIPS_selftest_check(void)
144 if (fips_selftest_fail)
146 OpenSSLDie(__FILE__,__LINE__, "FATAL FIPS SELFTEST FAILURE");
153 return FIPS_selftest_sha1()
154 && FIPS_selftest_hmac()
155 && FIPS_selftest_aes()
156 && FIPS_selftest_des()
157 && FIPS_selftest_rsa()
158 && FIPS_selftest_dsa();
161 extern const void *FIPS_text_start(), *FIPS_text_end();
162 extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
163 unsigned char FIPS_signature [20] = { 0 };
164 static const char FIPS_hmac_key[]="etaonrishdlcupfm";
166 unsigned int FIPS_incore_fingerprint(unsigned char *sig,unsigned int len)
168 const unsigned char *p1 = FIPS_text_start();
169 const unsigned char *p2 = FIPS_text_end();
170 const unsigned char *p3 = FIPS_rodata_start;
171 const unsigned char *p4 = FIPS_rodata_end;
175 HMAC_Init(&c,FIPS_hmac_key,strlen(FIPS_hmac_key),EVP_sha1());
177 /* detect overlapping regions */
178 if (p1<=p3 && p2>=p3)
179 p3=p1, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
180 else if (p3<=p1 && p4>=p1)
181 p3=p3, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
184 HMAC_Update(&c,p1,(size_t)p2-(size_t)p1);
186 if (FIPS_signature>=p3 && FIPS_signature<p4)
189 HMAC_Update(&c,p3,(size_t)FIPS_signature-(size_t)p3);
190 p3 = FIPS_signature+sizeof(FIPS_signature);
192 HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
195 HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
197 HMAC_Final(&c,sig,&len);
198 HMAC_CTX_cleanup(&c);
203 int FIPS_check_incore_fingerprint(void)
205 unsigned char sig[EVP_MAX_MD_SIZE];
207 extern int OPENSSL_NONPIC_relocated;
209 if (FIPS_text_start()==NULL)
211 FIPSerr(FIPS_F_FIPS_CHECK_FINGERPRINT,FIPS_R_UNSUPPORTED_PLATFORM);
215 len=FIPS_incore_fingerprint (sig,sizeof(sig));
217 if (len!=sizeof(FIPS_signature) ||
218 memcmp(FIPS_signature,sig,sizeof(FIPS_signature)))
220 if (FIPS_signature>=FIPS_rodata_start && FIPS_signature<FIPS_rodata_end)
221 FIPSerr(FIPS_F_FIPS_CHECK_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_SEGMENT_ALIASING);
222 else if (OPENSSL_NONPIC_relocated)
223 FIPSerr(FIPS_F_FIPS_CHECK_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_NONPIC_RELOCATED);
225 FIPSerr(FIPS_F_FIPS_CHECK_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
232 int FIPS_mode_set(int onoff)
234 int fips_set_owning_thread();
235 int fips_clear_owning_thread();
240 fips_set_owning_thread();
244 unsigned char buf[48];
246 fips_selftest_fail = 0;
248 /* Don't go into FIPS mode twice, just so we can do automagic
252 FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
253 fips_selftest_fail = 1;
258 #ifdef OPENSSL_IA32_SSE2
259 if ((OPENSSL_ia32cap & (1<<25|1<<26)) != (1<<25|1<<26))
261 FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_UNSUPPORTED_PLATFORM);
262 fips_selftest_fail = 1;
268 if(fips_signature_witness() != FIPS_signature)
270 FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_CONTRADICTING_EVIDENCE);
271 fips_selftest_fail = 1;
276 if(!FIPS_check_incore_fingerprint())
278 fips_selftest_fail = 1;
283 /* Perform RNG KAT before seeding */
284 if (!FIPS_selftest_rng())
286 fips_selftest_fail = 1;
291 /* automagically seed PRNG if not already seeded */
292 if(!FIPS_rand_status())
294 if(RAND_bytes(buf,sizeof buf) <= 0)
296 fips_selftest_fail = 1;
300 FIPS_rand_set_key(buf,32);
301 FIPS_rand_seed(buf+32,16);
304 /* now switch into FIPS mode */
305 fips_set_rand_check(FIPS_rand_method());
306 RAND_set_rand_method(FIPS_rand_method());
311 fips_selftest_fail = 1;
319 fips_selftest_fail = 0;
322 fips_clear_owning_thread();
328 /* here just to cause error codes to exist */
331 FIPSerr(FIPS_F_HASH_FINAL,FIPS_F_NON_FIPS_METHOD);
332 FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_FIPS_SELFTEST_FAILED);
336 void fips_w_lock(void) { CRYPTO_w_lock(CRYPTO_LOCK_FIPS); }
337 void fips_w_unlock(void) { CRYPTO_w_unlock(CRYPTO_LOCK_FIPS); }
338 void fips_r_lock(void) { CRYPTO_r_lock(CRYPTO_LOCK_FIPS); }
339 void fips_r_unlock(void) { CRYPTO_r_unlock(CRYPTO_LOCK_FIPS); }
341 static int fips_started = 0;
342 static unsigned long fips_thread = 0;
344 void fips_set_started(void)
349 int fips_is_started(void)
354 int fips_is_owning_thread(void)
358 if (fips_is_started())
360 CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
361 if (fips_thread != 0 && fips_thread == CRYPTO_thread_id())
363 CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
368 int fips_set_owning_thread(void)
372 if (fips_is_started())
374 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
375 if (fips_thread == 0)
377 fips_thread = CRYPTO_thread_id();
380 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
385 int fips_clear_owning_thread(void)
389 if (fips_is_started())
391 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
392 if (fips_thread == CRYPTO_thread_id())
397 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
402 unsigned char *fips_signature_witness(void)
404 extern unsigned char FIPS_signature[];
405 return FIPS_signature;
408 /* Generalized public key test routine. Signs and verifies the data
409 * supplied in tbs using mesage digest md and setting option digest
410 * flags md_flags. If the 'kat' parameter is not NULL it will
411 * additionally check the signature matches it: a known answer test
412 * The string "fail_str" is used for identification purposes in case
416 int fips_pkey_signature_test(EVP_PKEY *pkey,
417 const unsigned char *tbs, int tbslen,
418 const unsigned char *kat, unsigned int katlen,
419 const EVP_MD *digest, unsigned int md_flags,
420 const char *fail_str)
423 unsigned char sigtmp[256], *sig = sigtmp;
426 EVP_MD_CTX_init(&mctx);
428 if ((pkey->type == EVP_PKEY_RSA)
429 && (RSA_size(pkey->pkey.rsa) > sizeof(sigtmp)))
431 sig = OPENSSL_malloc(RSA_size(pkey->pkey.rsa));
434 FIPSerr(FIPS_F_FIPS_CHECK_PK_SIG,ERR_R_MALLOC_FAILURE);
440 tbslen = strlen((char *)tbs);
443 M_EVP_MD_CTX_set_flags(&mctx, md_flags);
445 if (!EVP_SignInit_ex(&mctx, digest, NULL))
447 if (!EVP_SignUpdate(&mctx, tbs, tbslen))
449 if (!EVP_SignFinal(&mctx, sig, &siglen, pkey))
452 if (kat && ((siglen != katlen) || memcmp(kat, sig, katlen)))
455 if (!EVP_VerifyInit_ex(&mctx, digest, NULL))
457 if (!EVP_VerifyUpdate(&mctx, tbs, tbslen))
459 ret = EVP_VerifyFinal(&mctx, sig, siglen, pkey);
464 EVP_MD_CTX_cleanup(&mctx);
467 FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,FIPS_R_TEST_FAILURE);
469 ERR_add_error_data(2, "Type=", fail_str);
475 /* Generalized symmetric cipher test routine. Encrypt data, verify result
476 * against known answer, decrypt and compare with original plaintext.
479 int fips_cipher_test(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
480 const unsigned char *key,
481 const unsigned char *iv,
482 const unsigned char *plaintext,
483 const unsigned char *ciphertext,
486 unsigned char pltmp[FIPS_MAX_CIPHER_TEST_SIZE];
487 unsigned char citmp[FIPS_MAX_CIPHER_TEST_SIZE];
488 OPENSSL_assert(len <= FIPS_MAX_CIPHER_TEST_SIZE);
489 if (EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 1) <= 0)
491 EVP_Cipher(ctx, citmp, plaintext, len);
492 if (memcmp(citmp, ciphertext, len))
494 if (EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 0) <= 0)
496 EVP_Cipher(ctx, pltmp, citmp, len);
497 if (memcmp(pltmp, plaintext, len))