1 1.6 nia /* $NetBSD: t_crypt.c,v 1.6 2021/10/12 13:24:21 nia Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.2 christos * This version is derived from the original implementation of FreeSec 5 1.2 christos * (release 1.1) by David Burren. I've reviewed the changes made in 6 1.2 christos * OpenBSD (as of 2.7) and modified the original code in a similar way 7 1.2 christos * where applicable. I've also made it reentrant and made a number of 8 1.2 christos * other changes. 9 1.2 christos * - Solar Designer <solar at openwall.com> 10 1.2 christos */ 11 1.2 christos 12 1.2 christos /* 13 1.2 christos * FreeSec: libcrypt for NetBSD 14 1.2 christos * 15 1.2 christos * Copyright (c) 1994 David Burren 16 1.2 christos * All rights reserved. 17 1.2 christos * 18 1.1 christos * Redistribution and use in source and binary forms, with or without 19 1.2 christos * modification, are permitted provided that the following conditions 20 1.2 christos * are met: 21 1.2 christos * 1. Redistributions of source code must retain the above copyright 22 1.2 christos * notice, this list of conditions and the following disclaimer. 23 1.2 christos * 2. Redistributions in binary form must reproduce the above copyright 24 1.2 christos * notice, this list of conditions and the following disclaimer in the 25 1.2 christos * documentation and/or other materials provided with the distribution. 26 1.2 christos * 3. Neither the name of the author nor the names of other contributors 27 1.2 christos * may be used to endorse or promote products derived from this software 28 1.2 christos * without specific prior written permission. 29 1.2 christos * 30 1.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 31 1.2 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 1.2 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 1.2 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 34 1.2 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 1.2 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 1.2 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 1.2 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 1.1 christos * SUCH DAMAGE. 41 1.2 christos * 42 1.2 christos * $Owl: Owl/packages/glibc/crypt_freesec.c,v 1.6 2010/02/20 14:45:06 solar Exp $ 43 1.2 christos * Id: crypt.c,v 1.15 1994/09/13 04:58:49 davidb Exp 44 1.2 christos * 45 1.2 christos * This is an original implementation of the DES and the crypt(3) interfaces 46 1.2 christos * by David Burren <davidb at werj.com.au>. 47 1.2 christos * 48 1.2 christos * An excellent reference on the underlying algorithm (and related 49 1.2 christos * algorithms) is: 50 1.2 christos * 51 1.2 christos * B. Schneier, Applied Cryptography: protocols, algorithms, 52 1.2 christos * and source code in C, John Wiley & Sons, 1994. 53 1.2 christos * 54 1.2 christos * Note that in that book's description of DES the lookups for the initial, 55 1.2 christos * pbox, and final permutations are inverted (this has been brought to the 56 1.2 christos * attention of the author). A list of errata for this book has been 57 1.2 christos * posted to the sci.crypt newsgroup by the author and is available for FTP. 58 1.2 christos * 59 1.2 christos * ARCHITECTURE ASSUMPTIONS: 60 1.2 christos * This code used to have some nasty ones, but these have been removed 61 1.2 christos * by now. The code requires a 32-bit integer type, though. 62 1.1 christos */ 63 1.2 christos #include <sys/cdefs.h> 64 1.6 nia __RCSID("$NetBSD: t_crypt.c,v 1.6 2021/10/12 13:24:21 nia Exp $"); 65 1.1 christos 66 1.1 christos #include <atf-c.h> 67 1.1 christos #include <stdio.h> 68 1.1 christos #include <string.h> 69 1.1 christos #include <stdlib.h> 70 1.1 christos #include <unistd.h> 71 1.1 christos 72 1.1 christos static const struct { 73 1.1 christos const char *hash; 74 1.1 christos const char *pw; 75 1.1 christos } tests[] = { 76 1.1 christos /* "new"-style */ 77 1.1 christos /* 0 */ { "_J9..CCCCXBrJUJV154M", "U*U*U*U*" }, 78 1.1 christos /* 1 */ { "_J9..CCCCXUhOBTXzaiE", "U*U***U" }, 79 1.1 christos /* 2 */ { "_J9..CCCC4gQ.mB/PffM", "U*U***U*" }, 80 1.1 christos /* 3 */ { "_J9..XXXXvlzQGqpPPdk", "*U*U*U*U" }, 81 1.1 christos /* 4 */ { "_J9..XXXXsqM/YSSP..Y", "*U*U*U*U*" }, 82 1.1 christos /* 5 */ { "_J9..XXXXVL7qJCnku0I", "*U*U*U*U*U*U*U*U" }, 83 1.1 christos /* 6 */ { "_J9..XXXXAj8cFbP5scI", "*U*U*U*U*U*U*U*U*" }, 84 1.1 christos /* 7 */ { "_J9..SDizh.vll5VED9g", "ab1234567" }, 85 1.1 christos /* 8 */ { "_J9..SDizRjWQ/zePPHc", "cr1234567" }, 86 1.1 christos /* 9 */ { "_J9..SDizxmRI1GjnQuE", "zxyDPWgydbQjgq" }, 87 1.1 christos /* 10 */ { "_K9..SaltNrQgIYUAeoY", "726 even" }, 88 1.1 christos /* 11 */ { "_J9..SDSD5YGyRCr4W4c", "" }, 89 1.1 christos /* "old"-style, valid salts */ 90 1.1 christos /* 12 */ { "CCNf8Sbh3HDfQ", "U*U*U*U*" }, 91 1.1 christos /* 13 */ { "CCX.K.MFy4Ois", "U*U***U" }, 92 1.1 christos /* 14 */ { "CC4rMpbg9AMZ.", "U*U***U*" }, 93 1.1 christos /* 15 */ { "XXxzOu6maQKqQ", "*U*U*U*U" }, 94 1.1 christos /* 16 */ { "SDbsugeBiC58A", "" }, 95 1.1 christos /* 17 */ { "./xZjzHv5vzVE", "password" }, 96 1.1 christos /* 18 */ { "0A2hXM1rXbYgo", "password" }, 97 1.1 christos /* 19 */ { "A9RXdR23Y.cY6", "password" }, 98 1.1 christos /* 20 */ { "ZziFATVXHo2.6", "password" }, 99 1.1 christos /* 21 */ { "zZDDIZ0NOlPzw", "password" }, 100 1.1 christos /* "old"-style, "reasonable" invalid salts, UFC-crypt behavior expected */ 101 1.1 christos /* 22 */ { "\001\002wyd0KZo65Jo", "password" }, 102 1.1 christos /* 23 */ { "a_C10Dk/ExaG.", "password" }, 103 1.1 christos /* 24 */ { "~\377.5OTsRVjwLo", "password" }, 104 1.1 christos /* The below are erroneous inputs, so NULL return is expected/required */ 105 1.1 christos /* 25 */ { "", "" }, /* no salt */ 106 1.1 christos /* 26 */ { " ", "" }, /* setting string is too short */ 107 1.1 christos /* 27 */ { "a:", "" }, /* unsafe character */ 108 1.1 christos /* 28 */ { "\na", "" }, /* unsafe character */ 109 1.1 christos /* 29 */ { "_/......", "" }, /* setting string is too short for its type */ 110 1.1 christos /* 30 */ { "_........", "" }, /* zero iteration count */ 111 1.1 christos /* 31 */ { "_/!......", "" }, /* invalid character in count */ 112 1.1 christos /* 32 */ { "_/......!", "" }, /* invalid character in salt */ 113 1.4 nia #if defined(HAVE_ARGON2) 114 1.4 nia /* Argon2i version number 16 */ 115 1.5 nia /* 33 */ { "$argon2i$v=16$m=256,t=2,p=1$c29tZXNhbHQ$/U3YPXYsSb3q9XxHvc0MLxur+GP960kN9j7emXX8zwY", "password" }, 116 1.5 nia /* 34 */ { "$argon2i$v=16$m=65536,t=1,p=1$c29tZXNhbHQ$gWMFUrjzsfSM2xmSxMZ4ZD1JCytetP9sSzQ4tWIXJLI", "password" }, 117 1.5 nia /* 35 */ { "$argon2i$v=16$m=65536,t=2,p=1$ZGlmZnNhbHQ$eaEDuQ/orvhXDLMfyLIiWXeJFvgza3vaw4kladTxxJc", "password" }, 118 1.5 nia /* 36 */ { "$argon2i$v=16$m=65536,t=2,p=1$c29tZXNhbHQ$6ckCB0tnVFMaOgvlGeW69ASzDOabPwGsO/ISKZYBCaM", "differentpassword" }, 119 1.4 nia /* Argon2i version number 19 */ 120 1.5 nia /* 37 */ { "$argon2i$v=19$m=256,t=2,p=1$c29tZXNhbHQ$iekCn0Y3spW+sCcFanM2xBT63UP2sghkUoHLIUpWRS8", "password" }, 121 1.5 nia /* 38 */ { "$argon2i$v=19$m=65536,t=1,p=1$c29tZXNhbHQ$0WgHXE2YXhPr6uVgz4uUw7XYoWxRkWtvSsLaOsEbvs8", "password" }, 122 1.5 nia /* 39 */ { "$argon2i$v=19$m=65536,t=2,p=1$ZGlmZnNhbHQ$sDV8zPvvkfOGCw26RHsjSMvv7K2vmQq/6cxAcmxSEnE", "password" }, 123 1.5 nia /* 40 */ { "$argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$FK6NoBr+qHAMI1jc73xTWNkCEoK9iGY6RWL1n7dNIu4", "differentpassword" }, 124 1.4 nia /* Argon2id version number 19 */ 125 1.5 nia /* 41 */ { "$argon2id$v=19$m=256,t=2,p=2$c29tZXNhbHQ$bQk8UB/VmZZF4Oo79iDXuL5/0ttZwg2f/5U52iv1cDc", "password" }, 126 1.5 nia /* 42 */ { "$argon2id$v=19$m=65536,t=4,p=1$c29tZXNhbHQ$kCXUjmjvc5XMqQedpMTsOv+zyJEf5PhtGiUghW9jFyw", "password" }, 127 1.5 nia /* 43 */ { "$argon2id$v=19$m=65536,t=2,p=1$ZGlmZnNhbHQ$vfMrBczELrFdWP0ZsfhWsRPaHppYdP3MVEMIVlqoFBw", "password" }, 128 1.5 nia /* 44 */ { "$argon2id$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$C4TWUs9rDEvq7w3+J4umqA32aWKB1+DSiRuBfYxFj94", "differentpassword" }, 129 1.4 nia #endif 130 1.4 nia /* 45 */ { NULL, NULL } 131 1.1 christos }; 132 1.1 christos 133 1.1 christos ATF_TC(crypt_salts); 134 1.1 christos 135 1.1 christos ATF_TC_HEAD(crypt_salts, tc) 136 1.1 christos { 137 1.1 christos 138 1.1 christos atf_tc_set_md_var(tc, "descr", "crypt(3) salt consistency checks"); 139 1.1 christos } 140 1.1 christos 141 1.1 christos ATF_TC_BODY(crypt_salts, tc) 142 1.1 christos { 143 1.1 christos for (size_t i = 0; tests[i].hash; i++) { 144 1.1 christos char *hash = crypt(tests[i].pw, tests[i].hash); 145 1.3 christos if (!hash) { 146 1.3 christos ATF_CHECK_MSG(0, "Test %zu NULL\n", i); 147 1.3 christos continue; 148 1.3 christos } 149 1.3 christos if (strcmp(hash, "*0") == 0 && strlen(tests[i].hash) < 13) 150 1.1 christos continue; /* expected failure */ 151 1.3 christos if (strcmp(hash, tests[i].hash)) 152 1.1 christos ATF_CHECK_MSG(0, "Test %zu %s != %s\n", 153 1.1 christos i, hash, tests[i].hash); 154 1.1 christos } 155 1.1 christos } 156 1.1 christos 157 1.1 christos ATF_TP_ADD_TCS(tp) 158 1.1 christos { 159 1.1 christos 160 1.1 christos ATF_TP_ADD_TC(tp, crypt_salts); 161 1.1 christos return atf_no_error(); 162 1.1 christos } 163