From af012bab8a421e734c91ee026e085657ddab0da5 Mon Sep 17 00:00:00 2001 From: Raymond Pasco Date: Tue, 24 May 2016 12:02:06 -0400 Subject: [PATCH] Jet AES ECB and CBC modes --- Makefile | 2 + include/jets/q.h | 14 ++ include/jets/w.h | 14 ++ jets/e/aes_cbc.c | 548 +++++++++++++++++++++++++++++++++++++++++++++++ jets/e/aes_ecb.c | 407 +++++++++++++++++++++++++++++++++++ jets/tree.c | 68 ++++++ 6 files changed, 1053 insertions(+) create mode 100644 jets/e/aes_cbc.c create mode 100644 jets/e/aes_ecb.c diff --git a/Makefile b/Makefile index 57658c7c45..1655fa28d7 100644 --- a/Makefile +++ b/Makefile @@ -244,6 +244,8 @@ J_D_OFILES=\ jets/d/by_dif.o J_E_OFILES=\ + jets/e/aes_ecb.o \ + jets/e/aes_cbc.o \ jets/e/aesc.o \ jets/e/cue.o \ jets/e/fl.o \ diff --git a/include/jets/q.h b/include/jets/q.h index d508712eac..5ddeb7904b 100644 --- a/include/jets/q.h +++ b/include/jets/q.h @@ -102,6 +102,20 @@ u3_noun u3qe_rexp(u3_noun, u3_noun); u3_noun u3qe_trip(u3_atom); + u3_noun u3qea_ecba_en(u3_atom, u3_atom); + u3_noun u3qea_ecba_de(u3_atom, u3_atom); + u3_noun u3qea_ecbb_en(u3_atom, u3_atom); + u3_noun u3qea_ecbb_de(u3_atom, u3_atom); + u3_noun u3qea_ecbc_en(u3_atom, u3_atom); + u3_noun u3qea_ecbc_de(u3_atom, u3_atom); + + u3_noun u3qea_cbca_en(u3_atom, u3_atom, u3_atom); + u3_noun u3qea_cbca_de(u3_atom, u3_atom, u3_atom); + u3_noun u3qea_cbcb_en(u3_atom, u3_atom, u3_atom); + u3_noun u3qea_cbcb_de(u3_atom, u3_atom, u3_atom); + u3_noun u3qea_cbcc_en(u3_atom, u3_atom, u3_atom); + u3_noun u3qea_cbcc_de(u3_atom, u3_atom, u3_atom); + u3_noun u3qea_de(u3_atom, u3_atom); u3_noun u3qea_en(u3_atom, u3_atom); diff --git a/include/jets/w.h b/include/jets/w.h index 7075c60472..6e28e2e565 100644 --- a/include/jets/w.h +++ b/include/jets/w.h @@ -114,6 +114,20 @@ u3_noun u3we_pose(u3_noun); u3_noun u3we_sfix(u3_noun); + u3_noun u3wea_ecba_en(u3_noun); + u3_noun u3wea_ecba_de(u3_noun); + u3_noun u3wea_ecbb_en(u3_noun); + u3_noun u3wea_ecbb_de(u3_noun); + u3_noun u3wea_ecbc_en(u3_noun); + u3_noun u3wea_ecbc_de(u3_noun); + + u3_noun u3wea_cbca_en(u3_noun); + u3_noun u3wea_cbca_de(u3_noun); + u3_noun u3wea_cbcb_en(u3_noun); + u3_noun u3wea_cbcb_de(u3_noun); + u3_noun u3wea_cbcc_en(u3_noun); + u3_noun u3wea_cbcc_de(u3_noun); + u3_noun u3wea_de(u3_noun); u3_noun u3wea_en(u3_noun); diff --git a/jets/e/aes_cbc.c b/jets/e/aes_cbc.c new file mode 100644 index 0000000000..81233e5068 --- /dev/null +++ b/jets/e/aes_cbc.c @@ -0,0 +1,548 @@ +/* j/5/aes_cbc.c +** +*/ +#include "all.h" + +#include + +/* functions +*/ + u3_noun + u3qea_cbca_en(u3_atom key, + u3_atom iv, + u3_atom msg) + { + c3_y key_y[16]; + c3_y iv_y[16]; + c3_w len_msg_w; + c3_w len_out_w; + c3_y *msg_y; + c3_y *out_y; + u3_atom out; + AES_KEY key_u; + + c3_assert(u3r_met(3, key) <= 16); + c3_assert(u3r_met(3, iv) <= 16); + len_msg_w = u3r_met(3, msg); + len_out_w = (len_msg_w % 16) == 0 ? len_msg_w : len_msg_w + (16 - (len_msg_w % 16)); + len_msg_w = len_out_w; + + msg_y = u3a_malloc(len_msg_w); + out_y = u3a_malloc(len_out_w); + + { + int i = 15; + + do { + key_y[i] = u3r_byte(15-i, key); + i--; + } while (i >= 0); + } + { + int i = 15; + + do { + iv_y[i] = u3r_byte(15-i, iv); + i--; + } while (i >= 0); + } + { + int i = len_msg_w - 1; + + do { + msg_y[i] = u3r_byte((len_msg_w - 1)-i, msg); + i--; + } while (i >= 0); + } + + if ( 0 != AES_set_encrypt_key(key_y, 128, &key_u) ) { + return u3m_bail(c3__exit); + } + else { + AES_cbc_encrypt(msg_y, out_y, len_msg_w, &key_u, iv_y, AES_ENCRYPT); + } + + /* array reverse - we can write backwards u3i_bytes * + * in the unlikely event that this becomes a problem */ + { + int i = len_out_w - 1; + int j = 0; + c3_y tmp; + + do { + tmp = out_y[i]; + out_y[i] = out_y[j]; + out_y[j] = tmp; + i--; j++; + } while (i > j); + } + + out = u3i_bytes(len_out_w, out_y); + u3a_free(msg_y); + u3a_free(out_y); + return out; + } + + u3_noun + u3wea_cbca_en(u3_noun cor) + { + u3_noun a, b, c; + + if ( c3n == u3r_mean(cor, u3x_sam, &c, 60, &a, 61, &b, 0) || + c3n == u3ud(a) || + c3n == u3ud(b) ) { + return u3m_bail(c3__exit); + } else { + return u3qea_cbca_en(a, b, c); + } + } + + u3_noun + u3qea_cbca_de(u3_atom key, + u3_atom iv, + u3_atom msg) + { + c3_y key_y[16]; + c3_y iv_y[16]; + c3_w len_msg_w; + c3_w len_out_w; + c3_y *msg_y; + c3_y *out_y; + u3_atom out; + AES_KEY key_u; + + c3_assert(u3r_met(3, key) <= 16); + c3_assert(u3r_met(3, iv) <= 16); + len_msg_w = u3r_met(3, msg); + len_out_w = (len_msg_w % 16) == 0 ? len_msg_w : len_msg_w + (16 - (len_msg_w % 16)); + len_msg_w = len_out_w; + + msg_y = u3a_malloc(len_msg_w); + out_y = u3a_malloc(len_out_w); + + { + int i = 15; + + do { + key_y[i] = u3r_byte(15-i, key); + i--; + } while (i >= 0); + } + { + int i = 15; + + do { + iv_y[i] = u3r_byte(15-i, iv); + i--; + } while (i >= 0); + } + { + int i = len_msg_w - 1; + + do { + msg_y[i] = u3r_byte((len_msg_w - 1)-i, msg); + i--; + } while (i >= 0); + } + + if ( 0 != AES_set_decrypt_key(key_y, 128, &key_u) ) { + return u3m_bail(c3__exit); + } + else { + AES_cbc_encrypt(msg_y, out_y, len_msg_w, &key_u, iv_y, AES_DECRYPT); + } + + /* array reverse - we can write backwards u3i_bytes * + * in the unlikely event that this becomes a problem */ + { + int i = len_out_w - 1; + int j = 0; + c3_y tmp; + + do { + tmp = out_y[i]; + out_y[i] = out_y[j]; + out_y[j] = tmp; + i--; j++; + } while (i > j); + } + + out = u3i_bytes(len_out_w, out_y); + u3a_free(msg_y); + u3a_free(out_y); + return out; + } + + u3_noun + u3wea_cbca_de(u3_noun cor) + { + u3_noun a, b, c; + + if ( c3n == u3r_mean(cor, u3x_sam, &c, 60, &a, 61, &b, 0) || + c3n == u3ud(a) || + c3n == u3ud(b) ) { + return u3m_bail(c3__exit); + } else { + return u3qea_cbca_de(a, b, c); + } + } + + u3_noun + u3qea_cbcb_en(u3_atom key, + u3_atom iv, + u3_atom msg) + { + c3_y key_y[24]; + c3_y iv_y[16]; + c3_w len_msg_w; + c3_w len_out_w; + c3_y *msg_y; + c3_y *out_y; + u3_atom out; + AES_KEY key_u; + + c3_assert(u3r_met(3, key) <= 24); + c3_assert(u3r_met(3, iv) <= 16); + len_msg_w = u3r_met(3, msg); + len_out_w = (len_msg_w % 16) == 0 ? len_msg_w : len_msg_w + (16 - (len_msg_w % 16)); + len_msg_w = len_out_w; + + msg_y = u3a_malloc(len_msg_w); + out_y = u3a_malloc(len_out_w); + + { + int i = 23; + + do { + key_y[i] = u3r_byte(23-i, key); + i--; + } while (i >= 0); + } + { + int i = 15; + + do { + iv_y[i] = u3r_byte(15-i, iv); + i--; + } while (i >= 0); + } + { + int i = len_msg_w - 1; + + do { + msg_y[i] = u3r_byte((len_msg_w - 1)-i, msg); + i--; + } while (i >= 0); + } + + if ( 0 != AES_set_encrypt_key(key_y, 192, &key_u) ) { + return u3m_bail(c3__exit); + } + else { + AES_cbc_encrypt(msg_y, out_y, len_msg_w, &key_u, iv_y, AES_ENCRYPT); + } + + /* array reverse - we can write backwards u3i_bytes * + * in the unlikely event that this becomes a problem */ + { + int i = len_out_w - 1; + int j = 0; + c3_y tmp; + + do { + tmp = out_y[i]; + out_y[i] = out_y[j]; + out_y[j] = tmp; + i--; j++; + } while (i > j); + } + + out = u3i_bytes(len_out_w, out_y); + u3a_free(msg_y); + u3a_free(out_y); + return out; + } + + u3_noun + u3wea_cbcb_en(u3_noun cor) + { + u3_noun a, b, c; + + if ( c3n == u3r_mean(cor, u3x_sam, &c, 60, &a, 61, &b, 0) || + c3n == u3ud(a) || + c3n == u3ud(b) ) { + return u3m_bail(c3__exit); + } else { + return u3qea_cbcb_en(a, b, c); + } + } + + u3_noun + u3qea_cbcb_de(u3_atom key, + u3_atom iv, + u3_atom msg) + { + c3_y key_y[24]; + c3_y iv_y[16]; + c3_w len_msg_w; + c3_w len_out_w; + c3_y *msg_y; + c3_y *out_y; + u3_atom out; + AES_KEY key_u; + + c3_assert(u3r_met(3, key) <= 24); + c3_assert(u3r_met(3, iv) <= 16); + len_msg_w = u3r_met(3, msg); + len_out_w = (len_msg_w % 16) == 0 ? len_msg_w : len_msg_w + (16 - (len_msg_w % 16)); + len_msg_w = len_out_w; + + msg_y = u3a_malloc(len_msg_w); + out_y = u3a_malloc(len_out_w); + + { + int i = 23; + + do { + key_y[i] = u3r_byte(23-i, key); + i--; + } while (i >= 0); + } + { + int i = 15; + + do { + iv_y[i] = u3r_byte(15-i, iv); + i--; + } while (i >= 0); + } + { + int i = len_msg_w - 1; + + do { + msg_y[i] = u3r_byte((len_msg_w - 1)-i, msg); + i--; + } while (i >= 0); + } + + if ( 0 != AES_set_decrypt_key(key_y, 192, &key_u) ) { + return u3m_bail(c3__exit); + } + else { + AES_cbc_encrypt(msg_y, out_y, len_msg_w, &key_u, iv_y, AES_DECRYPT); + } + + /* array reverse - we can write backwards u3i_bytes * + * in the unlikely event that this becomes a problem */ + { + int i = len_out_w - 1; + int j = 0; + c3_y tmp; + + do { + tmp = out_y[i]; + out_y[i] = out_y[j]; + out_y[j] = tmp; + i--; j++; + } while (i > j); + } + + out = u3i_bytes(len_out_w, out_y); + u3a_free(msg_y); + u3a_free(out_y); + return out; + } + + u3_noun + u3wea_cbcb_de(u3_noun cor) + { + u3_noun a, b, c; + + if ( c3n == u3r_mean(cor, u3x_sam, &c, 60, &a, 61, &b, 0) || + c3n == u3ud(a) || + c3n == u3ud(b) ) { + return u3m_bail(c3__exit); + } else { + return u3qea_cbcb_de(a, b, c); + } + } + + u3_noun + u3qea_cbcc_en(u3_atom key, + u3_atom iv, + u3_atom msg) + { + c3_y key_y[32]; + c3_y iv_y[16]; + c3_w len_msg_w; + c3_w len_out_w; + c3_y *msg_y; + c3_y *out_y; + u3_atom out; + AES_KEY key_u; + + c3_assert(u3r_met(3, key) <= 32); + c3_assert(u3r_met(3, iv) <= 16); + len_msg_w = u3r_met(3, msg); + len_out_w = (len_msg_w % 16) == 0 ? len_msg_w : len_msg_w + (16 - (len_msg_w % 16)); + len_msg_w = len_out_w; + + msg_y = u3a_malloc(len_msg_w); + out_y = u3a_malloc(len_out_w); + + { + int i = 31; + + do { + key_y[i] = u3r_byte(31-i, key); + i--; + } while (i >= 0); + } + { + int i = 15; + + do { + iv_y[i] = u3r_byte(15-i, iv); + i--; + } while (i >= 0); + } + { + int i = len_msg_w - 1; + + do { + msg_y[i] = u3r_byte((len_msg_w - 1)-i, msg); + i--; + } while (i >= 0); + } + + if ( 0 != AES_set_encrypt_key(key_y, 256, &key_u) ) { + return u3m_bail(c3__exit); + } + else { + AES_cbc_encrypt(msg_y, out_y, len_msg_w, &key_u, iv_y, AES_ENCRYPT); + } + + /* array reverse - we can write backwards u3i_bytes * + * in the unlikely event that this becomes a problem */ + { + int i = len_out_w - 1; + int j = 0; + c3_y tmp; + + do { + tmp = out_y[i]; + out_y[i] = out_y[j]; + out_y[j] = tmp; + i--; j++; + } while (i > j); + } + + out = u3i_bytes(len_out_w, out_y); + u3a_free(msg_y); + u3a_free(out_y); + return out; + } + + u3_noun + u3wea_cbcc_en(u3_noun cor) + { + u3_noun a, b, c; + + if ( c3n == u3r_mean(cor, u3x_sam, &c, 60, &a, 61, &b, 0) || + c3n == u3ud(a) || + c3n == u3ud(b) ) { + return u3m_bail(c3__exit); + } else { + return u3qea_cbcc_en(a, b, c); + } + } + + u3_noun + u3qea_cbcc_de(u3_atom key, + u3_atom iv, + u3_atom msg) + { + c3_y key_y[32]; + c3_y iv_y[16]; + c3_w len_msg_w; + c3_w len_out_w; + c3_y *msg_y; + c3_y *out_y; + u3_atom out; + AES_KEY key_u; + + c3_assert(u3r_met(3, key) <= 32); + c3_assert(u3r_met(3, iv) <= 16); + len_msg_w = u3r_met(3, msg); + len_out_w = (len_msg_w % 16) == 0 ? len_msg_w : len_msg_w + (16 - (len_msg_w % 16)); + len_msg_w = len_out_w; + + msg_y = u3a_malloc(len_msg_w); + out_y = u3a_malloc(len_out_w); + + { + int i = 31; + + do { + key_y[i] = u3r_byte(31-i, key); + i--; + } while (i >= 0); + } + { + int i = 15; + + do { + iv_y[i] = u3r_byte(15-i, iv); + i--; + } while (i >= 0); + } + { + int i = len_msg_w - 1; + + do { + msg_y[i] = u3r_byte((len_msg_w - 1)-i, msg); + i--; + } while (i >= 0); + } + + if ( 0 != AES_set_decrypt_key(key_y, 256, &key_u) ) { + return u3m_bail(c3__exit); + } + else { + AES_cbc_encrypt(msg_y, out_y, len_msg_w, &key_u, iv_y, AES_DECRYPT); + } + + /* array reverse - we can write backwards u3i_bytes * + * in the unlikely event that this becomes a problem */ + { + int i = len_out_w - 1; + int j = 0; + c3_y tmp; + + do { + tmp = out_y[i]; + out_y[i] = out_y[j]; + out_y[j] = tmp; + i--; j++; + } while (i > j); + } + + out = u3i_bytes(len_out_w, out_y); + u3a_free(msg_y); + u3a_free(out_y); + return out; + } + + u3_noun + u3wea_cbcc_de(u3_noun cor) + { + u3_noun a, b, c; + + if ( c3n == u3r_mean(cor, u3x_sam, &c, 60, &a, 61, &b, 0) || + c3n == u3ud(a) || + c3n == u3ud(b) ) { + return u3m_bail(c3__exit); + } else { + return u3qea_cbcc_de(a, b, c); + } + } diff --git a/jets/e/aes_ecb.c b/jets/e/aes_ecb.c new file mode 100644 index 0000000000..d90144321f --- /dev/null +++ b/jets/e/aes_ecb.c @@ -0,0 +1,407 @@ +/* j/5/aes_ecb.c +** +*/ +#include "all.h" + +#include + +/* functions +*/ + u3_noun + u3qea_ecba_en(u3_atom key, + u3_atom blk) + { + c3_y key_y[16]; + c3_y blk_y[16]; + AES_KEY key_u; + + c3_assert(u3r_met(3, key) <= 16); + c3_assert(u3r_met(3, blk) <= 16); + + { + int i = 15; + + do { + key_y[i] = u3r_byte(15-i, key); + i--; + } while (i >= 0); + } + { + int i = 15; + + do { + blk_y[i] = u3r_byte(15-i, blk); + i--; + } while (i >= 0); + } + + if ( 0 != AES_set_encrypt_key(key_y, 128, &key_u) ) { + return u3m_bail(c3__exit); + } + else { + AES_ecb_encrypt(blk_y, blk_y, &key_u, AES_ENCRYPT); + } + + /* array reverse - we can write backwards u3i_bytes * + * in the unlikely event that this becomes a problem */ + { + int i = 15; + int j = 0; + c3_y tmp; + + do { + tmp = blk_y[i]; + blk_y[i] = blk_y[j]; + blk_y[j] = tmp; + i--; j++; + } while (i > j); + } + + return u3i_bytes(16, blk_y); + } + + u3_noun + u3wea_ecba_en(u3_noun cor) + { + u3_noun a, b; + + if ( c3n == u3r_mean(cor, u3x_sam, &b, u3x_con_sam, &a, 0) || + c3n == u3ud(a) || + c3n == u3ud(b) ) { + return u3m_bail(c3__exit); + } else { + return u3qea_ecba_en(a, b); + } + } + + u3_noun + u3qea_ecba_de(u3_atom key, + u3_atom blk) + { + c3_y key_y[16]; + c3_y blk_y[16]; + AES_KEY key_u; + + c3_assert(u3r_met(3, key) <= 16); + c3_assert(u3r_met(3, blk) <= 16); + + { + int i = 15; + + do { + key_y[i] = u3r_byte(15-i, key); + i--; + } while (i >= 0); + } + { + int i = 15; + + do { + blk_y[i] = u3r_byte(15-i, blk); + i--; + } while (i >= 0); + } + + if ( 0 != AES_set_decrypt_key(key_y, 128, &key_u) ) { + return u3m_bail(c3__exit); + } + else { + AES_ecb_encrypt(blk_y, blk_y, &key_u, AES_DECRYPT); + } + + /* array reverse - we can write backwards u3i_bytes * + * in the unlikely event that this becomes a problem */ + { + int i = 15; + int j = 0; + c3_y tmp; + + do { + tmp = blk_y[i]; + blk_y[i] = blk_y[j]; + blk_y[j] = tmp; + i--; j++; + } while (i > j); + } + return u3i_bytes(16, blk_y); + } + + u3_noun + u3wea_ecba_de(u3_noun cor) + { + u3_noun a, b; + + if ( c3n == u3r_mean(cor, u3x_sam, &b, u3x_con_sam, &a, 0) || + c3n == u3ud(a) || + c3n == u3ud(b) ) { + return u3m_bail(c3__exit); + } else { + return u3qea_ecba_de(a, b); + } + } + + u3_noun + u3qea_ecbb_en(u3_atom key, + u3_atom blk) + { + c3_y key_y[24]; + c3_y blk_y[16]; + AES_KEY key_u; + + c3_assert(u3r_met(3, key) <= 24); + c3_assert(u3r_met(3, blk) <= 16); + + { + int i = 23; + + do { + key_y[i] = u3r_byte(23-i, key); + i--; + } while (i >= 0); + } + { + int i = 15; + + do { + blk_y[i] = u3r_byte(15-i, blk); + i--; + } while (i >= 0); + } + + if ( 0 != AES_set_encrypt_key(key_y, 192, &key_u) ) { + return u3m_bail(c3__exit); + } + else { + AES_ecb_encrypt(blk_y, blk_y, &key_u, AES_ENCRYPT); + } + + /* array reverse - we can write backwards u3i_bytes * + * in the unlikely event that this becomes a problem */ + { + int i = 15; + int j = 0; + c3_y tmp; + + do { + tmp = blk_y[i]; + blk_y[i] = blk_y[j]; + blk_y[j] = tmp; + i--; j++; + } while (i > j); + } + + return u3i_bytes(16, blk_y); + } + + u3_noun + u3wea_ecbb_en(u3_noun cor) + { + u3_noun a, b; + + if ( c3n == u3r_mean(cor, u3x_sam, &b, u3x_con_sam, &a, 0) || + c3n == u3ud(a) || + c3n == u3ud(b) ) { + return u3m_bail(c3__exit); + } else { + return u3qea_ecbb_en(a, b); + } + } + + u3_noun + u3qea_ecbb_de(u3_atom key, + u3_atom blk) + { + c3_y key_y[24]; + c3_y blk_y[16]; + AES_KEY key_u; + + c3_assert(u3r_met(3, key) <= 24); + c3_assert(u3r_met(3, blk) <= 16); + + { + int i = 23; + + do { + key_y[i] = u3r_byte(23-i, key); + i--; + } while (i >= 0); + } + { + int i = 15; + + do { + blk_y[i] = u3r_byte(15-i, blk); + i--; + } while (i >= 0); + } + + if ( 0 != AES_set_decrypt_key(key_y, 192, &key_u) ) { + return u3m_bail(c3__exit); + } + else { + AES_ecb_encrypt(blk_y, blk_y, &key_u, AES_DECRYPT); + } + + /* array reverse - we can write backwards u3i_bytes * + * in the unlikely event that this becomes a problem */ + { + int i = 15; + int j = 0; + c3_y tmp; + + do { + tmp = blk_y[i]; + blk_y[i] = blk_y[j]; + blk_y[j] = tmp; + i--; j++; + } while (i > j); + } + return u3i_bytes(16, blk_y); + } + + u3_noun + u3wea_ecbb_de(u3_noun cor) + { + u3_noun a, b; + + if ( c3n == u3r_mean(cor, u3x_sam, &b, u3x_con_sam, &a, 0) || + c3n == u3ud(a) || + c3n == u3ud(b) ) { + return u3m_bail(c3__exit); + } else { + return u3qea_ecbb_de(a, b); + } + } + + u3_noun + u3qea_ecbc_en(u3_atom key, + u3_atom blk) + { + c3_y key_y[32]; + c3_y blk_y[16]; + AES_KEY key_u; + + c3_assert(u3r_met(3, key) <= 32); + c3_assert(u3r_met(3, blk) <= 16); + + { + int i = 31; + + do { + key_y[i] = u3r_byte(31-i, key); + i--; + } while (i >= 0); + } + { + int i = 15; + + do { + blk_y[i] = u3r_byte(15-i, blk); + i--; + } while (i >= 0); + } + + if ( 0 != AES_set_encrypt_key(key_y, 256, &key_u) ) { + return u3m_bail(c3__exit); + } + else { + AES_ecb_encrypt(blk_y, blk_y, &key_u, AES_ENCRYPT); + } + + /* array reverse - we can write backwards u3i_bytes * + * in the unlikely event that this becomes a problem */ + { + int i = 15; + int j = 0; + c3_y tmp; + + do { + tmp = blk_y[i]; + blk_y[i] = blk_y[j]; + blk_y[j] = tmp; + i--; j++; + } while (i > j); + } + + return u3i_bytes(16, blk_y); + } + + u3_noun + u3wea_ecbc_en(u3_noun cor) + { + u3_noun a, b; + + if ( c3n == u3r_mean(cor, u3x_sam, &b, u3x_con_sam, &a, 0) || + c3n == u3ud(a) || + c3n == u3ud(b) ) { + return u3m_bail(c3__exit); + } else { + return u3qea_ecbc_en(a, b); + } + } + + u3_noun + u3qea_ecbc_de(u3_atom key, + u3_atom blk) + { + c3_y key_y[32]; + c3_y blk_y[16]; + AES_KEY key_u; + + c3_assert(u3r_met(3, key) <= 32); + c3_assert(u3r_met(3, blk) <= 16); + + { + int i = 31; + + do { + key_y[i] = u3r_byte(31-i, key); + i--; + } while (i >= 0); + } + { + int i = 15; + + do { + blk_y[i] = u3r_byte(15-i, blk); + i--; + } while (i >= 0); + } + + if ( 0 != AES_set_decrypt_key(key_y, 256, &key_u) ) { + return u3m_bail(c3__exit); + } + else { + AES_ecb_encrypt(blk_y, blk_y, &key_u, AES_DECRYPT); + } + + /* array reverse - we can write backwards u3i_bytes * + * in the unlikely event that this becomes a problem */ + { + int i = 15; + int j = 0; + c3_y tmp; + + do { + tmp = blk_y[i]; + blk_y[i] = blk_y[j]; + blk_y[j] = tmp; + i--; j++; + } while (i > j); + } + return u3i_bytes(16, blk_y); + } + + u3_noun + u3wea_ecbc_de(u3_noun cor) + { + u3_noun a, b; + + if ( c3n == u3r_mean(cor, u3x_sam, &b, u3x_con_sam, &a, 0) || + c3n == u3ud(a) || + c3n == u3ud(b) ) { + return u3m_bail(c3__exit); + } else { + return u3qea_ecbc_de(a, b); + } + } diff --git a/jets/tree.c b/jets/tree.c index d7bb3a6b44..5341351f8d 100644 --- a/jets/tree.c +++ b/jets/tree.c @@ -412,8 +412,76 @@ static u3j_core _down_d[] = {} }; +static u3j_harm _zuse__aes_ecba_en_a[] = {{".2", u3wea_ecba_en}, {}}; +static u3j_harm _zuse__aes_ecba_de_a[] = {{".2", u3wea_ecba_de}, {}}; +static u3j_harm _zuse__aes_ecbb_en_a[] = {{".2", u3wea_ecbb_en}, {}}; +static u3j_harm _zuse__aes_ecbb_de_a[] = {{".2", u3wea_ecbb_de}, {}}; +static u3j_harm _zuse__aes_ecbc_en_a[] = {{".2", u3wea_ecbc_en}, {}}; +static u3j_harm _zuse__aes_ecbc_de_a[] = {{".2", u3wea_ecbc_de}, {}}; + +static u3j_harm _zuse__aes_cbca_en_a[] = {{".2", u3wea_cbca_en}, {}}; +static u3j_harm _zuse__aes_cbca_de_a[] = {{".2", u3wea_cbca_de}, {}}; +static u3j_harm _zuse__aes_cbcb_en_a[] = {{".2", u3wea_cbcb_en}, {}}; +static u3j_harm _zuse__aes_cbcb_de_a[] = {{".2", u3wea_cbcb_de}, {}}; +static u3j_harm _zuse__aes_cbcc_en_a[] = {{".2", u3wea_cbcc_en}, {}}; +static u3j_harm _zuse__aes_cbcc_de_a[] = {{".2", u3wea_cbcc_de}, {}}; + +static u3j_core _zuse__aes_ecba_d[] = + { { "en", _zuse__aes_ecba_en_a }, + { "de", _zuse__aes_ecba_de_a }, + {} + }; + +static u3j_core _zuse__aes_ecbb_d[] = + { { "en", _zuse__aes_ecbb_en_a }, + { "de", _zuse__aes_ecbb_de_a }, + {} + }; + +static u3j_core _zuse__aes_ecbc_d[] = + { { "en", _zuse__aes_ecbc_en_a }, + { "de", _zuse__aes_ecbc_de_a }, + {} + }; + +static u3j_core _zuse__aes_cbca_d[] = + { { "en", _zuse__aes_cbca_en_a }, + { "de", _zuse__aes_cbca_de_a }, + {} + }; + +static u3j_core _zuse__aes_cbcb_d[] = + { { "en", _zuse__aes_cbcb_en_a }, + { "de", _zuse__aes_cbcb_de_a }, + {} + }; + +static u3j_core _zuse__aes_cbcc_d[] = + { { "en", _zuse__aes_cbcc_en_a }, + { "de", _zuse__aes_cbcc_de_a }, + {} + }; + +static u3j_core _zuse__aes_d[] = + { { "ecba", 0, _zuse__aes_ecba_d }, + { "ecbb", 0, _zuse__aes_ecbb_d }, + { "ecbc", 0, _zuse__aes_ecbc_d }, + { "cbca", 0, _zuse__aes_cbca_d }, + { "cbcb", 0, _zuse__aes_cbcb_d }, + { "cbcc", 0, _zuse__aes_cbcc_d }, + {} + }; + +/* +static u3j_core _zuse_d[] = + { { "aes", 0, _zuse__aes_d }, + {} + }; +*/ + static u3j_core _arvo_d[] = { { "down", 0, _down_d }, + { "aes", 0, _zuse__aes_d }, {} };