hkdf.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * HKDF implementation -- RFC 5869
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #if defined(MBEDTLS_HKDF_C)
  9. #include <string.h>
  10. #include "mbedtls/hkdf.h"
  11. #include "mbedtls/platform_util.h"
  12. #include "mbedtls/error.h"
  13. int mbedtls_hkdf(const mbedtls_md_info_t *md, const unsigned char *salt,
  14. size_t salt_len, const unsigned char *ikm, size_t ikm_len,
  15. const unsigned char *info, size_t info_len,
  16. unsigned char *okm, size_t okm_len)
  17. {
  18. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  19. unsigned char prk[MBEDTLS_MD_MAX_SIZE];
  20. ret = mbedtls_hkdf_extract(md, salt, salt_len, ikm, ikm_len, prk);
  21. if (ret == 0) {
  22. ret = mbedtls_hkdf_expand(md, prk, mbedtls_md_get_size(md),
  23. info, info_len, okm, okm_len);
  24. }
  25. mbedtls_platform_zeroize(prk, sizeof(prk));
  26. return ret;
  27. }
  28. int mbedtls_hkdf_extract(const mbedtls_md_info_t *md,
  29. const unsigned char *salt, size_t salt_len,
  30. const unsigned char *ikm, size_t ikm_len,
  31. unsigned char *prk)
  32. {
  33. unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
  34. if (salt == NULL) {
  35. size_t hash_len;
  36. if (salt_len != 0) {
  37. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  38. }
  39. hash_len = mbedtls_md_get_size(md);
  40. if (hash_len == 0) {
  41. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  42. }
  43. salt = null_salt;
  44. salt_len = hash_len;
  45. }
  46. return mbedtls_md_hmac(md, salt, salt_len, ikm, ikm_len, prk);
  47. }
  48. int mbedtls_hkdf_expand(const mbedtls_md_info_t *md, const unsigned char *prk,
  49. size_t prk_len, const unsigned char *info,
  50. size_t info_len, unsigned char *okm, size_t okm_len)
  51. {
  52. size_t hash_len;
  53. size_t where = 0;
  54. size_t n;
  55. size_t t_len = 0;
  56. size_t i;
  57. int ret = 0;
  58. mbedtls_md_context_t ctx;
  59. unsigned char t[MBEDTLS_MD_MAX_SIZE];
  60. if (okm == NULL) {
  61. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  62. }
  63. hash_len = mbedtls_md_get_size(md);
  64. if (prk_len < hash_len || hash_len == 0) {
  65. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  66. }
  67. if (info == NULL) {
  68. info = (const unsigned char *) "";
  69. info_len = 0;
  70. }
  71. n = okm_len / hash_len;
  72. if (okm_len % hash_len != 0) {
  73. n++;
  74. }
  75. /*
  76. * Per RFC 5869 Section 2.3, okm_len must not exceed
  77. * 255 times the hash length
  78. */
  79. if (n > 255) {
  80. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  81. }
  82. mbedtls_md_init(&ctx);
  83. if ((ret = mbedtls_md_setup(&ctx, md, 1)) != 0) {
  84. goto exit;
  85. }
  86. memset(t, 0, hash_len);
  87. /*
  88. * Compute T = T(1) | T(2) | T(3) | ... | T(N)
  89. * Where T(N) is defined in RFC 5869 Section 2.3
  90. */
  91. for (i = 1; i <= n; i++) {
  92. size_t num_to_copy;
  93. unsigned char c = i & 0xff;
  94. ret = mbedtls_md_hmac_starts(&ctx, prk, prk_len);
  95. if (ret != 0) {
  96. goto exit;
  97. }
  98. ret = mbedtls_md_hmac_update(&ctx, t, t_len);
  99. if (ret != 0) {
  100. goto exit;
  101. }
  102. ret = mbedtls_md_hmac_update(&ctx, info, info_len);
  103. if (ret != 0) {
  104. goto exit;
  105. }
  106. /* The constant concatenated to the end of each T(n) is a single octet.
  107. * */
  108. ret = mbedtls_md_hmac_update(&ctx, &c, 1);
  109. if (ret != 0) {
  110. goto exit;
  111. }
  112. ret = mbedtls_md_hmac_finish(&ctx, t);
  113. if (ret != 0) {
  114. goto exit;
  115. }
  116. num_to_copy = i != n ? hash_len : okm_len - where;
  117. memcpy(okm + where, t, num_to_copy);
  118. where += hash_len;
  119. t_len = hash_len;
  120. }
  121. exit:
  122. mbedtls_md_free(&ctx);
  123. mbedtls_platform_zeroize(t, sizeof(t));
  124. return ret;
  125. }
  126. #endif /* MBEDTLS_HKDF_C */