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