From 7069ede18dbe52fc34ffb8324c4326db3233d3f3 Mon Sep 17 00:00:00 2001 From: jsing Date: Mon, 23 Jan 2023 10:31:03 +0000 Subject: [PATCH] Move bn_add_words() and bn_sub_words from bn_asm.c to bn_add.c. 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 | 6 +- lib/libcrypto/bn/arch/i386/bn_arch.h | 6 +- lib/libcrypto/bn/arch/mips64/bn_arch.h | 6 +- lib/libcrypto/bn/arch/powerpc/bn_arch.h | 6 +- lib/libcrypto/bn/arch/sparc/bn_arch.h | 6 +- lib/libcrypto/bn/bn_add.c | 159 +++++++++++++++++++++++- lib/libcrypto/bn/bn_asm.c | 154 +---------------------- 7 files changed, 184 insertions(+), 159 deletions(-) diff --git a/lib/libcrypto/bn/arch/amd64/bn_arch.h b/lib/libcrypto/bn/arch/amd64/bn_arch.h index 7fb4e980e9c..fd4a6cde7ab 100644 --- a/lib/libcrypto/bn/arch/amd64/bn_arch.h +++ b/lib/libcrypto/bn/arch/amd64/bn_arch.h @@ -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 * @@ -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 diff --git a/lib/libcrypto/bn/arch/i386/bn_arch.h b/lib/libcrypto/bn/arch/i386/bn_arch.h index 17d22f3cec4..ab35efd56f5 100644 --- a/lib/libcrypto/bn/arch/i386/bn_arch.h +++ b/lib/libcrypto/bn/arch/i386/bn_arch.h @@ -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 * @@ -20,11 +20,15 @@ #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 diff --git a/lib/libcrypto/bn/arch/mips64/bn_arch.h b/lib/libcrypto/bn/arch/mips64/bn_arch.h index 8e8fd1110fe..e22d882970a 100644 --- a/lib/libcrypto/bn/arch/mips64/bn_arch.h +++ b/lib/libcrypto/bn/arch/mips64/bn_arch.h @@ -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 * @@ -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 diff --git a/lib/libcrypto/bn/arch/powerpc/bn_arch.h b/lib/libcrypto/bn/arch/powerpc/bn_arch.h index 17d22f3cec4..ab35efd56f5 100644 --- a/lib/libcrypto/bn/arch/powerpc/bn_arch.h +++ b/lib/libcrypto/bn/arch/powerpc/bn_arch.h @@ -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 * @@ -20,11 +20,15 @@ #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 diff --git a/lib/libcrypto/bn/arch/sparc/bn_arch.h b/lib/libcrypto/bn/arch/sparc/bn_arch.h index 17d22f3cec4..ab35efd56f5 100644 --- a/lib/libcrypto/bn/arch/sparc/bn_arch.h +++ b/lib/libcrypto/bn/arch/sparc/bn_arch.h @@ -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 * @@ -20,11 +20,15 @@ #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 diff --git a/lib/libcrypto/bn/bn_add.c b/lib/libcrypto/bn/bn_add.c index 3352e0e1d5c..b3fed564e90 100644 --- a/lib/libcrypto/bn/bn_add.c +++ b/lib/libcrypto/bn/bn_add.c @@ -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. * @@ -56,12 +56,169 @@ * [including the GNU Public Licence.] */ +#include #include #include #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) { diff --git a/lib/libcrypto/bn/bn_asm.c b/lib/libcrypto/bn/bn_asm.c index df4ddaea17e..4224396c00b 100644 --- a/lib/libcrypto/bn/bn_asm.c +++ b/lib/libcrypto/bn/bn_asm.c @@ -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 -- 2.20.1