1 /* ----------------------------------------------------------------------------
\r
2 * SAM Software Package License
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2013, Atmel Corporation
\r
6 * All rights reserved.
\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
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\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
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
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
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
51 * To Enable a AES encryption and decryption,the user has to follow these few steps:
\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
62 * For more accurate information, please look at the AES section of the
\r
76 * Implementation of Advanced Encryption Standard (AES)
\r
80 /*----------------------------------------------------------------------------
\r
82 *----------------------------------------------------------------------------*/
\r
86 /*----------------------------------------------------------------------------
\r
87 * Exported functions
\r
88 *----------------------------------------------------------------------------*/
\r
91 * \brief Starts Manual encryption/decryption process.
\r
93 void AES_Start(void)
\r
95 AES->AES_CR = AES_CR_START;
\r
99 * \brief Resets the AES. A software triggered hardware reset of the AES interface is performed.
\r
101 void AES_SoftReset(void)
\r
103 AES->AES_CR = AES_CR_SWRST;
\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
111 void AES_Configure(uint32_t mode)
\r
113 AES->AES_MR = mode;
\r
117 * \brief Enables the selected interrupts sources on a AES peripheral.
\r
118 * \param sources Bitwise OR of selected interrupt sources.
\r
120 void AES_EnableIt(uint32_t sources)
\r
122 AES->AES_IER = sources;
\r
126 * \brief Disables the selected interrupts sources on a AES peripheral.
\r
127 * \param sources Bitwise OR of selected interrupt sources.
\r
129 void AES_DisableIt(uint32_t sources)
\r
131 AES->AES_IDR = sources;
\r
135 * \brief Get the current status register of the given AES peripheral.
\r
136 * \return AES status register.
\r
138 uint32_t AES_GetStatus(void)
\r
140 return AES->AES_ISR;
\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
148 void AES_WriteKey(const uint32_t *pKey, uint32_t keyLength)
\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
155 if( keyLength >= 24 ) {
\r
156 AES->AES_KEYWR[4] = pKey[4];
\r
157 AES->AES_KEYWR[5] = pKey[5];
\r
159 if( keyLength == 32 ) {
\r
160 AES->AES_KEYWR[6] = pKey[6];
\r
161 AES->AES_KEYWR[7] = pKey[7];
\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
169 void AES_SetInput(uint32_t *data)
\r
172 for (i = 0; i< 4; i++)
\r
173 AES->AES_IDATAR[i] = data[i];
\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
180 void AES_GetOutput(uint32_t *data)
\r
183 for (i = 0; i< 4; i++)
\r
184 data[i] = AES->AES_ODATAR[i];
\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
192 void AES_SetVector(const uint32_t *pVector)
\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
202 * \brief Set Length in bytes of the AAD data that is to be processed.
\r
203 * \param len Length.
\r
205 void AES_SetAadLen(uint32_t len)
\r
207 AES->AES_AADLENR = len;
\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
215 void AES_SetDataLen(uint32_t len)
\r
217 AES->AES_CLENR = len;
\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
226 void AES_SetGcmHash(uint32_t * hash)
\r
229 for (i = 0; i< 4; i++)
\r
230 AES->AES_GHASHR[i] = hash[i];
\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
239 void AES_GetGcmTag(uint32_t * tag)
\r
242 for (i = 0; i< 4; i++)
\r
243 tag[i] = AES->AES_TAGR[i] ;
\r
247 * \brief Reports the current value of the 32-bit GCM counter
\r
248 * \param counter Point to value of GCM counter.
\r
250 void AES_GetGcmCounter(uint32_t * counter)
\r
252 *counter = AES->AES_CTRR;
\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
260 void AES_GetGcmH(uint32_t *h)
\r
263 for (i = 0; i< 4; i++)
\r
264 h[i] = AES->AES_GCMHR[i];
\r