Blame SOURCES/openssl-1.0.1e-fips-ec.patch

a5ef24
diff -up openssl-1.0.1e/crypto/ecdh/ecdh.h.fips-ec openssl-1.0.1e/crypto/ecdh/ecdh.h
a5ef24
--- openssl-1.0.1e/crypto/ecdh/ecdh.h.fips-ec	2013-11-08 17:59:42.755019363 +0100
a5ef24
+++ openssl-1.0.1e/crypto/ecdh/ecdh.h	2013-11-08 17:59:43.147028002 +0100
a5ef24
@@ -85,6 +85,8 @@
a5ef24
 extern "C" {
a5ef24
 #endif
a5ef24
 
a5ef24
+#define EC_FLAG_COFACTOR_ECDH	0x1000
a5ef24
+
a5ef24
 const ECDH_METHOD *ECDH_OpenSSL(void);
a5ef24
 
a5ef24
 void	  ECDH_set_default_method(const ECDH_METHOD *);
a5ef24
diff -up openssl-1.0.1e/crypto/ecdh/ecdhtest.c.fips-ec openssl-1.0.1e/crypto/ecdh/ecdhtest.c
a5ef24
--- openssl-1.0.1e/crypto/ecdh/ecdhtest.c.fips-ec	2013-02-11 16:26:04.000000000 +0100
a5ef24
+++ openssl-1.0.1e/crypto/ecdh/ecdhtest.c	2013-11-08 17:59:54.712282862 +0100
a5ef24
@@ -323,8 +323,10 @@ int main(int argc, char *argv[])
a5ef24
 	if ((ctx=BN_CTX_new()) == NULL) goto err;
a5ef24
 
a5ef24
 	/* NIST PRIME CURVES TESTS */
a5ef24
+#if 0
a5ef24
 	if (!test_ecdh_curve(NID_X9_62_prime192v1, "NIST Prime-Curve P-192", ctx, out)) goto err;
a5ef24
 	if (!test_ecdh_curve(NID_secp224r1, "NIST Prime-Curve P-224", ctx, out)) goto err;
a5ef24
+#endif
a5ef24
 	if (!test_ecdh_curve(NID_X9_62_prime256v1, "NIST Prime-Curve P-256", ctx, out)) goto err;
a5ef24
 	if (!test_ecdh_curve(NID_secp384r1, "NIST Prime-Curve P-384", ctx, out)) goto err;
a5ef24
 	if (!test_ecdh_curve(NID_secp521r1, "NIST Prime-Curve P-521", ctx, out)) goto err;
a5ef24
diff -up openssl-1.0.1e/crypto/ecdh/ech_lib.c.fips-ec openssl-1.0.1e/crypto/ecdh/ech_lib.c
a5ef24
--- openssl-1.0.1e/crypto/ecdh/ech_lib.c.fips-ec	2013-02-11 16:26:04.000000000 +0100
a5ef24
+++ openssl-1.0.1e/crypto/ecdh/ech_lib.c	2013-11-08 17:59:43.148028024 +0100
a5ef24
@@ -94,14 +94,7 @@ const ECDH_METHOD *ECDH_get_default_meth
a5ef24
 	{
a5ef24
 	if(!default_ECDH_method) 
a5ef24
 		{
a5ef24
-#ifdef OPENSSL_FIPS
a5ef24
-		if (FIPS_mode())
a5ef24
-			return FIPS_ecdh_openssl();
a5ef24
-		else
a5ef24
-			return ECDH_OpenSSL();
a5ef24
-#else
a5ef24
 		default_ECDH_method = ECDH_OpenSSL();
a5ef24
-#endif
a5ef24
 		}
a5ef24
 	return default_ECDH_method;
a5ef24
 	}
a5ef24
diff -up openssl-1.0.1e/crypto/ecdh/ech_ossl.c.fips-ec openssl-1.0.1e/crypto/ecdh/ech_ossl.c
a5ef24
--- openssl-1.0.1e/crypto/ecdh/ech_ossl.c.fips-ec	2013-02-11 16:26:04.000000000 +0100
a5ef24
+++ openssl-1.0.1e/crypto/ecdh/ech_ossl.c	2013-11-08 17:59:43.148028024 +0100
a5ef24
@@ -79,6 +79,10 @@
a5ef24
 #include <openssl/obj_mac.h>
a5ef24
 #include <openssl/bn.h>
a5ef24
 
a5ef24
+#ifdef OPENSSL_FIPS
a5ef24
+#include <openssl/fips.h>
a5ef24
+#endif
a5ef24
+
a5ef24
 static int ecdh_compute_key(void *out, size_t len, const EC_POINT *pub_key,
a5ef24
 	EC_KEY *ecdh, 
a5ef24
 	void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen));
a5ef24
@@ -90,7 +94,7 @@ static ECDH_METHOD openssl_ecdh_meth = {
a5ef24
 	NULL, /* init     */
a5ef24
 	NULL, /* finish   */
a5ef24
 #endif
a5ef24
-	0,    /* flags    */
a5ef24
+	ECDH_FLAG_FIPS_METHOD,    /* flags    */
a5ef24
 	NULL  /* app_data */
a5ef24
 };
a5ef24
 
a5ef24
@@ -118,6 +122,14 @@ static int ecdh_compute_key(void *out, s
a5ef24
 	size_t buflen, len;
a5ef24
 	unsigned char *buf=NULL;
a5ef24
 
a5ef24
+#ifdef OPENSSL_FIPS
a5ef24
+	if(FIPS_selftest_failed())
a5ef24
+		{
a5ef24
+		FIPSerr(FIPS_F_ECDH_COMPUTE_KEY,FIPS_R_FIPS_SELFTEST_FAILED);
a5ef24
+		return -1;
a5ef24
+		}
a5ef24
+#endif
a5ef24
+
a5ef24
 	if (outlen > INT_MAX)
a5ef24
 		{
a5ef24
 		ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ERR_R_MALLOC_FAILURE); /* sort of, anyway */
a5ef24
@@ -137,6 +149,18 @@ static int ecdh_compute_key(void *out, s
a5ef24
 		}
a5ef24
 
a5ef24
 	group = EC_KEY_get0_group(ecdh);
a5ef24
+
a5ef24
+	if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH)
a5ef24
+		{
a5ef24
+		if (!EC_GROUP_get_cofactor(group, x, ctx) ||
a5ef24
+			!BN_mul(x, x, priv_key, ctx))
a5ef24
+			{
a5ef24
+			ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
a5ef24
+			goto err;
a5ef24
+			}
a5ef24
+		priv_key = x;
a5ef24
+		}
a5ef24
+
a5ef24
 	if ((tmp=EC_POINT_new(group)) == NULL)
a5ef24
 		{
a5ef24
 		ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ERR_R_MALLOC_FAILURE);
a5ef24
diff -up openssl-1.0.1e/crypto/ecdsa/ecdsatest.c.fips-ec openssl-1.0.1e/crypto/ecdsa/ecdsatest.c
a5ef24
--- openssl-1.0.1e/crypto/ecdsa/ecdsatest.c.fips-ec	2013-02-11 16:26:04.000000000 +0100
a5ef24
+++ openssl-1.0.1e/crypto/ecdsa/ecdsatest.c	2013-11-08 17:59:43.148028024 +0100
a5ef24
@@ -138,11 +138,14 @@ int restore_rand(void)
a5ef24
 	}
a5ef24
 
a5ef24
 static int fbytes_counter = 0;
a5ef24
-static const char *numbers[8] = {
a5ef24
+static const char *numbers[10] = {
a5ef24
+	"651056770906015076056810763456358567190100156695615665659",
a5ef24
 	"651056770906015076056810763456358567190100156695615665659",
a5ef24
 	"6140507067065001063065065565667405560006161556565665656654",
a5ef24
 	"8763001015071075675010661307616710783570106710677817767166"
a5ef24
 	"71676178726717",
a5ef24
+	"8763001015071075675010661307616710783570106710677817767166"
a5ef24
+	"71676178726717",
a5ef24
 	"7000000175690566466555057817571571075705015757757057795755"
a5ef24
 	"55657156756655",
a5ef24
 	"1275552191113212300012030439187146164646146646466749494799",
a5ef24
@@ -157,7 +160,7 @@ int fbytes(unsigned char *buf, int num)
a5ef24
 	int	ret;
a5ef24
 	BIGNUM	*tmp = NULL;
a5ef24
 
a5ef24
-	if (fbytes_counter >= 8)
a5ef24
+	if (fbytes_counter >= 10)
a5ef24
 		return 0;
a5ef24
 	tmp = BN_new();
a5ef24
 	if (!tmp)
a5ef24
@@ -550,7 +553,9 @@ int main(void)
a5ef24
 	RAND_seed(rnd_seed, sizeof(rnd_seed));
a5ef24
 
a5ef24
 	/* the tests */
a5ef24
+#if 0
a5ef24
 	if (!x9_62_tests(out))  goto err;
a5ef24
+#endif
a5ef24
 	if (!test_builtin(out)) goto err;
a5ef24
 	
a5ef24
 	ret = 0;
a5ef24
diff -up openssl-1.0.1e/crypto/ecdsa/ecs_lib.c.fips-ec openssl-1.0.1e/crypto/ecdsa/ecs_lib.c
a5ef24
--- openssl-1.0.1e/crypto/ecdsa/ecs_lib.c.fips-ec	2013-02-11 16:26:04.000000000 +0100
a5ef24
+++ openssl-1.0.1e/crypto/ecdsa/ecs_lib.c	2013-11-08 17:59:43.148028024 +0100
a5ef24
@@ -81,14 +81,7 @@ const ECDSA_METHOD *ECDSA_get_default_me
a5ef24
 {
a5ef24
 	if(!default_ECDSA_method) 
a5ef24
 		{
a5ef24
-#ifdef OPENSSL_FIPS
a5ef24
-		if (FIPS_mode())
a5ef24
-			return FIPS_ecdsa_openssl();
a5ef24
-		else
a5ef24
-			return ECDSA_OpenSSL();
a5ef24
-#else
a5ef24
 		default_ECDSA_method = ECDSA_OpenSSL();
a5ef24
-#endif
a5ef24
 		}
a5ef24
 	return default_ECDSA_method;
a5ef24
 }
a5ef24
diff -up openssl-1.0.1e/crypto/ecdsa/ecs_ossl.c.fips-ec openssl-1.0.1e/crypto/ecdsa/ecs_ossl.c
a5ef24
--- openssl-1.0.1e/crypto/ecdsa/ecs_ossl.c.fips-ec	2013-02-11 16:26:04.000000000 +0100
a5ef24
+++ openssl-1.0.1e/crypto/ecdsa/ecs_ossl.c	2013-11-08 17:59:43.148028024 +0100
a5ef24
@@ -60,6 +60,9 @@
a5ef24
 #include <openssl/err.h>
a5ef24
 #include <openssl/obj_mac.h>
a5ef24
 #include <openssl/bn.h>
a5ef24
+#ifdef OPENSSL_FIPS
a5ef24
+#include <openssl/fips.h>
a5ef24
+#endif
a5ef24
 
a5ef24
 static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen, 
a5ef24
 		const BIGNUM *, const BIGNUM *, EC_KEY *eckey);
a5ef24
@@ -77,7 +80,7 @@ static ECDSA_METHOD openssl_ecdsa_meth =
a5ef24
 	NULL, /* init     */
a5ef24
 	NULL, /* finish   */
a5ef24
 #endif
a5ef24
-	0,    /* flags    */
a5ef24
+	ECDSA_FLAG_FIPS_METHOD,    /* flags    */
a5ef24
 	NULL  /* app_data */
a5ef24
 };
a5ef24
 
a5ef24
@@ -231,6 +234,14 @@ static ECDSA_SIG *ecdsa_do_sign(const un
a5ef24
 	ECDSA_DATA *ecdsa;
a5ef24
 	const BIGNUM *priv_key;
a5ef24
 
a5ef24
+#ifdef OPENSSL_FIPS
a5ef24
+	if(FIPS_selftest_failed())
a5ef24
+		{
a5ef24
+		FIPSerr(FIPS_F_ECDSA_DO_SIGN,FIPS_R_FIPS_SELFTEST_FAILED);
a5ef24
+		return NULL;
a5ef24
+		}
a5ef24
+#endif
a5ef24
+
a5ef24
 	ecdsa    = ecdsa_check(eckey);
a5ef24
 	group    = EC_KEY_get0_group(eckey);
a5ef24
 	priv_key = EC_KEY_get0_private_key(eckey);
a5ef24
@@ -360,6 +371,14 @@ static int ecdsa_do_verify(const unsigne
a5ef24
 	const EC_GROUP *group;
a5ef24
 	const EC_POINT *pub_key;
a5ef24
 
a5ef24
+#ifdef OPENSSL_FIPS
a5ef24
+	if(FIPS_selftest_failed())
a5ef24
+		{
a5ef24
+		FIPSerr(FIPS_F_ECDSA_DO_VERIFY,FIPS_R_FIPS_SELFTEST_FAILED);
a5ef24
+		return -1;
a5ef24
+		}
a5ef24
+#endif
a5ef24
+
a5ef24
 	/* check input values */
a5ef24
 	if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
a5ef24
 	    (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL)
a5ef24
diff -up openssl-1.0.1e/crypto/ec/ec_key.c.fips-ec openssl-1.0.1e/crypto/ec/ec_key.c
a5ef24
--- openssl-1.0.1e/crypto/ec/ec_key.c.fips-ec	2013-02-11 16:26:04.000000000 +0100
a5ef24
+++ openssl-1.0.1e/crypto/ec/ec_key.c	2013-11-08 17:59:43.148028024 +0100
a5ef24
@@ -64,9 +64,6 @@
a5ef24
 #include <string.h>
a5ef24
 #include "ec_lcl.h"
a5ef24
 #include <openssl/err.h>
a5ef24
-#ifdef OPENSSL_FIPS
a5ef24
-#include <openssl/fips.h>
a5ef24
-#endif
a5ef24
 
a5ef24
 EC_KEY *EC_KEY_new(void)
a5ef24
 	{
a5ef24
@@ -234,6 +231,39 @@ int EC_KEY_up_ref(EC_KEY *r)
a5ef24
 	return ((i > 1) ? 1 : 0);
a5ef24
 	}
a5ef24
 
a5ef24
+#ifdef OPENSSL_FIPS
a5ef24
+
a5ef24
+#include <openssl/evp.h>
a5ef24
+#include <openssl/fips.h>
a5ef24
+#include <openssl/fips_rand.h>
a5ef24
+
a5ef24
+static int fips_check_ec(EC_KEY *key)
a5ef24
+	{
a5ef24
+	EVP_PKEY *pk;
a5ef24
+	unsigned char tbs[] = "ECDSA Pairwise Check Data";
a5ef24
+	int ret = 0;
a5ef24
+
a5ef24
+	if ((pk=EVP_PKEY_new()) == NULL)
a5ef24
+		goto err;
a5ef24
+
a5ef24
+	EVP_PKEY_set1_EC_KEY(pk, key);
a5ef24
+
83c29f
+	if (fips_pkey_signature_test(pk, tbs, -1, NULL, 0, NULL, 0, NULL))
a5ef24
+		ret = 1;
a5ef24
+
a5ef24
+	err:
a5ef24
+	if (ret == 0)
a5ef24
+		{
a5ef24
+		FIPSerr(FIPS_F_FIPS_CHECK_EC,FIPS_R_PAIRWISE_TEST_FAILED);
a5ef24
+		fips_set_selftest_fail();
a5ef24
+		}
a5ef24
+	if (pk)
a5ef24
+		EVP_PKEY_free(pk);
a5ef24
+	return ret;
a5ef24
+	}
a5ef24
+
a5ef24
+#endif
a5ef24
+
a5ef24
 int EC_KEY_generate_key(EC_KEY *eckey)
a5ef24
 	{	
a5ef24
 	int	ok = 0;
a5ef24
@@ -242,8 +272,11 @@ int EC_KEY_generate_key(EC_KEY *eckey)
a5ef24
 	EC_POINT *pub_key = NULL;
a5ef24
 
a5ef24
 #ifdef OPENSSL_FIPS
a5ef24
-	if (FIPS_mode())
a5ef24
-		return FIPS_ec_key_generate_key(eckey);
a5ef24
+	if(FIPS_selftest_failed())
a5ef24
+		{
a5ef24
+		FIPSerr(FIPS_F_EC_KEY_GENERATE_KEY,FIPS_R_FIPS_SELFTEST_FAILED);
a5ef24
+		return 0;
a5ef24
+		}
a5ef24
 #endif
a5ef24
 
a5ef24
 	if (!eckey || !eckey->group)
a5ef24
@@ -287,6 +320,15 @@ int EC_KEY_generate_key(EC_KEY *eckey)
a5ef24
 	eckey->priv_key = priv_key;
a5ef24
 	eckey->pub_key  = pub_key;
a5ef24
 
a5ef24
+#ifdef OPENSSL_FIPS
a5ef24
+	if(!fips_check_ec(eckey))
a5ef24
+		{
a5ef24
+		eckey->priv_key = NULL;
a5ef24
+		eckey->pub_key  = NULL;
a5ef24
+	    	goto err;
a5ef24
+		}
a5ef24
+#endif
a5ef24
+
a5ef24
 	ok=1;
a5ef24
 
a5ef24
 err:	
a5ef24
@@ -429,10 +471,12 @@ int EC_KEY_set_public_key_affine_coordin
a5ef24
 								tx, ty, ctx))
a5ef24
 			goto err;
a5ef24
 		}
a5ef24
-	/* Check if retrieved coordinates match originals: if not values
a5ef24
-	 * are out of range.
a5ef24
+	/* Check if retrieved coordinates match originals and are less than
a5ef24
+	 * field order: if not values are out of range.
a5ef24
 	 */
a5ef24
-	if (BN_cmp(x, tx) || BN_cmp(y, ty))
a5ef24
+	if (BN_cmp(x, tx) || BN_cmp(y, ty)
a5ef24
+		|| (BN_cmp(x, &key->group->field) >= 0)
a5ef24
+		|| (BN_cmp(y, &key->group->field) >= 0))
a5ef24
 		{
a5ef24
 		ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
a5ef24
 			EC_R_COORDINATES_OUT_OF_RANGE);
a5ef24
diff -up openssl-1.0.1e/crypto/ec/ecp_mont.c.fips-ec openssl-1.0.1e/crypto/ec/ecp_mont.c
a5ef24
--- openssl-1.0.1e/crypto/ec/ecp_mont.c.fips-ec	2013-02-11 16:26:04.000000000 +0100
a5ef24
+++ openssl-1.0.1e/crypto/ec/ecp_mont.c	2013-11-08 17:59:43.149028046 +0100
a5ef24
@@ -63,18 +63,11 @@
a5ef24
 
a5ef24
 #include <openssl/err.h>
a5ef24
 
a5ef24
-#ifdef OPENSSL_FIPS
a5ef24
-#include <openssl/fips.h>
a5ef24
-#endif
a5ef24
-
a5ef24
 #include "ec_lcl.h"
a5ef24
 
a5ef24
 
a5ef24
 const EC_METHOD *EC_GFp_mont_method(void)
a5ef24
 	{
a5ef24
-#ifdef OPENSSL_FIPS
a5ef24
-	return fips_ec_gfp_mont_method();
a5ef24
-#else
a5ef24
 	static const EC_METHOD ret = {
a5ef24
 		EC_FLAGS_DEFAULT_OCT,
a5ef24
 		NID_X9_62_prime_field,
a5ef24
@@ -115,7 +108,6 @@ const EC_METHOD *EC_GFp_mont_method(void
a5ef24
 		ec_GFp_mont_field_set_to_one };
a5ef24
 
a5ef24
 	return &ret;
a5ef24
-#endif
a5ef24
 	}
a5ef24
 
a5ef24
 
a5ef24
diff -up openssl-1.0.1e/crypto/ec/ecp_nist.c.fips-ec openssl-1.0.1e/crypto/ec/ecp_nist.c
a5ef24
--- openssl-1.0.1e/crypto/ec/ecp_nist.c.fips-ec	2013-02-11 16:26:04.000000000 +0100
a5ef24
+++ openssl-1.0.1e/crypto/ec/ecp_nist.c	2013-11-08 17:59:43.149028046 +0100
a5ef24
@@ -67,15 +67,8 @@
a5ef24
 #include <openssl/obj_mac.h>
a5ef24
 #include "ec_lcl.h"
a5ef24
 
a5ef24
-#ifdef OPENSSL_FIPS
a5ef24
-#include <openssl/fips.h>
a5ef24
-#endif
a5ef24
-
a5ef24
 const EC_METHOD *EC_GFp_nist_method(void)
a5ef24
 	{
a5ef24
-#ifdef OPENSSL_FIPS
a5ef24
-	return fips_ec_gfp_nist_method();
a5ef24
-#else
a5ef24
 	static const EC_METHOD ret = {
a5ef24
 		EC_FLAGS_DEFAULT_OCT,
a5ef24
 		NID_X9_62_prime_field,
a5ef24
@@ -116,7 +109,6 @@ const EC_METHOD *EC_GFp_nist_method(void
a5ef24
 		0 /* field_set_to_one */ };
a5ef24
 
a5ef24
 	return &ret;
a5ef24
-#endif
a5ef24
 	}
a5ef24
 
a5ef24
 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
a5ef24
diff -up openssl-1.0.1e/crypto/ec/ecp_smpl.c.fips-ec openssl-1.0.1e/crypto/ec/ecp_smpl.c
a5ef24
--- openssl-1.0.1e/crypto/ec/ecp_smpl.c.fips-ec	2013-02-11 16:26:04.000000000 +0100
a5ef24
+++ openssl-1.0.1e/crypto/ec/ecp_smpl.c	2013-11-08 17:59:43.149028046 +0100
a5ef24
@@ -65,17 +65,10 @@
a5ef24
 #include <openssl/err.h>
a5ef24
 #include <openssl/symhacks.h>
a5ef24
 
a5ef24
-#ifdef OPENSSL_FIPS
a5ef24
-#include <openssl/fips.h>
a5ef24
-#endif
a5ef24
-
a5ef24
 #include "ec_lcl.h"
a5ef24
 
a5ef24
 const EC_METHOD *EC_GFp_simple_method(void)
a5ef24
 	{
a5ef24
-#ifdef OPENSSL_FIPS
a5ef24
-	return fips_ec_gfp_simple_method();
a5ef24
-#else
a5ef24
 	static const EC_METHOD ret = {
a5ef24
 		EC_FLAGS_DEFAULT_OCT,
a5ef24
 		NID_X9_62_prime_field,
a5ef24
@@ -116,7 +109,6 @@ const EC_METHOD *EC_GFp_simple_method(vo
a5ef24
 		0 /* field_set_to_one */ };
a5ef24
 
a5ef24
 	return &ret;
a5ef24
-#endif
a5ef24
 	}
a5ef24
 
a5ef24
 
a5ef24
@@ -186,6 +178,12 @@ int ec_GFp_simple_group_set_curve(EC_GRO
a5ef24
 		return 0;
a5ef24
 		}
a5ef24
 
a5ef24
+	if (BN_num_bits(p) < 256)
a5ef24
+		{
a5ef24
+		ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_UNSUPPORTED_FIELD);
a5ef24
+		return 0;
a5ef24
+		}
a5ef24
+
a5ef24
 	if (ctx == NULL)
a5ef24
 		{
a5ef24
 		ctx = new_ctx = BN_CTX_new();
a5ef24
diff -up openssl-1.0.1e/crypto/evp/m_ecdsa.c.fips-ec openssl-1.0.1e/crypto/evp/m_ecdsa.c
a5ef24
--- openssl-1.0.1e/crypto/evp/m_ecdsa.c.fips-ec	2013-02-11 16:26:04.000000000 +0100
a5ef24
+++ openssl-1.0.1e/crypto/evp/m_ecdsa.c	2013-11-08 17:59:43.149028046 +0100
a5ef24
@@ -116,7 +116,6 @@
a5ef24
 #include <openssl/x509.h>
a5ef24
 
a5ef24
 #ifndef OPENSSL_NO_SHA
a5ef24
-#ifndef OPENSSL_FIPS
a5ef24
 
a5ef24
 static int init(EVP_MD_CTX *ctx)
a5ef24
 	{ return SHA1_Init(ctx->md_data); }
a5ef24
@@ -132,7 +131,7 @@ static const EVP_MD ecdsa_md=
a5ef24
 	NID_ecdsa_with_SHA1,
a5ef24
 	NID_ecdsa_with_SHA1,
a5ef24
 	SHA_DIGEST_LENGTH,
a5ef24
-	EVP_MD_FLAG_PKEY_DIGEST,
a5ef24
+	EVP_MD_FLAG_PKEY_DIGEST|EVP_MD_FLAG_FIPS,
a5ef24
 	init,
a5ef24
 	update,
a5ef24
 	final,
a5ef24
@@ -148,4 +147,3 @@ const EVP_MD *EVP_ecdsa(void)
a5ef24
 	return(&ecdsa_md);
a5ef24
 	}
a5ef24
 #endif
a5ef24
-#endif
a5ef24
diff -up openssl-1.0.1e/crypto/fips/cavs/fips_ecdhvs.c.fips-ec openssl-1.0.1e/crypto/fips/cavs/fips_ecdhvs.c
a5ef24
--- openssl-1.0.1e/crypto/fips/cavs/fips_ecdhvs.c.fips-ec	2013-11-08 17:59:43.149028046 +0100
a5ef24
+++ openssl-1.0.1e/crypto/fips/cavs/fips_ecdhvs.c	2013-11-08 17:59:43.149028046 +0100
a5ef24
@@ -0,0 +1,496 @@
a5ef24
+/* fips/ecdh/fips_ecdhvs.c */
a5ef24
+/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
a5ef24
+ * project.
a5ef24
+ */
a5ef24
+/* ====================================================================
a5ef24
+ * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
a5ef24
+ *
a5ef24
+ * Redistribution and use in source and binary forms, with or without
a5ef24
+ * modification, are permitted provided that the following conditions
a5ef24
+ * are met:
a5ef24
+ *
a5ef24
+ * 1. Redistributions of source code must retain the above copyright
a5ef24
+ *    notice, this list of conditions and the following disclaimer. 
a5ef24
+ *
a5ef24
+ * 2. Redistributions in binary form must reproduce the above copyright
a5ef24
+ *    notice, this list of conditions and the following disclaimer in
a5ef24
+ *    the documentation and/or other materials provided with the
a5ef24
+ *    distribution.
a5ef24
+ *
a5ef24
+ * 3. All advertising materials mentioning features or use of this
a5ef24
+ *    software must display the following acknowledgment:
a5ef24
+ *    "This product includes software developed by the OpenSSL Project
a5ef24
+ *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
a5ef24
+ *
a5ef24
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
a5ef24
+ *    endorse or promote products derived from this software without
a5ef24
+ *    prior written permission. For written permission, please contact
a5ef24
+ *    licensing@OpenSSL.org.
a5ef24
+ *
a5ef24
+ * 5. Products derived from this software may not be called "OpenSSL"
a5ef24
+ *    nor may "OpenSSL" appear in their names without prior written
a5ef24
+ *    permission of the OpenSSL Project.
a5ef24
+ *
a5ef24
+ * 6. Redistributions of any form whatsoever must retain the following
a5ef24
+ *    acknowledgment:
a5ef24
+ *    "This product includes software developed by the OpenSSL Project
a5ef24
+ *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
a5ef24
+ *
a5ef24
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
a5ef24
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
a5ef24
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
a5ef24
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
a5ef24
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
a5ef24
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
a5ef24
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
a5ef24
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
a5ef24
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
a5ef24
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
a5ef24
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
a5ef24
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
a5ef24
+ * ====================================================================
a5ef24
+ */
a5ef24
+
a5ef24
+
a5ef24
+#define OPENSSL_FIPSAPI
a5ef24
+#include <openssl/opensslconf.h>
a5ef24
+
a5ef24
+#ifndef OPENSSL_FIPS
a5ef24
+#include <stdio.h>
a5ef24
+
a5ef24
+int main(int argc, char **argv)
a5ef24
+{
a5ef24
+    printf("No FIPS ECDH support\n");
a5ef24
+    return(0);
a5ef24
+}
a5ef24
+#else
a5ef24
+
a5ef24
+#include <openssl/crypto.h>
a5ef24
+#include <openssl/bn.h>
a5ef24
+#include <openssl/ecdh.h>
a5ef24
+#include <openssl/fips.h>
a5ef24
+#include <openssl/err.h>
a5ef24
+#include <openssl/evp.h>
a5ef24
+#include <string.h>
a5ef24
+#include <ctype.h>
a5ef24
+
a5ef24
+#include "fips_utl.h"
a5ef24
+
a5ef24
+static const EVP_MD *eparse_md(char *line)
a5ef24
+	{
a5ef24
+	char *p;
a5ef24
+	if (line[0] != '[' || line[1] != 'E')
a5ef24
+		return NULL;
a5ef24
+	p = strchr(line, '-');
a5ef24
+	if (!p)
a5ef24
+		return NULL;
a5ef24
+	line = p + 1;
a5ef24
+	p = strchr(line, ']');
a5ef24
+	if (!p)
a5ef24
+		return NULL;
a5ef24
+	*p = 0;
a5ef24
+	p = line;
a5ef24
+	while(isspace(*p))
a5ef24
+		p++;
a5ef24
+	if (!strcmp(p, "SHA1"))
a5ef24
+		return EVP_sha1();
a5ef24
+	else if (!strcmp(p, "SHA224"))
a5ef24
+		return EVP_sha224();
a5ef24
+	else if (!strcmp(p, "SHA256"))
a5ef24
+		return EVP_sha256();
a5ef24
+	else if (!strcmp(p, "SHA384"))
a5ef24
+		return EVP_sha384();
a5ef24
+	else if (!strcmp(p, "SHA512"))
a5ef24
+		return EVP_sha512();
a5ef24
+	else
a5ef24
+		return NULL;
a5ef24
+	}
a5ef24
+
a5ef24
+static int lookup_curve2(char *cname)
a5ef24
+	{
a5ef24
+	char *p;
a5ef24
+	p = strchr(cname, ']');
a5ef24
+	if (!p)
a5ef24
+		{
a5ef24
+		fprintf(stderr, "Parse error: missing ]\n");
a5ef24
+		return NID_undef;
a5ef24
+		}
a5ef24
+	*p = 0;
a5ef24
+
a5ef24
+	if (!strcmp(cname, "B-163"))
a5ef24
+		return NID_sect163r2;
a5ef24
+	if (!strcmp(cname, "B-233"))
a5ef24
+		return NID_sect233r1;
a5ef24
+	if (!strcmp(cname, "B-283"))
a5ef24
+		return NID_sect283r1;
a5ef24
+	if (!strcmp(cname, "B-409"))
a5ef24
+		return NID_sect409r1;
a5ef24
+	if (!strcmp(cname, "B-571"))
a5ef24
+		return NID_sect571r1;
a5ef24
+	if (!strcmp(cname, "K-163"))
a5ef24
+		return NID_sect163k1;
a5ef24
+	if (!strcmp(cname, "K-233"))
a5ef24
+		return NID_sect233k1;
a5ef24
+	if (!strcmp(cname, "K-283"))
a5ef24
+		return NID_sect283k1;
a5ef24
+	if (!strcmp(cname, "K-409"))
a5ef24
+		return NID_sect409k1;
a5ef24
+	if (!strcmp(cname, "K-571"))
a5ef24
+		return NID_sect571k1;
a5ef24
+	if (!strcmp(cname, "P-192"))
a5ef24
+		return NID_X9_62_prime192v1;
a5ef24
+	if (!strcmp(cname, "P-224"))
a5ef24
+		return NID_secp224r1;
a5ef24
+	if (!strcmp(cname, "P-256"))
a5ef24
+		return NID_X9_62_prime256v1;
a5ef24
+	if (!strcmp(cname, "P-384"))
a5ef24
+		return NID_secp384r1;
a5ef24
+	if (!strcmp(cname, "P-521"))
a5ef24
+		return NID_secp521r1;
a5ef24
+
a5ef24
+	fprintf(stderr, "Unknown Curve name %s\n", cname);
a5ef24
+	return NID_undef;
a5ef24
+	}
a5ef24
+
a5ef24
+static int lookup_curve(char *cname)
a5ef24
+	{
a5ef24
+	char *p;
a5ef24
+	p = strchr(cname, ':');
a5ef24
+	if (!p)
a5ef24
+		{
a5ef24
+		fprintf(stderr, "Parse error: missing :\n");
a5ef24
+		return NID_undef;
a5ef24
+		}
a5ef24
+	cname = p + 1;
a5ef24
+	while(isspace(*cname))
a5ef24
+		cname++;
a5ef24
+	return lookup_curve2(cname);
a5ef24
+	}
a5ef24
+
a5ef24
+static EC_POINT *make_peer(EC_GROUP *group, BIGNUM *x, BIGNUM *y)
a5ef24
+	{
a5ef24
+	EC_POINT *peer;
a5ef24
+	int rv;
a5ef24
+	BN_CTX *c;
a5ef24
+	peer = EC_POINT_new(group);
a5ef24
+	if (!peer)
a5ef24
+		return NULL;
a5ef24
+	c = BN_CTX_new();
a5ef24
+	if (EC_METHOD_get_field_type(EC_GROUP_method_of(group))
a5ef24
+		== NID_X9_62_prime_field)
a5ef24
+		rv = EC_POINT_set_affine_coordinates_GFp(group, peer, x, y, c);
a5ef24
+	else
a5ef24
+#ifdef OPENSSL_NO_EC2M
a5ef24
+		{
a5ef24
+		fprintf(stderr, "ERROR: GF2m not supported\n");
a5ef24
+		exit(1);
a5ef24
+		}
a5ef24
+#else
a5ef24
+		rv = EC_POINT_set_affine_coordinates_GF2m(group, peer, x, y, c);
a5ef24
+#endif
a5ef24
+
a5ef24
+	BN_CTX_free(c);
a5ef24
+	if (rv)
a5ef24
+		return peer;
a5ef24
+	EC_POINT_free(peer);
a5ef24
+	return NULL;
a5ef24
+	}
a5ef24
+
a5ef24
+static int ec_print_key(FILE *out, EC_KEY *key, int add_e, int exout)
a5ef24
+	{
a5ef24
+	const EC_POINT *pt;
a5ef24
+	const EC_GROUP *grp;
a5ef24
+	const EC_METHOD *meth;
a5ef24
+	int rv;
a5ef24
+	BIGNUM *tx, *ty;
a5ef24
+	const BIGNUM *d = NULL;
a5ef24
+	BN_CTX *ctx;
a5ef24
+	ctx = BN_CTX_new();
a5ef24
+	if (!ctx)
a5ef24
+		return 0;
a5ef24
+	tx = BN_CTX_get(ctx);
a5ef24
+	ty = BN_CTX_get(ctx);
a5ef24
+	if (!tx || !ty)
a5ef24
+		return 0;
a5ef24
+	grp = EC_KEY_get0_group(key);
a5ef24
+	pt = EC_KEY_get0_public_key(key);
a5ef24
+	if (exout)
a5ef24
+		d = EC_KEY_get0_private_key(key);
a5ef24
+	meth = EC_GROUP_method_of(grp);
a5ef24
+	if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
a5ef24
+		rv = EC_POINT_get_affine_coordinates_GFp(grp, pt, tx, ty, ctx);
a5ef24
+	else
a5ef24
+#ifdef OPENSSL_NO_EC2M
a5ef24
+		{
a5ef24
+		fprintf(stderr, "ERROR: GF2m not supported\n");
a5ef24
+		exit(1);
a5ef24
+		}
a5ef24
+#else
a5ef24
+		rv = EC_POINT_get_affine_coordinates_GF2m(grp, pt, tx, ty, ctx);
a5ef24
+#endif
a5ef24
+
a5ef24
+	if (add_e)
a5ef24
+		{
a5ef24
+		do_bn_print_name(out, "QeIUTx", tx);
a5ef24
+		do_bn_print_name(out, "QeIUTy", ty);
a5ef24
+		if (d)
a5ef24
+			do_bn_print_name(out, "QeIUTd", d);
a5ef24
+		}
a5ef24
+	else
a5ef24
+		{
a5ef24
+		do_bn_print_name(out, "QIUTx", tx);
a5ef24
+		do_bn_print_name(out, "QIUTy", ty);
a5ef24
+		if (d)
a5ef24
+			do_bn_print_name(out, "QIUTd", d);
a5ef24
+		}
a5ef24
+
a5ef24
+	BN_CTX_free(ctx);
a5ef24
+
a5ef24
+	return rv;
a5ef24
+
a5ef24
+	}
a5ef24
+
a5ef24
+static void ec_output_Zhash(FILE *out, int exout, EC_GROUP *group,
a5ef24
+			BIGNUM *ix, BIGNUM *iy, BIGNUM *id, BIGNUM *cx,
a5ef24
+			BIGNUM *cy, const EVP_MD *md,
a5ef24
+				unsigned char *rhash, size_t rhashlen)
a5ef24
+	{
a5ef24
+	EC_KEY *ec = NULL;
a5ef24
+	EC_POINT *peerkey = NULL;
a5ef24
+	unsigned char *Z;
a5ef24
+	unsigned char chash[EVP_MAX_MD_SIZE];
a5ef24
+	int Zlen;
a5ef24
+	ec = EC_KEY_new();
a5ef24
+	EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
a5ef24
+	EC_KEY_set_group(ec, group);
a5ef24
+	peerkey = make_peer(group, cx, cy);
a5ef24
+	if (rhash == NULL)
a5ef24
+		{
a5ef24
+		if (md)
a5ef24
+			rhashlen = M_EVP_MD_size(md);
a5ef24
+		EC_KEY_generate_key(ec);
a5ef24
+		ec_print_key(out, ec, md ? 1 : 0, exout);
a5ef24
+		}
a5ef24
+	else
a5ef24
+		{
a5ef24
+		EC_KEY_set_public_key_affine_coordinates(ec, ix, iy);
a5ef24
+		EC_KEY_set_private_key(ec, id);
a5ef24
+		}
a5ef24
+	Zlen = (EC_GROUP_get_degree(group) + 7)/8;
a5ef24
+	Z = OPENSSL_malloc(Zlen);
a5ef24
+	if (!Z)
a5ef24
+		exit(1);
a5ef24
+	ECDH_compute_key(Z, Zlen, peerkey, ec, 0);
a5ef24
+	if (md)
a5ef24
+		{
a5ef24
+		if (exout)
a5ef24
+			OutputValue("Z", Z, Zlen, out, 0);
a5ef24
+		FIPS_digest(Z, Zlen, chash, NULL, md);
a5ef24
+		OutputValue(rhash ? "IUTHashZZ" : "HashZZ",
a5ef24
+						chash, rhashlen, out, 0);
a5ef24
+		if (rhash)
a5ef24
+			{
a5ef24
+			fprintf(out, "Result = %s\n",
a5ef24
+				memcmp(chash, rhash, rhashlen) ? "F" : "P");
a5ef24
+			}
a5ef24
+		}
a5ef24
+	else
a5ef24
+		OutputValue("ZIUT", Z, Zlen, out, 0);
a5ef24
+	OPENSSL_cleanse(Z, Zlen);
a5ef24
+	OPENSSL_free(Z);
a5ef24
+	EC_KEY_free(ec);
a5ef24
+	EC_POINT_free(peerkey);
a5ef24
+	}
a5ef24
+		
a5ef24
+#ifdef FIPS_ALGVS
a5ef24
+int fips_ecdhvs_main(int argc, char **argv)
a5ef24
+#else
a5ef24
+int main(int argc, char **argv)
a5ef24
+#endif
a5ef24
+	{
a5ef24
+	char **args = argv + 1;
a5ef24
+	int argn = argc - 1;
a5ef24
+	FILE *in, *out;
a5ef24
+	char buf[2048], lbuf[2048];
a5ef24
+	unsigned char *rhash = NULL;
a5ef24
+	long rhashlen;
a5ef24
+	BIGNUM *cx = NULL, *cy = NULL;
a5ef24
+	BIGNUM *id = NULL, *ix = NULL, *iy = NULL;
a5ef24
+	const EVP_MD *md = NULL;
a5ef24
+	EC_GROUP *group = NULL;
a5ef24
+	char *keyword = NULL, *value = NULL;
a5ef24
+	int do_verify = -1, exout = 0;
a5ef24
+	int rv = 1;
a5ef24
+
a5ef24
+	int curve_nids[5] = {0,0,0,0,0};
a5ef24
+	int param_set = -1;
a5ef24
+
a5ef24
+	fips_algtest_init();
a5ef24
+
a5ef24
+	if (argn && !strcmp(*args, "ecdhver"))
a5ef24
+		{
a5ef24
+		do_verify = 1;
a5ef24
+		args++;
a5ef24
+		argn--;
a5ef24
+		}
a5ef24
+	else if (argn && !strcmp(*args, "ecdhgen"))
a5ef24
+		{
a5ef24
+		do_verify = 0;
a5ef24
+		args++;
a5ef24
+		argn--;
a5ef24
+		}
a5ef24
+
a5ef24
+	if (argn && !strcmp(*args, "-exout"))
a5ef24
+		{
a5ef24
+		exout = 1;
a5ef24
+		args++;
a5ef24
+		argn--;
a5ef24
+		}
a5ef24
+
a5ef24
+	if (do_verify == -1)
a5ef24
+		{
a5ef24
+		fprintf(stderr,"%s [ecdhver|ecdhgen|] [-exout] (infile outfile)\n",argv[0]);
a5ef24
+		exit(1);
a5ef24
+		}
a5ef24
+
a5ef24
+	if (argn == 2)
a5ef24
+		{
a5ef24
+		in = fopen(*args, "r");
a5ef24
+		if (!in)
a5ef24
+			{
a5ef24
+			fprintf(stderr, "Error opening input file\n");
a5ef24
+			exit(1);
a5ef24
+			}
a5ef24
+		out = fopen(args[1], "w");
a5ef24
+		if (!out)
a5ef24
+			{
a5ef24
+			fprintf(stderr, "Error opening output file\n");
a5ef24
+			exit(1);
a5ef24
+			}
a5ef24
+		}
a5ef24
+	else if (argn == 0)
a5ef24
+		{
a5ef24
+		in = stdin;
a5ef24
+		out = stdout;
a5ef24
+		}
a5ef24
+	else
a5ef24
+		{
a5ef24
+		fprintf(stderr,"%s [dhver|dhgen|] [-exout] (infile outfile)\n",argv[0]);
a5ef24
+		exit(1);
a5ef24
+		}
a5ef24
+
a5ef24
+	while (fgets(buf, sizeof(buf), in) != NULL)
a5ef24
+		{
a5ef24
+		fputs(buf, out);
a5ef24
+		if (buf[0] == '[' && buf[1] == 'E')
a5ef24
+			{
a5ef24
+			int c = buf[2];
a5ef24
+			if (c < 'A' || c > 'E')
a5ef24
+				goto parse_error;
a5ef24
+			param_set = c - 'A';
a5ef24
+			/* If just [E?] then initial paramset */
a5ef24
+			if (buf[3] == ']')
a5ef24
+				continue;
a5ef24
+			if (group)
a5ef24
+				EC_GROUP_free(group);
a5ef24
+			group = EC_GROUP_new_by_curve_name(curve_nids[c - 'A']);
a5ef24
+			}
a5ef24
+		if (strlen(buf) > 10 && !strncmp(buf, "[Curve", 6))
a5ef24
+			{
a5ef24
+			int nid;
a5ef24
+			if (param_set == -1)
a5ef24
+				goto parse_error;
a5ef24
+			nid = lookup_curve(buf);
a5ef24
+			if (nid == NID_undef)
a5ef24
+				goto parse_error;
a5ef24
+			curve_nids[param_set] = nid;
a5ef24
+			}
a5ef24
+
a5ef24
+		if (strlen(buf) > 4 && buf[0] == '[' && buf[2] == '-')
a5ef24
+			{
a5ef24
+			int nid = lookup_curve2(buf + 1);
a5ef24
+			if (nid == NID_undef)
a5ef24
+				goto parse_error;
a5ef24
+			if (group)
a5ef24
+				EC_GROUP_free(group);
a5ef24
+			group = EC_GROUP_new_by_curve_name(nid);
a5ef24
+			if (!group)
a5ef24
+				{
a5ef24
+				fprintf(stderr, "ERROR: unsupported curve %s\n", buf + 1);
a5ef24
+				return 1;
a5ef24
+				}
a5ef24
+			}
a5ef24
+
a5ef24
+		if (strlen(buf) > 6 && !strncmp(buf, "[E", 2))
a5ef24
+			{
a5ef24
+			md = eparse_md(buf);
a5ef24
+			if (md == NULL)
a5ef24
+				goto parse_error;
a5ef24
+			continue;
a5ef24
+			}
a5ef24
+		if (!parse_line(&keyword, &value, lbuf, buf))
a5ef24
+			continue;
a5ef24
+		if (!strcmp(keyword, "QeCAVSx") || !strcmp(keyword, "QCAVSx"))
a5ef24
+			{
a5ef24
+			if (!do_hex2bn(&cx, value))
a5ef24
+				goto parse_error;
a5ef24
+			}
a5ef24
+		else if (!strcmp(keyword, "QeCAVSy") || !strcmp(keyword, "QCAVSy"))
a5ef24
+			{
a5ef24
+			if (!do_hex2bn(&cy, value))
a5ef24
+				goto parse_error;
a5ef24
+			if (do_verify == 0)
a5ef24
+				ec_output_Zhash(out, exout, group,
a5ef24
+						NULL, NULL, NULL,
a5ef24
+						cx, cy, md, rhash, rhashlen);
a5ef24
+			}
a5ef24
+		else if (!strcmp(keyword, "deIUT"))
a5ef24
+			{
a5ef24
+			if (!do_hex2bn(&id, value))
a5ef24
+				goto parse_error;
a5ef24
+			}
a5ef24
+		else if (!strcmp(keyword, "QeIUTx"))
a5ef24
+			{
a5ef24
+			if (!do_hex2bn(&ix, value))
a5ef24
+				goto parse_error;
a5ef24
+			}
a5ef24
+		else if (!strcmp(keyword, "QeIUTy"))
a5ef24
+			{
a5ef24
+			if (!do_hex2bn(&iy, value))
a5ef24
+				goto parse_error;
a5ef24
+			}
a5ef24
+		else if (!strcmp(keyword, "CAVSHashZZ"))
a5ef24
+			{
a5ef24
+			if (!md)
a5ef24
+				goto parse_error;
a5ef24
+			rhash = hex2bin_m(value, &rhashlen);
a5ef24
+			if (!rhash || rhashlen != M_EVP_MD_size(md))
a5ef24
+				goto parse_error;
a5ef24
+			ec_output_Zhash(out, exout, group, ix, iy, id, cx, cy,
a5ef24
+					md, rhash, rhashlen);
a5ef24
+			}
a5ef24
+		}
a5ef24
+	rv = 0;
a5ef24
+	parse_error:
a5ef24
+	if (id)
a5ef24
+		BN_free(id);
a5ef24
+	if (ix)
a5ef24
+		BN_free(ix);
a5ef24
+	if (iy)
a5ef24
+		BN_free(iy);
a5ef24
+	if (cx)
a5ef24
+		BN_free(cx);
a5ef24
+	if (cy)
a5ef24
+		BN_free(cy);
a5ef24
+	if (group)
a5ef24
+		EC_GROUP_free(group);
a5ef24
+	if (in && in != stdin)
a5ef24
+		fclose(in);
a5ef24
+	if (out && out != stdout)
a5ef24
+		fclose(out);
a5ef24
+	if (rv)
a5ef24
+		fprintf(stderr, "Error Parsing request file\n");
a5ef24
+	return rv;
a5ef24
+	}
a5ef24
+
a5ef24
+#endif
a5ef24
diff -up openssl-1.0.1e/crypto/fips/cavs/fips_ecdsavs.c.fips-ec openssl-1.0.1e/crypto/fips/cavs/fips_ecdsavs.c
a5ef24
--- openssl-1.0.1e/crypto/fips/cavs/fips_ecdsavs.c.fips-ec	2013-11-08 17:59:43.150028068 +0100
a5ef24
+++ openssl-1.0.1e/crypto/fips/cavs/fips_ecdsavs.c	2013-11-08 17:59:43.150028068 +0100
a5ef24
@@ -0,0 +1,533 @@
a5ef24
+/* fips/ecdsa/fips_ecdsavs.c */
a5ef24
+/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
a5ef24
+ * project.
a5ef24
+ */
a5ef24
+/* ====================================================================
a5ef24
+ * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
a5ef24
+ *
a5ef24
+ * Redistribution and use in source and binary forms, with or without
a5ef24
+ * modification, are permitted provided that the following conditions
a5ef24
+ * are met:
a5ef24
+ *
a5ef24
+ * 1. Redistributions of source code must retain the above copyright
a5ef24
+ *    notice, this list of conditions and the following disclaimer. 
a5ef24
+ *
a5ef24
+ * 2. Redistributions in binary form must reproduce the above copyright
a5ef24
+ *    notice, this list of conditions and the following disclaimer in
a5ef24
+ *    the documentation and/or other materials provided with the
a5ef24
+ *    distribution.
a5ef24
+ *
a5ef24
+ * 3. All advertising materials mentioning features or use of this
a5ef24
+ *    software must display the following acknowledgment:
a5ef24
+ *    "This product includes software developed by the OpenSSL Project
a5ef24
+ *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
a5ef24
+ *
a5ef24
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
a5ef24
+ *    endorse or promote products derived from this software without
a5ef24
+ *    prior written permission. For written permission, please contact
a5ef24
+ *    licensing@OpenSSL.org.
a5ef24
+ *
a5ef24
+ * 5. Products derived from this software may not be called "OpenSSL"
a5ef24
+ *    nor may "OpenSSL" appear in their names without prior written
a5ef24
+ *    permission of the OpenSSL Project.
a5ef24
+ *
a5ef24
+ * 6. Redistributions of any form whatsoever must retain the following
a5ef24
+ *    acknowledgment:
a5ef24
+ *    "This product includes software developed by the OpenSSL Project
a5ef24
+ *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
a5ef24
+ *
a5ef24
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
a5ef24
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
a5ef24
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
a5ef24
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
a5ef24
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
a5ef24
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
a5ef24
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
a5ef24
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
a5ef24
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
a5ef24
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
a5ef24
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
a5ef24
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
a5ef24
+ * ====================================================================
a5ef24
+ */
a5ef24
+
a5ef24
+#define OPENSSL_FIPSAPI
a5ef24
+#include <openssl/opensslconf.h>
a5ef24
+#include <stdio.h>
a5ef24
+
a5ef24
+#ifndef OPENSSL_FIPS
a5ef24
+
a5ef24
+int main(int argc, char **argv)
a5ef24
+{
a5ef24
+    printf("No FIPS ECDSA support\n");
a5ef24
+    return(0);
a5ef24
+}
a5ef24
+#else
a5ef24
+
a5ef24
+#include <string.h>
a5ef24
+#include <ctype.h>
a5ef24
+#include <openssl/err.h>
a5ef24
+#include <openssl/bn.h>
a5ef24
+#include <openssl/ecdsa.h>
a5ef24
+#include <openssl/evp.h>
a5ef24
+#include "fips_utl.h"
a5ef24
+
a5ef24
+#include <openssl/objects.h>
a5ef24
+
a5ef24
+
a5ef24
+static int elookup_curve(char *in, char *curve_name, const EVP_MD **pmd)
a5ef24
+	{
a5ef24
+	char *cname, *p;
a5ef24
+	/* Copy buffer as we will change it */
a5ef24
+	strcpy(curve_name, in);
a5ef24
+	cname = curve_name + 1;
a5ef24
+	p = strchr(cname, ']');
a5ef24
+	if (!p)
a5ef24
+		{
a5ef24
+		fprintf(stderr, "Parse error: missing ]\n");
a5ef24
+		return NID_undef;
a5ef24
+		}
a5ef24
+	*p = 0;
a5ef24
+	p = strchr(cname, ',');
a5ef24
+	if (p)
a5ef24
+		{
a5ef24
+		if (!pmd)
a5ef24
+			{
a5ef24
+			fprintf(stderr, "Parse error: unexpected digest\n");
a5ef24
+			return NID_undef;
a5ef24
+			}
a5ef24
+		*p = 0;
a5ef24
+		p++;
a5ef24
+
a5ef24
+		if (!strcmp(p, "SHA-1"))
a5ef24
+			*pmd = EVP_sha1();
a5ef24
+		else if (!strcmp(p, "SHA-224"))
a5ef24
+			*pmd = EVP_sha224();
a5ef24
+		else if (!strcmp(p, "SHA-256"))
a5ef24
+			*pmd = EVP_sha256();
a5ef24
+		else if (!strcmp(p, "SHA-384"))
a5ef24
+			*pmd = EVP_sha384();
a5ef24
+		else if (!strcmp(p, "SHA-512"))
a5ef24
+			*pmd = EVP_sha512();
a5ef24
+		else
a5ef24
+			{
a5ef24
+			fprintf(stderr, "Unknown digest %s\n", p);
a5ef24
+			return NID_undef;
a5ef24
+			}
a5ef24
+		}
a5ef24
+	else if(pmd)
a5ef24
+		*pmd = EVP_sha1();
a5ef24
+
a5ef24
+	if (!strcmp(cname, "B-163"))
a5ef24
+		return NID_sect163r2;
a5ef24
+	if (!strcmp(cname, "B-233"))
a5ef24
+		return NID_sect233r1;
a5ef24
+	if (!strcmp(cname, "B-283"))
a5ef24
+		return NID_sect283r1;
a5ef24
+	if (!strcmp(cname, "B-409"))
a5ef24
+		return NID_sect409r1;
a5ef24
+	if (!strcmp(cname, "B-571"))
a5ef24
+		return NID_sect571r1;
a5ef24
+	if (!strcmp(cname, "K-163"))
a5ef24
+		return NID_sect163k1;
a5ef24
+	if (!strcmp(cname, "K-233"))
a5ef24
+		return NID_sect233k1;
a5ef24
+	if (!strcmp(cname, "K-283"))
a5ef24
+		return NID_sect283k1;
a5ef24
+	if (!strcmp(cname, "K-409"))
a5ef24
+		return NID_sect409k1;
a5ef24
+	if (!strcmp(cname, "K-571"))
a5ef24
+		return NID_sect571k1;
a5ef24
+	if (!strcmp(cname, "P-192"))
a5ef24
+		return NID_X9_62_prime192v1;
a5ef24
+	if (!strcmp(cname, "P-224"))
a5ef24
+		return NID_secp224r1;
a5ef24
+	if (!strcmp(cname, "P-256"))
a5ef24
+		return NID_X9_62_prime256v1;
a5ef24
+	if (!strcmp(cname, "P-384"))
a5ef24
+		return NID_secp384r1;
a5ef24
+	if (!strcmp(cname, "P-521"))
a5ef24
+		return NID_secp521r1;
a5ef24
+
a5ef24
+	fprintf(stderr, "Unknown Curve name %s\n", cname);
a5ef24
+	return NID_undef;
a5ef24
+	}
a5ef24
+
a5ef24
+static int ec_get_pubkey(EC_KEY *key, BIGNUM *x, BIGNUM *y)
a5ef24
+	{
a5ef24
+	const EC_POINT *pt;
a5ef24
+	const EC_GROUP *grp;
a5ef24
+	const EC_METHOD *meth;
a5ef24
+	int rv;
a5ef24
+	BN_CTX *ctx;
a5ef24
+	ctx = BN_CTX_new();
a5ef24
+	if (!ctx)
a5ef24
+		return 0;
a5ef24
+	grp = EC_KEY_get0_group(key);
a5ef24
+	pt = EC_KEY_get0_public_key(key);
a5ef24
+	meth = EC_GROUP_method_of(grp);
a5ef24
+	if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
a5ef24
+		rv = EC_POINT_get_affine_coordinates_GFp(grp, pt, x, y, ctx);
a5ef24
+	else
a5ef24
+#ifdef OPENSSL_NO_EC2M
a5ef24
+		{
a5ef24
+		fprintf(stderr, "ERROR: GF2m not supported\n");
a5ef24
+		exit(1);
a5ef24
+		}
a5ef24
+#else
a5ef24
+		rv = EC_POINT_get_affine_coordinates_GF2m(grp, pt, x, y, ctx);
a5ef24
+#endif
a5ef24
+
a5ef24
+	BN_CTX_free(ctx);
a5ef24
+
a5ef24
+	return rv;
a5ef24
+
a5ef24
+	}
a5ef24
+
a5ef24
+static int KeyPair(FILE *in, FILE *out)
a5ef24
+	{
a5ef24
+	char buf[2048], lbuf[2048];
a5ef24
+	char *keyword, *value;
a5ef24
+	int curve_nid = NID_undef;
a5ef24
+	int i, count;
a5ef24
+	BIGNUM *Qx = NULL, *Qy = NULL;
a5ef24
+	const BIGNUM *d = NULL;
a5ef24
+	EC_KEY *key = NULL;
a5ef24
+	Qx = BN_new();
a5ef24
+	Qy = BN_new();
a5ef24
+	while(fgets(buf, sizeof buf, in) != NULL)
a5ef24
+		{
a5ef24
+		if (*buf == '[' && buf[2] == '-')
a5ef24
+			{
a5ef24
+			if (buf[2] == '-')
a5ef24
+			curve_nid = elookup_curve(buf, lbuf, NULL);
a5ef24
+			fputs(buf, out);
a5ef24
+			continue;
a5ef24
+			}
a5ef24
+		if (!parse_line(&keyword, &value, lbuf, buf))
a5ef24
+			{
a5ef24
+			fputs(buf, out);
a5ef24
+			continue;
a5ef24
+			}
a5ef24
+		if (!strcmp(keyword, "N"))
a5ef24
+			{
a5ef24
+			count = atoi(value);
a5ef24
+
a5ef24
+			for (i = 0; i < count; i++)
a5ef24
+				{
a5ef24
+
a5ef24
+				key = EC_KEY_new_by_curve_name(curve_nid);
a5ef24
+				if (!EC_KEY_generate_key(key))
a5ef24
+					{
a5ef24
+					fprintf(stderr, "Error generating key\n");
a5ef24
+					return 0;
a5ef24
+					}
a5ef24
+
a5ef24
+				if (!ec_get_pubkey(key, Qx, Qy))
a5ef24
+					{
a5ef24
+					fprintf(stderr, "Error getting public key\n");
a5ef24
+					return 0;
a5ef24
+					}
a5ef24
+
a5ef24
+				d = EC_KEY_get0_private_key(key);
a5ef24
+
a5ef24
+				do_bn_print_name(out, "d", d);
a5ef24
+				do_bn_print_name(out, "Qx", Qx);
a5ef24
+				do_bn_print_name(out, "Qy", Qy);
a5ef24
+				fputs(RESP_EOL, out);
a5ef24
+				EC_KEY_free(key);
a5ef24
+
a5ef24
+				}
a5ef24
+
a5ef24
+			}
a5ef24
+
a5ef24
+		}
a5ef24
+	BN_free(Qx);
a5ef24
+	BN_free(Qy);
a5ef24
+	return 1;
a5ef24
+	}
a5ef24
+
a5ef24
+static int PKV(FILE *in, FILE *out)
a5ef24
+	{
a5ef24
+
a5ef24
+	char buf[2048], lbuf[2048];
a5ef24
+	char *keyword, *value;
a5ef24
+	int curve_nid = NID_undef;
a5ef24
+	BIGNUM *Qx = NULL, *Qy = NULL;
a5ef24
+	EC_KEY *key = NULL;
a5ef24
+	while(fgets(buf, sizeof buf, in) != NULL)
a5ef24
+		{
a5ef24
+		fputs(buf, out);
a5ef24
+		if (*buf == '[' && buf[2] == '-')
a5ef24
+			{
a5ef24
+			curve_nid = elookup_curve(buf, lbuf, NULL);
a5ef24
+			if (curve_nid == NID_undef)
a5ef24
+				return 0;
a5ef24
+				
a5ef24
+			}
a5ef24
+		if (!parse_line(&keyword, &value, lbuf, buf))
a5ef24
+			continue;
a5ef24
+		if (!strcmp(keyword, "Qx"))
a5ef24
+			{
a5ef24
+			if (!do_hex2bn(&Qx, value))
a5ef24
+				{
a5ef24
+				fprintf(stderr, "Invalid Qx value\n");
a5ef24
+				return 0;
a5ef24
+				}
a5ef24
+			}
a5ef24
+		if (!strcmp(keyword, "Qy"))
a5ef24
+			{
a5ef24
+			int rv;
a5ef24
+			if (!do_hex2bn(&Qy, value))
a5ef24
+				{
a5ef24
+				fprintf(stderr, "Invalid Qy value\n");
a5ef24
+				return 0;
a5ef24
+				}
a5ef24
+			key = EC_KEY_new_by_curve_name(curve_nid);
a5ef24
+			no_err = 1;
a5ef24
+			rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
a5ef24
+			no_err = 0;
a5ef24
+			EC_KEY_free(key);
a5ef24
+			fprintf(out, "Result = %s" RESP_EOL, rv ? "P":"F");
a5ef24
+			}
a5ef24
+
a5ef24
+		}
a5ef24
+	BN_free(Qx);
a5ef24
+	BN_free(Qy);
a5ef24
+	return 1;
a5ef24
+	}
a5ef24
+
a5ef24
+static int SigGen(FILE *in, FILE *out)
a5ef24
+	{
a5ef24
+	char buf[2048], lbuf[2048];
a5ef24
+	char *keyword, *value;
a5ef24
+	unsigned char *msg;
a5ef24
+	int curve_nid = NID_undef;
a5ef24
+	long mlen;
a5ef24
+	BIGNUM *Qx = NULL, *Qy = NULL;
a5ef24
+	EC_KEY *key = NULL;
a5ef24
+	ECDSA_SIG *sig = NULL;
a5ef24
+	const EVP_MD *digest = NULL;
a5ef24
+	Qx = BN_new();
a5ef24
+	Qy = BN_new();
a5ef24
+	while(fgets(buf, sizeof buf, in) != NULL)
a5ef24
+		{
a5ef24
+		fputs(buf, out);
a5ef24
+		if (*buf == '[')
a5ef24
+			{
a5ef24
+			curve_nid = elookup_curve(buf, lbuf, &digest);
a5ef24
+			if (curve_nid == NID_undef)
a5ef24
+				return 0;
a5ef24
+			}
a5ef24
+		if (!parse_line(&keyword, &value, lbuf, buf))
a5ef24
+			continue;
a5ef24
+		if (!strcmp(keyword, "Msg"))
a5ef24
+			{
a5ef24
+			msg = hex2bin_m(value, &mlen);
a5ef24
+			if (!msg)
a5ef24
+				{
a5ef24
+				fprintf(stderr, "Invalid Message\n");
a5ef24
+				return 0;
a5ef24
+				}
a5ef24
+
a5ef24
+			key = EC_KEY_new_by_curve_name(curve_nid);
a5ef24
+			if (!EC_KEY_generate_key(key))
a5ef24
+				{
a5ef24
+				fprintf(stderr, "Error generating key\n");
a5ef24
+				return 0;
a5ef24
+				}
a5ef24
+
a5ef24
+			if (!ec_get_pubkey(key, Qx, Qy))
a5ef24
+				{
a5ef24
+				fprintf(stderr, "Error getting public key\n");
a5ef24
+				return 0;
a5ef24
+				}
a5ef24
+
a5ef24
+	    		sig = FIPS_ecdsa_sign(key, msg, mlen, digest);
a5ef24
+
a5ef24
+			if (!sig)
a5ef24
+				{
a5ef24
+				fprintf(stderr, "Error signing message\n");
a5ef24
+				return 0;
a5ef24
+				}
a5ef24
+
a5ef24
+			do_bn_print_name(out, "Qx", Qx);
a5ef24
+			do_bn_print_name(out, "Qy", Qy);
a5ef24
+			do_bn_print_name(out, "R", sig->r);
a5ef24
+			do_bn_print_name(out, "S", sig->s);
a5ef24
+
a5ef24
+			EC_KEY_free(key);
a5ef24
+			OPENSSL_free(msg);
a5ef24
+			FIPS_ecdsa_sig_free(sig);
a5ef24
+
a5ef24
+			}
a5ef24
+
a5ef24
+		}
a5ef24
+	BN_free(Qx);
a5ef24
+	BN_free(Qy);
a5ef24
+	return 1;
a5ef24
+	}
a5ef24
+
a5ef24
+static int SigVer(FILE *in, FILE *out)
a5ef24
+	{
a5ef24
+	char buf[2048], lbuf[2048];
a5ef24
+	char *keyword, *value;
a5ef24
+	unsigned char *msg = NULL;
a5ef24
+	int curve_nid = NID_undef;
a5ef24
+	long mlen;
a5ef24
+	BIGNUM *Qx = NULL, *Qy = NULL;
a5ef24
+	EC_KEY *key = NULL;
a5ef24
+	ECDSA_SIG sg, *sig = &sg;
a5ef24
+	const EVP_MD *digest = NULL;
a5ef24
+	sig->r = NULL;
a5ef24
+	sig->s = NULL;
a5ef24
+	while(fgets(buf, sizeof buf, in) != NULL)
a5ef24
+		{
a5ef24
+		fputs(buf, out);
a5ef24
+		if (*buf == '[')
a5ef24
+			{
a5ef24
+			curve_nid = elookup_curve(buf, lbuf, &digest);
a5ef24
+			if (curve_nid == NID_undef)
a5ef24
+				return 0;
a5ef24
+			}
a5ef24
+		if (!parse_line(&keyword, &value, lbuf, buf))
a5ef24
+			continue;
a5ef24
+		if (!strcmp(keyword, "Msg"))
a5ef24
+			{
a5ef24
+			msg = hex2bin_m(value, &mlen);
a5ef24
+			if (!msg)
a5ef24
+				{
a5ef24
+				fprintf(stderr, "Invalid Message\n");
a5ef24
+				return 0;
a5ef24
+				}
a5ef24
+			}
a5ef24
+			
a5ef24
+		if (!strcmp(keyword, "Qx"))
a5ef24
+			{
a5ef24
+			if (!do_hex2bn(&Qx, value))
a5ef24
+				{
a5ef24
+				fprintf(stderr, "Invalid Qx value\n");
a5ef24
+				return 0;
a5ef24
+				}
a5ef24
+			}
a5ef24
+		if (!strcmp(keyword, "Qy"))
a5ef24
+			{
a5ef24
+			if (!do_hex2bn(&Qy, value))
a5ef24
+				{
a5ef24
+				fprintf(stderr, "Invalid Qy value\n");
a5ef24
+				return 0;
a5ef24
+				}
a5ef24
+			}
a5ef24
+		if (!strcmp(keyword, "R"))
a5ef24
+			{
a5ef24
+			if (!do_hex2bn(&sig->r, value))
a5ef24
+				{
a5ef24
+				fprintf(stderr, "Invalid R value\n");
a5ef24
+				return 0;
a5ef24
+				}
a5ef24
+			}
a5ef24
+		if (!strcmp(keyword, "S"))
a5ef24
+			{
a5ef24
+			int rv;
a5ef24
+			if (!do_hex2bn(&sig->s, value))
a5ef24
+				{
a5ef24
+				fprintf(stderr, "Invalid S value\n");
a5ef24
+				return 0;
a5ef24
+				}
a5ef24
+			key = EC_KEY_new_by_curve_name(curve_nid);
a5ef24
+			rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
a5ef24
+
a5ef24
+			if (rv != 1)
a5ef24
+				{
a5ef24
+				fprintf(stderr, "Error setting public key\n");
a5ef24
+				return 0;
a5ef24
+				}
a5ef24
+
a5ef24
+			no_err = 1;
a5ef24
+	    		rv = FIPS_ecdsa_verify(key, msg, mlen, digest, sig);
a5ef24
+			EC_KEY_free(key);
a5ef24
+			if (msg)
a5ef24
+				OPENSSL_free(msg);
a5ef24
+			no_err = 0;
a5ef24
+
a5ef24
+			fprintf(out, "Result = %s" RESP_EOL, rv ? "P":"F");
a5ef24
+			}
a5ef24
+
a5ef24
+		}
a5ef24
+	if (sig->r)
a5ef24
+		BN_free(sig->r);
a5ef24
+	if (sig->s)
a5ef24
+		BN_free(sig->s);
a5ef24
+	if (Qx)
a5ef24
+		BN_free(Qx);
a5ef24
+	if (Qy)
a5ef24
+		BN_free(Qy);
a5ef24
+	return 1;
a5ef24
+	}
a5ef24
+#ifdef FIPS_ALGVS
a5ef24
+int fips_ecdsavs_main(int argc, char **argv)
a5ef24
+#else
a5ef24
+int main(int argc, char **argv)
a5ef24
+#endif
a5ef24
+	{
a5ef24
+	FILE *in = NULL, *out = NULL;
a5ef24
+	const char *cmd = argv[1];
a5ef24
+	int rv = 0;
a5ef24
+	fips_algtest_init();
a5ef24
+
a5ef24
+	if (argc == 4)
a5ef24
+		{
a5ef24
+		in = fopen(argv[2], "r");
a5ef24
+		if (!in)
a5ef24
+			{
a5ef24
+			fprintf(stderr, "Error opening input file\n");
a5ef24
+			exit(1);
a5ef24
+			}
a5ef24
+		out = fopen(argv[3], "w");
a5ef24
+		if (!out)
a5ef24
+			{
a5ef24
+			fprintf(stderr, "Error opening output file\n");
a5ef24
+			exit(1);
a5ef24
+			}
a5ef24
+		}
a5ef24
+	else if (argc == 2)
a5ef24
+		{
a5ef24
+		in = stdin;
a5ef24
+		out = stdout;
a5ef24
+		}
a5ef24
+
a5ef24
+	if (!cmd)
a5ef24
+		{
a5ef24
+		fprintf(stderr, "fips_ecdsavs [KeyPair|PKV|SigGen|SigVer]\n");
a5ef24
+		return 1;
a5ef24
+		}
a5ef24
+	if (!strcmp(cmd, "KeyPair"))
a5ef24
+		rv = KeyPair(in, out);
a5ef24
+	else if (!strcmp(cmd, "PKV"))
a5ef24
+		rv = PKV(in, out);
a5ef24
+	else if (!strcmp(cmd, "SigVer"))
a5ef24
+		rv = SigVer(in, out);
a5ef24
+	else if (!strcmp(cmd, "SigGen"))
a5ef24
+		rv = SigGen(in, out);
a5ef24
+	else
a5ef24
+		{
a5ef24
+		fprintf(stderr, "Unknown command %s\n", cmd);
a5ef24
+		return 1;
a5ef24
+		}
a5ef24
+
a5ef24
+	if (argc == 4)
a5ef24
+		{
a5ef24
+		fclose(in);
a5ef24
+		fclose(out);
a5ef24
+		}
a5ef24
+
a5ef24
+	if (rv <= 0)
a5ef24
+		{
a5ef24
+		fprintf(stderr, "Error running %s\n", cmd);
a5ef24
+		return 1;
a5ef24
+		}
a5ef24
+
a5ef24
+	return 0;
a5ef24
+	}
a5ef24
+
a5ef24
+#endif
a5ef24
diff -up openssl-1.0.1e/crypto/fips/fips_ecdh_selftest.c.fips-ec openssl-1.0.1e/crypto/fips/fips_ecdh_selftest.c
a5ef24
--- openssl-1.0.1e/crypto/fips/fips_ecdh_selftest.c.fips-ec	2013-11-08 17:59:43.150028068 +0100
a5ef24
+++ openssl-1.0.1e/crypto/fips/fips_ecdh_selftest.c	2013-11-08 17:59:43.150028068 +0100
a5ef24
@@ -0,0 +1,252 @@
a5ef24
+/* fips/ecdh/fips_ecdh_selftest.c */
a5ef24
+/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
a5ef24
+ * project 2011.
a5ef24
+ */
a5ef24
+/* ====================================================================
a5ef24
+ * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
a5ef24
+ *
a5ef24
+ * Redistribution and use in source and binary forms, with or without
a5ef24
+ * modification, are permitted provided that the following conditions
a5ef24
+ * are met:
a5ef24
+ *
a5ef24
+ * 1. Redistributions of source code must retain the above copyright
a5ef24
+ *    notice, this list of conditions and the following disclaimer. 
a5ef24
+ *
a5ef24
+ * 2. Redistributions in binary form must reproduce the above copyright
a5ef24
+ *    notice, this list of conditions and the following disclaimer in
a5ef24
+ *    the documentation and/or other materials provided with the
a5ef24
+ *    distribution.
a5ef24
+ *
a5ef24
+ * 3. All advertising materials mentioning features or use of this
a5ef24
+ *    software must display the following acknowledgment:
a5ef24
+ *    "This product includes software developed by the OpenSSL Project
a5ef24
+ *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
a5ef24
+ *
a5ef24
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
a5ef24
+ *    endorse or promote products derived from this software without
a5ef24
+ *    prior written permission. For written permission, please contact
a5ef24
+ *    licensing@OpenSSL.org.
a5ef24
+ *
a5ef24
+ * 5. Products derived from this software may not be called "OpenSSL"
a5ef24
+ *    nor may "OpenSSL" appear in their names without prior written
a5ef24
+ *    permission of the OpenSSL Project.
a5ef24
+ *
a5ef24
+ * 6. Redistributions of any form whatsoever must retain the following
a5ef24
+ *    acknowledgment:
a5ef24
+ *    "This product includes software developed by the OpenSSL Project
a5ef24
+ *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
a5ef24
+ *
a5ef24
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
a5ef24
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
a5ef24
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
a5ef24
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
a5ef24
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
a5ef24
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
a5ef24
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
a5ef24
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
a5ef24
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
a5ef24
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
a5ef24
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
a5ef24
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
a5ef24
+ * ====================================================================
a5ef24
+ *
a5ef24
+ */
a5ef24
+
a5ef24
+#define OPENSSL_FIPSAPI
a5ef24
+
a5ef24
+#include <string.h>
a5ef24
+#include <openssl/crypto.h>
a5ef24
+#include <openssl/ec.h>
a5ef24
+#include <openssl/ecdh.h>
a5ef24
+#include <openssl/fips.h>
a5ef24
+#include <openssl/err.h>
a5ef24
+#include <openssl/evp.h>
a5ef24
+#include <openssl/bn.h>
a5ef24
+
a5ef24
+#ifdef OPENSSL_FIPS
a5ef24
+
a5ef24
+#include "fips_locl.h"
a5ef24
+
a5ef24
+static const unsigned char p256_qcavsx[] = {
a5ef24
+	0x52,0xc6,0xa5,0x75,0xf3,0x04,0x98,0xb3,0x29,0x66,0x0c,0x62,
a5ef24
+	0x18,0x60,0x55,0x41,0x59,0xd4,0x60,0x85,0x99,0xc1,0x51,0x13,
a5ef24
+	0x6f,0x97,0x85,0x93,0x33,0x34,0x07,0x50
a5ef24
+};
a5ef24
+static const unsigned char p256_qcavsy[] = {
a5ef24
+	0x6f,0x69,0x24,0xeb,0xe9,0x3b,0xa7,0xcc,0x47,0x17,0xaa,0x3f,
a5ef24
+	0x70,0xfc,0x10,0x73,0x0a,0xcd,0x21,0xee,0x29,0x19,0x1f,0xaf,
a5ef24
+	0xb4,0x1c,0x1e,0xc2,0x8e,0x97,0x81,0x6e
a5ef24
+};
a5ef24
+static const unsigned char p256_qiutx[] = {
a5ef24
+	0x71,0x46,0x88,0x08,0x92,0x21,0x1b,0x10,0x21,0x74,0xff,0x0c,
a5ef24
+	0x94,0xde,0x34,0x7c,0x86,0x74,0xbe,0x67,0x41,0x68,0xd4,0xc1,
a5ef24
+	0xe5,0x75,0x63,0x9c,0xa7,0x46,0x93,0x6f
a5ef24
+};
a5ef24
+static const unsigned char p256_qiuty[] = {
a5ef24
+	0x33,0x40,0xa9,0x6a,0xf5,0x20,0xb5,0x9e,0xfc,0x60,0x1a,0xae,
a5ef24
+	0x3d,0xf8,0x21,0xd2,0xa7,0xca,0x52,0x34,0xb9,0x5f,0x27,0x75,
a5ef24
+	0x6c,0x81,0xbe,0x32,0x4d,0xba,0xbb,0xf8
a5ef24
+};
a5ef24
+static const unsigned char p256_qiutd[] = {
a5ef24
+	0x1a,0x48,0x55,0x6b,0x11,0xbe,0x92,0xd4,0x1c,0xd7,0x45,0xc3,
a5ef24
+	0x82,0x81,0x51,0xf1,0x23,0x40,0xb7,0x83,0xfd,0x01,0x6d,0xbc,
a5ef24
+	0xa1,0x66,0xaf,0x0a,0x03,0x23,0xcd,0xc8
a5ef24
+};
a5ef24
+static const unsigned char p256_ziut[] = {
a5ef24
+	0x77,0x2a,0x1e,0x37,0xee,0xe6,0x51,0x02,0x71,0x40,0xf8,0x6a,
a5ef24
+	0x36,0xf8,0x65,0x61,0x2b,0x18,0x71,0x82,0x23,0xe6,0xf2,0x77,
a5ef24
+	0xce,0xec,0xb8,0x49,0xc7,0xbf,0x36,0x4f
a5ef24
+};
a5ef24
+
a5ef24
+
a5ef24
+typedef struct 
a5ef24
+	{
a5ef24
+	int curve;
a5ef24
+	const unsigned char *x1;
a5ef24
+	size_t x1len;
a5ef24
+	const unsigned char *y1;
a5ef24
+	size_t y1len;
a5ef24
+	const unsigned char *d1;
a5ef24
+	size_t d1len;
a5ef24
+	const unsigned char *x2;
a5ef24
+	size_t x2len;
a5ef24
+	const unsigned char *y2;
a5ef24
+	size_t y2len;
a5ef24
+	const unsigned char *z;
a5ef24
+	size_t zlen;
a5ef24
+	} ECDH_SELFTEST_DATA;
a5ef24
+
a5ef24
+#define make_ecdh_test(nid, pr) { nid, \
a5ef24
+				pr##_qiutx, sizeof(pr##_qiutx), \
a5ef24
+				pr##_qiuty, sizeof(pr##_qiuty), \
a5ef24
+				pr##_qiutd, sizeof(pr##_qiutd), \
a5ef24
+				pr##_qcavsx, sizeof(pr##_qcavsx), \
a5ef24
+				pr##_qcavsy, sizeof(pr##_qcavsy), \
a5ef24
+				pr##_ziut, sizeof(pr##_ziut) }
a5ef24
+
a5ef24
+static ECDH_SELFTEST_DATA test_ecdh_data[] = 
a5ef24
+	{
a5ef24
+	make_ecdh_test(NID_X9_62_prime256v1, p256),
a5ef24
+	};
a5ef24
+
a5ef24
+int FIPS_selftest_ecdh(void)
a5ef24
+	{
a5ef24
+	EC_KEY *ec1 = NULL, *ec2 = NULL;
a5ef24
+	const EC_POINT *ecp = NULL;
a5ef24
+	BIGNUM *x = NULL, *y = NULL, *d = NULL;
a5ef24
+	unsigned char *ztmp = NULL;
a5ef24
+	int rv = 1;
a5ef24
+	size_t i;
a5ef24
+
a5ef24
+	for (i = 0; i < sizeof(test_ecdh_data)/sizeof(ECDH_SELFTEST_DATA); i++)
a5ef24
+		{
a5ef24
+		ECDH_SELFTEST_DATA *ecd = test_ecdh_data + i;
a5ef24
+		if (!fips_post_started(FIPS_TEST_ECDH, ecd->curve, 0))
a5ef24
+			continue;
a5ef24
+		ztmp = OPENSSL_malloc(ecd->zlen);
a5ef24
+
a5ef24
+		x = BN_bin2bn(ecd->x1, ecd->x1len, x);
a5ef24
+		y = BN_bin2bn(ecd->y1, ecd->y1len, y);
a5ef24
+		d = BN_bin2bn(ecd->d1, ecd->d1len, d);
a5ef24
+
a5ef24
+		if (!x || !y || !d || !ztmp)
a5ef24
+			{
a5ef24
+			rv = -1;
a5ef24
+			goto err;
a5ef24
+			}
a5ef24
+
a5ef24
+		ec1 = EC_KEY_new_by_curve_name(ecd->curve);
a5ef24
+		if (!ec1)
a5ef24
+			{
a5ef24
+			rv = -1;
a5ef24
+			goto err;
a5ef24
+			}
a5ef24
+		EC_KEY_set_flags(ec1, EC_FLAG_COFACTOR_ECDH);
a5ef24
+
a5ef24
+		if (!EC_KEY_set_public_key_affine_coordinates(ec1, x, y))
a5ef24
+			{
a5ef24
+			rv = -1;
a5ef24
+			goto err;
a5ef24
+			}
a5ef24
+
a5ef24
+		if (!EC_KEY_set_private_key(ec1, d))
a5ef24
+			{
a5ef24
+			rv = -1;
a5ef24
+			goto err;
a5ef24
+			}
a5ef24
+
a5ef24
+		x = BN_bin2bn(ecd->x2, ecd->x2len, x);
a5ef24
+		y = BN_bin2bn(ecd->y2, ecd->y2len, y);
a5ef24
+
a5ef24
+		if (!x || !y)
a5ef24
+			{
a5ef24
+			rv = -1;
a5ef24
+			goto err;
a5ef24
+			}
a5ef24
+
a5ef24
+		ec2 = EC_KEY_new_by_curve_name(ecd->curve);
a5ef24
+		if (!ec2)
a5ef24
+			{
a5ef24
+			rv = -1;
a5ef24
+			goto err;
a5ef24
+			}
a5ef24
+		EC_KEY_set_flags(ec1, EC_FLAG_COFACTOR_ECDH);
a5ef24
+
a5ef24
+		if (!EC_KEY_set_public_key_affine_coordinates(ec2, x, y))
a5ef24
+			{
a5ef24
+			rv = -1;
a5ef24
+			goto err;
a5ef24
+			}
a5ef24
+
a5ef24
+		ecp = EC_KEY_get0_public_key(ec2);
a5ef24
+		if (!ecp)
a5ef24
+			{
a5ef24
+			rv = -1;
a5ef24
+			goto err;
a5ef24
+			}
a5ef24
+
a5ef24
+		if (!ECDH_compute_key(ztmp, ecd->zlen, ecp, ec1, 0))
a5ef24
+			{
a5ef24
+			rv = -1;
a5ef24
+			goto err;
a5ef24
+			}
a5ef24
+
a5ef24
+		if (!fips_post_corrupt(FIPS_TEST_ECDH, ecd->curve, NULL))
a5ef24
+			ztmp[0] ^= 0x1;
a5ef24
+
a5ef24
+		if (memcmp(ztmp, ecd->z, ecd->zlen))
a5ef24
+			{
a5ef24
+			fips_post_failed(FIPS_TEST_ECDH, ecd->curve, 0);
a5ef24
+			rv = 0;
a5ef24
+			}
a5ef24
+		else if (!fips_post_success(FIPS_TEST_ECDH, ecd->curve, 0))
a5ef24
+			goto err;
a5ef24
+
a5ef24
+		EC_KEY_free(ec1);
a5ef24
+		ec1 = NULL;
a5ef24
+		EC_KEY_free(ec2);
a5ef24
+		ec2 = NULL;
a5ef24
+		OPENSSL_free(ztmp);
a5ef24
+		ztmp = NULL;
a5ef24
+		}
a5ef24
+
a5ef24
+	err:
a5ef24
+
a5ef24
+	if (x)
a5ef24
+		BN_clear_free(x);
a5ef24
+	if (y)
a5ef24
+		BN_clear_free(y);
a5ef24
+	if (d)
a5ef24
+		BN_clear_free(d);
a5ef24
+	if (ec1)
a5ef24
+		EC_KEY_free(ec1);
a5ef24
+	if (ec2)
a5ef24
+		EC_KEY_free(ec2);
a5ef24
+	if (ztmp)
a5ef24
+		OPENSSL_free(ztmp);
a5ef24
+
a5ef24
+	return rv;
a5ef24
+
a5ef24
+	}
a5ef24
+
a5ef24
+#endif
a5ef24
diff -up openssl-1.0.1e/crypto/fips/fips_ecdsa_selftest.c.fips-ec openssl-1.0.1e/crypto/fips/fips_ecdsa_selftest.c
a5ef24
--- openssl-1.0.1e/crypto/fips/fips_ecdsa_selftest.c.fips-ec	2013-11-08 17:59:43.150028068 +0100
a5ef24
+++ openssl-1.0.1e/crypto/fips/fips_ecdsa_selftest.c	2013-11-08 17:59:43.150028068 +0100
a5ef24
@@ -0,0 +1,167 @@
a5ef24
+/* fips/ecdsa/fips_ecdsa_selftest.c */
a5ef24
+/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
a5ef24
+ * project 2011.
a5ef24
+ */
a5ef24
+/* ====================================================================
a5ef24
+ * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
a5ef24
+ *
a5ef24
+ * Redistribution and use in source and binary forms, with or without
a5ef24
+ * modification, are permitted provided that the following conditions
a5ef24
+ * are met:
a5ef24
+ *
a5ef24
+ * 1. Redistributions of source code must retain the above copyright
a5ef24
+ *    notice, this list of conditions and the following disclaimer. 
a5ef24
+ *
a5ef24
+ * 2. Redistributions in binary form must reproduce the above copyright
a5ef24
+ *    notice, this list of conditions and the following disclaimer in
a5ef24
+ *    the documentation and/or other materials provided with the
a5ef24
+ *    distribution.
a5ef24
+ *
a5ef24
+ * 3. All advertising materials mentioning features or use of this
a5ef24
+ *    software must display the following acknowledgment:
a5ef24
+ *    "This product includes software developed by the OpenSSL Project
a5ef24
+ *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
a5ef24
+ *
a5ef24
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
a5ef24
+ *    endorse or promote products derived from this software without
a5ef24
+ *    prior written permission. For written permission, please contact
a5ef24
+ *    licensing@OpenSSL.org.
a5ef24
+ *
a5ef24
+ * 5. Products derived from this software may not be called "OpenSSL"
a5ef24
+ *    nor may "OpenSSL" appear in their names without prior written
a5ef24
+ *    permission of the OpenSSL Project.
a5ef24
+ *
a5ef24
+ * 6. Redistributions of any form whatsoever must retain the following
a5ef24
+ *    acknowledgment:
a5ef24
+ *    "This product includes software developed by the OpenSSL Project
a5ef24
+ *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
a5ef24
+ *
a5ef24
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
a5ef24
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
a5ef24
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
a5ef24
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
a5ef24
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
a5ef24
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
a5ef24
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
a5ef24
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
a5ef24
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
a5ef24
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
a5ef24
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
a5ef24
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
a5ef24
+ * ====================================================================
a5ef24
+ *
a5ef24
+ */
a5ef24
+
a5ef24
+#define OPENSSL_FIPSAPI
a5ef24
+
a5ef24
+#include <string.h>
a5ef24
+#include <openssl/crypto.h>
a5ef24
+#include <openssl/ec.h>
a5ef24
+#include <openssl/ecdsa.h>
a5ef24
+#include <openssl/fips.h>
a5ef24
+#include <openssl/err.h>
a5ef24
+#include <openssl/evp.h>
a5ef24
+#include <openssl/bn.h>
a5ef24
+
a5ef24
+#ifdef OPENSSL_FIPS
a5ef24
+
a5ef24
+static const char P_256_name[] = "ECDSA P-256";
a5ef24
+
a5ef24
+static const unsigned char P_256_d[] = {
a5ef24
+	0x51,0xbd,0x06,0xa1,0x1c,0xda,0xe2,0x12,0x99,0xc9,0x52,0x3f,
a5ef24
+	0xea,0xa4,0xd2,0xd1,0xf4,0x7f,0xd4,0x3e,0xbd,0xf8,0xfc,0x87,
a5ef24
+	0xdc,0x82,0x53,0x21,0xee,0xa0,0xdc,0x64
a5ef24
+};
a5ef24
+static const unsigned char P_256_qx[] = {
a5ef24
+	0x23,0x89,0xe0,0xf4,0x69,0xe0,0x49,0xe5,0xc7,0xe5,0x40,0x6e,
a5ef24
+	0x8f,0x25,0xdd,0xad,0x11,0x16,0x14,0x9b,0xab,0x44,0x06,0x31,
a5ef24
+	0xbf,0x5e,0xa6,0x44,0xac,0x86,0x00,0x07
a5ef24
+};
a5ef24
+static const unsigned char P_256_qy[] = {
a5ef24
+	0xb3,0x05,0x0d,0xd0,0xdc,0xf7,0x40,0xe6,0xf9,0xd8,0x6d,0x7b,
a5ef24
+	0x63,0xca,0x97,0xe6,0x12,0xf9,0xd4,0x18,0x59,0xbe,0xb2,0x5e,
a5ef24
+	0x4a,0x6a,0x77,0x23,0xf4,0x11,0x9d,0xeb
a5ef24
+};
a5ef24
+
a5ef24
+typedef struct 
a5ef24
+	{
a5ef24
+	int curve;
a5ef24
+	const char *name;
a5ef24
+	const unsigned char *x;
a5ef24
+	size_t xlen;
a5ef24
+	const unsigned char *y;
a5ef24
+	size_t ylen;
a5ef24
+	const unsigned char *d;
a5ef24
+	size_t dlen;
a5ef24
+	} EC_SELFTEST_DATA;
a5ef24
+
a5ef24
+#define make_ecdsa_test(nid, pr) { nid, pr##_name, \
a5ef24
+				pr##_qx, sizeof(pr##_qx), \
a5ef24
+				pr##_qy, sizeof(pr##_qy), \
a5ef24
+				pr##_d, sizeof(pr##_d)}
a5ef24
+
a5ef24
+static EC_SELFTEST_DATA test_ec_data[] = 
a5ef24
+	{
a5ef24
+	make_ecdsa_test(NID_X9_62_prime256v1, P_256),
a5ef24
+	};
a5ef24
+
a5ef24
+int FIPS_selftest_ecdsa()
a5ef24
+	{
a5ef24
+	EC_KEY *ec = NULL;
a5ef24
+	BIGNUM *x = NULL, *y = NULL, *d = NULL;
a5ef24
+	EVP_PKEY *pk = NULL;
a5ef24
+	int rv = 0;
a5ef24
+	size_t i;
a5ef24
+
a5ef24
+	for (i = 0; i < sizeof(test_ec_data)/sizeof(EC_SELFTEST_DATA); i++)
a5ef24
+		{
a5ef24
+		EC_SELFTEST_DATA *ecd = test_ec_data + i;
a5ef24
+
a5ef24
+		x = BN_bin2bn(ecd->x, ecd->xlen, x);
a5ef24
+		y = BN_bin2bn(ecd->y, ecd->ylen, y);
a5ef24
+		d = BN_bin2bn(ecd->d, ecd->dlen, d);
a5ef24
+
a5ef24
+		if (!x || !y || !d)
a5ef24
+			goto err;
a5ef24
+
a5ef24
+		ec = EC_KEY_new_by_curve_name(ecd->curve);
a5ef24
+		if (!ec)
a5ef24
+			goto err;
a5ef24
+
a5ef24
+		if (!EC_KEY_set_public_key_affine_coordinates(ec, x, y))
a5ef24
+			goto err;
a5ef24
+
a5ef24
+		if (!EC_KEY_set_private_key(ec, d))
a5ef24
+			goto err;
a5ef24
+
a5ef24
+		if ((pk=EVP_PKEY_new()) == NULL)
a5ef24
+			goto err;
a5ef24
+
a5ef24
+		EVP_PKEY_assign_EC_KEY(pk, ec);
a5ef24
+
a5ef24
+		if (!fips_pkey_signature_test(pk, NULL, 0,
a5ef24
+						NULL, 0, EVP_sha256(), 0,
a5ef24
+						ecd->name))
a5ef24
+			goto err;
a5ef24
+		}
a5ef24
+
a5ef24
+	rv = 1;
a5ef24
+
a5ef24
+	err:
a5ef24
+
a5ef24
+	if (x)
a5ef24
+		BN_clear_free(x);
a5ef24
+	if (y)
a5ef24
+		BN_clear_free(y);
a5ef24
+	if (d)
a5ef24
+		BN_clear_free(d);
a5ef24
+	if (pk)
a5ef24
+		EVP_PKEY_free(pk);
a5ef24
+	else if (ec)
a5ef24
+		EC_KEY_free(ec);
a5ef24
+
a5ef24
+	return rv;
a5ef24
+
a5ef24
+	}
a5ef24
+
a5ef24
+#endif
a5ef24
diff -up openssl-1.0.1e/crypto/fips/fips.h.fips-ec openssl-1.0.1e/crypto/fips/fips.h
a5ef24
--- openssl-1.0.1e/crypto/fips/fips.h.fips-ec	2013-11-08 17:59:43.116027318 +0100
a5ef24
+++ openssl-1.0.1e/crypto/fips/fips.h	2013-11-08 17:59:43.150028068 +0100
a5ef24
@@ -93,6 +93,8 @@ int FIPS_selftest_rsa(void);
a5ef24
 void FIPS_corrupt_dsa(void);
a5ef24
 void FIPS_corrupt_dsa_keygen(void);
a5ef24
 int FIPS_selftest_dsa(void);
a5ef24
+int FIPS_selftest_ecdsa(void);
a5ef24
+int FIPS_selftest_ecdh(void);
a5ef24
 void FIPS_corrupt_rng(void);
a5ef24
 void FIPS_rng_stick(void);
a5ef24
 void FIPS_x931_stick(int onoff);
a5ef24
diff -up openssl-1.0.1e/crypto/fips/fips_post.c.fips-ec openssl-1.0.1e/crypto/fips/fips_post.c
a5ef24
--- openssl-1.0.1e/crypto/fips/fips_post.c.fips-ec	2013-11-08 17:59:43.117027340 +0100
a5ef24
+++ openssl-1.0.1e/crypto/fips/fips_post.c	2013-11-08 17:59:43.150028068 +0100
a5ef24
@@ -95,8 +95,12 @@ int FIPS_selftest(void)
a5ef24
 		rv = 0;
a5ef24
 	if (!FIPS_selftest_rsa())
a5ef24
 		rv = 0;
a5ef24
+	if (!FIPS_selftest_ecdsa())
a5ef24
+		rv = 0;
a5ef24
 	if (!FIPS_selftest_dsa())
a5ef24
 		rv = 0;
a5ef24
+	if (!FIPS_selftest_ecdh())
a5ef24
+		rv = 0;
a5ef24
 	return rv;
a5ef24
 	}
a5ef24
 
a5ef24
diff -up openssl-1.0.1e/crypto/fips/Makefile.fips-ec openssl-1.0.1e/crypto/fips/Makefile
a5ef24
--- openssl-1.0.1e/crypto/fips/Makefile.fips-ec	2013-11-08 17:59:43.119027384 +0100
a5ef24
+++ openssl-1.0.1e/crypto/fips/Makefile	2013-11-08 17:59:43.151028090 +0100
a5ef24
@@ -24,13 +24,13 @@ LIBSRC=fips_aes_selftest.c fips_des_self
a5ef24
     fips_rsa_selftest.c fips_sha_selftest.c fips.c fips_dsa_selftest.c  fips_rand.c \
a5ef24
     fips_rsa_x931g.c fips_post.c fips_drbg_ctr.c fips_drbg_hash.c fips_drbg_hmac.c \
a5ef24
     fips_drbg_lib.c fips_drbg_rand.c fips_drbg_selftest.c fips_rand_lib.c \
a5ef24
-    fips_cmac_selftest.c fips_enc.c fips_md.c
a5ef24
+    fips_cmac_selftest.c fips_ecdh_selftest.c fips_ecdsa_selftest.c fips_enc.c fips_md.c
a5ef24
 
a5ef24
 LIBOBJ=fips_aes_selftest.o fips_des_selftest.o fips_hmac_selftest.o fips_rand_selftest.o \
a5ef24
     fips_rsa_selftest.o fips_sha_selftest.o fips.o fips_dsa_selftest.o  fips_rand.o \
a5ef24
     fips_rsa_x931g.o fips_post.o fips_drbg_ctr.o fips_drbg_hash.o fips_drbg_hmac.o \
a5ef24
     fips_drbg_lib.o fips_drbg_rand.o fips_drbg_selftest.o fips_rand_lib.o \
a5ef24
-    fips_cmac_selftest.o fips_enc.o fips_md.o
a5ef24
+    fips_cmac_selftest.o fips_ecdh_selftest.o fips_ecdsa_selftest.o fips_enc.o fips_md.o
a5ef24
 
a5ef24
 LIBCRYPTO=-L.. -lcrypto
a5ef24
 
a5ef24
@@ -118,6 +118,21 @@ fips_aes_selftest.o: ../../include/opens
a5ef24
 fips_aes_selftest.o: ../../include/openssl/safestack.h
a5ef24
 fips_aes_selftest.o: ../../include/openssl/stack.h
a5ef24
 fips_aes_selftest.o: ../../include/openssl/symhacks.h fips_aes_selftest.c
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/cmac.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/crypto.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/evp.h ../../include/openssl/fips.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/lhash.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/obj_mac.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/objects.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/opensslconf.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/opensslv.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/ossl_typ.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/safestack.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/stack.h
a5ef24
+fips_cmac_selftest.o: ../../include/openssl/symhacks.h fips_cmac_selftest.c
a5ef24
+fips_cmac_selftest.o: fips_locl.h
a5ef24
 fips_des_selftest.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
a5ef24
 fips_des_selftest.o: ../../include/openssl/crypto.h
a5ef24
 fips_des_selftest.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
a5ef24
@@ -231,6 +246,46 @@ fips_dsa_selftest.o: ../../include/opens
a5ef24
 fips_dsa_selftest.o: ../../include/openssl/stack.h
a5ef24
 fips_dsa_selftest.o: ../../include/openssl/symhacks.h fips_dsa_selftest.c
a5ef24
 fips_dsa_selftest.o: fips_locl.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/bn.h ../../include/openssl/crypto.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/ecdh.h ../../include/openssl/err.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/evp.h ../../include/openssl/fips.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/lhash.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/obj_mac.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/objects.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/opensslconf.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/opensslv.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/ossl_typ.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/safestack.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/stack.h
a5ef24
+fips_ecdh_selftest.o: ../../include/openssl/symhacks.h fips_ecdh_selftest.c
a5ef24
+fips_ecdh_selftest.o: fips_locl.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/bn.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/crypto.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/ecdsa.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/err.h ../../include/openssl/evp.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/fips.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/lhash.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/obj_mac.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/objects.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/opensslconf.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/opensslv.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/ossl_typ.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/safestack.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/stack.h
a5ef24
+fips_ecdsa_selftest.o: ../../include/openssl/symhacks.h fips_ecdsa_selftest.c
a5ef24
+fips_enc.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
a5ef24
+fips_enc.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
a5ef24
+fips_enc.o: ../../include/openssl/err.h ../../include/openssl/evp.h
a5ef24
+fips_enc.o: ../../include/openssl/fips.h ../../include/openssl/lhash.h
a5ef24
+fips_enc.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
a5ef24
+fips_enc.o: ../../include/openssl/opensslconf.h
a5ef24
+fips_enc.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
a5ef24
+fips_enc.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
a5ef24
+fips_enc.o: ../../include/openssl/symhacks.h fips_enc.c
a5ef24
 fips_hmac_selftest.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
a5ef24
 fips_hmac_selftest.o: ../../include/openssl/crypto.h
a5ef24
 fips_hmac_selftest.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
a5ef24
@@ -245,6 +300,15 @@ fips_hmac_selftest.o: ../../include/open
a5ef24
 fips_hmac_selftest.o: ../../include/openssl/safestack.h
a5ef24
 fips_hmac_selftest.o: ../../include/openssl/stack.h
a5ef24
 fips_hmac_selftest.o: ../../include/openssl/symhacks.h fips_hmac_selftest.c
a5ef24
+fips_md.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
a5ef24
+fips_md.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
a5ef24
+fips_md.o: ../../include/openssl/err.h ../../include/openssl/evp.h
a5ef24
+fips_md.o: ../../include/openssl/fips.h ../../include/openssl/lhash.h
a5ef24
+fips_md.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
a5ef24
+fips_md.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h
a5ef24
+fips_md.o: ../../include/openssl/ossl_typ.h ../../include/openssl/safestack.h
a5ef24
+fips_md.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
a5ef24
+fips_md.o: fips_md.c
a5ef24
 fips_post.o: ../../include/openssl/aes.h ../../include/openssl/asn1.h
a5ef24
 fips_post.o: ../../include/openssl/bio.h ../../include/openssl/crypto.h
a5ef24
 fips_post.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h
a5ef24
diff -up openssl-1.0.1e/version.map.fips-ec openssl-1.0.1e/version.map
a5ef24
--- openssl-1.0.1e/version.map.fips-ec	2013-11-08 17:59:43.131027649 +0100
a5ef24
+++ openssl-1.0.1e/version.map	2013-11-08 17:59:43.151028090 +0100
a5ef24
@@ -6,3 +6,7 @@ OPENSSL_1.0.1 {
a5ef24
 	    _original*;
a5ef24
 	    _current*;
a5ef24
 };
a5ef24
+OPENSSL_1.0.1_EC {
a5ef24
+    global:
a5ef24
+	    EC*;
a5ef24
+};