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"
64 #include "fips_auth.h"
68 #include <openssl/fips.h>
74 #define atox(c) ((c)>='a'?((c)-'a'+10):((c)>='A'?(c)-'A'+10:(c)-'0'))
76 static int fips_selftest_fail = 0;
77 static int fips_auth_fail = 0;
78 static int fips_mode = 0;
79 static int fips_started = 0;
81 static int fips_is_owning_thread(void);
82 static int fips_set_owning_thread(void);
83 static int fips_clear_owning_thread(void);
84 static unsigned char *fips_signature_witness(void);
86 #define fips_w_lock() CRYPTO_w_lock(CRYPTO_LOCK_FIPS)
87 #define fips_w_unlock() CRYPTO_w_unlock(CRYPTO_LOCK_FIPS)
88 #define fips_r_lock() CRYPTO_r_lock(CRYPTO_LOCK_FIPS)
89 #define fips_r_unlock() CRYPTO_r_unlock(CRYPTO_LOCK_FIPS)
91 static void fips_set_mode(int onoff)
93 int owning_thread = fips_is_owning_thread();
97 if (!owning_thread) fips_w_lock();
99 if (!owning_thread) fips_w_unlock();
103 int FIPS_module_mode(void)
106 int owning_thread = fips_is_owning_thread();
110 if (!owning_thread) fips_r_lock();
112 if (!owning_thread) fips_r_unlock();
117 int FIPS_selftest_failed(void)
122 int owning_thread = fips_is_owning_thread();
124 if (!owning_thread) fips_r_lock();
125 ret = fips_selftest_fail;
126 if (!owning_thread) fips_r_unlock();
131 /* Selftest failure fatal exit routine. This will be called
132 * during *any* cryptographic operation. It has the minimum
133 * overhead possible to avoid too big a performance hit.
136 void FIPS_selftest_check(void)
138 if (fips_selftest_fail)
140 OpenSSLDie(__FILE__,__LINE__, "FATAL FIPS SELFTEST FAILURE");
144 void fips_set_selftest_fail(void)
146 fips_selftest_fail = 1;
149 extern const void *FIPS_text_start(), *FIPS_text_end();
150 extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
151 unsigned char FIPS_signature [20] = { 0 };
153 static const char FIPS_hmac_key[]="etaonrishdlcupfm";
155 unsigned int FIPS_incore_fingerprint(unsigned char *sig,unsigned int len)
157 const unsigned char *p1 = FIPS_text_start();
158 const unsigned char *p2 = FIPS_text_end();
159 const unsigned char *p3 = FIPS_rodata_start;
160 const unsigned char *p4 = FIPS_rodata_end;
164 HMAC_Init(&c,FIPS_hmac_key,strlen(FIPS_hmac_key),EVP_sha1());
166 /* detect overlapping regions */
167 if (p1<=p3 && p2>=p3)
168 p3=p1, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
169 else if (p3<=p1 && p4>=p1)
170 p3=p3, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
173 HMAC_Update(&c,p1,(size_t)p2-(size_t)p1);
175 if (FIPS_signature>=p3 && FIPS_signature<p4)
178 HMAC_Update(&c,p3,(size_t)FIPS_signature-(size_t)p3);
179 p3 = FIPS_signature+sizeof(FIPS_signature);
181 HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
184 HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
186 if (!fips_post_corrupt(FIPS_TEST_INTEGRITY, 0, NULL))
187 HMAC_Update(&c, (unsigned char *)FIPS_hmac_key, 1);
189 HMAC_Final(&c,sig,&len);
190 HMAC_CTX_cleanup(&c);
195 int FIPS_check_incore_fingerprint(void)
197 unsigned char sig[EVP_MAX_MD_SIZE];
200 #if defined(__sgi) && (defined(__mips) || defined(mips))
201 extern int __dso_displacement[];
203 extern int OPENSSL_NONPIC_relocated;
206 if (!fips_post_started(FIPS_TEST_INTEGRITY, 0, NULL))
209 if (FIPS_text_start()==NULL)
211 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_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_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_SEGMENT_ALIASING);
222 #if defined(__sgi) && (defined(__mips) || defined(mips))
223 else if (__dso_displacement!=NULL)
225 else if (OPENSSL_NONPIC_relocated)
227 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_NONPIC_RELOCATED);
229 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
230 #ifdef OPENSSL_FIPS_DEBUGGER
238 fips_post_failed(FIPS_TEST_INTEGRITY, 0, NULL);
240 if (!fips_post_success(FIPS_TEST_INTEGRITY, 0, NULL))
245 static int fips_asc_check(const unsigned char *sig, const char *asc_sig)
250 if (strlen(asc_sig) != 40)
252 for (i = 0, p = asc_sig; i < 20; i++, p += 2)
253 tsig[i] = (atox(p[0]) << 4) | atox(p[1]);
254 if (memcmp(tsig, sig, 20))
259 static int fips_check_auth(const char *auth)
261 unsigned char auth_hmac[20];
262 unsigned int hmac_len;
265 if (strlen(auth) < FIPS_AUTH_MIN_LEN)
267 if (!HMAC(EVP_sha1(), FIPS_AUTH_KEY, strlen(FIPS_AUTH_KEY),
268 (unsigned char *)auth, strlen(auth), auth_hmac, &hmac_len))
270 if (hmac_len != sizeof(auth_hmac))
273 if (fips_asc_check(auth_hmac, FIPS_AUTH_CRYPTO_OFFICER))
276 if (fips_asc_check(auth_hmac, FIPS_AUTH_CRYPTO_USER))
284 int FIPS_module_mode_set(int onoff, const char *auth)
290 fips_set_owning_thread();
295 fips_selftest_fail = 0;
296 if (!fips_check_auth(auth))
299 fips_selftest_fail = 1;
300 FIPSerr(FIPS_F_FIPS_MODULE_MODE_SET,FIPS_R_AUTHENTICATION_FAILURE);
304 /* Don't go into FIPS mode twice, just so we can do automagic
306 if(FIPS_module_mode())
308 FIPSerr(FIPS_F_FIPS_MODULE_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
309 fips_selftest_fail = 1;
314 #ifdef OPENSSL_IA32_SSE2
316 extern unsigned int OPENSSL_ia32cap_P[2];
317 if ((OPENSSL_ia32cap_P[0] & (1<<25|1<<26)) != (1<<25|1<<26))
319 FIPSerr(FIPS_F_FIPS_MODULE_MODE_SET,FIPS_R_UNSUPPORTED_PLATFORM);
320 fips_selftest_fail = 1;
324 OPENSSL_ia32cap_P[0] |= (1<<28); /* set "shared cache" */
325 OPENSSL_ia32cap_P[1] &= ~(1<<(60-32)); /* clear AVX */
329 if(fips_signature_witness() != FIPS_signature)
331 FIPSerr(FIPS_F_FIPS_MODULE_MODE_SET,FIPS_R_CONTRADICTING_EVIDENCE);
332 fips_selftest_fail = 1;
338 fips_set_mode(onoff);
341 fips_selftest_fail = 1;
349 fips_selftest_fail = 0;
352 fips_clear_owning_thread();
357 static CRYPTO_THREADID fips_thread;
358 static int fips_thread_set = 0;
360 static int fips_is_owning_thread(void)
366 CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
370 CRYPTO_THREADID_current(&cur);
371 if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
374 CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
379 int fips_set_owning_thread(void)
385 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
386 if (!fips_thread_set)
388 CRYPTO_THREADID_current(&fips_thread);
392 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
397 int fips_clear_owning_thread(void)
403 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
407 CRYPTO_THREADID_current(&cur);
408 if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
411 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
416 unsigned char *fips_signature_witness(void)
418 extern unsigned char FIPS_signature[];
419 return FIPS_signature;
422 unsigned long FIPS_module_version(void)
424 return FIPS_MODULE_VERSION_NUMBER;
427 const char *FIPS_module_version_text(void)
429 return FIPS_MODULE_VERSION_TEXT;
433 /* The purpose of this is to ensure the error code exists and the function
434 * name is to keep the error checking script quiet
436 void hash_final(void)
438 FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_NON_FIPS_METHOD);