From 87eb5cbff025dfdf03c06cb8b59ebc335a83def9 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 12 Dec 2023 18:45:01 +0100 Subject: [PATCH] Replace `wbyte` with a macro --- aes-32bit/aes.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/aes-32bit/aes.cpp b/aes-32bit/aes.cpp index 5713875..ac7f21e 100644 --- a/aes-32bit/aes.cpp +++ b/aes-32bit/aes.cpp @@ -11,6 +11,7 @@ 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) +#define WBYTE(value, position) ((value >> (position * 8)) & 0xFF) /* AES Constants */ // AES polynomial @@ -49,13 +50,9 @@ void hexprint16(uint8_t *p) { puts(""); } -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) { @@ -175,10 +172,10 @@ void aes(uint8_t *in, uint8_t *out, uint8_t *skey) } for (int i = 0; i < 16; i++) { - if (i < 4) out[i] = wbyte(state[0], i % 4); - else if (i < 8) out[i] = wbyte(state[1], i % 4); - else if (i < 12) out[i] = wbyte(state[2], i % 4); - else out[i] = wbyte(state[3], i % 4); + if (i < 4) out[i] = WBYTE(state[0], i % 4); + else if (i < 8) out[i] = WBYTE(state[1], i % 4); + else if (i < 12) out[i] = WBYTE(state[2], i % 4); + else out[i] = WBYTE(state[3], i % 4); } }