Lines 107-119
Link Here
|
107 |
* (eay@cryptsoft.com). This product includes software written by Tim |
107 |
* (eay@cryptsoft.com). This product includes software written by Tim |
108 |
* Hudson (tjh@cryptsoft.com). |
108 |
* Hudson (tjh@cryptsoft.com). |
109 |
* |
109 |
* |
110 |
*/ |
110 |
*/ |
111 |
|
111 |
|
112 |
#include "cryptlib.h" |
112 |
#include "cryptlib.h" |
113 |
#include "constant_time_locl.h" |
113 |
#include "bn_lcl.h" |
114 |
#include "bn_lcl.h" |
114 |
|
115 |
|
115 |
/* maximum precomputation table size for *variable* sliding windows */ |
116 |
/* maximum precomputation table size for *variable* sliding windows */ |
|
|
117 |
#define TABLE_SIZE 32 |
116 |
#define TABLE_SIZE 32 |
118 |
|
117 |
|
119 |
/* this one works - simple but works */ |
118 |
/* this one works - simple but works */ |
Lines 521-599
int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, c
Link Here
|
521 |
* pattern as far as cache lines are concerned. The following functions are |
520 |
* pattern as far as cache lines are concerned. The following functions are |
522 |
* used to transfer a BIGNUM from/to that table. |
521 |
* used to transfer a BIGNUM from/to that table. |
523 |
*/ |
522 |
*/ |
524 |
|
523 |
|
525 |
static int MOD_EXP_CTIME_COPY_TO_PREBUF(BIGNUM *b, int top, |
524 |
static int MOD_EXP_CTIME_COPY_TO_PREBUF(BIGNUM *b, int top, |
526 |
unsigned char *buf, int idx, |
525 |
unsigned char *buf, int idx, |
527 |
int window) |
526 |
int width) |
528 |
{ |
527 |
{ |
529 |
int i, j; |
528 |
size_t i, j; |
530 |
int width = 1 << window; |
529 |
|
531 |
BN_ULONG *table = (BN_ULONG *)buf; |
530 |
if (bn_wexpand(b, top) == NULL) |
532 |
|
531 |
return 0; |
533 |
if (bn_wexpand(b, top) == NULL) |
|
|
534 |
return 0; |
535 |
while (b->top < top) { |
532 |
while (b->top < top) { |
536 |
b->d[b->top++] = 0; |
533 |
b->d[b->top++] = 0; |
537 |
} |
534 |
} |
538 |
|
535 |
|
539 |
for (i = 0, j = idx; i < top; i++, j += width) { |
536 |
for (i = 0, j = idx; i < top * sizeof b->d[0]; i++, j += width) { |
540 |
table[j] = b->d[i]; |
537 |
buf[j] = ((unsigned char *)b->d)[i]; |
541 |
} |
538 |
} |
542 |
|
539 |
|
543 |
bn_correct_top(b); |
540 |
bn_correct_top(b); |
544 |
return 1; |
541 |
return 1; |
545 |
} |
542 |
} |
546 |
|
543 |
|
547 |
static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top, |
544 |
static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top, |
548 |
unsigned char *buf, int idx, |
545 |
unsigned char *buf, int idx, |
549 |
int window) |
546 |
int width) |
550 |
{ |
547 |
{ |
551 |
int i, j; |
548 |
size_t i, j; |
552 |
int width = 1 << window; |
549 |
|
553 |
volatile BN_ULONG *table = (volatile BN_ULONG *)buf; |
550 |
if (bn_wexpand(b, top) == NULL) |
554 |
|
551 |
return 0; |
555 |
if (bn_wexpand(b, top) == NULL) |
552 |
|
556 |
return 0; |
553 |
for (i = 0, j = idx; i < top * sizeof b->d[0]; i++, j += width) { |
557 |
|
554 |
((unsigned char *)b->d)[i] = buf[j]; |
558 |
if (window <= 3) { |
555 |
} |
559 |
for (i = 0; i < top; i++, table += width) { |
556 |
|
560 |
BN_ULONG acc = 0; |
557 |
b->top = top; |
561 |
|
|
|
562 |
for (j = 0; j < width; j++) { |
563 |
acc |= table[j] & |
564 |
((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1)); |
565 |
} |
566 |
|
567 |
b->d[i] = acc; |
568 |
} |
569 |
} else { |
570 |
int xstride = 1 << (window - 2); |
571 |
BN_ULONG y0, y1, y2, y3; |
572 |
|
573 |
i = idx >> (window - 2); /* equivalent of idx / xstride */ |
574 |
idx &= xstride - 1; /* equivalent of idx % xstride */ |
575 |
|
576 |
y0 = (BN_ULONG)0 - (constant_time_eq_int(i,0)&1); |
577 |
y1 = (BN_ULONG)0 - (constant_time_eq_int(i,1)&1); |
578 |
y2 = (BN_ULONG)0 - (constant_time_eq_int(i,2)&1); |
579 |
y3 = (BN_ULONG)0 - (constant_time_eq_int(i,3)&1); |
580 |
|
581 |
for (i = 0; i < top; i++, table += width) { |
582 |
BN_ULONG acc = 0; |
583 |
|
584 |
for (j = 0; j < xstride; j++) { |
585 |
acc |= ( (table[j + 0 * xstride] & y0) | |
586 |
(table[j + 1 * xstride] & y1) | |
587 |
(table[j + 2 * xstride] & y2) | |
588 |
(table[j + 3 * xstride] & y3) ) |
589 |
& ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1)); |
590 |
} |
591 |
|
592 |
b->d[i] = acc; |
593 |
} |
594 |
} |
595 |
|
596 |
b->top = top; |
597 |
bn_correct_top(b); |
558 |
bn_correct_top(b); |
598 |
return 1; |
559 |
return 1; |
599 |
} |
560 |
} |
Lines 684-696
int BN_mod_exp_mont_consttime(BIGNUM *rr, const BI
Link Here
|
684 |
/* |
645 |
/* |
685 |
* Initialize the intermediate result. Do this early to save double |
646 |
* Initialize the intermediate result. Do this early to save double |
686 |
* conversion, once each for a^0 and intermediate result. |
647 |
* conversion, once each for a^0 and intermediate result. |
687 |
*/ |
648 |
*/ |
688 |
if (!BN_to_montgomery(r, BN_value_one(), mont, ctx)) |
649 |
if (!BN_to_montgomery(r, BN_value_one(), mont, ctx)) |
689 |
goto err; |
650 |
goto err; |
690 |
if (!MOD_EXP_CTIME_COPY_TO_PREBUF(r, top, powerbuf, 0, window)) |
651 |
if (!MOD_EXP_CTIME_COPY_TO_PREBUF(r, top, powerbuf, 0, numPowers)) |
691 |
goto err; |
652 |
goto err; |
692 |
|
653 |
|
693 |
/* Initialize computeTemp as a^1 with montgomery precalcs */ |
654 |
/* Initialize computeTemp as a^1 with montgomery precalcs */ |
694 |
computeTemp = BN_CTX_get(ctx); |
655 |
computeTemp = BN_CTX_get(ctx); |
695 |
am = BN_CTX_get(ctx); |
656 |
am = BN_CTX_get(ctx); |
696 |
if (computeTemp == NULL || am == NULL) |
657 |
if (computeTemp == NULL || am == NULL) |
Lines 703-715
int BN_mod_exp_mont_consttime(BIGNUM *rr, const BI
Link Here
|
703 |
} else |
664 |
} else |
704 |
aa = a; |
665 |
aa = a; |
705 |
if (!BN_to_montgomery(am, aa, mont, ctx)) |
666 |
if (!BN_to_montgomery(am, aa, mont, ctx)) |
706 |
goto err; |
667 |
goto err; |
707 |
if (!BN_copy(computeTemp, am)) |
668 |
if (!BN_copy(computeTemp, am)) |
708 |
goto err; |
669 |
goto err; |
709 |
if (!MOD_EXP_CTIME_COPY_TO_PREBUF(am, top, powerbuf, 1, window)) |
670 |
if (!MOD_EXP_CTIME_COPY_TO_PREBUF(am, top, powerbuf, 1, numPowers)) |
710 |
goto err; |
671 |
goto err; |
711 |
|
672 |
|
712 |
/* |
673 |
/* |
713 |
* If the window size is greater than 1, then calculate |
674 |
* If the window size is greater than 1, then calculate |
714 |
* val[i=2..2^winsize-1]. Powers are computed as a*a^(i-1) (even powers |
675 |
* val[i=2..2^winsize-1]. Powers are computed as a*a^(i-1) (even powers |
715 |
* could instead be computed as (a^(i/2))^2 to use the slight performance |
676 |
* could instead be computed as (a^(i/2))^2 to use the slight performance |
Lines 718-731
int BN_mod_exp_mont_consttime(BIGNUM *rr, const BI
Link Here
|
718 |
if (window > 1) { |
679 |
if (window > 1) { |
719 |
for (i = 2; i < numPowers; i++) { |
680 |
for (i = 2; i < numPowers; i++) { |
720 |
/* Calculate a^i = a^(i-1) * a */ |
681 |
/* Calculate a^i = a^(i-1) * a */ |
721 |
if (!BN_mod_mul_montgomery |
682 |
if (!BN_mod_mul_montgomery |
722 |
(computeTemp, am, computeTemp, mont, ctx)) |
683 |
(computeTemp, am, computeTemp, mont, ctx)) |
723 |
goto err; |
684 |
goto err; |
724 |
if (!MOD_EXP_CTIME_COPY_TO_PREBUF(computeTemp, top, powerbuf, i, |
685 |
if (!MOD_EXP_CTIME_COPY_TO_PREBUF |
725 |
window)) |
686 |
(computeTemp, top, powerbuf, i, numPowers)) |
726 |
goto err; |
687 |
goto err; |
727 |
} |
688 |
} |
728 |
} |
689 |
} |
729 |
|
690 |
|
730 |
/* |
691 |
/* |
731 |
* Adjust the number of bits up to a multiple of the window size. If the |
692 |
* Adjust the number of bits up to a multiple of the window size. If the |