FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/evp.c
/* with set1 functions the pkey struct does not own the DSA structure
 *
 * returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
 */
with set1 functions the pkey struct does not own the DSA structure
returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
with set1 functions the pkey struct does not own the DSA structure returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
int wolfSSL_EVP_PKEY_set1_DSA(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_DSA *key)
{
    int derMax = 0;
    int derSz  = 0;
    DsaKey* dsa  = NULL;
    byte* derBuf = NULL;
    WOLFSSL_ENTER("wolfSSL_EVP_PKEY_set1_DSA");
    if((pkey == NULL) || (key == NULL))return WOLFSSL_FAILURE;
    if (pkey->dsa != NULL && pkey->ownDsa == 1) {
        wolfSSL_DSA_free(pkey->dsa);
    }
    pkey->dsa    = key;
    pkey->ownDsa = 0; 
    pkey->type   = EVP_PKEY_DSA;
    if (key->inSet == 0) {
        if (SetDsaInternal(key) != WOLFSSL_SUCCESS) {
            WOLFSSL_MSG("SetDsaInternal failed");
            return WOLFSSL_FAILURE;
        }
    }
    dsa = (DsaKey*)key->internal;
    derMax = 4 * wolfSSL_BN_num_bytes(key->g) + AES_BLOCK_SIZE;
    derBuf = (byte*)XMALLOC(derMax, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
    if (derBuf == NULL) {
        WOLFSSL_MSG("malloc failed");
        return WOLFSSL_FAILURE;
    }
    if (dsa->type == DSA_PRIVATE) {
        derSz = wc_DsaKeyToDer(dsa, derBuf, derMax);
    }
    else {
        derSz = wc_DsaKeyToPublicDer(dsa, derBuf, derMax);
    }
    if (derSz < 0) {
        if (dsa->type == DSA_PRIVATE) {
            WOLFSSL_MSG("wc_DsaKeyToDer failed");
        }
        else {
            WOLFSSL_MSG("wc_DsaKeyToPublicDer 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);
    return WOLFSSL_SUCCESS;
}
int wolfSSL_EVP_PKEY_set1_DSA ( WOLFSSL_EVP_PKEY * pkey , WOLFSSL_DSA * key ) { int derMax = 0 ; int derSz = 0 ; DsaKey * dsa = NULL ; byte * derBuf = NULL ; WOLFSSL_ENTER ( " " ) ; if ( ( pkey == NULL ) || ( key == NULL ) ) return WOLFSSL_FAILURE ; if ( pkey -> dsa != NULL && pkey -> ownDsa == 1 ) { wolfSSL_DSA_free ( pkey -> dsa ) ; } pkey -> dsa = key ; pkey -> ownDsa = 0 ; pkey -> type = EVP_PKEY_DSA ; if ( key -> inSet == 0 ) { if ( SetDsaInternal ( key ) != WOLFSSL_SUCCESS ) { WOLFSSL_MSG ( " " ) ; return WOLFSSL_FAILURE ; } } dsa = ( DsaKey * ) key -> internal ; derMax = 4 * wolfSSL_BN_num_bytes ( key -> g ) + AES_BLOCK_SIZE ; derBuf = ( byte * ) XMALLOC ( derMax , pkey -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( derBuf == NULL ) { WOLFSSL_MSG ( " " ) ; return WOLFSSL_FAILURE ; } if ( dsa -> type == DSA_PRIVATE ) { derSz = wc_DsaKeyToDer ( dsa , derBuf , derMax ) ; } else { derSz = wc_DsaKeyToPublicDer ( dsa , derBuf , derMax ) ; } if ( derSz < 0 ) { if ( dsa -> type == DSA_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 ) ; return WOLFSSL_SUCCESS ; }
with set1 functions the pkey struct does not own the DSA structure
 returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
with set1 functions the pkey struct does not own the DSA structure returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
/* pkey does not own DSA */ /* 4 > size of pub, priv, p, q, g + ASN.1 additional information */ /* Private key to DER */ /* Public key to DER */