2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #if defined OPENSSL_NO_MD5 || defined CHARSET_EBCDIC
11 # define NO_MD5CRYPT_1
14 #if defined OPENSSL_NO_SHA || defined CHARSET_EBCDIC
18 #if !defined(OPENSSL_NO_DES) || !defined(NO_MD5CRYPT_1) || !defined(NO_SHACRYPT)
24 # include <openssl/bio.h>
25 # include <openssl/err.h>
26 # include <openssl/evp.h>
27 # include <openssl/rand.h>
28 # ifndef OPENSSL_NO_DES
29 # include <openssl/des.h>
31 # ifndef NO_MD5CRYPT_1
32 # include <openssl/md5.h>
35 # include <openssl/sha.h>
38 static unsigned const char cov_2char[64] = {
39 /* from crypto/des/fcrypt.c */
40 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
41 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44,
42 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C,
43 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54,
44 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62,
45 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
46 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72,
47 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
60 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
61 char *passwd, BIO *out, int quiet, int table,
62 int reverse, size_t pw_maxlen, passwd_modes mode);
64 typedef enum OPTION_choice {
65 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
67 OPT_NOVERIFY, OPT_QUIET, OPT_TABLE, OPT_REVERSE, OPT_APR1,
68 OPT_1, OPT_5, OPT_6, OPT_CRYPT, OPT_AIXMD5, OPT_SALT, OPT_STDIN
71 const OPTIONS passwd_options[] = {
72 {"help", OPT_HELP, '-', "Display this summary"},
73 {"in", OPT_IN, '<', "Read passwords from file"},
74 {"noverify", OPT_NOVERIFY, '-',
75 "Never verify when reading password from terminal"},
76 {"quiet", OPT_QUIET, '-', "No warnings"},
77 {"table", OPT_TABLE, '-', "Format output as table"},
78 {"reverse", OPT_REVERSE, '-', "Switch table columns"},
79 {"salt", OPT_SALT, 's', "Use provided salt"},
80 {"stdin", OPT_STDIN, '-', "Read passwords from stdin"},
82 {"6", OPT_6, '-', "SHA512-based password algorithm"},
83 {"5", OPT_5, '-', "SHA256-based password algorithm"},
85 # ifndef NO_MD5CRYPT_1
86 {"apr1", OPT_APR1, '-', "MD5-based password algorithm, Apache variant"},
87 {"1", OPT_1, '-', "MD5-based password algorithm"},
88 {"aixmd5", OPT_AIXMD5, '-', "AIX MD5-based password algorithm"},
90 # ifndef OPENSSL_NO_DES
91 {"crypt", OPT_CRYPT, '-', "Standard Unix password algorithm (default)"},
96 int passwd_main(int argc, char **argv)
99 char *infile = NULL, *salt = NULL, *passwd = NULL, **passwds = NULL;
100 char *salt_malloc = NULL, *passwd_malloc = NULL, *prog;
102 int in_stdin = 0, pw_source_defined = 0;
103 # ifndef OPENSSL_NO_UI
106 int passed_salt = 0, quiet = 0, table = 0, reverse = 0;
108 passwd_modes mode = passwd_unset;
109 size_t passwd_malloc_size = 0;
110 size_t pw_maxlen = 256; /* arbitrary limit, should be enough for most
113 prog = opt_init(argc, argv, passwd_options);
114 while ((o = opt_next()) != OPT_EOF) {
119 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
122 opt_help(passwd_options);
126 if (pw_source_defined)
129 pw_source_defined = 1;
132 # ifndef OPENSSL_NO_UI
146 if (mode != passwd_unset)
151 if (mode != passwd_unset)
153 mode = passwd_sha256;
156 if (mode != passwd_unset)
158 mode = passwd_sha512;
161 if (mode != passwd_unset)
166 if (mode != passwd_unset)
168 mode = passwd_aixmd5;
171 if (mode != passwd_unset)
180 if (pw_source_defined)
183 pw_source_defined = 1;
187 argc = opt_num_rest();
191 if (pw_source_defined)
193 pw_source_defined = 1;
197 if (mode == passwd_unset) {
202 # ifdef OPENSSL_NO_DES
203 if (mode == passwd_crypt)
206 # ifdef NO_MD5CRYPT_1
207 if (mode == passwd_md5 || mode == passwd_apr1 || mode == passwd_aixmd5)
211 if (mode == passwd_sha256 || mode == passwd_sha512)
215 if (infile != NULL && in_stdin) {
216 BIO_printf(bio_err, "%s: Can't combine -in and -stdin\n", prog);
220 if (infile != NULL || in_stdin) {
222 * If in_stdin is true, we know that infile is NULL, and that
223 * bio_open_default() will give us back an alias for stdin.
225 in = bio_open_default(infile, 'r', FORMAT_TEXT);
230 if (mode == passwd_crypt)
233 if (passwds == NULL) {
234 /* no passwords on the command line */
236 passwd_malloc_size = pw_maxlen + 2;
237 /* longer than necessary so that we can warn about truncation */
238 passwd = passwd_malloc =
239 app_malloc(passwd_malloc_size, "password buffer");
242 if ((in == NULL) && (passwds == NULL)) {
244 * we use the following method to make sure what
245 * in the 'else' section is always compiled, to
246 * avoid rot of not-frequently-used code.
249 # ifndef OPENSSL_NO_UI
250 /* build a null-terminated list */
251 static char *passwds_static[2] = { NULL, NULL };
253 passwds = passwds_static;
255 if (EVP_read_pw_string
256 (passwd_malloc, passwd_malloc_size, "Password: ",
257 !(passed_salt || in_noverify)) != 0)
260 passwds[0] = passwd_malloc;
263 BIO_printf(bio_err, "password required\n");
269 assert(passwds != NULL);
270 assert(*passwds != NULL);
272 do { /* loop over list of passwords */
274 if (!do_passwd(passed_salt, &salt, &salt_malloc, passwd, bio_out,
275 quiet, table, reverse, pw_maxlen, mode))
277 } while (*passwds != NULL);
282 assert(passwd != NULL);
284 int r = BIO_gets(in, passwd, pw_maxlen + 1);
286 char *c = (strchr(passwd, '\n'));
288 *c = 0; /* truncate at newline */
290 /* ignore rest of line */
293 r = BIO_gets(in, trash, sizeof trash);
294 while ((r > 0) && (!strchr(trash, '\n')));
298 (passed_salt, &salt, &salt_malloc, passwd, bio_out, quiet,
299 table, reverse, pw_maxlen, mode))
308 ERR_print_errors(bio_err);
309 OPENSSL_free(salt_malloc);
310 OPENSSL_free(passwd_malloc);
315 # ifndef NO_MD5CRYPT_1
317 * MD5-based password algorithm (should probably be available as a library
318 * function; then the static buffer would not be acceptable). For magic
319 * string "1", this should be compatible to the MD5-based BSD password
320 * algorithm. For 'magic' string "apr1", this is compatible to the MD5-based
321 * Apache password algorithm. (Apparently, the Apache password algorithm is
322 * identical except that the 'magic' string was changed -- the laziest
323 * application of the NIH principle I've ever encountered.)
325 static char *md5crypt(const char *passwd, const char *magic, const char *salt)
327 /* "$apr1$..salt..$.......md5hash..........\0" */
328 static char out_buf[6 + 9 + 24 + 2];
329 unsigned char buf[MD5_DIGEST_LENGTH];
333 EVP_MD_CTX *md = NULL, *md2 = NULL;
334 size_t passwd_len, salt_len, magic_len;
336 passwd_len = strlen(passwd);
339 magic_len = strlen(magic);
345 if (magic_len > 4) /* assert it's "1" or "apr1" */
348 OPENSSL_strlcat(out_buf, magic, sizeof out_buf);
349 OPENSSL_strlcat(out_buf, "$", sizeof out_buf);
352 OPENSSL_strlcat(out_buf, salt, sizeof out_buf);
354 if (strlen(out_buf) > 6 + 8) /* assert "$apr1$..salt.." */
359 salt_out += 2 + magic_len;
360 salt_len = strlen(salt_out);
365 md = EVP_MD_CTX_new();
367 || !EVP_DigestInit_ex(md, EVP_md5(), NULL)
368 || !EVP_DigestUpdate(md, passwd, passwd_len))
372 if (!EVP_DigestUpdate(md, "$", 1)
373 || !EVP_DigestUpdate(md, magic, magic_len)
374 || !EVP_DigestUpdate(md, "$", 1))
377 if (!EVP_DigestUpdate(md, salt_out, salt_len))
380 md2 = EVP_MD_CTX_new();
382 || !EVP_DigestInit_ex(md2, EVP_md5(), NULL)
383 || !EVP_DigestUpdate(md2, passwd, passwd_len)
384 || !EVP_DigestUpdate(md2, salt_out, salt_len)
385 || !EVP_DigestUpdate(md2, passwd, passwd_len)
386 || !EVP_DigestFinal_ex(md2, buf, NULL))
389 for (i = passwd_len; i > sizeof buf; i -= sizeof buf) {
390 if (!EVP_DigestUpdate(md, buf, sizeof buf))
393 if (!EVP_DigestUpdate(md, buf, i))
398 if (!EVP_DigestUpdate(md, (n & 1) ? "\0" : passwd, 1))
402 if (!EVP_DigestFinal_ex(md, buf, NULL))
405 for (i = 0; i < 1000; i++) {
406 if (!EVP_DigestInit_ex(md2, EVP_md5(), NULL))
408 if (!EVP_DigestUpdate(md2,
409 (i & 1) ? (unsigned const char *)passwd : buf,
410 (i & 1) ? passwd_len : sizeof buf))
413 if (!EVP_DigestUpdate(md2, salt_out, salt_len))
417 if (!EVP_DigestUpdate(md2, passwd, passwd_len))
420 if (!EVP_DigestUpdate(md2,
421 (i & 1) ? buf : (unsigned const char *)passwd,
422 (i & 1) ? sizeof buf : passwd_len))
424 if (!EVP_DigestFinal_ex(md2, buf, NULL))
427 EVP_MD_CTX_free(md2);
433 /* transform buf into output string */
434 unsigned char buf_perm[sizeof buf];
438 /* silly output permutation */
439 for (dest = 0, source = 0; dest < 14;
440 dest++, source = (source + 6) % 17)
441 buf_perm[dest] = buf[source];
442 buf_perm[14] = buf[5];
443 buf_perm[15] = buf[11];
444 # ifndef PEDANTIC /* Unfortunately, this generates a "no
446 assert(16 == sizeof buf_perm);
449 output = salt_out + salt_len;
450 assert(output == out_buf + strlen(out_buf));
454 for (i = 0; i < 15; i += 3) {
455 *output++ = cov_2char[buf_perm[i + 2] & 0x3f];
456 *output++ = cov_2char[((buf_perm[i + 1] & 0xf) << 2) |
457 (buf_perm[i + 2] >> 6)];
458 *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
459 (buf_perm[i + 1] >> 4)];
460 *output++ = cov_2char[buf_perm[i] >> 2];
463 *output++ = cov_2char[buf_perm[i] & 0x3f];
464 *output++ = cov_2char[buf_perm[i] >> 6];
466 assert(strlen(out_buf) < sizeof(out_buf));
472 EVP_MD_CTX_free(md2);
480 * SHA based password algorithm, describe by Ulrich Drepper here:
481 * https://www.akkadia.org/drepper/SHA-crypt.txt
482 * (note that it's in the public domain)
484 static char *shacrypt(const char *passwd, const char *magic, const char *salt)
486 /* Prefix for optional rounds specification. */
487 static const char rounds_prefix[] = "rounds=";
488 /* Maximum salt string length. */
489 # define SALT_LEN_MAX 16
490 /* Default number of rounds if not explicitly specified. */
491 # define ROUNDS_DEFAULT 5000
492 /* Minimum number of rounds. */
493 # define ROUNDS_MIN 1000
494 /* Maximum number of rounds. */
495 # define ROUNDS_MAX 999999999
497 /* "$6$rounds=<N>$......salt......$...shahash(up to 86 chars)...\0" */
498 static char out_buf[3 + 17 + 17 + 86 + 1];
499 unsigned char buf[SHA512_DIGEST_LENGTH];
500 unsigned char temp_buf[SHA512_DIGEST_LENGTH];
502 char salt_copy[17]; /* Max 16 chars plus '\0' */
504 EVP_MD_CTX *md = NULL, *md2 = NULL;
505 const EVP_MD *sha = NULL;
506 size_t passwd_len, salt_len, magic_len;
507 unsigned int rounds = 5000; /* Default */
508 char rounds_custom = 0;
509 char *p_bytes = NULL;
510 char *s_bytes = NULL;
513 passwd_len = strlen(passwd);
514 magic_len = strlen(magic);
516 /* assert it's "5" or "6" */
533 if (strncmp(salt, rounds_prefix, sizeof(rounds_prefix) - 1) == 0) {
534 const char *num = salt + sizeof(rounds_prefix) - 1;
536 unsigned long int srounds = strtoul (num, &endp, 10);
539 if (srounds > ROUNDS_MAX)
541 else if (srounds < ROUNDS_MIN)
544 rounds = (unsigned int)srounds;
551 /* The salt gets truncated to 16 chars */
552 OPENSSL_strlcpy(salt_copy, salt, sizeof salt_copy);
553 salt_len = strlen(salt_copy);
556 OPENSSL_strlcat(out_buf, "$", sizeof out_buf);
557 OPENSSL_strlcat(out_buf, magic, sizeof out_buf);
558 OPENSSL_strlcat(out_buf, "$", sizeof out_buf);
560 char tmp_buf[80]; /* "rounds=999999999" */
561 sprintf(tmp_buf, "rounds=%u", rounds);
562 OPENSSL_strlcat(out_buf, tmp_buf, sizeof out_buf);
563 OPENSSL_strlcat(out_buf, "$", sizeof out_buf);
565 OPENSSL_strlcat(out_buf, salt_copy, sizeof out_buf);
567 /* assert "$5$rounds=999999999$......salt......" */
568 if (strlen(out_buf) > 3 + 17 * rounds_custom + salt_len )
571 md = EVP_MD_CTX_new();
573 || !EVP_DigestInit_ex(md, sha, NULL)
574 || !EVP_DigestUpdate(md, passwd, passwd_len)
575 || !EVP_DigestUpdate(md, salt_copy, salt_len))
578 md2 = EVP_MD_CTX_new();
580 || !EVP_DigestInit_ex(md2, sha, NULL)
581 || !EVP_DigestUpdate(md2, passwd, passwd_len)
582 || !EVP_DigestUpdate(md2, salt_copy, salt_len)
583 || !EVP_DigestUpdate(md2, passwd, passwd_len)
584 || !EVP_DigestFinal_ex(md2, buf, NULL))
587 for (n = passwd_len; n > buf_size; n -= buf_size) {
588 if (!EVP_DigestUpdate(md, buf, buf_size))
591 if (!EVP_DigestUpdate(md, buf, n))
596 if (!EVP_DigestUpdate(md,
597 (n & 1) ? buf : (unsigned const char *)passwd,
598 (n & 1) ? buf_size : passwd_len))
602 if (!EVP_DigestFinal_ex(md, buf, NULL))
606 if (!EVP_DigestInit_ex(md2, sha, NULL))
609 for (n = passwd_len; n > 0; n--)
610 if (!EVP_DigestUpdate(md2, passwd, passwd_len))
613 if (!EVP_DigestFinal_ex(md2, temp_buf, NULL))
616 if ((p_bytes = OPENSSL_zalloc(passwd_len)) == NULL)
618 for (cp = p_bytes, n = passwd_len; n > buf_size; n -= buf_size, cp += buf_size)
619 memcpy(cp, temp_buf, buf_size);
620 memcpy(cp, temp_buf, n);
623 if (!EVP_DigestInit_ex(md2, sha, NULL))
626 for (n = 16 + buf[0]; n > 0; n--)
627 if (!EVP_DigestUpdate(md2, salt, salt_len))
630 if (!EVP_DigestFinal_ex(md2, temp_buf, NULL))
633 if ((s_bytes = OPENSSL_zalloc(salt_len)) == NULL)
635 for (cp = s_bytes, n = salt_len; n > buf_size; n -= buf_size, cp += buf_size)
636 memcpy(cp, temp_buf, buf_size);
637 memcpy(cp, temp_buf, n);
639 for (n = 0; n < rounds; n++) {
640 if (!EVP_DigestInit_ex(md2, sha, NULL))
642 if (!EVP_DigestUpdate(md2,
643 (n & 1) ? (unsigned const char *)p_bytes : buf,
644 (n & 1) ? passwd_len : buf_size))
647 if (!EVP_DigestUpdate(md2, s_bytes, salt_len))
651 if (!EVP_DigestUpdate(md2, p_bytes, passwd_len))
654 if (!EVP_DigestUpdate(md2,
655 (n & 1) ? buf : (unsigned const char *)p_bytes,
656 (n & 1) ? buf_size : passwd_len))
658 if (!EVP_DigestFinal_ex(md2, buf, NULL))
661 EVP_MD_CTX_free(md2);
665 OPENSSL_free(p_bytes);
666 OPENSSL_free(s_bytes);
670 cp = out_buf + strlen(out_buf);
673 # define b64_from_24bit(B2, B1, B0, N) \
675 unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
679 *cp++ = cov_2char[w & 0x3f]; \
686 b64_from_24bit (buf[0], buf[10], buf[20], 4);
687 b64_from_24bit (buf[21], buf[1], buf[11], 4);
688 b64_from_24bit (buf[12], buf[22], buf[2], 4);
689 b64_from_24bit (buf[3], buf[13], buf[23], 4);
690 b64_from_24bit (buf[24], buf[4], buf[14], 4);
691 b64_from_24bit (buf[15], buf[25], buf[5], 4);
692 b64_from_24bit (buf[6], buf[16], buf[26], 4);
693 b64_from_24bit (buf[27], buf[7], buf[17], 4);
694 b64_from_24bit (buf[18], buf[28], buf[8], 4);
695 b64_from_24bit (buf[9], buf[19], buf[29], 4);
696 b64_from_24bit (0, buf[31], buf[30], 3);
699 b64_from_24bit (buf[0], buf[21], buf[42], 4);
700 b64_from_24bit (buf[22], buf[43], buf[1], 4);
701 b64_from_24bit (buf[44], buf[2], buf[23], 4);
702 b64_from_24bit (buf[3], buf[24], buf[45], 4);
703 b64_from_24bit (buf[25], buf[46], buf[4], 4);
704 b64_from_24bit (buf[47], buf[5], buf[26], 4);
705 b64_from_24bit (buf[6], buf[27], buf[48], 4);
706 b64_from_24bit (buf[28], buf[49], buf[7], 4);
707 b64_from_24bit (buf[50], buf[8], buf[29], 4);
708 b64_from_24bit (buf[9], buf[30], buf[51], 4);
709 b64_from_24bit (buf[31], buf[52], buf[10], 4);
710 b64_from_24bit (buf[53], buf[11], buf[32], 4);
711 b64_from_24bit (buf[12], buf[33], buf[54], 4);
712 b64_from_24bit (buf[34], buf[55], buf[13], 4);
713 b64_from_24bit (buf[56], buf[14], buf[35], 4);
714 b64_from_24bit (buf[15], buf[36], buf[57], 4);
715 b64_from_24bit (buf[37], buf[58], buf[16], 4);
716 b64_from_24bit (buf[59], buf[17], buf[38], 4);
717 b64_from_24bit (buf[18], buf[39], buf[60], 4);
718 b64_from_24bit (buf[40], buf[61], buf[19], 4);
719 b64_from_24bit (buf[62], buf[20], buf[41], 4);
720 b64_from_24bit (0, 0, buf[63], 2);
730 EVP_MD_CTX_free(md2);
732 OPENSSL_free(p_bytes);
733 OPENSSL_free(s_bytes);
738 static int do_passwd(int passed_salt, char **salt_p, char **salt_malloc_p,
739 char *passwd, BIO *out, int quiet, int table,
740 int reverse, size_t pw_maxlen, passwd_modes mode)
744 assert(salt_p != NULL);
745 assert(salt_malloc_p != NULL);
747 /* first make sure we have a salt */
749 # ifndef OPENSSL_NO_DES
750 if (mode == passwd_crypt) {
751 if (*salt_malloc_p == NULL)
752 *salt_p = *salt_malloc_p = app_malloc(3, "salt buffer");
753 if (RAND_bytes((unsigned char *)*salt_p, 2) <= 0)
755 (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
756 (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
758 # ifdef CHARSET_EBCDIC
759 ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert back
763 # endif /* !OPENSSL_NO_DES */
765 # ifndef NO_MD5CRYPT_1
766 if (mode == passwd_md5 || mode == passwd_apr1 || mode == passwd_aixmd5) {
769 if (*salt_malloc_p == NULL)
770 *salt_p = *salt_malloc_p = app_malloc(9, "salt buffer");
771 if (RAND_bytes((unsigned char *)*salt_p, 8) <= 0)
774 for (i = 0; i < 8; i++)
775 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
778 # endif /* !NO_MD5CRYPT_1 */
781 if (mode == passwd_sha256 || mode == passwd_sha512) {
784 if (*salt_malloc_p == NULL)
785 *salt_p = *salt_malloc_p = app_malloc(17, "salt buffer");
786 if (RAND_bytes((unsigned char *)*salt_p, 16) <= 0)
789 for (i = 0; i < 16; i++)
790 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
793 # endif /* !NO_SHACRYPT */
796 assert(*salt_p != NULL);
798 /* truncate password if necessary */
799 if ((strlen(passwd) > pw_maxlen)) {
802 * XXX: really we should know how to print a size_t, not cast it
805 "Warning: truncating password to %u characters\n",
806 (unsigned)pw_maxlen);
807 passwd[pw_maxlen] = 0;
809 assert(strlen(passwd) <= pw_maxlen);
811 /* now compute password hash */
812 # ifndef OPENSSL_NO_DES
813 if (mode == passwd_crypt)
814 hash = DES_crypt(passwd, *salt_p);
816 # ifndef NO_MD5CRYPT_1
817 if (mode == passwd_md5 || mode == passwd_apr1)
818 hash = md5crypt(passwd, (mode == passwd_md5 ? "1" : "apr1"), *salt_p);
819 if (mode == passwd_aixmd5)
820 hash = md5crypt(passwd, "", *salt_p);
823 if (mode == passwd_sha256 || mode == passwd_sha512)
824 hash = shacrypt(passwd, (mode == passwd_sha256 ? "5" : "6"), *salt_p);
826 assert(hash != NULL);
828 if (table && !reverse)
829 BIO_printf(out, "%s\t%s\n", passwd, hash);
830 else if (table && reverse)
831 BIO_printf(out, "%s\t%s\n", hash, passwd);
833 BIO_printf(out, "%s\n", hash);
841 int passwd_main(int argc, char **argv)
843 BIO_printf(bio_err, "Program not available.\n");