From: bg Date: Mon, 7 Apr 2008 22:25:31 +0000 (+0000) Subject: +serpent bitslice; modifyed cast5 testings X-Git-Url: https://git.cryptolib.org/?a=commitdiff_plain;h=158bb85c89631984291c93a9217ca84345349a46;p=avr-crypto-lib.git +serpent bitslice; modifyed cast5 testings --- diff --git a/cast5.mk b/cast5.mk index f2241c0..23dadc1 100644 --- a/cast5.mk +++ b/cast5.mk @@ -5,7 +5,8 @@ ALGO_NAME := CAST5 BLOCK_CIPHERS += $(ALGO_NAME) $(ALGO_NAME)_OBJ := cast5.o -$(ALGO_NAME)_TEST_BIN := main-cast5-test.o debug.o uart.o serial-tools.o cast5.o +$(ALGO_NAME)_TEST_BIN := main-cast5-test.o debug.o uart.o serial-tools.o \ + cast5.o nessie_bc_test.o $(ALGO_NAME)_NESSIE_TEST := "nessie" $(ALGO_NAME)_PEROFRMANCE_TEST := "performance" diff --git a/main-cast5-test.c b/main-cast5-test.c index 9b3795a..6f3b18a 100644 --- a/main-cast5-test.c +++ b/main-cast5-test.c @@ -9,14 +9,45 @@ #include "debug.h" #include "cast5.h" +#include "nessie_bc_test.h" #include #include +char* cipher_name = "cast-128 (cast5)"; /***************************************************************************** * additional validation-functions * *****************************************************************************/ +/* + void cast5_init(cast5_ctx_t* s, uint8_t* key, uint8_t keylength); + void cast5_enc(cast5_ctx_t *s, void* block); + void cast5_dec(cast5_ctx_t *s, void* block); +*/ + +void cast5_init_dummy(uint8_t* key, uint8_t keylength_b, cast5_ctx_t* ctx){ + cast5_init(ctx, key, keylength_b); +} + +void cast5_enc_dummy(void* buffer, cast5_ctx_t* ctx){ + cast5_enc(ctx, buffer); +} + +void cast5_dec_dummy(void* buffer, cast5_ctx_t* ctx){ + cast5_dec(ctx, buffer); +} + +void test_nessie_cast5(void){ + nessie_bc_ctx.blocksize_B = 8; + nessie_bc_ctx.keysize_b = 128; + nessie_bc_ctx.name = cipher_name; + nessie_bc_ctx.ctx_size_B = sizeof(cast5_ctx_t); + nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)cast5_enc_dummy; + nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)cast5_dec_dummy; + nessie_bc_ctx.cipher_genctx = (nessie_bc_gen_fpt)cast5_init_dummy; + + nessie_bc_run(); +} /***************************************************************************** * self tests * @@ -135,8 +166,9 @@ int main (void){ restart: while(1){ if (!getnextwordn(str,20)) {DEBUG_S("DBG: W1\r\n"); goto error;} - if (strcmp(str, "test")) {DEBUG_S("DBG: 1b\r\n"); goto error;} - testrun_cast5(); + if (strcmp(str, "nessie")) {DEBUG_S("DBG: 1b\r\n"); goto error;} + // testrun_cast5(); + test_nessie_cast5(); goto restart; continue; error: diff --git a/nessie_bc_test.c b/nessie_bc_test.c index 4d6d42b..c8f9257 100644 --- a/nessie_bc_test.c +++ b/nessie_bc_test.c @@ -241,9 +241,10 @@ void nessie_bc_run(void){ for(i=0; i +#include +#include +#include "serpent-sboxes.h" + + + + +/* S0: 3 8 15 1 10 6 5 11 14 13 4 2 7 0 9 12 */ + +/* depth = 5,7,4,2, Total gates=18 */ +static +void sb0(uint8_t* out, const uint8_t* in, uint8_t i){ +// (a,b,c,d,w,x,y,z) + uint8_t t01, t02, t03, t05, t06, t07, t08, t09, t11, t12, t13, t14, t15, t17; + t01 = in[4*1+i] ^ in[4*2+i]; + t02 = in[4*0+i] | in[4*3+i]; + t03 = in[4*0+i] ^ in[4*1+i]; + out[4*3+i] = t02 ^ t01; + t05 = in[4*2+i] | out[4*3+i]; + t06 = in[4*0+i] ^ in[4*3+i]; + t07 = in[4*1+i] | in[4*2+i]; + t08 = in[4*3+i] & t05; + t09 = t03 & t07; + out[4*2+i] = t09 ^ t08; + t11 = t09 & out[4*2+i]; + t12 = in[4*2+i] ^ in[4*3+i]; + t13 = t07 ^ t11; + t14 = in[4*1+i] & t06; + t15 = t06 ^ t13; + out[4*0+i] = ~ t15; + t17 = out[4*0+i] ^ t14; + out[4*1+i] = t12 ^ t17; +} + +/* InvS0: 13 3 11 0 10 6 5 12 1 14 4 7 15 9 8 2 */ + +/* depth = 8,4,3,6, Total gates=19 */ +static +void sb0_inv(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t05, t06, t08, t09, t10, t12, t13, t14, t15, t17, t18, t01; + t01 = in[4*2+i] ^ in[4*3+i]; + t02 = in[4*0+i] | in[4*1+i]; + t03 = in[4*1+i] | in[4*2+i]; + t04 = in[4*2+i] & t01; + t05 = t02 ^ t01; + t06 = in[4*0+i] | t04; + out[4*2+i] = ~ t05; + t08 = in[4*1+i] ^ in[4*3+i]; + t09 = t03 & t08; + t10 = in[4*3+i] | out[4*2+i]; + out[4*1+i] = t09 ^ t06; + t12 = in[4*0+i] | t05; + t13 = out[4*1+i] ^ t12; + t14 = t03 ^ t10; + t15 = in[4*0+i] ^ in[4*2+i]; + out[4*3+i] = t14 ^ t13; + t17 = t05 & t13; + t18 = t14 | t17; + out[4*0+i] = t15 ^ t18; +} + +/* S1: 15 12 2 7 9 0 5 10 1 11 14 8 6 13 3 4 */ + +/* depth = 10,7,3,5, Total gates=18 */ +static +void sb1(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t05, t06, t07, t08, t10, t11, t12, t13, t16, t17, t01; + t01 = in[4*0+i] | in[4*3+i]; + t02 = in[4*2+i] ^ in[4*3+i]; + t03 = ~ in[4*1+i]; + t04 = in[4*0+i] ^ in[4*2+i]; + t05 = in[4*0+i] | t03; + t06 = in[4*3+i] & t04; + t07 = t01 & t02; + t08 = in[4*1+i] | t06; + out[4*2+i] = t02 ^ t05; + t10 = t07 ^ t08; + t11 = t01 ^ t10; + t12 = out[4*2+i] ^ t11; + t13 = in[4*1+i] & in[4*3+i]; + out[4*3+i] = ~ t10; + out[4*1+i] = t13 ^ t12; + t16 = t10 | out[4*1+i]; + t17 = t05 & t16; + out[4*0+i] = in[4*2+i] ^ t17; +} + +/* InvS1: 5 8 2 14 15 6 12 3 11 4 7 9 1 13 10 0 */ + +/* depth = 7,4,5,3, Total gates=18 */ +static void sb1_inv(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t14, t15, t17, t01; + t01 = in[4*0+i] ^ in[4*1+i]; + t02 = in[4*1+i] | in[4*3+i]; + t03 = in[4*0+i] & in[4*2+i]; + t04 = in[4*2+i] ^ t02; + t05 = in[4*0+i] | t04; + t06 = t01 & t05; + t07 = in[4*3+i] | t03; + t08 = in[4*1+i] ^ t06; + t09 = t07 ^ t06; + t10 = t04 | t03; + t11 = in[4*3+i] & t08; + out[4*2+i] = ~ t09; + out[4*1+i] = t10 ^ t11; + t14 = in[4*0+i] | out[4*2+i]; + t15 = t06 ^ out[4*1+i]; + out[4*3+i] = t01 ^ t04; + t17 = in[4*2+i] ^ t15; + out[4*0+i] = t14 ^ t17; +} + +/* S2: 8 6 7 9 3 12 10 15 13 1 14 4 0 11 5 2 */ + +/* depth = 3,8,11,7, Total gates=16 */ +static void sb2(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t05, t06, t07, t08, t09, t10, t12, t13, t14, t01; + t01 = in[4*0+i] | in[4*2+i]; + t02 = in[4*0+i] ^ in[4*1+i]; + t03 = in[4*3+i] ^ t01; + out[4*0+i] = t02 ^ t03; + t05 = in[4*2+i] ^ out[4*0+i]; + t06 = in[4*1+i] ^ t05; + t07 = in[4*1+i] | t05; + t08 = t01 & t06; + t09 = t03 ^ t07; + t10 = t02 | t09; + out[4*1+i] = t10 ^ t08; + t12 = in[4*0+i] | in[4*3+i]; + t13 = t09 ^ out[4*1+i]; + t14 = in[4*1+i] ^ t13; + out[4*3+i] = ~ t09; + out[4*2+i] = t12 ^ t14; +} + +/* InvS2: 12 9 15 4 11 14 1 2 0 3 6 13 5 8 10 7 */ + +/* depth = 3,6,8,3, Total gates=18 */ +static void sb2_inv(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t06, t07, t08, t09, t10, t11, t12, t15, t16, t17, t01; + t01 = in[4*0+i] ^ in[4*3+i]; + t02 = in[4*2+i] ^ in[4*3+i]; + t03 = in[4*0+i] & in[4*2+i]; + t04 = in[4*1+i] | t02; + out[4*0+i] = t01 ^ t04; + t06 = in[4*0+i] | in[4*2+i]; + t07 = in[4*3+i] | out[4*0+i]; + t08 = ~ in[4*3+i]; + t09 = in[4*1+i] & t06; + t10 = t08 | t03; + t11 = in[4*1+i] & t07; + t12 = t06 & t02; + out[4*3+i] = t09 ^ t10; + out[4*1+i] = t12 ^ t11; + t15 = in[4*2+i] & out[4*3+i]; + t16 = out[4*0+i] ^ out[4*1+i]; + t17 = t10 ^ t15; + out[4*2+i] = t16 ^ t17; +} + +/* S3: 0 15 11 8 12 9 6 3 13 1 2 4 10 7 5 14 */ + +/* depth = 8,3,5,5, Total gates=18 */ +static void sb3(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t13, t14, t15, t01; + t01 = in[4*0+i] ^ in[4*2+i]; + t02 = in[4*0+i] | in[4*3+i]; + t03 = in[4*0+i] & in[4*3+i]; + t04 = t01 & t02; + t05 = in[4*1+i] | t03; + t06 = in[4*0+i] & in[4*1+i]; + t07 = in[4*3+i] ^ t04; + t08 = in[4*2+i] | t06; + t09 = in[4*1+i] ^ t07; + t10 = in[4*3+i] & t05; + t11 = t02 ^ t10; + out[4*3+i] = t08 ^ t09; + t13 = in[4*3+i] | out[4*3+i]; + t14 = in[4*0+i] | t07; + t15 = in[4*1+i] & t13; + out[4*2+i] = t08 ^ t11; + out[4*0+i] = t14 ^ t15; + out[4*1+i] = t05 ^ t04; +} + +/* InvS3: 0 9 10 7 11 14 6 13 3 5 12 2 4 8 15 1 */ + +/* depth = 3,6,4,4, Total gates=17 */ +static void sb3_inv(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t05, t06, t07, t09, t11, t12, t13, t14, t16, t01; + t01 = in[4*2+i] | in[4*3+i]; + t02 = in[4*0+i] | in[4*3+i]; + t03 = in[4*2+i] ^ t02; + t04 = in[4*1+i] ^ t02; + t05 = in[4*0+i] ^ in[4*3+i]; + t06 = t04 & t03; + t07 = in[4*1+i] & t01; + out[4*2+i] = t05 ^ t06; + t09 = in[4*0+i] ^ t03; + out[4*0+i] = t07 ^ t03; + t11 = out[4*0+i] | t05; + t12 = t09 & t11; + t13 = in[4*0+i] & out[4*2+i]; + t14 = t01 ^ t05; + out[4*1+i] = in[4*1+i] ^ t12; + t16 = in[4*1+i] | t13; + out[4*3+i] = t14 ^ t16; +} + +/* S4: 1 15 8 3 12 0 11 6 2 5 4 10 9 14 7 13 */ + +/* depth = 6,7,5,3, Total gates=19 */ +static void sb4(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t05, t06, t08, t09, t10, t11, t12, t13, t14, t15, t16, t01; + t01 = in[4*0+i] | in[4*1+i]; + t02 = in[4*1+i] | in[4*2+i]; + t03 = in[4*0+i] ^ t02; + t04 = in[4*1+i] ^ in[4*3+i]; + t05 = in[4*3+i] | t03; + t06 = in[4*3+i] & t01; + out[4*3+i] = t03 ^ t06; + t08 = out[4*3+i] & t04; + t09 = t04 & t05; + t10 = in[4*2+i] ^ t06; + t11 = in[4*1+i] & in[4*2+i]; + t12 = t04 ^ t08; + t13 = t11 | t03; + t14 = t10 ^ t09; + t15 = in[4*0+i] & t05; + t16 = t11 | t12; + out[4*2+i] = t13 ^ t08; + out[4*1+i] = t15 ^ t16; + out[4*0+i] = ~ t14; +} + +/* InvS4: 5 0 8 3 10 9 7 14 2 12 11 6 4 15 13 1 */ + +/* depth = 6,4,7,3, Total gates=17 */ +static void sb4_inv(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t05, t06, t07, t09, t10, t11, t12, t13, t15, t01; + t01 = in[4*1+i] | in[4*3+i]; + t02 = in[4*2+i] | in[4*3+i]; + t03 = in[4*0+i] & t01; + t04 = in[4*1+i] ^ t02; + t05 = in[4*2+i] ^ in[4*3+i]; + t06 = ~ t03; + t07 = in[4*0+i] & t04; + out[4*1+i] = t05 ^ t07; + t09 = out[4*1+i] | t06; + t10 = in[4*0+i] ^ t07; + t11 = t01 ^ t09; + t12 = in[4*3+i] ^ t04; + t13 = in[4*2+i] | t10; + out[4*3+i] = t03 ^ t12; + t15 = in[4*0+i] ^ t04; + out[4*2+i] = t11 ^ t13; + out[4*0+i] = t15 ^ t09; +} + +/* S5: 15 5 2 11 4 10 9 12 0 3 14 8 13 6 7 1 */ + +/* depth = 4,6,8,6, Total gates=17 */ +static void sb5(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t05, t07, t08, t09, t10, t11, t12, t13, t14, t01; + t01 = in[4*1+i] ^ in[4*3+i]; + t02 = in[4*1+i] | in[4*3+i]; + t03 = in[4*0+i] & t01; + t04 = in[4*2+i] ^ t02; + t05 = t03 ^ t04; + out[4*0+i] = ~ t05; + t07 = in[4*0+i] ^ t01; + t08 = in[4*3+i] | out[4*0+i]; + t09 = in[4*1+i] | t05; + t10 = in[4*3+i] ^ t08; + t11 = in[4*1+i] | t07; + t12 = t03 | out[4*0+i]; + t13 = t07 | t10; + t14 = t01 ^ t11; + out[4*2+i] = t09 ^ t13; + out[4*1+i] = t07 ^ t08; + out[4*3+i] = t12 ^ t14; +} + +/* InvS5: 8 15 2 9 4 1 13 14 11 6 5 3 7 12 10 0 */ + +/* depth = 4,6,9,7, Total gates=17 */ +static void sb5_inv(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t05, t07, t08, t09, t10, t12, t13, t15, t16, t01; + t01 = in[4*0+i] & in[4*3+i]; + t02 = in[4*2+i] ^ t01; + t03 = in[4*0+i] ^ in[4*3+i]; + t04 = in[4*1+i] & t02; + t05 = in[4*0+i] & in[4*2+i]; + out[4*0+i] = t03 ^ t04; + t07 = in[4*0+i] & out[4*0+i]; + t08 = t01 ^ out[4*0+i]; + t09 = in[4*1+i] | t05; + t10 = ~ in[4*1+i]; + out[4*1+i] = t08 ^ t09; + t12 = t10 | t07; + t13 = out[4*0+i] | out[4*1+i]; + out[4*3+i] = t02 ^ t12; + t15 = t02 ^ t13; + t16 = in[4*1+i] ^ in[4*3+i]; + out[4*2+i] = t16 ^ t15; +} + +/* S6: 7 2 12 5 8 4 6 11 14 9 1 15 13 3 10 0 */ + +/* depth = 8,3,6,3, Total gates=19 */ +static void sb6(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t05, t07, t08, t09, t10, t11, t12, t13, t15, t17, t18, t01; + t01 = in[4*0+i] & in[4*3+i]; + t02 = in[4*1+i] ^ in[4*2+i]; + t03 = in[4*0+i] ^ in[4*3+i]; + t04 = t01 ^ t02; + t05 = in[4*1+i] | in[4*2+i]; + out[4*1+i] = ~ t04; + t07 = t03 & t05; + t08 = in[4*1+i] & out[4*1+i]; + t09 = in[4*0+i] | in[4*2+i]; + t10 = t07 ^ t08; + t11 = in[4*1+i] | in[4*3+i]; + t12 = in[4*2+i] ^ t11; + t13 = t09 ^ t10; + out[4*2+i] = ~ t13; + t15 = out[4*1+i] & t03; + out[4*3+i] = t12 ^ t07; + t17 = in[4*0+i] ^ in[4*1+i]; + t18 = out[4*2+i] ^ t15; + out[4*0+i] = t17 ^ t18; +} + +/* InvS6: 15 10 1 13 5 3 6 0 4 9 14 7 2 12 8 11 */ + +/* depth = 5,3,8,6, Total gates=19 */ +static void sb6_inv(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t05, t06, t07, t08, t09, t12, t13, t14, t15, t16, t17, t01; + t01 = in[4*0+i] ^ in[4*2+i]; + t02 = ~ in[4*2+i]; + t03 = in[4*1+i] & t01; + t04 = in[4*1+i] | t02; + t05 = in[4*3+i] | t03; + t06 = in[4*1+i] ^ in[4*3+i]; + t07 = in[4*0+i] & t04; + t08 = in[4*0+i] | t02; + t09 = t07 ^ t05; + out[4*1+i] = t06 ^ t08; + out[4*0+i] = ~ t09; + t12 = in[4*1+i] & out[4*0+i]; + t13 = t01 & t05; + t14 = t01 ^ t12; + t15 = t07 ^ t13; + t16 = in[4*3+i] | t02; + t17 = in[4*0+i] ^ out[4*1+i]; + out[4*3+i] = t17 ^ t15; + out[4*2+i] = t16 ^ t14; +} + +/* S7: 1 13 15 0 14 8 2 11 7 4 12 10 9 3 5 6 */ + +/* depth = 10,7,10,4, Total gates=19 */ +static void sb7(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t05, t06, t08, t09, t10, t11, t13, t14, t15, t16, t17, t01; + t01 = in[4*0+i] & in[4*2+i]; + t02 = ~ in[4*3+i]; + t03 = in[4*0+i] & t02; + t04 = in[4*1+i] | t01; + t05 = in[4*0+i] & in[4*1+i]; + t06 = in[4*2+i] ^ t04; + out[4*3+i] = t03 ^ t06; + t08 = in[4*2+i] | out[4*3+i]; + t09 = in[4*3+i] | t05; + t10 = in[4*0+i] ^ t08; + t11 = t04 & out[4*3+i]; + out[4*1+i] = t09 ^ t10; + t13 = in[4*1+i] ^ out[4*1+i]; + t14 = t01 ^ out[4*1+i]; + t15 = in[4*2+i] ^ t05; + t16 = t11 | t13; + t17 = t02 | t14; + out[4*0+i] = t15 ^ t17; + out[4*2+i] = in[4*0+i] ^ t16; +} + +/* InvS7: 3 0 6 13 9 14 15 8 5 12 11 7 10 1 4 2 */ + +/* depth = 9,7,3,3, Total gates=18 */ +static void sb7_inv(uint8_t* out, const uint8_t* in, uint8_t i){ + uint8_t t02, t03, t04, t06, t07, t08, t09, t10, t11, t13, t14, t15, t16, t01; + t01 = in[4*0+i] & in[4*1+i]; + t02 = in[4*0+i] | in[4*1+i]; + t03 = in[4*2+i] | t01; + t04 = in[4*3+i] & t02; + out[4*3+i] = t03 ^ t04; + t06 = in[4*1+i] ^ t04; + t07 = in[4*3+i] ^ out[4*3+i]; + t08 = ~ t07; + t09 = t06 | t08; + t10 = in[4*1+i] ^ in[4*3+i]; + t11 = in[4*0+i] | in[4*3+i]; + out[4*1+i] = in[4*0+i] ^ t09; + t13 = in[4*2+i] ^ t06; + t14 = in[4*2+i] & t11; + t15 = in[4*3+i] | out[4*1+i]; + t16 = t01 | t10; + out[4*0+i] = t13 ^ t15; + out[4*2+i] = t14 ^ t16; +} + +typedef void(*sb_fpt)(uint8_t*, const uint8_t*, uint8_t i); + +sb_fpt sf_tab[] PROGMEM = { + sb0, sb1, sb2, sb3, + sb4, sb5, sb6, sb7 +}; + +sb_fpt sinvf_tab[] PROGMEM = { + sb0_inv, sb1_inv, sb2_inv, sb3_inv, + sb4_inv, sb5_inv, sb6_inv, sb7_inv +}; + +void sbox128(void * w, uint8_t box){ + uint8_t i, buffer[16]; + box &= 0x7; + + sb_fpt fp; + fp = (sb_fpt)pgm_read_word(&(sf_tab[box])); + for(i=0; i<4; ++i){ + fp(buffer, (uint8_t*)w, i); + } + memcpy(w, buffer, 16); +} + +void inv_sbox128(void * w, uint8_t box){ + uint8_t i, buffer[16]; + box &= 0x7; + + sb_fpt fp; + fp = (sb_fpt)pgm_read_word(&(sinvf_tab[box])); + for(i=0; i<4; ++i){ + fp(buffer, (uint8_t*)w, i); + } + memcpy(w, buffer, 16); +} + + + + + + + + diff --git a/serpent-sboxes.c b/serpent-sboxes.c new file mode 100644 index 0000000..8e54f6a --- /dev/null +++ b/serpent-sboxes.c @@ -0,0 +1,108 @@ +/* serpent-sboxes.c + * a non-bitsliced implementation of the serpent sboxes + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + */ + +#include +#include /* memset() */ +#include +#include "serpent-sboxes.h" + +uint8_t sbox[] PROGMEM = { + 0x38, 0xF1, 0xA6, 0x5B, 0xED, 0x42, 0x70, 0x9C, + 0xFC, 0x27, 0x90, 0x5A, 0x1B, 0xE8, 0x6D, 0x34, + 0x86, 0x79, 0x3C, 0xAF, 0xD1, 0xE4, 0x0B, 0x52, + 0x0F, 0xB8, 0xC9, 0x63, 0xD1, 0x24, 0xA7, 0x5E, + 0x1F, 0x83, 0xC0, 0xB6, 0x25, 0x4A, 0x9E, 0x7D, + 0xF5, 0x2B, 0x4A, 0x9C, 0x03, 0xE8, 0xD6, 0x71, + 0x72, 0xC5, 0x84, 0x6B, 0xE9, 0x1F, 0xD3, 0xA0, + 0x1D, 0xF0, 0xE8, 0x2B, 0x74, 0xCA, 0x93, 0x56, +/* now the inverted sboxes */ + 0xD3, 0xB0, 0xA6, 0x5C, 0x1E, 0x47, 0xF9, 0x82, + 0x58, 0x2E, 0xF6, 0xC3, 0xB4, 0x79, 0x1D, 0xA0, + 0xC9, 0xF4, 0xBE, 0x12, 0x03, 0x6D, 0x58, 0xA7, + 0x09, 0xA7, 0xBE, 0x6D, 0x35, 0xC2, 0x48, 0xF1, + 0x50, 0x83, 0xA9, 0x7E, 0x2C, 0xB6, 0x4F, 0xD1, + 0x8F, 0x29, 0x41, 0xDE, 0xB6, 0x53, 0x7C, 0xA0, + 0xFA, 0x1D, 0x53, 0x60, 0x49, 0xE7, 0x2C, 0x8B, + 0x30, 0x6D, 0x9E, 0xF8, 0x5C, 0xB7, 0xA1, 0x42 +}; + + +#define SHR_O(a) c=(a)&1; ((a) = (a)>>1) +#define SHR_I(a) ((a) = (c?0x80:0x00)| ((a)>>1)) + +static void ip(uint8_t *o, uint32_t *i){ + uint8_t c; // carry + uint8_t n,m; + memset(o, 0, 16); + for(n=0; n<16; ++n){ + for(m=0; m<2; ++m){ + SHR_O(i[0]); + SHR_I(o[n]); + SHR_O(i[1]); + SHR_I(o[n]); + SHR_O(i[2]); + SHR_I(o[n]); + SHR_O(i[3]); + SHR_I(o[n]); + } + } +} + +#undef SHR_I +#define SHR_I(a) ((a) = (c?0x80000000L:0x00L)| ((a)>>1)) /* we use 32-bit words here */ + + +static void fp(uint32_t *o, uint32_t *i){ + uint8_t c; // carry + uint8_t n,m; + memset(o, 0, 16); + for(n=0; n<4; ++n){ + for(m=0; m<8; ++m){ + SHR_O(i[n]); + SHR_I(o[0]); + SHR_O(i[n]); + SHR_I(o[1]); + SHR_O(i[n]); + SHR_I(o[2]); + SHR_O(i[n]); + SHR_I(o[3]); + } + } +} + +/******************************************************************************/ + +static void sbox128x(uint8_t box, void* w){ + uint8_t sb[16]; + uint8_t i,t,x; + box &= 0x0f; + /* load sbox */ + for(i=0; i<8; ++i){ + t = pgm_read_byte(sbox + box*8 + i); + sb[2*i+0]=t>>4; + sb[2*i+1]=t&0xf; + } + uint8_t o[16]; + ip(o, w); + + for(i=0; i<16; ++i){ + t = ((uint8_t*)o)[i]; + x = sb[t>>4]; + x <<= 4; + x |= sb[t&0xf]; + ((uint8_t*)o)[i] = x; + } + fp(w, (uint32_t*)o); +} + +void sbox128(void * w, uint8_t box){ + sbox128x(box&0x7, w); +} + +void inv_sbox128(void * w, uint8_t box){ + sbox128x(((box&0x7)|0x8), w); +} diff --git a/serpent-sboxes.h b/serpent-sboxes.h new file mode 100644 index 0000000..d44309d --- /dev/null +++ b/serpent-sboxes.h @@ -0,0 +1,10 @@ +#ifndef SERPENTSBOXES_H_ +#define SERPENTSBOXES_H_ + +#include + +void sbox128(void * w, uint8_t box); +void inv_sbox128(void * w, uint8_t box); + + +#endif /*SERPENTSBOXES_H_*/ diff --git a/serpent.c b/serpent.c index 85cf4a8..91e5158 100644 --- a/serpent.c +++ b/serpent.c @@ -9,103 +9,7 @@ #include /* memset() */ #include #include "serpent.h" - -uint8_t sbox[] PROGMEM = { - 0x38, 0xF1, 0xA6, 0x5B, 0xED, 0x42, 0x70, 0x9C, - 0xFC, 0x27, 0x90, 0x5A, 0x1B, 0xE8, 0x6D, 0x34, - 0x86, 0x79, 0x3C, 0xAF, 0xD1, 0xE4, 0x0B, 0x52, - 0x0F, 0xB8, 0xC9, 0x63, 0xD1, 0x24, 0xA7, 0x5E, - 0x1F, 0x83, 0xC0, 0xB6, 0x25, 0x4A, 0x9E, 0x7D, - 0xF5, 0x2B, 0x4A, 0x9C, 0x03, 0xE8, 0xD6, 0x71, - 0x72, 0xC5, 0x84, 0x6B, 0xE9, 0x1F, 0xD3, 0xA0, - 0x1D, 0xF0, 0xE8, 0x2B, 0x74, 0xCA, 0x93, 0x56, -/* now the inverted sboxes */ - 0xD3, 0xB0, 0xA6, 0x5C, 0x1E, 0x47, 0xF9, 0x82, - 0x58, 0x2E, 0xF6, 0xC3, 0xB4, 0x79, 0x1D, 0xA0, - 0xC9, 0xF4, 0xBE, 0x12, 0x03, 0x6D, 0x58, 0xA7, - 0x09, 0xA7, 0xBE, 0x6D, 0x35, 0xC2, 0x48, 0xF1, - 0x50, 0x83, 0xA9, 0x7E, 0x2C, 0xB6, 0x4F, 0xD1, - 0x8F, 0x29, 0x41, 0xDE, 0xB6, 0x53, 0x7C, 0xA0, - 0xFA, 0x1D, 0x53, 0x60, 0x49, 0xE7, 0x2C, 0x8B, - 0x30, 0x6D, 0x9E, 0xF8, 0x5C, 0xB7, 0xA1, 0x42 -}; - - -#define SHR_O(a) c=(a)&1; ((a) = (a)>>1) -#define SHR_I(a) ((a) = (c?0x80:0x00)| ((a)>>1)) - -static void ip(uint8_t *o, uint32_t *i){ - uint8_t c; // carry - uint8_t n,m; - memset(o, 0, 16); - for(n=0; n<16; ++n){ - for(m=0; m<2; ++m){ - SHR_O(i[0]); - SHR_I(o[n]); - SHR_O(i[1]); - SHR_I(o[n]); - SHR_O(i[2]); - SHR_I(o[n]); - SHR_O(i[3]); - SHR_I(o[n]); - } - } -} - -#undef SHR_I -#define SHR_I(a) ((a) = (c?0x80000000L:0x00L)| ((a)>>1)) /* we use 32-bit words here */ - - -static void fp(uint32_t *o, uint32_t *i){ - uint8_t c; // carry - uint8_t n,m; - memset(o, 0, 16); - for(n=0; n<4; ++n){ - for(m=0; m<8; ++m){ - SHR_O(i[n]); - SHR_I(o[0]); - SHR_O(i[n]); - SHR_I(o[1]); - SHR_O(i[n]); - SHR_I(o[2]); - SHR_O(i[n]); - SHR_I(o[3]); - } - } -} - -/******************************************************************************/ - -static void sbox128x(uint8_t box, void* w){ - uint8_t sb[16]; - uint8_t i,t,x; - box &= 0x0f; - /* load sbox */ - for(i=0; i<8; ++i){ - t = pgm_read_byte(sbox + box*8 + i); - sb[2*i+0]=t>>4; - sb[2*i+1]=t&0xf; - } - uint8_t o[16]; - ip(o, w); - - for(i=0; i<16; ++i){ - t = ((uint8_t*)o)[i]; - x = sb[t>>4]; - x <<= 4; - x |= sb[t&0xf]; - ((uint8_t*)o)[i] = x; - } - fp(w, (uint32_t*)o); -} - -static void sbox128(void * w, uint8_t box){ - sbox128x(box&0x7, w); -} - -static void inv_sbox128(void * w, uint8_t box){ - sbox128x(((box&0x7)|0x8), w); -} +#include "serpent-sboxes.h" /******************************************************************************/ diff --git a/serpent.mk b/serpent.mk index eb55908..5975ec0 100644 --- a/serpent.mk +++ b/serpent.mk @@ -5,9 +5,9 @@ ALGO_NAME := SERPENT BLOCK_CIPHERS += $(ALGO_NAME) -$(ALGO_NAME)_OBJ := serpent.o +$(ALGO_NAME)_OBJ := serpent.o serpent-sboxes-bitslice.o $(ALGO_NAME)_TEST_BIN := main-serpent-test.o debug.o uart.o serial-tools.o \ - serpent.o nessie_bc_test.o + serpent.o serpent-sboxes-bitslice.o nessie_bc_test.o $(ALGO_NAME)_NESSIE_TEST := "nessie" $(ALGO_NAME)_PEROFRMANCE_TEST := "performance"