/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 1999 Mark Murray4* Copyright (c) 2014 Dag-Erling Smørgrav5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY MARK MURRAY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL MARK MURRAY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <sys/types.h>3031#include <string.h>32#include <unistd.h>3334#include "crypt.h"3536/*37* List of supported crypt(3) formats.38*39* The default algorithm is the last entry in the list (second-to-last40* array element since the last is a sentinel). The reason for placing41* the default last rather than first is that DES needs to be at the42* bottom for the algorithm guessing logic in crypt(3) to work correctly,43* and it needs to be the default for backward compatibility.44*/45static const struct crypt_format {46const char *name;47int (*func)(const char *, const char *, char *);48const char *magic;49} crypt_formats[] = {50{ "md5", crypt_md5, "$1$" },51#ifdef HAS_BLOWFISH52{ "blf", crypt_blowfish, "$2" },53#endif54{ "nth", crypt_nthash, "$3$" },55{ "sha256", crypt_sha256, "$5$" },56{ "sha512", crypt_sha512, "$6$" },57#ifdef HAS_DES58{ "des", crypt_des, "_" },59#endif6061/* sentinel */62{ NULL, NULL, NULL }63};6465static const struct crypt_format *crypt_format =66&crypt_formats[(sizeof crypt_formats / sizeof *crypt_formats) - 2];6768#define DES_SALT_ALPHABET \69"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"7071/*72* Returns the name of the currently selected format.73*/74const char *75crypt_get_format(void)76{7778return (crypt_format->name);79}8081/*82* Selects the format to use for subsequent crypt(3) invocations.83*/84int85crypt_set_format(const char *format)86{87const struct crypt_format *cf;8889for (cf = crypt_formats; cf->name != NULL; ++cf) {90if (strcasecmp(cf->name, format) == 0) {91crypt_format = cf;92return (1);93}94}95return (0);96}9798/*99* Hash the given password with the given salt. If the salt begins with a100* magic string (e.g. "$6$" for sha512), the corresponding format is used;101* otherwise, the currently selected format is used.102*/103char *104crypt_r(const char *passwd, const char *salt, struct crypt_data *data)105{106const struct crypt_format *cf;107int (*func)(const char *, const char *, char *);108#ifdef HAS_DES109int len;110#endif111112for (cf = crypt_formats; cf->name != NULL; ++cf)113if (cf->magic != NULL && strstr(salt, cf->magic) == salt) {114func = cf->func;115goto match;116}117#ifdef HAS_DES118len = strlen(salt);119if ((len == 13 || len == 2) && strspn(salt, DES_SALT_ALPHABET) == len) {120func = crypt_des;121goto match;122}123#endif124func = crypt_format->func;125match:126if (func(passwd, salt, data->__buf) != 0)127return (NULL);128return (data->__buf);129}130131char *132crypt(const char *passwd, const char *salt)133{134static struct crypt_data data;135136return (crypt_r(passwd, salt, &data));137}138139140