1 /*- 2 * Copyright (c) 1999 Mark Murray 3 * Copyright (c) 2014 Dag-Erling Smørgrav 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY MARK MURRAY AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL MARK MURRAY OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/types.h> 32 33 #include <libutil.h> 34 #include <string.h> 35 #include <unistd.h> 36 37 #include "crypt.h" 38 39 /* 40 * List of supported crypt(3) formats. The first element in the list will 41 * be the default. 42 */ 43 static const struct crypt_format { 44 const char *const name; 45 char *(*const func)(const char *, const char *); 46 const char *const magic; 47 } crypt_formats[] = { 48 /* default format */ 49 { "sha512", crypt_sha512, "$6$" }, 50 51 /* other supported formats */ 52 { "md5", crypt_md5, "$1$" }, 53 #ifdef HAS_BLOWFISH 54 { "blf", crypt_blowfish, "$2" }, 55 #endif 56 { "nth", crypt_nthash, "$3$" }, 57 { "sha256", crypt_sha256, "$5$" }, 58 #ifdef HAS_DES 59 { "des", crypt_des, "_" }, 60 #endif 61 62 /* sentinel */ 63 { NULL, NULL, NULL } 64 }; 65 66 static const struct crypt_format *crypt_format = &crypt_formats[0]; 67 68 #define DES_SALT_ALPHABET \ 69 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 70 71 /* 72 * Returns the name of the currently selected format. 73 */ 74 const char * 75 crypt_get_format(void) 76 { 77 78 return (crypt_format->name); 79 } 80 81 /* 82 * Selects the format to use for subsequent crypt(3) invocations. 83 */ 84 int 85 crypt_set_format(const char *format) 86 { 87 const struct crypt_format *cf; 88 89 for (cf = crypt_formats; cf->name != NULL; ++cf) { 90 if (strcasecmp(cf->name, format) == 0) { 91 crypt_format = cf; 92 return (1); 93 } 94 } 95 return (0); 96 } 97 98 /* 99 * Hash the given password with the given salt. If the salt begins with a 100 * magic string (e.g. "$6$" for sha512), the corresponding format is used; 101 * otherwise, the currently selected format is used. 102 */ 103 char * 104 crypt(const char *passwd, const char *salt) 105 { 106 const struct crypt_format *cf; 107 108 for (cf = crypt_formats; cf->name != NULL; ++cf) 109 if (cf->magic != NULL && strstr(salt, cf->magic) == salt) 110 return (cf->func(passwd, salt)); 111 #ifdef HAS_DES 112 if (strlen(salt) == 13 && strspn(salt, DES_SALT_ALPHABET) == 13) 113 return (crypt_des(passwd, salt)); 114 #endif 115 return (crypt_format->func(passwd, salt)); 116 } 117