forked from wolfSSL/wolfssl
Implement BN_rand_range
This commit is contained in:
35
src/ssl.c
35
src/ssl.c
@ -52693,11 +52693,10 @@ int wolfSSL_mask_bits(WOLFSSL_BIGNUM* bn, int n)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* WOLFSSL_SUCCESS on ok */
|
/* WOLFSSL_SUCCESS on ok */
|
||||||
int wolfSSL_BN_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
|
int wolfSSL_BN_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = WOLFSSL_FAILURE;
|
||||||
int len;
|
int len;
|
||||||
int initTmpRng = 0;
|
int initTmpRng = 0;
|
||||||
WC_RNG* rng = NULL;
|
WC_RNG* rng = NULL;
|
||||||
@ -52770,6 +52769,38 @@ int wolfSSL_BN_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* N = length of range input var
|
||||||
|
* Generate N-bit length numbers until generated number is less than range
|
||||||
|
* @param r Output number
|
||||||
|
* @param range The upper limit of generated output
|
||||||
|
* @return WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
|
||||||
|
*/
|
||||||
|
int wolfSSL_BN_rand_range(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *range)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
WOLFSSL_MSG("wolfSSL_BN_rand_range");
|
||||||
|
|
||||||
|
if (r == NULL || range == NULL) {
|
||||||
|
WOLFSSL_MSG("Bad parameter");
|
||||||
|
return WOLFSSL_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
n = wolfSSL_BN_num_bits(range);
|
||||||
|
|
||||||
|
if (n <= 1) {
|
||||||
|
wolfSSL_BN_zero(r);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
do {
|
||||||
|
if (wolfSSL_BN_rand(r, n, 0, 0) == WOLFSSL_FAILURE) {
|
||||||
|
WOLFSSL_MSG("wolfSSL_BN_rand error");
|
||||||
|
return WOLFSSL_FAILURE;
|
||||||
|
}
|
||||||
|
} while(wolfSSL_BN_cmp(r, range) >= 0);
|
||||||
|
}
|
||||||
|
return WOLFSSL_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
/* WOLFSSL_SUCCESS on ok
|
/* WOLFSSL_SUCCESS on ok
|
||||||
* code is same as wolfSSL_BN_rand except for how top and bottom is handled.
|
* code is same as wolfSSL_BN_rand except for how top and bottom is handled.
|
||||||
|
@ -36775,6 +36775,7 @@ static void test_wolfSSL_BN_rand(void)
|
|||||||
{
|
{
|
||||||
#if defined(OPENSSL_EXTRA)
|
#if defined(OPENSSL_EXTRA)
|
||||||
BIGNUM* bn;
|
BIGNUM* bn;
|
||||||
|
BIGNUM* range;
|
||||||
|
|
||||||
printf(testingFmt, "wolfSSL_BN_rand()");
|
printf(testingFmt, "wolfSSL_BN_rand()");
|
||||||
|
|
||||||
@ -36790,6 +36791,13 @@ static void test_wolfSSL_BN_rand(void)
|
|||||||
AssertIntEQ(BN_rand(bn, 64, 0, 0), SSL_SUCCESS);
|
AssertIntEQ(BN_rand(bn, 64, 0, 0), SSL_SUCCESS);
|
||||||
BN_free(bn);
|
BN_free(bn);
|
||||||
|
|
||||||
|
AssertNotNull(bn = BN_new());
|
||||||
|
AssertNotNull(range = BN_new());
|
||||||
|
AssertIntEQ(BN_rand(range, 64, 0, 0), SSL_SUCCESS);
|
||||||
|
AssertIntEQ(BN_rand_range(bn, range), SSL_SUCCESS);
|
||||||
|
BN_free(bn);
|
||||||
|
BN_free(range);
|
||||||
|
|
||||||
printf(resultFmt, passed);
|
printf(resultFmt, passed);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -104,6 +104,7 @@ WOLFSSL_API int wolfSSL_mask_bits(WOLFSSL_BIGNUM*, int n);
|
|||||||
|
|
||||||
WOLFSSL_API int wolfSSL_BN_pseudo_rand(WOLFSSL_BIGNUM*, int bits, int top,
|
WOLFSSL_API int wolfSSL_BN_pseudo_rand(WOLFSSL_BIGNUM*, int bits, int top,
|
||||||
int bottom);
|
int bottom);
|
||||||
|
WOLFSSL_API int wolfSSL_BN_rand_range(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *range);
|
||||||
WOLFSSL_API int wolfSSL_BN_rand(WOLFSSL_BIGNUM*, int bits, int top, int bottom);
|
WOLFSSL_API int wolfSSL_BN_rand(WOLFSSL_BIGNUM*, int bits, int top, int bottom);
|
||||||
WOLFSSL_API int wolfSSL_BN_is_bit_set(const WOLFSSL_BIGNUM*, int n);
|
WOLFSSL_API int wolfSSL_BN_is_bit_set(const WOLFSSL_BIGNUM*, int n);
|
||||||
WOLFSSL_API int wolfSSL_BN_hex2bn(WOLFSSL_BIGNUM**, const char* str);
|
WOLFSSL_API int wolfSSL_BN_hex2bn(WOLFSSL_BIGNUM**, const char* str);
|
||||||
@ -187,6 +188,7 @@ typedef WOLFSSL_BN_GENCB BN_GENCB;
|
|||||||
|
|
||||||
#define BN_pseudo_rand wolfSSL_BN_pseudo_rand
|
#define BN_pseudo_rand wolfSSL_BN_pseudo_rand
|
||||||
#define BN_rand wolfSSL_BN_rand
|
#define BN_rand wolfSSL_BN_rand
|
||||||
|
#define BN_rand_range wolfSSL_BN_rand_range
|
||||||
#define BN_is_bit_set wolfSSL_BN_is_bit_set
|
#define BN_is_bit_set wolfSSL_BN_is_bit_set
|
||||||
#define BN_hex2bn wolfSSL_BN_hex2bn
|
#define BN_hex2bn wolfSSL_BN_hex2bn
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user