From 79d2776d417909e4af5f171d8a951dd334184688 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 14 Nov 2023 08:32:32 +0100 Subject: [PATCH] Implement `expandKey` --- aes/aes.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/aes/aes.cpp b/aes/aes.cpp index 95418b9..c388c64 100644 --- a/aes/aes.cpp +++ b/aes/aes.cpp @@ -139,8 +139,24 @@ void mixColumns(t_state s) { * to 11 round keys (11*4*32b) * each round key is 4*32b */ +// 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++) { + for (uint8_t j = 0; j < 4; j++) { + *(((uint8_t*)(&(ek[i]))) + j) = k[i * 4 + j]; + } + } + + for (uint8_t i = 4; i < 44; i++) { + uint32_t key = ek[i - 1]; + + if (i % 4 == 0) { + key = (key >> 8) | (key << 24); + key = subWord(key) ^ rCon[i / 4]; + } + + ek[i] = ek[i - 4] ^ key; + } }