Commit 84782b18e4 for openssl.org

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

    include/internal/safe_math.h: extend the domain of safe fused multiply and divide

    Keep the direct multiplication fast path and use binary addition of
    quotient/remainder pairs when the product does not fit. Store signed
    magnitudes as negative values so that the minimum value is supported
    without a wider integer type.

    Fixes: b037e3637a49 "header: add integer overflow helper functions"
    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:13 2026
    Merged-from: https://github.com/openssl/openssl/pull/33917

diff --git a/include/internal/safe_math.h b/include/internal/safe_math.h
index d174e07d94..42b9076f75 100644
--- a/include/internal/safe_math.h
+++ b/include/internal/safe_math.h
@@ -297,32 +297,26 @@
 /*
  * Safe fused multiply divide helpers
  *
- * These are a bit obscure:
- *    . They begin by checking the denominator for zero and getting rid of this
- *      corner case.
+ * First try the multiplication directly.  If it overflows, add and double
+ * quotient/remainder pairs while consuming the bits of b.  For unsigned
+ * inputs, (x, y) describes the current term as x * c + y, and (q, r)
+ * describes the accumulated sum.  Remainders stay below c: test against
+ * c - y before adding y so that the remainder addition cannot overflow.
  *
- *    . Second is an attempt to do the multiplication directly, if it doesn't
- *      overflow, the quotient is returned (for signed values there is a
- *      potential problem here which isn't present for unsigned).
+ * Signed inputs use negative magnitudes so that the minimum value remains
+ * representable.  The quotient and remainder bounds are reversed, and the
+ * result sign is restored at the end.
  *
- *    . Finally, the multiplication/division is transformed so that the larger
- *      of the numerators is divided first.  This requires a remainder
- *      correction:
- *
- *          a b / c = (a / c) b + (a mod c) b / c, where a > b
- *
- *      The individual operations need to be overflow checked (again signed
- *      being more problematic).
- *
- * The algorithm used is not perfect but it should be "good enough".
+ * Quotient magnitudes only increase, and a term is doubled only if a higher
+ * bit of b is still needed.  Thus an intermediate quotient overflow implies
+ * that the final quotient cannot fit.  No wider integer type is needed.
  */
-#define OSSL_SAFE_MATH_MULDIVS(type_name, type, max)                    \
+
+#define OSSL_SAFE_MATH_MULDIVS(type_name, type, min, max)               \
     static ossl_inline ossl_unused type safe_muldiv_##type_name(type a, \
-        type b,                                                         \
-        type c,                                                         \
-        int *err)                                                       \
+        type b, type c, int *err)                                       \
     {                                                                   \
-        int e2 = 0;                                                     \
+        int e2 = 0, negative;                                           \
         type q, r, x, y;                                                \
                                                                         \
         if (c == 0) {                                                   \
@@ -332,27 +326,58 @@
         x = safe_mul_##type_name(a, b, &e2);                            \
         if (!e2)                                                        \
             return safe_div_##type_name(x, c, err);                     \
-        if (b > a) {                                                    \
-            x = b;                                                      \
-            b = a;                                                      \
-            a = x;                                                      \
+                                                                        \
+        /* Use negative magnitudes: MIN need never be negated. */       \
+        negative = (a < 0) ^ (b < 0) ^ (c < 0);                         \
+        if (a > 0)                                                      \
+            a = -a;                                                     \
+        if (b > 0)                                                      \
+            b = -b;                                                     \
+        if (c > 0)                                                      \
+            c = -c;                                                     \
+        x = c == -1 ? a : -(a / c);                                     \
+        y = c == -1 ? 0 : a % c;                                        \
+        q = r = 0;                                                      \
+        e2 = 0;                                                         \
+        while (b != 0) {                                                \
+            if (b % 2 != 0) {                                           \
+                q = safe_add_##type_name(q, x, &e2);                    \
+                if (r <= c - y) {                                       \
+                    r -= c - y;                                         \
+                    q = safe_add_##type_name(q, -1, &e2);               \
+                } else {                                                \
+                    r += y;                                             \
+                }                                                       \
+                if (e2)                                                 \
+                    goto overflow;                                      \
+            }                                                           \
+            b /= 2;                                                     \
+            if (b == 0)                                                 \
+                break;                                                  \
+            /* Double the current quotient/remainder pair. */           \
+            x = safe_add_##type_name(x, x, &e2);                        \
+            if (y <= c - y) {                                           \
+                y -= c - y;                                             \
+                x = safe_add_##type_name(x, -1, &e2);                   \
+            } else {                                                    \
+                y += y;                                                 \
+            }                                                           \
+            if (e2)                                                     \
+                goto overflow;                                          \
         }                                                               \
-        q = safe_div_##type_name(a, c, err);                            \
-        r = safe_mod_##type_name(a, c, err);                            \
-        x = safe_mul_##type_name(r, b, err);                            \
-        y = safe_mul_##type_name(q, b, err);                            \
-        q = safe_div_##type_name(x, c, err);                            \
-        return safe_add_##type_name(y, q, err);                         \
+        return negative ? q : safe_neg_##type_name(q, err);             \
+                                                                        \
+    overflow:                                                           \
+        *err |= 1;                                                      \
+        return negative ? min : max;                                    \
     }

 #define OSSL_SAFE_MATH_MULDIVU(type_name, type, max)                    \
     static ossl_inline ossl_unused type safe_muldiv_##type_name(type a, \
-        type b,                                                         \
-        type c,                                                         \
-        int *err)                                                       \
+        type b, type c, int *err)                                       \
     {                                                                   \
         int e2 = 0;                                                     \
-        type x, y;                                                      \
+        type q, r, x, y;                                                \
                                                                         \
         if (c == 0) {                                                   \
             *err |= 1;                                                  \
@@ -361,14 +386,42 @@
         x = safe_mul_##type_name(a, b, &e2);                            \
         if (!e2)                                                        \
             return x / c;                                               \
-        if (b > a) {                                                    \
-            x = b;                                                      \
-            b = a;                                                      \
-            a = x;                                                      \
+                                                                        \
+        x = a / c;                                                      \
+        y = a % c;                                                      \
+        q = r = 0;                                                      \
+        e2 = 0;                                                         \
+        while (b != 0) {                                                \
+            if (b % 2 != 0) {                                           \
+                q = safe_add_##type_name(q, x, &e2);                    \
+                if (r >= c - y) {                                       \
+                    r -= c - y;                                         \
+                    q = safe_add_##type_name(q, 1, &e2);                \
+                } else {                                                \
+                    r += y;                                             \
+                }                                                       \
+                if (e2)                                                 \
+                    goto overflow;                                      \
+            }                                                           \
+            b /= 2;                                                     \
+            if (b == 0)                                                 \
+                break;                                                  \
+            /* Double the current quotient/remainder pair. */           \
+            x = safe_add_##type_name(x, x, &e2);                        \
+            if (y >= c - y) {                                           \
+                y -= c - y;                                             \
+                x = safe_add_##type_name(x, 1, &e2);                    \
+            } else {                                                    \
+                y += y;                                                 \
+            }                                                           \
+            if (e2)                                                     \
+                goto overflow;                                          \
         }                                                               \
-        x = safe_mul_##type_name(a % c, b, err);                        \
-        y = safe_mul_##type_name(a / c, b, err);                        \
-        return safe_add_##type_name(y, x / c, err);                     \
+        return q;                                                       \
+                                                                        \
+    overflow:                                                           \
+        *err |= 1;                                                      \
+        return max;                                                     \
     }

 /*
@@ -425,8 +478,9 @@
         OSSL_SAFE_MATH_MAXS(type))                                     \
     OSSL_SAFE_MATH_DIV_ROUND_UP(type_name, type,                       \
         OSSL_SAFE_MATH_MAXS(type))                                     \
-    OSSL_SAFE_MATH_MULDIVS(type_name, type, OSSL_SAFE_MATH_MAXS(type)) \
     OSSL_SAFE_MATH_NEGS(type_name, type, OSSL_SAFE_MATH_MINS(type))    \
+    OSSL_SAFE_MATH_MULDIVS(type_name, type, OSSL_SAFE_MATH_MINS(type), \
+        OSSL_SAFE_MATH_MAXS(type))                                     \
     OSSL_SAFE_MATH_ABSS(type_name, type, OSSL_SAFE_MATH_MINS(type))

 #define OSSL_SAFE_MATH_UNSIGNED(type_name, type)                       \