bcrypt_pbkdf.c revision 1.1 1 1.1 christos /* $OpenBSD: bcrypt_pbkdf.c,v 1.4 2013/07/29 00:55:53 tedu Exp $ */
2 1.1 christos /*
3 1.1 christos * Copyright (c) 2013 Ted Unangst <tedu (at) openbsd.org>
4 1.1 christos *
5 1.1 christos * Permission to use, copy, modify, and distribute this software for any
6 1.1 christos * purpose with or without fee is hereby granted, provided that the above
7 1.1 christos * copyright notice and this permission notice appear in all copies.
8 1.1 christos *
9 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 1.1 christos */
17 1.1 christos
18 1.1 christos #include "includes.h"
19 1.1 christos __RCSID("$NetBSD: bcrypt_pbkdf.c,v 1.1 2014/10/19 16:31:47 christos Exp $");
20 1.1 christos
21 1.1 christos #ifndef HAVE_BCRYPT_PBKDF
22 1.1 christos
23 1.1 christos #include <sys/types.h>
24 1.1 christos #include <sys/param.h>
25 1.1 christos
26 1.1 christos #ifdef HAVE_STDLIB_H
27 1.1 christos # include <stdlib.h>
28 1.1 christos #endif
29 1.1 christos #include <string.h>
30 1.1 christos
31 1.1 christos #ifdef HAVE_BLF_H
32 1.1 christos # include <blf.h>
33 1.1 christos #endif
34 1.1 christos
35 1.1 christos #include "crypto_api.h"
36 1.1 christos #include <stdio.h>
37 1.1 christos #include <time.h>
38 1.1 christos #include "misc.h"
39 1.1 christos #define SHA512_DIGEST_LENGTH crypto_hash_sha512_BYTES
40 1.1 christos
41 1.1 christos /*
42 1.1 christos * pkcs #5 pbkdf2 implementation using the "bcrypt" hash
43 1.1 christos *
44 1.1 christos * The bcrypt hash function is derived from the bcrypt password hashing
45 1.1 christos * function with the following modifications:
46 1.1 christos * 1. The input password and salt are preprocessed with SHA512.
47 1.1 christos * 2. The output length is expanded to 256 bits.
48 1.1 christos * 3. Subsequently the magic string to be encrypted is lengthened and modifed
49 1.1 christos * to "OxychromaticBlowfishSwatDynamite"
50 1.1 christos * 4. The hash function is defined to perform 64 rounds of initial state
51 1.1 christos * expansion. (More rounds are performed by iterating the hash.)
52 1.1 christos *
53 1.1 christos * Note that this implementation pulls the SHA512 operations into the caller
54 1.1 christos * as a performance optimization.
55 1.1 christos *
56 1.1 christos * One modification from official pbkdf2. Instead of outputting key material
57 1.1 christos * linearly, we mix it. pbkdf2 has a known weakness where if one uses it to
58 1.1 christos * generate (i.e.) 512 bits of key material for use as two 256 bit keys, an
59 1.1 christos * attacker can merely run once through the outer loop below, but the user
60 1.1 christos * always runs it twice. Shuffling output bytes requires computing the
61 1.1 christos * entirety of the key material to assemble any subkey. This is something a
62 1.1 christos * wise caller could do; we just do it for you.
63 1.1 christos */
64 1.1 christos
65 1.1 christos #define BCRYPT_BLOCKS 8
66 1.1 christos #define BCRYPT_HASHSIZE (BCRYPT_BLOCKS * 4)
67 1.1 christos
68 1.1 christos static void
69 1.1 christos bcrypt_hash(u_int8_t *sha2pass, u_int8_t *sha2salt, u_int8_t *out)
70 1.1 christos {
71 1.1 christos blf_ctx state;
72 1.1 christos u_int8_t ciphertext[BCRYPT_HASHSIZE] =
73 1.1 christos "OxychromaticBlowfishSwatDynamite";
74 1.1 christos uint32_t cdata[BCRYPT_BLOCKS];
75 1.1 christos int i;
76 1.1 christos uint16_t j;
77 1.1 christos size_t shalen = SHA512_DIGEST_LENGTH;
78 1.1 christos
79 1.1 christos /* key expansion */
80 1.1 christos Blowfish_initstate(&state);
81 1.1 christos Blowfish_expandstate(&state, sha2salt, shalen, sha2pass, shalen);
82 1.1 christos for (i = 0; i < 64; i++) {
83 1.1 christos Blowfish_expand0state(&state, sha2salt, shalen);
84 1.1 christos Blowfish_expand0state(&state, sha2pass, shalen);
85 1.1 christos }
86 1.1 christos
87 1.1 christos /* encryption */
88 1.1 christos j = 0;
89 1.1 christos for (i = 0; i < BCRYPT_BLOCKS; i++)
90 1.1 christos cdata[i] = Blowfish_stream2word(ciphertext, sizeof(ciphertext),
91 1.1 christos &j);
92 1.1 christos for (i = 0; i < 64; i++)
93 1.1 christos blf_enc(&state, cdata, sizeof(cdata) / sizeof(uint64_t));
94 1.1 christos
95 1.1 christos /* copy out */
96 1.1 christos for (i = 0; i < BCRYPT_BLOCKS; i++) {
97 1.1 christos out[4 * i + 3] = (cdata[i] >> 24) & 0xff;
98 1.1 christos out[4 * i + 2] = (cdata[i] >> 16) & 0xff;
99 1.1 christos out[4 * i + 1] = (cdata[i] >> 8) & 0xff;
100 1.1 christos out[4 * i + 0] = cdata[i] & 0xff;
101 1.1 christos }
102 1.1 christos
103 1.1 christos /* zap */
104 1.1 christos memset(ciphertext, 0, sizeof(ciphertext));
105 1.1 christos memset(cdata, 0, sizeof(cdata));
106 1.1 christos memset(&state, 0, sizeof(state));
107 1.1 christos }
108 1.1 christos
109 1.1 christos int
110 1.1 christos bcrypt_pbkdf(const char *pass, size_t passlen, const u_int8_t *salt, size_t saltlen,
111 1.1 christos u_int8_t *key, size_t keylen, unsigned int rounds)
112 1.1 christos {
113 1.1 christos u_int8_t sha2pass[SHA512_DIGEST_LENGTH];
114 1.1 christos u_int8_t sha2salt[SHA512_DIGEST_LENGTH];
115 1.1 christos u_int8_t out[BCRYPT_HASHSIZE];
116 1.1 christos u_int8_t tmpout[BCRYPT_HASHSIZE];
117 1.1 christos u_int8_t *countsalt;
118 1.1 christos size_t i, j, amt, stride;
119 1.1 christos uint32_t count;
120 1.1 christos
121 1.1 christos /* nothing crazy */
122 1.1 christos if (rounds < 1)
123 1.1 christos return -1;
124 1.1 christos if (passlen == 0 || saltlen == 0 || keylen == 0 ||
125 1.1 christos keylen > sizeof(out) * sizeof(out) || saltlen > 1<<20)
126 1.1 christos return -1;
127 1.1 christos if ((countsalt = calloc(1, saltlen + 4)) == NULL)
128 1.1 christos return -1;
129 1.1 christos stride = (keylen + sizeof(out) - 1) / sizeof(out);
130 1.1 christos amt = (keylen + stride - 1) / stride;
131 1.1 christos
132 1.1 christos memcpy(countsalt, salt, saltlen);
133 1.1 christos
134 1.1 christos /* collapse password */
135 1.1 christos crypto_hash_sha512(sha2pass, (const u_char *)pass, passlen);
136 1.1 christos
137 1.1 christos /* generate key, sizeof(out) at a time */
138 1.1 christos for (count = 1; keylen > 0; count++) {
139 1.1 christos countsalt[saltlen + 0] = (count >> 24) & 0xff;
140 1.1 christos countsalt[saltlen + 1] = (count >> 16) & 0xff;
141 1.1 christos countsalt[saltlen + 2] = (count >> 8) & 0xff;
142 1.1 christos countsalt[saltlen + 3] = count & 0xff;
143 1.1 christos
144 1.1 christos /* first round, salt is salt */
145 1.1 christos crypto_hash_sha512(sha2salt, countsalt, saltlen + 4);
146 1.1 christos
147 1.1 christos bcrypt_hash(sha2pass, sha2salt, tmpout);
148 1.1 christos memcpy(out, tmpout, sizeof(out));
149 1.1 christos
150 1.1 christos for (i = 1; i < rounds; i++) {
151 1.1 christos /* subsequent rounds, salt is previous output */
152 1.1 christos crypto_hash_sha512(sha2salt, tmpout, sizeof(tmpout));
153 1.1 christos bcrypt_hash(sha2pass, sha2salt, tmpout);
154 1.1 christos for (j = 0; j < sizeof(out); j++)
155 1.1 christos out[j] ^= tmpout[j];
156 1.1 christos }
157 1.1 christos
158 1.1 christos /*
159 1.1 christos * pbkdf2 deviation: ouput the key material non-linearly.
160 1.1 christos */
161 1.1 christos amt = MIN(amt, keylen);
162 1.1 christos for (i = 0; i < amt; i++)
163 1.1 christos key[i * stride + (count - 1)] = out[i];
164 1.1 christos keylen -= amt;
165 1.1 christos }
166 1.1 christos
167 1.1 christos /* zap */
168 1.1 christos memset(out, 0, sizeof(out));
169 1.1 christos memset(countsalt, 0, saltlen + 4);
170 1.1 christos free(countsalt);
171 1.1 christos
172 1.1 christos return 0;
173 1.1 christos }
174 1.1 christos #endif /* HAVE_BCRYPT_PBKDF */
175