From: bg Date: Tue, 12 Aug 2008 16:20:37 +0000 (+0000) Subject: finished asm version of serpent (may be optimized later) X-Git-Url: https://git.cryptolib.org/?a=commitdiff_plain;h=3b6caf79acc2653f594f2a62d4c220f78660d89e;p=avr-crypto-lib.git finished asm version of serpent (may be optimized later) --- diff --git a/mkfiles/serpent-bitslice.mk b/mkfiles/serpent-bitslice.mk index e17ab28..20bcf5e 100644 --- a/mkfiles/serpent-bitslice.mk +++ b/mkfiles/serpent-bitslice.mk @@ -5,7 +5,7 @@ ALGO_NAME := SERPENT_BITSLICE BLOCK_CIPHERS += $(ALGO_NAME) -$(ALGO_NAME)_OBJ := serpent.o serpent-sboxes-bitslice.o memxor.o +$(ALGO_NAME)_OBJ := serpent-asm.o serpent-sboxes-bitslice.o memxor.o $(ALGO_NAME)_TEST_BIN := main-serpent-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" diff --git a/mkfiles/serpent_asm_fast.mk b/mkfiles/serpent_asm_fast.mk index 45070aa..3b0d3c2 100644 --- a/mkfiles/serpent_asm_fast.mk +++ b/mkfiles/serpent_asm_fast.mk @@ -5,7 +5,7 @@ ALGO_NAME := SERPENT_ASM_FAST BLOCK_CIPHERS += $(ALGO_NAME) -$(ALGO_NAME)_OBJ := serpent.o serpent-sboxes-fast.o memxor.o +$(ALGO_NAME)_OBJ := serpent-asm.o serpent-sboxes-fast.o memxor.o $(ALGO_NAME)_TEST_BIN := main-serpent-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" diff --git a/mkfiles/serpent_asm_small.mk b/mkfiles/serpent_asm_small.mk index 835dfee..78b8217 100644 --- a/mkfiles/serpent_asm_small.mk +++ b/mkfiles/serpent_asm_small.mk @@ -5,7 +5,7 @@ ALGO_NAME := SERPENT_ASM_SMALL BLOCK_CIPHERS += $(ALGO_NAME) -$(ALGO_NAME)_OBJ := serpent.o serpent-asm.o serpent-sboxes-small.o memxor.o +$(ALGO_NAME)_OBJ := serpent-asm.o serpent-sboxes-small.o memxor.o $(ALGO_NAME)_TEST_BIN := main-serpent-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" diff --git a/serpent-asm.S b/serpent-asm.S index 1005fd3..56c5dd0 100644 --- a/serpent-asm.S +++ b/serpent-asm.S @@ -123,7 +123,6 @@ T1 = 21 T2 = 22 T3 = 23 -.global serpent_lt serpent_lt: push_range 4, 17 movw r26, r24 @@ -278,8 +277,8 @@ serpent_lt: pop_range 4, 17 ret - #endif + T0 = 22 T1 = 23 T2 = 24 @@ -339,7 +338,6 @@ memeor32: brne 1b ret -.global serpent_lt serpent_lt: /* X0 := X0 <<< 13 */ movw r26, r24 @@ -432,7 +430,6 @@ serpent_lt: rcall memrotr32 ret -.global serpent_inv_lt serpent_inv_lt: /* X0 := X0 >>> 5 */ movw r26, r24 @@ -541,7 +538,6 @@ static uint32_t serpent_gen_w(uint32_t * b, uint8_t i){ /* trashes: * r20-r25, r30-r31 */ -.global serpent_gen_w serpent_gen_w: movw r30, r24 /* ^i^b[0]*/ @@ -694,7 +690,111 @@ serpent_init: stack_free 32 ret +/* + * void serpent_enc(void* buffer, const serpent_ctx_t* ctx){ + */ +/* + * param buffer is passed in r24:r25 + * param ctx is passed in r22:r23 + */ +.global serpent_enc +serpent_enc: + + push_ r12, r13, r14, r15, r16 + clr r16 + movw r14, r24 + movw r12, r22 +1: + movw r24, r14 + movw r22, r12 + ldi r20, 16 + add r12, r20 + adc r13, r1 + clr r21 + rcall memxor + movw r24, r14 + mov r22, r16 + rcall sbox128 + movw r24, r14 + rcall serpent_lt + + inc r16 + cpi r16, 31 + brne 1b + + movw r24, r14 + movw r22, r12 + ldi r20, 16 + add r12, r20 + adc r13, r1 + clr r21 + rcall memxor + movw r24, r14 + mov r22, r16 + rcall sbox128 + + inc r16 + movw r24, r14 + movw r22, r12 + ldi r20, 16 + clr r21 + pop_ r16, r15, r14, r13, r12 + rjmp memxor +/* + * void serpent_dec(void* buffer, const serpent_ctx_t* ctx){ + */ +/* + * param buffer is passed in r24:r25 + * param ctx is passed in r22:r23 + */ +.global serpent_dec +serpent_dec: + push_ r12, r13, r14, r15, r16 + movw r14, r24 +// ldi r16, lo8(32*16) +// add r22, r16 + ldi r16, hi8(32*16) + add r23, r16 + movw r12, r22 + ldi r20, 16 + clr r21 + rcall memxor + + movw r24, r14 + ldi r22, 31 + rcall inv_sbox128 + + movw r24, r14 + ldi r20, 16 + sub r12, r20 + sbc r13, r1 + movw r22, r12 + clr r21 + rcall memxor + ldi r16, 31 +1: + dec r16 + movw r24, r14 + rcall serpent_inv_lt + movw r24, r14 + mov r22, r16 + rcall inv_sbox128 + movw r24, r14 + ldi r20, 16 + sub r12, r20 + sbc r13, r1 + movw r22, r12 + clr r21 + rcall memxor + + tst r16 + brne 1b + pop_ r16, r15, r14, r13, r12 + ret + + + diff --git a/serpent.c b/serpent.c index 7af779d..1a13412 100644 --- a/serpent.c +++ b/serpent.c @@ -47,8 +47,6 @@ uint32_t rotr32(uint32_t a, uint8_t n){ #define X2 (((uint32_t*)b)[2]) #define X3 (((uint32_t*)b)[3]) -void serpent_lt(uint8_t *b); -/* static void serpent_lt(uint8_t *b){ X0 = rotl32(X0, 13); X2 = rotl32(X2, 3); @@ -61,11 +59,7 @@ static void serpent_lt(uint8_t *b){ X0 = rotl32(X0, 5); X2 = rotr32(X2, 10); } -*/ - -static void serpent_inv_lt(uint8_t *b); -/* static void serpent_inv_lt(uint8_t *b){ X2 = rotl32(X2, 10); X0 = rotr32(X0, 5); @@ -78,10 +72,7 @@ static void serpent_inv_lt(uint8_t *b){ X2 = rotr32(X2, 3); X0 = rotr32(X0, 13); } -*/ -uint32_t serpent_gen_w(uint32_t * b, uint8_t i); -/* #define GOLDEN_RATIO 0x9e3779b9l static uint32_t serpent_gen_w(uint32_t * b, uint8_t i){ @@ -90,8 +81,7 @@ static uint32_t serpent_gen_w(uint32_t * b, uint8_t i){ ret = rotl32(ret, 11); return ret; } -*/ -#if 0 + void serpent_init(const void* key, uint16_t keysize_b, serpent_ctx_t* ctx){ uint32_t buffer[8]; uint8_t i,j; @@ -115,7 +105,6 @@ void serpent_init(const void* key, uint16_t keysize_b, serpent_ctx_t* ctx){ sbox128(ctx->k[i],3-i); } } -#endif void serpent_enc(void* buffer, const serpent_ctx_t* ctx){ uint8_t i; @@ -149,4 +138,3 @@ void serpent_dec(void* buffer, const serpent_ctx_t* ctx){ -