2004-05-11 12:46:24 +00:00
|
|
|
/* ====================================================================
|
|
|
|
* 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 <openssl/fips.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/fips_rand.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/hmac.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include "fips_locl.h"
|
|
|
|
|
|
|
|
#ifdef OPENSSL_FIPS
|
|
|
|
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 1024
|
|
|
|
#endif
|
|
|
|
|
2005-04-22 12:15:38 +00:00
|
|
|
static int fips_selftest_fail;
|
|
|
|
static int fips_mode;
|
|
|
|
static const void *fips_rand_check;
|
2005-04-17 23:26:40 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2004-07-30 14:38:02 +00:00
|
|
|
|
|
|
|
int FIPS_selftest_failed(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
if (fips_is_started())
|
|
|
|
{
|
|
|
|
int owning_thread = fips_is_owning_thread();
|
|
|
|
|
2005-04-17 23:26:40 +00:00
|
|
|
if (!owning_thread) fips_r_lock();
|
2004-07-30 14:38:02 +00:00
|
|
|
ret = fips_selftest_fail;
|
2005-04-17 23:26:40 +00:00
|
|
|
if (!owning_thread) fips_r_unlock();
|
2004-07-30 14:38:02 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2004-05-11 12:46:24 +00:00
|
|
|
|
|
|
|
int FIPS_selftest()
|
|
|
|
{
|
|
|
|
ERR_load_crypto_strings();
|
|
|
|
|
|
|
|
return FIPS_selftest_sha1()
|
2005-05-09 19:22:14 +00:00
|
|
|
&& FIPS_selftest_hmac()
|
2004-05-11 12:46:24 +00:00
|
|
|
&& FIPS_selftest_aes()
|
|
|
|
&& FIPS_selftest_des()
|
|
|
|
&& FIPS_selftest_rsa()
|
|
|
|
&& FIPS_selftest_dsa();
|
|
|
|
}
|
|
|
|
|
2006-01-21 14:01:30 +00:00
|
|
|
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";
|
2005-06-07 12:39:27 +00:00
|
|
|
|
2006-01-21 14:01:30 +00:00
|
|
|
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;
|
2005-06-07 12:39:27 +00:00
|
|
|
|
2006-01-21 14:01:30 +00:00
|
|
|
HMAC_CTX_init(&c);
|
|
|
|
HMAC_Init(&c,FIPS_hmac_key,strlen(FIPS_hmac_key),EVP_sha1());
|
2005-06-07 12:39:27 +00:00
|
|
|
|
2006-01-21 14:01:30 +00:00
|
|
|
/* 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;
|
2005-06-07 12:39:27 +00:00
|
|
|
|
2006-01-21 14:01:30 +00:00
|
|
|
if (p1)
|
|
|
|
HMAC_Update(&c,p1,(size_t)p2-(size_t)p1);
|
2005-06-07 12:39:27 +00:00
|
|
|
|
2006-01-21 14:01:30 +00:00
|
|
|
if (FIPS_signature>=p3 && FIPS_signature<p4)
|
2005-06-07 12:39:27 +00:00
|
|
|
{
|
2006-01-21 14:01:30 +00:00
|
|
|
/* "punch" hole */
|
|
|
|
HMAC_Update(&c,p3,(size_t)FIPS_signature-(size_t)p3);
|
|
|
|
p3 = FIPS_signature+sizeof(FIPS_signature);
|
|
|
|
if (p3<p4)
|
|
|
|
HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
|
2005-06-07 12:39:27 +00:00
|
|
|
}
|
2006-01-21 14:01:30 +00:00
|
|
|
else
|
|
|
|
HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
|
|
|
|
|
|
|
|
HMAC_Final(&c,sig,&len);
|
|
|
|
HMAC_CTX_cleanup(&c);
|
|
|
|
|
|
|
|
return len;
|
2005-06-07 12:39:27 +00:00
|
|
|
}
|
2006-01-21 14:01:30 +00:00
|
|
|
|
|
|
|
int FIPS_check_incore_fingerprint(void)
|
2004-05-11 12:46:24 +00:00
|
|
|
{
|
2006-01-21 14:01:30 +00:00
|
|
|
unsigned char sig[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int len;
|
|
|
|
extern int OPENSSL_NONPIC_relocated;
|
|
|
|
|
|
|
|
if (FIPS_text_start()==NULL)
|
2004-05-11 12:46:24 +00:00
|
|
|
{
|
2006-01-21 14:01:30 +00:00
|
|
|
FIPSerr(FIPS_F_FIPS_CHECK_FINGERPRINT,FIPS_R_UNSUPPORTED_PLATFORM);
|
2004-05-11 12:46:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-01-21 14:01:30 +00:00
|
|
|
|
|
|
|
len=FIPS_incore_fingerprint (sig,sizeof(sig));
|
|
|
|
|
|
|
|
if (len!=sizeof(FIPS_signature) ||
|
|
|
|
memcmp(FIPS_signature,sig,sizeof(FIPS_signature)))
|
2004-05-11 12:46:24 +00:00
|
|
|
{
|
2006-01-21 14:01:30 +00:00
|
|
|
if (FIPS_signature>=FIPS_rodata_start && FIPS_signature<FIPS_rodata_end)
|
|
|
|
FIPSerr(FIPS_F_FIPS_CHECK_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_SEGMENT_ALIASING);
|
|
|
|
else if (OPENSSL_NONPIC_relocated)
|
|
|
|
FIPSerr(FIPS_F_FIPS_CHECK_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_NONPIC_RELOCATED);
|
|
|
|
else
|
|
|
|
FIPSerr(FIPS_F_FIPS_CHECK_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
|
2004-05-11 12:46:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-01-21 14:01:30 +00:00
|
|
|
|
2004-05-11 12:46:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-01-21 14:01:30 +00:00
|
|
|
int FIPS_mode_set(int onoff)
|
2004-05-11 12:46:24 +00:00
|
|
|
{
|
2004-07-30 14:38:02 +00:00
|
|
|
int fips_set_owning_thread();
|
|
|
|
int fips_clear_owning_thread();
|
|
|
|
int ret = 0;
|
|
|
|
|
2005-04-17 23:26:40 +00:00
|
|
|
fips_w_lock();
|
2004-07-30 14:38:02 +00:00
|
|
|
fips_set_started();
|
|
|
|
fips_set_owning_thread();
|
|
|
|
|
2004-05-11 12:46:24 +00:00
|
|
|
if(onoff)
|
|
|
|
{
|
|
|
|
unsigned char buf[24];
|
|
|
|
|
2004-07-30 14:38:02 +00:00
|
|
|
fips_selftest_fail = 0;
|
2004-05-11 12:46:24 +00:00
|
|
|
|
|
|
|
/* Don't go into FIPS mode twice, just so we can do automagic
|
|
|
|
seeding */
|
2004-07-30 14:38:02 +00:00
|
|
|
if(FIPS_mode())
|
2004-05-11 12:46:24 +00:00
|
|
|
{
|
|
|
|
FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
|
2004-07-30 14:38:02 +00:00
|
|
|
fips_selftest_fail = 1;
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
2004-05-11 12:46:24 +00:00
|
|
|
}
|
|
|
|
|
2006-01-21 14:01:30 +00:00
|
|
|
if(fips_signature_witness() != FIPS_signature)
|
|
|
|
{
|
|
|
|
FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_CONTRADICTING_EVIDENCE);
|
|
|
|
fips_selftest_fail = 1;
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!FIPS_check_incore_fingerprint())
|
2004-05-11 12:46:24 +00:00
|
|
|
{
|
2004-07-30 14:38:02 +00:00
|
|
|
fips_selftest_fail = 1;
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
2004-05-11 12:46:24 +00:00
|
|
|
}
|
|
|
|
|
2005-05-07 22:06:40 +00:00
|
|
|
/* Perform RNG KAT before seeding */
|
|
|
|
if (!FIPS_selftest_rng())
|
|
|
|
{
|
|
|
|
fips_selftest_fail = 1;
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2004-05-11 12:46:24 +00:00
|
|
|
/* automagically seed PRNG if not already seeded */
|
|
|
|
if(!FIPS_rand_seeded())
|
|
|
|
{
|
2004-05-12 14:11:10 +00:00
|
|
|
if(RAND_bytes(buf,sizeof buf) <= 0)
|
|
|
|
{
|
2004-07-30 14:38:02 +00:00
|
|
|
fips_selftest_fail = 1;
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
2004-05-12 14:11:10 +00:00
|
|
|
}
|
2004-05-11 12:46:24 +00:00
|
|
|
FIPS_set_prng_key(buf,buf+8);
|
|
|
|
FIPS_rand_seed(buf+16,8);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now switch into FIPS mode */
|
2004-07-30 14:38:02 +00:00
|
|
|
fips_set_rand_check(FIPS_rand_method());
|
2004-05-11 12:46:24 +00:00
|
|
|
RAND_set_rand_method(FIPS_rand_method());
|
|
|
|
if(FIPS_selftest())
|
2004-07-30 14:38:02 +00:00
|
|
|
fips_set_mode(1);
|
2004-05-11 12:46:24 +00:00
|
|
|
else
|
|
|
|
{
|
2004-07-30 14:38:02 +00:00
|
|
|
fips_selftest_fail = 1;
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
2004-05-11 12:46:24 +00:00
|
|
|
}
|
2004-07-30 14:38:02 +00:00
|
|
|
ret = 1;
|
|
|
|
goto end;
|
2004-05-11 12:46:24 +00:00
|
|
|
}
|
2004-07-30 14:38:02 +00:00
|
|
|
fips_set_mode(0);
|
|
|
|
fips_selftest_fail = 0;
|
|
|
|
ret = 1;
|
|
|
|
end:
|
|
|
|
fips_clear_owning_thread();
|
2005-04-17 23:26:40 +00:00
|
|
|
fips_w_unlock();
|
2004-07-30 14:38:02 +00:00
|
|
|
return ret;
|
2004-05-11 12:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* here just to cause error codes to exist */
|
|
|
|
static void dummy()
|
|
|
|
{
|
|
|
|
FIPSerr(FIPS_F_HASH_FINAL,FIPS_F_NON_FIPS_METHOD);
|
|
|
|
FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_FIPS_SELFTEST_FAILED);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|