/* ==================================================================== * Copyright (c) 2003 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * openssl-core@openssl.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.openssl.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include #include #include #include #include #include #include #include #include "fips_locl.h" #ifdef OPENSSL_FIPS #include #ifndef PATH_MAX #define PATH_MAX 1024 #endif static int fips_selftest_fail; static int fips_mode; static const void *fips_rand_check; static void fips_set_mode(int onoff) { int owning_thread = fips_is_owning_thread(); if (fips_is_started()) { if (!owning_thread) fips_w_lock(); fips_mode = onoff; if (!owning_thread) fips_w_unlock(); } } static void fips_set_rand_check(const void *rand_check) { int owning_thread = fips_is_owning_thread(); if (fips_is_started()) { if (!owning_thread) fips_w_lock(); fips_rand_check = rand_check; if (!owning_thread) fips_w_unlock(); } } int FIPS_mode(void) { int ret = 0; int owning_thread = fips_is_owning_thread(); if (fips_is_started()) { if (!owning_thread) fips_r_lock(); ret = fips_mode; if (!owning_thread) fips_r_unlock(); } return ret; } const void *FIPS_rand_check(void) { const void *ret = 0; int owning_thread = fips_is_owning_thread(); if (fips_is_started()) { if (!owning_thread) fips_r_lock(); ret = fips_rand_check; if (!owning_thread) fips_r_unlock(); } return ret; } int FIPS_selftest_failed(void) { int ret = 0; if (fips_is_started()) { int owning_thread = fips_is_owning_thread(); if (!owning_thread) fips_r_lock(); ret = fips_selftest_fail; if (!owning_thread) fips_r_unlock(); } return ret; } /* Selftest failure fatal exit routine. This will be called * during *any* cryptographic operation. It has the minimum * overhead possible to avoid too big a performance hit. */ void FIPS_selftest_check(void) { if (fips_selftest_fail) { OpenSSLDie(__FILE__,__LINE__, "FATAL FIPS SELFTEST FAILURE"); } } void fips_set_selftest_fail(void) { fips_selftest_fail = 1; } int FIPS_selftest() { return FIPS_selftest_sha1() && FIPS_selftest_hmac() && FIPS_selftest_aes() && FIPS_selftest_des() && FIPS_selftest_rsa() && FIPS_selftest_dsa(); } extern const void *FIPS_text_start(), *FIPS_text_end(); extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[]; unsigned char FIPS_signature [20] = { 0 }; static const char FIPS_hmac_key[]="etaonrishdlcupfm"; unsigned int FIPS_incore_fingerprint(unsigned char *sig,unsigned int len) { const unsigned char *p1 = FIPS_text_start(); const unsigned char *p2 = FIPS_text_end(); const unsigned char *p3 = FIPS_rodata_start; const unsigned char *p4 = FIPS_rodata_end; HMAC_CTX c; HMAC_CTX_init(&c); HMAC_Init(&c,FIPS_hmac_key,strlen(FIPS_hmac_key),EVP_sha1()); /* detect overlapping regions */ if (p1<=p3 && p2>=p3) p3=p1, p4=p2>p4?p2:p4, p1=NULL, p2=NULL; else if (p3<=p1 && p4>=p1) p3=p3, p4=p2>p4?p2:p4, p1=NULL, p2=NULL; if (p1) HMAC_Update(&c,p1,(size_t)p2-(size_t)p1); if (FIPS_signature>=p3 && FIPS_signature=FIPS_rodata_start && FIPS_signaturetype == EVP_PKEY_RSA) && (RSA_size(pkey->pkey.rsa) > sizeof(sigtmp))) { sig = OPENSSL_malloc(RSA_size(pkey->pkey.rsa)); if (!sig) { FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,ERR_R_MALLOC_FAILURE); return 0; } } if (tbslen == -1) tbslen = strlen((char *)tbs); if (md_flags) M_EVP_MD_CTX_set_flags(&mctx, md_flags); if (!EVP_SignInit_ex(&mctx, digest, NULL)) goto error; if (!EVP_SignUpdate(&mctx, tbs, tbslen)) goto error; if (!EVP_SignFinal(&mctx, sig, &siglen, pkey)) goto error; if (kat && ((siglen != katlen) || memcmp(kat, sig, katlen))) goto error; if (!EVP_VerifyInit_ex(&mctx, digest, NULL)) goto error; if (!EVP_VerifyUpdate(&mctx, tbs, tbslen)) goto error; ret = EVP_VerifyFinal(&mctx, sig, siglen, pkey); error: if (sig != sigtmp) OPENSSL_free(sig); EVP_MD_CTX_cleanup(&mctx); if (ret != 1) { FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,FIPS_R_TEST_FAILURE); if (fail_str) ERR_add_error_data(2, "Type=", fail_str); return 0; } return 1; } /* Generalized symmetric cipher test routine. Encrypt data, verify result * against known answer, decrypt and compare with original plaintext. */ int fips_cipher_test(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, const unsigned char *key, const unsigned char *iv, const unsigned char *plaintext, const unsigned char *ciphertext, int len) { unsigned char pltmp[FIPS_MAX_CIPHER_TEST_SIZE]; unsigned char citmp[FIPS_MAX_CIPHER_TEST_SIZE]; OPENSSL_assert(len <= FIPS_MAX_CIPHER_TEST_SIZE); if (EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 1) <= 0) return 0; EVP_Cipher(ctx, citmp, plaintext, len); if (memcmp(citmp, ciphertext, len)) return 0; if (EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 0) <= 0) return 0; EVP_Cipher(ctx, pltmp, citmp, len); if (memcmp(pltmp, plaintext, len)) return 0; return 1; } #if 0 /* The purpose of this is to ensure the error code exists and the function * name is to keep the error checking script quiet */ void hash_final(void) { FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_NON_FIPS_METHOD); } #endif #endif