diff --git a/Makefile b/Makefile index 359e592..75876cf 100644 --- a/Makefile +++ b/Makefile @@ -80,6 +80,26 @@ info: # @echo " $(ALGORITHMS_TEST_BIN)" # @echo " ALGORITHMS_TEST_TARGET_ELF:" # @echo " $(ALGORITHMS_TEST_TARGET_ELF)" + @echo " targets:" + @echo " all - all algorithm cores" + @echo " cores - all algorithm cores" + @echo " listings - all algorithm core listings" + @echo " tests - all algorithm test programs" + @echo " stats - all algorithm size statistics" + @echo " blockciphers - all blockcipher cores" + @echo " streamciphers - all streamcipher cores" + @echo " hashes - all hash cores" + @echo " macs - all MAC cores" + @echo " prngs - all PRNG cores" + @echo " all_testrun - testrun all algorithms" + @echo " docu - build doxygen documentation" + @echo " clean - remove a lot of builded files" + @echo " xclean - also remove dependency files" + @echo " *_TEST_BIN - build test program" + @echo " *_TESTRUN - run nessie test" + @echo " *_OBJ - build algorithm core" + @echo " *_FLASH - flash test program" + @echo " *_LIST - build assembler listing" #------------------------------------------------------------------------------- @@ -161,12 +181,12 @@ $(foreach algo, $(ALGORITHMS),$(eval $(call FLASH_TEMPLATE, $(algo), $(TESTBIN_D define TESTRUN_TEMPLATE $(1)_TESTRUN: $(1)_FLASH @echo "[test]: $(1)" - $(RUBY) get_test.rb $(TESTPORT) $(TESTPORTBAUDR) 8 1 nessie $(TESTLOG_DIR)$(TESTPREFIX) $(2) + $(RUBY) $(GET_TEST) $(TESTPORT) $(TESTPORTBAUDR) 8 1 nessie $(TESTLOG_DIR)$(TESTPREFIX) $(2) endef $(foreach algo, $(ALGORITHMS),$(eval $(call TESTRUN_TEMPLATE, $(algo), $(call lc,$(algo)) ))) -ALL_TESTRUN: $(foreach algo, $(ALGORITHMS), $(algo)_TESTRUN) +all_testrun: $(foreach algo, $(ALGORITHMS), $(algo)_TESTRUN) #------------------------------------------------------------------------------- @@ -236,6 +256,20 @@ xclean: clean docu: doxygen +make.dump: Makefile + $(MAKE) -p -B -n -f $^ > $@ + +make.dot: make.dump + $(MAKE2GRAPH) $^ > $@ + +make.png: make.dot + $(TWOPI) -Tpng -o $@ $^ + +make.svg: make.dot + $(TWOPI) -Tsvg -o $@ $^ + +.PHONY: make-info +make-info: make.png make.svg # Rules for building the .text rom images diff --git a/avr-makefile.inc b/avr-makefile.inc index 08457a3..d40f6c7 100644 --- a/avr-makefile.inc +++ b/avr-makefile.inc @@ -11,7 +11,7 @@ TESTSRC_DIR = test_src/ #uisp -dprog=bsd -dlpt=/dev/parport1 --upload if=$(PRG).hex ERASECMD = TESTPORT = /dev/ttyUSB1 -TESTPORTBAUDR = 9600 +TESTPORTBAUDR = 38400 TESTLOG_DIR = testlog/ TESTPREFIX = nessie- LIST_DIR = listings/ @@ -29,3 +29,7 @@ OBJCOPY = avr-objcopy OBJDUMP = avr-objdump SIZE = avr-size RUBY = ruby +GET_TEST = host/get_test.rb +MAKE = make +MAKE2GRAPH = ~/bin/make2graph.rb +TWOPI = twopi diff --git a/cast5.c b/cast5.c index 0ed3ddf..a7984d4 100644 --- a/cast5.c +++ b/cast5.c @@ -108,7 +108,7 @@ void cast5_init_rM(uint8_t *klo, uint8_t *khi, uint8_t offset, uint8_t *src, boo -void cast5_init(void* key, uint8_t keylength_b, cast5_ctx_t* s){ +void cast5_init(const void* key, uint8_t keylength_b, cast5_ctx_t* s){ /* we migth return if the key is valid and if setup was sucessfull */ uint32_t x[4], z[4]; #define BPX ((uint8_t*)&(x[0])) @@ -198,8 +198,10 @@ uint32_t cast5_f1(uint32_t d, uint32_t m, uint8_t r){ #else - return (((pgm_read_dword(&s1[((uint8_t*)&t)[IA]] ) ^ pgm_read_dword(&s2[((uint8_t*)&t)[IB]] )) - - pgm_read_dword(&s3[((uint8_t*)&t)[IC]] )) + pgm_read_dword(&s4[((uint8_t*)&t)[ID]])); + return ((( pgm_read_dword(&s1[((uint8_t*)&t)[IA]]) + ^ pgm_read_dword(&s2[((uint8_t*)&t)[IB]]) ) + - pgm_read_dword(&s3[((uint8_t*)&t)[IC]]) ) + + pgm_read_dword(&s4[((uint8_t*)&t)[ID]]) ); #endif } @@ -227,8 +229,8 @@ uint32_t cast5_f2(uint32_t d, uint32_t m, uint8_t r){ return (((ia - ib) + ic) ^ id); #else - return (((pgm_read_dword(&s1[((uint8_t*)&t)[IA]]) - - pgm_read_dword(&s2[((uint8_t*)&t)[IB]]) ) + return ((( pgm_read_dword(&s1[((uint8_t*)&t)[IA]]) + - pgm_read_dword(&s2[((uint8_t*)&t)[IB]]) ) + pgm_read_dword(&s3[((uint8_t*)&t)[IC]]) ) ^ pgm_read_dword(&s4[((uint8_t*)&t)[ID]]) ); @@ -257,8 +259,10 @@ uint32_t cast5_f3(uint32_t d, uint32_t m, uint8_t r){ uart_putstr("\r\n\tID="); uart_hexdump(&id, 4); return (((ia + ib) ^ ic) - id); #else - return ((pgm_read_dword(&s1[((uint8_t*)&t)[IA]] ) + pgm_read_dword(&s2[((uint8_t*)&t)[IB]] )) - ^ pgm_read_dword(&s3[((uint8_t*)&t)[IC]] )) - pgm_read_dword(&s4[((uint8_t*)&t)[ID]] ); + return (( pgm_read_dword(&s1[((uint8_t*)&t)[IA]] ) + + pgm_read_dword(&s2[((uint8_t*)&t)[IB]] )) + ^ pgm_read_dword(&s3[((uint8_t*)&t)[IC]] )) + - pgm_read_dword(&s4[((uint8_t*)&t)[ID]] ); #endif } diff --git a/cast5.h b/cast5.h index aa81ca1..2b1d317 100644 --- a/cast5.h +++ b/cast5.h @@ -55,7 +55,7 @@ * * A variable of this type may hold a keyschedule for the CAST-5 cipher. * This context is regulary generated by the - * cast5_init(uint8_t* key, uint8_t keylength_b, cast5_ctx_t* s) funtion. + * cast5_init(uint8_t* key, uint8_t keylength_b, cast5_ctx_t* s) function. */ typedef struct cast5_ctx_st{ uint32_t mask[16]; @@ -74,7 +74,7 @@ typedef struct cast5_ctx_st{ * \param keylength_b length of the key in bits (maximum 128 bits) * \param s pointer to the context */ -void cast5_init(void* key, uint8_t keylength_b, cast5_ctx_t* s); +void cast5_init(const void* key, uint8_t keylength_b, cast5_ctx_t* s); /** \fn void cast5_enc(void* block, const cast5_ctx_t *s); * \brief encrypt a block with the CAST-5 algorithm diff --git a/config.h b/config.h index 529ff51..0a0caf1 100644 --- a/config.h +++ b/config.h @@ -30,7 +30,7 @@ /* uart.[ch] defines */ #define UART_INTERRUPT 1 -#define UART_BAUD_RATE 9600 +#define UART_BAUD_RATE 38400 #define UART_RXBUFSIZE 16 #define UART_TXBUFSIZE 16 #define UART_LINE_BUFFER_SIZE 40 diff --git a/mkfiles/omac_noekeon.mk b/mkfiles/omac_noekeon.mk new file mode 100644 index 0000000..41c5dff --- /dev/null +++ b/mkfiles/omac_noekeon.mk @@ -0,0 +1,13 @@ +# Makefile for noekeon +ALGO_NAME := OMAC_NOEKEON + +# comment out the following line for removement of noekeon from the build process +MACS += $(ALGO_NAME) + + +$(ALGO_NAME)_OBJ := noekeon_asm.o omac_noekeon.o memxor.o +$(ALGO_NAME)_TEST_BIN := main-omac-noekeon-test.o debug.o uart.o serial-tools.o \ + nessie_mac_test.o nessie_common.o cli.o performance_test.o +$(ALGO_NAME)_NESSIE_TEST := test nessie +$(ALGO_NAME)_PERFORMANCE_TEST := performance + diff --git a/mkfiles/omac_noekeon_c.mk b/mkfiles/omac_noekeon_c.mk new file mode 100644 index 0000000..d142283 --- /dev/null +++ b/mkfiles/omac_noekeon_c.mk @@ -0,0 +1,13 @@ +# Makefile for noekeon +ALGO_NAME := OMAC_NOEKEON_C + +# comment out the following line for removement of noekeon from the build process +MACS += $(ALGO_NAME) + + +$(ALGO_NAME)_OBJ := noekeon_asm.o omac_noekeon_C.o memxor.o +$(ALGO_NAME)_TEST_BIN := main-omac-noekeon-test.o debug.o uart.o serial-tools.o \ + nessie_mac_test.o nessie_common.o cli.o performance_test.o +$(ALGO_NAME)_NESSIE_TEST := test nessie +$(ALGO_NAME)_PERFORMANCE_TEST := performance + diff --git a/mkfiles/seed_C.mk b/mkfiles/seed_C.mk new file mode 100644 index 0000000..3005819 --- /dev/null +++ b/mkfiles/seed_C.mk @@ -0,0 +1,13 @@ +# Makefile for SEED +ALGO_NAME := SEED_C + +# comment out the following line for removement of SEED from the build process +BLOCK_CIPHERS += $(ALGO_NAME) + +$(ALGO_NAME)_OBJ := seed_C.o +$(ALGO_NAME)_TEST_BIN := main-seed-test.o debug.o uart.o serial-tools.o \ + nessie_bc_test.o nessie_common.o \ + cli.o performance_test.o +$(ALGO_NAME)_NESSIE_TEST := "nessie" +$(ALGO_NAME)_PERFORMANCE_TEST := "performance" + diff --git a/omac_noekeon_C.c b/omac_noekeon_C.c new file mode 100644 index 0000000..3046ede --- /dev/null +++ b/omac_noekeon_C.c @@ -0,0 +1,74 @@ +#include "noekeon.h" +#include "omac_noekeon.h" +#include "memxor.h" +#include +#include + + +void omac_noekeon_init(omac_noekeon_ctx_t* ctx){ + memset(ctx, 0, 16); +} + + +void omac_noekeon_tweak(uint8_t t, const void* key, omac_noekeon_ctx_t* ctx){ + *ctx[15] = t; + noekeon_enc(ctx, key); +} + +void omac_noekeon_next(const void* buffer, const void* key, omac_noekeon_ctx_t* ctx){ + memxor(ctx, buffer, 16); + noekeon_enc(ctx, key); +} + +static +void omac_noekeon_comppad(uint8_t* pad, const void* key, uint8_t length_b){ + uint8_t c1,c2,r,j; + memset(pad, 0, 16); + noekeon_enc(pad, key); + r=(length_b==128)?1:2; + for(;r!=0;--r){ + c1=0; + for(j=0;j<16;++j){ + c2 = c1; + c1 = (pad[15-j])>>7; + pad[15-j] = ((pad[15-j])<<1) | c2; + } + if(c1){ + pad[15] ^= 0x87; + } + } + if(length_b<128){ + pad[(length_b)/8] ^= 0x80 >> (length_b%8); + } +} + +void omac_noekeon_last(const void* buffer, uint8_t length_b, const void* key, omac_noekeon_ctx_t* ctx){ + while(length_b>128){ + omac_noekeon_next(buffer, key, ctx); + buffer = (uint8_t*)buffer +16; + length_b -= 128; + } + uint8_t pad[16]; + omac_noekeon_comppad(pad, key, length_b); + memxor(pad, buffer, (length_b+7)/8); + omac_noekeon_next(pad, key, ctx); +} + + +void omac_noekeon(void* dest, const void* msg, uint16_t msglength_b, + const void* key, uint8_t t){ + omac_noekeon_init(dest); + if(t!=0xff) + omac_noekeon_tweak(t,key,dest); + while(msglength_b>128){ + omac_noekeon_next(msg, key, dest); + msg = (uint8_t*)msg +16; + msglength_b -= 128; + } + omac_noekeon_last(msg, msglength_b, key, dest); +} + + + + + diff --git a/seed.h b/seed.h index 4ee1d8b..7283191 100644 --- a/seed.h +++ b/seed.h @@ -29,15 +29,53 @@ #define SEED_H_ #include - +/** \typedef seed_ctx_t + * \brief SEED context + * + * A variable of this type may hold the key material for the SEED cipher. + * This context is regulary generated by the + * void seed_init(const void * key, seed_ctx_t * ctx) function. + */ typedef struct{ uint32_t k[4]; } seed_ctx_t; /******************************************************************************/ -void seed_init(uint8_t * key, seed_ctx_t * ctx); -void seed_enc(void * buffer, seed_ctx_t * ctx); -void seed_dec(void * buffer, seed_ctx_t * ctx); +/** \fn void seed_init(void * key, seed_ctx_t * ctx) + * \brief initializes context for SEED operation + * + * This function copys the key material into a context variable. + * + * \param key pointer to the key material (128 bit = 16 bytes) + * \param ctx pointer to the context (seed_ctx_t) + */ +void seed_init(const void * key, seed_ctx_t * ctx); + +/** \fn void seed_enc(void * buffer, seed_ctx_t * ctx) + * \brief encrypt a block with SEED + * + * This function encrypts a block of 64 bits (8 bytes) with the SEED algorithm. + * The round keys are computed on demand, so the context is modifyed while + * encrypting but the original stated is restored when the function exits. + * + * \param buffer pointer to the block (64 bit = 8 byte) which will be encrypted + * \param ctx pointer to the key material (seed_ctx_t) + */ +void seed_enc(void * buffer, const seed_ctx_t * ctx); + + +/** \fn void seed_dec(void * buffer, seed_ctx_t * ctx) + * \brief decrypt a block with SEED + * + * This function decrypts a block of 64 bits (8 bytes) with the SEED algorithm. + * The round keys are computed on demand, so the context is modifyed while + * decrypting but the original stated is restored when the function exits. + * + * \param buffer pointer to the block (64 bit = 8 byte) which will be decrypted + * \param ctx pointer to the key material (seed_ctx_t) + */ +void seed_dec(void * buffer, const seed_ctx_t * ctx); + #endif /*SEED_H_*/ diff --git a/seed_C.c b/seed_C.c index e4b77f5..5cda64d 100644 --- a/seed_C.c +++ b/seed_C.c @@ -206,7 +206,7 @@ typedef struct{ /******************************************************************************/ -void seed_init(uint8_t * key, seed_ctx_t * ctx){ +void seed_init(const void * key, seed_ctx_t * ctx){ memcpy(ctx->k, key, 128/8); } @@ -215,11 +215,11 @@ void seed_init(uint8_t * key, seed_ctx_t * ctx){ #define L (((uint64_t*)buffer)[0]) #define R (((uint64_t*)buffer)[1]) -void seed_enc(void * buffer, seed_ctx_t * ctx){ +void seed_enc(void * buffer, const seed_ctx_t * ctx){ uint8_t r; keypair_t k; for(r=0; r<8; ++r){ - k = getnextkeys(ctx->k, 2*r); + k = getnextkeys(((seed_ctx_t*)ctx)->k, 2*r); /* DEBUG_S("\r\n\tDBG ka,0: "); uart_hexdump(&k.k0, 4); DEBUG_S("\r\n\tDBG ka,1: "); uart_hexdump(&k.k1, 4); @@ -228,7 +228,7 @@ void seed_enc(void * buffer, seed_ctx_t * ctx){ */ L ^= f_function(&R,k.k0,k.k1); - k = getnextkeys(ctx->k, 2*r+1); + k = getnextkeys(((seed_ctx_t*)ctx)->k, 2*r+1); /* DEBUG_S("\r\n\tDBG kb,0: "); uart_hexdump(&k.k0, 4); DEBUG_S("\r\n\tDBG kb,1: "); uart_hexdump(&k.k1, 4); @@ -252,7 +252,7 @@ void seed_dec(void * buffer, seed_ctx_t * ctx){ int8_t r; keypair_t k; for(r=7; r>=0; --r){ - k = getprevkeys(ctx->k, 2*r+1); + k = getprevkeys(((seed_ctx_t*)ctx)->k, 2*r+1); /* DEBUG_S("\r\n\tDBG ka,0: "); uart_hexdump(&k.k0, 4); DEBUG_S("\r\n\tDBG ka,1: "); uart_hexdump(&k.k1, 4); @@ -261,7 +261,7 @@ void seed_dec(void * buffer, seed_ctx_t * ctx){ */ L ^= f_function(&R,k.k0,k.k1); - k = getprevkeys(ctx->k, 2*r+0); + k = getprevkeys(((seed_ctx_t*)ctx)->k, 2*r+0); /* DEBUG_S("\r\n\tDBG kb,0: "); uart_hexdump(&k.k0, 4); DEBUG_S("\r\n\tDBG kb,1: "); uart_hexdump(&k.k1, 4);