FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/dh.c
/* Check DH Private Key for invalid numbers, optionally allowing
 * the private key to be checked against the large prime (q).
 * Check per process in SP 800-56Ar3, section 5.6.2.1.2.
 *
 * key     DH key group parameters.
 * priv    Private Key.
 * privSz  Private Key size.
 * prime   Large prime (q), optionally NULL to skip check
 * primeSz Size of large prime
 *
 *  returns 0 on success or error code
 */
Check DH Private Key for invalid numbers, optionally allowing
the private key to be checked against the large prime (q).

key     DH key group parameters.
priv    Private Key.
privSz  Private Key size.
prime   Large prime (q), optionally NULL to skip check
primeSz Size of large prime

returns 0 on success or error code
Check DH Private Key for invalid numbers optionally allowing the private key to be checked against the large prime ( q ) . key DH key group parameters . priv Private Key . privSz Private Key size . prime Large prime ( q ) optionally NULL to skip check primeSz Size of large prime returns 0 on success or error code
int wc_DhCheckPrivKey_ex(DhKey* key, const byte* priv, word32 privSz,
                         const byte* prime, word32 primeSz)
{
    int ret = 0;
#ifdef WOLFSSL_SMALL_STACK
    mp_int* x = NULL;
    mp_int* q = NULL;
#else
    mp_int x[1];
    mp_int q[1];
#endif
    if (key == NULL || priv == NULL) {
        return BAD_FUNC_ARG;
    }
#ifdef WOLFSSL_SMALL_STACK
    x = (mp_int*)XMALLOC(sizeof(mp_int), key->heap, DYNAMIC_TYPE_DH);
    if (x == NULL)
        return MEMORY_E;
    q = (mp_int*)XMALLOC(sizeof(mp_int), key->heap, DYNAMIC_TYPE_DH);
    if (q == NULL) {
        XFREE(x, key->heap, DYNAMIC_TYPE_DH);
        return MEMORY_E;
    }
#endif
    if (mp_init_multi(x, q, NULL, NULL, NULL, NULL) != MP_OKAY) {
    #ifdef WOLFSSL_SMALL_STACK
        XFREE(q, key->heap, DYNAMIC_TYPE_DH);
        XFREE(x, key->heap, DYNAMIC_TYPE_DH);
    #endif
        return MP_INIT_E;
    }
    if (mp_read_unsigned_bin(x, priv, privSz) != MP_OKAY) {
        ret = MP_READ_E;
    }
    if (ret == 0) {
        if (prime != NULL) {
            if (mp_read_unsigned_bin(q, prime, primeSz) != MP_OKAY)
                ret = MP_READ_E;
        }
        else if (mp_iszero(&key->q) == MP_NO) {
            if (mp_copy(&key->q, q) != MP_OKAY)
                ret = MP_INIT_E;
        }
    }
    if (ret == 0) {
        if (mp_cmp_d(x, 0) == MP_EQ)
            ret = MP_CMP_E;
    }
    if (ret == 0) {
        if (mp_iszero(q) == MP_NO) {
            if (ret == 0) {
                if (mp_copy(&key->q, q) != MP_OKAY)
                    ret = MP_INIT_E;
            }
            if (ret == 0) {
                if (mp_sub_d(q, 1, q) != MP_OKAY)
                    ret = MP_SUB_E;
            }
            if (ret == 0) {
                if (mp_cmp(x, q) == MP_GT)
                    ret = DH_CHECK_PRIV_E;
            }
        }
    }
    mp_clear(x);
    mp_clear(q);
#ifdef WOLFSSL_SMALL_STACK
    XFREE(q, key->heap, DYNAMIC_TYPE_DH);
    XFREE(x, key->heap, DYNAMIC_TYPE_DH);
#endif
    return ret;
}
int wc_DhCheckPrivKey_ex ( DhKey * key , const byte * priv , word32 privSz , const byte * prime , word32 primeSz ) { int ret = 0 ; #ifdef WOLFSSL_SMALL_STACK mp_int * x = NULL ; mp_int * q = NULL ; #else mp_int x [ 1 ] ; mp_int q [ 1 ] ; #endif if ( key == NULL || priv == NULL ) { return BAD_FUNC_ARG ; } #ifdef WOLFSSL_SMALL_STACK x = ( mp_int * ) XMALLOC ( sizeof ( mp_int ) , key -> heap , DYNAMIC_TYPE_DH ) ; if ( x == NULL ) return MEMORY_E ; q = ( mp_int * ) XMALLOC ( sizeof ( mp_int ) , key -> heap , DYNAMIC_TYPE_DH ) ; if ( q == NULL ) { XFREE ( x , key -> heap , DYNAMIC_TYPE_DH ) ; return MEMORY_E ; } #endif if ( mp_init_multi ( x , q , NULL , NULL , NULL , NULL ) != MP_OKAY ) { #ifdef WOLFSSL_SMALL_STACK XFREE ( q , key -> heap , DYNAMIC_TYPE_DH ) ; XFREE ( x , key -> heap , DYNAMIC_TYPE_DH ) ; #endif return MP_INIT_E ; } if ( mp_read_unsigned_bin ( x , priv , privSz ) != MP_OKAY ) { ret = MP_READ_E ; } if ( ret == 0 ) { if ( prime != NULL ) { if ( mp_read_unsigned_bin ( q , prime , primeSz ) != MP_OKAY ) ret = MP_READ_E ; } else if ( mp_iszero ( & key -> q ) == MP_NO ) { if ( mp_copy ( & key -> q , q ) != MP_OKAY ) ret = MP_INIT_E ; } } if ( ret == 0 ) { if ( mp_cmp_d ( x , 0 ) == MP_EQ ) ret = MP_CMP_E ; } if ( ret == 0 ) { if ( mp_iszero ( q ) == MP_NO ) { if ( ret == 0 ) { if ( mp_copy ( & key -> q , q ) != MP_OKAY ) ret = MP_INIT_E ; } if ( ret == 0 ) { if ( mp_sub_d ( q , 1 , q ) != MP_OKAY ) ret = MP_SUB_E ; } if ( ret == 0 ) { if ( mp_cmp ( x , q ) == MP_GT ) ret = DH_CHECK_PRIV_E ; } } } mp_clear ( x ) ; mp_clear ( q ) ; #ifdef WOLFSSL_SMALL_STACK XFREE ( q , key -> heap , DYNAMIC_TYPE_DH ) ; XFREE ( x , key -> heap , DYNAMIC_TYPE_DH ) ; #endif return ret ; }
Check DH Private Key for invalid numbers, optionally allowing
 the private key to be checked against the large prime (q).
Check DH Private Key for invalid numbers optionally allowing the private key to be checked against the large prime ( q ) .
/* use q available in DhKey */ /* priv (x) should not be 0 */ /* priv (x) shouldn't be greater than q - 1 */