Optionally display the value of several variables within the Status command.
[asterisk-bristuff.git] / main / aestab.c
blobb134d22d83d8ac2a07b6194b9710bdd48d955f52
1 /*
2 ---------------------------------------------------------------------------
3 Copyright (c) 2003, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
4 All rights reserved.
6 LICENSE TERMS
8 The free distribution and use of this software in both source and binary
9 form is allowed (with or without changes) provided that:
11 1. distributions of this source code include the above copyright
12 notice, this list of conditions and the following disclaimer;
14 2. distributions in binary form include the above copyright
15 notice, this list of conditions and the following disclaimer
16 in the documentation and/or other associated materials;
18 3. the copyright holder's name is not used to endorse products
19 built using this software without specific written permission.
21 ALTERNATIVELY, provided that this notice is retained in full, this product
22 may be distributed under the terms of the GNU General Public License (GPL),
23 in which case the provisions of the GPL apply INSTEAD OF those given above.
25 DISCLAIMER
27 This software is provided 'as is' with no explicit or implied warranties
28 in respect of its properties, including, but not limited to, correctness
29 and/or fitness for purpose.
30 ---------------------------------------------------------------------------
31 Issue Date: 26/08/2003
35 #if defined(__cplusplus)
36 extern "C"
38 #endif
40 #ifndef HAVE_CRYPTO
42 #define DO_TABLES
44 #include "aesopt.h"
46 #if defined(FIXED_TABLES)
48 /* implemented in case of wrong call for fixed tables */
50 void gen_tabs(void)
54 #else /* dynamic table generation */
56 #if !defined(FF_TABLES)
58 /* Generate the tables for the dynamic table option
60 It will generally be sensible to use tables to compute finite
61 field multiplies and inverses but where memory is scarse this
62 code might sometimes be better. But it only has effect during
63 initialisation so its pretty unimportant in overall terms.
66 /* return 2 ^ (n - 1) where n is the bit number of the highest bit
67 set in x with x in the range 1 < x < 0x00000200. This form is
68 used so that locals within fi can be bytes rather than words
71 static aes_08t hibit(const aes_32t x)
72 { aes_08t r = (aes_08t)((x >> 1) | (x >> 2));
74 r |= (r >> 2);
75 r |= (r >> 4);
76 return (r + 1) >> 1;
79 /* return the inverse of the finite field element x */
81 static aes_08t fi(const aes_08t x)
82 { aes_08t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;
84 if(x < 2) return x;
86 for(;;)
88 if(!n1) return v1;
90 while(n2 >= n1)
92 n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2);
95 if(!n2) return v2;
97 while(n1 >= n2)
99 n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1);
104 #endif
106 /* The forward and inverse affine transformations used in the S-box */
108 #define fwd_affine(x) \
109 (w = (aes_32t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(aes_08t)(w^(w>>8)))
111 #define inv_affine(x) \
112 (w = (aes_32t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(aes_08t)(w^(w>>8)))
114 static int init = 0;
116 void gen_tabs(void)
117 { aes_32t i, w;
119 #if defined(FF_TABLES)
121 aes_08t pow[512], log[256];
123 if(init) return;
124 /* log and power tables for GF(2^8) finite field with
125 WPOLY as modular polynomial - the simplest primitive
126 root is 0x03, used here to generate the tables
129 i = 0; w = 1;
132 pow[i] = (aes_08t)w;
133 pow[i + 255] = (aes_08t)w;
134 log[w] = (aes_08t)i++;
135 w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0);
137 while (w != 1);
139 #else
140 if(init) return;
141 #endif
143 for(i = 0, w = 1; i < RC_LENGTH; ++i)
145 t_set(r,c)[i] = bytes2word(w, 0, 0, 0);
146 w = f2(w);
149 for(i = 0; i < 256; ++i)
150 { aes_08t b;
152 b = fwd_affine(fi((aes_08t)i));
153 w = bytes2word(f2(b), b, b, f3(b));
155 #ifdef SBX_SET
156 t_set(s,box)[i] = b;
157 #endif
159 #ifdef FT1_SET /* tables for a normal encryption round */
160 t_set(f,n)[i] = w;
161 #endif
162 #ifdef FT4_SET
163 t_set(f,n)[0][i] = w;
164 t_set(f,n)[1][i] = upr(w,1);
165 t_set(f,n)[2][i] = upr(w,2);
166 t_set(f,n)[3][i] = upr(w,3);
167 #endif
168 w = bytes2word(b, 0, 0, 0);
170 #ifdef FL1_SET /* tables for last encryption round (may also */
171 t_set(f,l)[i] = w; /* be used in the key schedule) */
172 #endif
173 #ifdef FL4_SET
174 t_set(f,l)[0][i] = w;
175 t_set(f,l)[1][i] = upr(w,1);
176 t_set(f,l)[2][i] = upr(w,2);
177 t_set(f,l)[3][i] = upr(w,3);
178 #endif
180 #ifdef LS1_SET /* table for key schedule if t_set(f,l) above is */
181 t_set(l,s)[i] = w; /* not of the required form */
182 #endif
183 #ifdef LS4_SET
184 t_set(l,s)[0][i] = w;
185 t_set(l,s)[1][i] = upr(w,1);
186 t_set(l,s)[2][i] = upr(w,2);
187 t_set(l,s)[3][i] = upr(w,3);
188 #endif
190 b = fi(inv_affine((aes_08t)i));
191 w = bytes2word(fe(b), f9(b), fd(b), fb(b));
193 #ifdef IM1_SET /* tables for the inverse mix column operation */
194 t_set(i,m)[b] = w;
195 #endif
196 #ifdef IM4_SET
197 t_set(i,m)[0][b] = w;
198 t_set(i,m)[1][b] = upr(w,1);
199 t_set(i,m)[2][b] = upr(w,2);
200 t_set(i,m)[3][b] = upr(w,3);
201 #endif
203 #ifdef ISB_SET
204 t_set(i,box)[i] = b;
205 #endif
206 #ifdef IT1_SET /* tables for a normal decryption round */
207 t_set(i,n)[i] = w;
208 #endif
209 #ifdef IT4_SET
210 t_set(i,n)[0][i] = w;
211 t_set(i,n)[1][i] = upr(w,1);
212 t_set(i,n)[2][i] = upr(w,2);
213 t_set(i,n)[3][i] = upr(w,3);
214 #endif
215 w = bytes2word(b, 0, 0, 0);
216 #ifdef IL1_SET /* tables for last decryption round */
217 t_set(i,l)[i] = w;
218 #endif
219 #ifdef IL4_SET
220 t_set(i,l)[0][i] = w;
221 t_set(i,l)[1][i] = upr(w,1);
222 t_set(i,l)[2][i] = upr(w,2);
223 t_set(i,l)[3][i] = upr(w,3);
224 #endif
226 init = 1;
229 #endif
231 #endif /* !HAVE_CRYPTO */
233 #if defined(__cplusplus)
235 #endif