nrf_crypto_hash.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Copyright (c) 2018 - 2019, 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 "nrf_crypto_error.h"
  43. #include "nrf_crypto_hash.h"
  44. #include "nrf_crypto_mem.h"
  45. #include "nrf_crypto_hash_backend.h"
  46. #include "nrf_crypto_hash_shared.h"
  47. #include "nrf_crypto_shared.h"
  48. #if NRF_MODULE_ENABLED(NRF_CRYPTO_HASH)
  49. static ret_code_t verify_context(nrf_crypto_hash_internal_context_t * const p_context)
  50. {
  51. if (p_context == NULL)
  52. {
  53. return NRF_ERROR_CRYPTO_CONTEXT_NULL;
  54. }
  55. if (p_context->init_val != NRF_CRYPTO_HASH_INIT_VALUE)
  56. {
  57. return NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED;
  58. }
  59. return NRF_SUCCESS;
  60. }
  61. ret_code_t nrf_crypto_hash_init(nrf_crypto_hash_context_t * const p_context,
  62. nrf_crypto_hash_info_t const * p_info)
  63. {
  64. ret_code_t ret_val;
  65. nrf_crypto_hash_internal_context_t * p_int_context;
  66. VERIFY_TRUE(p_context != NULL, NRF_ERROR_CRYPTO_CONTEXT_NULL);
  67. VERIFY_TRUE(p_info != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  68. p_int_context = (nrf_crypto_hash_internal_context_t *) p_context;
  69. p_int_context->p_info = p_info;
  70. ret_val = p_info->init_fn(p_context);
  71. if (ret_val != NRF_SUCCESS)
  72. {
  73. return ret_val;
  74. }
  75. p_int_context->init_val = NRF_CRYPTO_HASH_INIT_VALUE;
  76. return NRF_SUCCESS;
  77. }
  78. ret_code_t nrf_crypto_hash_update(nrf_crypto_hash_context_t * const p_context,
  79. uint8_t const * p_data,
  80. size_t data_size)
  81. {
  82. ret_code_t ret_val;
  83. nrf_crypto_hash_internal_context_t * p_int_context
  84. = (nrf_crypto_hash_internal_context_t *) p_context;
  85. ret_val = verify_context(p_int_context);
  86. if (ret_val != NRF_SUCCESS)
  87. {
  88. return ret_val;
  89. }
  90. VERIFY_TRUE(p_data != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  91. // Allow zero size input
  92. if (data_size == 0)
  93. {
  94. return NRF_SUCCESS;
  95. }
  96. ret_val = p_int_context->p_info->update_fn(p_context, p_data, data_size);
  97. return ret_val;
  98. }
  99. ret_code_t nrf_crypto_hash_finalize(nrf_crypto_hash_context_t * const p_context,
  100. uint8_t * p_digest,
  101. size_t * const p_digest_size)
  102. {
  103. ret_code_t ret_val;
  104. nrf_crypto_hash_internal_context_t * p_int_context
  105. = (nrf_crypto_hash_internal_context_t *) p_context;
  106. ret_val = verify_context(p_int_context);
  107. if (ret_val != NRF_SUCCESS)
  108. {
  109. return ret_val;
  110. }
  111. VERIFY_TRUE(p_digest != NULL, NRF_ERROR_CRYPTO_OUTPUT_NULL);
  112. VERIFY_TRUE(*p_digest_size >= p_int_context->p_info->digest_size, NRF_ERROR_CRYPTO_OUTPUT_LENGTH);
  113. ret_val = p_int_context->p_info->finalize_fn(p_context, p_digest, p_digest_size);
  114. return ret_val;
  115. }
  116. ret_code_t nrf_crypto_hash_calculate(nrf_crypto_hash_context_t * const p_context,
  117. nrf_crypto_hash_info_t const * p_info,
  118. uint8_t const * p_data,
  119. size_t data_size,
  120. uint8_t * p_digest,
  121. size_t * const p_digest_size)
  122. {
  123. ret_code_t ret_val;
  124. nrf_crypto_hash_context_t * p_ctx = (nrf_crypto_hash_context_t *)p_context;
  125. void * p_allocated_context = NULL;
  126. // Internal allocation of context not available for CC310_BL in order to save code size.
  127. #if defined(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED == 1)
  128. // Do nothing
  129. #elif defined(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED == 0)
  130. // Validate input. Only validate input parameters that are used locally, others are validated
  131. // in the init, update and/or finalize functions.
  132. VERIFY_TRUE(p_info != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  133. // Allocate context if needed (not provided by the user).
  134. if (p_context == NULL)
  135. {
  136. p_allocated_context = NRF_CRYPTO_ALLOC(p_info->context_size);
  137. if (p_allocated_context == NULL)
  138. {
  139. return NRF_ERROR_CRYPTO_ALLOC_FAILED;
  140. }
  141. p_ctx = (nrf_crypto_hash_context_t *)p_allocated_context;
  142. }
  143. #else
  144. #warning NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED define not found in sdk_config.h (Is the sdk_config.h valid?).
  145. #endif // NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED
  146. ret_val = nrf_crypto_hash_init(p_ctx, p_info);
  147. NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(ret_val, p_allocated_context);
  148. ret_val = nrf_crypto_hash_update(p_ctx, p_data, data_size);
  149. NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(ret_val, p_allocated_context);
  150. ret_val = nrf_crypto_hash_finalize(p_ctx, p_digest, p_digest_size);
  151. NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(ret_val, p_allocated_context);
  152. #if !NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256)
  153. // Free context if allocated internally
  154. if (p_allocated_context != NULL)
  155. {
  156. NRF_CRYPTO_FREE(p_allocated_context);
  157. }
  158. #endif // !NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256)
  159. return NRF_SUCCESS;
  160. }
  161. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_HASH)
  162. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO)