nrf_crypto_aes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * Copyright (c) 2018, 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_aes.h"
  43. #include "nrf_crypto_mem.h"
  44. #include "nrf_crypto_error.h"
  45. #include "nrf_crypto_shared.h"
  46. #include "nrf_crypto_aes_shared.h"
  47. #include "nrf_crypto_aes_backend.h"
  48. #if NRF_MODULE_ENABLED(NRF_CRYPTO_AES)
  49. static ret_code_t context_verify(nrf_crypto_aes_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_value != NRF_CRYPTO_AES_INIT_MAGIC_VALUE)
  56. {
  57. return NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED;
  58. }
  59. return NRF_SUCCESS;
  60. }
  61. ret_code_t nrf_crypto_aes_init(nrf_crypto_aes_context_t * const p_context,
  62. nrf_crypto_aes_info_t const * const p_info,
  63. nrf_crypto_operation_t operation)
  64. {
  65. ret_code_t ret_val;
  66. nrf_crypto_aes_internal_context_t * p_int_context =
  67. (nrf_crypto_aes_internal_context_t *)p_context;
  68. ret_val = context_verify(p_int_context);
  69. VERIFY_TRUE((ret_val == NRF_SUCCESS) || (ret_val == NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED),
  70. ret_val);
  71. VERIFY_TRUE(p_info != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  72. p_int_context->p_info = p_info;
  73. ret_val = p_info->init_fn(p_context, operation);
  74. if (ret_val == NRF_SUCCESS)
  75. {
  76. p_int_context->init_value = NRF_CRYPTO_AES_INIT_MAGIC_VALUE;
  77. }
  78. return ret_val;
  79. }
  80. ret_code_t nrf_crypto_aes_uninit(nrf_crypto_aes_context_t * const p_context)
  81. {
  82. ret_code_t ret_val;
  83. nrf_crypto_aes_internal_context_t * p_int_context =
  84. (nrf_crypto_aes_internal_context_t *)p_context;
  85. ret_val = context_verify(p_int_context);
  86. if (ret_val == NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED)
  87. {
  88. /* If context was uninitialized with function nrf_crypto_aes_finalize it shall be still
  89. possible to clear init_value */
  90. if (p_int_context->init_value == NRF_CRYPTO_AES_UNINIT_MAGIC_VALUE)
  91. {
  92. ret_val = NRF_SUCCESS;
  93. }
  94. }
  95. VERIFY_SUCCESS(ret_val);
  96. ret_val = p_int_context->p_info->uninit_fn(p_context);
  97. p_int_context->init_value = 0;
  98. return ret_val;
  99. }
  100. ret_code_t nrf_crypto_aes_key_set(nrf_crypto_aes_context_t * const p_context, uint8_t * p_key)
  101. {
  102. ret_code_t ret_val;
  103. nrf_crypto_aes_internal_context_t * p_int_context =
  104. (nrf_crypto_aes_internal_context_t *)p_context;
  105. ret_val = context_verify(p_int_context);
  106. VERIFY_SUCCESS(ret_val);
  107. VERIFY_TRUE((p_key != NULL), NRF_ERROR_CRYPTO_INPUT_NULL);
  108. ret_val = p_int_context->p_info->key_set_fn(p_context, p_key);
  109. return ret_val;
  110. }
  111. ret_code_t nrf_crypto_aes_iv_set(nrf_crypto_aes_context_t * const p_context, uint8_t * p_iv)
  112. {
  113. ret_code_t ret_val;
  114. nrf_crypto_aes_internal_context_t * p_int_context =
  115. (nrf_crypto_aes_internal_context_t *)p_context;
  116. ret_val = context_verify(p_int_context);
  117. VERIFY_SUCCESS(ret_val);
  118. VERIFY_TRUE((p_int_context->p_info->iv_set_fn != NULL), NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE);
  119. VERIFY_TRUE((p_iv != NULL), NRF_ERROR_CRYPTO_INPUT_NULL);
  120. ret_val = p_int_context->p_info->iv_set_fn(p_context, p_iv);
  121. return ret_val;
  122. }
  123. ret_code_t nrf_crypto_aes_iv_get(nrf_crypto_aes_context_t * const p_context, uint8_t * p_iv)
  124. {
  125. ret_code_t ret_val;
  126. nrf_crypto_aes_internal_context_t * p_int_context =
  127. (nrf_crypto_aes_internal_context_t *)p_context;
  128. ret_val = context_verify(p_int_context);
  129. if (ret_val == NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED)
  130. {
  131. /* If context was uninitialized with function nrf_crypto_aes_finalize it shall be still
  132. possible to read IV value */
  133. if (p_int_context->init_value == NRF_CRYPTO_AES_UNINIT_MAGIC_VALUE)
  134. {
  135. ret_val = NRF_SUCCESS;
  136. }
  137. }
  138. VERIFY_SUCCESS(ret_val);
  139. VERIFY_TRUE((p_iv != NULL), NRF_ERROR_CRYPTO_INPUT_NULL);
  140. VERIFY_TRUE((p_int_context->p_info->iv_get_fn != NULL), NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE);
  141. ret_val = p_int_context->p_info->iv_get_fn(p_context, p_iv);
  142. return ret_val;
  143. }
  144. ret_code_t nrf_crypto_aes_update(nrf_crypto_aes_context_t * const p_context,
  145. uint8_t * p_data_in,
  146. size_t data_size,
  147. uint8_t * p_data_out)
  148. {
  149. ret_code_t ret_val;
  150. nrf_crypto_aes_internal_context_t * p_int_context =
  151. (nrf_crypto_aes_internal_context_t *)p_context;
  152. ret_val = context_verify(p_int_context);
  153. VERIFY_SUCCESS(ret_val);
  154. VERIFY_TRUE((data_size != 0), NRF_ERROR_CRYPTO_INPUT_LENGTH);
  155. VERIFY_TRUE((p_data_in != NULL), NRF_ERROR_CRYPTO_INPUT_NULL);
  156. VERIFY_TRUE((p_data_out != NULL), NRF_ERROR_CRYPTO_OUTPUT_NULL);
  157. if ((data_size & 0xF) != 0)
  158. {
  159. VERIFY_TRUE((p_int_context->p_info->mode == NRF_CRYPTO_AES_MODE_CFB),
  160. NRF_ERROR_CRYPTO_INPUT_LENGTH);
  161. }
  162. ret_val = p_int_context->p_info->update_fn(p_context,
  163. p_data_in,
  164. data_size,
  165. p_data_out);
  166. return ret_val;
  167. }
  168. ret_code_t nrf_crypto_aes_finalize(nrf_crypto_aes_context_t * const p_context,
  169. uint8_t * p_data_in,
  170. size_t data_size,
  171. uint8_t * p_data_out,
  172. size_t * p_data_out_size)
  173. {
  174. ret_code_t ret_val;
  175. nrf_crypto_aes_internal_context_t * p_int_context =
  176. (nrf_crypto_aes_internal_context_t *)p_context;
  177. ret_val = context_verify(p_int_context);
  178. VERIFY_SUCCESS(ret_val);
  179. VERIFY_TRUE((p_data_in != NULL), NRF_ERROR_CRYPTO_INPUT_NULL);
  180. VERIFY_TRUE((p_data_out != NULL), NRF_ERROR_CRYPTO_OUTPUT_NULL);
  181. VERIFY_TRUE((p_data_out_size != NULL), NRF_ERROR_CRYPTO_OUTPUT_NULL);
  182. ret_val = p_int_context->p_info->finalize_fn(p_context,
  183. p_data_in,
  184. data_size,
  185. p_data_out,
  186. p_data_out_size);
  187. VERIFY_TRUE((ret_val == NRF_SUCCESS), ret_val);
  188. ret_val = nrf_crypto_aes_uninit(p_context);
  189. if (ret_val == NRF_SUCCESS)
  190. {
  191. /* This line will allow to read IV for AES supporting IV get function. */
  192. p_int_context->init_value = NRF_CRYPTO_AES_UNINIT_MAGIC_VALUE;
  193. }
  194. return ret_val;
  195. }
  196. ret_code_t nrf_crypto_aes_crypt(nrf_crypto_aes_context_t * const p_context,
  197. nrf_crypto_aes_info_t const * const p_info,
  198. nrf_crypto_operation_t operation,
  199. uint8_t * p_key,
  200. uint8_t * p_iv,
  201. uint8_t * p_data_in,
  202. size_t data_size,
  203. uint8_t * p_data_out,
  204. size_t * p_data_out_size)
  205. {
  206. ret_code_t ret_val;
  207. void * p_allocated_context = NULL;
  208. nrf_crypto_aes_context_t * p_ctx = p_context;
  209. VERIFY_TRUE(p_info != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  210. if (p_ctx == NULL)
  211. {
  212. p_allocated_context = NRF_CRYPTO_ALLOC(p_info->context_size);
  213. if (p_allocated_context == NULL)
  214. {
  215. return NRF_ERROR_CRYPTO_ALLOC_FAILED;
  216. }
  217. p_ctx = (nrf_crypto_aes_context_t *)p_allocated_context;
  218. }
  219. ret_val = nrf_crypto_aes_init(p_ctx, p_info, operation);
  220. NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(ret_val, p_allocated_context);
  221. ret_val = nrf_crypto_aes_key_set(p_ctx, p_key);
  222. NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(ret_val, p_allocated_context);
  223. ret_val = nrf_crypto_aes_iv_set(p_ctx, p_iv);
  224. /* not all AES modes support IV */
  225. if (ret_val != NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE)
  226. {
  227. NRF_CRYPTO_VERIFY_SUCCESS_DEALLOCATE(ret_val, p_allocated_context);
  228. }
  229. ret_val = nrf_crypto_aes_finalize(p_ctx,
  230. p_data_in,
  231. data_size,
  232. p_data_out,
  233. p_data_out_size);
  234. if (ret_val != NRF_SUCCESS)
  235. {
  236. /* Context was not successfully deinitialized in nrf_crypto_aes_finalize */
  237. UNUSED_RETURN_VALUE(nrf_crypto_aes_uninit(p_ctx));
  238. }
  239. if (p_allocated_context != NULL)
  240. {
  241. NRF_CRYPTO_FREE(p_allocated_context);
  242. }
  243. return ret_val;
  244. }
  245. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_AES)
  246. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO)