328{
329#ifdef _MSC_VER
330 unsigned char alt_result[32];
331 unsigned char temp_result[32];
332#else
333 unsigned char alt_result[32]
335 unsigned char temp_result[32]
337#endif
340 size_t salt_len;
341 size_t key_len;
342 size_t cnt;
343 char *cp;
344 char *copied_key = NULL;
345 char *copied_salt = NULL;
346 char *p_bytes;
347 char *s_bytes;
348
350 int rounds_custom = 0;
351
352
353
355
357
359 == 0) {
361 char *endp;
362 unsigned long int srounds = strtoul(num, &endp, 10);
363 if (*endp == '$') {
364 salt = endp + 1;
366 rounds_custom = 1;
367 }
368 }
369
371 key_len = strlen(key);
372
373 if ((key - (char *) 0) % __alignof__(uint32_t) != 0) {
374 char *tmp = (char *) alloca(key_len + __alignof__(uint32_t));
375 key = copied_key = (
char *) memcpy(tmp + __alignof__(uint32_t)
376 - (tmp - (char *) 0) % __alignof__(uint32_t), key, key_len);
377 }
378
379 if ((salt - (char *) 0) % __alignof__(uint32_t) != 0) {
380 char *tmp = (char *) alloca(salt_len + __alignof__(uint32_t));
381 salt = copied_salt = (char *) memcpy(tmp + __alignof__(uint32_t)
382 - (tmp - (char *) 0) % __alignof__(uint32_t), salt, salt_len);
383 }
384
385
387
388
390
391
392
393
395
396
397
398
400
401
403
404
406
407
409
410
411
413
414
415 for (cnt = key_len; cnt > 32; cnt -= 32)
418
419
420
421 for (cnt = key_len; cnt > 0; cnt >>= 1)
422 if ((cnt & 1) != 0)
424 else
426
427
429
430
432
433
434 for (cnt = 0; cnt < key_len; ++cnt)
436
437
439
440
441 cp = p_bytes = (char*) alloca(key_len);
442 for (cnt = key_len; cnt >= 32; cnt -= 32) {
443 memcpy(cp, temp_result, 32);
444 cp += 32;
445 }
446 memcpy(cp, temp_result, cnt);
447
448
450
451
452 for (cnt = 0; (int) cnt < 16 + alt_result[0]; ++cnt)
454
455
457
458
459 cp = s_bytes = (char*) alloca(salt_len);
460 for (cnt = salt_len; cnt >= 32; cnt -= 32) {
461 memcpy(cp, temp_result, 32);
462 cp += 32;
463 }
464 memcpy(cp, temp_result, cnt);
465
466
467
468 for (cnt = 0; cnt < rounds; ++cnt) {
469
471
472
473 if ((cnt & 1) != 0)
475 else
477
478
479 if (cnt % 3 != 0)
481
482
483 if (cnt % 7 != 0)
485
486
487 if ((cnt & 1) != 0)
489 else
491
492
494 }
495
496
497
501
502 if (rounds_custom) {
503#ifdef _MSC_VER
504 int n = _snprintf(cp,
MAX(0,
buflen),
"%s%Iu$",
506#else
507 int n = snprintf(cp,
MAX(0,
buflen),
"%s%zu$",
509#endif
512 }
513
514 strncpy(cp, salt,
MIN((
size_t)
MAX(0,
buflen), salt_len));
515 cp = cp + strlen(cp);
517
519 *cp++ = '$';
521 }
522#define b64_from_24bit(B2, B1, B0, N) \
523 do { \
524 unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
525 int n = (N); \
526 while (n-- > 0 && buflen > 0) \
527 { \
528 *cp++ = b64t[w & 0x3f]; \
529 --buflen; \
530 w >>= 6; \
531 } \
532 } while (0)
533
546 errno = ERANGE;
548 } else
549 *cp = '\0';
550
551
552
553
554
557 memset(temp_result, '\0', sizeof(temp_result));
558 memset(p_bytes, '\0', key_len);
559 memset(s_bytes, '\0', salt_len);
560 memset(&ctx, '\0', sizeof(ctx));
561 memset(&alt_ctx, '\0', sizeof(alt_ctx));
562 if (copied_key != NULL)
563 memset(copied_key, '\0', key_len);
564 if (copied_salt != NULL)
565 memset(copied_salt, '\0', salt_len);
566
568}
static void * sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf)
static void sha256_init_ctx(struct sha256_ctx *ctx)
static void sha256_process_bytes(const void *buffer, size_t len, struct sha256_ctx *ctx)
static const char sha256_salt_prefix[]
#define b64_from_24bit(B2, B1, B0, N)
static const char sha256_rounds_prefix[]
struct __attribute__((__packed__)) InfoWord