From 8889fb9978b33e4e4b5379b76c5acd97ea3079c6 Mon Sep 17 00:00:00 2001 From: jsing Date: Mon, 23 Jan 2023 12:17:57 +0000 Subject: [PATCH] Move bn_mul_add_words() and bn_mul_words() from bn_asm.c to bn_mul.c. These are wrapped with #ifndef HAVE_BN_ADD_MUL_WORDS/HAVE_BN_MUL_WORDS, which are defined for architectures that provide their own assembly versions. --- lib/libcrypto/bn/arch/amd64/bn_arch.h | 4 +- lib/libcrypto/bn/arch/i386/bn_arch.h | 4 +- lib/libcrypto/bn/arch/mips64/bn_arch.h | 4 +- lib/libcrypto/bn/arch/powerpc/bn_arch.h | 4 +- lib/libcrypto/bn/arch/sparc/bn_arch.h | 4 +- lib/libcrypto/bn/bn_asm.c | 133 +--------------------- lib/libcrypto/bn/bn_mul.c | 141 +++++++++++++++++++++++- 7 files changed, 156 insertions(+), 138 deletions(-) diff --git a/lib/libcrypto/bn/arch/amd64/bn_arch.h b/lib/libcrypto/bn/arch/amd64/bn_arch.h index e8c9986bb30..065f6b1c3b4 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.6 2023/01/23 12:09:06 jsing Exp $ */ +/* $OpenBSD: bn_arch.h,v 1.7 2023/01/23 12:17:57 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -24,8 +24,10 @@ #define HAVE_BN_DIV_WORDS +#define HAVE_BN_MUL_ADD_WORDS #define HAVE_BN_MUL_COMBA4 #define HAVE_BN_MUL_COMBA8 +#define HAVE_BN_MUL_WORDS #define HAVE_BN_SQR #define HAVE_BN_SQR_COMBA4 diff --git a/lib/libcrypto/bn/arch/i386/bn_arch.h b/lib/libcrypto/bn/arch/i386/bn_arch.h index eeb273583ae..681c2090a70 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.5 2023/01/23 12:09:06 jsing Exp $ */ +/* $OpenBSD: bn_arch.h,v 1.6 2023/01/23 12:17:57 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -24,8 +24,10 @@ #define HAVE_BN_DIV_WORDS +#define HAVE_BN_MUL_ADD_WORDS #define HAVE_BN_MUL_COMBA4 #define HAVE_BN_MUL_COMBA8 +#define HAVE_BN_MUL_WORDS #define HAVE_BN_SQR_COMBA4 #define HAVE_BN_SQR_COMBA8 diff --git a/lib/libcrypto/bn/arch/mips64/bn_arch.h b/lib/libcrypto/bn/arch/mips64/bn_arch.h index f7676611d7a..53771bce1e7 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.6 2023/01/23 12:09:06 jsing Exp $ */ +/* $OpenBSD: bn_arch.h,v 1.7 2023/01/23 12:17:58 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -25,8 +25,10 @@ #define HAVE_BN_DIV_WORDS #define HAVE_BN_DIV_3_WORDS +#define HAVE_BN_MUL_ADD_WORDS #define HAVE_BN_MUL_COMBA4 #define HAVE_BN_MUL_COMBA8 +#define HAVE_BN_MUL_WORDS #define HAVE_BN_SQR_COMBA4 #define HAVE_BN_SQR_COMBA8 diff --git a/lib/libcrypto/bn/arch/powerpc/bn_arch.h b/lib/libcrypto/bn/arch/powerpc/bn_arch.h index eeb273583ae..46e932a2d55 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.5 2023/01/23 12:09:06 jsing Exp $ */ +/* $OpenBSD: bn_arch.h,v 1.6 2023/01/23 12:17:58 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -24,8 +24,10 @@ #define HAVE_BN_DIV_WORDS +#define HAVE_BN_MUL_ADD_WORDS #define HAVE_BN_MUL_COMBA4 #define HAVE_BN_MUL_COMBA8 +#define HAVE_BN_MUL_WORDS #define HAVE_BN_SQR_COMBA4 #define HAVE_BN_SQR_COMBA8 diff --git a/lib/libcrypto/bn/arch/sparc/bn_arch.h b/lib/libcrypto/bn/arch/sparc/bn_arch.h index eeb273583ae..46e932a2d55 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.5 2023/01/23 12:09:06 jsing Exp $ */ +/* $OpenBSD: bn_arch.h,v 1.6 2023/01/23 12:17:58 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -24,8 +24,10 @@ #define HAVE_BN_DIV_WORDS +#define HAVE_BN_MUL_ADD_WORDS #define HAVE_BN_MUL_COMBA4 #define HAVE_BN_MUL_COMBA8 +#define HAVE_BN_MUL_WORDS #define HAVE_BN_SQR_COMBA4 #define HAVE_BN_SQR_COMBA8 diff --git a/lib/libcrypto/bn/bn_asm.c b/lib/libcrypto/bn/bn_asm.c index 143c9393670..e2b584ee85c 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.22 2023/01/23 12:09:06 jsing Exp $ */ +/* $OpenBSD: bn_asm.c,v 1.23 2023/01/23 12:17:57 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -63,137 +63,6 @@ #include "bn_local.h" -#if defined(BN_LLONG) || defined(BN_UMULT_HIGH) - -BN_ULONG -bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) -{ - BN_ULONG c1 = 0; - - assert(num >= 0); - if (num <= 0) - return (c1); - -#ifndef OPENSSL_SMALL_FOOTPRINT - while (num & ~3) { - mul_add(rp[0], ap[0], w, c1); - mul_add(rp[1], ap[1], w, c1); - mul_add(rp[2], ap[2], w, c1); - mul_add(rp[3], ap[3], w, c1); - ap += 4; - rp += 4; - num -= 4; - } -#endif - while (num) { - mul_add(rp[0], ap[0], w, c1); - ap++; - rp++; - num--; - } - - return (c1); -} - -BN_ULONG -bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) -{ - BN_ULONG c1 = 0; - - assert(num >= 0); - if (num <= 0) - return (c1); - -#ifndef OPENSSL_SMALL_FOOTPRINT - while (num & ~3) { - mul(rp[0], ap[0], w, c1); - mul(rp[1], ap[1], w, c1); - mul(rp[2], ap[2], w, c1); - mul(rp[3], ap[3], w, c1); - ap += 4; - rp += 4; - num -= 4; - } -#endif - while (num) { - mul(rp[0], ap[0], w, c1); - ap++; - rp++; - num--; - } - return (c1); -} - -#else /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */ - -BN_ULONG -bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) -{ - BN_ULONG c = 0; - BN_ULONG bl, bh; - - assert(num >= 0); - if (num <= 0) - return ((BN_ULONG)0); - - bl = LBITS(w); - bh = HBITS(w); - -#ifndef OPENSSL_SMALL_FOOTPRINT - while (num & ~3) { - mul_add(rp[0], ap[0], bl, bh, c); - mul_add(rp[1], ap[1], bl, bh, c); - mul_add(rp[2], ap[2], bl, bh, c); - mul_add(rp[3], ap[3], bl, bh, c); - ap += 4; - rp += 4; - num -= 4; - } -#endif - while (num) { - mul_add(rp[0], ap[0], bl, bh, c); - ap++; - rp++; - num--; - } - return (c); -} - -BN_ULONG -bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) -{ - BN_ULONG carry = 0; - BN_ULONG bl, bh; - - assert(num >= 0); - if (num <= 0) - return ((BN_ULONG)0); - - bl = LBITS(w); - bh = HBITS(w); - -#ifndef OPENSSL_SMALL_FOOTPRINT - while (num & ~3) { - mul(rp[0], ap[0], bl, bh, carry); - mul(rp[1], ap[1], bl, bh, carry); - mul(rp[2], ap[2], bl, bh, carry); - mul(rp[3], ap[3], bl, bh, carry); - ap += 4; - rp += 4; - num -= 4; - } -#endif - while (num) { - mul(rp[0], ap[0], bl, bh, carry); - ap++; - rp++; - num--; - } - return (carry); -} - -#endif /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */ - #if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT) #ifdef OPENSSL_NO_ASM diff --git a/lib/libcrypto/bn/bn_mul.c b/lib/libcrypto/bn/bn_mul.c index 3bf8ce69866..bd679108db2 100644 --- a/lib/libcrypto/bn/bn_mul.c +++ b/lib/libcrypto/bn/bn_mul.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_mul.c,v 1.29 2023/01/21 15:40:13 jsing Exp $ */ +/* $OpenBSD: bn_mul.c,v 1.30 2023/01/23 12:17:57 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -65,6 +65,77 @@ #include "bn_arch.h" #include "bn_local.h" +#ifndef HAVE_BN_MUL_ADD_WORDS +#if defined(BN_LLONG) || defined(BN_UMULT_HIGH) + +BN_ULONG +bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) +{ + BN_ULONG c1 = 0; + + assert(num >= 0); + if (num <= 0) + return (c1); + +#ifndef OPENSSL_SMALL_FOOTPRINT + while (num & ~3) { + mul_add(rp[0], ap[0], w, c1); + mul_add(rp[1], ap[1], w, c1); + mul_add(rp[2], ap[2], w, c1); + mul_add(rp[3], ap[3], w, c1); + ap += 4; + rp += 4; + num -= 4; + } +#endif + while (num) { + mul_add(rp[0], ap[0], w, c1); + ap++; + rp++; + num--; + } + + return (c1); +} + +#else /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */ + +BN_ULONG +bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) +{ + BN_ULONG c = 0; + BN_ULONG bl, bh; + + assert(num >= 0); + if (num <= 0) + return ((BN_ULONG)0); + + bl = LBITS(w); + bh = HBITS(w); + +#ifndef OPENSSL_SMALL_FOOTPRINT + while (num & ~3) { + mul_add(rp[0], ap[0], bl, bh, c); + mul_add(rp[1], ap[1], bl, bh, c); + mul_add(rp[2], ap[2], bl, bh, c); + mul_add(rp[3], ap[3], bl, bh, c); + ap += 4; + rp += 4; + num -= 4; + } +#endif + while (num) { + mul_add(rp[0], ap[0], bl, bh, c); + ap++; + rp++; + num--; + } + return (c); +} + +#endif /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */ +#endif + #ifndef HAVE_BN_MUL_COMBA4 void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) @@ -213,6 +284,74 @@ bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) } #endif +#ifndef HAVE_BN_MUL_WORDS +#if defined(BN_LLONG) || defined(BN_UMULT_HIGH) + +BN_ULONG +bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) +{ + BN_ULONG c1 = 0; + + assert(num >= 0); + if (num <= 0) + return (c1); + +#ifndef OPENSSL_SMALL_FOOTPRINT + while (num & ~3) { + mul(rp[0], ap[0], w, c1); + mul(rp[1], ap[1], w, c1); + mul(rp[2], ap[2], w, c1); + mul(rp[3], ap[3], w, c1); + ap += 4; + rp += 4; + num -= 4; + } +#endif + while (num) { + mul(rp[0], ap[0], w, c1); + ap++; + rp++; + num--; + } + return (c1); +} +#else /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */ + +BN_ULONG +bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) +{ + BN_ULONG carry = 0; + BN_ULONG bl, bh; + + assert(num >= 0); + if (num <= 0) + return ((BN_ULONG)0); + + bl = LBITS(w); + bh = HBITS(w); + +#ifndef OPENSSL_SMALL_FOOTPRINT + while (num & ~3) { + mul(rp[0], ap[0], bl, bh, carry); + mul(rp[1], ap[1], bl, bh, carry); + mul(rp[2], ap[2], bl, bh, carry); + mul(rp[3], ap[3], bl, bh, carry); + ap += 4; + rp += 4; + num -= 4; + } +#endif + while (num) { + mul(rp[0], ap[0], bl, bh, carry); + ap++; + rp++; + num--; + } + return (carry); +} +#endif /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */ +#endif + #if defined(OPENSSL_NO_ASM) || !defined(OPENSSL_BN_ASM_PART_WORDS) /* * Here follows a specialised variant of bn_sub_words(), which has the property -- 2.20.1