nrf_ecb.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * Copyright (c) 2012 - 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_ECB_H__
  41. #define NRF_ECB_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_ecb_drv AES ECB encryption driver
  48. * @{
  49. * @ingroup nrf_ecb
  50. * @brief Driver for the Advanced Encryption Standard (AES) Electronic Code Book (ECB) peripheral.
  51. *
  52. * To encrypt data, the peripheral must first be powered on
  53. * using @ref nrf_ecb_init. Next, the key must be set using @ref nrf_ecb_set_key.
  54. */
  55. /**
  56. * @brief Function for initializing and powering on the ECB peripheral.
  57. *
  58. * This function allocates memory for the ECBDATAPTR.
  59. *
  60. * @retval true The initialization was successful.
  61. * @retval false The power-on failed.
  62. */
  63. bool nrf_ecb_init(void);
  64. /**
  65. * @brief Function for encrypting 16-byte data using current key.
  66. *
  67. * This function avoids unnecessary copying of data if the parameters point to the
  68. * correct locations in the ECB data structure.
  69. *
  70. * @param dst Result of encryption, 16 bytes will be written.
  71. * @param src Source with 16-byte data to be encrypted.
  72. *
  73. * @retval true The encryption operation completed.
  74. * @retval false The encryption operation did not complete.
  75. */
  76. bool nrf_ecb_crypt(uint8_t * dst, const uint8_t * src);
  77. /**
  78. * @brief Function for setting the key to be used for encryption.
  79. *
  80. * @param key Pointer to the key. 16 bytes will be read.
  81. */
  82. void nrf_ecb_set_key(const uint8_t * key);
  83. /** @} */
  84. /**
  85. * @defgroup nrf_ecb_hal AES ECB encryption HAL
  86. * @{
  87. * @ingroup nrf_ecb
  88. * @brief Hardware access layer (HAL) for managing the Advanced Encryption Standard (AES) Electronic Codebook (ECB) peripheral.
  89. */
  90. /** @brief ECB tasks. */
  91. typedef enum
  92. {
  93. NRF_ECB_TASK_STARTECB = offsetof(NRF_ECB_Type, TASKS_STARTECB), /**< Task for starting the ECB block encryption. */
  94. NRF_ECB_TASK_STOPECB = offsetof(NRF_ECB_Type, TASKS_STOPECB), /**< Task for stopping the ECB block encryption. */
  95. } nrf_ecb_task_t;
  96. /** @brief ECB events. */
  97. typedef enum
  98. {
  99. NRF_ECB_EVENT_ENDECB = offsetof(NRF_ECB_Type, EVENTS_ENDECB), /**< ECB block encrypt complete. */
  100. NRF_ECB_EVENT_ERRORECB = offsetof(NRF_ECB_Type, EVENTS_ERRORECB), /**< ECB block encrypt aborted because of a STOPECB task or due to an error. */
  101. } nrf_ecb_event_t;
  102. /** @brief ECB interrupts. */
  103. typedef enum
  104. {
  105. NRF_ECB_INT_ENDECB_MASK = ECB_INTENSET_ENDECB_Msk, ///< Interrupt on ENDECB event.
  106. NRF_ECB_INT_ERRORECB_MASK = ECB_INTENSET_ERRORECB_Msk, ///< Interrupt on ERRORECB event.
  107. } nrf_ecb_int_mask_t;
  108. /**
  109. * @brief Function for activating the specified ECB task.
  110. *
  111. * @param[in] p_reg Pointer to the peripheral register structure.
  112. * @param[in] task Task to be activated.
  113. */
  114. __STATIC_INLINE void nrf_ecb_task_trigger(NRF_ECB_Type * p_reg, nrf_ecb_task_t task);
  115. /**
  116. * @brief Function for getting the address of the specified ECB task register.
  117. *
  118. * @param[in] p_reg Pointer to the peripheral register structure.
  119. * @param[in] task Requested task.
  120. *
  121. * @return Address of the specified task register.
  122. */
  123. __STATIC_INLINE uint32_t nrf_ecb_task_address_get(NRF_ECB_Type const * p_reg,
  124. nrf_ecb_task_t task);
  125. /**
  126. * @brief Function for clearing the specified ECB event.
  127. *
  128. * @param[in] p_reg Pointer to the peripheral register structure.
  129. * @param[in] event Event to clear.
  130. */
  131. __STATIC_INLINE void nrf_ecb_event_clear(NRF_ECB_Type * p_reg, nrf_ecb_event_t event);
  132. /**
  133. * @brief Function for retrieving the state of the ECB event.
  134. *
  135. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  136. * @param[in] event Event to be checked.
  137. *
  138. * @retval true The event has been generated.
  139. * @retval false The event has not been generated.
  140. */
  141. __STATIC_INLINE bool nrf_ecb_event_check(NRF_ECB_Type const * p_reg, nrf_ecb_event_t event);
  142. /**
  143. * @brief Function for getting the address of the specified ECB event register.
  144. *
  145. * @param[in] p_reg Pointer to the peripheral register structure.
  146. * @param[in] event Requested event.
  147. *
  148. * @return Address of the specified event register.
  149. */
  150. __STATIC_INLINE uint32_t nrf_ecb_event_address_get(NRF_ECB_Type const * p_reg,
  151. nrf_ecb_event_t event);
  152. /**
  153. * @brief Function for enabling the specified interrupts.
  154. *
  155. * @param[in] p_reg Pointer to the peripheral register structure.
  156. * @param[in] mask Interrupts to be enabled.
  157. */
  158. __STATIC_INLINE void nrf_ecb_int_enable(NRF_ECB_Type * p_reg, uint32_t mask);
  159. /**
  160. * @brief Function for disabling the specified interrupts.
  161. *
  162. * @param[in] p_reg Pointer to the peripheral register structure.
  163. * @param[in] mask Interrupts to be disabled.
  164. */
  165. __STATIC_INLINE void nrf_ecb_int_disable(NRF_ECB_Type * p_reg, uint32_t mask);
  166. /**
  167. * @brief Function for retrieving the state of a given interrupt.
  168. *
  169. * @param[in] p_reg Pointer to the peripheral register structure.
  170. * @param[in] ecb_int Interrupt to be checked.
  171. *
  172. * @retval true The interrupt is enabled.
  173. * @retval false The interrupt is not enabled.
  174. */
  175. __STATIC_INLINE bool nrf_ecb_int_enable_check(NRF_ECB_Type const * p_reg,
  176. nrf_ecb_int_mask_t ecb_int);
  177. /**
  178. * @brief Function for setting the pointer to the ECB data buffer.
  179. *
  180. * @note The buffer has to be placed in the Data RAM region.
  181. * For description of the data structure in this buffer, see the Product Specification.
  182. *
  183. * @param[in] p_reg Pointer to the peripheral register structure.
  184. * @param[in] p_buffer Pointer to the ECB data buffer.
  185. */
  186. __STATIC_INLINE void nrf_ecb_data_pointer_set(NRF_ECB_Type * p_reg, void const * p_buffer);
  187. /**
  188. * @brief Function for getting the pointer to the ECB data buffer.
  189. *
  190. * @param[in] p_reg Pointer to the peripheral register structure.
  191. *
  192. * @return Pointer to the ECB data buffer.
  193. */
  194. __STATIC_INLINE void * nrf_ecb_data_pointer_get(NRF_ECB_Type const * p_reg);
  195. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  196. __STATIC_INLINE void nrf_ecb_task_trigger(NRF_ECB_Type * p_reg, nrf_ecb_task_t task)
  197. {
  198. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  199. }
  200. __STATIC_INLINE uint32_t nrf_ecb_task_address_get(NRF_ECB_Type const * p_reg,
  201. nrf_ecb_task_t task)
  202. {
  203. return ((uint32_t)p_reg + (uint32_t)task);
  204. }
  205. __STATIC_INLINE void nrf_ecb_event_clear(NRF_ECB_Type * p_reg, nrf_ecb_event_t event)
  206. {
  207. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  208. #if __CORTEX_M == 0x04
  209. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  210. (void)dummy;
  211. #endif
  212. }
  213. __STATIC_INLINE bool nrf_ecb_event_check(NRF_ECB_Type const * p_reg, nrf_ecb_event_t event)
  214. {
  215. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  216. }
  217. __STATIC_INLINE uint32_t nrf_ecb_event_address_get(NRF_ECB_Type const * p_reg,
  218. nrf_ecb_event_t event)
  219. {
  220. return ((uint32_t)p_reg + (uint32_t)event);
  221. }
  222. __STATIC_INLINE void nrf_ecb_int_enable(NRF_ECB_Type * p_reg, uint32_t mask)
  223. {
  224. p_reg->INTENSET = mask;
  225. }
  226. __STATIC_INLINE void nrf_ecb_int_disable(NRF_ECB_Type * p_reg, uint32_t mask)
  227. {
  228. p_reg->INTENCLR = mask;
  229. }
  230. __STATIC_INLINE bool nrf_ecb_int_enable_check(NRF_ECB_Type const * p_reg,
  231. nrf_ecb_int_mask_t ecb_int)
  232. {
  233. return (bool)(p_reg->INTENSET & ecb_int);
  234. }
  235. __STATIC_INLINE void nrf_ecb_data_pointer_set(NRF_ECB_Type * p_reg, void const * p_buffer)
  236. {
  237. p_reg->ECBDATAPTR = (uint32_t)p_buffer;
  238. }
  239. __STATIC_INLINE void * nrf_ecb_data_pointer_get(NRF_ECB_Type const * p_reg)
  240. {
  241. return (void *)(p_reg->ECBDATAPTR);
  242. }
  243. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  244. /** @} */
  245. #ifdef __cplusplus
  246. }
  247. #endif
  248. #endif // NRF_ECB_H__