summaryrefslogtreecommitdiff
path: root/openbsd-compat/bcrypt_pbkdf.c
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/bcrypt_pbkdf.c')
-rw-r--r--openbsd-compat/bcrypt_pbkdf.c29
1 files changed, 18 insertions, 11 deletions
diff --git a/openbsd-compat/bcrypt_pbkdf.c b/openbsd-compat/bcrypt_pbkdf.c
index 91b6ba07b..16912575a 100644
--- a/openbsd-compat/bcrypt_pbkdf.c
+++ b/openbsd-compat/bcrypt_pbkdf.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bcrypt_pbkdf.c,v 1.4 2013/07/29 00:55:53 tedu Exp $ */ 1/* $OpenBSD: bcrypt_pbkdf.c,v 1.9 2014/07/13 21:21:25 tedu Exp $ */
2/* 2/*
3 * Copyright (c) 2013 Ted Unangst <tedu@openbsd.org> 3 * Copyright (c) 2013 Ted Unangst <tedu@openbsd.org>
4 * 4 *
@@ -32,6 +32,9 @@
32#endif 32#endif
33 33
34#include "crypto_api.h" 34#include "crypto_api.h"
35#ifdef SHA512_DIGEST_LENGTH
36# undef SHA512_DIGEST_LENGTH
37#endif
35#define SHA512_DIGEST_LENGTH crypto_hash_sha512_BYTES 38#define SHA512_DIGEST_LENGTH crypto_hash_sha512_BYTES
36 39
37/* 40/*
@@ -51,8 +54,8 @@
51 * 54 *
52 * One modification from official pbkdf2. Instead of outputting key material 55 * One modification from official pbkdf2. Instead of outputting key material
53 * linearly, we mix it. pbkdf2 has a known weakness where if one uses it to 56 * linearly, we mix it. pbkdf2 has a known weakness where if one uses it to
54 * generate (i.e.) 512 bits of key material for use as two 256 bit keys, an 57 * generate (e.g.) 512 bits of key material for use as two 256 bit keys, an
55 * attacker can merely run once through the outer loop below, but the user 58 * attacker can merely run once through the outer loop, but the user
56 * always runs it twice. Shuffling output bytes requires computing the 59 * always runs it twice. Shuffling output bytes requires computing the
57 * entirety of the key material to assemble any subkey. This is something a 60 * entirety of the key material to assemble any subkey. This is something a
58 * wise caller could do; we just do it for you. 61 * wise caller could do; we just do it for you.
@@ -97,9 +100,9 @@ bcrypt_hash(u_int8_t *sha2pass, u_int8_t *sha2salt, u_int8_t *out)
97 } 100 }
98 101
99 /* zap */ 102 /* zap */
100 memset(ciphertext, 0, sizeof(ciphertext)); 103 explicit_bzero(ciphertext, sizeof(ciphertext));
101 memset(cdata, 0, sizeof(cdata)); 104 explicit_bzero(cdata, sizeof(cdata));
102 memset(&state, 0, sizeof(state)); 105 explicit_bzero(&state, sizeof(state));
103} 106}
104 107
105int 108int
@@ -113,6 +116,7 @@ bcrypt_pbkdf(const char *pass, size_t passlen, const u_int8_t *salt, size_t salt
113 u_int8_t *countsalt; 116 u_int8_t *countsalt;
114 size_t i, j, amt, stride; 117 size_t i, j, amt, stride;
115 uint32_t count; 118 uint32_t count;
119 size_t origkeylen = keylen;
116 120
117 /* nothing crazy */ 121 /* nothing crazy */
118 if (rounds < 1) 122 if (rounds < 1)
@@ -155,14 +159,17 @@ bcrypt_pbkdf(const char *pass, size_t passlen, const u_int8_t *salt, size_t salt
155 * pbkdf2 deviation: ouput the key material non-linearly. 159 * pbkdf2 deviation: ouput the key material non-linearly.
156 */ 160 */
157 amt = MIN(amt, keylen); 161 amt = MIN(amt, keylen);
158 for (i = 0; i < amt; i++) 162 for (i = 0; i < amt; i++) {
159 key[i * stride + (count - 1)] = out[i]; 163 size_t dest = i * stride + (count - 1);
160 keylen -= amt; 164 if (dest >= origkeylen)
165 break;
166 key[dest] = out[i];
167 }
168 keylen -= i;
161 } 169 }
162 170
163 /* zap */ 171 /* zap */
164 memset(out, 0, sizeof(out)); 172 explicit_bzero(out, sizeof(out));
165 memset(countsalt, 0, saltlen + 4);
166 free(countsalt); 173 free(countsalt);
167 174
168 return 0; 175 return 0;