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