From 222f3914d6308961e4e3d04ec0ea5e9ae652a1f9 Mon Sep 17 00:00:00 2001 From: bg Date: Sun, 6 Apr 2008 05:58:07 +0000 Subject: [PATCH] sha256 and xtea got each a own testsuit --- main-arcfour-test.c | 10 +- main-serpent-test.c | 7 +- main-sha256-test.c | 70 ++++++++++ main-xtea-test.c | 68 +++++++++ md5.c | 2 +- nessie_hash_test.c | 331 ++++++++++++++++++++++++++++++++++++++++++++ nessie_hash_test.h | 28 ++++ sha256.mk | 4 +- xtea.mk | 4 +- 9 files changed, 510 insertions(+), 14 deletions(-) create mode 100644 main-sha256-test.c create mode 100644 main-xtea-test.c create mode 100644 nessie_hash_test.c create mode 100644 nessie_hash_test.h diff --git a/main-arcfour-test.c b/main-arcfour-test.c index 24f88ec..0274bd5 100644 --- a/main-arcfour-test.c +++ b/main-arcfour-test.c @@ -25,13 +25,13 @@ void arcfour_genctx_dummy(uint8_t* key, uint16_t keysize, void* ctx){ -void testrun_arcfour(void){ +void testrun_nessie_arcfour(void){ nessie_stream_ctx.outsize_b = 8; /* actually unused */ nessie_stream_ctx.keysize_b = 128; /* this is theone we have refrence vectors for */ nessie_stream_ctx.name = cipher_name; nessie_stream_ctx.ctx_size_B = sizeof(arcfour_ctx_t); - nessie_stream_ctx.cipher_genctx = arcfour_genctx_dummy; - nessie_stream_ctx.cipher_enc = arcfour_gen; + nessie_stream_ctx.cipher_genctx = (nessie_stream_genctx_fpt)arcfour_genctx_dummy; + nessie_stream_ctx.cipher_enc = (nessie_stream_genenc_fpt)arcfour_gen; nessie_stream_run(); } @@ -54,8 +54,8 @@ int main (void){ restart: while(1){ if (!getnextwordn(str,20)) {DEBUG_S("DBG: W1\r\n"); goto error;} - if (strcmp(str, "test")) {DEBUG_S("DBG: 1b\r\n"); goto error;} - testrun_arcfour(); + if (strcmp(str, "nessie")) {DEBUG_S("DBG: 1b\r\n"); goto error;} + testrun_nessie_arcfour(); goto restart; continue; error: diff --git a/main-serpent-test.c b/main-serpent-test.c index ed8cf70..2f0f222 100644 --- a/main-serpent-test.c +++ b/main-serpent-test.c @@ -23,7 +23,7 @@ void serpent_genctx_dummy(uint8_t* key, uint16_t keysize, void* ctx){ serpent_genctx(key, keysize&0xff, ctx); } -void testrun_serpent(void){ +void testrun_nessie_serpent(void){ nessie_ctx.blocksize_B = 16; nessie_ctx.keysize = 128; nessie_ctx.name = cipher_name; @@ -60,14 +60,13 @@ int main (void){ restart: while(1){ if (!getnextwordn(str,20)) {DEBUG_S("DBG: W1\r\n"); goto error;} - if (strcmp(str, "test")) {DEBUG_S("DBG: 1b\r\n"); goto error;} - testrun_serpent(); + if (strcmp(str, "nessie")) {DEBUG_S("DBG: 1b\r\n"); goto error;} + testrun_nessie_serpent(); goto restart; continue; error: uart_putstr("ERROR\r\n"); } - } diff --git a/main-sha256-test.c b/main-sha256-test.c new file mode 100644 index 0000000..b1ea95d --- /dev/null +++ b/main-sha256-test.c @@ -0,0 +1,70 @@ +/* + * SHA-256 test-suit + * +*/ + +#include "config.h" +#include "serial-tools.h" +#include "uart.h" +#include "debug.h" + +#include "sha256.h" +#include "nessie_hash_test.h" + +#include +#include + +char* algo_name = "SHA-256"; + +/***************************************************************************** + * additional validation-functions * + *****************************************************************************/ +void sha256_next_dummy(void* buffer, void* ctx){ + sha256_nextBlock(ctx, buffer); +} + +void sha256_last_dummy(void* buffer, uint16_t size_b, void* ctx){ + sha256_lastBlock(ctx, buffer, size_b); +} + +void testrun_nessie_sha256(void){ + nessie_hash_ctx.hashsize_b = 256; + nessie_hash_ctx.blocksize_B = 512/8; + nessie_hash_ctx.ctx_size_B = sizeof(sha256_ctx_t); + nessie_hash_ctx.name = algo_name; + nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)sha256_init; + nessie_hash_ctx.hash_next = (nessie_hash_next_fpt)sha256_next_dummy; + nessie_hash_ctx.hash_last = (nessie_hash_last_fpt)sha256_last_dummy; + nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)sha256_ctx2hash; + + nessie_hash_run(); +} + + + +/***************************************************************************** + * main * + *****************************************************************************/ + +int main (void){ + char str[20]; + DEBUG_INIT(); + uart_putstr("\r\n"); + + uart_putstr_P(PSTR("\r\n\r\nCrypto-VS (")); + uart_putstr(algo_name); + uart_putstr_P(PSTR(")\r\nloaded and running\r\n")); + +restart: + while(1){ + if (!getnextwordn(str,20)) {DEBUG_S("DBG: W1\r\n"); goto error;} + if (strcmp(str, "nessie")) {DEBUG_S("DBG: 1b\r\n"); goto error;} + testrun_nessie_sha256(); + goto restart; + continue; + error: + uart_putstr("ERROR\r\n"); + } + +} + diff --git a/main-xtea-test.c b/main-xtea-test.c new file mode 100644 index 0000000..1649702 --- /dev/null +++ b/main-xtea-test.c @@ -0,0 +1,68 @@ +/* + * XTEA test-suit + * +*/ + +#include "config.h" +#include "serial-tools.h" +#include "uart.h" +#include "debug.h" + +#include "xtea.h" +#include "nessie_bc_test.h" + +#include +#include + +char* cipher_name = "XTEA"; + +void xtea_genctx_dummy(uint8_t* key, uint16_t keysize, void* ctx){ + memcpy(ctx, key, (keysize+7)/8); +} + +void xtea_enc_dummy(uint8_t* buffer, void* ctx){ + xtea_enc((uint32_t*)buffer, (uint32_t*)buffer, ctx); +} + +void xtea_dec_dummy(uint8_t* buffer, void* ctx){ + xtea_dec((uint32_t*)buffer, (uint32_t*)buffer, ctx); +} + +void testrun_xtea(void){ + nessie_ctx.blocksize_B = 8; + nessie_ctx.keysize = 128; + nessie_ctx.name = cipher_name; + nessie_ctx.ctx_size_B = 128/8; + nessie_ctx.cipher_enc = (nessie_enc_fpt)xtea_enc_dummy; + nessie_ctx.cipher_dec = (nessie_dec_fpt)xtea_dec_dummy; + nessie_ctx.cipher_genctx = (nessie_gen_fpt)xtea_genctx_dummy; + + nessie_run(); +} + + + +/***************************************************************************** + * main * + *****************************************************************************/ + +int main (void){ + char str[20]; + DEBUG_INIT(); + uart_putstr("\r\n"); + + uart_putstr_P(PSTR("\r\n\r\nCrypto-VS (")); + uart_putstr(cipher_name); + uart_putstr_P(PSTR(")\r\nloaded and running\r\n")); + +restart: + while(1){ + if (!getnextwordn(str,20)) {DEBUG_S("DBG: W1\r\n"); goto error;} + if (strcmp(str, "nessie")) {DEBUG_S("DBG: 1b\r\n"); goto error;} + testrun_xtea(); + goto restart; + continue; + error: + uart_putstr("ERROR\r\n"); + } +} diff --git a/md5.c b/md5.c index 3c520ed..5c7bb9a 100644 --- a/md5.c +++ b/md5.c @@ -120,7 +120,7 @@ void md5_lastBlock(md5_ctx_t *state, void* block, uint16_t length){ while (length >= 512){ md5_nextBlock(state, block); length -= 512; - block += 512/8; + block = ((uint8_t*)block) + 512/8; } memset(b, 0, 64); memcpy(b, block, length/8); diff --git a/nessie_hash_test.c b/nessie_hash_test.c new file mode 100644 index 0000000..9417623 --- /dev/null +++ b/nessie_hash_test.c @@ -0,0 +1,331 @@ +/** + * + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + * + * a suit for running the nessie-tests for hashes + * + * */ +#include +#include +#include "nessie_hash_test.h" +#include "uart.h" + +nessie_hash_ctx_t nessie_hash_ctx; + +static void printblock(uint8_t* block, uint16_t blocksize_bit){ + char tab [] = {'0', '1', '2', '3', + '4', '5', '6', '7', + '8', '9', 'A', 'B', + 'C', 'D', 'E', 'F'}; + uint16_t i; + for(i=0; i<(blocksize_bit+7)/8; ++i){ + uart_putc(tab[(block[i])>>4]); + uart_putc(tab[(block[i])&0xf]); + } +} + +#define SPACES 31 +#define BYTESPERLINE 16 + +static void printitem(char* name, uint8_t* buffer, uint16_t size_B){ + uint8_t name_len; + uint8_t i; + name_len=strlen(name); + if(name_len>SPACES-1){ + uart_putstr_P(PSTR("\r\n!!! formatting error !!!\r\n")); + return; + } + uart_putstr_P(PSTR("\r\n")); + for(i=0; iBYTESPERLINE)?BYTESPERLINE:toprint)*8); + buffer += BYTESPERLINE; + toprint -= BYTESPERLINE; + } + } +} + +static void print_set_vector(uint8_t set, uint16_t vector){ + uart_putstr_P(PSTR("\r\n\r\nSet ")); + uart_putc('0'+set%10); + uart_putstr_P(PSTR(", vector#")); + uart_putc((vector<100)?' ':'0'+vector/100); + uart_putc((vector<10 )?' ':'0'+(vector/10)%10); + uart_putc('0'+vector%10); + uart_putc(':'); +} + +/* example: +Test vectors -- set 3 +===================== + */ +static void print_setheader(uint8_t set){ + uart_putstr_P(PSTR("\r\n\r\nTest vectors -- set ")); + uart_putc('0'+set%10); + uart_putstr_P(PSTR("\r\n=====================")); +} + +/* example: +******************************************************************************** +*Project NESSIE - New European Schemes for Signature, Integrity, and Encryption* +******************************************************************************** + +Primitive Name: Serpent +======================= +Key size: 256 bits +Block size: 128 bits +*/ + +static void print_header(void){ + uint16_t i; + uart_putstr_P(PSTR("\r\n\r\n" + "********************************************************************************\r\n" + "* micro-cryt - crypto primitives for microcontrolles by Daniel Otte *\r\n" + "********************************************************************************\r\n" + "\r\n")); + uart_putstr_P(PSTR("Primitive Name: ")); + uart_putstr(nessie_hash_ctx.name); + uart_putstr_P(PSTR("\r\n")); + for(i=0; i<16+strlen(nessie_hash_ctx.name); ++i){ + uart_putc('='); + } + uart_putstr_P(PSTR("\r\nHash size: ")); + if(nessie_hash_ctx.hashsize_b >100){ + uart_putc('0'+nessie_hash_ctx.hashsize_b/100); + } + if(nessie_hash_ctx.hashsize_b>10){ + uart_putc('0'+(nessie_hash_ctx.hashsize_b/10)%10); + } + uart_putc('0'+nessie_hash_ctx.hashsize_b%10); + uart_putstr_P(PSTR(" bits\r\n")); +} + +static void print_footer(void){ + uart_putstr_P(PSTR("\r\n\r\n\r\n\r\nEnd of test vectors")); +} + +static +void ascii_hash(char* data, char* desc){ + uint8_t ctx[nessie_hash_ctx.ctx_size_B]; + uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8]; + uint16_t sl; + + uart_putstr_P(PSTR("\r\n message=")); + uart_putstr(desc); + nessie_hash_ctx.hash_init(ctx); + sl = strlen(data); + while(sl>=nessie_hash_ctx.blocksize_B){ + nessie_hash_ctx.hash_next(data, ctx); + data += nessie_hash_ctx.blocksize_B; + sl -= nessie_hash_ctx.blocksize_B; + } + nessie_hash_ctx.hash_last(data, sl*8, ctx); + nessie_hash_ctx.hash_conv(hash, ctx); + printitem("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8); +} + +// 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; + + uart_putstr_P(PSTR("\r\n message=")); + uart_putstr_P(PSTR("1 million times \"a\"")); + memset(block, 'a', nessie_hash_ctx.blocksize_B); + nessie_hash_ctx.hash_init(ctx); + while(n>=nessie_hash_ctx.blocksize_B){ + nessie_hash_ctx.hash_next(block, ctx); + n -= nessie_hash_ctx.blocksize_B; + } + nessie_hash_ctx.hash_last(block, n*8, ctx); + nessie_hash_ctx.hash_conv(hash, ctx); + printitem("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8); +} + + +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]; + + uart_putstr_P(PSTR("\r\n message=")); + if(n>=10000) + uart_putc('0'+n/10000); + if(n>=1000) + uart_putc('0'+(n/1000)%10); + if(n>=100) + uart_putc('0'+(n/100)%10); + if(n>=10) + uart_putc('0'+(n/10)%10); + uart_putc('0'+n%10); + uart_putstr_P(PSTR(" zero bits")); + + memset(block, 0, nessie_hash_ctx.blocksize_B); + nessie_hash_ctx.hash_init(ctx); + while(n>=nessie_hash_ctx.blocksize_B*8){ + nessie_hash_ctx.hash_next(block, ctx); + n -= nessie_hash_ctx.blocksize_B*8; + } + nessie_hash_ctx.hash_last(block, n, ctx); + nessie_hash_ctx.hash_conv(hash, ctx); + printitem("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8); +} + +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; + uart_putstr_P(PSTR("\r\n message=")); + uart_putstr_P(PSTR("512-bit string: ")); + if((pos/8) >=10){ + uart_putc('0'+(pos/8/10)%10); + } else { + uart_putc(' '); + } + uart_putc('0'+(pos/8)%10); + uart_putstr_P(PSTR("*00,")); + uart_putstr(tab[pos&7]); + uart_putc(','); + if(63-(pos/8) >=10){ + uart_putc('0'+((63-pos/8)/10)%10); + } else { + uart_putc(' '); + } + uart_putc('0'+(63-pos/8)%10); + uart_putstr_P(PSTR("*00")); + + /* now the real stuff */ + block[pos>>3] = 0x80>>(pos&0x7); + nessie_hash_ctx.hash_init(ctx); + while(n>=nessie_hash_ctx.blocksize_B*8){ + nessie_hash_ctx.hash_next(block, ctx); + n -= nessie_hash_ctx.blocksize_B*8; + } + nessie_hash_ctx.hash_last(block, n, ctx); + nessie_hash_ctx.hash_conv(hash, ctx); + printitem("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8); + + +} + +static +void tv4_hash(void){ + uint8_t ctx[nessie_hash_ctx.ctx_size_B]; + uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8]; + uint8_t block[256/8]; + uint16_t n=256; + uint32_t i; + + uart_putstr_P(PSTR("\r\n message=")); + uart_putstr(PSTR("256 zero bits")); + memset(block, 0, 256/8); + + nessie_hash_ctx.hash_init(ctx); + while(n>=nessie_hash_ctx.blocksize_B*8){ + nessie_hash_ctx.hash_next(block, ctx); + n -= nessie_hash_ctx.blocksize_B*8; + } + nessie_hash_ctx.hash_last(block, n*8, ctx); + nessie_hash_ctx.hash_conv(hash, ctx); + printitem("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8); + for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */ + nessie_hash_ctx.hash_init(ctx); + nessie_hash_ctx.hash_last(block, nessie_hash_ctx.hashsize_b, ctx); + nessie_hash_ctx.hash_conv(hash, ctx); + } + printitem("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8); +} + +/* + "" (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; + + print_header(); + /* test set 1 */ + char* challange[8][2]= { + {"", "\"\" (empty string)"}, + {"a", "\"a\""}, + {"abc", "\"abc\""}, + {"message digest", "\"message digest\""}, + {"abcdefghijklmnopqrstuvwxyz","\"abcdefghijklmnopqrstuvwxyz\""}, + {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\""}, + {"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" , "\"A...Za...z0...9\""}, + {"1234567890" "1234567890" "1234567890" "1234567890" + "1234567890" "1234567890" "1234567890" "1234567890", + "8 times \"1234567890\""} + }; + set=1; + print_setheader(set); + for(i=0; i<8; ++i){ + print_set_vector(set, i); + ascii_hash(challange[i][0], challange[i][1]); + } + print_set_vector(set, i); + amillion_hash(); + /* test set 2 */ + set=2; + print_setheader(set); + for(i=0; i<1024; ++i){ + print_set_vector(set, i); + zero_hash(i); + } + /* test set 3 */ + set=3; + print_setheader(set); + for(i=0; i<512; ++i){ + print_set_vector(set, i); + one_in512_hash(i); + } + /* test set 4 */ + set=4; + print_setheader(set); + print_set_vector(set, 0); + tv4_hash(); + + print_footer(); +} diff --git a/nessie_hash_test.h b/nessie_hash_test.h new file mode 100644 index 0000000..c5da494 --- /dev/null +++ b/nessie_hash_test.h @@ -0,0 +1,28 @@ +#ifndef NESSIE_HASH_TEST_H_ +#define NESSIE_HASH_TEST_H_ + +#include + +typedef void (*nessie_hash_init_fpt)(void* ctx); +typedef void (*nessie_hash_next_fpt)(void* buffer, void* ctx); +typedef void (*nessie_hash_last_fpt)(void* buffer, uint16_t size_b, void* ctx); +typedef void (*nessie_hash_conv_fpt)(void* buffer, void* ctx); + + +typedef struct nessie_hash_ctx_st{ + uint16_t hashsize_b; + uint16_t blocksize_B; + uint16_t ctx_size_B; + char* name; + nessie_hash_init_fpt hash_init; + nessie_hash_next_fpt hash_next; + nessie_hash_last_fpt hash_last; + nessie_hash_conv_fpt hash_conv; +} nessie_hash_ctx_t; + + +extern nessie_hash_ctx_t nessie_hash_ctx; + +void nessie_hash_run(void); + +#endif /*NESSIE_HASH_TEST_H_*/ diff --git a/sha256.mk b/sha256.mk index 8b757df..dae003c 100644 --- a/sha256.mk +++ b/sha256.mk @@ -5,8 +5,8 @@ ALGO_NAME := SHA256 HASHES += $(ALGO_NAME) $(ALGO_NAME)_OBJ := sha256-asm.o -$(ALGO_NAME)_TEST_BIN := main.o debug.o uart.o serial-tools.o sha256-asm.o \ - xtea-asm.o arcfour-asm.o prng.o cast5.o +$(ALGO_NAME)_TEST_BIN := main-sha256-test.o debug.o uart.o serial-tools.o \ + sha256-asm.o nessie_hash_test.o $(ALGO_NAME)_NESSIE_TEST := "nessie" $(ALGO_NAME)_PEROFRMANCE_TEST := "performance" diff --git a/xtea.mk b/xtea.mk index e2df69e..e7b0a1d 100644 --- a/xtea.mk +++ b/xtea.mk @@ -5,8 +5,8 @@ ALGO_NAME := XTEA BLOCK_CIPHERS += $(ALGO_NAME) $(ALGO_NAME)_OBJ := xtea-asm.o -$(ALGO_NAME)_TEST_BIN := main.o debug.o uart.o serial-tools.o sha256-asm.o \ - xtea-asm.o arcfour-asm.o prng.o cast5.o +$(ALGO_NAME)_TEST_BIN := main-xtea-test.o debug.o uart.o serial-tools.o \ + xtea-asm.o nessie_bc_test.o $(ALGO_NAME)_NESSIE_TEST := "nessie" $(ALGO_NAME)_PEROFRMANCE_TEST := "performance" -- 2.39.5