3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
36 void keccak_dump_state(uint64_t a[5][5]){
39 cli_putstr_P(PSTR("\r\n"));
41 cli_putstr_P(PSTR(": "));
43 cli_hexdump_rev(&(a[i][j]), 8);
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);
63 void keccak_f1600(uint8_t a[200]);
65 void keccak_lastBlock(keccak_ctx_t* ctx, const void* block, uint16_t length_b){
67 while(length_b >= ctx->r){
68 keccak_nextBlock(ctx, block);
69 block = (uint8_t*)block + ctx->bs;
72 length_B = length_b / 8;
73 memxor(ctx->a, block, length_B);
76 /* we have some single bits */
78 t = ((uint8_t*)block)[length_B] >> (8 - (length_b & 7));
79 t |= 0x01 << (length_b & 7);
80 ctx->a[length_B] ^= t;
82 ctx->a[length_B] ^= 0x01;
84 if(length_b == ctx->r - 1){
87 ctx->a[ctx->bs - 1] ^= 0x80;
92 void keccak_lastBlock(keccak_ctx_t* ctx, const void* block, uint16_t length_b){
94 while(length_b >= ctx->r){
95 keccak_nextBlock(ctx, block);
96 block = (uint8_t*)block + ctx->bs;
99 length_B = length_b / 8;
100 memxor(ctx->a, block, length_B);
103 /* we have some single bits */
105 t = ((uint8_t*)block)[length_B] >> (8 - (length_b & 7));
106 t |= 0x01 << (length_b & 7);
107 ctx->a[length_B] ^= t;
109 ctx->a[length_B] ^= 0x01;
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;
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;
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;
128 keccak_f1600(ctx->a);
130 ctx->a[1] ^= ctx->bs;
135 keccak_f1600(ctx->a);
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;
145 keccak_f1600(ctx->a);
147 memcpy(dest, ctx->a, (length_b+7)/8);
151 void keccak224_ctx2hash(void* dest, keccak_ctx_t* ctx){
152 keccak_ctx2hash(dest, 224, ctx);
155 void keccak256_ctx2hash(void* dest, keccak_ctx_t* ctx){
156 keccak_ctx2hash(dest, 256, ctx);
159 void keccak384_ctx2hash(void* dest, keccak_ctx_t* ctx){
160 keccak_ctx2hash(dest, 384, ctx);
163 void keccak512_ctx2hash(void* dest, keccak_ctx_t* ctx){
164 keccak_ctx2hash(dest, 512, ctx);
168 1. SHA3-224: ⌊Keccak[r = 1152, c = 448, d = 28]⌋224
169 2. SHA3-256: ⌊Keccak[r = 1088, c = 512, d = 32]⌋256
170 3. SHA3-384: ⌊Keccak[r = 832, c = 768, d = 48]⌋384
171 4. SHA3-512: ⌊Keccak[r = 576, c = 1024, d = 64]⌋512
174 void keccak_init(uint16_t r, keccak_ctx_t* ctx){
175 memset(ctx->a, 0x00, 5 * 5 * 8);
177 ctx->bs = (uint8_t)(r / 8);
180 void keccak224_init(keccak_ctx_t* ctx){
181 keccak_init(1152, ctx);
184 void keccak256_init(keccak_ctx_t* ctx){
185 keccak_init(1088, ctx);
188 void keccak384_init(keccak_ctx_t* ctx){
189 keccak_init( 832, ctx);
192 void keccak512_init(keccak_ctx_t* ctx){
193 keccak_init( 576, ctx);