diff options
author | jsing <> | 2025-08-30 07:16:06 +0000 |
---|---|---|
committer | jsing <> | 2025-08-30 07:16:06 +0000 |
commit | 3adccdb355e2f651255227b11a6324730cb85e48 (patch) | |
tree | fd3d5dda7990f2093417b07b765c94736fe90995 /src | |
parent | 1be59554e7c016fbcde4d8264e8db21d7efb3688 (diff) | |
download | openbsd-3adccdb355e2f651255227b11a6324730cb85e48.tar.gz openbsd-3adccdb355e2f651255227b11a6324730cb85e48.tar.bz2 openbsd-3adccdb355e2f651255227b11a6324730cb85e48.zip |
Rework bn_sqr() to use bn_sqr_words().
Rework some of the squaring code so that it calls bn_sqr_words() and use
this as the integration point for assembly. Convert bn_sqr_normal() to
bn_sqr_words(), which is then used on architectures that do not provide
their own version.
This means that we resume using the assembly version of bn_sqr_words() on
i386, mips64 and powerpc, which can provide considerable performance gains.
ok tb@
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libcrypto/bn/arch/amd64/bn_arch.c | 20 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/arch/amd64/bn_arch.h | 4 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_local.h | 4 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_sqr.c | 25 |
4 files changed, 27 insertions, 26 deletions
diff --git a/src/lib/libcrypto/bn/arch/amd64/bn_arch.c b/src/lib/libcrypto/bn/arch/amd64/bn_arch.c index 9ff8920ca2..e4fbb4cfc3 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.12 2025/08/14 15:29:17 jsing Exp $ */ | 1 | /* $OpenBSD: bn_arch.c,v 1.13 2025/08/30 07:16:06 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -142,16 +142,6 @@ bn_mul_comba8(BN_ULONG *rd, const BN_ULONG *ad, const BN_ULONG *bd) | |||
142 | } | 142 | } |
143 | #endif | 143 | #endif |
144 | 144 | ||
145 | #ifdef HAVE_BN_SQR | ||
146 | int | ||
147 | bn_sqr(BIGNUM *r, const BIGNUM *a, int r_len, BN_CTX *ctx) | ||
148 | { | ||
149 | bignum_sqr(r_len, (uint64_t *)r->d, a->top, (const uint64_t *)a->d); | ||
150 | |||
151 | return 1; | ||
152 | } | ||
153 | #endif | ||
154 | |||
155 | #ifdef HAVE_BN_SQR_COMBA4 | 145 | #ifdef HAVE_BN_SQR_COMBA4 |
156 | void | 146 | void |
157 | bn_sqr_comba4(BN_ULONG *rd, const BN_ULONG *ad) | 147 | bn_sqr_comba4(BN_ULONG *rd, const BN_ULONG *ad) |
@@ -191,6 +181,14 @@ bn_sqr_comba8(BN_ULONG *rd, const BN_ULONG *ad) | |||
191 | } | 181 | } |
192 | #endif | 182 | #endif |
193 | 183 | ||
184 | #ifdef HAVE_BN_SQR_WORDS | ||
185 | void | ||
186 | bn_sqr_words(BN_ULONG *rd, const BN_ULONG *ad, int a_len) | ||
187 | { | ||
188 | bignum_sqr(a_len * 2, (uint64_t *)rd, a_len, (const uint64_t *)ad); | ||
189 | } | ||
190 | #endif | ||
191 | |||
194 | #ifdef HAVE_BN_WORD_CLZ | 192 | #ifdef HAVE_BN_WORD_CLZ |
195 | int | 193 | int |
196 | bn_word_clz(BN_ULONG w) | 194 | bn_word_clz(BN_ULONG w) |
diff --git a/src/lib/libcrypto/bn/arch/amd64/bn_arch.h b/src/lib/libcrypto/bn/arch/amd64/bn_arch.h index 7359f993a7..dd7abd3002 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.16 2025/08/14 15:22:54 jsing Exp $ */ | 1 | /* $OpenBSD: bn_arch.h,v 1.17 2025/08/30 07:16:06 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -36,10 +36,10 @@ | |||
36 | #define HAVE_BN_MUL_COMBA8 | 36 | #define HAVE_BN_MUL_COMBA8 |
37 | #define HAVE_BN_MUL_WORDS | 37 | #define HAVE_BN_MUL_WORDS |
38 | 38 | ||
39 | #define HAVE_BN_SQR | ||
40 | #define HAVE_BN_SQR_COMBA4 | 39 | #define HAVE_BN_SQR_COMBA4 |
41 | #define HAVE_BN_SQR_COMBA6 | 40 | #define HAVE_BN_SQR_COMBA6 |
42 | #define HAVE_BN_SQR_COMBA8 | 41 | #define HAVE_BN_SQR_COMBA8 |
42 | #define HAVE_BN_SQR_WORDS | ||
43 | 43 | ||
44 | #define HAVE_BN_SUB | 44 | #define HAVE_BN_SUB |
45 | #define HAVE_BN_SUB_WORDS | 45 | #define HAVE_BN_SUB_WORDS |
diff --git a/src/lib/libcrypto/bn/bn_local.h b/src/lib/libcrypto/bn/bn_local.h index 1bd4c16baf..16d270b6a1 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.54 2025/08/05 15:08:13 jsing Exp $ */ | 1 | /* $OpenBSD: bn_local.h,v 1.55 2025/08/30 07:16:06 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 | * |
@@ -247,6 +247,7 @@ void bn_mul_comba8(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b); | |||
247 | void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a); | 247 | void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a); |
248 | void bn_sqr_comba6(BN_ULONG *r, const BN_ULONG *a); | 248 | void bn_sqr_comba6(BN_ULONG *r, const BN_ULONG *a); |
249 | void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a); | 249 | void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a); |
250 | void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int a_len); | ||
250 | 251 | ||
251 | int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, | 252 | int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, |
252 | const BN_ULONG *np, const BN_ULONG *n0, int num); | 253 | const BN_ULONG *np, const BN_ULONG *n0, int num); |
@@ -258,7 +259,6 @@ int bn_wexpand(BIGNUM *a, int words); | |||
258 | 259 | ||
259 | BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); | 260 | BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); |
260 | BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); | 261 | BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); |
261 | void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num); | ||
262 | BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d); | 262 | BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d); |
263 | void bn_div_rem_words(BN_ULONG h, BN_ULONG l, BN_ULONG d, BN_ULONG *out_q, | 263 | void bn_div_rem_words(BN_ULONG h, BN_ULONG l, BN_ULONG d, BN_ULONG *out_q, |
264 | BN_ULONG *out_r); | 264 | BN_ULONG *out_r); |
diff --git a/src/lib/libcrypto/bn/bn_sqr.c b/src/lib/libcrypto/bn/bn_sqr.c index 2f7f71f819..3a6eed06c6 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.38 2025/08/14 15:15:04 jsing Exp $ */ | 1 | /* $OpenBSD: bn_sqr.c,v 1.39 2025/08/30 07:16:06 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 | * |
@@ -64,8 +64,6 @@ | |||
64 | #include "bn_local.h" | 64 | #include "bn_local.h" |
65 | #include "bn_internal.h" | 65 | #include "bn_internal.h" |
66 | 66 | ||
67 | int bn_sqr(BIGNUM *r, const BIGNUM *a, int max, BN_CTX *ctx); | ||
68 | |||
69 | /* | 67 | /* |
70 | * bn_sqr_comba4() computes r[] = a[] * a[] using Comba multiplication | 68 | * bn_sqr_comba4() computes r[] = a[] * a[] using Comba multiplication |
71 | * (https://everything2.com/title/Comba+multiplication), where a is a | 69 | * (https://everything2.com/title/Comba+multiplication), where a is a |
@@ -205,7 +203,7 @@ bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a) | |||
205 | } | 203 | } |
206 | #endif | 204 | #endif |
207 | 205 | ||
208 | #ifndef HAVE_BN_SQR | 206 | #ifndef HAVE_BN_SQR_WORDS |
209 | /* | 207 | /* |
210 | * bn_sqr_add_words() computes (r[i*2+1]:r[i*2]) = (r[i*2+1]:r[i*2]) + a[i] * a[i]. | 208 | * bn_sqr_add_words() computes (r[i*2+1]:r[i*2]) = (r[i*2+1]:r[i*2]) + a[i] * a[i]. |
211 | */ | 209 | */ |
@@ -242,12 +240,16 @@ bn_sqr_add_words(BN_ULONG *r, const BN_ULONG *a, int n) | |||
242 | } | 240 | } |
243 | } | 241 | } |
244 | 242 | ||
245 | static void | 243 | /* |
246 | bn_sqr_normal(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len) | 244 | * bn_sqr_words() computes r[] = a[] * a[]. |
245 | */ | ||
246 | void | ||
247 | bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int a_len) | ||
247 | { | 248 | { |
248 | const BN_ULONG *ap; | 249 | const BN_ULONG *ap; |
249 | BN_ULONG *rp; | 250 | BN_ULONG *rp; |
250 | BN_ULONG w; | 251 | BN_ULONG w; |
252 | int r_len; | ||
251 | int n; | 253 | int n; |
252 | 254 | ||
253 | if (a_len <= 0) | 255 | if (a_len <= 0) |
@@ -258,6 +260,7 @@ bn_sqr_normal(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len) | |||
258 | ap++; | 260 | ap++; |
259 | 261 | ||
260 | rp = r; | 262 | rp = r; |
263 | r_len = a_len * 2; | ||
261 | rp[0] = rp[r_len - 1] = 0; | 264 | rp[0] = rp[r_len - 1] = 0; |
262 | rp++; | 265 | rp++; |
263 | 266 | ||
@@ -285,20 +288,20 @@ bn_sqr_normal(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len) | |||
285 | /* Add squares. */ | 288 | /* Add squares. */ |
286 | bn_sqr_add_words(r, a, a_len); | 289 | bn_sqr_add_words(r, a, a_len); |
287 | } | 290 | } |
291 | #endif | ||
288 | 292 | ||
289 | /* | 293 | /* |
290 | * bn_sqr() computes a * a, storing the result in r. The caller must ensure that | 294 | * bn_sqr() computes a * a, storing the result in r. The caller must ensure that |
291 | * r is not the same BIGNUM as a and that r has been expanded to rn = a->top * 2 | 295 | * r is not the same BIGNUM as a and that r has been expanded to rn = a->top * 2 |
292 | * words. | 296 | * words. |
293 | */ | 297 | */ |
294 | int | 298 | static int |
295 | bn_sqr(BIGNUM *r, const BIGNUM *a, int r_len, BN_CTX *ctx) | 299 | bn_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) |
296 | { | 300 | { |
297 | bn_sqr_normal(r->d, r_len, a->d, a->top); | 301 | bn_sqr_words(r->d, a->d, a->top); |
298 | 302 | ||
299 | return 1; | 303 | return 1; |
300 | } | 304 | } |
301 | #endif | ||
302 | 305 | ||
303 | int | 306 | int |
304 | BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) | 307 | BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) |
@@ -331,7 +334,7 @@ BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) | |||
331 | } else if (a->top == 8) { | 334 | } else if (a->top == 8) { |
332 | bn_sqr_comba8(rr->d, a->d); | 335 | bn_sqr_comba8(rr->d, a->d); |
333 | } else { | 336 | } else { |
334 | if (!bn_sqr(rr, a, r_len, ctx)) | 337 | if (!bn_sqr(rr, a, ctx)) |
335 | goto err; | 338 | goto err; |
336 | } | 339 | } |
337 | 340 | ||