3 * $Id: serpent_sboxes.h,v 1.1 2007/04/05 14:20:35 nisse Exp $
5 * For more details on this algorithm, see the Serpent website at
6 * http://www.cl.cam.ac.uk/~rja14/serpent.html
9 /* Copyright (C) 1998 Ross Anderson, Eli Biham, Lars Knudsen
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* I've modified this code a bit so that it interoperates with lsh
27 * properly. 2000-9-5, Rafael R. Sevilla <dido@pacific.net.ph>
30 /* NOTE: The copyright notice for the original version of this code
31 * said "All rights reserved. This code is freely distributed for AES
32 * selection process. No other use is allowed." However, the authors
33 * later decided to GPL the code. /nisse */
35 #ifndef SERPENT_SBOXES_H_INCLUDED
36 #define SERPENT_SBOXES_H_INCLUDED
40 /* S0: 3 8 15 1 10 6 5 11 14 13 4 2 7 0 9 12 */
42 /* depth = 5,7,4,2, Total gates=18 */
43 #define RND00(a,b,c,d,w,x,y,z) \
44 { register uint32_t t02, t03, t05, t06, t07, t08, t09, t11, t12, t13, t14, t15, t17, t01;\
64 /* InvS0: 13 3 11 0 10 6 5 12 1 14 4 7 15 9 8 2 */
66 /* depth = 8,4,3,6, Total gates=19 */
67 #define InvRND00(a,b,c,d,w,x,y,z) \
68 { register uint32_t t02, t03, t04, t05, t06, t08, t09, t10, t12, t13, t14, t15, t17, t18, t01;\
89 /* S1: 15 12 2 7 9 0 5 10 1 11 14 8 6 13 3 4 */
91 /* depth = 10,7,3,5, Total gates=18 */
92 #define RND01(a,b,c,d,w,x,y,z) \
93 { register uint32_t t02, t03, t04, t05, t06, t07, t08, t10, t11, t12, t13, t16, t17, t01;\
113 /* InvS1: 5 8 2 14 15 6 12 3 11 4 7 9 1 13 10 0 */
115 /* depth = 7,4,5,3, Total gates=18 */
116 #define InvRND01(a,b,c,d,w,x,y,z) \
117 { register uint32_t t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t14, t15, t17, t01;\
137 /* S2: 8 6 7 9 3 12 10 15 13 1 14 4 0 11 5 2 */
139 /* depth = 3,8,11,7, Total gates=16 */
140 #define RND02(a,b,c,d,w,x,y,z) \
141 { register uint32_t t02, t03, t05, t06, t07, t08, t09, t10, t12, t13, t14, t01;\
159 /* InvS2: 12 9 15 4 11 14 1 2 0 3 6 13 5 8 10 7 */
161 /* depth = 3,6,8,3, Total gates=18 */
162 #define InvRND02(a,b,c,d,w,x,y,z) \
163 { register uint32_t t02, t03, t04, t06, t07, t08, t09, t10, t11, t12, t15, t16, t17, t01;\
183 /* S3: 0 15 11 8 12 9 6 3 13 1 2 4 10 7 5 14 */
185 /* depth = 8,3,5,5, Total gates=18 */
186 #define RND03(a,b,c,d,w,x,y,z) \
187 { register uint32_t t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t13, t14, t15, t01;\
207 /* InvS3: 0 9 10 7 11 14 6 13 3 5 12 2 4 8 15 1 */
209 /* depth = 3,6,4,4, Total gates=17 */
210 #define InvRND03(a,b,c,d,w,x,y,z) \
211 { register uint32_t t02, t03, t04, t05, t06, t07, t09, t11, t12, t13, t14, t16, t01;\
230 /* S4: 1 15 8 3 12 0 11 6 2 5 4 10 9 14 7 13 */
232 /* depth = 6,7,5,3, Total gates=19 */
233 #define RND04(a,b,c,d,w,x,y,z) \
234 { register uint32_t t02, t03, t04, t05, t06, t08, t09, t10, t11, t12, t13, t14, t15, t16, t01;\
255 /* InvS4: 5 0 8 3 10 9 7 14 2 12 11 6 4 15 13 1 */
257 /* depth = 6,4,7,3, Total gates=17 */
258 #define InvRND04(a,b,c,d,w,x,y,z) \
259 { register uint32_t t02, t03, t04, t05, t06, t07, t09, t10, t11, t12, t13, t15, t01;\
278 /* S5: 15 5 2 11 4 10 9 12 0 3 14 8 13 6 7 1 */
280 /* depth = 4,6,8,6, Total gates=17 */
281 #define RND05(a,b,c,d,w,x,y,z) \
282 { register uint32_t t02, t03, t04, t05, t07, t08, t09, t10, t11, t12, t13, t14, t01;\
301 /* InvS5: 8 15 2 9 4 1 13 14 11 6 5 3 7 12 10 0 */
303 /* depth = 4,6,9,7, Total gates=17 */
304 #define InvRND05(a,b,c,d,w,x,y,z) \
305 { register uint32_t t02, t03, t04, t05, t07, t08, t09, t10, t12, t13, t15, t16, t01;\
324 /* S6: 7 2 12 5 8 4 6 11 14 9 1 15 13 3 10 0 */
326 /* depth = 8,3,6,3, Total gates=19 */
327 #define RND06(a,b,c,d,w,x,y,z) \
328 { register uint32_t t02, t03, t04, t05, t07, t08, t09, t10, t11, t12, t13, t15, t17, t18, t01;\
349 /* InvS6: 15 10 1 13 5 3 6 0 4 9 14 7 2 12 8 11 */
351 /* depth = 5,3,8,6, Total gates=19 */
352 #define InvRND06(a,b,c,d,w,x,y,z) \
353 { register uint32_t t02, t03, t04, t05, t06, t07, t08, t09, t12, t13, t14, t15, t16, t17, t01;\
374 /* S7: 1 13 15 0 14 8 2 11 7 4 12 10 9 3 5 6 */
376 /* depth = 10,7,10,4, Total gates=19 */
377 #define RND07(a,b,c,d,w,x,y,z) \
378 { register uint32_t t02, t03, t04, t05, t06, t08, t09, t10, t11, t13, t14, t15, t16, t17, t01;\
399 /* InvS7: 3 0 6 13 9 14 15 8 5 12 11 7 10 1 4 2 */
401 /* depth = 9,7,3,3, Total gates=18 */
402 #define InvRND07(a,b,c,d,w,x,y,z) \
403 { register uint32_t t02, t03, t04, t06, t07, t08, t09, t10, t11, t13, t14, t15, t16, t01;\
423 #define RND08(a,b,c,d,e,f,g,h) RND00(a,b,c,d,e,f,g,h)
424 #define RND09(a,b,c,d,e,f,g,h) RND01(a,b,c,d,e,f,g,h)
425 #define RND10(a,b,c,d,e,f,g,h) RND02(a,b,c,d,e,f,g,h)
426 #define RND11(a,b,c,d,e,f,g,h) RND03(a,b,c,d,e,f,g,h)
427 #define RND12(a,b,c,d,e,f,g,h) RND04(a,b,c,d,e,f,g,h)
428 #define RND13(a,b,c,d,e,f,g,h) RND05(a,b,c,d,e,f,g,h)
429 #define RND14(a,b,c,d,e,f,g,h) RND06(a,b,c,d,e,f,g,h)
430 #define RND15(a,b,c,d,e,f,g,h) RND07(a,b,c,d,e,f,g,h)
431 #define RND16(a,b,c,d,e,f,g,h) RND00(a,b,c,d,e,f,g,h)
432 #define RND17(a,b,c,d,e,f,g,h) RND01(a,b,c,d,e,f,g,h)
433 #define RND18(a,b,c,d,e,f,g,h) RND02(a,b,c,d,e,f,g,h)
434 #define RND19(a,b,c,d,e,f,g,h) RND03(a,b,c,d,e,f,g,h)
435 #define RND20(a,b,c,d,e,f,g,h) RND04(a,b,c,d,e,f,g,h)
436 #define RND21(a,b,c,d,e,f,g,h) RND05(a,b,c,d,e,f,g,h)
437 #define RND22(a,b,c,d,e,f,g,h) RND06(a,b,c,d,e,f,g,h)
438 #define RND23(a,b,c,d,e,f,g,h) RND07(a,b,c,d,e,f,g,h)
439 #define RND24(a,b,c,d,e,f,g,h) RND00(a,b,c,d,e,f,g,h)
440 #define RND25(a,b,c,d,e,f,g,h) RND01(a,b,c,d,e,f,g,h)
441 #define RND26(a,b,c,d,e,f,g,h) RND02(a,b,c,d,e,f,g,h)
442 #define RND27(a,b,c,d,e,f,g,h) RND03(a,b,c,d,e,f,g,h)
443 #define RND28(a,b,c,d,e,f,g,h) RND04(a,b,c,d,e,f,g,h)
444 #define RND29(a,b,c,d,e,f,g,h) RND05(a,b,c,d,e,f,g,h)
445 #define RND30(a,b,c,d,e,f,g,h) RND06(a,b,c,d,e,f,g,h)
446 #define RND31(a,b,c,d,e,f,g,h) RND07(a,b,c,d,e,f,g,h)
448 #define InvRND08(a,b,c,d,e,f,g,h) InvRND00(a,b,c,d,e,f,g,h)
449 #define InvRND09(a,b,c,d,e,f,g,h) InvRND01(a,b,c,d,e,f,g,h)
450 #define InvRND10(a,b,c,d,e,f,g,h) InvRND02(a,b,c,d,e,f,g,h)
451 #define InvRND11(a,b,c,d,e,f,g,h) InvRND03(a,b,c,d,e,f,g,h)
452 #define InvRND12(a,b,c,d,e,f,g,h) InvRND04(a,b,c,d,e,f,g,h)
453 #define InvRND13(a,b,c,d,e,f,g,h) InvRND05(a,b,c,d,e,f,g,h)
454 #define InvRND14(a,b,c,d,e,f,g,h) InvRND06(a,b,c,d,e,f,g,h)
455 #define InvRND15(a,b,c,d,e,f,g,h) InvRND07(a,b,c,d,e,f,g,h)
456 #define InvRND16(a,b,c,d,e,f,g,h) InvRND00(a,b,c,d,e,f,g,h)
457 #define InvRND17(a,b,c,d,e,f,g,h) InvRND01(a,b,c,d,e,f,g,h)
458 #define InvRND18(a,b,c,d,e,f,g,h) InvRND02(a,b,c,d,e,f,g,h)
459 #define InvRND19(a,b,c,d,e,f,g,h) InvRND03(a,b,c,d,e,f,g,h)
460 #define InvRND20(a,b,c,d,e,f,g,h) InvRND04(a,b,c,d,e,f,g,h)
461 #define InvRND21(a,b,c,d,e,f,g,h) InvRND05(a,b,c,d,e,f,g,h)
462 #define InvRND22(a,b,c,d,e,f,g,h) InvRND06(a,b,c,d,e,f,g,h)
463 #define InvRND23(a,b,c,d,e,f,g,h) InvRND07(a,b,c,d,e,f,g,h)
464 #define InvRND24(a,b,c,d,e,f,g,h) InvRND00(a,b,c,d,e,f,g,h)
465 #define InvRND25(a,b,c,d,e,f,g,h) InvRND01(a,b,c,d,e,f,g,h)
466 #define InvRND26(a,b,c,d,e,f,g,h) InvRND02(a,b,c,d,e,f,g,h)
467 #define InvRND27(a,b,c,d,e,f,g,h) InvRND03(a,b,c,d,e,f,g,h)
468 #define InvRND28(a,b,c,d,e,f,g,h) InvRND04(a,b,c,d,e,f,g,h)
469 #define InvRND29(a,b,c,d,e,f,g,h) InvRND05(a,b,c,d,e,f,g,h)
470 #define InvRND30(a,b,c,d,e,f,g,h) InvRND06(a,b,c,d,e,f,g,h)
471 #define InvRND31(a,b,c,d,e,f,g,h) InvRND07(a,b,c,d,e,f,g,h)
473 /* Linear transformations and key mixing: */
475 #define ROL(x,n) ((((uint32_t)(x))<<(n))| \
476 (((uint32_t)(x))>>(32-(n))))
477 #define ROR(x,n) ((((uint32_t)(x))<<(32-(n)))| \
478 (((uint32_t)(x))>>(n)))
480 #define transform(x0, x1, x2, x3, y0, y1, y2, y3) \
484 y3 = x3 ^ y2 ^ ((uint32_t)y0)<<3; \
488 y2 = y2 ^ y3 ^ ((uint32_t)y1<<7); \
492 #define inv_transform(x0, x1, x2, x3, y0, y1, y2, y3) \
495 y2 = y2 ^ x3 ^ ((uint32_t)x1<<7); \
499 y3 = y3 ^ y2 ^ ((uint32_t)y0)<<3; \
504 #define keying(x0, x1, x2, x3, subkey) \
505 x0^=subkey[0];x1^=subkey[1]; \
506 x2^=subkey[2];x3^=subkey[3]
508 /* PHI: Constant used in the key schedule */
509 #define PHI 0x9e3779b9L
511 #endif /* SERPENT_SBOXES_H_INCLUDED */