diff options
author | Jack Lloyd <[email protected]> | 2020-05-06 09:48:11 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2020-05-08 06:19:58 -0400 |
commit | c06cb332d7039256a9d3c0c3aa4024e1e37b7626 (patch) | |
tree | cbf0757741d58374e50327dcf540e937a8e59cdf /src/lib/block/aes | |
parent | 4583823284bc9e856b6c2a4d7bc48ffced89e066 (diff) |
Avoid allocating extra vector during AES key schedule
This ended up being about 10% of the total cost.
Diffstat (limited to 'src/lib/block/aes')
-rw-r--r-- | src/lib/block/aes/aes.cpp | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/src/lib/block/aes/aes.cpp b/src/lib/block/aes/aes.cpp index db9b68f38..21f580641 100644 --- a/src/lib/block/aes/aes.cpp +++ b/src/lib/block/aes/aes.cpp @@ -660,43 +660,43 @@ void aes_key_schedule(const uint8_t key[], size_t length, CT::poison(key, length); - secure_vector<uint32_t> XEK(length + 32); - secure_vector<uint32_t> XDK(length + 32); + EK.resize(length + 32); + DK.resize(length + 32); for(size_t i = 0; i != X; ++i) - XEK[i] = load_be<uint32_t>(key, i); + EK[i] = load_be<uint32_t>(key, i); for(size_t i = X; i < 4*(rounds+1); i += X) { - XEK[i] = XEK[i-X] ^ RC[(i-X)/X] ^ rotl<8>(SE_word(XEK[i-1])); + EK[i] = EK[i-X] ^ RC[(i-X)/X] ^ rotl<8>(SE_word(EK[i-1])); for(size_t j = 1; j != X; ++j) { - XEK[i+j] = XEK[i+j-X]; + EK[i+j] = EK[i+j-X]; if(X == 8 && j == 4) - XEK[i+j] ^= SE_word(XEK[i+j-1]); + EK[i+j] ^= SE_word(EK[i+j-1]); else - XEK[i+j] ^= XEK[i+j-1]; + EK[i+j] ^= EK[i+j-1]; } } for(size_t i = 0; i != 4*(rounds+1); i += 4) { - XDK[i ] = XEK[4*rounds-i ]; - XDK[i+1] = XEK[4*rounds-i+1]; - XDK[i+2] = XEK[4*rounds-i+2]; - XDK[i+3] = XEK[4*rounds-i+3]; + DK[i ] = EK[4*rounds-i ]; + DK[i+1] = EK[4*rounds-i+1]; + DK[i+2] = EK[4*rounds-i+2]; + DK[i+3] = EK[4*rounds-i+3]; } for(size_t i = 4; i != length + 24; ++i) { - const uint8_t s0 = get_byte(0, XDK[i]); - const uint8_t s1 = get_byte(1, XDK[i]); - const uint8_t s2 = get_byte(2, XDK[i]); - const uint8_t s3 = get_byte(3, XDK[i]); + const uint8_t s0 = get_byte(0, DK[i]); + const uint8_t s1 = get_byte(1, DK[i]); + const uint8_t s2 = get_byte(2, DK[i]); + const uint8_t s3 = get_byte(3, DK[i]); - XDK[i] = InvMixColumn(s0) ^ + DK[i] = InvMixColumn(s0) ^ rotr<8>(InvMixColumn(s1)) ^ rotr<16>(InvMixColumn(s2)) ^ rotr<24>(InvMixColumn(s3)); @@ -704,8 +704,6 @@ void aes_key_schedule(const uint8_t key[], size_t length, EK.resize(length + 24 + 4); DK.resize(length + 24 + 4); - copy_mem(EK.data(), XEK.data(), EK.size()); - copy_mem(DK.data(), XDK.data(), DK.size()); if(bswap_keys) { |