Commit 13bef2616f for openssl.org

commit 13bef2616f3435429acfe2af21955bbbb0aaa2a0
Author: Norbert Pocs <norbertp@openssl.org>
Date:   Tue Jul 14 14:31:24 2026 +0200

    tests: Check the return value of BN_hex2bn()

    Fixes coverity issues 1398635, 1696548, 1696549.

    Signed-off-by: Norbert Pocs <norbertp@openssl.org>

    Reviewed-by: Nikola Pajkovsky <nikolap@openssl.org>
    Reviewed-by: Tim Hudson <tjh@openssl.org>
    Reviewed-by: Paul Dale <paul.dale@oracle.com>
    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    MergeDate: Mon Jul 20 09:04:13 2026
    (Merged from https://github.com/openssl/openssl/pull/31943)

diff --git a/test/bntest.c b/test/bntest.c
index 5c8f76e1bc..d15af9dbe5 100644
--- a/test/bntest.c
+++ b/test/bntest.c
@@ -1800,15 +1800,16 @@ static int file_modexp(STANZA *s)
     }

     /* Regression test for carry propagation bug in sqr8x_reduction */
-    BN_hex2bn(&a, "050505050505");
-    BN_hex2bn(&b, "02");
-    BN_hex2bn(&c,
-        "4141414141414141414141274141414141414141414141414141414141414141"
-        "4141414141414141414141414141414141414141414141414141414141414141"
-        "4141414141414141414141800000000000000000000000000000000000000000"
-        "0000000000000000000000000000000000000000000000000000000000000000"
-        "0000000000000000000000000000000000000000000000000000000000000000"
-        "0000000000000000000000000000000000000000000000000000000001");
+    if (!TEST_true(BN_hex2bn(&a, "050505050505"))
+        || !TEST_true(BN_hex2bn(&b, "02"))
+        || !TEST_true(BN_hex2bn(&c,
+            "4141414141414141414141274141414141414141414141414141414141414141"
+            "4141414141414141414141414141414141414141414141414141414141414141"
+            "4141414141414141414141800000000000000000000000000000000000000000"
+            "0000000000000000000000000000000000000000000000000000000000000000"
+            "0000000000000000000000000000000000000000000000000000000000000000"
+            "0000000000000000000000000000000000000000000000000000000001")))
+        goto err;
     if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
         || !TEST_true(BN_mul(e, a, a, ctx))
         || !TEST_BN_eq(d, e))
diff --git a/test/ectest.c b/test/ectest.c
index 36fcb50089..363d119c96 100644
--- a/test/ectest.c
+++ b/test/ectest.c
@@ -1127,16 +1127,18 @@ static int group_field_test(void)
     EC_GROUP *secp521r1_group = NULL;
     EC_GROUP *sect163r2_group = NULL;

-    BN_hex2bn(&secp521r1_field,
-        "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
-        "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
-        "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
-        "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
-        "FFFF");
-
-    BN_hex2bn(&sect163r2_field,
-        "08000000000000000000000000000000"
-        "00000000C9");
+    if (!TEST_true(BN_hex2bn(&secp521r1_field,
+            "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
+            "FFFF"))
+        || !TEST_true(BN_hex2bn(&sect163r2_field,
+            "08000000000000000000000000000000"
+            "00000000C9"))) {
+        BN_free(secp521r1_field);
+        return 0;
+    }

     secp521r1_group = EC_GROUP_new_by_curve_name(NID_secp521r1);
     if (BN_cmp(secp521r1_field, EC_GROUP_get0_field(secp521r1_group)))
diff --git a/test/srptest.c b/test/srptest.c
index 0fceadd24b..d7620f95f9 100644
--- a/test/srptest.c
+++ b/test/srptest.c
@@ -149,11 +149,10 @@ static int run_srp_kat(void)
     /* use builtin 1024-bit params */
     const SRP_gN *GN;

-    if (!TEST_ptr(GN = SRP_get_default_gN("1024")))
-        goto err;
-    BN_hex2bn(&s, "BEB25379D1A8581EB5A727673A2441EE");
-    /* Set up server's password entry */
-    if (!TEST_true(SRP_create_verifier_BN("alice", "password123", &s, &v, GN->N,
+    if (!TEST_ptr(GN = SRP_get_default_gN("1024"))
+        || !TEST_true(BN_hex2bn(&s, "BEB25379D1A8581EB5A727673A2441EE"))
+        /* Set up server's password entry */
+        || !TEST_true(SRP_create_verifier_BN("alice", "password123", &s, &v, GN->N,
             GN->g)))
         goto err;

@@ -168,8 +167,9 @@ static int run_srp_kat(void)
     TEST_note("    okay");

     /* Server random */
-    BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"
-                  "05284D20");
+    if (!TEST_true(BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"
+                                 "05284D20")))
+        goto err;

     /* Server's first message */
     Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
@@ -187,8 +187,9 @@ static int run_srp_kat(void)
     TEST_note("    okay");

     /* Client random */
-    BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"
-                  "DA2D4393");
+    if (!TEST_true(BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"
+                                 "DA2D4393")))
+        goto err;

     /* Client's response */
     Apub = SRP_Calc_A(a, GN->N, GN->g);