FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/fips_test.c
/* Do in core memory integrity check, 0 on success */
Do in core memory integrity check, 0 on success
Do in core memory integrity check 0 on success
static int DoInCoreCheck(char* base16_hash, int base16_hashSz)
{
    Hmac     hmac;
    byte     hash[WC_SHA256_DIGEST_SIZE];
    word32   verifySz  = FIPS_IN_CORE_VERIFY_SZ;
    word32   binCoreSz  = FIPS_IN_CORE_KEY_SZ;
    int      ret;
    byte binCoreKey [FIPS_IN_CORE_KEY_SZ];       
    byte binVerify  [FIPS_IN_CORE_VERIFY_SZ];    
    static const char coreKey[] =
             "EAD92D38850B03D7160EA4F5C90EDD49C0B6933FAA4833213ED6F0F1596E356B";
    fips_address_function first = wolfCrypt_FIPS_first;
    fips_address_function last  = wolfCrypt_FIPS_last;
    char* start = (char*)wolfCrypt_FIPS_ro_start;
    char* end   = (char*)wolfCrypt_FIPS_ro_end;
    unsigned long code_sz = (unsigned long)last - (unsigned long)first;
    unsigned long data_sz = (unsigned long)end - (unsigned long)start;
    if (data_sz == 0 || data_sz > MAX_FIPS_DATA_SZ)
        return -1;  
    if (code_sz == 0 || code_sz > MAX_FIPS_CODE_SZ)
        return -1;  
    ret = ConvertHexToBin(coreKey, binCoreKey, &binCoreSz,
                          NULL, NULL, NULL,
                          NULL, NULL, NULL,
                          NULL, NULL, NULL);
    if (ret != 0) return ret;
    HmacSetKey_fips(&hmac, WC_SHA256, binCoreKey, binCoreSz);
    HmacUpdate_fips(&hmac, (byte*)first, (word32)code_sz);
    if (verifyCore >= start && verifyCore < end) {
        data_sz = (unsigned long)verifyCore - (unsigned long)start;
        HmacUpdate_fips(&hmac, (byte*)start, (word32)data_sz);
        start   = (char*)verifyCore + sizeof(verifyCore);
        data_sz = (unsigned long)end - (unsigned long)start;
    }
    HmacUpdate_fips(&hmac, (byte*)start, (word32)data_sz);
    HmacFinal_fips(&hmac, hash);
    ret = GenBase16_Hash(hash, sizeof(hash), base16_hash, base16_hashSz);
    if (ret != 0)
        return ret;
    ret = ConvertHexToBin(verifyCore, binVerify, &verifySz,
                          NULL, NULL, NULL,
                          NULL, NULL, NULL,
                          NULL, NULL, NULL);
    if (ret != 0)
        return ret;
    if (XMEMCMP(hash, binVerify, sizeof(hash)) != 0) {
        ret = -1;
    }
    return ret;
}
static int DoInCoreCheck ( char * base16_hash , int base16_hashSz ) { Hmac hmac ; byte hash [ WC_SHA256_DIGEST_SIZE ] ; word32 verifySz = FIPS_IN_CORE_VERIFY_SZ ; word32 binCoreSz = FIPS_IN_CORE_KEY_SZ ; int ret ; byte binCoreKey [ FIPS_IN_CORE_KEY_SZ ] ; byte binVerify [ FIPS_IN_CORE_VERIFY_SZ ] ; static const char coreKey [ ] = " " ; fips_address_function first = wolfCrypt_FIPS_first ; fips_address_function last = wolfCrypt_FIPS_last ; char * start = ( char * ) wolfCrypt_FIPS_ro_start ; char * end = ( char * ) wolfCrypt_FIPS_ro_end ; unsigned long code_sz = ( unsigned long ) last - ( unsigned long ) first ; unsigned long data_sz = ( unsigned long ) end - ( unsigned long ) start ; if ( data_sz == 0 || data_sz > MAX_FIPS_DATA_SZ ) return -1 ; if ( code_sz == 0 || code_sz > MAX_FIPS_CODE_SZ ) return -1 ; ret = ConvertHexToBin ( coreKey , binCoreKey , & binCoreSz , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ) ; if ( ret != 0 ) return ret ; HmacSetKey_fips ( & hmac , WC_SHA256 , binCoreKey , binCoreSz ) ; HmacUpdate_fips ( & hmac , ( byte * ) first , ( word32 ) code_sz ) ; if ( verifyCore >= start && verifyCore < end ) { data_sz = ( unsigned long ) verifyCore - ( unsigned long ) start ; HmacUpdate_fips ( & hmac , ( byte * ) start , ( word32 ) data_sz ) ; start = ( char * ) verifyCore + sizeof ( verifyCore ) ; data_sz = ( unsigned long ) end - ( unsigned long ) start ; } HmacUpdate_fips ( & hmac , ( byte * ) start , ( word32 ) data_sz ) ; HmacFinal_fips ( & hmac , hash ) ; ret = GenBase16_Hash ( hash , sizeof ( hash ) , base16_hash , base16_hashSz ) ; if ( ret != 0 ) return ret ; ret = ConvertHexToBin ( verifyCore , binVerify , & verifySz , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ) ; if ( ret != 0 ) return ret ; if ( XMEMCMP ( hash , binVerify , sizeof ( hash ) ) != 0 ) { ret = -1 ; } return ret ; }
Do in core memory integrity check, 0 on success
Do in core memory integrity check 0 on success
/* CoreKey bin will be 32 */ /* Verify bin will be 32 */ /* bad fips data size */ /* bad fips code size */ /* don't hash verifyCore or changing verifyCore will change hash */