Fix build error and set version 2.7.1
[platform/upstream/nettle.git] / desdata.c
1 /* desdata.c
2  *
3  * Generate tables used by des.c and desCode.h.
4  *
5  */
6
7 /*
8  *      des - fast & portable DES encryption & decryption.
9  *      Copyright (C) 1992  Dana L. How
10  *      Please see the file `descore.README' for the complete copyright notice.
11  *
12  */
13
14 #include <stdio.h>
15
16 #include        "desinfo.h"
17
18
19 /* list of weak and semi-weak keys
20
21          +0   +1   +2   +3   +4   +5   +6   +7
22         0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01
23         0x01 0x1f 0x01 0x1f 0x01 0x0e 0x01 0x0e
24         0x01 0xe0 0x01 0xe0 0x01 0xf1 0x01 0xf1
25         0x01 0xfe 0x01 0xfe 0x01 0xfe 0x01 0xfe
26         0x1f 0x01 0x1f 0x01 0x0e 0x01 0x0e 0x01
27         0x1f 0x1f 0x1f 0x1f 0x0e 0x0e 0x0e 0x0e
28         0x1f 0xe0 0x1f 0xe0 0x0e 0xf1 0x0e 0xf1
29         0x1f 0xfe 0x1f 0xfe 0x0e 0xfe 0x0e 0xfe
30         0xe0 0x01 0xe0 0x01 0xf1 0x01 0xf1 0x01
31         0xe0 0x1f 0xe0 0x1f 0xf1 0x0e 0xf1 0x0e
32         0xe0 0xe0 0xe0 0xe0 0xf1 0xf1 0xf1 0xf1
33         0xe0 0xfe 0xe0 0xfe 0xf1 0xfe 0xf1 0xfe
34         0xfe 0x01 0xfe 0x01 0xfe 0x01 0xfe 0x01
35         0xfe 0x1f 0xfe 0x1f 0xfe 0x0e 0xfe 0x0e
36         0xfe 0xe0 0xfe 0xe0 0xfe 0xf1 0xfe 0xf1
37         0xfe 0xfe 0xfe 0xfe 0xfe 0xfe 0xfe 0xfe
38  */
39
40 /* key bit order in each method pair: bits 31->00 of 1st, bits 31->00 of 2nd */
41 /* this does not reflect the rotate of the 2nd word */
42
43 #define S(box,bit)      (box*6+bit)
44 int korder[] = {
45         S(7, 5), S(7, 4), S(7, 3), S(7, 2), S(7, 1), S(7, 0),
46         S(5, 5), S(5, 4), S(5, 3), S(5, 2), S(5, 1), S(5, 0),
47         S(3, 5), S(3, 4), S(3, 3), S(3, 2), S(3, 1), S(3, 0),
48         S(1, 5), S(1, 4), S(1, 3), S(1, 2), S(1, 1), S(1, 0),
49         S(6, 5), S(6, 4), S(6, 3), S(6, 2), S(6, 1), S(6, 0),
50         S(4, 5), S(4, 4), S(4, 3), S(4, 2), S(4, 1), S(4, 0),
51         S(2, 5), S(2, 4), S(2, 3), S(2, 2), S(2, 1), S(2, 0),
52         S(0, 5), S(0, 4), S(0, 3), S(0, 2), S(0, 1), S(0, 0),
53 };
54
55 /* the order in which the algorithm accesses the s boxes */
56
57 int sorder[] = {
58         7, 5, 3, 1, 6, 4, 2, 0,
59 };
60
61 int
62 main(int argc, char **argv)
63 {
64         unsigned long d, i, j, k, l, m, n, s; /* Always at least 32 bits */
65         char b[256], ksr[56];
66
67         if (argc <= 1)
68                 return 1;
69
70         switch ( argv[1][0] ) {
71
72 default: 
73         return 1;
74         /*
75          * <<< make the key parity table >>>
76          */
77
78 case 'p':
79         printf(
80 "/* automagically produced - do not fuss with this information */\n\n");
81
82         /* store parity information */
83         for ( i = 0; i < 256; i++ ) {
84                 j  = i;
85                 j ^= j >> 4;    /* bits 3-0 have pairs */
86                 j ^= j << 2;    /* bits 3-2 have quads */
87                 j ^= j << 1;    /* bit  3 has the entire eight (no cox) */
88                 b[i] = 8 & ~j;  /* 0 is okay and 8 is bad parity */
89         }
90
91         /* only these characters can appear in a weak key */
92         b[0x01] = 1;
93         b[0x0e] = 2;
94         b[0x1f] = 3;
95         b[0xe0] = 4;
96         b[0xf1] = 5;
97         b[0xfe] = 6;
98
99         /* print it out */
100         for ( i = 0; i < 256; i++ ) {
101                 printf("%d,", b[i]);
102                 if ( (i & 31) == 31 )
103                         printf("\n");
104         }
105
106         break;
107
108
109         /*
110          * <<< make the key usage table >>>
111          */
112
113 case 'r':
114         printf("/* automagically made - do not fuss with this */\n\n");
115
116         /* KL specifies the initial key bit positions */
117         for (i = 0; i < 56; i++)
118                 ksr[i] = (KL[i] - 1) ^ 7;
119
120         for (i = 0; i < 16; i++) {
121
122                 /* apply the appropriate number of left shifts */
123                 for (j = 0; j < KS[i]; j++) {
124                         m = ksr[ 0];
125                         n = ksr[28];
126                         for (k = 0; k < 27; k++)
127                                 ksr[k     ] = ksr[k +  1],
128                                 ksr[k + 28] = ksr[k + 29];
129                         ksr[27] = m;
130                         ksr[55] = n;
131                 }
132
133                 /* output the key bit numbers */
134                 for (j = 0; j < 48; j++) {
135                         m = ksr[KC[korder[j]] - 1];
136                         m = (m / 8) * 7 + (m % 8) - 1;
137                         m = 55 - m;
138                         printf(" %2ld,", (long) m);
139                         if ((j % 12) == 11)
140                                 printf("\n");
141                 }
142                 printf("\n");
143         }
144
145         break;
146
147
148         /*
149          * <<< make the keymap table >>>
150          */
151
152 case 'k':
153         printf("/* automagically made - do not fuss with this */\n\n");
154
155         for ( i = 0; i <= 7 ; i++ ) {
156                 s = sorder[i];
157                 for ( d = 0; d <= 63; d++ ) {
158                         /* flip bits */
159                         k =     ((d << 5) & 32) |
160                                 ((d << 3) & 16) |
161                                 ((d << 1) &  8) |
162                                 ((d >> 1) &  4) |
163                                 ((d >> 3) &  2) |
164                                 ((d >> 5) &  1) ;
165                         /* more bit twiddling */
166                         l =     ((k << 0) & 32) |       /* overlap bit */
167                                 ((k << 4) & 16) |       /* overlap bit */
168                                 ((k >> 1) & 15) ;       /* unique bits */
169                         /* look up s box value */
170                         m = SB[s][l];
171                         /* flip bits */
172                         n =     ((m << 3) &  8) |
173                                 ((m << 1) &  4) |
174                                 ((m >> 1) &  2) |
175                                 ((m >> 3) &  1) ;
176                         /* put in correct nybble */
177                         n <<= (s << 2);
178                         /* perform p permutation */
179                         for ( m = j = 0; j < 32; j++ )
180                                 if ( n & (1 << (SP[j] - 1)) )
181                                         m |= (1UL << j);
182                         /* rotate right (alg keeps everything rotated by 1) */
183                         m = (m >> 1) | ((m & 1) << 31);
184                         /* print it out */
185                         printf(" 0x%08lx,", m);
186                         if ( ( d & 3 ) == 3 )
187                                 printf("\n");
188                 }
189                 printf("\n");
190         }
191
192         break;
193
194         }
195
196         return 0;
197 }