hkdf.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * HKDF implementation -- RFC 5869
  3. *
  4. * Copyright (C) 2016-2018, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_HKDF_C)
  27. #include <string.h>
  28. #include "mbedtls/hkdf.h"
  29. #include "mbedtls/platform_util.h"
  30. int mbedtls_hkdf( const mbedtls_md_info_t *md, const unsigned char *salt,
  31. size_t salt_len, const unsigned char *ikm, size_t ikm_len,
  32. const unsigned char *info, size_t info_len,
  33. unsigned char *okm, size_t okm_len )
  34. {
  35. int ret;
  36. unsigned char prk[MBEDTLS_MD_MAX_SIZE];
  37. ret = mbedtls_hkdf_extract( md, salt, salt_len, ikm, ikm_len, prk );
  38. if( ret == 0 )
  39. {
  40. ret = mbedtls_hkdf_expand( md, prk, mbedtls_md_get_size( md ),
  41. info, info_len, okm, okm_len );
  42. }
  43. mbedtls_platform_zeroize( prk, sizeof( prk ) );
  44. return( ret );
  45. }
  46. int mbedtls_hkdf_extract( const mbedtls_md_info_t *md,
  47. const unsigned char *salt, size_t salt_len,
  48. const unsigned char *ikm, size_t ikm_len,
  49. unsigned char *prk )
  50. {
  51. unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
  52. if( salt == NULL )
  53. {
  54. size_t hash_len;
  55. if( salt_len != 0 )
  56. {
  57. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  58. }
  59. hash_len = mbedtls_md_get_size( md );
  60. if( hash_len == 0 )
  61. {
  62. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  63. }
  64. salt = null_salt;
  65. salt_len = hash_len;
  66. }
  67. return( mbedtls_md_hmac( md, salt, salt_len, ikm, ikm_len, prk ) );
  68. }
  69. int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk,
  70. size_t prk_len, const unsigned char *info,
  71. size_t info_len, unsigned char *okm, size_t okm_len )
  72. {
  73. size_t hash_len;
  74. size_t where = 0;
  75. size_t n;
  76. size_t t_len = 0;
  77. size_t i;
  78. int ret = 0;
  79. mbedtls_md_context_t ctx;
  80. unsigned char t[MBEDTLS_MD_MAX_SIZE];
  81. if( okm == NULL )
  82. {
  83. return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
  84. }
  85. hash_len = mbedtls_md_get_size( md );
  86. if( prk_len < hash_len || hash_len == 0 )
  87. {
  88. return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
  89. }
  90. if( info == NULL )
  91. {
  92. info = (const unsigned char *) "";
  93. info_len = 0;
  94. }
  95. n = okm_len / hash_len;
  96. if( (okm_len % hash_len) != 0 )
  97. {
  98. n++;
  99. }
  100. /*
  101. * Per RFC 5869 Section 2.3, okm_len must not exceed
  102. * 255 times the hash length
  103. */
  104. if( n > 255 )
  105. {
  106. return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
  107. }
  108. mbedtls_md_init( &ctx );
  109. if( (ret = mbedtls_md_setup( &ctx, md, 1) ) != 0 )
  110. {
  111. goto exit;
  112. }
  113. /*
  114. * Compute T = T(1) | T(2) | T(3) | ... | T(N)
  115. * Where T(N) is defined in RFC 5869 Section 2.3
  116. */
  117. for( i = 1; i <= n; i++ )
  118. {
  119. size_t num_to_copy;
  120. unsigned char c = i & 0xff;
  121. ret = mbedtls_md_hmac_starts( &ctx, prk, prk_len );
  122. if( ret != 0 )
  123. {
  124. goto exit;
  125. }
  126. ret = mbedtls_md_hmac_update( &ctx, t, t_len );
  127. if( ret != 0 )
  128. {
  129. goto exit;
  130. }
  131. ret = mbedtls_md_hmac_update( &ctx, info, info_len );
  132. if( ret != 0 )
  133. {
  134. goto exit;
  135. }
  136. /* The constant concatenated to the end of each T(n) is a single octet.
  137. * */
  138. ret = mbedtls_md_hmac_update( &ctx, &c, 1 );
  139. if( ret != 0 )
  140. {
  141. goto exit;
  142. }
  143. ret = mbedtls_md_hmac_finish( &ctx, t );
  144. if( ret != 0 )
  145. {
  146. goto exit;
  147. }
  148. num_to_copy = i != n ? hash_len : okm_len - where;
  149. memcpy( okm + where, t, num_to_copy );
  150. where += hash_len;
  151. t_len = hash_len;
  152. }
  153. exit:
  154. mbedtls_md_free( &ctx );
  155. mbedtls_platform_zeroize( t, sizeof( t ) );
  156. return( ret );
  157. }
  158. #endif /* MBEDTLS_HKDF_C */