nrf_crypto_hmac.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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)
  42. #include "stddef.h"
  43. #include "nrf_log.h"
  44. #include "nrf_crypto_hmac.h"
  45. #include "nrf_crypto_hmac_shared.h"
  46. #include "nrf_crypto_error.h"
  47. #include "nrf_crypto_init.h"
  48. #include "nrf_crypto_mem.h"
  49. #include "nrf_crypto_shared.h"
  50. #if NRF_MODULE_ENABLED(NRF_CRYPTO_HMAC)
  51. // Magic word that is set when initializing the context and checked by functions that use it.
  52. #define NRF_CRYPTO_HMAC_INIT_MAGIC_VALUE 0xBADEBA11
  53. static ret_code_t verify_context_valid(nrf_crypto_hmac_internal_context_t * const p_context)
  54. {
  55. if (p_context == NULL)
  56. {
  57. return NRF_ERROR_CRYPTO_CONTEXT_NULL;
  58. }
  59. else if (p_context->init_value != NRF_CRYPTO_HMAC_INIT_MAGIC_VALUE)
  60. {
  61. return NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED;
  62. }
  63. else
  64. {
  65. return NRF_SUCCESS;
  66. }
  67. }
  68. ret_code_t nrf_crypto_hmac_init(nrf_crypto_hmac_context_t * const p_context,
  69. nrf_crypto_hmac_info_t const * p_info,
  70. uint8_t const * p_key,
  71. size_t key_size)
  72. {
  73. ret_code_t err_code;
  74. nrf_crypto_hmac_internal_context_t * p_ctx = (nrf_crypto_hmac_internal_context_t *)p_context;
  75. VERIFY_TRUE(nrf_crypto_is_initialized(), NRF_ERROR_CRYPTO_NOT_INITIALIZED);
  76. // Validate input
  77. VERIFY_TRUE(p_ctx != NULL, NRF_ERROR_CRYPTO_CONTEXT_NULL);
  78. VERIFY_TRUE(p_info != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  79. VERIFY_TRUE(p_key != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  80. VERIFY_TRUE(key_size > 0, NRF_ERROR_CRYPTO_INPUT_LENGTH);
  81. // Initialize generic part of the context
  82. p_ctx->p_info = p_info;
  83. // Do backend specific initialization by calling the backend init function pointed
  84. // to in the configuration struct in the context (nrf_crypto_hmac_config_t)
  85. err_code = p_ctx->p_info->init_fn(p_context, p_key, key_size);
  86. if (err_code == NRF_SUCCESS)
  87. {
  88. p_ctx->init_value = NRF_CRYPTO_HMAC_INIT_MAGIC_VALUE;
  89. }
  90. return err_code;
  91. }
  92. ret_code_t nrf_crypto_hmac_update(nrf_crypto_hmac_context_t * const p_context,
  93. uint8_t const * p_data,
  94. size_t data_size)
  95. {
  96. ret_code_t err_code;
  97. // The context header by definition has to be the first element of the context struct.
  98. nrf_crypto_hmac_internal_context_t * p_ctx = (nrf_crypto_hmac_internal_context_t *)p_context;
  99. // Validate input
  100. err_code = verify_context_valid(p_ctx);
  101. VERIFY_SUCCESS(err_code);
  102. VERIFY_TRUE(p_data != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  103. VERIFY_TRUE(data_size > 0, NRF_ERROR_CRYPTO_INPUT_LENGTH);
  104. // Call backend specific update function (pointed to by config struct in context)
  105. err_code = p_ctx->p_info->update_fn(p_context, p_data, data_size);
  106. return err_code;
  107. }
  108. ret_code_t nrf_crypto_hmac_finalize(nrf_crypto_hmac_context_t * const p_context,
  109. uint8_t * p_digest,
  110. size_t * const p_digest_size)
  111. {
  112. ret_code_t err_code;
  113. // The context header by definition has to be the first element of the context struct.
  114. nrf_crypto_hmac_internal_context_t * p_ctx = (nrf_crypto_hmac_internal_context_t *)p_context;
  115. // Validate input
  116. err_code = verify_context_valid(p_ctx);
  117. VERIFY_SUCCESS(err_code);
  118. VERIFY_TRUE(p_digest != NULL, NRF_ERROR_CRYPTO_OUTPUT_NULL);
  119. VERIFY_TRUE(*p_digest_size >= p_ctx->p_info->digest_size, NRF_ERROR_CRYPTO_OUTPUT_LENGTH);
  120. // Call backend specific finish function (pointed to by config struct in context)
  121. err_code = p_ctx->p_info->finalize_fn(p_context, p_digest, p_digest_size);
  122. return err_code;
  123. }
  124. ret_code_t nrf_crypto_hmac_calculate(nrf_crypto_hmac_context_t * const p_context,
  125. nrf_crypto_hmac_info_t const * p_info,
  126. uint8_t * p_digest,
  127. size_t * const p_digest_size,
  128. uint8_t const * p_key,
  129. size_t key_size,
  130. uint8_t const * p_data,
  131. size_t data_size)
  132. {
  133. ret_code_t err_code;
  134. nrf_crypto_hmac_context_t * p_ctx;
  135. void * p_allocated_context = NULL;
  136. // Validate input. Only validate input parameters that are used locally, others are validated
  137. // in the init, update and/or finalize functions.
  138. VERIFY_TRUE(p_info != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  139. // Allocate context if needed (not provided by the user).
  140. if (p_context == NULL)
  141. {
  142. p_allocated_context = NRF_CRYPTO_ALLOC(p_info->context_size);
  143. if (p_allocated_context == NULL)
  144. {
  145. return NRF_ERROR_CRYPTO_ALLOC_FAILED;
  146. }
  147. p_ctx = (nrf_crypto_hmac_context_t *)p_allocated_context;
  148. }
  149. else
  150. {
  151. p_ctx = (nrf_crypto_hmac_context_t *)p_context;
  152. }
  153. // Perform integrated HMAC calculation by caling the frontend functions defined in this file
  154. err_code = nrf_crypto_hmac_init(p_ctx, p_info, p_key, key_size);
  155. NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(err_code, p_allocated_context);
  156. err_code = nrf_crypto_hmac_update(p_ctx, p_data, data_size);
  157. NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(err_code, p_allocated_context);
  158. err_code = nrf_crypto_hmac_finalize(p_ctx, p_digest, p_digest_size);
  159. NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(err_code, p_allocated_context);
  160. // Free context if allocated internally
  161. if (p_allocated_context != NULL)
  162. {
  163. NRF_CRYPTO_FREE(p_allocated_context);
  164. }
  165. return err_code;
  166. }
  167. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_HMAC)
  168. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO)