summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn
diff options
context:
space:
mode:
authormiod <>2014-04-13 15:25:35 +0000
committermiod <>2014-04-13 15:25:35 +0000
commit1fb5cf10c3e597dbb1ecc4dd423bba727fd7721a (patch)
tree74f4ff344980894c7c9ceeab9b81176ac7572566 /src/lib/libcrypto/bn
parent92349eb53934e1b3e9b807e603d45417a6320d21 (diff)
downloadopenbsd-1fb5cf10c3e597dbb1ecc4dd423bba727fd7721a.tar.gz
openbsd-1fb5cf10c3e597dbb1ecc4dd423bba727fd7721a.tar.bz2
openbsd-1fb5cf10c3e597dbb1ecc4dd423bba727fd7721a.zip
Merge conflicts; remove MacOS, Netware, OS/2, VMS and Windows build machinery.
Diffstat (limited to 'src/lib/libcrypto/bn')
-rw-r--r--src/lib/libcrypto/bn/bn.h11
-rw-r--r--src/lib/libcrypto/bn/bn_div.c2
-rw-r--r--src/lib/libcrypto/bn/bn_gcd.c1
-rw-r--r--src/lib/libcrypto/bn/bn_lcl.h11
-rw-r--r--src/lib/libcrypto/bn/bn_lib.c52
-rw-r--r--src/lib/libcrypto/bn/bn_nist.c55
6 files changed, 106 insertions, 26 deletions
diff --git a/src/lib/libcrypto/bn/bn.h b/src/lib/libcrypto/bn/bn.h
index f34248ec4f..21a1a3fe35 100644
--- a/src/lib/libcrypto/bn/bn.h
+++ b/src/lib/libcrypto/bn/bn.h
@@ -538,6 +538,8 @@ BIGNUM *BN_mod_inverse(BIGNUM *ret,
538BIGNUM *BN_mod_sqrt(BIGNUM *ret, 538BIGNUM *BN_mod_sqrt(BIGNUM *ret,
539 const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); 539 const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx);
540 540
541void BN_consttime_swap(BN_ULONG swap, BIGNUM *a, BIGNUM *b, int nwords);
542
541/* Deprecated versions */ 543/* Deprecated versions */
542#ifndef OPENSSL_NO_DEPRECATED 544#ifndef OPENSSL_NO_DEPRECATED
543BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe, 545BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe,
@@ -774,11 +776,20 @@ int RAND_pseudo_bytes(unsigned char *buf,int num);
774 776
775#define bn_fix_top(a) bn_check_top(a) 777#define bn_fix_top(a) bn_check_top(a)
776 778
779#define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
780#define bn_wcheck_size(bn, words) \
781 do { \
782 const BIGNUM *_bnum2 = (bn); \
783 assert(words <= (_bnum2)->dmax && words >= (_bnum2)->top); \
784 } while(0)
785
777#else /* !BN_DEBUG */ 786#else /* !BN_DEBUG */
778 787
779#define bn_pollute(a) 788#define bn_pollute(a)
780#define bn_check_top(a) 789#define bn_check_top(a)
781#define bn_fix_top(a) bn_correct_top(a) 790#define bn_fix_top(a) bn_correct_top(a)
791#define bn_check_size(bn, bits)
792#define bn_wcheck_size(bn, words)
782 793
783#endif 794#endif
784 795
diff --git a/src/lib/libcrypto/bn/bn_div.c b/src/lib/libcrypto/bn/bn_div.c
index 52b3304293..7b2403185e 100644
--- a/src/lib/libcrypto/bn/bn_div.c
+++ b/src/lib/libcrypto/bn/bn_div.c
@@ -141,6 +141,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
141 * 141 *
142 * <appro@fy.chalmers.se> 142 * <appro@fy.chalmers.se>
143 */ 143 */
144#undef bn_div_words
144# define bn_div_words(n0,n1,d0) \ 145# define bn_div_words(n0,n1,d0) \
145 ({ asm volatile ( \ 146 ({ asm volatile ( \
146 "divl %4" \ 147 "divl %4" \
@@ -155,6 +156,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
155 * Same story here, but it's 128-bit by 64-bit division. Wow! 156 * Same story here, but it's 128-bit by 64-bit division. Wow!
156 * <appro@fy.chalmers.se> 157 * <appro@fy.chalmers.se>
157 */ 158 */
159# undef bn_div_words
158# define bn_div_words(n0,n1,d0) \ 160# define bn_div_words(n0,n1,d0) \
159 ({ asm volatile ( \ 161 ({ asm volatile ( \
160 "divq %4" \ 162 "divq %4" \
diff --git a/src/lib/libcrypto/bn/bn_gcd.c b/src/lib/libcrypto/bn/bn_gcd.c
index 4a352119ba..a808f53178 100644
--- a/src/lib/libcrypto/bn/bn_gcd.c
+++ b/src/lib/libcrypto/bn/bn_gcd.c
@@ -205,6 +205,7 @@ err:
205/* solves ax == 1 (mod n) */ 205/* solves ax == 1 (mod n) */
206static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in, 206static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
207 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); 207 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
208
208BIGNUM *BN_mod_inverse(BIGNUM *in, 209BIGNUM *BN_mod_inverse(BIGNUM *in,
209 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) 210 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
210 { 211 {
diff --git a/src/lib/libcrypto/bn/bn_lcl.h b/src/lib/libcrypto/bn/bn_lcl.h
index eecfd8cc99..817c773b65 100644
--- a/src/lib/libcrypto/bn/bn_lcl.h
+++ b/src/lib/libcrypto/bn/bn_lcl.h
@@ -282,16 +282,23 @@ extern "C" {
282# endif 282# endif
283# elif defined(__mips) && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)) 283# elif defined(__mips) && (defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG))
284# if defined(__GNUC__) && __GNUC__>=2 284# if defined(__GNUC__) && __GNUC__>=2
285# define BN_UMULT_HIGH(a,b) ({ \ 285# if __GNUC__>=4 && __GNUC_MINOR__>=4 /* "h" constraint is no more since 4.4 */
286# define BN_UMULT_HIGH(a,b) (((__uint128_t)(a)*(b))>>64)
287# define BN_UMULT_LOHI(low,high,a,b) ({ \
288 __uint128_t ret=(__uint128_t)(a)*(b); \
289 (high)=ret>>64; (low)=ret; })
290# else
291# define BN_UMULT_HIGH(a,b) ({ \
286 register BN_ULONG ret; \ 292 register BN_ULONG ret; \
287 asm ("dmultu %1,%2" \ 293 asm ("dmultu %1,%2" \
288 : "=h"(ret) \ 294 : "=h"(ret) \
289 : "r"(a), "r"(b) : "l"); \ 295 : "r"(a), "r"(b) : "l"); \
290 ret; }) 296 ret; })
291# define BN_UMULT_LOHI(low,high,a,b) \ 297# define BN_UMULT_LOHI(low,high,a,b)\
292 asm ("dmultu %2,%3" \ 298 asm ("dmultu %2,%3" \
293 : "=l"(low),"=h"(high) \ 299 : "=l"(low),"=h"(high) \
294 : "r"(a), "r"(b)); 300 : "r"(a), "r"(b));
301# endif
295# endif 302# endif
296# endif /* cpu */ 303# endif /* cpu */
297#endif /* OPENSSL_NO_ASM */ 304#endif /* OPENSSL_NO_ASM */
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c
index 7a5676de69..5461e6ee7d 100644
--- a/src/lib/libcrypto/bn/bn_lib.c
+++ b/src/lib/libcrypto/bn/bn_lib.c
@@ -824,3 +824,55 @@ int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,
824 } 824 }
825 return bn_cmp_words(a,b,cl); 825 return bn_cmp_words(a,b,cl);
826 } 826 }
827
828/*
829 * Constant-time conditional swap of a and b.
830 * a and b are swapped if condition is not 0. The code assumes that at most one bit of condition is set.
831 * nwords is the number of words to swap. The code assumes that at least nwords are allocated in both a and b,
832 * and that no more than nwords are used by either a or b.
833 * a and b cannot be the same number
834 */
835void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
836 {
837 BN_ULONG t;
838 int i;
839
840 bn_wcheck_size(a, nwords);
841 bn_wcheck_size(b, nwords);
842
843 assert(a != b);
844 assert((condition & (condition - 1)) == 0);
845 assert(sizeof(BN_ULONG) >= sizeof(int));
846
847 condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1;
848
849 t = (a->top^b->top) & condition;
850 a->top ^= t;
851 b->top ^= t;
852
853#define BN_CONSTTIME_SWAP(ind) \
854 do { \
855 t = (a->d[ind] ^ b->d[ind]) & condition; \
856 a->d[ind] ^= t; \
857 b->d[ind] ^= t; \
858 } while (0)
859
860
861 switch (nwords) {
862 default:
863 for (i = 10; i < nwords; i++)
864 BN_CONSTTIME_SWAP(i);
865 /* Fallthrough */
866 case 10: BN_CONSTTIME_SWAP(9); /* Fallthrough */
867 case 9: BN_CONSTTIME_SWAP(8); /* Fallthrough */
868 case 8: BN_CONSTTIME_SWAP(7); /* Fallthrough */
869 case 7: BN_CONSTTIME_SWAP(6); /* Fallthrough */
870 case 6: BN_CONSTTIME_SWAP(5); /* Fallthrough */
871 case 5: BN_CONSTTIME_SWAP(4); /* Fallthrough */
872 case 4: BN_CONSTTIME_SWAP(3); /* Fallthrough */
873 case 3: BN_CONSTTIME_SWAP(2); /* Fallthrough */
874 case 2: BN_CONSTTIME_SWAP(1); /* Fallthrough */
875 case 1: BN_CONSTTIME_SWAP(0);
876 }
877#undef BN_CONSTTIME_SWAP
878}
diff --git a/src/lib/libcrypto/bn/bn_nist.c b/src/lib/libcrypto/bn/bn_nist.c
index 43caee4770..e22968d4a3 100644
--- a/src/lib/libcrypto/bn/bn_nist.c
+++ b/src/lib/libcrypto/bn/bn_nist.c
@@ -286,26 +286,25 @@ const BIGNUM *BN_get0_nist_prime_521(void)
286 } 286 }
287 287
288 288
289static void nist_cp_bn_0(BN_ULONG *buf, BN_ULONG *a, int top, int max) 289static void nist_cp_bn_0(BN_ULONG *dst, const BN_ULONG *src, int top, int max)
290 { 290 {
291 int i; 291 int i;
292 BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
293 292
294#ifdef BN_DEBUG 293#ifdef BN_DEBUG
295 OPENSSL_assert(top <= max); 294 OPENSSL_assert(top <= max);
296#endif 295#endif
297 for (i = (top); i != 0; i--) 296 for (i = 0; i < top; i++)
298 *_tmp1++ = *_tmp2++; 297 dst[i] = src[i];
299 for (i = (max) - (top); i != 0; i--) 298 for (; i < max; i++)
300 *_tmp1++ = (BN_ULONG) 0; 299 dst[i] = 0;
301 } 300 }
302 301
303static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top) 302static void nist_cp_bn(BN_ULONG *dst, const BN_ULONG *src, int top)
304 { 303 {
305 int i; 304 int i;
306 BN_ULONG *_tmp1 = (buf), *_tmp2 = (a); 305
307 for (i = (top); i != 0; i--) 306 for (i = 0; i < top; i++)
308 *_tmp1++ = *_tmp2++; 307 dst[i] = src[i];
309 } 308 }
310 309
311#if BN_BITS2 == 64 310#if BN_BITS2 == 64
@@ -451,8 +450,9 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
451 */ 450 */
452 mask = 0-(PTR_SIZE_INT)bn_sub_words(c_d,r_d,_nist_p_192[0],BN_NIST_192_TOP); 451 mask = 0-(PTR_SIZE_INT)bn_sub_words(c_d,r_d,_nist_p_192[0],BN_NIST_192_TOP);
453 mask &= 0-(PTR_SIZE_INT)carry; 452 mask &= 0-(PTR_SIZE_INT)carry;
453 res = c_d;
454 res = (BN_ULONG *) 454 res = (BN_ULONG *)
455 (((PTR_SIZE_INT)c_d&~mask) | ((PTR_SIZE_INT)r_d&mask)); 455 (((PTR_SIZE_INT)res&~mask) | ((PTR_SIZE_INT)r_d&mask));
456 nist_cp_bn(r_d, res, BN_NIST_192_TOP); 456 nist_cp_bn(r_d, res, BN_NIST_192_TOP);
457 r->top = BN_NIST_192_TOP; 457 r->top = BN_NIST_192_TOP;
458 bn_correct_top(r); 458 bn_correct_top(r);
@@ -479,8 +479,11 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
479 int top = a->top, i; 479 int top = a->top, i;
480 int carry; 480 int carry;
481 BN_ULONG *r_d, *a_d = a->d; 481 BN_ULONG *r_d, *a_d = a->d;
482 BN_ULONG buf[BN_NIST_224_TOP], 482 union {
483 c_d[BN_NIST_224_TOP], 483 BN_ULONG bn[BN_NIST_224_TOP];
484 unsigned int ui[BN_NIST_224_TOP*sizeof(BN_ULONG)/sizeof(unsigned int)];
485 } buf;
486 BN_ULONG c_d[BN_NIST_224_TOP],
484 *res; 487 *res;
485 PTR_SIZE_INT mask; 488 PTR_SIZE_INT mask;
486 union { bn_addsub_f f; PTR_SIZE_INT p; } u; 489 union { bn_addsub_f f; PTR_SIZE_INT p; } u;
@@ -519,18 +522,18 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
519 /* copy upper 256 bits of 448 bit number ... */ 522 /* copy upper 256 bits of 448 bit number ... */
520 nist_cp_bn_0(c_d, a_d + (BN_NIST_224_TOP-1), top - (BN_NIST_224_TOP-1), BN_NIST_224_TOP); 523 nist_cp_bn_0(c_d, a_d + (BN_NIST_224_TOP-1), top - (BN_NIST_224_TOP-1), BN_NIST_224_TOP);
521 /* ... and right shift by 32 to obtain upper 224 bits */ 524 /* ... and right shift by 32 to obtain upper 224 bits */
522 nist_set_224(buf, c_d, 14, 13, 12, 11, 10, 9, 8); 525 nist_set_224(buf.bn, c_d, 14, 13, 12, 11, 10, 9, 8);
523 /* truncate lower part to 224 bits too */ 526 /* truncate lower part to 224 bits too */
524 r_d[BN_NIST_224_TOP-1] &= BN_MASK2l; 527 r_d[BN_NIST_224_TOP-1] &= BN_MASK2l;
525#else 528#else
526 nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP); 529 nist_cp_bn_0(buf.bn, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP);
527#endif 530#endif
528 531
529#if defined(NIST_INT64) && BN_BITS2!=64 532#if defined(NIST_INT64) && BN_BITS2!=64
530 { 533 {
531 NIST_INT64 acc; /* accumulator */ 534 NIST_INT64 acc; /* accumulator */
532 unsigned int *rp=(unsigned int *)r_d; 535 unsigned int *rp=(unsigned int *)r_d;
533 const unsigned int *bp=(const unsigned int *)buf; 536 const unsigned int *bp=(const unsigned int *)buf.ui;
534 537
535 acc = rp[0]; acc -= bp[7-7]; 538 acc = rp[0]; acc -= bp[7-7];
536 acc -= bp[11-7]; rp[0] = (unsigned int)acc; acc >>= 32; 539 acc -= bp[11-7]; rp[0] = (unsigned int)acc; acc >>= 32;
@@ -565,13 +568,13 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
565 { 568 {
566 BN_ULONG t_d[BN_NIST_224_TOP]; 569 BN_ULONG t_d[BN_NIST_224_TOP];
567 570
568 nist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0); 571 nist_set_224(t_d, buf.bn, 10, 9, 8, 7, 0, 0, 0);
569 carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP); 572 carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
570 nist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0); 573 nist_set_224(t_d, buf.bn, 0, 13, 12, 11, 0, 0, 0);
571 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP); 574 carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
572 nist_set_224(t_d, buf, 13, 12, 11, 10, 9, 8, 7); 575 nist_set_224(t_d, buf.bn, 13, 12, 11, 10, 9, 8, 7);
573 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP); 576 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
574 nist_set_224(t_d, buf, 0, 0, 0, 0, 13, 12, 11); 577 nist_set_224(t_d, buf.bn, 0, 0, 0, 0, 13, 12, 11);
575 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP); 578 carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
576 579
577#if BN_BITS2==64 580#if BN_BITS2==64
@@ -606,7 +609,8 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
606 /* otherwise it's effectively same as in BN_nist_mod_192... */ 609 /* otherwise it's effectively same as in BN_nist_mod_192... */
607 mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_224[0],BN_NIST_224_TOP); 610 mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_224[0],BN_NIST_224_TOP);
608 mask &= 0-(PTR_SIZE_INT)carry; 611 mask &= 0-(PTR_SIZE_INT)carry;
609 res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) | 612 res = c_d;
613 res = (BN_ULONG *)(((PTR_SIZE_INT)res&~mask) |
610 ((PTR_SIZE_INT)r_d&mask)); 614 ((PTR_SIZE_INT)r_d&mask));
611 nist_cp_bn(r_d, res, BN_NIST_224_TOP); 615 nist_cp_bn(r_d, res, BN_NIST_224_TOP);
612 r->top = BN_NIST_224_TOP; 616 r->top = BN_NIST_224_TOP;
@@ -805,7 +809,8 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
805 809
806 mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_256[0],BN_NIST_256_TOP); 810 mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_256[0],BN_NIST_256_TOP);
807 mask &= 0-(PTR_SIZE_INT)carry; 811 mask &= 0-(PTR_SIZE_INT)carry;
808 res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) | 812 res = c_d;
813 res = (BN_ULONG *)(((PTR_SIZE_INT)res&~mask) |
809 ((PTR_SIZE_INT)r_d&mask)); 814 ((PTR_SIZE_INT)r_d&mask));
810 nist_cp_bn(r_d, res, BN_NIST_256_TOP); 815 nist_cp_bn(r_d, res, BN_NIST_256_TOP);
811 r->top = BN_NIST_256_TOP; 816 r->top = BN_NIST_256_TOP;
@@ -1026,7 +1031,8 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
1026 1031
1027 mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_384[0],BN_NIST_384_TOP); 1032 mask = 0-(PTR_SIZE_INT)(*u.f)(c_d,r_d,_nist_p_384[0],BN_NIST_384_TOP);
1028 mask &= 0-(PTR_SIZE_INT)carry; 1033 mask &= 0-(PTR_SIZE_INT)carry;
1029 res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) | 1034 res = c_d;
1035 res = (BN_ULONG *)(((PTR_SIZE_INT)res&~mask) |
1030 ((PTR_SIZE_INT)r_d&mask)); 1036 ((PTR_SIZE_INT)r_d&mask));
1031 nist_cp_bn(r_d, res, BN_NIST_384_TOP); 1037 nist_cp_bn(r_d, res, BN_NIST_384_TOP);
1032 r->top = BN_NIST_384_TOP; 1038 r->top = BN_NIST_384_TOP;
@@ -1092,7 +1098,8 @@ int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
1092 1098
1093 bn_add_words(r_d,r_d,t_d,BN_NIST_521_TOP); 1099 bn_add_words(r_d,r_d,t_d,BN_NIST_521_TOP);
1094 mask = 0-(PTR_SIZE_INT)bn_sub_words(t_d,r_d,_nist_p_521,BN_NIST_521_TOP); 1100 mask = 0-(PTR_SIZE_INT)bn_sub_words(t_d,r_d,_nist_p_521,BN_NIST_521_TOP);
1095 res = (BN_ULONG *)(((PTR_SIZE_INT)t_d&~mask) | 1101 res = t_d;
1102 res = (BN_ULONG *)(((PTR_SIZE_INT)res&~mask) |
1096 ((PTR_SIZE_INT)r_d&mask)); 1103 ((PTR_SIZE_INT)r_d&mask));
1097 nist_cp_bn(r_d,res,BN_NIST_521_TOP); 1104 nist_cp_bn(r_d,res,BN_NIST_521_TOP);
1098 r->top = BN_NIST_521_TOP; 1105 r->top = BN_NIST_521_TOP;