1e28a4053SRui Paulo /*
2e28a4053SRui Paulo  * AES (Rijndael) cipher - decrypt
3e28a4053SRui Paulo  *
4e28a4053SRui Paulo  * Modifications to public domain implementation:
5e28a4053SRui Paulo  * - cleanup
6e28a4053SRui Paulo  * - use C pre-processor to make it easier to change S table access
7e28a4053SRui Paulo  * - added option (AES_SMALL_TABLES) for reducing code size by about 8 kB at
8e28a4053SRui Paulo  *   cost of reduced throughput (quite small difference on Pentium 4,
9e28a4053SRui Paulo  *   10-25% when using -O1 or -O2 optimization)
10e28a4053SRui Paulo  *
11f05cddf9SRui Paulo  * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
12e28a4053SRui Paulo  *
13f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
14f05cddf9SRui Paulo  * See README for more details.
15e28a4053SRui Paulo  */
16e28a4053SRui Paulo 
17e28a4053SRui Paulo #include "includes.h"
18e28a4053SRui Paulo 
19e28a4053SRui Paulo #include "common.h"
20e28a4053SRui Paulo #include "crypto.h"
21e28a4053SRui Paulo #include "aes_i.h"
22e28a4053SRui Paulo 
23e28a4053SRui Paulo /**
24e28a4053SRui Paulo  * Expand the cipher key into the decryption key schedule.
25e28a4053SRui Paulo  *
26e28a4053SRui Paulo  * @return	the number of rounds for the given cipher key size.
27e28a4053SRui Paulo  */
rijndaelKeySetupDec(u32 rk[],const u8 cipherKey[],int keyBits)28f05cddf9SRui Paulo static int rijndaelKeySetupDec(u32 rk[], const u8 cipherKey[], int keyBits)
29e28a4053SRui Paulo {
30f05cddf9SRui Paulo 	int Nr, i, j;
31e28a4053SRui Paulo 	u32 temp;
32e28a4053SRui Paulo 
33e28a4053SRui Paulo 	/* expand the cipher key: */
34f05cddf9SRui Paulo 	Nr = rijndaelKeySetupEnc(rk, cipherKey, keyBits);
35f05cddf9SRui Paulo 	if (Nr < 0)
36f05cddf9SRui Paulo 		return Nr;
37e28a4053SRui Paulo 	/* invert the order of the round keys: */
38e28a4053SRui Paulo 	for (i = 0, j = 4*Nr; i < j; i += 4, j -= 4) {
39e28a4053SRui Paulo 		temp = rk[i    ]; rk[i    ] = rk[j    ]; rk[j    ] = temp;
40e28a4053SRui Paulo 		temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
41e28a4053SRui Paulo 		temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
42e28a4053SRui Paulo 		temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
43e28a4053SRui Paulo 	}
44e28a4053SRui Paulo 	/* apply the inverse MixColumn transform to all round keys but the
45e28a4053SRui Paulo 	 * first and the last: */
46e28a4053SRui Paulo 	for (i = 1; i < Nr; i++) {
47e28a4053SRui Paulo 		rk += 4;
48e28a4053SRui Paulo 		for (j = 0; j < 4; j++) {
49e28a4053SRui Paulo 			rk[j] = TD0_(TE4((rk[j] >> 24)       )) ^
50e28a4053SRui Paulo 				TD1_(TE4((rk[j] >> 16) & 0xff)) ^
51e28a4053SRui Paulo 				TD2_(TE4((rk[j] >>  8) & 0xff)) ^
52e28a4053SRui Paulo 				TD3_(TE4((rk[j]      ) & 0xff));
53e28a4053SRui Paulo 		}
54e28a4053SRui Paulo 	}
55f05cddf9SRui Paulo 
56f05cddf9SRui Paulo 	return Nr;
57e28a4053SRui Paulo }
58e28a4053SRui Paulo 
aes_decrypt_init(const u8 * key,size_t len)59e28a4053SRui Paulo void * aes_decrypt_init(const u8 *key, size_t len)
60e28a4053SRui Paulo {
61e28a4053SRui Paulo 	u32 *rk;
62f05cddf9SRui Paulo 	int res;
63e28a4053SRui Paulo 	rk = os_malloc(AES_PRIV_SIZE);
64e28a4053SRui Paulo 	if (rk == NULL)
65e28a4053SRui Paulo 		return NULL;
66f05cddf9SRui Paulo 	res = rijndaelKeySetupDec(rk, key, len * 8);
67f05cddf9SRui Paulo 	if (res < 0) {
68f05cddf9SRui Paulo 		os_free(rk);
69f05cddf9SRui Paulo 		return NULL;
70f05cddf9SRui Paulo 	}
71f05cddf9SRui Paulo 	rk[AES_PRIV_NR_POS] = res;
72e28a4053SRui Paulo 	return rk;
73e28a4053SRui Paulo }
74e28a4053SRui Paulo 
rijndaelDecrypt(const u32 rk[],int Nr,const u8 ct[16],u8 pt[16])75f05cddf9SRui Paulo static void rijndaelDecrypt(const u32 rk[/*44*/], int Nr, const u8 ct[16],
76f05cddf9SRui Paulo 			    u8 pt[16])
77e28a4053SRui Paulo {
78e28a4053SRui Paulo 	u32 s0, s1, s2, s3, t0, t1, t2, t3;
79e28a4053SRui Paulo #ifndef FULL_UNROLL
80e28a4053SRui Paulo 	int r;
81e28a4053SRui Paulo #endif /* ?FULL_UNROLL */
82e28a4053SRui Paulo 
83e28a4053SRui Paulo 	/*
84e28a4053SRui Paulo 	 * map byte array block to cipher state
85e28a4053SRui Paulo 	 * and add initial round key:
86e28a4053SRui Paulo 	 */
87e28a4053SRui Paulo 	s0 = GETU32(ct     ) ^ rk[0];
88e28a4053SRui Paulo 	s1 = GETU32(ct +  4) ^ rk[1];
89e28a4053SRui Paulo 	s2 = GETU32(ct +  8) ^ rk[2];
90e28a4053SRui Paulo 	s3 = GETU32(ct + 12) ^ rk[3];
91e28a4053SRui Paulo 
92e28a4053SRui Paulo #define ROUND(i,d,s) \
93e28a4053SRui Paulo d##0 = TD0(s##0) ^ TD1(s##3) ^ TD2(s##2) ^ TD3(s##1) ^ rk[4 * i]; \
94e28a4053SRui Paulo d##1 = TD0(s##1) ^ TD1(s##0) ^ TD2(s##3) ^ TD3(s##2) ^ rk[4 * i + 1]; \
95e28a4053SRui Paulo d##2 = TD0(s##2) ^ TD1(s##1) ^ TD2(s##0) ^ TD3(s##3) ^ rk[4 * i + 2]; \
96e28a4053SRui Paulo d##3 = TD0(s##3) ^ TD1(s##2) ^ TD2(s##1) ^ TD3(s##0) ^ rk[4 * i + 3]
97e28a4053SRui Paulo 
98e28a4053SRui Paulo #ifdef FULL_UNROLL
99e28a4053SRui Paulo 
100e28a4053SRui Paulo 	ROUND(1,t,s);
101e28a4053SRui Paulo 	ROUND(2,s,t);
102e28a4053SRui Paulo 	ROUND(3,t,s);
103e28a4053SRui Paulo 	ROUND(4,s,t);
104e28a4053SRui Paulo 	ROUND(5,t,s);
105e28a4053SRui Paulo 	ROUND(6,s,t);
106e28a4053SRui Paulo 	ROUND(7,t,s);
107e28a4053SRui Paulo 	ROUND(8,s,t);
108e28a4053SRui Paulo 	ROUND(9,t,s);
109f05cddf9SRui Paulo 	if (Nr > 10) {
110f05cddf9SRui Paulo 		ROUND(10,s,t);
111f05cddf9SRui Paulo 		ROUND(11,t,s);
112f05cddf9SRui Paulo 		if (Nr > 12) {
113f05cddf9SRui Paulo 			ROUND(12,s,t);
114f05cddf9SRui Paulo 			ROUND(13,t,s);
115f05cddf9SRui Paulo 		}
116f05cddf9SRui Paulo 	}
117e28a4053SRui Paulo 
118e28a4053SRui Paulo 	rk += Nr << 2;
119e28a4053SRui Paulo 
120e28a4053SRui Paulo #else  /* !FULL_UNROLL */
121e28a4053SRui Paulo 
122e28a4053SRui Paulo 	/* Nr - 1 full rounds: */
123e28a4053SRui Paulo 	r = Nr >> 1;
124e28a4053SRui Paulo 	for (;;) {
125e28a4053SRui Paulo 		ROUND(1,t,s);
126e28a4053SRui Paulo 		rk += 8;
127e28a4053SRui Paulo 		if (--r == 0)
128e28a4053SRui Paulo 			break;
129e28a4053SRui Paulo 		ROUND(0,s,t);
130e28a4053SRui Paulo 	}
131e28a4053SRui Paulo 
132e28a4053SRui Paulo #endif /* ?FULL_UNROLL */
133e28a4053SRui Paulo 
134e28a4053SRui Paulo #undef ROUND
135e28a4053SRui Paulo 
136e28a4053SRui Paulo 	/*
137e28a4053SRui Paulo 	 * apply last round and
138e28a4053SRui Paulo 	 * map cipher state to byte array block:
139e28a4053SRui Paulo 	 */
140e28a4053SRui Paulo 	s0 = TD41(t0) ^ TD42(t3) ^ TD43(t2) ^ TD44(t1) ^ rk[0];
141e28a4053SRui Paulo 	PUTU32(pt     , s0);
142e28a4053SRui Paulo 	s1 = TD41(t1) ^ TD42(t0) ^ TD43(t3) ^ TD44(t2) ^ rk[1];
143e28a4053SRui Paulo 	PUTU32(pt +  4, s1);
144e28a4053SRui Paulo 	s2 = TD41(t2) ^ TD42(t1) ^ TD43(t0) ^ TD44(t3) ^ rk[2];
145e28a4053SRui Paulo 	PUTU32(pt +  8, s2);
146e28a4053SRui Paulo 	s3 = TD41(t3) ^ TD42(t2) ^ TD43(t1) ^ TD44(t0) ^ rk[3];
147e28a4053SRui Paulo 	PUTU32(pt + 12, s3);
148e28a4053SRui Paulo }
149e28a4053SRui Paulo 
150*85732ac8SCy Schubert 
aes_decrypt(void * ctx,const u8 * crypt,u8 * plain)151*85732ac8SCy Schubert int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
152e28a4053SRui Paulo {
153f05cddf9SRui Paulo 	u32 *rk = ctx;
154f05cddf9SRui Paulo 	rijndaelDecrypt(ctx, rk[AES_PRIV_NR_POS], crypt, plain);
155*85732ac8SCy Schubert 	return 0;
156e28a4053SRui Paulo }
157e28a4053SRui Paulo 
158e28a4053SRui Paulo 
aes_decrypt_deinit(void * ctx)159e28a4053SRui Paulo void aes_decrypt_deinit(void *ctx)
160e28a4053SRui Paulo {
161e28a4053SRui Paulo 	os_memset(ctx, 0, AES_PRIV_SIZE);
162e28a4053SRui Paulo 	os_free(ctx);
163e28a4053SRui Paulo }
164