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