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(uint64_t a[5][5]);
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;
71 memxor(ctx->a, block, (length_b)/8);
74 /* we have some single bits */
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;
80 ((uint8_t*)ctx->a)[length_b / 8] ^= 0x01;
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;
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;
91 ((uint8_t*)ctx->a)[0] ^= 0x01;
93 if(length_b/8+1+1 <= ctx->bs){
94 *((uint8_t*)ctx->a + length_b / 8 + 1) ^= ctx->d;
96 ((uint8_t*)ctx->a)[0] ^= ctx->bs;
97 ((uint8_t*)ctx->a)[1] ^= 0x01;
100 ((uint8_t*)ctx->a)[0] ^= ctx->d;
101 ((uint8_t*)ctx->a)[1] ^= ctx->bs;
102 ((uint8_t*)ctx->a)[2] ^= 0x01;
106 keccak_f1600(ctx->a);
109 void keccak_ctx2hash(void* dest, uint16_t length_b, keccak_ctx_t* ctx){
110 while(length_b>=ctx->r){
111 memcpy(dest, ctx->a, ctx->bs);
112 dest = (uint8_t*)dest + ctx->bs;
114 keccak_f1600(ctx->a);
116 memcpy(dest, ctx->a, (length_b+7)/8);
119 void keccak224_ctx2hash(void* dest, keccak_ctx_t* ctx){
120 keccak_ctx2hash(dest, 224, ctx);
123 void keccak256_ctx2hash(void* dest, keccak_ctx_t* ctx){
124 keccak_ctx2hash(dest, 256, ctx);
127 void keccak384_ctx2hash(void* dest, keccak_ctx_t* ctx){
128 keccak_ctx2hash(dest, 384, ctx);
131 void keccak512_ctx2hash(void* dest, keccak_ctx_t* ctx){
132 keccak_ctx2hash(dest, 512, ctx);
136 1. SHA3-224: ⌊Keccak[r = 1152, c = 448, d = 28]⌋224
137 2. SHA3-256: ⌊Keccak[r = 1088, c = 512, d = 32]⌋256
138 3. SHA3-384: ⌊Keccak[r = 832, c = 768, d = 48]⌋384
139 4. SHA3-512: ⌊Keccak[r = 576, c = 1024, d = 64]⌋512
141 void keccak_init(uint16_t r, uint16_t c, uint8_t d, keccak_ctx_t* ctx){
142 memset(ctx->a, 0x00, 5 * 5 * 8);
146 ctx->bs = (uint8_t)(r / 8);
149 void keccak224_init(keccak_ctx_t* ctx){
150 keccak_init(1152, 448, 28, ctx);
153 void keccak256_init(keccak_ctx_t* ctx){
154 keccak_init(1088, 512, 32, ctx);
157 void keccak384_init(keccak_ctx_t* ctx){
158 keccak_init( 832, 768, 48, ctx);
161 void keccak512_init(keccak_ctx_t* ctx){
162 keccak_init( 576, 1024, 64, ctx);