PostgreSQL Source Code git master
Loading...
Searching...
No Matches
crypt-gensalt.c
Go to the documentation of this file.
1/*
2 * Written by Solar Designer and placed in the public domain.
3 * See crypt_blowfish.c for more information.
4 *
5 * contrib/pgcrypto/crypt-gensalt.c
6 *
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.
10 *
11 * Put bcrypt generator also here as crypt-blowfish.c
12 * may not be compiled always. -- marko
13 */
14
15#include "postgres.h"
16
17#include "px-crypt.h"
18
19typedef unsigned int BF_word;
20
21static unsigned char _crypt_itoa64[64 + 1] =
22"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
23
24char *
26 const char *input, int size, char *output, int output_size)
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}
41
42char *
43_crypt_gensalt_extended_rn(unsigned long count,
44 const char *input, int size, char *output, int output_size)
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}
79
80char *
81_crypt_gensalt_md5_rn(unsigned long count,
82 const char *input, int size, char *output, int output_size)
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}
119
120
121
122static unsigned char BF_itoa64[64 + 1] =
123"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
124
125static void
126BF_encode(char *dst, const BF_word *src, int size)
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}
161
162char *
163_crypt_gensalt_blowfish_rn(unsigned long count,
164 const char *input, int size, char *output, int output_size)
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}
190
191/*
192 * Helper for _crypt_gensalt_sha256_rn and _crypt_gensalt_sha512_rn
193 */
194static char *
195_crypt_gensalt_sha(unsigned long count,
196 const char *input, int size, char *output, int output_size)
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}
242
243/* gen_list->gen function for sha512 */
244char *
245_crypt_gensalt_sha512_rn(unsigned long count,
246 char const *input, int size,
247 char *output, int output_size)
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}
257
258/* gen_list->gen function for sha256 */
259char *
260_crypt_gensalt_sha256_rn(unsigned long count,
261 const char *input, int size,
262 char *output, int output_size)
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}
unsigned int BF_word
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)
unsigned int BF_word
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)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
FILE * input
FILE * output
static struct @177 value
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
static int fb(int x)
#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