summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_internal.h
diff options
context:
space:
mode:
authorjsing <>2023-02-16 04:42:20 +0000
committerjsing <>2023-02-16 04:42:20 +0000
commit3170d87c6599656e7568dca509714cf70723f0d2 (patch)
treed64538cc7fb7e35be6b722a5c08898e2a13ddebf /src/lib/libcrypto/bn/bn_internal.h
parentd5d57084c52a85f904031b46cae5e1c26448c38c (diff)
downloadopenbsd-3170d87c6599656e7568dca509714cf70723f0d2.tar.gz
openbsd-3170d87c6599656e7568dca509714cf70723f0d2.tar.bz2
openbsd-3170d87c6599656e7568dca509714cf70723f0d2.zip
Reimplement bn_add_words() and bn_sub_words() using bignum primitives.
This removes the effectively duplicate BN_LLONG version of bn_add_words() and simplifies the code considerably. ok tb@
Diffstat (limited to 'src/lib/libcrypto/bn/bn_internal.h')
-rw-r--r--src/lib/libcrypto/bn/bn_internal.h59
1 files changed, 58 insertions, 1 deletions
diff --git a/src/lib/libcrypto/bn/bn_internal.h b/src/lib/libcrypto/bn/bn_internal.h
index 12ea3641e6..1b5ab9c42c 100644
--- a/src/lib/libcrypto/bn/bn_internal.h
+++ b/src/lib/libcrypto/bn/bn_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_internal.h,v 1.4 2023/02/15 04:46:49 tb Exp $ */ 1/* $OpenBSD: bn_internal.h,v 1.5 2023/02/16 04:42:20 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -102,6 +102,63 @@ bn_addw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_r1, BN_ULONG *out_r0)
102#endif 102#endif
103#endif 103#endif
104 104
105/*
106 * bn_addw_addw() computes (r1:r0) = a + b + c, where all inputs are single
107 * words, producing a double word result.
108 */
109#ifndef HAVE_BN_ADDW_ADDW
110static inline void
111bn_addw_addw(BN_ULONG a, BN_ULONG b, BN_ULONG c, BN_ULONG *out_r1,
112 BN_ULONG *out_r0)
113{
114 BN_ULONG carry, r1, r0;
115
116 bn_addw(a, b, &r1, &r0);
117 bn_addw(r0, c, &carry, &r0);
118 r1 += carry;
119
120 *out_r1 = r1;
121 *out_r0 = r0;
122}
123#endif
124
125/*
126 * bn_subw() computes r0 = a - b, where both inputs are single words,
127 * producing a single word result and borrow.
128 */
129#ifndef HAVE_BN_SUBW
130static inline void
131bn_subw(BN_ULONG a, BN_ULONG b, BN_ULONG *out_borrow, BN_ULONG *out_r0)
132{
133 BN_ULONG borrow, r0;
134
135 r0 = a - b;
136 borrow = ((r0 | (b & ~a)) & (b | ~a)) >> (BN_BITS2 - 1);
137
138 *out_borrow = borrow;
139 *out_r0 = r0;
140}
141#endif
142
143/*
144 * bn_subw_subw() computes r0 = a - b - c, where all inputs are single words,
145 * producing a single word result and borrow.
146 */
147#ifndef HAVE_BN_SUBW_SUBW
148static inline void
149bn_subw_subw(BN_ULONG a, BN_ULONG b, BN_ULONG c, BN_ULONG *out_borrow,
150 BN_ULONG *out_r0)
151{
152 BN_ULONG b1, b2, r0;
153
154 bn_subw(a, b, &b1, &r0);
155 bn_subw(r0, c, &b2, &r0);
156
157 *out_borrow = b1 + b2;
158 *out_r0 = r0;
159}
160#endif
161
105#ifndef HAVE_BN_UMUL_HILO 162#ifndef HAVE_BN_UMUL_HILO
106#ifdef BN_LLONG 163#ifdef BN_LLONG
107static inline void 164static inline void