Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

113 righe
3.3 KiB

  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/hkdf.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <openssl/err.h>
  18. #include <openssl/hmac.h>
  19. #include "../internal.h"
  20. int HKDF(uint8_t *out_key, size_t out_len, const EVP_MD *digest,
  21. const uint8_t *secret, size_t secret_len, const uint8_t *salt,
  22. size_t salt_len, const uint8_t *info, size_t info_len) {
  23. // https://tools.ietf.org/html/rfc5869#section-2
  24. uint8_t prk[EVP_MAX_MD_SIZE];
  25. size_t prk_len;
  26. if (!HKDF_extract(prk, &prk_len, digest, secret, secret_len, salt,
  27. salt_len) ||
  28. !HKDF_expand(out_key, out_len, digest, prk, prk_len, info, info_len)) {
  29. return 0;
  30. }
  31. return 1;
  32. }
  33. int HKDF_extract(uint8_t *out_key, size_t *out_len, const EVP_MD *digest,
  34. const uint8_t *secret, size_t secret_len, const uint8_t *salt,
  35. size_t salt_len) {
  36. // https://tools.ietf.org/html/rfc5869#section-2.2
  37. // If salt is not given, HashLength zeros are used. However, HMAC does that
  38. // internally already so we can ignore it.
  39. unsigned len;
  40. if (HMAC(digest, salt, salt_len, secret, secret_len, out_key, &len) == NULL) {
  41. OPENSSL_PUT_ERROR(HKDF, ERR_R_HMAC_LIB);
  42. return 0;
  43. }
  44. *out_len = len;
  45. assert(*out_len == EVP_MD_size(digest));
  46. return 1;
  47. }
  48. int HKDF_expand(uint8_t *out_key, size_t out_len, const EVP_MD *digest,
  49. const uint8_t *prk, size_t prk_len, const uint8_t *info,
  50. size_t info_len) {
  51. // https://tools.ietf.org/html/rfc5869#section-2.3
  52. const size_t digest_len = EVP_MD_size(digest);
  53. uint8_t previous[EVP_MAX_MD_SIZE];
  54. size_t n, done = 0;
  55. unsigned i;
  56. int ret = 0;
  57. HMAC_CTX hmac;
  58. // Expand key material to desired length.
  59. n = (out_len + digest_len - 1) / digest_len;
  60. if (out_len + digest_len < out_len || n > 255) {
  61. OPENSSL_PUT_ERROR(HKDF, HKDF_R_OUTPUT_TOO_LARGE);
  62. return 0;
  63. }
  64. HMAC_CTX_init(&hmac);
  65. if (!HMAC_Init_ex(&hmac, prk, prk_len, digest, NULL)) {
  66. goto out;
  67. }
  68. for (i = 0; i < n; i++) {
  69. uint8_t ctr = i + 1;
  70. size_t todo;
  71. if (i != 0 && (!HMAC_Init_ex(&hmac, NULL, 0, NULL, NULL) ||
  72. !HMAC_Update(&hmac, previous, digest_len))) {
  73. goto out;
  74. }
  75. if (!HMAC_Update(&hmac, info, info_len) ||
  76. !HMAC_Update(&hmac, &ctr, 1) ||
  77. !HMAC_Final(&hmac, previous, NULL)) {
  78. goto out;
  79. }
  80. todo = digest_len;
  81. if (done + todo > out_len) {
  82. todo = out_len - done;
  83. }
  84. OPENSSL_memcpy(out_key + done, previous, todo);
  85. done += todo;
  86. }
  87. ret = 1;
  88. out:
  89. HMAC_CTX_cleanup(&hmac);
  90. if (ret != 1) {
  91. OPENSSL_PUT_ERROR(HKDF, ERR_R_HMAC_LIB);
  92. }
  93. return ret;
  94. }