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