cc310_bl_backend_hash.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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_CC310_BL)
  42. #include "nrf.h"
  43. #include "cc310_bl_backend_hash.h"
  44. #include "cc310_bl_backend_shared.h"
  45. #include "cc310_backend_mutex.h"
  46. #include "cc310_backend_shared.h"
  47. #include "nrf_cc310_bl_hash_sha256.h"
  48. #include "crys_hash_error.h"
  49. #include "nrf_crypto_init.h"
  50. #include "nrf_crypto_types.h"
  51. #include "nrf_crypto_error.h"
  52. #include "nrf_crypto_shared.h"
  53. #include "nrf_crypto_hash_shared.h"
  54. #include "sdk_macros.h"
  55. #include "nrf_log.h"
  56. #include "nrf_assert.h"
  57. #include <drivers/nrfx_common.h>
  58. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256)
  59. #if defined(NRF_CRYPTO_BACKEND_CC310_BL_HASH_LITTLE_ENDIAN_DIGEST_ENABLED)
  60. #error The configuration NRF_CRYPTO_BACKEND_CC310_BL_HASH_LITTLE_ENDIAN_DIGEST_ENABLED was removed in SDK 15.1.0. Please see release notes for details on removing this error message.
  61. #endif // defined(NRF_CRYPTO_BACKEND_CC310_BL_HASH_LITTLE_ENDIAN_DIGEST_ENABLED)
  62. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER)
  63. __ALIGN(4) static uint8_t m_hash_buffer[NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE];
  64. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER)
  65. static ret_code_t hash_result_get(CRYSError_t error)
  66. {
  67. ret_code_t ret_val;
  68. switch (error)
  69. {
  70. case CRYS_OK:
  71. ret_val = NRF_SUCCESS;
  72. break;
  73. case CRYS_HASH_INVALID_USER_CONTEXT_POINTER_ERROR:
  74. ret_val = NRF_ERROR_CRYPTO_CONTEXT_NULL;
  75. break;
  76. case CRYS_HASH_ILLEGAL_OPERATION_MODE_ERROR:
  77. ret_val = NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
  78. break;
  79. case CRYS_HASH_USER_CONTEXT_CORRUPTED_ERROR:
  80. ret_val = NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED;
  81. break;
  82. // May be added to specialized errors for hash.
  83. case CRYS_HASH_LAST_BLOCK_ALREADY_PROCESSED_ERROR:
  84. ret_val = NRF_ERROR_CRYPTO_INTERNAL;
  85. break;
  86. case CRYS_HASH_IS_NOT_SUPPORTED:
  87. ret_val = NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
  88. break;
  89. default:
  90. ret_val = NRF_ERROR_CRYPTO_INTERNAL;
  91. break;
  92. }
  93. return ret_val;
  94. }
  95. static ret_code_t cc310_bl_backend_hash_sha256_init(void * const p_context)
  96. {
  97. uint32_t ret_val;
  98. CRYSError_t crys_error;
  99. // Limited parameter testing on this level.
  100. // This has been done on upper level.
  101. nrf_cc310_bl_hash_context_sha256_t * const p_backend_context
  102. = &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context);
  103. crys_error = nrf_cc310_bl_hash_sha256_init(p_backend_context);
  104. ret_val = hash_result_get(crys_error);
  105. return ret_val;
  106. }
  107. static uint32_t cc310_bl_backend_hash_sha256_update(void * const p_context,
  108. uint8_t const * p_data,
  109. size_t size)
  110. {
  111. ret_code_t ret_val;
  112. CRYSError_t crys_error;
  113. uint32_t cur_size;
  114. uint32_t size_left;
  115. uint8_t * p_cur;
  116. bool mutex_locked;
  117. // Limited parameter testing on this level.
  118. // This has been done on upper level.
  119. nrf_cc310_bl_hash_context_sha256_t * const p_backend_context
  120. = &(((nrf_crypto_backend_hash_sha256_context_t *)p_context)->context);
  121. p_cur = (uint8_t *)p_data;
  122. size_left = size;
  123. mutex_locked = cc310_backend_mutex_trylock();
  124. VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);
  125. cc310_bl_backend_enable();
  126. #if defined (NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED == 1)
  127. do
  128. {
  129. // Copy a block from FLASH to RAM for use in CC310
  130. cur_size = (size_left > NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE) ?
  131. NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE : size_left;
  132. // Copy from FLASH to ram
  133. memcpy(m_hash_buffer, p_cur, cur_size);
  134. // Update the hash with current input.
  135. crys_error = nrf_cc310_bl_hash_sha256_update(p_backend_context, m_hash_buffer, cur_size);
  136. size_left -= cur_size;
  137. p_cur += cur_size;
  138. } while(crys_error == SASI_OK && size_left > 0);
  139. #elif defined(NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED) && (NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED == 0)
  140. // Verify that the data is in RAM (required for CC310 hashing)
  141. VERIFY_TRUE(nrfx_is_in_ram(p_data), NRF_ERROR_CRYPTO_INPUT_LOCATION);
  142. do
  143. {
  144. // Get the largest block that can sent to the CC310 through DMA
  145. cur_size = (size_left > CC310_MAX_LENGTH_DMA_OPERATIONS) ?
  146. CC310_MAX_LENGTH_DMA_OPERATIONS : size_left;
  147. crys_error = nrf_cc310_bl_hash_sha256_update(p_backend_context, p_cur, cur_size);
  148. size_left -= cur_size;
  149. p_cur += cur_size;
  150. } while(crys_error == SASI_OK && size_left > 0);
  151. #else
  152. UNUSED_PARAMETER(p_backend_context);
  153. UNUSED_PARAMETER(cur_size);
  154. UNUSED_PARAMETER(size_left);
  155. UNUSED_PARAMETER(p_cur);
  156. #warning NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED define not found in sdk_config.h (Is the sdk_config.h valid?).
  157. #endif
  158. cc310_bl_backend_disable();
  159. cc310_backend_mutex_unlock();
  160. ret_val = hash_result_get(crys_error);
  161. return ret_val;
  162. }
  163. static uint32_t cc310_bl_backend_hash_sha256_finalize(void * const p_context,
  164. uint8_t * p_digest,
  165. size_t * const p_digest_size)
  166. {
  167. ret_code_t ret_val;
  168. CRYSError_t crys_error;
  169. bool mutex_locked;
  170. // Limited parameter testing on this level.
  171. // This has been done on upper level.
  172. nrf_cc310_bl_hash_context_sha256_t * const p_backend_context
  173. = &(((nrf_crypto_backend_hash_sha256_context_t * )p_context)->context);
  174. nrf_cc310_bl_hash_digest_sha256_t * p_int_digest
  175. = (nrf_cc310_bl_hash_digest_sha256_t *)p_digest;
  176. if (NRF_CRYPTO_HASH_SIZE_SHA256 > *p_digest_size)
  177. {
  178. return NRF_ERROR_CRYPTO_OUTPUT_LENGTH;
  179. }
  180. mutex_locked = cc310_backend_mutex_trylock();
  181. VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);
  182. cc310_bl_backend_enable();
  183. // Do the hash finalize calculation
  184. crys_error = nrf_cc310_bl_hash_sha256_finalize(p_backend_context, p_int_digest);
  185. cc310_bl_backend_disable();
  186. cc310_backend_mutex_unlock();
  187. ret_val = hash_result_get(crys_error);
  188. if (ret_val == NRF_SUCCESS)
  189. {
  190. *p_digest_size = NRF_CRYPTO_HASH_SIZE_SHA256;
  191. }
  192. return ret_val;
  193. }
  194. const nrf_crypto_hash_info_t g_nrf_crypto_hash_sha256_info =
  195. {
  196. .init_fn = cc310_bl_backend_hash_sha256_init,
  197. .update_fn = cc310_bl_backend_hash_sha256_update,
  198. .finalize_fn = cc310_bl_backend_hash_sha256_finalize,
  199. .digest_size = NRF_CRYPTO_HASH_SIZE_SHA256,
  200. .hash_mode = NRF_CRYPTO_HASH_MODE_SHA256
  201. };
  202. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256)
  203. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_BL)