PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
px-crypt.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PX_MAX_CRYPT   128
 
#define PX_MAX_SALT_LEN   128
 
#define PX_XDES_ROUNDS   (29 * 25)
 
#define PX_BF_ROUNDS   6
 
#define PX_SHACRYPT_SALT_MAX_LEN   16
 
#define PX_SHACRYPT_DIGEST_MAX_LEN   64
 
#define PX_SHACRYPT_SALT_BUF_LEN   (3 + 7 + 10 + PX_SHACRYPT_SALT_MAX_LEN + 1)
 
#define PX_SHACRYPT_BUF_LEN   (PX_SHACRYPT_SALT_BUF_LEN + 86 + 1)
 
#define PX_SHACRYPT_ROUNDS_DEFAULT   5000
 
#define PX_SHACRYPT_ROUNDS_MIN   1000
 
#define PX_SHACRYPT_ROUNDS_MAX   999999999
 

Functions

char * px_crypt (const char *psw, const char *salt, char *buf, unsigned len)
 
int px_gen_salt (const char *salt_type, char *buf, int rounds)
 
char * _crypt_gensalt_traditional_rn (unsigned long count, const char *input, int size, char *output, int output_size)
 
char * _crypt_gensalt_extended_rn (unsigned long count, const char *input, int size, char *output, int output_size)
 
char * _crypt_gensalt_md5_rn (unsigned long count, const char *input, int size, char *output, int output_size)
 
char * _crypt_gensalt_blowfish_rn (unsigned long count, const char *input, int size, char *output, int output_size)
 
char * _crypt_gensalt_sha256_rn (unsigned long count, const char *input, int size, char *output, int output_size)
 
char * _crypt_gensalt_sha512_rn (unsigned long count, const char *input, int size, char *output, int output_size)
 
char * _crypt_blowfish_rn (const char *key, const char *setting, char *output, int size)
 
char * px_crypt_des (const char *key, const char *setting)
 
char * px_crypt_md5 (const char *pw, const char *salt, char *passwd, unsigned dstlen)
 
char * px_crypt_shacrypt (const char *pw, const char *salt, char *passwd, unsigned dstlen)
 

Macro Definition Documentation

◆ PX_BF_ROUNDS

#define PX_BF_ROUNDS   6

Definition at line 46 of file px-crypt.h.

◆ PX_MAX_CRYPT

#define PX_MAX_CRYPT   128

Definition at line 36 of file px-crypt.h.

◆ PX_MAX_SALT_LEN

#define PX_MAX_SALT_LEN   128

Definition at line 39 of file px-crypt.h.

◆ PX_SHACRYPT_BUF_LEN

#define PX_SHACRYPT_BUF_LEN   (PX_SHACRYPT_SALT_BUF_LEN + 86 + 1)

Definition at line 61 of file px-crypt.h.

◆ PX_SHACRYPT_DIGEST_MAX_LEN

#define PX_SHACRYPT_DIGEST_MAX_LEN   64

Definition at line 52 of file px-crypt.h.

◆ PX_SHACRYPT_ROUNDS_DEFAULT

#define PX_SHACRYPT_ROUNDS_DEFAULT   5000

Definition at line 64 of file px-crypt.h.

◆ PX_SHACRYPT_ROUNDS_MAX

#define PX_SHACRYPT_ROUNDS_MAX   999999999

Definition at line 70 of file px-crypt.h.

◆ PX_SHACRYPT_ROUNDS_MIN

#define PX_SHACRYPT_ROUNDS_MIN   1000

Definition at line 67 of file px-crypt.h.

◆ PX_SHACRYPT_SALT_BUF_LEN

#define PX_SHACRYPT_SALT_BUF_LEN   (3 + 7 + 10 + PX_SHACRYPT_SALT_MAX_LEN + 1)

Definition at line 55 of file px-crypt.h.

◆ PX_SHACRYPT_SALT_MAX_LEN

#define PX_SHACRYPT_SALT_MAX_LEN   16

Definition at line 49 of file px-crypt.h.

◆ PX_XDES_ROUNDS

#define PX_XDES_ROUNDS   (29 * 25)

Definition at line 43 of file px-crypt.h.

Function Documentation

◆ _crypt_blowfish_rn()

char * _crypt_blowfish_rn ( const char *  key,
const char *  setting,
char *  output,
int  size 
)

Definition at line 582 of file crypt-blowfish.c.

584{
585 struct
586 {
587 BF_ctx ctx;
588 BF_key expanded_key;
589 union
590 {
591 BF_word salt[4];
592 BF_word output[6];
593 } binary;
594 } data;
595 BF_word L,
596 R;
597 BF_word tmp1,
598 tmp2,
599 tmp3,
600 tmp4;
601 BF_word *ptr;
602 BF_word count;
603 int i;
604
605 if (size < 7 + 22 + 31 + 1)
606 return NULL;
607
608 /*
609 * Blowfish salt value must be formatted as follows: "$2a$" or "$2x$", a
610 * two digit cost parameter, "$", and 22 digits from the alphabet
611 * "./0-9A-Za-z". -- from the PHP crypt docs. Apparently we enforce a few
612 * more restrictions on the count in the salt as well.
613 */
614 if (strlen(setting) < 29)
616 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
617 errmsg("invalid salt")));
618
619 if (setting[0] != '$' ||
620 setting[1] != '2' ||
621 (setting[2] != 'a' && setting[2] != 'x') ||
622 setting[3] != '$' ||
623 setting[4] < '0' || setting[4] > '3' ||
624 setting[5] < '0' || setting[5] > '9' ||
625 (setting[4] == '3' && setting[5] > '1') ||
626 setting[6] != '$')
627 {
629 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
630 errmsg("invalid salt")));
631 }
632
633 count = (BF_word) 1 << ((setting[4] - '0') * 10 + (setting[5] - '0'));
634 if (count < 16 || BF_decode(data.binary.salt, &setting[7], 16))
635 {
636 px_memset(data.binary.salt, 0, sizeof(data.binary.salt));
638 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
639 errmsg("invalid salt")));
640 }
641 BF_swap(data.binary.salt, 4);
642
643 BF_set_key(key, data.expanded_key, data.ctx.P, setting[2] == 'x');
644
645 memcpy(data.ctx.S, BF_init_state.S, sizeof(data.ctx.S));
646
647 L = R = 0;
648 for (i = 0; i < BF_N + 2; i += 2)
649 {
650 L ^= data.binary.salt[i & 2];
651 R ^= data.binary.salt[(i & 2) + 1];
653 data.ctx.P[i] = L;
654 data.ctx.P[i + 1] = R;
655 }
656
657 ptr = data.ctx.S[0];
658 do
659 {
660 ptr += 4;
661 L ^= data.binary.salt[(BF_N + 2) & 3];
662 R ^= data.binary.salt[(BF_N + 3) & 3];
664 *(ptr - 4) = L;
665 *(ptr - 3) = R;
666
667 L ^= data.binary.salt[(BF_N + 4) & 3];
668 R ^= data.binary.salt[(BF_N + 5) & 3];
670 *(ptr - 2) = L;
671 *(ptr - 1) = R;
672 } while (ptr < &data.ctx.S[3][0xFF]);
673
674 do
675 {
677
678 data.ctx.P[0] ^= data.expanded_key[0];
679 data.ctx.P[1] ^= data.expanded_key[1];
680 data.ctx.P[2] ^= data.expanded_key[2];
681 data.ctx.P[3] ^= data.expanded_key[3];
682 data.ctx.P[4] ^= data.expanded_key[4];
683 data.ctx.P[5] ^= data.expanded_key[5];
684 data.ctx.P[6] ^= data.expanded_key[6];
685 data.ctx.P[7] ^= data.expanded_key[7];
686 data.ctx.P[8] ^= data.expanded_key[8];
687 data.ctx.P[9] ^= data.expanded_key[9];
688 data.ctx.P[10] ^= data.expanded_key[10];
689 data.ctx.P[11] ^= data.expanded_key[11];
690 data.ctx.P[12] ^= data.expanded_key[12];
691 data.ctx.P[13] ^= data.expanded_key[13];
692 data.ctx.P[14] ^= data.expanded_key[14];
693 data.ctx.P[15] ^= data.expanded_key[15];
694 data.ctx.P[16] ^= data.expanded_key[16];
695 data.ctx.P[17] ^= data.expanded_key[17];
696
697 BF_body();
698
699 tmp1 = data.binary.salt[0];
700 tmp2 = data.binary.salt[1];
701 tmp3 = data.binary.salt[2];
702 tmp4 = data.binary.salt[3];
703 data.ctx.P[0] ^= tmp1;
704 data.ctx.P[1] ^= tmp2;
705 data.ctx.P[2] ^= tmp3;
706 data.ctx.P[3] ^= tmp4;
707 data.ctx.P[4] ^= tmp1;
708 data.ctx.P[5] ^= tmp2;
709 data.ctx.P[6] ^= tmp3;
710 data.ctx.P[7] ^= tmp4;
711 data.ctx.P[8] ^= tmp1;
712 data.ctx.P[9] ^= tmp2;
713 data.ctx.P[10] ^= tmp3;
714 data.ctx.P[11] ^= tmp4;
715 data.ctx.P[12] ^= tmp1;
716 data.ctx.P[13] ^= tmp2;
717 data.ctx.P[14] ^= tmp3;
718 data.ctx.P[15] ^= tmp4;
719 data.ctx.P[16] ^= tmp1;
720 data.ctx.P[17] ^= tmp2;
721
722 BF_body();
723 } while (--count);
724
725 for (i = 0; i < 6; i += 2)
726 {
727 L = BF_magic_w[i];
728 R = BF_magic_w[i + 1];
729
730 count = 64;
731 do
732 {
734 } while (--count);
735
736 data.binary.output[i] = L;
737 data.binary.output[i + 1] = R;
738 }
739
740 memcpy(output, setting, 7 + 22 - 1);
741 output[7 + 22 - 1] = BF_itoa64[(int)
742 BF_atoi64[(int) setting[7 + 22 - 1] - 0x20] & 0x30];
743
744/* This has to be bug-compatible with the original implementation, so
745 * only encode 23 of the 24 bytes. :-) */
746 BF_swap(data.binary.output, 6);
747 BF_encode(&output[7 + 22], data.binary.output, 23);
748 output[7 + 22 + 31] = '\0';
749
750/* Overwrite the most obvious sensitive data we have on the stack. Note
751 * that this does not guarantee there's no sensitive data left on the
752 * stack and/or in registers; I'm not aware of portable code that does. */
753 px_memset(&data, 0, sizeof(data));
754
755 return output;
756}
static int BF_decode(BF_word *dst, const char *src, int size)
static void BF_set_key(const char *key, BF_key expanded, BF_key initial, int sign_extension_bug)
static void BF_swap(BF_word *x, int count)
#define BF_ENCRYPT
#define BF_N
static BF_word BF_magic_w[6]
static unsigned char BF_itoa64[64+1]
static unsigned char BF_atoi64[0x60]
static BF_ctx BF_init_state
static void BF_encode(char *dst, const BF_word *src, int size)
#define BF_body()
unsigned int BF_word
BF_word BF_key[BF_N+2]
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
FILE * output
int i
Definition: isn.c:77
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
const void * data
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:123
#define R(b, x)
Definition: sha2.c:132
BF_word S[4][0x100]

References BF_atoi64, BF_body, BF_decode(), BF_encode(), BF_ENCRYPT, BF_init_state, BF_itoa64, BF_magic_w, BF_N, BF_set_key(), BF_swap(), CHECK_FOR_INTERRUPTS, data, ereport, errcode(), errmsg(), ERROR, i, sort-test::key, output, px_memset(), R, and BF_ctx::S.

Referenced by run_crypt_bf().

◆ _crypt_gensalt_blowfish_rn()

char * _crypt_gensalt_blowfish_rn ( unsigned long  count,
const char *  input,
int  size,
char *  output,
int  output_size 
)

Definition at line 161 of file crypt-gensalt.c.

163{
164 if (size < 16 || output_size < 7 + 22 + 1 ||
165 (count && (count < 4 || count > 31)))
166 {
167 if (output_size > 0)
168 output[0] = '\0';
169 return NULL;
170 }
171
172 if (!count)
173 count = 5;
174
175 output[0] = '$';
176 output[1] = '2';
177 output[2] = 'a';
178 output[3] = '$';
179 output[4] = '0' + count / 10;
180 output[5] = '0' + count % 10;
181 output[6] = '$';
182
183 BF_encode(&output[7], (const BF_word *) input, 16);
184 output[7 + 22] = '\0';
185
186 return output;
187}
static void BF_encode(char *dst, const BF_word *src, int size)
FILE * input

References BF_encode(), input, and output.

◆ _crypt_gensalt_extended_rn()

char * _crypt_gensalt_extended_rn ( unsigned long  count,
const char *  input,
int  size,
char *  output,
int  output_size 
)

Definition at line 43 of file crypt-gensalt.c.

45{
46 unsigned long value;
47
48/* Even iteration counts make it easier to detect weak DES keys from a look
49 * at the hash, so they should be avoided */
50 if (size < 3 || output_size < 1 + 4 + 4 + 1 ||
51 (count && (count > 0xffffff || !(count & 1))))
52 {
53 if (output_size > 0)
54 output[0] = '\0';
55 return NULL;
56 }
57
58 if (!count)
59 count = 725;
60
61 output[0] = '_';
62 output[1] = _crypt_itoa64[count & 0x3f];
63 output[2] = _crypt_itoa64[(count >> 6) & 0x3f];
64 output[3] = _crypt_itoa64[(count >> 12) & 0x3f];
65 output[4] = _crypt_itoa64[(count >> 18) & 0x3f];
66 value = (unsigned long) (unsigned char) input[0] |
67 ((unsigned long) (unsigned char) input[1] << 8) |
68 ((unsigned long) (unsigned char) input[2] << 16);
69 output[5] = _crypt_itoa64[value & 0x3f];
70 output[6] = _crypt_itoa64[(value >> 6) & 0x3f];
71 output[7] = _crypt_itoa64[(value >> 12) & 0x3f];
72 output[8] = _crypt_itoa64[(value >> 18) & 0x3f];
73 output[9] = '\0';
74
75 return output;
76}
static unsigned char _crypt_itoa64[64+1]
Definition: crypt-gensalt.c:21
static struct @165 value

References _crypt_itoa64, input, output, and value.

◆ _crypt_gensalt_md5_rn()

char * _crypt_gensalt_md5_rn ( unsigned long  count,
const char *  input,
int  size,
char *  output,
int  output_size 
)

Definition at line 79 of file crypt-gensalt.c.

81{
82 unsigned long value;
83
84 if (size < 3 || output_size < 3 + 4 + 1 || (count && count != 1000))
85 {
86 if (output_size > 0)
87 output[0] = '\0';
88 return NULL;
89 }
90
91 output[0] = '$';
92 output[1] = '1';
93 output[2] = '$';
94 value = (unsigned long) (unsigned char) input[0] |
95 ((unsigned long) (unsigned char) input[1] << 8) |
96 ((unsigned long) (unsigned char) input[2] << 16);
97 output[3] = _crypt_itoa64[value & 0x3f];
98 output[4] = _crypt_itoa64[(value >> 6) & 0x3f];
99 output[5] = _crypt_itoa64[(value >> 12) & 0x3f];
100 output[6] = _crypt_itoa64[(value >> 18) & 0x3f];
101 output[7] = '\0';
102
103 if (size >= 6 && output_size >= 3 + 4 + 4 + 1)
104 {
105 value = (unsigned long) (unsigned char) input[3] |
106 ((unsigned long) (unsigned char) input[4] << 8) |
107 ((unsigned long) (unsigned char) input[5] << 16);
108 output[7] = _crypt_itoa64[value & 0x3f];
109 output[8] = _crypt_itoa64[(value >> 6) & 0x3f];
110 output[9] = _crypt_itoa64[(value >> 12) & 0x3f];
111 output[10] = _crypt_itoa64[(value >> 18) & 0x3f];
112 output[11] = '\0';
113 }
114
115 return output;
116}

References _crypt_itoa64, input, output, and value.

◆ _crypt_gensalt_sha256_rn()

char * _crypt_gensalt_sha256_rn ( unsigned long  count,
const char *  input,
int  size,
char *  output,
int  output_size 
)

Definition at line 258 of file crypt-gensalt.c.

261{
262 memset(output, 0, output_size);
263 /* set magic byte for sha256crypt */
264 output[0] = '$';
265 output[1] = '5';
266 output[2] = '$';
267
268 return _crypt_gensalt_sha(count, input, size, output, output_size);
269}
static char * _crypt_gensalt_sha(unsigned long count, const char *input, int size, char *output, int output_size)

References _crypt_gensalt_sha(), input, and output.

◆ _crypt_gensalt_sha512_rn()

char * _crypt_gensalt_sha512_rn ( unsigned long  count,
const char *  input,
int  size,
char *  output,
int  output_size 
)

Definition at line 243 of file crypt-gensalt.c.

246{
247 memset(output, 0, output_size);
248 /* set magic byte for sha512crypt */
249 output[0] = '$';
250 output[1] = '6';
251 output[2] = '$';
252
253 return _crypt_gensalt_sha(count, input, size, output, output_size);
254}

References _crypt_gensalt_sha(), input, and output.

◆ _crypt_gensalt_traditional_rn()

char * _crypt_gensalt_traditional_rn ( unsigned long  count,
const char *  input,
int  size,
char *  output,
int  output_size 
)

Definition at line 25 of file crypt-gensalt.c.

27{
28 if (size < 2 || output_size < 2 + 1 || (count && count != 25))
29 {
30 if (output_size > 0)
31 output[0] = '\0';
32 return NULL;
33 }
34
35 output[0] = _crypt_itoa64[(unsigned int) input[0] & 0x3f];
36 output[1] = _crypt_itoa64[(unsigned int) input[1] & 0x3f];
37 output[2] = '\0';
38
39 return output;
40}

References _crypt_itoa64, input, and output.

◆ px_crypt()

char * px_crypt ( const char *  psw,
const char *  salt,
char *  buf,
unsigned  len 
)

Definition at line 102 of file px-crypt.c.

103{
104 const struct px_crypt_algo *c;
105
107
108 for (c = px_crypt_list; c->id; c++)
109 {
110 if (!c->id_len)
111 break;
112 if (strncmp(salt, c->id, c->id_len) == 0)
113 break;
114 }
115
116 if (c->crypt == NULL)
117 return NULL;
118
119 return c->crypt(psw, salt, buf, len);
120}
void CheckBuiltinCryptoMode(void)
Definition: openssl.c:874
const void size_t len
static char * buf
Definition: pg_test_fsync.c:72
char * c
static const struct px_crypt_algo px_crypt_list[]
Definition: px-crypt.c:89
char * id
Definition: px-crypt.c:82

References buf, CheckBuiltinCryptoMode(), px_crypt_algo::id, len, and px_crypt_list.

Referenced by pg_crypt().

◆ px_crypt_des()

char * px_crypt_des ( const char *  key,
const char *  setting 
)

Definition at line 651 of file crypt-des.c.

652{
653 int i;
654 uint32 count,
655 salt,
656 l,
657 r0,
658 r1,
659 keybuf[2];
660 char *p;
661 uint8 *q;
662 static char output[21];
663
664 if (!des_initialised)
665 des_init();
666
667
668 /*
669 * Copy the key, shifting each character up by one bit and padding with
670 * zeros.
671 */
672 q = (uint8 *) keybuf;
673 while (q - (uint8 *) keybuf - 8)
674 {
675 *q++ = *key << 1;
676 if (*key != '\0')
677 key++;
678 }
679 if (des_setkey((char *) keybuf))
680 return NULL;
681
682#ifndef DISABLE_XDES
683 if (*setting == _PASSWORD_EFMT1)
684 {
685 /*
686 * "new"-style: setting must be a 9-character (underscore, then 4
687 * bytes of count, then 4 bytes of salt) string. See CRYPT(3) under
688 * the "Extended crypt" heading for further details.
689 *
690 * Unlimited characters of the input key are used. This is known as
691 * the "Extended crypt" DES method.
692 *
693 */
694 if (strlen(setting) < 9)
696 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
697 errmsg("invalid salt")));
698
699 for (i = 1, count = 0L; i < 5; i++)
700 count |= ascii_to_bin(setting[i]) << (i - 1) * 6;
701
702 for (i = 5, salt = 0L; i < 9; i++)
703 salt |= ascii_to_bin(setting[i]) << (i - 5) * 6;
704
705 while (*key)
706 {
707 /*
708 * Encrypt the key with itself.
709 */
710 if (des_cipher((char *) keybuf, (char *) keybuf, 0L, 1))
711 return NULL;
712
713 /*
714 * And XOR with the next 8 characters of the key.
715 */
716 q = (uint8 *) keybuf;
717 while (q - (uint8 *) keybuf - 8 && *key)
718 *q++ ^= *key++ << 1;
719
720 if (des_setkey((char *) keybuf))
721 return NULL;
722 }
723 strlcpy(output, setting, 10);
724
725 /*
726 * Double check that we weren't given a short setting. If we were, the
727 * above code will probably have created weird values for count and
728 * salt, but we don't really care. Just make sure the output string
729 * doesn't have an extra NUL in it.
730 */
731 p = output + strlen(output);
732 }
733 else
734#endif /* !DISABLE_XDES */
735 {
736 /*
737 * "old"-style: setting - 2 bytes of salt key - only up to the first 8
738 * characters of the input key are used.
739 */
740 count = 25;
741
742 if (strlen(setting) < 2)
744 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
745 errmsg("invalid salt")));
746
747 salt = (ascii_to_bin(setting[1]) << 6)
748 | ascii_to_bin(setting[0]);
749
750 output[0] = setting[0];
751
752 /*
753 * If the encrypted password that the salt was extracted from is only
754 * 1 character long, the salt will be corrupted. We need to ensure
755 * that the output string doesn't have an extra NUL in it!
756 */
757 output[1] = setting[1] ? setting[1] : output[0];
758
759 p = output + 2;
760 }
761 setup_salt(salt);
762
763 /*
764 * Do it.
765 */
766 if (do_des(0L, 0L, &r0, &r1, count))
767 return NULL;
768
769 /*
770 * Now encode the result...
771 */
772 l = (r0 >> 8);
773 *p++ = _crypt_a64[(l >> 18) & 0x3f];
774 *p++ = _crypt_a64[(l >> 12) & 0x3f];
775 *p++ = _crypt_a64[(l >> 6) & 0x3f];
776 *p++ = _crypt_a64[l & 0x3f];
777
778 l = (r0 << 16) | ((r1 >> 16) & 0xffff);
779 *p++ = _crypt_a64[(l >> 18) & 0x3f];
780 *p++ = _crypt_a64[(l >> 12) & 0x3f];
781 *p++ = _crypt_a64[(l >> 6) & 0x3f];
782 *p++ = _crypt_a64[l & 0x3f];
783
784 l = r1 << 2;
785 *p++ = _crypt_a64[(l >> 12) & 0x3f];
786 *p++ = _crypt_a64[(l >> 6) & 0x3f];
787 *p++ = _crypt_a64[l & 0x3f];
788 *p = 0;
789
790 return output;
791}
uint8_t uint8
Definition: c.h:500
uint32_t uint32
Definition: c.h:502
static int des_cipher(const char *in, char *out, long salt, int count)
Definition: crypt-des.c:617
static int ascii_to_bin(char ch)
Definition: crypt-des.c:203
static void des_init(void)
Definition: crypt-des.c:221
static void setup_salt(long salt)
Definition: crypt-des.c:373
static int des_initialised
Definition: crypt-des.c:188
#define _PASSWORD_EFMT1
Definition: crypt-des.c:69
static int do_des(uint32 l_in, uint32 r_in, uint32 *l_out, uint32 *r_out, int count)
Definition: crypt-des.c:483
static int des_setkey(const char *key)
Definition: crypt-des.c:396
static const char _crypt_a64[]
Definition: crypt-des.c:71
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45

References _crypt_a64, _PASSWORD_EFMT1, ascii_to_bin(), des_cipher(), des_init(), des_initialised, des_setkey(), do_des(), ereport, errcode(), errmsg(), ERROR, i, sort-test::key, output, setup_salt(), and strlcpy().

Referenced by run_crypt_des().

◆ px_crypt_md5()

char * px_crypt_md5 ( const char *  pw,
const char *  salt,
char *  passwd,
unsigned  dstlen 
)

Definition at line 34 of file crypt-md5.c.

35{
36 static const char *magic = "$1$"; /* This string is magic for this
37 * algorithm. Having it this way, we
38 * can get better later on */
39 char *p;
40 const char *sp,
41 *ep;
42 unsigned char final[MD5_SIZE];
43 int sl,
44 pl,
45 i;
46 PX_MD *ctx,
47 *ctx1;
48 int err;
49 unsigned long l;
50
51 if (!passwd || dstlen < 120)
52 return NULL;
53
54 /* Refine the Salt first */
55 sp = salt;
56
57 /* If it starts with the magic string, then skip that */
58 if (strncmp(sp, magic, strlen(magic)) == 0)
59 sp += strlen(magic);
60
61 /* It stops at the first '$', max 8 chars */
62 for (ep = sp; *ep && *ep != '$' && ep < (sp + 8); ep++)
63 continue;
64
65 /* get the length of the true salt */
66 sl = ep - sp;
67
68 /* we need two PX_MD objects */
69 err = px_find_digest("md5", &ctx);
70 if (err)
71 return NULL;
72 err = px_find_digest("md5", &ctx1);
73 if (err)
74 {
75 /* this path is possible under low-memory circumstances */
76 px_md_free(ctx);
77 return NULL;
78 }
79
80 /* The password first, since that is what is most unknown */
81 px_md_update(ctx, (const uint8 *) pw, strlen(pw));
82
83 /* Then our magic string */
84 px_md_update(ctx, (const uint8 *) magic, strlen(magic));
85
86 /* Then the raw salt */
87 px_md_update(ctx, (const uint8 *) sp, sl);
88
89 /* Then just as many characters of the MD5(pw,salt,pw) */
90 px_md_update(ctx1, (const uint8 *) pw, strlen(pw));
91 px_md_update(ctx1, (const uint8 *) sp, sl);
92 px_md_update(ctx1, (const uint8 *) pw, strlen(pw));
93 px_md_finish(ctx1, final);
94 for (pl = strlen(pw); pl > 0; pl -= MD5_SIZE)
95 px_md_update(ctx, final, pl > MD5_SIZE ? MD5_SIZE : pl);
96
97 /* Don't leave anything around in vm they could use. */
98 px_memset(final, 0, sizeof final);
99
100 /* Then something really weird... */
101 for (i = strlen(pw); i; i >>= 1)
102 if (i & 1)
103 px_md_update(ctx, final, 1);
104 else
105 px_md_update(ctx, (const uint8 *) pw, 1);
106
107 /* Now make the output string */
108 strcpy(passwd, magic);
109 strncat(passwd, sp, sl);
110 strcat(passwd, "$");
111
112 px_md_finish(ctx, final);
113
114 /*
115 * and now, just to make sure things don't run too fast On a 60 Mhz
116 * Pentium this takes 34 msec, so you would need 30 seconds to build a
117 * 1000 entry dictionary...
118 */
119 for (i = 0; i < 1000; i++)
120 {
121 px_md_reset(ctx1);
122 if (i & 1)
123 px_md_update(ctx1, (const uint8 *) pw, strlen(pw));
124 else
125 px_md_update(ctx1, final, MD5_SIZE);
126
127 if (i % 3)
128 px_md_update(ctx1, (const uint8 *) sp, sl);
129
130 if (i % 7)
131 px_md_update(ctx1, (const uint8 *) pw, strlen(pw));
132
133 if (i & 1)
134 px_md_update(ctx1, final, MD5_SIZE);
135 else
136 px_md_update(ctx1, (const uint8 *) pw, strlen(pw));
137 px_md_finish(ctx1, final);
138 }
139
140 p = passwd + strlen(passwd);
141
142 l = (final[0] << 16) | (final[6] << 8) | final[12];
143 _crypt_to64(p, l, 4);
144 p += 4;
145 l = (final[1] << 16) | (final[7] << 8) | final[13];
146 _crypt_to64(p, l, 4);
147 p += 4;
148 l = (final[2] << 16) | (final[8] << 8) | final[14];
149 _crypt_to64(p, l, 4);
150 p += 4;
151 l = (final[3] << 16) | (final[9] << 8) | final[15];
152 _crypt_to64(p, l, 4);
153 p += 4;
154 l = (final[4] << 16) | (final[10] << 8) | final[5];
155 _crypt_to64(p, l, 4);
156 p += 4;
157 l = final[11];
158 _crypt_to64(p, l, 2);
159 p += 2;
160 *p = '\0';
161
162 /* Don't leave anything around in vm they could use. */
163 px_memset(final, 0, sizeof final);
164
165 px_md_free(ctx1);
166 px_md_free(ctx);
167
168 return passwd;
169}
#define MD5_SIZE
Definition: crypt-md5.c:14
static void _crypt_to64(char *s, unsigned long v, int n)
Definition: crypt-md5.c:20
void err(int eval, const char *fmt,...)
Definition: err.c:43
int px_find_digest(const char *name, PX_MD **res)
Definition: openssl.c:161
#define px_md_finish(md, buf)
Definition: px.h:206
#define px_md_free(md)
Definition: px.h:207
#define px_md_reset(md)
Definition: px.h:204
#define px_md_update(md, data, dlen)
Definition: px.h:205
Definition: px.h:108

References _crypt_to64(), err(), i, MD5_SIZE, px_find_digest(), px_md_finish, px_md_free, px_md_reset, px_md_update, and px_memset().

Referenced by run_crypt_md5().

◆ px_crypt_shacrypt()

char * px_crypt_shacrypt ( const char *  pw,
const char *  salt,
char *  passwd,
unsigned  dstlen 
)

Definition at line 69 of file crypt-sha.c.

70{
71 static const char rounds_prefix[] = "rounds=";
72 static const char *magic_bytes[2] = {"$5$", "$6$"};
73
74 /* Used to create the password hash string */
75 StringInfo out_buf = NULL;
76
78 PX_MD *digestA = NULL;
79 PX_MD *digestB = NULL;
80 int err;
81
82 const char *dec_salt_binary; /* pointer into the real salt string */
83 StringInfo decoded_salt = NULL; /* decoded salt string */
84 unsigned char sha_buf[PX_SHACRYPT_DIGEST_MAX_LEN];
85
86 /* temporary buffer for digests */
87 unsigned char sha_buf_tmp[PX_SHACRYPT_DIGEST_MAX_LEN];
88 char rounds_custom = 0;
89 char *p_bytes = NULL;
90 char *s_bytes = NULL;
91 char *cp = NULL;
92 const char *fp = NULL; /* intermediate pointer within salt string */
93 const char *ep = NULL; /* holds pointer to the end of the salt string */
94 size_t buf_size = 0; /* buffer size for sha256crypt/sha512crypt */
95 unsigned int block; /* number of bytes processed */
97 unsigned int len,
98 salt_len = 0;
99
100 /* Sanity checks */
101 if (!passwd)
102 return NULL;
103
104 if (pw == NULL)
105 elog(ERROR, "null value for password rejected");
106
107 if (salt == NULL)
108 elog(ERROR, "null value for salt rejected");
109
110 /*
111 * Make sure result buffers are large enough.
112 */
113 if (dstlen < PX_SHACRYPT_BUF_LEN)
114 elog(ERROR, "insufficient result buffer size to encrypt password");
115
116 /* Init result buffer */
119
120 /* Init contents of buffers properly */
121 memset(&sha_buf, '\0', sizeof(sha_buf));
122 memset(&sha_buf_tmp, '\0', sizeof(sha_buf_tmp));
123
124 /*
125 * Decode the salt string. We need to know how many rounds and which
126 * digest we have to use to hash the password.
127 */
128 len = strlen(pw);
129 dec_salt_binary = salt;
130
131 /*
132 * Analyze and prepare the salt string
133 *
134 * The magic string should be specified in the first three bytes of the
135 * salt string. Do some sanity checks first.
136 */
137 if (strlen(dec_salt_binary) < 3)
139 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
140 errmsg("invalid salt"));
141
142 /*
143 * Check format of magic bytes. These should define either 5=sha256crypt
144 * or 6=sha512crypt in the second byte, enclosed by ascii dollar signs.
145 */
146 if ((dec_salt_binary[0] != '$') || (dec_salt_binary[2] != '$'))
148 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
149 errmsg("invalid format of salt"),
150 errhint("magic byte format for shacrypt is either \"$5$\" or \"$6$\""));
151
152 /*
153 * Check magic byte for supported shacrypt digest.
154 *
155 * We're just interested in the very first 3 bytes of the salt string,
156 * since this defines the digest length to use.
157 */
158 if (strncmp(dec_salt_binary, magic_bytes[0], strlen(magic_bytes[0])) == 0)
159 {
161 dec_salt_binary += strlen(magic_bytes[0]);
162 }
163 else if (strncmp(dec_salt_binary, magic_bytes[1], strlen(magic_bytes[1])) == 0)
164 {
166 dec_salt_binary += strlen(magic_bytes[1]);
167 }
168
169 /*
170 * dec_salt_binary pointer is positioned after the magic bytes now
171 *
172 * We extract any options in the following code branch. The only optional
173 * setting we need to take care of is the "rounds" option. Note that the
174 * salt generator already checked for invalid settings before, but we need
175 * to do it here again to protect against injection of wrong values when
176 * called without the generator.
177 *
178 * If there is any garbage added after the magic byte and the options/salt
179 * string, we don't treat this special: This is just absorbed as part of
180 * the salt with up to PX_SHACRYPT_SALT_LEN_MAX.
181 *
182 * Unknown magic byte is handled further below.
183 */
184 if (strncmp(dec_salt_binary,
185 rounds_prefix, sizeof(rounds_prefix) - 1) == 0)
186 {
187 const char *num = dec_salt_binary + sizeof(rounds_prefix) - 1;
188 char *endp;
189 int srounds = strtoint(num, &endp, 10);
190
191 if (*endp != '$')
193 errcode(ERRCODE_SYNTAX_ERROR),
194 errmsg("could not parse salt options"));
195
196 dec_salt_binary = endp + 1;
197
198 /*
199 * We violate supported lower or upper bound of rounds, but in this
200 * case we change this value to the supported lower or upper value. We
201 * don't do this silently and print a NOTICE in such a case.
202 *
203 * Note that a salt string generated with gen_salt() would never
204 * generated such a salt string, since it would error out.
205 *
206 * But Drepper's upstream reference implementation supports this when
207 * passing the salt string directly, so we maintain compatibility.
208 */
209 if (srounds > PX_SHACRYPT_ROUNDS_MAX)
210 {
212 errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
213 errmsg("rounds=%d exceeds maximum supported value (%d), using %d instead",
214 srounds, PX_SHACRYPT_ROUNDS_MAX,
216 srounds = PX_SHACRYPT_ROUNDS_MAX;
217 }
218 else if (srounds < PX_SHACRYPT_ROUNDS_MIN)
219 {
221 errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
222 errmsg("rounds=%d is below supported value (%d), using %d instead",
223 srounds, PX_SHACRYPT_ROUNDS_MIN,
225 srounds = PX_SHACRYPT_ROUNDS_MIN;
226 }
227
228 rounds = (uint32) srounds;
229 rounds_custom = 1;
230 }
231
232 /*
233 * Choose the correct digest length and add the magic bytes to the result
234 * buffer. Also handle possible invalid magic byte we've extracted above.
235 */
236 switch (type)
237 {
239 {
240 /* Two PX_MD objects required */
241 err = px_find_digest("sha256", &digestA);
242 if (err)
243 goto error;
244
245 err = px_find_digest("sha256", &digestB);
246 if (err)
247 goto error;
248
249 /* digest buffer length is 32 for sha256 */
250 buf_size = 32;
251
252 appendStringInfoString(out_buf, magic_bytes[0]);
253 break;
254 }
255
257 {
258 /* Two PX_MD objects required */
259 err = px_find_digest("sha512", &digestA);
260 if (err)
261 goto error;
262
263 err = px_find_digest("sha512", &digestB);
264 if (err)
265 goto error;
266
268
269 appendStringInfoString(out_buf, magic_bytes[1]);
270 break;
271 }
272
274 elog(ERROR, "unknown crypt identifier \"%c\"", salt[1]);
275 }
276
277 if (rounds_custom > 0)
278 appendStringInfo(out_buf, "rounds=%u$", rounds);
279
280 /*
281 * We need the real decoded salt string from salt input, this is every
282 * character before the last '$' in the preamble. Append every compatible
283 * character up to PX_SHACRYPT_SALT_MAX_LEN to the result buffer. Note
284 * that depending on the input, there might be no '$' marker after the
285 * salt, when there is no password hash attached at the end.
286 *
287 * We try hard to recognize mistakes, but since we might get an input
288 * string which might also have the password hash after the salt string
289 * section we give up as soon we reach the end of the input or if there
290 * are any bytes consumed for the salt string until we reach the first '$'
291 * marker thereafter.
292 */
293 for (ep = dec_salt_binary;
294 *ep && ep < (dec_salt_binary + PX_SHACRYPT_SALT_MAX_LEN);
295 ep++)
296 {
297 /*
298 * Filter out any string which shouldn't be here.
299 *
300 * First check for accidentally embedded magic strings here. We don't
301 * support '$' in salt strings anyways and seeing a magic byte trying
302 * to identify shacrypt hashes might indicate that something went
303 * wrong when generating this salt string. Note that we later check
304 * for non-supported literals anyways, but any '$' here confuses us at
305 * this point.
306 */
307 fp = strstr(dec_salt_binary, magic_bytes[0]);
308 if (fp != NULL)
309 elog(ERROR, "bogus magic byte found in salt string");
310
311 fp = strstr(dec_salt_binary, magic_bytes[1]);
312 if (fp != NULL)
313 elog(ERROR, "bogus magic byte found in salt string");
314
315 /*
316 * This looks very strict, but we assume the caller did something
317 * wrong when we see a "rounds=" option here.
318 */
319 fp = strstr(dec_salt_binary, rounds_prefix);
320 if (fp != NULL)
321 elog(ERROR, "invalid rounds option specified in salt string");
322
323 if (*ep != '$')
324 {
325 if (strchr(_crypt_itoa64, *ep) != NULL)
326 appendStringInfoCharMacro(decoded_salt, *ep);
327 else
329 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
330 errmsg("invalid character in salt string: \"%.*s\"",
331 pg_mblen(ep), ep));
332 }
333 else
334 {
335 /*
336 * We encountered a '$' marker. Check if we already absorbed some
337 * bytes from input. If true, we are optimistic and terminate at
338 * this stage. If not, we try further.
339 *
340 * If we already consumed enough bytes for the salt string,
341 * everything that is after this marker is considered to be part
342 * of an optionally specified password hash and ignored.
343 */
344 if (decoded_salt->len > 0)
345 break;
346 }
347 }
348
349 salt_len = decoded_salt->len;
350 appendStringInfoString(out_buf, decoded_salt->data);
351 elog(DEBUG1, "using salt \"%s\", salt len = %d, rounds = %u",
352 decoded_salt->data, decoded_salt->len, rounds);
353
354 /*
355 * Sanity check: at this point the salt string buffer must not exceed
356 * expected size.
357 */
358 if (out_buf->len > (3 + 17 * rounds_custom + salt_len))
359 elog(ERROR, "unexpected length of salt string");
360
361 /*-
362 * 1. Start digest A
363 * 2. Add the password string to digest A
364 * 3. Add the salt to digest A
365 */
366 px_md_update(digestA, (const unsigned char *) pw, len);
367 px_md_update(digestA, (const unsigned char *) decoded_salt->data, salt_len);
368
369 /*-
370 * 4. Create digest B
371 * 5. Add password to digest B
372 * 6. Add the salt string to digest B
373 * 7. Add the password again to digest B
374 * 8. Finalize digest B
375 */
376 px_md_update(digestB, (const unsigned char *) pw, len);
377 px_md_update(digestB, (const unsigned char *) dec_salt_binary, salt_len);
378 px_md_update(digestB, (const unsigned char *) pw, len);
379 px_md_finish(digestB, sha_buf);
380
381 /*
382 * 9. For each block (excluding the NULL byte), add digest B to digest A.
383 */
384 for (block = len; block > buf_size; block -= buf_size)
385 px_md_update(digestA, sha_buf, buf_size);
386
387 /*-
388 * 10. For the remaining N bytes of the password string, add the first N
389 * bytes of digest B to A.
390 */
391 px_md_update(digestA, sha_buf, block);
392
393 /*-
394 * 11. For each bit of the binary representation of the length of the
395 * password string up to and including the highest 1-digit, starting from
396 * to lowest bit position (numeric value 1)
397 *
398 * a) for a 1-digit add digest B (sha_buf) to digest A
399 * b) for a 0-digit add the password string
400 */
401 block = len;
402 while (block)
403 {
404 px_md_update(digestA,
405 (block & 1) ? sha_buf : (const unsigned char *) pw,
406 (block & 1) ? buf_size : len);
407
408 /* right shift to next byte */
409 block >>= 1;
410 }
411
412 /* 12. Finalize digest A */
413 px_md_finish(digestA, sha_buf);
414
415 /* 13. Start digest DP */
416 px_md_reset(digestB);
417
418 /*-
419 * 14 Add every byte of the password string (excluding trailing NULL)
420 * to the digest DP
421 */
422 for (block = len; block > 0; block--)
423 px_md_update(digestB, (const unsigned char *) pw, len);
424
425 /* 15. Finalize digest DP */
426 px_md_finish(digestB, sha_buf_tmp);
427
428 /*-
429 * 16. produce byte sequence P with same length as password.
430 * a) for each block of 32 or 64 bytes of length of the password
431 * string the entire digest DP is used
432 * b) for the remaining N (up to 31 or 63) bytes use the
433 * first N bytes of digest DP
434 */
435 if ((p_bytes = palloc0(len)) == NULL)
436 {
437 goto error;
438 }
439
440 /* N step of 16, copy over the bytes from password */
441 for (cp = p_bytes, block = len; block > buf_size; block -= buf_size, cp += buf_size)
442 memcpy(cp, sha_buf_tmp, buf_size);
443 memcpy(cp, sha_buf_tmp, block);
444
445 /*
446 * 17. Start digest DS
447 */
448 px_md_reset(digestB);
449
450 /*-
451 * 18. Repeat the following 16+A[0] times, where A[0] represents the first
452 * byte in digest A interpreted as an 8-bit unsigned value
453 * add the salt to digest DS
454 */
455 for (block = 16 + sha_buf[0]; block > 0; block--)
456 px_md_update(digestB, (const unsigned char *) dec_salt_binary, salt_len);
457
458 /*
459 * 19. Finalize digest DS
460 */
461 px_md_finish(digestB, sha_buf_tmp);
462
463 /*-
464 * 20. Produce byte sequence S of the same length as the salt string where
465 *
466 * a) for each block of 32 or 64 bytes of length of the salt string the
467 * entire digest DS is used
468 *
469 * b) for the remaining N (up to 31 or 63) bytes use the first N
470 * bytes of digest DS
471 */
472 if ((s_bytes = palloc0(salt_len)) == NULL)
473 goto error;
474
475 for (cp = s_bytes, block = salt_len; block > buf_size; block -= buf_size, cp += buf_size)
476 memcpy(cp, sha_buf_tmp, buf_size);
477 memcpy(cp, sha_buf_tmp, block);
478
479 /* Make sure we don't leave something important behind */
480 px_memset(&sha_buf_tmp, 0, sizeof sha_buf);
481
482 /*-
483 * 21. Repeat a loop according to the number specified in the rounds=<N>
484 * specification in the salt (or the default value if none is
485 * present). Each round is numbered, starting with 0 and up to N-1.
486 *
487 * The loop uses a digest as input. In the first round it is the
488 * digest produced in step 12. In the latter steps it is the digest
489 * produced in step 21.h of the previous round. The following text
490 * uses the notation "digest A/B" to describe this behavior.
491 */
492 for (block = 0; block < rounds; block++)
493 {
494 /*
495 * Make it possible to abort in case large values for "rounds" are
496 * specified.
497 */
499
500 /* a) start digest B */
501 px_md_reset(digestB);
502
503 /*-
504 * b) for odd round numbers add the byte sequence P to digest B
505 * c) for even round numbers add digest A/B
506 */
507 px_md_update(digestB,
508 (block & 1) ? (const unsigned char *) p_bytes : sha_buf,
509 (block & 1) ? len : buf_size);
510
511 /* d) for all round numbers not divisible by 3 add the byte sequence S */
512 if ((block % 3) != 0)
513 px_md_update(digestB, (const unsigned char *) s_bytes, salt_len);
514
515 /* e) for all round numbers not divisible by 7 add the byte sequence P */
516 if ((block % 7) != 0)
517 px_md_update(digestB, (const unsigned char *) p_bytes, len);
518
519 /*-
520 * f) for odd round numbers add digest A/C
521 * g) for even round numbers add the byte sequence P
522 */
523 px_md_update(digestB,
524 (block & 1) ? sha_buf : (const unsigned char *) p_bytes,
525 (block & 1) ? buf_size : len);
526
527 /* h) finish digest C. */
528 px_md_finish(digestB, sha_buf);
529 }
530
531 px_md_free(digestA);
532 px_md_free(digestB);
533
534 digestA = NULL;
535 digestB = NULL;
536
537 pfree(s_bytes);
538 pfree(p_bytes);
539
540 s_bytes = NULL;
541 p_bytes = NULL;
542
543 /* prepare final result buffer */
544 appendStringInfoCharMacro(out_buf, '$');
545
546#define b64_from_24bit(B2, B1, B0, N) \
547 do { \
548 unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
549 int i = (N); \
550 while (i-- > 0) \
551 { \
552 appendStringInfoCharMacro(out_buf, _crypt_itoa64[w & 0x3f]); \
553 w >>= 6; \
554 } \
555 } while (0)
556
557 switch (type)
558 {
560 {
561 b64_from_24bit(sha_buf[0], sha_buf[10], sha_buf[20], 4);
562 b64_from_24bit(sha_buf[21], sha_buf[1], sha_buf[11], 4);
563 b64_from_24bit(sha_buf[12], sha_buf[22], sha_buf[2], 4);
564 b64_from_24bit(sha_buf[3], sha_buf[13], sha_buf[23], 4);
565 b64_from_24bit(sha_buf[24], sha_buf[4], sha_buf[14], 4);
566 b64_from_24bit(sha_buf[15], sha_buf[25], sha_buf[5], 4);
567 b64_from_24bit(sha_buf[6], sha_buf[16], sha_buf[26], 4);
568 b64_from_24bit(sha_buf[27], sha_buf[7], sha_buf[17], 4);
569 b64_from_24bit(sha_buf[18], sha_buf[28], sha_buf[8], 4);
570 b64_from_24bit(sha_buf[9], sha_buf[19], sha_buf[29], 4);
571 b64_from_24bit(0, sha_buf[31], sha_buf[30], 3);
572
573 break;
574 }
575
577 {
578 b64_from_24bit(sha_buf[0], sha_buf[21], sha_buf[42], 4);
579 b64_from_24bit(sha_buf[22], sha_buf[43], sha_buf[1], 4);
580 b64_from_24bit(sha_buf[44], sha_buf[2], sha_buf[23], 4);
581 b64_from_24bit(sha_buf[3], sha_buf[24], sha_buf[45], 4);
582 b64_from_24bit(sha_buf[25], sha_buf[46], sha_buf[4], 4);
583 b64_from_24bit(sha_buf[47], sha_buf[5], sha_buf[26], 4);
584 b64_from_24bit(sha_buf[6], sha_buf[27], sha_buf[48], 4);
585 b64_from_24bit(sha_buf[28], sha_buf[49], sha_buf[7], 4);
586 b64_from_24bit(sha_buf[50], sha_buf[8], sha_buf[29], 4);
587 b64_from_24bit(sha_buf[9], sha_buf[30], sha_buf[51], 4);
588 b64_from_24bit(sha_buf[31], sha_buf[52], sha_buf[10], 4);
589 b64_from_24bit(sha_buf[53], sha_buf[11], sha_buf[32], 4);
590 b64_from_24bit(sha_buf[12], sha_buf[33], sha_buf[54], 4);
591 b64_from_24bit(sha_buf[34], sha_buf[55], sha_buf[13], 4);
592 b64_from_24bit(sha_buf[56], sha_buf[14], sha_buf[35], 4);
593 b64_from_24bit(sha_buf[15], sha_buf[36], sha_buf[57], 4);
594 b64_from_24bit(sha_buf[37], sha_buf[58], sha_buf[16], 4);
595 b64_from_24bit(sha_buf[59], sha_buf[17], sha_buf[38], 4);
596 b64_from_24bit(sha_buf[18], sha_buf[39], sha_buf[60], 4);
597 b64_from_24bit(sha_buf[40], sha_buf[61], sha_buf[19], 4);
598 b64_from_24bit(sha_buf[62], sha_buf[20], sha_buf[41], 4);
599 b64_from_24bit(0, 0, sha_buf[63], 2);
600
601 break;
602 }
603
605 /* we shouldn't land here ... */
606 elog(ERROR, "unsupported digest length");
607 }
608
609 /*
610 * Copy over result to specified buffer.
611 *
612 * The passwd character buffer should have at least PX_SHACRYPT_BUF_LEN
613 * allocated, since we checked above if dstlen is smaller than
614 * PX_SHACRYPT_BUF_LEN (which also includes the NULL byte).
615 *
616 * In that case we would have failed above already.
617 */
618 memcpy(passwd, out_buf->data, out_buf->len);
619
620 /* make sure nothing important is left behind */
621 px_memset(&sha_buf, 0, sizeof sha_buf);
622 destroyStringInfo(out_buf);
623 destroyStringInfo(decoded_salt);
624
625 /* ...and we're done */
626 return passwd;
627
628error:
629 if (digestA != NULL)
630 px_md_free(digestA);
631
632 if (digestB != NULL)
633 px_md_free(digestB);
634
635 destroyStringInfo(out_buf);
636 destroyStringInfo(decoded_salt);
637
639 errcode(ERRCODE_INTERNAL_ERROR),
640 errmsg("cannot create encrypted password"));
641 return NULL; /* keep compiler quiet */
642}
PGCRYPTO_SHA_t
Definition: crypt-sha.c:56
@ PGCRYPTO_SHA256CRYPT
Definition: crypt-sha.c:57
@ PGCRYPTO_SHA_UNKOWN
Definition: crypt-sha.c:59
@ PGCRYPTO_SHA512CRYPT
Definition: crypt-sha.c:58
#define b64_from_24bit(B2, B1, B0, N)
static const char _crypt_itoa64[64+1]
Definition: crypt-sha.c:62
int errhint(const char *fmt,...)
Definition: elog.c:1318
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:226
#define NOTICE
Definition: elog.h:35
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1023
void pfree(void *pointer)
Definition: mcxt.c:2146
void * palloc0(Size size)
Definition: mcxt.c:1969
#define PX_SHACRYPT_ROUNDS_MAX
Definition: px-crypt.h:70
#define PX_SHACRYPT_BUF_LEN
Definition: px-crypt.h:61
#define PX_SHACRYPT_ROUNDS_MIN
Definition: px-crypt.h:67
#define PX_SHACRYPT_DIGEST_MAX_LEN
Definition: px-crypt.h:52
#define PX_SHACRYPT_SALT_MAX_LEN
Definition: px-crypt.h:49
#define PX_SHACRYPT_ROUNDS_DEFAULT
Definition: px-crypt.h:64
static void error(void)
Definition: sql-dyntest.c:147
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
void destroyStringInfo(StringInfo str)
Definition: stringinfo.c:409
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
StringInfo makeStringInfoExt(int initsize)
Definition: stringinfo.c:85
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:231
const char * type

References _crypt_itoa64, appendStringInfo(), appendStringInfoCharMacro, appendStringInfoString(), b64_from_24bit, CHECK_FOR_INTERRUPTS, StringInfoData::data, DEBUG1, destroyStringInfo(), elog, ereport, err(), errcode(), errhint(), errmsg(), ERROR, error(), StringInfoData::len, len, makeStringInfoExt(), NOTICE, palloc0(), pfree(), pg_mblen(), PGCRYPTO_SHA256CRYPT, PGCRYPTO_SHA512CRYPT, PGCRYPTO_SHA_UNKOWN, px_find_digest(), px_md_finish, px_md_free, px_md_reset, px_md_update, px_memset(), PX_SHACRYPT_BUF_LEN, PX_SHACRYPT_DIGEST_MAX_LEN, PX_SHACRYPT_ROUNDS_DEFAULT, PX_SHACRYPT_ROUNDS_MAX, PX_SHACRYPT_ROUNDS_MIN, PX_SHACRYPT_SALT_MAX_LEN, strtoint(), and type.

Referenced by run_crypt_sha().

◆ px_gen_salt()

int px_gen_salt ( const char *  salt_type,
char *  buf,
int  rounds 
)

Definition at line 156 of file px-crypt.c.

157{
158 struct generator *g;
159 char *p;
160 char rbuf[16];
161
163
164 for (g = gen_list; g->name; g++)
165 if (pg_strcasecmp(g->name, salt_type) == 0)
166 break;
167
168 if (g->name == NULL)
170
171 if (g->def_rounds)
172 {
173 if (rounds == 0)
174 rounds = g->def_rounds;
175
176 if (rounds < g->min_rounds || rounds > g->max_rounds)
177 return PXE_BAD_SALT_ROUNDS;
178 }
179
180 if (!pg_strong_random(rbuf, g->input_len))
181 return PXE_NO_RANDOM;
182
183 p = g->gen(rounds, rbuf, g->input_len, buf, PX_MAX_SALT_LEN);
184 px_memset(rbuf, 0, sizeof(rbuf));
185
186 if (p == NULL)
187 return PXE_BAD_SALT_ROUNDS;
188
189 return strlen(p);
190}
bool pg_strong_random(void *buf, size_t len)
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
static struct generator gen_list[]
Definition: px-crypt.c:137
#define PX_MAX_SALT_LEN
Definition: px-crypt.h:39
#define PXE_BAD_SALT_ROUNDS
Definition: px.h:61
#define PXE_NO_RANDOM
Definition: px.h:63
#define PXE_UNKNOWN_SALT_ALGO
Definition: px.h:60
int input_len
Definition: px-crypt.c:131
int max_rounds
Definition: px-crypt.c:134
char *(* gen)(unsigned long count, const char *input, int size, char *output, int output_size)
Definition: px-crypt.c:129
int def_rounds
Definition: px-crypt.c:132
char * name
Definition: px-crypt.c:128
int min_rounds
Definition: px-crypt.c:133

References buf, CheckBuiltinCryptoMode(), generator::def_rounds, generator::gen, gen_list, generator::input_len, generator::max_rounds, generator::min_rounds, generator::name, pg_strcasecmp(), pg_strong_random(), PX_MAX_SALT_LEN, px_memset(), PXE_BAD_SALT_ROUNDS, PXE_NO_RANDOM, and PXE_UNKNOWN_SALT_ALGO.

Referenced by pg_gen_salt(), and pg_gen_salt_rounds().