3 * Copyright (C) 1998 Ross Anderson, Eli Biham, Lars Knudsen
5 * For more information see http://www.cl.cam.ac.uk/users/rja14/serpent.html
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 /* S0: 3 8 15 1 10 6 5 11 14 13 4 2 7 0 9 12 */
26 /* depth = 5,7,4,2, Total gates=18 */
27 #define RND00(a,b,c,d,w,x,y,z) \
28 { register unsigned long t02, t03, t05, t06, t07, t08, t09, t11, t12, t13, t14, t15, t17, t01;\
48 /* InvS0: 13 3 11 0 10 6 5 12 1 14 4 7 15 9 8 2 */
50 /* depth = 8,4,3,6, Total gates=19 */
51 #define InvRND00(a,b,c,d,w,x,y,z) \
52 { register unsigned long t02, t03, t04, t05, t06, t08, t09, t10, t12, t13, t14, t15, t17, t18, t01;\
73 /* S1: 15 12 2 7 9 0 5 10 1 11 14 8 6 13 3 4 */
75 /* depth = 10,7,3,5, Total gates=18 */
76 #define RND01(a,b,c,d,w,x,y,z) \
77 { register unsigned long t02, t03, t04, t05, t06, t07, t08, t10, t11, t12, t13, t16, t17, t01;\
97 /* InvS1: 5 8 2 14 15 6 12 3 11 4 7 9 1 13 10 0 */
99 /* depth = 7,4,5,3, Total gates=18 */
100 #define InvRND01(a,b,c,d,w,x,y,z) \
101 { register unsigned long t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t14, t15, t17, t01;\
121 /* S2: 8 6 7 9 3 12 10 15 13 1 14 4 0 11 5 2 */
123 /* depth = 3,8,11,7, Total gates=16 */
124 #define RND02(a,b,c,d,w,x,y,z) \
125 { register unsigned long t02, t03, t05, t06, t07, t08, t09, t10, t12, t13, t14, t01;\
143 /* InvS2: 12 9 15 4 11 14 1 2 0 3 6 13 5 8 10 7 */
145 /* depth = 3,6,8,3, Total gates=18 */
146 #define InvRND02(a,b,c,d,w,x,y,z) \
147 { register unsigned long t02, t03, t04, t06, t07, t08, t09, t10, t11, t12, t15, t16, t17, t01;\
167 /* S3: 0 15 11 8 12 9 6 3 13 1 2 4 10 7 5 14 */
169 /* depth = 8,3,5,5, Total gates=18 */
170 #define RND03(a,b,c,d,w,x,y,z) \
171 { register unsigned long t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t13, t14, t15, t01;\
191 /* InvS3: 0 9 10 7 11 14 6 13 3 5 12 2 4 8 15 1 */
193 /* depth = 3,6,4,4, Total gates=17 */
194 #define InvRND03(a,b,c,d,w,x,y,z) \
195 { register unsigned long t02, t03, t04, t05, t06, t07, t09, t11, t12, t13, t14, t16, t01;\
214 /* S4: 1 15 8 3 12 0 11 6 2 5 4 10 9 14 7 13 */
216 /* depth = 6,7,5,3, Total gates=19 */
217 #define RND04(a,b,c,d,w,x,y,z) \
218 { register unsigned long t02, t03, t04, t05, t06, t08, t09, t10, t11, t12, t13, t14, t15, t16, t01;\
239 /* InvS4: 5 0 8 3 10 9 7 14 2 12 11 6 4 15 13 1 */
241 /* depth = 6,4,7,3, Total gates=17 */
242 #define InvRND04(a,b,c,d,w,x,y,z) \
243 { register unsigned long t02, t03, t04, t05, t06, t07, t09, t10, t11, t12, t13, t15, t01;\
262 /* S5: 15 5 2 11 4 10 9 12 0 3 14 8 13 6 7 1 */
264 /* depth = 4,6,8,6, Total gates=17 */
265 #define RND05(a,b,c,d,w,x,y,z) \
266 { register unsigned long t02, t03, t04, t05, t07, t08, t09, t10, t11, t12, t13, t14, t01;\
285 /* InvS5: 8 15 2 9 4 1 13 14 11 6 5 3 7 12 10 0 */
287 /* depth = 4,6,9,7, Total gates=17 */
288 #define InvRND05(a,b,c,d,w,x,y,z) \
289 { register unsigned long t02, t03, t04, t05, t07, t08, t09, t10, t12, t13, t15, t16, t01;\
308 /* S6: 7 2 12 5 8 4 6 11 14 9 1 15 13 3 10 0 */
310 /* depth = 8,3,6,3, Total gates=19 */
311 #define RND06(a,b,c,d,w,x,y,z) \
312 { register unsigned long t02, t03, t04, t05, t07, t08, t09, t10, t11, t12, t13, t15, t17, t18, t01;\
333 /* InvS6: 15 10 1 13 5 3 6 0 4 9 14 7 2 12 8 11 */
335 /* depth = 5,3,8,6, Total gates=19 */
336 #define InvRND06(a,b,c,d,w,x,y,z) \
337 { register unsigned long t02, t03, t04, t05, t06, t07, t08, t09, t12, t13, t14, t15, t16, t17, t01;\
358 /* S7: 1 13 15 0 14 8 2 11 7 4 12 10 9 3 5 6 */
360 /* depth = 10,7,10,4, Total gates=19 */
361 #define RND07(a,b,c,d,w,x,y,z) \
362 { register unsigned long t02, t03, t04, t05, t06, t08, t09, t10, t11, t13, t14, t15, t16, t17, t01;\
383 /* InvS7: 3 0 6 13 9 14 15 8 5 12 11 7 10 1 4 2 */
385 /* depth = 9,7,3,3, Total gates=18 */
386 #define InvRND07(a,b,c,d,w,x,y,z) \
387 { register unsigned long t02, t03, t04, t06, t07, t08, t09, t10, t11, t13, t14, t15, t16, t01;\
407 #define RND08(a,b,c,d,e,f,g,h) RND00(a,b,c,d,e,f,g,h)
408 #define RND09(a,b,c,d,e,f,g,h) RND01(a,b,c,d,e,f,g,h)
409 #define RND10(a,b,c,d,e,f,g,h) RND02(a,b,c,d,e,f,g,h)
410 #define RND11(a,b,c,d,e,f,g,h) RND03(a,b,c,d,e,f,g,h)
411 #define RND12(a,b,c,d,e,f,g,h) RND04(a,b,c,d,e,f,g,h)
412 #define RND13(a,b,c,d,e,f,g,h) RND05(a,b,c,d,e,f,g,h)
413 #define RND14(a,b,c,d,e,f,g,h) RND06(a,b,c,d,e,f,g,h)
414 #define RND15(a,b,c,d,e,f,g,h) RND07(a,b,c,d,e,f,g,h)
415 #define RND16(a,b,c,d,e,f,g,h) RND00(a,b,c,d,e,f,g,h)
416 #define RND17(a,b,c,d,e,f,g,h) RND01(a,b,c,d,e,f,g,h)
417 #define RND18(a,b,c,d,e,f,g,h) RND02(a,b,c,d,e,f,g,h)
418 #define RND19(a,b,c,d,e,f,g,h) RND03(a,b,c,d,e,f,g,h)
419 #define RND20(a,b,c,d,e,f,g,h) RND04(a,b,c,d,e,f,g,h)
420 #define RND21(a,b,c,d,e,f,g,h) RND05(a,b,c,d,e,f,g,h)
421 #define RND22(a,b,c,d,e,f,g,h) RND06(a,b,c,d,e,f,g,h)
422 #define RND23(a,b,c,d,e,f,g,h) RND07(a,b,c,d,e,f,g,h)
423 #define RND24(a,b,c,d,e,f,g,h) RND00(a,b,c,d,e,f,g,h)
424 #define RND25(a,b,c,d,e,f,g,h) RND01(a,b,c,d,e,f,g,h)
425 #define RND26(a,b,c,d,e,f,g,h) RND02(a,b,c,d,e,f,g,h)
426 #define RND27(a,b,c,d,e,f,g,h) RND03(a,b,c,d,e,f,g,h)
427 #define RND28(a,b,c,d,e,f,g,h) RND04(a,b,c,d,e,f,g,h)
428 #define RND29(a,b,c,d,e,f,g,h) RND05(a,b,c,d,e,f,g,h)
429 #define RND30(a,b,c,d,e,f,g,h) RND06(a,b,c,d,e,f,g,h)
430 #define RND31(a,b,c,d,e,f,g,h) RND07(a,b,c,d,e,f,g,h)
432 #define InvRND08(a,b,c,d,e,f,g,h) InvRND00(a,b,c,d,e,f,g,h)
433 #define InvRND09(a,b,c,d,e,f,g,h) InvRND01(a,b,c,d,e,f,g,h)
434 #define InvRND10(a,b,c,d,e,f,g,h) InvRND02(a,b,c,d,e,f,g,h)
435 #define InvRND11(a,b,c,d,e,f,g,h) InvRND03(a,b,c,d,e,f,g,h)
436 #define InvRND12(a,b,c,d,e,f,g,h) InvRND04(a,b,c,d,e,f,g,h)
437 #define InvRND13(a,b,c,d,e,f,g,h) InvRND05(a,b,c,d,e,f,g,h)
438 #define InvRND14(a,b,c,d,e,f,g,h) InvRND06(a,b,c,d,e,f,g,h)
439 #define InvRND15(a,b,c,d,e,f,g,h) InvRND07(a,b,c,d,e,f,g,h)
440 #define InvRND16(a,b,c,d,e,f,g,h) InvRND00(a,b,c,d,e,f,g,h)
441 #define InvRND17(a,b,c,d,e,f,g,h) InvRND01(a,b,c,d,e,f,g,h)
442 #define InvRND18(a,b,c,d,e,f,g,h) InvRND02(a,b,c,d,e,f,g,h)
443 #define InvRND19(a,b,c,d,e,f,g,h) InvRND03(a,b,c,d,e,f,g,h)
444 #define InvRND20(a,b,c,d,e,f,g,h) InvRND04(a,b,c,d,e,f,g,h)
445 #define InvRND21(a,b,c,d,e,f,g,h) InvRND05(a,b,c,d,e,f,g,h)
446 #define InvRND22(a,b,c,d,e,f,g,h) InvRND06(a,b,c,d,e,f,g,h)
447 #define InvRND23(a,b,c,d,e,f,g,h) InvRND07(a,b,c,d,e,f,g,h)
448 #define InvRND24(a,b,c,d,e,f,g,h) InvRND00(a,b,c,d,e,f,g,h)
449 #define InvRND25(a,b,c,d,e,f,g,h) InvRND01(a,b,c,d,e,f,g,h)
450 #define InvRND26(a,b,c,d,e,f,g,h) InvRND02(a,b,c,d,e,f,g,h)
451 #define InvRND27(a,b,c,d,e,f,g,h) InvRND03(a,b,c,d,e,f,g,h)
452 #define InvRND28(a,b,c,d,e,f,g,h) InvRND04(a,b,c,d,e,f,g,h)
453 #define InvRND29(a,b,c,d,e,f,g,h) InvRND05(a,b,c,d,e,f,g,h)
454 #define InvRND30(a,b,c,d,e,f,g,h) InvRND06(a,b,c,d,e,f,g,h)
455 #define InvRND31(a,b,c,d,e,f,g,h) InvRND07(a,b,c,d,e,f,g,h)
457 /* Linear transformations and key mixing: */
459 #define ROL(x,n) ((((unsigned long)(x))<<(n))| \
460 (((unsigned long)(x))>>(32-(n))))
461 #define ROR(x,n) ((((unsigned long)(x))<<(32-(n)))| \
462 (((unsigned long)(x))>>(n)))
464 #define transform(x0, x1, x2, x3, y0, y1, y2, y3) \
468 y3 = x3 ^ y2 ^ ((unsigned long)y0)<<3; \
472 y2 = y2 ^ y3 ^ ((unsigned long)y1<<7); \
476 #define inv_transform(x0, x1, x2, x3, y0, y1, y2, y3) \
479 y2 = y2 ^ x3 ^ ((unsigned long)x1<<7); \
483 y3 = y3 ^ y2 ^ ((unsigned long)y0)<<3; \
488 #define keying(x0, x1, x2, x3, subkey) \
489 x0^=subkey[0];x1^=subkey[1]; \
490 x2^=subkey[2];x3^=subkey[3]
492 /* PHI: Constant used in the key schedule */
493 #define PHI 0x9e3779b9L