From cca1eb15ac8da91f07bcbdb5d5887554ac47b114 Mon Sep 17 00:00:00 2001 From: "Tomohisa Tanaka (Maroontress)" Date: Sun, 20 Aug 2023 21:47:52 +0900 Subject: [PATCH] Merge InvShiftRows and InvSubBytes (#8) --- .../src/Aes128Cbc.c | 67 ++++++------------- testsuite/main.cxx | 16 +---- 2 files changed, 25 insertions(+), 58 deletions(-) diff --git a/libmimicssl-aes128-cbc-decrypt/src/Aes128Cbc.c b/libmimicssl-aes128-cbc-decrypt/src/Aes128Cbc.c index 9dc43b9..d02ff92 100644 --- a/libmimicssl-aes128-cbc-decrypt/src/Aes128Cbc.c +++ b/libmimicssl-aes128-cbc-decrypt/src/Aes128Cbc.c @@ -13,11 +13,6 @@ struct State { uint8_t data[16]; }; -static const uint32_t M0 = 0x000000ff; -static const uint32_t M1 = 0x0000ff00; -static const uint32_t M2 = 0x00ff0000; -static const uint32_t M3 = 0xff000000; - #include "sbox.h" #include "rsbox.h" #include "rcon.h" @@ -69,47 +64,31 @@ addRoundKey(const struct State *state, const struct Aes128Cbc_Key *key) } static struct State -invShiftRows(const struct State *state) +invShiftRowsSubBytes(const struct State *state) { - uint32_t *data = (uint32_t *)state->data; - uint32_t a0 = data[0]; - uint32_t a1 = data[1]; - uint32_t a2 = data[2]; - uint32_t a3 = data[3]; - struct State newState; - uint32_t *out = (uint32_t *)newState.data; + + const uint8_t *s = (const uint8_t *)state->data; + uint8_t *o = newState.data; // 0 1 2 3 4 5 6 7 8 9 A B C D E F // | | | | // 0 D A 7 4 1 E B 8 5 2 F C 9 6 3 - out[0] = (a0 & M0) | (a3 & M1) | (a2 & M2) | (a1 & M3); - out[1] = (a1 & M0) | (a0 & M1) | (a3 & M2) | (a2 & M3); - out[2] = (a2 & M0) | (a1 & M1) | (a0 & M2) | (a3 & M3); - out[3] = (a3 & M0) | (a2 & M1) | (a1 & M2) | (a0 & M3); - return newState; -} - -static struct State -invSubBytes(const struct State *state) -{ - struct State newState; - - const uint32_t *data = (const uint32_t *)state->data; - uint32_t *out = (uint32_t *)newState.data; - - for (uint32_t k = 0; k < 4; ++k) { - uint32_t d = *data; - uint8_t d0 = (uint8_t)d; - uint8_t d1 = (uint8_t)(d >> 8); - uint8_t d2 = (uint8_t)(d >> 16); - uint8_t d3 = (uint8_t)(d >> 24); - *out = ((uint32_t)RSBOX[d3] << 24) - | ((uint32_t)RSBOX[d2] << 16) - | ((uint32_t)RSBOX[d1] << 8) - | (uint32_t)RSBOX[d0]; - ++data; - ++out; - } + o[0] = RSBOX[s[0]]; + o[1] = RSBOX[s[13]]; + o[2] = RSBOX[s[10]]; + o[3] = RSBOX[s[7]]; + o[4] = RSBOX[s[4]]; + o[5] = RSBOX[s[1]]; + o[6] = RSBOX[s[14]]; + o[7] = RSBOX[s[11]]; + o[8] = RSBOX[s[8]]; + o[9] = RSBOX[s[5]]; + o[10] = RSBOX[s[2]]; + o[11] = RSBOX[s[15]]; + o[12] = RSBOX[s[12]]; + o[13] = RSBOX[s[9]]; + o[14] = RSBOX[s[6]]; + o[15] = RSBOX[s[3]]; return newState; } @@ -190,14 +169,12 @@ eqInvCipher(const struct State *state, const struct Aes128Cbc_RoundKey *roundKey struct State newState = addRoundKey(state, key); for (uint32_t round = 9; round > 0; --round) { --key; - newState = invSubBytes(&newState); - newState = invShiftRows(&newState); + newState = invShiftRowsSubBytes(&newState); newState = invMixColumns(&newState); newState = addRoundKey(&newState, key); } --key; - newState = invSubBytes(&newState); - newState = invShiftRows(&newState); + newState = invShiftRowsSubBytes(&newState); return addRoundKey(&newState, key); } diff --git a/testsuite/main.cxx b/testsuite/main.cxx index 41dc346..aa2b981 100644 --- a/testsuite/main.cxx +++ b/testsuite/main.cxx @@ -298,22 +298,12 @@ main(int ac, char** av) expect(actual[k]) == expected[k]; } }); - driver.add("invShiftRows", [] { + driver.add("invShiftRowsSubBytes", [] { auto state = toState("d41d8cd98f00b204e9800998ecf8427e"); - auto newState = invShiftRows(&state); + auto newState = invShiftRowsSubBytes(&state); dump(newState); auto actual = newState.data; - auto expected = toArray("d4f809048f1d4298e9008c7eec80b2d9"); - for (auto k = 0; k < 16; ++k) { - expect(actual[k]) == expected[k]; - } - }); - driver.add("invSubBytes", [] { - auto state = toState("d41d8cd98f00b204e9800998ecf8427e"); - auto newState = invSubBytes(&state); - dump(newState); - auto actual = newState.data; - auto expected = toArray("19def0e573523e30eb3a40e283e1f68a"); + auto expected = toArray("19e1403073def6e2eb52f08a833a3ee5"); for (auto k = 0; k < 16; ++k) { expect(actual[k]) == expected[k]; }