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