cc310_backend_hmac.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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)
  42. #include "sdk_common.h"
  43. #include "nrf_log.h"
  44. #include "nrf_crypto_hmac_shared.h"
  45. #include "cc310_backend_hmac.h"
  46. #include "nrf_crypto_error.h"
  47. #include "nrf_crypto_types.h"
  48. #include <drivers/nrfx_common.h>
  49. #include "crys_hmac.h"
  50. #include "crys_hmac_defs.h"
  51. #include "crys_hmac_error.h"
  52. #include "crys_hash.h"
  53. #include "cc310_backend_mutex.h"
  54. #include "cc310_backend_shared.h"
  55. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_HMAC_SHA256) || \
  56. NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_HMAC_SHA512)
  57. static ret_code_t result_get(CRYSError_t err_code)
  58. {
  59. ret_code_t ret_val;
  60. switch (err_code)
  61. {
  62. case CRYS_OK:
  63. ret_val = NRF_SUCCESS;
  64. break;
  65. case CRYS_HMAC_INVALID_USER_CONTEXT_POINTER_ERROR:
  66. ret_val = NRF_ERROR_CRYPTO_CONTEXT_NULL;
  67. break;
  68. case CRYS_HMAC_USER_CONTEXT_CORRUPTED_ERROR:
  69. ret_val = NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED;
  70. break;
  71. case CRYS_HMAC_DATA_IN_POINTER_INVALID_ERROR:
  72. case CRYS_HMAC_INVALID_KEY_POINTER_ERROR:
  73. ret_val = NRF_ERROR_CRYPTO_INPUT_NULL;
  74. break;
  75. case CRYS_HMAC_INVALID_RESULT_BUFFER_POINTER_ERROR:
  76. ret_val = NRF_ERROR_CRYPTO_OUTPUT_NULL;
  77. break;
  78. case CRYS_HMAC_ILLEGAL_PARAMS_ERROR:
  79. ret_val = NRF_ERROR_CRYPTO_INVALID_PARAM;
  80. break;
  81. case CRYS_HMAC_UNVALID_KEY_SIZE_ERROR:
  82. case CRYS_HMAC_DATA_SIZE_ILLEGAL:
  83. ret_val = NRF_ERROR_CRYPTO_INPUT_LENGTH;
  84. break;
  85. case CRYS_HMAC_ILLEGAL_OPERATION_MODE_ERROR:
  86. case CRYS_HMAC_LAST_BLOCK_ALREADY_PROCESSED_ERROR:
  87. case CRYS_HMAC_IS_NOT_SUPPORTED:
  88. case CRYS_HMAC_CTX_SIZES_ERROR:
  89. default:
  90. ret_val = NRF_ERROR_CRYPTO_INTERNAL;
  91. break;
  92. }
  93. return ret_val;
  94. }
  95. static ret_code_t cc310_backend_hmac_init(void * const p_context,
  96. uint8_t const * p_key,
  97. size_t key_size)
  98. {
  99. CRYSError_t err_code;
  100. CRYS_HASH_OperationMode_t hash_mode;
  101. ret_code_t ret_val;
  102. bool mutex_locked;
  103. nrf_crypto_backend_cc310_hmac_context_t * p_ctx =
  104. (nrf_crypto_backend_cc310_hmac_context_t *)p_context;
  105. switch (p_ctx->header.p_info->type)
  106. {
  107. case NRF_CRYPTO_HMAC_SHA256_TYPE:
  108. {
  109. hash_mode = CRYS_HASH_SHA256_mode;
  110. } break;
  111. case NRF_CRYPTO_HMAC_SHA512_TYPE:
  112. {
  113. hash_mode = CRYS_HASH_SHA512_mode;
  114. } break;
  115. default:
  116. {
  117. NRF_LOG_ERROR("Hash algorithm not supported by CC310 backend wrapper");
  118. return NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
  119. }
  120. }
  121. // Key in flash could lead to silently calculating wrong HMAC.
  122. VERIFY_TRUE(nrfx_is_in_ram(p_key), NRF_ERROR_CRYPTO_INPUT_LOCATION);
  123. mutex_locked = cc310_backend_mutex_trylock();
  124. VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);
  125. err_code = CRYS_HMAC_Init(&p_ctx->crys_context, hash_mode, (uint8_t *)p_key, key_size);
  126. cc310_backend_mutex_unlock();
  127. ret_val = result_get(err_code);
  128. return ret_val;
  129. }
  130. static ret_code_t cc310_backend_hmac_update(void * const p_context,
  131. uint8_t const * p_data,
  132. size_t size)
  133. {
  134. CRYSError_t err_code;
  135. ret_code_t ret_val;
  136. bool mutex_locked;
  137. size_t cur_len;
  138. size_t len_left = size;
  139. uint8_t const * p_cur = p_data;
  140. nrf_crypto_backend_cc310_hmac_context_t * p_ctx =
  141. (nrf_crypto_backend_cc310_hmac_context_t *)p_context;
  142. // Data in flash could lead to silently calculating wrong HMAC.
  143. VERIFY_TRUE(nrfx_is_in_ram(p_data), NRF_ERROR_CRYPTO_INPUT_LOCATION);
  144. mutex_locked = cc310_backend_mutex_trylock();
  145. VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);
  146. // If the input is larger than CC310_MAX_LENGTH_DMA_OPERATIONS, split into smaller
  147. do
  148. {
  149. cur_len = (len_left > CC310_MAX_LENGTH_DMA_OPERATIONS) ?
  150. CC310_MAX_LENGTH_DMA_OPERATIONS : len_left;
  151. err_code = CRYS_HMAC_Update(&p_ctx->crys_context, (uint8_t *)p_cur, cur_len);
  152. len_left -= cur_len;
  153. p_cur += cur_len;
  154. } while (err_code == CRYS_OK && len_left > 0);
  155. cc310_backend_mutex_unlock();
  156. ret_val = result_get(err_code);
  157. return ret_val;
  158. }
  159. static ret_code_t cc310_backend_hmac_finalize(void * const p_context,
  160. uint8_t * p_digest,
  161. size_t * const p_size)
  162. {
  163. CRYSError_t err_code;
  164. ret_code_t ret_val;
  165. bool mutex_locked;
  166. CRYS_HASH_Result_t * p_int_digest = (CRYS_HASH_Result_t *)p_digest;
  167. nrf_crypto_backend_cc310_hmac_context_t * p_ctx =
  168. (nrf_crypto_backend_cc310_hmac_context_t *)p_context;
  169. // Set the digest length to 0 so that this is used in case of any error.
  170. *p_size = 0;
  171. mutex_locked = cc310_backend_mutex_trylock();
  172. VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);
  173. err_code = CRYS_HMAC_Finish(&p_ctx->crys_context, *p_int_digest);
  174. cc310_backend_mutex_unlock();
  175. ret_val = result_get(err_code);
  176. if (err_code != NRF_SUCCESS)
  177. {
  178. return ret_val;
  179. }
  180. *p_size = p_ctx->header.p_info->digest_size;
  181. return ret_val;
  182. }
  183. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_HMAC_SHA256)
  184. // Information structure for HMAC SHA256 using CC310 backend.
  185. const nrf_crypto_hmac_info_t g_nrf_crypto_hmac_sha256_info =
  186. {
  187. .init_fn = cc310_backend_hmac_init,
  188. .update_fn = cc310_backend_hmac_update,
  189. .finalize_fn = cc310_backend_hmac_finalize,
  190. .digest_size = NRF_CRYPTO_HASH_SIZE_SHA256,
  191. .context_size = sizeof(nrf_crypto_backend_hmac_sha256_context_t),
  192. .type = NRF_CRYPTO_HMAC_SHA256_TYPE,
  193. };
  194. #endif // NRF_CRYPTO_BACKEND_CC310_HMAC_SHA256_ENABLED
  195. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_HMAC_SHA512)
  196. // Information structure for HMAC SHA512 using CC310 backend.
  197. const nrf_crypto_hmac_info_t g_nrf_crypto_hmac_sha512_info =
  198. {
  199. .init_fn = cc310_backend_hmac_init,
  200. .update_fn = cc310_backend_hmac_update,
  201. .finalize_fn = cc310_backend_hmac_finalize,
  202. .digest_size = NRF_CRYPTO_HASH_SIZE_SHA512,
  203. .context_size = sizeof(nrf_crypto_backend_hmac_sha512_context_t),
  204. .type = NRF_CRYPTO_HMAC_SHA512_TYPE,
  205. };
  206. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_HMAC_SHA512)
  207. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_HMAC_SHA256) || NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_HMAC_SHA512)
  208. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO) && NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310)