1272343Sngie/* $NetBSD: t_crypt.c,v 1.3 2011/12/28 22:07:40 christos Exp $ */
2272343Sngie
3272343Sngie/*
4272343Sngie * This version is derived from the original implementation of FreeSec
5272343Sngie * (release 1.1) by David Burren.  I've reviewed the changes made in
6272343Sngie * OpenBSD (as of 2.7) and modified the original code in a similar way
7272343Sngie * where applicable.  I've also made it reentrant and made a number of
8272343Sngie * other changes.
9272343Sngie * - Solar Designer <solar at openwall.com>
10272343Sngie */
11272343Sngie
12272343Sngie/*
13272343Sngie * FreeSec: libcrypt for NetBSD
14272343Sngie *
15272343Sngie * Copyright (c) 1994 David Burren
16272343Sngie * All rights reserved.
17272343Sngie *
18272343Sngie * Redistribution and use in source and binary forms, with or without
19272343Sngie * modification, are permitted provided that the following conditions
20272343Sngie * are met:
21272343Sngie * 1. Redistributions of source code must retain the above copyright
22272343Sngie *    notice, this list of conditions and the following disclaimer.
23272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
24272343Sngie *    notice, this list of conditions and the following disclaimer in the
25272343Sngie *    documentation and/or other materials provided with the distribution.
26272343Sngie * 3. Neither the name of the author nor the names of other contributors
27272343Sngie *    may be used to endorse or promote products derived from this software
28272343Sngie *    without specific prior written permission.
29272343Sngie *
30272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
31272343Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32272343Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33272343Sngie * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
34272343Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36272343Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37272343Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38272343Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39272343Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40272343Sngie * SUCH DAMAGE.
41272343Sngie *
42272343Sngie *	$Owl: Owl/packages/glibc/crypt_freesec.c,v 1.6 2010/02/20 14:45:06 solar Exp $
43272343Sngie *	Id: crypt.c,v 1.15 1994/09/13 04:58:49 davidb Exp
44272343Sngie *
45272343Sngie * This is an original implementation of the DES and the crypt(3) interfaces
46272343Sngie * by David Burren <davidb at werj.com.au>.
47272343Sngie *
48272343Sngie * An excellent reference on the underlying algorithm (and related
49272343Sngie * algorithms) is:
50272343Sngie *
51272343Sngie *	B. Schneier, Applied Cryptography: protocols, algorithms,
52272343Sngie *	and source code in C, John Wiley & Sons, 1994.
53272343Sngie *
54272343Sngie * Note that in that book's description of DES the lookups for the initial,
55272343Sngie * pbox, and final permutations are inverted (this has been brought to the
56272343Sngie * attention of the author).  A list of errata for this book has been
57272343Sngie * posted to the sci.crypt newsgroup by the author and is available for FTP.
58272343Sngie *
59272343Sngie * ARCHITECTURE ASSUMPTIONS:
60272343Sngie *	This code used to have some nasty ones, but these have been removed
61272343Sngie *	by now.	 The code requires a 32-bit integer type, though.
62272343Sngie */
63272343Sngie#include <sys/cdefs.h>
64272343Sngie__RCSID("$NetBSD: t_crypt.c,v 1.3 2011/12/28 22:07:40 christos Exp $");
65272343Sngie
66272343Sngie#include <atf-c.h>
67272343Sngie#include <stdio.h>
68272343Sngie#include <string.h>
69272343Sngie#include <stdlib.h>
70272343Sngie#include <unistd.h>
71272343Sngie
72272343Sngiestatic const struct {
73272343Sngie	const char *hash;
74272343Sngie	const char *pw;
75272343Sngie} tests[] = {
76272343Sngie/* "new"-style */
77272343Sngie/*  0 */	{ "_J9..CCCCXBrJUJV154M", "U*U*U*U*" },
78272343Sngie/*  1 */	{ "_J9..CCCCXUhOBTXzaiE", "U*U***U" },
79272343Sngie/*  2 */	{ "_J9..CCCC4gQ.mB/PffM", "U*U***U*" },
80272343Sngie/*  3 */	{ "_J9..XXXXvlzQGqpPPdk", "*U*U*U*U" },
81272343Sngie/*  4 */	{ "_J9..XXXXsqM/YSSP..Y", "*U*U*U*U*" },
82272343Sngie/*  5 */	{ "_J9..XXXXVL7qJCnku0I", "*U*U*U*U*U*U*U*U" },
83272343Sngie/*  6 */	{ "_J9..XXXXAj8cFbP5scI", "*U*U*U*U*U*U*U*U*" },
84272343Sngie/*  7 */	{ "_J9..SDizh.vll5VED9g", "ab1234567" },
85272343Sngie/*  8 */	{ "_J9..SDizRjWQ/zePPHc", "cr1234567" },
86272343Sngie/*  9 */	{ "_J9..SDizxmRI1GjnQuE", "zxyDPWgydbQjgq" },
87272343Sngie/* 10 */	{ "_K9..SaltNrQgIYUAeoY", "726 even" },
88272343Sngie/* 11 */	{ "_J9..SDSD5YGyRCr4W4c", "" },
89272343Sngie/* "old"-style, valid salts */
90272343Sngie/* 12 */	{ "CCNf8Sbh3HDfQ", "U*U*U*U*" },
91272343Sngie/* 13 */	{ "CCX.K.MFy4Ois", "U*U***U" },
92272343Sngie/* 14 */	{ "CC4rMpbg9AMZ.", "U*U***U*" },
93272343Sngie/* 15 */	{ "XXxzOu6maQKqQ", "*U*U*U*U" },
94272343Sngie/* 16 */	{ "SDbsugeBiC58A", "" },
95272343Sngie/* 17 */	{ "./xZjzHv5vzVE", "password" },
96272343Sngie/* 18 */	{ "0A2hXM1rXbYgo", "password" },
97272343Sngie/* 19 */	{ "A9RXdR23Y.cY6", "password" },
98272343Sngie/* 20 */	{ "ZziFATVXHo2.6", "password" },
99272343Sngie/* 21 */	{ "zZDDIZ0NOlPzw", "password" },
100272343Sngie/* "old"-style, "reasonable" invalid salts, UFC-crypt behavior expected */
101272343Sngie/* 22 */	{ "\001\002wyd0KZo65Jo", "password" },
102272343Sngie/* 23 */	{ "a_C10Dk/ExaG.", "password" },
103272343Sngie/* 24 */	{ "~\377.5OTsRVjwLo", "password" },
104272343Sngie/* The below are erroneous inputs, so NULL return is expected/required */
105272343Sngie/* 25 */	{ "", "" }, /* no salt */
106272343Sngie/* 26 */	{ " ", "" }, /* setting string is too short */
107272343Sngie/* 27 */	{ "a:", "" }, /* unsafe character */
108272343Sngie/* 28 */	{ "\na", "" }, /* unsafe character */
109272343Sngie/* 29 */	{ "_/......", "" }, /* setting string is too short for its type */
110272343Sngie/* 30 */	{ "_........", "" }, /* zero iteration count */
111272343Sngie/* 31 */	{ "_/!......", "" }, /* invalid character in count */
112272343Sngie/* 32 */	{ "_/......!", "" }, /* invalid character in salt */
113272343Sngie/* 33 */	{ NULL, NULL }
114272343Sngie};
115272343Sngie
116272343SngieATF_TC(crypt_salts);
117272343Sngie
118272343SngieATF_TC_HEAD(crypt_salts, tc)
119272343Sngie{
120272343Sngie
121272343Sngie	atf_tc_set_md_var(tc, "descr", "crypt(3) salt consistency checks");
122272343Sngie}
123272343Sngie
124272343SngieATF_TC_BODY(crypt_salts, tc)
125272343Sngie{
126272343Sngie	for (size_t i = 0; tests[i].hash; i++) {
127291615Srodrigc		char *hash = crypt(tests[i].pw, tests[i].hash);
128290907Sngie#if defined(__FreeBSD__)
129291615Srodrigc		if (i >= 22 && i != 24 && i != 25)
130290907Sngie			atf_tc_expect_fail("Old-style/bad inputs fail on FreeBSD");
131291615Srodrigc		else
132291615Srodrigc			atf_tc_expect_pass();
133290907Sngie#endif
134272343Sngie		if (!hash) {
135272343Sngie			ATF_CHECK_MSG(0, "Test %zu NULL\n", i);
136272343Sngie			continue;
137272343Sngie		}
138272343Sngie		if (strcmp(hash, "*0") == 0 && strlen(tests[i].hash) < 13)
139272343Sngie			continue; /* expected failure */
140272343Sngie		if (strcmp(hash, tests[i].hash))
141272343Sngie			ATF_CHECK_MSG(0, "Test %zu %s != %s\n",
142272343Sngie			    i, hash, tests[i].hash);
143272343Sngie	}
144272343Sngie}
145272343Sngie
146272343SngieATF_TP_ADD_TCS(tp)
147272343Sngie{
148272343Sngie
149272343Sngie	ATF_TP_ADD_TC(tp, crypt_salts);
150272343Sngie	return atf_no_error();
151272343Sngie}
152