Replace word with a macro

This commit is contained in:
Manuel Thalmann 2023-12-12 18:36:42 +01:00
parent cfe7cbd136
commit d8dd138d41

View file

@ -10,6 +10,8 @@ AES specification:
http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf 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 Constants */
// AES polynomial // AES polynomial
const uint16_t POLYNOMIAL = 0b100011011; const uint16_t POLYNOMIAL = 0b100011011;
@ -47,17 +49,13 @@ void hexprint16(uint8_t *p) {
puts(""); 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) { uint8_t wbyte(uint32_t w, int pos) {
return (w >> (pos * 8)) & 0xff; return (w >> (pos * 8)) & 0xff;
} }
// **************** AES functions **************** // **************** AES functions ****************
uint32_t subWord(uint32_t w) { 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) { 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/ // Taken from: https://www.brainkart.com/article/AES-Key-Expansion_8410/
void expandKey(uint8_t k[16], uint32_t ek[44]) { void expandKey(uint8_t k[16], uint32_t ek[44]) {
for (uint8_t i = 0; i < 4; i++) { 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++) { 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; t_state state;
for (uint8_t i = 0; i < 4; i++) { 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]; uint32_t expKey[11 * 4];