This working RSA OpenSSL code
void SwapBytes( unsigned char *pv, size_t n )
{
unsigned char *p = pv;
size_t lo, hi;
for ( lo = 0, hi = n - 1; hi > lo; lo++, hi-- )
{
char tmp = p[lo];
p[lo] = p[hi];
p[hi] = tmp;
}
}
void RSA(unsigned char *plaintext, unsigned char *ciphertext)
{
BIGNUM *bnN = NULL;
BIGNUM *bnE = NULL;
RSA *keys = RSA_new();
BN_hex2bn(&bnN, modulus);
BN_hex2bn(&bnE, public_exp);
RSA_set0_key(keys, bnN, bnE, NULL);
int modulus_size = RSA_size(keys);
SwapBytes(plaintext, modulus_size);
int cipher_len = RSA_public_encrypt(modulus_size, plaintext, ciphertext, keys, RSA_NO_PADDING);
RSA_free(keys);
SwapBytes(ciphertext, modulus_size);
}
when compiled produces deprecated warnings such as
/mnt/c/Projects/src/rsa.cpp:37:102: warning: ‘int RSA_public_encrypt(int, const unsigned char*, unsigned char*, RSA*, int)’ is deprecated: Since OpenSSL 3.0 [-Wdeprecated-declarations]
37 | int cipher_len = RSA_public_encrypt(modulus_size, plaintext, ciphertext, keys, RSA_NO_PADDING);
which can be suppressed using this compiler option
-Wno-deprecated-declarations
However, the OpenSSL dev team notes:
Use of the low level APIs has been informally discouraged by the OpenSSL dev team for a long time. However in OpenSSL 3.0 this is made more formal. All such low level APIs have been deprecated. You may still use them in your applications, but you may start to see deprecation warnings during compilation (dependent on compiler support for this). Deprecated APIs may be removed from future versions of OpenSSL so you are strongly encouraged to update your code to use the high level APIs instead.
Question
It's suggested to replace the above low level API's with EVP.
Is there an RSA example to replace the above code with OpenSSL EVP functions?