Debian Codegraph @

ext/bcrypt/wrapper.c

raw
1 /*
2 * Written by Solar Designer <solar at openwall.com> in 2000-2014.
3 * No copyright is claimed, and the software is hereby placed in the public
4 * domain. In case this attempt to disclaim copyright and place the software
5 * in the public domain is deemed null and void, then the software is
6 * Copyright (c) 2000-2014 Solar Designer and it is hereby released to the
7 * general public under the following terms:
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted.
11 *
12 * There's ABSOLUTELY NO WARRANTY, express or implied.
13 *
14 * See crypt_blowfish.c for more information.
15 */
16
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include <errno.h>
21 #ifndef __set_errno
22 #define __set_errno(val) errno = (val)
23 #endif
24
25 #ifdef TEST
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <time.h>
30 #include <sys/time.h>
31 #include <sys/times.h>
32 #ifdef TEST_THREADS
33 #include <pthread.h>
34 #endif
35 #endif
36
37 #define CRYPT_OUTPUT_SIZE (7 + 22 + 31 + 1)
38 #define CRYPT_GENSALT_OUTPUT_SIZE (7 + 22 + 1)
39
40 #if defined(__GLIBC__) && defined(_LIBC)
41 #define __SKIP_GNU
42 #endif
43 #include "ow-crypt.h"
44
45 #include "crypt_blowfish.h"
46 #include "crypt_gensalt.h"
47
48 #if defined(__GLIBC__) && defined(_LIBC)
49 /* crypt.h from glibc-crypt-2.1 will define struct crypt_data for us */
50 #include "crypt.h"
51 extern char *__md5_crypt_r(const char *key, const char *salt,
52 char *buffer, int buflen);
53 /* crypt-entry.c needs to be patched to define __des_crypt_r rather than
54 * __crypt_r, and not define crypt_r and crypt at all */
55 extern char *__des_crypt_r(const char *key, const char *salt,
56 struct crypt_data *data);
57 extern struct crypt_data _ufc_foobar;
58 #endif
59
60 static int _crypt_data_alloc(void **data, int *size, int need)
61 {
62 void *updated;
63
64 if (*data && *size >= need) return 0;
65
66 updated = realloc(*data, need);
67
68 if (!updated) {
69 #ifndef __GLIBC__
70 /* realloc(3) on glibc sets errno, so we don't need to bother */
71 __set_errno(ENOMEM);
72 #endif
73 return -1;
74 }
75
76 #if defined(__GLIBC__) && defined(_LIBC)
77 if (need >= sizeof(struct crypt_data))
78 ((struct crypt_data *)updated)->initialized = 0;
79 #endif
80
81 *data = updated;
82 *size = need;
83
84 return 0;
85 }
86
87 static char *_crypt_retval_magic(char *retval, const char *setting,
88 char *output, int size)
89 {
90 if (retval)
91 return retval;
92
93 if (_crypt_output_magic(setting, output, size))
94 return NULL; /* shouldn't happen */
95
96 return output;
97 }
98
99 #if defined(__GLIBC__) && defined(_LIBC)
100 /*
101 * Applications may re-use the same instance of struct crypt_data without
102 * resetting the initialized field in order to let crypt_r() skip some of
103 * its initialization code. Thus, it is important that our multiple hashing
104 * algorithms either don't conflict with each other in their use of the
105 * data area or reset the initialized field themselves whenever required.
106 * Currently, the hashing algorithms simply have no conflicts: the first
107 * field of struct crypt_data is the 128-byte large DES key schedule which
108 * __des_crypt_r() calculates each time it is called while the two other
109 * hashing algorithms use less than 128 bytes of the data area.
110 */
111
112 char *__crypt_rn(__const char *key, __const char *setting,
113 void *data, int size)
114 {
115 if (setting[0] == '$' && setting[1] == '2')
116 return _crypt_blowfish_rn(key, setting, (char *)data, size);
117 if (setting[0] == '$' && setting[1] == '1')
118 return __md5_crypt_r(key, setting, (char *)data, size);
119 if (setting[0] == '$' || setting[0] == '_') {
120 __set_errno(EINVAL);
121 return NULL;
122 }
123 if (size >= sizeof(struct crypt_data))
124 return __des_crypt_r(key, setting, (struct crypt_data *)data);
125 __set_errno(ERANGE);
126 return NULL;
127 }
128
129 char *__crypt_ra(__const char *key, __const char *setting,
130 void **data, int *size)
131 {
132 if (setting[0] == '$' && setting[1] == '2') {
133 if (_crypt_data_alloc(data, size, CRYPT_OUTPUT_SIZE))
134 return NULL;
135 return _crypt_blowfish_rn(key, setting, (char *)*data, *size);
136 }
137 if (setting[0] == '$' && setting[1] == '1') {
138 if (_crypt_data_alloc(data, size, CRYPT_OUTPUT_SIZE))
139 return NULL;
140 return __md5_crypt_r(key, setting, (char *)*data, *size);
141 }
142 if (setting[0] == '$' || setting[0] == '_') {
143 __set_errno(EINVAL);
144 return NULL;
145 }
146 if (_crypt_data_alloc(data, size, sizeof(struct crypt_data)))
147 return NULL;
148 return __des_crypt_r(key, setting, (struct crypt_data *)*data);
149 }
150
151 char *__crypt_r(__const char *key, __const char *setting,
152 struct crypt_data *data)
153 {
154 return _crypt_retval_magic(
155 __crypt_rn(key, setting, data, sizeof(*data)),
156 setting, (char *)data, sizeof(*data));
157 }
158
159 char *__crypt(__const char *key, __const char *setting)
160 {
161 return _crypt_retval_magic(
162 __crypt_rn(key, setting, &_ufc_foobar, sizeof(_ufc_foobar)),
163 setting, (char *)&_ufc_foobar, sizeof(_ufc_foobar));
164 }
165 #else
166 char *crypt_rn(const char *key, const char *setting, void *data, int size)
167 {
168 return _crypt_blowfish_rn(key, setting, (char *)data, size);
169 }
170
171 char *crypt_ra(const char *key, const char *setting,
172 void **data, int *size)
173 {
174 if (_crypt_data_alloc(data, size, CRYPT_OUTPUT_SIZE))
175 return NULL;
176 return _crypt_blowfish_rn(key, setting, (char *)*data, *size);
177 }
178
179 char *crypt_r(const char *key, const char *setting, void *data)
180 {
181 return _crypt_retval_magic(
182 crypt_rn(key, setting, data, CRYPT_OUTPUT_SIZE),
183 setting, (char *)data, CRYPT_OUTPUT_SIZE);
184 }
185
186 char *crypt(const char *key, const char *setting)
187 {
188 static char output[CRYPT_OUTPUT_SIZE];
189
190 return _crypt_retval_magic(
191 crypt_rn(key, setting, output, sizeof(output)),
192 setting, output, sizeof(output));
193 }
194
195 #define __crypt_gensalt_rn crypt_gensalt_rn
196 #define __crypt_gensalt_ra crypt_gensalt_ra
197 #define __crypt_gensalt crypt_gensalt
198 #endif
199
200 char *__crypt_gensalt_rn(const char *prefix, unsigned long count,
201 const char *input, int size, char *output, int output_size)
202 {
203 char *(*use)(const char *_prefix, unsigned long _count,
204 const char *_input, int _size,
205 char *_output, int _output_size);
206
207 /* This may be supported on some platforms in the future */
208 if (!input) {
209 __set_errno(EINVAL);
210 return NULL;
211 }
212
213 if (!strncmp(prefix, "$2a$", 4) || !strncmp(prefix, "$2b$", 4) ||
214 !strncmp(prefix, "$2y$", 4))
215 use = _crypt_gensalt_blowfish_rn;
216 else
217 if (!strncmp(prefix, "$1$", 3))
218 use = _crypt_gensalt_md5_rn;
219 else
220 if (prefix[0] == '_')
221 use = _crypt_gensalt_extended_rn;
222 else
223 if (!prefix[0] ||
224 (prefix[0] && prefix[1] &&
225 memchr(_crypt_itoa64, prefix[0], 64) &&
226 memchr(_crypt_itoa64, prefix[1], 64)))
227 use = _crypt_gensalt_traditional_rn;
228 else {
229 __set_errno(EINVAL);
230 return NULL;
231 }
232
233 return use(prefix, count, input, size, output, output_size);
234 }
235
236 char *__crypt_gensalt_ra(const char *prefix, unsigned long count,
237 const char *input, int size)
238 {
239 char output[CRYPT_GENSALT_OUTPUT_SIZE];
240 char *retval;
241
242 retval = __crypt_gensalt_rn(prefix, count,
243 input, size, output, sizeof(output));
244
245 if (retval) {
246 retval = strdup(retval);
247 #ifndef __GLIBC__
248 /* strdup(3) on glibc sets errno, so we don't need to bother */
249 if (!retval)
250 __set_errno(ENOMEM);
251 #endif
252 }
253
254 return retval;
255 }
256
257 char *__crypt_gensalt(const char *prefix, unsigned long count,
258 const char *input, int size)
259 {
260 static char output[CRYPT_GENSALT_OUTPUT_SIZE];
261
262 return __crypt_gensalt_rn(prefix, count,
263 input, size, output, sizeof(output));
264 }
265
266 #if defined(__GLIBC__) && defined(_LIBC)
267 weak_alias(__crypt_rn, crypt_rn)
268 weak_alias(__crypt_ra, crypt_ra)
269 weak_alias(__crypt_r, crypt_r)
270 weak_alias(__crypt, crypt)
271 weak_alias(__crypt_gensalt_rn, crypt_gensalt_rn)
272 weak_alias(__crypt_gensalt_ra, crypt_gensalt_ra)
273 weak_alias(__crypt_gensalt, crypt_gensalt)
274 weak_alias(crypt, fcrypt)
275 #endif
276
277 #ifdef TEST
278 static const char *tests[][3] = {
279 {"$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW",
280 "U*U"},
281 {"$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK",
282 "U*U*"},
283 {"$2a$05$XXXXXXXXXXXXXXXXXXXXXOAcXxm9kjPGEMsLznoKqmqw7tc8WCx4a",
284 "U*U*U"},
285 {"$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui",
286 "0123456789abcdefghijklmnopqrstuvwxyz"
287 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
288 "chars after 72 are ignored"},
289 {"$2x$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
290 "\xa3"},
291 {"$2x$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
292 "\xff\xff\xa3"},
293 {"$2y$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
294 "\xff\xff\xa3"},
295 {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.nqd1wy.pTMdcvrRWxyiGL2eMz.2a85.",
296 "\xff\xff\xa3"},
297 {"$2b$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
298 "\xff\xff\xa3"},
299 {"$2y$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
300 "\xa3"},
301 {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
302 "\xa3"},
303 {"$2b$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
304 "\xa3"},
305 {"$2x$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi",
306 "1\xa3" "345"},
307 {"$2x$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi",
308 "\xff\xa3" "345"},
309 {"$2x$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi",
310 "\xff\xa3" "34" "\xff\xff\xff\xa3" "345"},
311 {"$2y$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi",
312 "\xff\xa3" "34" "\xff\xff\xff\xa3" "345"},
313 {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.ZC1JEJ8Z4gPfpe1JOr/oyPXTWl9EFd.",
314 "\xff\xa3" "34" "\xff\xff\xff\xa3" "345"},
315 {"$2y$05$/OK.fbVrR/bpIqNJ5ianF.nRht2l/HRhr6zmCp9vYUvvsqynflf9e",
316 "\xff\xa3" "345"},
317 {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.nRht2l/HRhr6zmCp9vYUvvsqynflf9e",
318 "\xff\xa3" "345"},
319 {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.6IflQkJytoRVc1yuaNtHfiuq.FRlSIS",
320 "\xa3" "ab"},
321 {"$2x$05$/OK.fbVrR/bpIqNJ5ianF.6IflQkJytoRVc1yuaNtHfiuq.FRlSIS",
322 "\xa3" "ab"},
323 {"$2y$05$/OK.fbVrR/bpIqNJ5ianF.6IflQkJytoRVc1yuaNtHfiuq.FRlSIS",
324 "\xa3" "ab"},
325 {"$2x$05$6bNw2HLQYeqHYyBfLMsv/OiwqTymGIGzFsA4hOTWebfehXHNprcAS",
326 "\xd1\x91"},
327 {"$2x$05$6bNw2HLQYeqHYyBfLMsv/O9LIGgn8OMzuDoHfof8AQimSGfcSWxnS",
328 "\xd0\xc1\xd2\xcf\xcc\xd8"},
329 {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.swQOIzjOiJ9GHEPuhEkvqrUyvWhEMx6",
330 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
331 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
332 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
333 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
334 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
335 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
336 "chars after 72 are ignored as usual"},
337 {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.R9xrDjiycxMbQE2bp.vgqlYpW5wx2yy",
338 "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
339 "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
340 "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
341 "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
342 "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
343 "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"},
344 {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.9tQZzcJfm3uj2NvJ/n5xkhpqLrMpWCe",
345 "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
346 "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
347 "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
348 "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
349 "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
350 "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"},
351 {"$2a$05$CCCCCCCCCCCCCCCCCCCCC.7uG0VCzI2bS7j6ymqJi9CdcdxiRTWNy",
352 ""},
353 {"*0", "", "$2a$03$CCCCCCCCCCCCCCCCCCCCC."},
354 {"*0", "", "$2a$32$CCCCCCCCCCCCCCCCCCCCC."},
355 {"*0", "", "$2c$05$CCCCCCCCCCCCCCCCCCCCC."},
356 {"*0", "", "$2z$05$CCCCCCCCCCCCCCCCCCCCC."},
357 {"*0", "", "$2`$05$CCCCCCCCCCCCCCCCCCCCC."},
358 {"*0", "", "$2{$05$CCCCCCCCCCCCCCCCCCCCC."},
359 {"*1", "", "*0"},
360 {NULL}
361 };
362
363 #define which tests[0]
364
365 static volatile sig_atomic_t running;
366
367 static void handle_timer(int signum)
368 {
369 (void) signum;
370 running = 0;
371 }
372
373 static void *run(void *arg)
374 {
375 unsigned long count = 0;
376 int i = 0;
377 void *data = NULL;
378 int size = 0x12345678;
379
380 do {
381 const char *hash = tests[i][0];
382 const char *key = tests[i][1];
383 const char *setting = tests[i][2];
384
385 if (!tests[++i][0])
386 i = 0;
387
388 if (setting && strlen(hash) < 30) /* not for benchmark */
389 continue;
390
391 if (strcmp(crypt_ra(key, hash, &data, &size), hash)) {
392 printf("%d: FAILED (crypt_ra/%d/%lu)\n",
393 (int)((char *)arg - (char *)0), i, count);
394 free(data);
395 return NULL;
396 }
397 count++;
398 } while (running);
399
400 free(data);
401 return count + (char *)0;
402 }
403
404 int main(void)
405 {
406 struct itimerval it;
407 struct tms buf;
408 clock_t clk_tck, start_real, start_virtual, end_real, end_virtual;
409 unsigned long count;
410 void *data;
411 int size;
412 char *setting1, *setting2;
413 int i;
414 #ifdef TEST_THREADS
415 pthread_t t[TEST_THREADS];
416 void *t_retval;
417 #endif
418
419 data = NULL;
420 size = 0x12345678;
421
422 for (i = 0; tests[i][0]; i++) {
423 const char *hash = tests[i][0];
424 const char *key = tests[i][1];
425 const char *setting = tests[i][2];
426 const char *p;
427 int ok = !setting || strlen(hash) >= 30;
428 int o_size;
429 char s_buf[30], o_buf[61];
430 if (!setting) {
431 memcpy(s_buf, hash, sizeof(s_buf) - 1);
432 s_buf[sizeof(s_buf) - 1] = 0;
433 setting = s_buf;
434 }
435
436 __set_errno(0);
437 p = crypt(key, setting);
438 if ((!ok && !errno) || strcmp(p, hash)) {
439 printf("FAILED (crypt/%d)\n", i);
440 return 1;
441 }
442
443 if (ok && strcmp(crypt(key, hash), hash)) {
444 printf("FAILED (crypt/%d)\n", i);
445 return 1;
446 }
447
448 for (o_size = -1; o_size <= (int)sizeof(o_buf); o_size++) {
449 int ok_n = ok && o_size == (int)sizeof(o_buf);
450 const char *x = "abc";
451 strcpy(o_buf, x);
452 if (o_size >= 3) {
453 x = "*0";
454 if (setting[0] == '*' && setting[1] == '0')
455 x = "*1";
456 }
457 __set_errno(0);
458 p = crypt_rn(key, setting, o_buf, o_size);
459 if ((ok_n && (!p || strcmp(p, hash))) ||
460 (!ok_n && (!errno || p || strcmp(o_buf, x)))) {
461 printf("FAILED (crypt_rn/%d)\n", i);
462 return 1;
463 }
464 }
465
466 __set_errno(0);
467 p = crypt_ra(key, setting, &data, &size);
468 if ((ok && (!p || strcmp(p, hash))) ||
469 (!ok && (!errno || p || strcmp((char *)data, hash)))) {
470 printf("FAILED (crypt_ra/%d)\n", i);
471 return 1;
472 }
473 }
474
475 setting1 = crypt_gensalt(which[0], 12, data, size);
476 if (!setting1 || strncmp(setting1, "$2a$12$", 7)) {
477 puts("FAILED (crypt_gensalt)\n");
478 return 1;
479 }
480
481 setting2 = crypt_gensalt_ra(setting1, 12, data, size);
482 if (strcmp(setting1, setting2)) {
483 puts("FAILED (crypt_gensalt_ra/1)\n");
484 return 1;
485 }
486
487 (*(char *)data)++;
488 setting1 = crypt_gensalt_ra(setting2, 12, data, size);
489 if (!strcmp(setting1, setting2)) {
490 puts("FAILED (crypt_gensalt_ra/2)\n");
491 return 1;
492 }
493
494 free(setting1);
495 free(setting2);
496 free(data);
497
498 #if defined(_SC_CLK_TCK) || !defined(CLK_TCK)
499 clk_tck = sysconf(_SC_CLK_TCK);
500 #else
501 clk_tck = CLK_TCK;
502 #endif
503
504 running = 1;
505 signal(SIGALRM, handle_timer);
506
507 memset(&it, 0, sizeof(it));
508 it.it_value.tv_sec = 5;
509 setitimer(ITIMER_REAL, &it, NULL);
510
511 start_real = times(&buf);
512 start_virtual = buf.tms_utime + buf.tms_stime;
513
514 count = (char *)run((char *)0) - (char *)0;
515
516 end_real = times(&buf);
517 end_virtual = buf.tms_utime + buf.tms_stime;
518 if (end_virtual == start_virtual) end_virtual++;
519
520 printf("%.1f c/s real, %.1f c/s virtual\n",
521 (float)count * clk_tck / (end_real - start_real),
522 (float)count * clk_tck / (end_virtual - start_virtual));
523
524 #ifdef TEST_THREADS
525 running = 1;
526 it.it_value.tv_sec = 60;
527 setitimer(ITIMER_REAL, &it, NULL);
528 start_real = times(&buf);
529
530 for (i = 0; i < TEST_THREADS; i++)
531 if (pthread_create(&t[i], NULL, run, i + (char *)0)) {
532 perror("pthread_create");
533 return 1;
534 }
535
536 for (i = 0; i < TEST_THREADS; i++) {
537 if (pthread_join(t[i], &t_retval)) {
538 perror("pthread_join");
539 continue;
540 }
541 if (!t_retval) continue;
542 count = (char *)t_retval - (char *)0;
543 end_real = times(&buf);
544 printf("%d: %.1f c/s real\n", i,
545 (float)count * clk_tck / (end_real - start_real));
546 }
547 #endif
548
549 return 0;
550 }
551 #endif