bcrypt_pbkdf.c revision 1.5 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.5 2026/04/09 06:36:39 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.5 christos #ifndef SHA512_DIGEST_LENGTH
40 1.1 christos #define SHA512_DIGEST_LENGTH crypto_hash_sha512_BYTES
41 1.5 christos #endif
42 1.1 christos
43 1.1 christos /*
44 1.1 christos * pkcs #5 pbkdf2 implementation using the "bcrypt" hash
45 1.1 christos *
46 1.1 christos * The bcrypt hash function is derived from the bcrypt password hashing
47 1.1 christos * function with the following modifications:
48 1.1 christos * 1. The input password and salt are preprocessed with SHA512.
49 1.1 christos * 2. The output length is expanded to 256 bits.
50 1.1 christos * 3. Subsequently the magic string to be encrypted is lengthened and modifed
51 1.1 christos * to "OxychromaticBlowfishSwatDynamite"
52 1.1 christos * 4. The hash function is defined to perform 64 rounds of initial state
53 1.1 christos * expansion. (More rounds are performed by iterating the hash.)
54 1.1 christos *
55 1.1 christos * Note that this implementation pulls the SHA512 operations into the caller
56 1.1 christos * as a performance optimization.
57 1.1 christos *
58 1.1 christos * One modification from official pbkdf2. Instead of outputting key material
59 1.1 christos * linearly, we mix it. pbkdf2 has a known weakness where if one uses it to
60 1.1 christos * generate (i.e.) 512 bits of key material for use as two 256 bit keys, an
61 1.1 christos * attacker can merely run once through the outer loop below, but the user
62 1.1 christos * always runs it twice. Shuffling output bytes requires computing the
63 1.1 christos * entirety of the key material to assemble any subkey. This is something a
64 1.1 christos * wise caller could do; we just do it for you.
65 1.1 christos */
66 1.1 christos
67 1.1 christos #define BCRYPT_BLOCKS 8
68 1.1 christos #define BCRYPT_HASHSIZE (BCRYPT_BLOCKS * 4)
69 1.1 christos
70 1.1 christos static void
71 1.1 christos bcrypt_hash(u_int8_t *sha2pass, u_int8_t *sha2salt, u_int8_t *out)
72 1.1 christos {
73 1.1 christos blf_ctx state;
74 1.1 christos u_int8_t ciphertext[BCRYPT_HASHSIZE] =
75 1.1 christos "OxychromaticBlowfishSwatDynamite";
76 1.1 christos uint32_t cdata[BCRYPT_BLOCKS];
77 1.1 christos int i;
78 1.1 christos uint16_t j;
79 1.1 christos size_t shalen = SHA512_DIGEST_LENGTH;
80 1.1 christos
81 1.1 christos /* key expansion */
82 1.1 christos Blowfish_initstate(&state);
83 1.1 christos Blowfish_expandstate(&state, sha2salt, shalen, sha2pass, shalen);
84 1.1 christos for (i = 0; i < 64; i++) {
85 1.1 christos Blowfish_expand0state(&state, sha2salt, shalen);
86 1.1 christos Blowfish_expand0state(&state, sha2pass, shalen);
87 1.1 christos }
88 1.1 christos
89 1.1 christos /* encryption */
90 1.1 christos j = 0;
91 1.1 christos for (i = 0; i < BCRYPT_BLOCKS; i++)
92 1.1 christos cdata[i] = Blowfish_stream2word(ciphertext, sizeof(ciphertext),
93 1.1 christos &j);
94 1.1 christos for (i = 0; i < 64; i++)
95 1.1 christos blf_enc(&state, cdata, sizeof(cdata) / sizeof(uint64_t));
96 1.1 christos
97 1.1 christos /* copy out */
98 1.1 christos for (i = 0; i < BCRYPT_BLOCKS; i++) {
99 1.1 christos out[4 * i + 3] = (cdata[i] >> 24) & 0xff;
100 1.1 christos out[4 * i + 2] = (cdata[i] >> 16) & 0xff;
101 1.1 christos out[4 * i + 1] = (cdata[i] >> 8) & 0xff;
102 1.1 christos out[4 * i + 0] = cdata[i] & 0xff;
103 1.1 christos }
104 1.1 christos
105 1.1 christos /* zap */
106 1.1 christos memset(ciphertext, 0, sizeof(ciphertext));
107 1.1 christos memset(cdata, 0, sizeof(cdata));
108 1.1 christos memset(&state, 0, sizeof(state));
109 1.1 christos }
110 1.1 christos
111 1.1 christos int
112 1.1 christos bcrypt_pbkdf(const char *pass, size_t passlen, const u_int8_t *salt, size_t saltlen,
113 1.1 christos u_int8_t *key, size_t keylen, unsigned int rounds)
114 1.1 christos {
115 1.1 christos u_int8_t sha2pass[SHA512_DIGEST_LENGTH];
116 1.1 christos u_int8_t sha2salt[SHA512_DIGEST_LENGTH];
117 1.1 christos u_int8_t out[BCRYPT_HASHSIZE];
118 1.1 christos u_int8_t tmpout[BCRYPT_HASHSIZE];
119 1.1 christos u_int8_t *countsalt;
120 1.1 christos size_t i, j, amt, stride;
121 1.1 christos uint32_t count;
122 1.1 christos
123 1.1 christos /* nothing crazy */
124 1.1 christos if (rounds < 1)
125 1.1 christos return -1;
126 1.1 christos if (passlen == 0 || saltlen == 0 || keylen == 0 ||
127 1.1 christos keylen > sizeof(out) * sizeof(out) || saltlen > 1<<20)
128 1.1 christos return -1;
129 1.1 christos if ((countsalt = calloc(1, saltlen + 4)) == NULL)
130 1.1 christos return -1;
131 1.1 christos stride = (keylen + sizeof(out) - 1) / sizeof(out);
132 1.1 christos amt = (keylen + stride - 1) / stride;
133 1.1 christos
134 1.1 christos memcpy(countsalt, salt, saltlen);
135 1.1 christos
136 1.1 christos /* collapse password */
137 1.1 christos crypto_hash_sha512(sha2pass, (const u_char *)pass, passlen);
138 1.1 christos
139 1.1 christos /* generate key, sizeof(out) at a time */
140 1.1 christos for (count = 1; keylen > 0; count++) {
141 1.1 christos countsalt[saltlen + 0] = (count >> 24) & 0xff;
142 1.1 christos countsalt[saltlen + 1] = (count >> 16) & 0xff;
143 1.1 christos countsalt[saltlen + 2] = (count >> 8) & 0xff;
144 1.1 christos countsalt[saltlen + 3] = count & 0xff;
145 1.1 christos
146 1.1 christos /* first round, salt is salt */
147 1.1 christos crypto_hash_sha512(sha2salt, countsalt, saltlen + 4);
148 1.1 christos
149 1.1 christos bcrypt_hash(sha2pass, sha2salt, tmpout);
150 1.1 christos memcpy(out, tmpout, sizeof(out));
151 1.1 christos
152 1.1 christos for (i = 1; i < rounds; i++) {
153 1.1 christos /* subsequent rounds, salt is previous output */
154 1.1 christos crypto_hash_sha512(sha2salt, tmpout, sizeof(tmpout));
155 1.1 christos bcrypt_hash(sha2pass, sha2salt, tmpout);
156 1.1 christos for (j = 0; j < sizeof(out); j++)
157 1.1 christos out[j] ^= tmpout[j];
158 1.1 christos }
159 1.1 christos
160 1.1 christos /*
161 1.1 christos * pbkdf2 deviation: ouput the key material non-linearly.
162 1.1 christos */
163 1.1 christos amt = MIN(amt, keylen);
164 1.1 christos for (i = 0; i < amt; i++)
165 1.1 christos key[i * stride + (count - 1)] = out[i];
166 1.1 christos keylen -= amt;
167 1.1 christos }
168 1.1 christos
169 1.1 christos /* zap */
170 1.1 christos memset(out, 0, sizeof(out));
171 1.1 christos memset(countsalt, 0, saltlen + 4);
172 1.1 christos free(countsalt);
173 1.1 christos
174 1.1 christos return 0;
175 1.1 christos }
176 1.1 christos #endif /* HAVE_BCRYPT_PBKDF */
177