本文整理匯總了C++中BN_is_bit_set函數的典型用法代碼示例。如果您正苦於以下問題:C++ BN_is_bit_set函數的具體用法?C++ BN_is_bit_set怎麽用?C++ BN_is_bit_set使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了BN_is_bit_set函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。
示例1: bn_rand_range
static int
bn_rand_range(int pseudo, BIGNUM *r, BIGNUM *range)
{
int (*bn_rand)(BIGNUM *, int, int, int) = pseudo ? BN_pseudo_rand : BN_rand;
int n;
if (range->neg || BN_is_zero(range)) return 0;
n = BN_num_bits(range);
if (n == 1) {
if (!BN_zero(r)) return 0;
} else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
do {
if (!bn_rand(r, n + 1, -1, 0)) return 0;
if (BN_cmp(r ,range) >= 0) {
if (!BN_sub(r, r, range)) return 0;
if (BN_cmp(r, range) >= 0)
if (!BN_sub(r, r, range)) return 0;
}
} while (BN_cmp(r, range) >= 0);
} else {
do {
if (!bn_rand(r, n, -1, 0)) return 0;
} while (BN_cmp(r, range) >= 0);
}
return 1;
}
示例2: BN_rand_range_ex
int BN_rand_range_ex(BIGNUM *r, BN_ULONG min_inclusive,
const BIGNUM *max_exclusive, RAND *rng) {
unsigned n;
unsigned count = 100;
if (BN_cmp_word(max_exclusive, min_inclusive) <= 0) {
OPENSSL_PUT_ERROR(BN, BN_R_INVALID_RANGE);
return 0;
}
n = BN_num_bits(max_exclusive); /* n > 0 */
/* BN_is_bit_set(range, n - 1) always holds */
if (n == 1) {
BN_zero(r);
return 1;
}
do {
if (!--count) {
OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_ITERATIONS);
return 0;
}
if (!BN_is_bit_set(max_exclusive, n - 2) &&
!BN_is_bit_set(max_exclusive, n - 3)) {
/* range = 100..._2, so 3*range (= 11..._2) is exactly one bit longer
* than range. This is a common scenario when generating a random value
* modulo an RSA public modulus, e.g. for RSA base blinding. */
if (!BN_rand(r, n + 1, -1 /* don't set most significant bits */,
0 /* don't set least significant bits */, rng)) {
return 0;
}
/* If r < 3*range, use r := r MOD range (which is either r, r - range, or
* r - 2*range). Otherwise, iterate again. Since 3*range = 11..._2, each
* iteration succeeds with probability >= .75. */
if (BN_cmp(r, max_exclusive) >= 0) {
if (!BN_sub(r, r, max_exclusive)) {
return 0;
}
if (BN_cmp(r, max_exclusive) >= 0) {
if (!BN_sub(r, r, max_exclusive)) {
return 0;
}
}
}
} else {
/* range = 11..._2 or range = 101..._2 */
if (!BN_rand(r, n, -1, 0, rng)) {
return 0;
}
}
} while (BN_cmp_word(r, min_inclusive) < 0 ||
BN_cmp(r, max_exclusive) >= 0);
return 1;
}
示例3: BN_rand_range
int BN_rand_range(BIGNUM *r, const BIGNUM *range) {
unsigned n;
unsigned count = 100;
if (range->neg || BN_is_zero(range)) {
OPENSSL_PUT_ERROR(BN, BN_rand_range, BN_R_INVALID_RANGE);
return 0;
}
n = BN_num_bits(range); /* n > 0 */
/* BN_is_bit_set(range, n - 1) always holds */
if (n == 1) {
BN_zero(r);
} else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
/* range = 100..._2,
* so 3*range (= 11..._2) is exactly one bit longer than range */
do {
if (!BN_rand(r, n + 1, -1 /* don't set most significant bits */,
0 /* don't set least significant bits */)) {
return 0;
}
/* If r < 3*range, use r := r MOD range (which is either r, r - range, or
* r - 2*range). Otherwise, iterate again. Since 3*range = 11..._2, each
* iteration succeeds with probability >= .75. */
if (BN_cmp(r, range) >= 0) {
if (!BN_sub(r, r, range)) {
return 0;
}
if (BN_cmp(r, range) >= 0) {
if (!BN_sub(r, r, range)) {
return 0;
}
}
}
if (!--count) {
OPENSSL_PUT_ERROR(BN, BN_rand_range, BN_R_TOO_MANY_ITERATIONS);
return 0;
}
} while (BN_cmp(r, range) >= 0);
} else {
do {
/* range = 11..._2 or range = 101..._2 */
if (!BN_rand(r, n, -1, 0)) {
return 0;
}
if (!--count) {
OPENSSL_PUT_ERROR(BN, BN_rand_range, BN_R_TOO_MANY_ITERATIONS);
return 0;
}
} while (BN_cmp(r, range) >= 0);
}
return 1;
}
示例4: dh_gen_key
void
dh_gen_key(DH *dh, int need)
{
int i, bits_set, tries = 0;
if (dh->p == NULL)
fatal("dh_gen_key: dh->p == NULL");
if (need > INT_MAX / 2 || 2 * need >= BN_num_bits(dh->p))
fatal("dh_gen_key: group too small: %d (2*need %d)",
BN_num_bits(dh->p), 2*need);
do {
if (dh->priv_key != NULL)
BN_clear_free(dh->priv_key);
if ((dh->priv_key = BN_new()) == NULL)
fatal("dh_gen_key: BN_new failed");
/* generate a 2*need bits random private exponent */
if (!BN_rand(dh->priv_key, 2*need, 0, 0))
fatal("dh_gen_key: BN_rand failed");
if (DH_generate_key(dh) == 0)
fatal("DH_generate_key");
for (i = 0, bits_set = 0; i <= BN_num_bits(dh->priv_key); i++)
if (BN_is_bit_set(dh->priv_key, i))
bits_set++;
debug2("dh_gen_key: priv key bits set: %d/%d",
bits_set, BN_num_bits(dh->priv_key));
if (tries++ > 10)
fatal("dh_gen_key: too many bad keys: giving up");
} while (!dh_pub_is_valid(dh, dh->pub_key));
}
示例5: compute_y
void compute_y(BIGNUM *bn_y, BIGNUM *bn_a, BIGNUM *bn_r, BIGNUM *bn_n, BN_CTX *bn_ctx){
BIGNUM *bn_i = NULL;
BIGNUM *bn_1 = NULL;
int num_bits = 0;
int i = 0;
BIGNUM **bn_array = NULL;
num_bits = BN_num_bits(bn_r);
bn_array = (BIGNUM **)malloc(sizeof(BIGNUM*) * num_bits);
computeBNArray(bn_array, bn_a, bn_n, bn_ctx, num_bits);
bn_1 = BN_new();
bn_i = BN_new();
BN_one(bn_1);
BN_zero(bn_i);
BN_one(bn_y);
for(i = 0; i < num_bits; i++){
if(BN_is_bit_set(bn_r, i) == 1){
BN_mod_mul(bn_y, bn_y, bn_array[i], bn_n, bn_ctx);
}
}
BN_free(bn_1);
BN_free(bn_i);
}
示例6: BN_exp
int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) {
int i, bits, ret = 0;
BIGNUM *v, *rr;
if ((p->flags & BN_FLG_CONSTTIME) != 0) {
/* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
return 0;
}
BN_CTX_start(ctx);
if (r == a || r == p) {
rr = BN_CTX_get(ctx);
} else {
rr = r;
}
v = BN_CTX_get(ctx);
if (rr == NULL || v == NULL) {
goto err;
}
if (BN_copy(v, a) == NULL) {
goto err;
}
bits = BN_num_bits(p);
if (BN_is_odd(p)) {
if (BN_copy(rr, a) == NULL) {
goto err;
}
} else {
if (!BN_one(rr)) {
goto err;
}
}
for (i = 1; i < bits; i++) {
if (!BN_sqr(v, v, ctx)) {
goto err;
}
if (BN_is_bit_set(p, i)) {
if (!BN_mul(rr, rr, v, ctx)) {
goto err;
}
}
}
if (r != rr && !BN_copy(r, rr)) {
goto err;
}
ret = 1;
err:
BN_CTX_end(ctx);
return ret;
}
示例7: NativeBN_modifyBit
/**
* public static native void modifyBit(int, int, int)
*/
static jboolean NativeBN_modifyBit(JNIEnv* env, jclass cls, BIGNUM* a, int n, int op) {
// LOGD("NativeBN_BN_modifyBit");
if (!oneValidHandle(env, a)) return FALSE;
switch (op) {
case 1: return BN_set_bit(a, n);
case 0: return BN_clear_bit(a, n);
case -1:
if (BN_is_bit_set(a, n)) return BN_clear_bit(a, n);
else return BN_set_bit(a, n);
}
return FALSE;
}
示例8: ossl_bn_is_bit_set
/*
* call-seq:
* bn.bit_set?(bit) => true | false
*/
static VALUE
ossl_bn_is_bit_set(VALUE self, VALUE bit)
{
int b;
BIGNUM *bn;
b = NUM2INT(bit);
GetBN(self, bn);
if (BN_is_bit_set(bn, b)) {
return Qtrue;
}
return Qfalse;
}
示例9: BN_bn2solinas
int BN_bn2solinas(const BIGNUM *bn, BN_SOLINAS *solinas)
{
int ret = 0;
BIGNUM *tmp = NULL;
int nbits;
int i;
if (!solinas || !bn) {
BNerr(BN_F_BN_BN2SOLINAS, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
if (!BN_copy(tmp, bn)) {
goto end;
}
if ((nbits = BN_num_bits(bn) - 1) < 1) {
BNerr(BN_F_BN_BN2SOLINAS, BN_R_INVALID_SOLINAS);
goto end;
}
solinas->c = BN_is_bit_set(bn, 1) ? 1 : -1;
if (BN_is_bit_set(bn, nbits - 1)) {
solinas->s = -1;
solinas->a = nbits;
} else {
solinas->s = 1;
solinas->a = nbits - 1;
}
for (i = 1; i < nbits; i++) {
}
end:
return ret;
}
示例10: BN_rand_range
/* random number r: 0 <= r < range */
int BN_rand_range(BIGNUM *r, BIGNUM *range)
{
int n;
if (range->neg || BN_is_zero(range))
{
BNerr(BN_F_BN_RAND_RANGE, BN_R_INVALID_RANGE);
return 0;
}
n = BN_num_bits(range); /* n > 0 */
if (n == 1)
{
if (!BN_zero(r)) return 0;
}
else if (BN_is_bit_set(range, n - 2))
{
do
{
/* range = 11..._2, so each iteration succeeds with probability >= .75 */
if (!BN_rand(r, n, -1, 0)) return 0;
}
while (BN_cmp(r, range) >= 0);
}
else
{
/* range = 10..._2,
* so 3*range (= 11..._2) is exactly one bit longer than range */
do
{
if (!BN_rand(r, n + 1, -1, 0)) return 0;
/* If r < 3*range, use r := r MOD range
* (which is either r, r - range, or r - 2*range).
* Otherwise, iterate once more.
* Since 3*range = 11..._2, each iteration succeeds with
* probability >= .75. */
if (BN_cmp(r ,range) >= 0)
{
if (!BN_sub(r, r, range)) return 0;
if (BN_cmp(r, range) >= 0)
if (!BN_sub(r, r, range)) return 0;
}
}
while (BN_cmp(r, range) >= 0);
}
return 1;
}
示例11: bn_probable_prime_dh_coprime
int bn_probable_prime_dh_coprime(BIGNUM *rnd, int bits, BN_CTX *ctx)
{
int i;
BIGNUM *offset_index;
BIGNUM *offset_count;
int ret = 0;
OPENSSL_assert(bits > prime_multiplier_bits);
BN_CTX_start(ctx);
if ((offset_index = BN_CTX_get(ctx)) == NULL)
goto err;
if ((offset_count = BN_CTX_get(ctx)) == NULL)
goto err;
if (!BN_add_word(offset_count, prime_offset_count))
goto err;
loop:
if (!BN_rand(rnd, bits - prime_multiplier_bits,
BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
goto err;
if (BN_is_bit_set(rnd, bits))
goto loop;
if (!BN_rand_range(offset_index, offset_count))
goto err;
if (!BN_mul_word(rnd, prime_multiplier)
|| !BN_add_word(rnd, prime_offsets[BN_get_word(offset_index)]))
goto err;
/* we now have a random number 'rand' to test. */
/* skip coprimes */
for (i = first_prime_index; i < NUMPRIMES; i++) {
/* check that rnd is a prime */
BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
if (mod == (BN_ULONG)-1)
goto err;
if (mod <= 1)
goto loop;
}
ret = 1;
err:
BN_CTX_end(ctx);
bn_check_top(rnd);
return ret;
}
示例12: dh_pub_is_valid
int
dh_pub_is_valid(const DH *dh, const BIGNUM *dh_pub)
{
int i;
int n = BN_num_bits(dh_pub);
int bits_set = 0;
BIGNUM *tmp;
const BIGNUM *p;
if (BN_is_negative(dh_pub)) {
logit("invalid public DH value: negative");
return 0;
}
if (BN_cmp(dh_pub, BN_value_one()) != 1) { /* pub_exp <= 1 */
logit("invalid public DH value: <= 1");
return 0;
}
if ((tmp = BN_new()) == NULL) {
error("%s: BN_new failed", __func__);
return 0;
}
DH_get0_pqg(dh, &p, NULL, NULL);
if (!BN_sub(tmp, p, BN_value_one()) ||
BN_cmp(dh_pub, tmp) != -1) { /* pub_exp > p-2 */
BN_clear_free(tmp);
logit("invalid public DH value: >= p-1");
return 0;
}
BN_clear_free(tmp);
for (i = 0; i <= n; i++)
if (BN_is_bit_set(dh_pub, i))
bits_set++;
debug2("bits set: %d/%d", bits_set, BN_num_bits(p));
/*
* if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial
*/
if (bits_set < 4) {
logit("invalid public DH value (%d/%d)",
bits_set, BN_num_bits(p));
return 0;
}
return 1;
}
示例13: dh_pub_is_valid
int dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
{
int i;
int n = BN_num_bits(dh_pub);
int bits_set = 0;
if (dh_pub->neg) {
//logit("invalid public DH value: negativ");
return 0;
}
for (i = 0; i <= n; i++)
if (BN_is_bit_set(dh_pub, i))
bits_set++;
//debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
/* if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial */
if (bits_set > 1 && (BN_cmp(dh_pub, dh->p) == -1))
return 1;
//logit("invalid public DH value (%d/%d)", bits_set, BN_num_bits(dh->p));
return 0;
}
示例14: print_ascii
void
print_ascii(FILE *f, const struct number *n)
{
BIGNUM *v;
int numbits, i, ch;
v = BN_dup(n->number);
bn_checkp(v);
if (BN_cmp(v, &zero) < 0)
bn_check(BN_sub(v, &zero, v));
numbits = BN_num_bytes(v) * 8;
while (numbits > 0) {
ch = 0;
for (i = 0; i < 8; i++)
ch |= BN_is_bit_set(v, numbits-i-1) << (7 - i);
putc(ch, f);
numbits -= 8;
}
BN_free(v);
}
示例15: fixed_mod_exp
void
fixed_mod_exp (auto_BN & retval,
const auto_BN & base,
const auto_BN & exp,
const auto_BN & modulus,
auto_BN_CTX & ctx)
{
#if defined (MOD_EXP_STATS)
clock_t start_time = clock();
#endif
// Number of bits in the exponent
int numbits = BN_num_bits(exp);
// An iterator to search the map
std::map< g_base_mod_pair, g_bn_vec >::iterator m_it;
// The values corresponding to our base
g_bn_vec current_vector;
// Try to find base in map
VHUtil::AutoMutex m(g_mutex);
m_it = g_fme_map.find(g_base_mod_pair(base, modulus));
{
// Make a mutex object to keep the map in one thread at a time
if (m_it != g_fme_map.end() )
{
// Found it
current_vector = m_it->second;
if (current_vector.size() < numbits )
{
// Need more bits in the vector
for (int n=current_vector.size(); n<numbits; n++)
{
// The final table value
auto_BN t_value;
if ( !(BN_mod_mul(t_value, current_vector[n-1], current_vector[n-1],
modulus, ctx)) )
throw SSL_ERROR;
current_vector.push_back(t_value);
}
}
}
else
{
// Didn't find it, create the table values and put into a new vector
std::vector< auto_BN > table_values;
// Seed our table with the base^(2^0)
table_values.push_back(base);
for (int i=1; i<numbits; i++)
{
// The final table value
auto_BN t_value;
if ( !(BN_mod_mul(t_value, table_values[i-1], table_values[i-1],
modulus, ctx)) )
throw SSL_ERROR;
table_values.push_back(t_value);
}
current_vector = table_values;
// Add the base and vector to our map
g_fme_map.insert(std::pair< g_base_mod_pair, g_bn_vec >
(g_base_mod_pair(base, modulus), current_vector));
}
}
// Now look up the exponent
// Our running product
auto_BN prod_values;
BN_one(prod_values);
for (int j=0; j<numbits; j++)
{
// Find out which bits are set, then find those
// values in the vector and multiply them together
if (BN_is_bit_set(exp, j) != 0)
{
// The bit is set, so find the value in the vector and multiply
if ( !(BN_mod_mul(prod_values, prod_values, current_vector[j], modulus, ctx)) )
throw SSL_ERROR;
}
}
retval = prod_values;
#if defined (MOD_EXP_STATS)
time_spent_in_fme += clock() - start_time;
{
auto_BN alternate_retval;
clock_t start_time = clock();
BN_mod_exp(alternate_retval, base, exp, modulus, ctx);
time_spent_in_BN_mod_exp += clock() - start_time;
VH_zero(BN_cmp(alternate_retval, retval), FIXED_MOD_EXP_SCREWED_UP);
}
#endif
}