PostgreSQL Source Code git master
Loading...
Searching...
No Matches
crypt-gensalt.c File Reference
#include "postgres.h"
#include "px-crypt.h"
Include dependency graph for crypt-gensalt.c:

Go to the source code of this file.

Typedefs

typedef unsigned int BF_word
 

Functions

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)
 
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)
 
static char_crypt_gensalt_sha (unsigned long count, const char *input, int size, char *output, int output_size)
 
char_crypt_gensalt_sha512_rn (unsigned long count, char const *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)
 

Variables

static unsigned char _crypt_itoa64 [64+1]
 
static unsigned char BF_itoa64 [64+1]
 

Typedef Documentation

◆ BF_word

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

Function Documentation

◆ _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 163 of file crypt-gensalt.c.

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

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

References _crypt_itoa64, fb(), 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 81 of file crypt-gensalt.c.

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

References _crypt_itoa64, fb(), input, output, and value.

◆ _crypt_gensalt_sha()

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

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

197{
198 char *s_ptr = output;
200 int rc;
201
202 /* output buffer must be allocated with PX_MAX_SALT_LEN bytes */
206 errmsg("invalid size of salt"));
207
208 /*
209 * Care must be taken to not exceed the buffer size allocated for the
210 * input character buffer.
211 */
212 if ((PX_SHACRYPT_SALT_MAX_LEN != size) || (output_size < size))
215 errmsg("invalid length of salt buffer"));
216
217 /* Skip magic bytes, set by callers */
218 s_ptr += 3;
219 if ((rc = pg_snprintf(s_ptr, 18, "rounds=%lu$", count)) <= 0)
222 errmsg("cannot format salt string"));
223
224 /* s_ptr should now be positioned at the start of the salt string */
225 s_ptr += rc;
226
227 /*
228 * Normalize salt string
229 *
230 * size of input buffer was checked above to not exceed
231 * PX_SHACRYPT_SALT_LEN_MAX.
232 */
233 for (int i = 0; i < size; i++)
234 {
235 *s_ptr = _crypt_itoa64[input[i] & 0x3f];
236 s_ptr++;
237 }
238
239 /* We're done */
240 return output;
241}
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
int i
Definition isn.c:77
static char * errmsg
int int pg_snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PX_SHACRYPT_SALT_BUF_LEN
Definition px-crypt.h:55
#define PX_MAX_SALT_LEN
Definition px-crypt.h:39
#define PX_SHACRYPT_SALT_MAX_LEN
Definition px-crypt.h:49

References _crypt_itoa64, ereport, errcode(), errmsg, ERROR, fb(), i, input, output, pg_snprintf(), PX_MAX_SALT_LEN, PX_SHACRYPT_SALT_BUF_LEN, and PX_SHACRYPT_SALT_MAX_LEN.

Referenced by _crypt_gensalt_sha256_rn(), and _crypt_gensalt_sha512_rn().

◆ _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 260 of file crypt-gensalt.c.

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

References _crypt_gensalt_sha(), fb(), input, and output.

◆ _crypt_gensalt_sha512_rn()

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

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

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

References _crypt_gensalt_sha(), fb(), 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, fb(), input, and output.

◆ BF_encode()

static void BF_encode ( char dst,
const BF_word src,
int  size 
)
static

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

127{
128 const unsigned char *sptr = (const unsigned char *) src;
129 const unsigned char *end = sptr + size;
130 unsigned char *dptr = (unsigned char *) dst;
131 unsigned int c1,
132 c2;
133
134 do
135 {
136 c1 = *sptr++;
137 *dptr++ = BF_itoa64[c1 >> 2];
138 c1 = (c1 & 0x03) << 4;
139 if (sptr >= end)
140 {
141 *dptr++ = BF_itoa64[c1];
142 break;
143 }
144
145 c2 = *sptr++;
146 c1 |= c2 >> 4;
147 *dptr++ = BF_itoa64[c1];
148 c1 = (c2 & 0x0f) << 2;
149 if (sptr >= end)
150 {
151 *dptr++ = BF_itoa64[c1];
152 break;
153 }
154
155 c2 = *sptr++;
156 c1 |= c2 >> 6;
157 *dptr++ = BF_itoa64[c1];
158 *dptr++ = BF_itoa64[c2 & 0x3f];
159 } while (sptr < end);
160}
static unsigned char BF_itoa64[64+1]

References BF_itoa64, and fb().

Referenced by _crypt_gensalt_blowfish_rn().

Variable Documentation

◆ _crypt_itoa64

unsigned char _crypt_itoa64[64+1]
static
Initial value:
=
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

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

Referenced by _crypt_gensalt_extended_rn(), _crypt_gensalt_md5_rn(), _crypt_gensalt_sha(), and _crypt_gensalt_traditional_rn().

◆ BF_itoa64

unsigned char BF_itoa64[64+1]
static
Initial value:
=
"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

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

Referenced by BF_encode().