1295367Sdes/* $OpenBSD: bcrypt_pbkdf.c,v 1.13 2015/01/12 03:20:04 tedu Exp $ */
2261287Sdes/*
3261287Sdes * Copyright (c) 2013 Ted Unangst <tedu@openbsd.org>
4261287Sdes *
5261287Sdes * Permission to use, copy, modify, and distribute this software for any
6261287Sdes * purpose with or without fee is hereby granted, provided that the above
7261287Sdes * copyright notice and this permission notice appear in all copies.
8261287Sdes *
9261287Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10261287Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11261287Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12261287Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13261287Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14261287Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15261287Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16261287Sdes */
17261287Sdes
18261287Sdes#include "includes.h"
19261287Sdes
20261287Sdes#ifndef HAVE_BCRYPT_PBKDF
21261287Sdes
22261287Sdes#include <sys/types.h>
23261287Sdes#include <sys/param.h>
24261287Sdes
25261287Sdes#ifdef HAVE_STDLIB_H
26261287Sdes# include <stdlib.h>
27261287Sdes#endif
28261287Sdes#include <string.h>
29261287Sdes
30261287Sdes#ifdef HAVE_BLF_H
31261287Sdes# include <blf.h>
32261287Sdes#endif
33261287Sdes
34261287Sdes#include "crypto_api.h"
35295367Sdes#ifdef SHA512_DIGEST_LENGTH
36295367Sdes# undef SHA512_DIGEST_LENGTH
37295367Sdes#endif
38261287Sdes#define SHA512_DIGEST_LENGTH crypto_hash_sha512_BYTES
39261287Sdes
40295367Sdes#define	MINIMUM(a,b) (((a) < (b)) ? (a) : (b))
41295367Sdes
42261287Sdes/*
43261287Sdes * pkcs #5 pbkdf2 implementation using the "bcrypt" hash
44261287Sdes *
45261287Sdes * The bcrypt hash function is derived from the bcrypt password hashing
46261287Sdes * function with the following modifications:
47261287Sdes * 1. The input password and salt are preprocessed with SHA512.
48261287Sdes * 2. The output length is expanded to 256 bits.
49261287Sdes * 3. Subsequently the magic string to be encrypted is lengthened and modifed
50261287Sdes *    to "OxychromaticBlowfishSwatDynamite"
51261287Sdes * 4. The hash function is defined to perform 64 rounds of initial state
52261287Sdes *    expansion. (More rounds are performed by iterating the hash.)
53261287Sdes *
54261287Sdes * Note that this implementation pulls the SHA512 operations into the caller
55261287Sdes * as a performance optimization.
56261287Sdes *
57261287Sdes * One modification from official pbkdf2. Instead of outputting key material
58261287Sdes * linearly, we mix it. pbkdf2 has a known weakness where if one uses it to
59295367Sdes * generate (e.g.) 512 bits of key material for use as two 256 bit keys, an
60295367Sdes * attacker can merely run once through the outer loop, but the user
61261287Sdes * always runs it twice. Shuffling output bytes requires computing the
62261287Sdes * entirety of the key material to assemble any subkey. This is something a
63261287Sdes * wise caller could do; we just do it for you.
64261287Sdes */
65261287Sdes
66295367Sdes#define BCRYPT_WORDS 8
67295367Sdes#define BCRYPT_HASHSIZE (BCRYPT_WORDS * 4)
68261287Sdes
69261287Sdesstatic void
70261287Sdesbcrypt_hash(u_int8_t *sha2pass, u_int8_t *sha2salt, u_int8_t *out)
71261287Sdes{
72261287Sdes	blf_ctx state;
73261287Sdes	u_int8_t ciphertext[BCRYPT_HASHSIZE] =
74261287Sdes	    "OxychromaticBlowfishSwatDynamite";
75295367Sdes	uint32_t cdata[BCRYPT_WORDS];
76261287Sdes	int i;
77261287Sdes	uint16_t j;
78261287Sdes	size_t shalen = SHA512_DIGEST_LENGTH;
79261287Sdes
80261287Sdes	/* key expansion */
81261287Sdes	Blowfish_initstate(&state);
82261287Sdes	Blowfish_expandstate(&state, sha2salt, shalen, sha2pass, shalen);
83261287Sdes	for (i = 0; i < 64; i++) {
84261287Sdes		Blowfish_expand0state(&state, sha2salt, shalen);
85261287Sdes		Blowfish_expand0state(&state, sha2pass, shalen);
86261287Sdes	}
87261287Sdes
88261287Sdes	/* encryption */
89261287Sdes	j = 0;
90295367Sdes	for (i = 0; i < BCRYPT_WORDS; i++)
91261287Sdes		cdata[i] = Blowfish_stream2word(ciphertext, sizeof(ciphertext),
92261287Sdes		    &j);
93261287Sdes	for (i = 0; i < 64; i++)
94261287Sdes		blf_enc(&state, cdata, sizeof(cdata) / sizeof(uint64_t));
95261287Sdes
96261287Sdes	/* copy out */
97295367Sdes	for (i = 0; i < BCRYPT_WORDS; i++) {
98261287Sdes		out[4 * i + 3] = (cdata[i] >> 24) & 0xff;
99261287Sdes		out[4 * i + 2] = (cdata[i] >> 16) & 0xff;
100261287Sdes		out[4 * i + 1] = (cdata[i] >> 8) & 0xff;
101261287Sdes		out[4 * i + 0] = cdata[i] & 0xff;
102261287Sdes	}
103261287Sdes
104261287Sdes	/* zap */
105295367Sdes	explicit_bzero(ciphertext, sizeof(ciphertext));
106295367Sdes	explicit_bzero(cdata, sizeof(cdata));
107295367Sdes	explicit_bzero(&state, sizeof(state));
108261287Sdes}
109261287Sdes
110261287Sdesint
111261287Sdesbcrypt_pbkdf(const char *pass, size_t passlen, const u_int8_t *salt, size_t saltlen,
112261287Sdes    u_int8_t *key, size_t keylen, unsigned int rounds)
113261287Sdes{
114261287Sdes	u_int8_t sha2pass[SHA512_DIGEST_LENGTH];
115261287Sdes	u_int8_t sha2salt[SHA512_DIGEST_LENGTH];
116261287Sdes	u_int8_t out[BCRYPT_HASHSIZE];
117261287Sdes	u_int8_t tmpout[BCRYPT_HASHSIZE];
118261287Sdes	u_int8_t *countsalt;
119261287Sdes	size_t i, j, amt, stride;
120261287Sdes	uint32_t count;
121295367Sdes	size_t origkeylen = keylen;
122261287Sdes
123261287Sdes	/* nothing crazy */
124261287Sdes	if (rounds < 1)
125261287Sdes		return -1;
126261287Sdes	if (passlen == 0 || saltlen == 0 || keylen == 0 ||
127261287Sdes	    keylen > sizeof(out) * sizeof(out) || saltlen > 1<<20)
128261287Sdes		return -1;
129261287Sdes	if ((countsalt = calloc(1, saltlen + 4)) == NULL)
130261287Sdes		return -1;
131261287Sdes	stride = (keylen + sizeof(out) - 1) / sizeof(out);
132261287Sdes	amt = (keylen + stride - 1) / stride;
133261287Sdes
134261287Sdes	memcpy(countsalt, salt, saltlen);
135261287Sdes
136261287Sdes	/* collapse password */
137261287Sdes	crypto_hash_sha512(sha2pass, pass, passlen);
138261287Sdes
139261287Sdes	/* generate key, sizeof(out) at a time */
140261287Sdes	for (count = 1; keylen > 0; count++) {
141261287Sdes		countsalt[saltlen + 0] = (count >> 24) & 0xff;
142261287Sdes		countsalt[saltlen + 1] = (count >> 16) & 0xff;
143261287Sdes		countsalt[saltlen + 2] = (count >> 8) & 0xff;
144261287Sdes		countsalt[saltlen + 3] = count & 0xff;
145261287Sdes
146261287Sdes		/* first round, salt is salt */
147261287Sdes		crypto_hash_sha512(sha2salt, countsalt, saltlen + 4);
148261287Sdes
149261287Sdes		bcrypt_hash(sha2pass, sha2salt, tmpout);
150261287Sdes		memcpy(out, tmpout, sizeof(out));
151261287Sdes
152261287Sdes		for (i = 1; i < rounds; i++) {
153261287Sdes			/* subsequent rounds, salt is previous output */
154261287Sdes			crypto_hash_sha512(sha2salt, tmpout, sizeof(tmpout));
155261287Sdes			bcrypt_hash(sha2pass, sha2salt, tmpout);
156261287Sdes			for (j = 0; j < sizeof(out); j++)
157261287Sdes				out[j] ^= tmpout[j];
158261287Sdes		}
159261287Sdes
160261287Sdes		/*
161295367Sdes		 * pbkdf2 deviation: output the key material non-linearly.
162261287Sdes		 */
163295367Sdes		amt = MINIMUM(amt, keylen);
164295367Sdes		for (i = 0; i < amt; i++) {
165295367Sdes			size_t dest = i * stride + (count - 1);
166295367Sdes			if (dest >= origkeylen)
167295367Sdes				break;
168295367Sdes			key[dest] = out[i];
169295367Sdes		}
170295367Sdes		keylen -= i;
171261287Sdes	}
172261287Sdes
173261287Sdes	/* zap */
174295367Sdes	explicit_bzero(out, sizeof(out));
175261287Sdes	free(countsalt);
176261287Sdes
177261287Sdes	return 0;
178261287Sdes}
179261287Sdes#endif /* HAVE_BCRYPT_PBKDF */
180