1 /* twister-sha3api.c */
4 #include "twister-sha3api.h"
6 #include "twister-small.h"
7 #include "twister-big.h"
13 HashReturn Init(hashState *state, int hashbitlen){
16 if(hashbitlen<32 || hashbitlen>512 || hashbitlen%32)
17 return BAD_HASHBITLEN;
19 if (hashbitlen>=32 && hashbitlen<=256){
20 state->hashbitlen = hashbitlen;
22 state->ctx = malloc(sizeof(twister_state_t));
23 twister_small_init(state->ctx, hashbitlen);
24 state->buffer_fill_state=0;
25 memset(state->buffer, 0, 64);
28 if (hashbitlen>256 && hashbitlen<=512){
29 state->hashbitlen = hashbitlen;
31 state->ctx = malloc(sizeof(twister_big_ctx_t));
32 twister_big_init(state->ctx, hashbitlen);
33 state->buffer_fill_state=0;
34 memset(state->buffer, 0, 64);
37 return BAD_HASHBITLEN;
40 void insertBitToBuffer(hashState *state, uint8_t bit){
41 state->buffer[state->buffer_fill_state/8] |=
42 bit<<(7-(state->buffer_fill_state%8));
43 state->buffer_fill_state++;
47 HashReturn Update(hashState *state, const BitSequence *data,
48 DataLength databitlen){
49 if(state->buffer_fill_state+databitlen<BLOCKSIZE){
50 /* move bits from data to buffer */
52 for(i=0; i<databitlen; ++i){
53 insertBitToBuffer(state, 1&((data[i/8])>>(7-i%8)));
57 if(state->buffer_fill_state==0){
58 while(databitlen>=512){
60 twister_big_nextBlock(state->ctx, data);
62 twister_small_nextBlock(state->ctx, data);
68 Update(state, data, databitlen);
72 for(i=0; i<databitlen; ++i){
73 if(state->buffer_fill_state==512){
75 twister_big_nextBlock(state->ctx, state->buffer);
77 twister_small_nextBlock(state->ctx, state->buffer);
79 memset(state->buffer, 0, 64);
80 state->buffer_fill_state = 0;
82 insertBitToBuffer(state, 1&(data[i/8]>>(7-i%8)));
89 HashReturn Final(hashState *state, BitSequence *hashval){
91 twister_big_lastBlock(state->ctx, state->buffer, state->buffer_fill_state);
92 twister_big_ctx2hash(hashval, state->ctx, state->hashbitlen);
95 twister_small_lastBlock(state->ctx, state->buffer, state->buffer_fill_state);
96 twister_small_ctx2hash(hashval, state->ctx, state->hashbitlen);
101 HashReturn Hash(int hashbitlen, const BitSequence *data,
102 DataLength databitlen, BitSequence *hashval){
105 ret = Init(&state, hashbitlen);
108 ret = Update(&state, data, databitlen);
111 return Final(&state, hashval);