]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
small update (integrating NOEKEON_OMAC and SEED_C)
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Fri, 19 Dec 2008 22:40:58 +0000 (22:40 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Fri, 19 Dec 2008 22:40:58 +0000 (22:40 +0000)
Makefile
avr-makefile.inc
cast5.c
cast5.h
config.h
mkfiles/omac_noekeon.mk [new file with mode: 0644]
mkfiles/omac_noekeon_c.mk [new file with mode: 0644]
mkfiles/seed_C.mk [new file with mode: 0644]
omac_noekeon_C.c [new file with mode: 0644]
seed.h
seed_C.c

index 359e592f636474b632aa4e0ffd111e359c5ab988..75876cfb8a26567a6aeabdcf40f2c586d356021a 100644 (file)
--- 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
index 08457a3f6dc80392a9929de4cb6d5225495912a7..d40f6c76258ccf01bb501b999091938da5e27033 100644 (file)
@@ -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 0ed3ddfdaa6fec6bcf32f3ac0925799e4ba902e8..a7984d489861f1ac9246348a47efe90b3c69c890 100644 (file)
--- 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 aa81ca19a8fb0f5308ccd440363796f937c87938..2b1d31701fcf82f7c70fa5f327964fee79116598 100644 (file)
--- 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
index 529ff513a4ad5619d234804f8ffc8cdbf3838f03..0a0caf1520c716217c5ef9897dd01d483e925c1b 100644 (file)
--- 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 (file)
index 0000000..41c5dff
--- /dev/null
@@ -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 (file)
index 0000000..d142283
--- /dev/null
@@ -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 (file)
index 0000000..3005819
--- /dev/null
@@ -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 (file)
index 0000000..3046ede
--- /dev/null
@@ -0,0 +1,74 @@
+#include "noekeon.h"
+#include "omac_noekeon.h"
+#include "memxor.h"
+#include <string.h>
+#include <stdint.h>
+
+
+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 4ee1d8b9e65df58abe3697a4bee6d07a99b2cf52..7283191b45f74e3b767b53a2341834b45626ab9e 100644 (file)
--- a/seed.h
+++ b/seed.h
 #define SEED_H_
 
 #include <stdint.h>
-
+/** \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_*/
index e4b77f519b33db198f0c3b1a037ab6f9c77bdda3..5cda64decf0d8169a2ec4fb4a81b1a5ac2716fbb 100644 (file)
--- 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);