hkdf.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_HKDF_C)
  52. #include <string.h>
  53. #include "mbedtls/hkdf.h"
  54. #include "mbedtls/platform_util.h"
  55. int mbedtls_hkdf( const mbedtls_md_info_t *md, const unsigned char *salt,
  56. size_t salt_len, const unsigned char *ikm, size_t ikm_len,
  57. const unsigned char *info, size_t info_len,
  58. unsigned char *okm, size_t okm_len )
  59. {
  60. int ret;
  61. unsigned char prk[MBEDTLS_MD_MAX_SIZE];
  62. ret = mbedtls_hkdf_extract( md, salt, salt_len, ikm, ikm_len, prk );
  63. if( ret == 0 )
  64. {
  65. ret = mbedtls_hkdf_expand( md, prk, mbedtls_md_get_size( md ),
  66. info, info_len, okm, okm_len );
  67. }
  68. mbedtls_platform_zeroize( prk, sizeof( prk ) );
  69. return( ret );
  70. }
  71. int mbedtls_hkdf_extract( const mbedtls_md_info_t *md,
  72. const unsigned char *salt, size_t salt_len,
  73. const unsigned char *ikm, size_t ikm_len,
  74. unsigned char *prk )
  75. {
  76. unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
  77. if( salt == NULL )
  78. {
  79. size_t hash_len;
  80. if( salt_len != 0 )
  81. {
  82. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  83. }
  84. hash_len = mbedtls_md_get_size( md );
  85. if( hash_len == 0 )
  86. {
  87. return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
  88. }
  89. salt = null_salt;
  90. salt_len = hash_len;
  91. }
  92. return( mbedtls_md_hmac( md, salt, salt_len, ikm, ikm_len, prk ) );
  93. }
  94. int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk,
  95. size_t prk_len, const unsigned char *info,
  96. size_t info_len, unsigned char *okm, size_t okm_len )
  97. {
  98. size_t hash_len;
  99. size_t where = 0;
  100. size_t n;
  101. size_t t_len = 0;
  102. size_t i;
  103. int ret = 0;
  104. mbedtls_md_context_t ctx;
  105. unsigned char t[MBEDTLS_MD_MAX_SIZE];
  106. if( okm == NULL )
  107. {
  108. return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
  109. }
  110. hash_len = mbedtls_md_get_size( md );
  111. if( prk_len < hash_len || hash_len == 0 )
  112. {
  113. return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
  114. }
  115. if( info == NULL )
  116. {
  117. info = (const unsigned char *) "";
  118. info_len = 0;
  119. }
  120. n = okm_len / hash_len;
  121. if( (okm_len % hash_len) != 0 )
  122. {
  123. n++;
  124. }
  125. /*
  126. * Per RFC 5869 Section 2.3, okm_len must not exceed
  127. * 255 times the hash length
  128. */
  129. if( n > 255 )
  130. {
  131. return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
  132. }
  133. mbedtls_md_init( &ctx );
  134. if( (ret = mbedtls_md_setup( &ctx, md, 1) ) != 0 )
  135. {
  136. goto exit;
  137. }
  138. /*
  139. * Compute T = T(1) | T(2) | T(3) | ... | T(N)
  140. * Where T(N) is defined in RFC 5869 Section 2.3
  141. */
  142. for( i = 1; i <= n; i++ )
  143. {
  144. size_t num_to_copy;
  145. unsigned char c = i & 0xff;
  146. ret = mbedtls_md_hmac_starts( &ctx, prk, prk_len );
  147. if( ret != 0 )
  148. {
  149. goto exit;
  150. }
  151. ret = mbedtls_md_hmac_update( &ctx, t, t_len );
  152. if( ret != 0 )
  153. {
  154. goto exit;
  155. }
  156. ret = mbedtls_md_hmac_update( &ctx, info, info_len );
  157. if( ret != 0 )
  158. {
  159. goto exit;
  160. }
  161. /* The constant concatenated to the end of each T(n) is a single octet.
  162. * */
  163. ret = mbedtls_md_hmac_update( &ctx, &c, 1 );
  164. if( ret != 0 )
  165. {
  166. goto exit;
  167. }
  168. ret = mbedtls_md_hmac_finish( &ctx, t );
  169. if( ret != 0 )
  170. {
  171. goto exit;
  172. }
  173. num_to_copy = i != n ? hash_len : okm_len - where;
  174. memcpy( okm + where, t, num_to_copy );
  175. where += hash_len;
  176. t_len = hash_len;
  177. }
  178. exit:
  179. mbedtls_md_free( &ctx );
  180. mbedtls_platform_zeroize( t, sizeof( t ) );
  181. return( ret );
  182. }
  183. #endif /* MBEDTLS_HKDF_C */