Line data Source code
1 : #include "fd_keccak256.h" 2 : #include "fd_keccak256_private.h" 3 : 4 : fd_keccak256_t * 5 4628 : fd_keccak256_init( fd_keccak256_t * sha ) { 6 4628 : fd_memset( sha->state, 0, sizeof( sha->state ) ); 7 : 8 4628 : sha->padding_start = 0; 9 : 10 4628 : return sha; 11 4628 : } 12 : 13 : fd_keccak256_t * 14 : fd_keccak256_append( fd_keccak256_t * sha, 15 : void const * _data, 16 7557 : ulong sz ) { 17 : 18 : /* If no data to append, we are done */ 19 : 20 7557 : if( FD_UNLIKELY( !sz ) ) return sha; /* optimize for non-trivial append */ 21 : 22 : /* Unpack inputs */ 23 : 24 7484 : ulong * state = sha->state; 25 7484 : uchar * state_bytes = (uchar*) sha->state; 26 7484 : ulong padding_start = sha->padding_start; 27 : 28 7484 : uchar const * data = (uchar const *)_data; 29 : 30 7484 : ulong state_idx = padding_start; 31 960935 : for( ulong i = 0; i < sz; i++ ) { 32 953451 : state_bytes[state_idx] ^= data[i]; 33 953451 : state_idx++; 34 953451 : if( state_idx >= FD_KECCAK256_RATE ) { 35 5079 : fd_keccak256_core(state); 36 5079 : state_idx = 0; 37 5079 : } 38 953451 : } 39 : 40 7484 : sha->padding_start = state_idx; 41 : 42 7484 : return sha; 43 7557 : } 44 : 45 : void * 46 : fd_keccak256_fini( fd_keccak256_t * sha, 47 4621 : void * hash ) { 48 : 49 : /* Unpack inputs */ 50 : 51 4621 : ulong * state = sha->state; 52 4621 : uchar * state_bytes = (uchar*) sha->state; 53 4621 : ulong padding_start = sha->padding_start; 54 : 55 : 56 : /* Append the terminating message byte */ 57 : 58 4621 : state_bytes[padding_start] ^= (uchar)0x01; 59 4621 : state_bytes[FD_KECCAK256_RATE-1] ^= (uchar)0x80; 60 4621 : fd_keccak256_core(state); 61 : 62 : /* Copy the result into hash */ 63 : 64 4621 : fd_memcpy(hash, state, FD_KECCAK256_OUT_SZ); 65 4621 : return hash; 66 4621 : } 67 : 68 : void * 69 : fd_keccak256_hash( void const * _data, 70 : ulong sz, 71 4391 : void * _hash ) { 72 4391 : fd_keccak256_t sha; 73 4391 : fd_keccak256_init( &sha ); 74 4391 : fd_keccak256_append( &sha, _data, sz ); 75 4391 : fd_keccak256_fini( &sha, _hash ); 76 : 77 : 78 4391 : return _hash; 79 4391 : }