blob: 24d1345a223075b830a965cef5b1308b11969a12 [file] [log] [blame]
Steffen Jaeckel878c91b2021-07-08 15:57:34 +02001// SPDX-License-Identifier: CC0-1.0
2/* Based on libxcrypt v4.4.17-0-g6b110bc */
Steffen Jaeckel229bd512021-07-08 15:57:33 +02003/* One way encryption based on the SHA512-based Unix crypt implementation.
4 *
5 * Written by Ulrich Drepper <drepper at redhat.com> in 2007 [1].
6 * Modified by Zack Weinberg <zackw at panix.com> in 2017, 2018.
7 * Composed by Björn Esser <besser82 at fedoraproject.org> in 2018.
8 * Modified by Björn Esser <besser82 at fedoraproject.org> in 2020.
Steffen Jaeckel878c91b2021-07-08 15:57:34 +02009 * Modified by Steffen Jaeckel <jaeckel-floss at eyet-services.de> in 2021
10 * for U-Boot, instead of using the global errno to use a static one
11 * inside this file.
Steffen Jaeckel229bd512021-07-08 15:57:33 +020012 * To the extent possible under law, the named authors have waived all
13 * copyright and related or neighboring rights to this work.
14 *
15 * See https://creativecommons.org/publicdomain/zero/1.0/ for further
16 * details.
17 *
18 * This file is a modified except from [2], lines 1403 up to 1676.
19 *
20 * [1] https://www.akkadia.org/drepper/sha-crypt.html
21 * [2] https://www.akkadia.org/drepper/SHA-crypt.txt
22 */
23
24#include "crypt-port.h"
25#include "alg-sha512.h"
26
Steffen Jaeckel878c91b2021-07-08 15:57:34 +020027#include <linux/errno.h>
Steffen Jaeckel229bd512021-07-08 15:57:33 +020028#include <stdio.h>
29#include <stdlib.h>
30
31#if INCLUDE_sha512crypt
32
33/* Define our magic string to mark salt for SHA512 "encryption"
34 replacement. */
35static const char sha512_salt_prefix[] = "$6$";
36
37/* Prefix for optional rounds specification. */
38static const char sha512_rounds_prefix[] = "rounds=";
39
40/* Maximum salt string length. */
41#define SALT_LEN_MAX 16
42/* Default number of rounds if not explicitly specified. */
43#define ROUNDS_DEFAULT 5000
44/* Minimum number of rounds. */
45#define ROUNDS_MIN 1000
46/* Maximum number of rounds. */
47#define ROUNDS_MAX 999999999
48
49/* The maximum possible length of a SHA512-hashed password string,
50 including the terminating NUL character. Prefix (including its NUL)
51 + rounds tag ("rounds=$" = "rounds=\0") + strlen(ROUNDS_MAX)
52 + salt (up to SALT_LEN_MAX chars) + '$' + hash (86 chars). */
53
54#define LENGTH_OF_NUMBER(n) (sizeof #n - 1)
55
56#define SHA512_HASH_LENGTH \
57 (sizeof (sha512_salt_prefix) + sizeof (sha512_rounds_prefix) + \
58 LENGTH_OF_NUMBER (ROUNDS_MAX) + SALT_LEN_MAX + 1 + 86)
59
60static_assert (SHA512_HASH_LENGTH <= CRYPT_OUTPUT_SIZE,
61 "CRYPT_OUTPUT_SIZE is too small for SHA512");
62
63/* A sha512_buffer holds all of the sensitive intermediate data. */
64struct sha512_buffer
65{
66 SHA512_CTX ctx;
67 uint8_t result[64];
68 uint8_t p_bytes[64];
69 uint8_t s_bytes[64];
70};
71
72static_assert (sizeof (struct sha512_buffer) <= ALG_SPECIFIC_SIZE,
73 "ALG_SPECIFIC_SIZE is too small for SHA512");
74
Steffen Jaeckel878c91b2021-07-08 15:57:34 +020075/* Use this instead of including errno.h */
76static int errno;
77
78void crypt_sha512crypt_rn(const char *phrase, size_t phr_size,
79 const char *setting, size_t ARG_UNUSED(set_size),
80 uint8_t *output, size_t out_size, void *scratch,
81 size_t scr_size);
82
83int crypt_sha512crypt_rn_wrapped(const char *phrase, size_t phr_size,
84 const char *setting, size_t set_size,
85 u8 *output, size_t out_size, void *scratch,
86 size_t scr_size)
87{
88 errno = 0;
89 crypt_sha512crypt_rn(phrase, phr_size, setting, set_size, output,
90 out_size, scratch, scr_size);
91 return -errno;
92}
93
Steffen Jaeckel229bd512021-07-08 15:57:33 +020094/* Subroutine of _xcrypt_crypt_sha512crypt_rn: Feed CTX with LEN bytes of a
95 virtual byte sequence consisting of BLOCK repeated over and over
96 indefinitely. */
97static void
98sha512_process_recycled_bytes (unsigned char block[64], size_t len,
99 SHA512_CTX *ctx)
100{
101 size_t cnt;
102 for (cnt = len; cnt >= 64; cnt -= 64)
103 SHA512_Update (ctx, block, 64);
104 SHA512_Update (ctx, block, cnt);
105}
106
107void
108crypt_sha512crypt_rn (const char *phrase, size_t phr_size,
109 const char *setting, size_t ARG_UNUSED (set_size),
110 uint8_t *output, size_t out_size,
111 void *scratch, size_t scr_size)
112{
113 /* This shouldn't ever happen, but... */
114 if (out_size < SHA512_HASH_LENGTH
115 || scr_size < sizeof (struct sha512_buffer))
116 {
117 errno = ERANGE;
118 return;
119 }
120
121 struct sha512_buffer *buf = scratch;
122 SHA512_CTX *ctx = &buf->ctx;
123 uint8_t *result = buf->result;
124 uint8_t *p_bytes = buf->p_bytes;
125 uint8_t *s_bytes = buf->s_bytes;
126 char *cp = (char *)output;
127 const char *salt = setting;
128
129 size_t salt_size;
130 size_t cnt;
131 /* Default number of rounds. */
132 size_t rounds = ROUNDS_DEFAULT;
133 bool rounds_custom = false;
134
135 /* Find beginning of salt string. The prefix should normally always
136 be present. Just in case it is not. */
137 if (strncmp (sha512_salt_prefix, salt, sizeof (sha512_salt_prefix) - 1) == 0)
138 /* Skip salt prefix. */
139 salt += sizeof (sha512_salt_prefix) - 1;
140
141 if (strncmp (salt, sha512_rounds_prefix, sizeof (sha512_rounds_prefix) - 1)
142 == 0)
143 {
144 const char *num = salt + sizeof (sha512_rounds_prefix) - 1;
145 /* Do not allow an explicit setting of zero rounds, nor of the
146 default number of rounds, nor leading zeroes on the rounds. */
147 if (!(*num >= '1' && *num <= '9'))
148 {
149 errno = EINVAL;
150 return;
151 }
152
153 errno = 0;
154 char *endp;
155 rounds = strtoul (num, &endp, 10);
156 if (endp == num || *endp != '$'
157 || rounds < ROUNDS_MIN
158 || rounds > ROUNDS_MAX
159 || errno)
160 {
161 errno = EINVAL;
162 return;
163 }
164 salt = endp + 1;
165 rounds_custom = true;
166 }
167
168 /* The salt ends at the next '$' or the end of the string.
169 Ensure ':' does not appear in the salt (it is used as a separator in /etc/passwd).
170 Also check for '\n', as in /etc/passwd the whole parameters of the user data must
171 be on a single line. */
172 salt_size = strcspn (salt, "$:\n");
173 if (!(salt[salt_size] == '$' || !salt[salt_size]))
174 {
175 errno = EINVAL;
176 return;
177 }
178
179 /* Ensure we do not use more salt than SALT_LEN_MAX. */
180 if (salt_size > SALT_LEN_MAX)
181 salt_size = SALT_LEN_MAX;
182
183 /* Compute alternate SHA512 sum with input PHRASE, SALT, and PHRASE. The
184 final result will be added to the first context. */
185 SHA512_Init (ctx);
186
187 /* Add phrase. */
188 SHA512_Update (ctx, phrase, phr_size);
189
190 /* Add salt. */
191 SHA512_Update (ctx, salt, salt_size);
192
193 /* Add phrase again. */
194 SHA512_Update (ctx, phrase, phr_size);
195
196 /* Now get result of this (64 bytes) and add it to the other
197 context. */
198 SHA512_Final (result, ctx);
199
200 /* Prepare for the real work. */
201 SHA512_Init (ctx);
202
203 /* Add the phrase string. */
204 SHA512_Update (ctx, phrase, phr_size);
205
206 /* The last part is the salt string. This must be at most 8
207 characters and it ends at the first `$' character (for
208 compatibility with existing implementations). */
209 SHA512_Update (ctx, salt, salt_size);
210
211 /* Add for any character in the phrase one byte of the alternate sum. */
212 for (cnt = phr_size; cnt > 64; cnt -= 64)
213 SHA512_Update (ctx, result, 64);
214 SHA512_Update (ctx, result, cnt);
215
216 /* Take the binary representation of the length of the phrase and for every
217 1 add the alternate sum, for every 0 the phrase. */
218 for (cnt = phr_size; cnt > 0; cnt >>= 1)
219 if ((cnt & 1) != 0)
220 SHA512_Update (ctx, result, 64);
221 else
222 SHA512_Update (ctx, phrase, phr_size);
223
224 /* Create intermediate result. */
225 SHA512_Final (result, ctx);
226
227 /* Start computation of P byte sequence. */
228 SHA512_Init (ctx);
229
230 /* For every character in the password add the entire password. */
231 for (cnt = 0; cnt < phr_size; ++cnt)
232 SHA512_Update (ctx, phrase, phr_size);
233
234 /* Finish the digest. */
235 SHA512_Final (p_bytes, ctx);
236
237 /* Start computation of S byte sequence. */
238 SHA512_Init (ctx);
239
240 /* For every character in the password add the entire password. */
241 for (cnt = 0; cnt < (size_t) 16 + (size_t) result[0]; ++cnt)
242 SHA512_Update (ctx, salt, salt_size);
243
244 /* Finish the digest. */
245 SHA512_Final (s_bytes, ctx);
246
247 /* Repeatedly run the collected hash value through SHA512 to burn
248 CPU cycles. */
249 for (cnt = 0; cnt < rounds; ++cnt)
250 {
251 /* New context. */
252 SHA512_Init (ctx);
253
254 /* Add phrase or last result. */
255 if ((cnt & 1) != 0)
256 sha512_process_recycled_bytes (p_bytes, phr_size, ctx);
257 else
258 SHA512_Update (ctx, result, 64);
259
260 /* Add salt for numbers not divisible by 3. */
261 if (cnt % 3 != 0)
262 sha512_process_recycled_bytes (s_bytes, salt_size, ctx);
263
264 /* Add phrase for numbers not divisible by 7. */
265 if (cnt % 7 != 0)
266 sha512_process_recycled_bytes (p_bytes, phr_size, ctx);
267
268 /* Add phrase or last result. */
269 if ((cnt & 1) != 0)
270 SHA512_Update (ctx, result, 64);
271 else
272 sha512_process_recycled_bytes (p_bytes, phr_size, ctx);
273
274 /* Create intermediate result. */
275 SHA512_Final (result, ctx);
276 }
277
278 /* Now we can construct the result string. It consists of four
279 parts, one of which is optional. We already know that buflen is
280 at least sha512_hash_length, therefore none of the string bashing
281 below can overflow the buffer. */
282
283 memcpy (cp, sha512_salt_prefix, sizeof (sha512_salt_prefix) - 1);
284 cp += sizeof (sha512_salt_prefix) - 1;
285
286 if (rounds_custom)
287 {
288 int n = snprintf (cp,
289 SHA512_HASH_LENGTH - (sizeof (sha512_salt_prefix) - 1),
290 "%s%zu$", sha512_rounds_prefix, rounds);
291 cp += n;
292 }
293
294 memcpy (cp, salt, salt_size);
295 cp += salt_size;
296 *cp++ = '$';
297
298#define b64_from_24bit(B2, B1, B0, N) \
299 do { \
300 unsigned int w = ((((unsigned int)(B2)) << 16) | \
301 (((unsigned int)(B1)) << 8) | \
302 ((unsigned int)(B0))); \
303 int n = (N); \
304 while (n-- > 0) \
305 { \
306 *cp++ = b64t[w & 0x3f]; \
307 w >>= 6; \
308 } \
309 } while (0)
310
311 b64_from_24bit (result[0], result[21], result[42], 4);
312 b64_from_24bit (result[22], result[43], result[1], 4);
313 b64_from_24bit (result[44], result[2], result[23], 4);
314 b64_from_24bit (result[3], result[24], result[45], 4);
315 b64_from_24bit (result[25], result[46], result[4], 4);
316 b64_from_24bit (result[47], result[5], result[26], 4);
317 b64_from_24bit (result[6], result[27], result[48], 4);
318 b64_from_24bit (result[28], result[49], result[7], 4);
319 b64_from_24bit (result[50], result[8], result[29], 4);
320 b64_from_24bit (result[9], result[30], result[51], 4);
321 b64_from_24bit (result[31], result[52], result[10], 4);
322 b64_from_24bit (result[53], result[11], result[32], 4);
323 b64_from_24bit (result[12], result[33], result[54], 4);
324 b64_from_24bit (result[34], result[55], result[13], 4);
325 b64_from_24bit (result[56], result[14], result[35], 4);
326 b64_from_24bit (result[15], result[36], result[57], 4);
327 b64_from_24bit (result[37], result[58], result[16], 4);
328 b64_from_24bit (result[59], result[17], result[38], 4);
329 b64_from_24bit (result[18], result[39], result[60], 4);
330 b64_from_24bit (result[40], result[61], result[19], 4);
331 b64_from_24bit (result[62], result[20], result[41], 4);
332 b64_from_24bit (0, 0, result[63], 2);
333
334 *cp = '\0';
335}
336
337#ifndef NO_GENSALT
338
339void
340gensalt_sha512crypt_rn (unsigned long count,
341 const uint8_t *rbytes, size_t nrbytes,
342 uint8_t *output, size_t output_size)
343{
344 gensalt_sha_rn ('6', SALT_LEN_MAX, ROUNDS_DEFAULT, ROUNDS_MIN, ROUNDS_MAX,
345 count, rbytes, nrbytes, output, output_size);
346}
347
348#endif
349
350#endif