cifra_backend_aes_aead.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <stdbool.h>
  43. #include "cifra_backend_aes_aead.h"
  44. #if NRF_MODULE_ENABLED(NRF_CRYPTO_CIFRA_AES_AEAD)
  45. /**@internal @brief Type declaration of a template matching all possible context sizes
  46. * for this backend.
  47. */
  48. typedef struct
  49. {
  50. nrf_crypto_aead_internal_context_t header; /**< Common header for context. */
  51. cf_aes_context context;
  52. } nrf_crypto_backend_cifra_aes_aead_context_t;
  53. static ret_code_t result_get(int error)
  54. {
  55. switch (error)
  56. {
  57. case 0:
  58. return NRF_SUCCESS;
  59. case 1:
  60. return NRF_ERROR_CRYPTO_AEAD_INVALID_MAC;
  61. default:
  62. return NRF_ERROR_CRYPTO_INTERNAL;
  63. }
  64. }
  65. static ret_code_t backend_cifra_init(void * const p_context, uint8_t * p_key)
  66. {
  67. nrf_crypto_backend_cifra_aes_aead_context_t * p_ctx =
  68. (nrf_crypto_backend_cifra_aes_aead_context_t *)p_context;
  69. if ((p_ctx->header.p_info->key_size != NRF_CRYPTO_KEY_SIZE_128) &&
  70. (p_ctx->header.p_info->key_size != NRF_CRYPTO_KEY_SIZE_192) &&
  71. (p_ctx->header.p_info->key_size != NRF_CRYPTO_KEY_SIZE_256))
  72. {
  73. return NRF_ERROR_CRYPTO_KEY_SIZE;
  74. }
  75. VERIFY_TRUE((p_ctx->header.p_info->mode == NRF_CRYPTO_AEAD_MODE_AES_EAX),
  76. NRF_ERROR_CRYPTO_FEATURE_UNAVAILABLE);
  77. cf_aes_init(&p_ctx->context,
  78. p_key,
  79. (p_ctx->header.p_info->key_size)>>3); // >>3: changes bits to bytes
  80. return NRF_SUCCESS;
  81. }
  82. static ret_code_t backend_cifra_uninit(void * const p_context)
  83. {
  84. nrf_crypto_backend_cifra_aes_aead_context_t * p_ctx =
  85. (nrf_crypto_backend_cifra_aes_aead_context_t *)p_context;
  86. cf_aes_finish(&p_ctx->context);
  87. return NRF_SUCCESS;
  88. }
  89. static ret_code_t backend_cifra_crypt(void * const p_context,
  90. nrf_crypto_operation_t operation,
  91. uint8_t * p_nonce,
  92. uint8_t nonce_size,
  93. uint8_t * p_adata,
  94. size_t adata_size,
  95. uint8_t * p_data_in,
  96. size_t data_in_size,
  97. uint8_t * p_data_out,
  98. uint8_t * p_mac,
  99. uint8_t mac_size)
  100. {
  101. int result;
  102. ret_code_t ret_val;
  103. nrf_crypto_backend_cifra_aes_aead_context_t * p_ctx =
  104. (nrf_crypto_backend_cifra_aes_aead_context_t *)p_context;
  105. ret_val = NRF_SUCCESS;
  106. /* EAX mode allows following mac size: [1 ... 16] */
  107. if ((mac_size < 1) || (mac_size > NRF_CRYPTO_AES_BLOCK_SIZE))
  108. {
  109. return NRF_ERROR_CRYPTO_AEAD_MAC_SIZE;
  110. }
  111. if (operation == NRF_CRYPTO_ENCRYPT)
  112. {
  113. cf_eax_encrypt(&cf_aes,
  114. &p_ctx->context,
  115. p_data_in,
  116. data_in_size,
  117. p_adata,
  118. adata_size,
  119. p_nonce,
  120. (size_t)nonce_size,
  121. p_data_out,
  122. p_mac,
  123. mac_size);
  124. }
  125. else if (operation == NRF_CRYPTO_DECRYPT)
  126. {
  127. result = cf_eax_decrypt(&cf_aes,
  128. &p_ctx->context,
  129. p_data_in,
  130. data_in_size,
  131. p_adata,
  132. adata_size,
  133. p_nonce,
  134. (size_t)nonce_size,
  135. p_mac,
  136. mac_size,
  137. p_data_out);
  138. ret_val = result_get(result);
  139. }
  140. else
  141. {
  142. return NRF_ERROR_CRYPTO_INVALID_PARAM;
  143. }
  144. return ret_val;
  145. }
  146. #if NRF_MODULE_ENABLED(NRF_CRYPTO_BACKEND_CIFRA_AES_EAX)
  147. nrf_crypto_aead_info_t const g_nrf_crypto_aes_eax_128_info =
  148. {
  149. .key_size = NRF_CRYPTO_KEY_SIZE_128,
  150. .mode = NRF_CRYPTO_AEAD_MODE_AES_EAX,
  151. .init_fn = backend_cifra_init,
  152. .uninit_fn = backend_cifra_uninit,
  153. .crypt_fn = backend_cifra_crypt
  154. };
  155. nrf_crypto_aead_info_t const g_nrf_crypto_aes_eax_192_info =
  156. {
  157. .key_size = NRF_CRYPTO_KEY_SIZE_192,
  158. .mode = NRF_CRYPTO_AEAD_MODE_AES_EAX,
  159. .init_fn = backend_cifra_init,
  160. .uninit_fn = backend_cifra_uninit,
  161. .crypt_fn = backend_cifra_crypt
  162. };
  163. nrf_crypto_aead_info_t const g_nrf_crypto_aes_eax_256_info =
  164. {
  165. .key_size = NRF_CRYPTO_KEY_SIZE_256,
  166. .mode = NRF_CRYPTO_AEAD_MODE_AES_EAX,
  167. .init_fn = backend_cifra_init,
  168. .uninit_fn = backend_cifra_uninit,
  169. .crypt_fn = backend_cifra_crypt
  170. };
  171. #endif
  172. #endif // MODULE_ENABLED(NRF_CRYPTO_AES_CCM_BACKEND_MBEDTLS)
  173. #endif // MODULE_ENABLED(NRF_CRYPTO)