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