2008-05-26 19:13:21 +00:00
|
|
|
/* nessie_hash_test.c */
|
|
|
|
/*
|
2009-01-30 23:00:04 +00:00
|
|
|
This file is part of the AVR-Crypto-Lib.
|
2008-05-26 19:13:21 +00:00
|
|
|
Copyright (C) 2008 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/>.
|
|
|
|
*/
|
2008-04-06 05:58:07 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* author: Daniel Otte
|
|
|
|
* email: daniel.otte@rub.de
|
|
|
|
* license: GPLv3
|
|
|
|
*
|
|
|
|
* a suit for running the nessie-tests for hashes
|
|
|
|
*
|
|
|
|
* */
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "nessie_hash_test.h"
|
2008-04-08 03:21:13 +00:00
|
|
|
#include "nessie_common.h"
|
2009-03-01 19:29:27 +00:00
|
|
|
#include "dbz_strings.h"
|
2008-04-06 05:58:07 +00:00
|
|
|
|
|
|
|
nessie_hash_ctx_t nessie_hash_ctx;
|
2009-05-11 11:57:10 +00:00
|
|
|
uint8_t nessie_hash_quick=0;
|
2008-04-06 05:58:07 +00:00
|
|
|
|
2009-03-01 19:29:27 +00:00
|
|
|
#define HASHSIZE_B ((nessie_hash_ctx.hashsize_b+7)/8)
|
|
|
|
#define BLOCKSIZE_B (nessie_hash_ctx.blocksize_B)
|
|
|
|
|
2008-04-06 05:58:07 +00:00
|
|
|
static
|
2009-03-01 19:29:27 +00:00
|
|
|
void ascii_hash_P(PGM_P data, PGM_P desc){
|
2008-04-06 05:58:07 +00:00
|
|
|
uint8_t ctx[nessie_hash_ctx.ctx_size_B];
|
2009-03-01 19:29:27 +00:00
|
|
|
uint8_t hash[HASHSIZE_B];
|
2008-04-06 05:58:07 +00:00
|
|
|
uint16_t sl;
|
2009-03-01 19:29:27 +00:00
|
|
|
uint8_t buffer[BLOCKSIZE_B];
|
2008-04-06 05:58:07 +00:00
|
|
|
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTSTR_P(PSTR("\r\n message="));
|
2009-03-01 19:29:27 +00:00
|
|
|
NESSIE_PUTSTR_P(desc);
|
2008-04-06 05:58:07 +00:00
|
|
|
nessie_hash_ctx.hash_init(ctx);
|
2009-03-01 19:29:27 +00:00
|
|
|
sl = strlen_P(data);
|
|
|
|
while(sl>=BLOCKSIZE_B){
|
|
|
|
memcpy_P(buffer, data, BLOCKSIZE_B);
|
2009-04-20 12:35:04 +00:00
|
|
|
nessie_hash_ctx.hash_next(ctx, buffer);
|
2009-03-01 19:29:27 +00:00
|
|
|
data += BLOCKSIZE_B;
|
|
|
|
sl -= BLOCKSIZE_B;
|
2008-04-06 05:58:07 +00:00
|
|
|
}
|
2009-03-01 19:29:27 +00:00
|
|
|
memcpy_P(buffer, data, sl);
|
2009-04-20 12:35:04 +00:00
|
|
|
nessie_hash_ctx.hash_last(ctx, buffer, sl*8);
|
2008-04-06 05:58:07 +00:00
|
|
|
nessie_hash_ctx.hash_conv(hash, ctx);
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
|
2008-04-06 05:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// message=1 million times "a"
|
|
|
|
|
|
|
|
static
|
|
|
|
void amillion_hash(void){
|
|
|
|
uint8_t ctx[nessie_hash_ctx.ctx_size_B];
|
|
|
|
uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
|
|
|
|
uint8_t block[nessie_hash_ctx.blocksize_B];
|
|
|
|
uint32_t n=1000000LL;
|
2009-02-04 13:50:15 +00:00
|
|
|
uint16_t i=0;
|
2008-04-06 05:58:07 +00:00
|
|
|
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTSTR_P(PSTR("\r\n message="));
|
|
|
|
NESSIE_PUTSTR_P(PSTR("1 million times \"a\""));
|
2008-04-06 05:58:07 +00:00
|
|
|
memset(block, 'a', nessie_hash_ctx.blocksize_B);
|
|
|
|
nessie_hash_ctx.hash_init(ctx);
|
|
|
|
while(n>=nessie_hash_ctx.blocksize_B){
|
2009-04-20 12:35:04 +00:00
|
|
|
nessie_hash_ctx.hash_next(ctx, block);
|
2008-04-06 05:58:07 +00:00
|
|
|
n -= nessie_hash_ctx.blocksize_B;
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_SEND_ALIVE_A(i++);
|
2008-04-06 05:58:07 +00:00
|
|
|
}
|
2009-04-20 12:35:04 +00:00
|
|
|
nessie_hash_ctx.hash_last(ctx, block, n*8);
|
2008-04-06 05:58:07 +00:00
|
|
|
nessie_hash_ctx.hash_conv(hash, ctx);
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
|
2008-04-06 05:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
void zero_hash(uint16_t n){
|
|
|
|
uint8_t ctx[nessie_hash_ctx.ctx_size_B];
|
|
|
|
uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
|
|
|
|
uint8_t block[nessie_hash_ctx.blocksize_B];
|
|
|
|
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTSTR_P(PSTR("\r\n message="));
|
2008-04-06 05:58:07 +00:00
|
|
|
if(n>=10000)
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC('0'+n/10000);
|
2008-04-06 05:58:07 +00:00
|
|
|
if(n>=1000)
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC('0'+(n/1000)%10);
|
2008-04-06 05:58:07 +00:00
|
|
|
if(n>=100)
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC('0'+(n/100)%10);
|
2008-04-06 05:58:07 +00:00
|
|
|
if(n>=10)
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC('0'+(n/10)%10);
|
|
|
|
NESSIE_PUTC('0'+n%10);
|
|
|
|
NESSIE_PUTSTR_P(PSTR(" zero bits"));
|
2008-04-06 05:58:07 +00:00
|
|
|
|
|
|
|
memset(block, 0, nessie_hash_ctx.blocksize_B);
|
|
|
|
nessie_hash_ctx.hash_init(ctx);
|
|
|
|
while(n>=nessie_hash_ctx.blocksize_B*8){
|
2009-04-20 12:35:04 +00:00
|
|
|
nessie_hash_ctx.hash_next(ctx, block);
|
2008-04-06 05:58:07 +00:00
|
|
|
n -= nessie_hash_ctx.blocksize_B*8;
|
|
|
|
}
|
2009-04-20 12:35:04 +00:00
|
|
|
nessie_hash_ctx.hash_last(ctx, block, n);
|
2008-04-06 05:58:07 +00:00
|
|
|
nessie_hash_ctx.hash_conv(hash, ctx);
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
|
2008-04-06 05:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void one_in512_hash(uint16_t pos){
|
|
|
|
uint8_t ctx[nessie_hash_ctx.ctx_size_B];
|
|
|
|
uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
|
|
|
|
uint8_t block[nessie_hash_ctx.blocksize_B];
|
|
|
|
uint16_t n=512;
|
|
|
|
char* tab[8]={"80", "40", "20", "10",
|
|
|
|
"08", "04", "02", "01" };
|
|
|
|
|
|
|
|
pos&=511;
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTSTR_P(PSTR("\r\n message="));
|
|
|
|
NESSIE_PUTSTR_P(PSTR("512-bit string: "));
|
2008-04-06 05:58:07 +00:00
|
|
|
if((pos/8) >=10){
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC('0'+(pos/8/10)%10);
|
2008-04-06 05:58:07 +00:00
|
|
|
} else {
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC(' ');
|
2008-04-06 05:58:07 +00:00
|
|
|
}
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC('0'+(pos/8)%10);
|
|
|
|
NESSIE_PUTSTR_P(PSTR("*00,"));
|
|
|
|
NESSIE_PUTSTR(tab[pos&7]);
|
|
|
|
NESSIE_PUTC(',');
|
2008-04-06 05:58:07 +00:00
|
|
|
if(63-(pos/8) >=10){
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC('0'+((63-pos/8)/10)%10);
|
2008-04-06 05:58:07 +00:00
|
|
|
} else {
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC(' ');
|
2008-04-06 05:58:07 +00:00
|
|
|
}
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC('0'+(63-pos/8)%10);
|
|
|
|
NESSIE_PUTSTR_P(PSTR("*00"));
|
2008-04-06 05:58:07 +00:00
|
|
|
|
|
|
|
/* now the real stuff */
|
2008-04-06 12:24:49 +00:00
|
|
|
memset(block, 0, 512/8);
|
2008-04-06 05:58:07 +00:00
|
|
|
block[pos>>3] = 0x80>>(pos&0x7);
|
|
|
|
nessie_hash_ctx.hash_init(ctx);
|
|
|
|
while(n>=nessie_hash_ctx.blocksize_B*8){
|
2009-04-20 12:35:04 +00:00
|
|
|
nessie_hash_ctx.hash_next(ctx, block);
|
2008-04-06 05:58:07 +00:00
|
|
|
n -= nessie_hash_ctx.blocksize_B*8;
|
|
|
|
}
|
2009-04-20 12:35:04 +00:00
|
|
|
nessie_hash_ctx.hash_last(ctx, block, n);
|
2008-04-06 05:58:07 +00:00
|
|
|
nessie_hash_ctx.hash_conv(hash, ctx);
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
|
2008-04-06 05:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void tv4_hash(void){
|
|
|
|
uint8_t ctx[nessie_hash_ctx.ctx_size_B];
|
|
|
|
uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8];
|
2008-11-25 01:23:22 +00:00
|
|
|
uint8_t block[nessie_hash_ctx.hashsize_b/8];
|
|
|
|
uint16_t n=nessie_hash_ctx.hashsize_b;
|
2008-04-06 05:58:07 +00:00
|
|
|
uint32_t i;
|
|
|
|
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTSTR_P(PSTR("\r\n message="));
|
2008-11-25 01:23:22 +00:00
|
|
|
if(nessie_hash_ctx.hashsize_b>=10000)
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10000)%10);
|
2008-11-25 01:23:22 +00:00
|
|
|
if(nessie_hash_ctx.hashsize_b>=1000)
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/1000)%10);
|
2008-11-25 01:23:22 +00:00
|
|
|
if(nessie_hash_ctx.hashsize_b>=100)
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/100)%10);
|
2008-11-25 01:23:22 +00:00
|
|
|
if(nessie_hash_ctx.hashsize_b>=10)
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10)%10);
|
|
|
|
NESSIE_PUTC('0' + nessie_hash_ctx.hashsize_b%10);
|
2008-11-25 01:23:22 +00:00
|
|
|
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_PUTSTR_P(PSTR(" zero bits"));
|
2008-04-06 05:58:07 +00:00
|
|
|
memset(block, 0, 256/8);
|
|
|
|
|
|
|
|
nessie_hash_ctx.hash_init(ctx);
|
|
|
|
while(n>=nessie_hash_ctx.blocksize_B*8){
|
2009-04-20 12:35:04 +00:00
|
|
|
nessie_hash_ctx.hash_next(ctx, block);
|
2008-04-06 05:58:07 +00:00
|
|
|
n -= nessie_hash_ctx.blocksize_B*8;
|
|
|
|
}
|
2009-04-20 12:35:04 +00:00
|
|
|
nessie_hash_ctx.hash_last(ctx, block, n);
|
2008-04-06 05:58:07 +00:00
|
|
|
nessie_hash_ctx.hash_conv(hash, ctx);
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8);
|
2009-05-11 11:57:10 +00:00
|
|
|
if(nessie_hash_quick)
|
|
|
|
return;
|
2008-04-06 05:58:07 +00:00
|
|
|
for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */
|
|
|
|
nessie_hash_ctx.hash_init(ctx);
|
2009-04-20 12:35:04 +00:00
|
|
|
nessie_hash_ctx.hash_last(ctx, hash, nessie_hash_ctx.hashsize_b);
|
2008-04-06 05:58:07 +00:00
|
|
|
nessie_hash_ctx.hash_conv(hash, ctx);
|
2009-02-04 13:50:15 +00:00
|
|
|
NESSIE_SEND_ALIVE_A(i);
|
2008-04-06 05:58:07 +00:00
|
|
|
}
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8);
|
2008-04-06 05:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
"" (empty string)
|
|
|
|
message="a"
|
|
|
|
message="abc"
|
|
|
|
message="message digest"
|
|
|
|
message="abcdefghijklmnopqrstuvwxyz"
|
|
|
|
message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
|
|
|
|
message="A...Za...z0...9"
|
|
|
|
message=8 times "1234567890"
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
void nessie_hash_run(void){
|
|
|
|
uint16_t i;
|
|
|
|
uint8_t set;
|
|
|
|
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_header(nessie_hash_ctx.name, 0, 0, nessie_hash_ctx.hashsize_b, 0, 0);
|
2008-04-06 05:58:07 +00:00
|
|
|
/* test set 1 */
|
2009-03-01 19:29:27 +00:00
|
|
|
char* challange_dbz= PSTR(
|
|
|
|
"\0"
|
|
|
|
"\"\" (empty string)\0"
|
|
|
|
"a\0"
|
|
|
|
"\"a\"\0"
|
|
|
|
"abc\0"
|
|
|
|
"\"abc\"\0"
|
|
|
|
"message digest\0"
|
|
|
|
"\"message digest\"\0"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz\0"
|
|
|
|
"\"abcdefghijklmnopqrstuvwxyz\"\0"
|
|
|
|
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\0"
|
|
|
|
"\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\"\0"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"0123456789\0"
|
|
|
|
"\"A...Za...z0...9\"\0"
|
|
|
|
"1234567890123456789012345678901234567890"
|
|
|
|
"1234567890123456789012345678901234567890\0"
|
|
|
|
"8 times \"1234567890\"\0"
|
|
|
|
);
|
|
|
|
PGM_P challange[16];
|
2008-04-06 05:58:07 +00:00
|
|
|
set=1;
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_setheader(set);
|
2009-03-01 19:29:27 +00:00
|
|
|
dbz_splitup_P(challange_dbz, challange);
|
2008-04-06 05:58:07 +00:00
|
|
|
for(i=0; i<8; ++i){
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_set_vector(set, i);
|
2009-03-01 19:29:27 +00:00
|
|
|
ascii_hash_P(challange[2*i], challange[2*i+1]);
|
2008-04-06 05:58:07 +00:00
|
|
|
}
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_set_vector(set, i);
|
2009-05-11 11:57:10 +00:00
|
|
|
if(!nessie_hash_quick)
|
|
|
|
amillion_hash();
|
2008-04-06 05:58:07 +00:00
|
|
|
/* test set 2 */
|
|
|
|
set=2;
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_setheader(set);
|
2008-04-06 05:58:07 +00:00
|
|
|
for(i=0; i<1024; ++i){
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_set_vector(set, i);
|
2008-04-06 05:58:07 +00:00
|
|
|
zero_hash(i);
|
|
|
|
}
|
|
|
|
/* test set 3 */
|
|
|
|
set=3;
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_setheader(set);
|
2008-04-06 05:58:07 +00:00
|
|
|
for(i=0; i<512; ++i){
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_set_vector(set, i);
|
2008-04-06 05:58:07 +00:00
|
|
|
one_in512_hash(i);
|
|
|
|
}
|
|
|
|
/* test set 4 */
|
|
|
|
set=4;
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_setheader(set);
|
|
|
|
nessie_print_set_vector(set, 0);
|
2008-04-06 05:58:07 +00:00
|
|
|
tv4_hash();
|
|
|
|
|
2008-04-08 03:21:13 +00:00
|
|
|
nessie_print_footer();
|
2008-04-06 05:58:07 +00:00
|
|
|
}
|