hkdf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * HKDF implementation -- RFC 5869
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_HKDF_C)
  21. #include <string.h>
  22. #include "mbedtls/hkdf.h"
  23. #include "mbedtls/platform_util.h"
  24. #include "mbedtls/error.h"
  25. int mbedtls_hkdf(const mbedtls_md_info_t *md, const unsigned char *salt,
  26. size_t salt_len, const unsigned char *ikm, size_t ikm_len,
  27. const unsigned char *info, size_t info_len,
  28. unsigned char *okm, size_t okm_len)
  29. {
  30. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  31. unsigned char prk[MBEDTLS_MD_MAX_SIZE];
  32. ret = mbedtls_hkdf_extract(md, salt, salt_len, ikm, ikm_len, prk);
  33. if (ret == 0) {
  34. ret = mbedtls_hkdf_expand(md, prk, mbedtls_md_get_size(md),
  35. info, info_len, okm, okm_len);
  36. }
  37. mbedtls_platform_zeroize(prk, sizeof(prk));
  38. return ret;
  39. }
  40. int mbedtls_hkdf_extract(const mbedtls_md_info_t *md,
  41. const unsigned char *salt, size_t salt_len,
  42. const unsigned char *ikm, size_t ikm_len,
  43. unsigned char *prk)
  44. {
  45. unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
  46. if (salt == NULL) {
  47. size_t hash_len;
  48. if (salt_len != 0) {
  49. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  50. }
  51. hash_len = mbedtls_md_get_size(md);
  52. if (hash_len == 0) {
  53. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  54. }
  55. salt = null_salt;
  56. salt_len = hash_len;
  57. }
  58. return mbedtls_md_hmac(md, salt, salt_len, ikm, ikm_len, prk);
  59. }
  60. int mbedtls_hkdf_expand(const mbedtls_md_info_t *md, const unsigned char *prk,
  61. size_t prk_len, const unsigned char *info,
  62. size_t info_len, unsigned char *okm, size_t okm_len)
  63. {
  64. size_t hash_len;
  65. size_t where = 0;
  66. size_t n;
  67. size_t t_len = 0;
  68. size_t i;
  69. int ret = 0;
  70. mbedtls_md_context_t ctx;
  71. unsigned char t[MBEDTLS_MD_MAX_SIZE];
  72. if (okm == NULL) {
  73. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  74. }
  75. hash_len = mbedtls_md_get_size(md);
  76. if (prk_len < hash_len || hash_len == 0) {
  77. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  78. }
  79. if (info == NULL) {
  80. info = (const unsigned char *) "";
  81. info_len = 0;
  82. }
  83. n = okm_len / hash_len;
  84. if (okm_len % hash_len != 0) {
  85. n++;
  86. }
  87. /*
  88. * Per RFC 5869 Section 2.3, okm_len must not exceed
  89. * 255 times the hash length
  90. */
  91. if (n > 255) {
  92. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  93. }
  94. mbedtls_md_init(&ctx);
  95. if ((ret = mbedtls_md_setup(&ctx, md, 1)) != 0) {
  96. goto exit;
  97. }
  98. memset(t, 0, hash_len);
  99. /*
  100. * Compute T = T(1) | T(2) | T(3) | ... | T(N)
  101. * Where T(N) is defined in RFC 5869 Section 2.3
  102. */
  103. for (i = 1; i <= n; i++) {
  104. size_t num_to_copy;
  105. unsigned char c = i & 0xff;
  106. ret = mbedtls_md_hmac_starts(&ctx, prk, prk_len);
  107. if (ret != 0) {
  108. goto exit;
  109. }
  110. ret = mbedtls_md_hmac_update(&ctx, t, t_len);
  111. if (ret != 0) {
  112. goto exit;
  113. }
  114. ret = mbedtls_md_hmac_update(&ctx, info, info_len);
  115. if (ret != 0) {
  116. goto exit;
  117. }
  118. /* The constant concatenated to the end of each T(n) is a single octet.
  119. * */
  120. ret = mbedtls_md_hmac_update(&ctx, &c, 1);
  121. if (ret != 0) {
  122. goto exit;
  123. }
  124. ret = mbedtls_md_hmac_finish(&ctx, t);
  125. if (ret != 0) {
  126. goto exit;
  127. }
  128. num_to_copy = i != n ? hash_len : okm_len - where;
  129. memcpy(okm + where, t, num_to_copy);
  130. where += hash_len;
  131. t_len = hash_len;
  132. }
  133. exit:
  134. mbedtls_md_free(&ctx);
  135. mbedtls_platform_zeroize(t, sizeof(t));
  136. return ret;
  137. }
  138. #endif /* MBEDTLS_HKDF_C */