]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained_IAR_Keil/libchip_samv7/source/aes.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAMV71_Xplained_IAR_Keil / libchip_samv7 / source / aes.c
1 /* ----------------------------------------------------------------------------\r
2  *         SAM Software Package License\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2013, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\r
8  * Redistribution and use in source and binary forms, with or without\r
9  * modification, are permitted provided that the following conditions are met:\r
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\r
14  * Atmel's name may not be used to endorse or promote products derived from\r
15  * this software without specific prior written permission.\r
16  *\r
17  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
20  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
23  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  * ----------------------------------------------------------------------------\r
28  */\r
29 \r
30 /** \addtogroup aes_module Working with AES\r
31  * \ingroup peripherals_module\r
32  * The AES driver provides the interface to configure and use the AES peripheral.\r
33  * \n\r
34  *\r
35  * The Advanced Encryption Standard (AES) specifies a FIPS-approved cryptographic algorithm\r
36  * that can be used to protect electronic data. The AES algorithm is a symmetric block \r
37  * cipher that can encrypt (encipher) and decrypt (decipher) information.\r
38  * Encryption converts data to an unintelligible form called ciphertext. \r
39  * Decrypting the ciphertext converts the data back into its original form, \r
40  * called plaintext. The CIPHER bit in the AES Mode Register (AES_MR) allows selection \r
41  * between the encryption and the decryption processes. The AES is capable of using cryptographic \r
42  * keys of 128/192/256 bits to encrypt and decrypt data in blocks of 128 bits. \r
43  * This 128-bit/192-bit/256-bit key is defined in the Key Registers (AES_KEYWRx) and set by \r
44  * AES_WriteKey(). The input to the encryption processes of the CBC, CFB, and OFB modes includes,\r
45  * in addition to the plaintext, a 128-bit data block called the initialization vector (IV), \r
46  * which must be set with AES_SetVector(). \r
47  * The initialization vector is used in an initial step in the encryption of a message and \r
48  * in the corresponding decryption of the message. The Initialization Vector Registers are \r
49  * also used by the CTR mode to set the counter value.\r
50  *\r
51  * To Enable a AES encryption and decryption,the user has to follow these few steps:\r
52  * <ul>\r
53  * <li> A software triggered hardware reset of the AES interface is performed by AES_SoftReset().</li>\r
54  * <li> Configure AES algorithm mode, key mode, start mode and operation mode by AES_Configure(). </li>\r
55  * <li> Input AES data for encryption and decryption with function AES_SetInput() </li>\r
56  * <li> Set AES key with fucntion AES_WriteKey(). </li>\r
57  * <li> To start the encryption or the decryption process with AES_Start()</li>\r
58  * <li> To get the encryption or decryption reslut by AES_GetOutput() </li>\r
59  * </ul>\r
60  *\r
61  *\r
62  * For more accurate information, please look at the AES section of the\r
63  * Datasheet.\r
64  *\r
65  * Related files :\n\r
66  * \ref aes.c\n\r
67  * \ref aes.h\n\r
68  */\r
69 /*@{*/\r
70 /*@}*/\r
71 \r
72 \r
73 /**\r
74  * \file\r
75  *\r
76  * Implementation of Advanced Encryption Standard (AES)\r
77  *\r
78  */\r
79 \r
80 /*----------------------------------------------------------------------------\r
81  *        Headers\r
82  *----------------------------------------------------------------------------*/\r
83 \r
84 #include "chip.h"\r
85 \r
86 /*----------------------------------------------------------------------------\r
87  *        Exported functions\r
88  *----------------------------------------------------------------------------*/\r
89 \r
90 /**\r
91  * \brief Starts Manual encryption/decryption process.\r
92  */\r
93 void AES_Start(void)\r
94 {\r
95     AES->AES_CR = AES_CR_START;\r
96 }\r
97 \r
98 /**\r
99  * \brief Resets the AES. A software triggered hardware reset of the AES interface is performed.\r
100  */\r
101 void AES_SoftReset(void)\r
102 {\r
103     AES->AES_CR = AES_CR_SWRST;\r
104 }\r
105 \r
106 \r
107 /**\r
108  * \brief Configures an AES peripheral with the specified parameters.\r
109  *  \param mode  Desired value for the AES mode register (see the datasheet).\r
110  */\r
111 void AES_Configure(uint32_t mode)\r
112 {\r
113     AES->AES_MR = mode; \r
114 }\r
115 \r
116 /**\r
117  * \brief Enables the selected interrupts sources on a AES peripheral.\r
118  * \param sources  Bitwise OR of selected interrupt sources.\r
119  */\r
120 void AES_EnableIt(uint32_t sources)\r
121 {\r
122     AES->AES_IER = sources;\r
123 }\r
124 \r
125 /**\r
126  * \brief Disables the selected interrupts sources on a AES peripheral.\r
127  * \param sources  Bitwise OR of selected interrupt sources.\r
128  */\r
129 void AES_DisableIt(uint32_t sources)\r
130 {\r
131     AES->AES_IDR = sources;\r
132 }\r
133 \r
134 /**\r
135  * \brief Get the current status register of the given AES peripheral.\r
136  * \return  AES status register.\r
137  */\r
138 uint32_t AES_GetStatus(void)\r
139 {\r
140     return AES->AES_ISR;\r
141 }\r
142 \r
143 /**\r
144  * \brief Set the 128-bit/192-bit/256-bit cryptographic key used for encryption/decryption.\r
145  * \param pKey Pointer to a 16/24/32 bytes cipher key.\r
146  * \param keyLength length of key\r
147  */\r
148 void AES_WriteKey(const uint32_t *pKey, uint32_t keyLength)\r
149 {\r
150     AES->AES_KEYWR[0] = pKey[0];\r
151     AES->AES_KEYWR[1] = pKey[1];\r
152     AES->AES_KEYWR[2] = pKey[2];\r
153     AES->AES_KEYWR[3] = pKey[3];\r
154 \r
155     if( keyLength >= 24 ) {\r
156         AES->AES_KEYWR[4] = pKey[4];\r
157         AES->AES_KEYWR[5] = pKey[5];\r
158     }\r
159     if( keyLength == 32 ) {\r
160         AES->AES_KEYWR[6] = pKey[6];\r
161         AES->AES_KEYWR[7] = pKey[7];\r
162     }\r
163 }\r
164 \r
165 /**\r
166  * \brief Set the for 32-bit input Data allow to set the 128-bit data block used for encryption/decryption.\r
167  * \param data Pointer to the 16-bytes data to cipher/decipher.\r
168  */\r
169 void AES_SetInput(uint32_t *data)\r
170 {\r
171     uint8_t i;\r
172     for (i = 0; i< 4; i++)\r
173         AES->AES_IDATAR[i] = data[i];\r
174 }\r
175 \r
176 /**\r
177  * \brief Get the four 32-bit data contain the 128-bit data block which has been encrypted/decrypted.\r
178  * \param data pointer to the word that has been encrypted/decrypted..\r
179  */\r
180 void AES_GetOutput(uint32_t *data)\r
181 {\r
182     uint8_t i;\r
183     for (i = 0; i< 4; i++) \r
184         data[i] = AES->AES_ODATAR[i];\r
185 }\r
186 \r
187 /**\r
188  * \brief Set four 64-bit initialization vector data block, which is used by some\r
189  * modes of operation as an additional initial input.\r
190  * \param pVector point to the word of the initialization vector.\r
191  */\r
192 void AES_SetVector(const uint32_t *pVector)\r
193 {\r
194     AES->AES_IVR[0] = pVector[0];\r
195     AES->AES_IVR[1] = pVector[1];\r
196     AES->AES_IVR[2] = pVector[2];\r
197     AES->AES_IVR[3] = pVector[3];\r
198 }\r
199 \r
200 \r
201 /**\r
202  * \brief Set Length in bytes of the AAD data that is to be processed.\r
203  * \param len Length.\r
204  */\r
205 void AES_SetAadLen(uint32_t len)\r
206 {\r
207     AES->AES_AADLENR = len;\r
208 }\r
209 \r
210 /**\r
211  * \brief Set Length in bytes of the Length in bytes of the \r
212  * plaintext/ciphertext (C) data that is to be processed..\r
213  * \param len Length.\r
214  */\r
215 void AES_SetDataLen(uint32_t len)\r
216 {\r
217     AES->AES_CLENR = len;\r
218 }\r
219 \r
220 /**\r
221  * \brief Set The four 32-bit Hash Word registers expose the intermediate GHASH value. \r
222  * May be read to save the current GHASH value so processing can later be resumed, \r
223  * presumably on a later message fragment. modes of operation as an additional initial input.\r
224  * \param hash point to the word of the hash.\r
225  */\r
226 void AES_SetGcmHash(uint32_t * hash)\r
227 {\r
228     uint8_t i;\r
229     for (i = 0; i< 4; i++) \r
230         AES->AES_GHASHR[i] = hash[i];\r
231 }\r
232 \r
233 \r
234 /**\r
235  * \brief Get The four 32-bit Tag which contain the final 128-bit GCM Authentication tag \r
236  * ¡°T¡± when GCM processing is complete.\r
237  * \param tag point to the word of the tag.\r
238  */\r
239 void AES_GetGcmTag(uint32_t * tag)\r
240 {\r
241     uint8_t i;\r
242     for (i = 0; i< 4; i++) \r
243         tag[i] = AES->AES_TAGR[i] ;\r
244 }\r
245 \r
246 /**\r
247  * \brief Reports the current value of the 32-bit GCM counter\r
248  * \param counter Point to value of GCM counter.\r
249  */\r
250 void AES_GetGcmCounter(uint32_t * counter)\r
251 {\r
252     *counter = AES->AES_CTRR;\r
253 }\r
254 \r
255 \r
256 /**\r
257  * \brief Get the four 32-bit data contain the 128-bit H value computed from the KEYW value\r
258  * \param data point to the word that has been encrypted/decrypted..\r
259  */\r
260 void AES_GetGcmH(uint32_t *h)\r
261 {\r
262     uint8_t i;\r
263     for (i = 0; i< 4; i++) \r
264         h[i] = AES->AES_GCMHR[i];\r
265 }\r
266 \r
267 \r