本文整理汇总了C++中BN_hex2bn函数的典型用法代码示例。如果您正苦于以下问题:C++ BN_hex2bn函数的具体用法?C++ BN_hex2bn怎么用?C++ BN_hex2bn使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了BN_hex2bn函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: _BN_rand
int _BN_rand(BIGNUM *rnd, int bits, int top,int bottom)
{
static int i32 = 0;
static int i256 = 0;
int rsts = 0;
switch(bits)
{
case 32:
rsts = BN_hex2bn(&rnd, hex_bn32_rand[i32++]);
if (i32 >= sizeof(hex_bn32_rand)/sizeof(char *))
{
i32 = 0;
}
break;
case 256:
rsts = BN_hex2bn(&rnd, hex_bn256_rand[i256++]);
if (i256 >= sizeof(hex_bn256_rand)/sizeof(char *))
{
i256 = 0;
}
break;
default:
break;
}
return rsts;
}
示例2: dh_new_group_asc
DH *
dh_new_group_asc(const char *gen, const char *modulus)
{
DH *dh = NULL;
BIGNUM *p=NULL, *g=NULL;
if ((dh = DH_new()) == NULL ||
(p = BN_new()) == NULL ||
(g = BN_new()) == NULL)
goto null;
if (BN_hex2bn(&p, modulus) == 0 ||
BN_hex2bn(&g, gen) == 0) {
goto null;
}
if (DH_set0_pqg(dh, p, NULL, g) == 0) {
goto null;
}
p = g = NULL;
return (dh);
null:
BN_free(p);
BN_free(g);
DH_free(dh);
return NULL;
}
示例3: init_checksum
/*
* Initialize everything necessary to check the signature of the given image.
* Returns non-zero if successful, 0 otherwise.
*/
int
init_checksum(char *keyfile)
{
char str[1024];
FILE *file;
if (keyfile == NULL || (file = fopen(keyfile, "r")) == NULL) {
fprintf(stderr, "%s: cannot open keyfile\n", keyfile);
return 0;
}
if ((signature_key = RSA_new()) == NULL) {
fprintf(stderr, "%s: cannot allocate RSA struct\n", keyfile);
return 0;
}
fscanf(file, "%1024s", str);
BN_hex2bn(&signature_key->n, str);
fscanf(file, "%1024s", str);
BN_hex2bn(&signature_key->e, str);
fscanf(file, "%1024s", str);
BN_hex2bn(&signature_key->dmp1, str);
fscanf(file, "%1024s", str);
BN_hex2bn(&signature_key->dmq1, str);
fscanf(file, "%1024s", str);
BN_hex2bn(&signature_key->iqmp, str);
fclose(file);
return 1;
}
示例4: memcpy
/**
* @brief Deserialize the supplied buffer into an RSA public key.
*
* Allocate a new RSA keypair and fill in the modulus and the exponent from
* the public key stored in buf.
*
* @param buf Buffer containing the key to deserialize.
* @param len Length of the buffer.
*
* @return RSA *keypair on success, NULL on failure
*/
RSA *deserialize_rsa_pub_key(char *buf, size_t len) {
(void)len; // TODO: use len to make sure we don't try to read past end of buffer
/* get modulus */
uint32_t hexformodsize = *(uint32_t*)buf;
char *hexformod = (char*)malloc(hexformodsize + 1);
memcpy(hexformod, &buf[sizeof(uint32_t)], hexformodsize);
hexformod[hexformodsize] = '\0';
DBGF("hexformod: %s", hexformod);
/* get exponent */
uint32_t hexforexpsize = *(uint32_t*)&buf[sizeof(uint32_t) + hexformodsize];
char *hexforexp = (char*)malloc(hexforexpsize + 1);
memcpy(hexforexp, &buf[2*sizeof(uint32_t) + hexformodsize], hexforexpsize);
hexforexp[hexforexpsize] = '\0';
DBGF("hexforexp: %s", hexforexp);
/* make the keypair */
RSA *keypair = RSA_new();
BN_hex2bn(&keypair->n, hexformod); // TODO: need BN_new() first?
BN_hex2bn(&keypair->e, hexforexp);
keypair->d = NULL;
keypair->p = NULL;
keypair->q = NULL;
keypair->dmp1 = NULL;
keypair->dmq1 = NULL;
keypair->iqmp = NULL;
free(hexformod);
free(hexforexp);
return keypair;
}
示例5: fprintf
static EVP_PKEY *create_pkey(neverbleed_t *nb, size_t key_index, const char *ebuf, const char *nbuf)
{
struct st_neverbleed_rsa_exdata_t *exdata;
RSA *rsa;
EVP_PKEY *pkey;
if ((exdata = malloc(sizeof(*exdata))) == NULL) {
fprintf(stderr, "no memory\n");
abort();
}
exdata->nb = nb;
exdata->key_index = key_index;
rsa = RSA_new_method(nb->engine);
RSA_set_ex_data(rsa, 0, exdata);
if (BN_hex2bn(&rsa->e, ebuf) == 0) {
fprintf(stderr, "failed to parse e:%s\n", ebuf);
abort();
}
if (BN_hex2bn(&rsa->n, nbuf) == 0) {
fprintf(stderr, "failed to parse n:%s\n", nbuf);
abort();
}
rsa->flags |= RSA_FLAG_EXT_PKEY;
pkey = EVP_PKEY_new();
EVP_PKEY_set1_RSA(pkey, rsa);
RSA_free(rsa);
return pkey;
}
示例6: APSetKey
Boolean APSetKey(CFStringRef key)
{
hash = CFSTR("");
blacklist = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
// Create a new key
rsaKey = RSA_new();
// Public exponent is always 3
BN_hex2bn(&rsaKey->e, "3");
CFMutableStringRef mutableKey = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, key);
if (!mutableKey)
return FALSE;
CFIndex maximumCStringLength = CFStringGetMaximumSizeForEncoding(CFStringGetLength(mutableKey), kCFStringEncodingMacRoman) + 1;
char *keyCStringBuffer = malloc(maximumCStringLength);
// Determine if we have a hex or decimal key
CFStringLowercase(mutableKey, NULL);
if (CFStringHasPrefix(mutableKey, CFSTR("0x"))) {
CFStringTrim(mutableKey, CFSTR("0x"));
CFStringGetCString(mutableKey, keyCStringBuffer, maximumCStringLength, kCFStringEncodingMacRoman);
BN_hex2bn(&rsaKey->n, keyCStringBuffer);
}
else {
CFStringGetCString(mutableKey, keyCStringBuffer, maximumCStringLength, kCFStringEncodingMacRoman);
BN_dec2bn(&rsaKey->n, keyCStringBuffer);
}
CFRelease(mutableKey);
free(keyCStringBuffer);
return TRUE;
}
示例7: BN_CTX_new
polypseud_ctx *polypseud_ctx_new() {
polypseud_ctx *ctx = (polypseud_ctx*)malloc(sizeof(polypseud_ctx));
ctx->bn_ctx = BN_CTX_new();
ctx->p = BN_new();
ctx->a = BN_new();
ctx->b = BN_new();
BN_hex2bn(&ctx->p, "D35E472036BC4FB7E13C785ED201E065F98FCFA6F6F40DEF4F92B9EC7893EC28FCD412B1F1B32E27");
BN_hex2bn(&ctx->a, "3EE30B568FBAB0F883CCEBD46D3F3BB8A2A73513F5EB79DA66190EB085FFA9F492F375A97D860EB4");
BN_hex2bn(&ctx->b, "520883949DFDBC42D3AD198640688A6FE13F41349554B49ACC31DCCD884539816F5EB4AC8FB1F1A6");
ctx->ec_group = EC_GROUP_new_curve_GFp(ctx->p, ctx->a, ctx->b, ctx->bn_ctx);
if(ctx->ec_group == NULL) {
BN_free(ctx->p);
BN_free(ctx->a);
BN_free(ctx->b);
BN_CTX_free(ctx->bn_ctx);
return NULL;
}
ctx->q = BN_new();
BN_hex2bn(&ctx->q, "D35E472036BC4FB7E13C785ED201E065F98FCFA5B68F12A32D482EC7EE8658E98691555B44C59311");
ctx->g = EC_POINT_new(ctx->ec_group);
EC_POINT_hex2point(ctx->ec_group,
"0443BD7E9AFB53D8B85289BCC48EE5BFE6F20137D10A087EB6E7871E2A10A599C710AF8D0D39E2061114FDD05545EC1CC8AB4093247F77275E0743FFED117182EAA9C77877AAAC6AC7D35245D1692E8EE1",
ctx->g, ctx->bn_ctx);
EC_GROUP_set_generator(ctx->ec_group, ctx->g, ctx->q, BN_value_one());
return ctx;
}
示例8: test_Div_open_ssl
void test_Div_open_ssl()
{
myInt64 cycles;
myInt64 start;
int num_runs = NUM_RUNS;
BN_CTX *ctx;
ctx = BN_CTX_new();
BIGNUM *a = BN_new();
BIGNUM *b = BN_new();
BIGNUM *result = BN_new();
BIGNUM *rem = BN_new();
BN_hex2bn(&a, "7cd73b6fc007dfee34a23caf363ae67e8bb8782600000000032accceb");
BN_hex2bn(&b, "c69d8b898f0e43b4643a018e7b0569de6f8cf328e0bf6d59ace4e3bc2ca28d10");
start = start_tsc();
for(int i = 0; i < num_runs; i++)
{
BN_div(result, rem, b, a, ctx);
}
cycles = stop_tsc(start);
double r;
r = cycles / num_runs;
printf("RDTSC instruction:\n %lf cycles measured => %lf seconds, assuming frequency is %lf MHz. (change in source file if different)\n", r, r/(FREQUENCY), (FREQUENCY)/1e6);
BN_CTX_free(ctx);
BN_free(a);
BN_free(b);
BN_free(result);
BN_free(rem);
}
示例9: printf
static DH *dh_new_group_asc(const char *gen, const char *modulus)
{
DH *dh = NULL;
if ((dh = DH_new()) == NULL) {
printf("dh_new_group_asc: DH_new");
goto error;
}
// PとGは公開してもよい素数の組み合わせ
if (BN_hex2bn(&dh->p, modulus) == 0) {
printf("BN_hex2bn p");
goto error;
}
if (BN_hex2bn(&dh->g, gen) == 0) {
printf("BN_hex2bn g");
goto error;
}
return (dh);
error:
DH_free(dh);
return (NULL);
}
示例10: main
int main(int argc,char **argv)
{
ops_create_info_t *info;
const unsigned char *id;
const char *nstr;
const char *estr;
BIGNUM *n=NULL;
BIGNUM *e=NULL;
if(argc != 4)
{
fprintf(stderr,"%s <n> <e> <user id>\n",argv[0]);
exit(1);
}
nstr=argv[1];
estr=argv[2];
id=(unsigned char *)argv[3];
BN_hex2bn(&n,nstr);
BN_hex2bn(&e,estr);
info=ops_create_info_new();
ops_writer_set_fd(info,1);
ops_write_rsa_public_key(time(NULL),n,e,info);
ops_write_user_id(id,info);
ops_create_info_delete(info);
return 0;
}
示例11: Encode_RSA
void Encode_RSA(unsigned char *in,int fd)
{
RSA *r;
BIGNUM *bne,*bnn,*bnd;
int bits = 1024, ret, len, flen, padding, i;
unsigned char *key, *p;
BIO *b;
unsigned char *encData,*decData,*tmpData;//加密后的数据/解密后的数据/临时指针
/* Key data */
unsigned long e = 75011;
/* 构建RSA数据结构 */
bne = BN_new();
bnd = BN_new();
bnn = BN_new();
ret = BN_set_word(bne, e);
BN_hex2bn(&bnd, PRIVATE);
BN_hex2bn(&bnn, MODULUS);
r = RSA_new();
r->e=bne;
r->d=bnd;
r->n=bnn;
/* Debug output key */
/*RSA_print_fp(stdout, r, 5);*/
/*准备输出的加密数据结构 */
flen = RSA_size(r);// - 11;
encData = (unsigned char *)malloc(flen);
bzero(encData, flen);//memset(encData, 0, flen);
ret = RSA_private_encrypt(flen, in, encData, r, RSA_NO_PADDING);
if(ret < 0){
JCG("Encrypt failed!\n");
return;
}
write(fd,encData,flen);
tmpData=encData;
#if 0
for (i=0; i<ret; i++)
{
JDG("0x%02x, ", *tmpData);
if(i%16 == 7)
JDG("\t");
else if(i%16 == 15)
JDG("\n");
tmpData++;
}
JDG("\n");
JCG("end private encrypt ");
#endif
free(encData);
RSA_free(r);
}
示例12: read_rsa_private_key
bool read_rsa_private_key(void) {
FILE *fp;
char *fname, *key, *pubkey;
struct stat s;
if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
if(!get_config_string(lookup_config(config_tree, "PublicKey"), &pubkey)) {
logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
return false;
}
myself->connection->rsa_key = RSA_new();
// RSA_blinding_on(myself->connection->rsa_key, NULL);
BN_hex2bn(&myself->connection->rsa_key->d, key);
BN_hex2bn(&myself->connection->rsa_key->n, pubkey);
BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
free(key);
free(pubkey);
return true;
}
if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
xasprintf(&fname, "%s/rsa_key.priv", confbase);
fp = fopen(fname, "r");
if(!fp) {
logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
fname, strerror(errno));
free(fname);
return false;
}
#if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
if(fstat(fileno(fp), &s)) {
logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'",
fname, strerror(errno));
free(fname);
return false;
}
if(s.st_mode & ~0100700)
logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
#endif
myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
fclose(fp);
if(!myself->connection->rsa_key) {
logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s",
fname, strerror(errno));
free(fname);
return false;
}
free(fname);
return true;
}
示例13: BN_hex2bn
Person::Person(const char *p_str, const char *g_str) {
p = NULL;
g = NULL;
B = NULL;
hash = NULL;
BN_hex2bn(&p, p_str);
BN_hex2bn(&g, g_str);
set_keys();
}
示例14: get_own_data
void get_own_data(sqlite3* db, char** nickname, BIGNUM* e, BIGNUM* n, BIGNUM* d) {
sqlite3_stmt *stmt;
char* statement = "SELECT * FROM keys WHERE id = 0";
sqlite3_prepare_v2(db, statement, strlen(statement) + 1 , &stmt, NULL);
sqlite3_step(stmt);
*nickname = (char*) sqlite3_column_text(stmt, 1);
BN_hex2bn(&e, (const char*) sqlite3_column_text(stmt, 2));
BN_hex2bn(&n, (const char*) sqlite3_column_text(stmt, 3));
BN_hex2bn(&d, (const char*) sqlite3_column_text(stmt, 4));
}
示例15: test_BN_cmp
static int
test_BN_cmp(void)
{
BIGNUM *a, *b;
a = BN_new();
b = BN_new();
if (!BN_set_word(a, 1))
return 1;
if (!BN_set_word(b, 1))
return 1;
if (BN_cmp(a, b) != 0)
return 1;
if (BN_cmp(b, a) != 0)
return 1;
if (!BN_set_word(b, 2))
return 1;
if (BN_cmp(a, b) >= 0)
return 1;
if (BN_cmp(b, a) <= 0)
return 1;
BN_set_negative(b, 1);
if (BN_cmp(a, b) <= 0)
return 1;
if (BN_cmp(b, a) >= 0)
return 1;
BN_free(a);
BN_free(b);
BN_hex2bn(&a, "50212A3B611D46642C825A16A354CE0FD4D85DD1");
BN_hex2bn(&b, "50212A3B611D46642C825A16A354CE0FD4D85DD2");
if (BN_cmp(a, b) >= 0)
return 1;
if (BN_cmp(b, a) <= 0)
return 1;
BN_set_negative(b, 1);
if (BN_cmp(a, b) <= 0)
return 1;
if (BN_cmp(b, a) >= 0)
return 1;
BN_free(a);
BN_free(b);
return 0;
}