| 1 |
|
| /* SHA512-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 |
|
| #include <errno.h> |
| 8 |
|
| #include <limits.h> |
| 9 |
|
| #include <stdio.h> |
| 10 |
|
| #include <stdlib.h> |
| 11 |
|
| #include <string.h> |
| 12 |
|
| #include <sys/param.h> |
| 13 |
|
| #include <sys/types.h> |
| 14 |
|
| |
| 15 |
|
| /* To avoid stdbool.h */ |
| 16 |
|
| #define bool char |
| 17 |
|
| #define true 1 |
| 18 |
|
| #define false 0 |
| 19 |
|
| |
| 20 |
|
| #ifndef MIN |
| 21 |
|
| #define MIN(a,b) ( (a) > (b) ? (b) : (a) ) |
| 22 |
|
| #endif |
| 23 |
|
| #ifndef MAX |
| 24 |
|
| #define MAX(a,b) ( (a) > (b) ? (a) : (b) ) |
| 25 |
|
| #endif |
| 26 |
|
| |
| 27 |
|
| #ifdef WINDOWS |
| 28 |
|
| typedef unsigned long long u_int64_t; |
| 29 |
|
| #endif |
| 30 |
|
| |
| 31 |
|
| /* Structure to save state of computation between the single steps. */ |
| 32 |
|
| struct sha512_ctx |
| 33 |
|
| { |
| 34 |
|
| u_int64_t H[8]; |
| 35 |
|
| |
| 36 |
|
| u_int64_t total[2]; |
| 37 |
|
| u_int64_t buflen; |
| 38 |
|
| char buffer[256]; /* NB: always correctly aligned for u_int64_t. */ |
| 39 |
|
| }; |
| 40 |
|
| |
| 41 |
|
| |
| 42 |
|
| #if defined(ARCH_IS_BIG_ENDIAN) && ARCH_IS_BIG_ENDIAN == 0 |
| 43 |
|
| # define SWAP(n) \ |
| 44 |
|
| (((n) << 56) \ |
| 45 |
|
| | (((n) & 0xff00) << 40) \ |
| 46 |
|
| | (((n) & 0xff0000) << 24) \ |
| 47 |
|
| | (((n) & 0xff000000) << 8) \ |
| 48 |
|
| | (((n) >> 8) & 0xff000000) \ |
| 49 |
|
| | (((n) >> 24) & 0xff0000) \ |
| 50 |
|
| | (((n) >> 40) & 0xff00) \ |
| 51 |
|
| | ((n) >> 56)) |
| 52 |
|
| #else |
| 53 |
|
| # define SWAP(n) (n) |
| 54 |
|
| #endif |
| 55 |
|
| |
| 56 |
|
| |
| 57 |
|
| /* This array contains the bytes used to pad the buffer to the next |
| 58 |
|
| 64-byte boundary. (FIPS 180-2:5.1.2) */ |
| 59 |
|
| static const unsigned char fillbuf[128] = { 0x80, 0 /* , 0, 0, ... */ }; |
| 60 |
|
| |
| 61 |
|
| |
| 62 |
|
| /* Constants for SHA512 from FIPS 180-2:4.2.3. */ |
| 63 |
|
| static const u_int64_t K[80] = |
| 64 |
|
| { |
| 65 |
|
| 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, |
| 66 |
|
| 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, |
| 67 |
|
| 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, |
| 68 |
|
| 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, |
| 69 |
|
| 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, |
| 70 |
|
| 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, |
| 71 |
|
| 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, |
| 72 |
|
| 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, |
| 73 |
|
| 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, |
| 74 |
|
| 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, |
| 75 |
|
| 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, |
| 76 |
|
| 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, |
| 77 |
|
| 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, |
| 78 |
|
| 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, |
| 79 |
|
| 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, |
| 80 |
|
| 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, |
| 81 |
|
| 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, |
| 82 |
|
| 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, |
| 83 |
|
| 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, |
| 84 |
|
| 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, |
| 85 |
|
| 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, |
| 86 |
|
| 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, |
| 87 |
|
| 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, |
| 88 |
|
| 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, |
| 89 |
|
| 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, |
| 90 |
|
| 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, |
| 91 |
|
| 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, |
| 92 |
|
| 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, |
| 93 |
|
| 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, |
| 94 |
|
| 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, |
| 95 |
|
| 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, |
| 96 |
|
| 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, |
| 97 |
|
| 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, |
| 98 |
|
| 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, |
| 99 |
|
| 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, |
| 100 |
|
| 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, |
| 101 |
|
| 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, |
| 102 |
|
| 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, |
| 103 |
|
| 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, |
| 104 |
|
| 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL |
| 105 |
|
| }; |
| 106 |
|
| |
| 107 |
|
| |
| 108 |
|
| /* Process LEN bytes of BUFFER, accumulating context into CTX. |
| 109 |
|
| It is assumed that LEN % 128 == 0. */ |
| 110 |
|
| static void |
| 111 |
615651 |
| cpx_sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx) |
| 112 |
|
| { |
| 113 |
615651 |
| const u_int64_t *words = buffer; |
| 114 |
615651 |
| size_t nwords = len / sizeof (u_int64_t); |
| 115 |
615651 |
| u_int64_t a = ctx->H[0]; |
| 116 |
615651 |
| u_int64_t b = ctx->H[1]; |
| 117 |
615651 |
| u_int64_t c = ctx->H[2]; |
| 118 |
615651 |
| u_int64_t d = ctx->H[3]; |
| 119 |
615651 |
| u_int64_t e = ctx->H[4]; |
| 120 |
615651 |
| u_int64_t f = ctx->H[5]; |
| 121 |
615651 |
| u_int64_t g = ctx->H[6]; |
| 122 |
615651 |
| u_int64_t h = ctx->H[7]; |
| 123 |
|
| |
| 124 |
|
| /* First increment the byte count. FIPS 180-2 specifies the possible |
| 125 |
|
| length of the file up to 2^128 bits. Here we only compute the |
| 126 |
|
| number of bytes. Do a double word increment. */ |
| 127 |
615651 |
| ctx->total[0] += len; |
| 128 |
615651 |
▶ | if (ctx->total[0] < len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->total[0] < len) |
0 |
615651 |
615651 |
|
| 129 |
0 |
| ++ctx->total[1]; |
| 130 |
|
| |
| 131 |
|
| /* Process all bytes in the buffer with 128 bytes in each round of |
| 132 |
|
| the loop. */ |
| 133 |
1261030 |
▶ | while (nwords > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| while (nwords > 0) |
645379 |
615651 |
1261030 |
|
| 134 |
|
| { |
| 135 |
|
| u_int64_t W[80]; |
| 136 |
645379 |
| u_int64_t a_save = a; |
| 137 |
645379 |
| u_int64_t b_save = b; |
| 138 |
645379 |
| u_int64_t c_save = c; |
| 139 |
645379 |
| u_int64_t d_save = d; |
| 140 |
645379 |
| u_int64_t e_save = e; |
| 141 |
645379 |
| u_int64_t f_save = f; |
| 142 |
645379 |
| u_int64_t g_save = g; |
| 143 |
645379 |
| u_int64_t h_save = h; |
| 144 |
|
| unsigned int t; |
| 145 |
|
| /* Operators defined in FIPS 180-2:4.1.2. */ |
| 146 |
|
| #define Ch(x, y, z) ((x & y) ^ (~x & z)) |
| 147 |
|
| #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) |
| 148 |
|
| #define S0(x) (CYCLIC (x, 28) ^ CYCLIC (x, 34) ^ CYCLIC (x, 39)) |
| 149 |
|
| #define S1(x) (CYCLIC (x, 14) ^ CYCLIC (x, 18) ^ CYCLIC (x, 41)) |
| 150 |
|
| #define R0(x) (CYCLIC (x, 1) ^ CYCLIC (x, 8) ^ (x >> 7)) |
| 151 |
|
| #define R1(x) (CYCLIC (x, 19) ^ CYCLIC (x, 61) ^ (x >> 6)) |
| 152 |
|
| |
| 153 |
|
| /* It is unfortunate that C does not provide an operator for |
| 154 |
|
| cyclic rotation. Hope the C compiler is smart enough. */ |
| 155 |
|
| #define CYCLIC(w, s) ((w >> s) | (w << (64 - s))) |
| 156 |
|
| |
| 157 |
|
| /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2. */ |
| 158 |
10971443 |
▶ | for (t = 0; t < 16; ++t) |
Branch: 1 branch
| Branch | True | False | Total |
| for (t = 0; t < 16; ++t) |
10326064 |
645379 |
10971443 |
|
| 159 |
|
| { |
| 160 |
10326064 |
| W[t] = SWAP (*words); |
| 161 |
10326064 |
| ++words; |
| 162 |
|
| } |
| 163 |
41949635 |
▶ | for (t = 16; t < 80; ++t) |
Branch: 1 branch
| Branch | True | False | Total |
| for (t = 16; t < 80; ++t) |
41304256 |
645379 |
41949635 |
|
| 164 |
41304256 |
| W[t] = R1 (W[t - 2]) + W[t - 7] + R0 (W[t - 15]) + W[t - 16]; |
| 165 |
|
| |
| 166 |
|
| /* The actual computation according to FIPS 180-2:6.3.2 step 3. */ |
| 167 |
52275699 |
▶ | for (t = 0; t < 80; ++t) |
Branch: 1 branch
| Branch | True | False | Total |
| for (t = 0; t < 80; ++t) |
51630320 |
645379 |
52275699 |
|
| 168 |
|
| { |
| 169 |
51630320 |
| u_int64_t T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t]; |
| 170 |
51630320 |
| u_int64_t T2 = S0 (a) + Maj (a, b, c); |
| 171 |
51630320 |
| h = g; |
| 172 |
51630320 |
| g = f; |
| 173 |
51630320 |
| f = e; |
| 174 |
51630320 |
| e = d + T1; |
| 175 |
51630320 |
| d = c; |
| 176 |
51630320 |
| c = b; |
| 177 |
51630320 |
| b = a; |
| 178 |
51630320 |
| a = T1 + T2; |
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
|
| /* Add the starting values of the context according to FIPS 180-2:6.3.2 |
| 182 |
|
| step 4. */ |
| 183 |
645379 |
| a += a_save; |
| 184 |
645379 |
| b += b_save; |
| 185 |
645379 |
| c += c_save; |
| 186 |
645379 |
| d += d_save; |
| 187 |
645379 |
| e += e_save; |
| 188 |
645379 |
| f += f_save; |
| 189 |
645379 |
| g += g_save; |
| 190 |
645379 |
| h += h_save; |
| 191 |
|
| |
| 192 |
|
| /* Prepare for the next round. */ |
| 193 |
645379 |
| nwords -= 16; |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
|
| /* Put checksum in context given as argument. */ |
| 197 |
615651 |
| ctx->H[0] = a; |
| 198 |
615651 |
| ctx->H[1] = b; |
| 199 |
615651 |
| ctx->H[2] = c; |
| 200 |
615651 |
| ctx->H[3] = d; |
| 201 |
615651 |
| ctx->H[4] = e; |
| 202 |
615651 |
| ctx->H[5] = f; |
| 203 |
615651 |
| ctx->H[6] = g; |
| 204 |
615651 |
| ctx->H[7] = h; |
| 205 |
615651 |
| } |
| 206 |
|
| |
| 207 |
|
| |
| 208 |
|
| /* Initialize structure containing state of computation. |
| 209 |
|
| (FIPS 180-2:5.3.3) */ |
| 210 |
|
| static void |
| 211 |
477366 |
| cpx_sha512_init_ctx (struct sha512_ctx *ctx) |
| 212 |
|
| { |
| 213 |
477366 |
| ctx->H[0] = 0x6a09e667f3bcc908ULL; |
| 214 |
477366 |
| ctx->H[1] = 0xbb67ae8584caa73bULL; |
| 215 |
477366 |
| ctx->H[2] = 0x3c6ef372fe94f82bULL; |
| 216 |
477366 |
| ctx->H[3] = 0xa54ff53a5f1d36f1ULL; |
| 217 |
477366 |
| ctx->H[4] = 0x510e527fade682d1ULL; |
| 218 |
477366 |
| ctx->H[5] = 0x9b05688c2b3e6c1fULL; |
| 219 |
477366 |
| ctx->H[6] = 0x1f83d9abfb41bd6bULL; |
| 220 |
477366 |
| ctx->H[7] = 0x5be0cd19137e2179ULL; |
| 221 |
|
| |
| 222 |
477366 |
| ctx->total[0] = ctx->total[1] = 0; |
| 223 |
477366 |
| ctx->buflen = 0; |
| 224 |
477366 |
| } |
| 225 |
|
| |
| 226 |
|
| |
| 227 |
|
| /* Process the remaining bytes in the internal buffer and the usual |
| 228 |
|
| prolog according to the standard and write the result to RESBUF. |
| 229 |
|
| |
| 230 |
|
| IMPORTANT: On some systems it is required that RESBUF is correctly |
| 231 |
|
| aligned for a 32 bits value. */ |
| 232 |
|
| static void * |
| 233 |
477366 |
| cpx_sha512_finish_ctx (struct sha512_ctx *ctx, void *resbuf) |
| 234 |
|
| { |
| 235 |
|
| /* Take yet unprocessed bytes into account. */ |
| 236 |
477366 |
| u_int64_t bytes = ctx->buflen; |
| 237 |
|
| size_t pad; |
| 238 |
|
| unsigned int i; |
| 239 |
|
| |
| 240 |
|
| /* Now count remaining bytes. */ |
| 241 |
477366 |
| ctx->total[0] += bytes; |
| 242 |
477366 |
▶ | if (ctx->total[0] < bytes) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->total[0] < bytes) |
0 |
477366 |
477366 |
|
| 243 |
0 |
| ++ctx->total[1]; |
| 244 |
|
| |
| 245 |
477366 |
▶ | pad = bytes >= 112 ? 128 + 112 - bytes : 112 - bytes; |
Branch: 1 branch
| Branch | True | False | Total |
| pad = bytes >= 112 ? 128 + 112 - bytes : 112 - bytes; |
29728 |
447638 |
477366 |
|
| 246 |
477366 |
| memcpy (&ctx->buffer[bytes], fillbuf, pad); |
| 247 |
|
| |
| 248 |
|
| /* Put the 128-bit file length in *bits* at the end of the buffer. */ |
| 249 |
477366 |
| *(u_int64_t *) &ctx->buffer[bytes + pad + 8] = SWAP (ctx->total[0] << 3); |
| 250 |
477366 |
| *(u_int64_t *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) | |
| 251 |
|
| (ctx->total[0] >> 61)); |
| 252 |
|
| |
| 253 |
|
| /* Process last bytes. */ |
| 254 |
477366 |
| cpx_sha512_process_block (ctx->buffer, bytes + pad + 16, ctx); |
| 255 |
|
| |
| 256 |
|
| /* Put result from CTX in first 64 bytes following RESBUF. */ |
| 257 |
4296294 |
▶ | for (i = 0; i < 8; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; ++i) |
3818928 |
477366 |
4296294 |
|
| 258 |
3818928 |
| ((u_int64_t *) resbuf)[i] = SWAP (ctx->H[i]); |
| 259 |
|
| |
| 260 |
477366 |
| return resbuf; |
| 261 |
|
| } |
| 262 |
|
| |
| 263 |
|
| |
| 264 |
|
| static void |
| 265 |
1685526 |
| cpx_sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx) |
| 266 |
|
| { |
| 267 |
|
| /* When we already have some bits in our internal buffer concatenate |
| 268 |
|
| both inputs first. */ |
| 269 |
1685526 |
▶ | if (ctx->buflen != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->buflen != 0) |
1201180 |
484346 |
1685526 |
|
| 270 |
|
| { |
| 271 |
1201180 |
| size_t left_over = ctx->buflen; |
| 272 |
1201180 |
| size_t add = 256 - left_over > len ? len : 256 - left_over; |
| 273 |
|
| |
| 274 |
1201180 |
| memcpy (&ctx->buffer[left_over], buffer, add); |
| 275 |
1201180 |
| ctx->buflen += add; |
| 276 |
|
| |
| 277 |
1201180 |
▶ | if (ctx->buflen > 128) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->buflen > 128) |
138285 |
1062895 |
1201180 |
|
| 278 |
|
| { |
| 279 |
138285 |
| cpx_sha512_process_block (ctx->buffer, ctx->buflen & ~127, ctx); |
| 280 |
|
| |
| 281 |
138285 |
| ctx->buflen &= 127; |
| 282 |
|
| /* The regions in the following copy operation cannot overlap. */ |
| 283 |
138285 |
| memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~127], |
| 284 |
|
| ctx->buflen); |
| 285 |
|
| } |
| 286 |
|
| |
| 287 |
1201180 |
| buffer = (const char *) buffer + add; |
| 288 |
1201180 |
| len -= add; |
| 289 |
|
| } |
| 290 |
|
| |
| 291 |
|
| /* Process available complete blocks. */ |
| 292 |
1685526 |
▶ | if (len >= 128) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= 128) |
0 |
1685526 |
1685526 |
|
| 293 |
|
| { |
| 294 |
|
| #if !_STRING_ARCH_unaligned |
| 295 |
|
| /* To check alignment gcc has an appropriate operator. Other |
| 296 |
|
| compilers don't. */ |
| 297 |
|
| # if __GNUC__ >= 2 |
| 298 |
|
| # define UNALIGNED_P(p) (((size_t) p) % __alignof__ (u_int64_t) != 0) |
| 299 |
|
| # else |
| 300 |
|
| # define UNALIGNED_P(p) (((size_t) p) % sizeof (u_int64_t) != 0) |
| 301 |
|
| # endif |
| 302 |
0 |
▶ | if (UNALIGNED_P (buffer)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (UNALIGNED_P (buffer)) |
0 |
0 |
0 |
|
| 303 |
0 |
▶ | while (len > 128) |
Branch: 1 branch
| Branch | True | False | Total |
| while (len > 128) |
0 |
0 |
0 |
|
| 304 |
|
| { |
| 305 |
0 |
| cpx_sha512_process_block (memcpy (ctx->buffer, buffer, 128), 128, |
| 306 |
|
| ctx); |
| 307 |
0 |
| buffer = (const char *) buffer + 128; |
| 308 |
0 |
| len -= 128; |
| 309 |
|
| } |
| 310 |
|
| else |
| 311 |
|
| #endif |
| 312 |
|
| { |
| 313 |
0 |
| cpx_sha512_process_block (buffer, len & ~127, ctx); |
| 314 |
0 |
| buffer = (const char *) buffer + (len & ~127); |
| 315 |
0 |
| len &= 127; |
| 316 |
|
| } |
| 317 |
|
| } |
| 318 |
|
| |
| 319 |
|
| /* Move remaining bytes into internal buffer. */ |
| 320 |
1685526 |
▶ | if (len > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > 0) |
477341 |
1208185 |
1685526 |
|
| 321 |
|
| { |
| 322 |
477341 |
| size_t left_over = ctx->buflen; |
| 323 |
|
| |
| 324 |
477341 |
| memcpy (&ctx->buffer[left_over], buffer, len); |
| 325 |
477341 |
| left_over += len; |
| 326 |
477341 |
▶ | if (left_over >= 128) |
Branch: 1 branch
| Branch | True | False | Total |
| if (left_over >= 128) |
0 |
477341 |
477341 |
|
| 327 |
|
| { |
| 328 |
0 |
| cpx_sha512_process_block (ctx->buffer, 128, ctx); |
| 329 |
0 |
| left_over -= 128; |
| 330 |
0 |
| memcpy (ctx->buffer, &ctx->buffer[128], left_over); |
| 331 |
|
| } |
| 332 |
477341 |
| ctx->buflen = left_over; |
| 333 |
|
| } |
| 334 |
1685526 |
| } |
| 335 |
|
| |
| 336 |
|
| |
| 337 |
|
| /* Define our magic string to mark salt for SHA512 "encryption" |
| 338 |
|
| replacement. */ |
| 339 |
|
| static const char sha512_salt_prefix[] = "$6$"; |
| 340 |
|
| |
| 341 |
|
| /* Prefix for optional rounds specification. */ |
| 342 |
|
| static const char sha512_rounds_prefix[] = "rounds="; |
| 343 |
|
| |
| 344 |
|
| /* Maximum salt string length. */ |
| 345 |
|
| #define SALT_LEN_MAX 16 |
| 346 |
|
| /* Default number of rounds if not explicitly specified. */ |
| 347 |
|
| #define ROUNDS_DEFAULT 5000 |
| 348 |
|
| /* Minimum number of rounds. */ |
| 349 |
|
| #define ROUNDS_MIN 1000 |
| 350 |
|
| /* Maximum number of rounds. */ |
| 351 |
|
| #define ROUNDS_MAX 999999999 |
| 352 |
|
| |
| 353 |
|
| /* Table with characters for base64 transformation. */ |
| 354 |
|
| static const char b64t[64] = |
| 355 |
|
| "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 356 |
|
| |
| 357 |
|
| |
| 358 |
|
| static char * |
| 359 |
20 |
| cpx_sha512_crypt_r (const char *key, const char *salt, char *buffer, int buflen) |
| 360 |
|
| { |
| 361 |
|
| unsigned char alt_result[64] |
| 362 |
|
| __attribute__ ((__aligned__ (__alignof__ (u_int64_t)))); |
| 363 |
|
| unsigned char temp_result[64] |
| 364 |
|
| __attribute__ ((__aligned__ (__alignof__ (u_int64_t)))); |
| 365 |
|
| struct sha512_ctx ctx; |
| 366 |
|
| struct sha512_ctx alt_ctx; |
| 367 |
|
| size_t salt_len; |
| 368 |
|
| size_t key_len; |
| 369 |
|
| size_t cnt; |
| 370 |
|
| char *cp; |
| 371 |
20 |
| char *copied_key = NULL; |
| 372 |
20 |
| char *copied_salt = NULL; |
| 373 |
|
| char *p_bytes; |
| 374 |
|
| char *s_bytes; |
| 375 |
|
| /* Default number of rounds. */ |
| 376 |
20 |
| size_t rounds = ROUNDS_DEFAULT; |
| 377 |
20 |
| bool rounds_custom = false; |
| 378 |
|
| |
| 379 |
|
| /* Find beginning of salt string. The prefix should normally always |
| 380 |
|
| be present. Just in case it is not. */ |
| 381 |
20 |
▶ | if (strncmp (sha512_salt_prefix, salt, sizeof (sha512_salt_prefix) - 1) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp (sha512_salt_prefix, salt, sizeof (sha512_salt_prefix) - 1) == 0) |
15 |
5 |
20 |
|
| 382 |
|
| /* Skip salt prefix. */ |
| 383 |
15 |
| salt += sizeof (sha512_salt_prefix) - 1; |
| 384 |
|
| |
| 385 |
20 |
▶ | if (strncmp (salt, sha512_rounds_prefix, sizeof (sha512_rounds_prefix) - 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp (salt, sha512_rounds_prefix, sizeof (sha512_rounds_prefix) - 1) |
12 |
8 |
20 |
|
| 386 |
|
| == 0) |
| 387 |
|
| { |
| 388 |
12 |
| const char *num = salt + sizeof (sha512_rounds_prefix) - 1; |
| 389 |
|
| char *endp; |
| 390 |
12 |
| unsigned long int srounds = strtoul (num, &endp, 10); |
| 391 |
12 |
▶ | if (*endp == '$') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*endp == '$') |
12 |
0 |
12 |
|
| 392 |
|
| { |
| 393 |
12 |
| salt = endp + 1; |
| 394 |
12 |
▶ | rounds = MAX (ROUNDS_MIN, MIN (srounds, ROUNDS_MAX)); |
Branch: 1 branch
| Branch | True | False | Total |
| rounds = MAX (ROUNDS_MIN, MIN (srounds, ROUNDS_MAX)); |
10 |
2 |
12 |
|
| 395 |
12 |
| rounds_custom = true; |
| 396 |
|
| } |
| 397 |
|
| } |
| 398 |
|
| |
| 399 |
20 |
▶ | salt_len = MIN (strcspn (salt, "$"), SALT_LEN_MAX); |
Branch: 1 branch
| Branch | True | False | Total |
| salt_len = MIN (strcspn (salt, "$"), SALT_LEN_MAX); |
12 |
8 |
20 |
|
| 400 |
20 |
| key_len = strlen (key); |
| 401 |
|
| |
| 402 |
20 |
▶ | if ((key - (char *) 0) % __alignof__ (u_int64_t) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((key - (char *) 0) % __alignof__ (u_int64_t) != 0) |
0 |
20 |
20 |
|
| 403 |
|
| { |
| 404 |
0 |
| char *tmp = (char *) alloca (key_len + __alignof__ (u_int64_t)); |
| 405 |
0 |
| key = copied_key = |
| 406 |
0 |
| memcpy (tmp + __alignof__ (u_int64_t) |
| 407 |
0 |
| - (tmp - (char *) 0) % __alignof__ (u_int64_t), |
| 408 |
|
| key, key_len); |
| 409 |
|
| } |
| 410 |
|
| |
| 411 |
20 |
▶ | if ((salt - (char *) 0) % __alignof__ (u_int64_t) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((salt - (char *) 0) % __alignof__ (u_int64_t) != 0) |
12 |
8 |
20 |
|
| 412 |
|
| { |
| 413 |
12 |
| char *tmp = (char *) alloca (salt_len + __alignof__ (u_int64_t)); |
| 414 |
12 |
| salt = copied_salt = |
| 415 |
12 |
| memcpy (tmp + __alignof__ (u_int64_t) |
| 416 |
12 |
| - (tmp - (char *) 0) % __alignof__ (u_int64_t), |
| 417 |
|
| salt, salt_len); |
| 418 |
|
| } |
| 419 |
|
| |
| 420 |
|
| /* Prepare for the real work. */ |
| 421 |
20 |
| cpx_sha512_init_ctx (&ctx); |
| 422 |
|
| |
| 423 |
|
| /* Add the key string. */ |
| 424 |
20 |
| cpx_sha512_process_bytes (key, key_len, &ctx); |
| 425 |
|
| |
| 426 |
|
| /* The last part is the salt string. This must be at most 16 |
| 427 |
|
| characters and it ends at the first `$' character (for |
| 428 |
|
| compatibility with existing implementations). */ |
| 429 |
20 |
| cpx_sha512_process_bytes (salt, salt_len, &ctx); |
| 430 |
|
| |
| 431 |
|
| |
| 432 |
|
| /* Compute alternate SHA512 sum with input KEY, SALT, and KEY. The |
| 433 |
|
| final result will be added to the first context. */ |
| 434 |
20 |
| cpx_sha512_init_ctx (&alt_ctx); |
| 435 |
|
| |
| 436 |
|
| /* Add key. */ |
| 437 |
20 |
| cpx_sha512_process_bytes (key, key_len, &alt_ctx); |
| 438 |
|
| |
| 439 |
|
| /* Add salt. */ |
| 440 |
20 |
| cpx_sha512_process_bytes (salt, salt_len, &alt_ctx); |
| 441 |
|
| |
| 442 |
|
| /* Add key again. */ |
| 443 |
20 |
| cpx_sha512_process_bytes (key, key_len, &alt_ctx); |
| 444 |
|
| |
| 445 |
|
| /* Now get result of this (64 bytes) and add it to the other |
| 446 |
|
| context. */ |
| 447 |
20 |
| cpx_sha512_finish_ctx (&alt_ctx, alt_result); |
| 448 |
|
| |
| 449 |
|
| /* Add for any character in the key one byte of the alternate sum. */ |
| 450 |
22 |
▶ | for (cnt = key_len; cnt > 64; cnt -= 64) |
Branch: 1 branch
| Branch | True | False | Total |
| for (cnt = key_len; cnt > 64; cnt -= 64) |
2 |
20 |
22 |
|
| 451 |
2 |
| cpx_sha512_process_bytes (alt_result, 64, &ctx); |
| 452 |
20 |
| cpx_sha512_process_bytes (alt_result, cnt, &ctx); |
| 453 |
|
| |
| 454 |
|
| /* Take the binary representation of the length of the key and for every |
| 455 |
|
| 1 add the alternate sum, for every 0 the key. */ |
| 456 |
108 |
▶ | for (cnt = key_len; cnt > 0; cnt >>= 1) |
Branch: 1 branch
| Branch | True | False | Total |
| for (cnt = key_len; cnt > 0; cnt >>= 1) |
88 |
20 |
108 |
|
| 457 |
88 |
▶ | if ((cnt & 1) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((cnt & 1) != 0) |
41 |
47 |
88 |
|
| 458 |
41 |
| cpx_sha512_process_bytes (alt_result, 64, &ctx); |
| 459 |
|
| else |
| 460 |
47 |
| cpx_sha512_process_bytes (key, key_len, &ctx); |
| 461 |
|
| |
| 462 |
|
| /* Create intermediate result. */ |
| 463 |
20 |
| cpx_sha512_finish_ctx (&ctx, alt_result); |
| 464 |
|
| |
| 465 |
|
| /* Start computation of P byte sequence. */ |
| 466 |
20 |
| cpx_sha512_init_ctx (&alt_ctx); |
| 467 |
|
| |
| 468 |
|
| /* For every character in the password add the entire password. */ |
| 469 |
514 |
▶ | for (cnt = 0; cnt < key_len; ++cnt) |
Branch: 1 branch
| Branch | True | False | Total |
| for (cnt = 0; cnt < key_len; ++cnt) |
494 |
20 |
514 |
|
| 470 |
494 |
| cpx_sha512_process_bytes (key, key_len, &alt_ctx); |
| 471 |
|
| |
| 472 |
|
| /* Finish the digest. */ |
| 473 |
20 |
| cpx_sha512_finish_ctx (&alt_ctx, temp_result); |
| 474 |
|
| |
| 475 |
|
| /* Create byte sequence P. */ |
| 476 |
20 |
| cp = p_bytes = alloca (key_len); |
| 477 |
22 |
▶ | for (cnt = key_len; cnt >= 64; cnt -= 64) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (cnt = key_len; cnt >= 64; cnt -= 64) { |
2 |
20 |
22 |
|
| 478 |
2 |
| memcpy (cp, temp_result, 64); |
| 479 |
2 |
| cp += 64; |
| 480 |
|
| } |
| 481 |
20 |
| memcpy (cp, temp_result, cnt); |
| 482 |
|
| |
| 483 |
|
| /* Start computation of S byte sequence. */ |
| 484 |
20 |
| cpx_sha512_init_ctx (&alt_ctx); |
| 485 |
|
| |
| 486 |
|
| /* For every character in the password add the entire password. */ |
| 487 |
3064 |
▶ | for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt) |
Branch: 1 branch
| Branch | True | False | Total |
| for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt) |
3044 |
20 |
3064 |
|
| 488 |
3044 |
| cpx_sha512_process_bytes (salt, salt_len, &alt_ctx); |
| 489 |
|
| |
| 490 |
|
| /* Finish the digest. */ |
| 491 |
20 |
| cpx_sha512_finish_ctx (&alt_ctx, temp_result); |
| 492 |
|
| |
| 493 |
|
| /* Create byte sequence S. */ |
| 494 |
20 |
| cp = s_bytes = alloca (salt_len); |
| 495 |
20 |
▶ | for (cnt = salt_len; cnt >= 64; cnt -= 64) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (cnt = salt_len; cnt >= 64; cnt -= 64) { |
0 |
20 |
20 |
|
| 496 |
0 |
| memcpy (cp, temp_result, 64); |
| 497 |
0 |
| cp += 64; |
| 498 |
|
| } |
| 499 |
20 |
| memcpy (cp, temp_result, cnt); |
| 500 |
|
| |
| 501 |
|
| /* Repeatedly run the collected hash value through SHA512 to burn |
| 502 |
|
| CPU cycles. */ |
| 503 |
477286 |
▶ | for (cnt = 0; cnt < rounds; ++cnt) |
Branch: 1 branch
| Branch | True | False | Total |
| for (cnt = 0; cnt < rounds; ++cnt) |
477266 |
20 |
477286 |
|
| 504 |
|
| { |
| 505 |
|
| /* New context. */ |
| 506 |
477266 |
| cpx_sha512_init_ctx (&ctx); |
| 507 |
|
| |
| 508 |
|
| /* Add key or last result. */ |
| 509 |
477266 |
▶ | if ((cnt & 1) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((cnt & 1) != 0) |
238632 |
238634 |
477266 |
|
| 510 |
238632 |
| cpx_sha512_process_bytes (p_bytes, key_len, &ctx); |
| 511 |
|
| else |
| 512 |
238634 |
| cpx_sha512_process_bytes (alt_result, 64, &ctx); |
| 513 |
|
| |
| 514 |
|
| /* Add salt for numbers not divisible by 3. */ |
| 515 |
477266 |
▶ | if (cnt % 3 != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cnt % 3 != 0) |
318170 |
159096 |
477266 |
|
| 516 |
318170 |
| cpx_sha512_process_bytes (s_bytes, salt_len, &ctx); |
| 517 |
|
| |
| 518 |
|
| /* Add key for numbers not divisible by 7. */ |
| 519 |
477266 |
▶ | if (cnt % 7 != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cnt % 7 != 0) |
409076 |
68190 |
477266 |
|
| 520 |
409076 |
| cpx_sha512_process_bytes (p_bytes, key_len, &ctx); |
| 521 |
|
| |
| 522 |
|
| /* Add key or last result. */ |
| 523 |
477266 |
▶ | if ((cnt & 1) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((cnt & 1) != 0) |
238632 |
238634 |
477266 |
|
| 524 |
238632 |
| cpx_sha512_process_bytes (alt_result, 64, &ctx); |
| 525 |
|
| else |
| 526 |
238634 |
| cpx_sha512_process_bytes (p_bytes, key_len, &ctx); |
| 527 |
|
| |
| 528 |
|
| /* Create intermediate result. */ |
| 529 |
477266 |
| cpx_sha512_finish_ctx (&ctx, alt_result); |
| 530 |
|
| } |
| 531 |
|
| |
| 532 |
|
| /* Now we can construct the result string. It consists of three |
| 533 |
|
| parts. */ |
| 534 |
20 |
| cp = strncpy (buffer, sha512_salt_prefix, MAX (0, buflen)); |
| 535 |
20 |
| cp += MIN(strlen(sha512_salt_prefix), MAX (0, buflen)); |
| 536 |
20 |
| buflen -= sizeof (sha512_salt_prefix) - 1; |
| 537 |
|
| |
| 538 |
20 |
▶ | if (rounds_custom) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rounds_custom) |
12 |
8 |
20 |
|
| 539 |
|
| { |
| 540 |
12 |
| int n = snprintf (cp, MAX (0, buflen), "%s%u$", |
| 541 |
|
| sha512_rounds_prefix, (unsigned int)rounds); |
| 542 |
12 |
| cp += n; |
| 543 |
12 |
| buflen -= n; |
| 544 |
|
| } |
| 545 |
|
| |
| 546 |
20 |
| cp = strncpy (cp, salt, MIN ((size_t) MAX (0, buflen), salt_len)); |
| 547 |
20 |
| cp += MIN ((size_t) MAX (0, buflen), salt_len); |
| 548 |
20 |
| buflen -= MIN ((size_t) MAX (0, buflen), salt_len); |
| 549 |
|
| |
| 550 |
20 |
▶ | if (buflen > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (buflen > 0) |
20 |
0 |
20 |
|
| 551 |
|
| { |
| 552 |
20 |
| *cp++ = '$'; |
| 553 |
20 |
| --buflen; |
| 554 |
|
| } |
| 555 |
|
| |
| 556 |
|
| #define b64_from_24bit(B2, B1, B0, N) \ |
| 557 |
|
| do { \ |
| 558 |
|
| unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \ |
| 559 |
|
| int n = (N); \ |
| 560 |
|
| while (n-- > 0 && buflen > 0) \ |
| 561 |
|
| { \ |
| 562 |
|
| *cp++ = b64t[w & 0x3f]; \ |
| 563 |
|
| --buflen; \ |
| 564 |
|
| w >>= 6; \ |
| 565 |
|
| } \ |
| 566 |
|
| } while (0) |
| 567 |
|
| |
| 568 |
100 |
▶ | b64_from_24bit (alt_result[0], alt_result[21], alt_result[42], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[0], alt_result[21], alt_result[42], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[0], alt_result[21], alt_result[42], 4); |
80 |
0 |
80 |
|
| 569 |
100 |
▶ | b64_from_24bit (alt_result[22], alt_result[43], alt_result[1], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[22], alt_result[43], alt_result[1], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[22], alt_result[43], alt_result[1], 4); |
80 |
0 |
80 |
|
| 570 |
100 |
▶ | b64_from_24bit (alt_result[44], alt_result[2], alt_result[23], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[44], alt_result[2], alt_result[23], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[44], alt_result[2], alt_result[23], 4); |
80 |
0 |
80 |
|
| 571 |
100 |
▶ | b64_from_24bit (alt_result[3], alt_result[24], alt_result[45], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[3], alt_result[24], alt_result[45], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[3], alt_result[24], alt_result[45], 4); |
80 |
0 |
80 |
|
| 572 |
100 |
▶ | b64_from_24bit (alt_result[25], alt_result[46], alt_result[4], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[25], alt_result[46], alt_result[4], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[25], alt_result[46], alt_result[4], 4); |
80 |
0 |
80 |
|
| 573 |
100 |
▶ | b64_from_24bit (alt_result[47], alt_result[5], alt_result[26], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[47], alt_result[5], alt_result[26], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[47], alt_result[5], alt_result[26], 4); |
80 |
0 |
80 |
|
| 574 |
100 |
▶ | b64_from_24bit (alt_result[6], alt_result[27], alt_result[48], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[6], alt_result[27], alt_result[48], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[6], alt_result[27], alt_result[48], 4); |
80 |
0 |
80 |
|
| 575 |
100 |
▶ | b64_from_24bit (alt_result[28], alt_result[49], alt_result[7], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[28], alt_result[49], alt_result[7], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[28], alt_result[49], alt_result[7], 4); |
80 |
0 |
80 |
|
| 576 |
100 |
▶ | b64_from_24bit (alt_result[50], alt_result[8], alt_result[29], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[50], alt_result[8], alt_result[29], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[50], alt_result[8], alt_result[29], 4); |
80 |
0 |
80 |
|
| 577 |
100 |
▶ | b64_from_24bit (alt_result[9], alt_result[30], alt_result[51], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[9], alt_result[30], alt_result[51], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[9], alt_result[30], alt_result[51], 4); |
80 |
0 |
80 |
|
| 578 |
100 |
▶ | b64_from_24bit (alt_result[31], alt_result[52], alt_result[10], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[31], alt_result[52], alt_result[10], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[31], alt_result[52], alt_result[10], 4); |
80 |
0 |
80 |
|
| 579 |
100 |
▶ | b64_from_24bit (alt_result[53], alt_result[11], alt_result[32], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[53], alt_result[11], alt_result[32], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[53], alt_result[11], alt_result[32], 4); |
80 |
0 |
80 |
|
| 580 |
100 |
▶ | b64_from_24bit (alt_result[12], alt_result[33], alt_result[54], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[12], alt_result[33], alt_result[54], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[12], alt_result[33], alt_result[54], 4); |
80 |
0 |
80 |
|
| 581 |
100 |
▶ | b64_from_24bit (alt_result[34], alt_result[55], alt_result[13], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[34], alt_result[55], alt_result[13], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[34], alt_result[55], alt_result[13], 4); |
80 |
0 |
80 |
|
| 582 |
100 |
▶ | b64_from_24bit (alt_result[56], alt_result[14], alt_result[35], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[56], alt_result[14], alt_result[35], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[56], alt_result[14], alt_result[35], 4); |
80 |
0 |
80 |
|
| 583 |
100 |
▶ | b64_from_24bit (alt_result[15], alt_result[36], alt_result[57], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[15], alt_result[36], alt_result[57], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[15], alt_result[36], alt_result[57], 4); |
80 |
0 |
80 |
|
| 584 |
100 |
▶ | b64_from_24bit (alt_result[37], alt_result[58], alt_result[16], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[37], alt_result[58], alt_result[16], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[37], alt_result[58], alt_result[16], 4); |
80 |
0 |
80 |
|
| 585 |
100 |
▶ | b64_from_24bit (alt_result[59], alt_result[17], alt_result[38], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[59], alt_result[17], alt_result[38], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[59], alt_result[17], alt_result[38], 4); |
80 |
0 |
80 |
|
| 586 |
100 |
▶ | b64_from_24bit (alt_result[18], alt_result[39], alt_result[60], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[18], alt_result[39], alt_result[60], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[18], alt_result[39], alt_result[60], 4); |
80 |
0 |
80 |
|
| 587 |
100 |
▶ | b64_from_24bit (alt_result[40], alt_result[61], alt_result[19], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[40], alt_result[61], alt_result[19], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[40], alt_result[61], alt_result[19], 4); |
80 |
0 |
80 |
|
| 588 |
100 |
▶ | b64_from_24bit (alt_result[62], alt_result[20], alt_result[41], 4); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (alt_result[62], alt_result[20], alt_result[41], 4); |
80 |
20 |
100 |
| b64_from_24bit (alt_result[62], alt_result[20], alt_result[41], 4); |
80 |
0 |
80 |
|
| 589 |
60 |
▶ | b64_from_24bit (0, 0, alt_result[63], 2); |
Branch: 2 branches
| Branch | True | False | Total |
| b64_from_24bit (0, 0, alt_result[63], 2); |
40 |
20 |
60 |
| b64_from_24bit (0, 0, alt_result[63], 2); |
40 |
0 |
40 |
|
| 590 |
|
| |
| 591 |
20 |
▶ | if (buflen <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (buflen <= 0) |
0 |
20 |
20 |
|
| 592 |
|
| { |
| 593 |
0 |
| errno = ERANGE; |
| 594 |
0 |
| buffer = NULL; |
| 595 |
|
| } |
| 596 |
|
| else |
| 597 |
20 |
| *cp = '\0'; /* Terminate the string. */ |
| 598 |
|
| |
| 599 |
|
| /* Clear the buffer for the intermediate result so that people |
| 600 |
|
| attaching to processes or reading core dumps cannot get any |
| 601 |
|
| information. We do it in this way to clear correct_words[] |
| 602 |
|
| inside the SHA512 implementation as well. */ |
| 603 |
20 |
| cpx_sha512_init_ctx (&ctx); |
| 604 |
20 |
| cpx_sha512_finish_ctx (&ctx, alt_result); |
| 605 |
20 |
| memset (temp_result, '\0', sizeof (temp_result)); |
| 606 |
20 |
| memset (p_bytes, '\0', key_len); |
| 607 |
20 |
| memset (s_bytes, '\0', salt_len); |
| 608 |
20 |
| memset (&ctx, '\0', sizeof (ctx)); |
| 609 |
20 |
| memset (&alt_ctx, '\0', sizeof (alt_ctx)); |
| 610 |
20 |
▶ | if (copied_key != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (copied_key != NULL) |
0 |
20 |
20 |
|
| 611 |
0 |
| memset (copied_key, '\0', key_len); |
| 612 |
20 |
▶ | if (copied_salt != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (copied_salt != NULL) |
12 |
8 |
20 |
|
| 613 |
12 |
| memset (copied_salt, '\0', salt_len); |
| 614 |
|
| |
| 615 |
20 |
| return buffer; |
| 616 |
|
| } |
| 617 |
|
| |
| 618 |
|
| |
| 619 |
|
| /* This entry point is equivalent to the `crypt' function in Unix |
| 620 |
|
| libcs. */ |
| 621 |
|
| char * |
| 622 |
20 |
| cpx_sha512_crypt (const char *key, const char *salt) |
| 623 |
|
| { |
| 624 |
|
| /* We don't want to have an arbitrary limit in the size of the |
| 625 |
|
| password. We can compute an upper bound for the size of the |
| 626 |
|
| result in advance and so we can prepare the buffer we pass to |
| 627 |
|
| `cpx_sha512_crypt_r'. */ |
| 628 |
|
| static char *buffer; |
| 629 |
|
| static int buflen; |
| 630 |
20 |
| int needed = (sizeof (sha512_salt_prefix) - 1 |
| 631 |
|
| + sizeof (sha512_rounds_prefix) + 9 + 1 |
| 632 |
20 |
| + strlen (salt) + 1 + 86 + 1); |
| 633 |
|
| |
| 634 |
20 |
▶ | if (buflen < needed) |
Branch: 1 branch
| Branch | True | False | Total |
| if (buflen < needed) |
4 |
16 |
20 |
|
| 635 |
|
| { |
| 636 |
4 |
| char *new_buffer = (char *) realloc (buffer, needed); |
| 637 |
4 |
▶ | if (new_buffer == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_buffer == NULL) |
0 |
4 |
4 |
|
| 638 |
0 |
| return NULL; |
| 639 |
|
| |
| 640 |
4 |
| buffer = new_buffer; |
| 641 |
4 |
| buflen = needed; |
| 642 |
|
| } |
| 643 |
|
| |
| 644 |
20 |
| return cpx_sha512_crypt_r (key, salt, buffer, buflen); |
| 645 |
|
| } |