nrf_crypto_aead.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #ifndef NRF_CRYPTO_AEAD_H__
  41. #define NRF_CRYPTO_AEAD_H__
  42. /** @file
  43. *
  44. * @defgroup nrf_crypto_aead AEAD (Authenticated Encryption with Associated Data) related
  45. * functions.
  46. * @{
  47. * @ingroup nrf_crypto
  48. *
  49. * @brief Provides AEAD related functionality through nrf_crypto.
  50. */
  51. #include "sdk_common.h"
  52. #if NRF_MODULE_ENABLED(NRF_CRYPTO) || defined(__SDK_DOXYGEN__)
  53. #include <stdint.h>
  54. #include "nrf_crypto_types.h"
  55. #include "nrf_crypto_aead_shared.h"
  56. #include "nrf_crypto_aead_backend.h"
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. /**@brief External variable declaration to the info structure for AES CCM mode with a 128-bit key.
  61. *
  62. * @note The variable is defined in the nrf_crypto backend that is enabled in the @c sdk_config file.
  63. *
  64. */
  65. extern const nrf_crypto_aead_info_t g_nrf_crypto_aes_ccm_128_info;
  66. /**@brief External variable declaration to the info structure for AES CCM mode with a 192-bit key.
  67. *
  68. * @note The variable is defined in the nrf_crypto backend that is enabled in the @c sdk_config file.
  69. *
  70. */
  71. extern const nrf_crypto_aead_info_t g_nrf_crypto_aes_ccm_192_info;
  72. /**@brief External variable declaration to the info structure for AES CCM mode with a 256-bit key.
  73. *
  74. * @note The variable is defined in the nrf_crypto backend that is enabled in the @c sdk_config file.
  75. *
  76. */
  77. extern const nrf_crypto_aead_info_t g_nrf_crypto_aes_ccm_256_info;
  78. /**@brief External variable declaration to the info structure for AES CCM* mode with a 128-bit key.
  79. *
  80. * @note The variable is defined in the nrf_crypto backend that is enabled in the @c sdk_config file.
  81. *
  82. */
  83. extern const nrf_crypto_aead_info_t g_nrf_crypto_aes_ccm_star_128_info;
  84. /**@brief External variable declaration to the info structure for AES EAX mode with a 128-bit key.
  85. *
  86. * @note The variable is defined in the nrf_crypto backend that is enabled in the @c sdk_config file.
  87. *
  88. */
  89. extern const nrf_crypto_aead_info_t g_nrf_crypto_aes_eax_128_info;
  90. /**@brief External variable declaration to the info structure for AES EAX mode with a 192-bit key.
  91. *
  92. * @note The variable is defined in the nrf_crypto backend that is enabled in the @c sdk_config file.
  93. *
  94. */
  95. extern const nrf_crypto_aead_info_t g_nrf_crypto_aes_eax_192_info;
  96. /**@brief External variable declaration to the info structure for AES EAX mode with a 256-bit key.
  97. *
  98. * @note The variable is defined in the nrf_crypto backend that is enabled in the @c sdk_config file.
  99. *
  100. */
  101. extern const nrf_crypto_aead_info_t g_nrf_crypto_aes_eax_256_info;
  102. /**@brief External variable declaration to the info structure for AES GCM mode with a 128-bit key.
  103. *
  104. * @note The variable is defined in the nrf_crypto backend that is enabled in the @ref sdk_config.
  105. *
  106. */
  107. extern const nrf_crypto_aead_info_t g_nrf_crypto_aes_gcm_128_info;
  108. /**@brief External variable declaration to the info structure for AES GCM mode with a 192-bit key.
  109. *
  110. * @note The variable is defined in the nrf_crypto backend that is enabled in the @ref sdk_config.
  111. *
  112. */
  113. extern const nrf_crypto_aead_info_t g_nrf_crypto_aes_gcm_192_info;
  114. /**@brief External variable declaration to the info structure for AES GCM mode with a 256-bit key.
  115. *
  116. * @note The variable is defined in the nrf_crypto backend that is enabled in the @ref sdk_config.
  117. *
  118. */
  119. extern const nrf_crypto_aead_info_t g_nrf_crypto_aes_gcm_256_info;
  120. /**@brief External variable declaration to the info structure for CHACHA-POLY mode with a 256-bit
  121. * key.
  122. *
  123. * @note The variable is defined in the nrf_crypto backend that is enabled in the @ref sdk_config.
  124. *
  125. */
  126. extern const nrf_crypto_aead_info_t g_nrf_crypto_chacha_poly_256_info;
  127. /**
  128. * @brief Context type for AEAD.
  129. *
  130. * @note The size of this type is scaled for the largest AEAD backend context that is
  131. * enabled in @ref sdk_config.
  132. */
  133. typedef nrf_crypto_backend_aead_context_t nrf_crypto_aead_context_t;
  134. /**@brief Function for initializing the AEAD calculation context.
  135. *
  136. * @param[in] p_context Pointer to the context object. It must be a context type associated with
  137. * the object provided in the p_info parameter or other memory that can
  138. * hold that context type.
  139. * @param[in] p_info Pointer to structure holding information about: selected AES AEAD mode,
  140. * and key size.
  141. * @param[in] p_key Pointer to AEAD mode key.
  142. *
  143. * @retval NRF_SUCCESS Context was successfully initialized.
  144. */
  145. ret_code_t nrf_crypto_aead_init(nrf_crypto_aead_context_t * const p_context,
  146. nrf_crypto_aead_info_t const * const p_info,
  147. uint8_t * p_key);
  148. /**@brief Function for uninitializing the AEAD calculation context.
  149. *
  150. * @param[in] p_context Pointer to the context object. It must be initialized before function call.
  151. *
  152. * @retval NRF_SUCCESS Context was successfully uninitialized.
  153. */
  154. ret_code_t nrf_crypto_aead_uninit(void * const p_context);
  155. /**@brief Integrated encryption / decryption function.
  156. *
  157. * @param[in] p_context Context object. Must be initialized before the call.
  158. * @param[in] operation Parameter indicating whether an encrypt (NRF_CRYPTO_ENCRYPT) or
  159. * a decrypt (NRF_CRYPTO_DECRYPT) operation shall be performed.
  160. * @param[in] p_nonce Pointer to nonce. For nonce_size == 0 p_nonce can be NULL.
  161. * @param[in] nonce_size Nonce byte size. Valid values for supported modes:
  162. * - CCM [7 ... 13]
  163. * - CCM* [13]
  164. * - EAX nonce size can be any length
  165. * - GCM nonce size can be any length
  166. * - CHACHA-POLY [12]
  167. * @param[in] p_adata Pointer to additional authenticated data (adata).
  168. * @param[in] adata_size Length of additional authenticated data in bytes.
  169. * For CHACHA-POLY mode must be > 0.
  170. * @param[in] p_data_in Pointer to the input data buffer for encryption or decryption.
  171. * @param[in] data_in_size Length of the data in p_data_in buffer in bytes. Size of the
  172. * p_data_out buffer must not be smaller than this value.
  173. * When selecting CC310 backend data_in_size value shall be limited
  174. * to 65535 bytes. Data out buffer must be at least the same length.
  175. * @param[out] p_data_out Pointer to the output buffer where encrypted or decrypted data
  176. * will be stored. Must be at least 'data_in_size' bytes wide.
  177. * - GCM: On encryption, the p_data_out buffer can be the same as
  178. * the p_data_in buffer.
  179. * On decryption, the p_data_out buffer cannot be the same
  180. * as p_data_in buffer. If buffers overlap, the p_data_out
  181. * buffer must trail at least 8 bytes behind the p_data_in
  182. * buffer.
  183. * @param[out] p_mac Pointer to the MAC result buffer. Fo mac_size == 0 p_mac can be NULL.
  184. * @param[in] mac_size MAC byte size. Valid values for supported modes:
  185. * -CCM [4, 6, 8, 10, 12, 14, 16]
  186. * -CCM* [0, 4, 8, 16]
  187. * -EAX [1 ... 16]
  188. * -GCM [4 ... 16]
  189. * -CHACHA-POLY [16]
  190. *
  191. * @retval NRF_SUCCESS Message was successfully encrypted.
  192. */
  193. ret_code_t nrf_crypto_aead_crypt(nrf_crypto_aead_context_t * const p_context,
  194. nrf_crypto_operation_t operation,
  195. uint8_t * p_nonce,
  196. uint8_t nonce_size,
  197. uint8_t * p_adata,
  198. size_t adata_size,
  199. uint8_t * p_data_in,
  200. size_t data_in_size,
  201. uint8_t * p_data_out,
  202. uint8_t * p_mac,
  203. uint8_t mac_size);
  204. #ifdef __cplusplus
  205. }
  206. #endif
  207. #endif // #if NRF_MODULE_ENABLED(NRF_CRYPTO) || defined(__SDK_DOXYGEN__)
  208. /** @} */
  209. #endif // NRF_CRYPTO_AEAD_H__