summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bn')
-rw-r--r--src/lib/libcrypto/bn/arch/amd64/bn_arch.c6
-rw-r--r--src/lib/libcrypto/bn/asm/bn-586.pl3
-rwxr-xr-xsrc/lib/libcrypto/bn/asm/x86-mont.pl3
-rw-r--r--src/lib/libcrypto/bn/bn_add.c76
-rw-r--r--src/lib/libcrypto/bn/bn_add_sub.c178
-rw-r--r--src/lib/libcrypto/bn/bn_convert.c4
-rw-r--r--src/lib/libcrypto/bn/bn_ctx.c4
-rw-r--r--src/lib/libcrypto/bn/bn_div.c4
-rw-r--r--src/lib/libcrypto/bn/bn_exp.c5
-rw-r--r--src/lib/libcrypto/bn/bn_gcd.c11
-rw-r--r--src/lib/libcrypto/bn/bn_internal.h26
-rw-r--r--src/lib/libcrypto/bn/bn_isqrt.c4
-rw-r--r--src/lib/libcrypto/bn/bn_lib.c5
-rw-r--r--src/lib/libcrypto/bn/bn_local.h12
-rw-r--r--src/lib/libcrypto/bn/bn_mod.c5
-rw-r--r--src/lib/libcrypto/bn/bn_mod_sqrt.c5
-rw-r--r--src/lib/libcrypto/bn/bn_mod_words.c114
-rw-r--r--src/lib/libcrypto/bn/bn_mont.c78
-rw-r--r--src/lib/libcrypto/bn/bn_mul.c71
-rw-r--r--src/lib/libcrypto/bn/bn_prime.c5
-rw-r--r--src/lib/libcrypto/bn/bn_rand.c5
-rw-r--r--src/lib/libcrypto/bn/bn_recp.c5
-rw-r--r--src/lib/libcrypto/bn/bn_shift.c4
-rw-r--r--src/lib/libcrypto/bn/bn_sqr.c47
24 files changed, 519 insertions, 161 deletions
diff --git a/src/lib/libcrypto/bn/arch/amd64/bn_arch.c b/src/lib/libcrypto/bn/arch/amd64/bn_arch.c
index a377a05681..8eb3670def 100644
--- a/src/lib/libcrypto/bn/arch/amd64/bn_arch.c
+++ b/src/lib/libcrypto/bn/arch/amd64/bn_arch.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_arch.c,v 1.7 2023/06/24 16:01:44 jsing Exp $ */ 1/* $OpenBSD: bn_arch.c,v 1.8 2025/08/05 15:01:13 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -78,7 +78,7 @@ bn_mul_words(BN_ULONG *rd, const BN_ULONG *ad, int num, BN_ULONG w)
78 78
79#ifdef HAVE_BN_MUL_COMBA4 79#ifdef HAVE_BN_MUL_COMBA4
80void 80void
81bn_mul_comba4(BN_ULONG *rd, BN_ULONG *ad, BN_ULONG *bd) 81bn_mul_comba4(BN_ULONG *rd, const BN_ULONG *ad, const BN_ULONG *bd)
82{ 82{
83 /* XXX - consider using non-alt on CPUs that have the ADX extension. */ 83 /* XXX - consider using non-alt on CPUs that have the ADX extension. */
84 bignum_mul_4_8_alt((uint64_t *)rd, (uint64_t *)ad, (uint64_t *)bd); 84 bignum_mul_4_8_alt((uint64_t *)rd, (uint64_t *)ad, (uint64_t *)bd);
@@ -87,7 +87,7 @@ bn_mul_comba4(BN_ULONG *rd, BN_ULONG *ad, BN_ULONG *bd)
87 87
88#ifdef HAVE_BN_MUL_COMBA8 88#ifdef HAVE_BN_MUL_COMBA8
89void 89void
90bn_mul_comba8(BN_ULONG *rd, BN_ULONG *ad, BN_ULONG *bd) 90bn_mul_comba8(BN_ULONG *rd, const BN_ULONG *ad, const BN_ULONG *bd)
91{ 91{
92 /* XXX - consider using non-alt on CPUs that have the ADX extension. */ 92 /* XXX - consider using non-alt on CPUs that have the ADX extension. */
93 bignum_mul_8_16_alt((uint64_t *)rd, (uint64_t *)ad, (uint64_t *)bd); 93 bignum_mul_8_16_alt((uint64_t *)rd, (uint64_t *)ad, (uint64_t *)bd);
diff --git a/src/lib/libcrypto/bn/asm/bn-586.pl b/src/lib/libcrypto/bn/asm/bn-586.pl
index 71b775af8d..19a1afdbbe 100644
--- a/src/lib/libcrypto/bn/asm/bn-586.pl
+++ b/src/lib/libcrypto/bn/asm/bn-586.pl
@@ -6,8 +6,7 @@ require "x86asm.pl";
6 6
7&asm_init($ARGV[0],$0); 7&asm_init($ARGV[0],$0);
8 8
9$sse2=0; 9$sse2=1;
10for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
11 10
12&external_label("OPENSSL_ia32cap_P") if ($sse2); 11&external_label("OPENSSL_ia32cap_P") if ($sse2);
13 12
diff --git a/src/lib/libcrypto/bn/asm/x86-mont.pl b/src/lib/libcrypto/bn/asm/x86-mont.pl
index 6524651748..3be440f11f 100755
--- a/src/lib/libcrypto/bn/asm/x86-mont.pl
+++ b/src/lib/libcrypto/bn/asm/x86-mont.pl
@@ -32,8 +32,7 @@ require "x86asm.pl";
32 32
33&asm_init($ARGV[0],$0); 33&asm_init($ARGV[0],$0);
34 34
35$sse2=0; 35$sse2=1;
36for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
37 36
38&external_label("OPENSSL_ia32cap_P") if ($sse2); 37&external_label("OPENSSL_ia32cap_P") if ($sse2);
39 38
diff --git a/src/lib/libcrypto/bn/bn_add.c b/src/lib/libcrypto/bn/bn_add.c
index 86768a312a..81fa60e429 100644
--- a/src/lib/libcrypto/bn/bn_add.c
+++ b/src/lib/libcrypto/bn/bn_add.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_add.c,v 1.26 2023/07/08 12:21:58 beck Exp $ */ 1/* $OpenBSD: bn_add.c,v 1.29 2025/05/25 04:53:05 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -60,44 +60,10 @@
60#include <limits.h> 60#include <limits.h>
61#include <stdio.h> 61#include <stdio.h>
62 62
63#include <openssl/err.h>
64
65#include "bn_arch.h" 63#include "bn_arch.h"
66#include "bn_local.h" 64#include "bn_local.h"
67#include "bn_internal.h" 65#include "bn_internal.h"
68 66#include "err_local.h"
69/*
70 * bn_add_words() computes (carry:r[i]) = a[i] + b[i] + carry, where a and b
71 * are both arrays of words. Any carry resulting from the addition is returned.
72 */
73#ifndef HAVE_BN_ADD_WORDS
74BN_ULONG
75bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
76{
77 BN_ULONG carry = 0;
78
79 assert(n >= 0);
80 if (n <= 0)
81 return 0;
82
83 while (n & ~3) {
84 bn_qwaddqw(a[3], a[2], a[1], a[0], b[3], b[2], b[1], b[0],
85 carry, &carry, &r[3], &r[2], &r[1], &r[0]);
86 a += 4;
87 b += 4;
88 r += 4;
89 n -= 4;
90 }
91 while (n) {
92 bn_addw_addw(a[0], b[0], carry, &carry, &r[0]);
93 a++;
94 b++;
95 r++;
96 n--;
97 }
98 return carry;
99}
100#endif
101 67
102/* 68/*
103 * bn_add() computes (carry:r[i]) = a[i] + b[i] + carry, where a and b are both 69 * bn_add() computes (carry:r[i]) = a[i] + b[i] + carry, where a and b are both
@@ -147,40 +113,6 @@ bn_add(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len, const BN_ULONG *b,
147#endif 113#endif
148 114
149/* 115/*
150 * bn_sub_words() computes (borrow:r[i]) = a[i] - b[i] - borrow, where a and b
151 * are both arrays of words. Any borrow resulting from the subtraction is
152 * returned.
153 */
154#ifndef HAVE_BN_SUB_WORDS
155BN_ULONG
156bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
157{
158 BN_ULONG borrow = 0;
159
160 assert(n >= 0);
161 if (n <= 0)
162 return 0;
163
164 while (n & ~3) {
165 bn_qwsubqw(a[3], a[2], a[1], a[0], b[3], b[2], b[1], b[0],
166 borrow, &borrow, &r[3], &r[2], &r[1], &r[0]);
167 a += 4;
168 b += 4;
169 r += 4;
170 n -= 4;
171 }
172 while (n) {
173 bn_subw_subw(a[0], b[0], borrow, &borrow, &r[0]);
174 a++;
175 b++;
176 r++;
177 n--;
178 }
179 return borrow;
180}
181#endif
182
183/*
184 * bn_sub() computes (borrow:r[i]) = a[i] - b[i] - borrow, where a and b are both 116 * bn_sub() computes (borrow:r[i]) = a[i] - b[i] - borrow, where a and b are both
185 * arrays of words (r may be the same as a or b). The length of a and b may 117 * arrays of words (r may be the same as a or b). The length of a and b may
186 * differ, while r must be at least max(a_len, b_len) in length. Any borrow 118 * differ, while r must be at least max(a_len, b_len) in length. Any borrow
@@ -208,7 +140,7 @@ bn_sub(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len, const BN_ULONG *b,
208 /* XXX - consider doing four at a time to match bn_sub_words. */ 140 /* XXX - consider doing four at a time to match bn_sub_words. */
209 while (diff_len < 0) { 141 while (diff_len < 0) {
210 /* Compute r[0] = 0 - b[0] - borrow. */ 142 /* Compute r[0] = 0 - b[0] - borrow. */
211 bn_subw(0 - b[0], borrow, &borrow, &r[0]); 143 bn_subw_subw(0, b[0], borrow, &borrow, &r[0]);
212 diff_len++; 144 diff_len++;
213 b++; 145 b++;
214 r++; 146 r++;
@@ -217,7 +149,7 @@ bn_sub(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len, const BN_ULONG *b,
217 /* XXX - consider doing four at a time to match bn_sub_words. */ 149 /* XXX - consider doing four at a time to match bn_sub_words. */
218 while (diff_len > 0) { 150 while (diff_len > 0) {
219 /* Compute r[0] = a[0] - 0 - borrow. */ 151 /* Compute r[0] = a[0] - 0 - borrow. */
220 bn_subw(a[0], borrow, &borrow, &r[0]); 152 bn_subw_subw(a[0], 0, borrow, &borrow, &r[0]);
221 diff_len--; 153 diff_len--;
222 a++; 154 a++;
223 r++; 155 r++;
diff --git a/src/lib/libcrypto/bn/bn_add_sub.c b/src/lib/libcrypto/bn/bn_add_sub.c
new file mode 100644
index 0000000000..5c9d5a2b1a
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_add_sub.c
@@ -0,0 +1,178 @@
1/* $OpenBSD: bn_add_sub.c,v 1.1 2025/05/25 04:30:55 jsing Exp $ */
2/*
3 * Copyright (c) 2023,2024,2025 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <openssl/bn.h>
19
20#include "bn_internal.h"
21
22/*
23 * bn_add_words() computes (carry:r[i]) = a[i] + b[i] + carry, where a and b
24 * are both arrays of words. Any carry resulting from the addition is returned.
25 */
26#ifndef HAVE_BN_ADD_WORDS
27BN_ULONG
28bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
29{
30 BN_ULONG carry = 0;
31
32 while (n >= 4) {
33 bn_qwaddqw(a[3], a[2], a[1], a[0], b[3], b[2], b[1], b[0],
34 carry, &carry, &r[3], &r[2], &r[1], &r[0]);
35 a += 4;
36 b += 4;
37 r += 4;
38 n -= 4;
39 }
40 while (n > 0) {
41 bn_addw_addw(a[0], b[0], carry, &carry, &r[0]);
42 a++;
43 b++;
44 r++;
45 n--;
46 }
47
48 return carry;
49}
50#endif
51
52/*
53 * bn_sub_words() computes (borrow:r[i]) = a[i] - b[i] - borrow, where a and b
54 * are both arrays of words. Any borrow resulting from the subtraction is
55 * returned.
56 */
57#ifndef HAVE_BN_SUB_WORDS
58BN_ULONG
59bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
60{
61 BN_ULONG borrow = 0;
62
63 while (n >= 4) {
64 bn_qwsubqw(a[3], a[2], a[1], a[0], b[3], b[2], b[1], b[0],
65 borrow, &borrow, &r[3], &r[2], &r[1], &r[0]);
66 a += 4;
67 b += 4;
68 r += 4;
69 n -= 4;
70 }
71 while (n > 0) {
72 bn_subw_subw(a[0], b[0], borrow, &borrow, &r[0]);
73 a++;
74 b++;
75 r++;
76 n--;
77 }
78
79 return borrow;
80}
81#endif
82
83/*
84 * bn_sub_borrow() computes a[i] - b[i], returning the resulting borrow only.
85 */
86#ifndef HAVE_BN_SUB_WORDS_BORROW
87BN_ULONG
88bn_sub_words_borrow(const BN_ULONG *a, const BN_ULONG *b, size_t n)
89{
90 BN_ULONG borrow = 0;
91 BN_ULONG r;
92
93 while (n >= 4) {
94 bn_qwsubqw(a[3], a[2], a[1], a[0], b[3], b[2], b[1], b[0],
95 borrow, &borrow, &r, &r, &r, &r);
96 a += 4;
97 b += 4;
98 n -= 4;
99 }
100 while (n > 0) {
101 bn_subw_subw(a[0], b[0], borrow, &borrow, &r);
102 a++;
103 b++;
104 n--;
105 }
106
107 return borrow;
108}
109#endif
110
111/*
112 * bn_add_words_masked() computes r[] = a[] + (b[] & mask), where a, b and r are
113 * arrays of words with length n (r may be the same as a or b).
114 */
115#ifndef HAVE_BN_ADD_WORDS_MASKED
116BN_ULONG
117bn_add_words_masked(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
118 BN_ULONG mask, size_t n)
119{
120 BN_ULONG carry = 0;
121
122 /* XXX - consider conditional/masked versions of bn_addw_addw/bn_qwaddqw. */
123
124 while (n >= 4) {
125 bn_qwaddqw(a[3], a[2], a[1], a[0], b[3] & mask, b[2] & mask,
126 b[1] & mask, b[0] & mask, carry, &carry, &r[3], &r[2],
127 &r[1], &r[0]);
128 a += 4;
129 b += 4;
130 r += 4;
131 n -= 4;
132 }
133 while (n > 0) {
134 bn_addw_addw(a[0], b[0] & mask, carry, &carry, &r[0]);
135 a++;
136 b++;
137 r++;
138 n--;
139 }
140
141 return carry;
142}
143#endif
144
145/*
146 * bn_sub_words_masked() computes r[] = a[] - (b[] & mask), where a, b and r are
147 * arrays of words with length n (r may be the same as a or b).
148 */
149#ifndef HAVE_BN_SUB_WORDS_MASKED
150BN_ULONG
151bn_sub_words_masked(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
152 BN_ULONG mask, size_t n)
153{
154 BN_ULONG borrow = 0;
155
156 /* XXX - consider conditional/masked versions of bn_subw_subw/bn_qwsubqw. */
157
158 /* Compute conditional r[i] = a[i] - b[i]. */
159 while (n >= 4) {
160 bn_qwsubqw(a[3], a[2], a[1], a[0], b[3] & mask, b[2] & mask,
161 b[1] & mask, b[0] & mask, borrow, &borrow, &r[3], &r[2],
162 &r[1], &r[0]);
163 a += 4;
164 b += 4;
165 r += 4;
166 n -= 4;
167 }
168 while (n > 0) {
169 bn_subw_subw(a[0], b[0] & mask, borrow, &borrow, &r[0]);
170 a++;
171 b++;
172 r++;
173 n--;
174 }
175
176 return borrow;
177}
178#endif
diff --git a/src/lib/libcrypto/bn/bn_convert.c b/src/lib/libcrypto/bn/bn_convert.c
index 6a6354f44e..ca5c7d7865 100644
--- a/src/lib/libcrypto/bn/bn_convert.c
+++ b/src/lib/libcrypto/bn/bn_convert.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_convert.c,v 1.23 2024/11/08 14:18:44 jsing Exp $ */ 1/* $OpenBSD: bn_convert.c,v 1.24 2025/05/10 05:54:38 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -65,11 +65,11 @@
65 65
66#include <openssl/bio.h> 66#include <openssl/bio.h>
67#include <openssl/buffer.h> 67#include <openssl/buffer.h>
68#include <openssl/err.h>
69 68
70#include "bn_local.h" 69#include "bn_local.h"
71#include "bytestring.h" 70#include "bytestring.h"
72#include "crypto_internal.h" 71#include "crypto_internal.h"
72#include "err_local.h"
73 73
74static int bn_dec2bn_cbs(BIGNUM **bnp, CBS *cbs); 74static int bn_dec2bn_cbs(BIGNUM **bnp, CBS *cbs);
75static int bn_hex2bn_cbs(BIGNUM **bnp, CBS *cbs); 75static int bn_hex2bn_cbs(BIGNUM **bnp, CBS *cbs);
diff --git a/src/lib/libcrypto/bn/bn_ctx.c b/src/lib/libcrypto/bn/bn_ctx.c
index 129b9c9781..eda93dcaa4 100644
--- a/src/lib/libcrypto/bn/bn_ctx.c
+++ b/src/lib/libcrypto/bn/bn_ctx.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_ctx.c,v 1.22 2023/07/08 12:21:58 beck Exp $ */ 1/* $OpenBSD: bn_ctx.c,v 1.23 2025/05/10 05:54:38 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -19,9 +19,9 @@
19#include <string.h> 19#include <string.h>
20 20
21#include <openssl/opensslconf.h> 21#include <openssl/opensslconf.h>
22#include <openssl/err.h>
23 22
24#include "bn_local.h" 23#include "bn_local.h"
24#include "err_local.h"
25 25
26#define BN_CTX_INITIAL_LEN 8 26#define BN_CTX_INITIAL_LEN 8
27 27
diff --git a/src/lib/libcrypto/bn/bn_div.c b/src/lib/libcrypto/bn/bn_div.c
index 09a8a364df..1026b43add 100644
--- a/src/lib/libcrypto/bn/bn_div.c
+++ b/src/lib/libcrypto/bn/bn_div.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_div.c,v 1.41 2024/04/10 14:58:06 beck Exp $ */ 1/* $OpenBSD: bn_div.c,v 1.42 2025/05/10 05:54:38 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -62,11 +62,11 @@
62#include <openssl/opensslconf.h> 62#include <openssl/opensslconf.h>
63 63
64#include <openssl/bn.h> 64#include <openssl/bn.h>
65#include <openssl/err.h>
66 65
67#include "bn_arch.h" 66#include "bn_arch.h"
68#include "bn_local.h" 67#include "bn_local.h"
69#include "bn_internal.h" 68#include "bn_internal.h"
69#include "err_local.h"
70 70
71BN_ULONG bn_div_3_words(const BN_ULONG *m, BN_ULONG d1, BN_ULONG d0); 71BN_ULONG bn_div_3_words(const BN_ULONG *m, BN_ULONG d1, BN_ULONG d0);
72 72
diff --git a/src/lib/libcrypto/bn/bn_exp.c b/src/lib/libcrypto/bn/bn_exp.c
index e925d325d2..6a5c1c857a 100644
--- a/src/lib/libcrypto/bn/bn_exp.c
+++ b/src/lib/libcrypto/bn/bn_exp.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_exp.c,v 1.58 2025/02/13 11:15:09 tb Exp $ */ 1/* $OpenBSD: bn_exp.c,v 1.59 2025/05/10 05:54:38 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -112,10 +112,9 @@
112#include <stdlib.h> 112#include <stdlib.h>
113#include <string.h> 113#include <string.h>
114 114
115#include <openssl/err.h>
116
117#include "bn_local.h" 115#include "bn_local.h"
118#include "constant_time.h" 116#include "constant_time.h"
117#include "err_local.h"
119 118
120/* maximum precomputation table size for *variable* sliding windows */ 119/* maximum precomputation table size for *variable* sliding windows */
121#define TABLE_SIZE 32 120#define TABLE_SIZE 32
diff --git a/src/lib/libcrypto/bn/bn_gcd.c b/src/lib/libcrypto/bn/bn_gcd.c
index fa5d71a7f3..319d9ca390 100644
--- a/src/lib/libcrypto/bn/bn_gcd.c
+++ b/src/lib/libcrypto/bn/bn_gcd.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_gcd.c,v 1.29 2024/04/10 14:58:06 beck Exp $ */ 1/* $OpenBSD: bn_gcd.c,v 1.31 2025/06/02 12:40:10 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -109,9 +109,8 @@
109 * 109 *
110 */ 110 */
111 111
112#include <openssl/err.h>
113
114#include "bn_local.h" 112#include "bn_local.h"
113#include "err_local.h"
115 114
116static BIGNUM * 115static BIGNUM *
117euclid(BIGNUM *a, BIGNUM *b) 116euclid(BIGNUM *a, BIGNUM *b)
@@ -681,8 +680,10 @@ BN_mod_inverse_internal(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ct
681 /* A >= 2*B, so D=2 or D=3 */ 680 /* A >= 2*B, so D=2 or D=3 */
682 if (!BN_sub(M, A, T)) 681 if (!BN_sub(M, A, T))
683 goto err; 682 goto err;
684 if (!BN_add(D,T,B)) goto err; /* use D (:= 3*B) as temp */ 683 /* use D (:= 3*B) as temp */
685 if (BN_ucmp(A, D) < 0) { 684 if (!BN_add(D, T, B))
685 goto err;
686 if (BN_ucmp(A, D) < 0) {
686 /* A < 3*B, so D=2 */ 687 /* A < 3*B, so D=2 */
687 if (!BN_set_word(D, 2)) 688 if (!BN_set_word(D, 2))
688 goto err; 689 goto err;
diff --git a/src/lib/libcrypto/bn/bn_internal.h b/src/lib/libcrypto/bn/bn_internal.h
index fd04bc9f8a..8b5145e225 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.15 2023/06/25 11:42:26 jsing Exp $ */ 1/* $OpenBSD: bn_internal.h,v 1.20 2025/08/02 16:20:00 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -26,6 +26,30 @@ int bn_word_clz(BN_ULONG w);
26 26
27int bn_bitsize(const BIGNUM *bn); 27int bn_bitsize(const BIGNUM *bn);
28 28
29BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
30 int num);
31BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
32 int num);
33BN_ULONG bn_sub_words_borrow(const BN_ULONG *a, const BN_ULONG *b, size_t n);
34BN_ULONG bn_add_words_masked(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
35 BN_ULONG mask, size_t n);
36BN_ULONG bn_sub_words_masked(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
37 BN_ULONG mask, size_t n);
38void bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
39 const BN_ULONG *m, size_t n);
40void bn_mod_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
41 const BN_ULONG *m, size_t n);
42void bn_mod_mul_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
43 const BN_ULONG *m, BN_ULONG *t, BN_ULONG m0, size_t n);
44void bn_mod_sqr_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *m,
45 BN_ULONG *t, BN_ULONG m0, size_t n);
46
47void bn_montgomery_multiply_words(BN_ULONG *rp, const BN_ULONG *ap,
48 const BN_ULONG *bp, const BN_ULONG *np, BN_ULONG *tp, BN_ULONG n0,
49 int n_len);
50void bn_montgomery_reduce_words(BN_ULONG *r, BN_ULONG *a, const BN_ULONG *n,
51 BN_ULONG n0, int n_len);
52
29#ifndef HAVE_BN_CT_NE_ZERO 53#ifndef HAVE_BN_CT_NE_ZERO
30static inline int 54static inline int
31bn_ct_ne_zero(BN_ULONG w) 55bn_ct_ne_zero(BN_ULONG w)
diff --git a/src/lib/libcrypto/bn/bn_isqrt.c b/src/lib/libcrypto/bn/bn_isqrt.c
index 018d5f34bd..b725519e1a 100644
--- a/src/lib/libcrypto/bn/bn_isqrt.c
+++ b/src/lib/libcrypto/bn/bn_isqrt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_isqrt.c,v 1.10 2023/06/04 17:28:35 tb Exp $ */ 1/* $OpenBSD: bn_isqrt.c,v 1.11 2025/05/10 05:54:38 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2022 Theo Buehler <tb@openbsd.org> 3 * Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
4 * 4 *
@@ -19,10 +19,10 @@
19#include <stdint.h> 19#include <stdint.h>
20 20
21#include <openssl/bn.h> 21#include <openssl/bn.h>
22#include <openssl/err.h>
23 22
24#include "bn_local.h" 23#include "bn_local.h"
25#include "crypto_internal.h" 24#include "crypto_internal.h"
25#include "err_local.h"
26 26
27/* 27/*
28 * Calculate integer square root of |n| using a variant of Newton's method. 28 * Calculate integer square root of |n| using a variant of Newton's method.
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c
index 72b988650c..3e451a6191 100644
--- a/src/lib/libcrypto/bn/bn_lib.c
+++ b/src/lib/libcrypto/bn/bn_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_lib.c,v 1.93 2024/04/16 13:07:14 jsing Exp $ */ 1/* $OpenBSD: bn_lib.c,v 1.94 2025/05/10 05:54:38 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -63,10 +63,9 @@
63 63
64#include <openssl/opensslconf.h> 64#include <openssl/opensslconf.h>
65 65
66#include <openssl/err.h>
67
68#include "bn_local.h" 66#include "bn_local.h"
69#include "bn_internal.h" 67#include "bn_internal.h"
68#include "err_local.h"
70 69
71BIGNUM * 70BIGNUM *
72BN_new(void) 71BN_new(void)
diff --git a/src/lib/libcrypto/bn/bn_local.h b/src/lib/libcrypto/bn/bn_local.h
index 067ffab3d9..1bd4c16baf 100644
--- a/src/lib/libcrypto/bn/bn_local.h
+++ b/src/lib/libcrypto/bn/bn_local.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_local.h,v 1.50 2025/02/13 11:04:20 tb Exp $ */ 1/* $OpenBSD: bn_local.h,v 1.54 2025/08/05 15:08:13 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -240,10 +240,12 @@ BN_ULONG bn_sub(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len,
240 const BN_ULONG *b, int b_len); 240 const BN_ULONG *b, int b_len);
241 241
242void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb); 242void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb);
243void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b); 243void bn_mul_comba4(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b);
244void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b); 244void bn_mul_comba6(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b);
245void bn_mul_comba8(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b);
245 246
246void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a); 247void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a);
248void bn_sqr_comba6(BN_ULONG *r, const BN_ULONG *a);
247void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a); 249void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a);
248 250
249int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 251int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
@@ -254,10 +256,6 @@ int bn_expand_bits(BIGNUM *a, size_t bits);
254int bn_expand_bytes(BIGNUM *a, size_t bytes); 256int bn_expand_bytes(BIGNUM *a, size_t bytes);
255int bn_wexpand(BIGNUM *a, int words); 257int bn_wexpand(BIGNUM *a, int words);
256 258
257BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
258 int num);
259BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
260 int num);
261BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); 259BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
262BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); 260BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
263void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num); 261void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
diff --git a/src/lib/libcrypto/bn/bn_mod.c b/src/lib/libcrypto/bn/bn_mod.c
index 365f6fcf03..7198c02e3b 100644
--- a/src/lib/libcrypto/bn/bn_mod.c
+++ b/src/lib/libcrypto/bn/bn_mod.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mod.c,v 1.22 2023/07/08 12:21:58 beck Exp $ */ 1/* $OpenBSD: bn_mod.c,v 1.23 2025/05/10 05:54:38 tb Exp $ */
2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> 2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * for the OpenSSL project. */ 3 * for the OpenSSL project. */
4/* ==================================================================== 4/* ====================================================================
@@ -111,9 +111,8 @@
111 * [including the GNU Public Licence.] 111 * [including the GNU Public Licence.]
112 */ 112 */
113 113
114#include <openssl/err.h>
115
116#include "bn_local.h" 114#include "bn_local.h"
115#include "err_local.h"
117 116
118int 117int
119BN_mod_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) 118BN_mod_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
diff --git a/src/lib/libcrypto/bn/bn_mod_sqrt.c b/src/lib/libcrypto/bn/bn_mod_sqrt.c
index 280002cc48..fc55f84317 100644
--- a/src/lib/libcrypto/bn/bn_mod_sqrt.c
+++ b/src/lib/libcrypto/bn/bn_mod_sqrt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mod_sqrt.c,v 1.3 2023/08/03 18:53:55 tb Exp $ */ 1/* $OpenBSD: bn_mod_sqrt.c,v 1.4 2025/05/10 05:54:38 tb Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2022 Theo Buehler <tb@openbsd.org> 4 * Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
@@ -16,9 +16,8 @@
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19#include <openssl/err.h>
20
21#include "bn_local.h" 19#include "bn_local.h"
20#include "err_local.h"
22 21
23/* 22/*
24 * Tonelli-Shanks according to H. Cohen "A Course in Computational Algebraic 23 * Tonelli-Shanks according to H. Cohen "A Course in Computational Algebraic
diff --git a/src/lib/libcrypto/bn/bn_mod_words.c b/src/lib/libcrypto/bn/bn_mod_words.c
new file mode 100644
index 0000000000..d9aee8701a
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_mod_words.c
@@ -0,0 +1,114 @@
1/* $OpenBSD: bn_mod_words.c,v 1.3 2025/08/05 15:15:54 jsing Exp $ */
2/*
3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "bn_local.h"
19#include "bn_internal.h"
20
21/*
22 * bn_mod_add_words() computes r[] = (a[] + b[]) mod m[], where a, b, r and
23 * m are arrays of words with length n (r may be the same as a or b).
24 */
25#ifndef HAVE_BN_MOD_ADD_WORDS
26void
27bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
28 const BN_ULONG *m, size_t n)
29{
30 BN_ULONG carry, mask;
31
32 /*
33 * Compute a + b, then compute r - m to determine if r >= m, considering
34 * any carry that resulted from the addition. Finally complete a
35 * conditional subtraction of r - m.
36 */
37 /* XXX - change bn_add_words to use size_t. */
38 carry = bn_add_words(r, a, b, n);
39 mask = ~(carry - bn_sub_words_borrow(r, m, n));
40 bn_sub_words_masked(r, r, m, mask, n);
41}
42#endif
43
44/*
45 * bn_mod_sub_words() computes r[] = (a[] - b[]) mod m[], where a, b, r and
46 * m are arrays of words with length n (r may be the same as a or b).
47 */
48#ifndef HAVE_BN_MOD_SUB_WORDS
49void
50bn_mod_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
51 const BN_ULONG *m, size_t n)
52{
53 BN_ULONG borrow, mask;
54
55 /*
56 * Compute a - b, then complete a conditional addition of r + m
57 * based on the resulting borrow.
58 */
59 /* XXX - change bn_sub_words to use size_t. */
60 borrow = bn_sub_words(r, a, b, n);
61 mask = (0 - borrow);
62 bn_add_words_masked(r, r, m, mask, n);
63}
64#endif
65
66/*
67 * bn_mod_mul_words() computes r[] = (a[] * b[]) mod m[], where a, b, r and
68 * m are arrays of words with length n (r may be the same as a or b) in the
69 * Montgomery domain. The result remains in the Montgomery domain.
70 */
71#ifndef HAVE_BN_MOD_MUL_WORDS
72void
73bn_mod_mul_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
74 const BN_ULONG *m, BN_ULONG *t, BN_ULONG m0, size_t n)
75{
76 if (n == 4) {
77 bn_mul_comba4(t, a, b);
78 bn_montgomery_reduce_words(r, t, m, m0, n);
79 } else if (n == 6) {
80 bn_mul_comba6(t, a, b);
81 bn_montgomery_reduce_words(r, t, m, m0, n);
82 } else if (n == 8) {
83 bn_mul_comba8(t, a, b);
84 bn_montgomery_reduce_words(r, t, m, m0, n);
85 } else {
86 bn_montgomery_multiply_words(r, a, b, m, t, m0, n);
87 }
88}
89#endif
90
91/*
92 * bn_mod_sqr_words() computes r[] = (a[] * a[]) mod m[], where a, r and
93 * m are arrays of words with length n (r may be the same as a) in the
94 * Montgomery domain. The result remains in the Montgomery domain.
95 */
96#ifndef HAVE_BN_MOD_SQR_WORDS
97void
98bn_mod_sqr_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *m,
99 BN_ULONG *t, BN_ULONG m0, size_t n)
100{
101 if (n == 4) {
102 bn_sqr_comba4(t, a);
103 bn_montgomery_reduce_words(r, t, m, m0, n);
104 } else if (n == 6) {
105 bn_sqr_comba6(t, a);
106 bn_montgomery_reduce_words(r, t, m, m0, n);
107 } else if (n == 8) {
108 bn_sqr_comba8(t, a);
109 bn_montgomery_reduce_words(r, t, m, m0, n);
110 } else {
111 bn_montgomery_multiply_words(r, a, a, m, t, m0, n);
112 }
113}
114#endif
diff --git a/src/lib/libcrypto/bn/bn_mont.c b/src/lib/libcrypto/bn/bn_mont.c
index edd7bcd0c8..8280a8db27 100644
--- a/src/lib/libcrypto/bn/bn_mont.c
+++ b/src/lib/libcrypto/bn/bn_mont.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mont.c,v 1.66 2025/03/09 15:22:40 tb Exp $ */ 1/* $OpenBSD: bn_mont.c,v 1.69 2025/08/03 10:33:46 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -116,6 +116,7 @@
116 * sections 3.8 and 4.2 in http://security.ece.orst.edu/koc/papers/r01rsasw.pdf 116 * sections 3.8 and 4.2 in http://security.ece.orst.edu/koc/papers/r01rsasw.pdf
117 */ 117 */
118 118
119#include <limits.h>
119#include <stdio.h> 120#include <stdio.h>
120#include <stdint.h> 121#include <stdint.h>
121#include <string.h> 122#include <string.h>
@@ -214,7 +215,7 @@ BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
214 goto err; 215 goto err;
215 mont->N.neg = 0; 216 mont->N.neg = 0;
216 mont->ri = ((BN_num_bits(mod) + BN_BITS2 - 1) / BN_BITS2) * BN_BITS2; 217 mont->ri = ((BN_num_bits(mod) + BN_BITS2 - 1) / BN_BITS2) * BN_BITS2;
217 if (mont->ri * 2 < mont->ri) 218 if (mont->ri > INT_MAX / 2)
218 goto err; 219 goto err;
219 220
220 /* 221 /*
@@ -316,6 +317,44 @@ BN_MONT_CTX_set_locked(BN_MONT_CTX **pmctx, int lock, const BIGNUM *mod,
316LCRYPTO_ALIAS(BN_MONT_CTX_set_locked); 317LCRYPTO_ALIAS(BN_MONT_CTX_set_locked);
317 318
318/* 319/*
320 * bn_montgomery_reduce_words() performs Montgomery reduction, reducing the input
321 * from its Montgomery form aR to a, returning the result in r. a must be twice
322 * the length of the modulus. Note that the input is mutated in the process of
323 * performing the reduction.
324 */
325void
326bn_montgomery_reduce_words(BN_ULONG *r, BN_ULONG *a, const BN_ULONG *n,
327 BN_ULONG n0, int n_len)
328{
329 BN_ULONG v, mask;
330 BN_ULONG carry = 0;
331 int i;
332
333 /* Add multiples of the modulus, so that it becomes divisible by R. */
334 for (i = 0; i < n_len; i++) {
335 v = bn_mul_add_words(&a[i], n, n_len, a[i] * n0);
336 bn_addw_addw(v, a[i + n_len], carry, &carry, &a[i + n_len]);
337 }
338
339 /* Divide by R (this is the equivalent of right shifting by n_len). */
340 a = &a[n_len];
341
342 /*
343 * The output is now in the range of [0, 2N). Attempt to reduce once by
344 * subtracting the modulus. If the reduction was necessary then the
345 * result is already in r, otherwise copy the value prior to reduction
346 * from the top half of a.
347 */
348 mask = carry - bn_sub_words(r, a, n, n_len);
349
350 for (i = 0; i < n_len; i++) {
351 *r = (*r & ~mask) | (*a & mask);
352 r++;
353 a++;
354 }
355}
356
357/*
319 * bn_montgomery_reduce() performs Montgomery reduction, reducing the input 358 * bn_montgomery_reduce() performs Montgomery reduction, reducing the input
320 * from its Montgomery form aR to a, returning the result in r. Note that the 359 * from its Montgomery form aR to a, returning the result in r. Note that the
321 * input is mutated in the process of performing the reduction, destroying its 360 * input is mutated in the process of performing the reduction, destroying its
@@ -325,7 +364,6 @@ static int
325bn_montgomery_reduce(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mctx) 364bn_montgomery_reduce(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mctx)
326{ 365{
327 BIGNUM *n; 366 BIGNUM *n;
328 BN_ULONG *ap, *rp, n0, v, carry, mask;
329 int i, max, n_len; 367 int i, max, n_len;
330 368
331 n = &mctx->N; 369 n = &mctx->N;
@@ -341,7 +379,8 @@ bn_montgomery_reduce(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mctx)
341 379
342 /* 380 /*
343 * Expand a to twice the length of the modulus, zero if necessary. 381 * Expand a to twice the length of the modulus, zero if necessary.
344 * XXX - make this a requirement of the caller. 382 * XXX - make this a requirement of the caller or use a temporary
383 * allocation.
345 */ 384 */
346 if ((max = 2 * n_len) < n_len) 385 if ((max = 2 * n_len) < n_len)
347 return 0; 386 return 0;
@@ -350,33 +389,8 @@ bn_montgomery_reduce(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mctx)
350 for (i = a->top; i < max; i++) 389 for (i = a->top; i < max; i++)
351 a->d[i] = 0; 390 a->d[i] = 0;
352 391
353 carry = 0; 392 bn_montgomery_reduce_words(r->d, a->d, n->d, mctx->n0[0], n_len);
354 n0 = mctx->n0[0];
355 393
356 /* Add multiples of the modulus, so that it becomes divisible by R. */
357 for (i = 0; i < n_len; i++) {
358 v = bn_mul_add_words(&a->d[i], n->d, n_len, a->d[i] * n0);
359 bn_addw_addw(v, a->d[i + n_len], carry, &carry,
360 &a->d[i + n_len]);
361 }
362
363 /* Divide by R (this is the equivalent of right shifting by n_len). */
364 ap = &a->d[n_len];
365
366 /*
367 * The output is now in the range of [0, 2N). Attempt to reduce once by
368 * subtracting the modulus. If the reduction was necessary then the
369 * result is already in r, otherwise copy the value prior to reduction
370 * from the top half of a.
371 */
372 mask = carry - bn_sub_words(r->d, ap, n->d, n_len);
373
374 rp = r->d;
375 for (i = 0; i < n_len; i++) {
376 *rp = (*rp & ~mask) | (*ap & mask);
377 rp++;
378 ap++;
379 }
380 r->top = n_len; 394 r->top = n_len;
381 395
382 bn_correct_top(r); 396 bn_correct_top(r);
@@ -417,7 +431,7 @@ bn_mod_mul_montgomery_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
417 return ret; 431 return ret;
418} 432}
419 433
420static void 434static inline void
421bn_montgomery_multiply_word(const BN_ULONG *ap, BN_ULONG b, const BN_ULONG *np, 435bn_montgomery_multiply_word(const BN_ULONG *ap, BN_ULONG b, const BN_ULONG *np,
422 BN_ULONG *tp, BN_ULONG w, BN_ULONG *carry_a, BN_ULONG *carry_n, int n_len) 436 BN_ULONG *tp, BN_ULONG w, BN_ULONG *carry_a, BN_ULONG *carry_n, int n_len)
423{ 437{
@@ -452,7 +466,7 @@ bn_montgomery_multiply_word(const BN_ULONG *ap, BN_ULONG b, const BN_ULONG *np,
452 * given word arrays. The caller must ensure that rp, ap, bp and np are all 466 * given word arrays. The caller must ensure that rp, ap, bp and np are all
453 * n_len words in length, while tp must be n_len * 2 + 2 words in length. 467 * n_len words in length, while tp must be n_len * 2 + 2 words in length.
454 */ 468 */
455static void 469void
456bn_montgomery_multiply_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 470bn_montgomery_multiply_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
457 const BN_ULONG *np, BN_ULONG *tp, BN_ULONG n0, int n_len) 471 const BN_ULONG *np, BN_ULONG *tp, BN_ULONG n0, int n_len)
458{ 472{
diff --git a/src/lib/libcrypto/bn/bn_mul.c b/src/lib/libcrypto/bn/bn_mul.c
index bdeb9b0fe8..70f6534b8f 100644
--- a/src/lib/libcrypto/bn/bn_mul.c
+++ b/src/lib/libcrypto/bn/bn_mul.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mul.c,v 1.39 2023/07/08 12:21:58 beck Exp $ */ 1/* $OpenBSD: bn_mul.c,v 1.42 2025/08/05 15:06:13 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -57,6 +57,7 @@
57 */ 57 */
58 58
59#include <assert.h> 59#include <assert.h>
60#include <limits.h>
60#include <stdio.h> 61#include <stdio.h>
61#include <string.h> 62#include <string.h>
62 63
@@ -73,7 +74,7 @@
73 */ 74 */
74#ifndef HAVE_BN_MUL_COMBA4 75#ifndef HAVE_BN_MUL_COMBA4
75void 76void
76bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) 77bn_mul_comba4(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b)
77{ 78{
78 BN_ULONG c0, c1, c2; 79 BN_ULONG c0, c1, c2;
79 80
@@ -103,13 +104,73 @@ bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
103#endif 104#endif
104 105
105/* 106/*
107 * bn_mul_comba6() computes r[] = a[] * b[] using Comba multiplication
108 * (https://everything2.com/title/Comba+multiplication), where a and b are both
109 * six word arrays, producing a 12 word array result.
110 */
111#ifndef HAVE_BN_MUL_COMBA6
112void
113bn_mul_comba6(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b)
114{
115 BN_ULONG c0, c1, c2;
116
117 bn_mulw_addtw(a[0], b[0], 0, 0, 0, &c2, &c1, &r[0]);
118
119 bn_mulw_addtw(a[0], b[1], 0, c2, c1, &c2, &c1, &c0);
120 bn_mulw_addtw(a[1], b[0], c2, c1, c0, &c2, &c1, &r[1]);
121
122 bn_mulw_addtw(a[2], b[0], 0, c2, c1, &c2, &c1, &c0);
123 bn_mulw_addtw(a[1], b[1], c2, c1, c0, &c2, &c1, &c0);
124 bn_mulw_addtw(a[0], b[2], c2, c1, c0, &c2, &c1, &r[2]);
125
126 bn_mulw_addtw(a[0], b[3], 0, c2, c1, &c2, &c1, &c0);
127 bn_mulw_addtw(a[1], b[2], c2, c1, c0, &c2, &c1, &c0);
128 bn_mulw_addtw(a[2], b[1], c2, c1, c0, &c2, &c1, &c0);
129 bn_mulw_addtw(a[3], b[0], c2, c1, c0, &c2, &c1, &r[3]);
130
131 bn_mulw_addtw(a[4], b[0], 0, c2, c1, &c2, &c1, &c0);
132 bn_mulw_addtw(a[3], b[1], c2, c1, c0, &c2, &c1, &c0);
133 bn_mulw_addtw(a[2], b[2], c2, c1, c0, &c2, &c1, &c0);
134 bn_mulw_addtw(a[1], b[3], c2, c1, c0, &c2, &c1, &c0);
135 bn_mulw_addtw(a[0], b[4], c2, c1, c0, &c2, &c1, &r[4]);
136
137 bn_mulw_addtw(a[0], b[5], 0, c2, c1, &c2, &c1, &c0);
138 bn_mulw_addtw(a[1], b[4], c2, c1, c0, &c2, &c1, &c0);
139 bn_mulw_addtw(a[2], b[3], c2, c1, c0, &c2, &c1, &c0);
140 bn_mulw_addtw(a[3], b[2], c2, c1, c0, &c2, &c1, &c0);
141 bn_mulw_addtw(a[4], b[1], c2, c1, c0, &c2, &c1, &c0);
142 bn_mulw_addtw(a[5], b[0], c2, c1, c0, &c2, &c1, &r[5]);
143
144 bn_mulw_addtw(a[5], b[1], 0, c2, c1, &c2, &c1, &c0);
145 bn_mulw_addtw(a[4], b[2], c2, c1, c0, &c2, &c1, &c0);
146 bn_mulw_addtw(a[3], b[3], c2, c1, c0, &c2, &c1, &c0);
147 bn_mulw_addtw(a[2], b[4], c2, c1, c0, &c2, &c1, &c0);
148 bn_mulw_addtw(a[1], b[5], c2, c1, c0, &c2, &c1, &r[6]);
149
150 bn_mulw_addtw(a[2], b[5], 0, c2, c1, &c2, &c1, &c0);
151 bn_mulw_addtw(a[3], b[4], c2, c1, c0, &c2, &c1, &c0);
152 bn_mulw_addtw(a[4], b[3], c2, c1, c0, &c2, &c1, &c0);
153 bn_mulw_addtw(a[5], b[2], c2, c1, c0, &c2, &c1, &r[7]);
154
155 bn_mulw_addtw(a[5], b[3], 0, c2, c1, &c2, &c1, &c0);
156 bn_mulw_addtw(a[4], b[4], c2, c1, c0, &c2, &c1, &c0);
157 bn_mulw_addtw(a[3], b[5], c2, c1, c0, &c2, &c1, &r[8]);
158
159 bn_mulw_addtw(a[4], b[5], 0, c2, c1, &c2, &c1, &c0);
160 bn_mulw_addtw(a[5], b[4], c2, c1, c0, &c2, &c1, &r[9]);
161
162 bn_mulw_addtw(a[5], b[5], 0, c2, c1, &c2, &r[11], &r[10]);
163}
164#endif
165
166/*
106 * bn_mul_comba8() computes r[] = a[] * b[] using Comba multiplication 167 * bn_mul_comba8() computes r[] = a[] * b[] using Comba multiplication
107 * (https://everything2.com/title/Comba+multiplication), where a and b are both 168 * (https://everything2.com/title/Comba+multiplication), where a and b are both
108 * eight word arrays, producing a 16 word array result. 169 * eight word arrays, producing a 16 word array result.
109 */ 170 */
110#ifndef HAVE_BN_MUL_COMBA8 171#ifndef HAVE_BN_MUL_COMBA8
111void 172void
112bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) 173bn_mul_comba8(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b)
113{ 174{
114 BN_ULONG c0, c1, c2; 175 BN_ULONG c0, c1, c2;
115 176
@@ -338,9 +399,9 @@ BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
338 if (rr == NULL) 399 if (rr == NULL)
339 goto err; 400 goto err;
340 401
341 rn = a->top + b->top; 402 if (a->top > INT_MAX - b->top)
342 if (rn < a->top)
343 goto err; 403 goto err;
404 rn = a->top + b->top;
344 if (!bn_wexpand(rr, rn)) 405 if (!bn_wexpand(rr, rn))
345 goto err; 406 goto err;
346 407
diff --git a/src/lib/libcrypto/bn/bn_prime.c b/src/lib/libcrypto/bn/bn_prime.c
index 5a4aa50bf1..d85595e0dd 100644
--- a/src/lib/libcrypto/bn/bn_prime.c
+++ b/src/lib/libcrypto/bn/bn_prime.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_prime.c,v 1.34 2023/07/20 06:26:27 tb Exp $ */ 1/* $OpenBSD: bn_prime.c,v 1.35 2025/05/10 05:54:38 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -112,9 +112,8 @@
112#include <stdio.h> 112#include <stdio.h>
113#include <time.h> 113#include <time.h>
114 114
115#include <openssl/err.h>
116
117#include "bn_local.h" 115#include "bn_local.h"
116#include "err_local.h"
118 117
119/* The quick sieve algorithm approach to weeding out primes is 118/* The quick sieve algorithm approach to weeding out primes is
120 * Philip Zimmermann's, as implemented in PGP. I have had a read of 119 * Philip Zimmermann's, as implemented in PGP. I have had a read of
diff --git a/src/lib/libcrypto/bn/bn_rand.c b/src/lib/libcrypto/bn/bn_rand.c
index 9cfcd8e2c0..d3b16f70a0 100644
--- a/src/lib/libcrypto/bn/bn_rand.c
+++ b/src/lib/libcrypto/bn/bn_rand.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_rand.c,v 1.30 2024/03/16 20:42:33 tb Exp $ */ 1/* $OpenBSD: bn_rand.c,v 1.31 2025/05/10 05:54:38 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -115,9 +115,8 @@
115#include <string.h> 115#include <string.h>
116#include <time.h> 116#include <time.h>
117 117
118#include <openssl/err.h>
119
120#include "bn_local.h" 118#include "bn_local.h"
119#include "err_local.h"
121 120
122static int 121static int
123bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom) 122bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
diff --git a/src/lib/libcrypto/bn/bn_recp.c b/src/lib/libcrypto/bn/bn_recp.c
index e3f22c52a9..ed5049b772 100644
--- a/src/lib/libcrypto/bn/bn_recp.c
+++ b/src/lib/libcrypto/bn/bn_recp.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_recp.c,v 1.33 2025/02/04 20:22:20 tb Exp $ */ 1/* $OpenBSD: bn_recp.c,v 1.34 2025/05/10 05:54:38 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -58,9 +58,8 @@
58 58
59#include <stdio.h> 59#include <stdio.h>
60 60
61#include <openssl/err.h>
62
63#include "bn_local.h" 61#include "bn_local.h"
62#include "err_local.h"
64 63
65struct bn_recp_ctx_st { 64struct bn_recp_ctx_st {
66 BIGNUM *N; /* the divisor */ 65 BIGNUM *N; /* the divisor */
diff --git a/src/lib/libcrypto/bn/bn_shift.c b/src/lib/libcrypto/bn/bn_shift.c
index 12edc7c0a0..b9f73cc322 100644
--- a/src/lib/libcrypto/bn/bn_shift.c
+++ b/src/lib/libcrypto/bn/bn_shift.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_shift.c,v 1.22 2023/07/08 12:21:58 beck Exp $ */ 1/* $OpenBSD: bn_shift.c,v 1.23 2025/05/10 05:54:38 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2022, 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2022, 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -16,9 +16,9 @@
16 */ 16 */
17 17
18#include <openssl/bn.h> 18#include <openssl/bn.h>
19#include <openssl/err.h>
20 19
21#include "bn_local.h" 20#include "bn_local.h"
21#include "err_local.h"
22 22
23static inline int 23static inline int
24bn_lshift(BIGNUM *r, const BIGNUM *a, int n) 24bn_lshift(BIGNUM *r, const BIGNUM *a, int n)
diff --git a/src/lib/libcrypto/bn/bn_sqr.c b/src/lib/libcrypto/bn/bn_sqr.c
index 0dbccbf85d..ab1282e3b1 100644
--- a/src/lib/libcrypto/bn/bn_sqr.c
+++ b/src/lib/libcrypto/bn/bn_sqr.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_sqr.c,v 1.36 2023/07/08 12:21:58 beck Exp $ */ 1/* $OpenBSD: bn_sqr.c,v 1.37 2025/08/05 15:08:13 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -97,6 +97,51 @@ bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
97#endif 97#endif
98 98
99/* 99/*
100 * bn_sqr_comba6() computes r[] = a[] * a[] using Comba multiplication
101 * (https://everything2.com/title/Comba+multiplication), where a is an
102 * six word array, producing an 12 word array result.
103 */
104#ifndef HAVE_BN_SQR_COMBA6
105void
106bn_sqr_comba6(BN_ULONG *r, const BN_ULONG *a)
107{
108 BN_ULONG c2, c1, c0;
109
110 bn_mulw_addtw(a[0], a[0], 0, 0, 0, &c2, &c1, &r[0]);
111
112 bn_mul2_mulw_addtw(a[1], a[0], 0, c2, c1, &c2, &c1, &r[1]);
113
114 bn_mulw_addtw(a[1], a[1], 0, c2, c1, &c2, &c1, &c0);
115 bn_mul2_mulw_addtw(a[2], a[0], c2, c1, c0, &c2, &c1, &r[2]);
116
117 bn_mul2_mulw_addtw(a[3], a[0], 0, c2, c1, &c2, &c1, &c0);
118 bn_mul2_mulw_addtw(a[2], a[1], c2, c1, c0, &c2, &c1, &r[3]);
119
120 bn_mulw_addtw(a[2], a[2], 0, c2, c1, &c2, &c1, &c0);
121 bn_mul2_mulw_addtw(a[3], a[1], c2, c1, c0, &c2, &c1, &c0);
122 bn_mul2_mulw_addtw(a[4], a[0], c2, c1, c0, &c2, &c1, &r[4]);
123
124 bn_mul2_mulw_addtw(a[5], a[0], 0, c2, c1, &c2, &c1, &c0);
125 bn_mul2_mulw_addtw(a[4], a[1], c2, c1, c0, &c2, &c1, &c0);
126 bn_mul2_mulw_addtw(a[3], a[2], c2, c1, c0, &c2, &c1, &r[5]);
127
128 bn_mulw_addtw(a[3], a[3], 0, c2, c1, &c2, &c1, &c0);
129 bn_mul2_mulw_addtw(a[4], a[2], c2, c1, c0, &c2, &c1, &c0);
130 bn_mul2_mulw_addtw(a[5], a[1], c2, c1, c0, &c2, &c1, &r[6]);
131
132 bn_mul2_mulw_addtw(a[5], a[2], 0, c2, c1, &c2, &c1, &c0);
133 bn_mul2_mulw_addtw(a[4], a[3], c2, c1, c0, &c2, &c1, &r[7]);
134
135 bn_mulw_addtw(a[4], a[4], 0, c2, c1, &c2, &c1, &c0);
136 bn_mul2_mulw_addtw(a[5], a[3], c2, c1, c0, &c2, &c1, &r[8]);
137
138 bn_mul2_mulw_addtw(a[5], a[4], 0, c2, c1, &c2, &c1, &r[9]);
139
140 bn_mulw_addtw(a[5], a[5], 0, c2, c1, &c2, &r[11], &r[10]);
141}
142#endif
143
144/*
100 * bn_sqr_comba8() computes r[] = a[] * a[] using Comba multiplication 145 * bn_sqr_comba8() computes r[] = a[] * a[] using Comba multiplication
101 * (https://everything2.com/title/Comba+multiplication), where a is an 146 * (https://everything2.com/title/Comba+multiplication), where a is an
102 * eight word array, producing an 16 word array result. 147 * eight word array, producing an 16 word array result.