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