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