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