1*2c53affbSjmc /* $OpenBSD: bcrypt_pbkdf.c,v 1.17 2022/12/27 17:10:08 jmc Exp $ */
2410ca83dStedu /*
3410ca83dStedu * Copyright (c) 2013 Ted Unangst <tedu@openbsd.org>
4410ca83dStedu *
5410ca83dStedu * Permission to use, copy, modify, and distribute this software for any
6410ca83dStedu * purpose with or without fee is hereby granted, provided that the above
7410ca83dStedu * copyright notice and this permission notice appear in all copies.
8410ca83dStedu *
9410ca83dStedu * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10410ca83dStedu * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11410ca83dStedu * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12410ca83dStedu * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13410ca83dStedu * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14410ca83dStedu * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15410ca83dStedu * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16410ca83dStedu */
17410ca83dStedu
185051f9a6Sderaadt #include <sys/types.h>
19410ca83dStedu
20b283bc04Stedu #include <stdint.h>
21410ca83dStedu #include <stdlib.h>
22410ca83dStedu #include <blf.h>
23410ca83dStedu #include <sha2.h>
24410ca83dStedu #include <string.h>
25410ca83dStedu #include <util.h>
26410ca83dStedu
271baefdb0Sderaadt #define MINIMUM(a,b) (((a) < (b)) ? (a) : (b))
281baefdb0Sderaadt
29410ca83dStedu /*
30410ca83dStedu * pkcs #5 pbkdf2 implementation using the "bcrypt" hash
31410ca83dStedu *
32410ca83dStedu * The bcrypt hash function is derived from the bcrypt password hashing
33410ca83dStedu * function with the following modifications:
34410ca83dStedu * 1. The input password and salt are preprocessed with SHA512.
35410ca83dStedu * 2. The output length is expanded to 256 bits.
36*2c53affbSjmc * 3. Subsequently the magic string to be encrypted is lengthened and modified
37410ca83dStedu * to "OxychromaticBlowfishSwatDynamite"
38410ca83dStedu * 4. The hash function is defined to perform 64 rounds of initial state
39410ca83dStedu * expansion. (More rounds are performed by iterating the hash.)
40410ca83dStedu *
41410ca83dStedu * Note that this implementation pulls the SHA512 operations into the caller
42410ca83dStedu * as a performance optimization.
43410ca83dStedu *
44410ca83dStedu * One modification from official pbkdf2. Instead of outputting key material
45410ca83dStedu * linearly, we mix it. pbkdf2 has a known weakness where if one uses it to
46248b7c42Stedu * generate (e.g.) 512 bits of key material for use as two 256 bit keys, an
47248b7c42Stedu * attacker can merely run once through the outer loop, but the user
48410ca83dStedu * always runs it twice. Shuffling output bytes requires computing the
49410ca83dStedu * entirety of the key material to assemble any subkey. This is something a
50410ca83dStedu * wise caller could do; we just do it for you.
51410ca83dStedu */
52410ca83dStedu
53c6555c4fStedu #define BCRYPT_WORDS 8
54c6555c4fStedu #define BCRYPT_HASHSIZE (BCRYPT_WORDS * 4)
55410ca83dStedu
56410ca83dStedu static void
bcrypt_hash(uint8_t * sha2pass,uint8_t * sha2salt,uint8_t * out)57d4a1d777Stedu bcrypt_hash(uint8_t *sha2pass, uint8_t *sha2salt, uint8_t *out)
58410ca83dStedu {
59410ca83dStedu blf_ctx state;
60410ca83dStedu uint8_t ciphertext[BCRYPT_HASHSIZE] =
61410ca83dStedu "OxychromaticBlowfishSwatDynamite";
62c6555c4fStedu uint32_t cdata[BCRYPT_WORDS];
63410ca83dStedu int i;
64410ca83dStedu uint16_t j;
65d4a1d777Stedu size_t shalen = SHA512_DIGEST_LENGTH;
66410ca83dStedu
67410ca83dStedu /* key expansion */
68410ca83dStedu Blowfish_initstate(&state);
69d4a1d777Stedu Blowfish_expandstate(&state, sha2salt, shalen, sha2pass, shalen);
70410ca83dStedu for (i = 0; i < 64; i++) {
71d4a1d777Stedu Blowfish_expand0state(&state, sha2salt, shalen);
72d4a1d777Stedu Blowfish_expand0state(&state, sha2pass, shalen);
73410ca83dStedu }
74410ca83dStedu
75410ca83dStedu /* encryption */
76410ca83dStedu j = 0;
77c6555c4fStedu for (i = 0; i < BCRYPT_WORDS; i++)
78410ca83dStedu cdata[i] = Blowfish_stream2word(ciphertext, sizeof(ciphertext),
79410ca83dStedu &j);
80410ca83dStedu for (i = 0; i < 64; i++)
81278b6199Stb blf_enc(&state, cdata, BCRYPT_WORDS / 2);
82410ca83dStedu
83410ca83dStedu /* copy out */
84c6555c4fStedu for (i = 0; i < BCRYPT_WORDS; i++) {
85410ca83dStedu out[4 * i + 3] = (cdata[i] >> 24) & 0xff;
86410ca83dStedu out[4 * i + 2] = (cdata[i] >> 16) & 0xff;
87410ca83dStedu out[4 * i + 1] = (cdata[i] >> 8) & 0xff;
88410ca83dStedu out[4 * i + 0] = cdata[i] & 0xff;
89410ca83dStedu }
90410ca83dStedu
91410ca83dStedu /* zap */
92ba958d37Stedu explicit_bzero(ciphertext, sizeof(ciphertext));
93ba958d37Stedu explicit_bzero(cdata, sizeof(cdata));
94ba958d37Stedu explicit_bzero(&state, sizeof(state));
95410ca83dStedu }
96410ca83dStedu
97410ca83dStedu int
bcrypt_pbkdf(const char * pass,size_t passlen,const uint8_t * salt,size_t saltlen,uint8_t * key,size_t keylen,unsigned int rounds)98410ca83dStedu bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt, size_t saltlen,
99410ca83dStedu uint8_t *key, size_t keylen, unsigned int rounds)
100410ca83dStedu {
101410ca83dStedu SHA2_CTX ctx;
102410ca83dStedu uint8_t sha2pass[SHA512_DIGEST_LENGTH];
103410ca83dStedu uint8_t sha2salt[SHA512_DIGEST_LENGTH];
104410ca83dStedu uint8_t out[BCRYPT_HASHSIZE];
105410ca83dStedu uint8_t tmpout[BCRYPT_HASHSIZE];
106410ca83dStedu uint8_t countsalt[4];
107410ca83dStedu size_t i, j, amt, stride;
108410ca83dStedu uint32_t count;
1097fb87b87Stedu size_t origkeylen = keylen;
110410ca83dStedu
111410ca83dStedu /* nothing crazy */
1126eea5450Stedu if (rounds < 1)
1130912caf5Stedu goto bad;
114410ca83dStedu if (passlen == 0 || saltlen == 0 || keylen == 0 ||
115410ca83dStedu keylen > sizeof(out) * sizeof(out))
1160912caf5Stedu goto bad;
117410ca83dStedu stride = (keylen + sizeof(out) - 1) / sizeof(out);
118410ca83dStedu amt = (keylen + stride - 1) / stride;
119410ca83dStedu
120410ca83dStedu /* collapse password */
121410ca83dStedu SHA512Init(&ctx);
122410ca83dStedu SHA512Update(&ctx, pass, passlen);
123410ca83dStedu SHA512Final(sha2pass, &ctx);
124410ca83dStedu
125410ca83dStedu
126410ca83dStedu /* generate key, sizeof(out) at a time */
127410ca83dStedu for (count = 1; keylen > 0; count++) {
128410ca83dStedu countsalt[0] = (count >> 24) & 0xff;
129410ca83dStedu countsalt[1] = (count >> 16) & 0xff;
130410ca83dStedu countsalt[2] = (count >> 8) & 0xff;
131410ca83dStedu countsalt[3] = count & 0xff;
132410ca83dStedu
133410ca83dStedu /* first round, salt is salt */
134410ca83dStedu SHA512Init(&ctx);
135410ca83dStedu SHA512Update(&ctx, salt, saltlen);
136410ca83dStedu SHA512Update(&ctx, countsalt, sizeof(countsalt));
137410ca83dStedu SHA512Final(sha2salt, &ctx);
138410ca83dStedu bcrypt_hash(sha2pass, sha2salt, tmpout);
139410ca83dStedu memcpy(out, tmpout, sizeof(out));
140410ca83dStedu
141410ca83dStedu for (i = 1; i < rounds; i++) {
142410ca83dStedu /* subsequent rounds, salt is previous output */
143410ca83dStedu SHA512Init(&ctx);
144410ca83dStedu SHA512Update(&ctx, tmpout, sizeof(tmpout));
145410ca83dStedu SHA512Final(sha2salt, &ctx);
146410ca83dStedu bcrypt_hash(sha2pass, sha2salt, tmpout);
147410ca83dStedu for (j = 0; j < sizeof(out); j++)
148410ca83dStedu out[j] ^= tmpout[j];
149410ca83dStedu }
150410ca83dStedu
151410ca83dStedu /*
15213918886Sdjm * pbkdf2 deviation: output the key material non-linearly.
153410ca83dStedu */
1541baefdb0Sderaadt amt = MINIMUM(amt, keylen);
1557fb87b87Stedu for (i = 0; i < amt; i++) {
1567fb87b87Stedu size_t dest = i * stride + (count - 1);
1577fb87b87Stedu if (dest >= origkeylen)
1587fb87b87Stedu break;
1597fb87b87Stedu key[dest] = out[i];
1607fb87b87Stedu }
1617fb87b87Stedu keylen -= i;
162410ca83dStedu }
163410ca83dStedu
164410ca83dStedu /* zap */
165ba958d37Stedu explicit_bzero(&ctx, sizeof(ctx));
166ba958d37Stedu explicit_bzero(out, sizeof(out));
1674bc7f070Stedu explicit_bzero(tmpout, sizeof(tmpout));
168410ca83dStedu
169410ca83dStedu return 0;
1700912caf5Stedu
1710912caf5Stedu bad:
1720912caf5Stedu /* overwrite with random in case caller doesn't check return code */
1730912caf5Stedu arc4random_buf(key, keylen);
1740912caf5Stedu return -1;
175410ca83dStedu }
176