add cofactor ECDH support from fips branch
[openssl.git] / crypto / ecdh / ech_ossl.c
index ceaa2f06b6389cd81dd4d09404d852407242b4ba..2656797449e7075cd910bd69deaf4c6c8012b3f5 100644 (file)
@@ -91,7 +91,7 @@ static ECDH_METHOD openssl_ecdh_meth = {
        NULL, /* init     */
        NULL, /* finish   */
 #endif
-       0,    /* flags    */
+       ECDH_FLAG_FIPS_METHOD,    /* flags    */
        NULL  /* app_data */
 };
 
@@ -119,6 +119,14 @@ static int ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
        size_t buflen, len;
        unsigned char *buf=NULL;
 
+#ifdef OPENSSL_FIPS
+       if(FIPS_selftest_failed())
+               {
+               FIPSerr(FIPS_F_ECDH_COMPUTE_KEY,FIPS_R_FIPS_SELFTEST_FAILED);
+               return -1;
+               }
+#endif
+
        if (outlen > INT_MAX)
                {
                ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ERR_R_MALLOC_FAILURE); /* sort of, anyway */
@@ -138,6 +146,18 @@ static int ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
                }
 
        group = EC_KEY_get0_group(ecdh);
+
+       if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH)
+               {
+               if (!EC_GROUP_get_cofactor(group, x, ctx) ||
+                       !BN_mul(x, x, priv_key, ctx))
+                       {
+                       ECDHerr(ECDH_F_ECDH_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
+                       goto err;
+                       }
+               priv_key = x;
+               }
+
        if ((tmp=EC_POINT_new(group)) == NULL)
                {
                ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ERR_R_MALLOC_FAILURE);