]> git.cryptolib.org Git - avr-crypto-lib.git/blob - keccak/keccak-stub.c
5f3927d1a2632be2bdb17eabc7f416644f3699cd
[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(uint64_t a[5][5]);
64
65 void keccak_lastBlock(keccak_ctx_t* ctx, const void* block, uint16_t length_b){
66         while(length_b >= ctx->r){
67                 keccak_nextBlock(ctx, block);
68                 block = (uint8_t*)block + ctx->bs;
69                 length_b -=  ctx->r;
70         }
71         memxor(ctx->a, block, (length_b)/8);
72         /* append 1 */
73         if(length_b & 7){
74                 /* we have some single bits */
75                 uint8_t t;
76                 t = ((uint8_t*)block)[length_b / 8] >> (8 - (length_b & 7));
77                 t |= 0x01 << (length_b & 7);
78                 ((uint8_t*)ctx->a)[length_b / 8] ^= t;
79         }else{
80             ((uint8_t*)ctx->a)[length_b / 8] ^= 0x01;
81         }
82         if(length_b / 8 + 1 + 3 <= ctx->bs){
83         *((uint8_t*)ctx->a + length_b / 8 + 1) ^= ctx->d;
84         *((uint8_t*)ctx->a + length_b / 8 + 2) ^= ctx->bs;
85         *((uint8_t*)ctx->a + length_b / 8 + 3) ^= 1;
86         }else{
87                 if(length_b / 8 + 1 + 2 <= ctx->bs){
88             *((uint8_t*)ctx->a + length_b / 8 + 1) ^= ctx->d;
89             *((uint8_t*)ctx->a + length_b / 8 + 2) ^= ctx->bs;
90                         keccak_f1600(ctx->a);
91                         ((uint8_t*)ctx->a)[0] ^= 0x01;
92                 }else{
93                         if(length_b/8+1+1 <= ctx->bs){
94                                 *((uint8_t*)ctx->a + length_b / 8 + 1) ^= ctx->d;
95                                 keccak_f1600(ctx->a);
96                                 ((uint8_t*)ctx->a)[0] ^= ctx->bs;
97                                 ((uint8_t*)ctx->a)[1] ^= 0x01;
98                         }else{
99                                 keccak_f1600(ctx->a);
100                                 ((uint8_t*)ctx->a)[0] ^= ctx->d;
101                                 ((uint8_t*)ctx->a)[1] ^= ctx->bs;
102                                 ((uint8_t*)ctx->a)[2] ^= 0x01;
103                         }
104                 }
105         }
106         keccak_f1600(ctx->a);
107 }
108
109 void keccak224_ctx2hash(void* dest, keccak_ctx_t* ctx){
110         keccak_ctx2hash(dest, 224, ctx);
111 }
112
113 void keccak256_ctx2hash(void* dest, keccak_ctx_t* ctx){
114         keccak_ctx2hash(dest, 256, ctx);
115 }
116
117 void keccak384_ctx2hash(void* dest, keccak_ctx_t* ctx){
118         keccak_ctx2hash(dest, 384, ctx);
119 }
120
121 void keccak512_ctx2hash(void* dest, keccak_ctx_t* ctx){
122         keccak_ctx2hash(dest, 512, ctx);
123 }
124
125 /*
126   1. SHA3-224: ⌊Keccak[r = 1152, c =  448, d = 28]⌋224
127   2. SHA3-256: ⌊Keccak[r = 1088, c =  512, d = 32]⌋256
128   3. SHA3-384: ⌊Keccak[r =  832, c =  768, d = 48]⌋384
129   4. SHA3-512: ⌊Keccak[r =  576, c = 1024, d = 64]⌋512
130 */
131 void keccak_init(uint16_t r, uint16_t c, uint8_t d, keccak_ctx_t* ctx){
132         memset(ctx->a, 0x00, 5 * 5 * 8);
133         ctx->r = r;
134         ctx->c = c;
135         ctx->d = d;
136         ctx->bs = (uint8_t)(r / 8);
137 }
138
139 void keccak224_init(keccak_ctx_t* ctx){
140         keccak_init(1152, 448, 28, ctx);
141 }
142
143 void keccak256_init(keccak_ctx_t* ctx){
144         keccak_init(1088, 512, 32, ctx);
145 }
146
147 void keccak384_init(keccak_ctx_t* ctx){
148         keccak_init( 832, 768, 48, ctx);
149 }
150
151 void keccak512_init(keccak_ctx_t* ctx){
152         keccak_init( 576, 1024, 64, ctx);
153 }