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