test.c revision 6532:09f1081b8fb2
11558Srgrimes/*
298542Smckusick * CDDL HEADER START
398542Smckusick *
498542Smckusick * The contents of this file are subject to the terms of the
598542Smckusick * Common Development and Distribution License (the "License").
698542Smckusick * You may not use this file except in compliance with the License.
798542Smckusick *
898542Smckusick * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
998542Smckusick * or http://www.opensolaris.org/os/licensing.
1098542Smckusick * See the License for the specific language governing permissions
1198542Smckusick * and limitations under the License.
1298542Smckusick *
1398542Smckusick * When distributing Covered Code, include this CDDL HEADER in each
141558Srgrimes * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151558Srgrimes * If applicable, add the following below this CDDL HEADER, with the
161558Srgrimes * fields enclosed by brackets "[]" replaced with your own identifying
171558Srgrimes * information: Portions Copyright [yyyy] [name of copyright owner]
181558Srgrimes *
191558Srgrimes * CDDL HEADER END
201558Srgrimes *
211558Srgrimes * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
221558Srgrimes * Use is subject to license terms.
231558Srgrimes */
241558Srgrimes
251558Srgrimes#pragma ident	"%Z%%M%	%I%	%E% SMI"
261558Srgrimes
271558Srgrimes#include <crypt.h>
281558Srgrimes#include <string.h>
291558Srgrimes
301558Srgrimes#ifdef CRYPT_SHA256
311558Srgrimesstatic const struct
321558Srgrimes{
331558Srgrimes	const char *salt;
341558Srgrimes	const char *input;
351558Srgrimes	const char *expected;
361558Srgrimes} tests2[] = {
371558Srgrimes	{ "$5$saltstring", "Hello world!",
381558Srgrimes	    "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5" },
391558Srgrimes	{ "$5$rounds=10000$saltstringsaltstring", "Hello world!",
401558Srgrimes	    "$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBA"
411558Srgrimes	    "wqFMz2.opqey6IcA" },
421558Srgrimes	{ "$5$rounds=5000$toolongsaltstring", "This is just a test",
431558Srgrimes	    "$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07g"
441558Srgrimes	    "uHPvOW8mGRcvxa5" },
451558Srgrimes	{ "$5$rounds=1400$anotherlongsaltstring",
461558Srgrimes	    "a very much longer text to encrypt.  This one even stretches"
4736998Scharnier	    " over morethan one line.",
481558Srgrimes	    "$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIU"
491558Srgrimes	    "nzyxf12oP84Bnq1" },
501558Srgrimes	{ "$5$rounds=77777$short",
511558Srgrimes	    "we have a short salt string but not a short password",
521558Srgrimes	    "$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0"
5336998Scharnier	    "KQRd/" },
5423673Speter	{ "$5$rounds=123456$asaltof16chars..", "a short string",
5536998Scharnier	    "$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2"
5636998Scharnier	    "jxPyzV/cZKmF/wJvD" },
5750476Speter	{ "$5$rounds=10$roundstoolow", "the minimum number is still observed",
581558Srgrimes	    "$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY"
591558Srgrimes	    "9l/gL972bIC" },
601558Srgrimes};
611558Srgrimes#elif CRYPT_SHA512
6296478Sphkstatic const struct
631558Srgrimes{
6498542Smckusick	const char *salt;
651558Srgrimes	const char *input;
661558Srgrimes	const char *expected;
6723673Speter} tests2[] = {
68105741Sjmallett	{ "$6$saltstring", "Hello world!",
691558Srgrimes	    "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnI"
701558Srgrimes	    "FNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1" },
7199826Sjmallett	{ "$6$rounds=10000$saltstringsaltstring", "Hello world!",
72109525Sjmallett	    "$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3"
731558Srgrimes	    "Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v." },
741558Srgrimes	{ "$6$rounds=5000$toolongsaltstring", "This is just a test",
7523673Speter	    "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxG"
761558Srgrimes	    "oNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0" },
7799826Sjmallett	{ "$6$rounds=1400$anotherlongsaltstring",
78109510Sjmallett	    "a very much longer text to encrypt.  This one even stretches "
791558Srgrimes	    "over morethan one line.",
8099826Sjmallett	    "$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/p"
811558Srgrimes	    "Qs.wPvMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1" },
8292839Simp	{ "$6$rounds=77777$short",
83109519Sjmallett	    "we have a short salt string but not a short password",
84109525Sjmallett	    "$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXb"
8592839Simp	    "kvr0gge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0" },
86109525Sjmallett	{ "$6$rounds=123456$asaltof16chars..", "a short string",
8792839Simp	    "$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ"
881558Srgrimes	    "4oPwcelCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1" },
891558Srgrimes	{ "$6$rounds=10$roundstoolow", "the minimum number is still observed",
9092839Simp	    "$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50Y"
911558Srgrimes	    "hH1xhLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX." },
92109525Sjmallett};
93109525Sjmallett
941558Srgrimes#else
95109525Sjmallett#error "One of CRYPT_SHA256 or CRYPT_SHA512 must be defined"
96109507Sjmallett#endif
97109525Sjmallett
98109525Sjmallett#define	ntests2 (sizeof (tests2) / sizeof (tests2[0]))
99109525Sjmallett
100109525Sjmallettint
101109525Sjmallettmain(int argc, char *argv[])
102109525Sjmallett{
103109525Sjmallett	int cnt;
104109525Sjmallett	int failures = 0;
105109525Sjmallett	char ctbuffer[CRYPT_MAXCIPHERTEXTLEN];
106109525Sjmallett	size_t ctbufflen = sizeof (ctbuffer);
1071558Srgrimes
1081558Srgrimes#ifdef CRYPT_SHA256
1091558Srgrimes	fprintf(stderr, "CRYPT_SHA256 ");
1101558Srgrimes#elif CRYPT_SHA512
1111558Srgrimes	fprintf(stderr, "CRYPT_SHA512 ");
1121558Srgrimes#endif
113109525Sjmallett	fprintf(stderr, "CRYPT_MAXCIPHERTEXTLEN = %d\n",
114109525Sjmallett	    CRYPT_MAXCIPHERTEXTLEN);
115109525Sjmallett	for (cnt = 0; cnt < ntests2; ++cnt) {
116109525Sjmallett		char *cp;
117109525Sjmallett		fprintf(stderr, "test %d (outlen=%d):  ", cnt,
118109525Sjmallett		    strlen(tests2[cnt].expected));
119109525Sjmallett		cp = crypt_genhash_impl(ctbuffer, ctbufflen,
120109525Sjmallett		    tests2[cnt].input, tests2[cnt].salt, NULL);
121109525Sjmallett
122109525Sjmallett		if (cp == NULL || (strcmp(cp, tests2[cnt].expected) != 0)) {
123109525Sjmallett			fprintf(stderr,
124109525Sjmallett			    "FAILED\nE(%d): \"%s\"\nG(%d): \"%s\"\n",
1251558Srgrimes			    strlen(tests2[cnt].expected), tests2[cnt].expected,
1261558Srgrimes			    (cp ? strlen(cp) : 0), (cp ? cp : "NULL"));
1271558Srgrimes			failures++;
1281558Srgrimes		} else {
12992839Simp			fprintf(stderr, "OK\n");
1301558Srgrimes		}
13198542Smckusick	}
13298542Smckusick
13399827Sjmallett	if (failures == 0) {
1341558Srgrimes		fprintf(stderr, "all tests OK\n");
135101688Sjmallett	} else {
136101688Sjmallett		fprintf(stderr, "%d tests failed\n", failures);
13798542Smckusick	}
13898542Smckusick
13998542Smckusick	return (failures);
14098542Smckusick}
141107294Smckusick