LCOV - code coverage report
Current view: top level - ballet/hmac - fuzz_hmac.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 7 7 100.0 %
Date: 2025-01-08 12:08:44 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #if !FD_HAS_HOSTED
       2             : #error "This target requires FD_HAS_HOSTED"
       3             : #endif
       4             : 
       5             : #include <assert.h>
       6             : #include <stdio.h>
       7             : #include <stdlib.h>
       8             : 
       9             : #include "../../util/fd_util.h"
      10             : #include "../../util/sanitize/fd_fuzz.h"
      11             : #include "fd_hmac.h"
      12             : #include "../sha256/fd_sha256.h"
      13             : #include "../sha512/fd_sha512.h"
      14             : 
      15             : int
      16             : LLVMFuzzerInitialize( int  *   argc,
      17          18 :                       char *** argv ) {
      18             :   /* Set up shell without signal handlers */
      19          18 :   putenv( "FD_LOG_BACKTRACE=0" );
      20          18 :   fd_boot( argc, argv );
      21          18 :   atexit( fd_halt );
      22          18 :   fd_log_level_core_set(3); /* crash on warning log */
      23          18 :   return 0;
      24          18 : }
      25             : 
      26             : struct hmac_test {
      27             :   ulong key_sz;
      28             :   uchar key[ ];
      29             :   /* uchar msg[ ]; */
      30             : };
      31             : typedef struct hmac_test hmac_test_t;
      32             : 
      33             : #define KEY_MAX (256UL)
      34             : 
      35             : int
      36             : LLVMFuzzerTestOneInput( uchar const * data,
      37             :                         ulong         size ) {
      38             :   if( FD_UNLIKELY( size<64UL ) ) return -1;
      39             :   hmac_test_t * const test = ( hmac_test_t * const ) data;
      40             : 
      41             :   ulong key_size = test->key_sz & (KEY_MAX-1UL);
      42             :   if( FD_UNLIKELY( size<(64UL+key_size) ) ) return -1;
      43             :   char const * key = key_size ? ( char const * ) test->key : NULL;
      44             : 
      45             :   ulong msg_size = size-(64UL+key_size); 
      46             :   char const * msg = msg_size ? ( char const * ) test->key + key_size : NULL;
      47             : 
      48             :   uchar hash1[ 64 ] __attribute__((aligned(64)));
      49             :   uchar hash2[ 64 ] __attribute__((aligned(64)));
      50             : 
      51             :   assert( fd_hmac_sha256( msg, msg_size, key, key_size, hash1 ) == hash1 );
      52             :   assert( fd_hmac_sha256( msg, msg_size, key, key_size, hash2 ) == hash2 );
      53             :   assert( !memcmp( hash1, hash2, FD_SHA256_HASH_SZ ) );
      54             : 
      55             :   assert( fd_hmac_sha384( msg, msg_size, key, key_size, hash1 ) == hash1 );
      56             :   assert( fd_hmac_sha384( msg, msg_size, key, key_size, hash2 ) == hash2 );
      57             :   assert( !memcmp( hash1, hash2, FD_SHA384_HASH_SZ ) );
      58             : 
      59             :   assert( fd_hmac_sha512( msg, msg_size, key, key_size, hash1 ) == hash1 );
      60             :   assert( fd_hmac_sha512( msg, msg_size, key, key_size, hash2 ) == hash2 );
      61             :   assert( !memcmp( hash1, hash2, FD_SHA512_HASH_SZ ) );
      62             : 
      63             :   FD_FUZZ_MUST_BE_COVERED;
      64             :   return 0;
      65             : }

Generated by: LCOV version 1.14