From b567660a247f4d4a15de45db334add3581a7524d Mon Sep 17 00:00:00 2001 From: bg Date: Tue, 8 Apr 2008 06:41:15 +0000 Subject: [PATCH] +Present +some fixes at nessie_common --- cli.c | 55 +++++++++++++++++++++++ cli.h | 11 +++++ main-present-test.c | 105 ++++++++++++++++++++++++++++++++++++++++++++ main-serpent-test.c | 15 ++++--- nessie_common.c | 2 +- present.c | 105 ++++++++++++++++++++++++++++++++++++++++++++ present.h | 16 +++++++ present.mk | 13 ++++++ serpent.mk | 2 +- 9 files changed, 317 insertions(+), 7 deletions(-) create mode 100644 cli.c create mode 100644 cli.h create mode 100644 main-present-test.c create mode 100644 present.c create mode 100644 present.h create mode 100644 present.mk diff --git a/cli.c b/cli.c new file mode 100644 index 0000000..15e5448 --- /dev/null +++ b/cli.c @@ -0,0 +1,55 @@ +/** + * + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + * + * components to help implementing simple command based interaction + * + **/ + +#include +#include +#include + +int16_t findstring_d0(const char* str, const char* v){ + uint8_t i=0; + while(*v){ + if(!strcmp(str, v)){ + return i; + } + while(*v++) /* go to the next string */ + ; + ++i; + } + return -1; +} + +int16_t findstring_d0_P(const char* str, PGM_P v){ + uint8_t i=0; + while(pgm_read_byte(v)){ + if(!strcmp_P(str, v)){ + return i; + } + while(pgm_read_byte(v++)) /* go to the next string */ + ; + ++i; + } + return -1; +} + +int16_t execcommand_d0_P(const char* str, PGM_P v, void(*fpt[])(void) ){ + uint8_t i=0; + while(pgm_read_byte(v)){ + if(!strcmp_P(str, v)){ + (fpt[i])(); + return i; + } + while(pgm_read_byte(v++)) /* go to the next string */ + ; + ++i; + } + return -1; +} + + diff --git a/cli.h b/cli.h new file mode 100644 index 0000000..da146b8 --- /dev/null +++ b/cli.h @@ -0,0 +1,11 @@ +#ifndef CLI_H_ +#define CLI_H_ + +#include +#include + +int16_t findstring_d0(const char* str, const char* v); +int16_t findstring_d0_P(const char* str, PGM_P v); + +int16_t execcommand_d0_P(const char* str, PGM_P v, void(*fpt[])(void) ); +#endif /*CLI_H_*/ diff --git a/main-present-test.c b/main-present-test.c new file mode 100644 index 0000000..bcb0e54 --- /dev/null +++ b/main-present-test.c @@ -0,0 +1,105 @@ +/* + * present test-suit + * +*/ + +#include "config.h" +#include "serial-tools.h" +#include "uart.h" +#include "debug.h" + +#include "present.h" +#include "nessie_bc_test.h" +#include "cli.h" + +#include +#include + +char* cipher_name = "Present"; + +/***************************************************************************** + * additional validation-functions * + *****************************************************************************/ +void present_genctx_dummy(uint8_t* key, uint16_t keysize_b, present_ctx_t* ctx){ + present_init(key, keysize_b, ctx); +} + +void testrun_nessie_present(void){ + nessie_bc_ctx.blocksize_B = 8; + nessie_bc_ctx.keysize_b = 80; + nessie_bc_ctx.name = cipher_name; + nessie_bc_ctx.ctx_size_B = sizeof(present_ctx_t); + nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)present_enc; + nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)present_dec; + nessie_bc_ctx.cipher_genctx = (nessie_bc_gen_fpt)present_genctx_dummy; + + nessie_bc_run(); +} + +void testrun_selfenc(uint8_t* key, uint8_t* buffer){ + present_ctx_t ctx; + uart_putstr_P(PSTR("\r\nkey : ")); + uart_hexdump(key, 10); + uart_putstr_P(PSTR("\r\nplain : ")); + uart_hexdump(buffer, 8); + present_init(key, 80, &ctx); + present_enc(buffer, &ctx); + uart_putstr_P(PSTR("\r\ncipher: ")); + uart_hexdump(buffer, 8); + present_dec(buffer, &ctx); + uart_putstr_P(PSTR("\r\nplain : ")); + uart_hexdump(buffer, 8); + uart_putstr_P(PSTR("\r\n")); +} + +void testrun_self_present(void){ + uint8_t buffer[8], key[10]; + uart_putstr_P(PSTR("\r\n\r\n=== Testvectors from the paper ===\r\n")); + + memset(buffer, 0, 8); + memset(key, 0, 10); + testrun_selfenc(key, buffer); + + memset(buffer, 0, 8); + memset(key, 0xFF, 10); + testrun_selfenc(key, buffer); + + memset(buffer, 0xFF, 8); + memset(key, 0, 10); + testrun_selfenc(key, buffer); + + memset(buffer, 0xFF, 8); + memset(key, 0xFF, 10); + testrun_selfenc(key, buffer); + +} + +/***************************************************************************** + * 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\0"); + void_fpt v[] = {testrun_nessie_present, testrun_self_present}; + + 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-serpent-test.c b/main-serpent-test.c index 7f3d9ac..c05ef17 100644 --- a/main-serpent-test.c +++ b/main-serpent-test.c @@ -10,6 +10,7 @@ #include "serpent.h" #include "nessie_bc_test.h" +#include "cli.h" #include #include @@ -47,6 +48,8 @@ void testrun_nessie_serpent(void){ * main * *****************************************************************************/ +typedef void(*void_fpt)(void); + int main (void){ char str[20]; DEBUG_INIT(); @@ -56,12 +59,14 @@ int main (void){ uart_putstr(cipher_name); uart_putstr_P(PSTR(")\r\nloaded and running\r\n")); -restart: + PGM_P u = PSTR("nessie\0test\0"); + void_fpt v[] = {testrun_nessie_serpent, testrun_nessie_serpent}; + 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_serpent(); - goto restart; + 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/nessie_common.c b/nessie_common.c index 2f663a6..7adac82 100644 --- a/nessie_common.c +++ b/nessie_common.c @@ -146,7 +146,7 @@ void nessie_print_header(char* name, uart_putstr(str); uart_putstr_P(PSTR(" bits")); } - uart_putstr_P(PSTR(" bits")); + uart_putstr_P(PSTR("\r\n")); } void nessie_print_footer(void){ diff --git a/present.c b/present.c new file mode 100644 index 0000000..8e6dc78 --- /dev/null +++ b/present.c @@ -0,0 +1,105 @@ +/** + * present.c + * a implementation of the PRESENT block-cipher + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + * + * */ + +#include +#include +#include "present.h" + +static uint8_t sbox(uint8_t b){ + uint8_t sb[]={0xC, 0x5, 0x6, 0xB, + 0x9, 0x0, 0xA, 0xD, + 0x3, 0xE, 0xF, 0x8, + 0x4, 0x7, 0x1, 0x2 }; + return (((sb[b>>4])<<4)|(sb[b&0xf])); +} + +static uint8_t sbox_inv(uint8_t b){ + uint8_t sb[]={0x5, 0xE, 0xF, 0x8, + 0xC, 0x1, 0x2, 0xD, + 0xB, 0x4, 0x6, 0x3, + 0x0, 0x7, 0x9, 0xA }; + return (((sb[b>>4])<<4)|(sb[b&0xf])); +} + +#define SHR_O(a) c=(a)&1; (a)>>=1; +#define SHR_I(a) (a)=(c?0x8000:0x0000) | ((a)>>1); + +static void p(uint16_t* o, uint8_t* i){ + uint8_t c; + uint8_t m,n; + for(m=0; m<8; ++m){ + for(n=0; n<2; ++n){ + SHR_O(i[m]); + SHR_I(o[0]); + SHR_O(i[m]); + SHR_I(o[1]); + SHR_O(i[m]); + SHR_I(o[2]); + SHR_O(i[m]); + SHR_I(o[3]); + } + } +} + +static void p_inv(uint8_t* o, uint8_t* i){ + uint8_t tmp[8]; + p((uint16_t*)tmp, i); + p((uint16_t*)o, tmp); +} + +void present_init(const uint8_t* key, uint8_t keysize_b, present_ctx_t* ctx){ + uint8_t buffer[10], tmp[2]; + uint8_t i; + memcpy(buffer, key, 10); + memcpy(&(ctx->k[0]), buffer+2, 8); + for(i=1; i<32; ++i){ + /* rotate buffer 19 right */ + memcpy(tmp, buffer, 2); + memmove(buffer, buffer+2, 8); + memcpy(buffer+8, tmp, 2); + /* three shifts to do*/ + tmp[1]=buffer[0]; + *((uint64_t*)buffer)>>=3; + *((uint16_t*)(buffer+8))>>=3; + buffer[9] |= tmp[1]<<5; + buffer[7] |= tmp[0]<<5; + /* rotating done now substitution */ + buffer[9] = (sbox(buffer[9])&0xF0) | ((buffer[9])&0x0F); + /* xor with round counter */ + *((uint16_t*)(buffer+1)) ^= (uint16_t)i<<7; + memcpy(&(ctx->k[i]), buffer+2, 8); + } +} + +void present_enc(void* buffer, present_ctx_t* ctx){ + uint8_t i,j,tmp[8]; + for(i=0; i<31; ++i){ + *((uint64_t*)buffer) ^= ctx->k[i]; + for(j=0; j<8; ++j){ + tmp[j] = sbox(((uint8_t*)buffer)[j]); + } + p((uint16_t*)buffer, tmp); + } + *((uint64_t*)buffer) ^= ctx->k[31]; +} + + +void present_dec(void* buffer, present_ctx_t* ctx){ + uint8_t j,tmp[8]; + int8_t i; + *((uint64_t*)buffer) ^= ctx->k[31]; + + for(i=30; i>=0; --i){ + p_inv(tmp, (uint8_t*)buffer); + for(j=0; j<8; ++j){ + ((uint8_t*)buffer)[j] = sbox_inv(tmp[j]); + } + *((uint64_t*)buffer) ^= ctx->k[i]; + } +} diff --git a/present.h b/present.h new file mode 100644 index 0000000..d557800 --- /dev/null +++ b/present.h @@ -0,0 +1,16 @@ +#ifndef PRESENT_H_ +#define PRESENT_H_ + +#include + +typedef struct present_ctx_st{ + uint64_t k[32]; +} present_ctx_t; + + +void present_init(const uint8_t* key, uint8_t keysize_b, present_ctx_t* ctx); +void present_enc(void* buffer, present_ctx_t* ctx); +void present_dec(void* buffer, present_ctx_t* ctx); + + +#endif /*PRESENT_H_*/ diff --git a/present.mk b/present.mk new file mode 100644 index 0000000..eba6e78 --- /dev/null +++ b/present.mk @@ -0,0 +1,13 @@ +# Makefile for present +ALGO_NAME := PRESENT + +# comment out the following line for removement of present from the build process +BLOCK_CIPHERS += $(ALGO_NAME) + + +$(ALGO_NAME)_OBJ := present.o +$(ALGO_NAME)_TEST_BIN := main-present-test.o debug.o uart.o serial-tools.o \ + present.o nessie_bc_test.o nessie_common.o cli.o +$(ALGO_NAME)_NESSIE_TEST := "nessie" +$(ALGO_NAME)_PEROFRMANCE_TEST := "performance" + diff --git a/serpent.mk b/serpent.mk index ff6bd46..0ff61a6 100644 --- a/serpent.mk +++ b/serpent.mk @@ -8,7 +8,7 @@ BLOCK_CIPHERS += $(ALGO_NAME) $(ALGO_NAME)_OBJ := serpent.o serpent-sboxes-bitslice.o $(ALGO_NAME)_TEST_BIN := main-serpent-test.o debug.o uart.o serial-tools.o \ serpent.o serpent-sboxes-bitslice.o nessie_bc_test.o \ - nessie_common.o + nessie_common.o cli.o $(ALGO_NAME)_NESSIE_TEST := "nessie" $(ALGO_NAME)_PEROFRMANCE_TEST := "performance" -- 2.39.5