LCOV - code coverage report
Current view: top level - ballet/hmac - fuzz_hmac.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 6 6 100.0 %
Date: 2024-11-13 11:58:15 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 :   return 0;
      23          18 : }
      24             : 
      25             : struct hmac_test {
      26             :   ulong key_sz;
      27             :   uchar key[ ];
      28             :   /* uchar msg[ ]; */
      29             : };
      30             : typedef struct hmac_test hmac_test_t;
      31             : 
      32             : #define KEY_MAX (256UL)
      33             : 
      34             : int
      35             : LLVMFuzzerTestOneInput( uchar const * data,
      36             :                         ulong         size ) {
      37             :   if( FD_UNLIKELY( size<64UL ) ) return -1;
      38             :   hmac_test_t * const test = ( hmac_test_t * const ) data;
      39             : 
      40             :   ulong key_size = test->key_sz & (KEY_MAX-1UL);
      41             :   if( FD_UNLIKELY( size<(64UL+key_size) ) ) return -1;
      42             :   char const * key = key_size ? ( char const * ) test->key : NULL;
      43             : 
      44             :   ulong msg_size = size-(64UL+key_size); 
      45             :   char const * msg = msg_size ? ( char const * ) test->key + key_size : NULL;
      46             : 
      47             :   uchar hash1[ 64 ] __attribute__((aligned(64)));
      48             :   uchar hash2[ 64 ] __attribute__((aligned(64)));
      49             : 
      50             :   assert( fd_hmac_sha256( msg, msg_size, key, key_size, hash1 ) == hash1 );
      51             :   assert( fd_hmac_sha256( msg, msg_size, key, key_size, hash2 ) == hash2 );
      52             :   assert( !memcmp( hash1, hash2, FD_SHA256_HASH_SZ ) );
      53             : 
      54             :   assert( fd_hmac_sha384( msg, msg_size, key, key_size, hash1 ) == hash1 );
      55             :   assert( fd_hmac_sha384( msg, msg_size, key, key_size, hash2 ) == hash2 );
      56             :   assert( !memcmp( hash1, hash2, FD_SHA384_HASH_SZ ) );
      57             : 
      58             :   assert( fd_hmac_sha512( msg, msg_size, key, key_size, hash1 ) == hash1 );
      59             :   assert( fd_hmac_sha512( msg, msg_size, key, key_size, hash2 ) == hash2 );
      60             :   assert( !memcmp( hash1, hash2, FD_SHA512_HASH_SZ ) );
      61             : 
      62             :   FD_FUZZ_MUST_BE_COVERED;
      63             :   return 0;
      64             : }

Generated by: LCOV version 1.14