cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
aestab.h
Go to the documentation of this file.
1 /*
2  ---------------------------------------------------------------------------
3  Copyright (c) 1998-2006, 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 16/04/2007
31 
32  This file contains the code for declaring the tables needed to implement
33  AES. The file aesopt.h is assumed to be included before this header file.
34  If there are no global variables, the definitions here can be used to put
35  the AES tables in a structure so that a pointer can then be added to the
36  AES context to pass them to the AES routines that need them. If this
37  facility is used, the calling program has to ensure that this pointer is
38  managed appropriately. In particular, the value of the t_dec(in,it) item
39  in the table structure must be set to zero in order to ensure that the
40  tables are initialised. In practice the three code sequences in aeskey.c
41  that control the calls to aes_init() and the aes_init() routine itself will
42  have to be changed for a specific implementation. If global variables are
43  available it will generally be preferable to use them with the precomputed
44  FIXED_TABLES option that uses static global tables.
45 
46  The following defines can be used to control the way the tables
47  are defined, initialised and used in embedded environments that
48  require special features for these purposes
49 
50  the 't_dec' construction is used to declare fixed table arrays
51  the 't_set' construction is used to set fixed table values
52  the 't_use' construction is used to access fixed table values
53 
54  256 byte tables:
55 
56  t_xxx(s,box) => forward S box
57  t_xxx(i,box) => inverse S box
58 
59  256 32-bit word OR 4 x 256 32-bit word tables:
60 
61  t_xxx(f,n) => forward normal round
62  t_xxx(f,l) => forward last round
63  t_xxx(i,n) => inverse normal round
64  t_xxx(i,l) => inverse last round
65  t_xxx(l,s) => key schedule table
66  t_xxx(i,m) => key schedule table
67 
68  Other variables and tables:
69 
70  t_xxx(r,c) => the rcon table
71 */
72 
73 #if !defined( _AESTAB_H )
74 #define _AESTAB_H
75 
76 #define t_dec(m,n) t_##m##n
77 #define t_set(m,n) t_##m##n
78 #define t_use(m,n) t_##m##n
79 
80 #if defined(FIXED_TABLES)
81 # if !defined( __GNUC__ ) && (defined( __MSDOS__ ) || defined( __WIN16__ ))
82 /* make tables far data to avoid using too much DGROUP space (PG) */
83 # define CONST const far
84 # else
85 # define CONST const
86 # endif
87 #else
88 # define CONST
89 #endif
90 
91 #if defined(__cplusplus)
92 # define EXTERN extern "C"
93 #elif defined(DO_TABLES)
94 # define EXTERN
95 #else
96 # define EXTERN extern
97 #endif
98 
99 #if defined(_MSC_VER) && defined(TABLE_ALIGN)
100 #define ALIGN __declspec(align(TABLE_ALIGN))
101 #else
102 #define ALIGN
103 #endif
104 
105 #if defined( __WATCOMC__ ) && ( __WATCOMC__ >= 1100 )
106 # define XP_DIR __cdecl
107 #else
108 # define XP_DIR
109 #endif
110 
111 #if defined(DO_TABLES) && defined(FIXED_TABLES)
112 #define d_1(t,n,b,e) EXTERN ALIGN CONST XP_DIR t n[256] = b(e)
113 #define d_4(t,n,b,e,f,g,h) EXTERN ALIGN CONST XP_DIR t n[4][256] = { b(e), b(f), b(g), b(h) }
114 EXTERN ALIGN CONST uint_32t t_dec(r,c)[RC_LENGTH] = rc_data(w0);
115 #else
116 #define d_1(t,n,b,e) EXTERN ALIGN CONST XP_DIR t n[256]
117 #define d_4(t,n,b,e,f,g,h) EXTERN ALIGN CONST XP_DIR t n[4][256]
118 EXTERN ALIGN CONST uint_32t t_dec(r,c)[RC_LENGTH];
119 #endif
120 
121 #if defined( SBX_SET )
122  d_1(uint_8t, t_dec(s,box), sb_data, h0);
123 #endif
124 #if defined( ISB_SET )
125  d_1(uint_8t, t_dec(i,box), isb_data, h0);
126 #endif
127 
128 #if defined( FT1_SET )
129  d_1(uint_32t, t_dec(f,n), sb_data, u0);
130 #endif
131 #if defined( FT4_SET )
132  d_4(uint_32t, t_dec(f,n), sb_data, u0, u1, u2, u3);
133 #endif
134 
135 #if defined( FL1_SET )
136  d_1(uint_32t, t_dec(f,l), sb_data, w0);
137 #endif
138 #if defined( FL4_SET )
139  d_4(uint_32t, t_dec(f,l), sb_data, w0, w1, w2, w3);
140 #endif
141 
142 #if defined( IT1_SET )
143  d_1(uint_32t, t_dec(i,n), isb_data, v0);
144 #endif
145 #if defined( IT4_SET )
146  d_4(uint_32t, t_dec(i,n), isb_data, v0, v1, v2, v3);
147 #endif
148 
149 #if defined( IL1_SET )
150  d_1(uint_32t, t_dec(i,l), isb_data, w0);
151 #endif
152 #if defined( IL4_SET )
153  d_4(uint_32t, t_dec(i,l), isb_data, w0, w1, w2, w3);
154 #endif
155 
156 #if defined( LS1_SET )
157 #if defined( FL1_SET )
158 #undef LS1_SET
159 #else
160  d_1(uint_32t, t_dec(l,s), sb_data, w0);
161 #endif
162 #endif
163 
164 #if defined( LS4_SET )
165 #if defined( FL4_SET )
166 #undef LS4_SET
167 #else
168  d_4(uint_32t, t_dec(l,s), sb_data, w0, w1, w2, w3);
169 #endif
170 #endif
171 
172 #if defined( IM1_SET )
173  d_1(uint_32t, t_dec(i,m), mm_data, v0);
174 #endif
175 #if defined( IM4_SET )
176  d_4(uint_32t, t_dec(i,m), mm_data, v0, v1, v2, v3);
177 #endif
178 
179 #endif