bcrypt 3.1.12 → 3.1.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,16 @@
1
1
  /*
2
- * Written by Solar Designer and placed in the public domain.
2
+ * Written by Solar Designer <solar at openwall.com> in 2000-2002.
3
+ * No copyright is claimed, and the software is hereby placed in the public
4
+ * domain. In case this attempt to disclaim copyright and place the software
5
+ * in the public domain is deemed null and void, then the software is
6
+ * Copyright (c) 2000-2002 Solar Designer and it is hereby released to the
7
+ * general public under the following terms:
8
+ *
9
+ * Redistribution and use in source and binary forms, with or without
10
+ * modification, are permitted.
11
+ *
12
+ * There's ABSOLUTELY NO WARRANTY, express or implied.
13
+ *
3
14
  * See crypt_blowfish.c for more information.
4
15
  */
5
16
 
@@ -1,26 +1,38 @@
1
1
  /*
2
+ * The crypt_blowfish homepage is:
3
+ *
4
+ * http://www.openwall.com/crypt/
5
+ *
2
6
  * This code comes from John the Ripper password cracker, with reentrant
3
7
  * and crypt(3) interfaces added, but optimizations specific to password
4
8
  * cracking removed.
5
9
  *
6
- * Written by Solar Designer <solar at openwall.com> in 1998-2002 and
7
- * placed in the public domain. Quick self-test added in 2011 and also
8
- * placed in the public domain.
10
+ * Written by Solar Designer <solar at openwall.com> in 1998-2014.
11
+ * No copyright is claimed, and the software is hereby placed in the public
12
+ * domain. In case this attempt to disclaim copyright and place the software
13
+ * in the public domain is deemed null and void, then the software is
14
+ * Copyright (c) 1998-2014 Solar Designer and it is hereby released to the
15
+ * general public under the following terms:
16
+ *
17
+ * Redistribution and use in source and binary forms, with or without
18
+ * modification, are permitted.
9
19
  *
10
- * There's absolutely no warranty.
20
+ * There's ABSOLUTELY NO WARRANTY, express or implied.
11
21
  *
12
22
  * It is my intent that you should be able to use this on your system,
13
- * as a part of a software package, or anywhere else to improve security,
23
+ * as part of a software package, or anywhere else to improve security,
14
24
  * ensure compatibility, or for any other purpose. I would appreciate
15
25
  * it if you give credit where it is due and keep your modifications in
16
26
  * the public domain as well, but I don't require that in order to let
17
27
  * you place this code and any modifications you make under a license
18
28
  * of your choice.
19
29
  *
20
- * This implementation is compatible with OpenBSD bcrypt.c (version 2a)
21
- * by Niels Provos <provos at citi.umich.edu>, and uses some of his
22
- * ideas. The password hashing algorithm was designed by David Mazieres
23
- * <dm at lcs.mit.edu>.
30
+ * This implementation is fully compatible with OpenBSD's bcrypt.c for prefix
31
+ * "$2b$", originally by Niels Provos <provos at citi.umich.edu>, and it uses
32
+ * some of his ideas. The password hashing algorithm was designed by David
33
+ * Mazieres <dm at lcs.mit.edu>. For information on the level of
34
+ * compatibility for bcrypt hash prefixes other than "$2b$", please refer to
35
+ * the comments in BF_set_key() below and to the included crypt(3) man page.
24
36
  *
25
37
  * There's a paper on the algorithm that explains its design decisions:
26
38
  *
@@ -38,21 +50,13 @@
38
50
  #define __set_errno(val) errno = (val)
39
51
  #endif
40
52
 
41
- #undef __CONST
42
- #ifdef __GNUC__
43
- #define __CONST __const
44
- #else
45
- #define __CONST
46
- #endif
53
+ /* Just to make sure the prototypes match the actual definitions */
54
+ #include "crypt_blowfish.h"
47
55
 
48
- /*
49
- * Please keep this enabled. We really don't want incompatible hashes to be
50
- * produced. The performance cost of this quick self-test is around 0.6% at
51
- * the "$2a$08" setting.
52
- */
53
- #define BF_SELF_TEST
54
-
55
- #if defined(__x86_64__) || defined(__alpha__) || defined(__hppa__)
56
+ #ifdef __i386__
57
+ #define BF_ASM 1
58
+ #define BF_SCALE 1
59
+ #elif defined(__x86_64__) || defined(__alpha__) || defined(__hppa__)
56
60
  #define BF_ASM 0
57
61
  #define BF_SCALE 1
58
62
  #else
@@ -369,14 +373,6 @@ static unsigned char BF_atoi64[0x60] = {
369
373
  43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 64, 64, 64, 64, 64
370
374
  };
371
375
 
372
- /*
373
- * This may be optimized out if built with function inlining and no BF_ASM.
374
- */
375
- static void clean(void *data, int size)
376
- {
377
- memset(data, 0, size);
378
- }
379
-
380
376
  #define BF_safe_atoi64(dst, src) \
381
377
  { \
382
378
  tmp = (unsigned char)(src); \
@@ -386,11 +382,11 @@ static void clean(void *data, int size)
386
382
  (dst) = tmp; \
387
383
  }
388
384
 
389
- static int BF_decode(BF_word *dst, __CONST char *src, int size)
385
+ static int BF_decode(BF_word *dst, const char *src, int size)
390
386
  {
391
387
  unsigned char *dptr = (unsigned char *)dst;
392
388
  unsigned char *end = dptr + size;
393
- unsigned char *sptr = (unsigned char *)src;
389
+ const unsigned char *sptr = (const unsigned char *)src;
394
390
  unsigned int tmp, c1, c2, c3, c4;
395
391
 
396
392
  do {
@@ -410,10 +406,10 @@ static int BF_decode(BF_word *dst, __CONST char *src, int size)
410
406
  return 0;
411
407
  }
412
408
 
413
- static void BF_encode(char *dst, __CONST BF_word *src, int size)
409
+ static void BF_encode(char *dst, const BF_word *src, int size)
414
410
  {
415
- unsigned char *sptr = (unsigned char *)src;
416
- unsigned char *end = sptr + size;
411
+ const unsigned char *sptr = (const unsigned char *)src;
412
+ const unsigned char *end = sptr + size;
417
413
  unsigned char *dptr = (unsigned char *)dst;
418
414
  unsigned int c1, c2;
419
415
 
@@ -521,6 +517,10 @@ static void BF_swap(BF_word *x, int count)
521
517
  R = L; \
522
518
  L = tmp4 ^ data.ctx.P[BF_N + 1];
523
519
 
520
+ #if BF_ASM
521
+ #define BF_body() \
522
+ _BF_body_r(&data.ctx);
523
+ #else
524
524
  #define BF_body() \
525
525
  L = R = 0; \
526
526
  ptr = data.ctx.P; \
@@ -538,35 +538,121 @@ static void BF_swap(BF_word *x, int count)
538
538
  *(ptr - 2) = L; \
539
539
  *(ptr - 1) = R; \
540
540
  } while (ptr < &data.ctx.S[3][0xFF]);
541
+ #endif
541
542
 
542
- static void BF_set_key(__CONST char *key, BF_key expanded, BF_key initial,
543
- int sign_extension_bug)
543
+ static void BF_set_key(const char *key, BF_key expanded, BF_key initial,
544
+ unsigned char flags)
544
545
  {
545
- __CONST char *ptr = key;
546
- int i, j;
547
- BF_word tmp;
546
+ const char *ptr = key;
547
+ unsigned int bug, i, j;
548
+ BF_word safety, sign, diff, tmp[2];
549
+
550
+ /*
551
+ * There was a sign extension bug in older revisions of this function. While
552
+ * we would have liked to simply fix the bug and move on, we have to provide
553
+ * a backwards compatibility feature (essentially the bug) for some systems and
554
+ * a safety measure for some others. The latter is needed because for certain
555
+ * multiple inputs to the buggy algorithm there exist easily found inputs to
556
+ * the correct algorithm that produce the same hash. Thus, we optionally
557
+ * deviate from the correct algorithm just enough to avoid such collisions.
558
+ * While the bug itself affected the majority of passwords containing
559
+ * characters with the 8th bit set (although only a percentage of those in a
560
+ * collision-producing way), the anti-collision safety measure affects
561
+ * only a subset of passwords containing the '\xff' character (not even all of
562
+ * those passwords, just some of them). This character is not found in valid
563
+ * UTF-8 sequences and is rarely used in popular 8-bit character encodings.
564
+ * Thus, the safety measure is unlikely to cause much annoyance, and is a
565
+ * reasonable tradeoff to use when authenticating against existing hashes that
566
+ * are not reliably known to have been computed with the correct algorithm.
567
+ *
568
+ * We use an approach that tries to minimize side-channel leaks of password
569
+ * information - that is, we mostly use fixed-cost bitwise operations instead
570
+ * of branches or table lookups. (One conditional branch based on password
571
+ * length remains. It is not part of the bug aftermath, though, and is
572
+ * difficult and possibly unreasonable to avoid given the use of C strings by
573
+ * the caller, which results in similar timing leaks anyway.)
574
+ *
575
+ * For actual implementation, we set an array index in the variable "bug"
576
+ * (0 means no bug, 1 means sign extension bug emulation) and a flag in the
577
+ * variable "safety" (bit 16 is set when the safety measure is requested).
578
+ * Valid combinations of settings are:
579
+ *
580
+ * Prefix "$2a$": bug = 0, safety = 0x10000
581
+ * Prefix "$2b$": bug = 0, safety = 0
582
+ * Prefix "$2x$": bug = 1, safety = 0
583
+ * Prefix "$2y$": bug = 0, safety = 0
584
+ */
585
+ bug = (unsigned int)flags & 1;
586
+ safety = ((BF_word)flags & 2) << 15;
587
+
588
+ sign = diff = 0;
548
589
 
549
590
  for (i = 0; i < BF_N + 2; i++) {
550
- tmp = 0;
591
+ tmp[0] = tmp[1] = 0;
551
592
  for (j = 0; j < 4; j++) {
552
- tmp <<= 8;
553
- if (sign_extension_bug)
554
- tmp |= (BF_word_signed)(signed char)*ptr;
593
+ tmp[0] <<= 8;
594
+ tmp[0] |= (unsigned char)*ptr; /* correct */
595
+ tmp[1] <<= 8;
596
+ tmp[1] |= (BF_word_signed)(signed char)*ptr; /* bug */
597
+ /*
598
+ * Sign extension in the first char has no effect - nothing to overwrite yet,
599
+ * and those extra 24 bits will be fully shifted out of the 32-bit word. For
600
+ * chars 2, 3, 4 in each four-char block, we set bit 7 of "sign" if sign
601
+ * extension in tmp[1] occurs. Once this flag is set, it remains set.
602
+ */
603
+ if (j)
604
+ sign |= tmp[1] & 0x80;
605
+ if (!*ptr)
606
+ ptr = key;
555
607
  else
556
- tmp |= (unsigned char)*ptr;
557
-
558
- if (!*ptr) ptr = key; else ptr++;
608
+ ptr++;
559
609
  }
610
+ diff |= tmp[0] ^ tmp[1]; /* Non-zero on any differences */
560
611
 
561
- expanded[i] = tmp;
562
- initial[i] = BF_init_state.P[i] ^ tmp;
612
+ expanded[i] = tmp[bug];
613
+ initial[i] = BF_init_state.P[i] ^ tmp[bug];
563
614
  }
615
+
616
+ /*
617
+ * At this point, "diff" is zero iff the correct and buggy algorithms produced
618
+ * exactly the same result. If so and if "sign" is non-zero, which indicates
619
+ * that there was a non-benign sign extension, this means that we have a
620
+ * collision between the correctly computed hash for this password and a set of
621
+ * passwords that could be supplied to the buggy algorithm. Our safety measure
622
+ * is meant to protect from such many-buggy to one-correct collisions, by
623
+ * deviating from the correct algorithm in such cases. Let's check for this.
624
+ */
625
+ diff |= diff >> 16; /* still zero iff exact match */
626
+ diff &= 0xffff; /* ditto */
627
+ diff += 0xffff; /* bit 16 set iff "diff" was non-zero (on non-match) */
628
+ sign <<= 9; /* move the non-benign sign extension flag to bit 16 */
629
+ sign &= ~diff & safety; /* action needed? */
630
+
631
+ /*
632
+ * If we have determined that we need to deviate from the correct algorithm,
633
+ * flip bit 16 in initial expanded key. (The choice of 16 is arbitrary, but
634
+ * let's stick to it now. It came out of the approach we used above, and it's
635
+ * not any worse than any other choice we could make.)
636
+ *
637
+ * It is crucial that we don't do the same to the expanded key used in the main
638
+ * Eksblowfish loop. By doing it to only one of these two, we deviate from a
639
+ * state that could be directly specified by a password to the buggy algorithm
640
+ * (and to the fully correct one as well, but that's a side-effect).
641
+ */
642
+ initial[0] ^= sign;
564
643
  }
565
644
 
566
- static char *BF_crypt(__CONST char *key, __CONST char *setting,
645
+ static const unsigned char flags_by_subtype[26] =
646
+ {2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
647
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 0};
648
+
649
+ static char *BF_crypt(const char *key, const char *setting,
567
650
  char *output, int size,
568
651
  BF_word min)
569
652
  {
653
+ #if BF_ASM
654
+ extern void _BF_body_r(BF_ctx *ctx);
655
+ #endif
570
656
  struct {
571
657
  BF_ctx ctx;
572
658
  BF_key expanded_key;
@@ -588,7 +674,8 @@ static char *BF_crypt(__CONST char *key, __CONST char *setting,
588
674
 
589
675
  if (setting[0] != '$' ||
590
676
  setting[1] != '2' ||
591
- (setting[2] != 'a' && setting[2] != 'x') ||
677
+ setting[2] < 'a' || setting[2] > 'z' ||
678
+ !flags_by_subtype[(unsigned int)(unsigned char)setting[2] - 'a'] ||
592
679
  setting[3] != '$' ||
593
680
  setting[4] < '0' || setting[4] > '3' ||
594
681
  setting[5] < '0' || setting[5] > '9' ||
@@ -600,13 +687,13 @@ static char *BF_crypt(__CONST char *key, __CONST char *setting,
600
687
 
601
688
  count = (BF_word)1 << ((setting[4] - '0') * 10 + (setting[5] - '0'));
602
689
  if (count < min || BF_decode(data.binary.salt, &setting[7], 16)) {
603
- clean(data.binary.salt, sizeof(data.binary.salt));
604
690
  __set_errno(EINVAL);
605
691
  return NULL;
606
692
  }
607
693
  BF_swap(data.binary.salt, 4);
608
694
 
609
- BF_set_key(key, data.expanded_key, data.ctx.P, setting[2] == 'x');
695
+ BF_set_key(key, data.expanded_key, data.ctx.P,
696
+ flags_by_subtype[(unsigned int)(unsigned char)setting[2] - 'a']);
610
697
 
611
698
  memcpy(data.ctx.S, BF_init_state.S, sizeof(data.ctx.S));
612
699
 
@@ -636,51 +723,33 @@ static char *BF_crypt(__CONST char *key, __CONST char *setting,
636
723
  } while (ptr < &data.ctx.S[3][0xFF]);
637
724
 
638
725
  do {
639
- data.ctx.P[0] ^= data.expanded_key[0];
640
- data.ctx.P[1] ^= data.expanded_key[1];
641
- data.ctx.P[2] ^= data.expanded_key[2];
642
- data.ctx.P[3] ^= data.expanded_key[3];
643
- data.ctx.P[4] ^= data.expanded_key[4];
644
- data.ctx.P[5] ^= data.expanded_key[5];
645
- data.ctx.P[6] ^= data.expanded_key[6];
646
- data.ctx.P[7] ^= data.expanded_key[7];
647
- data.ctx.P[8] ^= data.expanded_key[8];
648
- data.ctx.P[9] ^= data.expanded_key[9];
649
- data.ctx.P[10] ^= data.expanded_key[10];
650
- data.ctx.P[11] ^= data.expanded_key[11];
651
- data.ctx.P[12] ^= data.expanded_key[12];
652
- data.ctx.P[13] ^= data.expanded_key[13];
653
- data.ctx.P[14] ^= data.expanded_key[14];
654
- data.ctx.P[15] ^= data.expanded_key[15];
655
- data.ctx.P[16] ^= data.expanded_key[16];
656
- data.ctx.P[17] ^= data.expanded_key[17];
657
-
658
- BF_body();
659
-
660
- tmp1 = data.binary.salt[0];
661
- tmp2 = data.binary.salt[1];
662
- tmp3 = data.binary.salt[2];
663
- tmp4 = data.binary.salt[3];
664
- data.ctx.P[0] ^= tmp1;
665
- data.ctx.P[1] ^= tmp2;
666
- data.ctx.P[2] ^= tmp3;
667
- data.ctx.P[3] ^= tmp4;
668
- data.ctx.P[4] ^= tmp1;
669
- data.ctx.P[5] ^= tmp2;
670
- data.ctx.P[6] ^= tmp3;
671
- data.ctx.P[7] ^= tmp4;
672
- data.ctx.P[8] ^= tmp1;
673
- data.ctx.P[9] ^= tmp2;
674
- data.ctx.P[10] ^= tmp3;
675
- data.ctx.P[11] ^= tmp4;
676
- data.ctx.P[12] ^= tmp1;
677
- data.ctx.P[13] ^= tmp2;
678
- data.ctx.P[14] ^= tmp3;
679
- data.ctx.P[15] ^= tmp4;
680
- data.ctx.P[16] ^= tmp1;
681
- data.ctx.P[17] ^= tmp2;
682
-
683
- BF_body();
726
+ int done;
727
+
728
+ for (i = 0; i < BF_N + 2; i += 2) {
729
+ data.ctx.P[i] ^= data.expanded_key[i];
730
+ data.ctx.P[i + 1] ^= data.expanded_key[i + 1];
731
+ }
732
+
733
+ done = 0;
734
+ do {
735
+ BF_body();
736
+ if (done)
737
+ break;
738
+ done = 1;
739
+
740
+ tmp1 = data.binary.salt[0];
741
+ tmp2 = data.binary.salt[1];
742
+ tmp3 = data.binary.salt[2];
743
+ tmp4 = data.binary.salt[3];
744
+ for (i = 0; i < BF_N; i += 4) {
745
+ data.ctx.P[i] ^= tmp1;
746
+ data.ctx.P[i + 1] ^= tmp2;
747
+ data.ctx.P[i + 2] ^= tmp3;
748
+ data.ctx.P[i + 3] ^= tmp4;
749
+ }
750
+ data.ctx.P[16] ^= tmp1;
751
+ data.ctx.P[17] ^= tmp2;
752
+ } while (1);
684
753
  } while (--count);
685
754
 
686
755
  for (i = 0; i < 6; i += 2) {
@@ -706,64 +775,116 @@ static char *BF_crypt(__CONST char *key, __CONST char *setting,
706
775
  BF_encode(&output[7 + 22], data.binary.output, 23);
707
776
  output[7 + 22 + 31] = '\0';
708
777
 
709
- #ifndef BF_SELF_TEST
710
- /* Overwrite the most obvious sensitive data we have on the stack. Note
711
- * that this does not guarantee there's no sensitive data left on the
712
- * stack and/or in registers; I'm not aware of portable code that does. */
713
- clean(&data, sizeof(data));
714
- #endif
715
-
716
778
  return output;
717
779
  }
718
780
 
719
- char *_crypt_blowfish_rn(__CONST char *key, __CONST char *setting,
781
+ int _crypt_output_magic(const char *setting, char *output, int size)
782
+ {
783
+ if (size < 3)
784
+ return -1;
785
+
786
+ output[0] = '*';
787
+ output[1] = '0';
788
+ output[2] = '\0';
789
+
790
+ if (setting[0] == '*' && setting[1] == '0')
791
+ output[1] = '1';
792
+
793
+ return 0;
794
+ }
795
+
796
+ /*
797
+ * Please preserve the runtime self-test. It serves two purposes at once:
798
+ *
799
+ * 1. We really can't afford the risk of producing incompatible hashes e.g.
800
+ * when there's something like gcc bug 26587 again, whereas an application or
801
+ * library integrating this code might not also integrate our external tests or
802
+ * it might not run them after every build. Even if it does, the miscompile
803
+ * might only occur on the production build, but not on a testing build (such
804
+ * as because of different optimization settings). It is painful to recover
805
+ * from incorrectly-computed hashes - merely fixing whatever broke is not
806
+ * enough. Thus, a proactive measure like this self-test is needed.
807
+ *
808
+ * 2. We don't want to leave sensitive data from our actual password hash
809
+ * computation on the stack or in registers. Previous revisions of the code
810
+ * would do explicit cleanups, but simply running the self-test after hash
811
+ * computation is more reliable.
812
+ *
813
+ * The performance cost of this quick self-test is around 0.6% at the "$2a$08"
814
+ * setting.
815
+ */
816
+ char *_crypt_blowfish_rn(const char *key, const char *setting,
720
817
  char *output, int size)
721
818
  {
722
- #ifdef BF_SELF_TEST
723
- __CONST char *test_key = "8b \xd0\xc1\xd2\xcf\xcc\xd8";
724
- __CONST char *test_2a =
725
- "$2a$00$abcdefghijklmnopqrstuui1D709vfamulimlGcq0qq3UvuUasvEa"
726
- "\0"
727
- "canary";
728
- __CONST char *test_2x =
729
- "$2x$00$abcdefghijklmnopqrstuuVUrPmXD6q/nVSSp7pNDhCR9071IfIRe"
730
- "\0"
731
- "canary";
732
- __CONST char *test_hash, *p;
733
- int ok;
734
- char buf[7 + 22 + 31 + 1 + 6 + 1];
735
-
736
- output = BF_crypt(key, setting, output, size, 16);
737
-
738
- /* Do a quick self-test. This also happens to overwrite BF_crypt()'s data. */
739
- test_hash = (setting[2] == 'x') ? test_2x : test_2a;
740
- memcpy(buf, test_hash, sizeof(buf));
741
- memset(buf, -1, sizeof(buf) - (6 + 1)); /* keep "canary" only */
742
- p = BF_crypt(test_key, test_hash, buf, sizeof(buf) - 6, 1);
743
-
744
- ok = (p == buf && !memcmp(p, test_hash, sizeof(buf)));
745
-
746
- /* This could reveal what hash type we were using last. Unfortunately, we
747
- * can't reliably clean the test_hash pointer. */
748
- clean(&buf, sizeof(buf));
819
+ const char *test_key = "8b \xd0\xc1\xd2\xcf\xcc\xd8";
820
+ const char *test_setting = "$2a$00$abcdefghijklmnopqrstuu";
821
+ static const char * const test_hashes[2] =
822
+ {"i1D709vfamulimlGcq0qq3UvuUasvEa\0\x55", /* 'a', 'b', 'y' */
823
+ "VUrPmXD6q/nVSSp7pNDhCR9071IfIRe\0\x55"}; /* 'x' */
824
+ const char *test_hash = test_hashes[0];
825
+ char *retval;
826
+ const char *p;
827
+ int save_errno, ok;
828
+ struct {
829
+ char s[7 + 22 + 1];
830
+ char o[7 + 22 + 31 + 1 + 1 + 1];
831
+ } buf;
832
+
833
+ /* Hash the supplied password */
834
+ _crypt_output_magic(setting, output, size);
835
+ retval = BF_crypt(key, setting, output, size, 16);
836
+ save_errno = errno;
837
+
838
+ /*
839
+ * Do a quick self-test. It is important that we make both calls to BF_crypt()
840
+ * from the same scope such that they likely use the same stack locations,
841
+ * which makes the second call overwrite the first call's sensitive data on the
842
+ * stack and makes it more likely that any alignment related issues would be
843
+ * detected by the self-test.
844
+ */
845
+ memcpy(buf.s, test_setting, sizeof(buf.s));
846
+ if (retval) {
847
+ unsigned int flags = flags_by_subtype[
848
+ (unsigned int)(unsigned char)setting[2] - 'a'];
849
+ test_hash = test_hashes[flags & 1];
850
+ buf.s[2] = setting[2];
851
+ }
852
+ memset(buf.o, 0x55, sizeof(buf.o));
853
+ buf.o[sizeof(buf.o) - 1] = 0;
854
+ p = BF_crypt(test_key, buf.s, buf.o, sizeof(buf.o) - (1 + 1), 1);
855
+
856
+ ok = (p == buf.o &&
857
+ !memcmp(p, buf.s, 7 + 22) &&
858
+ !memcmp(p + (7 + 22), test_hash, 31 + 1 + 1 + 1));
859
+
860
+ {
861
+ const char *k = "\xff\xa3" "34" "\xff\xff\xff\xa3" "345";
862
+ BF_key ae, ai, ye, yi;
863
+ BF_set_key(k, ae, ai, 2); /* $2a$ */
864
+ BF_set_key(k, ye, yi, 4); /* $2y$ */
865
+ ai[0] ^= 0x10000; /* undo the safety (for comparison) */
866
+ ok = ok && ai[0] == 0xdb9c59bc && ye[17] == 0x33343500 &&
867
+ !memcmp(ae, ye, sizeof(ae)) &&
868
+ !memcmp(ai, yi, sizeof(ai));
869
+ }
749
870
 
871
+ __set_errno(save_errno);
750
872
  if (ok)
751
- return output;
873
+ return retval;
752
874
 
753
875
  /* Should not happen */
876
+ _crypt_output_magic(setting, output, size);
754
877
  __set_errno(EINVAL); /* pretend we don't support this hash type */
755
878
  return NULL;
756
- #else
757
- #warning Self-test is disabled, please enable
758
- return BF_crypt(key, setting, output, size, 16);
759
- #endif
760
879
  }
761
880
 
762
- char *_crypt_gensalt_blowfish_rn(unsigned long count,
763
- __CONST char *input, int size, char *output, int output_size)
881
+ char *_crypt_gensalt_blowfish_rn(const char *prefix, unsigned long count,
882
+ const char *input, int size, char *output, int output_size)
764
883
  {
765
884
  if (size < 16 || output_size < 7 + 22 + 1 ||
766
- (count && (count < 4 || count > 31))) {
885
+ (count && (count < 4 || count > 31)) ||
886
+ prefix[0] != '$' || prefix[1] != '2' ||
887
+ (prefix[2] != 'a' && prefix[2] != 'b' && prefix[2] != 'y')) {
767
888
  if (output_size > 0) output[0] = '\0';
768
889
  __set_errno((output_size < 7 + 22 + 1) ? ERANGE : EINVAL);
769
890
  return NULL;
@@ -773,13 +894,13 @@ char *_crypt_gensalt_blowfish_rn(unsigned long count,
773
894
 
774
895
  output[0] = '$';
775
896
  output[1] = '2';
776
- output[2] = 'a';
897
+ output[2] = prefix[2];
777
898
  output[3] = '$';
778
899
  output[4] = '0' + count / 10;
779
900
  output[5] = '0' + count % 10;
780
901
  output[6] = '$';
781
902
 
782
- BF_encode(&output[7], (BF_word *)input, 16);
903
+ BF_encode(&output[7], (const BF_word *)input, 16);
783
904
  output[7 + 22] = '\0';
784
905
 
785
906
  return output;