d518241dd2a1e72f5bf7128c082b55b508652d51
[platform/upstream/nettle.git] / twofishdata.c
1 /* twofishdata.c
2
3    Generates the permutations q0 and q1 for twofish.
4
5    Copyright (C) 1999 Ruud de Rooij <ruud@debian.org>
6
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 of the License, or
10    (at your option) any later version.
11
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.
16
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
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02111-1301  USA.
21  */
22
23 #include <stdio.h>
24
25 #define ror4(x) (((x) >> 1) | (((x) & 1) << 3))
26
27 static unsigned char q0(unsigned char x)
28 {
29     static const unsigned char t0[16] = {
30       0x8, 0x1, 0x7, 0xD, 0x6, 0xF, 0x3, 0x2,
31       0x0, 0xB, 0x5, 0x9, 0xE, 0xC, 0xA, 0x4
32     };
33     static const unsigned char t1[16] = {
34       0xE, 0xC, 0xB, 0x8, 0x1, 0x2, 0x3, 0x5,
35       0xF, 0x4, 0xA, 0x6, 0x7, 0x0, 0x9, 0xD
36     };
37     static const unsigned char t2[16] = {
38       0xB, 0xA, 0x5, 0xE, 0x6, 0xD, 0x9, 0x0,
39       0xC, 0x8, 0xF, 0x3, 0x2, 0x4, 0x7, 0x1
40     };
41     static const unsigned char t3[16] = {
42       0xD, 0x7, 0xF, 0x4, 0x1, 0x2, 0x6, 0xE,
43       0x9, 0xB, 0x3, 0x0, 0x8, 0x5, 0xC, 0xA
44     };
45
46     unsigned char a0 = x / 16;
47     unsigned char b0 = x % 16;
48
49     unsigned char a1 = a0 ^ b0;
50     unsigned char b1 = a0 ^ ror4(b0) ^ ((8*a0) % 16);
51
52     unsigned char a2 = t0[a1];
53     unsigned char b2 = t1[b1];
54
55     unsigned char a3 = a2 ^ b2;
56     unsigned char b3 = a2 ^ ror4(b2) ^ ((8*a2) % 16);
57
58     unsigned char a4 = t2[a3];
59     unsigned char b4 = t3[b3];
60
61     unsigned char y = 16*b4 + a4;
62
63     return y;
64 }
65
66 static unsigned char q1(unsigned char x)
67 {
68   static const unsigned char t0[16] = {
69     0x2, 0x8, 0xB, 0xD, 0xF, 0x7, 0x6, 0xE,
70     0x3, 0x1, 0x9, 0x4, 0x0, 0xA, 0xC, 0x5
71   };
72   static const unsigned char t1[16] = {
73     0x1, 0xE, 0x2, 0xB, 0x4, 0xC, 0x3, 0x7,
74     0x6, 0xD, 0xA, 0x5, 0xF, 0x9, 0x0, 0x8
75   };
76   static const unsigned char t2[16] = {
77     0x4, 0xC, 0x7, 0x5, 0x1, 0x6, 0x9, 0xA,
78     0x0, 0xE, 0xD, 0x8, 0x2, 0xB, 0x3, 0xF
79   };
80   static const unsigned char t3[16] = {
81     0xB, 0x9, 0x5, 0x1, 0xC, 0x3, 0xD, 0xE,
82     0x6, 0x4, 0x7, 0xF, 0x2, 0x0, 0x8, 0xA
83   };
84
85   unsigned char a0 = x / 16;
86   unsigned char b0 = x % 16;
87
88   unsigned char a1 = a0 ^ b0;
89   unsigned char b1 = a0 ^ ror4(b0) ^ ((8*a0) % 16);
90
91   unsigned char a2 = t0[a1];
92   unsigned char b2 = t1[b1];
93
94   unsigned char a3 = a2 ^ b2;
95   unsigned char b3 = a2 ^ ror4(b2) ^ ((8*a2) % 16);
96
97   unsigned char a4 = t2[a3];
98   unsigned char b4 = t3[b3];
99
100   unsigned char y = 16*b4 + a4;
101
102   return y;
103 }
104
105 int
106 main(void)
107 {
108   unsigned i;
109
110   printf("static const uint8_t q0[256] = {");
111   for (i = 0; i < 256; i++) {
112     if ( (i % 8) == 0)
113       printf("\n  ");
114     printf("0x%02X,", q0(i));
115   }
116   printf("\n};\n\n");
117
118   printf("static const uint8_t q1[256] = {");
119   for (i = 0; i < 256; i++) {
120     if ( (i % 8) == 0)
121       printf("\n  ");
122     printf("0x%02X,", q1(i));
123   }
124   printf("\n};\n");
125
126   return 0;
127 }