From 66a3ffe2489727e85462e60e6dcec5dad1f5b20d Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Sat, 23 Mar 2013 20:52:54 +0100 Subject: [PATCH] unrolling loops in aesCCMencrypt() and aesCCMdecrypt() do the xoring word wise for 16-byte block instead of doing it bytewise in a loop. --- sys/src/9/pc/wifi.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/sys/src/9/pc/wifi.c b/sys/src/9/pc/wifi.c index 8b3748f71..938df8dbb 100644 --- a/sys/src/9/pc/wifi.c +++ b/sys/src/9/pc/wifi.c @@ -1161,11 +1161,18 @@ aesCCMencrypt(int L, int M, uchar *N /* N[15-L] */, xblock(L, M, N, a, la, lm, t, s); - for(i = 1; lm >= 16; i++, lm -= 16){ - for(p = sblock(L, N, i, b, s), x = t; p < &b[16]; x++, m++, p++){ - *x ^= *m; - *m ^= *p; - } + for(i = 1; lm >= 16; i++, m += 16, lm -= 16){ + sblock(L, N, i, b, s); + + *((u32int*)&t[0]) ^= *((u32int*)&m[0]); + *((u32int*)&m[0]) ^= *((u32int*)&b[0]); + *((u32int*)&t[4]) ^= *((u32int*)&m[4]); + *((u32int*)&m[4]) ^= *((u32int*)&b[4]); + *((u32int*)&t[8]) ^= *((u32int*)&m[8]); + *((u32int*)&m[8]) ^= *((u32int*)&b[8]); + *((u32int*)&t[12]) ^= *((u32int*)&m[12]); + *((u32int*)&m[12]) ^= *((u32int*)&b[12]); + aes_encrypt(s->ekey, s->rounds, t, t); } if(lm > 0){ @@ -1193,11 +1200,18 @@ aesCCMdecrypt(int L, int M, uchar *N /* N[15-L] */, xblock(L, M, N, a, la, lm, t, s); - for(i = 1; lm >= 16; i++, lm -= 16){ - for(p = sblock(L, N, i, b, s), x = t; p < &b[16]; x++, m++, p++){ - *m ^= *p; - *x ^= *m; - } + for(i = 1; lm >= 16; i++, m += 16, lm -= 16){ + sblock(L, N, i, b, s); + + *((u32int*)&m[0]) ^= *((u32int*)&b[0]); + *((u32int*)&t[0]) ^= *((u32int*)&m[0]); + *((u32int*)&m[4]) ^= *((u32int*)&b[4]); + *((u32int*)&t[4]) ^= *((u32int*)&m[4]); + *((u32int*)&m[8]) ^= *((u32int*)&b[8]); + *((u32int*)&t[8]) ^= *((u32int*)&m[8]); + *((u32int*)&m[12]) ^= *((u32int*)&b[12]); + *((u32int*)&t[12]) ^= *((u32int*)&m[12]); + aes_encrypt(s->ekey, s->rounds, t, t); } if(lm > 0){