sha256crypt.c

filter: Statementstmt 91.9% 218 / 237 Branchbran 80.9% 102 / 126 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.1% 320 / 363 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« sha512crypt.c ↑ Summary des.c »
1 /* SHA256-based Unix crypt implementation.
2 Released into the Public Domain by Ulrich Drepper <drepper@redhat.com>. */
3  
4 #ifndef _GNU_SOURCE
5 #define _GNU_SOURCE
6 #endif
7  
8 #include <errno.h>
9 #include <limits.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/param.h>
14 #include <sys/types.h>
15  
16 /* To avoid stdbool.h */
17 #define bool char
18 #define true 1
19 #define false 0
20  
21 #ifndef MIN
22 #define MIN(a,b) ( (a) > (b) ? (b) : (a) )
23 #endif
24 #ifndef MAX
25 #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
26 #endif
27  
28 #ifdef WINDOWS
29 typedef unsigned int u_int32_t;
30 #endif
31  
32 /* Structure to save state of computation between the single steps. */
33 struct sha256_ctx
34 {
35 u_int32_t H[8];
36  
37 u_int32_t total[2];
38 u_int32_t buflen;
39 char buffer[128]; /* NB: always correctly aligned for u_int32_t. */
40 };
41  
42  
43 #if defined(ARCH_IS_BIG_ENDIAN) && ARCH_IS_BIG_ENDIAN == 0
44 # define SWAP(n) \
45 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
46 #else
47 # define SWAP(n) (n)
48 #endif
49  
50  
51 /* This array contains the bytes used to pad the buffer to the next
52 64-byte boundary. (FIPS 180-2:5.1.1) */
53 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
54  
55  
56 /* Constants for SHA256 from FIPS 180-2:4.2.2. */
57 static const u_int32_t K[64] =
58 {
59 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
60 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
61 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
62 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
63 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
64 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
65 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
66 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
67 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
68 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
69 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
70 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
71 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
72 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
73 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
74 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
75 };
76  
77  
78 /* Process LEN bytes of BUFFER, accumulating context into CTX.
79 It is assumed that LEN % 64 == 0. */
80 static void
81 946281 cpx_sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx)
82 {
83 946281 const u_int32_t *words = buffer;
84 946281 size_t nwords = len / sizeof (u_int32_t);
85 946281 u_int32_t a = ctx->H[0];
86 946281 u_int32_t b = ctx->H[1];
87 946281 u_int32_t c = ctx->H[2];
88 946281 u_int32_t d = ctx->H[3];
89 946281 u_int32_t e = ctx->H[4];
90 946281 u_int32_t f = ctx->H[5];
91 946281 u_int32_t g = ctx->H[6];
92 946281 u_int32_t h = ctx->H[7];
93  
94 /* First increment the byte count. FIPS 180-2 specifies the possible
95 length of the file up to 2^64 bits. Here we only compute the
96 number of bytes. Do a double word increment. */
97 946281 ctx->total[0] += len;
98 946281 ▶ if (ctx->total[0] < len)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->total[0] < len) 0 946281 946281
99 0 ++ctx->total[1];
100  
101 /* Process all bytes in the buffer with 64 bytes in each round of
102 the loop. */
103 1999928 ▶ while (nwords > 0)
Branch: 1 branch
BranchTrueFalseTotal
while (nwords > 0) 1053647 946281 1999928
104 {
105 u_int32_t W[64];
106 1053647 u_int32_t a_save = a;
107 1053647 u_int32_t b_save = b;
108 1053647 u_int32_t c_save = c;
109 1053647 u_int32_t d_save = d;
110 1053647 u_int32_t e_save = e;
111 1053647 u_int32_t f_save = f;
112 1053647 u_int32_t g_save = g;
113 1053647 u_int32_t h_save = h;
114 unsigned int t;
115  
116 /* Operators defined in FIPS 180-2:4.1.2. */
117 #define Ch(x, y, z) ((x & y) ^ (~x & z))
118 #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
119 #define S0(x) (CYCLIC (x, 2) ^ CYCLIC (x, 13) ^ CYCLIC (x, 22))
120 #define S1(x) (CYCLIC (x, 6) ^ CYCLIC (x, 11) ^ CYCLIC (x, 25))
121 #define R0(x) (CYCLIC (x, 7) ^ CYCLIC (x, 18) ^ (x >> 3))
122 #define R1(x) (CYCLIC (x, 17) ^ CYCLIC (x, 19) ^ (x >> 10))
123  
124 /* It is unfortunate that C does not provide an operator for
125 cyclic rotation. Hope the C compiler is smart enough. */
126 #define CYCLIC(w, s) ((w >> s) | (w << (32 - s)))
127  
128 /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */
129 17911999 ▶ for (t = 0; t < 16; ++t)
Branch: 1 branch
BranchTrueFalseTotal
for (t = 0; t < 16; ++t) 16858352 1053647 17911999
130 {
131 16858352 W[t] = SWAP (*words);
132 16858352 ++words;
133 }
134 51628703 ▶ for (t = 16; t < 64; ++t)
Branch: 1 branch
BranchTrueFalseTotal
for (t = 16; t < 64; ++t) 50575056 1053647 51628703
135 50575056 W[t] = R1 (W[t - 2]) + W[t - 7] + R0 (W[t - 15]) + W[t - 16];
136  
137 /* The actual computation according to FIPS 180-2:6.2.2 step 3. */
138 68487055 ▶ for (t = 0; t < 64; ++t)
Branch: 1 branch
BranchTrueFalseTotal
for (t = 0; t < 64; ++t) 67433408 1053647 68487055
139 {
140 67433408 u_int32_t T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t];
141 67433408 u_int32_t T2 = S0 (a) + Maj (a, b, c);
142 67433408 h = g;
143 67433408 g = f;
144 67433408 f = e;
145 67433408 e = d + T1;
146 67433408 d = c;
147 67433408 c = b;
148 67433408 b = a;
149 67433408 a = T1 + T2;
150 }
151  
152 /* Add the starting values of the context according to FIPS 180-2:6.2.2
153 step 4. */
154 1053647 a += a_save;
155 1053647 b += b_save;
156 1053647 c += c_save;
157 1053647 d += d_save;
158 1053647 e += e_save;
159 1053647 f += f_save;
160 1053647 g += g_save;
161 1053647 h += h_save;
162  
163 /* Prepare for the next round. */
164 1053647 nwords -= 16;
165 }
166  
167 /* Put checksum in context given as argument. */
168 946281 ctx->H[0] = a;
169 946281 ctx->H[1] = b;
170 946281 ctx->H[2] = c;
171 946281 ctx->H[3] = d;
172 946281 ctx->H[4] = e;
173 946281 ctx->H[5] = f;
174 946281 ctx->H[6] = g;
175 946281 ctx->H[7] = h;
176 946281 }
177  
178  
179 /* Initialize structure containing state of computation.
180 (FIPS 180-2:5.3.2) */
181 static void
182 477366 cpx_sha256_init_ctx (struct sha256_ctx *ctx)
183 {
184 477366 ctx->H[0] = 0x6a09e667;
185 477366 ctx->H[1] = 0xbb67ae85;
186 477366 ctx->H[2] = 0x3c6ef372;
187 477366 ctx->H[3] = 0xa54ff53a;
188 477366 ctx->H[4] = 0x510e527f;
189 477366 ctx->H[5] = 0x9b05688c;
190 477366 ctx->H[6] = 0x1f83d9ab;
191 477366 ctx->H[7] = 0x5be0cd19;
192  
193 477366 ctx->total[0] = ctx->total[1] = 0;
194 477366 ctx->buflen = 0;
195 477366 }
196  
197  
198 /* Process the remaining bytes in the internal buffer and the usual
199 prolog according to the standard and write the result to RESBUF.
200  
201 IMPORTANT: On some systems it is required that RESBUF is correctly
202 aligned for a 32 bits value. */
203 static void *
204 477366 cpx_sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf)
205 {
206 /* Take yet unprocessed bytes into account. */
207 477366 u_int32_t bytes = ctx->buflen;
208 size_t pad;
209 unsigned int i;
210  
211 /* Now count remaining bytes. */
212 477366 ctx->total[0] += bytes;
213 477366 ▶ if (ctx->total[0] < bytes)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->total[0] < bytes) 0 477366 477366
214 0 ++ctx->total[1];
215  
216 477366 ▶ pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
Branch: 1 branch
BranchTrueFalseTotal
pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; 105976 371390 477366
217 477366 memcpy (&ctx->buffer[bytes], fillbuf, pad);
218  
219 /* Put the 64-bit file length in *bits* at the end of the buffer. */
220 477366 *(u_int32_t *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 3);
221 477366 *(u_int32_t *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) |
222 (ctx->total[0] >> 29));
223  
224 /* Process last bytes. */
225 477366 cpx_sha256_process_block (ctx->buffer, bytes + pad + 8, ctx);
226  
227 /* Put result from CTX in first 32 bytes following RESBUF. */
228 4296294 ▶ for (i = 0; i < 8; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; ++i) 3818928 477366 4296294
229 3818928 ((u_int32_t *) resbuf)[i] = SWAP (ctx->H[i]);
230  
231 477366 return resbuf;
232 }
233  
234  
235 static void
236 1686020 cpx_sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx)
237 {
238 /* When we already have some bits in our internal buffer concatenate
239 both inputs first. */
240 1686020 ▶ if (ctx->buflen != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->buflen != 0) 1201442 484578 1686020
241 {
242 1201442 size_t left_over = ctx->buflen;
243 1201442 size_t add = 128 - left_over > len ? len : 128 - left_over;
244  
245 1201442 memcpy (&ctx->buffer[left_over], buffer, add);
246 1201442 ctx->buflen += add;
247  
248 1201442 ▶ if (ctx->buflen > 64)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->buflen > 64) 467499 733943 1201442
249 {
250 467499 cpx_sha256_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
251  
252 467499 ctx->buflen &= 63;
253 /* The regions in the following copy operation cannot overlap. */
254 467499 memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
255 467499 ctx->buflen);
256 }
257  
258 1201442 buffer = (const char *) buffer + add;
259 1201442 len -= add;
260 }
261  
262 /* Process available complete blocks. */
263 1686020 ▶ if (len >= 64)
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 64) 1416 1684604 1686020
264 {
265 /* To check alignment gcc has an appropriate operator. Other
266 compilers don't. */
267 #if __GNUC__ >= 2
268 # define UNALIGNED_P(p) (((unsigned long) p) % __alignof__ (u_int32_t) != 0)
269 #else
270 # define UNALIGNED_P(p) (((unsigned long) p) % sizeof (u_int32_t) != 0)
271 #endif
272 1416 ▶ if (UNALIGNED_P (buffer))
Branch: 1 branch
BranchTrueFalseTotal
if (UNALIGNED_P (buffer)) 0 1416 1416
273 0 ▶ while (len > 64)
Branch: 1 branch
BranchTrueFalseTotal
while (len > 64) 0 0 0
274 {
275 0 cpx_sha256_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
276 0 buffer = (const char *) buffer + 64;
277 0 len -= 64;
278 }
279 else
280 {
281 1416 cpx_sha256_process_block (buffer, len & ~63, ctx);
282 1416 buffer = (const char *) buffer + (len & ~63);
283 1416 len &= 63;
284 }
285 }
286  
287 /* Move remaining bytes into internal buffer. */
288 1686020 ▶ if (len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) 478731 1207289 1686020
289 {
290 478731 size_t left_over = ctx->buflen;
291  
292 478731 memcpy (&ctx->buffer[left_over], buffer, len);
293 478731 left_over += len;
294 478731 ▶ if (left_over >= 64)
Branch: 1 branch
BranchTrueFalseTotal
if (left_over >= 64) 0 478731 478731
295 {
296 0 cpx_sha256_process_block (ctx->buffer, 64, ctx);
297 0 left_over -= 64;
298 0 memcpy (ctx->buffer, &ctx->buffer[64], left_over);
299 }
300 478731 ctx->buflen = left_over;
301 }
302 1686020 }
303  
304  
305 /* Define our magic string to mark salt for SHA256 "encryption"
306 replacement. */
307 static const char sha256_salt_prefix[] = "$5$";
308  
309 /* Prefix for optional rounds specification. */
310 static const char sha256_rounds_prefix[] = "rounds=";
311  
312 /* Maximum salt string length. */
313 #define SALT_LEN_MAX 16
314 /* Default number of rounds if not explicitly specified. */
315 #define ROUNDS_DEFAULT 5000
316 /* Minimum number of rounds. */
317 #define ROUNDS_MIN 1000
318 /* Maximum number of rounds. */
319 #define ROUNDS_MAX 999999999
320  
321 /* Table with characters for base64 transformation. */
322 static const char b64t[64] =
323 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
324  
325  
326 static char *
327 20 cpx_sha256_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
328 {
329 unsigned char alt_result[32]
330 __attribute__ ((__aligned__ (__alignof__ (u_int32_t))));
331 unsigned char temp_result[32]
332 __attribute__ ((__aligned__ (__alignof__ (u_int32_t))));
333 struct sha256_ctx ctx;
334 struct sha256_ctx alt_ctx;
335 size_t salt_len;
336 size_t key_len;
337 size_t cnt;
338 char *cp;
339 20 char *copied_key = NULL;
340 20 char *copied_salt = NULL;
341 char *p_bytes;
342 char *s_bytes;
343 /* Default number of rounds. */
344 20 size_t rounds = ROUNDS_DEFAULT;
345 20 bool rounds_custom = false;
346  
347 /* Find beginning of salt string. The prefix should normally always
348 be present. Just in case it is not. */
349 20 ▶ if (strncmp (sha256_salt_prefix, salt, sizeof (sha256_salt_prefix) - 1) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp (sha256_salt_prefix, salt, sizeof (sha256_salt_prefix) - 1) == 0) 15 5 20
350 /* Skip salt prefix. */
351 15 salt += sizeof (sha256_salt_prefix) - 1;
352  
353 20 ▶ if (strncmp (salt, sha256_rounds_prefix, sizeof (sha256_rounds_prefix) - 1)
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp (salt, sha256_rounds_prefix, sizeof (sha256_rounds_prefix) - 1) 12 8 20
354 == 0)
355 {
356 12 const char *num = salt + sizeof (sha256_rounds_prefix) - 1;
357 char *endp;
358 12 unsigned long int srounds = strtoul (num, &endp, 10);
359 12 ▶ if (*endp == '$')
Branch: 1 branch
BranchTrueFalseTotal
if (*endp == '$') 12 0 12
360 {
361 12 salt = endp + 1;
362 12 ▶ rounds = MAX (ROUNDS_MIN, MIN (srounds, ROUNDS_MAX));
Branch: 1 branch
BranchTrueFalseTotal
rounds = MAX (ROUNDS_MIN, MIN (srounds, ROUNDS_MAX)); 10 2 12
363 12 rounds_custom = true;
364 }
365 }
366  
367 20 ▶ salt_len = MIN (strcspn (salt, "$"), SALT_LEN_MAX);
Branch: 1 branch
BranchTrueFalseTotal
salt_len = MIN (strcspn (salt, "$"), SALT_LEN_MAX); 12 8 20
368 20 key_len = strlen (key);
369  
370 20 ▶ if ((key - (char *) 0) % __alignof__ (u_int32_t) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((key - (char *) 0) % __alignof__ (u_int32_t) != 0) 0 20 20
371 {
372 0 char *tmp = (char *) alloca (key_len + __alignof__ (u_int32_t));
373 0 key = copied_key =
374 0 memcpy (tmp + __alignof__ (u_int32_t)
375 0 - (tmp - (char *) 0) % __alignof__ (u_int32_t),
376 key, key_len);
377 }
378  
379 20 ▶ if ((salt - (char *) 0) % __alignof__ (u_int32_t) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((salt - (char *) 0) % __alignof__ (u_int32_t) != 0) 12 8 20
380 {
381 12 char *tmp = (char *) alloca (salt_len + __alignof__ (u_int32_t));
382 12 salt = copied_salt =
383 12 memcpy (tmp + __alignof__ (u_int32_t)
384 12 - (tmp - (char *) 0) % __alignof__ (u_int32_t),
385 salt, salt_len);
386 }
387  
388 /* Prepare for the real work. */
389 20 cpx_sha256_init_ctx (&ctx);
390  
391 /* Add the key string. */
392 20 cpx_sha256_process_bytes (key, key_len, &ctx);
393  
394 /* The last part is the salt string. This must be at most 16
395 characters and it ends at the first `$' character (for
396 compatibility with existing implementations). */
397 20 cpx_sha256_process_bytes (salt, salt_len, &ctx);
398  
399  
400 /* Compute alternate SHA256 sum with input KEY, SALT, and KEY. The
401 final result will be added to the first context. */
402 20 cpx_sha256_init_ctx (&alt_ctx);
403  
404 /* Add key. */
405 20 cpx_sha256_process_bytes (key, key_len, &alt_ctx);
406  
407 /* Add salt. */
408 20 cpx_sha256_process_bytes (salt, salt_len, &alt_ctx);
409  
410 /* Add key again. */
411 20 cpx_sha256_process_bytes (key, key_len, &alt_ctx);
412  
413 /* Now get result of this (32 bytes) and add it to the other
414 context. */
415 20 cpx_sha256_finish_ctx (&alt_ctx, alt_result);
416  
417 /* Add for any character in the key one byte of the alternate sum. */
418 28 ▶ for (cnt = key_len; cnt > 32; cnt -= 32)
Branch: 1 branch
BranchTrueFalseTotal
for (cnt = key_len; cnt > 32; cnt -= 32) 8 20 28
419 8 cpx_sha256_process_bytes (alt_result, 32, &ctx);
420 20 cpx_sha256_process_bytes (alt_result, cnt, &ctx);
421  
422 /* Take the binary representation of the length of the key and for every
423 1 add the alternate sum, for every 0 the key. */
424 108 ▶ for (cnt = key_len; cnt > 0; cnt >>= 1)
Branch: 1 branch
BranchTrueFalseTotal
for (cnt = key_len; cnt > 0; cnt >>= 1) 88 20 108
425 88 ▶ if ((cnt & 1) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((cnt & 1) != 0) 41 47 88
426 41 cpx_sha256_process_bytes (alt_result, 32, &ctx);
427 else
428 47 cpx_sha256_process_bytes (key, key_len, &ctx);
429  
430 /* Create intermediate result. */
431 20 cpx_sha256_finish_ctx (&ctx, alt_result);
432  
433 /* Start computation of P byte sequence. */
434 20 cpx_sha256_init_ctx (&alt_ctx);
435  
436 /* For every character in the password add the entire password. */
437 514 ▶ for (cnt = 0; cnt < key_len; ++cnt)
Branch: 1 branch
BranchTrueFalseTotal
for (cnt = 0; cnt < key_len; ++cnt) 494 20 514
438 494 cpx_sha256_process_bytes (key, key_len, &alt_ctx);
439  
440 /* Finish the digest. */
441 20 cpx_sha256_finish_ctx (&alt_ctx, temp_result);
442  
443 /* Create byte sequence P. */
444 20 cp = p_bytes = alloca (key_len);
445 28 ▶ for (cnt = key_len; cnt >= 32; cnt -= 32) {
Branch: 1 branch
BranchTrueFalseTotal
for (cnt = key_len; cnt >= 32; cnt -= 32) { 8 20 28
446 8 memcpy (cp, temp_result, 32);
447 8 cp += 32;
448 }
449 20 memcpy (cp, temp_result, cnt);
450  
451 /* Start computation of S byte sequence. */
452 20 cpx_sha256_init_ctx (&alt_ctx);
453  
454 /* For every character in the password add the entire password. */
455 3552 ▶ for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt)
Branch: 1 branch
BranchTrueFalseTotal
for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt) 3532 20 3552
456 3532 cpx_sha256_process_bytes (salt, salt_len, &alt_ctx);
457  
458 /* Finish the digest. */
459 20 cpx_sha256_finish_ctx (&alt_ctx, temp_result);
460  
461 /* Create byte sequence S. */
462 20 cp = s_bytes = alloca (salt_len);
463 20 ▶ for (cnt = salt_len; cnt >= 32; cnt -= 32) {
Branch: 1 branch
BranchTrueFalseTotal
for (cnt = salt_len; cnt >= 32; cnt -= 32) { 0 20 20
464 0 memcpy (cp, temp_result, 32);
465 0 cp += 32;
466 }
467 20 memcpy (cp, temp_result, cnt);
468  
469 /* Repeatedly run the collected hash value through SHA256 to burn
470 CPU cycles. */
471 477286 ▶ for (cnt = 0; cnt < rounds; ++cnt)
Branch: 1 branch
BranchTrueFalseTotal
for (cnt = 0; cnt < rounds; ++cnt) 477266 20 477286
472 {
473 /* New context. */
474 477266 cpx_sha256_init_ctx (&ctx);
475  
476 /* Add key or last result. */
477 477266 ▶ if ((cnt & 1) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((cnt & 1) != 0) 238632 238634 477266
478 238632 cpx_sha256_process_bytes (p_bytes, key_len, &ctx);
479 else
480 238634 cpx_sha256_process_bytes (alt_result, 32, &ctx);
481  
482 /* Add salt for numbers not divisible by 3. */
483 477266 ▶ if (cnt % 3 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cnt % 3 != 0) 318170 159096 477266
484 318170 cpx_sha256_process_bytes (s_bytes, salt_len, &ctx);
485  
486 /* Add key for numbers not divisible by 7. */
487 477266 ▶ if (cnt % 7 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cnt % 7 != 0) 409076 68190 477266
488 409076 cpx_sha256_process_bytes (p_bytes, key_len, &ctx);
489  
490 /* Add key or last result. */
491 477266 ▶ if ((cnt & 1) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((cnt & 1) != 0) 238632 238634 477266
492 238632 cpx_sha256_process_bytes (alt_result, 32, &ctx);
493 else
494 238634 cpx_sha256_process_bytes (p_bytes, key_len, &ctx);
495  
496 /* Create intermediate result. */
497 477266 cpx_sha256_finish_ctx (&ctx, alt_result);
498 }
499  
500 /* Now we can construct the result string. It consists of three
501 parts. */
502 20 cp = strncpy (buffer, sha256_salt_prefix, MAX (0, buflen));
503 20 cp += MIN(strlen(sha256_salt_prefix), MAX (0, buflen));
504 20 buflen -= sizeof (sha256_salt_prefix) - 1;
505  
506 20 ▶ if (rounds_custom)
Branch: 1 branch
BranchTrueFalseTotal
if (rounds_custom) 12 8 20
507 {
508 12 int n = snprintf (cp, MAX (0, buflen), "%s%u$",
509 sha256_rounds_prefix, (unsigned int)rounds);
510 12 cp += n;
511 12 buflen -= n;
512 }
513  
514 20 cp = strncpy (cp, salt, MIN ((size_t) MAX (0, buflen), salt_len));
515 20 cp += MIN ((size_t) MAX (0, buflen), salt_len);
516 20 buflen -= MIN ((size_t) MAX (0, buflen), salt_len);
517  
518 20 ▶ if (buflen > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (buflen > 0) 20 0 20
519 {
520 20 *cp++ = '$';
521 20 --buflen;
522 }
523  
524 #define b64_from_24bit(B2, B1, B0, N) \
525 do { \
526 unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
527 int n = (N); \
528 while (n-- > 0 && buflen > 0) \
529 { \
530 *cp++ = b64t[w & 0x3f]; \
531 --buflen; \
532 w >>= 6; \
533 } \
534 } while (0)
535  
536 100 ▶ b64_from_24bit (alt_result[0], alt_result[10], alt_result[20], 4);
Branch: 2 branches
BranchTrueFalseTotal
b64_from_24bit (alt_result[0], alt_result[10], alt_result[20], 4); 80 20 100
b64_from_24bit (alt_result[0], alt_result[10], alt_result[20], 4); 80 0 80
537 100 ▶ b64_from_24bit (alt_result[21], alt_result[1], alt_result[11], 4);
Branch: 2 branches
BranchTrueFalseTotal
b64_from_24bit (alt_result[21], alt_result[1], alt_result[11], 4); 80 20 100
b64_from_24bit (alt_result[21], alt_result[1], alt_result[11], 4); 80 0 80
538 100 ▶ b64_from_24bit (alt_result[12], alt_result[22], alt_result[2], 4);
Branch: 2 branches
BranchTrueFalseTotal
b64_from_24bit (alt_result[12], alt_result[22], alt_result[2], 4); 80 20 100
b64_from_24bit (alt_result[12], alt_result[22], alt_result[2], 4); 80 0 80
539 100 ▶ b64_from_24bit (alt_result[3], alt_result[13], alt_result[23], 4);
Branch: 2 branches
BranchTrueFalseTotal
b64_from_24bit (alt_result[3], alt_result[13], alt_result[23], 4); 80 20 100
b64_from_24bit (alt_result[3], alt_result[13], alt_result[23], 4); 80 0 80
540 100 ▶ b64_from_24bit (alt_result[24], alt_result[4], alt_result[14], 4);
Branch: 2 branches
BranchTrueFalseTotal
b64_from_24bit (alt_result[24], alt_result[4], alt_result[14], 4); 80 20 100
b64_from_24bit (alt_result[24], alt_result[4], alt_result[14], 4); 80 0 80
541 100 ▶ b64_from_24bit (alt_result[15], alt_result[25], alt_result[5], 4);
Branch: 2 branches
BranchTrueFalseTotal
b64_from_24bit (alt_result[15], alt_result[25], alt_result[5], 4); 80 20 100
b64_from_24bit (alt_result[15], alt_result[25], alt_result[5], 4); 80 0 80
542 100 ▶ b64_from_24bit (alt_result[6], alt_result[16], alt_result[26], 4);
Branch: 2 branches
BranchTrueFalseTotal
b64_from_24bit (alt_result[6], alt_result[16], alt_result[26], 4); 80 20 100
b64_from_24bit (alt_result[6], alt_result[16], alt_result[26], 4); 80 0 80
543 100 ▶ b64_from_24bit (alt_result[27], alt_result[7], alt_result[17], 4);
Branch: 2 branches
BranchTrueFalseTotal
b64_from_24bit (alt_result[27], alt_result[7], alt_result[17], 4); 80 20 100
b64_from_24bit (alt_result[27], alt_result[7], alt_result[17], 4); 80 0 80
544 100 ▶ b64_from_24bit (alt_result[18], alt_result[28], alt_result[8], 4);
Branch: 2 branches
BranchTrueFalseTotal
b64_from_24bit (alt_result[18], alt_result[28], alt_result[8], 4); 80 20 100
b64_from_24bit (alt_result[18], alt_result[28], alt_result[8], 4); 80 0 80
545 100 ▶ b64_from_24bit (alt_result[9], alt_result[19], alt_result[29], 4);
Branch: 2 branches
BranchTrueFalseTotal
b64_from_24bit (alt_result[9], alt_result[19], alt_result[29], 4); 80 20 100
b64_from_24bit (alt_result[9], alt_result[19], alt_result[29], 4); 80 0 80
546 80 ▶ b64_from_24bit (0, alt_result[31], alt_result[30], 3);
Branch: 2 branches
BranchTrueFalseTotal
b64_from_24bit (0, alt_result[31], alt_result[30], 3); 60 20 80
b64_from_24bit (0, alt_result[31], alt_result[30], 3); 60 0 60
547 20 ▶ if (buflen <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (buflen <= 0) 0 20 20
548 {
549 0 errno = ERANGE;
550 0 buffer = NULL;
551 }
552 else
553 20 *cp = '\0'; /* Terminate the string. */
554  
555 /* Clear the buffer for the intermediate result so that people
556 attaching to processes or reading core dumps cannot get any
557 information. We do it in this way to clear correct_words[]
558 inside the SHA256 implementation as well. */
559 20 cpx_sha256_init_ctx (&ctx);
560 20 cpx_sha256_finish_ctx (&ctx, alt_result);
561 20 memset (temp_result, '\0', sizeof (temp_result));
562 20 memset (p_bytes, '\0', key_len);
563 20 memset (s_bytes, '\0', salt_len);
564 20 memset (&ctx, '\0', sizeof (ctx));
565 20 memset (&alt_ctx, '\0', sizeof (alt_ctx));
566 20 ▶ if (copied_key != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (copied_key != NULL) 0 20 20
567 0 memset (copied_key, '\0', key_len);
568 20 ▶ if (copied_salt != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (copied_salt != NULL) 12 8 20
569 12 memset (copied_salt, '\0', salt_len);
570  
571 20 return buffer;
572 }
573  
574  
575 /* This entry point is equivalent to the `crypt' function in Unix
576 libcs. */
577 char *
578 20 cpx_sha256_crypt (const char *key, const char *salt)
579 {
580 /* We don't want to have an arbitrary limit in the size of the
581 password. We can compute an upper bound for the size of the
582 result in advance and so we can prepare the buffer we pass to
583 `cpx_sha256_crypt_r'. */
584 static char *buffer;
585 static int buflen;
586 20 int needed = (sizeof (sha256_salt_prefix) - 1
587 + sizeof (sha256_rounds_prefix) + 9 + 1
588 20 + strlen (salt) + 1 + 43 + 1);
589  
590 20 ▶ if (buflen < needed)
Branch: 1 branch
BranchTrueFalseTotal
if (buflen < needed) 4 16 20
591 {
592 4 char *new_buffer = (char *) realloc (buffer, needed);
593 4 ▶ if (new_buffer == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_buffer == NULL) 0 4 4
594 0 return NULL;
595  
596 4 buffer = new_buffer;
597 4 buflen = needed;
598 }
599  
600 20 return cpx_sha256_crypt_r (key, salt, buffer, buflen);
601 }
« sha512crypt.c ↑ Summary des.c »