Line data Source code
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 :
19 : typedef unsigned int BF_word;
20 :
21 : static unsigned char _crypt_itoa64[64 + 1] =
22 : "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
23 :
24 : char *
25 1 : _crypt_gensalt_traditional_rn(unsigned long count,
26 : const char *input, int size, char *output, int output_size)
27 : {
28 1 : if (size < 2 || output_size < 2 + 1 || (count && count != 25))
29 : {
30 0 : if (output_size > 0)
31 0 : output[0] = '\0';
32 0 : return NULL;
33 : }
34 :
35 1 : output[0] = _crypt_itoa64[(unsigned int) input[0] & 0x3f];
36 1 : output[1] = _crypt_itoa64[(unsigned int) input[1] & 0x3f];
37 1 : output[2] = '\0';
38 :
39 1 : return output;
40 : }
41 :
42 : char *
43 1 : _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 1 : if (size < 3 || output_size < 1 + 4 + 4 + 1 ||
53 1 : (count && (count > 0xffffff || !(count & 1))))
54 : {
55 0 : if (output_size > 0)
56 0 : output[0] = '\0';
57 0 : return NULL;
58 : }
59 :
60 1 : if (!count)
61 0 : count = 725;
62 :
63 1 : output[0] = '_';
64 1 : output[1] = _crypt_itoa64[count & 0x3f];
65 1 : output[2] = _crypt_itoa64[(count >> 6) & 0x3f];
66 1 : output[3] = _crypt_itoa64[(count >> 12) & 0x3f];
67 1 : output[4] = _crypt_itoa64[(count >> 18) & 0x3f];
68 1 : value = (unsigned long) (unsigned char) input[0] |
69 1 : ((unsigned long) (unsigned char) input[1] << 8) |
70 1 : ((unsigned long) (unsigned char) input[2] << 16);
71 1 : output[5] = _crypt_itoa64[value & 0x3f];
72 1 : output[6] = _crypt_itoa64[(value >> 6) & 0x3f];
73 1 : output[7] = _crypt_itoa64[(value >> 12) & 0x3f];
74 1 : output[8] = _crypt_itoa64[(value >> 18) & 0x3f];
75 1 : output[9] = '\0';
76 :
77 1 : return output;
78 : }
79 :
80 : char *
81 1 : _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 1 : if (size < 3 || output_size < 3 + 4 + 1 || (count && count != 1000))
87 : {
88 0 : if (output_size > 0)
89 0 : output[0] = '\0';
90 0 : return NULL;
91 : }
92 :
93 1 : output[0] = '$';
94 1 : output[1] = '1';
95 1 : output[2] = '$';
96 1 : value = (unsigned long) (unsigned char) input[0] |
97 1 : ((unsigned long) (unsigned char) input[1] << 8) |
98 1 : ((unsigned long) (unsigned char) input[2] << 16);
99 1 : output[3] = _crypt_itoa64[value & 0x3f];
100 1 : output[4] = _crypt_itoa64[(value >> 6) & 0x3f];
101 1 : output[5] = _crypt_itoa64[(value >> 12) & 0x3f];
102 1 : output[6] = _crypt_itoa64[(value >> 18) & 0x3f];
103 1 : output[7] = '\0';
104 :
105 1 : if (size >= 6 && output_size >= 3 + 4 + 4 + 1)
106 : {
107 1 : value = (unsigned long) (unsigned char) input[3] |
108 1 : ((unsigned long) (unsigned char) input[4] << 8) |
109 1 : ((unsigned long) (unsigned char) input[5] << 16);
110 1 : output[7] = _crypt_itoa64[value & 0x3f];
111 1 : output[8] = _crypt_itoa64[(value >> 6) & 0x3f];
112 1 : output[9] = _crypt_itoa64[(value >> 12) & 0x3f];
113 1 : output[10] = _crypt_itoa64[(value >> 18) & 0x3f];
114 1 : output[11] = '\0';
115 : }
116 :
117 1 : return output;
118 : }
119 :
120 :
121 :
122 : static unsigned char BF_itoa64[64 + 1] =
123 : "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
124 :
125 : static void
126 1 : BF_encode(char *dst, const BF_word *src, int size)
127 : {
128 1 : const unsigned char *sptr = (const unsigned char *) src;
129 1 : const unsigned char *end = sptr + size;
130 1 : unsigned char *dptr = (unsigned char *) dst;
131 : unsigned int c1,
132 : c2;
133 :
134 : do
135 : {
136 6 : c1 = *sptr++;
137 6 : *dptr++ = BF_itoa64[c1 >> 2];
138 6 : c1 = (c1 & 0x03) << 4;
139 6 : if (sptr >= end)
140 : {
141 1 : *dptr++ = BF_itoa64[c1];
142 1 : break;
143 : }
144 :
145 5 : c2 = *sptr++;
146 5 : c1 |= c2 >> 4;
147 5 : *dptr++ = BF_itoa64[c1];
148 5 : c1 = (c2 & 0x0f) << 2;
149 5 : if (sptr >= end)
150 : {
151 0 : *dptr++ = BF_itoa64[c1];
152 0 : break;
153 : }
154 :
155 5 : c2 = *sptr++;
156 5 : c1 |= c2 >> 6;
157 5 : *dptr++ = BF_itoa64[c1];
158 5 : *dptr++ = BF_itoa64[c2 & 0x3f];
159 5 : } while (sptr < end);
160 1 : }
161 :
162 : char *
163 1 : _crypt_gensalt_blowfish_rn(unsigned long count,
164 : const char *input, int size, char *output, int output_size)
165 : {
166 1 : if (size < 16 || output_size < 7 + 22 + 1 ||
167 1 : (count && (count < 4 || count > 31)))
168 : {
169 0 : if (output_size > 0)
170 0 : output[0] = '\0';
171 0 : return NULL;
172 : }
173 :
174 1 : if (!count)
175 0 : count = 5;
176 :
177 1 : output[0] = '$';
178 1 : output[1] = '2';
179 1 : output[2] = 'a';
180 1 : output[3] = '$';
181 1 : output[4] = '0' + count / 10;
182 1 : output[5] = '0' + count % 10;
183 1 : output[6] = '$';
184 :
185 1 : BF_encode(&output[7], (const BF_word *) input, 16);
186 1 : output[7 + 22] = '\0';
187 :
188 1 : return output;
189 : }
190 :
191 : /*
192 : * Helper for _crypt_gensalt_sha256_rn and _crypt_gensalt_sha512_rn
193 : */
194 : static char *
195 4 : _crypt_gensalt_sha(unsigned long count,
196 : const char *input, int size, char *output, int output_size)
197 : {
198 4 : char *s_ptr = output;
199 4 : unsigned int result_bufsize = PX_SHACRYPT_SALT_BUF_LEN;
200 : int rc;
201 :
202 : /* output buffer must be allocated with PX_MAX_SALT_LEN bytes */
203 4 : if (PX_MAX_SALT_LEN < result_bufsize)
204 0 : ereport(ERROR,
205 : errcode(ERRCODE_SYNTAX_ERROR),
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 4 : if ((PX_SHACRYPT_SALT_MAX_LEN != size) || (output_size < size))
213 0 : ereport(ERROR,
214 : errcode(ERRCODE_INTERNAL_ERROR),
215 : errmsg("invalid length of salt buffer"));
216 :
217 : /* Skip magic bytes, set by callers */
218 4 : s_ptr += 3;
219 4 : if ((rc = pg_snprintf(s_ptr, 18, "rounds=%lu$", count)) <= 0)
220 0 : ereport(ERROR,
221 : errcode(ERRCODE_INTERNAL_ERROR),
222 : errmsg("cannot format salt string"));
223 :
224 : /* s_ptr should now be positioned at the start of the salt string */
225 4 : 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 68 : for (int i = 0; i < size; i++)
234 : {
235 64 : *s_ptr = _crypt_itoa64[input[i] & 0x3f];
236 64 : s_ptr++;
237 : }
238 :
239 : /* We're done */
240 4 : return output;
241 : }
242 :
243 : /* gen_list->gen function for sha512 */
244 : char *
245 2 : _crypt_gensalt_sha512_rn(unsigned long count,
246 : char const *input, int size,
247 : char *output, int output_size)
248 : {
249 2 : memset(output, 0, output_size);
250 : /* set magic byte for sha512crypt */
251 2 : output[0] = '$';
252 2 : output[1] = '6';
253 2 : output[2] = '$';
254 :
255 2 : return _crypt_gensalt_sha(count, input, size, output, output_size);
256 : }
257 :
258 : /* gen_list->gen function for sha256 */
259 : char *
260 2 : _crypt_gensalt_sha256_rn(unsigned long count,
261 : const char *input, int size,
262 : char *output, int output_size)
263 : {
264 2 : memset(output, 0, output_size);
265 : /* set magic byte for sha256crypt */
266 2 : output[0] = '$';
267 2 : output[1] = '5';
268 2 : output[2] = '$';
269 :
270 2 : return _crypt_gensalt_sha(count, input, size, output, output_size);
271 : }
|