1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1999 Mark Murray
5 * Copyright (c) 2014 Dag-Erling Smørgrav
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY MARK MURRAY AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL MARK MURRAY OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 #include <sys/types.h>
32
33 #include <string.h>
34 #include <unistd.h>
35
36 #include "crypt.h"
37
38 /*
39 * List of supported crypt(3) formats.
40 *
41 * The default algorithm is the last entry in the list (second-to-last
42 * array element since the last is a sentinel). The reason for placing
43 * the default last rather than first is that DES needs to be at the
44 * bottom for the algorithm guessing logic in crypt(3) to work correctly,
45 * and it needs to be the default for backward compatibility.
46 */
47 static const struct crypt_format {
48 const char *name;
49 int (*func)(const char *, const char *, char *);
50 const char *magic;
51 } crypt_formats[] = {
52 { "md5", crypt_md5, "$1$" },
53 #ifdef HAS_BLOWFISH
54 { "blf", crypt_blowfish, "$2" },
55 #endif
56 { "nth", crypt_nthash, "$3$" },
57 { "sha256", crypt_sha256, "$5$" },
58 { "sha512", crypt_sha512, "$6$" },
59 #ifdef HAS_DES
60 { "des", crypt_des, "_" },
61 #endif
62
63 /* sentinel */
64 { NULL, NULL, NULL }
65 };
66
67 static const struct crypt_format *crypt_format =
68 &crypt_formats[(sizeof crypt_formats / sizeof *crypt_formats) - 2];
69
70 #define DES_SALT_ALPHABET \
71 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
72
73 /*
74 * Returns the name of the currently selected format.
75 */
76 const char *
crypt_get_format(void)77 crypt_get_format(void)
78 {
79
80 return (crypt_format->name);
81 }
82
83 /*
84 * Selects the format to use for subsequent crypt(3) invocations.
85 */
86 int
crypt_set_format(const char * format)87 crypt_set_format(const char *format)
88 {
89 const struct crypt_format *cf;
90
91 for (cf = crypt_formats; cf->name != NULL; ++cf) {
92 if (strcasecmp(cf->name, format) == 0) {
93 crypt_format = cf;
94 return (1);
95 }
96 }
97 return (0);
98 }
99
100 /*
101 * Hash the given password with the given salt. If the salt begins with a
102 * magic string (e.g. "$6$" for sha512), the corresponding format is used;
103 * otherwise, the currently selected format is used.
104 */
105 char *
crypt_r(const char * passwd,const char * salt,struct crypt_data * data)106 crypt_r(const char *passwd, const char *salt, struct crypt_data *data)
107 {
108 const struct crypt_format *cf;
109 int (*func)(const char *, const char *, char *);
110 #ifdef HAS_DES
111 int len;
112 #endif
113
114 for (cf = crypt_formats; cf->name != NULL; ++cf)
115 if (cf->magic != NULL && strstr(salt, cf->magic) == salt) {
116 func = cf->func;
117 goto match;
118 }
119 #ifdef HAS_DES
120 len = strlen(salt);
121 if ((len == 13 || len == 2) && strspn(salt, DES_SALT_ALPHABET) == len) {
122 func = crypt_des;
123 goto match;
124 }
125 #endif
126 func = crypt_format->func;
127 match:
128 if (func(passwd, salt, data->__buf) != 0)
129 return (NULL);
130 return (data->__buf);
131 }
132
133 char *
crypt(const char * passwd,const char * salt)134 crypt(const char *passwd, const char *salt)
135 {
136 static struct crypt_data data;
137
138 return (crypt_r(passwd, salt, &data));
139 }
140