summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_mul.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/bn/bn_mul.c172
1 files changed, 17 insertions, 155 deletions
diff --git a/src/lib/libcrypto/bn/bn_mul.c b/src/lib/libcrypto/bn/bn_mul.c
index 1d56e57b76..5e270b988f 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.33 2023/02/15 18:10:16 jsing Exp $ */ 1/* $OpenBSD: bn_mul.c,v 1.34 2023/02/22 05:57:19 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 *
@@ -269,144 +269,6 @@ bn_mul_add_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w)
269} 269}
270#endif 270#endif
271 271
272#if defined(OPENSSL_NO_ASM) || !defined(OPENSSL_BN_ASM_PART_WORDS)
273/*
274 * Here follows a specialised variant of bn_sub_words(), which has the property
275 * performing operations on arrays of different sizes. The sizes of those arrays
276 * is expressed through cl, which is the common length (basically,
277 * min(len(a),len(b))), and dl, which is the delta between the two lengths,
278 * calculated as len(a)-len(b). All lengths are the number of BN_ULONGs. For the
279 * operations that require a result array as parameter, it must have the length
280 * cl+abs(dl).
281 */
282BN_ULONG
283bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int cl,
284 int dl)
285{
286 BN_ULONG c, t;
287
288 assert(cl >= 0);
289 c = bn_sub_words(r, a, b, cl);
290
291 if (dl == 0)
292 return c;
293
294 r += cl;
295 a += cl;
296 b += cl;
297
298 if (dl < 0) {
299 for (;;) {
300 t = b[0];
301 r[0] = (0 - t - c) & BN_MASK2;
302 if (t != 0)
303 c = 1;
304 if (++dl >= 0)
305 break;
306
307 t = b[1];
308 r[1] = (0 - t - c) & BN_MASK2;
309 if (t != 0)
310 c = 1;
311 if (++dl >= 0)
312 break;
313
314 t = b[2];
315 r[2] = (0 - t - c) & BN_MASK2;
316 if (t != 0)
317 c = 1;
318 if (++dl >= 0)
319 break;
320
321 t = b[3];
322 r[3] = (0 - t - c) & BN_MASK2;
323 if (t != 0)
324 c = 1;
325 if (++dl >= 0)
326 break;
327
328 b += 4;
329 r += 4;
330 }
331 } else {
332 int save_dl = dl;
333 while (c) {
334 t = a[0];
335 r[0] = (t - c) & BN_MASK2;
336 if (t != 0)
337 c = 0;
338 if (--dl <= 0)
339 break;
340
341 t = a[1];
342 r[1] = (t - c) & BN_MASK2;
343 if (t != 0)
344 c = 0;
345 if (--dl <= 0)
346 break;
347
348 t = a[2];
349 r[2] = (t - c) & BN_MASK2;
350 if (t != 0)
351 c = 0;
352 if (--dl <= 0)
353 break;
354
355 t = a[3];
356 r[3] = (t - c) & BN_MASK2;
357 if (t != 0)
358 c = 0;
359 if (--dl <= 0)
360 break;
361
362 save_dl = dl;
363 a += 4;
364 r += 4;
365 }
366 if (dl > 0) {
367 if (save_dl > dl) {
368 switch (save_dl - dl) {
369 case 1:
370 r[1] = a[1];
371 if (--dl <= 0)
372 break;
373 case 2:
374 r[2] = a[2];
375 if (--dl <= 0)
376 break;
377 case 3:
378 r[3] = a[3];
379 if (--dl <= 0)
380 break;
381 }
382 a += 4;
383 r += 4;
384 }
385 }
386 if (dl > 0) {
387 for (;;) {
388 r[0] = a[0];
389 if (--dl <= 0)
390 break;
391 r[1] = a[1];
392 if (--dl <= 0)
393 break;
394 r[2] = a[2];
395 if (--dl <= 0)
396 break;
397 r[3] = a[3];
398 if (--dl <= 0)
399 break;
400
401 a += 4;
402 r += 4;
403 }
404 }
405 }
406 return c;
407}
408#endif
409
410void 272void
411bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) 273bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
412{ 274{
@@ -504,15 +366,15 @@ bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, int dna,
504 zero = neg = 0; 366 zero = neg = 0;
505 switch (c1 * 3 + c2) { 367 switch (c1 * 3 + c2) {
506 case -4: 368 case -4:
507 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */ 369 bn_sub(t, n, &a[n], tna, a, n); /* - */
508 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */ 370 bn_sub(&t[n], n, b, n, &b[n], tnb); /* - */
509 break; 371 break;
510 case -3: 372 case -3:
511 zero = 1; 373 zero = 1;
512 break; 374 break;
513 case -2: 375 case -2:
514 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */ 376 bn_sub(t, n, &a[n], tna, a, n); /* - */
515 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */ 377 bn_sub(&t[n], n, &b[n], tnb, b, n); /* + */
516 neg = 1; 378 neg = 1;
517 break; 379 break;
518 case -1: 380 case -1:
@@ -521,16 +383,16 @@ bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, int dna,
521 zero = 1; 383 zero = 1;
522 break; 384 break;
523 case 2: 385 case 2:
524 bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */ 386 bn_sub(t, n, a, n, &a[n], tna); /* + */
525 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */ 387 bn_sub(&t[n], n, b, n, &b[n], tnb); /* - */
526 neg = 1; 388 neg = 1;
527 break; 389 break;
528 case 3: 390 case 3:
529 zero = 1; 391 zero = 1;
530 break; 392 break;
531 case 4: 393 case 4:
532 bn_sub_part_words(t, a, &(a[n]), tna, n - tna); 394 bn_sub(t, n, a, n, &a[n], tna);
533 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); 395 bn_sub(&t[n], n, &b[n], tnb, b, n);
534 break; 396 break;
535 } 397 }
536 398
@@ -630,14 +492,14 @@ bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n, int tna,
630 neg = 0; 492 neg = 0;
631 switch (c1 * 3 + c2) { 493 switch (c1 * 3 + c2) {
632 case -4: 494 case -4:
633 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */ 495 bn_sub(t, n, &a[n], tna, a, n); /* - */
634 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */ 496 bn_sub(&t[n], n, b, n, &b[n], tnb); /* - */
635 break; 497 break;
636 case -3: 498 case -3:
637 /* break; */ 499 /* break; */
638 case -2: 500 case -2:
639 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */ 501 bn_sub(t, n, &a[n], tna, a, n); /* - */
640 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */ 502 bn_sub(&t[n], n, &b[n], tnb, b, n); /* + */
641 neg = 1; 503 neg = 1;
642 break; 504 break;
643 case -1: 505 case -1:
@@ -645,15 +507,15 @@ bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n, int tna,
645 case 1: 507 case 1:
646 /* break; */ 508 /* break; */
647 case 2: 509 case 2:
648 bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */ 510 bn_sub(t, n, a, n, &a[n], tna); /* + */
649 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */ 511 bn_sub(&t[n], n, b, n, &b[n], tnb); /* - */
650 neg = 1; 512 neg = 1;
651 break; 513 break;
652 case 3: 514 case 3:
653 /* break; */ 515 /* break; */
654 case 4: 516 case 4:
655 bn_sub_part_words(t, a, &(a[n]), tna, n - tna); 517 bn_sub(t, n, a, n, &a[n], tna);
656 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); 518 bn_sub(&t[n], n, &b[n], tnb, b, n);
657 break; 519 break;
658 } 520 }
659 /* The zero case isn't yet implemented here. The speedup 521 /* The zero case isn't yet implemented here. The speedup