1 /* ====================================================================
2 * Copyright (c) 2011 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/crypto.h>
53 #include <openssl/rand.h>
54 #include <openssl/fips_rand.h>
55 #include <openssl/err.h>
56 #include <openssl/bio.h>
57 #include <openssl/hmac.h>
58 #include <openssl/rsa.h>
59 #include <openssl/dsa.h>
60 #include <openssl/ecdsa.h>
63 #include "fips_locl.h"
67 #include <openssl/fips.h>
73 static int fips_selftest_fail = 0;
74 static int fips_mode = 0;
75 static int fips_started = 0;
77 static int fips_is_owning_thread(void);
78 static int fips_set_owning_thread(void);
79 static int fips_clear_owning_thread(void);
80 static unsigned char *fips_signature_witness(void);
82 #define fips_w_lock() CRYPTO_w_lock(CRYPTO_LOCK_FIPS)
83 #define fips_w_unlock() CRYPTO_w_unlock(CRYPTO_LOCK_FIPS)
84 #define fips_r_lock() CRYPTO_r_lock(CRYPTO_LOCK_FIPS)
85 #define fips_r_unlock() CRYPTO_r_unlock(CRYPTO_LOCK_FIPS)
87 static void fips_set_mode(int onoff)
89 int owning_thread = fips_is_owning_thread();
93 if (!owning_thread) fips_w_lock();
95 if (!owning_thread) fips_w_unlock();
99 int FIPS_module_mode(void)
102 int owning_thread = fips_is_owning_thread();
106 if (!owning_thread) fips_r_lock();
108 if (!owning_thread) fips_r_unlock();
113 int FIPS_selftest_failed(void)
118 int owning_thread = fips_is_owning_thread();
120 if (!owning_thread) fips_r_lock();
121 ret = fips_selftest_fail;
122 if (!owning_thread) fips_r_unlock();
127 /* Selftest failure fatal exit routine. This will be called
128 * during *any* cryptographic operation. It has the minimum
129 * overhead possible to avoid too big a performance hit.
132 void FIPS_selftest_check(void)
134 if (fips_selftest_fail)
136 OpenSSLDie(__FILE__,__LINE__, "FATAL FIPS SELFTEST FAILURE");
140 void fips_set_selftest_fail(void)
142 fips_selftest_fail = 1;
145 extern const void *FIPS_text_start(), *FIPS_text_end();
146 extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
147 unsigned char FIPS_signature [20] = { 0 };
148 static const char FIPS_hmac_key[]="etaonrishdlcupfm";
150 unsigned int FIPS_incore_fingerprint(unsigned char *sig,unsigned int len)
152 const unsigned char *p1 = FIPS_text_start();
153 const unsigned char *p2 = FIPS_text_end();
154 const unsigned char *p3 = FIPS_rodata_start;
155 const unsigned char *p4 = FIPS_rodata_end;
159 HMAC_Init(&c,FIPS_hmac_key,strlen(FIPS_hmac_key),EVP_sha1());
161 /* detect overlapping regions */
162 if (p1<=p3 && p2>=p3)
163 p3=p1, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
164 else if (p3<=p1 && p4>=p1)
165 p3=p3, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
168 HMAC_Update(&c,p1,(size_t)p2-(size_t)p1);
170 if (FIPS_signature>=p3 && FIPS_signature<p4)
173 HMAC_Update(&c,p3,(size_t)FIPS_signature-(size_t)p3);
174 p3 = FIPS_signature+sizeof(FIPS_signature);
176 HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
179 HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
181 if (!fips_post_corrupt(FIPS_TEST_INTEGRITY, 0, NULL))
182 HMAC_Update(&c, (unsigned char *)FIPS_hmac_key, 1);
184 HMAC_Final(&c,sig,&len);
185 HMAC_CTX_cleanup(&c);
190 int FIPS_check_incore_fingerprint(void)
192 unsigned char sig[EVP_MAX_MD_SIZE];
195 #if defined(__sgi) && (defined(__mips) || defined(mips))
196 extern int __dso_displacement[];
198 extern int OPENSSL_NONPIC_relocated;
201 if (!fips_post_started(FIPS_TEST_INTEGRITY, 0, NULL))
204 if (FIPS_text_start()==NULL)
206 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_UNSUPPORTED_PLATFORM);
210 len=FIPS_incore_fingerprint(sig,sizeof(sig));
212 if (len!=sizeof(FIPS_signature) ||
213 memcmp(FIPS_signature,sig,sizeof(FIPS_signature)))
215 if (FIPS_signature>=FIPS_rodata_start && FIPS_signature<FIPS_rodata_end)
216 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_SEGMENT_ALIASING);
217 #if defined(__sgi) && (defined(__mips) || defined(mips))
218 else if (__dso_displacement!=NULL)
220 else if (OPENSSL_NONPIC_relocated)
222 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_NONPIC_RELOCATED);
224 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
225 #ifdef OPENSSL_FIPS_DEBUGGER
233 fips_post_failed(FIPS_TEST_INTEGRITY, 0, NULL);
235 if (!fips_post_success(FIPS_TEST_INTEGRITY, 0, NULL))
240 int FIPS_module_mode_set(int onoff)
242 int fips_set_owning_thread();
243 int fips_clear_owning_thread();
248 fips_set_owning_thread();
253 fips_selftest_fail = 0;
255 /* Don't go into FIPS mode twice, just so we can do automagic
257 if(FIPS_module_mode())
259 FIPSerr(FIPS_F_FIPS_MODULE_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
260 fips_selftest_fail = 1;
265 #ifdef OPENSSL_IA32_SSE2
266 if ((OPENSSL_ia32cap & (1<<25|1<<26)) != (1<<25|1<<26))
268 FIPSerr(FIPS_F_FIPS_MODULE_MODE_SET,FIPS_R_UNSUPPORTED_PLATFORM);
269 fips_selftest_fail = 1;
275 if(fips_signature_witness() != FIPS_signature)
277 FIPSerr(FIPS_F_FIPS_MODULE_MODE_SET,FIPS_R_CONTRADICTING_EVIDENCE);
278 fips_selftest_fail = 1;
284 fips_set_mode(onoff);
287 fips_selftest_fail = 1;
295 fips_selftest_fail = 0;
298 fips_clear_owning_thread();
303 static CRYPTO_THREADID fips_thread;
304 static int fips_thread_set = 0;
306 static int fips_is_owning_thread(void)
312 CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
316 CRYPTO_THREADID_current(&cur);
317 if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
320 CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
325 int fips_set_owning_thread(void)
331 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
332 if (!fips_thread_set)
334 CRYPTO_THREADID_current(&fips_thread);
338 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
343 int fips_clear_owning_thread(void)
349 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
353 CRYPTO_THREADID_current(&cur);
354 if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
357 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
362 unsigned char *fips_signature_witness(void)
364 extern unsigned char FIPS_signature[];
365 return FIPS_signature;
369 /* The purpose of this is to ensure the error code exists and the function
370 * name is to keep the error checking script quiet
372 void hash_final(void)
374 FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_NON_FIPS_METHOD);