This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states.
| 153 | |
| 154 | // This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. |
| 155 | static void KeyExpansion(void) |
| 156 | { |
| 157 | uint32_t i, k; |
| 158 | uint8_t tempa[4]; // Used for the column/row operations |
| 159 | |
| 160 | // The first round key is the key itself. |
| 161 | for (i = 0; i < Nk; ++i) |
| 162 | { |
| 163 | RoundKey[(i * 4) + 0] = Key[(i * 4) + 0]; |
| 164 | RoundKey[(i * 4) + 1] = Key[(i * 4) + 1]; |
| 165 | RoundKey[(i * 4) + 2] = Key[(i * 4) + 2]; |
| 166 | RoundKey[(i * 4) + 3] = Key[(i * 4) + 3]; |
| 167 | } |
| 168 | |
| 169 | // All other round keys are found from the previous round keys. |
| 170 | //i == Nk |
| 171 | for (; i < Nb * (Nr + 1); ++i) |
| 172 | { |
| 173 | { |
| 174 | tempa[0] = RoundKey[(i - 1) * 4 + 0]; |
| 175 | tempa[1] = RoundKey[(i - 1) * 4 + 1]; |
| 176 | tempa[2] = RoundKey[(i - 1) * 4 + 2]; |
| 177 | tempa[3] = RoundKey[(i - 1) * 4 + 3]; |
| 178 | } |
| 179 | |
| 180 | if (i % Nk == 0) |
| 181 | { |
| 182 | // This function shifts the 4 bytes in a word to the left once. |
| 183 | // [a0,a1,a2,a3] becomes [a1,a2,a3,a0] |
| 184 | |
| 185 | // Function RotWord() |
| 186 | { |
| 187 | k = tempa[0]; |
| 188 | tempa[0] = tempa[1]; |
| 189 | tempa[1] = tempa[2]; |
| 190 | tempa[2] = tempa[3]; |
| 191 | tempa[3] = k; |
| 192 | } |
| 193 | |
| 194 | // SubWord() is a function that takes a four-byte input word and |
| 195 | // applies the S-box to each of the four bytes to produce an output word. |
| 196 | |
| 197 | // Function Subword() |
| 198 | { |
| 199 | tempa[0] = getSBoxValue(tempa[0]); |
| 200 | tempa[1] = getSBoxValue(tempa[1]); |
| 201 | tempa[2] = getSBoxValue(tempa[2]); |
| 202 | tempa[3] = getSBoxValue(tempa[3]); |
| 203 | } |
| 204 | |
| 205 | tempa[0] = tempa[0] ^ Rcon[i / Nk]; |
| 206 | } |
| 207 | #ifdef AES256 |
| 208 | if (i % Nk == 4) |
| 209 | { |
| 210 | // Function Subword() |
| 211 | { |
| 212 | tempa[0] = getSBoxValue(tempa[0]); |
no test coverage detected