xref: /freebsd/lib/libcrypt/crypt-sha512.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 The FreeBSD Project. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /* Based on:
29  * SHA512-based Unix crypt implementation. Released into the Public Domain by
30  * Ulrich Drepper <drepper@redhat.com>. */
31 
32 #include <sys/cdefs.h>
33 #include <sys/endian.h>
34 #include <sys/param.h>
35 
36 #include <errno.h>
37 #include <limits.h>
38 #include <sha512.h>
39 #include <stdbool.h>
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "crypt.h"
46 
47 /* Define our magic string to mark salt for SHA512 "encryption" replacement. */
48 static const char sha512_salt_prefix[] = "$6$";
49 
50 /* Prefix for optional rounds specification. */
51 static const char sha512_rounds_prefix[] = "rounds=";
52 
53 /* Maximum salt string length. */
54 #define SALT_LEN_MAX 16
55 /* Default number of rounds if not explicitly specified. */
56 #define ROUNDS_DEFAULT 5000
57 /* Minimum number of rounds. */
58 #define ROUNDS_MIN 1000
59 /* Maximum number of rounds. */
60 #define ROUNDS_MAX 999999999
61 
62 int
63 crypt_sha512(const char *key, const char *salt, char *buffer)
64 {
65 	u_long srounds;
66 	uint8_t alt_result[64], temp_result[64];
67 	SHA512_CTX ctx, alt_ctx;
68 	size_t salt_len, key_len, cnt, rounds;
69 	char *cp, *p_bytes, *s_bytes, *endp;
70 	const char *num;
71 	bool rounds_custom;
72 
73 	/* Default number of rounds. */
74 	rounds = ROUNDS_DEFAULT;
75 	rounds_custom = false;
76 
77 	/* Find beginning of salt string. The prefix should normally always
78 	 * be present. Just in case it is not. */
79 	if (strncmp(sha512_salt_prefix, salt, sizeof(sha512_salt_prefix) - 1) == 0)
80 		/* Skip salt prefix. */
81 		salt += sizeof(sha512_salt_prefix) - 1;
82 
83 	if (strncmp(salt, sha512_rounds_prefix, sizeof(sha512_rounds_prefix) - 1)
84 	    == 0) {
85 		num = salt + sizeof(sha512_rounds_prefix) - 1;
86 		srounds = strtoul(num, &endp, 10);
87 
88 		if (*endp == '$') {
89 			salt = endp + 1;
90 			rounds = MAX(ROUNDS_MIN, MIN(srounds, ROUNDS_MAX));
91 			rounds_custom = true;
92 		}
93 	}
94 
95 	salt_len = MIN(strcspn(salt, "$"), SALT_LEN_MAX);
96 	key_len = strlen(key);
97 
98 	/* Prepare for the real work. */
99 	SHA512_Init(&ctx);
100 
101 	/* Add the key string. */
102 	SHA512_Update(&ctx, key, key_len);
103 
104 	/* The last part is the salt string. This must be at most 8
105 	 * characters and it ends at the first `$' character (for
106 	 * compatibility with existing implementations). */
107 	SHA512_Update(&ctx, salt, salt_len);
108 
109 	/* Compute alternate SHA512 sum with input KEY, SALT, and KEY. The
110 	 * final result will be added to the first context. */
111 	SHA512_Init(&alt_ctx);
112 
113 	/* Add key. */
114 	SHA512_Update(&alt_ctx, key, key_len);
115 
116 	/* Add salt. */
117 	SHA512_Update(&alt_ctx, salt, salt_len);
118 
119 	/* Add key again. */
120 	SHA512_Update(&alt_ctx, key, key_len);
121 
122 	/* Now get result of this (64 bytes) and add it to the other context. */
123 	SHA512_Final(alt_result, &alt_ctx);
124 
125 	/* Add for any character in the key one byte of the alternate sum. */
126 	for (cnt = key_len; cnt > 64; cnt -= 64)
127 		SHA512_Update(&ctx, alt_result, 64);
128 	SHA512_Update(&ctx, alt_result, cnt);
129 
130 	/* Take the binary representation of the length of the key and for
131 	 * every 1 add the alternate sum, for every 0 the key. */
132 	for (cnt = key_len; cnt > 0; cnt >>= 1)
133 		if ((cnt & 1) != 0)
134 			SHA512_Update(&ctx, alt_result, 64);
135 		else
136 			SHA512_Update(&ctx, key, key_len);
137 
138 	/* Create intermediate result. */
139 	SHA512_Final(alt_result, &ctx);
140 
141 	/* Start computation of P byte sequence. */
142 	SHA512_Init(&alt_ctx);
143 
144 	/* For every character in the password add the entire password. */
145 	for (cnt = 0; cnt < key_len; ++cnt)
146 		SHA512_Update(&alt_ctx, key, key_len);
147 
148 	/* Finish the digest. */
149 	SHA512_Final(temp_result, &alt_ctx);
150 
151 	/* Create byte sequence P. */
152 	cp = p_bytes = alloca(key_len);
153 	for (cnt = key_len; cnt >= 64; cnt -= 64) {
154 		memcpy(cp, temp_result, 64);
155 		cp += 64;
156 	}
157 	memcpy(cp, temp_result, cnt);
158 
159 	/* Start computation of S byte sequence. */
160 	SHA512_Init(&alt_ctx);
161 
162 	/* For every character in the password add the entire password. */
163 	for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt)
164 		SHA512_Update(&alt_ctx, salt, salt_len);
165 
166 	/* Finish the digest. */
167 	SHA512_Final(temp_result, &alt_ctx);
168 
169 	/* Create byte sequence S. */
170 	cp = s_bytes = alloca(salt_len);
171 	for (cnt = salt_len; cnt >= 64; cnt -= 64) {
172 		memcpy(cp, temp_result, 64);
173 		cp += 64;
174 	}
175 	memcpy(cp, temp_result, cnt);
176 
177 	/* Repeatedly run the collected hash value through SHA512 to burn CPU
178 	 * cycles. */
179 	for (cnt = 0; cnt < rounds; ++cnt) {
180 		/* New context. */
181 		SHA512_Init(&ctx);
182 
183 		/* Add key or last result. */
184 		if ((cnt & 1) != 0)
185 			SHA512_Update(&ctx, p_bytes, key_len);
186 		else
187 			SHA512_Update(&ctx, alt_result, 64);
188 
189 		/* Add salt for numbers not divisible by 3. */
190 		if (cnt % 3 != 0)
191 			SHA512_Update(&ctx, s_bytes, salt_len);
192 
193 		/* Add key for numbers not divisible by 7. */
194 		if (cnt % 7 != 0)
195 			SHA512_Update(&ctx, p_bytes, key_len);
196 
197 		/* Add key or last result. */
198 		if ((cnt & 1) != 0)
199 			SHA512_Update(&ctx, alt_result, 64);
200 		else
201 			SHA512_Update(&ctx, p_bytes, key_len);
202 
203 		/* Create intermediate result. */
204 		SHA512_Final(alt_result, &ctx);
205 	}
206 
207 	/* Now we can construct the result string. It consists of three
208 	 * parts. */
209 	cp = stpcpy(buffer, sha512_salt_prefix);
210 
211 	if (rounds_custom)
212 		cp += sprintf(cp, "%s%zu$", sha512_rounds_prefix, rounds);
213 
214 	cp = stpncpy(cp, salt, salt_len);
215 
216 	*cp++ = '$';
217 
218 	b64_from_24bit(alt_result[0], alt_result[21], alt_result[42], 4, &cp);
219 	b64_from_24bit(alt_result[22], alt_result[43], alt_result[1], 4, &cp);
220 	b64_from_24bit(alt_result[44], alt_result[2], alt_result[23], 4, &cp);
221 	b64_from_24bit(alt_result[3], alt_result[24], alt_result[45], 4, &cp);
222 	b64_from_24bit(alt_result[25], alt_result[46], alt_result[4], 4, &cp);
223 	b64_from_24bit(alt_result[47], alt_result[5], alt_result[26], 4, &cp);
224 	b64_from_24bit(alt_result[6], alt_result[27], alt_result[48], 4, &cp);
225 	b64_from_24bit(alt_result[28], alt_result[49], alt_result[7], 4, &cp);
226 	b64_from_24bit(alt_result[50], alt_result[8], alt_result[29], 4, &cp);
227 	b64_from_24bit(alt_result[9], alt_result[30], alt_result[51], 4, &cp);
228 	b64_from_24bit(alt_result[31], alt_result[52], alt_result[10], 4, &cp);
229 	b64_from_24bit(alt_result[53], alt_result[11], alt_result[32], 4, &cp);
230 	b64_from_24bit(alt_result[12], alt_result[33], alt_result[54], 4, &cp);
231 	b64_from_24bit(alt_result[34], alt_result[55], alt_result[13], 4, &cp);
232 	b64_from_24bit(alt_result[56], alt_result[14], alt_result[35], 4, &cp);
233 	b64_from_24bit(alt_result[15], alt_result[36], alt_result[57], 4, &cp);
234 	b64_from_24bit(alt_result[37], alt_result[58], alt_result[16], 4, &cp);
235 	b64_from_24bit(alt_result[59], alt_result[17], alt_result[38], 4, &cp);
236 	b64_from_24bit(alt_result[18], alt_result[39], alt_result[60], 4, &cp);
237 	b64_from_24bit(alt_result[40], alt_result[61], alt_result[19], 4, &cp);
238 	b64_from_24bit(alt_result[62], alt_result[20], alt_result[41], 4, &cp);
239 	b64_from_24bit(0, 0, alt_result[63], 2, &cp);
240 
241 	*cp = '\0';	/* Terminate the string. */
242 
243 	/* Clear the buffer for the intermediate result so that people
244 	 * attaching to processes or reading core dumps cannot get any
245 	 * information. We do it in this way to clear correct_words[] inside
246 	 * the SHA512 implementation as well. */
247 	SHA512_Init(&ctx);
248 	SHA512_Final(alt_result, &ctx);
249 	memset(temp_result, '\0', sizeof(temp_result));
250 	memset(p_bytes, '\0', key_len);
251 	memset(s_bytes, '\0', salt_len);
252 
253 	return (0);
254 }
255 
256 #ifdef TEST
257 
258 static const struct {
259 	const char *input;
260 	const char result[64];
261 } tests[] =
262 {
263 	/* Test vectors from FIPS 180-2: appendix C.1. */
264 	{
265 		"abc",
266 		"\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41\x31"
267 		"\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55\xd3\x9a"
268 		"\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3\xfe\xeb\xbd"
269 		"\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f\xa5\x4c\xa4\x9f"
270 	},
271 	/* Test vectors from FIPS 180-2: appendix C.2. */
272 	{
273 		"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
274 		"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
275 		"\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14\x3f"
276 		"\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88\x90\x18"
277 		"\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4\xb5\x43\x3a"
278 		"\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b\x87\x4b\xe9\x09"
279 	},
280 	/* Test vectors from the NESSIE project. */
281 	{
282 		"",
283 		"\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80\x07"
284 		"\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c\xe9\xce"
285 		"\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87\x7e\xec\x2f"
286 		"\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a\xf9\x27\xda\x3e"
287 	},
288 	{
289 		"a",
290 		"\x1f\x40\xfc\x92\xda\x24\x16\x94\x75\x09\x79\xee\x6c\xf5\x82\xf2"
291 		"\xd5\xd7\xd2\x8e\x18\x33\x5d\xe0\x5a\xbc\x54\xd0\x56\x0e\x0f\x53"
292 		"\x02\x86\x0c\x65\x2b\xf0\x8d\x56\x02\x52\xaa\x5e\x74\x21\x05\x46"
293 		"\xf3\x69\xfb\xbb\xce\x8c\x12\xcf\xc7\x95\x7b\x26\x52\xfe\x9a\x75"
294 	},
295 	{
296 		"message digest",
297 		"\x10\x7d\xbf\x38\x9d\x9e\x9f\x71\xa3\xa9\x5f\x6c\x05\x5b\x92\x51"
298 		"\xbc\x52\x68\xc2\xbe\x16\xd6\xc1\x34\x92\xea\x45\xb0\x19\x9f\x33"
299 		"\x09\xe1\x64\x55\xab\x1e\x96\x11\x8e\x8a\x90\x5d\x55\x97\xb7\x20"
300 		"\x38\xdd\xb3\x72\xa8\x98\x26\x04\x6d\xe6\x66\x87\xbb\x42\x0e\x7c"
301 	},
302 	{
303 		"abcdefghijklmnopqrstuvwxyz",
304 		"\x4d\xbf\xf8\x6c\xc2\xca\x1b\xae\x1e\x16\x46\x8a\x05\xcb\x98\x81"
305 		"\xc9\x7f\x17\x53\xbc\xe3\x61\x90\x34\x89\x8f\xaa\x1a\xab\xe4\x29"
306 		"\x95\x5a\x1b\xf8\xec\x48\x3d\x74\x21\xfe\x3c\x16\x46\x61\x3a\x59"
307 		"\xed\x54\x41\xfb\x0f\x32\x13\x89\xf7\x7f\x48\xa8\x79\xc7\xb1\xf1"
308 	},
309 	{
310 		"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
311 		"\x20\x4a\x8f\xc6\xdd\xa8\x2f\x0a\x0c\xed\x7b\xeb\x8e\x08\xa4\x16"
312 		"\x57\xc1\x6e\xf4\x68\xb2\x28\xa8\x27\x9b\xe3\x31\xa7\x03\xc3\x35"
313 		"\x96\xfd\x15\xc1\x3b\x1b\x07\xf9\xaa\x1d\x3b\xea\x57\x78\x9c\xa0"
314 		"\x31\xad\x85\xc7\xa7\x1d\xd7\x03\x54\xec\x63\x12\x38\xca\x34\x45"
315 	},
316 	{
317 		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
318 		"\x1e\x07\xbe\x23\xc2\x6a\x86\xea\x37\xea\x81\x0c\x8e\xc7\x80\x93"
319 		"\x52\x51\x5a\x97\x0e\x92\x53\xc2\x6f\x53\x6c\xfc\x7a\x99\x96\xc4"
320 		"\x5c\x83\x70\x58\x3e\x0a\x78\xfa\x4a\x90\x04\x1d\x71\xa4\xce\xab"
321 		"\x74\x23\xf1\x9c\x71\xb9\xd5\xa3\xe0\x12\x49\xf0\xbe\xbd\x58\x94"
322 	},
323 	{
324 		"123456789012345678901234567890123456789012345678901234567890"
325 		"12345678901234567890",
326 		"\x72\xec\x1e\xf1\x12\x4a\x45\xb0\x47\xe8\xb7\xc7\x5a\x93\x21\x95"
327 		"\x13\x5b\xb6\x1d\xe2\x4e\xc0\xd1\x91\x40\x42\x24\x6e\x0a\xec\x3a"
328 		"\x23\x54\xe0\x93\xd7\x6f\x30\x48\xb4\x56\x76\x43\x46\x90\x0c\xb1"
329 		"\x30\xd2\xa4\xfd\x5d\xd1\x6a\xbb\x5e\x30\xbc\xb8\x50\xde\xe8\x43"
330 	}
331 };
332 
333 #define ntests (sizeof (tests) / sizeof (tests[0]))
334 
335 static const struct {
336 	const char *salt;
337 	const char *input;
338 	const char *expected;
339 } tests2[] =
340 {
341 	{
342 		"$6$saltstring", "Hello world!",
343 		"$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu"
344 		"esI68u4OTLiBFdcbYEdFCoEOfaS35inz1"
345 	},
346 	{
347 		"$6$rounds=10000$saltstringsaltstring", "Hello world!",
348 		"$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sb"
349 		"HbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v."
350 	},
351 	{
352 		"$6$rounds=5000$toolongsaltstring", "This is just a test",
353 		"$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQ"
354 		"zQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0"
355 	},
356 	{
357 		"$6$rounds=1400$anotherlongsaltstring",
358 		"a very much longer text to encrypt.  This one even stretches over more"
359 		"than one line.",
360 		"$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/pQs.wP"
361 		"vMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1"
362 	},
363 	{
364 		"$6$rounds=77777$short",
365 		"we have a short salt string but not a short password",
366 		"$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXbkvr0g"
367 		"ge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0"
368 	},
369 	{
370 		"$6$rounds=123456$asaltof16chars..", "a short string",
371 		"$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4oPwc"
372 		"elCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1"
373 	},
374 	{
375 		"$6$rounds=10$roundstoolow", "the minimum number is still observed",
376 		"$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50YhH1x"
377 		"hLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX."
378 	},
379 };
380 
381 #define ntests2 (sizeof (tests2) / sizeof (tests2[0]))
382 
383 int
384 main(void)
385 {
386 	SHA512_CTX ctx;
387 	uint8_t sum[64];
388 	int result = 0;
389 	int i, cnt;
390 
391 	for (cnt = 0; cnt < (int)ntests; ++cnt) {
392 		SHA512_Init(&ctx);
393 		SHA512_Update(&ctx, tests[cnt].input, strlen(tests[cnt].input));
394 		SHA512_Final(sum, &ctx);
395 		if (memcmp(tests[cnt].result, sum, 64) != 0) {
396 			printf("test %d run %d failed\n", cnt, 1);
397 			result = 1;
398 		}
399 
400 		SHA512_Init(&ctx);
401 		for (i = 0; tests[cnt].input[i] != '\0'; ++i)
402 			SHA512_Update(&ctx, &tests[cnt].input[i], 1);
403 		SHA512_Final(sum, &ctx);
404 		if (memcmp(tests[cnt].result, sum, 64) != 0) {
405 			printf("test %d run %d failed\n", cnt, 2);
406 			result = 1;
407 		}
408 	}
409 
410 	/* Test vector from FIPS 180-2: appendix C.3. */
411 	char buf[1000];
412 
413 	memset(buf, 'a', sizeof(buf));
414 	SHA512_Init(&ctx);
415 	for (i = 0; i < 1000; ++i)
416 		SHA512_Update(&ctx, buf, sizeof(buf));
417 	SHA512_Final(sum, &ctx);
418 	static const char expected[64] =
419 	"\xe7\x18\x48\x3d\x0c\xe7\x69\x64\x4e\x2e\x42\xc7\xbc\x15\xb4\x63"
420 	"\x8e\x1f\x98\xb1\x3b\x20\x44\x28\x56\x32\xa8\x03\xaf\xa9\x73\xeb"
421 	"\xde\x0f\xf2\x44\x87\x7e\xa6\x0a\x4c\xb0\x43\x2c\xe5\x77\xc3\x1b"
422 	"\xeb\x00\x9c\x5c\x2c\x49\xaa\x2e\x4e\xad\xb2\x17\xad\x8c\xc0\x9b";
423 
424 	if (memcmp(expected, sum, 64) != 0) {
425 		printf("test %d failed\n", cnt);
426 		result = 1;
427 	}
428 
429 	for (cnt = 0; cnt < ntests2; ++cnt) {
430 		char *cp = crypt_sha512(tests2[cnt].input, tests2[cnt].salt);
431 
432 		if (strcmp(cp, tests2[cnt].expected) != 0) {
433 			printf("test %d: expected \"%s\", got \"%s\"\n",
434 			       cnt, tests2[cnt].expected, cp);
435 			result = 1;
436 		}
437 	}
438 
439 	if (result == 0)
440 		puts("all tests OK");
441 
442 	return result;
443 }
444 
445 #endif /* TEST */
446