1*0cd13cbfSwyllys /*
2*0cd13cbfSwyllys  * CDDL HEADER START
3*0cd13cbfSwyllys  *
4*0cd13cbfSwyllys  * The contents of this file are subject to the terms of the
5*0cd13cbfSwyllys  * Common Development and Distribution License (the "License").
6*0cd13cbfSwyllys  * You may not use this file except in compliance with the License.
7*0cd13cbfSwyllys  *
8*0cd13cbfSwyllys  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*0cd13cbfSwyllys  * or http://www.opensolaris.org/os/licensing.
10*0cd13cbfSwyllys  * See the License for the specific language governing permissions
11*0cd13cbfSwyllys  * and limitations under the License.
12*0cd13cbfSwyllys  *
13*0cd13cbfSwyllys  * When distributing Covered Code, include this CDDL HEADER in each
14*0cd13cbfSwyllys  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*0cd13cbfSwyllys  * If applicable, add the following below this CDDL HEADER, with the
16*0cd13cbfSwyllys  * fields enclosed by brackets "[]" replaced with your own identifying
17*0cd13cbfSwyllys  * information: Portions Copyright [yyyy] [name of copyright owner]
18*0cd13cbfSwyllys  *
19*0cd13cbfSwyllys  * CDDL HEADER END
20*0cd13cbfSwyllys  *
21*0cd13cbfSwyllys  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
22*0cd13cbfSwyllys  * Use is subject to license terms.
23*0cd13cbfSwyllys  */
24*0cd13cbfSwyllys 
25*0cd13cbfSwyllys #pragma ident	"%Z%%M%	%I%	%E% SMI"
26*0cd13cbfSwyllys 
27*0cd13cbfSwyllys #include <crypt.h>
28*0cd13cbfSwyllys #include <string.h>
29*0cd13cbfSwyllys 
30*0cd13cbfSwyllys #ifdef CRYPT_SHA256
31*0cd13cbfSwyllys static const struct
32*0cd13cbfSwyllys {
33*0cd13cbfSwyllys 	const char *salt;
34*0cd13cbfSwyllys 	const char *input;
35*0cd13cbfSwyllys 	const char *expected;
36*0cd13cbfSwyllys } tests2[] = {
37*0cd13cbfSwyllys 	{ "$5$saltstring", "Hello world!",
38*0cd13cbfSwyllys 	    "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5" },
39*0cd13cbfSwyllys 	{ "$5$rounds=10000$saltstringsaltstring", "Hello world!",
40*0cd13cbfSwyllys 	    "$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBA"
41*0cd13cbfSwyllys 	    "wqFMz2.opqey6IcA" },
42*0cd13cbfSwyllys 	{ "$5$rounds=5000$toolongsaltstring", "This is just a test",
43*0cd13cbfSwyllys 	    "$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07g"
44*0cd13cbfSwyllys 	    "uHPvOW8mGRcvxa5" },
45*0cd13cbfSwyllys 	{ "$5$rounds=1400$anotherlongsaltstring",
46*0cd13cbfSwyllys 	    "a very much longer text to encrypt.  This one even stretches"
47*0cd13cbfSwyllys 	    " over morethan one line.",
48*0cd13cbfSwyllys 	    "$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIU"
49*0cd13cbfSwyllys 	    "nzyxf12oP84Bnq1" },
50*0cd13cbfSwyllys 	{ "$5$rounds=77777$short",
51*0cd13cbfSwyllys 	    "we have a short salt string but not a short password",
52*0cd13cbfSwyllys 	    "$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0"
53*0cd13cbfSwyllys 	    "KQRd/" },
54*0cd13cbfSwyllys 	{ "$5$rounds=123456$asaltof16chars..", "a short string",
55*0cd13cbfSwyllys 	    "$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2"
56*0cd13cbfSwyllys 	    "jxPyzV/cZKmF/wJvD" },
57*0cd13cbfSwyllys 	{ "$5$rounds=10$roundstoolow", "the minimum number is still observed",
58*0cd13cbfSwyllys 	    "$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY"
59*0cd13cbfSwyllys 	    "9l/gL972bIC" },
60*0cd13cbfSwyllys };
61*0cd13cbfSwyllys #elif CRYPT_SHA512
62*0cd13cbfSwyllys static const struct
63*0cd13cbfSwyllys {
64*0cd13cbfSwyllys 	const char *salt;
65*0cd13cbfSwyllys 	const char *input;
66*0cd13cbfSwyllys 	const char *expected;
67*0cd13cbfSwyllys } tests2[] = {
68*0cd13cbfSwyllys 	{ "$6$saltstring", "Hello world!",
69*0cd13cbfSwyllys 	    "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnI"
70*0cd13cbfSwyllys 	    "FNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1" },
71*0cd13cbfSwyllys 	{ "$6$rounds=10000$saltstringsaltstring", "Hello world!",
72*0cd13cbfSwyllys 	    "$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3"
73*0cd13cbfSwyllys 	    "Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v." },
74*0cd13cbfSwyllys 	{ "$6$rounds=5000$toolongsaltstring", "This is just a test",
75*0cd13cbfSwyllys 	    "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxG"
76*0cd13cbfSwyllys 	    "oNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0" },
77*0cd13cbfSwyllys 	{ "$6$rounds=1400$anotherlongsaltstring",
78*0cd13cbfSwyllys 	    "a very much longer text to encrypt.  This one even stretches "
79*0cd13cbfSwyllys 	    "over morethan one line.",
80*0cd13cbfSwyllys 	    "$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/p"
81*0cd13cbfSwyllys 	    "Qs.wPvMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1" },
82*0cd13cbfSwyllys 	{ "$6$rounds=77777$short",
83*0cd13cbfSwyllys 	    "we have a short salt string but not a short password",
84*0cd13cbfSwyllys 	    "$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXb"
85*0cd13cbfSwyllys 	    "kvr0gge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0" },
86*0cd13cbfSwyllys 	{ "$6$rounds=123456$asaltof16chars..", "a short string",
87*0cd13cbfSwyllys 	    "$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ"
88*0cd13cbfSwyllys 	    "4oPwcelCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1" },
89*0cd13cbfSwyllys 	{ "$6$rounds=10$roundstoolow", "the minimum number is still observed",
90*0cd13cbfSwyllys 	    "$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50Y"
91*0cd13cbfSwyllys 	    "hH1xhLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX." },
92*0cd13cbfSwyllys };
93*0cd13cbfSwyllys 
94*0cd13cbfSwyllys #else
95*0cd13cbfSwyllys #error "One of CRYPT_SHA256 or CRYPT_SHA512 must be defined"
96*0cd13cbfSwyllys #endif
97*0cd13cbfSwyllys 
98*0cd13cbfSwyllys #define	ntests2 (sizeof (tests2) / sizeof (tests2[0]))
99*0cd13cbfSwyllys 
100*0cd13cbfSwyllys int
101*0cd13cbfSwyllys main(int argc, char *argv[])
102*0cd13cbfSwyllys {
103*0cd13cbfSwyllys 	int cnt;
104*0cd13cbfSwyllys 	int failures = 0;
105*0cd13cbfSwyllys 	char ctbuffer[CRYPT_MAXCIPHERTEXTLEN];
106*0cd13cbfSwyllys 	size_t ctbufflen = sizeof (ctbuffer);
107*0cd13cbfSwyllys 
108*0cd13cbfSwyllys #ifdef CRYPT_SHA256
109*0cd13cbfSwyllys 	fprintf(stderr, "CRYPT_SHA256 ");
110*0cd13cbfSwyllys #elif CRYPT_SHA512
111*0cd13cbfSwyllys 	fprintf(stderr, "CRYPT_SHA512 ");
112*0cd13cbfSwyllys #endif
113*0cd13cbfSwyllys 	fprintf(stderr, "CRYPT_MAXCIPHERTEXTLEN = %d\n",
114*0cd13cbfSwyllys 	    CRYPT_MAXCIPHERTEXTLEN);
115*0cd13cbfSwyllys 	for (cnt = 0; cnt < ntests2; ++cnt) {
116*0cd13cbfSwyllys 		char *cp;
117*0cd13cbfSwyllys 		fprintf(stderr, "test %d (outlen=%d):  ", cnt,
118*0cd13cbfSwyllys 		    strlen(tests2[cnt].expected));
119*0cd13cbfSwyllys 		cp = crypt_genhash_impl(ctbuffer, ctbufflen,
120*0cd13cbfSwyllys 		    tests2[cnt].input, tests2[cnt].salt, NULL);
121*0cd13cbfSwyllys 
122*0cd13cbfSwyllys 		if (cp == NULL || (strcmp(cp, tests2[cnt].expected) != 0)) {
123*0cd13cbfSwyllys 			fprintf(stderr,
124*0cd13cbfSwyllys 			    "FAILED\nE(%d): \"%s\"\nG(%d): \"%s\"\n",
125*0cd13cbfSwyllys 			    strlen(tests2[cnt].expected), tests2[cnt].expected,
126*0cd13cbfSwyllys 			    (cp ? strlen(cp) : 0), (cp ? cp : "NULL"));
127*0cd13cbfSwyllys 			failures++;
128*0cd13cbfSwyllys 		} else {
129*0cd13cbfSwyllys 			fprintf(stderr, "OK\n");
130*0cd13cbfSwyllys 		}
131*0cd13cbfSwyllys 	}
132*0cd13cbfSwyllys 
133*0cd13cbfSwyllys 	if (failures == 0) {
134*0cd13cbfSwyllys 		fprintf(stderr, "all tests OK\n");
135*0cd13cbfSwyllys 	} else {
136*0cd13cbfSwyllys 		fprintf(stderr, "%d tests failed\n", failures);
137*0cd13cbfSwyllys 	}
138*0cd13cbfSwyllys 
139*0cd13cbfSwyllys 	return (failures);
140*0cd13cbfSwyllys }
141