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 if(fips_signature_witness() != FIPS_signature)
260 FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_CONTRADICTING_EVIDENCE);
261 fips_selftest_fail = 1;
266 if(!FIPS_check_incore_fingerprint())
268 fips_selftest_fail = 1;
273 /* Perform RNG KAT before seeding */
274 if (!FIPS_selftest_rng())
276 fips_selftest_fail = 1;
281 /* automagically seed PRNG if not already seeded */
282 if(!FIPS_rand_status())
284 if(RAND_bytes(buf,sizeof buf) <= 0)
286 fips_selftest_fail = 1;
290 FIPS_rand_set_key(buf,32);
291 FIPS_rand_seed(buf+32,16);
294 /* now switch into FIPS mode */
295 fips_set_rand_check(FIPS_rand_method());
296 RAND_set_rand_method(FIPS_rand_method());
301 fips_selftest_fail = 1;
309 fips_selftest_fail = 0;
312 fips_clear_owning_thread();
318 /* here just to cause error codes to exist */
321 FIPSerr(FIPS_F_HASH_FINAL,FIPS_F_NON_FIPS_METHOD);
322 FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_FIPS_SELFTEST_FAILED);
326 void fips_w_lock(void) { CRYPTO_w_lock(CRYPTO_LOCK_FIPS); }
327 void fips_w_unlock(void) { CRYPTO_w_unlock(CRYPTO_LOCK_FIPS); }
328 void fips_r_lock(void) { CRYPTO_r_lock(CRYPTO_LOCK_FIPS); }
329 void fips_r_unlock(void) { CRYPTO_r_unlock(CRYPTO_LOCK_FIPS); }
331 static int fips_started = 0;
332 static unsigned long fips_thread = 0;
334 void fips_set_started(void)
339 int fips_is_started(void)
344 int fips_is_owning_thread(void)
348 if (fips_is_started())
350 CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
351 if (fips_thread != 0 && fips_thread == CRYPTO_thread_id())
353 CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
358 int fips_set_owning_thread(void)
362 if (fips_is_started())
364 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
365 if (fips_thread == 0)
367 fips_thread = CRYPTO_thread_id();
370 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
375 int fips_clear_owning_thread(void)
379 if (fips_is_started())
381 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
382 if (fips_thread == CRYPTO_thread_id())
387 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
392 unsigned char *fips_signature_witness(void)
394 extern unsigned char FIPS_signature[];
395 return FIPS_signature;
398 /* Generalized public key test routine. Signs and verifies the data
399 * supplied in tbs using mesage digest md and setting option digest
400 * flags md_flags. If the 'kat' parameter is not NULL it will
401 * additionally check the signature matches it: a known answer test
402 * The string "fail_str" is used for identification purposes in case
406 int fips_pkey_signature_test(EVP_PKEY *pkey,
407 const unsigned char *tbs, int tbslen,
408 const unsigned char *kat, unsigned int katlen,
409 const EVP_MD *digest, unsigned int md_flags,
410 const char *fail_str)
413 unsigned char sigtmp[256], *sig = sigtmp;
416 EVP_MD_CTX_init(&mctx);
418 if ((pkey->type == EVP_PKEY_RSA)
419 && (RSA_size(pkey->pkey.rsa) > sizeof(sigtmp)))
421 sig = OPENSSL_malloc(RSA_size(pkey->pkey.rsa));
424 FIPSerr(FIPS_F_FIPS_CHECK_PK_SIG,ERR_R_MALLOC_FAILURE);
430 tbslen = strlen((char *)tbs);
433 M_EVP_MD_CTX_set_flags(&mctx, md_flags);
435 if (!EVP_SignInit_ex(&mctx, digest, NULL))
437 if (!EVP_SignUpdate(&mctx, tbs, tbslen))
439 if (!EVP_SignFinal(&mctx, sig, &siglen, pkey))
442 if (kat && ((siglen != katlen) || memcmp(kat, sig, katlen)))
445 if (!EVP_VerifyInit_ex(&mctx, digest, NULL))
447 if (!EVP_VerifyUpdate(&mctx, tbs, tbslen))
449 ret = EVP_VerifyFinal(&mctx, sig, siglen, pkey);
454 EVP_MD_CTX_cleanup(&mctx);
457 FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,FIPS_R_TEST_FAILURE);
459 ERR_add_error_data(2, "Type=", fail_str);