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 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 gen_tabs() and the gen_tabs() 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#define Const const
82#else
83#define Const
84#endif
85
86#if defined(DO_TABLES)
87#define Extern
88#else
89#define Extern extern
90#endif
91
92#if defined(_MSC_VER) && defined(TABLE_ALIGN)
93#define Align __declspec(align(TABLE_ALIGN))
94#else
95#define Align
96#endif
97
98#if defined(__cplusplus)
99extern "C"
100{
101#endif
102
103#if defined(DO_TABLES) && defined(FIXED_TABLES)
104#define d_1(t,n,b,e)       Align Const t n[256]    =   b(e)
105#define d_4(t,n,b,e,f,g,h) Align Const t n[4][256] = { b(e), b(f), b(g), b(h) }
106Extern Align Const aes_32t t_dec(r,c)[RC_LENGTH] = rc_data(w0);
107#else
108#define d_1(t,n,b,e)       Extern Align Const t n[256]
109#define d_4(t,n,b,e,f,g,h) Extern Align Const t n[4][256]
110Extern Align Const aes_32t t_dec(r,c)[RC_LENGTH];
111#endif
112
113#if defined( SBX_SET )
114    d_1(aes_08t, t_dec(s,box), sb_data, h0);
115#endif
116#if defined( ISB_SET )
117    d_1(aes_08t, t_dec(i,box), isb_data, h0);
118#endif
119
120#if defined( FT1_SET )
121    d_1(aes_32t, t_dec(f,n), sb_data, u0);
122#endif
123#if defined( FT4_SET )
124    d_4(aes_32t, t_dec(f,n), sb_data, u0, u1, u2, u3);
125#endif
126
127#if defined( FL1_SET )
128    d_1(aes_32t, t_dec(f,l), sb_data, w0);
129#endif
130#if defined( FL4_SET )
131    d_4(aes_32t, t_dec(f,l), sb_data, w0, w1, w2, w3);
132#endif
133
134#if defined( IT1_SET )
135    d_1(aes_32t, t_dec(i,n), isb_data, v0);
136#endif
137#if defined( IT4_SET )
138    d_4(aes_32t, t_dec(i,n), isb_data, v0, v1, v2, v3);
139#endif
140
141#if defined( IL1_SET )
142    d_1(aes_32t, t_dec(i,l), isb_data, w0);
143#endif
144#if defined( IL4_SET )
145    d_4(aes_32t, t_dec(i,l), isb_data, w0, w1, w2, w3);
146#endif
147
148#if defined( LS1_SET )
149#if defined( FL1_SET )
150#undef  LS1_SET
151#else
152    d_1(aes_32t, t_dec(l,s), sb_data, w0);
153#endif
154#endif
155
156#if defined( LS4_SET )
157#if defined( FL4_SET )
158#undef  LS4_SET
159#else
160    d_4(aes_32t, t_dec(l,s), sb_data, w0, w1, w2, w3);
161#endif
162#endif
163
164#if defined( IM1_SET )
165    d_1(aes_32t, t_dec(i,m), mm_data, v0);
166#endif
167#if defined( IM4_SET )
168    d_4(aes_32t, t_dec(i,m), mm_data, v0, v1, v2, v3);
169#endif
170
171#if defined(__cplusplus)
172}
173#endif
174
175#endif
176