Merge pull request #157 from dmitrystu/optimized-loop

loop optimization
This commit is contained in:
kokke 2020-03-07 02:51:39 +01:00 committed by GitHub
commit 1933872bba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

35
aes.c
View File

@ -412,23 +412,23 @@ static void Cipher(state_t* state, const uint8_t* RoundKey)
uint8_t round = 0; uint8_t round = 0;
// Add the First round key to the state before starting the rounds. // Add the First round key to the state before starting the rounds.
AddRoundKey(0, state, RoundKey); AddRoundKey(0, state, RoundKey);
// There will be Nr rounds. // There will be Nr rounds.
// The first Nr-1 rounds are identical. // The first Nr-1 rounds are identical.
// These Nr-1 rounds are executed in the loop below. // These Nr rounds are executed in the loop below.
for (round = 1; round < Nr; ++round) // Last one without MixColumns()
for (round = 1; ; ++round)
{ {
SubBytes(state); SubBytes(state);
ShiftRows(state); ShiftRows(state);
if (round == Nr) {
break;
}
MixColumns(state); MixColumns(state);
AddRoundKey(round, state, RoundKey); AddRoundKey(round, state, RoundKey);
} }
// Add round key to last round
// The last round is given below.
// The MixColumns function is not here in the last round.
SubBytes(state);
ShiftRows(state);
AddRoundKey(Nr, state, RoundKey); AddRoundKey(Nr, state, RoundKey);
} }
@ -438,24 +438,23 @@ static void InvCipher(state_t* state, const uint8_t* RoundKey)
uint8_t round = 0; uint8_t round = 0;
// Add the First round key to the state before starting the rounds. // Add the First round key to the state before starting the rounds.
AddRoundKey(Nr, state, RoundKey); AddRoundKey(Nr, state, RoundKey);
// There will be Nr rounds. // There will be Nr rounds.
// The first Nr-1 rounds are identical. // The first Nr-1 rounds are identical.
// These Nr-1 rounds are executed in the loop below. // These Nr rounds are executed in the loop below.
for (round = (Nr - 1); round > 0; --round) // Last one without InvMixColumn()
for (round = (Nr - 1); ; --round)
{ {
InvShiftRows(state); InvShiftRows(state);
InvSubBytes(state); InvSubBytes(state);
AddRoundKey(round, state, RoundKey); AddRoundKey(round, state, RoundKey);
if (round == 0) {
break;
}
InvMixColumns(state); InvMixColumns(state);
} }
// The last round is given below.
// The MixColumns function is not here in the last round.
InvShiftRows(state);
InvSubBytes(state);
AddRoundKey(0, state, RoundKey);
} }
#endif // #if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1) #endif // #if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)