本文整理汇总了C++中RSA_generate_key_ex函数的典型用法代码示例。如果您正苦于以下问题:C++ RSA_generate_key_ex函数的具体用法?C++ RSA_generate_key_ex怎么用?C++ RSA_generate_key_ex使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RSA_generate_key_ex函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: _pRSA
RSAKeyImpl::RSAKeyImpl(int keyLength, unsigned long exponent):
_pRSA(0)
{
#if OPENSSL_VERSION_NUMBER >= 0x00908000L
_pRSA = RSA_new();
int ret = 0;
BIGNUM* bn = 0;
try
{
bn = BN_new();
BN_set_word(bn, exponent);
ret = RSA_generate_key_ex(_pRSA, keyLength, bn, 0);
BN_free(bn);
}
catch (...)
{
BN_free(bn);
throw;
}
if (!ret) throw Poco::InvalidArgumentException("Failed to create RSA context");
#else
_pRSA = RSA_generate_key(keyLength, exponent, 0, 0);
if (!_pRSA) throw Poco::InvalidArgumentException("Failed to create RSA context");
#endif
}
示例2: main
int main(void)
{
BIO* bio_out;
RSA* key_pair = NULL;
BIGNUM* public_key_exponent = NULL;
unsigned char seed_data[ENTROPY_SIZE];
/* Setup the output */
bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
/* Before generating the keys, the pseudo-random number generator must be seeded */
obtain_seed_data(seed_data, ENTROPY_SIZE);
RAND_seed(seed_data, ENTROPY_SIZE);
BIO_printf(bio_out, "\nGenereating key pair:\n");
/* Generate a 2048-bit key pair with a public exponent of 65537 (RSA_F4) */
public_key_exponent = BN_new();
key_pair = RSA_new();
BN_set_word(public_key_exponent, RSA_F4);
RSA_generate_key_ex(key_pair, 2048, public_key_exponent, NULL);
BIO_printf(bio_out, "-----------------------\n\n");
BIO_printf(bio_out, "Value for the modulus \"n\":\n");
BN_print(bio_out, key_pair -> n);
BIO_printf(bio_out, "\n\n");
BIO_printf(bio_out, "Value for the distinct prime, \"p\":\n");
BN_print(bio_out, key_pair -> p);
BIO_printf(bio_out, "\n\n");
BIO_printf(bio_out, "Value for the distinct prime, \"q\":\n");
BN_print(bio_out, key_pair -> q);
BIO_printf(bio_out, "\n\n");
BIO_printf(bio_out, "Value for \"dP\":\n");
BN_print(bio_out, key_pair -> dmp1);
BIO_printf(bio_out, "\n\n");
BIO_printf(bio_out, "Value for \"dQ\":\n");
BN_print(bio_out, key_pair -> dmq1);
BIO_printf(bio_out, "\n\n");
BIO_printf(bio_out, "Value for \"qInv\":\n");
BN_print(bio_out, key_pair -> iqmp);
BIO_printf(bio_out, "\n\n");
BIO_printf(bio_out, "Value for the public key exponent \"e\":\n");
BN_print(bio_out, key_pair -> e);
BIO_printf(bio_out, "\n\n");
BIO_printf(bio_out, "Value for the private key exponent \"d\":\n");
BN_print(bio_out, key_pair -> d);
BIO_printf(bio_out, "\n\n");
return 0;
}
示例3: CreatePrivateKey
unsigned char* CreatePrivateKey(size_t* len, size_t* pubLen) {
//MOD, PUB_EXP, PRIV_EXP
RSA* msa = RSA_new();
BIGNUM* e = BN_new();
BN_set_word(e, 65537);
RSA_generate_key_ex(msa, 4096, e, 0);
BN_free(e);
size_t pubSize = 4+BN_num_bytes(msa->n)+4+BN_num_bytes(msa->e);
size_t privSize = 4+BN_num_bytes(msa->d);
unsigned char* retval = (unsigned char*)malloc(pubSize+privSize);
unsigned char* izard = retval;
uint32_t count = BN_num_bytes(msa->n);
memcpy(izard, &count, 4);
izard+=4;
BN_bn2bin(msa->n, izard);
izard+=count;
count = BN_num_bytes(msa->e);
memcpy(izard, &count, 4);
izard+=4;
BN_bn2bin(msa->e, izard);
izard+=count;
count = BN_num_bytes(msa->d);
memcpy(izard, &count, 4);
izard+=4;
BN_bn2bin(msa->d, izard);
*len = pubSize+privSize;
*pubLen = pubSize;
RSA_free(msa);
return retval;
}
示例4: test_bad_key
static int test_bad_key(void) {
RSA *key = RSA_new();
BIGNUM e;
BN_init(&e);
BN_set_word(&e, RSA_F4);
if (!RSA_generate_key_ex(key, 512, &e, NULL)) {
fprintf(stderr, "RSA_generate_key_ex failed.\n");
ERR_print_errors_fp(stderr);
return 0;
}
if (!BN_add(key->p, key->p, BN_value_one())) {
fprintf(stderr, "BN error.\n");
ERR_print_errors_fp(stderr);
return 0;
}
if (RSA_check_key(key)) {
fprintf(stderr, "RSA_check_key passed with invalid key!\n");
return 0;
}
ERR_clear_error();
BN_free(&e);
RSA_free(key);
return 1;
}
示例5: 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 };
int i, 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;
}
示例6: generate_private_key
static EVP_PKEY * generate_private_key (void)
{
RSA *rsa = RSA_new();
BIGNUM *bn = BN_new();
EVP_PKEY *pkey;
/*
* create an RSA keypair and assign them to a PKEY and return it.
*/
BN_set_word(bn, 0x10001);
RSA_generate_key_ex(rsa, 1024, bn, NULL);
pkey = EVP_PKEY_new();
if (pkey==NULL) {
printf("\nError allocating PKEY structure for new key pair\n");
return NULL;
}
if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
printf("\nError assigning RSA key pair to PKEY structure\n");
return NULL;
}
RSA_free(rsa);
BN_free(bn);
return (pkey);
}
示例7: lws_tls_openssl_rsa_new_key
static int
lws_tls_openssl_rsa_new_key(RSA **rsa, int bits)
{
BIGNUM *bn = BN_new();
int n;
if (!bn)
return 1;
if (BN_set_word(bn, RSA_F4) != 1) {
BN_free(bn);
return 1;
}
*rsa = RSA_new();
if (!*rsa) {
BN_free(bn);
return 1;
}
n = RSA_generate_key_ex(*rsa, bits, bn, NULL);
BN_free(bn);
if (n == 1)
return 0;
RSA_free(*rsa);
*rsa = NULL;
return 1;
}
示例8: 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, cb;
int ret;
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 (ctx->pkey_gencb)
{
pcb = &cb;
evp_pkey_set_cb_translate(pcb, ctx);
}
else
pcb = NULL;
ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
if (ret > 0)
EVP_PKEY_assign_RSA(pkey, rsa);
else
RSA_free(rsa);
return ret;
}
示例9: void
RSA *RSA_generate_key(int bits, unsigned long e_value,
void (*callback)(int,int,void *), void *cb_arg)
{
BN_GENCB cb;
int i;
RSA *rsa = RSA_new();
BIGNUM *e = BN_new();
if(!rsa || !e) goto err;
/* The problem is when building with 8, 16, or 32 BN_ULONG,
* unsigned long can be larger */
for (i=0; i<(int)sizeof(unsigned long)*8; i++)
{
if (e_value & (1UL<<i))
BN_set_bit(e,i);
}
BN_GENCB_set_old(&cb, callback, cb_arg);
if(RSA_generate_key_ex(rsa, bits, e, &cb)) {
BN_free(e);
return rsa;
}
err:
if(e) BN_free(e);
if(rsa) RSA_free(rsa);
return 0;
}
示例10: rsa_genkey
static RSA*
rsa_genkey(u32 size)
{
zassert(size >= DNSSEC_MINIMUM_KEY_SIZE && size <= DNSSEC_MAXIMUM_KEY_SIZE);
BN_CTX *ctx;
BIGNUM *e;
RSA* rsa;
ctx = BN_CTX_new();
zassert(ctx != NULL);
e = BN_new();
BN_set_word(e, 0x10001);
zassert(e != NULL);
rsa = RSA_new();
zassert(rsa != NULL);
int err = RSA_generate_key_ex(rsa, size, e, NULL); /* no callback */
if(err == 0)
{
RSA_free(rsa);
rsa = NULL;
}
BN_free(e);
BN_CTX_free(ctx);
return rsa;
}
示例11: GenerateRSAKeyPair
bool GenerateRSAKeyPair(int numBits, std::string& privKey, std::string& pubKey)
{
// TODO: add some error checking
RSA* rsa = RSA_new();
BIGNUM* bn = BN_new();
BN_GENCB cb;
BIO* bio_err = NULL;
BN_GENCB_set(&cb, genrsa_cb, bio_err);
BN_set_word(bn, RSA_F4);
RSA_generate_key_ex(rsa, numBits, bn, &cb);
BIO* privKeyBuff = BIO_new(BIO_s_mem());
BIO* pubKeyBuff = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(privKeyBuff, rsa, 0, 0, 0, 0, 0);
PEM_write_bio_RSA_PUBKEY(pubKeyBuff, rsa); // RSA_PUBKEY includes some data that RSAPublicKey doesn't have
char* privKeyData;
char* pubKeyData;
auto privKeySize = BIO_get_mem_data(privKeyBuff, &privKeyData);
auto pubKeySize = BIO_get_mem_data(pubKeyBuff, &pubKeyData);
privKey = std::string(privKeyData, privKeySize);
pubKey = std::string(pubKeyData, pubKeySize);
BIO_free_all(privKeyBuff);
BIO_free_all(pubKeyBuff);
BN_free(bn);
RSA_free(rsa);
return true;
}
示例12: keygen_init
int keygen_init(void)
{
m_bignumber = BN_new();
if(!m_bignumber)
{
fprintf(stderr, "Failed to init bignumber\n");
return -1;
}
m_rsa=RSA_new();
if(!m_rsa)
{
fprintf(stderr, "Failed to create RSA context\n");
return -1;
}
if(!BN_set_word(m_bignumber, RSA_F4) || !RSA_generate_key_ex(m_rsa,RSA_KEY_BITS,m_bignumber,NULL))
{
fprintf(stderr, "Failed to generate RSA key\n");
return -1;
}
m_evpkey=EVP_PKEY_new();
if(!EVP_PKEY_set1_RSA(m_evpkey, m_rsa))
{
fprintf(stderr, "Unable to convert RSA key to EVP key\n");
return -1;
}
m_p8info=EVP_PKEY2PKCS8(m_evpkey);
if(!m_p8info)
{
fprintf(stderr, "Failed to convert EVP to PKCS8\n");
return -1;
}
return 0;
}
示例13: MakeKey
// Generate a key pair. Caller is responsible for freeing the returned object.
static EVP_PKEY* MakeKey() {
LOG(LS_INFO) << "Making key pair";
EVP_PKEY* pkey = EVP_PKEY_new();
#if OPENSSL_VERSION_NUMBER < 0x00908000l
// Only RSA_generate_key is available. Use that.
RSA* rsa = RSA_generate_key(KEY_LENGTH, 0x10001, NULL, NULL);
if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
EVP_PKEY_free(pkey);
RSA_free(rsa);
return NULL;
}
#else
// RSA_generate_key is deprecated. Use _ex version.
BIGNUM* exponent = BN_new();
RSA* rsa = RSA_new();
if (!pkey || !exponent || !rsa ||
!BN_set_word(exponent, 0x10001) || // 65537 RSA exponent
!RSA_generate_key_ex(rsa, KEY_LENGTH, exponent, NULL) ||
!EVP_PKEY_assign_RSA(pkey, rsa)) {
EVP_PKEY_free(pkey);
BN_free(exponent);
RSA_free(rsa);
return NULL;
}
// ownership of rsa struct was assigned, don't free it.
BN_free(exponent);
#endif
LOG(LS_INFO) << "Returning key pair";
return pkey;
}
示例14: seed_rng
static RSA *gen_rsa(void) {
if (!RAND_status())
seed_rng();
int ret = 0;
_Thread_local static int e_init = 0;
_Thread_local static BIGNUM e;
RSA *r = NULL;
if (!e_init) {
BN_init(&e);
if (!BN_set_word(&e, 65537)) {
warnx("BN_set_word");
goto fail;
}
e_init = 1;
}
r = RSA_new();
if (!r) {
warnx("RSA_new");
goto fail;
}
ret = RSA_generate_key_ex(r, 1024, &e, NULL);
if (!ret) {
warnx("RSA_generate_key");
goto fail;
}
return r;
fail:
if (r) {
RSA_free(r);
r = NULL;
}
return NULL;
}
示例15: genRsaKey
int genRsaKey(const int bits, char * privkey)
{
BIO * out = BIO_new(BIO_s_mem());
RSA * rsa = 0;
BIGNUM * bn = 0;
int err = 0;
if (!(rsa = RSA_new())) return -1;
if (!(bn = BN_new())) return -2;
if (!(err = BN_set_word(bn,RSA_F4))) {
BN_free(bn);
return err;
}
if (!(err = RSA_generate_key_ex(rsa,bits,bn,NULL))) {
BN_free(bn);
RSA_free(rsa);
return err;
}
if (!(err = PEM_write_bio_RSAPrivateKey(out, rsa, NULL, NULL, 0, NULL, NULL))) {
BIO_free_all(out);
BN_free(bn);
RSA_free(rsa);
return err;
}
if (!(err = BIO_read(out,privkey,bits) <= 0)) {
BIO_free_all(out);
BN_free(bn);
RSA_free(rsa);
return err;
}
return 0;
}