cc310_backend_chacha_poly_aead.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <drivers/nrfx_common.h>
  43. #include <stdbool.h>
  44. #include "cc310_backend_mutex.h"
  45. #include "crys_chacha_poly_error.h"
  46. #include "cc310_backend_chacha_poly_aead.h"
  47. #include "cc310_backend_shared.h"
  48. #include "cc310_backend_shared.h"
  49. #if NRF_MODULE_ENABLED(NRF_CRYPTO_CC310_CHACHA_POLY_AEAD)
  50. static ret_code_t result_get(CRYSError_t error)
  51. {
  52. ret_code_t ret_val;
  53. switch (error)
  54. {
  55. case CRYS_OK:
  56. ret_val = NRF_SUCCESS;
  57. break;
  58. /*! Invalid Additional data. */
  59. case CRYS_CHACHA_POLY_DATA_INVALID_ERROR:
  60. case CRYS_CHACHA_POLY_ADATA_INVALID_ERROR:
  61. ret_val = NRF_ERROR_CRYPTO_INPUT_NULL;
  62. break;
  63. /*! Illegal encryption mode. */
  64. case CRYS_CHACHA_POLY_ENC_MODE_INVALID_ERROR:
  65. ret_val = NRF_ERROR_CRYPTO_INVALID_PARAM;
  66. break;
  67. /*! Illegal data size. */
  68. case CRYS_CHACHA_POLY_DATA_SIZE_INVALID_ERROR:
  69. ret_val = NRF_ERROR_CRYPTO_INPUT_LENGTH;
  70. break;
  71. /*! MAC comparison error. */
  72. case CRYS_CHACHA_POLY_MAC_ERROR:
  73. ret_val = NRF_ERROR_CRYPTO_AEAD_INVALID_MAC;
  74. break;
  75. default:
  76. ret_val = NRF_ERROR_CRYPTO_INTERNAL;
  77. break;
  78. }
  79. return ret_val;
  80. }
  81. static ret_code_t backend_cc310_init(void * const p_context, uint8_t * p_key)
  82. {
  83. nrf_crypto_backend_chacha_poly_context_t * p_ctx =
  84. (nrf_crypto_backend_chacha_poly_context_t *)p_context;
  85. if (!nrfx_is_in_ram(p_ctx))
  86. {
  87. return NRF_ERROR_CRYPTO_INPUT_LOCATION;
  88. }
  89. if (p_ctx->header.p_info->key_size != NRF_CRYPTO_KEY_SIZE_256)
  90. {
  91. return NRF_ERROR_CRYPTO_KEY_SIZE;
  92. }
  93. memcpy(p_ctx->key, p_key, sizeof(p_ctx->key));
  94. return NRF_SUCCESS;
  95. }
  96. static inline ret_code_t backend_cc310_uninit(void * const p_context)
  97. {
  98. return NRF_SUCCESS;
  99. }
  100. static ret_code_t backend_cc310_crypt(void * const p_context,
  101. nrf_crypto_operation_t operation,
  102. uint8_t * p_nonce,
  103. uint8_t nonce_size,
  104. uint8_t * p_adata,
  105. size_t adata_size,
  106. uint8_t * p_data_in,
  107. size_t data_in_size,
  108. uint8_t * p_data_out,
  109. uint8_t * p_mac,
  110. uint8_t mac_size)
  111. {
  112. CRYSError_t result;
  113. ret_code_t ret_val;
  114. bool mutex_locked;
  115. CRYS_CHACHA_EncryptMode_t operation_cc310;
  116. nrf_crypto_backend_chacha_poly_context_t * p_ctx =
  117. (nrf_crypto_backend_chacha_poly_context_t *)p_context;
  118. mutex_locked = cc310_backend_mutex_trylock();
  119. VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);
  120. if ((adata_size == 0) || (data_in_size == 0))
  121. {
  122. ret_val = NRF_ERROR_CRYPTO_INPUT_LENGTH;
  123. goto exit;
  124. }
  125. if (mac_size != NRF_CRYPTO_CHACHA_POLY_MAC_SIZE)
  126. {
  127. ret_val = NRF_ERROR_CRYPTO_AEAD_MAC_SIZE;
  128. goto exit;
  129. }
  130. if (nonce_size != NRF_CRYPTO_CHACHA_POLY_NONCE_SIZE)
  131. {
  132. ret_val = NRF_ERROR_CRYPTO_AEAD_NONCE_SIZE;
  133. goto exit;
  134. }
  135. if (!nrfx_is_in_ram(p_data_in) || !nrfx_is_in_ram(p_data_out) ||
  136. !nrfx_is_in_ram(p_mac) || !nrfx_is_in_ram(p_adata) ||
  137. !nrfx_is_in_ram(p_nonce))
  138. {
  139. ret_val = NRF_ERROR_CRYPTO_INPUT_LOCATION;
  140. goto exit;
  141. }
  142. if (operation == NRF_CRYPTO_DECRYPT)
  143. {
  144. operation_cc310 = CRYS_CHACHA_Decrypt;
  145. }
  146. else if (operation == NRF_CRYPTO_ENCRYPT)
  147. {
  148. operation_cc310 = CRYS_CHACHA_Encrypt;
  149. }
  150. else
  151. {
  152. ret_val = NRF_ERROR_CRYPTO_INVALID_PARAM;
  153. goto exit;
  154. }
  155. result = CRYS_CHACHA_POLY(p_nonce,
  156. p_ctx->key,
  157. operation_cc310,
  158. p_adata,
  159. adata_size,
  160. p_data_in,
  161. data_in_size,
  162. p_data_out,
  163. (uint32_t *)p_mac);
  164. ret_val = result_get(result);
  165. exit:
  166. cc310_backend_mutex_unlock();
  167. return ret_val;
  168. }
  169. nrf_crypto_aead_info_t const g_nrf_crypto_chacha_poly_256_info =
  170. {
  171. .key_size = NRF_CRYPTO_KEY_SIZE_256,
  172. .mode = NRF_CRYPTO_AEAD_MODE_CHACHA_POLY,
  173. .init_fn = backend_cc310_init,
  174. .uninit_fn = backend_cc310_uninit,
  175. .crypt_fn = backend_cc310_crypt
  176. };
  177. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_CC310_CHACHA_POLY_AEAD)
  178. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO)