]> git.cryptolib.org Git - avr-crypto-lib.git/blob - twister/twister-sha3api.c
JH and Blake updated for round 3
[avr-crypto-lib.git] / twister / twister-sha3api.c
1 /* twister-sha3api.c */
2
3 #include "sha3-api.h"
4 #include "twister-sha3api.h"
5 #include "twister.h"
6 #include "twister-small.h"
7 #include "twister-big.h"
8 #include <stdlib.h>     
9 #include <string.h>
10
11 #define BLOCKSIZE 512
12
13 HashReturn Init(hashState *state, int hashbitlen){
14         if(!state)
15                 return FAIL;
16         if(hashbitlen<32 || hashbitlen>512 || hashbitlen%32)
17                 return BAD_HASHBITLEN;
18         
19         if (hashbitlen>=32 && hashbitlen<=256){
20                 state->hashbitlen = hashbitlen;
21                 state->big = 0;
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);
26                 return SUCCESS;
27         }
28         if (hashbitlen>256 && hashbitlen<=512){
29                 state->hashbitlen = hashbitlen;
30                 state->big = 1;
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);
35                 return SUCCESS;
36         }
37         return BAD_HASHBITLEN;
38 }
39
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++;
44 }
45
46
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 */
51                 uint16_t i;
52                 for(i=0; i<databitlen; ++i){
53                         insertBitToBuffer(state, 1&((data[i/8])>>(7-i%8)));
54                 }
55                 return SUCCESS;
56         }                 
57         if(state->buffer_fill_state==0){
58                 while(databitlen>=512){
59                         if(state->big){
60                                 twister_big_nextBlock(state->ctx, data);
61                         }else{
62                                 twister_small_nextBlock(state->ctx, data);
63                         }
64                         data += 64;
65                         databitlen -= 512;
66                 }
67                 if(databitlen!=0)
68                         Update(state, data, databitlen);
69                 return SUCCESS;
70     }else{
71         DataLength i;
72         for(i=0; i<databitlen; ++i){
73                 if(state->buffer_fill_state==512){
74                         if(state->big){
75                                 twister_big_nextBlock(state->ctx, state->buffer);
76                                 }else{
77                                         twister_small_nextBlock(state->ctx, state->buffer);
78                                 }
79                         memset(state->buffer, 0, 64);
80                         state->buffer_fill_state = 0;
81                         }
82                         insertBitToBuffer(state, 1&(data[i/8]>>(7-i%8)));
83                 }
84                 return SUCCESS;
85         }              
86         return FAIL;    
87 }
88
89 HashReturn Final(hashState *state, BitSequence *hashval){
90         if(state->big){
91                 twister_big_lastBlock(state->ctx, state->buffer, state->buffer_fill_state);
92                 twister_big_ctx2hash(hashval, state->ctx, state->hashbitlen);
93                 return SUCCESS;
94         }else{
95                 twister_small_lastBlock(state->ctx, state->buffer, state->buffer_fill_state);
96                 twister_small_ctx2hash(hashval, state->ctx, state->hashbitlen);
97                 return SUCCESS;
98         }
99 }
100
101 HashReturn Hash(int hashbitlen, const BitSequence *data,
102                 DataLength databitlen, BitSequence *hashval){
103         HashReturn ret;
104         hashState state;
105         ret = Init(&state, hashbitlen);
106         if(ret!=SUCCESS)
107                 return ret;
108         ret = Update(&state, data, databitlen);
109         if(ret!=SUCCESS)
110                 return ret;
111         return Final(&state, hashval);
112 }
113