diff options
Diffstat (limited to 'src/lib/libcrypto/bn/bn_nist.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_nist.c | 338 |
1 files changed, 298 insertions, 40 deletions
diff --git a/src/lib/libcrypto/bn/bn_nist.c b/src/lib/libcrypto/bn/bn_nist.c index c6de032696..43caee4770 100644 --- a/src/lib/libcrypto/bn/bn_nist.c +++ b/src/lib/libcrypto/bn/bn_nist.c | |||
@@ -319,6 +319,13 @@ static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top) | |||
319 | :(to[(n)/2] =((m)&1)?(from[(m)/2]>>32):(from[(m)/2]&BN_MASK2l))) | 319 | :(to[(n)/2] =((m)&1)?(from[(m)/2]>>32):(from[(m)/2]&BN_MASK2l))) |
320 | #define bn_32_set_0(to, n) (((n)&1)?(to[(n)/2]&=BN_MASK2l):(to[(n)/2]=0)); | 320 | #define bn_32_set_0(to, n) (((n)&1)?(to[(n)/2]&=BN_MASK2l):(to[(n)/2]=0)); |
321 | #define bn_cp_32(to,n,from,m) ((m)>=0)?bn_cp_32_naked(to,n,from,m):bn_32_set_0(to,n) | 321 | #define bn_cp_32(to,n,from,m) ((m)>=0)?bn_cp_32_naked(to,n,from,m):bn_32_set_0(to,n) |
322 | # if defined(L_ENDIAN) | ||
323 | # if defined(__arch64__) | ||
324 | # define NIST_INT64 long | ||
325 | # else | ||
326 | # define NIST_INT64 long long | ||
327 | # endif | ||
328 | # endif | ||
322 | #else | 329 | #else |
323 | #define bn_cp_64(to, n, from, m) \ | 330 | #define bn_cp_64(to, n, from, m) \ |
324 | { \ | 331 | { \ |
@@ -330,13 +337,15 @@ static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top) | |||
330 | bn_32_set_0(to, (n)*2); \ | 337 | bn_32_set_0(to, (n)*2); \ |
331 | bn_32_set_0(to, (n)*2+1); \ | 338 | bn_32_set_0(to, (n)*2+1); \ |
332 | } | 339 | } |
333 | #if BN_BITS2 == 32 | ||
334 | #define bn_cp_32(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0; | 340 | #define bn_cp_32(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0; |
335 | #define bn_32_set_0(to, n) (to)[n] = (BN_ULONG)0; | 341 | #define bn_32_set_0(to, n) (to)[n] = (BN_ULONG)0; |
336 | #endif | 342 | # if defined(_WIN32) && !defined(__GNUC__) |
343 | # define NIST_INT64 __int64 | ||
344 | # elif defined(BN_LLONG) | ||
345 | # define NIST_INT64 long long | ||
346 | # endif | ||
337 | #endif /* BN_BITS2 != 64 */ | 347 | #endif /* BN_BITS2 != 64 */ |
338 | 348 | ||
339 | |||
340 | #define nist_set_192(to, from, a1, a2, a3) \ | 349 | #define nist_set_192(to, from, a1, a2, a3) \ |
341 | { \ | 350 | { \ |
342 | bn_cp_64(to, 0, from, (a3) - 3) \ | 351 | bn_cp_64(to, 0, from, (a3) - 3) \ |
@@ -350,9 +359,11 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, | |||
350 | int top = a->top, i; | 359 | int top = a->top, i; |
351 | int carry; | 360 | int carry; |
352 | register BN_ULONG *r_d, *a_d = a->d; | 361 | register BN_ULONG *r_d, *a_d = a->d; |
353 | BN_ULONG t_d[BN_NIST_192_TOP], | 362 | union { |
354 | buf[BN_NIST_192_TOP], | 363 | BN_ULONG bn[BN_NIST_192_TOP]; |
355 | c_d[BN_NIST_192_TOP], | 364 | unsigned int ui[BN_NIST_192_TOP*sizeof(BN_ULONG)/sizeof(unsigned int)]; |
365 | } buf; | ||
366 | BN_ULONG c_d[BN_NIST_192_TOP], | ||
356 | *res; | 367 | *res; |
357 | PTR_SIZE_INT mask; | 368 | PTR_SIZE_INT mask; |
358 | static const BIGNUM _bignum_nist_p_192_sqr = { | 369 | static const BIGNUM _bignum_nist_p_192_sqr = { |
@@ -385,15 +396,48 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, | |||
385 | else | 396 | else |
386 | r_d = a_d; | 397 | r_d = a_d; |
387 | 398 | ||
388 | nist_cp_bn_0(buf, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP, BN_NIST_192_TOP); | 399 | nist_cp_bn_0(buf.bn, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP, BN_NIST_192_TOP); |
400 | |||
401 | #if defined(NIST_INT64) | ||
402 | { | ||
403 | NIST_INT64 acc; /* accumulator */ | ||
404 | unsigned int *rp=(unsigned int *)r_d; | ||
405 | const unsigned int *bp=(const unsigned int *)buf.ui; | ||
406 | |||
407 | acc = rp[0]; acc += bp[3*2-6]; | ||
408 | acc += bp[5*2-6]; rp[0] = (unsigned int)acc; acc >>= 32; | ||
409 | |||
410 | acc += rp[1]; acc += bp[3*2-5]; | ||
411 | acc += bp[5*2-5]; rp[1] = (unsigned int)acc; acc >>= 32; | ||
389 | 412 | ||
390 | nist_set_192(t_d, buf, 0, 3, 3); | 413 | acc += rp[2]; acc += bp[3*2-6]; |
414 | acc += bp[4*2-6]; | ||
415 | acc += bp[5*2-6]; rp[2] = (unsigned int)acc; acc >>= 32; | ||
416 | |||
417 | acc += rp[3]; acc += bp[3*2-5]; | ||
418 | acc += bp[4*2-5]; | ||
419 | acc += bp[5*2-5]; rp[3] = (unsigned int)acc; acc >>= 32; | ||
420 | |||
421 | acc += rp[4]; acc += bp[4*2-6]; | ||
422 | acc += bp[5*2-6]; rp[4] = (unsigned int)acc; acc >>= 32; | ||
423 | |||
424 | acc += rp[5]; acc += bp[4*2-5]; | ||
425 | acc += bp[5*2-5]; rp[5] = (unsigned int)acc; | ||
426 | |||
427 | carry = (int)(acc>>32); | ||
428 | } | ||
429 | #else | ||
430 | { | ||
431 | BN_ULONG t_d[BN_NIST_192_TOP]; | ||
432 | |||
433 | nist_set_192(t_d, buf.bn, 0, 3, 3); | ||
391 | carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP); | 434 | carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP); |
392 | nist_set_192(t_d, buf, 4, 4, 0); | 435 | nist_set_192(t_d, buf.bn, 4, 4, 0); |
393 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP); | 436 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP); |
394 | nist_set_192(t_d, buf, 5, 5, 5) | 437 | nist_set_192(t_d, buf.bn, 5, 5, 5) |
395 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP); | 438 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP); |
396 | 439 | } | |
440 | #endif | ||
397 | if (carry > 0) | 441 | if (carry > 0) |
398 | carry = (int)bn_sub_words(r_d,r_d,_nist_p_192[carry-1],BN_NIST_192_TOP); | 442 | carry = (int)bn_sub_words(r_d,r_d,_nist_p_192[carry-1],BN_NIST_192_TOP); |
399 | else | 443 | else |
@@ -435,8 +479,7 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, | |||
435 | int top = a->top, i; | 479 | int top = a->top, i; |
436 | int carry; | 480 | int carry; |
437 | BN_ULONG *r_d, *a_d = a->d; | 481 | BN_ULONG *r_d, *a_d = a->d; |
438 | BN_ULONG t_d[BN_NIST_224_TOP], | 482 | BN_ULONG buf[BN_NIST_224_TOP], |
439 | buf[BN_NIST_224_TOP], | ||
440 | c_d[BN_NIST_224_TOP], | 483 | c_d[BN_NIST_224_TOP], |
441 | *res; | 484 | *res; |
442 | PTR_SIZE_INT mask; | 485 | PTR_SIZE_INT mask; |
@@ -474,14 +517,54 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, | |||
474 | 517 | ||
475 | #if BN_BITS2==64 | 518 | #if BN_BITS2==64 |
476 | /* copy upper 256 bits of 448 bit number ... */ | 519 | /* copy upper 256 bits of 448 bit number ... */ |
477 | nist_cp_bn_0(t_d, a_d + (BN_NIST_224_TOP-1), top - (BN_NIST_224_TOP-1), BN_NIST_224_TOP); | 520 | nist_cp_bn_0(c_d, a_d + (BN_NIST_224_TOP-1), top - (BN_NIST_224_TOP-1), BN_NIST_224_TOP); |
478 | /* ... and right shift by 32 to obtain upper 224 bits */ | 521 | /* ... and right shift by 32 to obtain upper 224 bits */ |
479 | nist_set_224(buf, t_d, 14, 13, 12, 11, 10, 9, 8); | 522 | nist_set_224(buf, c_d, 14, 13, 12, 11, 10, 9, 8); |
480 | /* truncate lower part to 224 bits too */ | 523 | /* truncate lower part to 224 bits too */ |
481 | r_d[BN_NIST_224_TOP-1] &= BN_MASK2l; | 524 | r_d[BN_NIST_224_TOP-1] &= BN_MASK2l; |
482 | #else | 525 | #else |
483 | nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP); | 526 | nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP); |
484 | #endif | 527 | #endif |
528 | |||
529 | #if defined(NIST_INT64) && BN_BITS2!=64 | ||
530 | { | ||
531 | NIST_INT64 acc; /* accumulator */ | ||
532 | unsigned int *rp=(unsigned int *)r_d; | ||
533 | const unsigned int *bp=(const unsigned int *)buf; | ||
534 | |||
535 | acc = rp[0]; acc -= bp[7-7]; | ||
536 | acc -= bp[11-7]; rp[0] = (unsigned int)acc; acc >>= 32; | ||
537 | |||
538 | acc += rp[1]; acc -= bp[8-7]; | ||
539 | acc -= bp[12-7]; rp[1] = (unsigned int)acc; acc >>= 32; | ||
540 | |||
541 | acc += rp[2]; acc -= bp[9-7]; | ||
542 | acc -= bp[13-7]; rp[2] = (unsigned int)acc; acc >>= 32; | ||
543 | |||
544 | acc += rp[3]; acc += bp[7-7]; | ||
545 | acc += bp[11-7]; | ||
546 | acc -= bp[10-7]; rp[3] = (unsigned int)acc; acc>>= 32; | ||
547 | |||
548 | acc += rp[4]; acc += bp[8-7]; | ||
549 | acc += bp[12-7]; | ||
550 | acc -= bp[11-7]; rp[4] = (unsigned int)acc; acc >>= 32; | ||
551 | |||
552 | acc += rp[5]; acc += bp[9-7]; | ||
553 | acc += bp[13-7]; | ||
554 | acc -= bp[12-7]; rp[5] = (unsigned int)acc; acc >>= 32; | ||
555 | |||
556 | acc += rp[6]; acc += bp[10-7]; | ||
557 | acc -= bp[13-7]; rp[6] = (unsigned int)acc; | ||
558 | |||
559 | carry = (int)(acc>>32); | ||
560 | # if BN_BITS2==64 | ||
561 | rp[7] = carry; | ||
562 | # endif | ||
563 | } | ||
564 | #else | ||
565 | { | ||
566 | BN_ULONG t_d[BN_NIST_224_TOP]; | ||
567 | |||
485 | nist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0); | 568 | nist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0); |
486 | carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP); | 569 | carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP); |
487 | nist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0); | 570 | nist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0); |
@@ -494,6 +577,8 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, | |||
494 | #if BN_BITS2==64 | 577 | #if BN_BITS2==64 |
495 | carry = (int)(r_d[BN_NIST_224_TOP-1]>>32); | 578 | carry = (int)(r_d[BN_NIST_224_TOP-1]>>32); |
496 | #endif | 579 | #endif |
580 | } | ||
581 | #endif | ||
497 | u.f = bn_sub_words; | 582 | u.f = bn_sub_words; |
498 | if (carry > 0) | 583 | if (carry > 0) |
499 | { | 584 | { |
@@ -548,9 +633,11 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, | |||
548 | int i, top = a->top; | 633 | int i, top = a->top; |
549 | int carry = 0; | 634 | int carry = 0; |
550 | register BN_ULONG *a_d = a->d, *r_d; | 635 | register BN_ULONG *a_d = a->d, *r_d; |
551 | BN_ULONG t_d[BN_NIST_256_TOP], | 636 | union { |
552 | buf[BN_NIST_256_TOP], | 637 | BN_ULONG bn[BN_NIST_256_TOP]; |
553 | c_d[BN_NIST_256_TOP], | 638 | unsigned int ui[BN_NIST_256_TOP*sizeof(BN_ULONG)/sizeof(unsigned int)]; |
639 | } buf; | ||
640 | BN_ULONG c_d[BN_NIST_256_TOP], | ||
554 | *res; | 641 | *res; |
555 | PTR_SIZE_INT mask; | 642 | PTR_SIZE_INT mask; |
556 | union { bn_addsub_f f; PTR_SIZE_INT p; } u; | 643 | union { bn_addsub_f f; PTR_SIZE_INT p; } u; |
@@ -584,12 +671,87 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, | |||
584 | else | 671 | else |
585 | r_d = a_d; | 672 | r_d = a_d; |
586 | 673 | ||
587 | nist_cp_bn_0(buf, a_d + BN_NIST_256_TOP, top - BN_NIST_256_TOP, BN_NIST_256_TOP); | 674 | nist_cp_bn_0(buf.bn, a_d + BN_NIST_256_TOP, top - BN_NIST_256_TOP, BN_NIST_256_TOP); |
675 | |||
676 | #if defined(NIST_INT64) | ||
677 | { | ||
678 | NIST_INT64 acc; /* accumulator */ | ||
679 | unsigned int *rp=(unsigned int *)r_d; | ||
680 | const unsigned int *bp=(const unsigned int *)buf.ui; | ||
681 | |||
682 | acc = rp[0]; acc += bp[8-8]; | ||
683 | acc += bp[9-8]; | ||
684 | acc -= bp[11-8]; | ||
685 | acc -= bp[12-8]; | ||
686 | acc -= bp[13-8]; | ||
687 | acc -= bp[14-8]; rp[0] = (unsigned int)acc; acc >>= 32; | ||
688 | |||
689 | acc += rp[1]; acc += bp[9-8]; | ||
690 | acc += bp[10-8]; | ||
691 | acc -= bp[12-8]; | ||
692 | acc -= bp[13-8]; | ||
693 | acc -= bp[14-8]; | ||
694 | acc -= bp[15-8]; rp[1] = (unsigned int)acc; acc >>= 32; | ||
695 | |||
696 | acc += rp[2]; acc += bp[10-8]; | ||
697 | acc += bp[11-8]; | ||
698 | acc -= bp[13-8]; | ||
699 | acc -= bp[14-8]; | ||
700 | acc -= bp[15-8]; rp[2] = (unsigned int)acc; acc >>= 32; | ||
701 | |||
702 | acc += rp[3]; acc += bp[11-8]; | ||
703 | acc += bp[11-8]; | ||
704 | acc += bp[12-8]; | ||
705 | acc += bp[12-8]; | ||
706 | acc += bp[13-8]; | ||
707 | acc -= bp[15-8]; | ||
708 | acc -= bp[8-8]; | ||
709 | acc -= bp[9-8]; rp[3] = (unsigned int)acc; acc >>= 32; | ||
710 | |||
711 | acc += rp[4]; acc += bp[12-8]; | ||
712 | acc += bp[12-8]; | ||
713 | acc += bp[13-8]; | ||
714 | acc += bp[13-8]; | ||
715 | acc += bp[14-8]; | ||
716 | acc -= bp[9-8]; | ||
717 | acc -= bp[10-8]; rp[4] = (unsigned int)acc; acc >>= 32; | ||
718 | |||
719 | acc += rp[5]; acc += bp[13-8]; | ||
720 | acc += bp[13-8]; | ||
721 | acc += bp[14-8]; | ||
722 | acc += bp[14-8]; | ||
723 | acc += bp[15-8]; | ||
724 | acc -= bp[10-8]; | ||
725 | acc -= bp[11-8]; rp[5] = (unsigned int)acc; acc >>= 32; | ||
726 | |||
727 | acc += rp[6]; acc += bp[14-8]; | ||
728 | acc += bp[14-8]; | ||
729 | acc += bp[15-8]; | ||
730 | acc += bp[15-8]; | ||
731 | acc += bp[14-8]; | ||
732 | acc += bp[13-8]; | ||
733 | acc -= bp[8-8]; | ||
734 | acc -= bp[9-8]; rp[6] = (unsigned int)acc; acc >>= 32; | ||
735 | |||
736 | acc += rp[7]; acc += bp[15-8]; | ||
737 | acc += bp[15-8]; | ||
738 | acc += bp[15-8]; | ||
739 | acc += bp[8 -8]; | ||
740 | acc -= bp[10-8]; | ||
741 | acc -= bp[11-8]; | ||
742 | acc -= bp[12-8]; | ||
743 | acc -= bp[13-8]; rp[7] = (unsigned int)acc; | ||
744 | |||
745 | carry = (int)(acc>>32); | ||
746 | } | ||
747 | #else | ||
748 | { | ||
749 | BN_ULONG t_d[BN_NIST_256_TOP]; | ||
588 | 750 | ||
589 | /*S1*/ | 751 | /*S1*/ |
590 | nist_set_256(t_d, buf, 15, 14, 13, 12, 11, 0, 0, 0); | 752 | nist_set_256(t_d, buf.bn, 15, 14, 13, 12, 11, 0, 0, 0); |
591 | /*S2*/ | 753 | /*S2*/ |
592 | nist_set_256(c_d, buf, 0, 15, 14, 13, 12, 0, 0, 0); | 754 | nist_set_256(c_d, buf.bn, 0, 15, 14, 13, 12, 0, 0, 0); |
593 | carry = (int)bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP); | 755 | carry = (int)bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP); |
594 | /* left shift */ | 756 | /* left shift */ |
595 | { | 757 | { |
@@ -607,24 +769,26 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, | |||
607 | } | 769 | } |
608 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP); | 770 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP); |
609 | /*S3*/ | 771 | /*S3*/ |
610 | nist_set_256(t_d, buf, 15, 14, 0, 0, 0, 10, 9, 8); | 772 | nist_set_256(t_d, buf.bn, 15, 14, 0, 0, 0, 10, 9, 8); |
611 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP); | 773 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP); |
612 | /*S4*/ | 774 | /*S4*/ |
613 | nist_set_256(t_d, buf, 8, 13, 15, 14, 13, 11, 10, 9); | 775 | nist_set_256(t_d, buf.bn, 8, 13, 15, 14, 13, 11, 10, 9); |
614 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP); | 776 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP); |
615 | /*D1*/ | 777 | /*D1*/ |
616 | nist_set_256(t_d, buf, 10, 8, 0, 0, 0, 13, 12, 11); | 778 | nist_set_256(t_d, buf.bn, 10, 8, 0, 0, 0, 13, 12, 11); |
617 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP); | 779 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP); |
618 | /*D2*/ | 780 | /*D2*/ |
619 | nist_set_256(t_d, buf, 11, 9, 0, 0, 15, 14, 13, 12); | 781 | nist_set_256(t_d, buf.bn, 11, 9, 0, 0, 15, 14, 13, 12); |
620 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP); | 782 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP); |
621 | /*D3*/ | 783 | /*D3*/ |
622 | nist_set_256(t_d, buf, 12, 0, 10, 9, 8, 15, 14, 13); | 784 | nist_set_256(t_d, buf.bn, 12, 0, 10, 9, 8, 15, 14, 13); |
623 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP); | 785 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP); |
624 | /*D4*/ | 786 | /*D4*/ |
625 | nist_set_256(t_d, buf, 13, 0, 11, 10, 9, 0, 15, 14); | 787 | nist_set_256(t_d, buf.bn, 13, 0, 11, 10, 9, 0, 15, 14); |
626 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP); | 788 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP); |
627 | 789 | ||
790 | } | ||
791 | #endif | ||
628 | /* see BN_nist_mod_224 for explanation */ | 792 | /* see BN_nist_mod_224 for explanation */ |
629 | u.f = bn_sub_words; | 793 | u.f = bn_sub_words; |
630 | if (carry > 0) | 794 | if (carry > 0) |
@@ -672,9 +836,11 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, | |||
672 | int i, top = a->top; | 836 | int i, top = a->top; |
673 | int carry = 0; | 837 | int carry = 0; |
674 | register BN_ULONG *r_d, *a_d = a->d; | 838 | register BN_ULONG *r_d, *a_d = a->d; |
675 | BN_ULONG t_d[BN_NIST_384_TOP], | 839 | union { |
676 | buf[BN_NIST_384_TOP], | 840 | BN_ULONG bn[BN_NIST_384_TOP]; |
677 | c_d[BN_NIST_384_TOP], | 841 | unsigned int ui[BN_NIST_384_TOP*sizeof(BN_ULONG)/sizeof(unsigned int)]; |
842 | } buf; | ||
843 | BN_ULONG c_d[BN_NIST_384_TOP], | ||
678 | *res; | 844 | *res; |
679 | PTR_SIZE_INT mask; | 845 | PTR_SIZE_INT mask; |
680 | union { bn_addsub_f f; PTR_SIZE_INT p; } u; | 846 | union { bn_addsub_f f; PTR_SIZE_INT p; } u; |
@@ -709,10 +875,100 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, | |||
709 | else | 875 | else |
710 | r_d = a_d; | 876 | r_d = a_d; |
711 | 877 | ||
712 | nist_cp_bn_0(buf, a_d + BN_NIST_384_TOP, top - BN_NIST_384_TOP, BN_NIST_384_TOP); | 878 | nist_cp_bn_0(buf.bn, a_d + BN_NIST_384_TOP, top - BN_NIST_384_TOP, BN_NIST_384_TOP); |
879 | |||
880 | #if defined(NIST_INT64) | ||
881 | { | ||
882 | NIST_INT64 acc; /* accumulator */ | ||
883 | unsigned int *rp=(unsigned int *)r_d; | ||
884 | const unsigned int *bp=(const unsigned int *)buf.ui; | ||
885 | |||
886 | acc = rp[0]; acc += bp[12-12]; | ||
887 | acc += bp[21-12]; | ||
888 | acc += bp[20-12]; | ||
889 | acc -= bp[23-12]; rp[0] = (unsigned int)acc; acc >>= 32; | ||
890 | |||
891 | acc += rp[1]; acc += bp[13-12]; | ||
892 | acc += bp[22-12]; | ||
893 | acc += bp[23-12]; | ||
894 | acc -= bp[12-12]; | ||
895 | acc -= bp[20-12]; rp[1] = (unsigned int)acc; acc >>= 32; | ||
896 | |||
897 | acc += rp[2]; acc += bp[14-12]; | ||
898 | acc += bp[23-12]; | ||
899 | acc -= bp[13-12]; | ||
900 | acc -= bp[21-12]; rp[2] = (unsigned int)acc; acc >>= 32; | ||
901 | |||
902 | acc += rp[3]; acc += bp[15-12]; | ||
903 | acc += bp[12-12]; | ||
904 | acc += bp[20-12]; | ||
905 | acc += bp[21-12]; | ||
906 | acc -= bp[14-12]; | ||
907 | acc -= bp[22-12]; | ||
908 | acc -= bp[23-12]; rp[3] = (unsigned int)acc; acc >>= 32; | ||
909 | |||
910 | acc += rp[4]; acc += bp[21-12]; | ||
911 | acc += bp[21-12]; | ||
912 | acc += bp[16-12]; | ||
913 | acc += bp[13-12]; | ||
914 | acc += bp[12-12]; | ||
915 | acc += bp[20-12]; | ||
916 | acc += bp[22-12]; | ||
917 | acc -= bp[15-12]; | ||
918 | acc -= bp[23-12]; | ||
919 | acc -= bp[23-12]; rp[4] = (unsigned int)acc; acc >>= 32; | ||
920 | |||
921 | acc += rp[5]; acc += bp[22-12]; | ||
922 | acc += bp[22-12]; | ||
923 | acc += bp[17-12]; | ||
924 | acc += bp[14-12]; | ||
925 | acc += bp[13-12]; | ||
926 | acc += bp[21-12]; | ||
927 | acc += bp[23-12]; | ||
928 | acc -= bp[16-12]; rp[5] = (unsigned int)acc; acc >>= 32; | ||
929 | |||
930 | acc += rp[6]; acc += bp[23-12]; | ||
931 | acc += bp[23-12]; | ||
932 | acc += bp[18-12]; | ||
933 | acc += bp[15-12]; | ||
934 | acc += bp[14-12]; | ||
935 | acc += bp[22-12]; | ||
936 | acc -= bp[17-12]; rp[6] = (unsigned int)acc; acc >>= 32; | ||
937 | |||
938 | acc += rp[7]; acc += bp[19-12]; | ||
939 | acc += bp[16-12]; | ||
940 | acc += bp[15-12]; | ||
941 | acc += bp[23-12]; | ||
942 | acc -= bp[18-12]; rp[7] = (unsigned int)acc; acc >>= 32; | ||
943 | |||
944 | acc += rp[8]; acc += bp[20-12]; | ||
945 | acc += bp[17-12]; | ||
946 | acc += bp[16-12]; | ||
947 | acc -= bp[19-12]; rp[8] = (unsigned int)acc; acc >>= 32; | ||
948 | |||
949 | acc += rp[9]; acc += bp[21-12]; | ||
950 | acc += bp[18-12]; | ||
951 | acc += bp[17-12]; | ||
952 | acc -= bp[20-12]; rp[9] = (unsigned int)acc; acc >>= 32; | ||
953 | |||
954 | acc += rp[10]; acc += bp[22-12]; | ||
955 | acc += bp[19-12]; | ||
956 | acc += bp[18-12]; | ||
957 | acc -= bp[21-12]; rp[10] = (unsigned int)acc; acc >>= 32; | ||
958 | |||
959 | acc += rp[11]; acc += bp[23-12]; | ||
960 | acc += bp[20-12]; | ||
961 | acc += bp[19-12]; | ||
962 | acc -= bp[22-12]; rp[11] = (unsigned int)acc; | ||
963 | |||
964 | carry = (int)(acc>>32); | ||
965 | } | ||
966 | #else | ||
967 | { | ||
968 | BN_ULONG t_d[BN_NIST_384_TOP]; | ||
713 | 969 | ||
714 | /*S1*/ | 970 | /*S1*/ |
715 | nist_set_256(t_d, buf, 0, 0, 0, 0, 0, 23-4, 22-4, 21-4); | 971 | nist_set_256(t_d, buf.bn, 0, 0, 0, 0, 0, 23-4, 22-4, 21-4); |
716 | /* left shift */ | 972 | /* left shift */ |
717 | { | 973 | { |
718 | register BN_ULONG *ap,t,c; | 974 | register BN_ULONG *ap,t,c; |
@@ -729,29 +985,31 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field, | |||
729 | carry = (int)bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2), | 985 | carry = (int)bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2), |
730 | t_d, BN_NIST_256_TOP); | 986 | t_d, BN_NIST_256_TOP); |
731 | /*S2 */ | 987 | /*S2 */ |
732 | carry += (int)bn_add_words(r_d, r_d, buf, BN_NIST_384_TOP); | 988 | carry += (int)bn_add_words(r_d, r_d, buf.bn, BN_NIST_384_TOP); |
733 | /*S3*/ | 989 | /*S3*/ |
734 | nist_set_384(t_d,buf,20,19,18,17,16,15,14,13,12,23,22,21); | 990 | nist_set_384(t_d,buf.bn,20,19,18,17,16,15,14,13,12,23,22,21); |
735 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP); | 991 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP); |
736 | /*S4*/ | 992 | /*S4*/ |
737 | nist_set_384(t_d,buf,19,18,17,16,15,14,13,12,20,0,23,0); | 993 | nist_set_384(t_d,buf.bn,19,18,17,16,15,14,13,12,20,0,23,0); |
738 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP); | 994 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP); |
739 | /*S5*/ | 995 | /*S5*/ |
740 | nist_set_384(t_d, buf,0,0,0,0,23,22,21,20,0,0,0,0); | 996 | nist_set_384(t_d, buf.bn,0,0,0,0,23,22,21,20,0,0,0,0); |
741 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP); | 997 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP); |
742 | /*S6*/ | 998 | /*S6*/ |
743 | nist_set_384(t_d,buf,0,0,0,0,0,0,23,22,21,0,0,20); | 999 | nist_set_384(t_d,buf.bn,0,0,0,0,0,0,23,22,21,0,0,20); |
744 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP); | 1000 | carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP); |
745 | /*D1*/ | 1001 | /*D1*/ |
746 | nist_set_384(t_d,buf,22,21,20,19,18,17,16,15,14,13,12,23); | 1002 | nist_set_384(t_d,buf.bn,22,21,20,19,18,17,16,15,14,13,12,23); |
747 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP); | 1003 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP); |
748 | /*D2*/ | 1004 | /*D2*/ |
749 | nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,22,21,20,0); | 1005 | nist_set_384(t_d,buf.bn,0,0,0,0,0,0,0,23,22,21,20,0); |
750 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP); | 1006 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP); |
751 | /*D3*/ | 1007 | /*D3*/ |
752 | nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,23,0,0,0); | 1008 | nist_set_384(t_d,buf.bn,0,0,0,0,0,0,0,23,23,0,0,0); |
753 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP); | 1009 | carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP); |
754 | 1010 | ||
1011 | } | ||
1012 | #endif | ||
755 | /* see BN_nist_mod_224 for explanation */ | 1013 | /* see BN_nist_mod_224 for explanation */ |
756 | u.f = bn_sub_words; | 1014 | u.f = bn_sub_words; |
757 | if (carry > 0) | 1015 | if (carry > 0) |