~ubuntu-branches/ubuntu/saucy/nettle/saucy-proposed

« back to all changes in this revision

Viewing changes to desdata.c

  • Committer: Bazaar Package Importer
  • Author(s): Marek Habersack
  • Date: 2004-05-04 15:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040504155602-7jbhw5mabvwksl3j
Tags: upstream-1.10
ImportĀ upstreamĀ versionĀ 1.10

Show diffs side-by-side

added added

removed removed

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