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