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