summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/arch
diff options
context:
space:
mode:
authorjsing <>2023-01-28 16:33:34 +0000
committerjsing <>2023-01-28 16:33:34 +0000
commit636918f6cfde69d37b71f6ff3da1a6eb6cf4ad65 (patch)
tree77285c9d112391ced3ea1c6ee831bf186ff9316b /src/lib/libcrypto/bn/arch
parent971c759a469620704a18f7c93e7d71fbae75e7c2 (diff)
downloadopenbsd-636918f6cfde69d37b71f6ff3da1a6eb6cf4ad65.tar.gz
openbsd-636918f6cfde69d37b71f6ff3da1a6eb6cf4ad65.tar.bz2
openbsd-636918f6cfde69d37b71f6ff3da1a6eb6cf4ad65.zip
Provide bn_div_rem_words() and make use of it.
Provide a function that divides a double word (h:l) by d, returning the quotient q and the remainder r, such that q * d + r is equal to the numerator. Call this from the three places that currently implement this themselves. This is implemented with some slight indirection, which allows for per architecture implementations, replacing the define/macro tangle, which messes with variables that are not passed to it. Also remove a duplicate of bn_div_words() for the BN_ULLONG && BN_DIV2W case - this is already handled. ok tb@
Diffstat (limited to 'src/lib/libcrypto/bn/arch')
-rw-r--r--src/lib/libcrypto/bn/arch/amd64/bn_arch.h27
-rw-r--r--src/lib/libcrypto/bn/arch/i386/bn_arch.h27
2 files changed, 52 insertions, 2 deletions
diff --git a/src/lib/libcrypto/bn/arch/amd64/bn_arch.h b/src/lib/libcrypto/bn/arch/amd64/bn_arch.h
index 065f6b1c3b..6b7eaf5eee 100644
--- a/src/lib/libcrypto/bn/arch/amd64/bn_arch.h
+++ b/src/lib/libcrypto/bn/arch/amd64/bn_arch.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_arch.h,v 1.7 2023/01/23 12:17:57 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.8 2023/01/28 16:33:34 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -15,6 +15,8 @@
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */ 16 */
17 17
18#include <openssl/bn.h>
19
18#ifndef HEADER_BN_ARCH_H 20#ifndef HEADER_BN_ARCH_H
19#define HEADER_BN_ARCH_H 21#define HEADER_BN_ARCH_H
20 22
@@ -36,5 +38,28 @@
36 38
37#define HAVE_BN_SUB_WORDS 39#define HAVE_BN_SUB_WORDS
38 40
41#if defined(__GNUC__)
42#define HAVE_BN_DIV_REM_WORDS_INLINE
43
44static inline void
45bn_div_rem_words_inline(BN_ULONG h, BN_ULONG l, BN_ULONG d, BN_ULONG *out_q,
46 BN_ULONG *out_r)
47{
48 BN_ULONG q, r;
49
50 /*
51 * Unsigned division of %rdx:%rax by d with quotient being stored in
52 * %rax and remainder in %rdx.
53 */
54 __asm__ volatile ("divq %4"
55 : "=a"(q), "=d"(r)
56 : "d"(h), "a"(l), "rm"(d)
57 : "cc");
58
59 *out_q = q;
60 *out_r = r;
61}
62#endif /* __GNUC__ */
63
39#endif 64#endif
40#endif 65#endif
diff --git a/src/lib/libcrypto/bn/arch/i386/bn_arch.h b/src/lib/libcrypto/bn/arch/i386/bn_arch.h
index 681c2090a7..e2b4957efc 100644
--- a/src/lib/libcrypto/bn/arch/i386/bn_arch.h
+++ b/src/lib/libcrypto/bn/arch/i386/bn_arch.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_arch.h,v 1.6 2023/01/23 12:17:57 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.7 2023/01/28 16:33:34 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -15,6 +15,8 @@
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */ 16 */
17 17
18#include <openssl/bn.h>
19
18#ifndef HEADER_BN_ARCH_H 20#ifndef HEADER_BN_ARCH_H
19#define HEADER_BN_ARCH_H 21#define HEADER_BN_ARCH_H
20 22
@@ -35,5 +37,28 @@
35 37
36#define HAVE_BN_SUB_WORDS 38#define HAVE_BN_SUB_WORDS
37 39
40#if defined(__GNUC__)
41#define HAVE_BN_DIV_REM_WORDS_INLINE
42
43static inline void
44bn_div_rem_words_inline(BN_ULONG h, BN_ULONG l, BN_ULONG d, BN_ULONG *out_q,
45 BN_ULONG *out_r)
46{
47 BN_ULONG q, r;
48
49 /*
50 * Unsigned division of %edx:%eax by d with quotient being stored in
51 * %eax and remainder in %edx.
52 */
53 __asm__ volatile ("divl %4"
54 : "=a"(q), "=d"(r)
55 : "a"(l), "d"(h), "rm"(d)
56 : "cc");
57
58 *out_q = q;
59 *out_r = r;
60}
61#endif /* __GNUC__ */
62
38#endif 63#endif
39#endif 64#endif