nrf_crypto_aes_shared.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #ifndef NRF_CRYPTO_AES_SHARED_H__
  41. #define NRF_CRYPTO_AES_SHARED_H__
  42. /** @file
  43. *
  44. * @defgroup nrf_crypto_aes AES related functions
  45. * @{
  46. * @ingroup nrf_crypto
  47. *
  48. * @brief Provides AES related functionality through nrf_crypto.
  49. */
  50. #include <stdint.h>
  51. #include "nrf_crypto_types.h"
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /**@internal @brief Magic value to signal that the nrf_crypto_hash context structure is initialized.
  56. */
  57. #define NRF_CRYPTO_AES_INIT_MAGIC_VALUE (0x53454163) // ASCII "cAES"
  58. #define NRF_CRYPTO_AES_UNINIT_MAGIC_VALUE (0x63414553) // ASCII "SEAc"
  59. #define NRF_CRYPTO_MBEDTLS_AES_IV_SIZE (16)
  60. /** @internal @brief Enumeration of supported modes of operation in nrf_crypto_aes.
  61. */
  62. typedef enum
  63. {
  64. NRF_CRYPTO_AES_MODE_CBC, // supported by: MBEDTLS & CC310
  65. NRF_CRYPTO_AES_MODE_CBC_PAD_PCKS7, // supported by: MBEDTLS & CC310
  66. NRF_CRYPTO_AES_MODE_CFB, // supported by: MBEDTLS
  67. NRF_CRYPTO_AES_MODE_CTR, // supported by: MBEDTLS & CC310
  68. NRF_CRYPTO_AES_MODE_ECB, // supported by: MBEDTLS & CC310
  69. NRF_CRYPTO_AES_MODE_ECB_PAD_PCKS7, // supported by: MBEDTLS & CC310
  70. // Authentication modes
  71. NRF_CRYPTO_AES_MODE_CBC_MAC, // supported by: MBEDTLS & CC310
  72. NRF_CRYPTO_AES_MODE_CBC_MAC_PAD_PCKS7, // supported by: MBEDTLS & CC310
  73. NRF_CRYPTO_AES_MODE_CMAC, // supported by: MBEDTLS & CC310
  74. } nrf_crypto_aes_mode_t;
  75. /**@internal @brief Type declaration to perform AES initialization in the nrf_crypto backend.
  76. *
  77. * This is internal API. See @ref nrf_crypto_aes_init for documentation.
  78. */
  79. typedef ret_code_t (*aes_init_fn_t)(void * const p_context, nrf_crypto_operation_t operation);
  80. /**@internal @brief Type declaration to perform AES uninitialization in the nrf_crypto backend.
  81. *
  82. * This is internal API. See @ref nrf_crypto_aes_uninit for documentation.
  83. */
  84. typedef ret_code_t (*aes_uninit_fn_t)(void * const p_context);
  85. /**@internal @brief Type declaration to set an AES key in the nrf_crypto backend.
  86. *
  87. * This is internal API. See @ref nrf_crypto_aes_key_set for documentation.
  88. */
  89. typedef ret_code_t (*aes_key_set_fn_t)(void * const p_context, uint8_t * p_key);
  90. /**@internal @brief Type declaration to set an AES IV in the nrf_crypto backend.
  91. *
  92. * This is internal API. See @ref nrf_crypto_aes_iv_set for documentation.
  93. */
  94. typedef ret_code_t (*aes_iv_set_fn_t)(void * const p_context, uint8_t * p_iv);
  95. /**@internal @brief Type declaration to get an AES IV in the nrf_crypto backend.
  96. *
  97. * This is internal API. See @ref nrf_crypto_aes_iv_get for documentation.
  98. */
  99. typedef ret_code_t (*aes_iv_get_fn_t)(void * const p_context, uint8_t * p_iv);
  100. /**@internal @brief Type declaration to perform AES block operation in the nrf_crypto backend.
  101. *
  102. * This is internal API. See @ref nrf_crypto_aes_update for documentation.
  103. */
  104. typedef ret_code_t (*aes_update_fn_t)(void * const p_context,
  105. uint8_t * p_data_in,
  106. size_t data_size,
  107. uint8_t * p_data_out);
  108. /**@internal @brief Type declaration to finalize AES operation in the nrf_crypto backend.
  109. *
  110. * This is internal API. See @ref nrf_crypto_aes_finalize for documentation.
  111. */
  112. typedef ret_code_t (*aes_finalize_fn_t)(void * const p_context,
  113. uint8_t * p_data_in,
  114. size_t data_size,
  115. uint8_t * p_data_out,
  116. size_t * p_data_out_size);
  117. /**@internal @brief Type declaration for an nrf_crypto_aes info structure.
  118. *
  119. * @details This structure contains the calling interface and any metadata required
  120. * to call the nrf_crypto_aes API functions.
  121. */
  122. typedef struct
  123. {
  124. nrf_crypto_aes_mode_t const mode;
  125. nrf_crypto_key_size_id_t const key_size;
  126. size_t const context_size;
  127. aes_init_fn_t const init_fn;
  128. aes_uninit_fn_t const uninit_fn;
  129. aes_key_set_fn_t const key_set_fn;
  130. aes_iv_set_fn_t const iv_set_fn;
  131. aes_iv_get_fn_t const iv_get_fn;
  132. aes_update_fn_t const update_fn;
  133. aes_finalize_fn_t const finalize_fn;
  134. } nrf_crypto_aes_info_t;
  135. /**@internal @brief Type declaration of internal representation of an AES context structure.
  136. *
  137. * @details This is an internal type that should not be used directly.
  138. */
  139. typedef struct
  140. {
  141. uint32_t init_value;
  142. nrf_crypto_aes_info_t const * p_info;
  143. } nrf_crypto_aes_internal_context_t;
  144. /**@internal @brief Type declaration of internal representation of an AES backend context structure.
  145. * with initialization vector.
  146. *
  147. * @details This is an internal type that should not be used directly.
  148. */
  149. typedef struct
  150. {
  151. nrf_crypto_operation_t operation;
  152. uint8_t iv[NRF_CRYPTO_MBEDTLS_AES_IV_SIZE]; // space for 128-bit initialization vector
  153. } nrf_crypto_backend_aes_ctx_t;
  154. /**@internal @brief Type declaration of internal representation of an AES backend context structure
  155. * without initialization vector.
  156. *
  157. * @details This is an internal type that should not be used directly.
  158. */
  159. typedef struct
  160. {
  161. nrf_crypto_operation_t operation;
  162. } nrf_crypto_backend_no_iv_aes_ctx_t;
  163. /**@internal @brief Function copies remainders (msg_ending_len) from p_message_buff to the
  164. * p_padding_buff. Next it adds pkcs7-padding to have a 16 bytes p_padding_buff.
  165. *
  166. * @param[in] p_padding_buff Pointer the buffer with padded message.
  167. * @param[in] p_message_buff Pointer to the buffer with message that must be padded.
  168. * @param[in] msg_ending_len Message remainders length.
  169. *
  170. */
  171. ret_code_t padding_pkcs7_add(uint8_t * p_padding_buff,
  172. uint8_t * p_message_buff,
  173. uint8_t msg_ending_len);
  174. /**@internal @brief Function calculate message length without padding.
  175. *
  176. * @param[in] p_padded_message Pointer the buffer with padded message.
  177. * @param[in/out] p_message_len IN: padded message length
  178. * OUT: message length without padding
  179. */
  180. ret_code_t padding_pkcs7_remove(uint8_t * p_padded_message,
  181. size_t * p_message_len);
  182. #ifdef __cplusplus
  183. }
  184. #endif
  185. /** @} */
  186. #endif // #ifndef NRF_CRYPTO_AES_SHARED_H__