]> git.cryptolib.org Git - avr-crypto-lib.git/blob - keccak/keccak-stub.c
[keccak-asm] removing unnecessary c and d fields from context
[avr-crypto-lib.git] / keccak / keccak-stub.c
1 /* keecak.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "memxor.h"
24 #include "keccak.h"
25
26 #ifdef DEBUG
27 #  undef DEBUG
28 #endif
29
30 #define DEBUG 0
31
32 #if DEBUG
33 #include "cli.h"
34 #include "stdio.h"
35
36 void keccak_dump_state(uint64_t a[5][5]){
37         uint8_t i,j;
38         for(i=0; i<5; ++i){
39                 cli_putstr_P(PSTR("\r\n"));
40                 cli_putc('0'+i);
41                 cli_putstr_P(PSTR(": "));
42                 for(j=0; j<5; ++j){
43                         cli_hexdump_rev(&(a[i][j]), 8);
44                         cli_putc(' ');
45                 }
46         }
47 }
48
49 void keccak_dump_ctx(keccak_ctx_t* ctx){
50         keccak_dump_state(ctx->a);
51         cli_putstr_P(PSTR("\r\nDBG: r: "));
52         cli_hexdump_rev(&(ctx->r), 2);
53         cli_putstr_P(PSTR("\t c: "));
54         cli_hexdump_rev(&(ctx->c), 2);
55         cli_putstr_P(PSTR("\t d: "));
56         cli_hexdump(&(ctx->d), 1);
57         cli_putstr_P(PSTR("\t bs: "));
58         cli_hexdump(&(ctx->bs), 1);
59 }
60
61 #endif
62
63 void keccak_f1600(uint8_t a[200]);
64
65 void keccak_lastBlock(keccak_ctx_t* ctx, const void* block, uint16_t length_b){
66     uint8_t length_B;
67     while(length_b >= ctx->r){
68         keccak_nextBlock(ctx, block);
69         block = (uint8_t*)block + ctx->bs;
70         length_b -=  ctx->r;
71     }
72     length_B = length_b / 8;
73     memxor(ctx->a, block, length_B);
74     /* append 1 */
75     if(length_b & 7){
76         /* we have some single bits */
77         uint8_t t;
78         t = ((uint8_t*)block)[length_B] >> (8 - (length_b & 7));
79         t |= 0x01 << (length_b & 7);
80         ctx->a[length_B] ^= t;
81     }else{
82         ctx->a[length_B] ^= 0x01;
83     }
84     if(length_b == ctx->r - 1){
85         keccak_f1600(ctx->a);
86     }
87     ctx->a[ctx->bs - 1] ^= 0x80;
88     keccak_f1600(ctx->a);
89 }
90
91 #if 0
92 void keccak_lastBlock(keccak_ctx_t* ctx, const void* block, uint16_t length_b){
93     uint8_t length_B;
94     while(length_b >= ctx->r){
95                 keccak_nextBlock(ctx, block);
96                 block = (uint8_t*)block + ctx->bs;
97                 length_b -=  ctx->r;
98         }
99     length_B = length_b / 8;
100         memxor(ctx->a, block, length_B);
101         /* append 1 */
102         if(length_b & 7){
103                 /* we have some single bits */
104                 uint8_t t;
105                 t = ((uint8_t*)block)[length_B] >> (8 - (length_b & 7));
106                 t |= 0x01 << (length_b & 7);
107                 ctx->a[length_B] ^= t;
108         }else{
109             ctx->a[length_B] ^= 0x01;
110         }
111         if(length_B + 1 + 3 <= ctx->bs){
112         ctx->a[length_B + 1] ^= ctx->d;
113         ctx->a[length_B + 2] ^= ctx->bs;
114         ctx->a[length_B + 3] ^= 1;
115         }else{
116                 if(length_B + 1 + 2 <= ctx->bs){
117             ctx->a[length_B + 1] ^= ctx->d;
118             ctx->a[length_B + 2] ^= ctx->bs;
119                         keccak_f1600(ctx->a);
120                         ((uint8_t*)ctx->a)[0] ^= 0x01;
121                 }else{
122                         if(length_B + 1 + 1 <= ctx->bs){
123                                 ctx->a[length_B + 1] ^= ctx->d;
124                                 keccak_f1600(ctx->a);
125                                 ctx->a[0] ^= ctx->bs;
126                                 ctx->a[1] ^= 0x01;
127                         }else{
128                                 keccak_f1600(ctx->a);
129                                 ctx->a[0] ^= ctx->d;
130                                 ctx->a[1] ^= ctx->bs;
131                                 ctx->a[2] ^= 0x01;
132                         }
133                 }
134         }
135         keccak_f1600(ctx->a);
136 }
137 #endif
138
139
140 void keccak_ctx2hash(void* dest, uint16_t length_b, keccak_ctx_t* ctx){
141     while(length_b>=ctx->r){
142         memcpy(dest, ctx->a, ctx->bs);
143         dest = (uint8_t*)dest + ctx->bs;
144         length_b -= ctx->r;
145         keccak_f1600(ctx->a);
146     }
147     memcpy(dest, ctx->a, (length_b+7)/8);
148 }
149
150 void keccak224_ctx2hash(void* dest, keccak_ctx_t* ctx){
151         keccak_ctx2hash(dest, 224, ctx);
152 }
153
154 void keccak256_ctx2hash(void* dest, keccak_ctx_t* ctx){
155         keccak_ctx2hash(dest, 256, ctx);
156 }
157
158 void keccak384_ctx2hash(void* dest, keccak_ctx_t* ctx){
159         keccak_ctx2hash(dest, 384, ctx);
160 }
161
162 void keccak512_ctx2hash(void* dest, keccak_ctx_t* ctx){
163         keccak_ctx2hash(dest, 512, ctx);
164 }
165
166 /*
167   1. SHA3-224: ⌊Keccak[r = 1152, c =  448, d = 28]⌋224
168   2. SHA3-256: ⌊Keccak[r = 1088, c =  512, d = 32]⌋256
169   3. SHA3-384: ⌊Keccak[r =  832, c =  768, d = 48]⌋384
170   4. SHA3-512: ⌊Keccak[r =  576, c = 1024, d = 64]⌋512
171 */
172 void keccak_init(uint16_t r, keccak_ctx_t* ctx){
173         memset(ctx->a, 0x00, 5 * 5 * 8);
174         ctx->r = r;
175         ctx->bs = (uint8_t)(r / 8);
176 }
177
178 void keccak224_init(keccak_ctx_t* ctx){
179         keccak_init(1152, ctx);
180 }
181
182 void keccak256_init(keccak_ctx_t* ctx){
183         keccak_init(1088, ctx);
184 }
185
186 void keccak384_init(keccak_ctx_t* ctx){
187         keccak_init( 832, ctx);
188 }
189
190 void keccak512_init(keccak_ctx_t* ctx){
191         keccak_init( 576, ctx);
192 }