UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
crypt_des.c
Go to the documentation of this file.
1/*****************************************************************************
2
3Saleem N. Bhatti
4February 1993
5
6Patch for Intel/Linux courtesy of Mark Handley & George Pavlou
7Added 2 August 1996, Saleem
8*****************************************************************************/
9
10#include "config.h"
11#include "config_unix.h"
12#include "config_win32.h"
13#include "crypto/crypt_des.h"
14#include "crypto/random.h"
15
16typedef unsigned int Word;
17
18#define B00 0x80000000
19#define B01 0x40000000
20#define B02 0x20000000
21#define B03 0x10000000
22#define B04 0x08000000
23#define B05 0x04000000
24#define B06 0x02000000
25#define B07 0x01000000
26#define B08 0x00800000
27#define B09 0x00400000
28#define B10 0x00200000
29#define B11 0x00100000
30#define B12 0x00080000
31#define B13 0x00040000
32#define B14 0x00020000
33#define B15 0x00010000
34#define B16 0x00008000
35#define B17 0x00004000
36#define B18 0x00002000
37#define B19 0x00001000
38#define B20 0x00000800
39#define B21 0x00000400
40#define B22 0x00000200
41#define B23 0x00000100
42#define B24 0x00000080
43#define B25 0x00000040
44#define B26 0x00000020
45#define B27 0x00000010
46#define B28 0x00000008
47#define B29 0x00000004
48#define B30 0x00000002
49#define B31 0x00000001
50
51#define INITIAL_PERMUTATION_AUX(_i0, _i1, _o0, _o1) \
52{ \
53_o0 = _o1 = 0; \
54_o0 |= (_i1 & B25) << 25; /* 58 */ \
55_o0 |= (_i1 & B17) << 16; /* 50 */ \
56_o0 |= (_i1 & B09) << 7; /* 42 */ \
57_o0 |= (_i1 & B01) >> 2; /* 34 */ \
58_o0 |= (_i0 & B25) << 21; /* 26 */ \
59_o0 |= (_i0 & B17) << 12; /* 18 */ \
60_o0 |= (_i0 & B09) << 3; /* 10 */ \
61_o0 |= (_i0 & B01) >> 6; /* 2 */ \
62_o0 |= (_i1 & B27) << 19; /* 60 */ \
63_o0 |= (_i1 & B19) << 10; /* 52 */ \
64_o0 |= (_i1 & B11) << 1; /* 44 */ \
65_o0 |= (_i1 & B03) >> 8; /* 36 */ \
66_o0 |= (_i0 & B27) << 15; /* 28 */ \
67_o0 |= (_i0 & B19) << 6; /* 20 */ \
68_o0 |= (_i0 & B11) >> 3; /* 12 */ \
69_o0 |= (_i0 & B03) >> 12; /* 4 */ \
70_o0 |= (_i1 & B29) << 13; /* 62 */ \
71_o0 |= (_i1 & B21) << 4; /* 54 */ \
72_o0 |= (_i1 & B13) >> 5; /* 46 */ \
73_o0 |= (_i1 & B05) >> 14; /* 38 */ \
74_o0 |= (_i0 & B29) << 9; /* 30 */ \
75_o0 |= (_i0 & B21) ; /* 22 */ \
76_o0 |= (_i0 & B13) >> 9; /* 14 */ \
77_o0 |= (_i0 & B05) >> 18; /* 6 */ \
78_o0 |= (_i1 & B31) << 7; /* 64 */ \
79_o0 |= (_i1 & B23) >> 2; /* 56 */ \
80_o0 |= (_i1 & B15) >> 11; /* 48 */ \
81_o0 |= (_i1 & B07) >> 20; /* 40 */ \
82_o0 |= (_i0 & B31) << 3; /* 32 */ \
83_o0 |= (_i0 & B23) >> 6; /* 24 */ \
84_o0 |= (_i0 & B15) >> 15; /* 16 */ \
85_o0 |= (_i0 & B07) >> 24; /* 8 */ \
86_o1 |= (_i1 & B24) << 24; /* 57 */ \
87_o1 |= (_i1 & B16) << 15; /* 49 */ \
88_o1 |= (_i1 & B08) << 6; /* 41 */ \
89_o1 |= (_i1 & B00) >> 3; /* 33 */ \
90_o1 |= (_i0 & B24) << 20; /* 25 */ \
91_o1 |= (_i0 & B16) << 11; /* 17 */ \
92_o1 |= (_i0 & B08) << 2; /* 9 */ \
93_o1 |= (_i0 & B00) >> 7; /* 1 */ \
94_o1 |= (_i1 & B26) << 18; /* 59 */ \
95_o1 |= (_i1 & B18) << 9; /* 51 */ \
96_o1 |= (_i1 & B10) ; /* 43 */ \
97_o1 |= (_i1 & B02) >> 9; /* 35 */ \
98_o1 |= (_i0 & B26) << 14; /* 27 */ \
99_o1 |= (_i0 & B18) << 5; /* 19 */ \
100_o1 |= (_i0 & B10) >> 4; /* 11 */ \
101_o1 |= (_i0 & B02) >> 13; /* 3 */ \
102_o1 |= (_i1 & B28) << 12; /* 61 */ \
103_o1 |= (_i1 & B20) << 3; /* 53 */ \
104_o1 |= (_i1 & B12) >> 6; /* 45 */ \
105_o1 |= (_i1 & B04) >> 15; /* 37 */ \
106_o1 |= (_i0 & B28) << 8; /* 29 */ \
107_o1 |= (_i0 & B20) >> 1; /* 21 */ \
108_o1 |= (_i0 & B12) >> 10; /* 13 */ \
109_o1 |= (_i0 & B04) >> 19; /* 5 */ \
110_o1 |= (_i1 & B30) << 6; /* 63 */ \
111_o1 |= (_i1 & B22) >> 3; /* 55 */ \
112_o1 |= (_i1 & B14) >> 12; /* 47 */ \
113_o1 |= (_i1 & B06) >> 21; /* 39 */ \
114_o1 |= (_i0 & B30) << 2; /* 31 */ \
115_o1 |= (_i0 & B22) >> 7; /* 23 */ \
116_o1 |= (_i0 & B14) >> 16; /* 15 */ \
117_o1 |= (_i0 & B06) >> 25; /* 7 */ \
118}
119
120#define FINAL_PERMUTATION_AUX(_i0, _i1, _o0, _o1) \
121{ \
122_o0 = _o1 = 0; \
123_o0 |= (_i1 & B07) << 7; /* 40 */ \
124_o0 |= (_i0 & B07) << 6; /* 8 */ \
125_o0 |= (_i1 & B15) << 13; /* 48 */ \
126_o0 |= (_i0 & B15) << 12; /* 16 */ \
127_o0 |= (_i1 & B23) << 19; /* 56 */ \
128_o0 |= (_i0 & B23) << 18; /* 24 */ \
129_o0 |= (_i1 & B31) << 25; /* 64 */ \
130_o0 |= (_i0 & B31) << 24; /* 32 */ \
131_o0 |= (_i1 & B06) >> 2; /* 39 */ \
132_o0 |= (_i0 & B06) >> 3; /* 7 */ \
133_o0 |= (_i1 & B14) << 4; /* 47 */ \
134_o0 |= (_i0 & B14) << 3; /* 15 */ \
135_o0 |= (_i1 & B22) << 10; /* 55 */ \
136_o0 |= (_i0 & B22) << 9; /* 23 */ \
137_o0 |= (_i1 & B30) << 16; /* 63 */ \
138_o0 |= (_i0 & B30) << 15; /* 31 */ \
139_o0 |= (_i1 & B05) >> 11; /* 38 */ \
140_o0 |= (_i0 & B05) >> 12; /* 6 */ \
141_o0 |= (_i1 & B13) >> 5; /* 46 */ \
142_o0 |= (_i0 & B13) >> 6; /* 14 */ \
143_o0 |= (_i1 & B21) << 1; /* 54 */ \
144_o0 |= (_i0 & B21) ; /* 22 */ \
145_o0 |= (_i1 & B29) << 7; /* 62 */ \
146_o0 |= (_i0 & B29) << 6; /* 30 */ \
147_o0 |= (_i1 & B04) >> 20; /* 37 */ \
148_o0 |= (_i0 & B04) >> 21; /* 5 */ \
149_o0 |= (_i1 & B12) >> 14; /* 45 */ \
150_o0 |= (_i0 & B12) >> 15; /* 13 */ \
151_o0 |= (_i1 & B20) >> 8; /* 53 */ \
152_o0 |= (_i0 & B20) >> 9; /* 21 */ \
153_o0 |= (_i1 & B28) >> 2; /* 61 */ \
154_o0 |= (_i0 & B28) >> 3; /* 29 */ \
155_o1 |= (_i1 & B03) << 3; /* 36 */ \
156_o1 |= (_i0 & B03) << 2; /* 4 */ \
157_o1 |= (_i1 & B11) << 9; /* 44 */ \
158_o1 |= (_i0 & B11) << 8; /* 12 */ \
159_o1 |= (_i1 & B19) << 15; /* 52 */ \
160_o1 |= (_i0 & B19) << 14; /* 20 */ \
161_o1 |= (_i1 & B27) << 21; /* 60 */ \
162_o1 |= (_i0 & B27) << 20; /* 28 */ \
163_o1 |= (_i1 & B02) >> 6; /* 35 */ \
164_o1 |= (_i0 & B02) >> 7; /* 3 */ \
165_o1 |= (_i1 & B10) ; /* 43 */ \
166_o1 |= (_i0 & B10) >> 1; /* 11 */ \
167_o1 |= (_i1 & B18) << 6; /* 51 */ \
168_o1 |= (_i0 & B18) << 5; /* 19 */ \
169_o1 |= (_i1 & B26) << 12; /* 59 */ \
170_o1 |= (_i0 & B26) << 11; /* 27 */ \
171_o1 |= (_i1 & B01) >> 15; /* 34 */ \
172_o1 |= (_i0 & B01) >> 16; /* 2 */ \
173_o1 |= (_i1 & B09) >> 9; /* 42 */ \
174_o1 |= (_i0 & B09) >> 10; /* 10 */ \
175_o1 |= (_i1 & B17) >> 3; /* 50 */ \
176_o1 |= (_i0 & B17) >> 4; /* 18 */ \
177_o1 |= (_i1 & B25) << 3; /* 58 */ \
178_o1 |= (_i0 & B25) << 2; /* 26 */ \
179_o1 |= (_i1 & B00) >> 24; /* 33 */ \
180_o1 |= (_i0 & B00) >> 25; /* 1 */ \
181_o1 |= (_i1 & B08) >> 18; /* 41 */ \
182_o1 |= (_i0 & B08) >> 19; /* 9 */ \
183_o1 |= (_i1 & B16) >> 12; /* 49 */ \
184_o1 |= (_i0 & B16) >> 13; /* 17 */ \
185_o1 |= (_i1 & B24) >> 6; /* 57 */ \
186_o1 |= (_i0 & B24) >> 7; /* 25 */ \
187}
188
189/* 64b -> 2x28b */
190#define PC1_AUX(_i0, _i1, _o0, _o1) \
191{ \
192_o0 = _o1 = 0; \
193_o0 |= (_i1 & B24) << 24; /* 57 */ \
194_o0 |= (_i1 & B16) << 15; /* 49 */ \
195_o0 |= (_i1 & B08) << 6; /* 41 */ \
196_o0 |= (_i1 & B00) >> 3; /* 33 */ \
197_o0 |= (_i0 & B24) << 20; /* 25 */ \
198_o0 |= (_i0 & B16) << 11; /* 17 */ \
199_o0 |= (_i0 & B08) << 2; /* 9 */ \
200_o0 |= (_i0 & B00) >> 7; /* 1 */ \
201_o0 |= (_i1 & B25) << 17; /* 58 */ \
202_o0 |= (_i1 & B17) << 8; /* 50 */ \
203_o0 |= (_i1 & B09) >> 1; /* 42 */ \
204_o0 |= (_i1 & B01) >> 10; /* 34 */ \
205_o0 |= (_i0 & B25) << 13; /* 26 */ \
206_o0 |= (_i0 & B17) << 4; /* 18 */ \
207_o0 |= (_i0 & B09) >> 5; /* 10 */ \
208_o0 |= (_i0 & B01) >> 14; /* 2 */ \
209_o0 |= (_i1 & B26) << 10; /* 59 */ \
210_o0 |= (_i1 & B18) << 1; /* 51 */ \
211_o0 |= (_i1 & B10) >> 8; /* 43 */ \
212_o0 |= (_i1 & B02) >> 17; /* 35 */ \
213_o0 |= (_i0 & B26) << 6; /* 27 */ \
214_o0 |= (_i0 & B18) >> 3; /* 19 */ \
215_o0 |= (_i0 & B10) >> 12; /* 11 */ \
216_o0 |= (_i0 & B02) >> 21; /* 3 */ \
217_o0 |= (_i1 & B27) << 3; /* 60 */ \
218_o0 |= (_i1 & B19) >> 6; /* 52 */ \
219_o0 |= (_i1 & B11) >> 15; /* 44 */ \
220_o0 |= (_i1 & B03) >> 24; /* 36 */ \
221_o1 |= (_i1 & B30) << 30; /* 63 */ \
222_o1 |= (_i1 & B22) << 21; /* 55 */ \
223_o1 |= (_i1 & B14) << 12; /* 47 */ \
224_o1 |= (_i1 & B06) << 3; /* 39 */ \
225_o1 |= (_i0 & B30) << 26; /* 31 */ \
226_o1 |= (_i0 & B22) << 17; /* 23 */ \
227_o1 |= (_i0 & B14) << 8; /* 15 */ \
228_o1 |= (_i0 & B06) >> 1; /* 7 */ \
229_o1 |= (_i1 & B29) << 21; /* 62 */ \
230_o1 |= (_i1 & B21) << 12; /* 54 */ \
231_o1 |= (_i1 & B13) << 3; /* 46 */ \
232_o1 |= (_i1 & B05) >> 6; /* 38 */ \
233_o1 |= (_i0 & B29) << 17; /* 30 */ \
234_o1 |= (_i0 & B21) << 8; /* 22 */ \
235_o1 |= (_i0 & B13) >> 1; /* 14 */ \
236_o1 |= (_i0 & B05) >> 10; /* 6 */ \
237_o1 |= (_i1 & B28) << 12; /* 61 */ \
238_o1 |= (_i1 & B20) << 3; /* 53 */ \
239_o1 |= (_i1 & B12) >> 6; /* 45 */ \
240_o1 |= (_i1 & B04) >> 15; /* 37 */ \
241_o1 |= (_i0 & B28) << 8; /* 29 */ \
242_o1 |= (_i0 & B20) >> 1; /* 21 */ \
243_o1 |= (_i0 & B12) >> 10; /* 13 */ \
244_o1 |= (_i0 & B04) >> 19; /* 5 */ \
245_o1 |= (_i0 & B27) << 3; /* 28 */ \
246_o1 |= (_i0 & B19) >> 6; /* 20 */ \
247_o1 |= (_i0 & B11) >> 15; /* 12 */ \
248_o1 |= (_i0 & B03) >> 24; /* 4 */ \
249}
250
251/* 2x28b -> 8x6b */
252#define PC2_AUX(_i0, _i1, _o0, _o1) \
253{ \
254_o0 = _o1 = 0; \
255_o0 |= (_i0 & B13) << 11; /* 14 */ \
256_o0 |= (_i0 & B16) << 13; /* 17 */ \
257_o0 |= (_i0 & B10) << 6; /* 11 */ \
258_o0 |= (_i0 & B23) << 18; /* 24 */ \
259_o0 |= (_i0 & B00) >> 6; /* 1 */ \
260_o0 |= (_i0 & B04) >> 3; /* 5 */ \
261_o0 |= (_i0 & B02) >> 8; /* 3 */ \
262_o0 |= (_i0 & B27) << 16; /* 28 */ \
263_o0 |= (_i0 & B14) << 2; /* 15 */ \
264_o0 |= (_i0 & B05) >> 8; /* 6 */ \
265_o0 |= (_i0 & B20) << 6; /* 21 */ \
266_o0 |= (_i0 & B09) >> 6; /* 10 */ \
267_o0 |= (_i0 & B22) << 4; /* 23 */ \
268_o0 |= (_i0 & B18) >> 1; /* 19 */ \
269_o0 |= (_i0 & B11) >> 9; /* 12 */ \
270_o0 |= (_i0 & B03) >> 18; /* 4 */ \
271_o0 |= (_i0 & B25) << 3; /* 26 */ \
272_o0 |= (_i0 & B07) >> 16; /* 8 */ \
273_o0 |= (_i0 & B15) >> 11; /* 16 */ \
274_o0 |= (_i0 & B06) >> 21; /* 7 */ \
275_o0 |= (_i0 & B26) >> 2; /* 27 */ \
276_o0 |= (_i0 & B19) >> 10; /* 20 */ \
277_o0 |= (_i0 & B12) >> 18; /* 13 */ \
278_o0 |= (_i0 & B01) >> 30; /* 2 */ \
279_o1 |= (_i1 & B12) << 10; /* 41 */ \
280_o1 |= (_i1 & B23) << 20; /* 52 */ \
281_o1 |= (_i1 & B02) >> 2; /* 31 */ \
282_o1 |= (_i1 & B08) << 3; /* 37 */ \
283_o1 |= (_i1 & B18) << 12; /* 47 */ \
284_o1 |= (_i1 & B26) << 19; /* 55 */ \
285_o1 |= (_i1 & B01) >> 9; /* 30 */ \
286_o1 |= (_i1 & B11) ; /* 40 */ \
287_o1 |= (_i1 & B22) << 10; /* 51 */ \
288_o1 |= (_i1 & B16) << 3; /* 45 */ \
289_o1 |= (_i1 & B04) >> 10; /* 33 */ \
290_o1 |= (_i1 & B19) << 4; /* 48 */ \
291_o1 |= (_i1 & B15) >> 3; /* 44 */ \
292_o1 |= (_i1 & B20) << 1; /* 49 */ \
293_o1 |= (_i1 & B10) >> 10; /* 39 */ \
294_o1 |= (_i1 & B27) << 6; /* 56 */ \
295_o1 |= (_i1 & B05) >> 17; /* 34 */ \
296_o1 |= (_i1 & B24) << 1; /* 53 */ \
297_o1 |= (_i1 & B17) >> 9; /* 46 */ \
298_o1 |= (_i1 & B13) >> 14; /* 42 */ \
299_o1 |= (_i1 & B21) >> 7; /* 50 */ \
300_o1 |= (_i1 & B07) >> 22; /* 36 */ \
301_o1 |= (_i1 & B00) >> 30; /* 29 */ \
302_o1 |= (_i1 & B03) >> 28; /* 32 */ \
303}
304
305static
306Word s_p0[64] = { /* Combined S-Box1 and permutation P */
307 0x00808200, 0x00000000, 0x00008000, 0x00808202,
308 0x00808002, 0x00008202, 0x00000002, 0x00008000,
309 0x00000200, 0x00808200, 0x00808202, 0x00000200,
310 0x00800202, 0x00808002, 0x00800000, 0x00000002,
311 0x00000202, 0x00800200, 0x00800200, 0x00008200,
312 0x00008200, 0x00808000, 0x00808000, 0x00800202,
313 0x00008002, 0x00800002, 0x00800002, 0x00008002,
314 0x00000000, 0x00000202, 0x00008202, 0x00800000,
315 0x00008000, 0x00808202, 0x00000002, 0x00808000,
316 0x00808200, 0x00800000, 0x00800000, 0x00000200,
317 0x00808002, 0x00008000, 0x00008200, 0x00800002,
318 0x00000200, 0x00000002, 0x00800202, 0x00008202,
319 0x00808202, 0x00008002, 0x00808000, 0x00800202,
320 0x00800002, 0x00000202, 0x00008202, 0x00808200,
321 0x00000202, 0x00800200, 0x00800200, 0x00000000,
322 0x00008002, 0x00008200, 0x00000000, 0x00808002
323};
324
325static
326Word s_p1[64] = { /* Combined S-Box2 and permutation P */
327 0x40084010, 0x40004000, 0x00004000, 0x00084010,
328 0x00080000, 0x00000010, 0x40080010, 0x40004010,
329 0x40000010, 0x40084010, 0x40084000, 0x40000000,
330 0x40004000, 0x00080000, 0x00000010, 0x40080010,
331 0x00084000, 0x00080010, 0x40004010, 0x00000000,
332 0x40000000, 0x00004000, 0x00084010, 0x40080000,
333 0x00080010, 0x40000010, 0x00000000, 0x00084000,
334 0x00004010, 0x40084000, 0x40080000, 0x00004010,
335 0x00000000, 0x00084010, 0x40080010, 0x00080000,
336 0x40004010, 0x40080000, 0x40084000, 0x00004000,
337 0x40080000, 0x40004000, 0x00000010, 0x40084010,
338 0x00084010, 0x00000010, 0x00004000, 0x40000000,
339 0x00004010, 0x40084000, 0x00080000, 0x40000010,
340 0x00080010, 0x40004010, 0x40000010, 0x00080010,
341 0x00084000, 0x00000000, 0x40004000, 0x00004010,
342 0x40000000, 0x40080010, 0x40084010, 0x00084000
343};
344
345static
346Word s_p2[64] = { /* Combined S-Box3 and permutation P */
347 0x00000104, 0x04010100, 0x00000000, 0x04010004,
348 0x04000100, 0x00000000, 0x00010104, 0x04000100,
349 0x00010004, 0x04000004, 0x04000004, 0x00010000,
350 0x04010104, 0x00010004, 0x04010000, 0x00000104,
351 0x04000000, 0x00000004, 0x04010100, 0x00000100,
352 0x00010100, 0x04010000, 0x04010004, 0x00010104,
353 0x04000104, 0x00010100, 0x00010000, 0x04000104,
354 0x00000004, 0x04010104, 0x00000100, 0x04000000,
355 0x04010100, 0x04000000, 0x00010004, 0x00000104,
356 0x00010000, 0x04010100, 0x04000100, 0x00000000,
357 0x00000100, 0x00010004, 0x04010104, 0x04000100,
358 0x04000004, 0x00000100, 0x00000000, 0x04010004,
359 0x04000104, 0x00010000, 0x04000000, 0x04010104,
360 0x00000004, 0x00010104, 0x00010100, 0x04000004,
361 0x04010000, 0x04000104, 0x00000104, 0x04010000,
362 0x00010104, 0x00000004, 0x04010004, 0x00010100
363};
364
365static
366Word s_p3[64] = { /* Combined S-Box4 and permutation P */
367 0x80401000, 0x80001040, 0x80001040, 0x00000040,
368 0x00401040, 0x80400040, 0x80400000, 0x80001000,
369 0x00000000, 0x00401000, 0x00401000, 0x80401040,
370 0x80000040, 0x00000000, 0x00400040, 0x80400000,
371 0x80000000, 0x00001000, 0x00400000, 0x80401000,
372 0x00000040, 0x00400000, 0x80001000, 0x00001040,
373 0x80400040, 0x80000000, 0x00001040, 0x00400040,
374 0x00001000, 0x00401040, 0x80401040, 0x80000040,
375 0x00400040, 0x80400000, 0x00401000, 0x80401040,
376 0x80000040, 0x00000000, 0x00000000, 0x00401000,
377 0x00001040, 0x00400040, 0x80400040, 0x80000000,
378 0x80401000, 0x80001040, 0x80001040, 0x00000040,
379 0x80401040, 0x80000040, 0x80000000, 0x00001000,
380 0x80400000, 0x80001000, 0x00401040, 0x80400040,
381 0x80001000, 0x00001040, 0x00400000, 0x80401000,
382 0x00000040, 0x00400000, 0x00001000, 0x00401040
383};
384
385static
386Word s_p4[64] = { /* Combined S-Box5 and permutation P */
387 0x00000080, 0x01040080, 0x01040000, 0x21000080,
388 0x00040000, 0x00000080, 0x20000000, 0x01040000,
389 0x20040080, 0x00040000, 0x01000080, 0x20040080,
390 0x21000080, 0x21040000, 0x00040080, 0x20000000,
391 0x01000000, 0x20040000, 0x20040000, 0x00000000,
392 0x20000080, 0x21040080, 0x21040080, 0x01000080,
393 0x21040000, 0x20000080, 0x00000000, 0x21000000,
394 0x01040080, 0x01000000, 0x21000000, 0x00040080,
395 0x00040000, 0x21000080, 0x00000080, 0x01000000,
396 0x20000000, 0x01040000, 0x21000080, 0x20040080,
397 0x01000080, 0x20000000, 0x21040000, 0x01040080,
398 0x20040080, 0x00000080, 0x01000000, 0x21040000,
399 0x21040080, 0x00040080, 0x21000000, 0x21040080,
400 0x01040000, 0x00000000, 0x20040000, 0x21000000,
401 0x00040080, 0x01000080, 0x20000080, 0x00040000,
402 0x00000000, 0x20040000, 0x01040080, 0x20000080
403};
404
405static
406Word s_p5[64] = { /* Combined S-Box6 and permutation P */
407 0x10000008, 0x10200000, 0x00002000, 0x10202008,
408 0x10200000, 0x00000008, 0x10202008, 0x00200000,
409 0x10002000, 0x00202008, 0x00200000, 0x10000008,
410 0x00200008, 0x10002000, 0x10000000, 0x00002008,
411 0x00000000, 0x00200008, 0x10002008, 0x00002000,
412 0x00202000, 0x10002008, 0x00000008, 0x10200008,
413 0x10200008, 0x00000000, 0x00202008, 0x10202000,
414 0x00002008, 0x00202000, 0x10202000, 0x10000000,
415 0x10002000, 0x00000008, 0x10200008, 0x00202000,
416 0x10202008, 0x00200000, 0x00002008, 0x10000008,
417 0x00200000, 0x10002000, 0x10000000, 0x00002008,
418 0x10000008, 0x10202008, 0x00202000, 0x10200000,
419 0x00202008, 0x10202000, 0x00000000, 0x10200008,
420 0x00000008, 0x00002000, 0x10200000, 0x00202008,
421 0x00002000, 0x00200008, 0x10002008, 0x00000000,
422 0x10202000, 0x10000000, 0x00200008, 0x10002008
423};
424
425static
426Word s_p6[64] = { /* Combined S-Box7 and permutation P */
427 0x00100000, 0x02100001, 0x02000401, 0x00000000,
428 0x00000400, 0x02000401, 0x00100401, 0x02100400,
429 0x02100401, 0x00100000, 0x00000000, 0x02000001,
430 0x00000001, 0x02000000, 0x02100001, 0x00000401,
431 0x02000400, 0x00100401, 0x00100001, 0x02000400,
432 0x02000001, 0x02100000, 0x02100400, 0x00100001,
433 0x02100000, 0x00000400, 0x00000401, 0x02100401,
434 0x00100400, 0x00000001, 0x02000000, 0x00100400,
435 0x02000000, 0x00100400, 0x00100000, 0x02000401,
436 0x02000401, 0x02100001, 0x02100001, 0x00000001,
437 0x00100001, 0x02000000, 0x02000400, 0x00100000,
438 0x02100400, 0x00000401, 0x00100401, 0x02100400,
439 0x00000401, 0x02000001, 0x02100401, 0x02100000,
440 0x00100400, 0x00000000, 0x00000001, 0x02100401,
441 0x00000000, 0x00100401, 0x02100000, 0x00000400,
442 0x02000001, 0x02000400, 0x00000400, 0x00100001
443};
444
445static
446Word s_p7[64] = { /* Combined S-Box8 and permutation P */
447 0x08000820, 0x00000800, 0x00020000, 0x08020820,
448 0x08000000, 0x08000820, 0x00000020, 0x08000000,
449 0x00020020, 0x08020000, 0x08020820, 0x00020800,
450 0x08020800, 0x00020820, 0x00000800, 0x00000020,
451 0x08020000, 0x08000020, 0x08000800, 0x00000820,
452 0x00020800, 0x00020020, 0x08020020, 0x08020800,
453 0x00000820, 0x00000000, 0x00000000, 0x08020020,
454 0x08000020, 0x08000800, 0x00020820, 0x00020000,
455 0x00020820, 0x00020000, 0x08020800, 0x00000800,
456 0x00000020, 0x08020020, 0x00000800, 0x00020820,
457 0x08000800, 0x00000020, 0x08000020, 0x08020000,
458 0x08020020, 0x08000000, 0x00020000, 0x08000820,
459 0x00000000, 0x08020820, 0x00020020, 0x08000020,
460 0x08020000, 0x08000800, 0x08000820, 0x00000000,
461 0x08020820, 0x00020800, 0x00020800, 0x00000820,
462 0x00000820, 0x00020020, 0x08000000, 0x08020800
463};
464
465#define INITIAL_PERMUTATION(t, regL, regR) \
466 INITIAL_PERMUTATION_AUX(t[0], t[1], regL, regR)
467
468#define FINAL_PERMUTATION(regR, regL, t) \
469 FINAL_PERMUTATION_AUX(regR, regL, t[0], t[1])
470
471#define PC1(k, regC, regD) \
472 PC1_AUX(k[0], k[1], regC, regD)
473
474#define PC2(regC, regD, k) \
475 PC2_AUX(regC, regD, k[0], k[1])
476
477unsigned char G_padChar = (char)0; /* Default pad charcater */
478
480{
481 Word a;
482 a = (x & 0x80000000) >> 27;
483 return (x << 1) | a;
484}
485
487{
488 Word a;
489 a = x & 0x00000010;
490 return (x >> 1) | (a << 27);
491}
492
493/*
494** The S Box transformations and the permutation P are combined in the vectors
495** s_p0 - s_p7. Permutation E and the MOD 2 addition with the intermediate key
496** are then done "inline" on each round. The intermediate key is already in a
497** a 8x6bit form because of the modified permuation PC2 used.
498*/
499
500#if !defined(WORDS_BIGENDIAN)
501
502#define DES(t, ik) \
503{ \
504 register Word l, r, reg32, round; \
505 register unsigned char *bb; \
506 INITIAL_PERMUTATION(t, l, r); \
507 for(bb = (unsigned char *) ik, round = 0x8000; round; bb += 8, round >>= 1) { \
508 register Word w = (r << 1) | (r >> 31); \
509 reg32 = s_p7[( w & 0x3f) ^ bb[4]]; \
510 reg32 |= s_p6[((w >>= 4) & 0x3f) ^ bb[5]]; \
511 reg32 |= s_p5[((w >>= 4) & 0x3f) ^ bb[6]]; \
512 reg32 |= s_p4[((w >>= 4) & 0x3f) ^ bb[7]]; \
513 reg32 |= s_p3[((w >>= 4) & 0x3f) ^ bb[0]]; \
514 reg32 |= s_p2[((w >>= 4) & 0x3f) ^ bb[1]]; \
515 reg32 |= s_p1[((w >> 4) & 0x3f) ^ bb[2]]; \
516 reg32 |= s_p0[(((r & 0x1) << 5) | ((r & 0xf8000000) >> 27)) ^ bb[3]]; \
517 reg32 ^= l; \
518 l = r; \
519 r = reg32; \
520 } \
521 FINAL_PERMUTATION(r, l, t); \
522}
523
524#define MAKE_LITTLE_ENDIAN(t, s) \
525{ \
526 register unsigned int z, l = s/4; \
527 register Word *tp = (Word *)(void *) t; \
528 for(z = 0; z < l; ++z) tp[z] = htonl(tp[z]); \
529}
530
531#else /* WORDS_BIGENDIAN */
532
533#define DES(t, ik) \
534{ \
535 register Word l, r, reg32, round; \
536 register unsigned char *bb; \
537 INITIAL_PERMUTATION(t, l, r); \
538 for(bb = (unsigned char *) ik, round = 0x8000; round; bb += 8, round >>= 1) { \
539 register Word w = (r << 1) | (r >> 31); \
540 reg32 = s_p7[( w & 0x3f) ^ bb[7]]; \
541 reg32 |= s_p6[((w >>= 4) & 0x3f) ^ bb[6]]; \
542 reg32 |= s_p5[((w >>= 4) & 0x3f) ^ bb[5]]; \
543 reg32 |= s_p4[((w >>= 4) & 0x3f) ^ bb[4]]; \
544 reg32 |= s_p3[((w >>= 4) & 0x3f) ^ bb[3]]; \
545 reg32 |= s_p2[((w >>= 4) & 0x3f) ^ bb[2]]; \
546 reg32 |= s_p1[((w >> 4) & 0x3f) ^ bb[1]]; \
547 reg32 |= s_p0[(((r & 0x1) << 5) | ((r & 0xf8000000) >> 27)) ^ bb[0]]; \
548 reg32 ^= l; \
549 l = r; \
550 r = reg32; \
551 } \
552 FINAL_PERMUTATION(r, l, t); \
553}
554
555#endif /* WORDS_BIGENDIAN */
556
557int
558qfDES(unsigned char *key,
559 unsigned char *data,
560 unsigned int size,
561 const QFDES_what what, const QFDES_mode mode, unsigned char *initVec)
562{
563 /* Store some info to optimise for multiple calls ... */
564 static unsigned char desKey[8], desKeys[128];
565 static Word *oldKey = (Word *)(void *) desKey, *keys = (Word *) desKeys;
566 static QFDES_what oldWhat;
567 static QFDES_mode oldMode __attribute__((unused));
568 unsigned char b0[8] = "", b1[8]; /* feedback blocks */
569 Word *newKey = (Word *)(void *) key, /* key from user */
570 *text, /* text to be [en|de]crypted */
571 *cb = (Word *)(void *) b0, /* the chained block in CBC mode */
572 *cb1 = (Word *)(void *) b1; /* a copy for use when decrypting */
573
574#if !defined(WORDS_BIGENDIAN)
575 unsigned int origSize = size;
577 MAKE_LITTLE_ENDIAN(data, origSize);
578#endif
579
580 /*
581 ** Check new key against old key
582 ** and set up intermediate keys.
583 */
584 if (newKey[0] != oldKey[0] || newKey[1] != oldKey[1]) {
585 Word c, d; /* C and D registers */
586
587 oldKey[0] = newKey[0];
588 oldKey[1] = newKey[1];
589 oldWhat = what;
590 oldMode = mode;
591
592 PC1(newKey, c, d);
593
594 if ((what == qfDES_encrypt) || (mode == qfDES_cfb)
595 || (mode == qfDES_ofb)) {
596 int z;
597 Word r;
598 Word *k = keys;
599 Word rol[16] =
600 { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
601
602 for (z = 0; z < 16; z++, k += 2) {
603 for (r = 0; r < rol[z]; r++) {
604 c = ROTATE_LEFT(c);
605 d = ROTATE_LEFT(d);
606 }
607 PC2(c, d, k);
608 }
609 } else {
610 int z;
611 Word r;
612 Word *k = keys;
613 Word ror[16] =
614 { 0, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
615
616 for (z = 0; z < 16; z++, k += 2) {
617 r = 0;
618 while (ror[z] > r) {
619 r++;
620 c = ROTATE_RIGHT(c);
621 d = ROTATE_RIGHT(d);
622 }
623 PC2(c, d, k);
624 }
625 }
626 } else if ((what != oldWhat)
627 && ((mode == qfDES_ecb) || (mode == qfDES_cbc))) {
628 /*
629 ** Same key but different direction.
630 ** Reverse intermediate key sequence (ECB and CBC).
631 */
632 Word *ik1, *ik2, ik3[2];
633
634 for (ik1 = keys, ik2 = &(keys[30]); ik1 < ik2;
635 ik1 += 2, ik2 -= 2) {
636 ik3[0] = ik1[0];
637 ik3[1] = ik1[1];
638 ik1[0] = ik2[0];
639 ik1[1] = ik2[1];
640 ik2[0] = ik3[0];
641 ik2[1] = ik3[1];
642 }
643
644 oldWhat = what;
645 }
646
647 /* Set up initilaisation vector */
648 if (mode != qfDES_ecb) {
649 if (initVec) {
650 {
651 cb[0] = ((Word *)(void *) initVec)[0];
652 cb[1] = ((Word *)(void *) initVec)[1];
653 }
654 } else {
655 cb[0] = 0;
656 cb[1] = 0;
657 }
658#if !defined(WORDS_BIGENDIAN)
659 MAKE_LITTLE_ENDIAN(cb, 8);
660#endif
661 }
662
663 /*
664 ** Lots of gotos and code duplication follow (arrgh) but it speeds
665 ** it up a wee bit!
666 ** What would be more useful is looking more carefully at the DES
667 ** permutations to produce more efficient versions of the macros
668 ** of the "auto-generated" versions used in qfDES-aux.c.
669 */
670
671 size >>= 3; /* this is always a multpile of 8 */
672
673 if (what == qfDES_encrypt) {
674 switch ((int)mode) {
675 case ((int)qfDES_ecb):
676 goto _ECB_;
677 case ((int)qfDES_cbc):
678 goto _CBC_encrypt_;
679 case ((int)qfDES_cfb):
680 goto _CFB_encrypt_;
681 case ((int)qfDES_ofb):
682 goto _OFB_;
683 }
684 } else {
685 switch ((int)mode) {
686 case ((int)qfDES_ecb):
687 goto _ECB_;
688 case ((int)qfDES_cbc):
689 goto _CBC_decrypt_;
690 case ((int)qfDES_cfb):
691 goto _CFB_decrypt_;
692 case ((int)qfDES_ofb):
693 goto _OFB_;
694 }
695 }
696
697 _ECB_:
698
699 /* ECB */
700 for (text = (Word *)(void *) data; size; --size, text += 2) {
701 DES(text, keys);
702 }
703
704 goto _exit_qfDES_;
705
706 _CBC_encrypt_:
707
708 /* CBC Encryption */
709 for (text = (Word *)(void *) data; size; --size, text += 2) {
710
711 /* chaining block */
712 text[0] ^= cb[0];
713 text[1] ^= cb[1];
714
715 DES(text, keys);
716
717 /* set up chaining block for next round */
718 cb[0] = text[0];
719 cb[1] = text[1];
720 }
721
722 goto _initVec_;
723
724 _CBC_decrypt_:
725
726 /* CBC Decryption */
727 for (text = (Word *)(void *) data; size; --size, text += 2) {
728
729 /* set up chaining block */
730 /*
731 ** The decryption is done in place so I need
732 ** to copy this text block for the next round.
733 */
734 cb1[0] = text[0];
735 cb1[1] = text[1];
736
737 DES(text, keys);
738
739 /* chaining block for next round */
740 text[0] ^= cb[0];
741 text[1] ^= cb[1];
742
743 /*
744 ** Copy back the saved encrypted text - this makes
745 ** CBC decryption slower than CBC encryption.
746 */
747 cb[0] = cb1[0];
748 cb[1] = cb1[1];
749 }
750
751 goto _initVec_;
752
753 _CFB_encrypt_:
754
755 /* CFB Encryption */
756 for (text = (Word *)(void *) data; size; --size, text += 2) {
757
758 /* use cb as the feedback block */
759 DES(cb, keys);
760
761 text[0] ^= cb[0];
762 text[1] ^= cb[1];
763
764 /* set up feedback block for next round */
765 cb[0] = text[0];
766 cb[1] = text[1];
767 }
768
769 goto _initVec_;
770
771 _CFB_decrypt_:
772
773 /* CFB Decryption */
774 for (text = (Word *)(void *) data; size; --size, text += 2) {
775
776 /* set up feedback block */
777 /*
778 ** The decryption is done in place so I need
779 ** to copy this text block for the next round.
780 */
781 cb1[0] = text[0];
782 cb1[1] = text[1];
783
784 /* use cb as the feedback block */
785 DES(cb, keys);
786
787 text[0] ^= cb[0];
788 text[1] ^= cb[1];
789
790 /* set up feedback block for next round */
791 cb[0] = cb1[0];
792 cb[1] = cb1[1];
793 }
794
795 goto _initVec_;
796
797 _OFB_:
798
799 /* OFB */
800 for (text = (Word *)(void *) data; size; --size, text += 2) {
801
802 /* use cb as the feed back block */
803 DES(cb, keys);
804
805 text[0] ^= cb[0];
806 text[1] ^= cb[1];
807 }
808
809 _initVec_:
810
811 /*
812 ** Copy the final chained block back to initVec (CBC, CFB and OFB).
813 ** This allows the [en|de]cryption of large amounts of data in small
814 ** chunks.
815 */
816 if (initVec) {
817 ((Word *)(void *) initVec)[0] = cb[0];
818 ((Word *)(void *) initVec)[1] = cb[1];
819
820#if !defined(WORDS_BIGENDIAN)
821 MAKE_LITTLE_ENDIAN(initVec, 8);
822#endif
823 }
824
825 _exit_qfDES_:
826
827#if !defined(WORDS_BIGENDIAN)
829 MAKE_LITTLE_ENDIAN(data, origSize);
830#endif
831
832 return 0;
833}
834
835/*
836** This function sets bit 8 of each byte to odd or even parity as requested.
837** It is assumed that the right-most bit of each byte is the parity bit.
838** Although this is really only used by the two key generation functions below,
839** it may be useful to someone.
840*/
841
842void qfDES_setParity(unsigned char *ptr, unsigned int size,
843 const QFDES_parity parity)
844{
845 unsigned int i, mask, bits;
846
847 for (i = 0; i < size; ++i, ++ptr) {
848 for (mask = 0x80, bits = 0; mask > 0x01; mask >>= 1)
849 if (((unsigned int)*ptr) & mask)
850 ++bits;
851
852 *ptr |= bits % 2 == (unsigned int)parity ? 0x00 : 0x01;
853 }
854}
855
856unsigned int qfDES_checkParity(unsigned char *ptr, unsigned int size,
857 const QFDES_parity parity)
858{
859 unsigned int i, mask, bits, parityBit, parityErrors = 0;
860
861 for (i = 0; i < size; ++i, ++ptr) {
862 for (mask = 0x80, bits = 0; mask > 0x01; mask >>= 1)
863 if (((unsigned int)*ptr) & mask)
864 ++bits;
865
866 parityBit = bits % 2 == (unsigned int)parity ? 0 : 1;
867
868 if ((((unsigned int)*ptr) & 0x1) != parityBit)
869 ++parityErrors;
870 }
871
872 return parityErrors;
873}
874
875static
876unsigned char weakKeys[18][8] =
877 { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
878{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
879{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
880{0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe},
881{0x1f, 0x1f, 0x1f, 0x1f, 0x0e, 0x0e, 0x0e, 0x0e},
882{0xe0, 0xe0, 0xe0, 0xe0, 0xf1, 0xf1, 0xf1, 0xf1},
883{0x01, 0xfe, 0x01, 0xfe, 0x01, 0xfe, 0x01, 0xfe},
884{0xfe, 0x01, 0xfe, 0x01, 0xfe, 0x01, 0xfe, 0x01},
885{0x1f, 0xe0, 0x1f, 0xe0, 0x0e, 0xf1, 0x0e, 0xf1},
886{0xe0, 0x1f, 0xe0, 0x1f, 0xf1, 0x0e, 0xf1, 0x0e},
887{0x01, 0xe0, 0x01, 0xe0, 0x01, 0xf1, 0x01, 0xf1},
888{0xe0, 0x01, 0xe0, 0x01, 0xf1, 0x01, 0xf1, 0x01},
889{0x1f, 0xfe, 0x1f, 0xfe, 0x0e, 0xfe, 0x0e, 0xfe},
890{0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x0e, 0xfe, 0x0e},
891{0x01, 0x1f, 0x01, 0x1f, 0x01, 0x0e, 0x01, 0x0e},
892{0x1f, 0x01, 0x1f, 0x01, 0x0e, 0x01, 0x0e, 0x01},
893{0xe0, 0xfe, 0xe0, 0xfe, 0xf1, 0xfe, 0xf1, 0xfe},
894{0xfe, 0xe0, 0xfe, 0xe0, 0xfe, 0xf1, 0xfe, 0xf1}
895};
896
897/*
898** Although this is really only used by the key generation function below,
899** it may be handy to someone.
900*/
901
902int qfDES_checkWeakKeys(unsigned char *key)
903{
904 unsigned char *bp;
905 int i;
906
907 for (bp = weakKeys[i = 0]; i < 18; bp = weakKeys[++i])
908 if (memcmp((void *)key, (void *)bp, 8) == 0)
909 return -1;
910
911 return 0;
912}
913
914/*
915** The following function attempts to genreate a random key or IV.
916** It relies on the randomness of the of the random(3) function. Although
917** it is probably not particularly fast, keys and IV will most probably be
918** generated off-line so it does not matter too much.
919*/
920
921unsigned char *qfDES_generate(const QFDES_generate what)
922{
923 static
924 unsigned char buffer[8];
925 static
926 int flag = 0;
927
928 unsigned char *bp;
929 int mask = what == qfDES_key ? 0xfe : 0xff;
930
931 /* Set up a seed - 42 is the answer ... */
932 if (!flag) {
933 lbl_srandom((int)(getpid() * 42) ^ (int)time((time_t *) 0));
934 flag = 1;
935 }
936 do {
937
938 for (bp = buffer; bp <= &(buffer[7]);
939 *bp++ = (unsigned char)(lbl_random() & mask)) ;
940
941 if (what == qfDES_key)
942 qfDES_setParity(buffer, 8, qfDES_odd);
943
944 } while (what == qfDES_key ? qfDES_checkWeakKeys(buffer) : 0);
945
946 return buffer;
947}
948
949unsigned char qfDES_setPad(unsigned char pad)
950{
951 unsigned char b = G_padChar;
952 G_padChar = pad;
953 return b;
954}
static Word s_p3[64]
Definition crypt_des.c:366
static Word s_p5[64]
Definition crypt_des.c:406
static Word s_p2[64]
Definition crypt_des.c:346
unsigned char G_padChar
Definition crypt_des.c:477
#define PC2(regC, regD, k)
Definition crypt_des.c:474
#define MAKE_LITTLE_ENDIAN(t, s)
Definition crypt_des.c:524
#define DES(t, ik)
Definition crypt_des.c:502
static unsigned char weakKeys[18][8]
Definition crypt_des.c:876
static Word s_p4[64]
Definition crypt_des.c:386
static Word s_p1[64]
Definition crypt_des.c:326
static Word s_p6[64]
Definition crypt_des.c:426
static Word ROTATE_RIGHT(Word x)
Definition crypt_des.c:486
static Word s_p0[64]
Definition crypt_des.c:306
unsigned int Word
Definition crypt_des.c:16
unsigned int qfDES_checkParity(unsigned char *ptr, unsigned int size, const QFDES_parity parity)
Definition crypt_des.c:856
int qfDES(unsigned char *key, unsigned char *data, unsigned int size, const QFDES_what what, const QFDES_mode mode, unsigned char *initVec)
Definition crypt_des.c:558
#define PC1(k, regC, regD)
Definition crypt_des.c:471
int qfDES_checkWeakKeys(unsigned char *key)
Definition crypt_des.c:902
static Word ROTATE_LEFT(Word x)
Definition crypt_des.c:479
static Word s_p7[64]
Definition crypt_des.c:446
unsigned char * qfDES_generate(const QFDES_generate what)
Definition crypt_des.c:921
void qfDES_setParity(unsigned char *ptr, unsigned int size, const QFDES_parity parity)
Definition crypt_des.c:842
unsigned char qfDES_setPad(unsigned char pad)
Definition crypt_des.c:949
QFDES_generate
Definition crypt_des.h:27
@ qfDES_key
Definition crypt_des.h:27
QFDES_mode
Definition crypt_des.h:21
@ qfDES_ecb
Definition crypt_des.h:21
@ qfDES_cbc
Definition crypt_des.h:21
@ qfDES_ofb
Definition crypt_des.h:21
@ qfDES_cfb
Definition crypt_des.h:21
QFDES_what
Definition crypt_des.h:18
@ qfDES_encrypt
Definition crypt_des.h:18
QFDES_parity
Definition crypt_des.h:24
@ qfDES_odd
Definition crypt_des.h:24
uint32_t lbl_random(void)
Definition random.c:61
void lbl_srandom(uint32_t seed)
Definition random.c:56
enum openssl_mode mode
struct __attribute__((packed))
Definition rtp.h:88
const char * key
Definition cineform.cpp:125