FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/evp.c
/* with set1 functions the pkey struct does not own the RSA structure
 *
 * returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
 */
with set1 functions the pkey struct does not own the RSA structure
returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
with set1 functions the pkey struct does not own the RSA structure returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
int wolfSSL_EVP_PKEY_set1_RSA(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_RSA *key)
{
#if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
    int derMax = 0;
    int derSz  = 0;
    byte* derBuf = NULL;
    RsaKey* rsa  = NULL;
#endif
    WOLFSSL_ENTER("wolfSSL_EVP_PKEY_set1_RSA");
    if ((pkey == NULL) || (key == NULL))
        return WOLFSSL_FAILURE;
    if (pkey->rsa != NULL && pkey->ownRsa == 1) {
        wolfSSL_RSA_free(pkey->rsa);
    }
    pkey->rsa    = key;
    pkey->ownRsa = 0; 
    pkey->type   = EVP_PKEY_RSA;
    if (key->inSet == 0) {
        if (SetRsaInternal(key) != WOLFSSL_SUCCESS) {
            WOLFSSL_MSG("SetRsaInternal failed");
            return WOLFSSL_FAILURE;
        }
    }
#if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
    rsa = (RsaKey*)key->internal;
    derMax = 5 * wolfSSL_RSA_size(key) + (2 * AES_BLOCK_SIZE);
    derBuf = (byte*)XMALLOC(derMax, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
    if (derBuf == NULL) {
        WOLFSSL_MSG("malloc failed");
        return WOLFSSL_FAILURE;
    }
    if (rsa->type == RSA_PRIVATE) {
        derSz = wc_RsaKeyToDer(rsa, derBuf, derMax);
    }
    else {
        derSz = wc_RsaKeyToPublicDer(rsa, derBuf, derMax);
    }
    if (derSz < 0) {
        if (rsa->type == RSA_PRIVATE) {
            WOLFSSL_MSG("wc_RsaKeyToDer failed");
        }
        else {
            WOLFSSL_MSG("wc_RsaKeyToPublicDer failed");
        }
        XFREE(derBuf, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
        return WOLFSSL_FAILURE;
    }
    pkey->pkey.ptr = (char*)XMALLOC(derSz, pkey->heap, DYNAMIC_TYPE_DER);
    if (pkey->pkey.ptr == NULL) {
        WOLFSSL_MSG("key malloc failed");
        XFREE(derBuf, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
        return WOLFSSL_FAILURE;
    }
    pkey->pkey_sz = derSz;
    XMEMCPY(pkey->pkey.ptr, derBuf, derSz);
    XFREE(derBuf, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif 
#ifdef WC_RSA_BLINDING
    if (key->ownRng == 0) {
        if (wc_RsaSetRNG((RsaKey*)(pkey->rsa->internal), &(pkey->rng)) != 0) {
            WOLFSSL_MSG("Error setting RSA rng");
            return WOLFSSL_FAILURE;
        }
    }
#endif
    return WOLFSSL_SUCCESS;
}
int wolfSSL_EVP_PKEY_set1_RSA ( WOLFSSL_EVP_PKEY * pkey , WOLFSSL_RSA * key ) { #if defined ( WOLFSSL_KEY_GEN ) && ! defined ( HAVE_USER_RSA ) int derMax = 0 ; int derSz = 0 ; byte * derBuf = NULL ; RsaKey * rsa = NULL ; #endif WOLFSSL_ENTER ( " " ) ; if ( ( pkey == NULL ) || ( key == NULL ) ) return WOLFSSL_FAILURE ; if ( pkey -> rsa != NULL && pkey -> ownRsa == 1 ) { wolfSSL_RSA_free ( pkey -> rsa ) ; } pkey -> rsa = key ; pkey -> ownRsa = 0 ; pkey -> type = EVP_PKEY_RSA ; if ( key -> inSet == 0 ) { if ( SetRsaInternal ( key ) != WOLFSSL_SUCCESS ) { WOLFSSL_MSG ( " " ) ; return WOLFSSL_FAILURE ; } } #if defined ( WOLFSSL_KEY_GEN ) && ! defined ( HAVE_USER_RSA ) rsa = ( RsaKey * ) key -> internal ; derMax = 5 * wolfSSL_RSA_size ( key ) + ( 2 * AES_BLOCK_SIZE ) ; derBuf = ( byte * ) XMALLOC ( derMax , pkey -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( derBuf == NULL ) { WOLFSSL_MSG ( " " ) ; return WOLFSSL_FAILURE ; } if ( rsa -> type == RSA_PRIVATE ) { derSz = wc_RsaKeyToDer ( rsa , derBuf , derMax ) ; } else { derSz = wc_RsaKeyToPublicDer ( rsa , derBuf , derMax ) ; } if ( derSz < 0 ) { if ( rsa -> type == RSA_PRIVATE ) { WOLFSSL_MSG ( " " ) ; } else { WOLFSSL_MSG ( " " ) ; } XFREE ( derBuf , pkey -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; return WOLFSSL_FAILURE ; } pkey -> pkey . ptr = ( char * ) XMALLOC ( derSz , pkey -> heap , DYNAMIC_TYPE_DER ) ; if ( pkey -> pkey . ptr == NULL ) { WOLFSSL_MSG ( " " ) ; XFREE ( derBuf , pkey -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; return WOLFSSL_FAILURE ; } pkey -> pkey_sz = derSz ; XMEMCPY ( pkey -> pkey . ptr , derBuf , derSz ) ; XFREE ( derBuf , pkey -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; #endif #ifdef WC_RSA_BLINDING if ( key -> ownRng == 0 ) { if ( wc_RsaSetRNG ( ( RsaKey * ) ( pkey -> rsa -> internal ) , & ( pkey -> rng ) ) != 0 ) { WOLFSSL_MSG ( " " ) ; return WOLFSSL_FAILURE ; } } #endif return WOLFSSL_SUCCESS ; }
with set1 functions the pkey struct does not own the RSA structure
 returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
with set1 functions the pkey struct does not own the RSA structure returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
/* pkey does not own RSA */ /* 5 > size of n, d, p, q, d%(p-1), d(q-1), 1/q%p, e + ASN.1 additional
     * information */ /* Private key to DER */ /* Public key to DER */ /* WOLFSSL_KEY_GEN && !HAVE_USER_RSA */