DEFINITIONS
This source file includes following functions.
- transpose
- rotate
- f
- definekey
- encrypt
- crypt
1
2
3
4
5 struct block {
6 unsigned char b_data[64];
7 };
8
9 struct ordering {
10 unsigned char o_data[64];
11 };
12
13 static struct block key;
14
15 static struct ordering InitialTr = {
16 58,50,42,34,26,18,10, 2,60,52,44,36,28,20,12, 4,
17 62,54,46,38,30,22,14, 6,64,56,48,40,32,24,16, 8,
18 57,49,41,33,25,17, 9, 1,59,51,43,35,27,19,11, 3,
19 61,53,45,37,29,21,13, 5,63,55,47,39,31,23,15, 7,
20 };
21
22 static struct ordering FinalTr = {
23 40, 8,48,16,56,24,64,32,39, 7,47,15,55,23,63,31,
24 38, 6,46,14,54,22,62,30,37, 5,45,13,53,21,61,29,
25 36, 4,44,12,52,20,60,28,35, 3,43,11,51,19,59,27,
26 34, 2,42,10,50,18,58,26,33, 1,41, 9,49,17,57,25,
27 };
28
29 static struct ordering swap = {
30 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,
31 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,
32 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,
33 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,
34 };
35
36 static struct ordering KeyTr1 = {
37 57,49,41,33,25,17, 9, 1,58,50,42,34,26,18,
38 10, 2,59,51,43,35,27,19,11, 3,60,52,44,36,
39 63,55,47,39,31,23,15, 7,62,54,46,38,30,22,
40 14, 6,61,53,45,37,29,21,13, 5,28,20,12, 4,
41 };
42
43 static struct ordering KeyTr2 = {
44 14,17,11,24, 1, 5, 3,28,15, 6,21,10,
45 23,19,12, 4,26, 8,16, 7,27,20,13, 2,
46 41,52,31,37,47,55,30,40,51,45,33,48,
47 44,49,39,56,34,53,46,42,50,36,29,32,
48 };
49
50 static struct ordering etr = {
51 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
52 8, 9,10,11,12,13,12,13,14,15,16,17,
53 16,17,18,19,20,21,20,21,22,23,24,25,
54 24,25,26,27,28,29,28,29,30,31,32, 1,
55 };
56
57 static struct ordering ptr = {
58 16, 7,20,21,29,12,28,17, 1,15,23,26, 5,18,31,10,
59 2, 8,24,14,32,27, 3, 9,19,13,30, 6,22,11, 4,25,
60 };
61
62 static unsigned char s_boxes[8][64] = {
63 { 14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
64 0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
65 4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
66 15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13,
67 },
68
69 { 15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
70 3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
71 0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
72 13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9,
73 },
74
75 { 10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
76 13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
77 13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
78 1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12,
79 },
80
81 { 7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
82 13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
83 10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
84 3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14,
85 },
86
87 { 2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
88 14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
89 4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
90 11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,
91 },
92
93 { 12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
94 10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
95 9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
96 4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13,
97 },
98
99 { 4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
100 13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
101 1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
102 6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12,
103 },
104
105 { 13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
106 1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
107 7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
108 2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11,
109 },
110 };
111
112 static int rots[] = {
113 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,
114 };
115
116 static void transpose(struct block *data, struct ordering *t, int n)
117 {
118 struct block x;
119
120 x = *data;
121
122 while (n-- > 0) {
123 data->b_data[n] = x.b_data[t->o_data[n] - 1];
124 }
125 }
126
127 static void rotate(struct block *key)
128 {
129 register unsigned char *p = key->b_data;
130 register unsigned char *ep = &(key->b_data[55]);
131 int data0 = key->b_data[0], data28 = key->b_data[28];
132
133 while (p++ < ep) *(p-1) = *p;
134 key->b_data[27] = (char) data0;
135 key->b_data[55] = (char) data28;
136 }
137
138 static struct ordering *EP = &etr;
139
140 static void f(int i, struct block *key, struct block *a, struct block *x)
141 {
142 struct block e, ikey, y;
143 int k;
144 register unsigned char *p, *q, *r;
145
146 e = *a;
147 transpose(&e, EP, 48);
148 for (k = rots[i]; k; k--) rotate(key);
149 ikey = *key;
150 transpose(&ikey, &KeyTr2, 48);
151 p = &(y.b_data[48]);
152 q = &(e.b_data[48]);
153 r = &(ikey.b_data[48]);
154 while (p > y.b_data) {
155 *--p = *--q ^ *--r;
156 }
157 q = x->b_data;
158 for (k = 0; k < 8; k++) {
159 register int xb, r;
160
161 r = *p++ << 5;
162 r += *p++ << 3;
163 r += *p++ << 2;
164 r += *p++ << 1;
165 r += *p++;
166 r += *p++ << 4;
167
168 xb = s_boxes[k][r];
169
170 *q++ = (char) (xb >> 3) & 1;
171 *q++ = (char) (xb>>2) & 1;
172 *q++ = (char) (xb>>1) & 1;
173 *q++ = (char) (xb & 1);
174 }
175 transpose(x, &ptr, 32);
176 }
177
178 void definekey(char *k)
179 {
180
181 key = *((struct block *) k);
182 transpose(&key, &KeyTr1, 56);
183 }
184
185 void encrypt(char *blck, int edflag)
186 {
187 register struct block *p = (struct block *) blck;
188 register int i;
189
190 transpose(p, &InitialTr, 64);
191 for (i = 15; i>= 0; i--) {
192 int j = edflag ? i : 15 - i;
193 register int k;
194 struct block b, x;
195
196 b = *p;
197 for (k = 31; k >= 0; k--) {
198 p->b_data[k] = b.b_data[k + 32];
199 }
200 f(j, &key, p, &x);
201 for (k = 31; k >= 0; k--) {
202 p->b_data[k+32] = b.b_data[k] ^ x.b_data[k];
203 }
204 }
205 transpose(p, &swap, 64);
206 transpose(p, &FinalTr, 64);
207 }
208
209 char *crypt(char *pw, char *salt)
210 {
211
212 char pwb[66];
213 static char result[16];
214 register char *p = pwb;
215 struct ordering new_etr;
216 register int i;
217
218 while (*pw && p < &pwb[64]) {
219 register int j = 7;
220
221 while (j--) {
222 *p++ = (*pw >> j) & 01;
223 }
224 pw++;
225 *p++ = 0;
226 }
227 while (p < &pwb[64]) *p++ = 0;
228
229 definekey(p = pwb);
230
231 while (p < &pwb[66]) *p++ = 0;
232
233 new_etr = etr;
234 EP = &new_etr;
235 for (i = 0; i < 2; i++) {
236 register char c = *salt++;
237 register int j;
238
239 result[i] = c;
240 if ( c > 'Z') c -= 6 + 7 + '.';
241 else if ( c > '9') c -= 7 + '.';
242 else c -= '.';
243
244 for (j = 0; j < 6; j++) {
245 if ((c >> j) & 01) {
246 int t = 6*i + j;
247 int temp = new_etr.o_data[t];
248 new_etr.o_data[t] = new_etr.o_data[t+24];
249 new_etr.o_data[t+24] = (char) temp;
250 }
251 }
252 }
253
254 if (result[1] == 0) result[1] = result[0];
255
256 for (i = 0; i < 25; i++) encrypt(pwb,0);
257 EP = &etr;
258
259 p = pwb;
260 pw = result+2;
261 while (p < &pwb[66]) {
262 register int c = 0;
263 register int j = 6;
264
265 while (j--) {
266 c <<= 1;
267 c |= *p++;
268 }
269 c += '.';
270 if (c > '9') c += 7;
271 if (c > 'Z') c += 6;
272 *pw++ = (char) c;
273 }
274 *pw = 0;
275 return result;
276 }