22d461
diff -up openssl-3.0.1/crypto/dh/dh_key.c.fips3 openssl-3.0.1/crypto/dh/dh_key.c
22d461
--- openssl-3.0.1/crypto/dh/dh_key.c.fips3	2022-07-18 16:01:41.159543735 +0200
22d461
+++ openssl-3.0.1/crypto/dh/dh_key.c	2022-07-18 16:24:30.251388248 +0200
22d461
@@ -43,6 +43,9 @@ int ossl_dh_compute_key(unsigned char *k
22d461
     BN_MONT_CTX *mont = NULL;
22d461
     BIGNUM *z = NULL, *pminus1;
22d461
     int ret = -1;
22d461
+#ifdef FIPS_MODULE
22d461
+    int validate = 0;
22d461
+#endif
22d461
 
22d461
     if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
22d461
         ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
22d461
@@ -54,6 +57,13 @@ int ossl_dh_compute_key(unsigned char *k
22d461
         return 0;
22d461
     }
22d461
 
22d461
+#ifdef FIPS_MODULE
22d461
+    if (DH_check_pub_key(dh, pub_key, &validate) <= 0) {
22d461
+        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
22d461
+        return 0;
22d461
+    }
22d461
+#endif
22d461
+
22d461
     ctx = BN_CTX_new_ex(dh->libctx);
22d461
     if (ctx == NULL)
22d461
         goto err;
22d461
@@ -262,6 +272,9 @@ static int generate_key(DH *dh)
22d461
 #endif
22d461
     BN_CTX *ctx = NULL;
22d461
     BIGNUM *pub_key = NULL, *priv_key = NULL;
22d461
+#ifdef FIPS_MODULE
22d461
+    int validate = 0;
22d461
+#endif
22d461
 
22d461
     if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
22d461
         ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
22d461
@@ -354,8 +367,23 @@ static int generate_key(DH *dh)
22d461
     if (!ossl_dh_generate_public_key(ctx, dh, priv_key, pub_key))
22d461
         goto err;
22d461
 
22d461
+#ifdef FIPS_MODULE
22d461
+    if (DH_check_pub_key(dh, pub_key, &validate) <= 0) {
22d461
+        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
22d461
+        goto err;
22d461
+    }
22d461
+#endif
22d461
+
22d461
     dh->pub_key = pub_key;
22d461
     dh->priv_key = priv_key;
22d461
+#ifdef FIPS_MODULE
22d461
+    if (ossl_dh_check_pairwise(dh) <= 0) {
22d461
+        dh->pub_key = dh->priv_key = NULL;
22d461
+        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
22d461
+        goto err;
22d461
+    }
22d461
+#endif
22d461
+
22d461
     dh->dirty_cnt++;
22d461
     ok = 1;
22d461
  err:
22d461
diff -up openssl-3.0.1/crypto/ec/ec_key.c.fips3 openssl-3.0.1/crypto/ec/ec_key.c
22d461
diff -up openssl-3.0.1/providers/implementations/exchange/ecdh_exch.c.fips3 openssl-3.0.1/providers/implementations/exchange/ecdh_exch.c
22d461
--- openssl-3.0.1/providers/implementations/exchange/ecdh_exch.c.fips3	2022-07-25 13:42:46.814952053 +0200
22d461
+++ openssl-3.0.1/providers/implementations/exchange/ecdh_exch.c	2022-07-25 13:52:12.292065706 +0200
22d461
@@ -488,6 +488,25 @@ int ecdh_plain_derive(void *vpecdhctx, u
22d461
     }
22d461
 
22d461
     ppubkey = EC_KEY_get0_public_key(pecdhctx->peerk);
22d461
+#ifdef FIPS_MODULE
22d461
+    {
22d461
+        BN_CTX *bn_ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(privk));
22d461
+        int check = 0;
22d461
+
22d461
+        if (bn_ctx == NULL) {
22d461
+            ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
22d461
+            goto end;
22d461
+        }
22d461
+
22d461
+        check = ossl_ec_key_public_check(pecdhctx->peerk, bn_ctx);
22d461
+        BN_CTX_free(bn_ctx);
22d461
+
22d461
+        if (check <= 0) {
22d461
+            ERR_raise(ERR_LIB_PROV, EC_R_INVALID_PEER_KEY);
22d461
+            goto end;
22d461
+        }
22d461
+    }
22d461
+#endif
22d461
 
22d461
     retlen = ECDH_compute_key(secret, size, ppubkey, privk, NULL);
22d461
 
22d461
diff -up openssl-3.0.1/crypto/ec/ec_key.c.fips3 openssl-3.0.1/crypto/ec/ec_key.c
22d461
--- openssl-3.0.1/crypto/ec/ec_key.c.fips3	2022-07-25 14:03:34.420222507 +0200
22d461
+++ openssl-3.0.1/crypto/ec/ec_key.c	2022-07-25 14:09:00.728164294 +0200
22d461
@@ -336,6 +336,11 @@ static int ec_generate_key(EC_KEY *eckey
22d461
 
22d461
         OSSL_SELF_TEST_get_callback(eckey->libctx, &cb, &cbarg);
22d461
         ok = ecdsa_keygen_pairwise_test(eckey, cb, cbarg);
22d461
+
22d461
+#ifdef FIPS_MODULE
22d461
+        ok &= ossl_ec_key_public_check(eckey, ctx);
22d461
+        ok &= ossl_ec_key_pairwise_check(eckey, ctx);
22d461
+#endif /* FIPS_MODULE */
22d461
     }
22d461
 err:
22d461
     /* Step (9): If there is an error return an invalid keypair. */
22d461
diff -up openssl-3.0.1/crypto/rsa/rsa_gen.c.fips3 openssl-3.0.1/crypto/rsa/rsa_gen.c
22d461
--- openssl-3.0.1/crypto/rsa/rsa_gen.c.fips3	2022-07-25 17:02:17.807271297 +0200
22d461
+++ openssl-3.0.1/crypto/rsa/rsa_gen.c	2022-07-25 17:18:24.931959649 +0200
22d461
@@ -23,6 +23,7 @@
22d461
 #include <time.h>
22d461
 #include "internal/cryptlib.h"
22d461
 #include <openssl/bn.h>
22d461
+#include <openssl/obj_mac.h>
22d461
 #include <openssl/self_test.h>
22d461
 #include "prov/providercommon.h"
22d461
 #include "rsa_local.h"
22d461
@@ -476,52 +476,43 @@ static int rsa_keygen(OSSL_LIB_CTX *libc
22d461
 static int rsa_keygen_pairwise_test(RSA *rsa, OSSL_CALLBACK *cb, void *cbarg)
22d461
 {
22d461
     int ret = 0;
22d461
-    unsigned int ciphertxt_len;
22d461
-    unsigned char *ciphertxt = NULL;
22d461
-    const unsigned char plaintxt[16] = {0};
22d461
-    unsigned char *decoded = NULL;
22d461
-    unsigned int decoded_len;
22d461
-    unsigned int plaintxt_len = (unsigned int)sizeof(plaintxt_len);
22d461
-    int padding = RSA_PKCS1_PADDING;
22d461
+    unsigned int signature_len;
22d461
+    unsigned char *signature = NULL;
22d461
     OSSL_SELF_TEST *st = NULL;
22d461
+    static const unsigned char dgst[] = {
22d461
+        0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
22d461
+        0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
22d461
+        0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
22d461
+    };
22d461
 
22d461
     st = OSSL_SELF_TEST_new(cb, cbarg);
22d461
     if (st == NULL)
22d461
         goto err;
22d461
     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
22d461
+                           /* No special name for RSA signature PCT*/
22d461
                            OSSL_SELF_TEST_DESC_PCT_RSA_PKCS1);
22d461
 
22d461
-    ciphertxt_len = RSA_size(rsa);
22d461
+    signature_len = RSA_size(rsa);
22d461
-    /*
22d461
-     * RSA_private_encrypt() and RSA_private_decrypt() requires the 'to'
22d461
-     * parameter to be a maximum of RSA_size() - allocate space for both.
22d461
-     */
22d461
-    ciphertxt = OPENSSL_zalloc(ciphertxt_len * 2);
22d461
-    if (ciphertxt == NULL)
22d461
+    signature = OPENSSL_zalloc(signature_len);
22d461
+    if (signature == NULL)
22d461
         goto err;
22d461
-    decoded = ciphertxt + ciphertxt_len;
22d461
 
22d461
-    ciphertxt_len = RSA_public_encrypt(plaintxt_len, plaintxt, ciphertxt, rsa,
22d461
-                                       padding);
22d461
-    if (ciphertxt_len <= 0)
22d461
+    if (RSA_sign(NID_sha256, dgst, sizeof(dgst), signature, &signature_len, rsa) <= 0)
22d461
         goto err;
22d461
-    if (ciphertxt_len == plaintxt_len
22d461
-        && memcmp(ciphertxt, plaintxt, plaintxt_len) == 0)
22d461
+
22d461
+    if (signature_len <= 0)
22d461
         goto err;
22d461
 
22d461
-    OSSL_SELF_TEST_oncorrupt_byte(st, ciphertxt);
22d461
+    OSSL_SELF_TEST_oncorrupt_byte(st, signature);
22d461
 
22d461
-    decoded_len = RSA_private_decrypt(ciphertxt_len, ciphertxt, decoded, rsa,
22d461
-                                      padding);
22d461
-    if (decoded_len != plaintxt_len
22d461
-        || memcmp(decoded, plaintxt,  decoded_len) != 0)
22d461
+    if (RSA_verify(NID_sha256, dgst, sizeof(dgst), signature, signature_len, rsa) <= 0)
22d461
         goto err;
22d461
 
22d461
     ret = 1;
22d461
 err:
22d461
     OSSL_SELF_TEST_onend(st, ret);
22d461
     OSSL_SELF_TEST_free(st);
22d461
-    OPENSSL_free(ciphertxt);
22d461
+    OPENSSL_free(signature);
22d461
 
22d461
     return ret;
22d461
 }