Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(408)

Side by Side Diff: srtp/tables/aes_tables.c

Issue 2344973002: Update libsrtp to version 2.0 (Closed)
Patch Set: Add '.' back to include_dirs Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « srtp/srtp/srtp.c ('k') | srtp/test/.cvsignore » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * aes_tables.c
3 *
4 * generate tables for the AES cipher
5 *
6 * David A. McGrew
7 * Cisco Systems, Inc.
8 */
9 /*
10 *
11 * Copyright(c) 2001-2006 Cisco Systems, Inc.
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials provided
24 * with the distribution.
25 *
26 * Neither the name of the Cisco Systems, Inc. nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41 * OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 */
44
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif
48
49 #include <stdio.h>
50 #include "gf2_8.h"
51 #include "crypto_math.h"
52
53
54 unsigned char aes_sbox[256];
55
56 unsigned char aes_inv_sbox[256];
57
58 uint32_t T0[256], T1[256], T2[256], T3[256], T4[256];
59
60
61 #define AES_INVERSE_TEST 0 /* set to 1 to test forward/backwards aes */
62
63 /* functions for precomputing AES values */
64
65 /*
66 * A[] is the 8 x 8 binary matrix (represented as an array of columns,
67 * where each column is an octet) which defines the affine
68 * transformation used in the AES substitution table (Section
69 * 4.2.1 of the spec).
70 */
71
72 uint8_t A[8] = { 31, 62, 124, 248, 241, 227, 199, 143 };
73
74 /*
75 * b is the 8 bit vector (represented as an octet) used in the affine
76 * transform described above.
77 */
78
79 uint8_t b = 99;
80
81
82 void
83 aes_init_sbox(void) {
84 unsigned int i;
85 uint8_t x;
86
87 for (i=0; i < 256; i++) {
88 x = gf2_8_compute_inverse((gf2_8)i);
89 x = A_times_x_plus_b(A, x, b);
90 aes_sbox[i] = x;
91 aes_inv_sbox[x] = i;
92 }
93 }
94
95 void
96 aes_compute_tables(void) {
97 int i;
98 uint32_t x1, x2, x3;
99 v32_t tmp;
100
101 /* initialize substitution table */
102 aes_init_sbox();
103
104 /* combine sbox with linear operations to form 8-bit to 32-bit tables */
105 for (i=0; i < 256; i++) {
106 x1 = aes_sbox[i];
107 x2 = gf2_8_shift(x1);
108 x3 = x2 ^ x1;
109
110 tmp.v8[0] = x2;
111 tmp.v8[1] = x1;
112 tmp.v8[2] = x1;
113 tmp.v8[3] = x3;
114 T0[i] = tmp.value;
115
116 tmp.v8[0] = x3;
117 tmp.v8[1] = x2;
118 tmp.v8[2] = x1;
119 tmp.v8[3] = x1;
120 T1[i] = tmp.value;
121
122 tmp.v8[0] = x1;
123 tmp.v8[1] = x3;
124 tmp.v8[2] = x2;
125 tmp.v8[3] = x1;
126 T2[i] = tmp.value;
127
128 tmp.v8[0] = x1;
129 tmp.v8[1] = x1;
130 tmp.v8[2] = x3;
131 tmp.v8[3] = x2;
132 T3[i] = tmp.value;
133
134 }
135 }
136
137
138 /*
139 * the tables U0, U1, U2, U3 implement the aes operations invSubBytes,
140 * invMixColumns, and invShiftRows
141 */
142
143 uint32_t U0[256], U1[256], U2[256], U3[256], U4[256];
144
145 extern uint8_t aes_inv_sbox[256];
146
147 void
148 aes_compute_inv_tables(void) {
149 int i;
150 uint8_t x, xe, x9, xd, xb;
151 v32_t tmp;
152
153 /* combine sbox with linear operations to form 8-bit to 32-bit tables */
154 for (i=0; i < 256; i++) {
155 x = aes_inv_sbox[i];
156
157 xe = gf2_8_multiply(0x0e, x);
158 x9 = gf2_8_multiply(0x09, x);
159 xd = gf2_8_multiply(0x0d, x);
160 xb = gf2_8_multiply(0x0b, x);
161
162 tmp.v8[0] = xe;
163 tmp.v8[1] = x9;
164 tmp.v8[2] = xd;
165 tmp.v8[3] = xb;
166 U0[i] = tmp.value;
167
168 tmp.v8[0] = xb;
169 tmp.v8[1] = xe;
170 tmp.v8[2] = x9;
171 tmp.v8[3] = xd;
172 U1[i] = tmp.value;
173
174 tmp.v8[0] = xd;
175 tmp.v8[1] = xb;
176 tmp.v8[2] = xe;
177 tmp.v8[3] = x9;
178 U2[i] = tmp.value;
179
180 tmp.v8[0] = x9;
181 tmp.v8[1] = xd;
182 tmp.v8[2] = xb;
183 tmp.v8[3] = xe;
184 U3[i] = tmp.value;
185
186 tmp.v8[0] = tmp.v8[1] = tmp.v8[2] = tmp.v8[3] = x;
187 U4[i] = tmp.value;
188 }
189 }
190
191
192 /*
193 * aes_test_inverse() returns err_status_ok if aes
194 * encryption and decryption are true inverses of each other, and
195 * returns err_status_algo_fail otherwise
196 */
197
198 #include "err.h"
199
200 err_status_t
201 aes_test_inverse(void);
202
203 #define TABLES_32BIT 1
204
205 int
206 main(void) {
207 int i;
208
209 aes_init_sbox();
210 aes_compute_inv_tables();
211
212 #if TABLES_32BIT
213 printf("uint32_t U0 = {");
214 for (i=0; i < 256; i++) {
215 if ((i % 4) == 0)
216 printf("\n");
217 printf("0x%0x, ", U0[i]);
218 }
219 printf("\n}\n");
220
221 printf("uint32_t U1 = {");
222 for (i=0; i < 256; i++) {
223 if ((i % 4) == 0)
224 printf("\n");
225 printf("0x%x, ", U1[i]);
226 }
227 printf("\n}\n");
228
229 printf("uint32_t U2 = {");
230 for (i=0; i < 256; i++) {
231 if ((i % 4) == 0)
232 printf("\n");
233 printf("0x%x, ", U2[i]);
234 }
235 printf("\n}\n");
236
237 printf("uint32_t U3 = {");
238 for (i=0; i < 256; i++) {
239 if ((i % 4) == 0)
240 printf("\n");
241 printf("0x%x, ", U3[i]);
242 }
243 printf("\n}\n");
244
245 printf("uint32_t U4 = {");
246 for (i=0; i < 256; i++) {
247 if ((i % 4) == 0)
248 printf("\n");
249 printf("0x%x, ", U4[i]);
250 }
251 printf("\n}\n");
252
253 #else
254
255 printf("uint32_t U0 = {");
256 for (i=0; i < 256; i++) {
257 if ((i % 4) == 0)
258 printf("\n");
259 printf("0x%lx, ", U0[i]);
260 }
261 printf("\n}\n");
262
263 printf("uint32_t U1 = {");
264 for (i=0; i < 256; i++) {
265 if ((i % 4) == 0)
266 printf("\n");
267 printf("0x%lx, ", U1[i]);
268 }
269 printf("\n}\n");
270
271 printf("uint32_t U2 = {");
272 for (i=0; i < 256; i++) {
273 if ((i % 4) == 0)
274 printf("\n");
275 printf("0x%lx, ", U2[i]);
276 }
277 printf("\n}\n");
278
279 printf("uint32_t U3 = {");
280 for (i=0; i < 256; i++) {
281 if ((i % 4) == 0)
282 printf("\n");
283 printf("0x%lx, ", U3[i]);
284 }
285 printf("\n}\n");
286
287 printf("uint32_t U4 = {");
288 for (i=0; i < 256; i++) {
289 if ((i % 4) == 0)
290 printf("\n");
291 printf("0x%lx, ", U4[i]);
292 }
293 printf("\n}\n");
294
295
296 #endif /* TABLES_32BIT */
297
298
299 #if AES_INVERSE_TEST
300 /*
301 * test that aes_encrypt and aes_decrypt are actually
302 * inverses of each other
303 */
304
305 printf("aes inverse test: ");
306 if (aes_test_inverse() == err_status_ok)
307 printf("passed\n");
308 else {
309 printf("failed\n");
310 exit(1);
311 }
312 #endif
313
314 return 0;
315 }
316
317 #if AES_INVERSE_TEST
318
319 err_status_t
320 aes_test_inverse(void) {
321 v128_t x, y;
322 aes_expanded_key_t expanded_key, decrypt_key;
323 uint8_t plaintext[16] = {
324 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
325 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
326 };
327 uint8_t key[16] = {
328 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
329 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
330 };
331 v128_t k;
332 v128_set_to_zero(&x);
333
334 v128_copy_octet_string(&k, key);
335 v128_copy_octet_string(&x, plaintext);
336 aes_expand_encryption_key(k, expanded_key);
337 aes_expand_decryption_key(k, decrypt_key);
338 aes_encrypt(&x, expanded_key);
339 aes_decrypt(&x, decrypt_key);
340
341 /* compare to expected value then report */
342 v128_copy_octet_string(&y, plaintext);
343
344 if (v128_is_eq(&x, &y))
345 return err_status_ok;
346 return err_status_algo_fail;
347
348 }
349
350 #endif
OLDNEW
« no previous file with comments | « srtp/srtp/srtp.c ('k') | srtp/test/.cvsignore » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698