summaryrefslogtreecommitdiff
path: root/usr/src/lib/crypt_modules/sha256/test.c
blob: 091f062f8c6bdc6a0ad7046ed43538979c25dbc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <crypt.h>
#include <string.h>

#ifdef CRYPT_SHA256
static const struct
{
	const char *salt;
	const char *input;
	const char *expected;
} tests2[] = {
	{ "$5$saltstring", "Hello world!",
	    "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5" },
	{ "$5$rounds=10000$saltstringsaltstring", "Hello world!",
	    "$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBA"
	    "wqFMz2.opqey6IcA" },
	{ "$5$rounds=5000$toolongsaltstring", "This is just a test",
	    "$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07g"
	    "uHPvOW8mGRcvxa5" },
	{ "$5$rounds=1400$anotherlongsaltstring",
	    "a very much longer text to encrypt.  This one even stretches"
	    " over morethan one line.",
	    "$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIU"
	    "nzyxf12oP84Bnq1" },
	{ "$5$rounds=77777$short",
	    "we have a short salt string but not a short password",
	    "$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0"
	    "KQRd/" },
	{ "$5$rounds=123456$asaltof16chars..", "a short string",
	    "$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2"
	    "jxPyzV/cZKmF/wJvD" },
	{ "$5$rounds=10$roundstoolow", "the minimum number is still observed",
	    "$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY"
	    "9l/gL972bIC" },
};
#elif CRYPT_SHA512
static const struct
{
	const char *salt;
	const char *input;
	const char *expected;
} tests2[] = {
	{ "$6$saltstring", "Hello world!",
	    "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnI"
	    "FNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1" },
	{ "$6$rounds=10000$saltstringsaltstring", "Hello world!",
	    "$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3"
	    "Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v." },
	{ "$6$rounds=5000$toolongsaltstring", "This is just a test",
	    "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxG"
	    "oNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0" },
	{ "$6$rounds=1400$anotherlongsaltstring",
	    "a very much longer text to encrypt.  This one even stretches "
	    "over morethan one line.",
	    "$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/p"
	    "Qs.wPvMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1" },
	{ "$6$rounds=77777$short",
	    "we have a short salt string but not a short password",
	    "$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXb"
	    "kvr0gge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0" },
	{ "$6$rounds=123456$asaltof16chars..", "a short string",
	    "$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ"
	    "4oPwcelCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1" },
	{ "$6$rounds=10$roundstoolow", "the minimum number is still observed",
	    "$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50Y"
	    "hH1xhLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX." },
};

#else
#error "One of CRYPT_SHA256 or CRYPT_SHA512 must be defined"
#endif

#define	ntests2 (sizeof (tests2) / sizeof (tests2[0]))

int
main(int argc, char *argv[])
{
	int cnt;
	int failures = 0;
	char ctbuffer[CRYPT_MAXCIPHERTEXTLEN];
	size_t ctbufflen = sizeof (ctbuffer);

#ifdef CRYPT_SHA256
	fprintf(stderr, "CRYPT_SHA256 ");
#elif CRYPT_SHA512
	fprintf(stderr, "CRYPT_SHA512 ");
#endif
	fprintf(stderr, "CRYPT_MAXCIPHERTEXTLEN = %d\n",
	    CRYPT_MAXCIPHERTEXTLEN);
	for (cnt = 0; cnt < ntests2; ++cnt) {
		char *cp;
		fprintf(stderr, "test %d (outlen=%d):  ", cnt,
		    strlen(tests2[cnt].expected));
		cp = crypt_genhash_impl(ctbuffer, ctbufflen,
		    tests2[cnt].input, tests2[cnt].salt, NULL);

		if (cp == NULL || (strcmp(cp, tests2[cnt].expected) != 0)) {
			fprintf(stderr,
			    "FAILED\nE(%d): \"%s\"\nG(%d): \"%s\"\n",
			    strlen(tests2[cnt].expected), tests2[cnt].expected,
			    (cp ? strlen(cp) : 0), (cp ? cp : "NULL"));
			failures++;
		} else {
			fprintf(stderr, "OK\n");
		}
	}

	if (failures == 0) {
		fprintf(stderr, "all tests OK\n");
	} else {
		fprintf(stderr, "%d tests failed\n", failures);
	}

	return (failures);
}