diff --git a/aes-32bit/aes.cpp b/aes-32bit/aes.cpp index 5ab5990..5713875 100644 --- a/aes-32bit/aes.cpp +++ b/aes-32bit/aes.cpp @@ -10,6 +10,8 @@ AES specification: http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf */ +#define WORD(byte0, byte1, byte2, byte3) ((((((uint16_t)(byte3 << 8) | byte2) << 8) | byte1) << 8) | byte0) + /* AES Constants */ // AES polynomial const uint16_t POLYNOMIAL = 0b100011011; @@ -47,17 +49,13 @@ void hexprint16(uint8_t *p) { puts(""); } -uint32_t word(uint8_t a0, uint8_t a1, uint8_t a2, uint8_t a3) { - return a0 | (uint32_t)a1 << 8 | (uint32_t)a2 << 16 | (uint32_t)a3 << 24; -} - uint8_t wbyte(uint32_t w, int pos) { return (w >> (pos * 8)) & 0xff; } // **************** AES functions **************** uint32_t subWord(uint32_t w) { - return word(SBOX[wbyte(w, 0)], SBOX[wbyte(w, 1)], SBOX[wbyte(w, 2)], SBOX[wbyte(w, 3)]); + return WORD(SBOX[wbyte(w, 0)], SBOX[wbyte(w, 1)], SBOX[wbyte(w, 2)], SBOX[wbyte(w, 3)]); } void subBytes(t_state s) { @@ -123,7 +121,7 @@ void mixColumns(t_state s) { // Taken from: https://www.brainkart.com/article/AES-Key-Expansion_8410/ void expandKey(uint8_t k[16], uint32_t ek[44]) { for (uint8_t i = 0; i < 4; i++) { - ek[i] = word(k[i * 4], k[i * 4 + 1], k[i * 4 + 2], k[i * 4 + 3]); + ek[i] = WORD(k[i * 4], k[i * 4 + 1], k[i * 4 + 2], k[i * 4 + 3]); } for (uint8_t i = 4; i < 44; i++) { @@ -155,7 +153,7 @@ void aes(uint8_t *in, uint8_t *out, uint8_t *skey) t_state state; for (uint8_t i = 0; i < 4; i++) { - state[i] = word(in[i * 4], in[i * 4 + 1], in[i * 4 + 2], in[i * 4 + 3]); + state[i] = WORD(in[i * 4], in[i * 4 + 1], in[i * 4 + 2], in[i * 4 + 3]); } uint32_t expKey[11 * 4];