本文整理汇总了C++中BN_set_word函数的典型用法代码示例。如果您正苦于以下问题:C++ BN_set_word函数的具体用法?C++ BN_set_word怎么用?C++ BN_set_word使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了BN_set_word函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: DH_check_pub_key
int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
{
int ok = 0;
BIGNUM *q = NULL;
*ret = 0;
q = BN_new();
if (q == NULL)
goto err;
BN_set_word(q, 1);
if (BN_cmp(pub_key, q) <= 0)
*ret |= DH_CHECK_PUBKEY_TOO_SMALL;
BN_copy(q, dh->p);
BN_sub_word(q, 1);
if (BN_cmp(pub_key, q) >= 0)
*ret |= DH_CHECK_PUBKEY_TOO_LARGE;
ok = 1;
err:
if (q != NULL)
BN_free(q);
return (ok);
}
开发者ID:GrayKing,项目名称:Leakfix-on-OpenSSL,代码行数:23,代码来源:fips_dh_check.c
示例2: pkey_rsa_keygen
static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
RSA *rsa = NULL;
RSA_PKEY_CTX *rctx = ctx->data;
if (!rctx->pub_exp) {
rctx->pub_exp = BN_new();
if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4)) {
return 0;
}
}
rsa = RSA_new();
if (!rsa) {
return 0;
}
if (!RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, NULL)) {
RSA_free(rsa);
return 0;
}
EVP_PKEY_assign_RSA(pkey, rsa);
return 1;
}
开发者ID:randombit,项目名称:hacrypto,代码行数:23,代码来源:p_rsa.c
示例3: crypto_pk_generate_key_with_bits
/** Generate a <b>bits</b>-bit new public/private keypair in <b>env</b>.
* Return 0 on success, -1 on failure.
*/
int crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits)
{
if (env->key)
RSA_free(env->key);
{
BIGNUM *e = BN_new();
RSA *r = NULL;
if (!e)
goto done;
if (! BN_set_word(e, 65537))
goto done;
r = RSA_new();
if (!r)
goto done;
if (RSA_generate_key_ex(r, bits, e, NULL) == -1)
goto done;
env->key = r;
r = NULL;
done:
if (e)
BN_clear_free(e);
if (r)
RSA_free(r);
}
if (!env->key) {
sgx_puts("generate RSA key");
return -1;
}
return 0;
}
开发者ID:johnjohnsp1,项目名称:opensgx,代码行数:40,代码来源:sgx-tor.c
示例4: main
int main(int argc, char const *argv[])
{
RSA *rsa;
int modulelen = 1024;
int ret, i;
unsigned int len;
unsigned long e = RSA_3;
BIGNUM *bn;
unsigned char from[128];
unsigned char to[128];
bn = BN_new();
ret = BN_set_word(bn, e);
rsa = RSA_new();
ret = RSA_generate_key_ex(rsa, modulelen, bn, NULL);
if(ret != 1)
{
printf("ERROR in RSA_generate_key_ex\n");
goto finally;
}
for (i = 0; i < 100; ++i)
{
memset(&from[i], i, 1);
/* code */
}
ret = RSA_sign(NID_sha1, from ,100, to, &len, rsa);
printf("ret = %d, len = %d\n", ret, len);
ret = RSA_verify(NID_sha1, to, 100, from, len, rsa);
printf("ret = %d, len = %d\n",ret, len );
finally:
RSA_free(rsa);
return 0;
}
开发者ID:gwg-bhb,项目名称:exercise-win,代码行数:37,代码来源:openssl_sign.c
示例5: pkey_rsa_keygen
static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
{
RSA *rsa = NULL;
RSA_PKEY_CTX *rctx = ctx->data;
BN_GENCB *pcb;
int ret;
if (rctx->pub_exp == NULL) {
rctx->pub_exp = BN_new();
if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
return 0;
}
rsa = RSA_new();
if (rsa == NULL)
return 0;
if (ctx->pkey_gencb) {
pcb = BN_GENCB_new();
if (pcb == NULL) {
RSA_free(rsa);
return 0;
}
evp_pkey_set_cb_translate(pcb, ctx);
} else {
pcb = NULL;
}
ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
BN_GENCB_free(pcb);
if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
RSA_free(rsa);
return 0;
}
if (ret > 0)
EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
else
RSA_free(rsa);
return ret;
}
开发者ID:danielctull-forks,项目名称:openssl,代码行数:37,代码来源:rsa_pmeth.c
示例6: DH_check_pub_key
int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
{
int ok = 0;
BIGNUM *tmp = NULL;
BN_CTX *ctx = NULL;
*ret = 0;
ctx = BN_CTX_new();
if (ctx == NULL)
goto err;
BN_CTX_start(ctx);
tmp = BN_CTX_get(ctx);
if (tmp == NULL || !BN_set_word(tmp, 1))
goto err;
if (BN_cmp(pub_key, tmp) <= 0)
*ret |= DH_CHECK_PUBKEY_TOO_SMALL;
if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
goto err;
if (BN_cmp(pub_key, tmp) >= 0)
*ret |= DH_CHECK_PUBKEY_TOO_LARGE;
if (dh->q != NULL) {
/* Check pub_key^q == 1 mod p */
if (!BN_mod_exp(tmp, pub_key, dh->q, dh->p, ctx))
goto err;
if (!BN_is_one(tmp))
*ret |= DH_CHECK_PUBKEY_INVALID;
}
ok = 1;
err:
if (ctx != NULL) {
BN_CTX_end(ctx);
BN_CTX_free(ctx);
}
return (ok);
}
开发者ID:03050903,项目名称:godot,代码行数:37,代码来源:dh_check.c
示例7: push_scale
static void
push_scale(void)
{
struct number *n;
struct value *value;
u_int scale = 0;
value = pop();
if (value != NULL) {
switch (value->type) {
case BCODE_NONE:
return;
case BCODE_NUMBER:
scale = value->u.num->scale;
break;
case BCODE_STRING:
break;
}
stack_free_value(value);
n = new_number();
bn_check(BN_set_word(n->number, scale));
push_number(n);
}
}
开发者ID:FreeBSDFoundation,项目名称:freebsd,代码行数:24,代码来源:bcode.c
示例8: Zeroize
/* Zeroize
*/
static int Zeroize()
{
RSA *key;
BIGNUM *bn;
unsigned char userkey[16] =
{ 0x48, 0x50, 0xf0, 0xa3, 0x3a, 0xed, 0xd3, 0xaf, 0x6e, 0x47, 0x7f, 0x83, 0x02, 0xb1, 0x09, 0x68 };
size_t i;
int n;
key = FIPS_rsa_new();
bn = BN_new();
if (!key || !bn)
return 0;
BN_set_word(bn, 65537);
if (!RSA_generate_key_ex(key, 1024,bn,NULL))
return 0;
BN_free(bn);
n = BN_num_bytes(key->d);
printf(" Generated %d byte RSA private key\n", n);
printf("\tBN key before overwriting:\n");
do_bn_print(stdout, key->d);
BN_rand(key->d,n*8,-1,0);
printf("\tBN key after overwriting:\n");
do_bn_print(stdout, key->d);
printf("\tchar buffer key before overwriting: \n\t\t");
for(i = 0; i < sizeof(userkey); i++) printf("%02x", userkey[i]);
printf("\n");
RAND_bytes(userkey, sizeof userkey);
printf("\tchar buffer key after overwriting: \n\t\t");
for(i = 0; i < sizeof(userkey); i++) printf("%02x", userkey[i]);
printf("\n");
return 1;
}
开发者ID:leloulight,项目名称:eme,代码行数:38,代码来源:fips_test_suite.c
示例9: BN_mod_inverse_no_branch
//.........这里部分代码省略.........
{
BIGNUM *tmp;
/*
* 0 < B < A,
* (*) -sign*X*a == B (mod |n|),
* sign*Y*a == A (mod |n|)
*/
/* (D, M) := (A/B, A%B) ... */
if (BN_num_bits(A) == BN_num_bits(B))
{
if (!BN_one(D)) goto err;
if (!BN_sub(M,A,B)) goto err;
}
else if (BN_num_bits(A) == BN_num_bits(B) + 1)
{
/* A/B is 1, 2, or 3 */
if (!BN_lshift1(T,B)) goto err;
if (BN_ucmp(A,T) < 0)
{
/* A < 2*B, so D=1 */
if (!BN_one(D)) goto err;
if (!BN_sub(M,A,B)) goto err;
}
else
{
/* A >= 2*B, so D=2 or D=3 */
if (!BN_sub(M,A,T)) goto err;
if (!BN_add(D,T,B)) goto err; /* use D (:= 3*B) as temp */
if (BN_ucmp(A,D) < 0)
{
/* A < 3*B, so D=2 */
if (!BN_set_word(D,2)) goto err;
/* M (= A - 2*B) already has the correct value */
}
else
{
/* only D=3 remains */
if (!BN_set_word(D,3)) goto err;
/* currently M = A - 2*B, but we need M = A - 3*B */
if (!BN_sub(M,M,B)) goto err;
}
}
}
else
{
if (!BN_div(D,M,A,B,ctx)) goto err;
}
/* Now
* A = D*B + M;
* thus we have
* (**) sign*Y*a == D*B + M (mod |n|).
*/
tmp=A; /* keep the BIGNUM object, the value does not matter */
/* (A, B) := (B, A mod B) ... */
A=B;
B=M;
/* ... so we have 0 <= B < A again */
/* Since the former M is now B and the former B is now A,
* (**) translates into
* sign*Y*a == D*A + B (mod |n|),
开发者ID:RyunosukeOno,项目名称:rayjack,代码行数:67,代码来源:bn_gcd.c
示例10: a2d_ASN1_OBJECT
int
a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
{
int i, first, len = 0, c, use_bn;
char ftmp[24], *tmp = ftmp;
int tmpsize = sizeof ftmp;
const char *p;
unsigned long l;
BIGNUM *bl = NULL;
if (num == 0)
return (0);
else if (num == -1)
num = strlen(buf);
p = buf;
c = *(p++);
num--;
if ((c >= '0') && (c <= '2')) {
first= c-'0';
} else {
ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_FIRST_NUM_TOO_LARGE);
goto err;
}
if (num <= 0) {
ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_MISSING_SECOND_NUMBER);
goto err;
}
c = *(p++);
num--;
for (;;) {
if (num <= 0)
break;
if ((c != '.') && (c != ' ')) {
ASN1err(ASN1_F_A2D_ASN1_OBJECT,
ASN1_R_INVALID_SEPARATOR);
goto err;
}
l = 0;
use_bn = 0;
for (;;) {
if (num <= 0)
break;
num--;
c = *(p++);
if ((c == ' ') || (c == '.'))
break;
if ((c < '0') || (c > '9')) {
ASN1err(ASN1_F_A2D_ASN1_OBJECT,
ASN1_R_INVALID_DIGIT);
goto err;
}
if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {
use_bn = 1;
if (!bl)
bl = BN_new();
if (!bl || !BN_set_word(bl, l))
goto err;
}
if (use_bn) {
if (!BN_mul_word(bl, 10L) ||
!BN_add_word(bl, c-'0'))
goto err;
} else
l = l * 10L + (long)(c - '0');
}
if (len == 0) {
if ((first < 2) && (l >= 40)) {
ASN1err(ASN1_F_A2D_ASN1_OBJECT,
ASN1_R_SECOND_NUMBER_TOO_LARGE);
goto err;
}
if (use_bn) {
if (!BN_add_word(bl, first * 40))
goto err;
} else
l += (long)first * 40;
}
i = 0;
if (use_bn) {
int blsize;
blsize = BN_num_bits(bl);
blsize = (blsize + 6) / 7;
if (blsize > tmpsize) {
if (tmp != ftmp)
free(tmp);
tmpsize = blsize + 32;
tmp = malloc(tmpsize);
if (!tmp)
goto err;
}
while (blsize--)
tmp[i++] = (unsigned char)BN_div_word(bl, 0x80L);
} else {
for (;;) {
tmp[i++] = (unsigned char)l & 0x7f;
l >>= 7L;
if (l == 0L)
//.........这里部分代码省略.........
开发者ID:Basskrapfen,项目名称:openbsd,代码行数:101,代码来源:a_object.c
示例11: BN_set_word
void BigNumber::SetDword(uint32 val)
{
BN_set_word(_bn, val);
}
开发者ID:AdrElecTro,项目名称:CactusEMU,代码行数:4,代码来源:BigNumber.cpp
示例12: BN_CTX_new
static EC_GROUP *ec_group_new_from_data(const EC_CURVE_DATA *data)
{
EC_GROUP *group = NULL;
EC_POINT *P = NULL;
BN_CTX *ctx = NULL;
BIGNUM *p = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL, *order = NULL;
int ok = 0;
int seed_len = 0;
int param_len = 0;
const unsigned char *params = NULL;
ctx = BN_CTX_new();
if (ctx == NULL) {
ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_MALLOC_FAILURE);
goto err;
}
seed_len = data->seed_len;
param_len = data->param_len;
params = (const unsigned char *)(data+1); /* skip header */
params += seed_len; /* skip seed */
if (
!(p = BN_bin2bn(params+0*param_len, param_len, NULL))
|| !(a = BN_bin2bn(params+1*param_len, param_len, NULL))
|| !(b = BN_bin2bn(params+2*param_len, param_len, NULL))
) {
ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_BN_LIB);
goto err;
}
if ((group = EC_GROUP_new_curve_GFp(p, a, b, ctx)) == NULL) {
ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
goto err;
}
if ((P = EC_POINT_new(group)) == NULL) {
ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
goto err;
}
if (
!(x = BN_bin2bn(params+3*param_len, param_len, NULL))
|| !(y = BN_bin2bn(params+4*param_len, param_len, NULL))
) {
ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_BN_LIB);
goto err;
}
if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx)) {
ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
goto err;
}
if (
!(order = BN_bin2bn(params+5*param_len, param_len, NULL))
|| !BN_set_word(x, (BN_ULONG)data->cofactor)
) {
ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_BN_LIB);
goto err;
}
if (!EC_GROUP_set_generator(group, P, order, x)) {
ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
goto err;
}
if (seed_len) {
if (!EC_GROUP_set_seed(group, params-seed_len, seed_len)) {
ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
goto err;
}
}
ok = 1;
err:
if (!ok) {
EC_GROUP_free(group);
group = NULL;
}
if (P) { EC_POINT_free(P); }
if (ctx) { BN_CTX_free(ctx); }
if (p) { BN_free(p); }
if (a) { BN_free(a); }
if (b) { BN_free(b); }
if (order) { BN_free(order);}
if (x) { BN_free(x); }
if (y) { BN_free(y); }
return group;
}
开发者ID:matja,项目名称:bitcoin-tool,代码行数:91,代码来源:keys.c
示例13: dh_builtin_genparams
/* Actually there is no reason to insist that 'generator' be a generator.
* It's just as OK (and in some sense better) to use a generator of the
* order-q subgroup.
*/
static int dh_builtin_genparams(DH *ret, int prime_len, int generator, BN_GENCB *cb)
{
BIGNUM *t1,*t2;
int g,ok= -1;
BN_CTX *ctx=NULL;
ctx=BN_CTX_new();
if (ctx == NULL) goto err;
BN_CTX_start(ctx);
t1 = BN_CTX_get(ctx);
t2 = BN_CTX_get(ctx);
if (t1 == NULL || t2 == NULL) goto err;
/* Make sure 'ret' has the necessary elements */
if(!ret->p && ((ret->p = BN_new()) == NULL)) goto err;
if(!ret->g && ((ret->g = BN_new()) == NULL)) goto err;
if (generator <= 1)
{
DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
goto err;
}
if (generator == DH_GENERATOR_2)
{
if (!BN_set_word(t1,24)) goto err;
if (!BN_set_word(t2,11)) goto err;
g=2;
}
#if 0 /* does not work for safe primes */
else if (generator == DH_GENERATOR_3)
{
if (!BN_set_word(t1,12)) goto err;
if (!BN_set_word(t2,5)) goto err;
g=3;
}
#endif
else if (generator == DH_GENERATOR_5)
{
if (!BN_set_word(t1,10)) goto err;
if (!BN_set_word(t2,3)) goto err;
/* BN_set_word(t3,7); just have to miss
* out on these ones :-( */
g=5;
}
else
{
/* in the general case, don't worry if 'generator' is a
* generator or not: since we are using safe primes,
* it will generate either an order-q or an order-2q group,
* which both is OK */
if (!BN_set_word(t1,2)) goto err;
if (!BN_set_word(t2,1)) goto err;
g=generator;
}
if(!BN_generate_prime_ex(ret->p,prime_len,1,t1,t2,cb)) goto err;
if(!BN_GENCB_call(cb, 3, 0)) goto err;
if (!BN_set_word(ret->g,g)) goto err;
ok=1;
err:
if (ok == -1)
{
DHerr(DH_F_DH_BUILTIN_GENPARAMS,ERR_R_BN_LIB);
ok=0;
}
if (ctx != NULL)
{
BN_CTX_end(ctx);
BN_CTX_free(ctx);
}
return ok;
}
开发者ID:10045125,项目名称:xuggle-xuggler,代码行数:77,代码来源:dh_gen.c
示例14: _bn
BigNumber::BigNumber(uint32 val)
: _bn(BN_new())
{
BN_set_word(_bn, val);
}
开发者ID:mysql1,项目名称:TournamentCore,代码行数:5,代码来源:BigNumber.cpp
示例15: printnumber
void
printnumber(FILE *f, const struct number *b, u_int base)
{
struct number *int_part, *fract_part;
int digits;
char buf[11];
size_t sz;
int i;
struct stack stack;
char *p;
charcount = 0;
lastchar = -1;
if (BN_is_zero(b->number))
putcharwrap(f, '0');
int_part = new_number();
fract_part = new_number();
fract_part->scale = b->scale;
if (base <= 16)
digits = 1;
else {
digits = snprintf(buf, sizeof(buf), "%u", base-1);
}
split_number(b, int_part->number, fract_part->number);
i = 0;
stack_init(&stack);
while (!BN_is_zero(int_part->number)) {
BN_ULONG rem = BN_div_word(int_part->number, base);
stack_pushstring(&stack, get_digit(rem, digits, base));
i++;
}
sz = i;
if (BN_cmp(b->number, &zero) < 0)
putcharwrap(f, '-');
for (i = 0; i < sz; i++) {
p = stack_popstring(&stack);
if (base > 16)
putcharwrap(f, ' ');
printwrap(f, p);
free(p);
}
stack_clear(&stack);
if (b->scale > 0) {
struct number *num_base;
BIGNUM mult, stop;
putcharwrap(f, '.');
num_base = new_number();
BN_set_word(num_base->number, base);
BN_init(&mult);
BN_one(&mult);
BN_init(&stop);
BN_one(&stop);
scale_number(&stop, b->scale);
i = 0;
while (BN_cmp(&mult, &stop) < 0) {
u_long rem;
if (i && base > 16)
putcharwrap(f, ' ');
i = 1;
bmul_number(fract_part, fract_part, num_base);
split_number(fract_part, int_part->number, NULL);
rem = BN_get_word(int_part->number);
p = get_digit(rem, digits, base);
int_part->scale = 0;
normalize(int_part, fract_part->scale);
BN_sub(fract_part->number, fract_part->number,
int_part->number);
printwrap(f, p);
free(p);
BN_mul_word(&mult, base);
}
free_number(num_base);
BN_free(&mult);
BN_free(&stop);
}
flushwrap(f);
free_number(int_part);
free_number(fract_part);
}
开发者ID:AhmadTux,项目名称:DragonFlyBSD,代码行数:86,代码来源:inout.c
示例16: DH_check_pubkey
int
DH_check_pubkey(const DH *dh, const BIGNUM *pub_key, int *codes)
{
BIGNUM *bn = NULL, *sum = NULL;
int ret = 0;
*codes = 0;
/**
* Checks that the function performs are:
* - pub_key is not negative
*/
if (BN_is_negative(pub_key))
goto out;
/**
* - pub_key > 1 and pub_key < p - 1,
* to avoid small subgroups attack.
*/
bn = BN_new();
if (bn == NULL)
goto out;
if (!BN_set_word(bn, 1))
goto out;
if (BN_cmp(bn, pub_key) >= 0)
*codes |= DH_CHECK_PUBKEY_TOO_SMALL;
sum = BN_new();
if (sum == NULL)
goto out;
BN_uadd(sum, pub_key, bn);
if (BN_cmp(sum, dh->p) >= 0)
*codes |= DH_CHECK_PUBKEY_TOO_LARGE;
/**
* - if g == 2, pub_key have more then one bit set,
* if bits set is 1, log_2(pub_key) is trival
*/
if (!BN_set_word(bn, 2))
goto out;
if (BN_cmp(bn, dh->g) == 0) {
unsigned i, n = BN_num_bits(pub_key);
unsigned bits = 0;
for (i = 0; i <= n; i++)
if (BN_is_bit_set(pub_key, i))
bits++;
if (bits < 2) {
*codes |= DH_CHECK_PUBKEY_TOO_SMALL;
goto out;
}
}
ret = 1;
out:
if (bn)
BN_free(bn);
if (sum)
BN_free(sum);
return ret;
}
开发者ID:Henauxg,项目名称:minix,代码行数:71,代码来源:dh.c
示例17: strdup
/* ------------------------------------------------------------------ */
char *eg_encode (char *s, int length, char *public_key)
{
// we use NULL-with-random padding. this allows to pass
// NULL-terminated strings without any additional processing.
// if you want to exchange binary data, define your own padding in
// your application or pass the data length somehow
BIGNUM message, gamma, delta, k, temp;
BIGNUM *p=NULL, *g=NULL, *key=NULL;
int i, nl, nc, no, pc, rc1, rc2, rc3, index;
unsigned char *buf = NULL;
BN_CTX *ctx = NULL;
char *p1, *p2, *p3, *output;
// setup key (p, g, key)
p1 = strdup (public_key);
p2 = strchr (p1, ':');
if (p2 == NULL) {free (p1); return NULL;}
*p2 = '\0';
p3 = strchr (p1, ',');
if (p3 == NULL)
{
index = atoi (p1);
if (index > sizeof(precomp)/sizeof(precomp[0])-1) return NULL;
p = NULL;
rc1 = BN_hex2bn (&p, precomp[index].prime);
if (rc1 == 0) return NULL;
g = BN_new ();
if (g == NULL) return NULL;
BN_set_word (g, precomp[index].generator);
}
else
{
rc1 = BN_hex2bn (&p, p1);
rc2 = BN_hex2bn (&g, p3+1);
if (rc1 == 0 || rc2 == 0) return NULL;
}
rc3 = BN_hex2bn (&key, p2+1);
free (p1);
if (rc3 == 0) return NULL;
// initialize temp variables
BN_init (&message);
BN_init (&gamma);
BN_init (&delta);
BN_init (&k);
BN_init (&temp);
ctx = BN_CTX_new ();
if (ctx == NULL) return NULL;
// number of bytes in p. this is the amount of bytes
// we can convert in one gulp
nl = BN_num_bytes (p);
buf = malloc (nl);
if (buf == NULL) return NULL;
// compute the 'nc', the number of cycles (gulps)
nc = length/nl;
if (length % nl) nc++;
// preallocate output buffer: nl*2 -- bin->hex conversion,
// nl*2*2 + 1 -- each gulp consists of two bignums and comma
// between them, nl*2*2+1+1 -- spaces between gulps in the output
no = nc * (nl*2*2+1+1) + 1;
output = malloc (no);
if (output == NULL) return NULL;
output[0] = '\0';
// cycle by pieces of input, each piece is 'nl' bytes long
// (except the last one)
for (i=0; i<nc; i++)
{
// compute piece length
pc = (i == nc-1) ? length % nl : nl;
memcpy (buf, s+i*nl, pc);
// do NULL+random padding if necessary
if (pc != nl)
{
buf[pc] = '\0';
if (nl-pc-1 > 0)
rand_bytes (buf+pc+1, nl-pc-1);
}
// convert to bignum
BN_bin2bn (buf, nl, &message);
// ElGamal: get random k, gamma = g^k mod p, delta = message * key^k mod p
BN_rand (&k, BN_num_bits (p)-1, 0, 0);
BN_mod_exp (&gamma, g, &k, p, ctx);
BN_mod_exp (&temp, key, &k, p, ctx);
BN_mod_mul (&delta, &temp, &message, p, ctx);
// convert into hex
p1 = BN_bn2hex (&gamma);
p2 = BN_bn2hex (&delta);
// copy result to output buffer and add delimiting space
// fairly ineffective at the moment
strcat (output, p1);
strcat (output, ",");
strcat (output, p2);
if (i != nc-1) strcat (output, " ");
free (p1);
//.........这里部分代码省略.........
开发者ID:OS2World,项目名称:LIB-libcrypto,代码行数:101,代码来源:elgamal.c
示例18: main
int main(int argc, char* argv[]){
// Local Variable definitions
int i, j, k;
size_t ret;
int rsa_byte_size = RSA_KEY_SIZE/8;
// buffer used to seed the PRNG
unsigned char seed[rsa_byte_size];
//unsigned char *keybuff;
unsigned char *priv;
unsigned char *pub;
unsigned char *mod;
size_t keybuff_len=0;
// File pointers
FILE *urand;
FILE *pubkeyfile;
FILE *privkeyfile;
// RSA Struct used to store Priv/Pub key vals
RSA *key = RSA_new();
// Set the exponent size, e, to be used by RSA.
BIGNUM *e = BN_new();
// Open the public keyfile
pubkeyfile = fopen("./publickey.txt","w+");
if(pubkeyfile == NULL){
fprintf(stderr, "ERROR: Unable to open publickey.txt for writing!\n");
exit(-1);
}
// Open the private keyfile
privkeyfile = fopen("./secretkey.txt","w+");
if(privkeyfile == NULL){
fprintf(stderr, "ERROR: Unable to open privatekey.txt for writing!\n");
exit(-1);
}
// Open dev rand to seed our random data.
urand = fopen("/dev/urandom","r");
if(urand == NULL){
fprintf(stderr, "ERROR: Unable to open /dev/urandom for reading!\n");
exit(-1);
}
// Read the rand data from /dev/urandom
ret = fread(&seed, sizeof(char), RSA_KEY_SIZE/8, urand);
if(ret < RSA_KEY_SIZE/8){
fprintf(stderr, "ERROR: Unable to obtain random seed from /dev/urandom!\n");
exit(-1);
}
// Seed the PRNG
RAND_seed(&seed, RSA_KEY_SIZE/8);
// Setup our BIGNUM, this acts as the exponent e and will be stored with the pub/priv keys struct
// read the BN_rand description to see why the last two args are 1.
//ret = BN_generate_prime_ex(e, RSA_KEY_SIZE, 1, NULL, NULL, NULL);
ret = BN_set_word(e, 0x10001); // 65537
if(!ret){
fprintf(stderr, "ERROR: There was a problem generating the mod 'e'\n");
exit(-1);
}
// NOTE: As per the OpenSSL docs, RSA_generate_key(...) is deprecated.
// int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
// Generate the RSA keys
ret = RSA_generate_key_ex(key, RSA_KEY_SIZE, e, NULL);
/* Currently, the OpenSSL doc does not detail the return value of RSA_generate_key_ex :-( */
if(!ret){
fprintf(stderr, "ERROR: There was a problem generating RSA key!\n");
exit(-1);
}
/*
printf("DBG: Public Key - ");
char * n_val = BN_bn2hex(key->n);
for(i = 0; i < 256; i++){
printf("%c", n_val[i]);
}
printf("\n");
*/
if(!PEM_write_RSAPublicKey(pubkeyfile, key)){
fprintf(stderr, "ERROR: There was a problem writing the Public RSA key!\n");
exit(-1);
}
if(!PEM_write_RSAPrivateKey(privkeyfile, key, NULL, NULL, 0, NULL, NULL)){
fprintf(stderr, "ERROR: There was a problem writing the Private RSA key!\n");
exit(-1);
}
/*
// Write the public and private key values out to disk respectively
//i = BN_num_bytes(key->e);
//.........这里部分代码省略.........
开发者ID:PoppySeedPlehzr,项目名称:school,代码行数:101,代码来源:hybridkeygen.c
示例19: eg_decode
/* ------------------------------------------------------------------ */
int eg_decode (char *s, char *private_key, char **result)
{
// there is no any padding processing in the decoding routine
// (see comment in rsa_encode)
BIGNUM message, *gamma, *delta, k, temp1, temp2, one;
BIGNUM *p=NULL, *g=NULL, *key=NULL;
int i, nl, nc, rc1, rc2, rc3, length, index;
unsigned char *buf = NULL;
BN_CTX *ctx = NULL;
char *p1, *p2, *p3;
// setup key (p, g, key)
p1 = strdup (private_key);
p2 = strchr (p1, ':');
if (p2 == NULL) {free (p1); return -1;}
*p2 = '\0';
p3 = strchr (p1, ',');
if (p3 == NULL)
{
index = atoi (p1);
if (index > sizeof(precomp)/sizeof(precomp[0])-1) return -1;
p = NULL;
rc1 = BN_hex2bn (&p, precomp[index].prime);
if (rc1 == 0) return -1;
g = BN_new ();
if (g == NULL) return -1;
BN_set_word (g, precomp[index].generator);
}
else
{
rc1 = BN_hex2bn (&p, p1);
rc2 = BN_hex2bn (&g, p3+1);
if (rc1 == 0 || rc2 == 0) return -1;
}
rc3 = BN_hex2bn (&key, p2+1);
free (p1);
if (rc3 == 0) return -1;
// initialize temp variables
BN_init (&message);
BN_init (&k);
BN_init (&temp1);
BN_init (&temp2);
BN_init (&one);
BN_one (&one);
gamma = BN_new ();
if (gamma == NULL) return -1;
delta = BN_new ();
if (delta == NULL) return -1;
ctx = BN_CTX_new ();
if (ctx == NULL) return -1;
// number of bytes in the modulus. this is the amount of bytes
// we can convert in one gulp and should expect to be in one
// group
nl = BN_num_bytes (p);
buf = malloc (nl);
if (buf == NULL) return -1;
// find the number of pieces in the encrypted message (the last
// piece is not terminated with space)
nc = str_numchars (s, ' ') + 1;
// preallocate output buffer
length = nc * nl;
*result = malloc (length);
if (*result == NULL) return -1;
// cycle by pieces of input, each piece is 'nl' bytes long
// (except the last one)
p1 = s;
for (i=0; i<nc; i++)
{
// extract next piece
p2 = strchr (p1, ' ');
if (p2 == NULL)
{
if (i != nc-1) return -1;
}
else
{
*p2 = '\0';
}
p3 = strchr (p1, ',');
if (p3 == NULL) return -1;
*p3++ = '\0';
// convert to bignum
rc1 = BN_hex2bn (&gamma, p1);
if (rc1 == 0) return -1;
rc1 = BN_hex2bn (&delta, p3);
if (rc1 == 0) return -1;
// ElGamal
BN_sub (&temp1, p, &one);
BN_sub (&temp2, &temp1, key);
BN_mod_exp (&temp1, gamma, &temp2, p, ctx);
BN_mod_mul (&message, &temp1, delta, p, ctx);
// convert into binary output
BN_bn2bin (&message, (unsigned char *)(*result+i*nl));
//.........这里部分代码省略.........
开发者ID:OS2World,项目名称:LIB-libcrypto,代码行数:101,代码来源:elgamal.c
示例20: cert_init
static int cert_init() {
X509 *x509 = NULL;
EVP_PKEY *pkey = NULL;
BIGNUM *exponent = NULL, *serial_number = NULL;
RSA *rsa = NULL;
ASN1_INTEGER *asn1_serial_number;
X509_NAME *name;
struct dtls_cert *new_cert;
ilog(LOG_INFO, "Generating new DTLS certificate");
/* objects */
pkey = EVP_PKEY_new();
exponent = BN_new();
rsa = RSA_new();
serial_number = BN_new();
name = X509_NAME_new();
x509 = X509_new();
if (!exponent || !pkey || !rsa || !serial_number || !name || !x509)
goto err;
/* key */
if (!BN_set_word(exponent, 0x10001))
goto err;
if (!RSA_generate_key_ex(rsa, 1024, exponent, NULL))
goto err;
if (!EVP_PKEY_assign_RSA(pkey, rsa))
goto err;
/* x509 cert */
if (!X509_set_pubkey(x509, pkey))
goto err;
/* serial */
if (!BN_pseudo_rand(serial_number, 64, 0, 0))
goto err;
asn1_serial_number = X509_get_serialNumber(x509);
if (!asn1_serial_number)
goto err;
if (!BN_to_ASN1_INTEGER(serial_number, asn1_serial_number))
goto err;
/* version 1 */
if (!X509_set_version(x509, 0L))
goto err;
/* common name */
if (!X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
(unsigned char *) "rtpengine", -1, -1, 0))
goto err;
if (!X509_set_subject_name(x509, name))
goto err;
if (!X509_set_issuer_name(x509, name))
goto err;
/* cert lifetime */
if (!X509_gmtime_adj(X509_get_notBefore(x509), -60*60*24))
goto err;
if (!X509_gmtime_adj(X509_get_notAfter(x509), CERT_EXPIRY_TIME))
goto err;
/* sign it */
if (!X509_sign(x509, pkey, EVP_sha1()))
goto err;
/* digest */
new_cert = obj_alloc0("dtls_cert", sizeof(*new_cert), cert_free);
new_cert->fingerprint.hash_func = &hash_funcs[0];
dtls_fingerprint_hash(&new_cert->fingerprint, x509);
new_cert->x509 = x509;
new_cert->pkey = pkey;
new_cert->expires = time(NULL) + CERT_EXPIRY_TIME;
dump_cert(new_cert);
/* swap out certs */
rwlock_lock_w(&__dtls_cert_lock);
if (__dtls_cert)
obj_put(__dtls_cert);
__dtls_cert = new_cert;
//.........这里部分代码省略.........
开发者ID:cahlbin,项目名称:rtpengine,代码行数:101,代码来源:dtls.c
注:本文中的BN_set_word函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论