@@ -185,3 +185,85 @@ _crypt_gensalt_blowfish_rn(unsigned long count,
185
185
186
186
return output ;
187
187
}
188
+
189
+ /*
190
+ * Helper for _crypt_gensalt_sha256_rn and _crypt_gensalt_sha512_rn
191
+ */
192
+ static char *
193
+ _crypt_gensalt_sha (unsigned long count ,
194
+ const char * input , int size , char * output , int output_size )
195
+ {
196
+ char * s_ptr = output ;
197
+ unsigned int result_bufsize = PX_SHACRYPT_SALT_BUF_LEN ;
198
+ int rc ;
199
+
200
+ /* output buffer must be allocated with PX_MAX_SALT_LEN bytes */
201
+ if (PX_MAX_SALT_LEN < result_bufsize )
202
+ ereport (ERROR ,
203
+ errcode (ERRCODE_SYNTAX_ERROR ),
204
+ errmsg ("invalid size of salt" ));
205
+
206
+ /*
207
+ * Care must be taken to not exceed the buffer size allocated for the
208
+ * input character buffer.
209
+ */
210
+ if ((PX_SHACRYPT_SALT_MAX_LEN != size ) || (output_size < size ))
211
+ ereport (ERROR ,
212
+ errcode (ERRCODE_INTERNAL_ERROR ),
213
+ errmsg ("invalid length of salt buffer" ));
214
+
215
+ /* Skip magic bytes, set by callers */
216
+ s_ptr += 3 ;
217
+ if ((rc = pg_snprintf (s_ptr , 18 , "rounds=%ld$" , count )) <= 0 )
218
+ ereport (ERROR ,
219
+ errcode (ERRCODE_INTERNAL_ERROR ),
220
+ errmsg ("cannot format salt string" ));
221
+
222
+ /* s_ptr should now be positioned at the start of the salt string */
223
+ s_ptr += rc ;
224
+
225
+ /*
226
+ * Normalize salt string
227
+ *
228
+ * size of input buffer was checked above to not exceed
229
+ * PX_SHACRYPT_SALT_LEN_MAX.
230
+ */
231
+ for (int i = 0 ; i < size ; i ++ )
232
+ {
233
+ * s_ptr = _crypt_itoa64 [input [i ] & 0x3f ];
234
+ s_ptr ++ ;
235
+ }
236
+
237
+ /* We're done */
238
+ return output ;
239
+ }
240
+
241
+ /* gen_list->gen function for sha512 */
242
+ char *
243
+ _crypt_gensalt_sha512_rn (unsigned long count ,
244
+ char const * input , int size ,
245
+ char * output , int output_size )
246
+ {
247
+ memset (output , 0 , output_size );
248
+ /* set magic byte for sha512crypt */
249
+ output [0 ] = '$' ;
250
+ output [1 ] = '6' ;
251
+ output [2 ] = '$' ;
252
+
253
+ return _crypt_gensalt_sha (count , input , size , output , output_size );
254
+ }
255
+
256
+ /* gen_list->gen function for sha256 */
257
+ char *
258
+ _crypt_gensalt_sha256_rn (unsigned long count ,
259
+ const char * input , int size ,
260
+ char * output , int output_size )
261
+ {
262
+ memset (output , 0 , output_size );
263
+ /* set magic byte for sha256crypt */
264
+ output [0 ] = '$' ;
265
+ output [1 ] = '5' ;
266
+ output [2 ] = '$' ;
267
+
268
+ return _crypt_gensalt_sha (count , input , size , output , output_size );
269
+ }
0 commit comments