crypt-sha512.c revision 220497
1220497Smarkm/* 2220497Smarkm * Copyright (c) 2011 The FreeBSD Project. All rights reserved. 3220497Smarkm * 4220497Smarkm * Redistribution and use in source and binary forms, with or without 5220497Smarkm * modification, are permitted provided that the following conditions 6220497Smarkm * are met: 7220497Smarkm * 1. Redistributions of source code must retain the above copyright 8220497Smarkm * notice, this list of conditions and the following disclaimer. 9220497Smarkm * 2. Redistributions in binary form must reproduce the above copyright 10220497Smarkm * notice, this list of conditions and the following disclaimer in the 11220497Smarkm * documentation and/or other materials provided with the distribution. 12220497Smarkm * 13220497Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14220497Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15220497Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16220497Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17220497Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18220497Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19220497Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20220497Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21220497Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22220497Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23220497Smarkm * SUCH DAMAGE. 24220497Smarkm */ 25220497Smarkm 26220497Smarkm/* Based on: 27220497Smarkm * SHA512-based Unix crypt implementation. Released into the Public Domain by 28220497Smarkm * Ulrich Drepper <drepper@redhat.com>. */ 29220497Smarkm 30220497Smarkm#include <sys/cdefs.h> 31220497Smarkm__FBSDID("$FreeBSD: head/lib/libcrypt/crypt-sha512.c 220497 2011-04-09 14:02:04Z markm $"); 32220497Smarkm 33220497Smarkm#include <sys/endian.h> 34220497Smarkm#include <sys/param.h> 35220497Smarkm 36220497Smarkm#include <errno.h> 37220497Smarkm#include <limits.h> 38220497Smarkm#include <sha512.h> 39220497Smarkm#include <stdbool.h> 40220497Smarkm#include <stdint.h> 41220497Smarkm#include <stdio.h> 42220497Smarkm#include <stdlib.h> 43220497Smarkm#include <string.h> 44220497Smarkm 45220497Smarkm#include "crypt.h" 46220497Smarkm 47220497Smarkm/* Define our magic string to mark salt for SHA512 "encryption" replacement. */ 48220497Smarkmstatic const char sha512_salt_prefix[] = "$6$"; 49220497Smarkm 50220497Smarkm/* Prefix for optional rounds specification. */ 51220497Smarkmstatic const char sha512_rounds_prefix[] = "rounds="; 52220497Smarkm 53220497Smarkm/* Maximum salt string length. */ 54220497Smarkm#define SALT_LEN_MAX 16 55220497Smarkm/* Default number of rounds if not explicitly specified. */ 56220497Smarkm#define ROUNDS_DEFAULT 5000 57220497Smarkm/* Minimum number of rounds. */ 58220497Smarkm#define ROUNDS_MIN 1000 59220497Smarkm/* Maximum number of rounds. */ 60220497Smarkm#define ROUNDS_MAX 999999999 61220497Smarkm 62220497Smarkmstatic char * 63220497Smarkmsha512_crypt_r(const char *key, const char *salt, char *buffer, int buflen) 64220497Smarkm{ 65220497Smarkm u_long srounds; 66220497Smarkm int n; 67220497Smarkm uint8_t alt_result[64], temp_result[64]; 68220497Smarkm SHA512_CTX ctx, alt_ctx; 69220497Smarkm size_t salt_len, key_len, cnt, rounds; 70220497Smarkm char *cp, *copied_key, *copied_salt, *p_bytes, *s_bytes, *endp; 71220497Smarkm const char *num; 72220497Smarkm bool rounds_custom; 73220497Smarkm 74220497Smarkm copied_key = NULL; 75220497Smarkm copied_salt = NULL; 76220497Smarkm 77220497Smarkm /* Default number of rounds. */ 78220497Smarkm rounds = ROUNDS_DEFAULT; 79220497Smarkm rounds_custom = false; 80220497Smarkm 81220497Smarkm /* Find beginning of salt string. The prefix should normally always 82220497Smarkm * be present. Just in case it is not. */ 83220497Smarkm if (strncmp(sha512_salt_prefix, salt, sizeof(sha512_salt_prefix) - 1) == 0) 84220497Smarkm /* Skip salt prefix. */ 85220497Smarkm salt += sizeof(sha512_salt_prefix) - 1; 86220497Smarkm 87220497Smarkm if (strncmp(salt, sha512_rounds_prefix, sizeof(sha512_rounds_prefix) - 1) 88220497Smarkm == 0) { 89220497Smarkm num = salt + sizeof(sha512_rounds_prefix) - 1; 90220497Smarkm srounds = strtoul(num, &endp, 10); 91220497Smarkm 92220497Smarkm if (*endp == '$') { 93220497Smarkm salt = endp + 1; 94220497Smarkm rounds = MAX(ROUNDS_MIN, MIN(srounds, ROUNDS_MAX)); 95220497Smarkm rounds_custom = true; 96220497Smarkm } 97220497Smarkm } 98220497Smarkm 99220497Smarkm salt_len = MIN(strcspn(salt, "$"), SALT_LEN_MAX); 100220497Smarkm key_len = strlen(key); 101220497Smarkm 102220497Smarkm /* Prepare for the real work. */ 103220497Smarkm SHA512_Init(&ctx); 104220497Smarkm 105220497Smarkm /* Add the key string. */ 106220497Smarkm SHA512_Update(&ctx, key, key_len); 107220497Smarkm 108220497Smarkm /* The last part is the salt string. This must be at most 8 109220497Smarkm * characters and it ends at the first `$' character (for 110220497Smarkm * compatibility with existing implementations). */ 111220497Smarkm SHA512_Update(&ctx, salt, salt_len); 112220497Smarkm 113220497Smarkm /* Compute alternate SHA512 sum with input KEY, SALT, and KEY. The 114220497Smarkm * final result will be added to the first context. */ 115220497Smarkm SHA512_Init(&alt_ctx); 116220497Smarkm 117220497Smarkm /* Add key. */ 118220497Smarkm SHA512_Update(&alt_ctx, key, key_len); 119220497Smarkm 120220497Smarkm /* Add salt. */ 121220497Smarkm SHA512_Update(&alt_ctx, salt, salt_len); 122220497Smarkm 123220497Smarkm /* Add key again. */ 124220497Smarkm SHA512_Update(&alt_ctx, key, key_len); 125220497Smarkm 126220497Smarkm /* Now get result of this (64 bytes) and add it to the other context. */ 127220497Smarkm SHA512_Final(alt_result, &alt_ctx); 128220497Smarkm 129220497Smarkm /* Add for any character in the key one byte of the alternate sum. */ 130220497Smarkm for (cnt = key_len; cnt > 64; cnt -= 64) 131220497Smarkm SHA512_Update(&ctx, alt_result, 64); 132220497Smarkm SHA512_Update(&ctx, alt_result, cnt); 133220497Smarkm 134220497Smarkm /* Take the binary representation of the length of the key and for 135220497Smarkm * every 1 add the alternate sum, for every 0 the key. */ 136220497Smarkm for (cnt = key_len; cnt > 0; cnt >>= 1) 137220497Smarkm if ((cnt & 1) != 0) 138220497Smarkm SHA512_Update(&ctx, alt_result, 64); 139220497Smarkm else 140220497Smarkm SHA512_Update(&ctx, key, key_len); 141220497Smarkm 142220497Smarkm /* Create intermediate result. */ 143220497Smarkm SHA512_Final(alt_result, &ctx); 144220497Smarkm 145220497Smarkm /* Start computation of P byte sequence. */ 146220497Smarkm SHA512_Init(&alt_ctx); 147220497Smarkm 148220497Smarkm /* For every character in the password add the entire password. */ 149220497Smarkm for (cnt = 0; cnt < key_len; ++cnt) 150220497Smarkm SHA512_Update(&alt_ctx, key, key_len); 151220497Smarkm 152220497Smarkm /* Finish the digest. */ 153220497Smarkm SHA512_Final(temp_result, &alt_ctx); 154220497Smarkm 155220497Smarkm /* Create byte sequence P. */ 156220497Smarkm cp = p_bytes = alloca(key_len); 157220497Smarkm for (cnt = key_len; cnt >= 64; cnt -= 64) { 158220497Smarkm memcpy(cp, temp_result, 64); 159220497Smarkm cp += 64; 160220497Smarkm } 161220497Smarkm memcpy(cp, temp_result, cnt); 162220497Smarkm 163220497Smarkm /* Start computation of S byte sequence. */ 164220497Smarkm SHA512_Init(&alt_ctx); 165220497Smarkm 166220497Smarkm /* For every character in the password add the entire password. */ 167220497Smarkm for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt) 168220497Smarkm SHA512_Update(&alt_ctx, salt, salt_len); 169220497Smarkm 170220497Smarkm /* Finish the digest. */ 171220497Smarkm SHA512_Final(temp_result, &alt_ctx); 172220497Smarkm 173220497Smarkm /* Create byte sequence S. */ 174220497Smarkm cp = s_bytes = alloca(salt_len); 175220497Smarkm for (cnt = salt_len; cnt >= 64; cnt -= 64) { 176220497Smarkm memcpy(cp, temp_result, 64); 177220497Smarkm cp += 64; 178220497Smarkm } 179220497Smarkm memcpy(cp, temp_result, cnt); 180220497Smarkm 181220497Smarkm /* Repeatedly run the collected hash value through SHA512 to burn CPU 182220497Smarkm * cycles. */ 183220497Smarkm for (cnt = 0; cnt < rounds; ++cnt) { 184220497Smarkm /* New context. */ 185220497Smarkm SHA512_Init(&ctx); 186220497Smarkm 187220497Smarkm /* Add key or last result. */ 188220497Smarkm if ((cnt & 1) != 0) 189220497Smarkm SHA512_Update(&ctx, p_bytes, key_len); 190220497Smarkm else 191220497Smarkm SHA512_Update(&ctx, alt_result, 64); 192220497Smarkm 193220497Smarkm /* Add salt for numbers not divisible by 3. */ 194220497Smarkm if (cnt % 3 != 0) 195220497Smarkm SHA512_Update(&ctx, s_bytes, salt_len); 196220497Smarkm 197220497Smarkm /* Add key for numbers not divisible by 7. */ 198220497Smarkm if (cnt % 7 != 0) 199220497Smarkm SHA512_Update(&ctx, p_bytes, key_len); 200220497Smarkm 201220497Smarkm /* Add key or last result. */ 202220497Smarkm if ((cnt & 1) != 0) 203220497Smarkm SHA512_Update(&ctx, alt_result, 64); 204220497Smarkm else 205220497Smarkm SHA512_Update(&ctx, p_bytes, key_len); 206220497Smarkm 207220497Smarkm /* Create intermediate result. */ 208220497Smarkm SHA512_Final(alt_result, &ctx); 209220497Smarkm } 210220497Smarkm 211220497Smarkm /* Now we can construct the result string. It consists of three 212220497Smarkm * parts. */ 213220497Smarkm cp = stpncpy(buffer, sha512_salt_prefix, MAX(0, buflen)); 214220497Smarkm buflen -= sizeof(sha512_salt_prefix) - 1; 215220497Smarkm 216220497Smarkm if (rounds_custom) { 217220497Smarkm n = snprintf(cp, MAX(0, buflen), "%s%zu$", 218220497Smarkm sha512_rounds_prefix, rounds); 219220497Smarkm 220220497Smarkm cp += n; 221220497Smarkm buflen -= n; 222220497Smarkm } 223220497Smarkm 224220497Smarkm cp = stpncpy(cp, salt, MIN((size_t)MAX(0, buflen), salt_len)); 225220497Smarkm buflen -= MIN((size_t)MAX(0, buflen), salt_len); 226220497Smarkm 227220497Smarkm if (buflen > 0) { 228220497Smarkm *cp++ = '$'; 229220497Smarkm --buflen; 230220497Smarkm } 231220497Smarkm 232220497Smarkm b64_from_24bit(alt_result[0], alt_result[21], alt_result[42], 4, &buflen, &cp); 233220497Smarkm b64_from_24bit(alt_result[22], alt_result[43], alt_result[1], 4, &buflen, &cp); 234220497Smarkm b64_from_24bit(alt_result[44], alt_result[2], alt_result[23], 4, &buflen, &cp); 235220497Smarkm b64_from_24bit(alt_result[3], alt_result[24], alt_result[45], 4, &buflen, &cp); 236220497Smarkm b64_from_24bit(alt_result[25], alt_result[46], alt_result[4], 4, &buflen, &cp); 237220497Smarkm b64_from_24bit(alt_result[47], alt_result[5], alt_result[26], 4, &buflen, &cp); 238220497Smarkm b64_from_24bit(alt_result[6], alt_result[27], alt_result[48], 4, &buflen, &cp); 239220497Smarkm b64_from_24bit(alt_result[28], alt_result[49], alt_result[7], 4, &buflen, &cp); 240220497Smarkm b64_from_24bit(alt_result[50], alt_result[8], alt_result[29], 4, &buflen, &cp); 241220497Smarkm b64_from_24bit(alt_result[9], alt_result[30], alt_result[51], 4, &buflen, &cp); 242220497Smarkm b64_from_24bit(alt_result[31], alt_result[52], alt_result[10], 4, &buflen, &cp); 243220497Smarkm b64_from_24bit(alt_result[53], alt_result[11], alt_result[32], 4, &buflen, &cp); 244220497Smarkm b64_from_24bit(alt_result[12], alt_result[33], alt_result[54], 4, &buflen, &cp); 245220497Smarkm b64_from_24bit(alt_result[34], alt_result[55], alt_result[13], 4, &buflen, &cp); 246220497Smarkm b64_from_24bit(alt_result[56], alt_result[14], alt_result[35], 4, &buflen, &cp); 247220497Smarkm b64_from_24bit(alt_result[15], alt_result[36], alt_result[57], 4, &buflen, &cp); 248220497Smarkm b64_from_24bit(alt_result[37], alt_result[58], alt_result[16], 4, &buflen, &cp); 249220497Smarkm b64_from_24bit(alt_result[59], alt_result[17], alt_result[38], 4, &buflen, &cp); 250220497Smarkm b64_from_24bit(alt_result[18], alt_result[39], alt_result[60], 4, &buflen, &cp); 251220497Smarkm b64_from_24bit(alt_result[40], alt_result[61], alt_result[19], 4, &buflen, &cp); 252220497Smarkm b64_from_24bit(alt_result[62], alt_result[20], alt_result[41], 4, &buflen, &cp); 253220497Smarkm b64_from_24bit(0, 0, alt_result[63], 2, &buflen, &cp); 254220497Smarkm 255220497Smarkm if (buflen <= 0) { 256220497Smarkm errno = ERANGE; 257220497Smarkm buffer = NULL; 258220497Smarkm } 259220497Smarkm else 260220497Smarkm *cp = '\0'; /* Terminate the string. */ 261220497Smarkm 262220497Smarkm /* Clear the buffer for the intermediate result so that people 263220497Smarkm * attaching to processes or reading core dumps cannot get any 264220497Smarkm * information. We do it in this way to clear correct_words[] inside 265220497Smarkm * the SHA512 implementation as well. */ 266220497Smarkm SHA512_Init(&ctx); 267220497Smarkm SHA512_Final(alt_result, &ctx); 268220497Smarkm memset(temp_result, '\0', sizeof(temp_result)); 269220497Smarkm memset(p_bytes, '\0', key_len); 270220497Smarkm memset(s_bytes, '\0', salt_len); 271220497Smarkm memset(&ctx, '\0', sizeof(ctx)); 272220497Smarkm memset(&alt_ctx, '\0', sizeof(alt_ctx)); 273220497Smarkm if (copied_key != NULL) 274220497Smarkm memset(copied_key, '\0', key_len); 275220497Smarkm if (copied_salt != NULL) 276220497Smarkm memset(copied_salt, '\0', salt_len); 277220497Smarkm 278220497Smarkm return buffer; 279220497Smarkm} 280220497Smarkm 281220497Smarkm/* This entry point is equivalent to crypt(3). */ 282220497Smarkmchar * 283220497Smarkmsha512_crypt(const char *key, const char *salt) 284220497Smarkm{ 285220497Smarkm /* We don't want to have an arbitrary limit in the size of the 286220497Smarkm * password. We can compute an upper bound for the size of the 287220497Smarkm * result in advance and so we can prepare the buffer we pass to 288220497Smarkm * `sha512_crypt_r'. */ 289220497Smarkm static char *buffer; 290220497Smarkm static int buflen; 291220497Smarkm int needed; 292220497Smarkm char *new_buffer; 293220497Smarkm 294220497Smarkm needed = (sizeof(sha512_salt_prefix) - 1 295220497Smarkm + sizeof(sha512_rounds_prefix) + 9 + 1 296220497Smarkm + strlen(salt) + 1 + 86 + 1); 297220497Smarkm 298220497Smarkm if (buflen < needed) { 299220497Smarkm new_buffer = (char *)realloc(buffer, needed); 300220497Smarkm 301220497Smarkm if (new_buffer == NULL) 302220497Smarkm return NULL; 303220497Smarkm 304220497Smarkm buffer = new_buffer; 305220497Smarkm buflen = needed; 306220497Smarkm } 307220497Smarkm 308220497Smarkm return sha512_crypt_r(key, salt, buffer, buflen); 309220497Smarkm} 310220497Smarkm 311220497Smarkm#ifdef TEST 312220497Smarkm 313220497Smarkmstatic const struct { 314220497Smarkm const char *input; 315220497Smarkm const char result[64]; 316220497Smarkm} tests[] = 317220497Smarkm{ 318220497Smarkm /* Test vectors from FIPS 180-2: appendix C.1. */ 319220497Smarkm { 320220497Smarkm "abc", 321220497Smarkm "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41\x31" 322220497Smarkm "\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55\xd3\x9a" 323220497Smarkm "\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3\xfe\xeb\xbd" 324220497Smarkm "\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f\xa5\x4c\xa4\x9f" 325220497Smarkm }, 326220497Smarkm /* Test vectors from FIPS 180-2: appendix C.2. */ 327220497Smarkm { 328220497Smarkm "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" 329220497Smarkm "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 330220497Smarkm "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14\x3f" 331220497Smarkm "\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88\x90\x18" 332220497Smarkm "\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4\xb5\x43\x3a" 333220497Smarkm "\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b\x87\x4b\xe9\x09" 334220497Smarkm }, 335220497Smarkm /* Test vectors from the NESSIE project. */ 336220497Smarkm { 337220497Smarkm "", 338220497Smarkm "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80\x07" 339220497Smarkm "\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c\xe9\xce" 340220497Smarkm "\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87\x7e\xec\x2f" 341220497Smarkm "\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a\xf9\x27\xda\x3e" 342220497Smarkm }, 343220497Smarkm { 344220497Smarkm "a", 345220497Smarkm "\x1f\x40\xfc\x92\xda\x24\x16\x94\x75\x09\x79\xee\x6c\xf5\x82\xf2" 346220497Smarkm "\xd5\xd7\xd2\x8e\x18\x33\x5d\xe0\x5a\xbc\x54\xd0\x56\x0e\x0f\x53" 347220497Smarkm "\x02\x86\x0c\x65\x2b\xf0\x8d\x56\x02\x52\xaa\x5e\x74\x21\x05\x46" 348220497Smarkm "\xf3\x69\xfb\xbb\xce\x8c\x12\xcf\xc7\x95\x7b\x26\x52\xfe\x9a\x75" 349220497Smarkm }, 350220497Smarkm { 351220497Smarkm "message digest", 352220497Smarkm "\x10\x7d\xbf\x38\x9d\x9e\x9f\x71\xa3\xa9\x5f\x6c\x05\x5b\x92\x51" 353220497Smarkm "\xbc\x52\x68\xc2\xbe\x16\xd6\xc1\x34\x92\xea\x45\xb0\x19\x9f\x33" 354220497Smarkm "\x09\xe1\x64\x55\xab\x1e\x96\x11\x8e\x8a\x90\x5d\x55\x97\xb7\x20" 355220497Smarkm "\x38\xdd\xb3\x72\xa8\x98\x26\x04\x6d\xe6\x66\x87\xbb\x42\x0e\x7c" 356220497Smarkm }, 357220497Smarkm { 358220497Smarkm "abcdefghijklmnopqrstuvwxyz", 359220497Smarkm "\x4d\xbf\xf8\x6c\xc2\xca\x1b\xae\x1e\x16\x46\x8a\x05\xcb\x98\x81" 360220497Smarkm "\xc9\x7f\x17\x53\xbc\xe3\x61\x90\x34\x89\x8f\xaa\x1a\xab\xe4\x29" 361220497Smarkm "\x95\x5a\x1b\xf8\xec\x48\x3d\x74\x21\xfe\x3c\x16\x46\x61\x3a\x59" 362220497Smarkm "\xed\x54\x41\xfb\x0f\x32\x13\x89\xf7\x7f\x48\xa8\x79\xc7\xb1\xf1" 363220497Smarkm }, 364220497Smarkm { 365220497Smarkm "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 366220497Smarkm "\x20\x4a\x8f\xc6\xdd\xa8\x2f\x0a\x0c\xed\x7b\xeb\x8e\x08\xa4\x16" 367220497Smarkm "\x57\xc1\x6e\xf4\x68\xb2\x28\xa8\x27\x9b\xe3\x31\xa7\x03\xc3\x35" 368220497Smarkm "\x96\xfd\x15\xc1\x3b\x1b\x07\xf9\xaa\x1d\x3b\xea\x57\x78\x9c\xa0" 369220497Smarkm "\x31\xad\x85\xc7\xa7\x1d\xd7\x03\x54\xec\x63\x12\x38\xca\x34\x45" 370220497Smarkm }, 371220497Smarkm { 372220497Smarkm "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 373220497Smarkm "\x1e\x07\xbe\x23\xc2\x6a\x86\xea\x37\xea\x81\x0c\x8e\xc7\x80\x93" 374220497Smarkm "\x52\x51\x5a\x97\x0e\x92\x53\xc2\x6f\x53\x6c\xfc\x7a\x99\x96\xc4" 375220497Smarkm "\x5c\x83\x70\x58\x3e\x0a\x78\xfa\x4a\x90\x04\x1d\x71\xa4\xce\xab" 376220497Smarkm "\x74\x23\xf1\x9c\x71\xb9\xd5\xa3\xe0\x12\x49\xf0\xbe\xbd\x58\x94" 377220497Smarkm }, 378220497Smarkm { 379220497Smarkm "123456789012345678901234567890123456789012345678901234567890" 380220497Smarkm "12345678901234567890", 381220497Smarkm "\x72\xec\x1e\xf1\x12\x4a\x45\xb0\x47\xe8\xb7\xc7\x5a\x93\x21\x95" 382220497Smarkm "\x13\x5b\xb6\x1d\xe2\x4e\xc0\xd1\x91\x40\x42\x24\x6e\x0a\xec\x3a" 383220497Smarkm "\x23\x54\xe0\x93\xd7\x6f\x30\x48\xb4\x56\x76\x43\x46\x90\x0c\xb1" 384220497Smarkm "\x30\xd2\xa4\xfd\x5d\xd1\x6a\xbb\x5e\x30\xbc\xb8\x50\xde\xe8\x43" 385220497Smarkm } 386220497Smarkm}; 387220497Smarkm 388220497Smarkm#define ntests (sizeof (tests) / sizeof (tests[0])) 389220497Smarkm 390220497Smarkmstatic const struct { 391220497Smarkm const char *salt; 392220497Smarkm const char *input; 393220497Smarkm const char *expected; 394220497Smarkm} tests2[] = 395220497Smarkm{ 396220497Smarkm { 397220497Smarkm "$6$saltstring", "Hello world!", 398220497Smarkm "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu" 399220497Smarkm "esI68u4OTLiBFdcbYEdFCoEOfaS35inz1" 400220497Smarkm }, 401220497Smarkm { 402220497Smarkm "$6$rounds=10000$saltstringsaltstring", "Hello world!", 403220497Smarkm "$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sb" 404220497Smarkm "HbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v." 405220497Smarkm }, 406220497Smarkm { 407220497Smarkm "$6$rounds=5000$toolongsaltstring", "This is just a test", 408220497Smarkm "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQ" 409220497Smarkm "zQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0" 410220497Smarkm }, 411220497Smarkm { 412220497Smarkm "$6$rounds=1400$anotherlongsaltstring", 413220497Smarkm "a very much longer text to encrypt. This one even stretches over more" 414220497Smarkm "than one line.", 415220497Smarkm "$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/pQs.wP" 416220497Smarkm "vMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1" 417220497Smarkm }, 418220497Smarkm { 419220497Smarkm "$6$rounds=77777$short", 420220497Smarkm "we have a short salt string but not a short password", 421220497Smarkm "$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXbkvr0g" 422220497Smarkm "ge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0" 423220497Smarkm }, 424220497Smarkm { 425220497Smarkm "$6$rounds=123456$asaltof16chars..", "a short string", 426220497Smarkm "$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4oPwc" 427220497Smarkm "elCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1" 428220497Smarkm }, 429220497Smarkm { 430220497Smarkm "$6$rounds=10$roundstoolow", "the minimum number is still observed", 431220497Smarkm "$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50YhH1x" 432220497Smarkm "hLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX." 433220497Smarkm }, 434220497Smarkm}; 435220497Smarkm 436220497Smarkm#define ntests2 (sizeof (tests2) / sizeof (tests2[0])) 437220497Smarkm 438220497Smarkmint 439220497Smarkmmain(void) 440220497Smarkm{ 441220497Smarkm SHA512_CTX ctx; 442220497Smarkm uint8_t sum[64]; 443220497Smarkm int result = 0; 444220497Smarkm int i, cnt; 445220497Smarkm 446220497Smarkm for (cnt = 0; cnt < (int)ntests; ++cnt) { 447220497Smarkm SHA512_Init(&ctx); 448220497Smarkm SHA512_Update(&ctx, tests[cnt].input, strlen(tests[cnt].input)); 449220497Smarkm SHA512_Final(sum, &ctx); 450220497Smarkm if (memcmp(tests[cnt].result, sum, 64) != 0) { 451220497Smarkm printf("test %d run %d failed\n", cnt, 1); 452220497Smarkm result = 1; 453220497Smarkm } 454220497Smarkm 455220497Smarkm SHA512_Init(&ctx); 456220497Smarkm for (i = 0; tests[cnt].input[i] != '\0'; ++i) 457220497Smarkm SHA512_Update(&ctx, &tests[cnt].input[i], 1); 458220497Smarkm SHA512_Final(sum, &ctx); 459220497Smarkm if (memcmp(tests[cnt].result, sum, 64) != 0) { 460220497Smarkm printf("test %d run %d failed\n", cnt, 2); 461220497Smarkm result = 1; 462220497Smarkm } 463220497Smarkm } 464220497Smarkm 465220497Smarkm /* Test vector from FIPS 180-2: appendix C.3. */ 466220497Smarkm char buf[1000]; 467220497Smarkm 468220497Smarkm memset(buf, 'a', sizeof(buf)); 469220497Smarkm SHA512_Init(&ctx); 470220497Smarkm for (i = 0; i < 1000; ++i) 471220497Smarkm SHA512_Update(&ctx, buf, sizeof(buf)); 472220497Smarkm SHA512_Final(sum, &ctx); 473220497Smarkm static const char expected[64] = 474220497Smarkm "\xe7\x18\x48\x3d\x0c\xe7\x69\x64\x4e\x2e\x42\xc7\xbc\x15\xb4\x63" 475220497Smarkm "\x8e\x1f\x98\xb1\x3b\x20\x44\x28\x56\x32\xa8\x03\xaf\xa9\x73\xeb" 476220497Smarkm "\xde\x0f\xf2\x44\x87\x7e\xa6\x0a\x4c\xb0\x43\x2c\xe5\x77\xc3\x1b" 477220497Smarkm "\xeb\x00\x9c\x5c\x2c\x49\xaa\x2e\x4e\xad\xb2\x17\xad\x8c\xc0\x9b"; 478220497Smarkm 479220497Smarkm if (memcmp(expected, sum, 64) != 0) { 480220497Smarkm printf("test %d failed\n", cnt); 481220497Smarkm result = 1; 482220497Smarkm } 483220497Smarkm 484220497Smarkm for (cnt = 0; cnt < ntests2; ++cnt) { 485220497Smarkm char *cp = sha512_crypt(tests2[cnt].input, tests2[cnt].salt); 486220497Smarkm 487220497Smarkm if (strcmp(cp, tests2[cnt].expected) != 0) { 488220497Smarkm printf("test %d: expected \"%s\", got \"%s\"\n", 489220497Smarkm cnt, tests2[cnt].expected, cp); 490220497Smarkm result = 1; 491220497Smarkm } 492220497Smarkm } 493220497Smarkm 494220497Smarkm if (result == 0) 495220497Smarkm puts("all tests OK"); 496220497Smarkm 497220497Smarkm return result; 498220497Smarkm} 499220497Smarkm 500220497Smarkm#endif /* TEST */ 501