bcrypt_pbkdf.c 5.4 KB

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