]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
+Present +some fixes at nessie_common
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Tue, 8 Apr 2008 06:41:15 +0000 (06:41 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Tue, 8 Apr 2008 06:41:15 +0000 (06:41 +0000)
cli.c [new file with mode: 0644]
cli.h [new file with mode: 0644]
main-present-test.c [new file with mode: 0644]
main-serpent-test.c
nessie_common.c
present.c [new file with mode: 0644]
present.h [new file with mode: 0644]
present.mk [new file with mode: 0644]
serpent.mk

diff --git a/cli.c b/cli.c
new file mode 100644 (file)
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 <stdint.h>
+#include <string.h>
+#include <avr/pgmspace.h>
+
+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 (file)
index 0000000..da146b8
--- /dev/null
+++ b/cli.h
@@ -0,0 +1,11 @@
+#ifndef CLI_H_
+#define CLI_H_
+
+#include <stdint.h>
+#include <avr/pgmspace.h>
+
+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 (file)
index 0000000..bcb0e54
--- /dev/null
@@ -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 <stdint.h>
+#include <string.h>
+
+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");
+       }
+       
+}
index 7f3d9ac2d89d0aad2cc25bd9570fc2b467434d4d..c05ef173557fc9ba25f167a50aefcd1f7d1c5d09 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "serpent.h"
 #include "nessie_bc_test.h"
+#include "cli.h"
 
 #include <stdint.h>
 #include <string.h>
@@ -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");
index 2f663a6a1377151482d3bcfbf1c5f94d9e37568f..7adac8232dabf199c987e5b0d497057c464a66b6 100644 (file)
@@ -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 (file)
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 <string.h>
+#include <stdint.h> 
+#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 (file)
index 0000000..d557800
--- /dev/null
+++ b/present.h
@@ -0,0 +1,16 @@
+#ifndef PRESENT_H_
+#define PRESENT_H_
+
+#include <stdint.h>
+
+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 (file)
index 0000000..eba6e78
--- /dev/null
@@ -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"
+
index ff6bd467b468c88be76968c00786fa11e88c7330..0ff61a6dc1096844608fd8611c953824e7d3c808 100644 (file)
@@ -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"