diff --git a/prng.c b/entropium.c similarity index 89% rename from prng.c rename to entropium.c index ad73bdd..db143cb 100644 --- a/prng.c +++ b/entropium.c @@ -1,5 +1,5 @@ /** - * \file prng.c + * \file entropium.c * \author Daniel Otte * \date 17.05.2006 * \par License: @@ -50,7 +50,7 @@ #include #include #include "sha256.h" -#include "prng.h" +#include "entropium.h" /** * \brief secret entropy pool. @@ -72,17 +72,17 @@ uint32_t rndCore[16]; * * we simply first "hash" rndCore, then entropy. */ -void addEntropy(unsigned length, void* data){ +void entropium_addEntropy(unsigned length_b, const void* data){ sha256_ctx_t s; static uint8_t offset=0; /* selects if higher or lower half gets updated */ sha256_init(&s); sha256_nextBlock(&s, rndCore); - while (length>=512){ + while (length_b>=512){ sha256_nextBlock(&s, data); data = (uint8_t*)data+ 512/8; - length -= 512; + length_b -= 512; } - sha256_lastBlock(&s, data, length); + sha256_lastBlock(&s, data, length_b); uint8_t i; for (i=0; i<8; ++i){ rndCore[i+offset] ^= s.h[i]; @@ -95,7 +95,7 @@ void addEntropy(unsigned length, void* data){ * \brief This function fills a given buffer with 32 random bytes * @param b Pointer to buffer wich is to fill */ -void getRandomBlock(uint32_t *b){ +void entropium_getRandomBlock(void *b){ sha256_ctx_t s; uint8_t offset=8; @@ -107,7 +107,7 @@ void getRandomBlock(uint32_t *b){ } offset ^= 8; /* hehe */ memcpy(b, s.h, 32); /* back up first hash in b */ - ((uint8_t*)b)[*b&31]++; /* the important increment step */ + ((uint8_t*)b)[*((uint8_t*)b)&31]++; /* the important increment step */ sha256_init(&s); sha256_lastBlock(&s, b, 256); memcpy(b, s.h, 32); @@ -119,12 +119,12 @@ void getRandomBlock(uint32_t *b){ * \brief This function simply returns a random byte * @return a random byte */ -uint8_t getRandomByte(void){ +uint8_t entropium_getRandomByte(void){ static uint8_t block[32]; static uint8_t i=32; if (i==32){ - getRandomBlock((void*)block); + entropium_getRandomBlock((void*)block); i=0; } return block[i++]; @@ -137,15 +137,15 @@ uint8_t getRandomByte(void){ * @return a random byte */ -void fillBlockRandom(void* block, unsigned length){ - while(length>RANDOMBLOCK_SIZE){ - getRandomBlock(block); - block = (uint8_t*)block + RANDOMBLOCK_SIZE; - length -= RANDOMBLOCK_SIZE; +void entropium_fillBlockRandom(void* block, unsigned length_B){ + while(length_B>ENTROPIUM_RANDOMBLOCK_SIZE){ + entropium_getRandomBlock(block); + block = (uint8_t*)block + ENTROPIUM_RANDOMBLOCK_SIZE; + length_B -= ENTROPIUM_RANDOMBLOCK_SIZE; } - while(length){ - *((uint8_t*)block) = getRandomByte(); - block= (uint8_t*)block +1; --length; + while(length_B){ + *((uint8_t*)block) = entropium_getRandomByte(); + block= (uint8_t*)block +1; --length_B; } } diff --git a/entropium.h b/entropium.h new file mode 100644 index 0000000..87d4334 --- /dev/null +++ b/entropium.h @@ -0,0 +1,25 @@ +/** + * File: entropium.h + * Author: Daniel Otte + * Date: 23.07.2006 + * License: GPL + * Description: This file contains the declarations for the pseudo-random-number generator. + **/ + +#ifndef ENTROPIUM_H_ +#define ENTROPIUM_H_ + +#include +/* + * length in bits + */ +#define ENTROPIUM_RANDOMBLOCK_SIZE 32 /* bytes */ + +void entropium_addEntropy(unsigned length_b, const void* data); +void entropium_getRandomBlock(void* b); +/* this does some simple buffering */ +uint8_t entropium_getRandomByte(void); + +void entropium_fillBlockRandom(void* block, unsigned length_B); + +#endif /*PRNG_H_*/ diff --git a/entropium.mk b/entropium.mk new file mode 100644 index 0000000..589ff6f --- /dev/null +++ b/entropium.mk @@ -0,0 +1,13 @@ +# Makefile for Entropium PRNG +ALGO_NAME := ENTROPIUM + +# comment out the following line for removement of PRNG from the build process +PRNGS += $(ALGO_NAME) + +$(ALGO_NAME)_OBJ := entropium.o sha256-asm.o +$(ALGO_NAME)_TEST_BIN := main-entropium-test.o debug.o uart.o serial-tools.o \ + sha256-asm.o entropium.o cli.o performance_test.o + +$(ALGO_NAME)_NESSIE_TEST := "nessie" +$(ALGO_NAME)_PEROFRMANCE_TEST := "performance" + diff --git a/main-entropium-test.c b/main-entropium-test.c new file mode 100644 index 0000000..82f0c29 --- /dev/null +++ b/main-entropium-test.c @@ -0,0 +1,108 @@ +/* + * entropium test-suit + * +*/ + +#include "config.h" +#include "serial-tools.h" +#include "uart.h" +#include "debug.h" + +#include "entropium.h" +#include "nessie_bc_test.h" +#include "cli.h" +#include "performance_test.h" + +#include +#include +#include + +char* cipher_name = "Entropium"; + +/***************************************************************************** + * additional validation-functions * + *****************************************************************************/ + +void testrun_entropium(void){ + char c, str[16]; + uint8_t data[32]; + uint32_t i=0; + while(!uart_getc_nb(&c)){ + entropium_getRandomBlock(data); + uart_putstr_P(PSTR("\r\n ")); + ultoa(i, str, 10); + for(c=strlen(str); c<11; ++c){ + uart_putc(' '); + } + uart_putstr(str); + ++i; + uart_putstr_P(PSTR(" : ")); + uart_hexdump(data, 32); + } + uart_putstr_P(PSTR("\r\n\r\n")); +} + + +void testrun_performance_entropium(void){ + uint16_t i,c; + uint64_t t; + char str[16]; + uint8_t data[32]; + + calibrateTimer(); + getOverhead(&c, &i); + uart_putstr_P(PSTR("\r\n\r\n=== benchmark ===")); + utoa(c, str, 10); + uart_putstr_P(PSTR("\r\n\tconst overhead: ")); + uart_putstr(str); + utoa(i, str, 10); + uart_putstr_P(PSTR("\r\n\tinterrupt overhead: ")); + uart_putstr(str); + + startTimer(1); + entropium_addEntropy(128, data); + t = stopTimer(); + uart_putstr_P(PSTR("\r\n\tadd entropy time: ")); + ultoa((unsigned long)t, str, 10); + uart_putstr(str); + + + startTimer(1); + entropium_getRandomBlock(data); + t = stopTimer(); + uart_putstr_P(PSTR("\r\n\tget random time: ")); + ultoa((unsigned long)t, str, 10); + uart_putstr(str); + + uart_putstr_P(PSTR("\r\n")); +} +/***************************************************************************** + * main * + *****************************************************************************/ + +typedef void(*void_fpt)(void); + +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")); + + PGM_P u = PSTR("nessie\0test\0performance\0"); + void_fpt v[] = {testrun_entropium, testrun_entropium, testrun_performance_entropium}; + + while(1){ + if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;} + if(execcommand_d0_P(str, u, v)<0){ + uart_putstr_P(PSTR("\r\nunknown command\r\n")); + } + continue; + error: + uart_putstr("ERROR\r\n"); + } + +} + diff --git a/main.c b/main.c deleted file mode 100644 index f747ffe..0000000 --- a/main.c +++ /dev/null @@ -1,284 +0,0 @@ -/* - * crypto-test - * -*/ - - -#include "config.h" -#include "serial-tools.h" -#include "uart.h" -#include "debug.h" - -#include "sha256.h" -#include "xtea.h" -#include "prng.h" -#include "cast5.h" - -#include -#include - - -/***************************************************************************** - * additional validation-functions * -*****************************************************************************/ - -void shavs_rnd(sha256_hash_t seed){ - uint8_t md[4][SHA256_HASH_BITS/8], buffer[3*SHA256_HASH_BITS/8]; - uint8_t j; - uint16_t i; - - for(j=0; j< 100; ++j){ - memcpy(md[0], seed, SHA256_HASH_BITS/8); - memcpy(md[1], seed, SHA256_HASH_BITS/8); - memcpy(md[2], seed, SHA256_HASH_BITS/8); - for(i=3; i<1003; ++i){ - memcpy(buffer+0*(SHA256_HASH_BITS/8), md[(i-3)%4], SHA256_HASH_BITS/8); - memcpy(buffer+1*(SHA256_HASH_BITS/8), md[(i-2)%4], SHA256_HASH_BITS/8); - memcpy(buffer+2*(SHA256_HASH_BITS/8), md[(i-1)%4], SHA256_HASH_BITS/8); - sha256(((void*)md[i%4]), buffer, 3*SHA256_HASH_BITS); - uart_putc('.'); - } - /* OUTPUT */ - --i; - uart_putstr("\r\nMD = "); - uart_hexdump(md[i%4], SHA256_HASH_BITS/8); - uart_putstr("\r\n"); - memcpy(seed, (md[i%4]), SHA256_HASH_BITS/8); - } -} - -/***************************************************************************** - * self tests * -*****************************************************************************/ -void testrun_sha256(void){ - uint8_t block[SHA256_BLOCK_BITS/8]; - - uart_putstr("\r\nsha256(\"\", 0)= "); - sha256((void*)block, (void*)"\x00", 0); - uart_hexdump(block, SHA256_HASH_BITS/8); - - - uart_putstr("\r\nsha256(0x80, 8)= "); - sha256((void*)block, (void*)"\x80", 8); - uart_hexdump(block, SHA256_HASH_BITS/8); - - uart_putstr("\r\nsha256(0x02, 8)= "); - sha256((void*)block, (void*)"\x02", 8); - uart_hexdump(block, SHA256_HASH_BITS/8); - - - uart_putstr("\r\nsha256(\"abc\", 24)= "); - sha256((void*)block, (void*)"abc", 24); - uart_hexdump(block, SHA256_HASH_BITS/8); - - uart_putstr("\r\nsha256(\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\", 24)= "); - sha256((void*)block, (void*) "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 448); - uart_hexdump(block, SHA256_HASH_BITS/8); - - uart_putstr("\r\nsha256(1,000,000 x 'a')= "); - { - uint16_t i; - sha256_ctx_t s; - sha256_init(&s); - memset(block,'a',SHA256_BLOCK_BITS/8); - for(i=0; i<(1000000/(SHA256_BLOCK_BITS/8)); ++i){ /* 15625 times*/ - sha256_nextBlock(&s, block); - } - sha256_lastBlock(&s, block, 0); - sha256_ctx2hash((void*)block, &s); - } - uart_hexdump(block, SHA256_HASH_BITS/8); -} - -void testrun_xtea(void){ - uint8_t block[8], block2[8]; - uint8_t key [16]; - - memcpy (block, "abcdefgh", 8); - memset (key, 0, 16); - memset (block2, 0, 8); - uart_putstr("\r\nxtea_enc(\"abcdefgh\", 0)= "); - xtea_enc((void*)block2, (void*)block, (void*)key); - uart_hexdump(block2, 8); - uart_putstr("\r\nxtea_dec(form above)= "); - xtea_dec((void*)block, (void*)block2, (void*)key); - uart_hexdump(block, 8); - - memcpy (key, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16); - uart_putstr("\r\nxtea_enc(\"abcdefgh\", 000102030405060708090a0b0c0d0e0f)= "); - xtea_enc((void*)block, (void*)block, (void*)key); - uart_hexdump(block, 8); - uart_putstr("\r\nxtea_dec(form above)= "); - xtea_dec((void*)block, (void*)block, (void*)key); - uart_hexdump(block, 8); -} - -#if 0 - -void testrun_arcfour(void){ - arcfour_ctx_t s; - char *b; - /* using wikipedia test-vectors: - * RC4( "Key", "Plaintext" ) == "bbf316e8 d940af0a d3" - * RC4( "Wiki", "pedia" ) == "1021bf0420" - * RC4( "Secret", "Attack at dawn" ) == "45a01f64 5fc35b38 3552544b 9bf5" - **/ - uart_putstr("\r\narcfour(\"Plaintext\", \"Key\")="); - arcfour_init(&s, (uint8_t*)"Key", 3); - b="Plaintext"; - while (*b) - *b++ ^= arcfour_gen(&s); - uart_hexdump(b-9, 9); - - uart_putstr("\r\narcfour(\"pedia\", \"Wiki\")="); - arcfour_init(&s, (uint8_t*)"Wiki", 4); - b="pedia"; - while (*b) - *b++ ^= arcfour_gen(&s); - uart_hexdump(b-5, 5); - - uart_putstr("\r\narcfour(\"Attack at dawn\", \"Secret\")="); - arcfour_init(&s, (uint8_t*)"Secret", 6); - b="Attack at dawn"; - while (*b) - *b++ ^= arcfour_gen(&s); - uart_hexdump(b-14, 14); - - uart_putstr("\r\narcfour(00.00.00.00.00.00.00.00, 01.23.45.67.89.AB.CD.EF)="); - arcfour_init(&s, (uint8_t*)"\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8); - int i=0; - uint8_t a[8]; - memset(a, 0 , 8); - while (i < 8) - a[i++] ^= arcfour_gen(&s); - uart_hexdump(a, 8); -} - -#endif - -void testrun_prng(void){ - uint8_t i,block[32]; - uart_putstr("\r\naddEntropy(32, 0x00000000)"); - addEntropy(32,"\x00\x00\x00\x00"); - for(i=0;i<12;++i){ - getRandomBlock((void*)block); - uart_putstr("\r\n"); - uart_hexdump(block, 32); - } -} - -void testrun_cast5(void){ - cast5_ctx_t s; - uint8_t i; - uart_putstr("\r\nCAST5:\r\nkey: 01 23 45 67 34 56 78 23 45 67 89 34 56 78 9A"); - cast5_init(&s, (uint8_t*)"\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9A", 128); - uint8_t block[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}; - uart_putstr("\r\nplaintext: "); - uart_hexdump(block, 8); - cast5_enc(&s, block); - uart_putstr("\r\nciphertext: "); - uart_hexdump(block, 8); - for(i=0; i<16; ++i){ - uart_putstr("\r\nK"); uart_putc('0'+(i+1)/10); uart_putc('0'+(i+1)%10); uart_putstr(": "); - uart_hexdump(&(s.mask[i]),4); - } -} - -/***************************************************************************** - * main * - *****************************************************************************/ - -int main (void){ - uint64_t length=0; - sha256_ctx_t s; - char str[20]; - int i; - uint8_t block[SHA256_BLOCK_BITS/8]; - - DEBUG_INIT(); - - sha256_init(&s); - uart_putstr("\r\n"); - - uart_putstr("\r\n\r\nCrypto-VS\r\nloaded and running\r\n"); -restart: - while(1){ - if (!getnextwordn(str,20)) {DEBUG_S("DBG: W1\r\n"); goto error;} - if (strcmp(str, "REQ")) {DEBUG_S("DBG: 1b\r\n"); goto error;} - if (!getnextwordn(str,20)) {DEBUG_S("DBG: W2\r\n"); goto error;} - if (strcmp(str, "SHA256")) { - if (strcmp(str, "test")){DEBUG_S("DBG: 1d\r\n"); goto error;}; - /* use some fixed test-vectors and all Algos */ - uart_putstr("\r\n intergrated selftests:\r\n"); - testrun_xtea(); - uart_putstr("\r\n"); - testrun_prng(); - uart_putstr("\r\n"); - testrun_cast5(); - uart_putstr("\r\n"); - // testrun_arcfour(); - // uart_putstr("\r\n"); - testrun_sha256(); - goto restart; - } - if (!getnextwordn(str,20)) {DEBUG_S("DBG: W4\r\n"); goto error;} - if (strcmp(str, "Len=")) { - /* 1d9370cdccba99b23670e2e0d6514001006f50d3c7a453201d2776f03c5e58fd */ - /* f41ece26 13e45739 15696b5a dcd51ca3 - 28be3bf5 66a9ca99 c9ceb027 9c1cb0a7 - */ - if(strcmp(str, "rnd")){DEBUG_S("DBG: 2b\r\n"); goto error;} - sha256_hash_t seed = {0x1d, 0x93, 0x70, 0xcd, 0xcc, 0xba, 0x99, 0xb2, 0x36, 0x70, - 0xe2, 0xe0, 0xd6, 0x51, 0x40, 0x01, 0x00, 0x6f, 0x50, 0xd3, - 0xc7, 0xa4, 0x53, 0x20, 0x1d, 0x27, 0x76, 0xf0, 0x3c, 0x5e, - 0x58, 0xfd }; /* - { 0xf4, 0x1e, 0xce, 0x26, 0x13, 0xe4, 0x57, 0x39, 0x15, 0x69, 0x6b, 0x5a, 0xdc, 0xd5, 0x1c, 0xa3, - 0x28, 0xbe, 0x3b, 0xf5, 0x66, 0xa9, 0xca, 0x99, 0xc9, 0xce, 0xb0, 0x27, 0x9c, 0x1c, 0xb0, 0xa7 }; - // */ - shavs_rnd(seed); - goto restart; - - } - if (!getnextwordn(str,20)) {DEBUG_S("DBG: W5\r\n"); goto error;} - { - length=0; - i=0; - while (str[i]){ /* we should check for error here */ - length *= 10; - length += str[i++] - '0'; - } - }; -// DEBUG_S("\r\nDBG: Length="); DEBUG_B(length&0xff); DEBUG_S("\r\n"); -// DEBUG_S("A"); - sha256_init(&s); -// DEBUG_S("B"); - if (!getnextwordn(str,20)) {DEBUG_S("DBG: W6\r\n"); goto error;} -// DEBUG_S("b2"); - if (strcmp(str, "Msg=")) {DEBUG_S("DBG: 4b\r\n"); goto error;} -// DEBUG_S("b3"); - { - memset(block, 0, SHA256_BLOCK_BITS/8); -// DEBUG_S("b3.0"); - while (length>=SHA256_BLOCK_BITS){ - readhex2buffer(block, SHA256_BLOCK_BITS/8); -// DEBUG_S("b3.1"); - sha256_nextBlock(&s, block); -// DEBUG_S("b3.2"); - length -= SHA256_BLOCK_BITS; - } -// DEBUG_S("C"); - readhex2buffer(block, (length/8) + ((length&0x7)?1:0) + ((length)?0:1)); -// DEBUG_S("D"); - sha256_lastBlock(&s, block, length); -// DEBUG_S("E"); - sha256_ctx2hash((void*)block, &s); - uart_putstr("\n\rMD= "); - uart_hexdump(block, SHA256_HASH_BITS/8); - uart_putstr("\n\r\n\r"); - } - continue; - error: - uart_putstr("ERROR\r\n"); - } /* while (1) */ -} - diff --git a/prng.h b/prng.h deleted file mode 100644 index b2d3135..0000000 --- a/prng.h +++ /dev/null @@ -1,25 +0,0 @@ -/** - * File: prng.h - * Author: Daniel Otte - * Date: 23.07.2006 - * License: GPL - * Description: This file contains the declarations for the pseudo-random-number generator. - **/ - -#ifndef PRNG_H_ -#define PRNG_H_ - -#include -/* - * length in bits - */ -#define RANDOMBLOCK_SIZE 32 /* bytes */ - -void addEntropy(unsigned length, void* data); -void getRandomBlock(uint32_t* b); -/* this does some simple buffering */ -uint8_t getRandomByte(void); - -void fillBlockRandom(void* block, unsigned length); - -#endif /*PRNG_H_*/ diff --git a/prng.mk b/prng.mk deleted file mode 100644 index 9114ce8..0000000 --- a/prng.mk +++ /dev/null @@ -1,13 +0,0 @@ -# Makefile for PRNG -ALGO_NAME := PRNG - -# comment out the following line for removement of PRNG from the build process -PRNGS += $(ALGO_NAME) - -$(ALGO_NAME)_OBJ := prng.o 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)_NESSIE_TEST := "nessie" -$(ALGO_NAME)_PEROFRMANCE_TEST := "performance" - diff --git a/sha256.c b/sha256.c index bf9103b..f2dea4b 100644 --- a/sha256.c +++ b/sha256.c @@ -93,7 +93,7 @@ uint32_t k[]={ /** * block must be, 512, Bit = 64, Byte, long !!! */ -void sha256_nextBlock (sha256_ctx_t *state, void* block){ +void sha256_nextBlock (sha256_ctx_t *state, const void* block){ uint32_t w[64]; /* this is 256, byte, large, */ uint8_t i; uint32_t a[8],t1,t2; @@ -140,7 +140,7 @@ void sha256_nextBlock (sha256_ctx_t *state, void* block){ * bits are big endian, meaning high bits come first. * if you have a message with bits at the end, the byte must be padded with zeros */ -void sha256_lastBlock(sha256_ctx_t *state, void* block, uint16_t length){ +void sha256_lastBlock(sha256_ctx_t *state, const void* block, uint16_t length){ uint8_t lb[SHA256_BLOCK_BITS/8]; /* local block */ state->length += length; memcpy (&(lb[0]), block, length/8); @@ -180,7 +180,7 @@ void sha256_lastBlock(sha256_ctx_t *state, void* block, uint16_t length){ /* * length in bits! */ -void sha256(sha256_hash_t *dest, void* msg, uint32_t length){ /* length could be choosen longer but this is for µC */ +void sha256(sha256_hash_t *dest, const void* msg, uint32_t length){ /* length could be choosen longer but this is for µC */ sha256_ctx_t s; sha256_init(&s); while(length >= SHA256_BLOCK_BITS){ @@ -196,7 +196,7 @@ void sha256(sha256_hash_t *dest, void* msg, uint32_t length){ /* length could be /*************************************************************************/ -void sha256_ctx2hash(sha256_hash_t *dest, sha256_ctx_t *state){ +void sha256_ctx2hash(sha256_hash_t *dest, const sha256_ctx_t *state){ #if defined LITTLE_ENDIAN uint8_t i; for(i=0; i<8; ++i){ diff --git a/sha256.h b/sha256.h index 5f2f275..82606af 100644 --- a/sha256.h +++ b/sha256.h @@ -32,15 +32,15 @@ typedef uint8_t sha256_hash_t[SHA256_HASH_BITS/8]; void sha256_init(sha256_ctx_t *state); -void sha256_nextBlock (sha256_ctx_t *state, void* block); -void sha256_lastBlock(sha256_ctx_t *state, void* block, uint16_t length); +void sha256_nextBlock (sha256_ctx_t *state, const void* block); +void sha256_lastBlock(sha256_ctx_t *state, const void* block, uint16_t length); -void sha256_ctx2hash(sha256_hash_t *dest, sha256_ctx_t *state); +void sha256_ctx2hash(sha256_hash_t *dest, const sha256_ctx_t *state); /* * length in bits! */ -void sha256(sha256_hash_t *dest, void* msg, uint32_t length); +void sha256(sha256_hash_t *dest, const void* msg, uint32_t length); uint32_t change_endian32(uint32_t x);