2 * Written by Solar Designer and placed in the public domain.
3 * See crypt_blowfish.c for more information.
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
20 typedef unsigned int BF_word
;
22 static unsigned char _crypt_itoa64
[64 + 1] =
23 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
26 _crypt_gensalt_traditional_rn(unsigned long count
,
27 const char *input
, int size
, char *output
, int output_size
)
29 if (size
< 2 || output_size
< 2 + 1 || (count
&& count
!= 25))
36 output
[0] = _crypt_itoa64
[(unsigned int) input
[0] & 0x3f];
37 output
[1] = _crypt_itoa64
[(unsigned int) input
[1] & 0x3f];
44 _crypt_gensalt_extended_rn(unsigned long count
,
45 const char *input
, int size
, char *output
, int output_size
)
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 if (size
< 3 || output_size
< 1 + 4 + 4 + 1 ||
52 (count
&& (count
> 0xffffff || !(count
& 1))))
63 output
[1] = _crypt_itoa64
[count
& 0x3f];
64 output
[2] = _crypt_itoa64
[(count
>> 6) & 0x3f];
65 output
[3] = _crypt_itoa64
[(count
>> 12) & 0x3f];
66 output
[4] = _crypt_itoa64
[(count
>> 18) & 0x3f];
67 value
= (unsigned long) (unsigned char) input
[0] |
68 ((unsigned long) (unsigned char) input
[1] << 8) |
69 ((unsigned long) (unsigned char) input
[2] << 16);
70 output
[5] = _crypt_itoa64
[value
& 0x3f];
71 output
[6] = _crypt_itoa64
[(value
>> 6) & 0x3f];
72 output
[7] = _crypt_itoa64
[(value
>> 12) & 0x3f];
73 output
[8] = _crypt_itoa64
[(value
>> 18) & 0x3f];
80 _crypt_gensalt_md5_rn(unsigned long count
,
81 const char *input
, int size
, char *output
, int output_size
)
85 if (size
< 3 || output_size
< 3 + 4 + 1 || (count
&& count
!= 1000))
95 value
= (unsigned long) (unsigned char) input
[0] |
96 ((unsigned long) (unsigned char) input
[1] << 8) |
97 ((unsigned long) (unsigned char) input
[2] << 16);
98 output
[3] = _crypt_itoa64
[value
& 0x3f];
99 output
[4] = _crypt_itoa64
[(value
>> 6) & 0x3f];
100 output
[5] = _crypt_itoa64
[(value
>> 12) & 0x3f];
101 output
[6] = _crypt_itoa64
[(value
>> 18) & 0x3f];
104 if (size
>= 6 && output_size
>= 3 + 4 + 4 + 1)
106 value
= (unsigned long) (unsigned char) input
[3] |
107 ((unsigned long) (unsigned char) input
[4] << 8) |
108 ((unsigned long) (unsigned char) input
[5] << 16);
109 output
[7] = _crypt_itoa64
[value
& 0x3f];
110 output
[8] = _crypt_itoa64
[(value
>> 6) & 0x3f];
111 output
[9] = _crypt_itoa64
[(value
>> 12) & 0x3f];
112 output
[10] = _crypt_itoa64
[(value
>> 18) & 0x3f];
121 static unsigned char BF_itoa64
[64 + 1] =
122 "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
125 BF_encode(char *dst
, const BF_word
* src
, int size
)
127 unsigned char *sptr
= (unsigned char *) src
;
128 unsigned char *end
= sptr
+ size
;
129 unsigned char *dptr
= (unsigned char *) dst
;
136 *dptr
++ = BF_itoa64
[c1
>> 2];
137 c1
= (c1
& 0x03) << 4;
140 *dptr
++ = BF_itoa64
[c1
];
146 *dptr
++ = BF_itoa64
[c1
];
147 c1
= (c2
& 0x0f) << 2;
150 *dptr
++ = BF_itoa64
[c1
];
156 *dptr
++ = BF_itoa64
[c1
];
157 *dptr
++ = BF_itoa64
[c2
& 0x3f];
158 } while (sptr
< end
);
162 _crypt_gensalt_blowfish_rn(unsigned long count
,
163 const char *input
, int size
, char *output
, int output_size
)
165 if (size
< 16 || output_size
< 7 + 22 + 1 ||
166 (count
&& (count
< 4 || count
> 31)))
180 output
[4] = '0' + count
/ 10;
181 output
[5] = '0' + count
% 10;
184 BF_encode(&output
[7], (BF_word
*) input
, 16);
185 output
[7 + 22] = '\0';