PostgreSQL Source Code  git master
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
 

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_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)
 

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_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)
615  ereport(ERROR,
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  {
628  ereport(ERROR,
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));
637  ereport(ERROR,
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];
652  BF_ENCRYPT;
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];
663  BF_ENCRYPT;
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];
669  BF_ENCRYPT;
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  {
733  BF_ENCRYPT;
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:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
FILE * output
int i
Definition: isn.c:73
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
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
static pg_noinline void Size size
Definition: slab.c:607
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, BF_ctx::S, and size.

◆ _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, output, and size.

◆ _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 @155 value

References _crypt_itoa64, input, output, size, 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, size, and value.

◆ _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, output, and size.

◆ px_crypt()

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

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

91 {
92  const struct px_crypt_algo *c;
93 
94  for (c = px_crypt_list; c->id; c++)
95  {
96  if (!c->id_len)
97  break;
98  if (strncmp(salt, c->id, c->id_len) == 0)
99  break;
100  }
101 
102  if (c->crypt == NULL)
103  return NULL;
104 
105  return c->crypt(psw, salt, buf, len);
106 }
const void size_t len
static char * buf
Definition: pg_test_fsync.c:73
char * c
static const struct px_crypt_algo px_crypt_list[]
Definition: px-crypt.c:79
char * id
Definition: px-crypt.c:72

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)
695  ereport(ERROR,
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)
743  ereport(ERROR,
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 }
unsigned int uint32
Definition: c.h:506
unsigned char uint8
Definition: c.h:504
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 char *magic = "$1$"; /* This string is magic for this algorithm.
37  * Having it this way, we can get better later
38  * on */
39  static char *p;
40  static 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, (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:162
#define px_md_finish(md, buf)
Definition: px.h:195
#define px_md_free(md)
Definition: px.h:196
#define px_md_reset(md)
Definition: px.h:193
#define px_md_update(md, data, dlen)
Definition: px.h:194
Definition: px.h:100

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_gen_salt()

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

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

133 {
134  struct generator *g;
135  char *p;
136  char rbuf[16];
137 
138  for (g = gen_list; g->name; g++)
139  if (pg_strcasecmp(g->name, salt_type) == 0)
140  break;
141 
142  if (g->name == NULL)
143  return PXE_UNKNOWN_SALT_ALGO;
144 
145  if (g->def_rounds)
146  {
147  if (rounds == 0)
148  rounds = g->def_rounds;
149 
150  if (rounds < g->min_rounds || rounds > g->max_rounds)
151  return PXE_BAD_SALT_ROUNDS;
152  }
153 
154  if (!pg_strong_random(rbuf, g->input_len))
155  return PXE_NO_RANDOM;
156 
157  p = g->gen(rounds, rbuf, g->input_len, buf, PX_MAX_SALT_LEN);
158  px_memset(rbuf, 0, sizeof(rbuf));
159 
160  if (p == NULL)
161  return PXE_BAD_SALT_ROUNDS;
162 
163  return strlen(p);
164 }
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:123
#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:117
int max_rounds
Definition: px-crypt.c:120
char *(* gen)(unsigned long count, const char *input, int size, char *output, int output_size)
Definition: px-crypt.c:115
int def_rounds
Definition: px-crypt.c:118
char * name
Definition: px-crypt.c:114
int min_rounds
Definition: px-crypt.c:119

References buf, 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().