Home | History | Annotate | Line # | Download | only in libcrypt
pw_gensalt.c revision 1.13.2.1
      1  1.13.2.1    martin /*	$NetBSD: pw_gensalt.c,v 1.13.2.1 2024/08/23 16:40:48 martin 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.13.2.1    martin __RCSID("$NetBSD: pw_gensalt.c,v 1.13.2.1 2024/08/23 16:40:48 martin 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.13       nia 
     63      1.13       nia crypt_private int
     64      1.13       nia estimate_argon2_params(argon2_type, uint32_t *, uint32_t *, uint32_t *);
     65       1.8     jhigh #endif /* HAVE_ARGON2 */
     66       1.1  christos 
     67       1.1  christos static const struct pw_salt {
     68       1.1  christos 	const char *name;
     69       1.4  christos 	int (*gensalt)(char *, size_t, const char *);
     70       1.1  christos } salts[] = {
     71       1.1  christos 	{ "old", __gensalt_old },
     72       1.1  christos 	{ "new", __gensalt_new },
     73       1.1  christos 	{ "newsalt", __gensalt_new },
     74       1.1  christos 	{ "md5", __gensalt_md5 },
     75       1.1  christos 	{ "sha1", __gensalt_sha1 },
     76       1.1  christos 	{ "blowfish", __gensalt_blowfish },
     77       1.8     jhigh #ifdef HAVE_ARGON2
     78       1.8     jhigh 	/* argon2 default to argon2id */
     79       1.8     jhigh 	{ "argon2", __gensalt_argon2id},
     80       1.8     jhigh 	{ "argon2id", __gensalt_argon2id},
     81       1.8     jhigh 	{ "argon2i", __gensalt_argon2i},
     82       1.8     jhigh 	{ "argon2d", __gensalt_argon2d},
     83       1.8     jhigh #endif /* HAVE_ARGON2 */
     84       1.1  christos 	{ NULL, NULL }
     85       1.1  christos };
     86       1.1  christos 
     87      1.12       nia crypt_private int
     88       1.3  christos /*ARGSUSED2*/
     89       1.4  christos __gensalt_old(char *salt, size_t saltsiz, const char *option)
     90       1.1  christos {
     91       1.1  christos 	if (saltsiz < 3) {
     92       1.1  christos 		errno = ENOSPC;
     93       1.1  christos 		return -1;
     94       1.1  christos 	}
     95       1.1  christos 	__crypt_to64(&salt[0], arc4random(), 2);
     96       1.1  christos 	salt[2] = '\0';
     97       1.1  christos 	return 0;
     98       1.1  christos }
     99       1.1  christos 
    100      1.12       nia crypt_private int
    101       1.3  christos /*ARGSUSED2*/
    102       1.4  christos __gensalt_new(char *salt, size_t saltsiz, const char* option)
    103       1.1  christos {
    104       1.4  christos 	size_t nrounds;
    105       1.4  christos 
    106       1.1  christos 	if (saltsiz < 10) {
    107       1.1  christos 		errno = ENOSPC;
    108       1.1  christos 		return -1;
    109       1.1  christos 	}
    110       1.4  christos 
    111       1.4  christos 	if (getnum(option, &nrounds) == -1)
    112       1.4  christos 		return -1;
    113       1.4  christos 
    114       1.1  christos 	/* Check rounds, 24 bit is max */
    115       1.1  christos 	if (nrounds < 7250)
    116       1.1  christos 		nrounds = 7250;
    117       1.1  christos 	else if (nrounds > 0xffffff)
    118       1.1  christos 		nrounds = 0xffffff;
    119       1.1  christos 	salt[0] = _PASSWORD_EFMT1;
    120       1.1  christos 	__crypt_to64(&salt[1], (uint32_t)nrounds, 4);
    121       1.1  christos 	__crypt_to64(&salt[5], arc4random(), 4);
    122       1.1  christos 	salt[9] = '\0';
    123       1.1  christos 	return 0;
    124       1.1  christos }
    125       1.1  christos 
    126      1.12       nia crypt_private int
    127       1.3  christos /*ARGSUSED2*/
    128       1.4  christos __gensalt_md5(char *salt, size_t saltsiz, const char *option)
    129       1.1  christos {
    130       1.1  christos 	if (saltsiz < 13) {  /* $1$8salt$\0 */
    131       1.1  christos 		errno = ENOSPC;
    132       1.1  christos 		return -1;
    133       1.1  christos 	}
    134       1.1  christos 	salt[0] = _PASSWORD_NONDES;
    135       1.1  christos 	salt[1] = '1';
    136       1.1  christos 	salt[2] = '$';
    137       1.1  christos 	__crypt_to64(&salt[3], arc4random(), 4);
    138       1.1  christos 	__crypt_to64(&salt[7], arc4random(), 4);
    139       1.1  christos 	salt[11] = '$';
    140       1.1  christos 	salt[12] = '\0';
    141       1.1  christos 	return 0;
    142       1.1  christos }
    143       1.1  christos 
    144      1.12       nia crypt_private int
    145       1.4  christos __gensalt_sha1(char *salt, size_t saltsiz, const char *option)
    146       1.1  christos {
    147       1.1  christos 	int n;
    148       1.4  christos 	size_t nrounds;
    149       1.1  christos 
    150       1.4  christos 	if (getnum(option, &nrounds) == -1)
    151       1.4  christos 		return -1;
    152       1.1  christos 	n = snprintf(salt, saltsiz, "%s%u$", SHA1_MAGIC,
    153       1.1  christos 	    __crypt_sha1_iterations(nrounds));
    154       1.1  christos 	/*
    155       1.1  christos 	 * The salt can be up to 64 bytes, but 8
    156       1.1  christos 	 * is considered enough for now.
    157       1.1  christos 	 */
    158       1.7     lukem 	if ((size_t)n + 9 >= saltsiz)
    159       1.1  christos 		return 0;
    160       1.1  christos 	__crypt_to64(&salt[n], arc4random(), 4);
    161       1.1  christos 	__crypt_to64(&salt[n + 4], arc4random(), 4);
    162       1.1  christos 	salt[n + 8] = '$';
    163       1.1  christos 	salt[n + 9] = '\0';
    164       1.1  christos 	return 0;
    165       1.1  christos }
    166       1.1  christos 
    167       1.8     jhigh #ifdef HAVE_ARGON2
    168      1.12       nia static int
    169      1.13       nia __gensalt_argon2_decode_option(char *dst, size_t dlen,
    170      1.13       nia     const char *option, argon2_type atype)
    171       1.8     jhigh {
    172      1.13       nia 	char *in = 0;
    173      1.13       nia 	char *a = 0;
    174       1.8     jhigh 	size_t tmp = 0;
    175       1.8     jhigh 	int error = 0;
    176      1.13       nia 	uint32_t memory = 0;
    177      1.13       nia 	uint32_t time = 0;
    178      1.13       nia 	uint32_t threads = 0;
    179       1.8     jhigh 
    180       1.8     jhigh 	memset(dst, 0, dlen);
    181       1.8     jhigh 
    182       1.8     jhigh 	if (option == NULL) {
    183       1.8     jhigh 		goto done;
    184       1.8     jhigh 	}
    185       1.8     jhigh 
    186      1.13       nia 	in = strdup(option);
    187       1.8     jhigh 
    188       1.8     jhigh 	while ((a = strsep(&in, ",")) != NULL) {
    189      1.13       nia 		switch (*a) {
    190       1.8     jhigh 			case 'm':
    191       1.8     jhigh 				a += strlen("m=");
    192       1.8     jhigh 				if ((getnum(a, &tmp)) == -1) {
    193       1.8     jhigh 					--error;
    194       1.8     jhigh 				} else {
    195      1.13       nia 					memory = tmp;
    196       1.8     jhigh 				}
    197       1.8     jhigh 				break;
    198       1.8     jhigh 			case 't':
    199       1.8     jhigh 				a += strlen("t=");
    200       1.8     jhigh 				if ((getnum(a, &tmp)) == -1) {
    201       1.8     jhigh 					--error;
    202       1.8     jhigh 				} else {
    203      1.13       nia 					time = tmp;
    204       1.8     jhigh 				}
    205       1.8     jhigh 				break;
    206       1.8     jhigh 			case 'p':
    207       1.8     jhigh 				a += strlen("p=");
    208       1.8     jhigh 				if ((getnum(a, &tmp)) == -1) {
    209       1.8     jhigh 					--error;
    210       1.8     jhigh 				} else {
    211      1.13       nia 					threads = tmp;
    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.13       nia 
    221       1.8     jhigh done:
    222      1.13       nia 	/*
    223      1.13       nia 	 * If parameters are unspecified, calculate some reasonable
    224      1.13       nia 	 * ones based on system time.
    225      1.13       nia 	 */
    226      1.13       nia 	if (memory < ARGON2_MIN_MEMORY ||
    227      1.13       nia 	    time < ARGON2_MIN_TIME ||
    228      1.13       nia 	    threads < ARGON2_MIN_THREADS) {
    229      1.13       nia 		estimate_argon2_params(atype, &time, &memory, &threads);
    230      1.13       nia 	}
    231      1.13       nia 
    232      1.13       nia 	snprintf(dst, dlen, "m=%d,t=%d,p=%d", memory, time, threads);
    233       1.8     jhigh 
    234       1.8     jhigh 	return error;
    235       1.8     jhigh }
    236       1.8     jhigh 
    237       1.8     jhigh 
    238       1.8     jhigh static int
    239      1.13       nia __gensalt_argon2(char *salt, size_t saltsiz,
    240      1.13       nia     const char *option, argon2_type atype)
    241       1.8     jhigh {
    242       1.8     jhigh 	int rc;
    243       1.8     jhigh 	int n;
    244       1.8     jhigh 	char buf[64];
    245       1.8     jhigh 
    246       1.8     jhigh 	/* get param, enforcing order and applying defaults */
    247      1.13       nia 	if ((rc = __gensalt_argon2_decode_option(buf,
    248      1.13       nia 	    sizeof(buf), option, atype)) < 0) {
    249       1.8     jhigh 		return 0;
    250       1.8     jhigh 	}
    251       1.8     jhigh 
    252  1.13.2.1    martin 	n = snprintf(salt, saltsiz, "$%s$v=%d$%s$",
    253       1.8     jhigh 		argon2_type2string(atype,0), ARGON2_VERSION_NUMBER, buf);
    254       1.8     jhigh 
    255       1.8     jhigh 	if ((size_t)n + 16 >= saltsiz) {
    256       1.8     jhigh 		return 0;
    257       1.8     jhigh 	}
    258       1.8     jhigh 
    259      1.11       nia 	__crypt_tobase64(&salt[n], arc4random(), 4);
    260      1.11       nia 	__crypt_tobase64(&salt[n + 4], arc4random(), 4);
    261      1.11       nia 	__crypt_tobase64(&salt[n + 8], arc4random(), 4);
    262      1.11       nia 	__crypt_tobase64(&salt[n + 12], arc4random(), 4);
    263       1.8     jhigh 
    264       1.8     jhigh 	salt[n + 16] = '$';
    265       1.8     jhigh 	salt[n + 17] = '\0';
    266       1.8     jhigh 
    267       1.8     jhigh 	return 0;
    268       1.8     jhigh }
    269       1.8     jhigh 
    270       1.8     jhigh /* argon2 variant-specific hooks to generic */
    271      1.12       nia crypt_private int
    272       1.8     jhigh __gensalt_argon2id(char *salt, size_t saltsiz, const char *option)
    273       1.8     jhigh {
    274       1.8     jhigh 	return __gensalt_argon2(salt, saltsiz, option, Argon2_id);
    275       1.8     jhigh }
    276       1.8     jhigh 
    277      1.12       nia crypt_private int
    278       1.8     jhigh __gensalt_argon2i(char *salt, size_t saltsiz, const char *option)
    279       1.8     jhigh {
    280       1.8     jhigh 	return __gensalt_argon2(salt, saltsiz, option, Argon2_i);
    281       1.8     jhigh }
    282       1.8     jhigh 
    283      1.12       nia crypt_private int
    284       1.8     jhigh __gensalt_argon2d(char *salt, size_t saltsiz, const char *option)
    285       1.8     jhigh {
    286       1.8     jhigh 	return __gensalt_argon2(salt, saltsiz, option, Argon2_d);
    287       1.8     jhigh }
    288       1.8     jhigh 
    289       1.8     jhigh #endif /* HAVE_ARGON2 */
    290       1.8     jhigh 
    291       1.8     jhigh 
    292       1.1  christos int
    293       1.4  christos pw_gensalt(char *salt, size_t saltlen, const char *type, const char *option)
    294       1.1  christos {
    295       1.5  christos 	const struct pw_salt *sp;
    296       1.5  christos 
    297       1.1  christos 	for (sp = salts; sp->name; sp++)
    298       1.4  christos 		if (strcmp(sp->name, type) == 0)
    299       1.4  christos 			return (*sp->gensalt)(salt, saltlen, option);
    300       1.1  christos 
    301       1.1  christos 	errno = EINVAL;
    302       1.1  christos 	return -1;
    303       1.1  christos }
    304