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