Move bn_add_words() and bn_sub_words from bn_asm.c to bn_add.c.
authorjsing <jsing@openbsd.org>
Mon, 23 Jan 2023 10:31:03 +0000 (10:31 +0000)
committerjsing <jsing@openbsd.org>
Mon, 23 Jan 2023 10:31:03 +0000 (10:31 +0000)
These are wrapped with #ifndef HAVE_BN_ADD_WORDS/HAVE_BN_SUB_WORDS, which
are defined for architectures that provide their own assembly versions.

lib/libcrypto/bn/arch/amd64/bn_arch.h
lib/libcrypto/bn/arch/i386/bn_arch.h
lib/libcrypto/bn/arch/mips64/bn_arch.h
lib/libcrypto/bn/arch/powerpc/bn_arch.h
lib/libcrypto/bn/arch/sparc/bn_arch.h
lib/libcrypto/bn/bn_add.c
lib/libcrypto/bn/bn_asm.c

index 7fb4e98..fd4a6cd 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: bn_arch.h,v 1.3 2023/01/21 17:30:50 jsing Exp $ */
+/*     $OpenBSD: bn_arch.h,v 1.4 2023/01/23 10:31:03 jsing Exp $ */
 /*
  * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
  *
@@ -20,6 +20,8 @@
 
 #ifndef OPENSSL_NO_ASM
 
+#define HAVE_BN_ADD_WORDS
+
 #define HAVE_BN_MUL_COMBA4
 #define HAVE_BN_MUL_COMBA8
 
@@ -27,5 +29,7 @@
 #define HAVE_BN_SQR_COMBA4
 #define HAVE_BN_SQR_COMBA8
 
+#define HAVE_BN_SUB_WORDS
+
 #endif
 #endif
index 17d22f3..ab35efd 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: bn_arch.h,v 1.2 2023/01/20 17:31:52 jsing Exp $ */
+/*     $OpenBSD: bn_arch.h,v 1.3 2023/01/23 10:31:03 jsing Exp $ */
 /*
  * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
  *
 
 #ifndef OPENSSL_NO_ASM
 
+#define HAVE_BN_ADD_WORDS
+
 #define HAVE_BN_MUL_COMBA4
 #define HAVE_BN_MUL_COMBA8
 
 #define HAVE_BN_SQR_COMBA4
 #define HAVE_BN_SQR_COMBA8
 
+#define HAVE_BN_SUB_WORDS
+
 #endif
 #endif
index 8e8fd11..e22d882 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: bn_arch.h,v 1.3 2023/01/20 17:31:52 jsing Exp $ */
+/*     $OpenBSD: bn_arch.h,v 1.4 2023/01/23 10:31:03 jsing Exp $ */
 /*
  * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
  *
@@ -20,6 +20,8 @@
 
 #ifndef OPENSSL_NO_ASM
 
+#define HAVE_BN_ADD_WORDS
+
 #define HAVE_BN_DIV_3_WORDS
 
 #define HAVE_BN_MUL_COMBA4
@@ -28,5 +30,7 @@
 #define HAVE_BN_SQR_COMBA4
 #define HAVE_BN_SQR_COMBA8
 
+#define HAVE_BN_SUB_WORDS
+
 #endif
 #endif
index 17d22f3..ab35efd 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: bn_arch.h,v 1.2 2023/01/20 17:31:52 jsing Exp $ */
+/*     $OpenBSD: bn_arch.h,v 1.3 2023/01/23 10:31:03 jsing Exp $ */
 /*
  * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
  *
 
 #ifndef OPENSSL_NO_ASM
 
+#define HAVE_BN_ADD_WORDS
+
 #define HAVE_BN_MUL_COMBA4
 #define HAVE_BN_MUL_COMBA8
 
 #define HAVE_BN_SQR_COMBA4
 #define HAVE_BN_SQR_COMBA8
 
+#define HAVE_BN_SUB_WORDS
+
 #endif
 #endif
index 17d22f3..ab35efd 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: bn_arch.h,v 1.2 2023/01/20 17:31:52 jsing Exp $ */
+/*     $OpenBSD: bn_arch.h,v 1.3 2023/01/23 10:31:03 jsing Exp $ */
 /*
  * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
  *
 
 #ifndef OPENSSL_NO_ASM
 
+#define HAVE_BN_ADD_WORDS
+
 #define HAVE_BN_MUL_COMBA4
 #define HAVE_BN_MUL_COMBA8
 
 #define HAVE_BN_SQR_COMBA4
 #define HAVE_BN_SQR_COMBA8
 
+#define HAVE_BN_SUB_WORDS
+
 #endif
 #endif
index 3352e0e..b3fed56 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: bn_add.c,v 1.17 2023/01/20 04:49:48 jsing Exp $ */
+/* $OpenBSD: bn_add.c,v 1.18 2023/01/23 10:31:03 jsing Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * [including the GNU Public Licence.]
  */
 
+#include <assert.h>
 #include <stdio.h>
 
 #include <openssl/err.h>
 
 #include "bn_local.h"
 
+#ifndef HAVE_BN_ADD_WORDS
+#ifdef BN_LLONG
+BN_ULONG
+bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
+{
+       BN_ULLONG ll = 0;
+
+       assert(n >= 0);
+       if (n <= 0)
+               return ((BN_ULONG)0);
+
+#ifndef OPENSSL_SMALL_FOOTPRINT
+       while (n & ~3) {
+               ll += (BN_ULLONG)a[0] + b[0];
+               r[0] = (BN_ULONG)ll & BN_MASK2;
+               ll >>= BN_BITS2;
+               ll += (BN_ULLONG)a[1] + b[1];
+               r[1] = (BN_ULONG)ll & BN_MASK2;
+               ll >>= BN_BITS2;
+               ll += (BN_ULLONG)a[2] + b[2];
+               r[2] = (BN_ULONG)ll & BN_MASK2;
+               ll >>= BN_BITS2;
+               ll += (BN_ULLONG)a[3] + b[3];
+               r[3] = (BN_ULONG)ll & BN_MASK2;
+               ll >>= BN_BITS2;
+               a += 4;
+               b += 4;
+               r += 4;
+               n -= 4;
+       }
+#endif
+       while (n) {
+               ll += (BN_ULLONG)a[0] + b[0];
+               r[0] = (BN_ULONG)ll & BN_MASK2;
+               ll >>= BN_BITS2;
+               a++;
+               b++;
+               r++;
+               n--;
+       }
+       return ((BN_ULONG)ll);
+}
+#else /* !BN_LLONG */
+BN_ULONG
+bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
+{
+       BN_ULONG c, l, t;
+
+       assert(n >= 0);
+       if (n <= 0)
+               return ((BN_ULONG)0);
+
+       c = 0;
+#ifndef OPENSSL_SMALL_FOOTPRINT
+       while (n & ~3) {
+               t = a[0];
+               t = (t + c) & BN_MASK2;
+               c = (t < c);
+               l = (t + b[0]) & BN_MASK2;
+               c += (l < t);
+               r[0] = l;
+               t = a[1];
+               t = (t + c) & BN_MASK2;
+               c = (t < c);
+               l = (t + b[1]) & BN_MASK2;
+               c += (l < t);
+               r[1] = l;
+               t = a[2];
+               t = (t + c) & BN_MASK2;
+               c = (t < c);
+               l = (t + b[2]) & BN_MASK2;
+               c += (l < t);
+               r[2] = l;
+               t = a[3];
+               t = (t + c) & BN_MASK2;
+               c = (t < c);
+               l = (t + b[3]) & BN_MASK2;
+               c += (l < t);
+               r[3] = l;
+               a += 4;
+               b += 4;
+               r += 4;
+               n -= 4;
+       }
+#endif
+       while (n) {
+               t = a[0];
+               t = (t + c) & BN_MASK2;
+               c = (t < c);
+               l = (t + b[0]) & BN_MASK2;
+               c += (l < t);
+               r[0] = l;
+               a++;
+               b++;
+               r++;
+               n--;
+       }
+       return ((BN_ULONG)c);
+}
+#endif /* !BN_LLONG */
+#endif
+
+#ifndef HAVE_BN_SUB_WORDS
+BN_ULONG
+bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
+{
+       BN_ULONG t1, t2;
+       int c = 0;
+
+       assert(n >= 0);
+       if (n <= 0)
+               return ((BN_ULONG)0);
+
+#ifndef OPENSSL_SMALL_FOOTPRINT
+       while (n&~3) {
+               t1 = a[0];
+               t2 = b[0];
+               r[0] = (t1 - t2 - c) & BN_MASK2;
+               if (t1 != t2)
+                       c = (t1 < t2);
+               t1 = a[1];
+               t2 = b[1];
+               r[1] = (t1 - t2 - c) & BN_MASK2;
+               if (t1 != t2)
+                       c = (t1 < t2);
+               t1 = a[2];
+               t2 = b[2];
+               r[2] = (t1 - t2 - c) & BN_MASK2;
+               if (t1 != t2)
+                       c = (t1 < t2);
+               t1 = a[3];
+               t2 = b[3];
+               r[3] = (t1 - t2 - c) & BN_MASK2;
+               if (t1 != t2)
+                       c = (t1 < t2);
+               a += 4;
+               b += 4;
+               r += 4;
+               n -= 4;
+       }
+#endif
+       while (n) {
+               t1 = a[0];
+               t2 = b[0];
+               r[0] = (t1 - t2 - c) & BN_MASK2;
+               if (t1 != t2)
+                       c = (t1 < t2);
+               a++;
+               b++;
+               r++;
+               n--;
+       }
+       return (c);
+}
+#endif
+
 int
 BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
 {
index df4ddae..4224396 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: bn_asm.c,v 1.19 2023/01/20 17:31:52 jsing Exp $ */
+/* $OpenBSD: bn_asm.c,v 1.20 2023/01/23 10:31:03 jsing Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
@@ -325,158 +325,6 @@ bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
 }
 #endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */
 
-#ifdef BN_LLONG
-BN_ULONG
-bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
-{
-       BN_ULLONG ll = 0;
-
-       assert(n >= 0);
-       if (n <= 0)
-               return ((BN_ULONG)0);
-
-#ifndef OPENSSL_SMALL_FOOTPRINT
-       while (n & ~3) {
-               ll += (BN_ULLONG)a[0] + b[0];
-               r[0] = (BN_ULONG)ll & BN_MASK2;
-               ll >>= BN_BITS2;
-               ll += (BN_ULLONG)a[1] + b[1];
-               r[1] = (BN_ULONG)ll & BN_MASK2;
-               ll >>= BN_BITS2;
-               ll += (BN_ULLONG)a[2] + b[2];
-               r[2] = (BN_ULONG)ll & BN_MASK2;
-               ll >>= BN_BITS2;
-               ll += (BN_ULLONG)a[3] + b[3];
-               r[3] = (BN_ULONG)ll & BN_MASK2;
-               ll >>= BN_BITS2;
-               a += 4;
-               b += 4;
-               r += 4;
-               n -= 4;
-       }
-#endif
-       while (n) {
-               ll += (BN_ULLONG)a[0] + b[0];
-               r[0] = (BN_ULONG)ll & BN_MASK2;
-               ll >>= BN_BITS2;
-               a++;
-               b++;
-               r++;
-               n--;
-       }
-       return ((BN_ULONG)ll);
-}
-#else /* !BN_LLONG */
-BN_ULONG
-bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
-{
-       BN_ULONG c, l, t;
-
-       assert(n >= 0);
-       if (n <= 0)
-               return ((BN_ULONG)0);
-
-       c = 0;
-#ifndef OPENSSL_SMALL_FOOTPRINT
-       while (n & ~3) {
-               t = a[0];
-               t = (t + c) & BN_MASK2;
-               c = (t < c);
-               l = (t + b[0]) & BN_MASK2;
-               c += (l < t);
-               r[0] = l;
-               t = a[1];
-               t = (t + c) & BN_MASK2;
-               c = (t < c);
-               l = (t + b[1]) & BN_MASK2;
-               c += (l < t);
-               r[1] = l;
-               t = a[2];
-               t = (t + c) & BN_MASK2;
-               c = (t < c);
-               l = (t + b[2]) & BN_MASK2;
-               c += (l < t);
-               r[2] = l;
-               t = a[3];
-               t = (t + c) & BN_MASK2;
-               c = (t < c);
-               l = (t + b[3]) & BN_MASK2;
-               c += (l < t);
-               r[3] = l;
-               a += 4;
-               b += 4;
-               r += 4;
-               n -= 4;
-       }
-#endif
-       while (n) {
-               t = a[0];
-               t = (t + c) & BN_MASK2;
-               c = (t < c);
-               l = (t + b[0]) & BN_MASK2;
-               c += (l < t);
-               r[0] = l;
-               a++;
-               b++;
-               r++;
-               n--;
-       }
-       return ((BN_ULONG)c);
-}
-#endif /* !BN_LLONG */
-
-BN_ULONG
-bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
-{
-       BN_ULONG t1, t2;
-       int c = 0;
-
-       assert(n >= 0);
-       if (n <= 0)
-               return ((BN_ULONG)0);
-
-#ifndef OPENSSL_SMALL_FOOTPRINT
-       while (n&~3) {
-               t1 = a[0];
-               t2 = b[0];
-               r[0] = (t1 - t2 - c) & BN_MASK2;
-               if (t1 != t2)
-                       c = (t1 < t2);
-               t1 = a[1];
-               t2 = b[1];
-               r[1] = (t1 - t2 - c) & BN_MASK2;
-               if (t1 != t2)
-                       c = (t1 < t2);
-               t1 = a[2];
-               t2 = b[2];
-               r[2] = (t1 - t2 - c) & BN_MASK2;
-               if (t1 != t2)
-                       c = (t1 < t2);
-               t1 = a[3];
-               t2 = b[3];
-               r[3] = (t1 - t2 - c) & BN_MASK2;
-               if (t1 != t2)
-                       c = (t1 < t2);
-               a += 4;
-               b += 4;
-               r += 4;
-               n -= 4;
-       }
-#endif
-       while (n) {
-               t1 = a[0];
-               t2 = b[0];
-               r[0] = (t1 - t2 - c) & BN_MASK2;
-               if (t1 != t2)
-                       c = (t1 < t2);
-               a++;
-               b++;
-               r++;
-               n--;
-       }
-       return (c);
-}
-
 #if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT)
 
 #ifdef OPENSSL_NO_ASM