OpenSSL 1.1.1-pre2

This commit is contained in:
Hakase
2018-04-07 17:29:40 +09:00
parent 82a44d2483
commit bbac8ca55d
17755 changed files with 221242 additions and 98415 deletions
+54 -139
View File
@@ -65,15 +65,15 @@ void BN_set_params(int mult, int high, int low, int mont)
int BN_get_params(int which)
{
if (which == 0)
return (bn_limit_bits);
return bn_limit_bits;
else if (which == 1)
return (bn_limit_bits_high);
return bn_limit_bits_high;
else if (which == 2)
return (bn_limit_bits_low);
return bn_limit_bits_low;
else if (which == 3)
return (bn_limit_bits_mont);
return bn_limit_bits_mont;
else
return (0);
return 0;
}
#endif
@@ -83,7 +83,7 @@ const BIGNUM *BN_value_one(void)
static const BIGNUM const_one =
{ (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
return (&const_one);
return &const_one;
}
int BN_num_bits_word(BN_ULONG l)
@@ -152,37 +152,26 @@ static void bn_free_d(BIGNUM *a)
void BN_clear_free(BIGNUM *a)
{
int i;
if (a == NULL)
return;
bn_check_top(a);
if (a->d != NULL) {
if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {
OPENSSL_cleanse(a->d, a->dmax * sizeof(a->d[0]));
if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
bn_free_d(a);
bn_free_d(a);
}
i = BN_get_flags(a, BN_FLG_MALLOCED);
OPENSSL_cleanse(a, sizeof(*a));
if (i)
if (BN_get_flags(a, BN_FLG_MALLOCED)) {
OPENSSL_cleanse(a, sizeof(*a));
OPENSSL_free(a);
}
}
void BN_free(BIGNUM *a)
{
if (a == NULL)
return;
bn_check_top(a);
if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
bn_free_d(a);
if (a->flags & BN_FLG_MALLOCED)
OPENSSL_free(a);
else {
#if OPENSSL_API_COMPAT < 0x00908000L
a->flags |= BN_FLG_FREE;
#endif
a->d = NULL;
}
}
void bn_init(BIGNUM *a)
@@ -199,11 +188,11 @@ BIGNUM *BN_new(void)
if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);
return (NULL);
return NULL;
}
ret->flags = BN_FLG_MALLOCED;
bn_check_top(ret);
return (ret);
return ret;
}
BIGNUM *BN_secure_new(void)
@@ -211,16 +200,14 @@ BIGNUM *BN_new(void)
BIGNUM *ret = BN_new();
if (ret != NULL)
ret->flags |= BN_FLG_SECURE;
return (ret);
return ret;
}
/* This is used by bn_expand2() */
/* The caller MUST check that words > b->dmax before calling this */
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *A, *a = NULL;
const BN_ULONG *B;
int i;
BN_ULONG *a = NULL;
bn_check_top(b);
@@ -230,62 +217,22 @@ static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = A = OPENSSL_secure_zalloc(words * sizeof(*a));
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = A = OPENSSL_zalloc(words * sizeof(*a));
if (A == NULL) {
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
return NULL;
}
#if 1
B = b->d;
/* Check if the previous number needs to be copied */
if (B != NULL) {
for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
/*
* The fact that the loop is unrolled
* 4-wise is a tribute to Intel. It's
* the one that doesn't have enough
* registers to accommodate more data.
* I'd unroll it 8-wise otherwise:-)
*
* <appro@fy.chalmers.se>
*/
BN_ULONG a0, a1, a2, a3;
a0 = B[0];
a1 = B[1];
a2 = B[2];
a3 = B[3];
A[0] = a0;
A[1] = a1;
A[2] = a2;
A[3] = a3;
}
switch (b->top & 3) {
case 3:
A[2] = B[2];
/* fall thru */
case 2:
A[1] = B[1];
/* fall thru */
case 1:
A[0] = B[0];
/* fall thru */
case 0:
/* Without the "case 0" some old optimizers got this wrong. */
;
}
}
#else
memset(A, 0, sizeof(*A) * words);
memcpy(A, b->d, sizeof(b->d[0]) * b->top);
#endif
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return (a);
return a;
}
/*
@@ -337,52 +284,20 @@ BIGNUM *BN_dup(const BIGNUM *a)
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
int i;
BN_ULONG *A;
const BN_ULONG *B;
bn_check_top(b);
if (a == b)
return (a);
return a;
if (bn_wexpand(a, b->top) == NULL)
return (NULL);
return NULL;
#if 1
A = a->d;
B = b->d;
for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
BN_ULONG a0, a1, a2, a3;
a0 = B[0];
a1 = B[1];
a2 = B[2];
a3 = B[3];
A[0] = a0;
A[1] = a1;
A[2] = a2;
A[3] = a3;
}
/* ultrix cc workaround, see comments in bn_expand_internal */
switch (b->top & 3) {
case 3:
A[2] = B[2];
/* fall thru */
case 2:
A[1] = B[1];
/* fall thru */
case 1:
A[0] = B[0];
/* fall thru */
case 0:;
}
#else
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
#endif
if (b->top > 0)
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
a->top = b->top;
a->neg = b->neg;
bn_check_top(a);
return (a);
return a;
}
void BN_swap(BIGNUM *a, BIGNUM *b)
@@ -443,12 +358,12 @@ int BN_set_word(BIGNUM *a, BN_ULONG w)
{
bn_check_top(a);
if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
return (0);
return 0;
a->neg = 0;
a->d[0] = w;
a->top = (w ? 1 : 0);
bn_check_top(a);
return (1);
return 1;
}
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
@@ -461,7 +376,7 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
if (ret == NULL)
ret = bn = BN_new();
if (ret == NULL)
return (NULL);
return NULL;
bn_check_top(ret);
/* Skip leading zero's. */
for ( ; len > 0 && *s == 0; s++, len--)
@@ -469,7 +384,7 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
n = len;
if (n == 0) {
ret->top = 0;
return (ret);
return ret;
}
i = ((n - 1) / BN_BYTES) + 1;
m = ((n - 1) % (BN_BYTES));
@@ -493,7 +408,7 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
* bit set (-ve number)
*/
bn_correct_top(ret);
return (ret);
return ret;
}
/* ignore negative */
@@ -542,7 +457,7 @@ BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
if (ret == NULL)
ret = bn = BN_new();
if (ret == NULL)
return (NULL);
return NULL;
bn_check_top(ret);
s += len;
/* Skip trailing zeroes. */
@@ -609,7 +524,7 @@ int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
i = a->top - b->top;
if (i != 0)
return (i);
return i;
ap = a->d;
bp = b->d;
for (i = a->top - 1; i >= 0; i--) {
@@ -618,7 +533,7 @@ int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
if (t1 != t2)
return ((t1 > t2) ? 1 : -1);
}
return (0);
return 0;
}
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
@@ -629,11 +544,11 @@ int BN_cmp(const BIGNUM *a, const BIGNUM *b)
if ((a == NULL) || (b == NULL)) {
if (a != NULL)
return (-1);
return -1;
else if (b != NULL)
return (1);
return 1;
else
return (0);
return 0;
}
bn_check_top(a);
@@ -641,9 +556,9 @@ int BN_cmp(const BIGNUM *a, const BIGNUM *b)
if (a->neg != b->neg) {
if (a->neg)
return (-1);
return -1;
else
return (1);
return 1;
}
if (a->neg == 0) {
gt = 1;
@@ -654,18 +569,18 @@ int BN_cmp(const BIGNUM *a, const BIGNUM *b)
}
if (a->top > b->top)
return (gt);
return gt;
if (a->top < b->top)
return (lt);
return lt;
for (i = a->top - 1; i >= 0; i--) {
t1 = a->d[i];
t2 = b->d[i];
if (t1 > t2)
return (gt);
return gt;
if (t1 < t2)
return (lt);
return lt;
}
return (0);
return 0;
}
int BN_set_bit(BIGNUM *a, int n)
@@ -679,7 +594,7 @@ int BN_set_bit(BIGNUM *a, int n)
j = n % BN_BITS2;
if (a->top <= i) {
if (bn_wexpand(a, i + 1) == NULL)
return (0);
return 0;
for (k = a->top; k < i + 1; k++)
a->d[k] = 0;
a->top = i + 1;
@@ -687,7 +602,7 @@ int BN_set_bit(BIGNUM *a, int n)
a->d[i] |= (((BN_ULONG)1) << j);
bn_check_top(a);
return (1);
return 1;
}
int BN_clear_bit(BIGNUM *a, int n)
@@ -701,11 +616,11 @@ int BN_clear_bit(BIGNUM *a, int n)
i = n / BN_BITS2;
j = n % BN_BITS2;
if (a->top <= i)
return (0);
return 0;
a->d[i] &= (~(((BN_ULONG)1) << j));
bn_correct_top(a);
return (1);
return 1;
}
int BN_is_bit_set(const BIGNUM *a, int n)
@@ -741,7 +656,7 @@ int BN_mask_bits(BIGNUM *a, int n)
a->d[w] &= ~(BN_MASK2 << b);
}
bn_correct_top(a);
return (1);
return 1;
}
void BN_set_negative(BIGNUM *a, int b)
@@ -767,7 +682,7 @@ int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
if (aa != bb)
return ((aa > bb) ? 1 : -1);
}
return (0);
return 0;
}
/*
@@ -944,7 +859,7 @@ BN_GENCB *BN_GENCB_new(void)
if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
BNerr(BN_F_BN_GENCB_NEW, ERR_R_MALLOC_FAILURE);
return (NULL);
return NULL;
}
return ret;