1 /***************************************************************************//**
2  * @file
3  * @brief Advanced encryption standard (AES) accelerator peripheral API for
4  *   EFM32.
5  * @author Energy Micro AS
6  * @version 3.0.0
7  *******************************************************************************
8  * @section License
9  * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
10  *******************************************************************************
11  *
12  * Permission is granted to anyone to use this software for any purpose,
13  * including commercial applications, and to alter it and redistribute it
14  * freely, subject to the following restrictions:
15  *
16  * 1. The origin of this software must not be misrepresented; you must not
17  *    claim that you wrote the original software.
18  * 2. Altered source versions must be plainly marked as such, and must not be
19  *    misrepresented as being the original software.
20  * 3. This notice may not be removed or altered from any source distribution.
21  *
22  * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
23  * obligation to support this Software. Energy Micro AS is providing the
24  * Software "AS IS", with no express or implied warranties of any kind,
25  * including, but not limited to, any implied warranties of merchantability
26  * or fitness for any particular purpose or warranties against infringement
27  * of any proprietary rights of a third party.
28  *
29  * Energy Micro AS will not be liable for any consequential, incidental, or
30  * special damages, or any other relief, or for any claim by any third party,
31  * arising from your use of this Software.
32  *
33  ******************************************************************************/
34 #ifndef __EM_AES_H
35 #define __EM_AES_H
36 
37 #include <stdbool.h>
38 #include "em_part.h"
39 
40 #if defined(AES_COUNT) && (AES_COUNT > 0)
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /***************************************************************************//**
47  * @addtogroup EM_Library
48  * @{
49  ******************************************************************************/
50 
51 /***************************************************************************//**
52  * @addtogroup AES
53  * @{
54  ******************************************************************************/
55 
56 /*******************************************************************************
57  ******************************   TYPEDEFS   ***********************************
58  ******************************************************************************/
59 
60 /**
61  * @brief
62  *   AES counter modification function pointer.
63  * @details
64  *   Parameters:
65  *   @li ctr - Ptr to byte array (16 bytes) holding counter to be modified.
66  */
67 typedef void (*AES_CtrFuncPtr_TypeDef)(uint8_t *ctr);
68 
69 /*******************************************************************************
70  *****************************   PROTOTYPES   **********************************
71  ******************************************************************************/
72 
73 void AES_CBC128(uint8_t *out,
74                 const uint8_t *in,
75                 unsigned int len,
76                 const uint8_t *key,
77                 const uint8_t *iv,
78                 bool encrypt);
79 
80 void AES_CBC256(uint8_t *out,
81                 const uint8_t *in,
82                 unsigned int len,
83                 const uint8_t *key,
84                 const uint8_t *iv,
85                 bool encrypt);
86 
87 void AES_CFB128(uint8_t *out,
88                 const uint8_t *in,
89                 unsigned int len,
90                 const uint8_t *key,
91                 const uint8_t *iv,
92                 bool encrypt);
93 
94 void AES_CFB256(uint8_t *out,
95                 const uint8_t *in,
96                 unsigned int len,
97                 const uint8_t *key,
98                 const uint8_t *iv,
99                 bool encrypt);
100 
101 void AES_CTR128(uint8_t *out,
102                 const uint8_t *in,
103                 unsigned int len,
104                 const uint8_t *key,
105                 uint8_t *ctr,
106                 AES_CtrFuncPtr_TypeDef ctrFunc);
107 
108 void AES_CTR256(uint8_t *out,
109                 const uint8_t *in,
110                 unsigned int len,
111                 const uint8_t *key,
112                 uint8_t *ctr,
113                 AES_CtrFuncPtr_TypeDef ctrFunc);
114 
115 void AES_CTRUpdate32Bit(uint8_t *ctr);
116 
117 void AES_DecryptKey128(uint8_t *out, const uint8_t *in);
118 
119 void AES_DecryptKey256(uint8_t *out, const uint8_t *in);
120 
121 void AES_ECB128(uint8_t *out,
122                 const uint8_t *in,
123                 unsigned int len,
124                 const uint8_t *key,
125                 bool encrypt);
126 
127 void AES_ECB256(uint8_t *out,
128                 const uint8_t *in,
129                 unsigned int len,
130                 const uint8_t *key,
131                 bool encrypt);
132 
133 /***************************************************************************//**
134  * @brief
135  *   Clear one or more pending AES interrupts.
136  *
137  * @param[in] flags
138  *   Pending AES interrupt source to clear. Use a bitwise logic OR combination of
139  *   valid interrupt flags for the AES module (AES_IF_nnn).
140  ******************************************************************************/
AES_IntClear(uint32_t flags)141 __STATIC_INLINE void AES_IntClear(uint32_t flags)
142 {
143   AES->IFC = flags;
144 }
145 
146 
147 /***************************************************************************//**
148  * @brief
149  *   Disable one or more AES interrupts.
150  *
151  * @param[in] flags
152  *   AES interrupt sources to disable. Use a bitwise logic OR combination of
153  *   valid interrupt flags for the AES module (AES_IF_nnn).
154  ******************************************************************************/
AES_IntDisable(uint32_t flags)155 __STATIC_INLINE void AES_IntDisable(uint32_t flags)
156 {
157   AES->IEN &= ~(flags);
158 }
159 
160 
161 /***************************************************************************//**
162  * @brief
163  *   Enable one or more AES interrupts.
164  *
165  * @note
166  *   Depending on the use, a pending interrupt may already be set prior to
167  *   enabling the interrupt. Consider using AES_IntClear() prior to enabling
168  *   if such a pending interrupt should be ignored.
169  *
170  * @param[in] flags
171  *   AES interrupt sources to enable. Use a bitwise logic OR combination of
172  *   valid interrupt flags for the AES module (AES_IF_nnn).
173  ******************************************************************************/
AES_IntEnable(uint32_t flags)174 __STATIC_INLINE void AES_IntEnable(uint32_t flags)
175 {
176   AES->IEN |= flags;
177 }
178 
179 
180 /***************************************************************************//**
181  * @brief
182  *   Get pending AES interrupt flags.
183  *
184  * @note
185  *   The event bits are not cleared by the use of this function.
186  *
187  * @return
188  *   AES interrupt sources pending. A bitwise logic OR combination of valid
189  *   interrupt flags for the AES module (AES_IF_nnn).
190  ******************************************************************************/
AES_IntGet(void)191 __STATIC_INLINE uint32_t AES_IntGet(void)
192 {
193   return(AES->IF);
194 }
195 
196 
197 /***************************************************************************//**
198  * @brief
199  *   Set one or more pending AES interrupts from SW.
200  *
201  * @param[in] flags
202  *   AES interrupt sources to set to pending. Use a bitwise logic OR combination
203  *   of valid interrupt flags for the AES module (AES_IF_nnn).
204  ******************************************************************************/
AES_IntSet(uint32_t flags)205 __STATIC_INLINE void AES_IntSet(uint32_t flags)
206 {
207   AES->IFS = flags;
208 }
209 
210 
211 void AES_OFB128(uint8_t *out,
212                 const uint8_t *in,
213                 unsigned int len,
214                 const uint8_t *key,
215                 const uint8_t *iv);
216 
217 void AES_OFB256(uint8_t *out,
218                 const uint8_t *in,
219                 unsigned int len,
220                 const uint8_t *key,
221                 const uint8_t *iv);
222 
223 
224 /** @} (end addtogroup AES) */
225 /** @} (end addtogroup EM_Library) */
226 
227 #ifdef __cplusplus
228 }
229 #endif
230 
231 #endif /* __EM_AES_H */
232 
233 #endif /* defined(AES_COUNT) && (AES_COUNT > 0) */
234