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