0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys/*
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * CDDL HEADER START
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys *
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * The contents of this file are subject to the terms of the
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * Common Development and Distribution License (the "License").
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * You may not use this file except in compliance with the License.
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys *
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * or http://www.opensolaris.org/os/licensing.
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * See the License for the specific language governing permissions
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * and limitations under the License.
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys *
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * When distributing Covered Code, include this CDDL HEADER in each
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * If applicable, add the following below this CDDL HEADER, with the
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * fields enclosed by brackets "[]" replaced with your own identifying
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * information: Portions Copyright [yyyy] [name of copyright owner]
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys *
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * CDDL HEADER END
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys *
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys * Use is subject to license terms.
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys */
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys#pragma ident "%Z%%M% %I% %E% SMI"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys#include <crypt.h>
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys#include <string.h>
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys#ifdef CRYPT_SHA256
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllysstatic const struct
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys{
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys const char *salt;
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys const char *input;
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys const char *expected;
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys} tests2[] = {
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$5$saltstring", "Hello world!",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5" },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$5$rounds=10000$saltstringsaltstring", "Hello world!",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBA"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "wqFMz2.opqey6IcA" },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$5$rounds=5000$toolongsaltstring", "This is just a test",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07g"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "uHPvOW8mGRcvxa5" },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$5$rounds=1400$anotherlongsaltstring",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "a very much longer text to encrypt. This one even stretches"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys " over morethan one line.",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIU"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "nzyxf12oP84Bnq1" },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$5$rounds=77777$short",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "we have a short salt string but not a short password",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "KQRd/" },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$5$rounds=123456$asaltof16chars..", "a short string",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "jxPyzV/cZKmF/wJvD" },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$5$rounds=10$roundstoolow", "the minimum number is still observed",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "9l/gL972bIC" },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys};
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys#elif CRYPT_SHA512
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllysstatic const struct
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys{
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys const char *salt;
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys const char *input;
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys const char *expected;
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys} tests2[] = {
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$6$saltstring", "Hello world!",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnI"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "FNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1" },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$6$rounds=10000$saltstringsaltstring", "Hello world!",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v." },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$6$rounds=5000$toolongsaltstring", "This is just a test",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxG"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "oNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0" },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$6$rounds=1400$anotherlongsaltstring",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "a very much longer text to encrypt. This one even stretches "
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "over morethan one line.",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/p"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "Qs.wPvMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1" },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$6$rounds=77777$short",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "we have a short salt string but not a short password",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXb"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "kvr0gge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0" },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$6$rounds=123456$asaltof16chars..", "a short string",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "4oPwcelCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1" },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys { "$6$rounds=10$roundstoolow", "the minimum number is still observed",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50Y"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "hH1xhLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX." },
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys};
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys#else
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys#error "One of CRYPT_SHA256 or CRYPT_SHA512 must be defined"
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys#endif
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys#define ntests2 (sizeof (tests2) / sizeof (tests2[0]))
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllysint
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllysmain(int argc, char *argv[])
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys{
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys int cnt;
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys int failures = 0;
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys char ctbuffer[CRYPT_MAXCIPHERTEXTLEN];
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys size_t ctbufflen = sizeof (ctbuffer);
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys#ifdef CRYPT_SHA256
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys fprintf(stderr, "CRYPT_SHA256 ");
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys#elif CRYPT_SHA512
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys fprintf(stderr, "CRYPT_SHA512 ");
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys#endif
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys fprintf(stderr, "CRYPT_MAXCIPHERTEXTLEN = %d\n",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys CRYPT_MAXCIPHERTEXTLEN);
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys for (cnt = 0; cnt < ntests2; ++cnt) {
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys char *cp;
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys fprintf(stderr, "test %d (outlen=%d): ", cnt,
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys strlen(tests2[cnt].expected));
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys cp = crypt_genhash_impl(ctbuffer, ctbufflen,
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys tests2[cnt].input, tests2[cnt].salt, NULL);
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys if (cp == NULL || (strcmp(cp, tests2[cnt].expected) != 0)) {
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys fprintf(stderr,
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys "FAILED\nE(%d): \"%s\"\nG(%d): \"%s\"\n",
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys strlen(tests2[cnt].expected), tests2[cnt].expected,
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys (cp ? strlen(cp) : 0), (cp ? cp : "NULL"));
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys failures++;
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys } else {
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys fprintf(stderr, "OK\n");
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys }
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys }
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys if (failures == 0) {
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys fprintf(stderr, "all tests OK\n");
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys } else {
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys fprintf(stderr, "%d tests failed\n", failures);
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys }
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys return (failures);
0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02bwyllys}