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