diff options
Diffstat (limited to 'src/lib/libcrypto/bn/bn_mul.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_mul.c | 67 |
1 files changed, 31 insertions, 36 deletions
diff --git a/src/lib/libcrypto/bn/bn_mul.c b/src/lib/libcrypto/bn/bn_mul.c index a30d05fb02..6ba05f2eba 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.43 2025/08/14 15:15:04 jsing Exp $ */ | 1 | /* $OpenBSD: bn_mul.c,v 1.44 2025/08/30 07:54:27 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 | * |
@@ -256,14 +256,13 @@ bn_mul_comba8(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b) | |||
256 | #endif | 256 | #endif |
257 | 257 | ||
258 | /* | 258 | /* |
259 | * bn_mul_words() computes (carry:r[i]) = a[i] * w + carry, where a is an array | 259 | * bn_mulw_words() computes (carry:r[i]) = a[i] * w + carry, where a is an array |
260 | * of words and w is a single word. This should really be called bn_mulw_words() | 260 | * of words and w is a single word. This is used as a step in the multiplication |
261 | * since only one input is an array. This is used as a step in the multiplication | ||
262 | * of word arrays. | 261 | * of word arrays. |
263 | */ | 262 | */ |
264 | #ifndef HAVE_BN_MUL_WORDS | 263 | #ifndef HAVE_BN_MULW_WORDS |
265 | BN_ULONG | 264 | BN_ULONG |
266 | bn_mul_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w) | 265 | bn_mulw_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w) |
267 | { | 266 | { |
268 | BN_ULONG carry = 0; | 267 | BN_ULONG carry = 0; |
269 | 268 | ||
@@ -289,14 +288,13 @@ bn_mul_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w) | |||
289 | #endif | 288 | #endif |
290 | 289 | ||
291 | /* | 290 | /* |
292 | * bn_mul_add_words() computes (carry:r[i]) = a[i] * w + r[i] + carry, where | 291 | * bn_mulw_add_words() computes (carry:r[i]) = a[i] * w + r[i] + carry, where |
293 | * a is an array of words and w is a single word. This should really be called | 292 | * a is an array of words and w is a single word. This is used as a step in the |
294 | * bn_mulw_add_words() since only one input is an array. This is used as a step | 293 | * multiplication of word arrays. |
295 | * in the multiplication of word arrays. | ||
296 | */ | 294 | */ |
297 | #ifndef HAVE_BN_MUL_ADD_WORDS | 295 | #ifndef HAVE_BN_MULW_ADD_WORDS |
298 | BN_ULONG | 296 | BN_ULONG |
299 | bn_mul_add_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w) | 297 | bn_mulw_add_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w) |
300 | { | 298 | { |
301 | BN_ULONG carry = 0; | 299 | BN_ULONG carry = 0; |
302 | 300 | ||
@@ -323,62 +321,59 @@ bn_mul_add_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w) | |||
323 | } | 321 | } |
324 | #endif | 322 | #endif |
325 | 323 | ||
324 | #ifndef HAVE_BN_MUL_WORDS | ||
326 | void | 325 | void |
327 | bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) | 326 | bn_mul_words(BN_ULONG *r, BN_ULONG *a, int a_len, BN_ULONG *b, int b_len) |
328 | { | 327 | { |
329 | BN_ULONG *rr; | 328 | BN_ULONG *rr; |
330 | 329 | ||
331 | 330 | if (a_len < b_len) { | |
332 | if (na < nb) { | ||
333 | int itmp; | 331 | int itmp; |
334 | BN_ULONG *ltmp; | 332 | BN_ULONG *ltmp; |
335 | 333 | ||
336 | itmp = na; | 334 | itmp = a_len; |
337 | na = nb; | 335 | a_len = b_len; |
338 | nb = itmp; | 336 | b_len = itmp; |
339 | ltmp = a; | 337 | ltmp = a; |
340 | a = b; | 338 | a = b; |
341 | b = ltmp; | 339 | b = ltmp; |
342 | 340 | ||
343 | } | 341 | } |
344 | rr = &(r[na]); | 342 | rr = &(r[a_len]); |
345 | if (nb <= 0) { | 343 | if (b_len <= 0) { |
346 | (void)bn_mul_words(r, a, na, 0); | 344 | (void)bn_mulw_words(r, a, a_len, 0); |
347 | return; | 345 | return; |
348 | } else | 346 | } else |
349 | rr[0] = bn_mul_words(r, a, na, b[0]); | 347 | rr[0] = bn_mulw_words(r, a, a_len, b[0]); |
350 | 348 | ||
351 | for (;;) { | 349 | for (;;) { |
352 | if (--nb <= 0) | 350 | if (--b_len <= 0) |
353 | return; | 351 | return; |
354 | rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]); | 352 | rr[1] = bn_mulw_add_words(&(r[1]), a, a_len, b[1]); |
355 | if (--nb <= 0) | 353 | if (--b_len <= 0) |
356 | return; | 354 | return; |
357 | rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]); | 355 | rr[2] = bn_mulw_add_words(&(r[2]), a, a_len, b[2]); |
358 | if (--nb <= 0) | 356 | if (--b_len <= 0) |
359 | return; | 357 | return; |
360 | rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]); | 358 | rr[3] = bn_mulw_add_words(&(r[3]), a, a_len, b[3]); |
361 | if (--nb <= 0) | 359 | if (--b_len <= 0) |
362 | return; | 360 | return; |
363 | rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]); | 361 | rr[4] = bn_mulw_add_words(&(r[4]), a, a_len, b[4]); |
364 | rr += 4; | 362 | rr += 4; |
365 | r += 4; | 363 | r += 4; |
366 | b += 4; | 364 | b += 4; |
367 | } | 365 | } |
368 | } | 366 | } |
367 | #endif | ||
369 | 368 | ||
370 | 369 | static int | |
371 | #ifndef HAVE_BN_MUL | ||
372 | int | ||
373 | bn_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, int rn, BN_CTX *ctx) | 370 | bn_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, int rn, BN_CTX *ctx) |
374 | { | 371 | { |
375 | bn_mul_normal(r->d, a->d, a->top, b->d, b->top); | 372 | bn_mul_words(r->d, a->d, a->top, b->d, b->top); |
376 | 373 | ||
377 | return 1; | 374 | return 1; |
378 | } | 375 | } |
379 | 376 | ||
380 | #endif /* HAVE_BN_MUL */ | ||
381 | |||
382 | int | 377 | int |
383 | BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) | 378 | BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) |
384 | { | 379 | { |