now including whirlpool

This commit is contained in:
bg 2011-01-04 16:40:47 +00:00
parent d1181933dc
commit 1de7784dc5
8 changed files with 12906 additions and 0 deletions

49
hfal/hfal_whirlpool.c Normal file
View File

@ -0,0 +1,49 @@
/* hfal_whirlpool.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2011 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_whirlpool.c
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2011-02-04
* \license GPLv3 or later
*
*/
#include <avr/pgmspace.h>
#include <stdlib.h>
#include "hashfunction_descriptor.h"
#include "whirlpool.h"
static const char whirlpool_str[] PROGMEM = "Whirlpool";
const hfdesc_t whirlpool_desc PROGMEM = {
HFDESC_TYPE_HASHFUNCTION,
0,
whirlpool_str,
sizeof(whirlpool_ctx_t),
512,
512,
(hf_init_fpt)whirlpool_init,
(hf_nextBlock_fpt)whirlpool_nextBlock,
(hf_lastBlock_fpt)whirlpool_lastBlock,
(hf_ctx2hash_fpt)whirlpool_ctx2hash,
(hf_free_fpt)NULL,
(hf_mem_fpt)NULL
};

36
hfal/hfal_whirlpool.h Normal file
View File

@ -0,0 +1,36 @@
/* hfal_whirlpool.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2011 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_whirlpool.h
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2011-02-04
* \license GPLv3 or later
*
*/
#ifndef HFAL_WHIRLPOOL_H_
#define HFAL_WHIRLPOOL_H_
#include <avr/pgmspace.h>
#include "hashfunction_descriptor.h"
extern const hfdesc_t whirlpool_desc;
#endif /* HFAL_WHIRLPOOL_H_ */

13
mkfiles/whirlpool.mk Normal file
View File

@ -0,0 +1,13 @@
# Makefile for Whirlpool
ALGO_NAME := WHIRLPOOL_C
# comment out the following line for removement of Whirlpool from the build process
HASHES += $(ALGO_NAME)
$(ALGO_NAME)_DIR := whirlpool/
$(ALGO_NAME)_INCDIR := hfal/ memxor/ gf256mul/
$(ALGO_NAME)_OBJ := whirlpool.o gf256mul.o memxor.o
$(ALGO_NAME)_TEST_BIN := main-whirlpool-test.o $(CLI_STD) $(HFAL_STD) hfal_whirlpool.o dump-asm.o dump-decl.o
$(ALGO_NAME)_NESSIE_TEST := "nessie"
$(ALGO_NAME)_PERFORMANCE_TEST := "performance"

View File

@ -0,0 +1,185 @@
/* main-whirlpool-test.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2011 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Whirlpool test-suit
*
*/
#include "config.h"
#include "uart_i.h"
#include "debug.h"
#include "whirlpool.h"
#include "nessie_hash_test.h"
#include "performance_test.h"
#include "hfal-performance.h"
#include "hfal-nessie.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "cli.h"
#include "shavs.h"
#include "hfal_whirlpool.h"
#include "dump.h"
char* algo_name = "Whirlpool";
const hfdesc_t* algolist[] PROGMEM = {
(hfdesc_t*)&whirlpool_desc,
NULL
};
/*****************************************************************************
* additional validation-functions *
*****************************************************************************/
void testrun_nessie_whirlpool(void){
hfal_nessie_multiple(algolist);
}
void testrun_whirlpool(void){
whirlpool_ctx_t ctx;
uint8_t hash[64];
uint8_t data[64];
memset(data, 0, 64);
data[0] = 'a';
data[1] = 'b';
data[2] = 'c';
whirlpool_init(&ctx);
whirlpool_lastBlock(&ctx, data, 3*8);
whirlpool_ctx2hash(hash, &ctx);
cli_putstr_P(PSTR("\r\nEmpty message hash:"));
cli_hexdump_block(hash, 64, 4, 16);
}
void testrun_performance_whirlpool(void){
hfal_performance_multiple(algolist);
}
void test_sbox(void){
uint8_t i=0;
cli_putstr_P(PSTR("\r\nWhirlpool S-Box:"));
do{
if(i%16==0){
cli_putstr_P(PSTR("\r\n\t"));
}
cli_hexdump_byte(whirlpool_sbox(i++));
cli_putc(' ');
}while(i);
}
void testrun_nessie2(void){
uint16_t i;
uint8_t j;
uint8_t data[128], hash[64];
whirlpool_ctx_t ctx;
char str[8];
memset(data, 0, 128);
cli_putstr_P(PSTR("\r\nMessage digests of strings of 0-bits and length L:"));
for(i=0; i<1024; ++i){
cli_putstr_P(PSTR("\r\n L = "));
itoa(i, str, 10);
j=4;
j-= strlen(str);
if(j>3){
j=0;
}
while(j--){
cli_putc(' ');
}
cli_putstr(str);
cli_putstr_P(PSTR(": "));
whirlpool_init(&ctx);
whirlpool_lastBlock(&ctx, data, i);
whirlpool_ctx2hash(hash, &ctx);
cli_hexdump(hash, 64);
}
cli_putstr_P(PSTR("\r\nMessage digests of all 512-bit strings S containing a single 1-bit:"));
for(i=0; i<512; ++i){
cli_putstr_P(PSTR("\r\n S = "));
data[i/8] = 0x80>>(i&7);
cli_hexdump(data, 64);
cli_putstr_P(PSTR(": "));
whirlpool_init(&ctx);
whirlpool_lastBlock(&ctx, data, 512);
whirlpool_ctx2hash(hash, &ctx);
data[i/8] = 0x00;
cli_hexdump(hash, 64);
}
cli_putstr_P(PSTR("\r\nIterated message digest computation (100000000 times): "));
uint32_t c;
memset(hash, 0, 64);
for(c=0; c<1000000; ++c){
whirlpool_init(&ctx);
whirlpool_lastBlock(&ctx, hash, 512);
whirlpool_ctx2hash(hash, &ctx);
}
cli_hexdump(hash, 64);
cli_putstr_P(PSTR("\r\n/* finish generating testvectors */\r\n"));
}
/*****************************************************************************
* main *
*****************************************************************************/
const char nessie_str[] PROGMEM = "nessie";
const char nessie2_str[] PROGMEM = "nessie2";
const char test_str[] PROGMEM = "test";
const char test_sbox_str[] PROGMEM = "test_sbox";
const char performance_str[] PROGMEM = "performance";
const char echo_str[] PROGMEM = "echo";
const char shavs_list_str[] PROGMEM = "shavs_list";
const char shavs_set_str[] PROGMEM = "shavs_set";
const char shavs_test1_str[] PROGMEM = "shavs_test1";
const char shavs_test2_str[] PROGMEM = "shavs_test2";
const char shavs_test3_str[] PROGMEM = "shavs_test3";
const char dump_str[] PROGMEM = "dump";
cmdlist_entry_t cmdlist[] PROGMEM = {
{ nessie_str, NULL, testrun_nessie_whirlpool },
{ nessie2_str, NULL, testrun_nessie2 },
{ test_str, NULL, testrun_whirlpool },
{ test_sbox_str, NULL, test_sbox },
{ performance_str, NULL, testrun_performance_whirlpool },
{ echo_str, (void*)1, (void_fpt)echo_ctrl },
{ shavs_list_str, NULL, shavs_listalgos },
{ shavs_set_str, (void*)1, (void_fpt)shavs_setalgo },
{ shavs_test1_str, NULL, shavs_test1 },
{ shavs_test2_str, NULL, shavs_test2 },
{ shavs_test3_str, NULL, shavs_test3 },
{ dump_str, (void*)1, (void_fpt)dump },
{ NULL, NULL, NULL }
};
int main (void){
DEBUG_INIT();
cli_rx = (cli_rx_fpt)uart0_getc;
cli_tx = (cli_tx_fpt)uart0_putc;
shavs_algolist=(hfdesc_t**)algolist;
shavs_algo=(hfdesc_t*)&whirlpool_desc;
for(;;){
cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
cli_putstr(algo_name);
cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
cmd_interface(cmdlist);
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

194
whirlpool/whirlpool.c Normal file
View File

@ -0,0 +1,194 @@
/* whirlpool.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2006-2011 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <avr/pgmspace.h>
#include <string.h>
#include "gf256mul.h"
#include "memxor.h"
#include "whirlpool.h"
#define DEBUG 0
#if DEBUG
#include "cli.h"
#endif
/*
u 0 1 2 3 4 5 6 7 8 9 A B C D E F
E(u) 1 B 9 C D 6 F 3 E 8 7 4 A 2 5 0
E -1(u) F 0 D 7 B E 5 A 9 2 C 1 3 4 8 6
*/
static uint8_t eeinv_box[16] PROGMEM = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
0x1F, 0xB0, 0x9D, 0xC7, 0xDB, 0x6E, 0xF5, 0x3A,
0xE9, 0x82, 0x7C, 0x41, 0xA3, 0x24, 0x58, 0x06
};
static uint8_t r_box[16] PROGMEM = {
0x77, 0xCC, 0xBB, 0xDD, 0xEE, 0x44, 0x99, 0xFF,
0x66, 0x33, 0x88, 0xAA, 0x22, 0x55, 0x11, 0x00
};
uint8_t whirlpool_sbox(uint8_t a){
uint8_t b,c,d;
b = pgm_read_byte(eeinv_box+(a&0x0f));
d = pgm_read_byte(eeinv_box+(a>>4));
c = ((d>>4)^b)&0x0f;
b = (b&0x0f)|(d&0xf0);
b ^= pgm_read_byte(r_box+c);
c = pgm_read_byte(eeinv_box+(b&0x0f))&0x0f;
c |= pgm_read_byte(eeinv_box+(b>>4))&0xf0;
return c;
}
static void gamma(uint8_t* a){
uint8_t i;
for(i=0; i<64; ++i){
*a = whirlpool_sbox(*a);
++a;
}
}
static void pi(uint8_t* a){
uint8_t b[8];
uint8_t i,j;
for(i=1; i<8; ++i){
for(j=0; j<8; ++j){
b[j] = a[i+8*((8+j-i)%8)];
}
for(j=0; j<8; ++j){
a[j*8+i] = b[j];
}
}
}
static uint8_t theta_matrix[8] PROGMEM = {
0x1, 0x1, 0x4, 0x1, 0x8, 0x5, 0x2, 0x9
};
#define POLYNOM 0x1D
static void theta(uint8_t* a){
uint8_t b[8], c, accu;
uint8_t i,j,k;
for(i=0; i<8; ++i){
for(j=0; j<8;++j){
accu = 0;
for(k=0; k<8; ++k){
c = pgm_read_byte(theta_matrix+((8+j-k)%8));
accu ^= gf256mul(a[8*i+k], c, POLYNOM);
}
b[j] = accu;
}
memcpy(a+8*i, b, 8);
}
}
static void w_round(uint8_t* a, const uint8_t* k){
gamma(a);
#if DEBUG
cli_putstr_P(PSTR("\r\n pre-pi:"));
cli_hexdump_block(a, 64, 4, 8);
#endif
pi(a);
#if DEBUG
cli_putstr_P(PSTR("\r\n post-pi & pre-theta:"));
cli_hexdump_block(a, 64, 4, 8);
#endif
theta(a);
#if DEBUG
cli_putstr_P(PSTR("\r\n post-theta:"));
cli_hexdump_block(a, 64, 4, 8);
#endif
memxor(a, k, 64);
}
static void w_enc(uint8_t *a, const uint8_t* k){
#if DEBUG
cli_putstr_P(PSTR("\r\n== w_enc ==\r\n w'_00:"));
cli_hexdump_block(a, 64, 4, 8);
cli_putstr_P(PSTR("\r\n k_00:"));
cli_hexdump_block(k, 64, 4, 8);
#endif
uint8_t rk[64], rc[64];
uint8_t r,i;
memxor(a, k, 64);
memcpy(rk, k, 64);
memset(rc+8, 0, 56);
for(r=0; r<10; ++r){
for(i=0; i<8; ++i){
rc[i] = whirlpool_sbox(r*8+i);
}
w_round(rk, rc);
w_round(a, rk);
#if DEBUG
cli_putstr_P(PSTR("\r\n w'_"));
cli_hexdump_byte(r+1);
cli_putc(':');
cli_hexdump_block(a, 64, 4, 8);
cli_putstr_P(PSTR("\r\n k_"));
cli_hexdump_byte(r+1);
cli_putc(':');
cli_hexdump_block(rk, 64, 4, 8);
#endif
}
}
void whirlpool_init(whirlpool_ctx_t* ctx){
memset(ctx->s, 0, 64);
ctx->blocks = 0;
}
void whirlpool_nextBlock(whirlpool_ctx_t* ctx, const void* block){
uint8_t state[64];
ctx->blocks += 1;
memcpy(state, block, 64);
w_enc(state, (uint8_t*)(ctx->s));
memxor(ctx->s, state, 64);
memxor((ctx->s), block, 64);
}
void whirlpool_lastBlock(whirlpool_ctx_t* ctx, const void* block, uint16_t length_b){
while(length_b>=512){
whirlpool_nextBlock(ctx, block);
block = (uint8_t*)block + 64;
length_b -= 512;
}
uint8_t buffer[64];
uint8_t i=8;
uint64_t length;
length = ctx->blocks*512+length_b;
memset(buffer, 0, 64);
memcpy(buffer, block, (length_b+7)/8);
buffer[length_b/8] |= 0x80>>(length_b&7);
if(length_b>255){
whirlpool_nextBlock(ctx, buffer);
memset(buffer, 0, 56);
}
do{
buffer[56+(--i)] = length&0xff;
length >>= 8;
}while(i);
whirlpool_nextBlock(ctx, buffer);
}
void whirlpool_ctx2hash(void* dest, const whirlpool_ctx_t* ctx){
memcpy(dest, (ctx->s), 64);
}

37
whirlpool/whirlpool.h Normal file
View File

@ -0,0 +1,37 @@
/* wirlpool.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2011 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WIRLPOOL_H_
#define WIRLPOOL_H_
#include <stdint.h>
typedef struct {
uint8_t s[8][8];
uint32_t blocks;
} whirlpool_ctx_t;
uint8_t whirlpool_sbox(uint8_t a);
void whirlpool_init(whirlpool_ctx_t* ctx);
void whirlpool_nextBlock(whirlpool_ctx_t* ctx,const void* block);
void whirlpool_lastBlock(whirlpool_ctx_t* ctx, const void* block, uint16_t length_b);
void whirlpool_ctx2hash(void* dest, const whirlpool_ctx_t* ctx);
#endif /* WIRLPOOL_H_ */