cc310_backend_aes_aead.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. #include <drivers/nrfx_common.h>
  42. #if NRF_MODULE_ENABLED(NRF_CRYPTO)
  43. #include <stdbool.h>
  44. #include "crys_aesccm_error.h"
  45. #include "cc310_backend_aes_aead.h"
  46. #include "cc310_backend_mutex.h"
  47. #include "cc310_backend_shared.h"
  48. #if NRF_MODULE_ENABLED(NRF_CRYPTO_CC310_AES_AEAD)
  49. /**@internal @brief Type declaration of a template suiting all possible context sizes
  50. * for this backend.
  51. */
  52. typedef struct
  53. {
  54. nrf_crypto_aead_internal_context_t header; /**< Common header for context. */
  55. CRYS_AESCCM_UserContext_t context;
  56. uint8_t key[16]; /**< Only supported key size by CC310 is 128 bit */
  57. } nrf_crypto_backend_cc310_aes_aead_context_t;
  58. static ret_code_t result_get(CRYSError_t error)
  59. {
  60. ret_code_t ret_val;
  61. switch (error)
  62. {
  63. case CRYS_OK:
  64. ret_val = NRF_SUCCESS;
  65. break;
  66. case CRYS_AESCCM_INVALID_USER_CONTEXT_POINTER_ERROR:
  67. ret_val = NRF_ERROR_CRYPTO_CONTEXT_NULL;
  68. break;
  69. case CRYS_AESCCM_ILLEGAL_KEY_SIZE_ERROR:
  70. ret_val = NRF_ERROR_CRYPTO_KEY_SIZE;
  71. break;
  72. case CRYS_AESCCM_ILLEGAL_TAG_SIZE_ERROR:
  73. ret_val = NRF_ERROR_CRYPTO_AEAD_MAC_SIZE;
  74. break;
  75. case CRYS_AESCCM_ILLEGAL_NONCE_SIZE_ERROR:
  76. ret_val = NRF_ERROR_CRYPTO_AEAD_NONCE_SIZE;
  77. break;
  78. case CRYS_AESCCM_ILLEGAL_PARAMETER_SIZE_ERROR:
  79. case CRYS_AESCCM_DATA_IN_SIZE_ILLEGAL:
  80. ret_val = NRF_ERROR_CRYPTO_INPUT_LENGTH;
  81. break;
  82. case CRYS_AESCCM_INVALID_KEY_POINTER_ERROR:
  83. case CRYS_AESCCM_ILLEGAL_PARAMETER_PTR_ERROR:
  84. case CRYS_AESCCM_DATA_IN_POINTER_INVALID_ERROR:
  85. ret_val = NRF_ERROR_CRYPTO_INPUT_NULL;
  86. break;
  87. case CRYS_AESCCM_IS_NOT_SUPPORTED:
  88. case CRYS_AESCCM_INVALID_ENCRYPT_MODE_ERROR:
  89. ret_val = NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
  90. break;
  91. case CRYS_AESCCM_DATA_OUT_SIZE_INVALID_ERROR:
  92. ret_val = NRF_ERROR_CRYPTO_OUTPUT_LENGTH;
  93. break;
  94. case CRYS_AESCCM_DATA_OUT_POINTER_INVALID_ERROR:
  95. ret_val = NRF_ERROR_CRYPTO_OUTPUT_NULL;
  96. break;
  97. case CRYS_AESCCM_ILLEGAL_PARAMETER_ERROR:
  98. ret_val = NRF_ERROR_CRYPTO_INVALID_PARAM;
  99. break;
  100. case CRYS_AESCCM_CCM_MAC_INVALID_ERROR:
  101. ret_val = NRF_ERROR_CRYPTO_AEAD_INVALID_MAC;
  102. break;
  103. case CRYS_AESCCM_CTX_SIZES_ERROR:
  104. default:
  105. ret_val = NRF_ERROR_CRYPTO_INTERNAL;
  106. break;
  107. }
  108. return ret_val;
  109. }
  110. static ret_code_t backend_cc310_init(void * const p_context, uint8_t * p_key)
  111. {
  112. ret_code_t ret_val;
  113. nrf_crypto_backend_cc310_aes_aead_context_t * p_ctx =
  114. (nrf_crypto_backend_cc310_aes_aead_context_t *)p_context;
  115. if (!nrfx_is_in_ram(p_ctx))
  116. {
  117. return NRF_ERROR_CRYPTO_INPUT_LOCATION;
  118. }
  119. if (p_ctx->header.p_info->key_size != NRF_CRYPTO_KEY_SIZE_128)
  120. {
  121. return NRF_ERROR_CRYPTO_KEY_SIZE;
  122. }
  123. switch (p_ctx->header.p_info->mode)
  124. {
  125. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_AES_CCM)
  126. case NRF_CRYPTO_AEAD_MODE_AES_CCM:
  127. ret_val = NRF_SUCCESS;
  128. break;
  129. #endif
  130. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_AES_CCM_STAR)
  131. case NRF_CRYPTO_AEAD_MODE_AES_CCM_STAR:
  132. ret_val = NRF_SUCCESS;
  133. break;
  134. #endif
  135. default:
  136. return NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE;
  137. }
  138. memcpy(p_ctx->key, p_key, sizeof(p_ctx->key));
  139. return ret_val;
  140. }
  141. static ret_code_t backend_cc310_uninit(void * const p_context)
  142. {
  143. nrf_crypto_backend_cc310_aes_aead_context_t * p_ctx =
  144. (nrf_crypto_backend_cc310_aes_aead_context_t *)p_context;
  145. memset(&p_ctx->context, 0, sizeof(CRYS_AESCCM_UserContext_t));
  146. return NRF_SUCCESS;
  147. }
  148. static ret_code_t backend_cc310_crypt(void * const p_context,
  149. nrf_crypto_operation_t operation,
  150. uint8_t * p_nonce,
  151. uint8_t nonce_size,
  152. uint8_t * p_adata,
  153. size_t adata_size,
  154. uint8_t * p_data_in,
  155. size_t data_in_size,
  156. uint8_t * p_data_out,
  157. uint8_t * p_mac,
  158. uint8_t mac_size)
  159. {
  160. uint32_t mode;
  161. CRYSError_t result;
  162. ret_code_t ret_val;
  163. bool mutex_locked;
  164. SaSiAesEncryptMode_t operation_cc310;
  165. CRYS_AESCCM_Mac_Res_t mac_buffer;
  166. nrf_crypto_backend_cc310_aes_aead_context_t * p_ctx =
  167. (nrf_crypto_backend_cc310_aes_aead_context_t *)p_context;
  168. mutex_locked = cc310_backend_mutex_trylock();
  169. VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);
  170. if (!nrfx_is_in_ram(p_adata) && (adata_size > 0))
  171. {
  172. ret_val = NRF_ERROR_CRYPTO_INPUT_LOCATION;
  173. goto exit;
  174. }
  175. /* CC310 supports: CCM & CCM*, where nonce_size must be > 0, so p_nonce must always
  176. point to RAM. */
  177. if (!nrfx_is_in_ram(p_nonce) ||
  178. !nrfx_is_in_ram(p_data_in) ||
  179. !nrfx_is_in_ram(p_data_out) ||
  180. !nrfx_is_in_ram(p_mac))
  181. {
  182. ret_val = NRF_ERROR_CRYPTO_INPUT_LOCATION;
  183. goto exit;
  184. }
  185. if (operation == NRF_CRYPTO_DECRYPT)
  186. {
  187. operation_cc310 = SASI_AES_DECRYPT;
  188. }
  189. else if (operation == NRF_CRYPTO_ENCRYPT)
  190. {
  191. operation_cc310 = SASI_AES_ENCRYPT;
  192. }
  193. else
  194. {
  195. ret_val = NRF_ERROR_CRYPTO_INVALID_PARAM;
  196. goto exit;
  197. }
  198. if (p_ctx->header.p_info->mode == NRF_CRYPTO_AEAD_MODE_AES_CCM)
  199. {
  200. mode = CRYS_AESCCM_MODE_CCM;
  201. /* Allowed MAC size in CCM mode: [4, 6, 8, 10, 12, 14, 16] */
  202. if ((mac_size < NRF_CRYPTO_AES_CCM_MAC_MIN) ||
  203. (mac_size > NRF_CRYPTO_AES_CCM_MAC_MAX) ||
  204. ((mac_size & 0x01) != 0))
  205. {
  206. ret_val = NRF_ERROR_CRYPTO_AEAD_MAC_SIZE;
  207. goto exit;
  208. }
  209. if ((nonce_size < NRF_CRYPTO_AES_CCM_NONCE_SIZE_MIN) ||
  210. (nonce_size > NRF_CRYPTO_AES_CCM_NONCE_SIZE_MAX))
  211. {
  212. ret_val = NRF_ERROR_CRYPTO_AEAD_NONCE_SIZE;
  213. goto exit;
  214. }
  215. }
  216. else
  217. {
  218. mode = CRYS_AESCCM_MODE_STAR;
  219. /* Allowed MAC size in CCM* mode: [0, 4, 8, 16] */
  220. if ((mac_size | NRF_CRYPTO_AES_CCM_STAR_MAC_BITMASK) != NRF_CRYPTO_AES_CCM_STAR_MAC_BITMASK)
  221. {
  222. ret_val = NRF_ERROR_CRYPTO_AEAD_MAC_SIZE;
  223. goto exit;
  224. }
  225. /* Allowed nonce size in CCM* mode: [13] */
  226. if (nonce_size != NRF_CRYPTO_AES_CCM_STAR_NONCE_SIZE)
  227. {
  228. ret_val = NRF_ERROR_CRYPTO_AEAD_NONCE_SIZE;
  229. goto exit;
  230. }
  231. }
  232. result = CC_AESCCM_Init(&p_ctx->context,
  233. operation_cc310,
  234. p_ctx->key,
  235. CRYS_AES_Key128BitSize, // the only allowed key size for CC310
  236. (uint32_t)adata_size,
  237. (uint32_t)data_in_size,
  238. p_nonce,
  239. nonce_size,
  240. mac_size,
  241. mode);
  242. ret_val = result_get(result);
  243. if (ret_val != NRF_SUCCESS)
  244. {
  245. goto exit;
  246. }
  247. if ((adata_size > 0) && (p_adata != NULL))
  248. {
  249. result = CRYS_AESCCM_BlockAdata(&p_ctx->context,
  250. p_adata,
  251. (uint32_t)adata_size);
  252. ret_val = result_get(result);
  253. if (ret_val != NRF_SUCCESS)
  254. {
  255. goto exit;
  256. }
  257. }
  258. /* CC310 backend always needs 16 bytes buffer for MAC calculation. */
  259. memcpy(mac_buffer, p_mac, mac_size);
  260. result = CRYS_AESCCM_Finish(&p_ctx->context,
  261. p_data_in,
  262. (uint32_t)data_in_size,
  263. p_data_out,
  264. mac_buffer,
  265. &mac_size);
  266. ret_val = result_get(result);
  267. if (ret_val == NRF_SUCCESS)
  268. {
  269. memcpy(p_mac, mac_buffer, mac_size);
  270. }
  271. exit:
  272. cc310_backend_mutex_unlock();
  273. return ret_val;
  274. }
  275. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_AES_CCM)
  276. nrf_crypto_aead_info_t const g_nrf_crypto_aes_ccm_128_info =
  277. {
  278. .key_size = NRF_CRYPTO_KEY_SIZE_128,
  279. .mode = NRF_CRYPTO_AEAD_MODE_AES_CCM,
  280. .init_fn = backend_cc310_init,
  281. .uninit_fn = backend_cc310_uninit,
  282. .crypt_fn = backend_cc310_crypt
  283. };
  284. #endif
  285. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CC310_AES_CCM_STAR)
  286. nrf_crypto_aead_info_t const g_nrf_crypto_aes_ccm_star_128_info =
  287. {
  288. .key_size = NRF_CRYPTO_KEY_SIZE_128,
  289. .mode = NRF_CRYPTO_AEAD_MODE_AES_CCM_STAR,
  290. .init_fn = backend_cc310_init,
  291. .uninit_fn = backend_cc310_uninit,
  292. .crypt_fn = backend_cc310_crypt
  293. };
  294. #endif
  295. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_CC310_AES_AEAD)
  296. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO)