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 #include <crypt.h>
26*0cd13cbfSwyllys #include <string.h>
27*0cd13cbfSwyllys 
28*0cd13cbfSwyllys #ifdef CRYPT_SHA256
29*0cd13cbfSwyllys static const struct
30*0cd13cbfSwyllys {
31*0cd13cbfSwyllys 	const char *salt;
32*0cd13cbfSwyllys 	const char *input;
33*0cd13cbfSwyllys 	const char *expected;
34*0cd13cbfSwyllys } tests2[] = {
35*0cd13cbfSwyllys 	{ "$5$saltstring", "Hello world!",
36*0cd13cbfSwyllys 	    "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5" },
37*0cd13cbfSwyllys 	{ "$5$rounds=10000$saltstringsaltstring", "Hello world!",
38*0cd13cbfSwyllys 	    "$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBA"
39*0cd13cbfSwyllys 	    "wqFMz2.opqey6IcA" },
40*0cd13cbfSwyllys 	{ "$5$rounds=5000$toolongsaltstring", "This is just a test",
41*0cd13cbfSwyllys 	    "$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07g"
42*0cd13cbfSwyllys 	    "uHPvOW8mGRcvxa5" },
43*0cd13cbfSwyllys 	{ "$5$rounds=1400$anotherlongsaltstring",
44*0cd13cbfSwyllys 	    "a very much longer text to encrypt.  This one even stretches"
45*0cd13cbfSwyllys 	    " over morethan one line.",
46*0cd13cbfSwyllys 	    "$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIU"
47*0cd13cbfSwyllys 	    "nzyxf12oP84Bnq1" },
48*0cd13cbfSwyllys 	{ "$5$rounds=77777$short",
49*0cd13cbfSwyllys 	    "we have a short salt string but not a short password",
50*0cd13cbfSwyllys 	    "$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0"
51*0cd13cbfSwyllys 	    "KQRd/" },
52*0cd13cbfSwyllys 	{ "$5$rounds=123456$asaltof16chars..", "a short string",
53*0cd13cbfSwyllys 	    "$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2"
54*0cd13cbfSwyllys 	    "jxPyzV/cZKmF/wJvD" },
55*0cd13cbfSwyllys 	{ "$5$rounds=10$roundstoolow", "the minimum number is still observed",
56*0cd13cbfSwyllys 	    "$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY"
57*0cd13cbfSwyllys 	    "9l/gL972bIC" },
58*0cd13cbfSwyllys };
59*0cd13cbfSwyllys #elif CRYPT_SHA512
60*0cd13cbfSwyllys static const struct
61*0cd13cbfSwyllys {
62*0cd13cbfSwyllys 	const char *salt;
63*0cd13cbfSwyllys 	const char *input;
64*0cd13cbfSwyllys 	const char *expected;
65*0cd13cbfSwyllys } tests2[] = {
66*0cd13cbfSwyllys 	{ "$6$saltstring", "Hello world!",
67*0cd13cbfSwyllys 	    "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnI"
68*0cd13cbfSwyllys 	    "FNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1" },
69*0cd13cbfSwyllys 	{ "$6$rounds=10000$saltstringsaltstring", "Hello world!",
70*0cd13cbfSwyllys 	    "$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3"
71*0cd13cbfSwyllys 	    "Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v." },
72*0cd13cbfSwyllys 	{ "$6$rounds=5000$toolongsaltstring", "This is just a test",
73*0cd13cbfSwyllys 	    "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxG"
74*0cd13cbfSwyllys 	    "oNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0" },
75*0cd13cbfSwyllys 	{ "$6$rounds=1400$anotherlongsaltstring",
76*0cd13cbfSwyllys 	    "a very much longer text to encrypt.  This one even stretches "
77*0cd13cbfSwyllys 	    "over morethan one line.",
78*0cd13cbfSwyllys 	    "$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/p"
79*0cd13cbfSwyllys 	    "Qs.wPvMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1" },
80*0cd13cbfSwyllys 	{ "$6$rounds=77777$short",
81*0cd13cbfSwyllys 	    "we have a short salt string but not a short password",
82*0cd13cbfSwyllys 	    "$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXb"
83*0cd13cbfSwyllys 	    "kvr0gge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0" },
84*0cd13cbfSwyllys 	{ "$6$rounds=123456$asaltof16chars..", "a short string",
85*0cd13cbfSwyllys 	    "$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ"
86*0cd13cbfSwyllys 	    "4oPwcelCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1" },
87*0cd13cbfSwyllys 	{ "$6$rounds=10$roundstoolow", "the minimum number is still observed",
88*0cd13cbfSwyllys 	    "$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50Y"
89*0cd13cbfSwyllys 	    "hH1xhLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX." },
90*0cd13cbfSwyllys };
91*0cd13cbfSwyllys 
92*0cd13cbfSwyllys #else
93*0cd13cbfSwyllys #error "One of CRYPT_SHA256 or CRYPT_SHA512 must be defined"
94*0cd13cbfSwyllys #endif
95*0cd13cbfSwyllys 
96*0cd13cbfSwyllys #define	ntests2 (sizeof (tests2) / sizeof (tests2[0]))
97*0cd13cbfSwyllys 
98*0cd13cbfSwyllys int
main(int argc,char * argv[])99*0cd13cbfSwyllys main(int argc, char *argv[])
100*0cd13cbfSwyllys {
101*0cd13cbfSwyllys 	int cnt;
102*0cd13cbfSwyllys 	int failures = 0;
103*0cd13cbfSwyllys 	char ctbuffer[CRYPT_MAXCIPHERTEXTLEN];
104*0cd13cbfSwyllys 	size_t ctbufflen = sizeof (ctbuffer);
105*0cd13cbfSwyllys 
106*0cd13cbfSwyllys #ifdef CRYPT_SHA256
107*0cd13cbfSwyllys 	fprintf(stderr, "CRYPT_SHA256 ");
108*0cd13cbfSwyllys #elif CRYPT_SHA512
109*0cd13cbfSwyllys 	fprintf(stderr, "CRYPT_SHA512 ");
110*0cd13cbfSwyllys #endif
111*0cd13cbfSwyllys 	fprintf(stderr, "CRYPT_MAXCIPHERTEXTLEN = %d\n",
112*0cd13cbfSwyllys 	    CRYPT_MAXCIPHERTEXTLEN);
113*0cd13cbfSwyllys 	for (cnt = 0; cnt < ntests2; ++cnt) {
114*0cd13cbfSwyllys 		char *cp;
115*0cd13cbfSwyllys 		fprintf(stderr, "test %d (outlen=%d):  ", cnt,
116*0cd13cbfSwyllys 		    strlen(tests2[cnt].expected));
117*0cd13cbfSwyllys 		cp = crypt_genhash_impl(ctbuffer, ctbufflen,
118*0cd13cbfSwyllys 		    tests2[cnt].input, tests2[cnt].salt, NULL);
119*0cd13cbfSwyllys 
120*0cd13cbfSwyllys 		if (cp == NULL || (strcmp(cp, tests2[cnt].expected) != 0)) {
121*0cd13cbfSwyllys 			fprintf(stderr,
122*0cd13cbfSwyllys 			    "FAILED\nE(%d): \"%s\"\nG(%d): \"%s\"\n",
123*0cd13cbfSwyllys 			    strlen(tests2[cnt].expected), tests2[cnt].expected,
124*0cd13cbfSwyllys 			    (cp ? strlen(cp) : 0), (cp ? cp : "NULL"));
125*0cd13cbfSwyllys 			failures++;
126*0cd13cbfSwyllys 		} else {
127*0cd13cbfSwyllys 			fprintf(stderr, "OK\n");
128*0cd13cbfSwyllys 		}
129*0cd13cbfSwyllys 	}
130*0cd13cbfSwyllys 
131*0cd13cbfSwyllys 	if (failures == 0) {
132*0cd13cbfSwyllys 		fprintf(stderr, "all tests OK\n");
133*0cd13cbfSwyllys 	} else {
134*0cd13cbfSwyllys 		fprintf(stderr, "%d tests failed\n", failures);
135*0cd13cbfSwyllys 	}
136*0cd13cbfSwyllys 
137*0cd13cbfSwyllys 	return (failures);
138*0cd13cbfSwyllys }
139