|
|
1.1 root 1: /*
2: ---------------------------------------------------------------------------
3: Copyright (c) 2003, Dr Brian Gladman, Worcester, UK. All rights reserved.
4:
5: LICENSE TERMS
6:
7: The free distribution and use of this software in both source and binary
8: form is allowed (with or without changes) provided that:
9:
10: 1. distributions of this source code include the above copyright
11: notice, this list of conditions and the following disclaimer;
12:
13: 2. distributions in binary form include the above copyright
14: notice, this list of conditions and the following disclaimer
15: in the documentation and/or other associated materials;
16:
17: 3. the copyright holder's name is not used to endorse products
18: built using this software without specific written permission.
19:
20: ALTERNATIVELY, provided that this notice is retained in full, this product
21: may be distributed under the terms of the GNU General Public License (GPL),
22: in which case the provisions of the GPL apply INSTEAD OF those given above.
23:
24: DISCLAIMER
25:
26: This software is provided 'as is' with no explicit or implied warranties
27: in respect of its properties, including, but not limited to, correctness
28: and/or fitness for purpose.
29: ---------------------------------------------------------------------------
30: Issue 28/01/2004
31:
32: My thanks go to Dag Arne Osvik for devising the schemes used here for key
33: length derivation from the form of the key schedule
34:
35: This file contains the compilation options for AES (Rijndael) and code
36: that is common across encryption, key scheduling and table generation.
37:
38: OPERATION
39:
40: These source code files implement the AES algorithm Rijndael designed by
41: Joan Daemen and Vincent Rijmen. This version is designed for the standard
42: block size of 16 bytes and for key sizes of 128, 192 and 256 bits (16, 24
43: and 32 bytes).
44:
45: This version is designed for flexibility and speed using operations on
46: 32-bit words rather than operations on bytes. It can be compiled with
47: either big or little endian internal byte order but is faster when the
48: native byte order for the processor is used.
49:
50: THE CIPHER INTERFACE
51:
52: The cipher interface is implemented as an array of bytes in which lower
53: AES bit sequence indexes map to higher numeric significance within bytes.
54:
55: aes_08t (an unsigned 8-bit type)
56: aes_32t (an unsigned 32-bit type)
57: struct aes_encrypt_ctx (structure for the cipher encryption context)
58: struct aes_decrypt_ctx (structure for the cipher decryption context)
59: aes_rval the function return type
60:
61: C subroutine calls:
62:
63: aes_rval aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
64: aes_rval aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
65: aes_rval aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
66: aes_rval aes_encrypt(const unsigned char *in, unsigned char *out,
67: const aes_encrypt_ctx cx[1]);
68:
69: aes_rval aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
70: aes_rval aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
71: aes_rval aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
72: aes_rval aes_decrypt(const unsigned char *in, unsigned char *out,
73: const aes_decrypt_ctx cx[1]);
74:
75: IMPORTANT NOTE: If you are using this C interface with dynamic tables make sure that
76: you call genTabs() before AES is used so that the tables are initialised.
77:
78: C++ aes class subroutines:
79:
80: Class AESencrypt for encryption
81:
82: Construtors:
83: AESencrypt(void)
84: AESencrypt(const unsigned char *key) - 128 bit key
85: Members:
86: aes_rval key128(const unsigned char *key)
87: aes_rval key192(const unsigned char *key)
88: aes_rval key256(const unsigned char *key)
89: aes_rval encrypt(const unsigned char *in, unsigned char *out) const
90:
91: Class AESdecrypt for encryption
92: Construtors:
93: AESdecrypt(void)
94: AESdecrypt(const unsigned char *key) - 128 bit key
95: Members:
96: aes_rval key128(const unsigned char *key)
97: aes_rval key192(const unsigned char *key)
98: aes_rval key256(const unsigned char *key)
99: aes_rval decrypt(const unsigned char *in, unsigned char *out) const
100:
101: COMPILATION
102:
103: The files used to provide AES (Rijndael) are
104:
105: a. aes.h for the definitions needed for use in C.
106: b. aescpp.h for the definitions needed for use in C++.
107: c. aesopt.h for setting compilation options (also includes common code).
108: d. aescrypt.c for encryption and decrytpion, or
109: e. aeskey.c for key scheduling.
110: f. aestab.c for table loading or generation.
111: g. aescrypt.asm for encryption and decryption using assembler code.
112: h. aescrypt.mmx.asm for encryption and decryption using MMX assembler.
113:
114: To compile AES (Rijndael) for use in C code use aes.h and set the
115: defines here for the facilities you need (key lengths, encryption
116: and/or decryption). Do not define AES_DLL or AES_CPP. Set the options
117: for optimisations and table sizes here.
118:
119: To compile AES (Rijndael) for use in in C++ code use aescpp.h but do
120: not define AES_DLL
121:
122: To compile AES (Rijndael) in C as a Dynamic Link Library DLL) use
123: aes.h and include the AES_DLL define.
124:
125: CONFIGURATION OPTIONS (here and in aes.h)
126:
127: a. set AES_DLL in aes.h if AES (Rijndael) is to be compiled as a DLL
128: b. You may need to set PLATFORM_BYTE_ORDER to define the byte order.
129: c. If you want the code to run in a specific internal byte order, then
130: ALGORITHM_BYTE_ORDER must be set accordingly.
131: d. set other configuration options decribed below.
132: */
133:
134: #if !defined( _AESOPT_H )
135: #define _AESOPT_H
136:
1.1.1.2 ! root 137: #include "Aes.h"
1.1 root 138:
139: /* CONFIGURATION - USE OF DEFINES
140:
141: Later in this section there are a number of defines that control the
142: operation of the code. In each section, the purpose of each define is
143: explained so that the relevant form can be included or excluded by
144: setting either 1's or 0's respectively on the branches of the related
145: #if clauses.
146:
147: PLATFORM SPECIFIC INCLUDES AND BYTE ORDER IN 32-BIT WORDS
148:
149: To obtain the highest speed on processors with 32-bit words, this code
150: needs to determine the byte order of the target machine. The following
151: block of code is an attempt to capture the most obvious ways in which
152: various environemnts define byte order. It may well fail, in which case
153: the definitions will need to be set by editing at the points marked
154: **** EDIT HERE IF NECESSARY **** below. My thanks go to Peter Gutmann
155: for his assistance with this endian detection nightmare.
156: */
157:
1.1.1.2 ! root 158: /* Adapted for TrueCrypt by the TrueCrypt Foundation */
! 159: #include "Endian.h"
! 160:
1.1 root 161: #define BRG_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
162: #define BRG_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
163:
1.1.1.2 ! root 164: #if BYTE_ORDER == LITTLE_ENDIAN
1.1 root 165: # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
166: #endif
167:
1.1.1.2 ! root 168: #if BYTE_ORDER == BIG_ENDIAN
! 169: # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
1.1 root 170: #endif
171:
172: /* SOME LOCAL DEFINITIONS */
173:
174: #define NO_TABLES 0
175: #define ONE_TABLE 1
176: #define FOUR_TABLES 4
177: #define NONE 0
178: #define PARTIAL 1
179: #define FULL 2
180:
181: #if defined(bswap32)
182: #define aes_sw32 bswap32
183: #elif defined(bswap_32)
184: #define aes_sw32 bswap_32
185: #else
186: #define brot(x,n) (((aes_32t)(x) << n) | ((aes_32t)(x) >> (32 - n)))
187: #define aes_sw32(x) ((brot((x),8) & 0x00ff00ff) | (brot((x),24) & 0xff00ff00))
188: #endif
189:
190: /* 1. FUNCTIONS REQUIRED
191:
192: This implementation provides subroutines for encryption, decryption
193: and for setting the three key lengths (separately) for encryption
194: and decryption. When the assembler code is not being used the following
195: definition blocks allow the selection of the routines that are to be
196: included in the compilation.
197: */
198: #if defined( AES_ENCRYPT )
199: #define ENCRYPTION
200: #define ENCRYPTION_KEY_SCHEDULE
201: #endif
202:
203: #if defined( AES_DECRYPT )
204: #define DECRYPTION
205: #define DECRYPTION_KEY_SCHEDULE
206: #endif
207:
208: /* 2. ASSEMBLER SUPPORT
209:
210: This define (which can be on the command line) enables the use of the
211: assembler code routines for encryption and decryption with the C code
212: only providing key scheduling
213: */
214: #if 0 && !defined(AES_ASM)
215: #define AES_ASM
216: #endif
217:
218: /* 3. BYTE ORDER WITHIN 32 BIT WORDS
219:
220: The fundamental data processing units in Rijndael are 8-bit bytes. The
221: input, output and key input are all enumerated arrays of bytes in which
222: bytes are numbered starting at zero and increasing to one less than the
223: number of bytes in the array in question. This enumeration is only used
224: for naming bytes and does not imply any adjacency or order relationship
225: from one byte to another. When these inputs and outputs are considered
226: as bit sequences, bits 8*n to 8*n+7 of the bit sequence are mapped to
227: byte[n] with bit 8n+i in the sequence mapped to bit 7-i within the byte.
228: In this implementation bits are numbered from 0 to 7 starting at the
229: numerically least significant end of each byte (bit n represents 2^n).
230:
231: However, Rijndael can be implemented more efficiently using 32-bit
232: words by packing bytes into words so that bytes 4*n to 4*n+3 are placed
233: into word[n]. While in principle these bytes can be assembled into words
234: in any positions, this implementation only supports the two formats in
235: which bytes in adjacent positions within words also have adjacent byte
236: numbers. This order is called big-endian if the lowest numbered bytes
237: in words have the highest numeric significance and little-endian if the
238: opposite applies.
239:
240: This code can work in either order irrespective of the order used by the
241: machine on which it runs. Normally the internal byte order will be set
242: to the order of the processor on which the code is to be run but this
243: define can be used to reverse this in special situations
244:
245: NOTE: Assembler code versions rely on PLATFORM_BYTE_ORDER being set
246: */
247: #if 1 || defined(AES_ASM)
248: #define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER
249: #elif 0
250: #define ALGORITHM_BYTE_ORDER BRG_LITTLE_ENDIAN
251: #elif 0
252: #define ALGORITHM_BYTE_ORDER BRG_BIG_ENDIAN
253: #else
254: #error The algorithm byte order is not defined
255: #endif
256:
257: /* 4. FAST INPUT/OUTPUT OPERATIONS.
258:
259: On some machines it is possible to improve speed by transferring the
260: bytes in the input and output arrays to and from the internal 32-bit
261: variables by addressing these arrays as if they are arrays of 32-bit
262: words. On some machines this will always be possible but there may
263: be a large performance penalty if the byte arrays are not aligned on
264: the normal word boundaries. On other machines this technique will
265: lead to memory access errors when such 32-bit word accesses are not
266: properly aligned. The option SAFE_IO avoids such problems but will
267: often be slower on those machines that support misaligned access
268: (especially so if care is taken to align the input and output byte
269: arrays on 32-bit word boundaries). If SAFE_IO is not defined it is
270: assumed that access to byte arrays as if they are arrays of 32-bit
271: words will not cause problems when such accesses are misaligned.
272: */
273: #if 1 && !defined(_MSC_VER)
274: #define SAFE_IO
275: #endif
276:
277: /* 5. LOOP UNROLLING
278:
279: The code for encryption and decrytpion cycles through a number of rounds
280: that can be implemented either in a loop or by expanding the code into a
281: long sequence of instructions, the latter producing a larger program but
282: one that will often be much faster. The latter is called loop unrolling.
283: There are also potential speed advantages in expanding two iterations in
284: a loop with half the number of iterations, which is called partial loop
285: unrolling. The following options allow partial or full loop unrolling
286: to be set independently for encryption and decryption
287: */
288: #if 1
289: #define ENC_UNROLL FULL
290: #elif 0
291: #define ENC_UNROLL PARTIAL
292: #else
293: #define ENC_UNROLL NONE
294: #endif
295:
296: #if 1
297: #define DEC_UNROLL FULL
298: #elif 0
299: #define DEC_UNROLL PARTIAL
300: #else
301: #define DEC_UNROLL NONE
302: #endif
303:
304: /* 6. FAST FINITE FIELD OPERATIONS
305:
306: If this section is included, tables are used to provide faster finite
307: field arithmetic (this has no effect if FIXED_TABLES is defined).
308: */
309: #if 1
310: #define FF_TABLES
311: #endif
312:
313: /* 7. INTERNAL STATE VARIABLE FORMAT
314:
315: The internal state of Rijndael is stored in a number of local 32-bit
316: word varaibles which can be defined either as an array or as individual
317: names variables. Include this section if you want to store these local
318: varaibles in arrays. Otherwise individual local variables will be used.
319: */
320: #if 1
321: #define ARRAYS
322: #endif
323:
324: /* In this implementation the columns of the state array are each held in
325: 32-bit words. The state array can be held in various ways: in an array
326: of words, in a number of individual word variables or in a number of
327: processor registers. The following define maps a variable name x and
328: a column number c to the way the state array variable is to be held.
329: The first define below maps the state into an array x[c] whereas the
330: second form maps the state into a number of individual variables x0,
331: x1, etc. Another form could map individual state colums to machine
332: register names.
333: */
334:
335: #if defined(ARRAYS)
336: #define s(x,c) x[c]
337: #else
338: #define s(x,c) x##c
339: #endif
340:
341: /* 8. FIXED OR DYNAMIC TABLES
342:
343: When this section is included the tables used by the code are compiled
344: statically into the binary file. Otherwise the subroutine gen_tabs()
345: must be called to compute them before the code is first used.
346: */
347: #if 1
348: #define FIXED_TABLES
349: #endif
350:
351: /* 9. TABLE ALIGNMENT
352:
353: On some sytsems speed will be improved by aligning the AES large lookup
354: tables on particular boundaries. This define should be set to a power of
355: two giving the desired alignment. It can be left undefined if alignment
356: is not needed. This option is specific to the Microsft VC++ compiler -
357: it seems to sometimes cause trouble for the VC++ version 6 compiler.
358: */
359:
360: #if 0 && defined(_MSC_VER) && (_MSC_VER >= 1300)
361: #define TABLE_ALIGN 64
362: #endif
363:
364: /* 10. INTERNAL TABLE CONFIGURATION
365:
366: This cipher proceeds by repeating in a number of cycles known as 'rounds'
367: which are implemented by a round function which can optionally be speeded
368: up using tables. The basic tables are each 256 32-bit words, with either
369: one or four tables being required for each round function depending on
370: how much speed is required. The encryption and decryption round functions
371: are different and the last encryption and decrytpion round functions are
372: different again making four different round functions in all.
373:
374: This means that:
375: 1. Normal encryption and decryption rounds can each use either 0, 1
376: or 4 tables and table spaces of 0, 1024 or 4096 bytes each.
377: 2. The last encryption and decryption rounds can also use either 0, 1
378: or 4 tables and table spaces of 0, 1024 or 4096 bytes each.
379:
380: Include or exclude the appropriate definitions below to set the number
381: of tables used by this implementation.
382: */
383:
384: #if 1 /* set tables for the normal encryption round */
385: #define ENC_ROUND FOUR_TABLES
386: #elif 0
387: #define ENC_ROUND ONE_TABLE
388: #else
389: #define ENC_ROUND NO_TABLES
390: #endif
391:
392: #if 1 /* set tables for the last encryption round */
393: #define LAST_ENC_ROUND FOUR_TABLES
394: #elif 0
395: #define LAST_ENC_ROUND ONE_TABLE
396: #else
397: #define LAST_ENC_ROUND NO_TABLES
398: #endif
399:
400: #if 1 /* set tables for the normal decryption round */
401: #define DEC_ROUND FOUR_TABLES
402: #elif 0
403: #define DEC_ROUND ONE_TABLE
404: #else
405: #define DEC_ROUND NO_TABLES
406: #endif
407:
408: #if 1 /* set tables for the last decryption round */
409: #define LAST_DEC_ROUND FOUR_TABLES
410: #elif 0
411: #define LAST_DEC_ROUND ONE_TABLE
412: #else
413: #define LAST_DEC_ROUND NO_TABLES
414: #endif
415:
416: /* The decryption key schedule can be speeded up with tables in the same
417: way that the round functions can. Include or exclude the following
418: defines to set this requirement.
419: */
420: #if 1
421: #define KEY_SCHED FOUR_TABLES
422: #elif 0
423: #define KEY_SCHED ONE_TABLE
424: #else
425: #define KEY_SCHED NO_TABLES
426: #endif
427:
428: /* END OF CONFIGURATION OPTIONS */
429:
430: #define RC_LENGTH (5 * (AES_BLOCK_SIZE / 4 - 2))
431:
432: /* Disable or report errors on some combinations of options */
433:
434: #if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES
435: #undef LAST_ENC_ROUND
436: #define LAST_ENC_ROUND NO_TABLES
437: #elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES
438: #undef LAST_ENC_ROUND
439: #define LAST_ENC_ROUND ONE_TABLE
440: #endif
441:
442: #if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE
443: #undef ENC_UNROLL
444: #define ENC_UNROLL NONE
445: #endif
446:
447: #if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES
448: #undef LAST_DEC_ROUND
449: #define LAST_DEC_ROUND NO_TABLES
450: #elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES
451: #undef LAST_DEC_ROUND
452: #define LAST_DEC_ROUND ONE_TABLE
453: #endif
454:
455: #if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE
456: #undef DEC_UNROLL
457: #define DEC_UNROLL NONE
458: #endif
459:
460: /* upr(x,n): rotates bytes within words by n positions, moving bytes to
461: higher index positions with wrap around into low positions
462: ups(x,n): moves bytes by n positions to higher index positions in
463: words but without wrap around
464: bval(x,n): extracts a byte from a word
465:
466: NOTE: The definitions given here are intended only for use with
467: unsigned variables and with shift counts that are compile
468: time constants
469: */
470:
471: #if (ALGORITHM_BYTE_ORDER == BRG_LITTLE_ENDIAN)
472: #define upr(x,n) (((aes_32t)(x) << (8 * (n))) | ((aes_32t)(x) >> (32 - 8 * (n))))
473: #define ups(x,n) ((aes_32t) (x) << (8 * (n)))
474: #define bval(x,n) ((aes_08t)((x) >> (8 * (n))))
475: #define bytes2word(b0, b1, b2, b3) \
476: (((aes_32t)(b3) << 24) | ((aes_32t)(b2) << 16) | ((aes_32t)(b1) << 8) | (b0))
477: #endif
478:
479: #if (ALGORITHM_BYTE_ORDER == BRG_BIG_ENDIAN)
480: #define upr(x,n) (((aes_32t)(x) >> (8 * (n))) | ((aes_32t)(x) << (32 - 8 * (n))))
481: #define ups(x,n) ((aes_32t) (x) >> (8 * (n))))
482: #define bval(x,n) ((aes_08t)((x) >> (24 - 8 * (n))))
483: #define bytes2word(b0, b1, b2, b3) \
484: (((aes_32t)(b0) << 24) | ((aes_32t)(b1) << 16) | ((aes_32t)(b2) << 8) | (b3))
485: #endif
486:
487: #if defined(SAFE_IO)
488:
489: #define word_in(x,c) bytes2word(((aes_08t*)(x)+4*c)[0], ((aes_08t*)(x)+4*c)[1], \
490: ((aes_08t*)(x)+4*c)[2], ((aes_08t*)(x)+4*c)[3])
491: #define word_out(x,c,v) { ((aes_08t*)(x)+4*c)[0] = bval(v,0); ((aes_08t*)(x)+4*c)[1] = bval(v,1); \
492: ((aes_08t*)(x)+4*c)[2] = bval(v,2); ((aes_08t*)(x)+4*c)[3] = bval(v,3); }
493:
494: #elif (ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER)
495:
496: #define word_in(x,c) (*((aes_32t*)(x)+(c)))
497: #define word_out(x,c,v) (*((aes_32t*)(x)+(c)) = (v))
498:
499: #else
500:
501: #define word_in(x,c) aes_sw32(*((aes_32t*)(x)+(c)))
502: #define word_out(x,c,v) (*((aes_32t*)(x)+(c)) = aes_sw32(v))
503:
504: #endif
505:
506: /* the finite field modular polynomial and elements */
507:
508: #define WPOLY 0x011b
509: #define BPOLY 0x1b
510:
511: /* multiply four bytes in GF(2^8) by 'x' {02} in parallel */
512:
513: #define m1 0x80808080
514: #define m2 0x7f7f7f7f
515: #define gf_mulx(x) ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * BPOLY))
516:
517: /* The following defines provide alternative definitions of gf_mulx that might
518: give improved performance if a fast 32-bit multiply is not available. Note
519: that a temporary variable u needs to be defined where gf_mulx is used.
520:
521: #define gf_mulx(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ ((u >> 3) | (u >> 6))
522: #define m4 (0x01010101 * BPOLY)
523: #define gf_mulx(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) & m4)
524: */
525:
526: /* Work out which tables are needed for the different options */
527:
528: #if defined( AES_ASM )
529: #if defined( ENC_ROUND )
530: #undef ENC_ROUND
531: #endif
532: #define ENC_ROUND FOUR_TABLES
533: #if defined( LAST_ENC_ROUND )
534: #undef LAST_ENC_ROUND
535: #endif
536: #define LAST_ENC_ROUND FOUR_TABLES
537: #if defined( DEC_ROUND )
538: #undef DEC_ROUND
539: #endif
540: #define DEC_ROUND FOUR_TABLES
541: #if defined( LAST_DEC_ROUND )
542: #undef LAST_DEC_ROUND
543: #endif
544: #define LAST_DEC_ROUND FOUR_TABLES
545: #if defined( KEY_SCHED )
546: #undef KEY_SCHED
547: #define KEY_SCHED FOUR_TABLES
548: #endif
549: #endif
550:
551: #if defined(ENCRYPTION) || defined(AES_ASM)
552: #if ENC_ROUND == ONE_TABLE
553: #define FT1_SET
554: #elif ENC_ROUND == FOUR_TABLES
555: #define FT4_SET
556: #else
557: #define SBX_SET
558: #endif
559: #if LAST_ENC_ROUND == ONE_TABLE
560: #define FL1_SET
561: #elif LAST_ENC_ROUND == FOUR_TABLES
562: #define FL4_SET
563: #elif !defined(SBX_SET)
564: #define SBX_SET
565: #endif
566: #endif
567:
568: #if defined(DECRYPTION) || defined(AES_ASM)
569: #if DEC_ROUND == ONE_TABLE
570: #define IT1_SET
571: #elif DEC_ROUND == FOUR_TABLES
572: #define IT4_SET
573: #else
574: #define ISB_SET
575: #endif
576: #if LAST_DEC_ROUND == ONE_TABLE
577: #define IL1_SET
578: #elif LAST_DEC_ROUND == FOUR_TABLES
579: #define IL4_SET
580: #elif !defined(ISB_SET)
581: #define ISB_SET
582: #endif
583: #endif
584:
585: #if defined(ENCRYPTION_KEY_SCHEDULE) || defined(DECRYPTION_KEY_SCHEDULE)
586: #if KEY_SCHED == ONE_TABLE
587: #define LS1_SET
588: #define IM1_SET
589: #elif KEY_SCHED == FOUR_TABLES
590: #define LS4_SET
591: #define IM4_SET
592: #elif !defined(SBX_SET)
593: #define SBX_SET
594: #endif
595: #endif
596:
597: /* generic definitions of Rijndael macros that use tables */
598:
599: #define no_table(x,box,vf,rf,c) bytes2word( \
600: box[bval(vf(x,0,c),rf(0,c))], \
601: box[bval(vf(x,1,c),rf(1,c))], \
602: box[bval(vf(x,2,c),rf(2,c))], \
603: box[bval(vf(x,3,c),rf(3,c))])
604:
605: #define one_table(x,op,tab,vf,rf,c) \
606: ( tab[bval(vf(x,0,c),rf(0,c))] \
607: ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \
608: ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \
609: ^ op(tab[bval(vf(x,3,c),rf(3,c))],3))
610:
611: #define four_tables(x,tab,vf,rf,c) \
612: ( tab[0][bval(vf(x,0,c),rf(0,c))] \
613: ^ tab[1][bval(vf(x,1,c),rf(1,c))] \
614: ^ tab[2][bval(vf(x,2,c),rf(2,c))] \
615: ^ tab[3][bval(vf(x,3,c),rf(3,c))])
616:
617: #define vf1(x,r,c) (x)
618: #define rf1(r,c) (r)
619: #define rf2(r,c) ((8+r-c)&3)
620:
621: /* perform forward and inverse column mix operation on four bytes in long word x in */
622: /* parallel. NOTE: x must be a simple variable, NOT an expression in these macros. */
623:
624: #if defined(FM4_SET) /* not currently used */
625: #define fwd_mcol(x) four_tables(x,t_use(f,m),vf1,rf1,0)
626: #elif defined(FM1_SET) /* not currently used */
627: #define fwd_mcol(x) one_table(x,upr,t_use(f,m),vf1,rf1,0)
628: #else
629: #define dec_fmvars aes_32t g2
630: #define fwd_mcol(x) (g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ upr((x), 2) ^ upr((x), 1))
631: #endif
632:
633: #if defined(IM4_SET)
634: #define inv_mcol(x) four_tables(x,t_use(i,m),vf1,rf1,0)
635: #elif defined(IM1_SET)
636: #define inv_mcol(x) one_table(x,upr,t_use(i,m),vf1,rf1,0)
637: #else
638: #define dec_imvars aes_32t g2, g4, g9
639: #define inv_mcol(x) (g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = (x) ^ gf_mulx(g4), g4 ^= g9, \
640: (x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ upr(g4, 2) ^ upr(g9, 1))
641: #endif
642:
643: #if defined(FL4_SET)
644: #define ls_box(x,c) four_tables(x,t_use(f,l),vf1,rf2,c)
645: #elif defined(LS4_SET)
646: #define ls_box(x,c) four_tables(x,t_use(l,s),vf1,rf2,c)
647: #elif defined(FL1_SET)
648: #define ls_box(x,c) one_table(x,upr,t_use(f,l),vf1,rf2,c)
649: #elif defined(LS1_SET)
650: #define ls_box(x,c) one_table(x,upr,t_use(l,s),vf1,rf2,c)
651: #else
652: #define ls_box(x,c) no_table(x,t_use(s,box),vf1,rf2,c)
653: #endif
654:
655: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.