mbedtls_backend_hmac.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * Copyright (c) 2018 - 2020, 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) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)
  42. #include "nrf_log.h"
  43. #include "nrf_crypto_error.h"
  44. #include "nrf_crypto_types.h"
  45. #include "mbedtls_backend_hmac.h"
  46. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256)
  47. static ret_code_t mbedtls_backend_hmac_init_sha256(void * const p_context,
  48. uint8_t const * p_key,
  49. size_t key_size)
  50. {
  51. int err_code;
  52. nrf_crypto_backend_mbedtls_hmac_sha256_context_t * p_ctx =
  53. (nrf_crypto_backend_mbedtls_hmac_sha256_context_t *)p_context;
  54. // Memset context to 0. This is equevalend with a call to mbedtls_md_init().
  55. memset(p_ctx->md_ctx_buffer, 0, sizeof(p_ctx->md_ctx_buffer));
  56. memset(p_ctx->hmac_ctx_buffer, 0, sizeof(p_ctx->hmac_ctx_buffer));
  57. // Set info and context pointers to buffer allocated by user.
  58. // This is Normally handled by mbedtls_md_setup(), but has to be done here in order
  59. // to avoid dynamic allocation of memory inside mbed TLS.
  60. p_ctx->mbedtls_ctx.md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
  61. p_ctx->mbedtls_ctx.md_ctx = p_ctx->md_ctx_buffer;
  62. p_ctx->mbedtls_ctx.hmac_ctx = p_ctx->hmac_ctx_buffer;
  63. // Enter key to start
  64. err_code = mbedtls_md_hmac_starts(&p_ctx->mbedtls_ctx,
  65. p_key,
  66. key_size);
  67. if (err_code != 0)
  68. {
  69. NRF_LOG_ERROR("Error in mbedtls_md_hmac_starts: %u", err_code);
  70. return NRF_ERROR_CRYPTO_INTERNAL;
  71. }
  72. return NRF_SUCCESS;
  73. }
  74. static ret_code_t mbedtls_backend_hmac_update_sha256(void * const p_context,
  75. uint8_t const * p_data,
  76. size_t size)
  77. {
  78. int err_code;
  79. nrf_crypto_backend_mbedtls_hmac_sha256_context_t * p_ctx =
  80. (nrf_crypto_backend_mbedtls_hmac_sha256_context_t *)p_context;
  81. err_code = mbedtls_md_hmac_update(&p_ctx->mbedtls_ctx, p_data, size);
  82. if (err_code != 0)
  83. {
  84. NRF_LOG_ERROR("Error in mbedtls_md_hmac_update: %u", err_code);
  85. return NRF_ERROR_CRYPTO_INTERNAL;
  86. }
  87. return NRF_SUCCESS;
  88. }
  89. static ret_code_t mbedtls_backend_hmac_finalize_sha256(void * const p_context,
  90. uint8_t * p_digest,
  91. size_t * const p_size)
  92. {
  93. int err_code;
  94. nrf_crypto_backend_mbedtls_hmac_sha256_context_t * const p_ctx =
  95. (nrf_crypto_backend_mbedtls_hmac_sha256_context_t *)p_context;
  96. // Set the digest length to 0 so that this is used in case of any error.
  97. *p_size = 0;
  98. err_code = mbedtls_md_hmac_finish(&p_ctx->mbedtls_ctx, p_digest);
  99. if (err_code != 0)
  100. {
  101. NRF_LOG_ERROR("Error in mbedtls_md_hmac_finish: %u", err_code);
  102. return NRF_ERROR_CRYPTO_INTERNAL;
  103. }
  104. *p_size = p_ctx->header.p_info->digest_size;
  105. return NRF_SUCCESS;
  106. }
  107. // Information structure for HMAC SHA256 using mbed TLS backend.
  108. const nrf_crypto_hmac_info_t g_nrf_crypto_hmac_sha256_info =
  109. {
  110. .init_fn = mbedtls_backend_hmac_init_sha256,
  111. .update_fn = mbedtls_backend_hmac_update_sha256,
  112. .finalize_fn = mbedtls_backend_hmac_finalize_sha256,
  113. .digest_size = NRF_CRYPTO_HASH_SIZE_SHA256,
  114. .context_size = sizeof(nrf_crypto_backend_hmac_sha256_context_t),
  115. .type = NRF_CRYPTO_HMAC_SHA256_TYPE
  116. };
  117. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256)
  118. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512)
  119. static ret_code_t mbedtls_backend_hmac_init_sha512(void * const p_context,
  120. uint8_t const * p_key,
  121. size_t key_size)
  122. {
  123. int err_code;
  124. nrf_crypto_backend_mbedtls_hmac_sha512_context_t * p_ctx =
  125. (nrf_crypto_backend_mbedtls_hmac_sha512_context_t *)p_context;
  126. // Memset context to 0. This is equevalend with a call to mbedtls_md_init().
  127. memset(p_ctx->md_ctx_buffer, 0, sizeof(p_ctx->md_ctx_buffer));
  128. memset(p_ctx->hmac_ctx_buffer, 0, sizeof(p_ctx->hmac_ctx_buffer));
  129. // Set info and context pointers to buffer allocated by user.
  130. // (Normally handled by mbedtls_md_setup())
  131. p_ctx->mbedtls_ctx.md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
  132. p_ctx->mbedtls_ctx.md_ctx = p_ctx->md_ctx_buffer;
  133. p_ctx->mbedtls_ctx.hmac_ctx = p_ctx->hmac_ctx_buffer;
  134. // Enter key to start
  135. err_code = mbedtls_md_hmac_starts(&p_ctx->mbedtls_ctx, p_key, key_size);
  136. if (err_code != 0)
  137. {
  138. NRF_LOG_ERROR("Error in mbedtls_md_hmac_starts: %u", err_code);
  139. return NRF_ERROR_CRYPTO_INTERNAL;
  140. }
  141. return NRF_SUCCESS;
  142. }
  143. static ret_code_t mbedtls_backend_hmac_update_sha512(void * const p_context,
  144. uint8_t const * p_data,
  145. size_t size)
  146. {
  147. int err_code;
  148. nrf_crypto_backend_mbedtls_hmac_sha512_context_t * p_ctx =
  149. (nrf_crypto_backend_mbedtls_hmac_sha512_context_t *)p_context;
  150. err_code = mbedtls_md_hmac_update(&p_ctx->mbedtls_ctx, p_data, size);
  151. if (err_code != 0)
  152. {
  153. NRF_LOG_ERROR("Error in mbedtls_md_hmac_update: %u", err_code);
  154. return NRF_ERROR_CRYPTO_INTERNAL;
  155. }
  156. return NRF_SUCCESS;
  157. }
  158. static ret_code_t mbedtls_backend_hmac_finalize_sha512(void * const p_context,
  159. uint8_t * p_digest,
  160. size_t * const p_size)
  161. {
  162. int err_code;
  163. nrf_crypto_backend_mbedtls_hmac_sha512_context_t * p_ctx =
  164. (nrf_crypto_backend_mbedtls_hmac_sha512_context_t *)p_context;
  165. // Set the digest length to 0 so that this is used in case of any error.
  166. *p_size = 0;
  167. err_code = mbedtls_md_hmac_finish(&p_ctx->mbedtls_ctx, p_digest);
  168. if (err_code != 0)
  169. {
  170. NRF_LOG_ERROR("Error in mbedtls_md_hmac_finish: %u", err_code);
  171. return NRF_ERROR_CRYPTO_INTERNAL; }
  172. *p_size = p_ctx->header.p_info->digest_size;
  173. return NRF_SUCCESS;
  174. }
  175. // Information structure for HMAC SHA512 using mbed TLS backend.
  176. const nrf_crypto_hmac_info_t g_nrf_crypto_hmac_sha512_info =
  177. {
  178. .init_fn = mbedtls_backend_hmac_init_sha512,
  179. .update_fn = mbedtls_backend_hmac_update_sha512,
  180. .finalize_fn = mbedtls_backend_hmac_finalize_sha512,
  181. .digest_size = NRF_CRYPTO_HASH_SIZE_SHA512,
  182. .context_size = sizeof(nrf_crypto_backend_hmac_sha512_context_t),
  183. .type = NRF_CRYPTO_HMAC_SHA512_TYPE
  184. };
  185. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512)
  186. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_MBEDTLS)