Blame SOURCES/openssl-1.0.1e-new-fips-reqs.patch

2b5643
diff -up openssl-1.0.1e/crypto/bn/bn_rand.c.fips-reqs openssl-1.0.1e/crypto/bn/bn_rand.c
2b5643
--- openssl-1.0.1e/crypto/bn/bn_rand.c.fips-reqs	2013-02-11 16:02:47.000000000 +0100
2b5643
+++ openssl-1.0.1e/crypto/bn/bn_rand.c	2014-02-13 16:50:34.280893285 +0100
2b5643
@@ -138,9 +138,12 @@ static int bnrand(int pseudorand, BIGNUM
2b5643
 		goto err;
2b5643
 		}
2b5643
 
2b5643
-	/* make a random number and set the top and bottom bits */
2b5643
-	time(&tim);
2b5643
-	RAND_add(&tim,sizeof(tim),0.0);
2b5643
+	if (!FIPS_mode()) /* in FIPS mode the RNG is always properly seeded or the module fails */
2b5643
+		{
2b5643
+		/* make a random number and set the top and bottom bits */
2b5643
+		time(&tim);
2b5643
+		RAND_add(&tim,sizeof(tim),0.0);
2b5643
+		}
2b5643
 
2b5643
 	if (pseudorand)
2b5643
 		{
2b5643
diff -up openssl-1.0.1e/crypto/dh/dh_gen.c.fips-reqs openssl-1.0.1e/crypto/dh/dh_gen.c
2b5643
--- openssl-1.0.1e/crypto/dh/dh_gen.c.fips-reqs	2013-12-18 12:17:09.748636614 +0100
2b5643
+++ openssl-1.0.1e/crypto/dh/dh_gen.c	2013-12-18 12:17:09.798637687 +0100
2b5643
@@ -125,7 +125,7 @@ static int dh_builtin_genparams(DH *ret,
2b5643
 		return 0;
2b5643
 		}
2b5643
 
2b5643
-	if (FIPS_mode() && (prime_len < OPENSSL_DH_FIPS_MIN_MODULUS_BITS))
2b5643
+	if (FIPS_mode() && (prime_len < OPENSSL_DH_FIPS_MIN_MODULUS_BITS_GEN))
2b5643
 		{
2b5643
 		DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_KEY_SIZE_TOO_SMALL);
2b5643
 		goto err;
2b5643
diff -up openssl-1.0.1e/crypto/dh/dh.h.fips-reqs openssl-1.0.1e/crypto/dh/dh.h
2b5643
--- openssl-1.0.1e/crypto/dh/dh.h.fips-reqs	2013-12-18 12:17:09.748636614 +0100
2b5643
+++ openssl-1.0.1e/crypto/dh/dh.h	2013-12-18 12:17:09.798637687 +0100
2b5643
@@ -78,6 +78,7 @@
2b5643
 #endif
2b5643
 
2b5643
 #define OPENSSL_DH_FIPS_MIN_MODULUS_BITS 1024
2b5643
+#define OPENSSL_DH_FIPS_MIN_MODULUS_BITS_GEN 2048
2b5643
 
2b5643
 #define DH_FLAG_CACHE_MONT_P     0x01
2b5643
 #define DH_FLAG_NO_EXP_CONSTTIME 0x02 /* new with 0.9.7h; the built-in DH
2b5643
diff -up openssl-1.0.1e/crypto/dh/dh_check.c.fips-reqs openssl-1.0.1e/crypto/dh/dh_check.c
2b5643
--- openssl-1.0.1e/crypto/dh/dh_check.c.fips-reqs	2013-02-11 16:26:04.000000000 +0100
2b5643
+++ openssl-1.0.1e/crypto/dh/dh_check.c	2013-12-18 12:17:09.799637708 +0100
2b5643
@@ -134,7 +134,33 @@ int DH_check_pub_key(const DH *dh, const
2b5643
 	BN_sub_word(q,1);
2b5643
 	if (BN_cmp(pub_key,q)>=0)
2b5643
 		*ret|=DH_CHECK_PUBKEY_TOO_LARGE;
2b5643
+#ifdef OPENSSL_FIPS
2b5643
+	if (FIPS_mode() && dh->q != NULL)
2b5643
+		{
2b5643
+		BN_CTX *ctx = NULL;
2b5643
 
2b5643
+		ctx = BN_CTX_new();
2b5643
+		if (ctx == NULL)
2b5643
+			goto err;
2b5643
+
2b5643
+		if (BN_mod_exp_mont(q, pub_key, dh->q, dh->p, ctx, NULL) <= 0)
2b5643
+			{
2b5643
+			BN_CTX_free(ctx);
2b5643
+			goto err;
2b5643
+			}
2b5643
+		if (!BN_is_one(q))
2b5643
+			{
2b5643
+			/* it would be more correct to add new return flag 
2b5643
+			 * for this test, but we do not want to do it
2b5643
+			 * so just error out
2b5643
+			 */
2b5643
+			BN_CTX_free(ctx);
2b5643
+			goto err;
2b5643
+			}
2b5643
+		
2b5643
+		BN_CTX_free(ctx);
2b5643
+		}
2b5643
+#endif
2b5643
 	ok = 1;
2b5643
 err:
2b5643
 	if (q != NULL) BN_free(q);
2b5643
diff -up openssl-1.0.1e/crypto/dsa/dsa_gen.c.fips-reqs openssl-1.0.1e/crypto/dsa/dsa_gen.c
2b5643
--- openssl-1.0.1e/crypto/dsa/dsa_gen.c.fips-reqs	2013-12-18 12:17:09.749636636 +0100
2b5643
+++ openssl-1.0.1e/crypto/dsa/dsa_gen.c	2013-12-18 12:17:09.799637708 +0100
2b5643
@@ -159,7 +159,6 @@ int dsa_builtin_paramgen(DSA *ret, size_
2b5643
 	    }
2b5643
 
2b5643
 	if (FIPS_module_mode() &&
2b5643
-	    (bits != 1024 || qbits != 160) &&
2b5643
 	    (bits != 2048 || qbits != 224) &&
2b5643
 	    (bits != 2048 || qbits != 256) &&
2b5643
 	    (bits != 3072 || qbits != 256))
2b5643
diff -up openssl-1.0.1e/crypto/dsa/dsa.h.fips-reqs openssl-1.0.1e/crypto/dsa/dsa.h
2b5643
--- openssl-1.0.1e/crypto/dsa/dsa.h.fips-reqs	2013-12-18 12:17:09.749636636 +0100
2b5643
+++ openssl-1.0.1e/crypto/dsa/dsa.h	2013-12-18 12:17:09.799637708 +0100
2b5643
@@ -89,6 +89,7 @@
2b5643
 #endif
2b5643
 
2b5643
 #define OPENSSL_DSA_FIPS_MIN_MODULUS_BITS 1024
2b5643
+#define OPENSSL_DSA_FIPS_MIN_MODULUS_BITS_GEN 2048
2b5643
 
2b5643
 #define DSA_FLAG_CACHE_MONT_P	0x01
2b5643
 #define DSA_FLAG_NO_EXP_CONSTTIME       0x02 /* new with 0.9.7h; the built-in DSA
2b5643
@@ -254,9 +255,9 @@ int	DSAparams_print_fp(FILE *fp, const D
2b5643
 int	DSA_print_fp(FILE *bp, const DSA *x, int off);
2b5643
 #endif
2b5643
 
2b5643
-#define DSS_prime_checks 50
2b5643
-/* Primality test according to FIPS PUB 186[-1], Appendix 2.1:
2b5643
- * 50 rounds of Rabin-Miller */
2b5643
+#define DSS_prime_checks 64
2b5643
+/* Primality test according to FIPS PUB 186[-4], Appendix 2.1:
2b5643
+ * 64 rounds of Rabin-Miller */
2b5643
 #define DSA_is_prime(n, callback, cb_arg) \
2b5643
 	BN_is_prime(n, DSS_prime_checks, callback, NULL, cb_arg)
2b5643
 
2b5643
diff -up openssl-1.0.1e/crypto/dsa/dsa_key.c.fips-reqs openssl-1.0.1e/crypto/dsa/dsa_key.c
2b5643
--- openssl-1.0.1e/crypto/dsa/dsa_key.c.fips-reqs	2013-12-18 12:17:09.797637665 +0100
2b5643
+++ openssl-1.0.1e/crypto/dsa/dsa_key.c	2013-12-18 12:17:09.799637708 +0100
2b5643
@@ -127,7 +127,7 @@ static int dsa_builtin_keygen(DSA *dsa)
2b5643
 
2b5643
 #ifdef OPENSSL_FIPS
2b5643
 	if (FIPS_mode() && !(dsa->flags & DSA_FLAG_NON_FIPS_ALLOW)
2b5643
-		&& (BN_num_bits(dsa->p) < OPENSSL_DSA_FIPS_MIN_MODULUS_BITS))
2b5643
+		&& (BN_num_bits(dsa->p) < OPENSSL_DSA_FIPS_MIN_MODULUS_BITS_GEN))
2b5643
 		{
2b5643
 		DSAerr(DSA_F_DSA_BUILTIN_KEYGEN, DSA_R_KEY_SIZE_TOO_SMALL);
2b5643
 		goto err;
2b5643
diff -up openssl-1.0.1e/crypto/fips/fips_dh_selftest.c.fips-reqs openssl-1.0.1e/crypto/fips/fips_dh_selftest.c
2b5643
--- openssl-1.0.1e/crypto/fips/fips_dh_selftest.c.fips-reqs	2013-12-18 17:06:36.575114314 +0100
2b5643
+++ openssl-1.0.1e/crypto/fips/fips_dh_selftest.c	2013-12-18 17:26:14.409036334 +0100
2b5643
@@ -0,0 +1,162 @@
2b5643
+/* ====================================================================
2b5643
+ * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
2b5643
+ * Copyright (c) 2013 Red Hat, Inc.
2b5643
+ *
2b5643
+ * Redistribution and use in source and binary forms, with or without
2b5643
+ * modification, are permitted provided that the following conditions
2b5643
+ * are met:
2b5643
+ *
2b5643
+ * 1. Redistributions of source code must retain the above copyright
2b5643
+ *    notice, this list of conditions and the following disclaimer. 
2b5643
+ *
2b5643
+ * 2. Redistributions in binary form must reproduce the above copyright
2b5643
+ *    notice, this list of conditions and the following disclaimer in
2b5643
+ *    the documentation and/or other materials provided with the
2b5643
+ *    distribution.
2b5643
+ *
2b5643
+ * 3. All advertising materials mentioning features or use of this
2b5643
+ *    software must display the following acknowledgment:
2b5643
+ *    "This product includes software developed by the OpenSSL Project
2b5643
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
2b5643
+ *
2b5643
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2b5643
+ *    endorse or promote products derived from this software without
2b5643
+ *    prior written permission. For written permission, please contact
2b5643
+ *    openssl-core@openssl.org.
2b5643
+ *
2b5643
+ * 5. Products derived from this software may not be called "OpenSSL"
2b5643
+ *    nor may "OpenSSL" appear in their names without prior written
2b5643
+ *    permission of the OpenSSL Project.
2b5643
+ *
2b5643
+ * 6. Redistributions of any form whatsoever must retain the following
2b5643
+ *    acknowledgment:
2b5643
+ *    "This product includes software developed by the OpenSSL Project
2b5643
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
2b5643
+ *
2b5643
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
2b5643
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2b5643
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2b5643
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
2b5643
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2b5643
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2b5643
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2b5643
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2b5643
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2b5643
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2b5643
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
2b5643
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
2b5643
+ *
2b5643
+ */
2b5643
+
2b5643
+#include <string.h>
2b5643
+#include <openssl/crypto.h>
2b5643
+#include <openssl/dh.h>
2b5643
+#include <openssl/fips.h>
2b5643
+#include <openssl/err.h>
2b5643
+#include <openssl/evp.h>
2b5643
+#include <openssl/bn.h>
2b5643
+#include "fips_locl.h"
2b5643
+
2b5643
+#ifdef OPENSSL_FIPS
2b5643
+
2b5643
+static const unsigned char dh_test_2048_p[] = {
2b5643
+	0xAE,0xEC,0xEE,0x22,0xFA,0x3A,0xA5,0x22,0xC0,0xDE,0x0F,0x09,
2b5643
+	0x7E,0x17,0xC0,0x05,0xF9,0xF1,0xE7,0xC6,0x87,0x14,0x6D,0x11,
2b5643
+	0xE7,0xAE,0xED,0x2F,0x72,0x59,0xC5,0xA9,0x9B,0xB8,0x02,0xA5,
2b5643
+	0xF3,0x69,0x70,0xD6,0xDD,0x90,0xF9,0x19,0x79,0xBE,0x60,0x8F,
2b5643
+	0x25,0x92,0x30,0x1C,0x51,0x51,0x38,0x26,0x82,0x25,0xE6,0xFC,
2b5643
+	0xED,0x65,0x96,0x8F,0x57,0xE5,0x53,0x8B,0x38,0x63,0xC7,0xCE,
2b5643
+	0xBC,0x1B,0x4D,0x18,0x2A,0x5B,0x04,0x3F,0x6A,0x3C,0x94,0x39,
2b5643
+	0xAE,0x36,0xD6,0x5E,0x0F,0xA2,0xCC,0xD0,0xD4,0xD5,0xC6,0x1E,
2b5643
+	0xF6,0xA0,0xF5,0x89,0x4E,0xB4,0x0B,0xA4,0xB3,0x2B,0x3D,0xE2,
2b5643
+	0x4E,0xE1,0x49,0x25,0x99,0x5F,0x32,0x16,0x33,0x32,0x1B,0x7A,
2b5643
+	0xA5,0x5C,0x6B,0x34,0x0D,0x39,0x99,0xDC,0xF0,0x76,0xE5,0x5A,
2b5643
+	0xD4,0x71,0x00,0xED,0x5A,0x73,0xFB,0xC8,0x01,0xAD,0x99,0xCF,
2b5643
+	0x99,0x52,0x7C,0x9C,0x64,0xC6,0x76,0x40,0x57,0xAF,0x59,0xD7,
2b5643
+	0x38,0x0B,0x40,0xDE,0x33,0x0D,0xB8,0x76,0xEC,0xA9,0xD8,0x73,
2b5643
+	0xF8,0xEF,0x26,0x66,0x06,0x27,0xDD,0x7C,0xA4,0x10,0x9C,0xA6,
2b5643
+	0xAA,0xF9,0x53,0x62,0x73,0x1D,0xBA,0x1C,0xF1,0x67,0xF4,0x35,
2b5643
+	0xED,0x6F,0x37,0x92,0xE8,0x4F,0x6C,0xBA,0x52,0x6E,0xA1,0xED,
2b5643
+	0xDA,0x9F,0x85,0x11,0x82,0x52,0x62,0x08,0x44,0xF1,0x30,0x03,
2b5643
+	0xC3,0x38,0x2C,0x79,0xBD,0xD4,0x43,0x45,0xEE,0x8E,0x50,0xFC,
2b5643
+	0x29,0x46,0x9A,0xFE,0x54,0x1A,0x19,0x8F,0x4B,0x84,0x08,0xDE,
2b5643
+	0x20,0x62,0x73,0xCC,0xDD,0x7E,0xF0,0xEF,0xA2,0xFD,0x86,0x58,
2b5643
+	0x4B,0xD8,0x37,0xEB
2b5643
+};
2b5643
+
2b5643
+static const unsigned char dh_test_2048_g[] = {
2b5643
+	0x02
2b5643
+};
2b5643
+
2b5643
+static const unsigned char dh_test_2048_pub_key[] = {
2b5643
+	0xA0,0x39,0x11,0x77,0x9A,0xC1,0x30,0x1F,0xBE,0x48,0xA7,0xAA,
2b5643
+	0xA0,0x84,0x54,0x64,0xAD,0x1B,0x70,0xFA,0x13,0x55,0x63,0xD2,
2b5643
+	0x1F,0x62,0x32,0x93,0x8E,0xC9,0x3E,0x09,0xA7,0x64,0xE4,0x12,
2b5643
+	0x6E,0x1B,0xF2,0x92,0x3B,0xB9,0xCB,0x56,0xEA,0x07,0x88,0xB5,
2b5643
+	0xA6,0xBC,0x16,0x1F,0x27,0xFE,0xD8,0xAA,0x40,0xB2,0xB0,0x2D,
2b5643
+	0x37,0x76,0xA6,0xA4,0x82,0x2C,0x0E,0x22,0x64,0x9D,0xCB,0xD1,
2b5643
+	0x00,0xB7,0x89,0x14,0x72,0x4E,0xBE,0x48,0x41,0xF8,0xB2,0x51,
2b5643
+	0x11,0x09,0x4B,0x22,0x01,0x23,0x39,0x96,0xE0,0x15,0xD7,0x9F,
2b5643
+	0x60,0xD1,0xB7,0xAE,0xFE,0x5F,0xDB,0xE7,0x03,0x17,0x97,0xA6,
2b5643
+	0x16,0x74,0xBD,0x53,0x81,0x19,0xC5,0x47,0x5E,0xCE,0x8D,0xED,
2b5643
+	0x45,0x5D,0x3C,0x00,0xA0,0x0A,0x68,0x6A,0xE0,0x8E,0x06,0x46,
2b5643
+	0x6F,0xD7,0xF9,0xDF,0x31,0x7E,0x77,0x44,0x0D,0x98,0xE0,0xCA,
2b5643
+	0x98,0x09,0x52,0x04,0x90,0xEA,0x6D,0xF4,0x30,0x69,0x8F,0xB1,
2b5643
+	0x9B,0xC1,0x43,0xDB,0xD5,0x8D,0xC8,0x8E,0xB6,0x0B,0x05,0xBE,
2b5643
+	0x0E,0xC5,0x99,0xC8,0x6E,0x4E,0xF3,0xCB,0xC3,0x5E,0x9B,0x53,
2b5643
+	0xF7,0x06,0x1C,0x4F,0xC7,0xB8,0x6E,0x30,0x18,0xCA,0x9B,0xB9,
2b5643
+	0xBC,0x5F,0x17,0x72,0x29,0x5A,0xE5,0xD9,0x96,0xB7,0x0B,0xF3,
2b5643
+	0x2D,0x8C,0xF1,0xE1,0x0E,0x0D,0x74,0xD5,0x9D,0xF0,0x06,0xA9,
2b5643
+	0xB4,0x95,0x63,0x76,0x46,0x55,0x48,0x82,0x39,0x90,0xEF,0x56,
2b5643
+	0x75,0x34,0xB8,0x34,0xC3,0x18,0x6E,0x1E,0xAD,0xE3,0x48,0x7E,
2b5643
+	0x93,0x2C,0x23,0xE7,0xF8,0x90,0x73,0xB1,0x77,0x80,0x67,0xA9,
2b5643
+	0x36,0x9E,0xDA,0xD2
2b5643
+};
2b5643
+
2b5643
+static const unsigned char dh_test_2048_priv_key[] = {
2b5643
+	0x0C,0x4B,0x30,0x89,0xD1,0xB8,0x62,0xCB,0x3C,0x43,0x64,0x91,
2b5643
+	0xF0,0x91,0x54,0x70,0xC5,0x27,0x96,0xE3,0xAC,0xBE,0xE8,0x00,
2b5643
+	0xEC,0x55,0xF6,0xCC
2b5643
+};
2b5643
+
2b5643
+int FIPS_selftest_dh()
2b5643
+	{
2b5643
+	DH *dh = NULL;
2b5643
+	int ret = 0;
2b5643
+	void *pub_key = NULL;
2b5643
+	int len;
2b5643
+
2b5643
+	dh = DH_new();
2b5643
+
2b5643
+	if(dh == NULL)
2b5643
+		goto err;
2b5643
+
2b5643
+	fips_load_key_component(dh, p, dh_test_2048);
2b5643
+	fips_load_key_component(dh, g, dh_test_2048);
2b5643
+	/* note that the private key is much shorter than normally used
2b5643
+	 * but still g ** priv_key > p
2b5643
+	 */
2b5643
+	fips_load_key_component(dh, priv_key, dh_test_2048);
2b5643
+
2b5643
+	if (DH_generate_key(dh) <= 0)
2b5643
+		goto err;
2b5643
+
2b5643
+	len = BN_num_bytes(dh->pub_key);
2b5643
+	if ((pub_key = OPENSSL_malloc(len)) == NULL)
2b5643
+		goto err;
2b5643
+	BN_bn2bin(dh->pub_key, pub_key);
2b5643
+
2b5643
+	if (len != sizeof(dh_test_2048_pub_key) ||
2b5643
+		memcmp(pub_key, dh_test_2048_pub_key, len) != 0)
2b5643
+		goto err;
2b5643
+
2b5643
+	ret = 1;
2b5643
+
2b5643
+	err:
2b5643
+	if (dh)
2b5643
+		DH_free(dh);
2b5643
+
2b5643
+	OPENSSL_free(pub_key);
2b5643
+	return ret;
2b5643
+	}
2b5643
+#endif
2b5643
diff -up openssl-1.0.1e/crypto/fips/fips_drbg_rand.c.fips-reqs openssl-1.0.1e/crypto/fips/fips_drbg_rand.c
2b5643
--- openssl-1.0.1e/crypto/fips/fips_drbg_rand.c.fips-reqs	2013-12-18 12:17:09.757636808 +0100
2b5643
+++ openssl-1.0.1e/crypto/fips/fips_drbg_rand.c	2013-12-18 18:53:33.263711297 +0100
2b5643
@@ -77,7 +77,8 @@ static int fips_drbg_bytes(unsigned char
2b5643
 	int rv = 0;
2b5643
 	unsigned char *adin = NULL;
2b5643
 	size_t adinlen = 0;
2b5643
-	CRYPTO_w_lock(CRYPTO_LOCK_RAND);
2b5643
+	int locked;
2b5643
+	locked = private_RAND_lock(1);
2b5643
 	do 
2b5643
 		{
2b5643
 		size_t rcnt;
2b5643
@@ -109,7 +110,8 @@ static int fips_drbg_bytes(unsigned char
2b5643
 	while (count);
2b5643
 	rv = 1;
2b5643
 	err:
2b5643
-	CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
2b5643
+	if (locked)
2b5643
+		private_RAND_lock(0);
2b5643
 	return rv;
2b5643
 	}
2b5643
 
2b5643
@@ -124,35 +126,51 @@ static int fips_drbg_status(void)
2b5643
 	{
2b5643
 	DRBG_CTX *dctx = &ossl_dctx;
2b5643
 	int rv;
2b5643
-	CRYPTO_r_lock(CRYPTO_LOCK_RAND);
2b5643
+	int locked;
2b5643
+	locked = private_RAND_lock(1);
2b5643
 	rv = dctx->status == DRBG_STATUS_READY ? 1 : 0;
2b5643
-	CRYPTO_r_unlock(CRYPTO_LOCK_RAND);
2b5643
+	if (locked)
2b5643
+		private_RAND_lock(0);
2b5643
 	return rv;
2b5643
 	}
2b5643
 
2b5643
 static void fips_drbg_cleanup(void)
2b5643
 	{
2b5643
 	DRBG_CTX *dctx = &ossl_dctx;
2b5643
-	CRYPTO_w_lock(CRYPTO_LOCK_RAND);
2b5643
+	int locked;
2b5643
+	locked = private_RAND_lock(1);
2b5643
 	FIPS_drbg_uninstantiate(dctx);
2b5643
-	CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
2b5643
+	if (locked)
2b5643
+		private_RAND_lock(0);
2b5643
 	}
2b5643
 
2b5643
 static int fips_drbg_seed(const void *seed, int seedlen)
2b5643
 	{
2b5643
 	DRBG_CTX *dctx = &ossl_dctx;
2b5643
+	int locked;
2b5643
+	int ret = 1;
2b5643
+
2b5643
+	locked = private_RAND_lock(1);
2b5643
 	if (dctx->rand_seed_cb)
2b5643
-		return dctx->rand_seed_cb(dctx, seed, seedlen);
2b5643
-	return 1;
2b5643
+		ret = dctx->rand_seed_cb(dctx, seed, seedlen);
2b5643
+	if (locked)
2b5643
+		private_RAND_lock(0);
2b5643
+	return ret;
2b5643
 	}
2b5643
 
2b5643
 static int fips_drbg_add(const void *seed, int seedlen,
2b5643
 					double add_entropy)
2b5643
 	{
2b5643
 	DRBG_CTX *dctx = &ossl_dctx;
2b5643
+	int locked;
2b5643
+	int ret = 1;
2b5643
+
2b5643
+	locked = private_RAND_lock(1);
2b5643
 	if (dctx->rand_add_cb)
2b5643
-		return dctx->rand_add_cb(dctx, seed, seedlen, add_entropy);
2b5643
-	return 1;
2b5643
+		ret = dctx->rand_add_cb(dctx, seed, seedlen, add_entropy);
2b5643
+	if (locked)
2b5643
+		private_RAND_lock(0);
2b5643
+	return ret;
2b5643
 	}
2b5643
 
2b5643
 static const RAND_METHOD rand_drbg_meth =
2b5643
diff -up openssl-1.0.1e/crypto/fips/fips.h.fips-reqs openssl-1.0.1e/crypto/fips/fips.h
2b5643
--- openssl-1.0.1e/crypto/fips/fips.h.fips-reqs	2013-12-18 12:17:09.000000000 +0100
2b5643
+++ openssl-1.0.1e/crypto/fips/fips.h	2013-12-18 17:13:00.928586689 +0100
2b5643
@@ -96,6 +96,7 @@ void FIPS_corrupt_dsa_keygen(void);
2b5643
 int FIPS_selftest_dsa(void);
2b5643
 int FIPS_selftest_ecdsa(void);
2b5643
 int FIPS_selftest_ecdh(void);
2b5643
+int FIPS_selftest_dh(void);
2b5643
 void FIPS_corrupt_rng(void);
2b5643
 void FIPS_rng_stick(void);
2b5643
 void FIPS_x931_stick(int onoff);
2b5643
diff -up openssl-1.0.1e/crypto/fips/fips_post.c.fips-reqs openssl-1.0.1e/crypto/fips/fips_post.c
2b5643
--- openssl-1.0.1e/crypto/fips/fips_post.c.fips-reqs	2013-12-18 12:17:09.000000000 +0100
2b5643
+++ openssl-1.0.1e/crypto/fips/fips_post.c	2013-12-18 17:12:26.721832716 +0100
2b5643
@@ -99,6 +99,8 @@ int FIPS_selftest(void)
2b5643
 		rv = 0;
2b5643
 	if (!FIPS_selftest_dsa())
2b5643
 		rv = 0;
2b5643
+	if (!FIPS_selftest_dh())
2b5643
+		rv = 0;
2b5643
 	if (!FIPS_selftest_ecdh())
2b5643
 		rv = 0;
2b5643
 	return rv;
2b5643
diff -up openssl-1.0.1e/crypto/fips/fips_rsa_selftest.c.fips-reqs openssl-1.0.1e/crypto/fips/fips_rsa_selftest.c
2b5643
--- openssl-1.0.1e/crypto/fips/fips_rsa_selftest.c.fips-reqs	2013-12-18 12:17:09.761636893 +0100
2b5643
+++ openssl-1.0.1e/crypto/fips/fips_rsa_selftest.c	2013-12-18 12:17:09.799637708 +0100
2b5643
@@ -340,6 +340,42 @@ static const unsigned char kat_RSA_X931_
2b5643
   0x60, 0x83, 0x18, 0x88, 0xA3, 0xF5, 0x59, 0xC3
2b5643
 };
2b5643
 
2b5643
+static int fips_rsa_encrypt_test(RSA *rsa, const unsigned char *plaintext, int ptlen)
2b5643
+	{
2b5643
+	unsigned char *ctbuf = NULL, *ptbuf = NULL;
2b5643
+	int ret = 0;
2b5643
+	int len;
2b5643
+
2b5643
+	ctbuf = OPENSSL_malloc(RSA_size(rsa));
2b5643
+	if (!ctbuf)
2b5643
+		goto err;
2b5643
+
2b5643
+	len = RSA_public_encrypt(ptlen, plaintext, ctbuf, rsa, RSA_PKCS1_PADDING);
2b5643
+	if (len <= 0)
2b5643
+		goto err;
2b5643
+	/* Check ciphertext doesn't match plaintext */
2b5643
+	if (len >= ptlen && !memcmp(plaintext, ctbuf, ptlen))
2b5643
+		goto err;
2b5643
+
2b5643
+	ptbuf = OPENSSL_malloc(RSA_size(rsa));
2b5643
+	if (!ptbuf)
2b5643
+		goto err;
2b5643
+
2b5643
+	len = RSA_private_decrypt(len, ctbuf, ptbuf, rsa, RSA_PKCS1_PADDING);
2b5643
+	if (len != ptlen)
2b5643
+		goto err;
2b5643
+	if (memcmp(ptbuf, plaintext, len))
2b5643
+		goto err;
2b5643
+
2b5643
+	ret = 1;
2b5643
+
2b5643
+	err:
2b5643
+	if (ctbuf)
2b5643
+		OPENSSL_free(ctbuf);
2b5643
+	if (ptbuf)
2b5643
+		OPENSSL_free(ptbuf);
2b5643
+	return ret;
2b5643
+	}
2b5643
 
2b5643
 int FIPS_selftest_rsa()
2b5643
 	{
2b5643
@@ -353,7 +389,7 @@ int FIPS_selftest_rsa()
2b5643
 	if ((pk=EVP_PKEY_new()) == NULL)
2b5643
 		goto err;
2b5643
 
2b5643
-	EVP_PKEY_assign_RSA(pk, key);
2b5643
+	EVP_PKEY_set1_RSA(pk, key);
2b5643
 
2b5643
 	if (!fips_pkey_signature_test(pk, kat_tbs, sizeof(kat_tbs) - 1,
2b5643
 				kat_RSA_SHA1, sizeof(kat_RSA_SHA1),
2b5643
@@ -430,13 +466,15 @@ int FIPS_selftest_rsa()
2b5643
 			"RSA SHA512 X931"))
2b5643
 		goto err;
2b5643
 
2b5643
+	if (!fips_rsa_encrypt_test(key, kat_tbs, sizeof(kat_tbs) - 1))
2b5643
+		goto err;
2b5643
 
2b5643
 	ret = 1;
2b5643
 
2b5643
 	err:
2b5643
 	if (pk)
2b5643
 		EVP_PKEY_free(pk);
2b5643
-	else if (key)
2b5643
+	if (key)
2b5643
 		RSA_free(key);
2b5643
 	return ret;
2b5643
 	}
2b5643
diff -up openssl-1.0.1e/crypto/fips/Makefile.fips-reqs openssl-1.0.1e/crypto/fips/Makefile
2b5643
--- openssl-1.0.1e/crypto/fips/Makefile.fips-reqs	2013-12-18 12:17:20.000000000 +0100
2b5643
+++ openssl-1.0.1e/crypto/fips/Makefile	2013-12-18 17:14:20.348337362 +0100
2b5643
@@ -24,13 +24,15 @@ LIBSRC=fips_aes_selftest.c fips_des_self
2b5643
     fips_rsa_selftest.c fips_sha_selftest.c fips.c fips_dsa_selftest.c  fips_rand.c \
2b5643
     fips_rsa_x931g.c fips_post.c fips_drbg_ctr.c fips_drbg_hash.c fips_drbg_hmac.c \
2b5643
     fips_drbg_lib.c fips_drbg_rand.c fips_drbg_selftest.c fips_rand_lib.c \
2b5643
-    fips_cmac_selftest.c fips_ecdh_selftest.c fips_ecdsa_selftest.c fips_enc.c fips_md.c
2b5643
+    fips_cmac_selftest.c fips_ecdh_selftest.c fips_ecdsa_selftest.c fips_enc.c fips_md.c \
2b5643
+    fips_dh_selftest.c
2b5643
 
2b5643
 LIBOBJ=fips_aes_selftest.o fips_des_selftest.o fips_hmac_selftest.o fips_rand_selftest.o \
2b5643
     fips_rsa_selftest.o fips_sha_selftest.o fips.o fips_dsa_selftest.o  fips_rand.o \
2b5643
     fips_rsa_x931g.o fips_post.o fips_drbg_ctr.o fips_drbg_hash.o fips_drbg_hmac.o \
2b5643
     fips_drbg_lib.o fips_drbg_rand.o fips_drbg_selftest.o fips_rand_lib.o \
2b5643
-    fips_cmac_selftest.o fips_ecdh_selftest.o fips_ecdsa_selftest.o fips_enc.o fips_md.o
2b5643
+    fips_cmac_selftest.o fips_ecdh_selftest.o fips_ecdsa_selftest.o fips_enc.o fips_md.o \
2b5643
+    fips_dh_selftest.o
2b5643
 
2b5643
 LIBCRYPTO=-L.. -lcrypto
2b5643
 
2b5643
diff -up openssl-1.0.1e/crypto/modes/gcm128.c.fips-reqs openssl-1.0.1e/crypto/modes/gcm128.c
2b5643
--- openssl-1.0.1e/crypto/modes/gcm128.c.fips-reqs	2013-02-11 16:26:04.000000000 +0100
2b5643
+++ openssl-1.0.1e/crypto/modes/gcm128.c	2013-12-18 12:17:09.800637730 +0100
2b5643
@@ -898,6 +898,10 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT
2b5643
 # endif
2b5643
 #endif
2b5643
 
2b5643
+	ctx->totlen += len;
2b5643
+	if (ctx->totlen>(U64(1)<<36) || (sizeof(len)==8 && ctx->totlen
2b5643
+		return -1;
2b5643
+
2b5643
 #if 0
2b5643
 	n = (unsigned int)mlen%16; /* alternative to ctx->mres */
2b5643
 #endif
2b5643
@@ -1200,6 +1204,10 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_C
2b5643
 # endif
2b5643
 #endif
2b5643
 
2b5643
+	ctx->totlen += len;
2b5643
+	if (ctx->totlen>(U64(1)<<36) || (sizeof(len)==8 && ctx->totlen
2b5643
+		return -1;
2b5643
+
2b5643
 	mlen += len;
2b5643
 	if (mlen>((U64(1)<<36)-32) || (sizeof(len)==8 && mlen
2b5643
 		return -1;
2b5643
diff -up openssl-1.0.1e/crypto/modes/modes_lcl.h.fips-reqs openssl-1.0.1e/crypto/modes/modes_lcl.h
2b5643
--- openssl-1.0.1e/crypto/modes/modes_lcl.h.fips-reqs	2013-12-18 12:17:09.388628895 +0100
2b5643
+++ openssl-1.0.1e/crypto/modes/modes_lcl.h	2013-12-18 12:17:09.800637730 +0100
2b5643
@@ -115,6 +115,7 @@ struct gcm128_context {
2b5643
 	unsigned int mres, ares;
2b5643
 	block128_f block;
2b5643
 	void *key;
2b5643
+	u64 totlen;
2b5643
 };
2b5643
 
2b5643
 struct xts128_context {
2b5643
diff -up openssl-1.0.1e/crypto/rand/md_rand.c.fips-reqs openssl-1.0.1e/crypto/rand/md_rand.c
2b5643
--- openssl-1.0.1e/crypto/rand/md_rand.c.fips-reqs	2013-12-18 12:17:09.763636936 +0100
2b5643
+++ openssl-1.0.1e/crypto/rand/md_rand.c	2013-12-18 12:17:09.800637730 +0100
2b5643
@@ -143,12 +143,6 @@ static long md_count[2]={0,0};
2b5643
 static double entropy=0;
2b5643
 static int initialized=0;
2b5643
 
2b5643
-static unsigned int crypto_lock_rand = 0; /* may be set only when a thread
2b5643
-                                           * holds CRYPTO_LOCK_RAND
2b5643
-                                           * (to prevent double locking) */
2b5643
-/* access to lockin_thread is synchronized by CRYPTO_LOCK_RAND2 */
2b5643
-static CRYPTO_THREADID locking_threadid; /* valid iff crypto_lock_rand is set */
2b5643
-
2b5643
 
2b5643
 #ifdef PREDICT
2b5643
 int rand_predictable=0;
2b5643
@@ -196,7 +190,7 @@ static void ssleay_rand_add(const void *
2b5643
 	long md_c[2];
2b5643
 	unsigned char local_md[MD_DIGEST_LENGTH];
2b5643
 	EVP_MD_CTX m;
2b5643
-	int do_not_lock;
2b5643
+	int locked;
2b5643
 
2b5643
 	/*
2b5643
 	 * (Based on the rand(3) manpage)
2b5643
@@ -213,19 +207,8 @@ static void ssleay_rand_add(const void *
2b5643
          * hash function.
2b5643
 	 */
2b5643
 
2b5643
-	/* check if we already have the lock */
2b5643
-	if (crypto_lock_rand)
2b5643
-		{
2b5643
-		CRYPTO_THREADID cur;
2b5643
-		CRYPTO_THREADID_current(&cur);
2b5643
-		CRYPTO_r_lock(CRYPTO_LOCK_RAND2);
2b5643
-		do_not_lock = !CRYPTO_THREADID_cmp(&locking_threadid, &cur);
2b5643
-		CRYPTO_r_unlock(CRYPTO_LOCK_RAND2);
2b5643
-		}
2b5643
-	else
2b5643
-		do_not_lock = 0;
2b5643
+	locked = private_RAND_lock(1);
2b5643
 
2b5643
-	if (!do_not_lock) CRYPTO_w_lock(CRYPTO_LOCK_RAND);
2b5643
 	st_idx=state_index;
2b5643
 
2b5643
 	/* use our own copies of the counters so that even
2b5643
@@ -257,7 +240,8 @@ static void ssleay_rand_add(const void *
2b5643
 
2b5643
 	md_count[1] += (num / MD_DIGEST_LENGTH) + (num % MD_DIGEST_LENGTH > 0);
2b5643
 
2b5643
-	if (!do_not_lock) CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
2b5643
+	if (locked)
2b5643
+		private_RAND_lock(0);
2b5643
 
2b5643
 	EVP_MD_CTX_init(&m);
2b5643
 	for (i=0; i
2b5643
@@ -308,7 +292,7 @@ static void ssleay_rand_add(const void *
2b5643
 		}
2b5643
 	EVP_MD_CTX_cleanup(&m);
2b5643
 
2b5643
-	if (!do_not_lock) CRYPTO_w_lock(CRYPTO_LOCK_RAND);
2b5643
+	locked = private_RAND_lock(1);
2b5643
 	/* Don't just copy back local_md into md -- this could mean that
2b5643
 	 * other thread's seeding remains without effect (except for
2b5643
 	 * the incremented counter).  By XORing it we keep at least as
2b5643
@@ -319,7 +303,8 @@ static void ssleay_rand_add(const void *
2b5643
 		}
2b5643
 	if (entropy < ENTROPY_NEEDED) /* stop counting when we have enough */
2b5643
 	    entropy += add;
2b5643
-	if (!do_not_lock) CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
2b5643
+	if (locked)
2b5643
+		private_RAND_lock(0);
2b5643
 	
2b5643
 #if !defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32)
2b5643
 	assert(md_c[1] == md_count[1]);
2b5643
@@ -344,6 +329,7 @@ static int ssleay_rand_bytes(unsigned ch
2b5643
 	pid_t curr_pid = getpid();
2b5643
 #endif
2b5643
 	int do_stir_pool = 0;
2b5643
+	int locked;
2b5643
 
2b5643
 #ifdef PREDICT
2b5643
 	if (rand_predictable)
2b5643
@@ -381,13 +367,7 @@ static int ssleay_rand_bytes(unsigned ch
2b5643
 	 * global 'md'.
2b5643
 	 */
2b5643
 
2b5643
-	CRYPTO_w_lock(CRYPTO_LOCK_RAND);
2b5643
-
2b5643
-	/* prevent ssleay_rand_bytes() from trying to obtain the lock again */
2b5643
-	CRYPTO_w_lock(CRYPTO_LOCK_RAND2);
2b5643
-	CRYPTO_THREADID_current(&locking_threadid);
2b5643
-	CRYPTO_w_unlock(CRYPTO_LOCK_RAND2);
2b5643
-	crypto_lock_rand = 1;
2b5643
+	locked = private_RAND_lock(1);
2b5643
 
2b5643
 	/* always poll for external entropy in FIPS mode, drbg provides the 
2b5643
 	 * expansion
2b5643
@@ -461,9 +441,8 @@ static int ssleay_rand_bytes(unsigned ch
2b5643
 
2b5643
 	md_count[0] += 1;
2b5643
 
2b5643
-	/* before unlocking, we must clear 'crypto_lock_rand' */
2b5643
-	crypto_lock_rand = 0;
2b5643
-	CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
2b5643
+	if (locked)
2b5643
+		private_RAND_lock(0);
2b5643
 
2b5643
 	while (num > 0)
2b5643
 		{
2b5643
@@ -515,10 +494,11 @@ static int ssleay_rand_bytes(unsigned ch
2b5643
 	MD_Init(&m);
2b5643
 	MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c));
2b5643
 	MD_Update(&m,local_md,MD_DIGEST_LENGTH);
2b5643
-	CRYPTO_w_lock(CRYPTO_LOCK_RAND);
2b5643
+	locked = private_RAND_lock(1);
2b5643
 	MD_Update(&m,md,MD_DIGEST_LENGTH);
2b5643
 	MD_Final(&m,md);
2b5643
-	CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
2b5643
+	if (locked)
2b5643
+		private_RAND_lock(0);
2b5643
 
2b5643
 	EVP_MD_CTX_cleanup(&m);
2b5643
 	if (ok)
2b5643
@@ -548,32 +528,10 @@ static int ssleay_rand_pseudo_bytes(unsi
2b5643
 
2b5643
 static int ssleay_rand_status(void)
2b5643
 	{
2b5643
-	CRYPTO_THREADID cur;
2b5643
 	int ret;
2b5643
-	int do_not_lock;
2b5643
+	int locked;
2b5643
 
2b5643
-	CRYPTO_THREADID_current(&cur);
2b5643
-	/* check if we already have the lock
2b5643
-	 * (could happen if a RAND_poll() implementation calls RAND_status()) */
2b5643
-	if (crypto_lock_rand)
2b5643
-		{
2b5643
-		CRYPTO_r_lock(CRYPTO_LOCK_RAND2);
2b5643
-		do_not_lock = !CRYPTO_THREADID_cmp(&locking_threadid, &cur);
2b5643
-		CRYPTO_r_unlock(CRYPTO_LOCK_RAND2);
2b5643
-		}
2b5643
-	else
2b5643
-		do_not_lock = 0;
2b5643
-	
2b5643
-	if (!do_not_lock)
2b5643
-		{
2b5643
-		CRYPTO_w_lock(CRYPTO_LOCK_RAND);
2b5643
-		
2b5643
-		/* prevent ssleay_rand_bytes() from trying to obtain the lock again */
2b5643
-		CRYPTO_w_lock(CRYPTO_LOCK_RAND2);
2b5643
-		CRYPTO_THREADID_cpy(&locking_threadid, &cur);
2b5643
-		CRYPTO_w_unlock(CRYPTO_LOCK_RAND2);
2b5643
-		crypto_lock_rand = 1;
2b5643
-		}
2b5643
+	locked = private_RAND_lock(1);
2b5643
 	
2b5643
 	if (!initialized)
2b5643
 		{
2b5643
@@ -583,13 +541,8 @@ static int ssleay_rand_status(void)
2b5643
 
2b5643
 	ret = entropy >= ENTROPY_NEEDED;
2b5643
 
2b5643
-	if (!do_not_lock)
2b5643
-		{
2b5643
-		/* before unlocking, we must clear 'crypto_lock_rand' */
2b5643
-		crypto_lock_rand = 0;
2b5643
-		
2b5643
-		CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
2b5643
-		}
2b5643
+	if (locked)
2b5643
+		private_RAND_lock(0);
2b5643
 	
2b5643
 	return ret;
2b5643
 	}
2b5643
diff -up openssl-1.0.1e/crypto/rand/rand.h.fips-reqs openssl-1.0.1e/crypto/rand/rand.h
2b5643
--- openssl-1.0.1e/crypto/rand/rand.h.fips-reqs	2013-12-18 12:17:09.764636958 +0100
2b5643
+++ openssl-1.0.1e/crypto/rand/rand.h	2013-12-18 12:17:09.800637730 +0100
2b5643
@@ -124,6 +124,8 @@ void RAND_set_fips_drbg_type(int type, i
2b5643
 int RAND_init_fips(void);
2b5643
 #endif
2b5643
 
2b5643
+int private_RAND_lock(int lock);
2b5643
+
2b5643
 /* BEGIN ERROR CODES */
2b5643
 /* The following lines are auto generated by the script mkerr.pl. Any changes
2b5643
  * made after this point may be overwritten when the script is next run.
2b5643
diff -up openssl-1.0.1e/crypto/rand/rand_lcl.h.fips-reqs openssl-1.0.1e/crypto/rand/rand_lcl.h
2b5643
--- openssl-1.0.1e/crypto/rand/rand_lcl.h.fips-reqs	2013-12-18 12:17:09.507631447 +0100
2b5643
+++ openssl-1.0.1e/crypto/rand/rand_lcl.h	2013-12-18 12:17:09.800637730 +0100
2b5643
@@ -112,7 +112,7 @@
2b5643
 #ifndef HEADER_RAND_LCL_H
2b5643
 #define HEADER_RAND_LCL_H
2b5643
 
2b5643
-#define ENTROPY_NEEDED 32  /* require 256 bits = 32 bytes of randomness */
2b5643
+#define ENTROPY_NEEDED 48  /* require 384 bits = 48 bytes of randomness */
2b5643
 
2b5643
 
2b5643
 #if !defined(USE_MD5_RAND) && !defined(USE_SHA1_RAND) && !defined(USE_MDC2_RAND) && !defined(USE_MD2_RAND)
2b5643
diff -up openssl-1.0.1e/crypto/rand/rand_lib.c.fips-reqs openssl-1.0.1e/crypto/rand/rand_lib.c
2b5643
--- openssl-1.0.1e/crypto/rand/rand_lib.c.fips-reqs	2013-02-11 16:26:04.000000000 +0100
2b5643
+++ openssl-1.0.1e/crypto/rand/rand_lib.c	2013-12-18 18:16:45.625850730 +0100
2b5643
@@ -181,6 +181,41 @@ int RAND_status(void)
2b5643
 	return 0;
2b5643
 	}
2b5643
 
2b5643
+int private_RAND_lock(int lock)
2b5643
+	{
2b5643
+	static int crypto_lock_rand;
2b5643
+	static CRYPTO_THREADID locking_threadid;
2b5643
+	int do_lock;
2b5643
+
2b5643
+	if (!lock)
2b5643
+		{
2b5643
+		crypto_lock_rand = 0;
2b5643
+		CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
2b5643
+		return 0;
2b5643
+		}
2b5643
+
2b5643
+	/* check if we already have the lock */
2b5643
+	if (crypto_lock_rand)
2b5643
+		{
2b5643
+		CRYPTO_THREADID cur;
2b5643
+		CRYPTO_THREADID_current(&cur);
2b5643
+		CRYPTO_r_lock(CRYPTO_LOCK_RAND2);
2b5643
+		do_lock = !!CRYPTO_THREADID_cmp(&locking_threadid, &cur);
2b5643
+		CRYPTO_r_unlock(CRYPTO_LOCK_RAND2);
2b5643
+		}
2b5643
+        else
2b5643
+		do_lock = 1;
2b5643
+	if (do_lock)
2b5643
+		{
2b5643
+		CRYPTO_w_lock(CRYPTO_LOCK_RAND);
2b5643
+		crypto_lock_rand = 1;
2b5643
+		CRYPTO_w_lock(CRYPTO_LOCK_RAND2);
2b5643
+		CRYPTO_THREADID_current(&locking_threadid);
2b5643
+		CRYPTO_w_unlock(CRYPTO_LOCK_RAND2);
2b5643
+		}
2b5643
+	return do_lock;
2b5643
+	}
2b5643
+
2b5643
 #ifdef OPENSSL_FIPS
2b5643
 
2b5643
 /* FIPS DRBG initialisation code. This sets up the DRBG for use by the
2b5643
@@ -239,12 +274,16 @@ static int drbg_rand_add(DRBG_CTX *ctx,
2b5643
 				double entropy)
2b5643
 	{
2b5643
 	RAND_SSLeay()->add(in, inlen, entropy);
2b5643
+	if (FIPS_rand_status())
2b5643
+		FIPS_drbg_reseed(ctx, NULL, 0);
2b5643
 	return 1;
2b5643
 	}
2b5643
 
2b5643
 static int drbg_rand_seed(DRBG_CTX *ctx, const void *in, int inlen)
2b5643
 	{
2b5643
 	RAND_SSLeay()->seed(in, inlen);
2b5643
+	if (FIPS_rand_status())
2b5643
+		FIPS_drbg_reseed(ctx, NULL, 0);
2b5643
 	return 1;
2b5643
 	}
2b5643
 
2b5643
diff -up openssl-1.0.1e/crypto/rsa/rsa_gen.c.fips-reqs openssl-1.0.1e/crypto/rsa/rsa_gen.c
2b5643
--- openssl-1.0.1e/crypto/rsa/rsa_gen.c.fips-reqs	2013-12-18 12:17:09.764636958 +0100
2b5643
+++ openssl-1.0.1e/crypto/rsa/rsa_gen.c	2013-12-19 17:40:58.483154314 +0100
2b5643
@@ -1,5 +1,6 @@
2b5643
 /* crypto/rsa/rsa_gen.c */
2b5643
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2b5643
+ * Copyright (C) 2013 Red Hat, Inc.
2b5643
  * All rights reserved.
2b5643
  *
2b5643
  * This package is an SSL implementation written
2b5643
@@ -165,6 +166,222 @@ int RSA_generate_key_ex(RSA *rsa, int bi
2b5643
 	return rsa_builtin_keygen(rsa, bits, e_value, cb);
2b5643
 	}
2b5643
 
2b5643
+#ifdef OPENSSL_FIPS
2b5643
+static int FIPS_rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
2b5643
+	{
2b5643
+	BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
2b5643
+	BIGNUM local_r0, local_d, local_p;
2b5643
+	BIGNUM *pr0, *d, *p;
2b5643
+	BN_CTX *ctx = NULL;
2b5643
+	int ok = -1;
2b5643
+	int i;
2b5643
+        int n = 0;
2b5643
+	int test = 0;
2b5643
+        int pbits = bits/2;
2b5643
+
2b5643
+	if(FIPS_selftest_failed())
2b5643
+		{
2b5643
+		FIPSerr(FIPS_F_RSA_BUILTIN_KEYGEN, FIPS_R_FIPS_SELFTEST_FAILED);
2b5643
+	    	return 0;
2b5643
+	    	}
2b5643
+
2b5643
+	if (bits != 2048 && bits != 3072)
2b5643
+		{
2b5643
+		FIPSerr(FIPS_F_RSA_BUILTIN_KEYGEN, FIPS_R_INVALID_KEY_LENGTH);
2b5643
+		return 0;
2b5643
+		}
2b5643
+
2b5643
+	ctx = BN_CTX_new();
2b5643
+	if (ctx == NULL) goto err;
2b5643
+	BN_CTX_start(ctx);
2b5643
+	r0 = BN_CTX_get(ctx);
2b5643
+	r1 = BN_CTX_get(ctx);
2b5643
+	r2 = BN_CTX_get(ctx);
2b5643
+	r3 = BN_CTX_get(ctx);
2b5643
+
2b5643
+	if (r3 == NULL) goto err;
2b5643
+
2b5643
+	/* We need the RSA components non-NULL */
2b5643
+	if (!rsa->n && ((rsa->n=BN_new()) == NULL)) goto err;
2b5643
+	if (!rsa->d && ((rsa->d=BN_new()) == NULL)) goto err;
2b5643
+	if (!rsa->e && ((rsa->e=BN_new()) == NULL)) goto err;
2b5643
+	if (!rsa->p && ((rsa->p=BN_new()) == NULL)) goto err;
2b5643
+	if (!rsa->q && ((rsa->q=BN_new()) == NULL)) goto err;
2b5643
+	if (!rsa->dmp1 && ((rsa->dmp1=BN_new()) == NULL)) goto err;
2b5643
+	if (!rsa->dmq1 && ((rsa->dmq1=BN_new()) == NULL)) goto err;
2b5643
+	if (!rsa->iqmp && ((rsa->iqmp=BN_new()) == NULL)) goto err;
2b5643
+
2b5643
+	if (!BN_set_word(r0, RSA_F4)) goto err;
2b5643
+	if (BN_cmp(e_value, r0) < 0 || BN_num_bits(e_value) > 256)
2b5643
+		{
2b5643
+		ok = 0; /* we set our own err */
2b5643
+		RSAerr(RSA_F_RSA_BUILTIN_KEYGEN,RSA_R_BAD_E_VALUE);
2b5643
+		goto err;
2b5643
+		}
2b5643
+
2b5643
+	/* prepare approximate minimum p and q */
2b5643
+	if (!BN_set_word(r0, 0xB504F334)) goto err;
2b5643
+	if (!BN_lshift(r0, r0, pbits - 32)) goto err;
2b5643
+
2b5643
+	/* prepare minimum p and q difference */
2b5643
+	if (!BN_one(r3)) goto err;
2b5643
+	if (!BN_lshift(r3, r3, pbits - 100)) goto err;
2b5643
+
2b5643
+	BN_copy(rsa->e, e_value);
2b5643
+
2b5643
+	if (!BN_is_zero(rsa->p) && !BN_is_zero(rsa->q))
2b5643
+		test = 1;
2b5643
+
2b5643
+	/* generate p and q */
2b5643
+	for (i = 0; i < 5 * pbits; i++)
2b5643
+		{
2b5643
+        ploop:
2b5643
+                if (!test)
2b5643
+                        if (!BN_rand(rsa->p, pbits, 0, 1)) goto err;
2b5643
+                if (BN_cmp(rsa->p, r0) < 0)
2b5643
+                        {
2b5643
+                        if (test) goto err;
2b5643
+                        goto ploop;
2b5643
+                        }
2b5643
+
2b5643
+		if (!BN_sub(r2, rsa->p, BN_value_one())) goto err;
2b5643
+		if (!BN_gcd(r1, r2, rsa->e, ctx)) goto err;
2b5643
+		if (BN_is_one(r1))
2b5643
+                        {
2b5643
+                        int r;
2b5643
+                        r = BN_is_prime_fasttest_ex(rsa->p, pbits>1024?4:5, ctx, 0, cb);
2b5643
+        		if (r == -1 || (test && r <= 0)) goto err;
2b5643
+        		if (r > 0) break;
2b5643
+                        } 
2b5643
+
2b5643
+		if(!BN_GENCB_call(cb, 2, n++))
2b5643
+			goto err;
2b5643
+		}
2b5643
+
2b5643
+	if(!BN_GENCB_call(cb, 3, 0))
2b5643
+		goto err;
2b5643
+
2b5643
+        if(i >= 5*pbits)
2b5643
+                /* prime not found */
2b5643
+                goto err;
2b5643
+
2b5643
+	for (i = 0; i < 5 * pbits; i++)
2b5643
+		{
2b5643
+        qloop:
2b5643
+                if (!test)
2b5643
+                        if (!BN_rand(rsa->q, pbits, 0, 1)) goto err;
2b5643
+                if (BN_cmp(rsa->q, r0) < 0)
2b5643
+                        {
2b5643
+                        if (test) goto err;
2b5643
+                        goto qloop;
2b5643
+                        }
2b5643
+       		if (!BN_sub(r2, rsa->q, rsa->p)) goto err;
2b5643
+                if (BN_ucmp(r2, r3) <= 0)
2b5643
+                        {
2b5643
+                        if (test) goto err;
2b5643
+                        goto qloop;
2b5643
+                        }
2b5643
+
2b5643
+		if (!BN_sub(r2, rsa->q, BN_value_one())) goto err;
2b5643
+		if (!BN_gcd(r1, r2, rsa->e, ctx)) goto err;
2b5643
+		if (BN_is_one(r1))
2b5643
+                        {
2b5643
+                        int r;
2b5643
+                        r = BN_is_prime_fasttest_ex(rsa->q, pbits>1024?4:5, ctx, 0, cb);
2b5643
+        		if (r == -1 || (test && r <= 0)) goto err;
2b5643
+        		if (r > 0) break;
2b5643
+                        } 
2b5643
+
2b5643
+		if(!BN_GENCB_call(cb, 2, n++))
2b5643
+			goto err;
2b5643
+		}
2b5643
+
2b5643
+	if(!BN_GENCB_call(cb, 3, 1))
2b5643
+		goto err;
2b5643
+
2b5643
+        if(i >= 5*pbits)
2b5643
+                /* prime not found */
2b5643
+                goto err;
2b5643
+
2b5643
+        if (test)
2b5643
+                {
2b5643
+                /* do not try to calculate the remaining key values */
2b5643
+                BN_clear(rsa->n);
2b5643
+                ok = 1;
2b5643
+                goto err;
2b5643
+                }
2b5643
+
2b5643
+	if (BN_cmp(rsa->p,rsa->q) < 0)
2b5643
+		{
2b5643
+		tmp=rsa->p;
2b5643
+		rsa->p=rsa->q;
2b5643
+		rsa->q=tmp;
2b5643
+		}
2b5643
+
2b5643
+	/* calculate n */
2b5643
+	if (!BN_mul(rsa->n,rsa->p,rsa->q,ctx)) goto err;
2b5643
+
2b5643
+	/* calculate d */
2b5643
+	if (!BN_sub(r1,rsa->p,BN_value_one())) goto err;	/* p-1 */
2b5643
+	if (!BN_sub(r2,rsa->q,BN_value_one())) goto err;	/* q-1 */
2b5643
+	if (!BN_mul(r0,r1,r2,ctx)) goto err;	/* (p-1)(q-1) */
2b5643
+	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
2b5643
+		{
2b5643
+		  pr0 = &local_r0;
2b5643
+		  BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
2b5643
+		}
2b5643
+	else
2b5643
+	  pr0 = r0;
2b5643
+	if (!BN_mod_inverse(rsa->d,rsa->e,pr0,ctx)) goto err;	/* d */
2b5643
+
2b5643
+	/* set up d for correct BN_FLG_CONSTTIME flag */
2b5643
+	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
2b5643
+		{
2b5643
+		d = &local_d;
2b5643
+		BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
2b5643
+		}
2b5643
+	else
2b5643
+		d = rsa->d;
2b5643
+
2b5643
+	/* calculate d mod (p-1) */
2b5643
+	if (!BN_mod(rsa->dmp1,d,r1,ctx)) goto err;
2b5643
+
2b5643
+	/* calculate d mod (q-1) */
2b5643
+	if (!BN_mod(rsa->dmq1,d,r2,ctx)) goto err;
2b5643
+
2b5643
+	/* calculate inverse of q mod p */
2b5643
+	if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
2b5643
+		{
2b5643
+		p = &local_p;
2b5643
+		BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
2b5643
+		}
2b5643
+	else
2b5643
+		p = rsa->p;
2b5643
+	if (!BN_mod_inverse(rsa->iqmp,rsa->q,p,ctx)) goto err;
2b5643
+
2b5643
+	if (fips_rsa_pairwise_fail)
2b5643
+		BN_add_word(rsa->n, 1);
2b5643
+
2b5643
+	if(!fips_check_rsa(rsa))
2b5643
+	    goto err;
2b5643
+
2b5643
+	ok=1;
2b5643
+err:
2b5643
+	if (ok == -1)
2b5643
+		{
2b5643
+		RSAerr(RSA_F_RSA_BUILTIN_KEYGEN,ERR_LIB_BN);
2b5643
+		ok = 0;
2b5643
+		}
2b5643
+	if (ctx != NULL)
2b5643
+		{
2b5643
+		BN_CTX_end(ctx);
2b5643
+		BN_CTX_free(ctx);
2b5643
+		}
2b5643
+
2b5643
+	return ok;
2b5643
+	}
2b5643
+#endif
2b5643
+
2b5643
 static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
2b5643
 	{
2b5643
 	BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL,*tmp;
2b5643
@@ -176,17 +393,7 @@ static int rsa_builtin_keygen(RSA *rsa,
2b5643
 #ifdef OPENSSL_FIPS
2b5643
 	if (FIPS_module_mode())
2b5643
 		{
2b5643
-		if(FIPS_selftest_failed())
2b5643
-	    	{
2b5643
-		    FIPSerr(FIPS_F_RSA_BUILTIN_KEYGEN,FIPS_R_FIPS_SELFTEST_FAILED);
2b5643
-	    	return 0;
2b5643
-	    	}
2b5643
-
2b5643
-		if (bits < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS)
2b5643
-		    {
2b5643
-		    FIPSerr(FIPS_F_RSA_BUILTIN_KEYGEN,FIPS_R_KEY_TOO_SHORT);
2b5643
-		    return 0;
2b5643
-		    }
2b5643
+		return FIPS_rsa_builtin_keygen(rsa, bits, e_value, cb);
2b5643
 		}
2b5643
 #endif
2b5643
 
2b5643
@@ -301,17 +508,6 @@ static int rsa_builtin_keygen(RSA *rsa,
2b5643
 		p = rsa->p;
2b5643
 	if (!BN_mod_inverse(rsa->iqmp,rsa->q,p,ctx)) goto err;
2b5643
 
2b5643
-#ifdef OPENSSL_FIPS
2b5643
-	if (FIPS_module_mode())
2b5643
-		{
2b5643
-		if (fips_rsa_pairwise_fail)
2b5643
-			BN_add_word(rsa->n, 1);
2b5643
-
2b5643
-		if(!fips_check_rsa(rsa))
2b5643
-		    goto err;
2b5643
-		}
2b5643
-#endif
2b5643
-
2b5643
 	ok=1;
2b5643
 err:
2b5643
 	if (ok == -1)
2b5643
diff -up openssl-1.0.1e/ssl/t1_enc.c.fips-reqs openssl-1.0.1e/ssl/t1_enc.c
2b5643
--- openssl-1.0.1e/ssl/t1_enc.c.fips-reqs	2013-02-11 16:26:04.000000000 +0100
2b5643
+++ openssl-1.0.1e/ssl/t1_enc.c	2013-12-18 12:17:09.801637751 +0100
2b5643
@@ -291,6 +291,27 @@ static int tls1_PRF(long digest_mask,
2b5643
 err:
2b5643
 	return ret;
2b5643
 }
2b5643
+
2b5643
+int private_tls1_PRF(long digest_mask,
2b5643
+		     const void *seed1, int seed1_len,
2b5643
+		     const void *seed2, int seed2_len,
2b5643
+		     const void *seed3, int seed3_len,
2b5643
+		     const void *seed4, int seed4_len,
2b5643
+		     const void *seed5, int seed5_len,
2b5643
+		     const unsigned char *sec, int slen,
2b5643
+		     unsigned char *out1,
2b5643
+		     unsigned char *out2, int olen)
2b5643
+	{
2b5643
+	return tls1_PRF(digest_mask,
2b5643
+		seed1, seed1_len,
2b5643
+		seed2, seed2_len,
2b5643
+		seed3, seed3_len,
2b5643
+		seed4, seed4_len,
2b5643
+		seed5, seed5_len,
2b5643
+		sec, slen,
2b5643
+		out1, out2, olen);
2b5643
+	}
2b5643
+
2b5643
 static int tls1_generate_key_block(SSL *s, unsigned char *km,
2b5643
 	     unsigned char *tmp, int num)
2b5643
 	{