From 2b3e134485c42a28f4d8d981c8cb62dc27e71aa5 Mon Sep 17 00:00:00 2001 From: bg Date: Wed, 16 Apr 2008 01:02:42 +0000 Subject: [PATCH] +Grain +corrected orthograpic some errors --- des.h | 2 +- grain.c | 121 +++++++++++++++++++++++++++++++++ grain.h | 24 +++++++ grain.mk | 13 ++++ grain_h_lutgen.c | 60 ++++++++++++++++ main-grain-test.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++ nessie_common.c | 2 +- shabea.c | 2 +- shabea.h | 2 +- 9 files changed, 391 insertions(+), 4 deletions(-) create mode 100644 grain.c create mode 100644 grain.h create mode 100644 grain.mk create mode 100644 grain_h_lutgen.c create mode 100644 main-grain-test.c diff --git a/des.h b/des.h index 45b29b5..241a4b5 100644 --- a/des.h +++ b/des.h @@ -11,7 +11,7 @@ #define DES_H_ #include -/* the FIPS 46-3 (1999-10-25) name for triple DES is triple data encrytion algorithm so TDEA. +/* the FIPS 46-3 (1999-10-25) name for triple DES is triple data encryption algorithm so TDEA. * Also we only implement the three key mode */ #define tdea_encrypt tdes_encrypt #define tdea_decrypt tdes_decrypt diff --git a/grain.c b/grain.c new file mode 100644 index 0000000..b4bfb2d --- /dev/null +++ b/grain.c @@ -0,0 +1,121 @@ +/** + * + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + * + */ + + +#include +#include +#include +#include "grain.h" + + +#define GRAIN_REVERSEKEY + +/* s0, s1, s2, ..., s78, s79 */ +#define S(i) ((ctx->lfsr[9-((i)/8)])>>(7-((i)%8))) +/* b0, b1, b2, ..., b78, b79 */ +#define B(i) ((ctx->nfsr[9-((i)/8)])>>(7-((i)%8))) + + +uint8_t h_lut[4] PROGMEM = {0x4C, 0xB6, 0xD3, 0x26}; + +uint8_t grain_enc(grain_ctx_t* ctx){ + uint8_t s80, s0, c1, c2; + uint8_t i; + /* clock the LFSR */ + s0=S(0); + s80 =S(62) ^ S(51) ^ S(38) ^ S(23) ^ S(13) ^ s0; + s80 &= 1; + c1 = s80; + for(i=0; i<10; ++i){ + c2 = (ctx->lfsr[i])>>7; + ctx->lfsr[i] = ((ctx->lfsr[i])<<1) | c1; + c1 = c2; + } + /* clock the NFSR */ + uint8_t b80, a,b,d,e; + b80 = B(62) ^ B(60) ^ B(52) ^ B(45) ^ + B(37) ^ B(33) ^ B(28) ^ B(21) ^ + B(14) ^ B( 9) ^ B( 0) ^ s0; + b80 ^= (a = B(63) & B(60)); + b80 ^= (b = B(37) & B(33)); + b80 ^= B(15) & B( 9); /* c */ + b80 ^= (d = B(60) & B(52) & B(45)); + b80 ^= (e = B(33) & B(28) & B(21)); + b80 ^= B(63) & B(45) & B(28) & B(9); /* f */ + /* -- */ + b80 ^= b & B(60) & B(52); /* g */ + b80 ^= a & B(21) & B(15); /* h */ + b80 ^= d & B(63) & B(37); /* i */ + b80 ^= e & B(15) & B( 9); /* j */ + b80 ^= e & B(52) & B(45) & B(37); /* k */ + c1 = b80 & 1; + for(i=0; i<10; ++i){ + c2 = (ctx->nfsr[i])>>7; + ctx->nfsr[i] = ((ctx->nfsr[i])<<1) | c1; + c1 = c2; + } + /* now the h function */ + uint8_t h; + i = (S(2)&1) | + ((S(24)&1) << 1) | + ((S(45)&1) << 2) | + ((S(63)&1) << 3) | + ((B(62)&1) << 4); + + h = (pgm_read_byte(h_lut+(i/8)))>>(i%8); + + h ^= B(0) ^ B(1) ^ B(3) ^ B(9) ^ B(30) ^ B(42) ^ B(55); + return h&1; +} + +#ifdef GRAIN_REVERSEKEY + +static +uint8_t reverse_bits(uint8_t a){ + uint8_t lut[16] = { + 0x0, 0x8, 0x4, 0xC, /* 0000 1000 0100 1100 */ + 0x2, 0xA, 0x6, 0xE, /* 0010 1010 0110 1110 */ + 0x1, 0x9, 0x5, 0xD, /* 0001 1001 0101 1101 */ + 0x3, 0xB, 0x7, 0xF }; /* 0011 1011 0111 1111 */ + uint8_t x; + x = ((lut[a&0xf]) << 4) | lut[a>>4]; + return x; +} +#else + +#define reverse_bits(a) (a) + +#endif + +void grain_init(const void* key, const void* iv, grain_ctx_t* ctx){ + uint8_t i,t; + + /* load the 80bit key */ + for(i=0; i<10; ++i){ + ctx->nfsr[9-i] = reverse_bits(((uint8_t*)key)[i]); + } + /* load the 64bit iv */ + for(i=0; i<8; ++i){ + ctx->lfsr[9-i] = reverse_bits(((uint8_t*)iv)[i]); + } + /* set the other bits of iv to 1 */ + ctx->lfsr[0] = ctx->lfsr[1] = 0xFF; + + /* run it 160 times */ + for(i=0; i<160; ++i){ + t = grain_enc(ctx); + (ctx->lfsr[0]) ^= t; + (ctx->nfsr[0]) ^= t; + } +} + + + + + + diff --git a/grain.h b/grain.h new file mode 100644 index 0000000..231aa95 --- /dev/null +++ b/grain.h @@ -0,0 +1,24 @@ +/** + * + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + * + */ + +#ifndef GRAIN_H_ +#define GRAIN_H_ + + +#include + +typedef struct gain_ctx_st{ + uint8_t lfsr[10]; + uint8_t nfsr[10]; +} grain_ctx_t; + + +uint8_t grain_enc(grain_ctx_t* ctx); +void grain_init(const void* key, const void* iv, grain_ctx_t* ctx); + +#endif /*GRAIN_H_*/ diff --git a/grain.mk b/grain.mk new file mode 100644 index 0000000..ed330a6 --- /dev/null +++ b/grain.mk @@ -0,0 +1,13 @@ +# Makefile for Grain +ALGO_NAME := GRAIN + +# comment out the following line for removement of Grain from the build process +STREAM_CIPHERS += $(ALGO_NAME) + +$(ALGO_NAME)_OBJ := grain.o +$(ALGO_NAME)_TEST_BIN := main-grain-test.o debug.o uart.o serial-tools.o \ + nessie_stream_test.o nessie_common.o grain.o cli.o \ + performance_test.o +$(ALGO_NAME)_NESSIE_TEST := "nessie" +$(ALGO_NAME)_PEROFRMANCE_TEST := "performance" + diff --git a/grain_h_lutgen.c b/grain_h_lutgen.c new file mode 100644 index 0000000..4b5ede0 --- /dev/null +++ b/grain_h_lutgen.c @@ -0,0 +1,60 @@ +/** + * + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + * + * this program generate a lookuptable for the h-function in grain + */ + +#include +#include + +#define X(i) ((x)>>((i))) +uint8_t h(uint8_t x){ + uint8_t h; + + h = (X(1)) ^ (X(4)) ^ + (X(0)&X(3)) ^ (X(2)&X(3)) ^ (X(3)&X(4)) ^ + (X(0)&X(1)&X(2)) ^ (X(0)&X(2)&X(3)) ^ (X(0)&X(2)&X(4)) ^ + (X(1)&X(2)&X(4)) ^ (X(2)&X(3)&X(4)) ; + + return h&1; +} + +int main(void){ + uint8_t i; + uint32_t lut; + puts( + "/* \n" + " * author: Daniel Otte \n" + " * email: daniel.otte@rub.de \n" + " * license: GPLv3 \n" + " * \n" + " * this program generate a lookuptable for the h-function in grain \n" + " * \n" + " */ \n"); + puts("/* \n" + " * x0 x1 x2 x3 x4 - h"); + + for(i=0; i<0x20; ++i){ + printf(" * %c %c %c %c %c - %c\n", + (i&0x01)?'1':'0', + (i&0x02)?'1':'0', + (i&0x04)?'1':'0', + (i&0x08)?'1':'0', + (i&0x10)?'1':'0', + (h(i))?'1':'0' ); + lut >>=1; + lut |= h(i)?0x80000000:0x00000000; + if(i%4==3){ + puts(" * --"); + } + } + puts(" */\n"); + printf(" uint8_t lut[4]= {0x%2.2X, 0x%2.2X, 0x%2.2X, 0x%2.2X} \n", + lut&0xFF, (lut>>8)&0xFF, (lut>>16)&0xFF, (lut>>24)&0xFF); + + return 0; +} + diff --git a/main-grain-test.c b/main-grain-test.c new file mode 100644 index 0000000..d8da791 --- /dev/null +++ b/main-grain-test.c @@ -0,0 +1,169 @@ +/* + * grain test-suit + * +*/ + +#include "config.h" +#include "serial-tools.h" +#include "uart.h" +#include "debug.h" +#include "cli.h" + +#include "grain.h" +#include "nessie_stream_test.h" +#include "performance_test.h" + +#include +#include +#include + +char* cipher_name = "Grain"; + +/***************************************************************************** + * additional validation-functions * + *****************************************************************************/ +void grain_genctx_dummy(uint8_t* key, uint16_t keysize_b, void* ctx){ + uint8_t iv[8]={0}; + grain_init(key, &iv, ctx); +} + +uint8_t grain_getbyte_dummy(grain_ctx_t* ctx){ + uint8_t i,ret=0; + for(i=0; i<8; ++i){ + ret<<=1; + ret |= grain_enc(ctx); + } + return ret; +} + +uint8_t grain_getbyte_dummy_rev(grain_ctx_t* ctx){ + uint8_t i,ret=0; + for(i=0; i<8; ++i){ + ret >>= 1; + ret |= grain_enc(ctx)?0x80:0x00; + } + return ret; +} + +void testrun_nessie_grain(void){ + nessie_stream_ctx.outsize_b = 8; /* actually unused */ + nessie_stream_ctx.keysize_b = 80; /* this is the one we have refrence vectors for */ + nessie_stream_ctx.ivsize_b = 64; + nessie_stream_ctx.name = cipher_name; + nessie_stream_ctx.ctx_size_B = sizeof(grain_ctx_t); + nessie_stream_ctx.cipher_genctx = (nessie_stream_genctx_fpt)grain_genctx_dummy; + nessie_stream_ctx.cipher_enc = (nessie_stream_genenc_fpt)grain_getbyte_dummy_rev; + + nessie_stream_run(); +} + + +void testrun_std_grain(void){ + grain_ctx_t ctx; + uint8_t i, key[10], iv[8], out[10]; + + /* 1 */ + memset(key, 0, 10); + memset(iv, 0, 8); + uart_putstr_P(PSTR("\r\n=== std test ===")); + uart_putstr_P(PSTR("\r\n key: ")); + uart_hexdump(key, 10); + uart_putstr_P(PSTR("\r\n iv: ")); + uart_hexdump(key, 8); + grain_init(key, iv, &ctx); + for(i=0; i<10; ++i){ + out[i] = grain_getbyte_dummy(&ctx); + } + uart_putstr_P(PSTR("\r\n out: ")); + uart_hexdump(out, 10); + + /* 2 */ + for(i=0; i<8; ++i){ + key[i] = i*0x22+1; + } + key[8]=0x12; + key[9]=0x34; + + for(i=0; i<8; ++i){ + iv[i] = i*0x22+1; + } + uart_putstr_P(PSTR("\r\n\r\n key: ")); + uart_hexdump(key, 10); + uart_putstr_P(PSTR("\r\n iv: ")); + uart_hexdump(key, 8); + grain_init(key, iv, &ctx); + for(i=0; i<10; ++i){ + out[i] = grain_getbyte_dummy(&ctx); + } + uart_putstr_P(PSTR("\r\n out: ")); + uart_hexdump(out, 10); + + + uart_putstr_P(PSTR("\r\n\r\n")); +} + +void testrun_performance_grain(void){ + uint16_t i,c; + uint64_t t; + char str[16]; + uint8_t key[10], iv[8]; + grain_ctx_t ctx; + + 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); + + memset(key, 0, 10); + memset(iv, 0, 8); + + startTimer(1); + grain_init(key, iv, &ctx); + t = stopTimer(); + uart_putstr_P(PSTR("\r\n\tctx-gen time: ")); + ultoa((unsigned long)t, str, 10); + uart_putstr(str); + + startTimer(1); + grain_enc(&ctx); + t = stopTimer(); + uart_putstr_P(PSTR("\r\n\tencrypt 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_nessie_grain, testrun_std_grain, testrun_performance_grain}; + + 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/nessie_common.c b/nessie_common.c index b828052..b5f64c8 100644 --- a/nessie_common.c +++ b/nessie_common.c @@ -105,7 +105,7 @@ void nessie_print_header(char* name, uint16_t i; uart_putstr_P(PSTR("\r\n\r\n" "********************************************************************************\r\n" - "* micro-cryt - crypto primitives for microcontrolles by Daniel Otte *\r\n" + "* micro-crypt - crypto primitives for microcontrolles by Daniel Otte *\r\n" "********************************************************************************\r\n" "\r\n")); uart_putstr_P(PSTR("Primitive Name: ")); diff --git a/shabea.c b/shabea.c index e786139..7acb4d3 100644 --- a/shabea.c +++ b/shabea.c @@ -2,7 +2,7 @@ * \file shabea.c * \author Daniel Otte * \date 2007-06-07 - * \brief SHABEA - a SHA Based Encrytion Algorithm implementation + * \brief SHABEA - a SHA Based Encryption Algorithm implementation * \par License * GPL * diff --git a/shabea.h b/shabea.h index 5388f44..8130448 100644 --- a/shabea.h +++ b/shabea.h @@ -2,7 +2,7 @@ * \file shabea.h * \author Daniel Otte * \date 2007-06-07 - * \brief SHABEA - a SHA Based Encrytion Algorithm declarations + * \brief SHABEA - a SHA Based Encryption Algorithm declarations * \par License * GPL * -- 2.39.5