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 * SHA512-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 <sha512.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 SHA512 "encryption" replacement. */ 493d6f63c0SMark Murray static const char sha512_salt_prefix[] = "$6$"; 503d6f63c0SMark Murray 513d6f63c0SMark Murray /* Prefix for optional rounds specification. */ 523d6f63c0SMark Murray static const char sha512_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 645f521d7bSEd Schouten crypt_sha512(const char *key, const char *salt, char *buffer) 653d6f63c0SMark Murray { 663d6f63c0SMark Murray u_long srounds; 673d6f63c0SMark Murray uint8_t alt_result[64], temp_result[64]; 683d6f63c0SMark Murray SHA512_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(sha512_salt_prefix, salt, sizeof(sha512_salt_prefix) - 1) == 0) 813d6f63c0SMark Murray /* Skip salt prefix. */ 823d6f63c0SMark Murray salt += sizeof(sha512_salt_prefix) - 1; 833d6f63c0SMark Murray 843d6f63c0SMark Murray if (strncmp(salt, sha512_rounds_prefix, sizeof(sha512_rounds_prefix) - 1) 853d6f63c0SMark Murray == 0) { 863d6f63c0SMark Murray num = salt + sizeof(sha512_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 SHA512_Init(&ctx); 1013d6f63c0SMark Murray 1023d6f63c0SMark Murray /* Add the key string. */ 1033d6f63c0SMark Murray SHA512_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 SHA512_Update(&ctx, salt, salt_len); 1093d6f63c0SMark Murray 1103d6f63c0SMark Murray /* Compute alternate SHA512 sum with input KEY, SALT, and KEY. The 1113d6f63c0SMark Murray * final result will be added to the first context. */ 1123d6f63c0SMark Murray SHA512_Init(&alt_ctx); 1133d6f63c0SMark Murray 1143d6f63c0SMark Murray /* Add key. */ 1153d6f63c0SMark Murray SHA512_Update(&alt_ctx, key, key_len); 1163d6f63c0SMark Murray 1173d6f63c0SMark Murray /* Add salt. */ 1183d6f63c0SMark Murray SHA512_Update(&alt_ctx, salt, salt_len); 1193d6f63c0SMark Murray 1203d6f63c0SMark Murray /* Add key again. */ 1213d6f63c0SMark Murray SHA512_Update(&alt_ctx, key, key_len); 1223d6f63c0SMark Murray 1233d6f63c0SMark Murray /* Now get result of this (64 bytes) and add it to the other context. */ 1243d6f63c0SMark Murray SHA512_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 > 64; cnt -= 64) 1283d6f63c0SMark Murray SHA512_Update(&ctx, alt_result, 64); 1293d6f63c0SMark Murray SHA512_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 SHA512_Update(&ctx, alt_result, 64); 1363d6f63c0SMark Murray else 1373d6f63c0SMark Murray SHA512_Update(&ctx, key, key_len); 1383d6f63c0SMark Murray 1393d6f63c0SMark Murray /* Create intermediate result. */ 1403d6f63c0SMark Murray SHA512_Final(alt_result, &ctx); 1413d6f63c0SMark Murray 1423d6f63c0SMark Murray /* Start computation of P byte sequence. */ 1433d6f63c0SMark Murray SHA512_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 SHA512_Update(&alt_ctx, key, key_len); 1483d6f63c0SMark Murray 1493d6f63c0SMark Murray /* Finish the digest. */ 1503d6f63c0SMark Murray SHA512_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 >= 64; cnt -= 64) { 1553d6f63c0SMark Murray memcpy(cp, temp_result, 64); 1563d6f63c0SMark Murray cp += 64; 1573d6f63c0SMark Murray } 1583d6f63c0SMark Murray memcpy(cp, temp_result, cnt); 1593d6f63c0SMark Murray 1603d6f63c0SMark Murray /* Start computation of S byte sequence. */ 1613d6f63c0SMark Murray SHA512_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 SHA512_Update(&alt_ctx, salt, salt_len); 1663d6f63c0SMark Murray 1673d6f63c0SMark Murray /* Finish the digest. */ 1683d6f63c0SMark Murray SHA512_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 >= 64; cnt -= 64) { 1733d6f63c0SMark Murray memcpy(cp, temp_result, 64); 1743d6f63c0SMark Murray cp += 64; 1753d6f63c0SMark Murray } 1763d6f63c0SMark Murray memcpy(cp, temp_result, cnt); 1773d6f63c0SMark Murray 1783d6f63c0SMark Murray /* Repeatedly run the collected hash value through SHA512 to burn CPU 1793d6f63c0SMark Murray * cycles. */ 1803d6f63c0SMark Murray for (cnt = 0; cnt < rounds; ++cnt) { 1813d6f63c0SMark Murray /* New context. */ 1823d6f63c0SMark Murray SHA512_Init(&ctx); 1833d6f63c0SMark Murray 1843d6f63c0SMark Murray /* Add key or last result. */ 1853d6f63c0SMark Murray if ((cnt & 1) != 0) 1863d6f63c0SMark Murray SHA512_Update(&ctx, p_bytes, key_len); 1873d6f63c0SMark Murray else 1883d6f63c0SMark Murray SHA512_Update(&ctx, alt_result, 64); 1893d6f63c0SMark Murray 1903d6f63c0SMark Murray /* Add salt for numbers not divisible by 3. */ 1913d6f63c0SMark Murray if (cnt % 3 != 0) 1923d6f63c0SMark Murray SHA512_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 SHA512_Update(&ctx, p_bytes, key_len); 1973d6f63c0SMark Murray 1983d6f63c0SMark Murray /* Add key or last result. */ 1993d6f63c0SMark Murray if ((cnt & 1) != 0) 2003d6f63c0SMark Murray SHA512_Update(&ctx, alt_result, 64); 2013d6f63c0SMark Murray else 2023d6f63c0SMark Murray SHA512_Update(&ctx, p_bytes, key_len); 2033d6f63c0SMark Murray 2043d6f63c0SMark Murray /* Create intermediate result. */ 2053d6f63c0SMark Murray SHA512_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, sha512_salt_prefix); 2113d6f63c0SMark Murray 2125f521d7bSEd Schouten if (rounds_custom) 2135f521d7bSEd Schouten cp += sprintf(cp, "%s%zu$", sha512_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[21], alt_result[42], 4, &cp); 2205f521d7bSEd Schouten b64_from_24bit(alt_result[22], alt_result[43], alt_result[1], 4, &cp); 2215f521d7bSEd Schouten b64_from_24bit(alt_result[44], alt_result[2], alt_result[23], 4, &cp); 2225f521d7bSEd Schouten b64_from_24bit(alt_result[3], alt_result[24], alt_result[45], 4, &cp); 2235f521d7bSEd Schouten b64_from_24bit(alt_result[25], alt_result[46], alt_result[4], 4, &cp); 2245f521d7bSEd Schouten b64_from_24bit(alt_result[47], alt_result[5], alt_result[26], 4, &cp); 2255f521d7bSEd Schouten b64_from_24bit(alt_result[6], alt_result[27], alt_result[48], 4, &cp); 2265f521d7bSEd Schouten b64_from_24bit(alt_result[28], alt_result[49], alt_result[7], 4, &cp); 2275f521d7bSEd Schouten b64_from_24bit(alt_result[50], alt_result[8], alt_result[29], 4, &cp); 2285f521d7bSEd Schouten b64_from_24bit(alt_result[9], alt_result[30], alt_result[51], 4, &cp); 2295f521d7bSEd Schouten b64_from_24bit(alt_result[31], alt_result[52], alt_result[10], 4, &cp); 2305f521d7bSEd Schouten b64_from_24bit(alt_result[53], alt_result[11], alt_result[32], 4, &cp); 2315f521d7bSEd Schouten b64_from_24bit(alt_result[12], alt_result[33], alt_result[54], 4, &cp); 2325f521d7bSEd Schouten b64_from_24bit(alt_result[34], alt_result[55], alt_result[13], 4, &cp); 2335f521d7bSEd Schouten b64_from_24bit(alt_result[56], alt_result[14], alt_result[35], 4, &cp); 2345f521d7bSEd Schouten b64_from_24bit(alt_result[15], alt_result[36], alt_result[57], 4, &cp); 2355f521d7bSEd Schouten b64_from_24bit(alt_result[37], alt_result[58], alt_result[16], 4, &cp); 2365f521d7bSEd Schouten b64_from_24bit(alt_result[59], alt_result[17], alt_result[38], 4, &cp); 2375f521d7bSEd Schouten b64_from_24bit(alt_result[18], alt_result[39], alt_result[60], 4, &cp); 2385f521d7bSEd Schouten b64_from_24bit(alt_result[40], alt_result[61], alt_result[19], 4, &cp); 2395f521d7bSEd Schouten b64_from_24bit(alt_result[62], alt_result[20], alt_result[41], 4, &cp); 2405f521d7bSEd Schouten b64_from_24bit(0, 0, alt_result[63], 2, &cp); 2413d6f63c0SMark Murray 2423d6f63c0SMark Murray *cp = '\0'; /* Terminate the string. */ 2433d6f63c0SMark Murray 2443d6f63c0SMark Murray /* Clear the buffer for the intermediate result so that people 2453d6f63c0SMark Murray * attaching to processes or reading core dumps cannot get any 2463d6f63c0SMark Murray * information. We do it in this way to clear correct_words[] inside 2473d6f63c0SMark Murray * the SHA512 implementation as well. */ 2483d6f63c0SMark Murray SHA512_Init(&ctx); 2493d6f63c0SMark Murray SHA512_Final(alt_result, &ctx); 250*a2c0d202SRobert Clausecker explicit_bzero(temp_result, sizeof(temp_result)); 251*a2c0d202SRobert Clausecker explicit_bzero(p_bytes, key_len); 252*a2c0d202SRobert Clausecker explicit_bzero(s_bytes, salt_len); 2533d6f63c0SMark Murray 2545f521d7bSEd Schouten return (0); 2553d6f63c0SMark Murray } 2563d6f63c0SMark Murray 2573d6f63c0SMark Murray #ifdef TEST 2583d6f63c0SMark Murray 2593d6f63c0SMark Murray static const struct { 2603d6f63c0SMark Murray const char *input; 2613d6f63c0SMark Murray const char result[64]; 2623d6f63c0SMark Murray } tests[] = 2633d6f63c0SMark Murray { 2643d6f63c0SMark Murray /* Test vectors from FIPS 180-2: appendix C.1. */ 2653d6f63c0SMark Murray { 2663d6f63c0SMark Murray "abc", 2673d6f63c0SMark Murray "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41\x31" 2683d6f63c0SMark Murray "\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55\xd3\x9a" 2693d6f63c0SMark Murray "\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3\xfe\xeb\xbd" 2703d6f63c0SMark Murray "\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f\xa5\x4c\xa4\x9f" 2713d6f63c0SMark Murray }, 2723d6f63c0SMark Murray /* Test vectors from FIPS 180-2: appendix C.2. */ 2733d6f63c0SMark Murray { 2743d6f63c0SMark Murray "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" 2753d6f63c0SMark Murray "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 2763d6f63c0SMark Murray "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14\x3f" 2773d6f63c0SMark Murray "\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88\x90\x18" 2783d6f63c0SMark Murray "\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4\xb5\x43\x3a" 2793d6f63c0SMark Murray "\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b\x87\x4b\xe9\x09" 2803d6f63c0SMark Murray }, 2813d6f63c0SMark Murray /* Test vectors from the NESSIE project. */ 2823d6f63c0SMark Murray { 2833d6f63c0SMark Murray "", 2843d6f63c0SMark Murray "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80\x07" 2853d6f63c0SMark Murray "\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c\xe9\xce" 2863d6f63c0SMark Murray "\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87\x7e\xec\x2f" 2873d6f63c0SMark Murray "\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a\xf9\x27\xda\x3e" 2883d6f63c0SMark Murray }, 2893d6f63c0SMark Murray { 2903d6f63c0SMark Murray "a", 2913d6f63c0SMark Murray "\x1f\x40\xfc\x92\xda\x24\x16\x94\x75\x09\x79\xee\x6c\xf5\x82\xf2" 2923d6f63c0SMark Murray "\xd5\xd7\xd2\x8e\x18\x33\x5d\xe0\x5a\xbc\x54\xd0\x56\x0e\x0f\x53" 2933d6f63c0SMark Murray "\x02\x86\x0c\x65\x2b\xf0\x8d\x56\x02\x52\xaa\x5e\x74\x21\x05\x46" 2943d6f63c0SMark Murray "\xf3\x69\xfb\xbb\xce\x8c\x12\xcf\xc7\x95\x7b\x26\x52\xfe\x9a\x75" 2953d6f63c0SMark Murray }, 2963d6f63c0SMark Murray { 2973d6f63c0SMark Murray "message digest", 2983d6f63c0SMark Murray "\x10\x7d\xbf\x38\x9d\x9e\x9f\x71\xa3\xa9\x5f\x6c\x05\x5b\x92\x51" 2993d6f63c0SMark Murray "\xbc\x52\x68\xc2\xbe\x16\xd6\xc1\x34\x92\xea\x45\xb0\x19\x9f\x33" 3003d6f63c0SMark Murray "\x09\xe1\x64\x55\xab\x1e\x96\x11\x8e\x8a\x90\x5d\x55\x97\xb7\x20" 3013d6f63c0SMark Murray "\x38\xdd\xb3\x72\xa8\x98\x26\x04\x6d\xe6\x66\x87\xbb\x42\x0e\x7c" 3023d6f63c0SMark Murray }, 3033d6f63c0SMark Murray { 3043d6f63c0SMark Murray "abcdefghijklmnopqrstuvwxyz", 3053d6f63c0SMark Murray "\x4d\xbf\xf8\x6c\xc2\xca\x1b\xae\x1e\x16\x46\x8a\x05\xcb\x98\x81" 3063d6f63c0SMark Murray "\xc9\x7f\x17\x53\xbc\xe3\x61\x90\x34\x89\x8f\xaa\x1a\xab\xe4\x29" 3073d6f63c0SMark Murray "\x95\x5a\x1b\xf8\xec\x48\x3d\x74\x21\xfe\x3c\x16\x46\x61\x3a\x59" 3083d6f63c0SMark Murray "\xed\x54\x41\xfb\x0f\x32\x13\x89\xf7\x7f\x48\xa8\x79\xc7\xb1\xf1" 3093d6f63c0SMark Murray }, 3103d6f63c0SMark Murray { 3113d6f63c0SMark Murray "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 3123d6f63c0SMark Murray "\x20\x4a\x8f\xc6\xdd\xa8\x2f\x0a\x0c\xed\x7b\xeb\x8e\x08\xa4\x16" 3133d6f63c0SMark Murray "\x57\xc1\x6e\xf4\x68\xb2\x28\xa8\x27\x9b\xe3\x31\xa7\x03\xc3\x35" 3143d6f63c0SMark Murray "\x96\xfd\x15\xc1\x3b\x1b\x07\xf9\xaa\x1d\x3b\xea\x57\x78\x9c\xa0" 3153d6f63c0SMark Murray "\x31\xad\x85\xc7\xa7\x1d\xd7\x03\x54\xec\x63\x12\x38\xca\x34\x45" 3163d6f63c0SMark Murray }, 3173d6f63c0SMark Murray { 3183d6f63c0SMark Murray "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 3193d6f63c0SMark Murray "\x1e\x07\xbe\x23\xc2\x6a\x86\xea\x37\xea\x81\x0c\x8e\xc7\x80\x93" 3203d6f63c0SMark Murray "\x52\x51\x5a\x97\x0e\x92\x53\xc2\x6f\x53\x6c\xfc\x7a\x99\x96\xc4" 3213d6f63c0SMark Murray "\x5c\x83\x70\x58\x3e\x0a\x78\xfa\x4a\x90\x04\x1d\x71\xa4\xce\xab" 3223d6f63c0SMark Murray "\x74\x23\xf1\x9c\x71\xb9\xd5\xa3\xe0\x12\x49\xf0\xbe\xbd\x58\x94" 3233d6f63c0SMark Murray }, 3243d6f63c0SMark Murray { 3253d6f63c0SMark Murray "123456789012345678901234567890123456789012345678901234567890" 3263d6f63c0SMark Murray "12345678901234567890", 3273d6f63c0SMark Murray "\x72\xec\x1e\xf1\x12\x4a\x45\xb0\x47\xe8\xb7\xc7\x5a\x93\x21\x95" 3283d6f63c0SMark Murray "\x13\x5b\xb6\x1d\xe2\x4e\xc0\xd1\x91\x40\x42\x24\x6e\x0a\xec\x3a" 3293d6f63c0SMark Murray "\x23\x54\xe0\x93\xd7\x6f\x30\x48\xb4\x56\x76\x43\x46\x90\x0c\xb1" 3303d6f63c0SMark Murray "\x30\xd2\xa4\xfd\x5d\xd1\x6a\xbb\x5e\x30\xbc\xb8\x50\xde\xe8\x43" 3313d6f63c0SMark Murray } 3323d6f63c0SMark Murray }; 3333d6f63c0SMark Murray 3343d6f63c0SMark Murray #define ntests (sizeof (tests) / sizeof (tests[0])) 3353d6f63c0SMark Murray 3363d6f63c0SMark Murray static const struct { 3373d6f63c0SMark Murray const char *salt; 3383d6f63c0SMark Murray const char *input; 3393d6f63c0SMark Murray const char *expected; 3403d6f63c0SMark Murray } tests2[] = 3413d6f63c0SMark Murray { 3423d6f63c0SMark Murray { 3433d6f63c0SMark Murray "$6$saltstring", "Hello world!", 3443d6f63c0SMark Murray "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu" 3453d6f63c0SMark Murray "esI68u4OTLiBFdcbYEdFCoEOfaS35inz1" 3463d6f63c0SMark Murray }, 3473d6f63c0SMark Murray { 3483d6f63c0SMark Murray "$6$rounds=10000$saltstringsaltstring", "Hello world!", 3493d6f63c0SMark Murray "$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sb" 3503d6f63c0SMark Murray "HbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v." 3513d6f63c0SMark Murray }, 3523d6f63c0SMark Murray { 3533d6f63c0SMark Murray "$6$rounds=5000$toolongsaltstring", "This is just a test", 3543d6f63c0SMark Murray "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQ" 3553d6f63c0SMark Murray "zQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0" 3563d6f63c0SMark Murray }, 3573d6f63c0SMark Murray { 3583d6f63c0SMark Murray "$6$rounds=1400$anotherlongsaltstring", 3593d6f63c0SMark Murray "a very much longer text to encrypt. This one even stretches over more" 3603d6f63c0SMark Murray "than one line.", 3613d6f63c0SMark Murray "$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/pQs.wP" 3623d6f63c0SMark Murray "vMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1" 3633d6f63c0SMark Murray }, 3643d6f63c0SMark Murray { 3653d6f63c0SMark Murray "$6$rounds=77777$short", 3663d6f63c0SMark Murray "we have a short salt string but not a short password", 3673d6f63c0SMark Murray "$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXbkvr0g" 3683d6f63c0SMark Murray "ge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0" 3693d6f63c0SMark Murray }, 3703d6f63c0SMark Murray { 3713d6f63c0SMark Murray "$6$rounds=123456$asaltof16chars..", "a short string", 3723d6f63c0SMark Murray "$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4oPwc" 3733d6f63c0SMark Murray "elCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1" 3743d6f63c0SMark Murray }, 3753d6f63c0SMark Murray { 3763d6f63c0SMark Murray "$6$rounds=10$roundstoolow", "the minimum number is still observed", 3773d6f63c0SMark Murray "$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50YhH1x" 3783d6f63c0SMark Murray "hLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX." 3793d6f63c0SMark Murray }, 3803d6f63c0SMark Murray }; 3813d6f63c0SMark Murray 3823d6f63c0SMark Murray #define ntests2 (sizeof (tests2) / sizeof (tests2[0])) 3833d6f63c0SMark Murray 3843d6f63c0SMark Murray int 3853d6f63c0SMark Murray main(void) 3863d6f63c0SMark Murray { 3873d6f63c0SMark Murray SHA512_CTX ctx; 3883d6f63c0SMark Murray uint8_t sum[64]; 3893d6f63c0SMark Murray int result = 0; 3903d6f63c0SMark Murray int i, cnt; 3913d6f63c0SMark Murray 3923d6f63c0SMark Murray for (cnt = 0; cnt < (int)ntests; ++cnt) { 3933d6f63c0SMark Murray SHA512_Init(&ctx); 3943d6f63c0SMark Murray SHA512_Update(&ctx, tests[cnt].input, strlen(tests[cnt].input)); 3953d6f63c0SMark Murray SHA512_Final(sum, &ctx); 3963d6f63c0SMark Murray if (memcmp(tests[cnt].result, sum, 64) != 0) { 3973d6f63c0SMark Murray printf("test %d run %d failed\n", cnt, 1); 3983d6f63c0SMark Murray result = 1; 3993d6f63c0SMark Murray } 4003d6f63c0SMark Murray 4013d6f63c0SMark Murray SHA512_Init(&ctx); 4023d6f63c0SMark Murray for (i = 0; tests[cnt].input[i] != '\0'; ++i) 4033d6f63c0SMark Murray SHA512_Update(&ctx, &tests[cnt].input[i], 1); 4043d6f63c0SMark Murray SHA512_Final(sum, &ctx); 4053d6f63c0SMark Murray if (memcmp(tests[cnt].result, sum, 64) != 0) { 4063d6f63c0SMark Murray printf("test %d run %d failed\n", cnt, 2); 4073d6f63c0SMark Murray result = 1; 4083d6f63c0SMark Murray } 4093d6f63c0SMark Murray } 4103d6f63c0SMark Murray 4113d6f63c0SMark Murray /* Test vector from FIPS 180-2: appendix C.3. */ 4123d6f63c0SMark Murray char buf[1000]; 4133d6f63c0SMark Murray 4143d6f63c0SMark Murray memset(buf, 'a', sizeof(buf)); 4153d6f63c0SMark Murray SHA512_Init(&ctx); 4163d6f63c0SMark Murray for (i = 0; i < 1000; ++i) 4173d6f63c0SMark Murray SHA512_Update(&ctx, buf, sizeof(buf)); 4183d6f63c0SMark Murray SHA512_Final(sum, &ctx); 4193d6f63c0SMark Murray static const char expected[64] = 4203d6f63c0SMark Murray "\xe7\x18\x48\x3d\x0c\xe7\x69\x64\x4e\x2e\x42\xc7\xbc\x15\xb4\x63" 4213d6f63c0SMark Murray "\x8e\x1f\x98\xb1\x3b\x20\x44\x28\x56\x32\xa8\x03\xaf\xa9\x73\xeb" 4223d6f63c0SMark Murray "\xde\x0f\xf2\x44\x87\x7e\xa6\x0a\x4c\xb0\x43\x2c\xe5\x77\xc3\x1b" 4233d6f63c0SMark Murray "\xeb\x00\x9c\x5c\x2c\x49\xaa\x2e\x4e\xad\xb2\x17\xad\x8c\xc0\x9b"; 4243d6f63c0SMark Murray 4253d6f63c0SMark Murray if (memcmp(expected, sum, 64) != 0) { 4263d6f63c0SMark Murray printf("test %d failed\n", cnt); 4273d6f63c0SMark Murray result = 1; 4283d6f63c0SMark Murray } 4293d6f63c0SMark Murray 4303d6f63c0SMark Murray for (cnt = 0; cnt < ntests2; ++cnt) { 431ad45dd41SDavid E. O'Brien char *cp = crypt_sha512(tests2[cnt].input, tests2[cnt].salt); 4323d6f63c0SMark Murray 4333d6f63c0SMark Murray if (strcmp(cp, tests2[cnt].expected) != 0) { 4343d6f63c0SMark Murray printf("test %d: expected \"%s\", got \"%s\"\n", 4353d6f63c0SMark Murray cnt, tests2[cnt].expected, cp); 4363d6f63c0SMark Murray result = 1; 4373d6f63c0SMark Murray } 4383d6f63c0SMark Murray } 4393d6f63c0SMark Murray 4403d6f63c0SMark Murray if (result == 0) 4413d6f63c0SMark Murray puts("all tests OK"); 4423d6f63c0SMark Murray 4433d6f63c0SMark Murray return result; 4443d6f63c0SMark Murray } 4453d6f63c0SMark Murray 4463d6f63c0SMark Murray #endif /* TEST */ 447