summaryrefslogtreecommitdiff
path: root/rijndael.cpp
blob: 857e6bfe480f8c8d5af0264e6cbf198c249865ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// rijndael.cpp - modified by Chris Morgan <cmorgan@wpi.edu>
// and Wei Dai from Paulo Baretto's Rijndael implementation
// The original code and all modifications are in the public domain.

// This is the original introductory comment:

/**
 * version 3.0 (December 2000)
 *
 * Optimised ANSI C code for the Rijndael cipher (now AES)
 *
 * author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
 * author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
 * author Paulo Barreto <paulo.barreto@terra.com.br>
 *
 * This code is hereby placed in the public domain.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "pch.h"
#include "rijndael.h"
#include "misc.h"

NAMESPACE_BEGIN(CryptoPP)

void Rijndael::Base::UncheckedSetKey(CipherDir dir, const byte *userKey, unsigned int keylen)
{
	AssertValidKeyLength(keylen);

	m_rounds = keylen/4 + 6;
	m_key.New(4*(m_rounds+1));

	word32 temp, *rk = m_key;
	unsigned int i=0;

	GetUserKey(BIG_ENDIAN_ORDER, rk, keylen/4, userKey, keylen);

	switch(keylen)
	{
	case 16:
		while (true)
		{
			temp  = rk[3];
			rk[4] = rk[0] ^
				(Te4[GETBYTE(temp, 2)] & 0xff000000) ^
				(Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^
				(Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^
				(Te4[GETBYTE(temp, 3)] & 0x000000ff) ^
				rcon[i];
			rk[5] = rk[1] ^ rk[4];
			rk[6] = rk[2] ^ rk[5];
			rk[7] = rk[3] ^ rk[6];
			if (++i == 10)
				break;
			rk += 4;
		}
		break;

	case 24:
		while (true)	// for (;;) here triggers a bug in VC60 SP4 w/ Processor Pack
		{
			temp = rk[ 5];
			rk[ 6] = rk[ 0] ^
				(Te4[GETBYTE(temp, 2)] & 0xff000000) ^
				(Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^
				(Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^
				(Te4[GETBYTE(temp, 3)] & 0x000000ff) ^
				rcon[i];
			rk[ 7] = rk[ 1] ^ rk[ 6];
			rk[ 8] = rk[ 2] ^ rk[ 7];
			rk[ 9] = rk[ 3] ^ rk[ 8];
			if (++i == 8)
				break;
			rk[10] = rk[ 4] ^ rk[ 9];
			rk[11] = rk[ 5] ^ rk[10];
			rk += 6;
		}
		break;

	case 32:
        while (true)
		{
        	temp = rk[ 7];
        	rk[ 8] = rk[ 0] ^
        		(Te4[GETBYTE(temp, 2)] & 0xff000000) ^
        		(Te4[GETBYTE(temp, 1)] & 0x00ff0000) ^
        		(Te4[GETBYTE(temp, 0)] & 0x0000ff00) ^
        		(Te4[GETBYTE(temp, 3)] & 0x000000ff) ^
        		rcon[i];
        	rk[ 9] = rk[ 1] ^ rk[ 8];
        	rk[10] = rk[ 2] ^ rk[ 9];
        	rk[11] = rk[ 3] ^ rk[10];
			if (++i == 7)
				break;
        	temp = rk[11];
        	rk[12] = rk[ 4] ^
        		(Te4[GETBYTE(temp, 3)] & 0xff000000) ^
        		(Te4[GETBYTE(temp, 2)] & 0x00ff0000) ^
        		(Te4[GETBYTE(temp, 1)] & 0x0000ff00) ^
        		(Te4[GETBYTE(temp, 0)] & 0x000000ff);
        	rk[13] = rk[ 5] ^ rk[12];
        	rk[14] = rk[ 6] ^ rk[13];
        	rk[15] = rk[ 7] ^ rk[14];

			rk += 8;
        }
		break;
	}

	if (dir == DECRYPTION)
	{
		unsigned int i, j;
		rk = m_key;

		/* invert the order of the round keys: */
		for (i = 0, j = 4*m_rounds; i < j; i += 4, j -= 4) {
			temp = rk[i    ]; rk[i    ] = rk[j    ]; rk[j    ] = temp;
			temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
			temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
			temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
		}
		/* apply the inverse MixColumn transform to all round keys but the first and the last: */
		for (i = 1; i < m_rounds; i++) {
			rk += 4;
			rk[0] =
				Td0[Te4[GETBYTE(rk[0], 3)] & 0xff] ^
				Td1[Te4[GETBYTE(rk[0], 2)] & 0xff] ^
				Td2[Te4[GETBYTE(rk[0], 1)] & 0xff] ^
				Td3[Te4[GETBYTE(rk[0], 0)] & 0xff];
			rk[1] =
				Td0[Te4[GETBYTE(rk[1], 3)] & 0xff] ^
				Td1[Te4[GETBYTE(rk[1], 2)] & 0xff] ^
				Td2[Te4[GETBYTE(rk[1], 1)] & 0xff] ^
				Td3[Te4[GETBYTE(rk[1], 0)] & 0xff];
			rk[2] =
				Td0[Te4[GETBYTE(rk[2], 3)] & 0xff] ^
				Td1[Te4[GETBYTE(rk[2], 2)] & 0xff] ^
				Td2[Te4[GETBYTE(rk[2], 1)] & 0xff] ^
				Td3[Te4[GETBYTE(rk[2], 0)] & 0xff];
			rk[3] =
				Td0[Te4[GETBYTE(rk[3], 3)] & 0xff] ^
				Td1[Te4[GETBYTE(rk[3], 2)] & 0xff] ^
				Td2[Te4[GETBYTE(rk[3], 1)] & 0xff] ^
				Td3[Te4[GETBYTE(rk[3], 0)] & 0xff];
		}
	}
}

typedef BlockGetAndPut<word32, BigEndian> Block;

void Rijndael::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
{
	word32 s0, s1, s2, s3, t0, t1, t2, t3;
	const word32 *rk = m_key;

    /*
	 * map byte array block to cipher state
	 * and add initial round key:
	 */
	Block::Get(inBlock)(s0)(s1)(s2)(s3);
	s0 ^= rk[0];
	s1 ^= rk[1];
	s2 ^= rk[2];
	s3 ^= rk[3];
    /*
	 * Nr - 1 full rounds:
	 */
    unsigned int r = m_rounds >> 1;
    for (;;) {
        t0 =
            Te0[GETBYTE(s0, 3)] ^
            Te1[GETBYTE(s1, 2)] ^
            Te2[GETBYTE(s2, 1)] ^
            Te3[GETBYTE(s3, 0)] ^
            rk[4];
        t1 =
            Te0[GETBYTE(s1, 3)] ^
            Te1[GETBYTE(s2, 2)] ^
            Te2[GETBYTE(s3, 1)] ^
            Te3[GETBYTE(s0, 0)] ^
            rk[5];
        t2 =
            Te0[GETBYTE(s2, 3)] ^
            Te1[GETBYTE(s3, 2)] ^
            Te2[GETBYTE(s0, 1)] ^
            Te3[GETBYTE(s1, 0)] ^
            rk[6];
        t3 =
            Te0[GETBYTE(s3, 3)] ^
            Te1[GETBYTE(s0, 2)] ^
            Te2[GETBYTE(s1, 1)] ^
            Te3[GETBYTE(s2, 0)] ^
            rk[7];

        rk += 8;
        if (--r == 0) {
            break;
        }

        s0 =
            Te0[GETBYTE(t0, 3)] ^
            Te1[GETBYTE(t1, 2)] ^
            Te2[GETBYTE(t2, 1)] ^
            Te3[GETBYTE(t3, 0)] ^
            rk[0];
        s1 =
            Te0[GETBYTE(t1, 3)] ^
            Te1[GETBYTE(t2, 2)] ^
            Te2[GETBYTE(t3, 1)] ^
            Te3[GETBYTE(t0, 0)] ^
            rk[1];
        s2 =
            Te0[GETBYTE(t2, 3)] ^
            Te1[GETBYTE(t3, 2)] ^
            Te2[GETBYTE(t0, 1)] ^
            Te3[GETBYTE(t1, 0)] ^
            rk[2];
        s3 =
            Te0[GETBYTE(t3, 3)] ^
            Te1[GETBYTE(t0, 2)] ^
            Te2[GETBYTE(t1, 1)] ^
            Te3[GETBYTE(t2, 0)] ^
            rk[3];
    }
    /*
	 * apply last round and
	 * map cipher state to byte array block:
	 */

	s0 =
		(Te4[GETBYTE(t0, 3)] & 0xff000000) ^
		(Te4[GETBYTE(t1, 2)] & 0x00ff0000) ^
		(Te4[GETBYTE(t2, 1)] & 0x0000ff00) ^
		(Te4[GETBYTE(t3, 0)] & 0x000000ff) ^
		rk[0];
	s1 =
		(Te4[GETBYTE(t1, 3)] & 0xff000000) ^
		(Te4[GETBYTE(t2, 2)] & 0x00ff0000) ^
		(Te4[GETBYTE(t3, 1)] & 0x0000ff00) ^
		(Te4[GETBYTE(t0, 0)] & 0x000000ff) ^
		rk[1];
	s2 =
		(Te4[GETBYTE(t2, 3)] & 0xff000000) ^
		(Te4[GETBYTE(t3, 2)] & 0x00ff0000) ^
		(Te4[GETBYTE(t0, 1)] & 0x0000ff00) ^
		(Te4[GETBYTE(t1, 0)] & 0x000000ff) ^
		rk[2];
	s3 =
		(Te4[GETBYTE(t3, 3)] & 0xff000000) ^
		(Te4[GETBYTE(t0, 2)] & 0x00ff0000) ^
		(Te4[GETBYTE(t1, 1)] & 0x0000ff00) ^
		(Te4[GETBYTE(t2, 0)] & 0x000000ff) ^
		rk[3];

	Block::Put(xorBlock, outBlock)(s0)(s1)(s2)(s3);
}

void Rijndael::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
{
	word32 s0, s1, s2, s3, t0, t1, t2, t3;
    const word32 *rk = m_key;

    /*
	 * map byte array block to cipher state
	 * and add initial round key:
	 */
	Block::Get(inBlock)(s0)(s1)(s2)(s3);
	s0 ^= rk[0];
	s1 ^= rk[1];
	s2 ^= rk[2];
	s3 ^= rk[3];
    /*
     * Nr - 1 full rounds:
     */
    unsigned int r = m_rounds >> 1;
    for (;;) {
        t0 =
            Td0[GETBYTE(s0, 3)] ^
            Td1[GETBYTE(s3, 2)] ^
            Td2[GETBYTE(s2, 1)] ^
            Td3[GETBYTE(s1, 0)] ^
            rk[4];
        t1 =
            Td0[GETBYTE(s1, 3)] ^
            Td1[GETBYTE(s0, 2)] ^
            Td2[GETBYTE(s3, 1)] ^
            Td3[GETBYTE(s2, 0)] ^
            rk[5];
        t2 =
            Td0[GETBYTE(s2, 3)] ^
            Td1[GETBYTE(s1, 2)] ^
            Td2[GETBYTE(s0, 1)] ^
            Td3[GETBYTE(s3, 0)] ^
            rk[6];
        t3 =
            Td0[GETBYTE(s3, 3)] ^
            Td1[GETBYTE(s2, 2)] ^
            Td2[GETBYTE(s1, 1)] ^
            Td3[GETBYTE(s0, 0)] ^
            rk[7];

        rk += 8;
        if (--r == 0) {
            break;
        }

        s0 =
            Td0[GETBYTE(t0, 3)] ^
            Td1[GETBYTE(t3, 2)] ^
            Td2[GETBYTE(t2, 1)] ^
            Td3[GETBYTE(t1, 0)] ^
            rk[0];
        s1 =
            Td0[GETBYTE(t1, 3)] ^
            Td1[GETBYTE(t0, 2)] ^
            Td2[GETBYTE(t3, 1)] ^
            Td3[GETBYTE(t2, 0)] ^
            rk[1];
        s2 =
            Td0[GETBYTE(t2, 3)] ^
            Td1[GETBYTE(t1, 2)] ^
            Td2[GETBYTE(t0, 1)] ^
            Td3[GETBYTE(t3, 0)] ^
            rk[2];
        s3 =
            Td0[GETBYTE(t3, 3)] ^
            Td1[GETBYTE(t2, 2)] ^
            Td2[GETBYTE(t1, 1)] ^
            Td3[GETBYTE(t0, 0)] ^
            rk[3];
    }
    /*
	 * apply last round and
	 * map cipher state to byte array block:
	 */
   	s0 =
   		(Td4[GETBYTE(t0, 3)] & 0xff000000) ^
   		(Td4[GETBYTE(t3, 2)] & 0x00ff0000) ^
   		(Td4[GETBYTE(t2, 1)] & 0x0000ff00) ^
   		(Td4[GETBYTE(t1, 0)] & 0x000000ff) ^
   		rk[0];
   	s1 =
   		(Td4[GETBYTE(t1, 3)] & 0xff000000) ^
   		(Td4[GETBYTE(t0, 2)] & 0x00ff0000) ^
   		(Td4[GETBYTE(t3, 1)] & 0x0000ff00) ^
   		(Td4[GETBYTE(t2, 0)] & 0x000000ff) ^
   		rk[1];
   	s2 =
   		(Td4[GETBYTE(t2, 3)] & 0xff000000) ^
   		(Td4[GETBYTE(t1, 2)] & 0x00ff0000) ^
   		(Td4[GETBYTE(t0, 1)] & 0x0000ff00) ^
   		(Td4[GETBYTE(t3, 0)] & 0x000000ff) ^
   		rk[2];
   	s3 =
   		(Td4[GETBYTE(t3, 3)] & 0xff000000) ^
   		(Td4[GETBYTE(t2, 2)] & 0x00ff0000) ^
   		(Td4[GETBYTE(t1, 1)] & 0x0000ff00) ^
   		(Td4[GETBYTE(t0, 0)] & 0x000000ff) ^
   		rk[3];

	Block::Put(xorBlock, outBlock)(s0)(s1)(s2)(s3);
}

NAMESPACE_END