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 12 : char *** argv ) {
18 : /* Set up shell without signal handlers */
19 12 : putenv( "FD_LOG_BACKTRACE=0" );
20 12 : setenv( "FD_LOG_PATH", "", 0 );
21 12 : fd_boot( argc, argv );
22 12 : atexit( fd_halt );
23 12 : fd_log_level_core_set(3); /* crash on warning log */
24 12 : return 0;
25 12 : }
26 :
27 : struct hmac_test {
28 : ulong key_sz;
29 : uchar key[ ];
30 : /* uchar msg[ ]; */
31 : };
32 : typedef struct hmac_test hmac_test_t;
33 :
34 : #define KEY_MAX (256UL)
35 :
36 : int
37 : LLVMFuzzerTestOneInput( uchar const * data,
38 : ulong size ) {
39 : if( FD_UNLIKELY( size<64UL ) ) return -1;
40 : hmac_test_t * const test = ( hmac_test_t * const ) data;
41 :
42 : ulong key_size = test->key_sz & (KEY_MAX-1UL);
43 : if( FD_UNLIKELY( size<(64UL+key_size) ) ) return -1;
44 : char const * key = key_size ? ( char const * ) test->key : NULL;
45 :
46 : ulong msg_size = size-(64UL+key_size);
47 : char const * msg = msg_size ? ( char const * ) test->key + key_size : NULL;
48 :
49 : uchar hash1[ 64 ] __attribute__((aligned(64)));
50 : uchar hash2[ 64 ] __attribute__((aligned(64)));
51 :
52 : assert( fd_hmac_sha256( msg, msg_size, key, key_size, hash1 ) == hash1 );
53 : assert( fd_hmac_sha256( msg, msg_size, key, key_size, hash2 ) == hash2 );
54 : assert( !memcmp( hash1, hash2, FD_SHA256_HASH_SZ ) );
55 :
56 : assert( fd_hmac_sha384( msg, msg_size, key, key_size, hash1 ) == hash1 );
57 : assert( fd_hmac_sha384( msg, msg_size, key, key_size, hash2 ) == hash2 );
58 : assert( !memcmp( hash1, hash2, FD_SHA384_HASH_SZ ) );
59 :
60 : assert( fd_hmac_sha512( msg, msg_size, key, key_size, hash1 ) == hash1 );
61 : assert( fd_hmac_sha512( msg, msg_size, key, key_size, hash2 ) == hash2 );
62 : assert( !memcmp( hash1, hash2, FD_SHA512_HASH_SZ ) );
63 :
64 : FD_FUZZ_MUST_BE_COVERED;
65 : return 0;
66 : }
|