2 * Written by Solar Designer and placed in the public domain.
3 * See crypt_blowfish.c for more information.
5 * contrib/pgcrypto/crypt-gensalt.c
7 * This file contains salt generation functions for the traditional and
8 * other common crypt(3) algorithms, except for bcrypt which is defined
9 * entirely in crypt_blowfish.c.
11 * Put bcrypt generator also here as crypt-blowfish.c
12 * may not be compiled always. -- marko
22"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
26 const char *
input,
int size,
char *
output,
int output_size)
28 if (size < 2 || output_size < 2 + 1 || (count && count != 25))
44 const char *
input,
int size,
char *
output,
int output_size)
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))))
66 value = (
unsigned long) (
unsigned char)
input[0] |
67 ((
unsigned long) (
unsigned char)
input[1] << 8) |
68 ((
unsigned long) (
unsigned char)
input[2] << 16);
80 const char *
input,
int size,
char *
output,
int output_size)
84 if (size < 3 || output_size < 3 + 4 + 1 || (count && count != 1000))
94 value = (
unsigned long) (
unsigned char)
input[0] |
95 ((
unsigned long) (
unsigned char)
input[1] << 8) |
96 ((
unsigned long) (
unsigned char)
input[2] << 16);
103 if (size >= 6 && output_size >= 3 + 4 + 4 + 1)
105 value = (
unsigned long) (
unsigned char)
input[3] |
106 ((
unsigned long) (
unsigned char)
input[4] << 8) |
107 ((
unsigned long) (
unsigned char)
input[5] << 16);
121"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
126 const unsigned char *sptr = (
const unsigned char *) src;
127 const unsigned char *end = sptr + size;
128 unsigned char *dptr = (
unsigned char *) dst;
136 c1 = (c1 & 0x03) << 4;
146 c1 = (c2 & 0x0f) << 2;
157 }
while (sptr < end);
162 const char *
input,
int size,
char *
output,
int output_size)
164 if (size < 16 || output_size < 7 + 22 + 1 ||
165 (count && (count < 4 || count > 31)))
179 output[4] =
'0' + count / 10;
180 output[5] =
'0' + count % 10;
190 * Helper for _crypt_gensalt_sha256_rn and _crypt_gensalt_sha512_rn
194 const char *
input,
int size,
char *
output,
int output_size)
200 /* output buffer must be allocated with PX_MAX_SALT_LEN bytes */
204 errmsg(
"invalid size of salt"));
207 * Care must be taken to not exceed the buffer size allocated for the
208 * input character buffer.
212 errcode(ERRCODE_INTERNAL_ERROR),
213 errmsg(
"invalid length of salt buffer"));
215 /* Skip magic bytes, set by callers */
217 if ((rc =
pg_snprintf(s_ptr, 18,
"rounds=%lu$", count)) <= 0)
219 errcode(ERRCODE_INTERNAL_ERROR),
220 errmsg(
"cannot format salt string"));
222 /* s_ptr should now be positioned at the start of the salt string */
226 * Normalize salt string
228 * size of input buffer was checked above to not exceed
229 * PX_SHACRYPT_SALT_LEN_MAX.
231 for (
int i = 0;
i < size;
i++)
241/* gen_list->gen function for sha512 */
244 char const *
input,
int size,
245 char *
output,
int output_size)
247 memset(
output, 0, output_size);
248 /* set magic byte for sha512crypt */
256/* gen_list->gen function for sha256 */
259 const char *
input,
int size,
260 char *
output,
int output_size)
262 memset(
output, 0, output_size);
263 /* set magic byte for sha256crypt */
char * _crypt_gensalt_sha512_rn(unsigned long count, char const *input, int size, char *output, int output_size)
char * _crypt_gensalt_traditional_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)
static unsigned char _crypt_itoa64[64+1]
char * _crypt_gensalt_md5_rn(unsigned long count, const char *input, int size, char *output, int output_size)
static char * _crypt_gensalt_sha(unsigned long count, const char *input, int size, char *output, int output_size)
static unsigned char BF_itoa64[64+1]
static void BF_encode(char *dst, const BF_word *src, int size)
char * _crypt_gensalt_blowfish_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)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
int int pg_snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PX_SHACRYPT_SALT_BUF_LEN
#define PX_SHACRYPT_SALT_MAX_LEN