Replace wbyte with a macro

This commit is contained in:
Manuel Thalmann 2023-12-12 18:45:01 +01:00
parent 7ca06f642a
commit 87eb5cbff0

View file

@ -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);
}
}