From a15d02c94b1ce1fc9861e654e96e0ee369fe08e9 Mon Sep 17 00:00:00 2001 From: bg Date: Tue, 12 Aug 2008 13:25:31 +0000 Subject: [PATCH] even more serpent in asm --- serpent-asm.S | 120 +++++++++++++++++++++++++++++++++++ serpent.c | 11 ++-- serpent.h | 2 +- test_src/main-serpent-test.c | 15 ++++- 4 files changed, 141 insertions(+), 7 deletions(-) diff --git a/serpent-asm.S b/serpent-asm.S index a16f6af..1005fd3 100644 --- a/serpent-asm.S +++ b/serpent-asm.S @@ -538,6 +538,9 @@ static uint32_t serpent_gen_w(uint32_t * b, uint8_t i){ * param i is passed in r22 * return value is returned in r22.r23.r24.r25 */ + /* trashes: + * r20-r25, r30-r31 + */ .global serpent_gen_w serpent_gen_w: movw r30, r24 @@ -589,3 +592,120 @@ serpent_gen_w: brne 1b ret +/* + * void serpent_init(const void* key, uint16_t keysize_b, serpent_ctx_t* ctx) + */ +/* + * param key is passed in r24:r25 + * param keysize is passed in r22:r23 + * param ctx is passed in r20:r21 + */ +.global serpent_init +serpent_init: + stack_alloc 32 + adiw r30, 1 + push_ r30, r31 + movw r26, r22 + adiw r26, 7 + tst r27 + breq 1f + ldi r26, 32 + rjmp 2f +1: + lsr r26 + lsr r26 + lsr r26 +2: + mov r22, r26 + bst r22, 5 /* store in T if we have to do the "append 1 thing"*/ + ldi r27, 32 +3: /* set buffer to zero */ + st Z+, r1 + dec r27 + brne 3b + + movw r26, r24 /* X points to the key */ + sbiw r30, 32 + tst r22 + breq 5f /* if keylength_b==0 */ +4: /* copy keybytes to buffer */ + ld r19, X+ + st Z+, r19 + dec r22 + brne 4b +5: + brts 7f /* if keylength_b == 256 */ + ldi r18, 0x01 + andi r22, 0x07 + brne 6f + st Z, r18 + rjmp 7f +6: /* shift the one to the right position */ + lsl r18 + dec r22 + brne 6b + or r18, r19 + st -Z, r18 +7: /* post "appending 1 thing" buffer is ready for subkey generation */ + movw r26, r20 /* X points to the context */ + + pop_ r19, r18 /* r18:r19 points to the buffer */ + push r16 + clr r16 +8: + movw r24, r18 + mov r22, r16 + rcall serpent_gen_w + movw r30, r18 + ldi r20, 7*4 +1: /* the memmove */ + ldd r0, Z+4 + st Z+, r0 + dec r20 + brne 1b + /* store new word in buffer and context */ + st Z+, r22 + st Z+, r23 + st Z+, r24 + st Z+, r25 + st X+, r22 + st X+, r23 + st X+, r24 + st X+, r25 + + inc r16 + cpi r16, 132 + brne 8b + + push_ r28, r29 + movw r28, r26 + subi r28, lo8(132*4) + sbci r29, hi8(132*4) + ldi r16, 33 +2: + movw r24, r28 + adiw r28, 16 + ldi r22, 2 + add r22, r16 + rcall sbox128 + dec r16 + brne 2b + pop_ r29, r28, r16 + stack_free 32 + ret + + + + + + + + + + + + + + + + diff --git a/serpent.c b/serpent.c index 4960664..7af779d 100644 --- a/serpent.c +++ b/serpent.c @@ -91,14 +91,15 @@ static uint32_t serpent_gen_w(uint32_t * b, uint8_t i){ return ret; } */ -void serpent_init(const void* key, uint16_t keysize, serpent_ctx_t* ctx){ +#if 0 +void serpent_init(const void* key, uint16_t keysize_b, serpent_ctx_t* ctx){ uint32_t buffer[8]; uint8_t i,j; - if(keysize<256){ + if(keysize_b<256){ /* keysize is less than 256 bit, padding needed */ memset(buffer, 0, 32); - memcpy(buffer, key, (keysize+7)/8); - ((uint8_t*)buffer)[keysize/8] |= 1<<(keysize%8); + memcpy(buffer, key, (keysize_b+7)/8); + ((uint8_t*)buffer)[keysize_b/8] |= 1<<(keysize_b%8); } else { /* keysize is 256 bit */ memcpy(buffer, key, 32); @@ -114,7 +115,7 @@ void serpent_init(const void* key, uint16_t keysize, serpent_ctx_t* ctx){ sbox128(ctx->k[i],3-i); } } - +#endif void serpent_enc(void* buffer, const serpent_ctx_t* ctx){ uint8_t i; diff --git a/serpent.h b/serpent.h index 3e90253..f65d36f 100644 --- a/serpent.h +++ b/serpent.h @@ -39,7 +39,7 @@ typedef struct serpent_ctx_st { /* key must be 256bit (32 byte) large! */ -void serpent_init(const void* key, uint16_t keysize, serpent_ctx_t* ctx); +void serpent_init(const void* key, uint16_t keysize_b, serpent_ctx_t* ctx); void serpent_enc(void* buffer, const serpent_ctx_t* ctx); void serpent_dec(void* buffer, const serpent_ctx_t* ctx); diff --git a/test_src/main-serpent-test.c b/test_src/main-serpent-test.c index 5c450b6..b4de5f4 100644 --- a/test_src/main-serpent-test.c +++ b/test_src/main-serpent-test.c @@ -62,6 +62,19 @@ void testrun_nessie_serpent(void){ nessie_bc_run(); } +void testrun_test_serpent(void){ + uint8_t key[32]; + serpent_ctx_t ctx; + uint8_t i; + memset(key, 0, 16); + serpent_init(key, 128, &ctx); + for(i=0; i<33; ++i){ + uart_putstr_P(PSTR("\r\n subkekey ")); + uart_hexdump(&i, 1); + uart_putstr_P(PSTR(" : ")); + uart_hexdump(ctx.k[i], 16); + } +} void testrun_performance_serpent(void){ uint64_t t; @@ -114,7 +127,7 @@ int main (void){ uart_putstr_P(PSTR(")\r\nloaded and running\r\n")); PGM_P u = PSTR("nessie\0test\0performance\0"); - void_fpt v[] = {testrun_nessie_serpent, testrun_nessie_serpent, testrun_performance_serpent}; + void_fpt v[] = {testrun_nessie_serpent, testrun_test_serpent, testrun_performance_serpent}; while(1){ if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;} -- 2.39.5