15e53a4f9SPedro F. Giffuni /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni *
43d6f63c0SMark Murray * Copyright (c) 2011 The FreeBSD Project. All rights reserved.
53d6f63c0SMark Murray *
63d6f63c0SMark Murray * Redistribution and use in source and binary forms, with or without
73d6f63c0SMark Murray * modification, are permitted provided that the following conditions
83d6f63c0SMark Murray * are met:
93d6f63c0SMark Murray * 1. Redistributions of source code must retain the above copyright
103d6f63c0SMark Murray * notice, this list of conditions and the following disclaimer.
113d6f63c0SMark Murray * 2. Redistributions in binary form must reproduce the above copyright
123d6f63c0SMark Murray * notice, this list of conditions and the following disclaimer in the
133d6f63c0SMark Murray * documentation and/or other materials provided with the distribution.
143d6f63c0SMark Murray *
153d6f63c0SMark Murray * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
163d6f63c0SMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173d6f63c0SMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
183d6f63c0SMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
193d6f63c0SMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
203d6f63c0SMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
213d6f63c0SMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223d6f63c0SMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
233d6f63c0SMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
243d6f63c0SMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
253d6f63c0SMark Murray * SUCH DAMAGE.
263d6f63c0SMark Murray */
273d6f63c0SMark Murray
283d6f63c0SMark Murray /* Based on:
293d6f63c0SMark Murray * SHA256-based Unix crypt implementation. Released into the Public Domain by
303d6f63c0SMark Murray * Ulrich Drepper <drepper@redhat.com>. */
313d6f63c0SMark Murray
323d6f63c0SMark Murray #include <sys/cdefs.h>
333d6f63c0SMark Murray #include <sys/endian.h>
343d6f63c0SMark Murray #include <sys/param.h>
353d6f63c0SMark Murray
363d6f63c0SMark Murray #include <errno.h>
373d6f63c0SMark Murray #include <limits.h>
383d6f63c0SMark Murray #include <sha256.h>
393d6f63c0SMark Murray #include <stdbool.h>
403d6f63c0SMark Murray #include <stdint.h>
413d6f63c0SMark Murray #include <stdio.h>
423d6f63c0SMark Murray #include <stdlib.h>
433d6f63c0SMark Murray #include <string.h>
44*a2c0d202SRobert Clausecker #include <strings.h>
453d6f63c0SMark Murray
463d6f63c0SMark Murray #include "crypt.h"
473d6f63c0SMark Murray
483d6f63c0SMark Murray /* Define our magic string to mark salt for SHA256 "encryption" replacement. */
493d6f63c0SMark Murray static const char sha256_salt_prefix[] = "$5$";
503d6f63c0SMark Murray
513d6f63c0SMark Murray /* Prefix for optional rounds specification. */
523d6f63c0SMark Murray static const char sha256_rounds_prefix[] = "rounds=";
533d6f63c0SMark Murray
543d6f63c0SMark Murray /* Maximum salt string length. */
553d6f63c0SMark Murray #define SALT_LEN_MAX 16
563d6f63c0SMark Murray /* Default number of rounds if not explicitly specified. */
573d6f63c0SMark Murray #define ROUNDS_DEFAULT 5000
583d6f63c0SMark Murray /* Minimum number of rounds. */
593d6f63c0SMark Murray #define ROUNDS_MIN 1000
603d6f63c0SMark Murray /* Maximum number of rounds. */
613d6f63c0SMark Murray #define ROUNDS_MAX 999999999
623d6f63c0SMark Murray
635f521d7bSEd Schouten int
crypt_sha256(const char * key,const char * salt,char * buffer)645f521d7bSEd Schouten crypt_sha256(const char *key, const char *salt, char *buffer)
653d6f63c0SMark Murray {
663d6f63c0SMark Murray u_long srounds;
673d6f63c0SMark Murray uint8_t alt_result[32], temp_result[32];
683d6f63c0SMark Murray SHA256_CTX ctx, alt_ctx;
693d6f63c0SMark Murray size_t salt_len, key_len, cnt, rounds;
70a7e92a77SXin LI char *cp, *p_bytes, *s_bytes, *endp;
713d6f63c0SMark Murray const char *num;
723d6f63c0SMark Murray bool rounds_custom;
733d6f63c0SMark Murray
743d6f63c0SMark Murray /* Default number of rounds. */
753d6f63c0SMark Murray rounds = ROUNDS_DEFAULT;
763d6f63c0SMark Murray rounds_custom = false;
773d6f63c0SMark Murray
783d6f63c0SMark Murray /* Find beginning of salt string. The prefix should normally always
793d6f63c0SMark Murray * be present. Just in case it is not. */
803d6f63c0SMark Murray if (strncmp(sha256_salt_prefix, salt, sizeof(sha256_salt_prefix) - 1) == 0)
813d6f63c0SMark Murray /* Skip salt prefix. */
823d6f63c0SMark Murray salt += sizeof(sha256_salt_prefix) - 1;
833d6f63c0SMark Murray
843d6f63c0SMark Murray if (strncmp(salt, sha256_rounds_prefix, sizeof(sha256_rounds_prefix) - 1)
853d6f63c0SMark Murray == 0) {
863d6f63c0SMark Murray num = salt + sizeof(sha256_rounds_prefix) - 1;
873d6f63c0SMark Murray srounds = strtoul(num, &endp, 10);
883d6f63c0SMark Murray
893d6f63c0SMark Murray if (*endp == '$') {
903d6f63c0SMark Murray salt = endp + 1;
913d6f63c0SMark Murray rounds = MAX(ROUNDS_MIN, MIN(srounds, ROUNDS_MAX));
923d6f63c0SMark Murray rounds_custom = true;
933d6f63c0SMark Murray }
943d6f63c0SMark Murray }
953d6f63c0SMark Murray
963d6f63c0SMark Murray salt_len = MIN(strcspn(salt, "$"), SALT_LEN_MAX);
973d6f63c0SMark Murray key_len = strlen(key);
983d6f63c0SMark Murray
993d6f63c0SMark Murray /* Prepare for the real work. */
1003d6f63c0SMark Murray SHA256_Init(&ctx);
1013d6f63c0SMark Murray
1023d6f63c0SMark Murray /* Add the key string. */
1033d6f63c0SMark Murray SHA256_Update(&ctx, key, key_len);
1043d6f63c0SMark Murray
1053d6f63c0SMark Murray /* The last part is the salt string. This must be at most 8
1063d6f63c0SMark Murray * characters and it ends at the first `$' character (for
1073d6f63c0SMark Murray * compatibility with existing implementations). */
1083d6f63c0SMark Murray SHA256_Update(&ctx, salt, salt_len);
1093d6f63c0SMark Murray
1103d6f63c0SMark Murray /* Compute alternate SHA256 sum with input KEY, SALT, and KEY. The
1113d6f63c0SMark Murray * final result will be added to the first context. */
1123d6f63c0SMark Murray SHA256_Init(&alt_ctx);
1133d6f63c0SMark Murray
1143d6f63c0SMark Murray /* Add key. */
1153d6f63c0SMark Murray SHA256_Update(&alt_ctx, key, key_len);
1163d6f63c0SMark Murray
1173d6f63c0SMark Murray /* Add salt. */
1183d6f63c0SMark Murray SHA256_Update(&alt_ctx, salt, salt_len);
1193d6f63c0SMark Murray
1203d6f63c0SMark Murray /* Add key again. */
1213d6f63c0SMark Murray SHA256_Update(&alt_ctx, key, key_len);
1223d6f63c0SMark Murray
1233d6f63c0SMark Murray /* Now get result of this (32 bytes) and add it to the other context. */
1243d6f63c0SMark Murray SHA256_Final(alt_result, &alt_ctx);
1253d6f63c0SMark Murray
1263d6f63c0SMark Murray /* Add for any character in the key one byte of the alternate sum. */
1273d6f63c0SMark Murray for (cnt = key_len; cnt > 32; cnt -= 32)
1283d6f63c0SMark Murray SHA256_Update(&ctx, alt_result, 32);
1293d6f63c0SMark Murray SHA256_Update(&ctx, alt_result, cnt);
1303d6f63c0SMark Murray
1313d6f63c0SMark Murray /* Take the binary representation of the length of the key and for
1323d6f63c0SMark Murray * every 1 add the alternate sum, for every 0 the key. */
1333d6f63c0SMark Murray for (cnt = key_len; cnt > 0; cnt >>= 1)
1343d6f63c0SMark Murray if ((cnt & 1) != 0)
1353d6f63c0SMark Murray SHA256_Update(&ctx, alt_result, 32);
1363d6f63c0SMark Murray else
1373d6f63c0SMark Murray SHA256_Update(&ctx, key, key_len);
1383d6f63c0SMark Murray
1393d6f63c0SMark Murray /* Create intermediate result. */
1403d6f63c0SMark Murray SHA256_Final(alt_result, &ctx);
1413d6f63c0SMark Murray
1423d6f63c0SMark Murray /* Start computation of P byte sequence. */
1433d6f63c0SMark Murray SHA256_Init(&alt_ctx);
1443d6f63c0SMark Murray
1453d6f63c0SMark Murray /* For every character in the password add the entire password. */
1463d6f63c0SMark Murray for (cnt = 0; cnt < key_len; ++cnt)
1473d6f63c0SMark Murray SHA256_Update(&alt_ctx, key, key_len);
1483d6f63c0SMark Murray
1493d6f63c0SMark Murray /* Finish the digest. */
1503d6f63c0SMark Murray SHA256_Final(temp_result, &alt_ctx);
1513d6f63c0SMark Murray
1523d6f63c0SMark Murray /* Create byte sequence P. */
1533d6f63c0SMark Murray cp = p_bytes = alloca(key_len);
1543d6f63c0SMark Murray for (cnt = key_len; cnt >= 32; cnt -= 32) {
1553d6f63c0SMark Murray memcpy(cp, temp_result, 32);
1563d6f63c0SMark Murray cp += 32;
1573d6f63c0SMark Murray }
1583d6f63c0SMark Murray memcpy(cp, temp_result, cnt);
1593d6f63c0SMark Murray
1603d6f63c0SMark Murray /* Start computation of S byte sequence. */
1613d6f63c0SMark Murray SHA256_Init(&alt_ctx);
1623d6f63c0SMark Murray
1633d6f63c0SMark Murray /* For every character in the password add the entire password. */
1643d6f63c0SMark Murray for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt)
1653d6f63c0SMark Murray SHA256_Update(&alt_ctx, salt, salt_len);
1663d6f63c0SMark Murray
1673d6f63c0SMark Murray /* Finish the digest. */
1683d6f63c0SMark Murray SHA256_Final(temp_result, &alt_ctx);
1693d6f63c0SMark Murray
1703d6f63c0SMark Murray /* Create byte sequence S. */
1713d6f63c0SMark Murray cp = s_bytes = alloca(salt_len);
1723d6f63c0SMark Murray for (cnt = salt_len; cnt >= 32; cnt -= 32) {
1733d6f63c0SMark Murray memcpy(cp, temp_result, 32);
1743d6f63c0SMark Murray cp += 32;
1753d6f63c0SMark Murray }
1763d6f63c0SMark Murray memcpy(cp, temp_result, cnt);
1773d6f63c0SMark Murray
1783d6f63c0SMark Murray /* Repeatedly run the collected hash value through SHA256 to burn CPU
1793d6f63c0SMark Murray * cycles. */
1803d6f63c0SMark Murray for (cnt = 0; cnt < rounds; ++cnt) {
1813d6f63c0SMark Murray /* New context. */
1823d6f63c0SMark Murray SHA256_Init(&ctx);
1833d6f63c0SMark Murray
1843d6f63c0SMark Murray /* Add key or last result. */
1853d6f63c0SMark Murray if ((cnt & 1) != 0)
1863d6f63c0SMark Murray SHA256_Update(&ctx, p_bytes, key_len);
1873d6f63c0SMark Murray else
1883d6f63c0SMark Murray SHA256_Update(&ctx, alt_result, 32);
1893d6f63c0SMark Murray
1903d6f63c0SMark Murray /* Add salt for numbers not divisible by 3. */
1913d6f63c0SMark Murray if (cnt % 3 != 0)
1923d6f63c0SMark Murray SHA256_Update(&ctx, s_bytes, salt_len);
1933d6f63c0SMark Murray
1943d6f63c0SMark Murray /* Add key for numbers not divisible by 7. */
1953d6f63c0SMark Murray if (cnt % 7 != 0)
1963d6f63c0SMark Murray SHA256_Update(&ctx, p_bytes, key_len);
1973d6f63c0SMark Murray
1983d6f63c0SMark Murray /* Add key or last result. */
1993d6f63c0SMark Murray if ((cnt & 1) != 0)
2003d6f63c0SMark Murray SHA256_Update(&ctx, alt_result, 32);
2013d6f63c0SMark Murray else
2023d6f63c0SMark Murray SHA256_Update(&ctx, p_bytes, key_len);
2033d6f63c0SMark Murray
2043d6f63c0SMark Murray /* Create intermediate result. */
2053d6f63c0SMark Murray SHA256_Final(alt_result, &ctx);
2063d6f63c0SMark Murray }
2073d6f63c0SMark Murray
2083d6f63c0SMark Murray /* Now we can construct the result string. It consists of three
2093d6f63c0SMark Murray * parts. */
2105f521d7bSEd Schouten cp = stpcpy(buffer, sha256_salt_prefix);
2113d6f63c0SMark Murray
2125f521d7bSEd Schouten if (rounds_custom)
2135f521d7bSEd Schouten cp += sprintf(cp, "%s%zu$", sha256_rounds_prefix, rounds);
2143d6f63c0SMark Murray
2155f521d7bSEd Schouten cp = stpncpy(cp, salt, salt_len);
2163d6f63c0SMark Murray
2173d6f63c0SMark Murray *cp++ = '$';
2183d6f63c0SMark Murray
2195f521d7bSEd Schouten b64_from_24bit(alt_result[0], alt_result[10], alt_result[20], 4, &cp);
2205f521d7bSEd Schouten b64_from_24bit(alt_result[21], alt_result[1], alt_result[11], 4, &cp);
2215f521d7bSEd Schouten b64_from_24bit(alt_result[12], alt_result[22], alt_result[2], 4, &cp);
2225f521d7bSEd Schouten b64_from_24bit(alt_result[3], alt_result[13], alt_result[23], 4, &cp);
2235f521d7bSEd Schouten b64_from_24bit(alt_result[24], alt_result[4], alt_result[14], 4, &cp);
2245f521d7bSEd Schouten b64_from_24bit(alt_result[15], alt_result[25], alt_result[5], 4, &cp);
2255f521d7bSEd Schouten b64_from_24bit(alt_result[6], alt_result[16], alt_result[26], 4, &cp);
2265f521d7bSEd Schouten b64_from_24bit(alt_result[27], alt_result[7], alt_result[17], 4, &cp);
2275f521d7bSEd Schouten b64_from_24bit(alt_result[18], alt_result[28], alt_result[8], 4, &cp);
2285f521d7bSEd Schouten b64_from_24bit(alt_result[9], alt_result[19], alt_result[29], 4, &cp);
2295f521d7bSEd Schouten b64_from_24bit(0, alt_result[31], alt_result[30], 3, &cp);
2303d6f63c0SMark Murray *cp = '\0'; /* Terminate the string. */
2313d6f63c0SMark Murray
2323d6f63c0SMark Murray /* Clear the buffer for the intermediate result so that people
2333d6f63c0SMark Murray * attaching to processes or reading core dumps cannot get any
2343d6f63c0SMark Murray * information. We do it in this way to clear correct_words[] inside
2353d6f63c0SMark Murray * the SHA256 implementation as well. */
2363d6f63c0SMark Murray SHA256_Init(&ctx);
2373d6f63c0SMark Murray SHA256_Final(alt_result, &ctx);
238*a2c0d202SRobert Clausecker explicit_bzero(temp_result, sizeof(temp_result));
239*a2c0d202SRobert Clausecker explicit_bzero(p_bytes, key_len);
240*a2c0d202SRobert Clausecker explicit_bzero(s_bytes, salt_len);
2413d6f63c0SMark Murray
2425f521d7bSEd Schouten return (0);
2433d6f63c0SMark Murray }
2443d6f63c0SMark Murray
2453d6f63c0SMark Murray #ifdef TEST
2463d6f63c0SMark Murray
2473d6f63c0SMark Murray static const struct {
2483d6f63c0SMark Murray const char *input;
2493d6f63c0SMark Murray const char result[32];
2503d6f63c0SMark Murray } tests[] =
2513d6f63c0SMark Murray {
2523d6f63c0SMark Murray /* Test vectors from FIPS 180-2: appendix B.1. */
2533d6f63c0SMark Murray {
2543d6f63c0SMark Murray "abc",
2553d6f63c0SMark Murray "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23"
2563d6f63c0SMark Murray "\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad"
2573d6f63c0SMark Murray },
2583d6f63c0SMark Murray /* Test vectors from FIPS 180-2: appendix B.2. */
2593d6f63c0SMark Murray {
2603d6f63c0SMark Murray "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
2613d6f63c0SMark Murray "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39"
2623d6f63c0SMark Murray "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1"
2633d6f63c0SMark Murray },
2643d6f63c0SMark Murray /* Test vectors from the NESSIE project. */
2653d6f63c0SMark Murray {
2663d6f63c0SMark Murray "",
2673d6f63c0SMark Murray "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9\x24"
2683d6f63c0SMark Murray "\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52\xb8\x55"
2693d6f63c0SMark Murray },
2703d6f63c0SMark Murray {
2713d6f63c0SMark Murray "a",
2723d6f63c0SMark Murray "\xca\x97\x81\x12\xca\x1b\xbd\xca\xfa\xc2\x31\xb3\x9a\x23\xdc\x4d"
2733d6f63c0SMark Murray "\xa7\x86\xef\xf8\x14\x7c\x4e\x72\xb9\x80\x77\x85\xaf\xee\x48\xbb"
2743d6f63c0SMark Murray },
2753d6f63c0SMark Murray {
2763d6f63c0SMark Murray "message digest",
2773d6f63c0SMark Murray "\xf7\x84\x6f\x55\xcf\x23\xe1\x4e\xeb\xea\xb5\xb4\xe1\x55\x0c\xad"
2783d6f63c0SMark Murray "\x5b\x50\x9e\x33\x48\xfb\xc4\xef\xa3\xa1\x41\x3d\x39\x3c\xb6\x50"
2793d6f63c0SMark Murray },
2803d6f63c0SMark Murray {
2813d6f63c0SMark Murray "abcdefghijklmnopqrstuvwxyz",
2823d6f63c0SMark Murray "\x71\xc4\x80\xdf\x93\xd6\xae\x2f\x1e\xfa\xd1\x44\x7c\x66\xc9\x52"
2833d6f63c0SMark Murray "\x5e\x31\x62\x18\xcf\x51\xfc\x8d\x9e\xd8\x32\xf2\xda\xf1\x8b\x73"
2843d6f63c0SMark Murray },
2853d6f63c0SMark Murray {
2863d6f63c0SMark Murray "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
2873d6f63c0SMark Murray "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39"
2883d6f63c0SMark Murray "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1"
2893d6f63c0SMark Murray },
2903d6f63c0SMark Murray {
2913d6f63c0SMark Murray "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
2923d6f63c0SMark Murray "\xdb\x4b\xfc\xbd\x4d\xa0\xcd\x85\xa6\x0c\x3c\x37\xd3\xfb\xd8\x80"
2933d6f63c0SMark Murray "\x5c\x77\xf1\x5f\xc6\xb1\xfd\xfe\x61\x4e\xe0\xa7\xc8\xfd\xb4\xc0"
2943d6f63c0SMark Murray },
2953d6f63c0SMark Murray {
2963d6f63c0SMark Murray "123456789012345678901234567890123456789012345678901234567890"
2973d6f63c0SMark Murray "12345678901234567890",
2983d6f63c0SMark Murray "\xf3\x71\xbc\x4a\x31\x1f\x2b\x00\x9e\xef\x95\x2d\xd8\x3c\xa8\x0e"
2993d6f63c0SMark Murray "\x2b\x60\x02\x6c\x8e\x93\x55\x92\xd0\xf9\xc3\x08\x45\x3c\x81\x3e"
3003d6f63c0SMark Murray }
3013d6f63c0SMark Murray };
3023d6f63c0SMark Murray
3033d6f63c0SMark Murray #define ntests (sizeof (tests) / sizeof (tests[0]))
3043d6f63c0SMark Murray
3053d6f63c0SMark Murray static const struct {
3063d6f63c0SMark Murray const char *salt;
3073d6f63c0SMark Murray const char *input;
3083d6f63c0SMark Murray const char *expected;
3093d6f63c0SMark Murray } tests2[] =
3103d6f63c0SMark Murray {
3113d6f63c0SMark Murray {
3123d6f63c0SMark Murray "$5$saltstring", "Hello world!",
3133d6f63c0SMark Murray "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5"
3143d6f63c0SMark Murray },
3153d6f63c0SMark Murray {
3163d6f63c0SMark Murray "$5$rounds=10000$saltstringsaltstring", "Hello world!",
3173d6f63c0SMark Murray "$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBAwqFMz2."
3183d6f63c0SMark Murray "opqey6IcA"
3193d6f63c0SMark Murray },
3203d6f63c0SMark Murray {
3213d6f63c0SMark Murray "$5$rounds=5000$toolongsaltstring", "This is just a test",
3223d6f63c0SMark Murray "$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07guHPvOW8"
3233d6f63c0SMark Murray "mGRcvxa5"
3243d6f63c0SMark Murray },
3253d6f63c0SMark Murray {
3263d6f63c0SMark Murray "$5$rounds=1400$anotherlongsaltstring",
3273d6f63c0SMark Murray "a very much longer text to encrypt. This one even stretches over more"
3283d6f63c0SMark Murray "than one line.",
3293d6f63c0SMark Murray "$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIUnzyxf12"
3303d6f63c0SMark Murray "oP84Bnq1"
3313d6f63c0SMark Murray },
3323d6f63c0SMark Murray {
3333d6f63c0SMark Murray "$5$rounds=77777$short",
3343d6f63c0SMark Murray "we have a short salt string but not a short password",
3353d6f63c0SMark Murray "$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0KQRd/"
3363d6f63c0SMark Murray },
3373d6f63c0SMark Murray {
3383d6f63c0SMark Murray "$5$rounds=123456$asaltof16chars..", "a short string",
3393d6f63c0SMark Murray "$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2jxPyzV/"
3403d6f63c0SMark Murray "cZKmF/wJvD"
3413d6f63c0SMark Murray },
3423d6f63c0SMark Murray {
3433d6f63c0SMark Murray "$5$rounds=10$roundstoolow", "the minimum number is still observed",
3443d6f63c0SMark Murray "$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY9l/gL97"
3453d6f63c0SMark Murray "2bIC"
3463d6f63c0SMark Murray },
3473d6f63c0SMark Murray };
3483d6f63c0SMark Murray
3493d6f63c0SMark Murray #define ntests2 (sizeof (tests2) / sizeof (tests2[0]))
3503d6f63c0SMark Murray
3513d6f63c0SMark Murray int
main(void)3523d6f63c0SMark Murray main(void)
3533d6f63c0SMark Murray {
3543d6f63c0SMark Murray SHA256_CTX ctx;
3553d6f63c0SMark Murray uint8_t sum[32];
3563d6f63c0SMark Murray int result = 0;
3573d6f63c0SMark Murray int i, cnt;
3583d6f63c0SMark Murray
3593d6f63c0SMark Murray for (cnt = 0; cnt < (int)ntests; ++cnt) {
3603d6f63c0SMark Murray SHA256_Init(&ctx);
3613d6f63c0SMark Murray SHA256_Update(&ctx, tests[cnt].input, strlen(tests[cnt].input));
3623d6f63c0SMark Murray SHA256_Final(sum, &ctx);
3633d6f63c0SMark Murray if (memcmp(tests[cnt].result, sum, 32) != 0) {
3643d6f63c0SMark Murray for (i = 0; i < 32; i++)
3653d6f63c0SMark Murray printf("%02X", tests[cnt].result[i]);
3663d6f63c0SMark Murray printf("\n");
3673d6f63c0SMark Murray for (i = 0; i < 32; i++)
3683d6f63c0SMark Murray printf("%02X", sum[i]);
3693d6f63c0SMark Murray printf("\n");
3703d6f63c0SMark Murray printf("test %d run %d failed\n", cnt, 1);
3713d6f63c0SMark Murray result = 1;
3723d6f63c0SMark Murray }
3733d6f63c0SMark Murray
3743d6f63c0SMark Murray SHA256_Init(&ctx);
3753d6f63c0SMark Murray for (i = 0; tests[cnt].input[i] != '\0'; ++i)
3763d6f63c0SMark Murray SHA256_Update(&ctx, &tests[cnt].input[i], 1);
3773d6f63c0SMark Murray SHA256_Final(sum, &ctx);
3783d6f63c0SMark Murray if (memcmp(tests[cnt].result, sum, 32) != 0) {
3793d6f63c0SMark Murray for (i = 0; i < 32; i++)
3803d6f63c0SMark Murray printf("%02X", tests[cnt].result[i]);
3813d6f63c0SMark Murray printf("\n");
3823d6f63c0SMark Murray for (i = 0; i < 32; i++)
3833d6f63c0SMark Murray printf("%02X", sum[i]);
3843d6f63c0SMark Murray printf("\n");
3853d6f63c0SMark Murray printf("test %d run %d failed\n", cnt, 2);
3863d6f63c0SMark Murray result = 1;
3873d6f63c0SMark Murray }
3883d6f63c0SMark Murray }
3893d6f63c0SMark Murray
3903d6f63c0SMark Murray /* Test vector from FIPS 180-2: appendix B.3. */
3913d6f63c0SMark Murray char buf[1000];
3923d6f63c0SMark Murray
3933d6f63c0SMark Murray memset(buf, 'a', sizeof(buf));
3943d6f63c0SMark Murray SHA256_Init(&ctx);
3953d6f63c0SMark Murray for (i = 0; i < 1000; ++i)
3963d6f63c0SMark Murray SHA256_Update(&ctx, buf, sizeof(buf));
3973d6f63c0SMark Murray SHA256_Final(sum, &ctx);
3983d6f63c0SMark Murray static const char expected[32] =
3993d6f63c0SMark Murray "\xcd\xc7\x6e\x5c\x99\x14\xfb\x92\x81\xa1\xc7\xe2\x84\xd7\x3e\x67"
4003d6f63c0SMark Murray "\xf1\x80\x9a\x48\xa4\x97\x20\x0e\x04\x6d\x39\xcc\xc7\x11\x2c\xd0";
4013d6f63c0SMark Murray
4023d6f63c0SMark Murray if (memcmp(expected, sum, 32) != 0) {
4033d6f63c0SMark Murray printf("test %d failed\n", cnt);
4043d6f63c0SMark Murray result = 1;
4053d6f63c0SMark Murray }
4063d6f63c0SMark Murray
4073d6f63c0SMark Murray for (cnt = 0; cnt < ntests2; ++cnt) {
408ad45dd41SDavid E. O'Brien char *cp = crypt_sha256(tests2[cnt].input, tests2[cnt].salt);
4093d6f63c0SMark Murray
4103d6f63c0SMark Murray if (strcmp(cp, tests2[cnt].expected) != 0) {
4113d6f63c0SMark Murray printf("test %d: expected \"%s\", got \"%s\"\n",
4123d6f63c0SMark Murray cnt, tests2[cnt].expected, cp);
4133d6f63c0SMark Murray result = 1;
4143d6f63c0SMark Murray }
4153d6f63c0SMark Murray }
4163d6f63c0SMark Murray
4173d6f63c0SMark Murray if (result == 0)
4183d6f63c0SMark Murray puts("all tests OK");
4193d6f63c0SMark Murray
4203d6f63c0SMark Murray return result;
4213d6f63c0SMark Murray }
4223d6f63c0SMark Murray
4233d6f63c0SMark Murray #endif /* TEST */
424