projects
/
oweals
/
openssl.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix a ** 0 mod 1 = 0 for real this time.
[oweals/openssl.git]
/
crypto
/
bn
/
bn_rand.c
diff --git
a/crypto/bn/bn_rand.c
b/crypto/bn/bn_rand.c
index ecdce9ff1498c71d118f807ce026cbe017daaaee..66a175c32a2155e3ddd29d2863ddda3907c26d6e 100644
(file)
--- a/
crypto/bn/bn_rand.c
+++ b/
crypto/bn/bn_rand.c
@@
-111,7
+111,7
@@
#include <stdio.h>
#include <time.h>
#include <stdio.h>
#include <time.h>
-#include "cryptlib.h"
+#include "
internal/
cryptlib.h"
#include "bn_lcl.h"
#include <openssl/rand.h>
#include <openssl/sha.h>
#include "bn_lcl.h"
#include <openssl/rand.h>
#include <openssl/sha.h>
@@
-122,6
+122,11
@@
static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
int ret = 0, bit, bytes, mask;
time_t tim;
int ret = 0, bit, bytes, mask;
time_t tim;
+ if (bits < 0 || (bits == 1 && top > 0)) {
+ BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);
+ return 0;
+ }
+
if (bits == 0) {
BN_zero(rnd);
return 1;
if (bits == 0) {
BN_zero(rnd);
return 1;
@@
-131,7
+136,7
@@
static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
bit = (bits - 1) % 8;
mask = 0xff << (bit + 1);
bit = (bits - 1) % 8;
mask = 0xff << (bit + 1);
- buf =
(unsigned char *)
OPENSSL_malloc(bytes);
+ buf = OPENSSL_malloc(bytes);
if (buf == NULL) {
BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
goto err;
if (buf == NULL) {
BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
goto err;
@@
-142,14
+147,13
@@
static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
RAND_add(&tim, sizeof(tim), 0.0);
if (pseudorand) {
RAND_add(&tim, sizeof(tim), 0.0);
if (pseudorand) {
- if (RAND_
pseudo_bytes(buf, bytes) == -1
)
+ if (RAND_
bytes(buf, bytes) <= 0
)
goto err;
} else {
if (RAND_bytes(buf, bytes) <= 0)
goto err;
}
goto err;
} else {
if (RAND_bytes(buf, bytes) <= 0)
goto err;
}
-#if 1
if (pseudorand == 2) {
/*
* generate patterns that are more likely to trigger BN library bugs
if (pseudorand == 2) {
/*
* generate patterns that are more likely to trigger BN library bugs
@@
-158,7
+162,8
@@
static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
unsigned char c;
for (i = 0; i < bytes; i++) {
unsigned char c;
for (i = 0; i < bytes; i++) {
- RAND_pseudo_bytes(&c, 1);
+ if (RAND_bytes(&c, 1) <= 0)
+ goto err;
if (c >= 128 && i > 0)
buf[i] = buf[i - 1];
else if (c < 42)
if (c >= 128 && i > 0)
buf[i] = buf[i - 1];
else if (c < 42)
@@
-167,9
+172,8
@@
static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
buf[i] = 255;
}
}
buf[i] = 255;
}
}
-#endif
- if (top
!= -1
) {
+ if (top
>= 0
) {
if (top) {
if (bit == 0) {
buf[0] = 1;
if (top) {
if (bit == 0) {
buf[0] = 1;
@@
-188,10
+192,7
@@
static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
goto err;
ret = 1;
err:
goto err;
ret = 1;
err:
- if (buf != NULL) {
- OPENSSL_cleanse(buf, bytes);
- OPENSSL_free(buf);
- }
+ OPENSSL_clear_free(buf, bytes);
bn_check_top(rnd);
return (ret);
}
bn_check_top(rnd);
return (ret);
}
@@
-206,12
+207,10
@@
int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)
return bnrand(1, rnd, bits, top, bottom);
}
return bnrand(1, rnd, bits, top, bottom);
}
-#if 1
int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
{
return bnrand(2, rnd, bits, top, bottom);
}
int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
{
return bnrand(2, rnd, bits, top, bottom);
}
-#endif
/* random number r: 0 <= r < range */
static int bn_rand_range(int pseudo, BIGNUM *r, const BIGNUM *range)
/* random number r: 0 <= r < range */
static int bn_rand_range(int pseudo, BIGNUM *r, const BIGNUM *range)
@@
-316,7
+315,7
@@
int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,
int ret = 0;
k_bytes = OPENSSL_malloc(num_k_bytes);
int ret = 0;
k_bytes = OPENSSL_malloc(num_k_bytes);
- if (
!k_bytes
)
+ if (
k_bytes == NULL
)
goto err;
/* We copy |priv| into a local buffer to avoid exposing its length. */
goto err;
/* We copy |priv| into a local buffer to avoid exposing its length. */
@@
-357,7
+356,6
@@
int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,
ret = 1;
err:
ret = 1;
err:
- if (k_bytes)
- OPENSSL_free(k_bytes);
+ OPENSSL_free(k_bytes);
return ret;
}
return ret;
}