]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
rcfour optimized++; memxor optimized++
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Thu, 7 Aug 2008 09:45:50 +0000 (09:45 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Thu, 7 Aug 2008 09:45:50 +0000 (09:45 +0000)
arcfour-asm.S
arcfour.c
memxor.S [new file with mode: 0644]
memxor.c [deleted file]
memxor_c.c [new file with mode: 0644]
mkfiles/arcfour.mk
mkfiles/arcfour_c.mk
test_src/main-arcfour-test.c

index 05cac9255180b52b5dc5dbb7554a9936631d465e..5fbf2a271837374b8d7c9f3f651bae9dfd189d0b 100644 (file)
@@ -90,7 +90,7 @@
  *     given in r20:r21
  */
 arcfour_init:
-       push_ r2, r28, r29
+       push_ r28, r29
        movw r26, r20   /* X points to ctx */
        movw r30, r24   /* Z points to key */
        st X+, r1
@@ -103,29 +103,27 @@ arcfour_init:
        brne 1b
        
        movw r26, r20
-       clr r18         /* r18 is keyindex counter */
+       add r22, r30         /* r18 is keyindex counter */
        clr r0
        clr r19
 2:
        ld r23, X
-       ld r2, Z+
-       add r19, r2
+       ld r18, Z+
+       add r19, r18
        add r19, r23
        movw r28, r20   /* load pointer to S in Y */
        add r28, r19
        adc r29, r1
-       ld r2, Y
+       ld r18, Y
        st Y,  r23
-       st X+, r2 
-       inc r18
-       cp r18, r22
+       st X+, r18
+       cp r30, r22
        brne 3f
        movw r30, r24
-       clr r18
 3:             
        inc r0
        brne 2b 
-       pop_ r29, r28, r2
+       pop_ r29, r28
        ret
 
 /*
index 7c35a0353f3fef055708ca56ea39f5fd13f32448..93b2e26bd2e1cd61c34bf8d5bc761c22486b5675 100644 (file)
--- a/arcfour.c
+++ b/arcfour.c
@@ -54,6 +54,7 @@ uint8_t arcfour_gen(arcfour_ctx_t *ctx){
        uint8_t t;
        ctx->i++;
        ctx->j += ctx->s[ctx->i];
+       /* ctx->s[i] <--> ctx->s[j] */
        t = ctx->s[ctx->j];
        ctx->s[ctx->j] = ctx->s[ctx->i];
        ctx->s[ctx->i] = t;
diff --git a/memxor.S b/memxor.S
new file mode 100644 (file)
index 0000000..0e04198
--- /dev/null
+++ b/memxor.S
@@ -0,0 +1,115 @@
+/* memxor.S */
+/*
+    This file is part of the Crypto-avr-lib/microcrypt-lib.
+    Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/* 
+ * File:        memxor.S
+ * Author:      Daniel Otte
+ * Date:        2006-07-06
+ * License:     GPLv3 or later
+ * Description: Implementation of the ARCFOUR (RC4 compatible) stream cipher algorithm.
+ * 
+ */
+#include <avr/io.h>
+
+
+.macro push_ p1:req, p2:vararg
+       push \p1
+.ifnb \p2      
+       push_ \p2
+.endif
+.endm
+
+.macro pop_ p1:req, p2:vararg
+       pop \p1
+.ifnb \p2      
+       pop_ \p2
+.endif
+.endm
+
+.macro push_range from:req, to:req
+       push \from
+.if     \to-\from
+       push_range "(\from+1)",\to
+.endif         
+.endm
+
+.macro pop_range from:req, to:req
+       pop \to
+.if     \to-\from
+       pop_range \from,"(\to-1)"       
+.endif
+.endm
+
+.macro stack_alloc size:req, reg1=r30, reg2=r31
+       in \reg1, _SFR_IO_ADDR(SPL)
+       in \reg2, _SFR_IO_ADDR(SPH)
+       sbiw r30, \size 
+       out  _SFR_IO_ADDR(SPH), \reg2
+       out  _SFR_IO_ADDR(SPL), \reg1
+.endm
+
+.macro stack_free size:req, reg1=r30, reg2=r31
+       in \reg1, _SFR_IO_ADDR(SPL)
+       in \reg2, _SFR_IO_ADDR(SPH)
+       adiw r30, \size 
+       out  _SFR_IO_ADDR(SPH), \reg2
+       out  _SFR_IO_ADDR(SPL), \reg1
+.endm
+
+/*
+ * void memxor(void* dest, const void* src, uint16_t n);
+ */
+ /*
+  * param dest is passed in r24:r25
+  * param src  is passed in r22:r23
+  * param n    is passed in r20:r21
+  */
+.global memxor
+memxor:
+       movw r30, r24
+       movw r26, r22
+       movw r24, r20
+       tst r24
+       brne 1f
+       tst r25
+       breq 2f
+1:
+       ld r20, X+
+       ld r21, Z
+       eor r20, r21
+       st Z+, r20
+       sbiw r24, 1
+       brne 1b
+2:
+       ret
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+
diff --git a/memxor.c b/memxor.c
deleted file mode 100644 (file)
index 7485b3e..0000000
--- a/memxor.c
+++ /dev/null
@@ -1,12 +0,0 @@
-#include <stdint.h>
-
-#include "memxor.h"
-
-void memxor(void* dest, const void* src, uint16_t n){
-  while(n--){
-    *((uint8_t*)dest) ^= *((uint8_t*)src);
-    dest = (uint8_t*)dest +1;
-    src  = (uint8_t*)src  +1;
-  }
-}
-
diff --git a/memxor_c.c b/memxor_c.c
new file mode 100644 (file)
index 0000000..7485b3e
--- /dev/null
@@ -0,0 +1,12 @@
+#include <stdint.h>
+
+#include "memxor.h"
+
+void memxor(void* dest, const void* src, uint16_t n){
+  while(n--){
+    *((uint8_t*)dest) ^= *((uint8_t*)src);
+    dest = (uint8_t*)dest +1;
+    src  = (uint8_t*)src  +1;
+  }
+}
+
index f5b4089818c099c46d91e1d4f563535cda88a6d7..58563f6fa5e7516a10a0cb87b3f212c21014380a 100644 (file)
@@ -6,7 +6,8 @@ STREAM_CIPHERS += $(ALGO_NAME)
 
 $(ALGO_NAME)_OBJ      := arcfour-asm.o
 $(ALGO_NAME)_TEST_BIN := main-arcfour-test.o debug.o uart.o serial-tools.o \
-                         nessie_stream_test.o nessie_common.o
+                         nessie_stream_test.o nessie_common.o cli.o \
+                         performance_test.o
 $(ALGO_NAME)_NESSIE_TEST      := "nessie"
 $(ALGO_NAME)_PEROFRMANCE_TEST := "performance"
 
index 21061a5eedc1e6697868a0528c88e95e1ae3730a..7e3d23ed7aa591f2a95f1bf32000ec7920ca5d61 100644 (file)
@@ -6,7 +6,8 @@ STREAM_CIPHERS += $(ALGO_NAME)
 
 $(ALGO_NAME)_OBJ      := arcfour.o
 $(ALGO_NAME)_TEST_BIN := main-arcfour-test.o debug.o uart.o serial-tools.o \
-                         nessie_stream_test.o nessie_common.o
+                         nessie_stream_test.o nessie_common.o cli.o \
+                         performance_test.o
 $(ALGO_NAME)_NESSIE_TEST      := "nessie"
 $(ALGO_NAME)_PEROFRMANCE_TEST := "performance"
 
index 7ebe5277b9db008026b693f2657dd9b9cbca1edc..009366f29c993276a61367d5e74f95bad6c52048 100644 (file)
 
 #include "arcfour.h"
 #include "nessie_stream_test.h"
+#include "cli.h"
+#include "performance_test.h"
 
+#include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
 
@@ -56,15 +59,31 @@ void testrun_nessie_arcfour(void){
 }
 
 void testrun_performance_arcfour(void){
-       nessie_stream_ctx.outsize_b = 8; /* actually unused */
-       nessie_stream_ctx.keysize_b = 128; /* this is theone we have refrence vectors for */
-       nessie_stream_ctx.ivsize_b = (uint16_t)-1;
-       nessie_stream_ctx.name = cipher_name;
-       nessie_stream_ctx.ctx_size_B = sizeof(arcfour_ctx_t);
-       nessie_stream_ctx.cipher_genctx = (nessie_stream_genctx_fpt)arcfour_genctx_dummy;
-       nessie_stream_ctx.cipher_enc = (nessie_stream_genenc_fpt)arcfour_gen;
+       uint64_t t;
+       char str[16];
+       uint8_t key[16];
+       arcfour_ctx_t ctx;
        
-       nessie_stream_run();    
+       calibrateTimer();
+       print_overhead();       
+       
+       memset(key,  0, 16);
+       
+       startTimer(1);
+       arcfour_init(key, 16, &ctx);
+       t = stopTimer();
+       uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
+       ultoa((unsigned long)t, str, 10);
+       uart_putstr(str);       
+       
+       startTimer(1);
+       arcfour_gen(&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"));    
 }
 
 
@@ -75,18 +94,21 @@ void testrun_performance_arcfour(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"));
 
-restart:
+       PGM_P    u   = PSTR("nessie\0test\0performance\0");
+       void_fpt v[] = {testrun_nessie_arcfour, 
+                           testrun_nessie_arcfour, 
+                           testrun_performance_arcfour};
+
        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_arcfour();
-               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");