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>
58 #include "fips_locl.h"
66 static int fips_selftest_fail = 0;
67 static int fips_mode = 0;
68 static const void *fips_rand_check = 0;
70 static void fips_set_mode(int onoff)
72 int owning_thread = fips_is_owning_thread();
74 if (fips_is_started())
76 if (!owning_thread) fips_w_lock();
78 if (!owning_thread) fips_w_unlock();
82 static void fips_set_rand_check(const void *rand_check)
84 int owning_thread = fips_is_owning_thread();
86 if (fips_is_started())
88 if (!owning_thread) fips_w_lock();
89 fips_rand_check = rand_check;
90 if (!owning_thread) fips_w_unlock();
97 int owning_thread = fips_is_owning_thread();
99 if (fips_is_started())
101 if (!owning_thread) fips_r_lock();
103 if (!owning_thread) fips_r_unlock();
108 const void *FIPS_rand_check(void)
111 int owning_thread = fips_is_owning_thread();
113 if (fips_is_started())
115 if (!owning_thread) fips_r_lock();
116 ret = fips_rand_check;
117 if (!owning_thread) fips_r_unlock();
122 int FIPS_selftest_failed(void)
125 if (fips_is_started())
127 int owning_thread = fips_is_owning_thread();
129 if (!owning_thread) fips_r_lock();
130 ret = fips_selftest_fail;
131 if (!owning_thread) fips_r_unlock();
138 ERR_load_crypto_strings();
140 return FIPS_selftest_sha1()
141 && FIPS_selftest_aes()
142 && FIPS_selftest_des()
143 && FIPS_selftest_rsa()
144 && FIPS_selftest_dsa();
147 static int FIPS_check_exe(const char *path)
149 unsigned char buf[1024];
152 unsigned char mdbuf[EVP_MAX_MD_SIZE];
154 static char key[]="etaonrishdlcupfm";
156 const char *sha1_fmt="%s.sha1";
160 /* cygwin scrupulously strips .exe extentions:-( as of now it's
161 actually no point to attempt above fopen, but we keep the call
162 just in case the behavior changes in the future... */
165 sha1_fmt="%s.exe.sha1";
166 BIO_snprintf(p2,sizeof p2,"%s.exe",path);
172 FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_CANNOT_READ_EXE);
175 HMAC_Init(&hmac,key,strlen(key),EVP_sha1());
178 n=fread(buf,1,sizeof buf,f);
183 FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_CANNOT_READ_EXE);
186 if (n) HMAC_Update(&hmac,buf,n);
189 HMAC_Final(&hmac,mdbuf,&n);
190 HMAC_CTX_cleanup(&hmac);
191 BIO_snprintf(p2,sizeof p2,sha1_fmt,path);
193 if(!f || fread(buf,1,20,f) != 20)
196 FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_CANNOT_READ_EXE_DIGEST);
200 if(memcmp(buf,mdbuf,20))
202 FIPSerr(FIPS_F_FIPS_CHECK_EXE,FIPS_R_EXE_DIGEST_DOES_NOT_MATCH);
208 int FIPS_mode_set(int onoff,const char *path)
210 void fips_set_mode(int _onoff);
211 int fips_set_owning_thread();
212 int fips_clear_owning_thread();
217 fips_set_owning_thread();
221 unsigned char buf[24];
223 fips_selftest_fail = 0;
225 /* Don't go into FIPS mode twice, just so we can do automagic
229 FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
230 fips_selftest_fail = 1;
235 if(!FIPS_check_exe(path))
237 fips_selftest_fail = 1;
242 /* automagically seed PRNG if not already seeded */
243 if(!FIPS_rand_seeded())
245 if(RAND_bytes(buf,sizeof buf) <= 0)
247 fips_selftest_fail = 1;
251 FIPS_set_prng_key(buf,buf+8);
252 FIPS_rand_seed(buf+16,8);
255 /* now switch into FIPS mode */
256 fips_set_rand_check(FIPS_rand_method());
257 RAND_set_rand_method(FIPS_rand_method());
262 fips_selftest_fail = 1;
270 fips_selftest_fail = 0;
273 fips_clear_owning_thread();
279 /* here just to cause error codes to exist */
282 FIPSerr(FIPS_F_HASH_FINAL,FIPS_F_NON_FIPS_METHOD);
283 FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_FIPS_SELFTEST_FAILED);