157718be8SEnji Cooper /* $NetBSD: t_crypt.c,v 1.3 2011/12/28 22:07:40 christos Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*
457718be8SEnji Cooper * This version is derived from the original implementation of FreeSec
557718be8SEnji Cooper * (release 1.1) by David Burren. I've reviewed the changes made in
657718be8SEnji Cooper * OpenBSD (as of 2.7) and modified the original code in a similar way
757718be8SEnji Cooper * where applicable. I've also made it reentrant and made a number of
857718be8SEnji Cooper * other changes.
957718be8SEnji Cooper * - Solar Designer <solar at openwall.com>
1057718be8SEnji Cooper */
1157718be8SEnji Cooper
1257718be8SEnji Cooper /*
1357718be8SEnji Cooper * FreeSec: libcrypt for NetBSD
1457718be8SEnji Cooper *
1557718be8SEnji Cooper * Copyright (c) 1994 David Burren
1657718be8SEnji Cooper * All rights reserved.
1757718be8SEnji Cooper *
1857718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
1957718be8SEnji Cooper * modification, are permitted provided that the following conditions
2057718be8SEnji Cooper * are met:
2157718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
2257718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
2357718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
2457718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
2557718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
2657718be8SEnji Cooper * 3. Neither the name of the author nor the names of other contributors
2757718be8SEnji Cooper * may be used to endorse or promote products derived from this software
2857718be8SEnji Cooper * without specific prior written permission.
2957718be8SEnji Cooper *
3057718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
3157718be8SEnji Cooper * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3257718be8SEnji Cooper * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3357718be8SEnji Cooper * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3457718be8SEnji Cooper * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3557718be8SEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3657718be8SEnji Cooper * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3757718be8SEnji Cooper * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3857718be8SEnji Cooper * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3957718be8SEnji Cooper * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4057718be8SEnji Cooper * SUCH DAMAGE.
4157718be8SEnji Cooper *
4257718be8SEnji Cooper * $Owl: Owl/packages/glibc/crypt_freesec.c,v 1.6 2010/02/20 14:45:06 solar Exp $
4357718be8SEnji Cooper * Id: crypt.c,v 1.15 1994/09/13 04:58:49 davidb Exp
4457718be8SEnji Cooper *
4557718be8SEnji Cooper * This is an original implementation of the DES and the crypt(3) interfaces
4657718be8SEnji Cooper * by David Burren <davidb at werj.com.au>.
4757718be8SEnji Cooper *
4857718be8SEnji Cooper * An excellent reference on the underlying algorithm (and related
4957718be8SEnji Cooper * algorithms) is:
5057718be8SEnji Cooper *
5157718be8SEnji Cooper * B. Schneier, Applied Cryptography: protocols, algorithms,
5257718be8SEnji Cooper * and source code in C, John Wiley & Sons, 1994.
5357718be8SEnji Cooper *
5457718be8SEnji Cooper * Note that in that book's description of DES the lookups for the initial,
5557718be8SEnji Cooper * pbox, and final permutations are inverted (this has been brought to the
5657718be8SEnji Cooper * attention of the author). A list of errata for this book has been
5757718be8SEnji Cooper * posted to the sci.crypt newsgroup by the author and is available for FTP.
5857718be8SEnji Cooper *
5957718be8SEnji Cooper * ARCHITECTURE ASSUMPTIONS:
6057718be8SEnji Cooper * This code used to have some nasty ones, but these have been removed
6157718be8SEnji Cooper * by now. The code requires a 32-bit integer type, though.
6257718be8SEnji Cooper */
6357718be8SEnji Cooper #include <sys/cdefs.h>
6457718be8SEnji Cooper __RCSID("$NetBSD: t_crypt.c,v 1.3 2011/12/28 22:07:40 christos Exp $");
6557718be8SEnji Cooper
6657718be8SEnji Cooper #include <atf-c.h>
6757718be8SEnji Cooper #include <stdio.h>
6857718be8SEnji Cooper #include <string.h>
6957718be8SEnji Cooper #include <stdlib.h>
7057718be8SEnji Cooper #include <unistd.h>
7157718be8SEnji Cooper
7257718be8SEnji Cooper static const struct {
7357718be8SEnji Cooper const char *hash;
7457718be8SEnji Cooper const char *pw;
7557718be8SEnji Cooper } tests[] = {
7657718be8SEnji Cooper /* "new"-style */
7757718be8SEnji Cooper /* 0 */ { "_J9..CCCCXBrJUJV154M", "U*U*U*U*" },
7857718be8SEnji Cooper /* 1 */ { "_J9..CCCCXUhOBTXzaiE", "U*U***U" },
7957718be8SEnji Cooper /* 2 */ { "_J9..CCCC4gQ.mB/PffM", "U*U***U*" },
8057718be8SEnji Cooper /* 3 */ { "_J9..XXXXvlzQGqpPPdk", "*U*U*U*U" },
8157718be8SEnji Cooper /* 4 */ { "_J9..XXXXsqM/YSSP..Y", "*U*U*U*U*" },
8257718be8SEnji Cooper /* 5 */ { "_J9..XXXXVL7qJCnku0I", "*U*U*U*U*U*U*U*U" },
8357718be8SEnji Cooper /* 6 */ { "_J9..XXXXAj8cFbP5scI", "*U*U*U*U*U*U*U*U*" },
8457718be8SEnji Cooper /* 7 */ { "_J9..SDizh.vll5VED9g", "ab1234567" },
8557718be8SEnji Cooper /* 8 */ { "_J9..SDizRjWQ/zePPHc", "cr1234567" },
8657718be8SEnji Cooper /* 9 */ { "_J9..SDizxmRI1GjnQuE", "zxyDPWgydbQjgq" },
8757718be8SEnji Cooper /* 10 */ { "_K9..SaltNrQgIYUAeoY", "726 even" },
8857718be8SEnji Cooper /* 11 */ { "_J9..SDSD5YGyRCr4W4c", "" },
8957718be8SEnji Cooper /* "old"-style, valid salts */
9057718be8SEnji Cooper /* 12 */ { "CCNf8Sbh3HDfQ", "U*U*U*U*" },
9157718be8SEnji Cooper /* 13 */ { "CCX.K.MFy4Ois", "U*U***U" },
9257718be8SEnji Cooper /* 14 */ { "CC4rMpbg9AMZ.", "U*U***U*" },
9357718be8SEnji Cooper /* 15 */ { "XXxzOu6maQKqQ", "*U*U*U*U" },
9457718be8SEnji Cooper /* 16 */ { "SDbsugeBiC58A", "" },
9557718be8SEnji Cooper /* 17 */ { "./xZjzHv5vzVE", "password" },
9657718be8SEnji Cooper /* 18 */ { "0A2hXM1rXbYgo", "password" },
9757718be8SEnji Cooper /* 19 */ { "A9RXdR23Y.cY6", "password" },
9857718be8SEnji Cooper /* 20 */ { "ZziFATVXHo2.6", "password" },
9957718be8SEnji Cooper /* 21 */ { "zZDDIZ0NOlPzw", "password" },
10057718be8SEnji Cooper /* "old"-style, "reasonable" invalid salts, UFC-crypt behavior expected */
10157718be8SEnji Cooper /* 22 */ { "\001\002wyd0KZo65Jo", "password" },
10257718be8SEnji Cooper /* 23 */ { "a_C10Dk/ExaG.", "password" },
10357718be8SEnji Cooper /* 24 */ { "~\377.5OTsRVjwLo", "password" },
10457718be8SEnji Cooper /* The below are erroneous inputs, so NULL return is expected/required */
10557718be8SEnji Cooper /* 25 */ { "", "" }, /* no salt */
10657718be8SEnji Cooper /* 26 */ { " ", "" }, /* setting string is too short */
10757718be8SEnji Cooper /* 27 */ { "a:", "" }, /* unsafe character */
10857718be8SEnji Cooper /* 28 */ { "\na", "" }, /* unsafe character */
10957718be8SEnji Cooper /* 29 */ { "_/......", "" }, /* setting string is too short for its type */
11057718be8SEnji Cooper /* 30 */ { "_........", "" }, /* zero iteration count */
11157718be8SEnji Cooper /* 31 */ { "_/!......", "" }, /* invalid character in count */
11257718be8SEnji Cooper /* 32 */ { "_/......!", "" }, /* invalid character in salt */
11357718be8SEnji Cooper /* 33 */ { NULL, NULL }
11457718be8SEnji Cooper };
11557718be8SEnji Cooper
11657718be8SEnji Cooper ATF_TC(crypt_salts);
11757718be8SEnji Cooper
ATF_TC_HEAD(crypt_salts,tc)11857718be8SEnji Cooper ATF_TC_HEAD(crypt_salts, tc)
11957718be8SEnji Cooper {
12057718be8SEnji Cooper
12157718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "crypt(3) salt consistency checks");
12257718be8SEnji Cooper }
12357718be8SEnji Cooper
ATF_TC_BODY(crypt_salts,tc)12457718be8SEnji Cooper ATF_TC_BODY(crypt_salts, tc)
12557718be8SEnji Cooper {
12657718be8SEnji Cooper for (size_t i = 0; tests[i].hash; i++) {
12757718be8SEnji Cooper char *hash = crypt(tests[i].pw, tests[i].hash);
128*26144b61SCraig Rodrigues #if defined(__FreeBSD__)
129*26144b61SCraig Rodrigues if (i >= 22 && i != 24 && i != 25)
130*26144b61SCraig Rodrigues atf_tc_expect_fail("Old-style/bad inputs fail on FreeBSD");
131*26144b61SCraig Rodrigues else
132*26144b61SCraig Rodrigues atf_tc_expect_pass();
133*26144b61SCraig Rodrigues #endif
13457718be8SEnji Cooper if (!hash) {
13557718be8SEnji Cooper ATF_CHECK_MSG(0, "Test %zu NULL\n", i);
13657718be8SEnji Cooper continue;
13757718be8SEnji Cooper }
13857718be8SEnji Cooper if (strcmp(hash, "*0") == 0 && strlen(tests[i].hash) < 13)
13957718be8SEnji Cooper continue; /* expected failure */
14057718be8SEnji Cooper if (strcmp(hash, tests[i].hash))
14157718be8SEnji Cooper ATF_CHECK_MSG(0, "Test %zu %s != %s\n",
14257718be8SEnji Cooper i, hash, tests[i].hash);
14357718be8SEnji Cooper }
14457718be8SEnji Cooper }
14557718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)14657718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
14757718be8SEnji Cooper {
14857718be8SEnji Cooper
14957718be8SEnji Cooper ATF_TP_ADD_TC(tp, crypt_salts);
15057718be8SEnji Cooper return atf_no_error();
15157718be8SEnji Cooper }
152