Branch data Line data Source code
1 : : /*
2 : : * FreeSec: libcrypt for NetBSD
3 : : *
4 : : * contrib/pgcrypto/crypt-des.c
5 : : *
6 : : * Copyright (c) 1994 David Burren
7 : : * All rights reserved.
8 : : *
9 : : * Adapted for FreeBSD-2.0 by Geoffrey M. Rehmet
10 : : * this file should now *only* export crypt(), in order to make
11 : : * binaries of libcrypt exportable from the USA
12 : : *
13 : : * Adapted for FreeBSD-4.0 by Mark R V Murray
14 : : * this file should now *only* export px_crypt_des(), in order to make
15 : : * a module that can be optionally included in libcrypt.
16 : : *
17 : : * Redistribution and use in source and binary forms, with or without
18 : : * modification, are permitted provided that the following conditions
19 : : * are met:
20 : : * 1. Redistributions of source code must retain the above copyright
21 : : * notice, this list of conditions and the following disclaimer.
22 : : * 2. Redistributions in binary form must reproduce the above copyright
23 : : * notice, this list of conditions and the following disclaimer in the
24 : : * documentation and/or other materials provided with the distribution.
25 : : * 3. Neither the name of the author nor the names of other contributors
26 : : * may be used to endorse or promote products derived from this software
27 : : * without specific prior written permission.
28 : : *
29 : : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
30 : : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 : : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 : : * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
33 : : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 : : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 : : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 : : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 : : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 : : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 : : * SUCH DAMAGE.
40 : : *
41 : : * $FreeBSD: src/secure/lib/libcrypt/crypt-des.c,v 1.12 1999/09/20 12:39:20 markm Exp $
42 : : *
43 : : * This is an original implementation of the DES and the crypt(3) interfaces
44 : : * by David Burren <davidb@werj.com.au>.
45 : : *
46 : : * An excellent reference on the underlying algorithm (and related
47 : : * algorithms) is:
48 : : *
49 : : * B. Schneier, Applied Cryptography: protocols, algorithms,
50 : : * and source code in C, John Wiley & Sons, 1994.
51 : : *
52 : : * Note that in that book's description of DES the lookups for the initial,
53 : : * pbox, and final permutations are inverted (this has been brought to the
54 : : * attention of the author). A list of errata for this book has been
55 : : * posted to the sci.crypt newsgroup by the author and is available for FTP.
56 : : */
57 : :
58 : : #include "postgres.h"
59 : : #include "miscadmin.h"
60 : : #include "port/pg_bswap.h"
61 : :
62 : : #include "px-crypt.h"
63 : :
64 : : #define _PASSWORD_EFMT1 '_'
65 : :
66 : : static const char _crypt_a64[] =
67 : : "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
68 : :
69 : : static uint8 IP[64] = {
70 : : 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
71 : : 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
72 : : 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
73 : : 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
74 : : };
75 : :
76 : : static uint8 inv_key_perm[64];
77 : : static uint8 u_key_perm[56];
78 : : static uint8 key_perm[56] = {
79 : : 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
80 : : 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
81 : : 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
82 : : 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
83 : : };
84 : :
85 : : static uint8 key_shifts[16] = {
86 : : 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
87 : : };
88 : :
89 : : static uint8 inv_comp_perm[56];
90 : : static uint8 comp_perm[48] = {
91 : : 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
92 : : 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
93 : : 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
94 : : 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
95 : : };
96 : :
97 : : /*
98 : : * No E box is used, as it's replaced by some ANDs, shifts, and ORs.
99 : : */
100 : :
101 : : static uint8 u_sbox[8][64];
102 : : static uint8 sbox[8][64] = {
103 : : {
104 : : 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
105 : : 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
106 : : 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
107 : : 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
108 : : },
109 : : {
110 : : 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
111 : : 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
112 : : 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
113 : : 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
114 : : },
115 : : {
116 : : 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
117 : : 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
118 : : 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
119 : : 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
120 : : },
121 : : {
122 : : 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
123 : : 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
124 : : 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
125 : : 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
126 : : },
127 : : {
128 : : 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
129 : : 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
130 : : 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
131 : : 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
132 : : },
133 : : {
134 : : 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
135 : : 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
136 : : 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
137 : : 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
138 : : },
139 : : {
140 : : 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
141 : : 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
142 : : 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
143 : : 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
144 : : },
145 : : {
146 : : 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
147 : : 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
148 : : 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
149 : : 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
150 : : }
151 : : };
152 : :
153 : : static uint8 un_pbox[32];
154 : : static uint8 pbox[32] = {
155 : : 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
156 : : 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
157 : : };
158 : :
159 : : static uint32 _crypt_bits32[32] =
160 : : {
161 : : 0x80000000, 0x40000000, 0x20000000, 0x10000000,
162 : : 0x08000000, 0x04000000, 0x02000000, 0x01000000,
163 : : 0x00800000, 0x00400000, 0x00200000, 0x00100000,
164 : : 0x00080000, 0x00040000, 0x00020000, 0x00010000,
165 : : 0x00008000, 0x00004000, 0x00002000, 0x00001000,
166 : : 0x00000800, 0x00000400, 0x00000200, 0x00000100,
167 : : 0x00000080, 0x00000040, 0x00000020, 0x00000010,
168 : : 0x00000008, 0x00000004, 0x00000002, 0x00000001
169 : : };
170 : :
171 : : static uint8 _crypt_bits8[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
172 : :
173 : : static uint32 saltbits;
174 : : static long old_salt;
175 : : static uint32 *bits28,
176 : : *bits24;
177 : : static uint8 init_perm[64],
178 : : final_perm[64];
179 : : static uint32 en_keysl[16],
180 : : en_keysr[16];
181 : : static uint32 de_keysl[16],
182 : : de_keysr[16];
183 : : static int des_initialised = 0;
184 : : static uint8 m_sbox[4][4096];
185 : : static uint32 psbox[4][256];
186 : : static uint32 ip_maskl[8][256],
187 : : ip_maskr[8][256];
188 : : static uint32 fp_maskl[8][256],
189 : : fp_maskr[8][256];
190 : : static uint32 key_perm_maskl[8][128],
191 : : key_perm_maskr[8][128];
192 : : static uint32 comp_maskl[8][128],
193 : : comp_maskr[8][128];
194 : : static uint32 old_rawkey0,
195 : : old_rawkey1;
196 : :
197 : : static inline int
198 : 72 : ascii_to_bin(char ch)
199 : : {
200 [ - + ]: 72 : if (ch > 'z')
201 : 0 : return 0;
202 [ + + ]: 72 : if (ch >= 'a')
203 : 17 : return (ch - 'a' + 38);
204 [ - + ]: 55 : if (ch > 'Z')
205 : 0 : return 0;
206 [ + + ]: 55 : if (ch >= 'A')
207 : 11 : return (ch - 'A' + 12);
208 [ - + ]: 44 : if (ch > '9')
209 : 0 : return 0;
210 [ + + ]: 44 : if (ch >= '.')
211 : 31 : return (ch - '.');
212 : 13 : return 0;
213 : : }
214 : :
215 : : static void
216 : 2 : des_init(void)
217 : : {
218 : : int i,
219 : : j,
220 : : b,
221 : : k,
222 : : inbit,
223 : : obit;
224 : : uint32 *p,
225 : : *il,
226 : : *ir,
227 : : *fl,
228 : : *fr;
229 : :
230 : 2 : old_rawkey0 = old_rawkey1 = 0L;
231 : 2 : saltbits = 0L;
232 : 2 : old_salt = 0L;
233 : 2 : bits24 = (bits28 = _crypt_bits32 + 4) + 4;
234 : :
235 : : /*
236 : : * Invert the S-boxes, reordering the input bits.
237 : : */
238 [ + + ]: 18 : for (i = 0; i < 8; i++)
239 [ + + ]: 1040 : for (j = 0; j < 64; j++)
240 : : {
241 : 1024 : b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf);
242 : 1024 : u_sbox[i][j] = sbox[i][b];
243 : : }
244 : :
245 : : /*
246 : : * Convert the inverted S-boxes into 4 arrays of 8 bits. Each will handle
247 : : * 12 bits of the S-box input.
248 : : */
249 [ + + ]: 10 : for (b = 0; b < 4; b++)
250 [ + + ]: 520 : for (i = 0; i < 64; i++)
251 [ + + ]: 33280 : for (j = 0; j < 64; j++)
252 : 32768 : m_sbox[b][(i << 6) | j] =
253 : 32768 : (u_sbox[(b << 1)][i] << 4) |
254 : 32768 : u_sbox[(b << 1) + 1][j];
255 : :
256 : : /*
257 : : * Set up the initial & final permutations into a useful form, and
258 : : * initialise the inverted key permutation.
259 : : */
260 [ + + ]: 130 : for (i = 0; i < 64; i++)
261 : : {
262 : 128 : init_perm[final_perm[i] = IP[i] - 1] = i;
263 : 128 : inv_key_perm[i] = 255;
264 : : }
265 : :
266 : : /*
267 : : * Invert the key permutation and initialise the inverted key compression
268 : : * permutation.
269 : : */
270 [ + + ]: 114 : for (i = 0; i < 56; i++)
271 : : {
272 : 112 : u_key_perm[i] = key_perm[i] - 1;
273 : 112 : inv_key_perm[key_perm[i] - 1] = i;
274 : 112 : inv_comp_perm[i] = 255;
275 : : }
276 : :
277 : : /*
278 : : * Invert the key compression permutation.
279 : : */
280 [ + + ]: 98 : for (i = 0; i < 48; i++)
281 : 96 : inv_comp_perm[comp_perm[i] - 1] = i;
282 : :
283 : : /*
284 : : * Set up the OR-mask arrays for the initial and final permutations, and
285 : : * for the key initial and compression permutations.
286 : : */
287 [ + + ]: 18 : for (k = 0; k < 8; k++)
288 : : {
289 [ + + ]: 4112 : for (i = 0; i < 256; i++)
290 : : {
291 : 4096 : *(il = &ip_maskl[k][i]) = 0L;
292 : 4096 : *(ir = &ip_maskr[k][i]) = 0L;
293 : 4096 : *(fl = &fp_maskl[k][i]) = 0L;
294 : 4096 : *(fr = &fp_maskr[k][i]) = 0L;
295 [ + + ]: 36864 : for (j = 0; j < 8; j++)
296 : : {
297 : 32768 : inbit = 8 * k + j;
298 [ + + ]: 32768 : if (i & _crypt_bits8[j])
299 : : {
300 [ + + ]: 16384 : if ((obit = init_perm[inbit]) < 32)
301 : 8192 : *il |= _crypt_bits32[obit];
302 : : else
303 : 8192 : *ir |= _crypt_bits32[obit - 32];
304 [ + + ]: 16384 : if ((obit = final_perm[inbit]) < 32)
305 : 8192 : *fl |= _crypt_bits32[obit];
306 : : else
307 : 8192 : *fr |= _crypt_bits32[obit - 32];
308 : : }
309 : : }
310 : : }
311 [ + + ]: 2064 : for (i = 0; i < 128; i++)
312 : : {
313 : 2048 : *(il = &key_perm_maskl[k][i]) = 0L;
314 : 2048 : *(ir = &key_perm_maskr[k][i]) = 0L;
315 [ + + ]: 16384 : for (j = 0; j < 7; j++)
316 : : {
317 : 14336 : inbit = 8 * k + j;
318 [ + + ]: 14336 : if (i & _crypt_bits8[j + 1])
319 : : {
320 [ - + ]: 7168 : if ((obit = inv_key_perm[inbit]) == 255)
321 : 0 : continue;
322 [ + + ]: 7168 : if (obit < 28)
323 : 3584 : *il |= bits28[obit];
324 : : else
325 : 3584 : *ir |= bits28[obit - 28];
326 : : }
327 : : }
328 : 2048 : *(il = &comp_maskl[k][i]) = 0L;
329 : 2048 : *(ir = &comp_maskr[k][i]) = 0L;
330 [ + + ]: 16384 : for (j = 0; j < 7; j++)
331 : : {
332 : 14336 : inbit = 7 * k + j;
333 [ + + ]: 14336 : if (i & _crypt_bits8[j + 1])
334 : : {
335 [ + + ]: 7168 : if ((obit = inv_comp_perm[inbit]) == 255)
336 : 1024 : continue;
337 [ + + ]: 6144 : if (obit < 24)
338 : 3072 : *il |= bits24[obit];
339 : : else
340 : 3072 : *ir |= bits24[obit - 24];
341 : : }
342 : : }
343 : : }
344 : : }
345 : :
346 : : /*
347 : : * Invert the P-box permutation, and convert into OR-masks for handling
348 : : * the output of the S-box arrays setup above.
349 : : */
350 [ + + ]: 66 : for (i = 0; i < 32; i++)
351 : 64 : un_pbox[pbox[i] - 1] = i;
352 : :
353 [ + + ]: 10 : for (b = 0; b < 4; b++)
354 [ + + ]: 2056 : for (i = 0; i < 256; i++)
355 : : {
356 : 2048 : *(p = &psbox[b][i]) = 0L;
357 [ + + ]: 18432 : for (j = 0; j < 8; j++)
358 : : {
359 [ + + ]: 16384 : if (i & _crypt_bits8[j])
360 : 8192 : *p |= _crypt_bits32[un_pbox[8 * b + j]];
361 : : }
362 : : }
363 : :
364 : 2 : des_initialised = 1;
365 : 2 : }
366 : :
367 : : static void
368 : 13 : setup_salt(long salt)
369 : : {
370 : : uint32 obit,
371 : : saltbit;
372 : : int i;
373 : :
374 [ + + ]: 13 : if (salt == old_salt)
375 : 6 : return;
376 : 7 : old_salt = salt;
377 : :
378 : 7 : saltbits = 0L;
379 : 7 : saltbit = 1;
380 : 7 : obit = 0x800000;
381 [ + + ]: 175 : for (i = 0; i < 24; i++)
382 : : {
383 [ + + ]: 168 : if (salt & saltbit)
384 : 57 : saltbits |= obit;
385 : 168 : saltbit <<= 1;
386 : 168 : obit >>= 1;
387 : : }
388 : : }
389 : :
390 : : static int
391 : 15 : des_setkey(const uint32 *key)
392 : : {
393 : : uint32 k0,
394 : : k1,
395 : : rawkey0,
396 : : rawkey1;
397 : : int shifts,
398 : : round;
399 : :
400 [ - + ]: 15 : if (!des_initialised)
401 : 0 : des_init();
402 : :
403 : 15 : rawkey0 = pg_ntoh32(key[0]);
404 : 15 : rawkey1 = pg_ntoh32(key[1]);
405 : :
406 [ + + ]: 15 : if ((rawkey0 | rawkey1)
407 [ + + ]: 13 : && rawkey0 == old_rawkey0
408 [ + - ]: 6 : && rawkey1 == old_rawkey1)
409 : : {
410 : : /*
411 : : * Already setup for this key. This optimization fails on a zero key
412 : : * (which is weak and has bad parity anyway) in order to simplify the
413 : : * starting conditions.
414 : : */
415 : 6 : return 0;
416 : : }
417 : 9 : old_rawkey0 = rawkey0;
418 : 9 : old_rawkey1 = rawkey1;
419 : :
420 : : /*
421 : : * Do key permutation and split into two 28-bit subkeys.
422 : : */
423 : 9 : k0 = key_perm_maskl[0][rawkey0 >> 25]
424 : 9 : | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f]
425 : 9 : | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f]
426 : 9 : | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f]
427 : 9 : | key_perm_maskl[4][rawkey1 >> 25]
428 : 9 : | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f]
429 : 9 : | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f]
430 : 9 : | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f];
431 : 9 : k1 = key_perm_maskr[0][rawkey0 >> 25]
432 : 9 : | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f]
433 : 9 : | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f]
434 : 9 : | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f]
435 : 9 : | key_perm_maskr[4][rawkey1 >> 25]
436 : 9 : | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f]
437 : 9 : | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f]
438 : 9 : | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f];
439 : :
440 : : /*
441 : : * Rotate subkeys and do compression permutation.
442 : : */
443 : 9 : shifts = 0;
444 [ + + ]: 153 : for (round = 0; round < 16; round++)
445 : : {
446 : : uint32 t0,
447 : : t1;
448 : :
449 : 144 : shifts += key_shifts[round];
450 : :
451 : 144 : t0 = (k0 << shifts) | (k0 >> (28 - shifts));
452 : 144 : t1 = (k1 << shifts) | (k1 >> (28 - shifts));
453 : :
454 : 144 : de_keysl[15 - round] =
455 : 144 : en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f]
456 : 144 : | comp_maskl[1][(t0 >> 14) & 0x7f]
457 : 144 : | comp_maskl[2][(t0 >> 7) & 0x7f]
458 : 144 : | comp_maskl[3][t0 & 0x7f]
459 : 144 : | comp_maskl[4][(t1 >> 21) & 0x7f]
460 : 144 : | comp_maskl[5][(t1 >> 14) & 0x7f]
461 : 144 : | comp_maskl[6][(t1 >> 7) & 0x7f]
462 : 144 : | comp_maskl[7][t1 & 0x7f];
463 : :
464 : 144 : de_keysr[15 - round] =
465 : 144 : en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f]
466 : 144 : | comp_maskr[1][(t0 >> 14) & 0x7f]
467 : 144 : | comp_maskr[2][(t0 >> 7) & 0x7f]
468 : 144 : | comp_maskr[3][t0 & 0x7f]
469 : 144 : | comp_maskr[4][(t1 >> 21) & 0x7f]
470 : 144 : | comp_maskr[5][(t1 >> 14) & 0x7f]
471 : 144 : | comp_maskr[6][(t1 >> 7) & 0x7f]
472 : 144 : | comp_maskr[7][t1 & 0x7f];
473 : : }
474 : 9 : return 0;
475 : : }
476 : :
477 : : static int
478 : 13 : do_des(uint32 l_in, uint32 r_in, uint32 *l_out, uint32 *r_out, int count)
479 : : {
480 : : /*
481 : : * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format.
482 : : */
483 : : uint32 l,
484 : : r,
485 : : *kl,
486 : : *kr,
487 : : *kl1,
488 : : *kr1;
489 : : uint32 f,
490 : : r48l,
491 : : r48r;
492 : : int round;
493 : :
494 [ + + ]: 13 : if (count == 0)
495 : 2 : return 1;
496 [ + - ]: 11 : else if (count > 0)
497 : : {
498 : : /*
499 : : * Encrypting
500 : : */
501 : 11 : kl1 = en_keysl;
502 : 11 : kr1 = en_keysr;
503 : : }
504 : : else
505 : : {
506 : : /*
507 : : * Decrypting
508 : : */
509 : 0 : count = -count;
510 : 0 : kl1 = de_keysl;
511 : 0 : kr1 = de_keysr;
512 : : }
513 : :
514 : : /*
515 : : * Do initial permutation (IP).
516 : : */
517 : 11 : l = ip_maskl[0][l_in >> 24]
518 : 11 : | ip_maskl[1][(l_in >> 16) & 0xff]
519 : 11 : | ip_maskl[2][(l_in >> 8) & 0xff]
520 : 11 : | ip_maskl[3][l_in & 0xff]
521 : 11 : | ip_maskl[4][r_in >> 24]
522 : 11 : | ip_maskl[5][(r_in >> 16) & 0xff]
523 : 11 : | ip_maskl[6][(r_in >> 8) & 0xff]
524 : 11 : | ip_maskl[7][r_in & 0xff];
525 : 11 : r = ip_maskr[0][l_in >> 24]
526 : 11 : | ip_maskr[1][(l_in >> 16) & 0xff]
527 : 11 : | ip_maskr[2][(l_in >> 8) & 0xff]
528 : 11 : | ip_maskr[3][l_in & 0xff]
529 : 11 : | ip_maskr[4][r_in >> 24]
530 : 11 : | ip_maskr[5][(r_in >> 16) & 0xff]
531 : 11 : | ip_maskr[6][(r_in >> 8) & 0xff]
532 : 11 : | ip_maskr[7][r_in & 0xff];
533 : :
534 [ + + ]: 4290 : while (count--)
535 : : {
536 [ - + ]: 4279 : CHECK_FOR_INTERRUPTS();
537 : :
538 : : /*
539 : : * Do each round.
540 : : */
541 : 4279 : kl = kl1;
542 : 4279 : kr = kr1;
543 : 4279 : round = 16;
544 [ + + ]: 72743 : while (round--)
545 : : {
546 : : /*
547 : : * Expand R to 48 bits (simulate the E-box).
548 : : */
549 : 68464 : r48l = ((r & 0x00000001) << 23)
550 : 68464 : | ((r & 0xf8000000) >> 9)
551 : 68464 : | ((r & 0x1f800000) >> 11)
552 : 68464 : | ((r & 0x01f80000) >> 13)
553 : 68464 : | ((r & 0x001f8000) >> 15);
554 : :
555 : 68464 : r48r = ((r & 0x0001f800) << 7)
556 : 68464 : | ((r & 0x00001f80) << 5)
557 : 68464 : | ((r & 0x000001f8) << 3)
558 : 68464 : | ((r & 0x0000001f) << 1)
559 : 68464 : | ((r & 0x80000000) >> 31);
560 : :
561 : : /*
562 : : * Do salting for crypt() and friends, and XOR with the permuted
563 : : * key.
564 : : */
565 : 68464 : f = (r48l ^ r48r) & saltbits;
566 : 68464 : r48l ^= f ^ *kl++;
567 : 68464 : r48r ^= f ^ *kr++;
568 : :
569 : : /*
570 : : * Do sbox lookups (which shrink it back to 32 bits) and do the
571 : : * pbox permutation at the same time.
572 : : */
573 : 68464 : f = psbox[0][m_sbox[0][r48l >> 12]]
574 : 68464 : | psbox[1][m_sbox[1][r48l & 0xfff]]
575 : 68464 : | psbox[2][m_sbox[2][r48r >> 12]]
576 : 68464 : | psbox[3][m_sbox[3][r48r & 0xfff]];
577 : :
578 : : /*
579 : : * Now that we've permuted things, complete f().
580 : : */
581 : 68464 : f ^= l;
582 : 68464 : l = r;
583 : 68464 : r = f;
584 : : }
585 : 4279 : r = l;
586 : 4279 : l = f;
587 : : }
588 : :
589 : : /*
590 : : * Do final permutation (inverse of IP).
591 : : */
592 : 11 : *l_out = fp_maskl[0][l >> 24]
593 : 11 : | fp_maskl[1][(l >> 16) & 0xff]
594 : 11 : | fp_maskl[2][(l >> 8) & 0xff]
595 : 11 : | fp_maskl[3][l & 0xff]
596 : 11 : | fp_maskl[4][r >> 24]
597 : 11 : | fp_maskl[5][(r >> 16) & 0xff]
598 : 11 : | fp_maskl[6][(r >> 8) & 0xff]
599 : 11 : | fp_maskl[7][r & 0xff];
600 : 11 : *r_out = fp_maskr[0][l >> 24]
601 : 11 : | fp_maskr[1][(l >> 16) & 0xff]
602 : 11 : | fp_maskr[2][(l >> 8) & 0xff]
603 : 11 : | fp_maskr[3][l & 0xff]
604 : 11 : | fp_maskr[4][r >> 24]
605 : 11 : | fp_maskr[5][(r >> 16) & 0xff]
606 : 11 : | fp_maskr[6][(r >> 8) & 0xff]
607 : 11 : | fp_maskr[7][r & 0xff];
608 : 11 : return 0;
609 : : }
610 : :
611 : : static int
612 : 1 : des_cipher(const uint32 *in, uint32 *out, long salt, int count)
613 : : {
614 : : uint32 l_out,
615 : : r_out,
616 : : rawl,
617 : : rawr;
618 : : int retval;
619 : :
620 [ - + ]: 1 : if (!des_initialised)
621 : 0 : des_init();
622 : :
623 : 1 : setup_salt(salt);
624 : :
625 : 1 : rawl = pg_ntoh32(in[0]);
626 : 1 : rawr = pg_ntoh32(in[1]);
627 : :
628 : 1 : retval = do_des(rawl, rawr, &l_out, &r_out, count);
629 [ - + ]: 1 : if (retval)
630 : 0 : return retval;
631 : :
632 : 1 : out[0] = pg_hton32(l_out);
633 : 1 : out[1] = pg_hton32(r_out);
634 : :
635 : 1 : return retval;
636 : : }
637 : :
638 : : char *
639 : 14 : px_crypt_des(const char *key, const char *setting)
640 : : {
641 : : int i;
642 : : uint32 count,
643 : : salt,
644 : : l,
645 : : r0,
646 : : r1;
647 : : union
648 : : {
649 : : uint8 bytes[8];
650 : : uint32 ints[2];
651 : : } keybuf;
652 : : char *p;
653 : : static char output[21];
654 : :
655 [ + + ]: 14 : if (!des_initialised)
656 : 2 : des_init();
657 : :
658 : :
659 : : /*
660 : : * Copy the key, shifting each character up by one bit and padding with
661 : : * zeros.
662 : : */
663 [ + + ]: 126 : for (size_t q = 0; q < lengthof(keybuf.bytes); q++)
664 : : {
665 : 112 : keybuf.bytes[q] = *key << 1;
666 [ + + ]: 112 : if (*key != '\0')
667 : 84 : key++;
668 : : }
669 [ - + ]: 14 : if (des_setkey(keybuf.ints))
670 : 0 : return NULL;
671 : :
672 : : #ifndef DISABLE_XDES
673 [ + + ]: 14 : if (*setting == _PASSWORD_EFMT1)
674 : : {
675 : : /*
676 : : * "new"-style: setting must be a 9-character (underscore, then 4
677 : : * bytes of count, then 4 bytes of salt) string. See CRYPT(3) under
678 : : * the "Extended crypt" heading for further details.
679 : : *
680 : : * Unlimited characters of the input key are used. This is known as
681 : : * the "Extended crypt" DES method.
682 : : *
683 : : */
684 [ + + ]: 9 : if (strlen(setting) < 9)
685 [ + - ]: 1 : ereport(ERROR,
686 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
687 : : errmsg("invalid salt")));
688 : :
689 [ + + ]: 40 : for (i = 1, count = 0L; i < 5; i++)
690 : 32 : count |= ascii_to_bin(setting[i]) << (i - 1) * 6;
691 : :
692 [ + + ]: 40 : for (i = 5, salt = 0L; i < 9; i++)
693 : 32 : salt |= ascii_to_bin(setting[i]) << (i - 5) * 6;
694 : :
695 [ + + ]: 9 : while (*key)
696 : : {
697 : : /*
698 : : * Encrypt the key with itself.
699 : : */
700 [ - + ]: 1 : if (des_cipher(keybuf.ints, keybuf.ints, 0L, 1))
701 : 0 : return NULL;
702 : :
703 : : /*
704 : : * And XOR with the next 8 characters of the key.
705 : : */
706 [ + + + - ]: 9 : for (size_t q = 0; q < lengthof(keybuf.bytes) && *key; q++)
707 : 8 : keybuf.bytes[q] ^= *key++ << 1;
708 : :
709 [ - + ]: 1 : if (des_setkey(keybuf.ints))
710 : 0 : return NULL;
711 : : }
712 : 8 : strlcpy(output, setting, 10);
713 : :
714 : : /*
715 : : * Double check that we weren't given a short setting. If we were, the
716 : : * above code will probably have created weird values for count and
717 : : * salt, but we don't really care. Just make sure the output string
718 : : * doesn't have an extra NUL in it.
719 : : */
720 : 8 : p = output + strlen(output);
721 : : }
722 : : else
723 : : #endif /* !DISABLE_XDES */
724 : : {
725 : : /*
726 : : * "old"-style: setting - 2 bytes of salt key - only up to the first 8
727 : : * characters of the input key are used.
728 : : */
729 : 5 : count = 25;
730 : :
731 [ + + ]: 5 : if (strlen(setting) < 2)
732 [ + - ]: 1 : ereport(ERROR,
733 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
734 : : errmsg("invalid salt")));
735 : :
736 : 4 : salt = (ascii_to_bin(setting[1]) << 6)
737 : 4 : | ascii_to_bin(setting[0]);
738 : :
739 : 4 : output[0] = setting[0];
740 : :
741 : : /*
742 : : * If the encrypted password that the salt was extracted from is only
743 : : * 1 character long, the salt will be corrupted. We need to ensure
744 : : * that the output string doesn't have an extra NUL in it!
745 : : */
746 [ + - ]: 4 : output[1] = setting[1] ? setting[1] : output[0];
747 : :
748 : 4 : p = output + 2;
749 : : }
750 : 12 : setup_salt(salt);
751 : :
752 : : /*
753 : : * Do it.
754 : : */
755 [ + + ]: 12 : if (do_des(0L, 0L, &r0, &r1, count))
756 : 2 : return NULL;
757 : :
758 : : /*
759 : : * Now encode the result...
760 : : */
761 : 10 : l = (r0 >> 8);
762 : 10 : *p++ = _crypt_a64[(l >> 18) & 0x3f];
763 : 10 : *p++ = _crypt_a64[(l >> 12) & 0x3f];
764 : 10 : *p++ = _crypt_a64[(l >> 6) & 0x3f];
765 : 10 : *p++ = _crypt_a64[l & 0x3f];
766 : :
767 : 10 : l = (r0 << 16) | ((r1 >> 16) & 0xffff);
768 : 10 : *p++ = _crypt_a64[(l >> 18) & 0x3f];
769 : 10 : *p++ = _crypt_a64[(l >> 12) & 0x3f];
770 : 10 : *p++ = _crypt_a64[(l >> 6) & 0x3f];
771 : 10 : *p++ = _crypt_a64[l & 0x3f];
772 : :
773 : 10 : l = r1 << 2;
774 : 10 : *p++ = _crypt_a64[(l >> 12) & 0x3f];
775 : 10 : *p++ = _crypt_a64[(l >> 6) & 0x3f];
776 : 10 : *p++ = _crypt_a64[l & 0x3f];
777 : 10 : *p = 0;
778 : :
779 : 10 : return output;
780 : : }
|