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