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