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