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