In order to generate an RSA key, an EVP_PKEY
must first be allocated with EVP_PKEY_new
:
EVP_PKEY *pkey;
pkey = EVP_PKEY_new();
An exponent for the key is also needed, which will require allocating a BIGNUM
with BN_new
and then assigning with BN_set_word
:
BIGNUM *bn;
bn = BN_new();
BN_set_word(bn, RSA_F4);
To generate the key, create a new RSA
with RSA_new
and call RSA_generate_key_ex
:
RSA *rsa;
rsa = RSA_new();
RSA_generate_key_ex(
rsa, /* pointer to the RSA structure */
2048, /* number of bits for the key - 2048 is a good value */
bn, /* exponent allocated earlier */
NULL, /* callback - can be NULL if progress isn't needed */
);
To assign the newly generated key to the EVP_PKEY
structure, call EVP_PKEY_assign_RSA
:
EVP_PKEY_assign_RSA(pkey, rsa);
The RSA
structure will be automatically freed when the EVP_PKEY
structure is freed. This is done with EVP_PKEY_free
:
EVP_PKEY_free(pkey);