Debian Codegraph @

lib/crypt-des.c

raw
1 /*
2 * FreeSec: libcrypt for NetBSD
3 *
4 * Copyright (c) 1994 David Burren
5 * All rights reserved.
6 *
7 * Adapted for FreeBSD-2.0 by Geoffrey M. Rehmet
8 * this file should now *only* export crypt(), in order to make
9 * binaries of libcrypt exportable from the USA
10 *
11 * Adapted for FreeBSD-4.0 by Mark R V Murray
12 * this file should now *only* export crypt_des(), in order to make
13 * a module that can be optionally included in libcrypt.
14 *
15 * Adapted for libxcrypt by Zack Weinberg, 2017
16 * see notes in des.c
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the author nor the names of other contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * This is an original implementation of the DES and the crypt(3) interfaces
43 * by David Burren <[email protected]>.
44 */
45
46 #include "crypt-port.h"
47 #include "alg-des.h"
48
49 #include <errno.h>
50
51 #if INCLUDE_descrypt || INCLUDE_bsdicrypt || INCLUDE_bigcrypt
52
53 #define DES_TRD_OUTPUT_LEN 14 /* SShhhhhhhhhhh0 */
54 #define DES_EXT_OUTPUT_LEN 21 /* _CCCCSSSShhhhhhhhhhh0 */
55 #define DES_BIG_OUTPUT_LEN ((16*11) + 2 + 1) /* SS (hhhhhhhhhhh){1,16} 0 */
56
57 #define DES_MAX_OUTPUT_LEN \
58 MAX (DES_TRD_OUTPUT_LEN, MAX (DES_EXT_OUTPUT_LEN, DES_BIG_OUTPUT_LEN))
59
60 static_assert (DES_MAX_OUTPUT_LEN <= CRYPT_OUTPUT_SIZE,
61 "CRYPT_OUTPUT_SIZE is too small for DES");
62
63 /* A des_buffer holds all of the sensitive intermediate data used by
64 crypt_des_*. */
65
66 struct des_buffer
67 {
68 struct des_ctx ctx;
69 uint8_t keybuf[8];
70 uint8_t pkbuf[8];
71 };
72
73 static_assert (sizeof (struct des_buffer) <= ALG_SPECIFIC_SIZE,
74 "ALG_SPECIFIC_SIZE is too small for DES");
75
76
77 static inline int
78 ascii_to_bin(char ch)
79 {
80 if (ch > 'z')
81 return -1;
82 if (ch >= 'a')
83 return ch - 'a' + 38;
84 if (ch > 'Z')
85 return -1;
86 if (ch >= 'A')
87 return ch - 'A' + 12;
88 if (ch > '9')
89 return -1;
90 if (ch >= '.')
91 return ch - '.';
92 return -1;
93 }
94
95 /* Generate an 11-character DES password hash into the buffer at
96 OUTPUT, and nul-terminate it. The salt and key have already been
97 set. The plaintext is 64 bits of zeroes, and the raw ciphertext is
98 written to cbuf[]. */
99 static void
100 des_gen_hash (struct des_ctx *ctx, uint32_t count, uint8_t *output,
101 uint8_t cbuf[8])
102 {
103 uint8_t plaintext[8];
104 memset (plaintext, 0, 8);
105 des_crypt_block (ctx, cbuf, plaintext, count, false);
106
107 /* Now encode the result. */
108 const uint8_t *sptr = cbuf;
109 const uint8_t *end = sptr + 8;
110 unsigned int c1, c2;
111
112 do
113 {
114 c1 = *sptr++;
115 *output++ = ascii64[c1 >> 2];
116 c1 = (c1 & 0x03) << 4;
117 if (end - sptr <= 0)
118 {
119 *output++ = ascii64[c1];
120 break;
121 }
122
123 c2 = *sptr++;
124 c1 |= c2 >> 4;
125 *output++ = ascii64[c1];
126 c1 = (c2 & 0x0f) << 2;
127 if (end - sptr <= 0)
128 {
129 *output++ = ascii64[c1];
130 break;
131 }
132
133 c2 = *sptr++;
134 c1 |= c2 >> 6;
135 *output++ = ascii64[c1];
136 *output++ = ascii64[c2 & 0x3f];
137 }
138 while (end - sptr > 0);
139 *output = '\0';
140 }
141 #endif
142
143 #if INCLUDE_descrypt
144 /* The original UNIX DES-based password hash, no extensions. */
145 void
146 crypt_descrypt_rn (const char *phrase, size_t ARG_UNUSED (phr_size),
147 const char *setting, size_t ARG_UNUSED (set_size),
148 uint8_t *output, size_t out_size,
149 void *scratch, size_t scr_size)
150 {
151 /* This shouldn't ever happen, but... */
152 if (out_size < DES_TRD_OUTPUT_LEN || scr_size < sizeof (struct des_buffer))
153 {
154 errno = ERANGE;
155 return;
156 }
157
158 struct des_buffer *buf = scratch;
159 struct des_ctx *ctx = &buf->ctx;
160 uint32_t salt = 0;
161 uint8_t *keybuf = buf->keybuf, *pkbuf = buf->pkbuf;
162 uint8_t *cp = output;
163 int i;
164
165 /* "old"-style: setting - 2 bytes of salt, phrase - up to 8 characters.
166 Note: ascii_to_bin maps all byte values outside the ascii64
167 alphabet to -1. Do not read past the end of the string. */
168 i = ascii_to_bin (setting[0]);
169 if (i < 0)
170 {
171 errno = EINVAL;
172 return;
173 }
174 salt = (unsigned int)i;
175 i = ascii_to_bin (setting[1]);
176 if (i < 0)
177 {
178 errno = EINVAL;
179 return;
180 }
181 salt |= ((unsigned int)i << 6);
182
183 /* Write the canonical form of the salt to the output buffer. We do
184 this instead of copying from the setting because the setting
185 might be catastrophically malformed (e.g. a 0- or 1-byte string;
186 this could plausibly happen if e.g. login(8) doesn't special-case
187 "*" or "!" in the password database). */
188 *cp++ = ascii64[salt & 0x3f];
189 *cp++ = ascii64[(salt >> 6) & 0x3f];
190
191 /* Copy the first 8 characters of the password into keybuf, shifting
192 each character up by 1 bit and padding on the right with zeroes. */
193 for (i = 0; i < 8; i++)
194 {
195 keybuf[i] = (uint8_t)(*phrase << 1);
196 if (*phrase)
197 phrase++;
198 }
199
200 des_set_key (ctx, keybuf);
201 des_set_salt (ctx, salt);
202 des_gen_hash (ctx, 25, cp, pkbuf);
203 }
204 #endif
205
206 #if INCLUDE_bigcrypt
207 /* This algorithm is algorithm 0 (default) shipped with the C2 secure
208 implementation of Digital UNIX.
209
210 Disclaimer: This work is not based on the source code to Digital
211 UNIX, nor am I (Andy Phillips) connected to Digital Equipment Corp,
212 in any way other than as a customer. This code is based on
213 published interfaces and reasonable guesswork.
214
215 Description: The cleartext is divided into blocks of 8 characters
216 or less. Each block is encrypted using the standard UNIX libc crypt
217 function. The result of the encryption for one block provides the
218 salt for the succeeding block. The output is simply the
219 concatenation of all the blocks. Up to 16 blocks are supported
220 (that is, the password can be no more than 128 characters long).
221
222 Andy Phillips <[email protected]> */
223 void
224 crypt_bigcrypt_rn (const char *phrase, size_t phr_size,
225 const char *setting, size_t set_size,
226 uint8_t *output, size_t out_size,
227 void *scratch, size_t scr_size)
228 {
229 /* descrypt and bigcrypt generate identical hashes when the phrase
230 contains no more than 8 characters. When the phrase is longer
231 than 8 characters, descrypt would truncate it, and bigcrypt would
232 generate a different, longer hash. Therefore, when we encounter
233 a phrase longer than 8 characters together with a short setting
234 string, the appropriate behavior depends on whether descrypt is
235 enabled. When it is, forward to descrypt. When it isn't, reject
236 the operation. */
237 if (phr_size > 8 && set_size <= 13)
238 {
239 #if INCLUDE_descrypt
240 crypt_descrypt_rn (phrase, phr_size, setting, set_size,
241 output, out_size, scratch, scr_size);
242 #else
243 errno = EINVAL;
244 #endif
245 return;
246 }
247
248 /* This shouldn't ever happen, but... */
249 if (out_size < DES_BIG_OUTPUT_LEN || scr_size < sizeof (struct des_buffer))
250 {
251 errno = ERANGE;
252 return;
253 }
254
255 struct des_buffer *buf = scratch;
256 struct des_ctx *ctx = &buf->ctx;
257 uint32_t salt = 0;
258 uint8_t *keybuf = buf->keybuf, *pkbuf = buf->pkbuf;
259 uint8_t *cp = output;
260 int i, seg;
261
262 /* The setting string is exactly the same as for a traditional DES
263 hash. */
264 i = ascii_to_bin (setting[0]);
265 if (i < 0)
266 {
267 errno = EINVAL;
268 return;
269 }
270 salt = (unsigned int)i;
271 i = ascii_to_bin (setting[1]);
272 if (i < 0)
273 {
274 errno = EINVAL;
275 return;
276 }
277 salt |= ((unsigned int)i << 6);
278
279 *cp++ = ascii64[salt & 0x3f];
280 *cp++ = ascii64[(salt >> 6) & 0x3f];
281
282 for (seg = 0; seg < 16; seg++)
283 {
284 /* Copy and shift each block as for the traditional DES. */
285 for (i = 0; i < 8; i++)
286 {
287 keybuf[i] = (uint8_t)(*phrase << 1);
288 if (*phrase)
289 phrase++;
290 }
291
292 des_set_key (ctx, keybuf);
293 des_set_salt (ctx, salt);
294 des_gen_hash (ctx, 25, cp, pkbuf);
295
296 if (*phrase == 0)
297 break;
298
299 /* change the salt (1st 2 chars of previous block) - this was found
300 by dowsing - no need to check for invalid characters here */
301 salt = (unsigned int)ascii_to_bin ((char)cp[0]);
302 salt |= (unsigned int)ascii_to_bin ((char)cp[1]) << 6;
303 cp += 11;
304 }
305 }
306 #endif
307
308 #if INCLUDE_bsdicrypt
309 /* crypt_rn() entry point for BSD-style extended DES hashes. These
310 permit long passwords and have more salt and a controllable iteration
311 count, but are still unacceptably weak by modern standards. */
312 void
313 crypt_bsdicrypt_rn (const char *phrase, size_t ARG_UNUSED (phr_size),
314 const char *setting, size_t set_size,
315 uint8_t *output, size_t out_size,
316 void *scratch, size_t scr_size)
317 {
318 /* This shouldn't ever happen, but... */
319 if (out_size < DES_EXT_OUTPUT_LEN || scr_size < sizeof (struct des_buffer))
320 {
321 errno = ERANGE;
322 return;
323 }
324
325 /* If this is true, this function shouldn't have been called.
326 Setting must be at least 9 bytes long, byte 10+ is ignored. */
327 if (*setting != '_' || set_size < 9)
328 {
329 errno = EINVAL;
330 return;
331 }
332
333 struct des_buffer *buf = scratch;
334 struct des_ctx *ctx = &buf->ctx;
335 uint32_t count = 0, salt = 0;
336 uint8_t *keybuf = buf->keybuf, *pkbuf = buf->pkbuf;
337 uint8_t *cp = output;
338 int i, x;
339
340 /* "new"-style DES hash:
341 setting - underscore, 4 bytes of count, 4 bytes of salt
342 phrase - unlimited characters
343 */
344 for (i = 1; i < 5; i++)
345 {
346 x = ascii_to_bin(setting[i]);
347 if (x < 0)
348 {
349 errno = EINVAL;
350 return;
351 }
352 count |= (unsigned int)x << ((i - 1) * 6);
353 }
354
355 for (i = 5; i < 9; i++)
356 {
357 x = ascii_to_bin(setting[i]);
358 if (x < 0)
359 {
360 errno = EINVAL;
361 return;
362 }
363 salt |= (unsigned int)x << ((i - 5) * 6);
364 }
365
366 memcpy (cp, setting, 9);
367 cp += 9;
368
369 /* Fold passwords longer than 8 bytes into a single DES key using a
370 procedure similar to a Merkle-Dåmgard hash construction. Each
371 block is shifted and padded, as for the traditional hash, then
372 XORed with the output of the previous round (IV all bits zero),
373 set as the DES key, and encrypted to produce the round output.
374 The salt is zero throughout this procedure. */
375 des_set_salt (ctx, 0);
376 memset (pkbuf, 0, 8);
377 for (;;)
378 {
379 for (i = 0; i < 8; i++)
380 {
381 keybuf[i] = (uint8_t)(pkbuf[i] ^ (*phrase << 1));
382 if (*phrase)
383 phrase++;
384 }
385 des_set_key (ctx, keybuf);
386 if (*phrase == 0)
387 break;
388 des_crypt_block (ctx, pkbuf, keybuf, 1, false);
389 }
390
391 /* Proceed as for the traditional DES hash. */
392 des_set_salt (ctx, salt);
393 des_gen_hash (ctx, count, cp, pkbuf);
394 }
395 #endif
396
397 #if INCLUDE_descrypt || INCLUDE_bigcrypt
398 void
399 gensalt_descrypt_rn (unsigned long count,
400 const uint8_t *rbytes, size_t nrbytes,
401 uint8_t *output, size_t output_size)
402 {
403 if (output_size < 3)
404 {
405 errno = ERANGE;
406 return;
407 }
408
409 if (nrbytes < 2 || count != 0)
410 {
411 errno = EINVAL;
412 return;
413 }
414
415 output[0] = ascii64[(unsigned int) rbytes[0] & 0x3f];
416 output[1] = ascii64[(unsigned int) rbytes[1] & 0x3f];
417 output[2] = '\0';
418 }
419 #if INCLUDE_bigcrypt
420 void
421 gensalt_bigcrypt_rn (unsigned long count,
422 const uint8_t *rbytes, size_t nrbytes,
423 uint8_t *output, size_t output_size)
424 {
425 #if !INCLUDE_descrypt
426 /* We need descrypt + 12 bytes. */
427 if (output_size < 3 + 12)
428 {
429 errno = ERANGE;
430 return;
431 }
432 #endif
433
434 /* Same setting string as descrypt, but... */
435 gensalt_descrypt_rn (count, rbytes, nrbytes, output, output_size);
436
437 #if !INCLUDE_descrypt
438 /* ... add 12 trailing dummy characters, which makes the string too
439 long to be a descrypt setting, thus bigcrypt will be used. */
440 strcpy_or_abort (output + 2, output_size - 2, "............");
441 #endif
442 }
443 #endif
444 #endif
445
446 #if INCLUDE_bsdicrypt
447 void
448 gensalt_bsdicrypt_rn (unsigned long count,
449 const uint8_t *rbytes, size_t nrbytes,
450 uint8_t *output, size_t output_size)
451 {
452 if (output_size < 1 + 4 + 4 + 1)
453 {
454 errno = ERANGE;
455 return;
456 }
457 if (nrbytes < 3)
458 {
459 errno = EINVAL;
460 return;
461 }
462
463 if (count == 0)
464 count = 725;
465 if (count > 0xffffff)
466 count = 0xffffff;
467
468 /* Even iteration counts make it easier to detect weak DES keys from a look
469 at the hash, so they should be avoided. */
470 count |= 1;
471
472 unsigned long value =
473 ((unsigned long) (unsigned char) rbytes[0] << 0) |
474 ((unsigned long) (unsigned char) rbytes[1] << 8) |
475 ((unsigned long) (unsigned char) rbytes[2] << 16);
476
477 output[0] = '_';
478
479 output[1] = ascii64[(count >> 0) & 0x3f];
480 output[2] = ascii64[(count >> 6) & 0x3f];
481 output[3] = ascii64[(count >> 12) & 0x3f];
482 output[4] = ascii64[(count >> 18) & 0x3f];
483
484 output[5] = ascii64[(value >> 0) & 0x3f];
485 output[6] = ascii64[(value >> 6) & 0x3f];
486 output[7] = ascii64[(value >> 12) & 0x3f];
487 output[8] = ascii64[(value >> 18) & 0x3f];
488
489 output[9] = '\0';
490 }
491 #endif