nrf_crypto_hkdf.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * Copyright (c) 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_CRYPTO)
  42. #include "stddef.h"
  43. #include "nrf_assert.h"
  44. #include "nrf_crypto_hmac.h"
  45. #include "nrf_crypto_hkdf.h"
  46. #include "nrf_crypto_error.h"
  47. #include "nrf_crypto_mem.h"
  48. #include "nrf_crypto_shared.h"
  49. #include "nrf_crypto_hmac_shared.h"
  50. #if NRF_MODULE_ENABLED(NRF_CRYPTO_HMAC)
  51. static ret_code_t hkdf_expand(nrf_crypto_hmac_context_t * const p_context,
  52. nrf_crypto_hmac_info_t const * p_info,
  53. uint8_t * const p_output_key,
  54. size_t output_key_size,
  55. uint8_t const * const p_ainfo,
  56. size_t ainfo_size,
  57. uint8_t * const p_temp,
  58. uint8_t const * const p_prk,
  59. size_t prk_size)
  60. {
  61. size_t const hash_digest_size = p_info->digest_size;
  62. uint32_t const n_iterations = (output_key_size + hash_digest_size - 1) / hash_digest_size;
  63. ret_code_t err_code = NRF_SUCCESS;
  64. size_t temp_size;
  65. uint8_t n_current;
  66. int write_offset;
  67. VERIFY_TRUE(n_iterations <= 255, NRF_ERROR_CRYPTO_OUTPUT_LENGTH);
  68. write_offset = 0;
  69. for (uint32_t i = 0; i < n_iterations; i++)
  70. {
  71. n_current = i + 1;
  72. err_code = nrf_crypto_hmac_init(p_context, p_info, p_prk, prk_size);
  73. VERIFY_SUCCESS(err_code);
  74. if (i != 0)
  75. {
  76. err_code = nrf_crypto_hmac_update(p_context, p_temp, hash_digest_size);
  77. VERIFY_SUCCESS(err_code);
  78. }
  79. if (p_ainfo != NULL)
  80. {
  81. err_code = nrf_crypto_hmac_update(p_context, p_ainfo, ainfo_size);
  82. VERIFY_SUCCESS(err_code);
  83. }
  84. err_code = nrf_crypto_hmac_update(p_context, &n_current, 1);
  85. VERIFY_SUCCESS(err_code);
  86. temp_size = hash_digest_size;
  87. err_code = nrf_crypto_hmac_finalize(p_context, p_temp, &temp_size);
  88. VERIFY_SUCCESS(err_code);
  89. memcpy(p_output_key + write_offset,
  90. p_temp,
  91. (n_current != n_iterations) ? hash_digest_size : (output_key_size - write_offset));
  92. write_offset += hash_digest_size;
  93. }
  94. return err_code;
  95. }
  96. ret_code_t nrf_crypto_hkdf_calculate(nrf_crypto_hmac_context_t * const p_context,
  97. nrf_crypto_hmac_info_t const * p_info,
  98. uint8_t * const p_output_key,
  99. size_t * const p_output_key_size,
  100. uint8_t const * const p_input_key,
  101. size_t input_key_size,
  102. uint8_t const * p_salt,
  103. size_t salt_size,
  104. uint8_t const * const p_ainfo,
  105. size_t ainfo_size,
  106. nrf_crypto_hkdf_mode_t mode)
  107. {
  108. uint8_t prk[NRF_CRYPTO_HASH_SIZE_SHA512]; // Scaled for the largest supported hash size.
  109. uint8_t temp[NRF_CRYPTO_HASH_SIZE_SHA512]; // Scaled for the largest supported hash size.
  110. void * p_ctx = NULL;
  111. void * p_allocated_context = NULL;
  112. size_t prk_size = sizeof(prk);
  113. size_t output_key_size = *p_output_key_size;
  114. ret_code_t err_code;
  115. VERIFY_TRUE(p_info != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  116. VERIFY_TRUE(p_output_key != NULL, NRF_ERROR_CRYPTO_OUTPUT_NULL);
  117. VERIFY_TRUE(*p_output_key_size > 0, NRF_ERROR_CRYPTO_OUTPUT_LENGTH);
  118. VERIFY_TRUE(p_input_key != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  119. VERIFY_TRUE(input_key_size > 0, NRF_ERROR_CRYPTO_INPUT_LENGTH);
  120. if (p_salt != NULL)
  121. {
  122. VERIFY_TRUE(salt_size > 0, NRF_ERROR_CRYPTO_INPUT_LENGTH);
  123. }
  124. if (p_ainfo != NULL)
  125. {
  126. VERIFY_TRUE(ainfo_size > 0, NRF_ERROR_CRYPTO_INPUT_LENGTH);
  127. }
  128. *p_output_key_size = 0; // Set output length to 0 as default value (in case of error).
  129. // Allocate context internally if p_context is NULL
  130. if (p_context == NULL)
  131. {
  132. p_allocated_context = NRF_CRYPTO_ALLOC(p_info->context_size);
  133. if (p_allocated_context == NULL)
  134. {
  135. return NRF_ERROR_CRYPTO_ALLOC_FAILED;
  136. }
  137. p_ctx = p_allocated_context;
  138. }
  139. else
  140. {
  141. p_ctx = p_context;
  142. }
  143. if (mode == NRF_CRYPTO_HKDF_EXTRACT_AND_EXPAND)
  144. {
  145. if (p_salt == NULL)
  146. {
  147. // Use default salt defined in RFC 5869: String of zeros of hash length.
  148. salt_size = p_info->digest_size;
  149. ASSERT(sizeof(temp) >= salt_size);
  150. memset(temp, 0, salt_size);
  151. p_salt = temp;
  152. }
  153. // Step 1: Extract
  154. err_code = nrf_crypto_hmac_calculate(p_context,
  155. p_info,
  156. prk,
  157. &prk_size,
  158. p_salt,
  159. salt_size,
  160. p_input_key,
  161. input_key_size);
  162. NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(err_code, p_allocated_context);
  163. // Step 2: Expand
  164. err_code = hkdf_expand(p_ctx,
  165. p_info,
  166. p_output_key,
  167. output_key_size,
  168. p_ainfo,
  169. ainfo_size,
  170. temp,
  171. prk,
  172. prk_size);
  173. NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(err_code, p_allocated_context);
  174. }
  175. else // NRF_CRYPTO_HKDF_EXPAND_ONLY
  176. {
  177. err_code = hkdf_expand(p_ctx,
  178. p_info,
  179. p_output_key,
  180. output_key_size,
  181. p_ainfo,
  182. ainfo_size,
  183. temp,
  184. p_input_key,
  185. input_key_size);
  186. NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(err_code, p_allocated_context);
  187. }
  188. if (p_allocated_context != NULL)
  189. {
  190. NRF_CRYPTO_FREE(p_allocated_context);
  191. }
  192. *p_output_key_size = output_key_size;
  193. return NRF_SUCCESS;
  194. }
  195. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_HMAC)
  196. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO)