FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/random.c
/* Hash Derivation Function */
/* Returns: DRBG_SUCCESS or DRBG_FAILURE */
Hash Derivation Function
Returns: DRBG_SUCCESS or DRBG_FAILURE
Hash Derivation Function Returns : DRBG_SUCCESS or DRBG_FAILURE
static int Hash_df(DRBG* drbg, byte* out, word32 outSz, byte type,
                                                  const byte* inA, word32 inASz,
                                                  const byte* inB, word32 inBSz)
{
    int ret = DRBG_FAILURE;
    byte ctr;
    int i;
    int len;
    word32 bits = (outSz * 8); 
#ifdef WOLFSSL_SMALL_STACK_CACHE
    wc_Sha256* sha = &drbg->sha256;
#else
    wc_Sha256 sha[1];
#endif
#ifdef WC_ASYNC_ENABLE_SHA256
    DECLARE_VAR(digest, byte, WC_SHA256_DIGEST_SIZE, drbg->heap);
    if (digest == NULL)
        return MEMORY_E;
#else
    byte digest[WC_SHA256_DIGEST_SIZE];
#endif
    (void)drbg;
#ifdef WC_ASYNC_ENABLE_SHA256
    if (digest == NULL)
        return DRBG_FAILURE;
#endif
#ifdef LITTLE_ENDIAN_ORDER
    bits = ByteReverseWord32(bits);
#endif
    len = (outSz / OUTPUT_BLOCK_LEN)
        + ((outSz % OUTPUT_BLOCK_LEN) ? 1 : 0);
    for (i = 0, ctr = 1; i < len; i++, ctr++) {
#ifndef WOLFSSL_SMALL_STACK_CACHE
    #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
        ret = wc_InitSha256_ex(sha, drbg->heap, drbg->devId);
    #else
        ret = wc_InitSha256(sha);
    #endif
        if (ret != 0)
            break;
        if (ret == 0)
#endif
            ret = wc_Sha256Update(sha, &ctr, sizeof(ctr));
        if (ret == 0)
            ret = wc_Sha256Update(sha, (byte*)&bits, sizeof(bits));
        if (ret == 0) {
            if (type != drbgInitV)
                ret = wc_Sha256Update(sha, &type, sizeof(type));
        }
        if (ret == 0)
            ret = wc_Sha256Update(sha, inA, inASz);
        if (ret == 0) {
            if (inB != NULL && inBSz > 0)
                ret = wc_Sha256Update(sha, inB, inBSz);
        }
        if (ret == 0)
            ret = wc_Sha256Final(sha, digest);
#ifndef WOLFSSL_SMALL_STACK_CACHE
        wc_Sha256Free(sha);
#endif
        if (ret == 0) {
            if (outSz > OUTPUT_BLOCK_LEN) {
                XMEMCPY(out, digest, OUTPUT_BLOCK_LEN);
                outSz -= OUTPUT_BLOCK_LEN;
                out += OUTPUT_BLOCK_LEN;
            }
            else {
                XMEMCPY(out, digest, outSz);
            }
        }
    }
    ForceZero(digest, WC_SHA256_DIGEST_SIZE);
#ifdef WC_ASYNC_ENABLE_SHA256
    FREE_VAR(digest, drbg->heap);
#endif
    return (ret == 0) ? DRBG_SUCCESS : DRBG_FAILURE;
}
static int Hash_df ( DRBG * drbg , byte * out , word32 outSz , byte type , const byte * inA , word32 inASz , const byte * inB , word32 inBSz ) { int ret = DRBG_FAILURE ; byte ctr ; int i ; int len ; word32 bits = ( outSz * 8 ) ; #ifdef WOLFSSL_SMALL_STACK_CACHE wc_Sha256 * sha = & drbg -> sha256 ; #else wc_Sha256 sha [ 1 ] ; #endif #ifdef WC_ASYNC_ENABLE_SHA256 DECLARE_VAR ( digest , byte , WC_SHA256_DIGEST_SIZE , drbg -> heap ) ; if ( digest == NULL ) return MEMORY_E ; #else byte digest [ WC_SHA256_DIGEST_SIZE ] ; #endif ( void ) drbg ; #ifdef WC_ASYNC_ENABLE_SHA256 if ( digest == NULL ) return DRBG_FAILURE ; #endif #ifdef LITTLE_ENDIAN_ORDER bits = ByteReverseWord32 ( bits ) ; #endif len = ( outSz / OUTPUT_BLOCK_LEN ) + ( ( outSz % OUTPUT_BLOCK_LEN ) ? 1 : 0 ) ; for ( i = 0 , ctr = 1 ; i < len ; i ++ , ctr ++ ) { #ifndef WOLFSSL_SMALL_STACK_CACHE #if defined ( WOLFSSL_ASYNC_CRYPT ) || defined ( WOLF_CRYPTO_CB ) ret = wc_InitSha256_ex ( sha , drbg -> heap , drbg -> devId ) ; #else ret = wc_InitSha256 ( sha ) ; #endif if ( ret != 0 ) break ; if ( ret == 0 ) #endif ret = wc_Sha256Update ( sha , & ctr , sizeof ( ctr ) ) ; if ( ret == 0 ) ret = wc_Sha256Update ( sha , ( byte * ) & bits , sizeof ( bits ) ) ; if ( ret == 0 ) { if ( type != drbgInitV ) ret = wc_Sha256Update ( sha , & type , sizeof ( type ) ) ; } if ( ret == 0 ) ret = wc_Sha256Update ( sha , inA , inASz ) ; if ( ret == 0 ) { if ( inB != NULL && inBSz > 0 ) ret = wc_Sha256Update ( sha , inB , inBSz ) ; } if ( ret == 0 ) ret = wc_Sha256Final ( sha , digest ) ; #ifndef WOLFSSL_SMALL_STACK_CACHE wc_Sha256Free ( sha ) ; #endif if ( ret == 0 ) { if ( outSz > OUTPUT_BLOCK_LEN ) { XMEMCPY ( out , digest , OUTPUT_BLOCK_LEN ) ; outSz -= OUTPUT_BLOCK_LEN ; out += OUTPUT_BLOCK_LEN ; } else { XMEMCPY ( out , digest , outSz ) ; } } } ForceZero ( digest , WC_SHA256_DIGEST_SIZE ) ; #ifdef WC_ASYNC_ENABLE_SHA256 FREE_VAR ( digest , drbg -> heap ) ; #endif return ( ret == 0 ) ? DRBG_SUCCESS : DRBG_FAILURE ; }
Hash Derivation Function
Returns: DRBG_SUCCESS or DRBG_FAILURE
Hash Derivation Function Returns : DRBG_SUCCESS or DRBG_FAILURE
/* reverse byte order */ /* churning V is the only string that doesn't have the type added */