[openssl] master update

shane.lontis at oracle.com shane.lontis at oracle.com
Thu Jan 23 10:35:38 UTC 2020


The branch master has been updated
       via  62f49b90d7e88d3c36fc1f5e4d677997aeb97b0a (commit)
      from  f10048301390283523d3d1623880be7518cf46ac (commit)


- Log -----------------------------------------------------------------
commit 62f49b90d7e88d3c36fc1f5e4d677997aeb97b0a
Author: Shane Lontis <shane.lontis at oracle.com>
Date:   Thu Jan 23 20:33:28 2020 +1000

    Add DH key exchange to fips provider
    
    Reviewed-by: Matt Caswell <matt at openssl.org>
    (Merged from https://github.com/openssl/openssl/pull/10826)

-----------------------------------------------------------------------

Summary of changes:
 crypto/dh/build.info                         |   9 +-
 crypto/dh/dh_key.c                           | 200 ++++++++------
 crypto/dh/dh_lib.c                           |  17 +-
 crypto/dh/dh_local.h                         |   2 +-
 crypto/evp/build.info                        |   4 +-
 crypto/evp/exchange.c                        |  10 +-
 crypto/evp/pmeth_lib.c                       |  10 +-
 include/crypto/dh.h                          |  15 ++
 providers/fips/fipsprov.c                    | 389 +++++++++++++++++++++------
 providers/implementations/exchange/dh_exch.c |  16 +-
 10 files changed, 479 insertions(+), 193 deletions(-)
 create mode 100644 include/crypto/dh.h

diff --git a/crypto/dh/build.info b/crypto/dh/build.info
index b19ff6dbac..924ca8f92f 100644
--- a/crypto/dh/build.info
+++ b/crypto/dh/build.info
@@ -1,5 +1,10 @@
 LIBS=../../libcrypto
-SOURCE[../../libcrypto]=\
-        dh_asn1.c dh_gen.c dh_key.c dh_lib.c dh_check.c dh_err.c dh_depr.c \
+
+$COMMON=dh_lib.c dh_key.c
+
+SOURCE[../../libcrypto]=$COMMON\
+        dh_asn1.c dh_gen.c dh_check.c dh_err.c dh_depr.c \
         dh_ameth.c dh_pmeth.c dh_prn.c dh_rfc5114.c dh_kdf.c dh_meth.c \
         dh_rfc7919.c
+
+SOURCE[../../providers/libfips.a]=$COMMON
diff --git a/crypto/dh/dh_key.c b/crypto/dh/dh_key.c
index a8a9dbe764..bd9b5c824b 100644
--- a/crypto/dh/dh_key.c
+++ b/crypto/dh/dh_key.c
@@ -11,29 +11,94 @@
 #include "internal/cryptlib.h"
 #include "dh_local.h"
 #include "crypto/bn.h"
+#include "crypto/dh.h"
 
+#ifndef FIPS_MODE
 static int generate_key(DH *dh);
-static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
+#endif /* FIPS_MODE */
+
 static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
                          const BIGNUM *a, const BIGNUM *p,
                          const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
 static int dh_init(DH *dh);
 static int dh_finish(DH *dh);
 
-int DH_generate_key(DH *dh)
+int dh_compute_key(OPENSSL_CTX *libctx, unsigned char *key,
+                   const BIGNUM *pub_key, DH *dh)
 {
-    return dh->meth->generate_key(dh);
+    BN_CTX *ctx = NULL;
+    BN_MONT_CTX *mont = NULL;
+    BIGNUM *tmp;
+    int ret = -1;
+#ifndef FIPS_MODE
+    int check_result;
+#endif
+
+    if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
+        DHerr(0, DH_R_MODULUS_TOO_LARGE);
+        goto err;
+    }
+
+    if (BN_num_bits(dh->p) < DH_MIN_MODULUS_BITS) {
+        DHerr(0, DH_R_MODULUS_TOO_SMALL);
+        return 0;
+    }
+
+    ctx = BN_CTX_new_ex(libctx);
+    if (ctx == NULL)
+        goto err;
+    BN_CTX_start(ctx);
+    tmp = BN_CTX_get(ctx);
+    if (tmp == NULL)
+        goto err;
+
+    if (dh->priv_key == NULL) {
+        DHerr(0, DH_R_NO_PRIVATE_VALUE);
+        goto err;
+    }
+
+    if (dh->flags & DH_FLAG_CACHE_MONT_P) {
+        mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
+                                      dh->lock, dh->p, ctx);
+        BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
+        if (!mont)
+            goto err;
+    }
+/* TODO(3.0) : Solve in a PR related to Key validation for DH */
+#ifndef FIPS_MODE
+    if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
+        DHerr(0, DH_R_INVALID_PUBKEY);
+        goto err;
+    }
+#endif
+    if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx,
+                              mont)) {
+        DHerr(0, ERR_R_BN_LIB);
+        goto err;
+    }
+
+    ret = BN_bn2bin(tmp, key);
+ err:
+    BN_CTX_end(ctx);
+    BN_CTX_free(ctx);
+    return ret;
 }
 
-int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
+static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
 {
-    return dh->meth->compute_key(key, pub_key, dh);
+    return dh_compute_key(NULL, key, pub_key, dh);
 }
 
-int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
+int dh_compute_key_padded(OPENSSL_CTX *libctx, unsigned char *key,
+                          const BIGNUM *pub_key, DH *dh)
 {
     int rv, pad;
+
+#ifdef FIPS_MODE
+    rv = dh_compute_key(libctx, key, pub_key, dh);
+#else
     rv = dh->meth->compute_key(key, pub_key, dh);
+#endif
     if (rv <= 0)
         return rv;
     pad = BN_num_bytes(dh->p) - rv;
@@ -44,9 +109,25 @@ int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
     return rv + pad;
 }
 
+#ifndef FIPS_MODE
+int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
+{
+    return dh->meth->compute_key(key, pub_key, dh);
+}
+
+int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
+{
+    return dh_compute_key_padded(NULL, key, pub_key, dh);
+}
+#endif
+
 static DH_METHOD dh_ossl = {
     "OpenSSL DH Method",
+#ifndef FIPS_MODE
     generate_key,
+#else
+    NULL, /* TODO(3.0) : solve this in a keygen related PR */
+#endif
     compute_key,
     dh_bn_mod_exp,
     dh_init,
@@ -63,14 +144,40 @@ const DH_METHOD *DH_OpenSSL(void)
     return &dh_ossl;
 }
 
+const DH_METHOD *DH_get_default_method(void)
+{
+    return default_DH_method;
+}
+
+static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
+                         const BIGNUM *a, const BIGNUM *p,
+                         const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
+{
+    return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
+}
+
+static int dh_init(DH *dh)
+{
+    dh->flags |= DH_FLAG_CACHE_MONT_P;
+    return 1;
+}
+
+static int dh_finish(DH *dh)
+{
+    BN_MONT_CTX_free(dh->method_mont_p);
+    return 1;
+}
+
+#ifndef FIPS_MODE
+
 void DH_set_default_method(const DH_METHOD *meth)
 {
     default_DH_method = meth;
 }
 
-const DH_METHOD *DH_get_default_method(void)
+int DH_generate_key(DH *dh)
 {
-    return default_DH_method;
+    return dh->meth->generate_key(dh);
 }
 
 static int generate_key(DH *dh)
@@ -173,82 +280,6 @@ static int generate_key(DH *dh)
     return ok;
 }
 
-static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
-{
-    BN_CTX *ctx = NULL;
-    BN_MONT_CTX *mont = NULL;
-    BIGNUM *tmp;
-    int ret = -1;
-    int check_result;
-
-    if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
-        DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_LARGE);
-        goto err;
-    }
-
-    if (BN_num_bits(dh->p) < DH_MIN_MODULUS_BITS) {
-        DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_SMALL);
-        return 0;
-    }
-
-    ctx = BN_CTX_new();
-    if (ctx == NULL)
-        goto err;
-    BN_CTX_start(ctx);
-    tmp = BN_CTX_get(ctx);
-    if (tmp == NULL)
-        goto err;
-
-    if (dh->priv_key == NULL) {
-        DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE);
-        goto err;
-    }
-
-    if (dh->flags & DH_FLAG_CACHE_MONT_P) {
-        mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
-                                      dh->lock, dh->p, ctx);
-        BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
-        if (!mont)
-            goto err;
-    }
-
-    if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
-        DHerr(DH_F_COMPUTE_KEY, DH_R_INVALID_PUBKEY);
-        goto err;
-    }
-
-    if (!dh->
-        meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)) {
-        DHerr(DH_F_COMPUTE_KEY, ERR_R_BN_LIB);
-        goto err;
-    }
-
-    ret = BN_bn2bin(tmp, key);
- err:
-    BN_CTX_end(ctx);
-    BN_CTX_free(ctx);
-    return ret;
-}
-
-static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
-                         const BIGNUM *a, const BIGNUM *p,
-                         const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
-{
-    return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
-}
-
-static int dh_init(DH *dh)
-{
-    dh->flags |= DH_FLAG_CACHE_MONT_P;
-    return 1;
-}
-
-static int dh_finish(DH *dh)
-{
-    BN_MONT_CTX_free(dh->method_mont_p);
-    return 1;
-}
-
 int dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
 {
     int err_reason = DH_R_BN_ERROR;
@@ -311,3 +342,4 @@ size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out)
     *pbuf_out = pbuf;
     return p_size;
 }
+#endif /* FIPS_MODE */
diff --git a/crypto/dh/dh_lib.c b/crypto/dh/dh_lib.c
index 65c2154c26..c9679d7e52 100644
--- a/crypto/dh/dh_lib.c
+++ b/crypto/dh/dh_lib.c
@@ -12,8 +12,10 @@
 #include "internal/refcount.h"
 #include <openssl/bn.h>
 #include "dh_local.h"
+#include "crypto/dh.h"
 #include <openssl/engine.h>
 
+#ifndef FIPS_MODE
 int DH_set_method(DH *dh, const DH_METHOD *meth)
 {
     /*
@@ -33,6 +35,7 @@ int DH_set_method(DH *dh, const DH_METHOD *meth)
         meth->init(dh);
     return 1;
 }
+#endif /* !FIPS_MODE */
 
 DH *DH_new(void)
 {
@@ -57,7 +60,7 @@ DH *DH_new_method(ENGINE *engine)
     }
 
     ret->meth = DH_get_default_method();
-#ifndef OPENSSL_NO_ENGINE
+#if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
     ret->flags = ret->meth->flags;  /* early default init */
     if (engine) {
         if (!ENGINE_init(engine)) {
@@ -81,7 +84,7 @@ DH *DH_new_method(ENGINE *engine)
 #ifndef FIPS_MODE
     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
         goto err;
-#endif
+#endif /* FIPS_MODE */
 
     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
         DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
@@ -110,11 +113,10 @@ void DH_free(DH *r)
 
     if (r->meth != NULL && r->meth->finish != NULL)
         r->meth->finish(r);
-#ifndef OPENSSL_NO_ENGINE
+#if !defined(FIPS_MODE)
+# if !defined(OPENSSL_NO_ENGINE)
     ENGINE_finish(r->engine);
-#endif
-
-#ifndef FIPS_MODE
+# endif
     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
 #endif
 
@@ -177,7 +179,6 @@ int DH_security_bits(const DH *dh)
     return BN_security_bits(BN_num_bits(dh->p), N);
 }
 
-
 void DH_get0_pqg(const DH *dh,
                  const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
 {
@@ -293,7 +294,9 @@ void DH_set_flags(DH *dh, int flags)
     dh->flags |= flags;
 }
 
+#ifndef FIPS_MODE
 ENGINE *DH_get0_engine(DH *dh)
 {
     return dh->engine;
 }
+#endif /*FIPS_MODE */
diff --git a/crypto/dh/dh_local.h b/crypto/dh/dh_local.h
index 378cf5c957..2a200c748e 100644
--- a/crypto/dh/dh_local.h
+++ b/crypto/dh/dh_local.h
@@ -35,9 +35,9 @@ struct dh_st {
     CRYPTO_REF_COUNT references;
 #ifndef FIPS_MODE
     CRYPTO_EX_DATA ex_data;
+    ENGINE *engine;
 #endif
     const DH_METHOD *meth;
-    ENGINE *engine;
     CRYPTO_RWLOCK *lock;
 
     /* Provider data */
diff --git a/crypto/evp/build.info b/crypto/evp/build.info
index 71e0ebaf44..7f566b80ce 100644
--- a/crypto/evp/build.info
+++ b/crypto/evp/build.info
@@ -1,7 +1,7 @@
 LIBS=../../libcrypto
 $COMMON=digest.c evp_enc.c evp_lib.c evp_fetch.c cmeth_lib.c evp_utils.c \
         mac_lib.c mac_meth.c keymgmt_meth.c keymgmt_lib.c kdf_lib.c kdf_meth.c \
-        m_sigver.c pmeth_lib.c signature.c p_lib.c pmeth_gn.c
+        m_sigver.c pmeth_lib.c signature.c p_lib.c pmeth_gn.c exchange.c
 
 SOURCE[../../libcrypto]=$COMMON\
         encode.c evp_key.c evp_cnf.c \
@@ -15,7 +15,7 @@ SOURCE[../../libcrypto]=$COMMON\
         pkey_kdf.c e_old.c pmeth_fn.c\
         e_aes_cbc_hmac_sha1.c e_aes_cbc_hmac_sha256.c e_rc4_hmac_md5.c \
         e_chacha20_poly1305.c \
-        pkey_mac.c exchange.c \
+        pkey_mac.c \
         legacy_sha.c
 
 IF[{- !$disabled{md2} -}]
diff --git a/crypto/evp/exchange.c b/crypto/evp/exchange.c
index 1f9e39be4c..1f87c59343 100644
--- a/crypto/evp/exchange.c
+++ b/crypto/evp/exchange.c
@@ -256,6 +256,9 @@ int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
      */
     ERR_pop_to_mark();
 
+#ifdef FIPS_MODE
+    return 0;
+#else
     if (ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
         return -2;
@@ -267,11 +270,12 @@ int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
     if (ret <= 0)
         ctx->operation = EVP_PKEY_OP_UNDEFINED;
     return ret;
+#endif
 }
 
 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
 {
-    int ret;
+    int ret = 0;
     void *provkey = NULL;
 
     if (ctx == NULL) {
@@ -296,6 +300,9 @@ int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
     return ctx->op.kex.exchange->set_peer(ctx->op.kex.exchprovctx, provkey);
 
  legacy:
+#ifdef FIPS_MODE
+    return ret;
+#else
     if (ctx->pmeth == NULL
         || !(ctx->pmeth->derive != NULL
              || ctx->pmeth->encrypt != NULL
@@ -356,6 +363,7 @@ int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
 
     EVP_PKEY_up_ref(peer);
     return 1;
+#endif
 }
 
 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
diff --git a/crypto/evp/pmeth_lib.c b/crypto/evp/pmeth_lib.c
index 24e2fdc9b6..e8bcb7e5a4 100644
--- a/crypto/evp/pmeth_lib.c
+++ b/crypto/evp/pmeth_lib.c
@@ -282,16 +282,16 @@ void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
         EVP_SIGNATURE_free(ctx->op.sig.signature);
         ctx->op.sig.sigprovctx = NULL;
         ctx->op.sig.signature = NULL;
-    }
-/* TODO(3.0): add dependancies and uncomment this when available for fips mode */
-#ifndef FIPS_MODE
-    else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
+    } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
         if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
             ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
         EVP_KEYEXCH_free(ctx->op.kex.exchange);
         ctx->op.kex.exchprovctx = NULL;
         ctx->op.kex.exchange = NULL;
-    } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
+    }
+/* TODO(3.0): add dependancies and uncomment this when available for fips mode */
+#ifndef FIPS_MODE
+    else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
         if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL)
             ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx);
         EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher);
diff --git a/include/crypto/dh.h b/include/crypto/dh.h
new file mode 100644
index 0000000000..4032bdc10e
--- /dev/null
+++ b/include/crypto/dh.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <openssl/dh.h>
+
+int dh_compute_key(OPENSSL_CTX *ctx, unsigned char *key, const BIGNUM *pub_key,
+                   DH *dh);
+int dh_compute_key_padded(OPENSSL_CTX *ctx, unsigned char *key,
+                          const BIGNUM *pub_key, DH *dh);
diff --git a/providers/fips/fipsprov.c b/providers/fips/fipsprov.c
index eeb1b1e1cf..0ed425bc22 100644
--- a/providers/fips/fipsprov.c
+++ b/providers/fips/fipsprov.c
@@ -26,6 +26,7 @@
 #include "internal/cryptlib.h"
 #include "internal/property.h"
 #include "internal/nelem.h"
+#include "internal/param_build.h"
 #include "crypto/evp.h"
 #include "prov/implementations.h"
 #include "prov/provider_ctx.h"
@@ -125,65 +126,80 @@ static OSSL_PARAM core_params[] =
 };
 
 /*
- * This routine is currently necessary as bn params are currently processed
- * using BN_native2bn when raw data is received. This means we need to do
- * magic to reverse the order of the bytes to match native format.
- * The array of hexdata is to get around compilers that dont like
+ * Convert a string into a bignumber.
+ * The array of hex_data is used to get around compilers that dont like
  * strings longer than 509 bytes,
  */
-static int rawnative_fromhex(const char *hex_data[],
-                             unsigned char **native, size_t *nativelen)
+static int hextobn(const char *hex_data[], BIGNUM **bn)
 {
     int ret = 0;
-    unsigned char *data = NULL;
-    BIGNUM *bn = NULL;
-    int i, slen, datalen, sz;
+    int i, slen;
     char *str = NULL;
 
+    /* Get the total length of the strings */
     for (slen = 0, i = 0; hex_data[i] != NULL; ++i)
         slen += strlen(hex_data[i]);
+
+    /* Add 1 for the string terminator */
     str = OPENSSL_zalloc(slen + 1);
     if (str == NULL)
         return 0;
+
+    /* join the strings together into 1 buffer */
     for (i = 0; hex_data[i] != NULL; ++i)
         strcat(str, hex_data[i]);
 
-    if (BN_hex2bn(&bn, str) <= 0)
-        return 0;
+    if (BN_hex2bn(bn, str) <= 0)
+        goto err;
+    ret = 1;
+err:
+    OPENSSL_free(str);
+    return ret;
+}
 
-    datalen = slen / 2;
-    data = (unsigned char *)str; /* reuse the str buffer */
+static int hextobin(const char *hex_data[], unsigned char **out, size_t *len)
+{
+    int ret = 0, sz;
+    BIGNUM *bn = NULL;
+    unsigned char *buf = NULL;
 
-    sz = BN_bn2nativepad(bn, data, datalen);
-    if (sz <= 0)
+    if (!hextobn(hex_data, &bn))
+        return 0;
+    sz = BN_num_bytes(bn);
+    buf = OPENSSL_zalloc(sz);
+    if (buf == NULL)
+        goto err;
+    if (BN_bn2binpad(bn, buf, sz) <= 0)
         goto err;
+
+    *out = buf;
+    *len = sz;
+    buf = NULL; /* Set to NULL so it is not freed */
     ret = 1;
-    *native = data;
-    *nativelen = datalen;
-    data = NULL; /* so it does not get freed */
 err:
+    OPENSSL_free(buf);
     BN_free(bn);
-    OPENSSL_free(data);
     return ret;
 }
 
-/* TODO(3.0): To be removed */
-static int dummy_evp_call(void *provctx)
+#ifndef OPENSSL_NO_DSA
+static int dsa_key_signature_test(OPENSSL_CTX *libctx)
 {
-    OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
-    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
-    EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA256", NULL);
-    EVP_KDF *kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, NULL);
+    int ret = 0;
+    BIGNUM *p = NULL, *q = NULL, *g = NULL;
+    BIGNUM *pub = NULL, *priv = NULL;
+    OSSL_PARAM *params = NULL, *params_sig = NULL;
+    OSSL_PARAM_BLD bld;
     EVP_PKEY_CTX *sctx = NULL, *kctx = NULL;
     EVP_PKEY *pkey = NULL;
-    OSSL_PARAM *p;
-    OSSL_PARAM params[16];
     unsigned char sig[64];
-    size_t siglen, sigdgstlen;
-    unsigned char *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
-    unsigned char *dsa_pub = NULL, *dsa_priv = NULL;
-    size_t dsa_p_len, dsa_q_len, dsa_g_len, dsa_pub_len, dsa_priv_len;
+    size_t siglen;
 
+    static const unsigned char dgst[SHA256_DIGEST_LENGTH] = {
+        0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
+        0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
+        0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
+    };
     /* dsa 2048 */
     static const char *dsa_p_hex[] = {
         "a29b8872ce8b8423b7d5d21d4b02f57e03e9e6b8a258dc16611ba098ab543415"
@@ -226,14 +242,237 @@ static int dummy_evp_call(void *provctx)
         "6ccaeef6d73b4e80f11c17b8e9627c036635bac39423505e407e5cb7",
         NULL
     };
-    char msg[] = "Hello World!";
-    const unsigned char exptd[] = {
-        0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
-        0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
-        0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
+
+    if (!hextobn(dsa_p_hex, &p)
+        || !hextobn(dsa_q_hex, &q)
+        || !hextobn(dsa_g_hex, &g)
+        || !hextobn(dsa_pub_hex, &pub)
+        || !hextobn(dsa_priv_hex, &priv))
+        goto err;
+
+    ossl_param_bld_init(&bld);
+    if (!ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_FFC_P, p)
+        || !ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_FFC_Q, q)
+        || !ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_FFC_G, g)
+        || !ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_DSA_PUB_KEY, pub)
+        || !ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_DSA_PRIV_KEY, priv))
+        goto err;
+    params = ossl_param_bld_to_param(&bld);
+
+    /* Create a EVP_PKEY_CTX to load the DSA key into */
+    kctx = EVP_PKEY_CTX_new_from_name(libctx, SN_dsa, "");
+    if (kctx == NULL || params == NULL)
+        goto err;
+    if (EVP_PKEY_key_fromdata_init(kctx) <= 0
+        || EVP_PKEY_fromdata(kctx, &pkey, params) <= 0)
+        goto err;
+
+    /* Create a EVP_PKEY_CTX to use for the signing operation */
+    sctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey);
+    if (sctx == NULL
+        || EVP_PKEY_sign_init(sctx) <= 0)
+        goto err;
+
+    /* set signature parameters */
+    ossl_param_bld_init(&bld);
+    if (!ossl_param_bld_push_utf8_string(&bld, OSSL_SIGNATURE_PARAM_DIGEST,
+                                         SN_sha256,strlen(SN_sha256) + 1)
+        || !ossl_param_bld_push_size_t(&bld, OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
+                                       SHA256_DIGEST_LENGTH))
+        goto err;
+    params_sig = ossl_param_bld_to_param(&bld);
+    if (EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
+        goto err;
+
+    if (EVP_PKEY_sign(sctx, sig, &siglen, dgst, sizeof(dgst)) <= 0
+        || EVP_PKEY_verify_init(sctx) <= 0
+        || EVP_PKEY_verify(sctx, sig, siglen, dgst, sizeof(dgst)) <= 0)
+        goto err;
+    ret = 1;
+err:
+    ossl_param_bld_free(params);
+    ossl_param_bld_free(params_sig);
+    BN_free(p);
+    BN_free(q);
+    BN_free(g);
+    BN_free(pub);
+    BN_free(priv);
+    EVP_PKEY_free(pkey);
+    EVP_PKEY_CTX_free(kctx);
+    EVP_PKEY_CTX_free(sctx);
+    return ret;
+}
+#endif /* OPENSSL_NO_DSA */
+
+#ifndef OPENSSL_NO_DH
+static int dh_key_exchange_test(OPENSSL_CTX *libctx)
+{
+    int ret = 0;
+    BIGNUM *p = NULL, *q = NULL, *g = NULL;
+    BIGNUM *pub = NULL, *priv = NULL, *pub_peer = NULL;
+    unsigned char *kat_secret = NULL;
+    EVP_PKEY_CTX *kactx = NULL, *dctx = NULL;
+    EVP_PKEY *pkey = NULL, *peerkey = NULL;
+    OSSL_PARAM *params = NULL;
+    OSSL_PARAM *params_peer = NULL;
+    unsigned char secret[256];
+    size_t secret_len, kat_secret_len = 0;
+    OSSL_PARAM_BLD bld;
+
+    /* DH KAT */
+    static const char *dh_p_hex[] = {
+        "dcca1511b2313225f52116e1542789e001f0425bccc7f366f7406407f1c9fa8b"
+        "e610f1778bb170be39dbb76f85bf24ce6880adb7629f7c6d015e61d43fa3ee4d"
+        "e185f2cfd041ffde9d418407e15138bb021daeb35f762d1782acc658d32bd4b0"
+        "232c927dd38fa097b3d1859fa8acafb98f066608fc644ec7ddb6f08599f92ac1",
+        "b59825da8432077def695646063c20823c9507ab6f0176d4730d990dbbe6361c"
+        "d8b2b94d3d2f329b82099bd661f42950f403df3ede62a33188b02798ba823f44"
+        "b946fe9df677a0c5a1238eaa97b70f80da8cac88e092b1127060ffbf45579994"
+        "011dc2faa5e7f6c76245e1cc312231c17d1ca6b19007ef0db99f9cb60e1d5f69",
+        NULL
+    };
+    static const char *dh_q_hex[] = {
+        "898b226717ef039e603e82e5c7afe48374ac5f625c54f1ea11acb57d",
+        NULL
+    };
+    static const char *dh_g_hex[] = {
+        "5ef7b88f2df60139351dfbfe1266805fdf356cdfd13a4da0050c7ede"
+        "246df59f6abf96ade5f2b28ffe88d6bce7f7894a3d535fc82126ddd4"
+        "24872e16b838df8c51e9016f889c7c203e98a8b631f9c72563d38a49"
+        "589a0753d358e783318cefd9677c7b2dbb77d6dce2a1963795ca64b9",
+        "2d1c9aac6d0e8d431de5e50060dff78689c9eca1c1248c16ed09c7ad",
+        "412a17406d2b525aa1cabb237b9734ec7b8ce3fae02f29c5efed30d6"
+        "9187da109c2c9fe2aadbb0c22af54c616655000c431c6b4a379763b0"
+        "a91658efc84e8b06358c8b4f213710fd10172cf39b830c2dd84a0c8a"
+        "b82516ecab995fa4215e023e4ecf8074c39d6c88b70d1ee4e96fdc20",
+        "ea115c32",
+        NULL
     };
-    unsigned int dgstlen = 0;
+    static const char *dh_priv_hex[] = {
+        "1433e0b5a917b60a3023f2f8aa2c2d70d2968aba9aeac81540b8fce6",
+        NULL
+    };
+    static const char *dh_pub_hex[] = {
+        "95dd338d29e5710492b918317b72a36936e1951a2ee5a5591699c048"
+        "6d0d4f9bdd6d5a3f6b98890c62b37652d36e712111e68a7355372506"
+        "99efe330537391fbc2c548bc5ac3e5b23386c3eef5eb43c099d70a52"
+        "02687e83964248fca91f40908e8fb3319315f6d2606d7f7cd52cc6e7",
+        "c5843afb22519cf0f0f9d3a0a4e8c88899efede7364351fb6a363ee7"
+        "17e5445adab4c931a6483997b87dad83677e4d1d3a7775e0f6d00fdf"
+        "73c7ad801e665a0e5a796d0a0380a19fa182efc8a04f5e4db90d1a86"
+        "37f95db16436bdc8f3fc096c4ff7f234be8fef479ac4b0dc4b77263e",
+        "07d9959de0f1bf3f0ae3d9d50e4b89c99e3ea1217343dd8c6581acc4"
+        "959c91d3",
+        NULL
+    };
+    static const char *dh_peer_pub_hex[] = {
+        "1fc1da341d1a846a96b7be24340f877dd010aa0356d5ad58aae9c7b0"
+        "8f749a32235110b5d88eb5dbfa978d27ecc530f02d3114005b64b1c0"
+        "e024cb8ae21698bca9e60d42808622f181c56e1de7a96e6efee9d665"
+        "67e91b977042c7e3d0448f05fb77f522b9bfc8d33cc3c31ed3b31f0f",
+        "ecb6db4f6ea311e77afdbcd47aee1bb150f216873578fb96468e8f9f"
+        "3de8efbfce75624b1df05322a34f1463e839e8984c4ad0a96e1ac842"
+        "e5318cc23c062a8ca171b8d575980dde7fc56f1536523820d43192bf"
+        "d51e8e228978aca5b94472f339caeb9931b42be301268bc99789c9b2",
+        "5571c3c0e4cb3f007f1a511cbb53c8519cdd1302abca6c0f34f96739"
+        "f17ff48b",
+        NULL
+    };
+    static const char *dh_secret_exptd_hex[] = {
+        "08ff33bb2ecff49a7d4a7912aeb1bb6ab511641b4a76770c8cc1bcc2"
+        "33343dfe700d11813d2c9ed23b211ca9e8786921edca283c68b16153"
+        "fa01e91ab82c90ddab4a95816770a98710e14c92ab83b6e46e1e426e"
+        "e852430d6187daa3720a6bcd73235c6b0f941f3364f50420551a4bfe",
+        "afe2bc438505a59a4a40daca7a895a73db575c74c13a23ad8832957d"
+        "582d38f0a6165fb0d7e9b8799e42fd3220e332e98185a0c9429757b2"
+        "d0d02c17dbaa1ff6ed93d7e73e241eaed90caf394d2bc6570f18c81f"
+        "2be5d01a2ca99ff142b5d963f9f500325e7556f95849b3ffc7479486",
+        "be1d4596a3106bd5cb4f61c57ec5f100fb7a0c82a10b82526a97d1d9"
+        "7d98eaf6",
+        NULL
+    };
+
+    if (!hextobn(dh_p_hex, &p)
+        || !hextobn(dh_q_hex, &q)
+        || !hextobn(dh_g_hex, &g)
+        || !hextobn(dh_pub_hex, &pub)
+        || !hextobn(dh_priv_hex, &priv)
+        || !hextobn(dh_peer_pub_hex, &pub_peer)
+        || !hextobin(dh_secret_exptd_hex, &kat_secret, &kat_secret_len))
+        goto err;
+
+    ossl_param_bld_init(&bld);
+    if (!ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_FFC_P, p)
+        || !ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_FFC_Q, q)
+        || !ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_FFC_G, g)
+        || !ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_DH_PUB_KEY, pub)
+        || !ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_DH_PRIV_KEY, priv))
+        goto err;
+    params = ossl_param_bld_to_param(&bld);
+
+    ossl_param_bld_init(&bld);
+    if (!ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_FFC_P, p)
+        || !ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_FFC_Q, q)
+        || !ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_FFC_G, g)
+        || !ossl_param_bld_push_BN(&bld, OSSL_PKEY_PARAM_DH_PUB_KEY, pub_peer))
+        goto err;
+
+    params_peer = ossl_param_bld_to_param(&bld);
+    if (params == NULL || params_peer == NULL)
+        goto err;
+
+    /* Create a EVP_PKEY_CTX to load the DH keys into */
+    kactx = EVP_PKEY_CTX_new_from_name(libctx, "DH", "");
+    if (kactx == NULL)
+        goto err;
+    if (EVP_PKEY_key_fromdata_init(kactx) <= 0
+        || EVP_PKEY_fromdata(kactx, &pkey, params) <= 0)
+        goto err;
+    if (EVP_PKEY_key_fromdata_init(kactx) <= 0
+        || EVP_PKEY_fromdata(kactx, &peerkey, params_peer) <= 0)
+        goto err;
+
+    /* Create a EVP_PKEY_CTX to perform key derivation */
+    dctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey);
+    if (dctx == NULL)
+        goto err;
+
+    if (EVP_PKEY_derive_init(dctx) <= 0
+        || EVP_PKEY_derive_set_peer(dctx, peerkey) <= 0
+        || EVP_PKEY_derive(dctx, secret, &secret_len) <= 0)
+        goto err;
+
+    if (secret_len != kat_secret_len
+        || memcmp(secret, kat_secret, secret_len) != 0)
+        goto err;
+    ret = 1;
+err:
+    ossl_param_bld_free(params_peer);
+    ossl_param_bld_free(params);
+    BN_free(p);
+    BN_free(q);
+    BN_free(g);
+    BN_free(pub);
+    BN_free(priv);
+    BN_free(pub_peer);
+    OPENSSL_free(kat_secret);
+    EVP_PKEY_free(pkey);
+    EVP_PKEY_free(peerkey);
+    EVP_PKEY_CTX_free(kactx);
+    EVP_PKEY_CTX_free(dctx);
+    return ret;
+}
+#endif /* OPENSSL_NO_DH */
+
+/* TODO(3.0): To be removed */
+static int dummy_evp_call(void *provctx)
+{
+    OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
+    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
+    EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA256", NULL);
+    EVP_KDF *kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, NULL);
     unsigned char dgst[SHA256_DIGEST_LENGTH];
+    unsigned int dgstlen;
     int ret = 0;
     BN_CTX *bnctx = NULL;
     BIGNUM *a = NULL, *b = NULL;
@@ -243,6 +482,13 @@ static int dummy_evp_call(void *provctx)
     EC_KEY *key = NULL;
 #endif
 
+    static const char msg[] = "Hello World!";
+    static const unsigned char exptd[] = {
+        0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
+        0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
+        0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
+    };
+
     if (ctx == NULL || sha256 == NULL || drbg == NULL || kdf == NULL)
         goto err;
 
@@ -284,54 +530,17 @@ static int dummy_evp_call(void *provctx)
     if (!EC_KEY_generate_key(key))
         goto err;
 #endif
-    if (!rawnative_fromhex(dsa_p_hex, &dsa_p, &dsa_p_len)
-            || !rawnative_fromhex(dsa_q_hex, &dsa_q, &dsa_q_len)
-            || !rawnative_fromhex(dsa_g_hex, &dsa_g, &dsa_g_len)
-            || !rawnative_fromhex(dsa_pub_hex, &dsa_pub, &dsa_pub_len)
-            || !rawnative_fromhex(dsa_priv_hex, &dsa_priv, &dsa_priv_len))
-        goto err;
-
-    p = params;
-    *p++ = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_FFC_P, dsa_p, dsa_p_len);
-    *p++ = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_FFC_Q, dsa_q, dsa_q_len);
-    *p++ = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_FFC_G, dsa_g, dsa_g_len);
-    *p++ = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_DSA_PUB_KEY,
-                                   dsa_pub, dsa_pub_len);
-    *p++ = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_DSA_PRIV_KEY,
-                                   dsa_priv, dsa_priv_len);
-    *p = OSSL_PARAM_construct_end();
-
-    kctx = EVP_PKEY_CTX_new_from_name(libctx, SN_dsa, "");
-    if (kctx == NULL)
-        goto err;
-    if (EVP_PKEY_key_fromdata_init(kctx) <= 0
-            || EVP_PKEY_fromdata(kctx, &pkey, params) <= 0)
-        goto err;
 
-    sctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey);
-    if (sctx == NULL)
-        goto err;;
-
-    if (EVP_PKEY_sign_init(sctx) <= 0)
+#ifndef OPENSSL_NO_DSA
+    if (!dsa_key_signature_test(libctx))
         goto err;
+#endif
 
-    /* set signature parameters */
-    sigdgstlen = SHA256_DIGEST_LENGTH;
-    p = params;
-    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
-                                            SN_sha256,
-                                            strlen(SN_sha256) + 1);
-
-    *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
-                                       &sigdgstlen);
-    *p = OSSL_PARAM_construct_end();
-    if (EVP_PKEY_CTX_set_params(sctx, params) <= 0)
+#ifndef OPENSSL_NO_DH
+    if (!dh_key_exchange_test(libctx))
         goto err;
+#endif /* OPENSSL_NO_DH */
 
-    if (EVP_PKEY_sign(sctx, sig, &siglen, dgst, sizeof(dgst)) <= 0
-            || EVP_PKEY_verify_init(sctx) <= 0
-            || EVP_PKEY_verify(sctx, sig, siglen, dgst, sizeof(dgst)) <= 0)
-        goto err;
     ret = 1;
  err:
     BN_CTX_end(bnctx);
@@ -344,14 +553,6 @@ static int dummy_evp_call(void *provctx)
 #ifndef OPENSSL_NO_EC
     EC_KEY_free(key);
 #endif
-    OPENSSL_free(dsa_p);
-    OPENSSL_free(dsa_q);
-    OPENSSL_free(dsa_g);
-    OPENSSL_free(dsa_pub);
-    OPENSSL_free(dsa_priv);
-    EVP_PKEY_free(pkey);
-    EVP_PKEY_CTX_free(kctx);
-    EVP_PKEY_CTX_free(sctx);
     return ret;
 }
 
@@ -588,6 +789,13 @@ static const OSSL_ALGORITHM fips_kdfs[] = {
     { NULL, NULL, NULL }
 };
 
+static const OSSL_ALGORITHM fips_keyexch[] = {
+#ifndef OPENSSL_NO_DH
+    { "DH:dhKeyAgreement", "fips=yes", dh_keyexch_functions },
+#endif
+    { NULL, NULL, NULL }
+};
+
 static const OSSL_ALGORITHM fips_signature[] = {
 #ifndef OPENSSL_NO_DSA
     { "DSA:dsaEncryption", "fips=yes", dsa_signature_functions },
@@ -596,6 +804,9 @@ static const OSSL_ALGORITHM fips_signature[] = {
 };
 
 static const OSSL_ALGORITHM fips_keymgmt[] = {
+#ifndef OPENSSL_NO_DH
+    { "DH:dhKeyAgreement", "fips=yes", dh_keymgmt_functions },
+#endif
 #ifndef OPENSSL_NO_DSA
     { "DSA", "fips=yes", dsa_keymgmt_functions },
 #endif
@@ -619,6 +830,8 @@ static const OSSL_ALGORITHM *fips_query(OSSL_PROVIDER *prov,
         return fips_kdfs;
     case OSSL_OP_KEYMGMT:
         return fips_keymgmt;
+    case OSSL_OP_KEYEXCH:
+        return fips_keyexch;
     case OSSL_OP_SIGNATURE:
         return fips_signature;
     }
diff --git a/providers/implementations/exchange/dh_exch.c b/providers/implementations/exchange/dh_exch.c
index 1b16a83245..94c232965f 100644
--- a/providers/implementations/exchange/dh_exch.c
+++ b/providers/implementations/exchange/dh_exch.c
@@ -13,6 +13,8 @@
 #include <openssl/dh.h>
 #include <openssl/params.h>
 #include "prov/implementations.h"
+#include "prov/provider_ctx.h"
+#include "crypto/dh.h"
 
 static OSSL_OP_keyexch_newctx_fn dh_newctx;
 static OSSL_OP_keyexch_init_fn dh_init;
@@ -30,6 +32,7 @@ static OSSL_OP_keyexch_settable_ctx_params_fn dh_settable_ctx_params;
  */
 
 typedef struct {
+    OPENSSL_CTX *libctx;
     DH *dh;
     DH *dhpeer;
     unsigned int pad : 1;
@@ -37,7 +40,12 @@ typedef struct {
 
 static void *dh_newctx(void *provctx)
 {
-    return OPENSSL_zalloc(sizeof(PROV_DH_CTX));
+    PROV_DH_CTX *pdhctx = OPENSSL_zalloc(sizeof(PROV_DH_CTX));
+
+    if (pdhctx == NULL)
+        return NULL;
+    pdhctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
+    return pdhctx;
 }
 
 static int dh_init(void *vpdhctx, void *vdh)
@@ -83,8 +91,10 @@ static int dh_derive(void *vpdhctx, unsigned char *secret, size_t *secretlen,
         return 0;
 
     DH_get0_key(pdhctx->dhpeer, &pub_key, NULL);
-    ret = (pdhctx->pad) ? DH_compute_key_padded(secret, pub_key, pdhctx->dh)
-                        : DH_compute_key(secret, pub_key, pdhctx->dh);
+    if (pdhctx->pad)
+        ret = dh_compute_key_padded(pdhctx->libctx, secret, pub_key, pdhctx->dh);
+    else
+        ret = dh_compute_key(pdhctx->libctx, secret, pub_key, pdhctx->dh);
     if (ret <= 0)
         return 0;
 


More information about the openssl-commits mailing list