Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.3 KiB

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