From f0ca870318011539b8cab50cf3e9b96a19307deb Mon Sep 17 00:00:00 2001 From: bg Date: Thu, 7 Aug 2008 09:45:50 +0000 Subject: [PATCH] rcfour optimized++; memxor optimized++ --- arcfour-asm.S | 18 +++--- arcfour.c | 1 + memxor.S | 115 +++++++++++++++++++++++++++++++++++ memxor.c => memxor_c.c | 0 mkfiles/arcfour.mk | 3 +- mkfiles/arcfour_c.mk | 3 +- test_src/main-arcfour-test.c | 52 +++++++++++----- 7 files changed, 165 insertions(+), 27 deletions(-) create mode 100644 memxor.S rename memxor.c => memxor_c.c (100%) diff --git a/arcfour-asm.S b/arcfour-asm.S index 05cac92..5fbf2a2 100644 --- a/arcfour-asm.S +++ b/arcfour-asm.S @@ -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 /* diff --git a/arcfour.c b/arcfour.c index 7c35a03..93b2e26 100644 --- 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 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 . +*/ + +/* + * 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 + + +.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.c similarity index 100% rename from memxor.c rename to memxor_c.c diff --git a/mkfiles/arcfour.mk b/mkfiles/arcfour.mk index f5b4089..58563f6 100644 --- a/mkfiles/arcfour.mk +++ b/mkfiles/arcfour.mk @@ -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" diff --git a/mkfiles/arcfour_c.mk b/mkfiles/arcfour_c.mk index 21061a5..7e3d23e 100644 --- a/mkfiles/arcfour_c.mk +++ b/mkfiles/arcfour_c.mk @@ -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" diff --git a/test_src/main-arcfour-test.c b/test_src/main-arcfour-test.c index 7ebe527..009366f 100644 --- a/test_src/main-arcfour-test.c +++ b/test_src/main-arcfour-test.c @@ -28,7 +28,10 @@ #include "arcfour.h" #include "nessie_stream_test.h" +#include "cli.h" +#include "performance_test.h" +#include #include #include @@ -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"); -- 2.39.5