xref: /nrf52832-nimble/nordic/nrfx/hal/nrf_ecb.h (revision 150812a83cab50279bd772ef6db1bfaf255f2c5b)
1 /*
2  * Copyright (c) 2012 - 2018, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef NRF_ECB_H__
33 #define NRF_ECB_H__
34 
35 #include <nrfx.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @defgroup nrf_ecb_drv AES ECB encryption driver
43  * @{
44  * @ingroup nrf_ecb
45  * @brief   Driver for the AES Electronic Code Book (ECB) peripheral.
46  *
47  * To encrypt data, the peripheral must first be powered on
48  * using @ref nrf_ecb_init. Next, the key must be set using @ref nrf_ecb_set_key.
49  */
50 
51 /**
52  * @brief Function for initializing and powering on the ECB peripheral.
53  *
54  * This function allocates memory for the ECBDATAPTR.
55  * @retval true If initialization was successful.
56  * @retval false If powering on failed.
57  */
58 bool nrf_ecb_init(void);
59 
60 /**
61  * @brief Function for encrypting 16-byte data using current key.
62  *
63  * This function avoids unnecessary copying of data if the parameters point to the
64  * correct locations in the ECB data structure.
65  *
66  * @param dst Result of encryption, 16 bytes will be written.
67  * @param src Source with 16-byte data to be encrypted.
68  *
69  * @retval true  If the encryption operation completed.
70  * @retval false If the encryption operation did not complete.
71  */
72 bool nrf_ecb_crypt(uint8_t * dst, const uint8_t * src);
73 
74 /**
75  * @brief Function for setting the key to be used for encryption.
76  *
77  * @param key Pointer to the key. 16 bytes will be read.
78  */
79 void nrf_ecb_set_key(const uint8_t * key);
80 
81 /** @} */
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 /**
91  * @brief ECB tasks.
92  */
93 typedef enum
94 {
95     /*lint -save -e30 -esym(628,__INTADDR__)*/
96     NRF_ECB_TASK_STARTECB = offsetof(NRF_ECB_Type, TASKS_STARTECB), /**< Task for starting ECB block encryption. */
97     NRF_ECB_TASK_STOPECB  = offsetof(NRF_ECB_Type, TASKS_STOPECB),  /**< Task for stopping ECB block encryption. */
98     /*lint -restore*/
99 } nrf_ecb_task_t;
100 
101 /**
102  * @brief ECB events.
103  */
104 typedef enum
105 {
106     /*lint -save -e30*/
107     NRF_ECB_EVENT_ENDECB   = offsetof(NRF_ECB_Type, EVENTS_ENDECB),   /**< ECB block encrypt complete. */
108     NRF_ECB_EVENT_ERRORECB = offsetof(NRF_ECB_Type, EVENTS_ERRORECB), /**< ECB block encrypt aborted because of a STOPECB task or due to an error. */
109     /*lint -restore*/
110 } nrf_ecb_event_t;
111 
112 /**
113  * @brief ECB interrupts.
114  */
115 typedef enum
116 {
117     NRF_ECB_INT_ENDECB_MASK   = ECB_INTENSET_ENDECB_Msk,   ///< Interrupt on ENDECB event.
118     NRF_ECB_INT_ERRORECB_MASK = ECB_INTENSET_ERRORECB_Msk, ///< Interrupt on ERRORECB event.
119 } nrf_ecb_int_mask_t;
120 
121 
122 /**
123  * @brief Function for activating a specific ECB task.
124  *
125  * @param[in] p_reg Pointer to the peripheral register structure.
126  * @param[in] task  Task to activate.
127  */
128 __STATIC_INLINE void nrf_ecb_task_trigger(NRF_ECB_Type * p_reg, nrf_ecb_task_t task);
129 
130 /**
131  * @brief Function for getting the address of a specific ECB task register.
132  *
133  * @param[in] p_reg Pointer to the peripheral register structure.
134  * @param[in] task  Requested task.
135  *
136  * @return Address of the specified task register.
137  */
138 __STATIC_INLINE uint32_t nrf_ecb_task_address_get(NRF_ECB_Type const * p_reg,
139                                                   nrf_ecb_task_t       task);
140 
141 /**
142  * @brief Function for clearing a specific ECB event.
143  *
144  * @param[in] p_reg Pointer to the peripheral register structure.
145  * @param[in] event Event to clear.
146  */
147 __STATIC_INLINE void nrf_ecb_event_clear(NRF_ECB_Type * p_reg, nrf_ecb_event_t event);
148 
149 /**
150  * @brief Function for checking the state of a specific ECB event.
151  *
152  * @param[in] p_reg Pointer to the peripheral register structure.
153  * @param[in] event Event to check.
154  *
155  * @retval true  If the event is set.
156  * @retval false If the event is not set.
157  */
158 __STATIC_INLINE bool nrf_ecb_event_check(NRF_ECB_Type const * p_reg, nrf_ecb_event_t event);
159 
160 /**
161  * @brief Function for getting the address of a specific ECB event register.
162  *
163  * @param[in] p_reg Pointer to the peripheral register structure.
164  * @param[in] event Requested event.
165  *
166  * @return Address of the specified event register.
167  */
168 __STATIC_INLINE uint32_t nrf_ecb_event_address_get(NRF_ECB_Type const * p_reg,
169                                                    nrf_ecb_event_t      event);
170 
171 /**
172  * @brief Function for enabling specified interrupts.
173  *
174  * @param[in] p_reg Pointer to the peripheral register structure.
175  * @param[in] mask  Interrupts to enable.
176  */
177 __STATIC_INLINE void nrf_ecb_int_enable(NRF_ECB_Type * p_reg, uint32_t mask);
178 
179 /**
180  * @brief Function for disabling specified interrupts.
181  *
182  * @param[in] p_reg Pointer to the peripheral register structure.
183  * @param[in] mask  Interrupts to disable.
184  */
185 __STATIC_INLINE void nrf_ecb_int_disable(NRF_ECB_Type * p_reg, uint32_t mask);
186 
187 /**
188  * @brief Function for retrieving the state of a given interrupt.
189  *
190  * @param[in] p_reg   Pointer to the peripheral register structure.
191  * @param[in] ecb_int Interrupt to check.
192  *
193  * @retval true  If the interrupt is enabled.
194  * @retval false If the interrupt is not enabled.
195  */
196 __STATIC_INLINE bool nrf_ecb_int_enable_check(NRF_ECB_Type const * p_reg,
197                                               nrf_ecb_int_mask_t   ecb_int);
198 
199 /**
200  * @brief Function for setting the pointer to the ECB data buffer.
201  *
202  * @note The buffer has to be placed in the Data RAM region.
203  *       For description of the data structure in this buffer, see the Product Specification.
204  *
205  * @param[in] p_reg    Pointer to the peripheral register structure.
206  * @param[in] p_buffer Pointer to the ECB data buffer.
207  */
208 __STATIC_INLINE void nrf_ecb_data_pointer_set(NRF_ECB_Type * p_reg, void const * p_buffer);
209 
210 /**
211  * @brief Function for getting the pointer to the ECB data buffer.
212  *
213  * @param[in] p_reg Pointer to the peripheral register structure.
214  *
215  * @return Pointer to the ECB data buffer.
216  */
217 __STATIC_INLINE void * nrf_ecb_data_pointer_get(NRF_ECB_Type const * p_reg);
218 
219 #ifndef SUPPRESS_INLINE_IMPLEMENTATION
220 
nrf_ecb_task_trigger(NRF_ECB_Type * p_reg,nrf_ecb_task_t task)221 __STATIC_INLINE void nrf_ecb_task_trigger(NRF_ECB_Type * p_reg, nrf_ecb_task_t task)
222 {
223     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
224 }
225 
nrf_ecb_task_address_get(NRF_ECB_Type const * p_reg,nrf_ecb_task_t task)226 __STATIC_INLINE uint32_t nrf_ecb_task_address_get(NRF_ECB_Type const * p_reg,
227                                                   nrf_ecb_task_t       task)
228 {
229     return ((uint32_t)p_reg + (uint32_t)task);
230 }
231 
nrf_ecb_event_clear(NRF_ECB_Type * p_reg,nrf_ecb_event_t event)232 __STATIC_INLINE void nrf_ecb_event_clear(NRF_ECB_Type * p_reg, nrf_ecb_event_t event)
233 {
234     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
235 #if __CORTEX_M == 0x04
236     volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
237     (void)dummy;
238 #endif
239 }
240 
nrf_ecb_event_check(NRF_ECB_Type const * p_reg,nrf_ecb_event_t event)241 __STATIC_INLINE bool nrf_ecb_event_check(NRF_ECB_Type const * p_reg, nrf_ecb_event_t event)
242 {
243     return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
244 }
245 
nrf_ecb_event_address_get(NRF_ECB_Type const * p_reg,nrf_ecb_event_t event)246 __STATIC_INLINE uint32_t nrf_ecb_event_address_get(NRF_ECB_Type const * p_reg,
247                                                    nrf_ecb_event_t      event)
248 {
249     return ((uint32_t)p_reg + (uint32_t)event);
250 }
251 
nrf_ecb_int_enable(NRF_ECB_Type * p_reg,uint32_t mask)252 __STATIC_INLINE void nrf_ecb_int_enable(NRF_ECB_Type * p_reg, uint32_t mask)
253 {
254     p_reg->INTENSET = mask;
255 }
256 
nrf_ecb_int_disable(NRF_ECB_Type * p_reg,uint32_t mask)257 __STATIC_INLINE void nrf_ecb_int_disable(NRF_ECB_Type * p_reg, uint32_t mask)
258 {
259     p_reg->INTENCLR = mask;
260 }
261 
nrf_ecb_int_enable_check(NRF_ECB_Type const * p_reg,nrf_ecb_int_mask_t ecb_int)262 __STATIC_INLINE bool nrf_ecb_int_enable_check(NRF_ECB_Type const * p_reg,
263                                               nrf_ecb_int_mask_t   ecb_int)
264 {
265     return (bool)(p_reg->INTENSET & ecb_int);
266 }
267 
nrf_ecb_data_pointer_set(NRF_ECB_Type * p_reg,void const * p_buffer)268 __STATIC_INLINE void nrf_ecb_data_pointer_set(NRF_ECB_Type * p_reg, void const * p_buffer)
269 {
270     p_reg->ECBDATAPTR = (uint32_t)p_buffer;
271 }
272 
nrf_ecb_data_pointer_get(NRF_ECB_Type const * p_reg)273 __STATIC_INLINE void * nrf_ecb_data_pointer_get(NRF_ECB_Type const * p_reg)
274 {
275     return (void *)(p_reg->ECBDATAPTR);
276 }
277 
278 #endif // SUPPRESS_INLINE_IMPLEMENTATION
279 
280 /** @} */
281 
282 #ifdef __cplusplus
283 }
284 #endif
285 
286 #endif  // NRF_ECB_H__
287 
288