aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/bitsliced-aes/aes.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/monniaux/bitsliced-aes/aes.c')
-rw-r--r--test/monniaux/bitsliced-aes/aes.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/test/monniaux/bitsliced-aes/aes.c b/test/monniaux/bitsliced-aes/aes.c
new file mode 100644
index 00000000..d4474e14
--- /dev/null
+++ b/test/monniaux/bitsliced-aes/aes.c
@@ -0,0 +1,129 @@
+
+#include <string.h>
+#include "aes.h"
+#include "bs.h"
+#include "utils.h"
+
+void aes_ecb_encrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key)
+{
+ word_t input_space[BLOCK_SIZE];
+ word_t rk[11][BLOCK_SIZE];
+
+ memset(outputb,0,size);
+ word_t * state = (word_t *)outputb;
+
+ bs_expand_key(rk, key);
+
+ while (size > 0)
+ {
+ if (size < BS_BLOCK_SIZE)
+ {
+ memset(input_space,0,BS_BLOCK_SIZE);
+ memmove(input_space, inputb, size);
+ bs_cipher(input_space,rk);
+ memmove(outputb, input_space, size);
+ size = 0;
+ state += size;
+ }
+ else
+ {
+ memmove(state,inputb,BS_BLOCK_SIZE);
+ bs_cipher(state,rk);
+ size -= BS_BLOCK_SIZE;
+ state += BS_BLOCK_SIZE;
+ }
+
+ }
+}
+
+void aes_ecb_decrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key)
+{
+ word_t input_space[BLOCK_SIZE];
+ word_t rk[11][BLOCK_SIZE];
+
+ memset(outputb,0,size);
+ word_t * state = (word_t *)outputb;
+
+ bs_expand_key(rk, key);
+
+ while (size > 0)
+ {
+ if (size < BS_BLOCK_SIZE)
+ {
+ memset(input_space,0,BS_BLOCK_SIZE);
+ memmove(input_space, inputb, size);
+ bs_cipher_rev(input_space,rk);
+ memmove(outputb, input_space, size);
+ size = 0;
+ state += size;
+ }
+ else
+ {
+ memmove(state,inputb,BS_BLOCK_SIZE);
+ bs_cipher_rev(state,rk);
+ size -= BS_BLOCK_SIZE;
+ state += BS_BLOCK_SIZE;
+ }
+
+ }
+}
+
+static void INC_CTR(uint8_t * ctr, uint8_t i)
+{
+ ctr += BLOCK_SIZE/8 - 1;
+ uint8_t n = *(ctr);
+ *ctr += i;
+ while(*ctr < n)
+ {
+ ctr--;
+ n = *ctr;
+ (*ctr)++;
+ }
+}
+
+void aes_ctr_encrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key, uint8_t * iv)
+{
+ word_t rk[11][BLOCK_SIZE];
+ word_t ctr[BLOCK_SIZE];
+ uint8_t iv_copy[BLOCK_SIZE/8];
+
+ memset(outputb,0,size);
+ memset(ctr,0,sizeof(ctr));
+ memmove(iv_copy,iv,BLOCK_SIZE/8);
+
+ word_t * state = (word_t *)outputb;
+ bs_expand_key(rk, key);
+
+ do
+ {
+ int chunk = MIN(size, BS_BLOCK_SIZE);
+ int blocks = chunk / (BLOCK_SIZE/8);
+ if (chunk % (BLOCK_SIZE/8))
+ {
+ blocks++;
+ }
+
+ int i;
+ for (i = 0; i < blocks; i++)
+ {
+ memmove(ctr + (i * WORDS_PER_BLOCK), iv_copy, BLOCK_SIZE/8);
+ INC_CTR(iv_copy,1);
+ }
+
+ bs_cipher(ctr, rk);
+ size -= chunk;
+
+ uint8_t * ctr_p = (uint8_t *) ctr;
+ while(chunk--)
+ {
+ *outputb++ = *ctr_p++ ^ *inputb++;
+ }
+
+ }
+ while(size);
+
+}
+
+
+
+