FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/evp.c
/* with set1 functions the pkey struct does not own the DH structure
 * Build the following DH Key format from the passed in WOLFSSL_DH
 * then store in WOLFSSL_EVP_PKEY in DER format.
 *
 * returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
 */
with set1 functions the pkey struct does not own the DH structure
Build the following DH Key format from the passed in WOLFSSL_DH
then store in WOLFSSL_EVP_PKEY in DER format.

returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
with set1 functions the pkey struct does not own the DH structure Build the following DH Key format from the passed in WOLFSSL_DH then store in WOLFSSL_EVP_PKEY in DER format . returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
int wolfSSL_EVP_PKEY_set1_DH(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_DH *key)
{
    byte havePublic = 0, havePrivate = 0;
    int ret;
    word32 derSz = 0;
    byte* derBuf = NULL;
    DhKey* dhkey = NULL;
    WOLFSSL_ENTER("wolfSSL_EVP_PKEY_set1_DH");
    if (pkey == NULL || key == NULL)
        return WOLFSSL_FAILURE;
    if (pkey->dh != NULL && pkey->ownDh == 1)
        wolfSSL_DH_free(pkey->dh);
    pkey->dh    = key;
    pkey->ownDh = 0; 
    pkey->type  = EVP_PKEY_DH;
    if (key->inSet == 0) {
        if (SetDhInternal(key) != WOLFSSL_SUCCESS) {
            WOLFSSL_MSG("SetDhInternal failed");
            return WOLFSSL_FAILURE;
        }
    }
    dhkey = (DhKey*)key->internal;
    havePublic  = mp_unsigned_bin_size(&dhkey->pub)  > 0;
    havePrivate = mp_unsigned_bin_size(&dhkey->priv) > 0;
    if (havePublic && !havePrivate) {
        ret = wc_DhPubKeyToDer(dhkey, NULL, &derSz);
    } else if (havePrivate && !havePublic) {
        ret = wc_DhPrivKeyToDer(dhkey, NULL, &derSz);
    } else {
        ret = wc_DhParamsToDer(dhkey,NULL,&derSz);
    }
    if (derSz <= 0 || ret != LENGTH_ONLY_E) {
       WOLFSSL_MSG("Failed to get size of DH Key");
       return WOLFSSL_FAILURE;
    }
    derBuf = (byte*)XMALLOC(derSz, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
    if (derBuf == NULL) {
        WOLFSSL_MSG("malloc failed");
        return WOLFSSL_FAILURE;
    }
    if (havePublic && !havePrivate) {
        ret = wc_DhPubKeyToDer(dhkey, derBuf, &derSz);
    } else if (havePrivate && !havePublic) {
        ret = wc_DhPrivKeyToDer(dhkey, derBuf, &derSz);
    } else {
        ret = wc_DhParamsToDer(dhkey,derBuf,&derSz);
    }
    if (ret <= 0) {
        WOLFSSL_MSG("Failed to export DH Key");
        XFREE(derBuf, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
        return WOLFSSL_FAILURE;
    }
    pkey->pkey.ptr = (char*)derBuf;
    pkey->pkey_sz = derSz;
    return WOLFSSL_SUCCESS;
}
int wolfSSL_EVP_PKEY_set1_DH ( WOLFSSL_EVP_PKEY * pkey , WOLFSSL_DH * key ) { byte havePublic = 0 , havePrivate = 0 ; int ret ; word32 derSz = 0 ; byte * derBuf = NULL ; DhKey * dhkey = NULL ; WOLFSSL_ENTER ( " " ) ; if ( pkey == NULL || key == NULL ) return WOLFSSL_FAILURE ; if ( pkey -> dh != NULL && pkey -> ownDh == 1 ) wolfSSL_DH_free ( pkey -> dh ) ; pkey -> dh = key ; pkey -> ownDh = 0 ; pkey -> type = EVP_PKEY_DH ; if ( key -> inSet == 0 ) { if ( SetDhInternal ( key ) != WOLFSSL_SUCCESS ) { WOLFSSL_MSG ( " " ) ; return WOLFSSL_FAILURE ; } } dhkey = ( DhKey * ) key -> internal ; havePublic = mp_unsigned_bin_size ( & dhkey -> pub ) > 0 ; havePrivate = mp_unsigned_bin_size ( & dhkey -> priv ) > 0 ; if ( havePublic && ! havePrivate ) { ret = wc_DhPubKeyToDer ( dhkey , NULL , & derSz ) ; } else if ( havePrivate && ! havePublic ) { ret = wc_DhPrivKeyToDer ( dhkey , NULL , & derSz ) ; } else { ret = wc_DhParamsToDer ( dhkey , NULL , & derSz ) ; } if ( derSz <= 0 || ret != LENGTH_ONLY_E ) { WOLFSSL_MSG ( " " ) ; return WOLFSSL_FAILURE ; } derBuf = ( byte * ) XMALLOC ( derSz , pkey -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( derBuf == NULL ) { WOLFSSL_MSG ( " " ) ; return WOLFSSL_FAILURE ; } if ( havePublic && ! havePrivate ) { ret = wc_DhPubKeyToDer ( dhkey , derBuf , & derSz ) ; } else if ( havePrivate && ! havePublic ) { ret = wc_DhPrivKeyToDer ( dhkey , derBuf , & derSz ) ; } else { ret = wc_DhParamsToDer ( dhkey , derBuf , & derSz ) ; } if ( ret <= 0 ) { WOLFSSL_MSG ( " " ) ; XFREE ( derBuf , pkey -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; return WOLFSSL_FAILURE ; } pkey -> pkey . ptr = ( char * ) derBuf ; pkey -> pkey_sz = derSz ; return WOLFSSL_SUCCESS ; }
with set1 functions the pkey struct does not own the DH structure
 Build the following DH Key format from the passed in WOLFSSL_DH
 then store in WOLFSSL_EVP_PKEY in DER format.
with set1 functions the pkey struct does not own the DH structure Build the following DH Key format from the passed in WOLFSSL_DH then store in WOLFSSL_EVP_PKEY in DER format .
/* pkey does not own DH */ /* Get size of DER buffer only */ /* Fill DER buffer */ /* Store DH key into pkey (DER format) */