Commit a90517c224 for openssl.org

commit a90517c22487dec7df69aed515f5e166f4f5db32
Author: Paul Dale <paul.dale@oracle.com>
Date:   Mon Nov 23 12:03:42 2026 +1100

    Test the extended safe multiply and divide range

    Check exact results, overflow, both operand orders and existing error
    bits for signed and unsigned inputs through 64 bits. Cover products at
    the signed minimum and intermediate products that do not fit.

    Add a recipe that runs the safe arithmetic tests with built-in overflow
    checks both enabled and disabled.

    Assisted-by: ChatGPT:gpt-6-Sol
    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    Reviewed-by: Neil Horman <nhorman@openssl.org>
    Merge-date: Tue Nov 24 18:13:14 2026
    Merged-from: https://github.com/openssl/openssl/pull/33917

diff --git a/test/build.info b/test/build.info
index 939783411e..67d688dc4d 100644
--- a/test/build.info
+++ b/test/build.info
@@ -58,7 +58,7 @@ IF[{- !$disabled{tests} -}]
           crltest danetest bad_dtls_test lhash_test sparse_array_test \
           conf_include_test conf_parse_list_test params_api_test params_conversion_test \
           constant_time_test crypto_memcmp_test ct_validation_helpers_test \
-          safe_math_test verify_extra_test clienthellotest \
+          safe_math_test safe_math_test_no_builtin verify_extra_test clienthellotest \
           packettest asynctest secmemtest srptest memleaktest stack_test \
           asn1_string_poison_test \
           ct_test threadstest d2i_test \
@@ -417,6 +417,11 @@ IF[{- !$disabled{tests} -}]
   INCLUDE[safe_math_test]=../include ../apps/include
   DEPEND[safe_math_test]=../libcrypto libtestutil.a

+  SOURCE[safe_math_test_no_builtin]=safe_math_test.c
+  INCLUDE[safe_math_test_no_builtin]=../include ../apps/include
+  DEPEND[safe_math_test_no_builtin]=../libcrypto libtestutil.a
+  DEFINE[safe_math_test_no_builtin]=OPENSSL_NO_BUILTIN_OVERFLOW_CHECKING
+
   SOURCE[verify_extra_test]=verify_extra_test.c
   INCLUDE[verify_extra_test]=../include ../apps/include
   DEPEND[verify_extra_test]=../libcrypto libtestutil.a
diff --git a/test/recipes/02-test_safe_math.t b/test/recipes/02-test_safe_math.t
new file mode 100644
index 0000000000..1abfe6e97d
--- /dev/null
+++ b/test/recipes/02-test_safe_math.t
@@ -0,0 +1,18 @@
+#! /usr/bin/env perl
+# Copyright 2026 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
+
+use strict;
+use warnings;
+use OpenSSL::Test;
+
+setup("test_safe_math");
+
+plan tests => 2;
+
+ok(run(test(["safe_math_test"])), "safe arithmetic");
+ok(run(test(["safe_math_test_no_builtin"])), "safe arithmetic without builtins");
diff --git a/test/safe_math_test.c b/test/safe_math_test.c
index f353f9730d..9609e707ad 100644
--- a/test/safe_math_test.c
+++ b/test/safe_math_test.c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2021-2024 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2021-2026 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
@@ -10,12 +10,6 @@
 #include <stdio.h>
 #include <stdlib.h>

-/*
- * Uncomment this if the fallback non-builtin overflow checking is to
- * be tested.
- */
-/*#define OPENSSL_NO_BUILTIN_OVERFLOW_CHECKING*/
-
 #include "internal/nelem.h"
 #include "internal/safe_math.h"
 #include "testutil.h"
@@ -24,6 +18,8 @@
 OSSL_SAFE_MATH_SIGNED(int, int)
 OSSL_SAFE_MATH_UNSIGNED(uint, unsigned int)
 OSSL_SAFE_MATH_UNSIGNED(size_t, size_t)
+OSSL_SAFE_MATH_SIGNED(int64_t, int64_t)
+OSSL_SAFE_MATH_UNSIGNED(uint64_t, uint64_t)

 static const struct {
     int a, b;
@@ -295,38 +291,70 @@ err:
 static const struct {
     int a, b, c;
     int err;
+    int result;
 } test_muldiv_ints[] = {
-    { 3, 1, 2, 0 },
-    { 1, 3, 2, 0 },
-    { -3, 1, 2, 0 },
-    { 1, 3, -2, 0 },
-    { INT_MAX, INT_MAX, INT_MAX, 0 },
-    { INT_MIN, INT_MIN, INT_MAX, 1 },
-    { INT_MIN, INT_MIN, INT_MIN, 0 },
-    { INT_MAX, 2, 4, 0 },
-    { 8, INT_MAX, 4, 1 },
-    { INT_MAX, 8, 4, 1 },
-    { INT_MIN, 2, 4, 1 },
-    { 8, INT_MIN, 4, 1 },
-    { INT_MIN, 8, 4, 1 },
-    { 3, 4, 0, 1 },
+    { 3, 1, 2, 0, 1 },
+    { 1, 3, 2, 0, 1 },
+    { -3, 1, 2, 0, -1 },
+    { 1, 3, -2, 0, -1 },
+    { INT_MAX, INT_MAX, INT_MAX, 0, INT_MAX },
+    { INT_MIN, INT_MIN, INT_MAX, 1, 0 },
+    { INT_MIN, INT_MIN, INT_MIN, 0, INT_MIN },
+    { INT_MAX, 2, 4, 0, INT_MAX / 2 },
+    { 8, INT_MAX, 4, 1, 0 },
+    { INT_MIN, 2, 4, 0, INT_MIN / 2 },
+    { 8, INT_MIN, 4, 1, 0 },
+    { INT_MIN, -1, INT_MIN, 0, -1 },
+    { INT_MIN, -1, 2, 0, -(INT_MIN / 2) },
+    { INT_MIN, -1, -1, 0, INT_MIN },
+    { INT_MIN, 1, -1, 1, 0 },
+    { INT_MIN, 2, 2, 0, INT_MIN },
+    { INT_MIN, 2, -2, 1, 0 },
+    { INT_MAX - 1, INT_MAX - 1, INT_MAX, 0, INT_MAX - 2 },
+    { -(INT_MAX - 1), INT_MAX - 1, INT_MAX, 0, -(INT_MAX - 2) },
+    { INT_MAX - 1, INT_MAX - 1, -INT_MAX, 0, -(INT_MAX - 2) },
+    { -(INT_MAX - 1), INT_MAX - 1, -INT_MAX, 0, INT_MAX - 2 },
+    { -(INT_MAX - 1), -(INT_MAX - 1), INT_MAX, 0, INT_MAX - 2 },
+    { -(INT_MAX - 1), -(INT_MAX - 1), -INT_MAX, 0, -(INT_MAX - 2) },
+    { INT_MAX, INT_MAX / 2 + 1, INT_MAX / 2 + 1, 0, INT_MAX },
+    { INT_MIN, INT_MAX, INT_MIN, 0, INT_MAX },
+    { INT_MAX, INT_MAX, INT_MIN, 0, -(INT_MAX - 1) },
+    { INT_MAX, INT_MAX, 1, 1, 0 },
+    { -INT_MAX, INT_MAX, 1, 1, 0 },
+    { 0, INT_MIN, -1, 0, 0 },
+    { INT_MAX, 0, INT_MIN, 0, 0 },
+    { 3, 4, 0, 1, 0 },
+    { 0, 4, 0, 1, 0 },
+    { 3, 0, 0, 1, 0 },
+    { 0, 0, 0, 1, 0 },
+    /* Minimum products and overflowing intermediate products. */
+    { INT_MIN / 2, 2, 4, 0, INT_MIN / 4 },
+    { -2, INT_MAX / 2 + 1, 3, 0, INT_MIN / 3 },
+    { INT_MIN / 2, 2, INT_MIN, 0, 1 },
+    { INT_MIN / 2, 2, -1, 1, 0 },
+    { INT_MIN / 2, -4, -2, 0, INT_MIN },
 };

 static int test_int_muldiv(int n)
 {
-    int err = 0;
-    int r, real = 0;
     const int a = test_muldiv_ints[n].a;
     const int b = test_muldiv_ints[n].b;
     const int c = test_muldiv_ints[n].c;
-
-    r = safe_muldiv_int(a, b, c, &err);
-    if (c != 0)
-        real = (int)((int64_t)a * (int64_t)b / (int64_t)c);
-    if (!TEST_int_eq(err, test_muldiv_ints[n].err)
-        || (!err && !TEST_int_eq(r, real))) {
-        TEST_info("%d * %d / %d  r = %d  err = %d", a, b, c, r, err);
-        return 0;
+    int initial, swap, err;
+    int r;
+
+    for (initial = 0; initial <= 2; initial += 2) {
+        for (swap = 0; swap < 2; swap++) {
+            err = initial;
+            r = safe_muldiv_int(swap ? b : a, swap ? a : b, c, &err);
+            if (!TEST_int_eq(err, initial | test_muldiv_ints[n].err)
+                || (!test_muldiv_ints[n].err
+                    && !TEST_int_eq(r, test_muldiv_ints[n].result))
+                || (c == 0 && !TEST_int_eq(r, a == 0 || b == 0 ? 0 : INT_MAX))) {
+                TEST_info("case %d: initial error = %d, swap = %d", n, initial, swap);
+                return 0;
+            }
+        }
     }
     return 1;
 }
@@ -334,31 +362,162 @@ static int test_int_muldiv(int n)
 static const struct {
     unsigned int a, b, c;
     int err;
+    unsigned int result;
 } test_muldiv_uints[] = {
-    { 3, 1, 2, 0 },
-    { 1, 3, 2, 0 },
-    { UINT_MAX, UINT_MAX, UINT_MAX, 0 },
-    { UINT_MAX, 2, 4, 0 },
-    { 8, UINT_MAX, 4, 1 },
-    { UINT_MAX, 8, 4, 1 },
-    { 3, 4, 0, 1 },
+    { 3, 1, 2, 0, 1 },
+    { 1, 3, 2, 0, 1 },
+    { UINT_MAX, UINT_MAX, UINT_MAX, 0, UINT_MAX },
+    { UINT_MAX, 2, 4, 0, UINT_MAX / 2 },
+    { 8, UINT_MAX, 4, 1, 0 },
+    { UINT_MAX - 1, UINT_MAX - 1, UINT_MAX, 0, UINT_MAX - 2 },
+    { UINT_MAX, UINT_MAX / 2 + 1, UINT_MAX / 2 + 1, 0, UINT_MAX },
+    { UINT_MAX, UINT_MAX, 1, 1, 0 },
+    { UINT_MAX, UINT_MAX, UINT_MAX - 1, 1, 0 },
+    { 0, UINT_MAX, 1, 0, 0 },
+    { UINT_MAX, 0, UINT_MAX, 0, 0 },
+    { 3, 4, 0, 1, 0 },
+    { 0, 4, 0, 1, 0 },
+    { 3, 0, 0, 1, 0 },
+    { 0, 0, 0, 1, 0 },
 };

 static int test_uint_muldiv(int n)
 {
-    int err = 0;
-    unsigned int r, real = 0;
     const unsigned int a = test_muldiv_uints[n].a;
     const unsigned int b = test_muldiv_uints[n].b;
     const unsigned int c = test_muldiv_uints[n].c;
+    int initial, swap, err;
+    unsigned int r;
+
+    for (initial = 0; initial <= 2; initial += 2) {
+        for (swap = 0; swap < 2; swap++) {
+            err = initial;
+            r = safe_muldiv_uint(swap ? b : a, swap ? a : b, c, &err);
+            if (!TEST_int_eq(err, initial | test_muldiv_uints[n].err)
+                || (!test_muldiv_uints[n].err
+                    && !TEST_uint_eq(r, test_muldiv_uints[n].result))
+                || (c == 0 && !TEST_uint_eq(r, a == 0 || b == 0 ? 0 : UINT_MAX))) {
+                TEST_info("case %d: initial error = %d, swap = %d", n, initial, swap);
+                return 0;
+            }
+        }
+    }
+    return 1;
+}

-    r = safe_muldiv_uint(a, b, c, &err);
-    if (c != 0)
-        real = (unsigned int)((uint64_t)a * (uint64_t)b / (uint64_t)c);
-    if (!TEST_int_eq(err, test_muldiv_uints[n].err)
-        || (!err && !TEST_uint_eq(r, real))) {
-        TEST_info("%u * %u / %u  r = %u  err = %d", a, b, c, r, err);
-        return 0;
+static const struct {
+    int64_t a, b, c;
+    int err;
+    int64_t result;
+} test_muldiv_int64_ts[] = {
+    { 3, 1, 2, 0, 1 },
+    { 1, 3, 2, 0, 1 },
+    { -3, 1, 2, 0, -1 },
+    { 1, 3, -2, 0, -1 },
+    { INT64_MAX, INT64_MAX, INT64_MAX, 0, INT64_MAX },
+    { INT64_MIN, INT64_MIN, INT64_MAX, 1, 0 },
+    { INT64_MIN, INT64_MIN, INT64_MIN, 0, INT64_MIN },
+    { INT64_MAX, 2, 4, 0, INT64_MAX / 2 },
+    { 8, INT64_MAX, 4, 1, 0 },
+    { INT64_MIN, 2, 4, 0, INT64_MIN / 2 },
+    { 8, INT64_MIN, 4, 1, 0 },
+    { INT64_MIN, -1, INT64_MIN, 0, -1 },
+    { INT64_MIN, -1, 2, 0, -(INT64_MIN / 2) },
+    { INT64_MIN, -1, -1, 0, INT64_MIN },
+    { INT64_MIN, 1, -1, 1, 0 },
+    { INT64_MIN, 2, 2, 0, INT64_MIN },
+    { INT64_MIN, 2, -2, 1, 0 },
+    { INT64_MAX - 1, INT64_MAX - 1, INT64_MAX, 0, INT64_MAX - 2 },
+    { -(INT64_MAX - 1), INT64_MAX - 1, INT64_MAX, 0, -(INT64_MAX - 2) },
+    { INT64_MAX - 1, INT64_MAX - 1, -INT64_MAX, 0, -(INT64_MAX - 2) },
+    { -(INT64_MAX - 1), INT64_MAX - 1, -INT64_MAX, 0, INT64_MAX - 2 },
+    { -(INT64_MAX - 1), -(INT64_MAX - 1), INT64_MAX, 0, INT64_MAX - 2 },
+    { -(INT64_MAX - 1), -(INT64_MAX - 1), -INT64_MAX, 0, -(INT64_MAX - 2) },
+    { INT64_MAX, INT64_MAX / 2 + 1, INT64_MAX / 2 + 1, 0, INT64_MAX },
+    { INT64_MIN, INT64_MAX, INT64_MIN, 0, INT64_MAX },
+    { INT64_MAX, INT64_MAX, INT64_MIN, 0, -(INT64_MAX - 1) },
+    { INT64_MAX, INT64_MAX, 1, 1, 0 },
+    { -INT64_MAX, INT64_MAX, 1, 1, 0 },
+    { 0, INT64_MIN, -1, 0, 0 },
+    { INT64_MAX, 0, INT64_MIN, 0, 0 },
+    { 3, 4, 0, 1, 0 },
+    { 0, 4, 0, 1, 0 },
+    { 3, 0, 0, 1, 0 },
+    { 0, 0, 0, 1, 0 },
+    /* Minimum products and overflowing intermediate products. */
+    { INT64_MIN / 2, 2, 4, 0, INT64_MIN / 4 },
+    { -2, INT64_MAX / 2 + 1, 3, 0, INT64_MIN / 3 },
+    { INT64_MIN / 2, 2, INT64_MIN, 0, 1 },
+    { INT64_MIN / 2, 2, -1, 1, 0 },
+    { INT64_MIN / 2, -4, -2, 0, INT64_MIN },
+};
+
+static int test_int64_t_muldiv(int n)
+{
+    const int64_t a = test_muldiv_int64_ts[n].a;
+    const int64_t b = test_muldiv_int64_ts[n].b;
+    const int64_t c = test_muldiv_int64_ts[n].c;
+    int initial, swap, err;
+    int64_t r;
+
+    for (initial = 0; initial <= 2; initial += 2) {
+        for (swap = 0; swap < 2; swap++) {
+            err = initial;
+            r = safe_muldiv_int64_t(swap ? b : a, swap ? a : b, c, &err);
+            if (!TEST_int_eq(err, initial | test_muldiv_int64_ts[n].err)
+                || (!test_muldiv_int64_ts[n].err
+                    && !TEST_int64_t_eq(r, test_muldiv_int64_ts[n].result))
+                || (c == 0 && !TEST_int64_t_eq(r, a == 0 || b == 0 ? 0 : INT64_MAX))) {
+                TEST_info("case %d: initial error = %d, swap = %d", n, initial, swap);
+                return 0;
+            }
+        }
+    }
+    return 1;
+}
+
+static const struct {
+    uint64_t a, b, c;
+    int err;
+    uint64_t result;
+} test_muldiv_uint64_ts[] = {
+    { 3, 1, 2, 0, 1 },
+    { 1, 3, 2, 0, 1 },
+    { UINT64_MAX, UINT64_MAX, UINT64_MAX, 0, UINT64_MAX },
+    { UINT64_MAX, 2, 4, 0, UINT64_MAX / 2 },
+    { 8, UINT64_MAX, 4, 1, 0 },
+    { UINT64_MAX - 1, UINT64_MAX - 1, UINT64_MAX, 0, UINT64_MAX - 2 },
+    { UINT64_MAX, UINT64_MAX / 2 + 1, UINT64_MAX / 2 + 1, 0, UINT64_MAX },
+    { UINT64_MAX, UINT64_MAX, 1, 1, 0 },
+    { UINT64_MAX, UINT64_MAX, UINT64_MAX - 1, 1, 0 },
+    { 0, UINT64_MAX, 1, 0, 0 },
+    { UINT64_MAX, 0, UINT64_MAX, 0, 0 },
+    { 3, 4, 0, 1, 0 },
+    { 0, 4, 0, 1, 0 },
+    { 3, 0, 0, 1, 0 },
+    { 0, 0, 0, 1, 0 },
+};
+
+static int test_uint64_t_muldiv(int n)
+{
+    const uint64_t a = test_muldiv_uint64_ts[n].a;
+    const uint64_t b = test_muldiv_uint64_ts[n].b;
+    const uint64_t c = test_muldiv_uint64_ts[n].c;
+    int initial, swap, err;
+    uint64_t r;
+
+    for (initial = 0; initial <= 2; initial += 2) {
+        for (swap = 0; swap < 2; swap++) {
+            err = initial;
+            r = safe_muldiv_uint64_t(swap ? b : a, swap ? a : b, c, &err);
+            if (!TEST_int_eq(err, initial | test_muldiv_uint64_ts[n].err)
+                || (!test_muldiv_uint64_ts[n].err
+                    && !TEST_uint64_t_eq(r, test_muldiv_uint64_ts[n].result))
+                || (c == 0 && !TEST_uint64_t_eq(r, a == 0 || b == 0 ? 0 : UINT64_MAX))) {
+                TEST_info("case %d: initial error = %d, swap = %d", n, initial, swap);
+                return 0;
+            }
+        }
     }
     return 1;
 }
@@ -370,5 +529,7 @@ int setup_tests(void)
     ADD_ALL_TESTS(test_size_t_ops, OSSL_NELEM(test_size_ts));
     ADD_ALL_TESTS(test_int_muldiv, OSSL_NELEM(test_muldiv_ints));
     ADD_ALL_TESTS(test_uint_muldiv, OSSL_NELEM(test_muldiv_uints));
+    ADD_ALL_TESTS(test_int64_t_muldiv, OSSL_NELEM(test_muldiv_int64_ts));
+    ADD_ALL_TESTS(test_uint64_t_muldiv, OSSL_NELEM(test_muldiv_uint64_ts));
     return 1;
 }