mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-02 20:24:39 +02:00
EVP PKEY RSA encrypt/decrypt, tentative
This commit is contained in:
committed by
Jacob Barthelmeh
parent
a11e389bc8
commit
bebe60a4c1
91
src/ssl.c
91
src/ssl.c
@@ -20381,26 +20381,22 @@ int wolfSSL_RSA_blinding_on(WOLFSSL_RSA* rsa, WOLFSSL_BN_CTX* bn)
|
|||||||
int wolfSSL_RSA_public_encrypt(int len, const unsigned char* fr,
|
int wolfSSL_RSA_public_encrypt(int len, const unsigned char* fr,
|
||||||
unsigned char* to, WOLFSSL_RSA* rsa, int padding)
|
unsigned char* to, WOLFSSL_RSA* rsa, int padding)
|
||||||
{
|
{
|
||||||
int tlen = 0;
|
int initTmpRng = 0;
|
||||||
int initTmpRng = 0;
|
WC_RNG *rng = NULL;
|
||||||
WC_RNG* rng = NULL;
|
int outLen;
|
||||||
|
int ret = 0;
|
||||||
#ifdef WOLFSSL_SMALL_STACK
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
WC_RNG* tmpRNG = NULL;
|
WC_RNG* tmpRNG = NULL;
|
||||||
#else
|
#else
|
||||||
WC_RNG tmpRNG[1];
|
WC_RNG tmpRNG[1];
|
||||||
#endif
|
#endif
|
||||||
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
|
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
|
||||||
int mgf = WC_MGF1NONE;
|
int mgf = WC_MGF1NONE;
|
||||||
enum wc_HashType hash = WC_HASH_TYPE_NONE;
|
enum wc_HashType hash = WC_HASH_TYPE_NONE;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
WOLFSSL_MSG("wolfSSL_RSA_public_encrypt");
|
WOLFSSL_MSG("wolfSSL_RSA_public_encrypt");
|
||||||
|
|
||||||
if (rsa == NULL || rsa->internal == NULL || fr == NULL) {
|
|
||||||
WOLFSSL_MSG("Bad function arguments");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Check and remap the padding to internal values, if needed. */
|
/* Check and remap the padding to internal values, if needed. */
|
||||||
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
|
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
|
||||||
if (padding == RSA_PKCS1_PADDING)
|
if (padding == RSA_PKCS1_PADDING)
|
||||||
@@ -20421,23 +20417,23 @@ int wolfSSL_RSA_public_encrypt(int len, const unsigned char* fr,
|
|||||||
|
|
||||||
if (rsa->inSet == 0)
|
if (rsa->inSet == 0)
|
||||||
{
|
{
|
||||||
WOLFSSL_MSG("No RSA internal set, do it");
|
if (SetRsaInternal(rsa) != SSL_SUCCESS) {
|
||||||
|
|
||||||
if (SetRsaInternal(rsa) != WOLFSSL_SUCCESS) {
|
|
||||||
WOLFSSL_MSG("SetRsaInternal failed");
|
WOLFSSL_MSG("SetRsaInternal failed");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
outLen = wolfSSL_RSA_size(rsa);
|
||||||
|
|
||||||
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && \
|
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && \
|
||||||
!defined(HAVE_FAST_RSA) && defined(WC_RSA_BLINDING)
|
!defined(HAVE_FAST_RSA) && defined(WC_RSA_BLINDING)
|
||||||
rng = ((RsaKey*)rsa->internal)->rng;
|
rng = ((RsaKey*)rsa->internal)->rng;
|
||||||
#endif
|
#endif
|
||||||
if (rng == NULL) {
|
if (rng == NULL) {
|
||||||
#ifdef WOLFSSL_SMALL_STACK
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
|
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
if (tmpRNG == NULL)
|
if (tmpRNG == NULL)
|
||||||
return WOLFSSL_FATAL_ERROR;
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (wc_InitRng(tmpRNG) == 0) {
|
if (wc_InitRng(tmpRNG) == 0) {
|
||||||
@@ -20453,30 +20449,38 @@ int wolfSSL_RSA_public_encrypt(int len, const unsigned char* fr,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* size of 'to' buffer must be size of RSA key */
|
if (outLen == 0) {
|
||||||
|
WOLFSSL_MSG("Bad RSA size");
|
||||||
|
}
|
||||||
|
|
||||||
if (rng) {
|
if (rng) {
|
||||||
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
|
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
|
||||||
tlen = wc_RsaPublicEncrypt_ex(fr, len, to, wolfSSL_RSA_size(rsa),
|
ret = wc_RsaPublicEncrypt_ex(fr, len, to, outLen,
|
||||||
(RsaKey*)rsa->internal, rng, padding,
|
(RsaKey*)rsa->internal, rng, padding,
|
||||||
hash, mgf, NULL, 0);
|
hash, mgf, NULL, 0);
|
||||||
#else
|
#else
|
||||||
tlen = wc_RsaPublicEncrypt(fr, len, to, wolfSSL_RSA_size(rsa),
|
ret = wc_RsaPublicEncrypt(fr, len, to, outLen,
|
||||||
(RsaKey*)rsa->internal, rng);
|
(RsaKey*)rsa->internal, rng);
|
||||||
#endif
|
#endif
|
||||||
if (tlen <= 0) {
|
if (ret <= 0) {
|
||||||
WOLFSSL_MSG("wolfSSL_RSA_public_encrypt failed");
|
WOLFSSL_MSG("Bad Rsa Encrypt");
|
||||||
}
|
|
||||||
else {
|
|
||||||
WOLFSSL_MSG("wolfSSL_RSA_public_encrypt success");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (initTmpRng)
|
if (initTmpRng)
|
||||||
wc_FreeRng(tmpRNG);
|
wc_FreeRng(tmpRNG);
|
||||||
|
|
||||||
#ifdef WOLFSSL_SMALL_STACK
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
|
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
#endif
|
#endif
|
||||||
return tlen;
|
|
||||||
|
if (ret > 0)
|
||||||
|
WOLFSSL_MSG("wolfSSL_RSA_public_encrypt success");
|
||||||
|
else {
|
||||||
|
WOLFSSL_MSG("wolfSSL_RSA_public_encrypt failed");
|
||||||
|
ret = WOLFSSL_FATAL_ERROR; /* return -1 on error case */
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return compliant with OpenSSL
|
/* return compliant with OpenSSL
|
||||||
@@ -20485,20 +20489,15 @@ int wolfSSL_RSA_public_encrypt(int len, const unsigned char* fr,
|
|||||||
int wolfSSL_RSA_private_decrypt(int len, const unsigned char* fr,
|
int wolfSSL_RSA_private_decrypt(int len, const unsigned char* fr,
|
||||||
unsigned char* to, WOLFSSL_RSA* rsa, int padding)
|
unsigned char* to, WOLFSSL_RSA* rsa, int padding)
|
||||||
{
|
{
|
||||||
int tlen = 0;
|
int outLen;
|
||||||
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
|
int ret = 0;
|
||||||
|
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
|
||||||
int mgf = WC_MGF1NONE;
|
int mgf = WC_MGF1NONE;
|
||||||
enum wc_HashType hash = WC_HASH_TYPE_NONE;
|
enum wc_HashType hash = WC_HASH_TYPE_NONE;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
WOLFSSL_MSG("wolfSSL_RSA_private_decrypt");
|
WOLFSSL_MSG("wolfSSL_RSA_private_decrypt");
|
||||||
|
|
||||||
if (rsa == NULL || rsa->internal == NULL || fr == NULL) {
|
|
||||||
WOLFSSL_MSG("Bad function arguments");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Check and remap the padding to internal values, if needed. */
|
|
||||||
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
|
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
|
||||||
if (padding == RSA_PKCS1_PADDING)
|
if (padding == RSA_PKCS1_PADDING)
|
||||||
padding = WC_RSA_PKCSV15_PAD;
|
padding = WC_RSA_PKCSV15_PAD;
|
||||||
@@ -20518,30 +20517,38 @@ int wolfSSL_RSA_private_decrypt(int len, const unsigned char* fr,
|
|||||||
|
|
||||||
if (rsa->inSet == 0)
|
if (rsa->inSet == 0)
|
||||||
{
|
{
|
||||||
WOLFSSL_MSG("No RSA internal set, do it");
|
if (SetRsaInternal(rsa) != SSL_SUCCESS) {
|
||||||
|
|
||||||
if (SetRsaInternal(rsa) != WOLFSSL_SUCCESS) {
|
|
||||||
WOLFSSL_MSG("SetRsaInternal failed");
|
WOLFSSL_MSG("SetRsaInternal failed");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
outLen = wolfSSL_RSA_size(rsa);
|
||||||
|
if (outLen == 0) {
|
||||||
|
WOLFSSL_MSG("Bad RSA size");
|
||||||
|
}
|
||||||
|
|
||||||
/* size of 'to' buffer must be size of RSA key */
|
/* size of 'to' buffer must be size of RSA key */
|
||||||
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
|
#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
|
||||||
tlen = wc_RsaPrivateDecrypt_ex(fr, len, to, wolfSSL_RSA_size(rsa),
|
ret = wc_RsaPrivateDecrypt_ex(fr, len, to, outLen,
|
||||||
(RsaKey*)rsa->internal, padding,
|
(RsaKey*)rsa->internal, padding,
|
||||||
hash, mgf, NULL, 0);
|
hash, mgf, NULL, 0);
|
||||||
#else
|
#else
|
||||||
tlen = wc_RsaPrivateDecrypt(fr, len, to, wolfSSL_RSA_size(rsa),
|
ret = wc_RsaPrivateDecrypt(fr, len, to, outLen,
|
||||||
(RsaKey*)rsa->internal);
|
(RsaKey*)rsa->internal);
|
||||||
#endif
|
#endif
|
||||||
if (tlen <= 0) {
|
|
||||||
WOLFSSL_MSG("wolfSSL_RSA_private_decrypt failed");
|
if (len <= 0) {
|
||||||
|
WOLFSSL_MSG("Bad Rsa Decrypt");
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
|
if (ret > 0)
|
||||||
WOLFSSL_MSG("wolfSSL_RSA_private_decrypt success");
|
WOLFSSL_MSG("wolfSSL_RSA_private_decrypt success");
|
||||||
|
else {
|
||||||
|
WOLFSSL_MSG("wolfSSL_RSA_private_decrypt failed");
|
||||||
|
ret = WOLFSSL_FATAL_ERROR;
|
||||||
}
|
}
|
||||||
return tlen;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return compliant with OpenSSL
|
/* return compliant with OpenSSL
|
||||||
|
@@ -590,15 +590,8 @@ WOLFSSL_API int wolfSSL_EVP_add_digest(const WOLFSSL_EVP_MD *digest)
|
|||||||
{
|
{
|
||||||
(void)digest;
|
(void)digest;
|
||||||
/* nothing to do */
|
/* nothing to do */
|
||||||
return 0;
|
return 0;}
|
||||||
}
|
|
||||||
|
|
||||||
WOLFSSL_API int wolfSSL_EVP_PKEY_bits(const WOLFSSL_EVP_PKEY *pkey)
|
|
||||||
{
|
|
||||||
if (pkey == NULL)return 0;
|
|
||||||
WOLFSSL_ENTER("EVP_PKEY_bits");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
WOLFSSL_API int wolfSSL_EVP_PKEY_CTX_free(WOLFSSL_EVP_PKEY_CTX *ctx)
|
WOLFSSL_API int wolfSSL_EVP_PKEY_CTX_free(WOLFSSL_EVP_PKEY_CTX *ctx)
|
||||||
{
|
{
|
||||||
@@ -708,6 +701,16 @@ WOLFSSL_API int wolfSSL_EVP_PKEY_encrypt_init(WOLFSSL_EVP_PKEY_CTX *ctx)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WOLFSSL_API int wolfSSL_EVP_PKEY_bits(const WOLFSSL_EVP_PKEY *pkey)
|
||||||
|
{
|
||||||
|
int bytes;
|
||||||
|
|
||||||
|
if (pkey == NULL)return 0;
|
||||||
|
WOLFSSL_ENTER("EVP_PKEY_bits");
|
||||||
|
if((bytes = wolfSSL_EVP_PKEY_size((WOLFSSL_EVP_PKEY*)pkey)) ==0)return 0;
|
||||||
|
return bytes*8 ;
|
||||||
|
}
|
||||||
|
|
||||||
WOLFSSL_API int wolfSSL_EVP_PKEY_size(WOLFSSL_EVP_PKEY *pkey)
|
WOLFSSL_API int wolfSSL_EVP_PKEY_size(WOLFSSL_EVP_PKEY *pkey)
|
||||||
{
|
{
|
||||||
if (pkey == NULL)return 0;
|
if (pkey == NULL)return 0;
|
||||||
|
Reference in New Issue
Block a user