2 * serpentsboxes.h: S-boxes; internal to Serpent implementation.
5 * This file is Free Software. It is now being distributed with
8 * Copyright 1998 Ross Anderson, Eli Biham, Lars Knudsen
9 * Copyright 1995-2001 Stephen Early <steve@greenend.org.uk>
10 * Copyright 2011-2013 Ian Jackson
12 * For more information about Serpent see
13 * http://www.cl.cam.ac.uk/users/rja14/serpent.html
15 * You may redistribute secnet as a whole and/or modify it under the
16 * terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 3, or (at your option) any
20 * You may redistribute this file and/or modify it under the terms of
21 * the GNU General Public License as published by the Free Software
22 * Foundation; either version 2, or (at your option) any later
25 * This software is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this software; if not, see
32 * https://www.gnu.org/licenses/gpl.html.
36 /* S0: 3 8 15 1 10 6 5 11 14 13 4 2 7 0 9 12 */
38 /* depth = 5,7,4,2, Total gates=18 */
39 #define RND00(a,b,c,d,w,x,y,z) \
40 { register unsigned long t02, t03, t05, t06, t07, t08, t09, t11, t12, t13, t14, t15, t17, t01;\
60 /* InvS0: 13 3 11 0 10 6 5 12 1 14 4 7 15 9 8 2 */
62 /* depth = 8,4,3,6, Total gates=19 */
63 #define InvRND00(a,b,c,d,w,x,y,z) \
64 { register unsigned long t02, t03, t04, t05, t06, t08, t09, t10, t12, t13, t14, t15, t17, t18, t01;\
85 /* S1: 15 12 2 7 9 0 5 10 1 11 14 8 6 13 3 4 */
87 /* depth = 10,7,3,5, Total gates=18 */
88 #define RND01(a,b,c,d,w,x,y,z) \
89 { register unsigned long t02, t03, t04, t05, t06, t07, t08, t10, t11, t12, t13, t16, t17, t01;\
109 /* InvS1: 5 8 2 14 15 6 12 3 11 4 7 9 1 13 10 0 */
111 /* depth = 7,4,5,3, Total gates=18 */
112 #define InvRND01(a,b,c,d,w,x,y,z) \
113 { register unsigned long t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t14, t15, t17, t01;\
133 /* S2: 8 6 7 9 3 12 10 15 13 1 14 4 0 11 5 2 */
135 /* depth = 3,8,11,7, Total gates=16 */
136 #define RND02(a,b,c,d,w,x,y,z) \
137 { register unsigned long t02, t03, t05, t06, t07, t08, t09, t10, t12, t13, t14, t01;\
155 /* InvS2: 12 9 15 4 11 14 1 2 0 3 6 13 5 8 10 7 */
157 /* depth = 3,6,8,3, Total gates=18 */
158 #define InvRND02(a,b,c,d,w,x,y,z) \
159 { register unsigned long t02, t03, t04, t06, t07, t08, t09, t10, t11, t12, t15, t16, t17, t01;\
179 /* S3: 0 15 11 8 12 9 6 3 13 1 2 4 10 7 5 14 */
181 /* depth = 8,3,5,5, Total gates=18 */
182 #define RND03(a,b,c,d,w,x,y,z) \
183 { register unsigned long t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t13, t14, t15, t01;\
203 /* InvS3: 0 9 10 7 11 14 6 13 3 5 12 2 4 8 15 1 */
205 /* depth = 3,6,4,4, Total gates=17 */
206 #define InvRND03(a,b,c,d,w,x,y,z) \
207 { register unsigned long t02, t03, t04, t05, t06, t07, t09, t11, t12, t13, t14, t16, t01;\
226 /* S4: 1 15 8 3 12 0 11 6 2 5 4 10 9 14 7 13 */
228 /* depth = 6,7,5,3, Total gates=19 */
229 #define RND04(a,b,c,d,w,x,y,z) \
230 { register unsigned long t02, t03, t04, t05, t06, t08, t09, t10, t11, t12, t13, t14, t15, t16, t01;\
251 /* InvS4: 5 0 8 3 10 9 7 14 2 12 11 6 4 15 13 1 */
253 /* depth = 6,4,7,3, Total gates=17 */
254 #define InvRND04(a,b,c,d,w,x,y,z) \
255 { register unsigned long t02, t03, t04, t05, t06, t07, t09, t10, t11, t12, t13, t15, t01;\
274 /* S5: 15 5 2 11 4 10 9 12 0 3 14 8 13 6 7 1 */
276 /* depth = 4,6,8,6, Total gates=17 */
277 #define RND05(a,b,c,d,w,x,y,z) \
278 { register unsigned long t02, t03, t04, t05, t07, t08, t09, t10, t11, t12, t13, t14, t01;\
297 /* InvS5: 8 15 2 9 4 1 13 14 11 6 5 3 7 12 10 0 */
299 /* depth = 4,6,9,7, Total gates=17 */
300 #define InvRND05(a,b,c,d,w,x,y,z) \
301 { register unsigned long t02, t03, t04, t05, t07, t08, t09, t10, t12, t13, t15, t16, t01;\
320 /* S6: 7 2 12 5 8 4 6 11 14 9 1 15 13 3 10 0 */
322 /* depth = 8,3,6,3, Total gates=19 */
323 #define RND06(a,b,c,d,w,x,y,z) \
324 { register unsigned long t02, t03, t04, t05, t07, t08, t09, t10, t11, t12, t13, t15, t17, t18, t01;\
345 /* InvS6: 15 10 1 13 5 3 6 0 4 9 14 7 2 12 8 11 */
347 /* depth = 5,3,8,6, Total gates=19 */
348 #define InvRND06(a,b,c,d,w,x,y,z) \
349 { register unsigned long t02, t03, t04, t05, t06, t07, t08, t09, t12, t13, t14, t15, t16, t17, t01;\
370 /* S7: 1 13 15 0 14 8 2 11 7 4 12 10 9 3 5 6 */
372 /* depth = 10,7,10,4, Total gates=19 */
373 #define RND07(a,b,c,d,w,x,y,z) \
374 { register unsigned long t02, t03, t04, t05, t06, t08, t09, t10, t11, t13, t14, t15, t16, t17, t01;\
395 /* InvS7: 3 0 6 13 9 14 15 8 5 12 11 7 10 1 4 2 */
397 /* depth = 9,7,3,3, Total gates=18 */
398 #define InvRND07(a,b,c,d,w,x,y,z) \
399 { register unsigned long t02, t03, t04, t06, t07, t08, t09, t10, t11, t13, t14, t15, t16, t01;\
419 #define RND08(a,b,c,d,e,f,g,h) RND00(a,b,c,d,e,f,g,h)
420 #define RND09(a,b,c,d,e,f,g,h) RND01(a,b,c,d,e,f,g,h)
421 #define RND10(a,b,c,d,e,f,g,h) RND02(a,b,c,d,e,f,g,h)
422 #define RND11(a,b,c,d,e,f,g,h) RND03(a,b,c,d,e,f,g,h)
423 #define RND12(a,b,c,d,e,f,g,h) RND04(a,b,c,d,e,f,g,h)
424 #define RND13(a,b,c,d,e,f,g,h) RND05(a,b,c,d,e,f,g,h)
425 #define RND14(a,b,c,d,e,f,g,h) RND06(a,b,c,d,e,f,g,h)
426 #define RND15(a,b,c,d,e,f,g,h) RND07(a,b,c,d,e,f,g,h)
427 #define RND16(a,b,c,d,e,f,g,h) RND00(a,b,c,d,e,f,g,h)
428 #define RND17(a,b,c,d,e,f,g,h) RND01(a,b,c,d,e,f,g,h)
429 #define RND18(a,b,c,d,e,f,g,h) RND02(a,b,c,d,e,f,g,h)
430 #define RND19(a,b,c,d,e,f,g,h) RND03(a,b,c,d,e,f,g,h)
431 #define RND20(a,b,c,d,e,f,g,h) RND04(a,b,c,d,e,f,g,h)
432 #define RND21(a,b,c,d,e,f,g,h) RND05(a,b,c,d,e,f,g,h)
433 #define RND22(a,b,c,d,e,f,g,h) RND06(a,b,c,d,e,f,g,h)
434 #define RND23(a,b,c,d,e,f,g,h) RND07(a,b,c,d,e,f,g,h)
435 #define RND24(a,b,c,d,e,f,g,h) RND00(a,b,c,d,e,f,g,h)
436 #define RND25(a,b,c,d,e,f,g,h) RND01(a,b,c,d,e,f,g,h)
437 #define RND26(a,b,c,d,e,f,g,h) RND02(a,b,c,d,e,f,g,h)
438 #define RND27(a,b,c,d,e,f,g,h) RND03(a,b,c,d,e,f,g,h)
439 #define RND28(a,b,c,d,e,f,g,h) RND04(a,b,c,d,e,f,g,h)
440 #define RND29(a,b,c,d,e,f,g,h) RND05(a,b,c,d,e,f,g,h)
441 #define RND30(a,b,c,d,e,f,g,h) RND06(a,b,c,d,e,f,g,h)
442 #define RND31(a,b,c,d,e,f,g,h) RND07(a,b,c,d,e,f,g,h)
444 #define InvRND08(a,b,c,d,e,f,g,h) InvRND00(a,b,c,d,e,f,g,h)
445 #define InvRND09(a,b,c,d,e,f,g,h) InvRND01(a,b,c,d,e,f,g,h)
446 #define InvRND10(a,b,c,d,e,f,g,h) InvRND02(a,b,c,d,e,f,g,h)
447 #define InvRND11(a,b,c,d,e,f,g,h) InvRND03(a,b,c,d,e,f,g,h)
448 #define InvRND12(a,b,c,d,e,f,g,h) InvRND04(a,b,c,d,e,f,g,h)
449 #define InvRND13(a,b,c,d,e,f,g,h) InvRND05(a,b,c,d,e,f,g,h)
450 #define InvRND14(a,b,c,d,e,f,g,h) InvRND06(a,b,c,d,e,f,g,h)
451 #define InvRND15(a,b,c,d,e,f,g,h) InvRND07(a,b,c,d,e,f,g,h)
452 #define InvRND16(a,b,c,d,e,f,g,h) InvRND00(a,b,c,d,e,f,g,h)
453 #define InvRND17(a,b,c,d,e,f,g,h) InvRND01(a,b,c,d,e,f,g,h)
454 #define InvRND18(a,b,c,d,e,f,g,h) InvRND02(a,b,c,d,e,f,g,h)
455 #define InvRND19(a,b,c,d,e,f,g,h) InvRND03(a,b,c,d,e,f,g,h)
456 #define InvRND20(a,b,c,d,e,f,g,h) InvRND04(a,b,c,d,e,f,g,h)
457 #define InvRND21(a,b,c,d,e,f,g,h) InvRND05(a,b,c,d,e,f,g,h)
458 #define InvRND22(a,b,c,d,e,f,g,h) InvRND06(a,b,c,d,e,f,g,h)
459 #define InvRND23(a,b,c,d,e,f,g,h) InvRND07(a,b,c,d,e,f,g,h)
460 #define InvRND24(a,b,c,d,e,f,g,h) InvRND00(a,b,c,d,e,f,g,h)
461 #define InvRND25(a,b,c,d,e,f,g,h) InvRND01(a,b,c,d,e,f,g,h)
462 #define InvRND26(a,b,c,d,e,f,g,h) InvRND02(a,b,c,d,e,f,g,h)
463 #define InvRND27(a,b,c,d,e,f,g,h) InvRND03(a,b,c,d,e,f,g,h)
464 #define InvRND28(a,b,c,d,e,f,g,h) InvRND04(a,b,c,d,e,f,g,h)
465 #define InvRND29(a,b,c,d,e,f,g,h) InvRND05(a,b,c,d,e,f,g,h)
466 #define InvRND30(a,b,c,d,e,f,g,h) InvRND06(a,b,c,d,e,f,g,h)
467 #define InvRND31(a,b,c,d,e,f,g,h) InvRND07(a,b,c,d,e,f,g,h)
469 /* Linear transformations and key mixing: */
471 #define ROL(x,n) ((((unsigned long)(x))<<(n))| \
472 (((unsigned long)(x))>>(32-(n))))
473 #define ROR(x,n) ((((unsigned long)(x))<<(32-(n)))| \
474 (((unsigned long)(x))>>(n)))
476 #define transform(x0, x1, x2, x3, y0, y1, y2, y3) \
480 y3 = x3 ^ y2 ^ ((unsigned long)y0)<<3; \
484 y2 = y2 ^ y3 ^ ((unsigned long)y1<<7); \
488 #define inv_transform(x0, x1, x2, x3, y0, y1, y2, y3) \
491 y2 = y2 ^ x3 ^ ((unsigned long)x1<<7); \
495 y3 = y3 ^ y2 ^ ((unsigned long)y0)<<3; \
500 #define keying(x0, x1, x2, x3, subkey) \
501 x0^=subkey[0];x1^=subkey[1]; \
502 x2^=subkey[2];x3^=subkey[3]
504 /* PHI: Constant used in the key schedule */
505 #define PHI 0x9e3779b9L