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();
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 int FIPS_selftest(void)
148 return FIPS_selftest_sha1()
149 && FIPS_selftest_hmac()
150 && FIPS_selftest_cmac()
151 && FIPS_selftest_aes()
152 && FIPS_selftest_aes_gcm()
153 && FIPS_selftest_des()
154 && FIPS_selftest_rsa()
155 && FIPS_selftest_ecdsa()
156 && FIPS_selftest_dsa();
159 extern const void *FIPS_text_start(), *FIPS_text_end();
160 extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
161 unsigned char FIPS_signature [20] = { 0 };
162 static const char FIPS_hmac_key[]="etaonrishdlcupfm";
164 unsigned int FIPS_incore_fingerprint(unsigned char *sig,unsigned int len)
166 const unsigned char *p1 = FIPS_text_start();
167 const unsigned char *p2 = FIPS_text_end();
168 const unsigned char *p3 = FIPS_rodata_start;
169 const unsigned char *p4 = FIPS_rodata_end;
173 HMAC_Init(&c,FIPS_hmac_key,strlen(FIPS_hmac_key),EVP_sha1());
175 /* detect overlapping regions */
176 if (p1<=p3 && p2>=p3)
177 p3=p1, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
178 else if (p3<=p1 && p4>=p1)
179 p3=p3, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
182 HMAC_Update(&c,p1,(size_t)p2-(size_t)p1);
184 if (FIPS_signature>=p3 && FIPS_signature<p4)
187 HMAC_Update(&c,p3,(size_t)FIPS_signature-(size_t)p3);
188 p3 = FIPS_signature+sizeof(FIPS_signature);
190 HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
193 HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
195 HMAC_Final(&c,sig,&len);
196 HMAC_CTX_cleanup(&c);
201 int FIPS_check_incore_fingerprint(void)
203 unsigned char sig[EVP_MAX_MD_SIZE];
205 #if defined(__sgi) && (defined(__mips) || defined(mips))
206 extern int __dso_displacement[];
208 extern int OPENSSL_NONPIC_relocated;
211 if (FIPS_text_start()==NULL)
213 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_UNSUPPORTED_PLATFORM);
217 len=FIPS_incore_fingerprint (sig,sizeof(sig));
219 if (len!=sizeof(FIPS_signature) ||
220 memcmp(FIPS_signature,sig,sizeof(FIPS_signature)))
222 if (FIPS_signature>=FIPS_rodata_start && FIPS_signature<FIPS_rodata_end)
223 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_SEGMENT_ALIASING);
224 #if defined(__sgi) && (defined(__mips) || defined(mips))
225 else if (__dso_displacement!=NULL)
227 else if (OPENSSL_NONPIC_relocated)
229 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_NONPIC_RELOCATED);
231 FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
232 #ifdef OPENSSL_FIPS_DEBUGGER
241 int FIPS_mode_set(int onoff)
243 int fips_set_owning_thread();
244 int fips_clear_owning_thread();
249 fips_set_owning_thread();
254 fips_selftest_fail = 0;
256 /* Don't go into FIPS mode twice, just so we can do automagic
260 FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
261 fips_selftest_fail = 1;
266 #ifdef OPENSSL_IA32_SSE2
267 if ((OPENSSL_ia32cap & (1<<25|1<<26)) != (1<<25|1<<26))
269 FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_UNSUPPORTED_PLATFORM);
270 fips_selftest_fail = 1;
276 if(fips_signature_witness() != FIPS_signature)
278 FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_CONTRADICTING_EVIDENCE);
279 fips_selftest_fail = 1;
284 if(!FIPS_check_incore_fingerprint())
286 fips_selftest_fail = 1;
291 if (!FIPS_selftest_drbg())
293 fips_selftest_fail = 1;
298 /* Perform RNG KAT before seeding */
299 if (!FIPS_selftest_x931())
301 fips_selftest_fail = 1;
310 fips_selftest_fail = 1;
318 fips_selftest_fail = 0;
321 fips_clear_owning_thread();
326 static CRYPTO_THREADID fips_thread;
327 static int fips_thread_set = 0;
329 static int fips_is_owning_thread(void)
335 CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
339 CRYPTO_THREADID_current(&cur);
340 if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
343 CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
348 int fips_set_owning_thread(void)
354 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
355 if (!fips_thread_set)
357 CRYPTO_THREADID_current(&fips_thread);
361 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
366 int fips_clear_owning_thread(void)
372 CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
376 CRYPTO_THREADID_current(&cur);
377 if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
380 CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
385 unsigned char *fips_signature_witness(void)
387 extern unsigned char FIPS_signature[];
388 return FIPS_signature;
391 /* Generalized public key test routine. Signs and verifies the data
392 * supplied in tbs using mesage digest md and setting RSA padding mode
393 * pad_mode. If the 'kat' parameter is not NULL it will
394 * additionally check the signature matches it: a known answer test
395 * The string "fail_str" is used for identification purposes in case
399 int fips_pkey_signature_test(EVP_PKEY *pkey,
400 const unsigned char *tbs, size_t tbslen,
401 const unsigned char *kat, size_t katlen,
402 const EVP_MD *digest, int pad_mode,
403 const char *fail_str)
406 unsigned char *sig = NULL;
408 static const unsigned char str1[]="12345678901234567890";
409 DSA_SIG *dsig = NULL;
410 ECDSA_SIG *esig = NULL;
412 FIPS_md_ctx_init(&mctx);
418 if (pkey->type == EVP_PKEY_RSA)
420 sig = OPENSSL_malloc(RSA_size(pkey->pkey.rsa));
423 FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,ERR_R_MALLOC_FAILURE);
429 tbslen = strlen((char *)tbs);
432 digest = EVP_sha256();
434 if (!FIPS_digestinit(&mctx, digest))
436 if (!FIPS_digestupdate(&mctx, tbs, tbslen))
438 if (pkey->type == EVP_PKEY_RSA)
440 if (!FIPS_rsa_sign_ctx(pkey->pkey.rsa, &mctx,
441 pad_mode, 0, NULL, sig, &siglen))
444 else if (pkey->type == EVP_PKEY_DSA)
446 dsig = FIPS_dsa_sign_ctx(pkey->pkey.dsa, &mctx);
450 else if (pkey->type == EVP_PKEY_EC)
452 esig = FIPS_ecdsa_sign_ctx(pkey->pkey.ec, &mctx);
457 if (kat && ((siglen != katlen) || memcmp(kat, sig, katlen)))
461 /* Debug code to print out self test KAT discrepancies */
463 fprintf(stderr, "%s=", fail_str);
464 for (i = 0; i < siglen; i++)
465 fprintf(stderr, "%02X", sig[i]);
466 fprintf(stderr, "\n");
470 if (!FIPS_digestinit(&mctx, digest))
472 if (!FIPS_digestupdate(&mctx, tbs, tbslen))
474 if (pkey->type == EVP_PKEY_RSA)
476 ret = FIPS_rsa_verify_ctx(pkey->pkey.rsa, &mctx,
477 pad_mode, 0, NULL, sig, siglen);
479 else if (pkey->type == EVP_PKEY_DSA)
481 ret = FIPS_dsa_verify_ctx(pkey->pkey.dsa, &mctx, dsig);
483 else if (pkey->type == EVP_PKEY_EC)
485 ret = FIPS_ecdsa_verify_ctx(pkey->pkey.ec, &mctx, esig);
490 FIPS_dsa_sig_free(dsig);
492 FIPS_ecdsa_sig_free(esig);
495 FIPS_md_ctx_cleanup(&mctx);
498 FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,FIPS_R_TEST_FAILURE);
500 FIPS_add_error_data(2, "Type=", fail_str);
506 /* Generalized symmetric cipher test routine. Encrypt data, verify result
507 * against known answer, decrypt and compare with original plaintext.
510 int fips_cipher_test(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
511 const unsigned char *key,
512 const unsigned char *iv,
513 const unsigned char *plaintext,
514 const unsigned char *ciphertext,
517 unsigned char pltmp[FIPS_MAX_CIPHER_TEST_SIZE];
518 unsigned char citmp[FIPS_MAX_CIPHER_TEST_SIZE];
519 OPENSSL_assert(len <= FIPS_MAX_CIPHER_TEST_SIZE);
520 memset(pltmp, 0, FIPS_MAX_CIPHER_TEST_SIZE);
521 memset(citmp, 0, FIPS_MAX_CIPHER_TEST_SIZE);
522 if (FIPS_cipherinit(ctx, cipher, key, iv, 1) <= 0)
524 if (!FIPS_cipher(ctx, citmp, plaintext, len))
526 if (memcmp(citmp, ciphertext, len))
528 if (FIPS_cipherinit(ctx, cipher, key, iv, 0) <= 0)
530 FIPS_cipher(ctx, pltmp, citmp, len);
531 if (memcmp(pltmp, plaintext, len))
537 /* The purpose of this is to ensure the error code exists and the function
538 * name is to keep the error checking script quiet
540 void hash_final(void)
542 FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_NON_FIPS_METHOD);