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
19 typedef unsigned int BF_word
;
21 static unsigned char _crypt_itoa64
[64 + 1] =
22 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
25 _crypt_gensalt_traditional_rn(unsigned long count
,
26 const char *input
, int size
, char *output
, int output_size
)
28 if (size
< 2 || output_size
< 2 + 1 || (count
&& count
!= 25))
35 output
[0] = _crypt_itoa64
[(unsigned int) input
[0] & 0x3f];
36 output
[1] = _crypt_itoa64
[(unsigned int) input
[1] & 0x3f];
43 _crypt_gensalt_extended_rn(unsigned long count
,
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))))
62 output
[1] = _crypt_itoa64
[count
& 0x3f];
63 output
[2] = _crypt_itoa64
[(count
>> 6) & 0x3f];
64 output
[3] = _crypt_itoa64
[(count
>> 12) & 0x3f];
65 output
[4] = _crypt_itoa64
[(count
>> 18) & 0x3f];
66 value
= (unsigned long) (unsigned char) input
[0] |
67 ((unsigned long) (unsigned char) input
[1] << 8) |
68 ((unsigned long) (unsigned char) input
[2] << 16);
69 output
[5] = _crypt_itoa64
[value
& 0x3f];
70 output
[6] = _crypt_itoa64
[(value
>> 6) & 0x3f];
71 output
[7] = _crypt_itoa64
[(value
>> 12) & 0x3f];
72 output
[8] = _crypt_itoa64
[(value
>> 18) & 0x3f];
79 _crypt_gensalt_md5_rn(unsigned long count
,
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);
97 output
[3] = _crypt_itoa64
[value
& 0x3f];
98 output
[4] = _crypt_itoa64
[(value
>> 6) & 0x3f];
99 output
[5] = _crypt_itoa64
[(value
>> 12) & 0x3f];
100 output
[6] = _crypt_itoa64
[(value
>> 18) & 0x3f];
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);
108 output
[7] = _crypt_itoa64
[value
& 0x3f];
109 output
[8] = _crypt_itoa64
[(value
>> 6) & 0x3f];
110 output
[9] = _crypt_itoa64
[(value
>> 12) & 0x3f];
111 output
[10] = _crypt_itoa64
[(value
>> 18) & 0x3f];
120 static unsigned char BF_itoa64
[64 + 1] =
121 "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
124 BF_encode(char *dst
, const BF_word
*src
, int size
)
126 const unsigned char *sptr
= (const unsigned char *) src
;
127 const unsigned char *end
= sptr
+ size
;
128 unsigned char *dptr
= (unsigned char *) dst
;
135 *dptr
++ = BF_itoa64
[c1
>> 2];
136 c1
= (c1
& 0x03) << 4;
139 *dptr
++ = BF_itoa64
[c1
];
145 *dptr
++ = BF_itoa64
[c1
];
146 c1
= (c2
& 0x0f) << 2;
149 *dptr
++ = BF_itoa64
[c1
];
155 *dptr
++ = BF_itoa64
[c1
];
156 *dptr
++ = BF_itoa64
[c2
& 0x3f];
157 } while (sptr
< end
);
161 _crypt_gensalt_blowfish_rn(unsigned long count
,
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;
183 BF_encode(&output
[7], (const BF_word
*) input
, 16);
184 output
[7 + 22] = '\0';