Blame SOURCES/openssl-1.0.2a-fips-ec.patch

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