FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/dh.c
/* Sets private and public key in DhKey if both are available, otherwise sets
    either private or public key, depending on which is available. */
Sets private and public key in DhKey if both are available, otherwise sets
either private or public key, depending on which is available.
Sets private and public key in DhKey if both are available otherwise sets either private or public key depending on which is available .
int wc_DhImportKeyPair(DhKey* key, const byte* priv, word32 privSz,
                       const byte* pub, word32 pubSz)
{
    byte havePriv, havePub;
    mp_int *keyPriv = NULL, *keyPub  = NULL;
    if (key == NULL) {
        return BAD_FUNC_ARG;
    }
    havePriv = ( (priv != NULL) && (privSz > 0) );
    havePub  = ( (pub  != NULL) && (pubSz  > 0) );
    if (!havePub && !havePriv) {
        WOLFSSL_MSG("No Public or Private Key to Set");
        return BAD_FUNC_ARG;
    }
    if (havePriv) {
        if (priv[0] == 0) {
            privSz--; priv++;
        }
        if (mp_init(&key->priv) != MP_OKAY)
            havePriv = 0;
    }
    if (havePriv) {
        if (mp_read_unsigned_bin(&key->priv, priv, privSz) != MP_OKAY) {
            mp_clear(&key->priv);
            havePriv = 0;
        } else {
            keyPriv = &key->priv;
            WOLFSSL_MSG("DH Private Key Set");
        }
    }
    if (havePub) {
        if (pub[0] == 0) {
            pubSz--; pub++;
        }
        if (mp_init(&key->pub) != MP_OKAY)
            havePub = 0;
    }
    if (havePub) {
        if (mp_read_unsigned_bin(&key->pub, pub, pubSz) != MP_OKAY) {
            mp_clear(&key->pub);
            havePub = 0;
        } else {
            keyPub = &key->pub;
            WOLFSSL_MSG("DH Public Key Set");
        }
    }
    if (havePriv == 0 && keyPriv != NULL)
        mp_clear(keyPriv);
    if (havePub == 0 && keyPub != NULL)
        mp_clear(keyPub);
    if (havePriv == 0 && havePub == 0) {
        return MEMORY_E;
    }
    return 0;
}
int wc_DhImportKeyPair ( DhKey * key , const byte * priv , word32 privSz , const byte * pub , word32 pubSz ) { byte havePriv , havePub ; mp_int * keyPriv = NULL , * keyPub = NULL ; if ( key == NULL ) { return BAD_FUNC_ARG ; } havePriv = ( ( priv != NULL ) && ( privSz > 0 ) ) ; havePub = ( ( pub != NULL ) && ( pubSz > 0 ) ) ; if ( ! havePub && ! havePriv ) { WOLFSSL_MSG ( " " ) ; return BAD_FUNC_ARG ; } if ( havePriv ) { if ( priv [ 0 ] == 0 ) { privSz -- ; priv ++ ; } if ( mp_init ( & key -> priv ) != MP_OKAY ) havePriv = 0 ; } if ( havePriv ) { if ( mp_read_unsigned_bin ( & key -> priv , priv , privSz ) != MP_OKAY ) { mp_clear ( & key -> priv ) ; havePriv = 0 ; } else { keyPriv = & key -> priv ; WOLFSSL_MSG ( " " ) ; } } if ( havePub ) { if ( pub [ 0 ] == 0 ) { pubSz -- ; pub ++ ; } if ( mp_init ( & key -> pub ) != MP_OKAY ) havePub = 0 ; } if ( havePub ) { if ( mp_read_unsigned_bin ( & key -> pub , pub , pubSz ) != MP_OKAY ) { mp_clear ( & key -> pub ) ; havePub = 0 ; } else { keyPub = & key -> pub ; WOLFSSL_MSG ( " " ) ; } } if ( havePriv == 0 && keyPriv != NULL ) mp_clear ( keyPriv ) ; if ( havePub == 0 && keyPub != NULL ) mp_clear ( keyPub ) ; if ( havePriv == 0 && havePub == 0 ) { return MEMORY_E ; } return 0 ; }
Sets private and public key in DhKey if both are available, otherwise sets
either private or public key, depending on which is available.
Sets private and public key in DhKey if both are available otherwise sets either private or public key depending on which is available .
/* Set Private Key */ /* may have leading 0 */ /* Set Public Key */ /* may have leading 0 */ /* Free Memory if error occurred */