2 * AES (Rijndael) cipher - decrypt
4 * Modifications to public domain implementation:
6 * - use C pre-processor to make it easier to change S table access
7 * - added option (AES_SMALL_TABLES) for reducing code size by about 8 kB at
8 * cost of reduced throughput (quite small difference on Pentium 4,
9 * 10-25% when using -O1 or -O2 optimization)
11 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
13 * This software may be distributed under the terms of the BSD license.
14 * See README for more details.
24 * Expand the cipher key into the decryption key schedule.
26 * @return the number of rounds for the given cipher key size.
28 static int rijndaelKeySetupDec(u32 rk[], const u8 cipherKey[], int keyBits)
33 /* expand the cipher key: */
34 Nr = rijndaelKeySetupEnc(rk, cipherKey, keyBits);
37 /* invert the order of the round keys: */
38 for (i = 0, j = 4*Nr; i < j; i += 4, j -= 4) {
39 temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
40 temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
41 temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
42 temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
44 /* apply the inverse MixColumn transform to all round keys but the
45 * first and the last: */
46 for (i = 1; i < Nr; i++) {
48 for (j = 0; j < 4; j++) {
49 rk[j] = TD0_(TE4((rk[j] >> 24) )) ^
50 TD1_(TE4((rk[j] >> 16) & 0xff)) ^
51 TD2_(TE4((rk[j] >> 8) & 0xff)) ^
52 TD3_(TE4((rk[j] ) & 0xff));
59 void * aes_decrypt_init(const u8 *key, size_t len)
63 rk = os_malloc(AES_PRIV_SIZE);
66 res = rijndaelKeySetupDec(rk, key, len * 8);
71 rk[AES_PRIV_NR_POS] = res;
75 static void rijndaelDecrypt(const u32 rk[/*44*/], int Nr, const u8 ct[16],
78 u32 s0, s1, s2, s3, t0, t1, t2, t3;
81 #endif /* ?FULL_UNROLL */
84 * map byte array block to cipher state
85 * and add initial round key:
87 s0 = GETU32(ct ) ^ rk[0];
88 s1 = GETU32(ct + 4) ^ rk[1];
89 s2 = GETU32(ct + 8) ^ rk[2];
90 s3 = GETU32(ct + 12) ^ rk[3];
92 #define ROUND(i,d,s) \
93 d##0 = TD0(s##0) ^ TD1(s##3) ^ TD2(s##2) ^ TD3(s##1) ^ rk[4 * i]; \
94 d##1 = TD0(s##1) ^ TD1(s##0) ^ TD2(s##3) ^ TD3(s##2) ^ rk[4 * i + 1]; \
95 d##2 = TD0(s##2) ^ TD1(s##1) ^ TD2(s##0) ^ TD3(s##3) ^ rk[4 * i + 2]; \
96 d##3 = TD0(s##3) ^ TD1(s##2) ^ TD2(s##1) ^ TD3(s##0) ^ rk[4 * i + 3]
120 #else /* !FULL_UNROLL */
122 /* Nr - 1 full rounds: */
132 #endif /* ?FULL_UNROLL */
137 * apply last round and
138 * map cipher state to byte array block:
140 s0 = TD41(t0) ^ TD42(t3) ^ TD43(t2) ^ TD44(t1) ^ rk[0];
142 s1 = TD41(t1) ^ TD42(t0) ^ TD43(t3) ^ TD44(t2) ^ rk[1];
144 s2 = TD41(t2) ^ TD42(t1) ^ TD43(t0) ^ TD44(t3) ^ rk[2];
146 s3 = TD41(t3) ^ TD42(t2) ^ TD43(t1) ^ TD44(t0) ^ rk[3];
150 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
153 rijndaelDecrypt(ctx, rk[AES_PRIV_NR_POS], crypt, plain);
157 void aes_decrypt_deinit(void *ctx)
159 os_memset(ctx, 0, AES_PRIV_SIZE);