31 for (i = 0; i < 8; i++) {
32 s->
slice[i] |= (
byte & 1) << (r * 4 + c);
40 for (c = 0; c < 4; c++) {
42 for (r = 0; r < 4; r++) {
51 for (c = 0; c < 4; c++) {
53 for (r = 0; r < 4; r++) {
56 for (b = 0; b < 8; b++) {
57 v |= ((s->
slice[b] >> (r * 4 + c)) & 1) << b;
75 uint16_t T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,
77 uint16_t T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, D;
78 uint16_t M1, M6, M11, M13, M15, M20, M21, M22, M23, M25, M37, M38, M39, M40;
79 uint16_t M41, M42, M43, M44, M45, M46, M47, M48, M49, M50, M51, M52, M53,
81 uint16_t M55, M56, M57, M58, M59, M60, M61, M62, M63;
84 uint16_t R5, R13, R17, R18, R19;
150 M13 = (T4 & T27) ^ M11;
151 M15 = (T2 & T10) ^ M11;
152 M20 = T14 ^ M1 ^ (T23 & T8) ^ M13;
153 M21 = (T19 & D) ^ M1 ^ T24 ^ M15;
154 M22 = T26 ^ M6 ^ (T22 & T9) ^ M13;
155 M23 = (T20 & T17) ^ M6 ^ M15 ^ T25;
157 M37 = M21 ^ ((M20 ^ M21) & (M23 ^ M25));
158 M38 = M20 ^ M25 ^ (M21 | (M20 & M23));
159 M39 = M23 ^ ((M22 ^ M23) & (M21 ^ M25));
160 M40 = M22 ^ M25 ^ (M23 | (M21 & M22));
187 uint16_t P0 = M52 ^ M61;
188 uint16_t P1 = M58 ^ M59;
189 uint16_t P2 = M54 ^ M62;
190 uint16_t P3 = M47 ^ M50;
191 uint16_t P4 = M48 ^ M56;
192 uint16_t P5 = M46 ^ M51;
193 uint16_t P6 = M49 ^ M60;
194 uint16_t P7 = P0 ^ P1;
195 uint16_t P8 = M50 ^ M53;
196 uint16_t P9 = M55 ^ M63;
197 uint16_t P10 = M57 ^ P4;
198 uint16_t P11 = P0 ^ P3;
199 uint16_t P12 = M46 ^ M48;
200 uint16_t P13 = M49 ^ M51;
201 uint16_t P14 = M49 ^ M62;
202 uint16_t P15 = M54 ^ M59;
203 uint16_t P16 = M57 ^ M61;
204 uint16_t P17 = M58 ^ P2;
205 uint16_t P18 = M63 ^ P5;
206 uint16_t P19 = P2 ^ P3;
207 uint16_t P20 = P4 ^ P6;
208 uint16_t P22 = P2 ^ P7;
209 uint16_t P23 = P7 ^ P8;
210 uint16_t P24 = P5 ^ P7;
211 uint16_t P25 = P6 ^ P10;
212 uint16_t P26 = P9 ^ P11;
213 uint16_t P27 = P10 ^ P18;
214 uint16_t P28 = P11 ^ P25;
215 uint16_t P29 = P15 ^ P20;
216 s->
slice[7] = P13 ^ P22;
217 s->
slice[6] = P26 ^ P29;
218 s->
slice[5] = P17 ^ P28;
219 s->
slice[4] = P12 ^ P22;
220 s->
slice[3] = P23 ^ P27;
221 s->
slice[2] = P19 ^ P24;
222 s->
slice[1] = P14 ^ P23;
223 s->
slice[0] = P9 ^ P16;
226 uint16_t L0 = M61 ^ M62;
227 uint16_t L1 = M50 ^ M56;
228 uint16_t L2 = M46 ^ M48;
229 uint16_t L3 = M47 ^ M55;
230 uint16_t L4 = M54 ^ M58;
231 uint16_t L5 = M49 ^ M61;
232 uint16_t L6 = M62 ^ L5;
233 uint16_t L7 = M46 ^ L3;
234 uint16_t L8 = M51 ^ M59;
235 uint16_t L9 = M52 ^ M53;
236 uint16_t L10 = M53 ^ L4;
237 uint16_t L11 = M60 ^ L2;
238 uint16_t L12 = M48 ^ M51;
239 uint16_t L13 = M50 ^ L0;
240 uint16_t L14 = M52 ^ M61;
241 uint16_t L15 = M55 ^ L1;
242 uint16_t L16 = M56 ^ L0;
243 uint16_t L17 = M57 ^ L1;
244 uint16_t L18 = M58 ^ L8;
245 uint16_t L19 = M63 ^ L4;
246 uint16_t L20 = L0 ^ L1;
247 uint16_t L21 = L1 ^ L7;
248 uint16_t L22 = L3 ^ L12;
249 uint16_t L23 = L18 ^ L2;
250 uint16_t L24 = L15 ^ L9;
251 uint16_t L25 = L6 ^ L10;
252 uint16_t L26 = L7 ^ L9;
253 uint16_t L27 = L8 ^ L10;
254 uint16_t L28 = L11 ^ L14;
255 uint16_t L29 = L11 ^ L17;
256 s->
slice[7] = L6 ^ L24;
257 s->
slice[6] = ~(L16 ^ L26);
258 s->
slice[5] = ~(L19 ^ L28);
259 s->
slice[4] = L6 ^ L21;
260 s->
slice[3] = L20 ^ L22;
261 s->
slice[2] = L25 ^ L29;
262 s->
slice[1] = ~(L13 ^ L27);
263 s->
slice[0] = ~(L6 ^ L23);
267#define BIT_RANGE(from, to) (((1 << ((to) - (from))) - 1) << (from))
269#define BIT_RANGE_LEFT(x, from, to, shift) \
270 (((x)&BIT_RANGE((from), (to))) << (shift))
271#define BIT_RANGE_RIGHT(x, from, to, shift) \
272 (((x)&BIT_RANGE((from), (to))) >> (shift))
276 for (i = 0; i < 8; i++) {
277 uint16_t v = s->
slice[i];
288 for (i = 0; i < 8; i++) {
289 uint16_t v = s->
slice[i];
298#define ROT(x, b) (((x) >> ((b)*4)) | ((x) << ((4 - (b)) * 4)))
325 uint16_t s0_01 = s0 ^
ROT(s0, 1), s0_123 =
ROT(s0_01, 1) ^
ROT(s0, 3);
326 uint16_t s1_01 = s1 ^
ROT(s1, 1), s1_123 =
ROT(s1_01, 1) ^
ROT(s1, 3);
327 uint16_t s2_01 = s2 ^
ROT(s2, 1), s2_123 =
ROT(s2_01, 1) ^
ROT(s2, 3);
328 uint16_t s3_01 = s3 ^
ROT(s3, 1), s3_123 =
ROT(s3_01, 1) ^
ROT(s3, 3);
329 uint16_t s4_01 = s4 ^
ROT(s4, 1), s4_123 =
ROT(s4_01, 1) ^
ROT(s4, 3);
330 uint16_t s5_01 = s5 ^
ROT(s5, 1), s5_123 =
ROT(s5_01, 1) ^
ROT(s5, 3);
331 uint16_t s6_01 = s6 ^
ROT(s6, 1), s6_123 =
ROT(s6_01, 1) ^
ROT(s6, 3);
332 uint16_t s7_01 = s7 ^
ROT(s7, 1), s7_123 =
ROT(s7_01, 1) ^
ROT(s7, 3);
334 s->
slice[0] = s7_01 ^ s0_123;
335 s->
slice[1] = s7_01 ^ s0_01 ^ s1_123;
336 s->
slice[2] = s1_01 ^ s2_123;
337 s->
slice[3] = s7_01 ^ s2_01 ^ s3_123;
338 s->
slice[4] = s7_01 ^ s3_01 ^ s4_123;
339 s->
slice[5] = s4_01 ^ s5_123;
340 s->
slice[6] = s5_01 ^ s6_123;
341 s->
slice[7] = s6_01 ^ s7_123;
356 s->
slice[0] ^= t6_02;
357 s->
slice[1] ^= t6_02 ^ t7_02;
358 s->
slice[2] ^= t0_02 ^ t7_02;
359 s->
slice[3] ^= t1_02 ^ t6_02;
360 s->
slice[4] ^= t2_02 ^ t6_02 ^ t7_02;
361 s->
slice[5] ^= t3_02 ^ t7_02;
362 s->
slice[6] ^= t4_02;
363 s->
slice[7] ^= t5_02;
369 for (b = 0; b < 8; b++) {
377 for (b = 0; b < 8; b++) {
386 for (b = 0; b < 8; b++) {
388 ((s->
slice[b] ^= ((a->
slice[b] >> c2) & 0x1111)) & 0x1111) << c1;
395 for (b = 0; b < 8; b++) {
403 uint16_t top = s->
slice[7];
428 AES_state rcon = {{1, 0, 0, 0, 0, 0, 0, 0}};
434 for (i = 0; i < nrounds + 1; i++) {
436 for (b = 0; b < 8; b++) {
437 rounds[i].
slice[b] = 0;
443 for (i = 0; i < nkeywords; i++) {
445 for (r = 0; r < 4; r++) {
446 LoadByte(&rounds[i >> 2], *(key++), r, i & 3);
450 GetOneColumn(&column, &rounds[(nkeywords - 1) >> 2], (nkeywords - 1) & 3);
452 for (i = nkeywords; i < 4 * (nrounds + 1); i++) {
458 }
else if (nkeywords > 6 && pos == 4) {
461 if (++pos == nkeywords) pos = 0;
463 &rounds[(i - nkeywords) >> 2], i & 3,
464 (i - nkeywords) & 3);
469 const uint8_t *plain16) {
476 for (round = 1; round < nrounds; round++) {
491 const uint8_t *cipher16) {
504 for (round = 1; round < nrounds; round++) {
523 const uint8_t *plain16) {
532 const uint8_t *cipher16) {
545 const uint8_t *plain16) {
554 const uint8_t *cipher16) {
567 const uint8_t *plain16) {
576 const uint8_t *cipher16) {
void AES192_decrypt(const AES192_ctx *ctx, size_t blocks, uint8_t *plain16, const uint8_t *cipher16)
void AES128_decrypt(const AES128_ctx *ctx, size_t blocks, uint8_t *plain16, const uint8_t *cipher16)
static void SaveBytes(uint8_t *data16, const AES_state *s)
Convert 8 sliced integers into 16 bytes of data.
static void LoadByte(AES_state *s, uint8_t byte, int r, int c)
Convert a byte to sliced form, storing it corresponding to given row and column in s.
void AES256_decrypt(const AES256_ctx *ctx, size_t blocks, uint8_t *plain16, const uint8_t *cipher16)
static void KeySetupColumnMix(AES_state *s, AES_state *r, const AES_state *a, int c1, int c2)
column_c1(r) |= (column_0(s) ^= column_c2(a))
void AES128_init(AES128_ctx *ctx, const uint8_t *key16)
static void LoadBytes(AES_state *s, const uint8_t *data16)
Load 16 bytes of data into 8 sliced integers.
static void InvShiftRows(AES_state *s)
static void SubBytes(AES_state *s, int inv)
static void AES_setup(AES_state *rounds, const uint8_t *key, int nkeywords, int nrounds)
Expand the cipher key into the key schedule.
#define BIT_RANGE_RIGHT(x, from, to, shift)
void AES256_encrypt(const AES256_ctx *ctx, size_t blocks, uint8_t *cipher16, const uint8_t *plain16)
void AES192_encrypt(const AES192_ctx *ctx, size_t blocks, uint8_t *cipher16, const uint8_t *plain16)
static void AES_encrypt(const AES_state *rounds, int nrounds, uint8_t *cipher16, const uint8_t *plain16)
static void KeySetupTransform(AES_state *s, const AES_state *r)
Rotate the rows in s one position upwards, and xor in r.
void AES256_init(AES256_ctx *ctx, const uint8_t *key32)
static void AddRoundKey(AES_state *s, const AES_state *round)
void AES128_encrypt(const AES128_ctx *ctx, size_t blocks, uint8_t *cipher16, const uint8_t *plain16)
static void AES_decrypt(const AES_state *rounds, int nrounds, uint8_t *plain16, const uint8_t *cipher16)
static void ShiftRows(AES_state *s)
static void MixColumns(AES_state *s, int inv)
void AES192_init(AES192_ctx *ctx, const uint8_t *key24)
static void GetOneColumn(AES_state *s, const AES_state *a, int c)
column_0(s) = column_c(a)
static void MultX(AES_state *s)
#define BIT_RANGE(from, to)
#define BIT_RANGE_LEFT(x, from, to, shift)