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

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