nrf_ecb.h 9.5 KB

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