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