git ssb

0+

dangerousbeans / %aPBe2k3ugtjBr4rrsU1…



Tree: 7e4f6e0afadfe4fcafb532df1846f9f05c340463

Files: 7e4f6e0afadfe4fcafb532df1846f9f05c340463 / ext / mri / bcrypt.c

8833 bytesRaw
1/* $OpenBSD: bcrypt.c,v 1.22 2007/02/20 01:44:16 ray Exp $ */
2
3/*
4 * Modified by <hongli@phusion.nl> on 2009-08-05:
5 *
6 * - Got rid of the global variables; they're not thread-safe.
7 * Modified the functions to accept local buffers instead.
8 *
9 * Modified by <coda.hale@gmail.com> on 2007-02-27:
10 *
11 * - Changed bcrypt_gensalt to accept a random seed as a parameter,
12 * to remove the code's dependency on arc4random(), which isn't
13 * available on Linux.
14 */
15
16/*
17 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
18 * All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by Niels Provos.
31 * 4. The name of the author may not be used to endorse or promote products
32 * derived from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
35 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45
46/* This password hashing algorithm was designed by David Mazieres
47 * <dm@lcs.mit.edu> and works as follows:
48 *
49 * 1. state := InitState ()
50 * 2. state := ExpandKey (state, salt, password) 3.
51 * REPEAT rounds:
52 * state := ExpandKey (state, 0, salt)
53 * state := ExpandKey(state, 0, password)
54 * 4. ctext := "OrpheanBeholderScryDoubt"
55 * 5. REPEAT 64:
56 * ctext := Encrypt_ECB (state, ctext);
57 * 6. RETURN Concatenate (salt, ctext);
58 *
59 */
60
61#if 0
62#include <stdio.h>
63#endif
64
65#include <stdio.h>
66#include <stdlib.h>
67#include <sys/types.h>
68#include <string.h>
69#include "blf.h"
70#include "bcrypt.h"
71
72/* This implementation is adaptable to current computing power.
73 * You can have up to 2^31 rounds which should be enough for some
74 * time to come.
75 */
76
77static void encode_salt(char *, u_int8_t *, u_int16_t, u_int8_t);
78static void encode_base64(u_int8_t *, u_int8_t *, u_int16_t);
79static void decode_base64(u_int8_t *, u_int16_t, u_int8_t *);
80
81const static u_int8_t Base64Code[] =
82"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
83
84const static u_int8_t index_64[128] = {
85 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
86 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
87 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
88 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
89 255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
90 56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
91 255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
92 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
93 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
94 255, 255, 255, 255, 255, 255, 28, 29, 30,
95 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
96 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
97 51, 52, 53, 255, 255, 255, 255, 255
98};
99#define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)])
100
101static void
102decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data)
103{
104 u_int8_t *bp = buffer;
105 u_int8_t *p = data;
106 u_int8_t c1, c2, c3, c4;
107 while (bp < buffer + len) {
108 c1 = CHAR64(*p);
109 c2 = CHAR64(*(p + 1));
110
111 /* Invalid data */
112 if (c1 == 255 || c2 == 255)
113 break;
114
115 *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4);
116 if (bp >= buffer + len)
117 break;
118
119 c3 = CHAR64(*(p + 2));
120 if (c3 == 255)
121 break;
122
123 *bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
124 if (bp >= buffer + len)
125 break;
126
127 c4 = CHAR64(*(p + 3));
128 if (c4 == 255)
129 break;
130 *bp++ = ((c3 & 0x03) << 6) | c4;
131
132 p += 4;
133 }
134}
135
136static void
137encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr)
138{
139 salt[0] = '$';
140 salt[1] = BCRYPT_VERSION;
141 salt[2] = 'a';
142 salt[3] = '$';
143
144 snprintf(salt + 4, 4, "%2.2u$", logr);
145
146 encode_base64((u_int8_t *) salt + 7, csalt, clen);
147}
148/* Generates a salt for this version of crypt.
149 Since versions may change. Keeping this here
150 seems sensible.
151 */
152
153char *
154bcrypt_gensalt(char *output, u_int8_t log_rounds, u_int8_t *rseed)
155{
156 if (log_rounds < 4)
157 log_rounds = 4;
158 else if (log_rounds > 31)
159 log_rounds = 31;
160
161 encode_salt(output, rseed, BCRYPT_MAXSALT, log_rounds);
162 return output;
163}
164/* We handle $Vers$log2(NumRounds)$salt+passwd$
165 i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */
166
167char *
168bcrypt(char *output, const char *key, const char *salt)
169{
170 blf_ctx state;
171 u_int32_t rounds, i, k;
172 u_int16_t j;
173 u_int8_t key_len, salt_len, logr, minor;
174 u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OrpheanBeholderScryDoubt";
175 u_int8_t csalt[BCRYPT_MAXSALT];
176 u_int32_t cdata[BCRYPT_BLOCKS];
177 int n;
178
179 /* Discard "$" identifier */
180 salt++;
181
182 if (*salt > BCRYPT_VERSION) {
183 return NULL;
184 }
185
186 /* Check for minor versions */
187 if (salt[1] != '$') {
188 switch (salt[1]) {
189 case 'a':
190 /* 'ab' should not yield the same as 'abab' */
191 minor = salt[1];
192 salt++;
193 break;
194 default:
195 return NULL;
196 }
197 } else
198 minor = 0;
199
200 /* Discard version + "$" identifier */
201 salt += 2;
202
203 if (salt[2] != '$')
204 /* Out of sync with passwd entry */
205 return NULL;
206
207 /* Computer power doesn't increase linear, 2^x should be fine */
208 n = atoi(salt);
209 if (n > 31 || n < 0)
210 return NULL;
211 logr = (u_int8_t)n;
212 if ((rounds = (u_int32_t) 1 << logr) < BCRYPT_MINROUNDS)
213 return NULL;
214
215 /* Discard num rounds + "$" identifier */
216 salt += 3;
217
218 if (strlen(salt) * 3 / 4 < BCRYPT_MAXSALT)
219 return NULL;
220
221 /* We dont want the base64 salt but the raw data */
222 decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt);
223 salt_len = BCRYPT_MAXSALT;
224 key_len = strlen(key) + (minor >= 'a' ? 1 : 0);
225
226 /* Setting up S-Boxes and Subkeys */
227 Blowfish_initstate(&state);
228 Blowfish_expandstate(&state, csalt, salt_len,
229 (u_int8_t *) key, key_len);
230 for (k = 0; k < rounds; k++) {
231 Blowfish_expand0state(&state, (u_int8_t *) key, key_len);
232 Blowfish_expand0state(&state, csalt, salt_len);
233 }
234
235 /* This can be precomputed later */
236 j = 0;
237 for (i = 0; i < BCRYPT_BLOCKS; i++)
238 cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j);
239
240 /* Now do the encryption */
241 for (k = 0; k < 64; k++)
242 blf_enc(&state, cdata, BCRYPT_BLOCKS / 2);
243
244 for (i = 0; i < BCRYPT_BLOCKS; i++) {
245 ciphertext[4 * i + 3] = cdata[i] & 0xff;
246 cdata[i] = cdata[i] >> 8;
247 ciphertext[4 * i + 2] = cdata[i] & 0xff;
248 cdata[i] = cdata[i] >> 8;
249 ciphertext[4 * i + 1] = cdata[i] & 0xff;
250 cdata[i] = cdata[i] >> 8;
251 ciphertext[4 * i + 0] = cdata[i] & 0xff;
252 }
253
254
255 i = 0;
256 output[i++] = '$';
257 output[i++] = BCRYPT_VERSION;
258 if (minor)
259 output[i++] = minor;
260 output[i++] = '$';
261
262 snprintf(output + i, 4, "%2.2u$", logr);
263
264 encode_base64((u_int8_t *) output + i + 3, csalt, BCRYPT_MAXSALT);
265 encode_base64((u_int8_t *) output + strlen(output), ciphertext,
266 4 * BCRYPT_BLOCKS - 1);
267 return output;
268}
269
270static void
271encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
272{
273 u_int8_t *bp = buffer;
274 u_int8_t *p = data;
275 u_int8_t c1, c2;
276 while (p < data + len) {
277 c1 = *p++;
278 *bp++ = Base64Code[(c1 >> 2)];
279 c1 = (c1 & 0x03) << 4;
280 if (p >= data + len) {
281 *bp++ = Base64Code[c1];
282 break;
283 }
284 c2 = *p++;
285 c1 |= (c2 >> 4) & 0x0f;
286 *bp++ = Base64Code[c1];
287 c1 = (c2 & 0x0f) << 2;
288 if (p >= data + len) {
289 *bp++ = Base64Code[c1];
290 break;
291 }
292 c2 = *p++;
293 c1 |= (c2 >> 6) & 0x03;
294 *bp++ = Base64Code[c1];
295 *bp++ = Base64Code[c2 & 0x3f];
296 }
297 *bp = '\0';
298}
299#if 0
300void
301main()
302{
303 char blubber[73];
304 char salt[100];
305 char *p;
306 salt[0] = '$';
307 salt[1] = BCRYPT_VERSION;
308 salt[2] = '$';
309
310 snprintf(salt + 3, 4, "%2.2u$", 5);
311
312 printf("24 bytes of salt: ");
313 fgets(salt + 6, sizeof(salt) - 6, stdin);
314 salt[99] = 0;
315 printf("72 bytes of password: ");
316 fpurge(stdin);
317 fgets(blubber, sizeof(blubber), stdin);
318 blubber[72] = 0;
319
320 p = crypt(blubber, salt);
321 printf("Passwd entry: %s\n\n", p);
322
323 p = bcrypt_gensalt(5);
324 printf("Generated salt: %s\n", p);
325 p = crypt(blubber, p);
326 printf("Passwd entry: %s\n", p);
327}
328#endif
329

Built with git-ssb-web