pw_gensalt.c revision 1.12 1 1.12 nia /* $NetBSD: pw_gensalt.c,v 1.12 2021/10/16 10:53:33 nia Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright 1997 Niels Provos <provos (at) physnet.uni-hamburg.de>
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos * 3. All advertising materials mentioning features or use of this software
16 1.1 christos * must display the following acknowledgement:
17 1.1 christos * This product includes software developed by Niels Provos.
18 1.1 christos * 4. The name of the author may not be used to endorse or promote products
19 1.1 christos * derived from this software without specific prior written permission.
20 1.1 christos *
21 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 christos *
32 1.1 christos * from OpenBSD: pwd_gensalt.c,v 1.9 1998/07/05 21:08:32 provos Exp
33 1.1 christos */
34 1.1 christos
35 1.1 christos #include <sys/cdefs.h>
36 1.1 christos #ifndef lint
37 1.12 nia __RCSID("$NetBSD: pw_gensalt.c,v 1.12 2021/10/16 10:53:33 nia Exp $");
38 1.1 christos #endif /* not lint */
39 1.1 christos
40 1.1 christos #include <sys/syslimits.h>
41 1.1 christos #include <sys/types.h>
42 1.1 christos
43 1.1 christos #include <stdio.h>
44 1.1 christos #include <stdlib.h>
45 1.1 christos #include <string.h>
46 1.1 christos #include <limits.h>
47 1.1 christos #include <err.h>
48 1.1 christos #include <grp.h>
49 1.1 christos #include <pwd.h>
50 1.1 christos #include <util.h>
51 1.1 christos #include <time.h>
52 1.1 christos #include <errno.h>
53 1.1 christos
54 1.3 christos #include "crypt.h"
55 1.1 christos
56 1.8 jhigh #ifdef HAVE_ARGON2
57 1.8 jhigh #include <argon2.h>
58 1.8 jhigh #define ARGON2_ARGON2_STR "argon2"
59 1.8 jhigh #define ARGON2_ARGON2I_STR "argon2i"
60 1.8 jhigh #define ARGON2_ARGON2D_STR "argon2d"
61 1.8 jhigh #define ARGON2_ARGON2ID_STR "argon2id"
62 1.8 jhigh #endif /* HAVE_ARGON2 */
63 1.1 christos
64 1.1 christos static const struct pw_salt {
65 1.1 christos const char *name;
66 1.4 christos int (*gensalt)(char *, size_t, const char *);
67 1.1 christos } salts[] = {
68 1.1 christos { "old", __gensalt_old },
69 1.1 christos { "new", __gensalt_new },
70 1.1 christos { "newsalt", __gensalt_new },
71 1.1 christos { "md5", __gensalt_md5 },
72 1.1 christos { "sha1", __gensalt_sha1 },
73 1.1 christos { "blowfish", __gensalt_blowfish },
74 1.8 jhigh #ifdef HAVE_ARGON2
75 1.8 jhigh /* argon2 default to argon2id */
76 1.8 jhigh { "argon2", __gensalt_argon2id},
77 1.8 jhigh { "argon2id", __gensalt_argon2id},
78 1.8 jhigh { "argon2i", __gensalt_argon2i},
79 1.8 jhigh { "argon2d", __gensalt_argon2d},
80 1.8 jhigh #endif /* HAVE_ARGON2 */
81 1.1 christos { NULL, NULL }
82 1.1 christos };
83 1.1 christos
84 1.12 nia crypt_private int
85 1.3 christos /*ARGSUSED2*/
86 1.4 christos __gensalt_old(char *salt, size_t saltsiz, const char *option)
87 1.1 christos {
88 1.1 christos if (saltsiz < 3) {
89 1.1 christos errno = ENOSPC;
90 1.1 christos return -1;
91 1.1 christos }
92 1.1 christos __crypt_to64(&salt[0], arc4random(), 2);
93 1.1 christos salt[2] = '\0';
94 1.1 christos return 0;
95 1.1 christos }
96 1.1 christos
97 1.12 nia crypt_private int
98 1.3 christos /*ARGSUSED2*/
99 1.4 christos __gensalt_new(char *salt, size_t saltsiz, const char* option)
100 1.1 christos {
101 1.4 christos size_t nrounds;
102 1.4 christos
103 1.1 christos if (saltsiz < 10) {
104 1.1 christos errno = ENOSPC;
105 1.1 christos return -1;
106 1.1 christos }
107 1.4 christos
108 1.4 christos if (getnum(option, &nrounds) == -1)
109 1.4 christos return -1;
110 1.4 christos
111 1.1 christos /* Check rounds, 24 bit is max */
112 1.1 christos if (nrounds < 7250)
113 1.1 christos nrounds = 7250;
114 1.1 christos else if (nrounds > 0xffffff)
115 1.1 christos nrounds = 0xffffff;
116 1.1 christos salt[0] = _PASSWORD_EFMT1;
117 1.1 christos __crypt_to64(&salt[1], (uint32_t)nrounds, 4);
118 1.1 christos __crypt_to64(&salt[5], arc4random(), 4);
119 1.1 christos salt[9] = '\0';
120 1.1 christos return 0;
121 1.1 christos }
122 1.1 christos
123 1.12 nia crypt_private int
124 1.3 christos /*ARGSUSED2*/
125 1.4 christos __gensalt_md5(char *salt, size_t saltsiz, const char *option)
126 1.1 christos {
127 1.1 christos if (saltsiz < 13) { /* $1$8salt$\0 */
128 1.1 christos errno = ENOSPC;
129 1.1 christos return -1;
130 1.1 christos }
131 1.1 christos salt[0] = _PASSWORD_NONDES;
132 1.1 christos salt[1] = '1';
133 1.1 christos salt[2] = '$';
134 1.1 christos __crypt_to64(&salt[3], arc4random(), 4);
135 1.1 christos __crypt_to64(&salt[7], arc4random(), 4);
136 1.1 christos salt[11] = '$';
137 1.1 christos salt[12] = '\0';
138 1.1 christos return 0;
139 1.1 christos }
140 1.1 christos
141 1.12 nia crypt_private int
142 1.4 christos __gensalt_sha1(char *salt, size_t saltsiz, const char *option)
143 1.1 christos {
144 1.1 christos int n;
145 1.4 christos size_t nrounds;
146 1.1 christos
147 1.4 christos if (getnum(option, &nrounds) == -1)
148 1.4 christos return -1;
149 1.1 christos n = snprintf(salt, saltsiz, "%s%u$", SHA1_MAGIC,
150 1.1 christos __crypt_sha1_iterations(nrounds));
151 1.1 christos /*
152 1.1 christos * The salt can be up to 64 bytes, but 8
153 1.1 christos * is considered enough for now.
154 1.1 christos */
155 1.7 lukem if ((size_t)n + 9 >= saltsiz)
156 1.1 christos return 0;
157 1.1 christos __crypt_to64(&salt[n], arc4random(), 4);
158 1.1 christos __crypt_to64(&salt[n + 4], arc4random(), 4);
159 1.1 christos salt[n + 8] = '$';
160 1.1 christos salt[n + 9] = '\0';
161 1.1 christos return 0;
162 1.1 christos }
163 1.1 christos
164 1.8 jhigh #ifdef HAVE_ARGON2
165 1.12 nia static int
166 1.12 nia __gensalt_argon2_decode_option(char *dst, size_t dlen, const char *option)
167 1.8 jhigh {
168 1.8 jhigh
169 1.9 msaitoh char * in = 0;
170 1.8 jhigh char * a = 0;
171 1.8 jhigh size_t tmp = 0;
172 1.8 jhigh int error = 0;
173 1.8 jhigh /* ob buffer: m_cost, t_cost, threads */
174 1.8 jhigh uint32_t ob[3] = {4096, 3, 1};
175 1.8 jhigh
176 1.8 jhigh memset(dst, 0, dlen);
177 1.8 jhigh
178 1.8 jhigh if (option == NULL) {
179 1.8 jhigh goto done;
180 1.8 jhigh }
181 1.8 jhigh
182 1.8 jhigh in = (char *)strdup(option);
183 1.8 jhigh
184 1.8 jhigh while ((a = strsep(&in, ",")) != NULL) {
185 1.8 jhigh switch(*a) {
186 1.8 jhigh
187 1.8 jhigh case 'm':
188 1.8 jhigh a += strlen("m=");
189 1.8 jhigh if ((getnum(a, &tmp)) == -1) {
190 1.8 jhigh --error;
191 1.8 jhigh } else {
192 1.8 jhigh ob[0] = tmp;
193 1.8 jhigh }
194 1.8 jhigh
195 1.8 jhigh break;
196 1.8 jhigh case 't':
197 1.8 jhigh a += strlen("t=");
198 1.8 jhigh if ((getnum(a, &tmp)) == -1) {
199 1.8 jhigh --error;
200 1.8 jhigh } else {
201 1.8 jhigh ob[1] = tmp;
202 1.8 jhigh }
203 1.8 jhigh
204 1.8 jhigh break;
205 1.8 jhigh case 'p':
206 1.8 jhigh a += strlen("p=");
207 1.8 jhigh if ((getnum(a, &tmp)) == -1) {
208 1.8 jhigh --error;
209 1.8 jhigh } else {
210 1.8 jhigh ob[2] = tmp;
211 1.8 jhigh }
212 1.8 jhigh
213 1.8 jhigh break;
214 1.8 jhigh default:
215 1.8 jhigh --error;
216 1.8 jhigh }
217 1.8 jhigh }
218 1.8 jhigh
219 1.8 jhigh free(in);
220 1.8 jhigh done:
221 1.8 jhigh snprintf(dst, dlen, "m=%d,t=%d,p=%d", ob[0], ob[1], ob[2]);
222 1.8 jhigh
223 1.8 jhigh return error;
224 1.8 jhigh }
225 1.8 jhigh
226 1.8 jhigh
227 1.8 jhigh static int
228 1.8 jhigh __gensalt_argon2(char *salt, size_t saltsiz, const char *option,argon2_type atype)
229 1.8 jhigh {
230 1.8 jhigh int rc;
231 1.8 jhigh int n;
232 1.8 jhigh char buf[64];
233 1.8 jhigh
234 1.8 jhigh /* get param, enforcing order and applying defaults */
235 1.8 jhigh if ((rc = __gensalt_argon2_decode_option(buf, sizeof(buf), option)) < 0) {
236 1.8 jhigh return 0;
237 1.8 jhigh }
238 1.8 jhigh
239 1.8 jhigh n = snprintf(salt, saltsiz, "$%s$v=%d$%s$",
240 1.8 jhigh argon2_type2string(atype,0), ARGON2_VERSION_NUMBER, buf);
241 1.8 jhigh
242 1.8 jhigh if ((size_t)n + 16 >= saltsiz) {
243 1.8 jhigh return 0;
244 1.8 jhigh }
245 1.8 jhigh
246 1.11 nia __crypt_tobase64(&salt[n], arc4random(), 4);
247 1.11 nia __crypt_tobase64(&salt[n + 4], arc4random(), 4);
248 1.11 nia __crypt_tobase64(&salt[n + 8], arc4random(), 4);
249 1.11 nia __crypt_tobase64(&salt[n + 12], arc4random(), 4);
250 1.8 jhigh
251 1.8 jhigh salt[n + 16] = '$';
252 1.8 jhigh salt[n + 17] = '\0';
253 1.8 jhigh
254 1.8 jhigh return 0;
255 1.8 jhigh }
256 1.8 jhigh
257 1.8 jhigh /* argon2 variant-specific hooks to generic */
258 1.12 nia crypt_private int
259 1.8 jhigh __gensalt_argon2id(char *salt, size_t saltsiz, const char *option)
260 1.8 jhigh {
261 1.8 jhigh return __gensalt_argon2(salt, saltsiz, option, Argon2_id);
262 1.8 jhigh }
263 1.8 jhigh
264 1.12 nia crypt_private int
265 1.8 jhigh __gensalt_argon2i(char *salt, size_t saltsiz, const char *option)
266 1.8 jhigh {
267 1.8 jhigh return __gensalt_argon2(salt, saltsiz, option, Argon2_i);
268 1.8 jhigh }
269 1.8 jhigh
270 1.12 nia crypt_private int
271 1.8 jhigh __gensalt_argon2d(char *salt, size_t saltsiz, const char *option)
272 1.8 jhigh {
273 1.8 jhigh return __gensalt_argon2(salt, saltsiz, option, Argon2_d);
274 1.8 jhigh }
275 1.8 jhigh
276 1.8 jhigh #endif /* HAVE_ARGON2 */
277 1.8 jhigh
278 1.8 jhigh
279 1.1 christos int
280 1.4 christos pw_gensalt(char *salt, size_t saltlen, const char *type, const char *option)
281 1.1 christos {
282 1.5 christos const struct pw_salt *sp;
283 1.5 christos
284 1.1 christos for (sp = salts; sp->name; sp++)
285 1.4 christos if (strcmp(sp->name, type) == 0)
286 1.4 christos return (*sp->gensalt)(salt, saltlen, option);
287 1.1 christos
288 1.1 christos errno = EINVAL;
289 1.1 christos return -1;
290 1.1 christos }
291