00001 /* ---------------------------------------------------------------------------- */ 00002 /* Atmel Microcontroller Software Support */ 00003 /* SAM Software Package License */ 00004 /* ---------------------------------------------------------------------------- */ 00005 /* Copyright (c) 2015, Atmel Corporation */ 00006 /* */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without */ 00010 /* modification, are permitted provided that the following condition is met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, */ 00013 /* this list of conditions and the disclaimer below. */ 00014 /* */ 00015 /* Atmel's name may not be used to endorse or promote products derived from */ 00016 /* this software without specific prior written permission. */ 00017 /* */ 00018 /* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR */ 00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ 00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE */ 00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, */ 00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ 00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */ 00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ 00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ 00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */ 00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 00028 /* ---------------------------------------------------------------------------- */ 00029 00030 /** \addtogroup aes_module Working with AES 00031 * \ingroup peripherals_module 00032 * The AES driver provides the interface to configure and use the AES peripheral. 00033 * \n 00034 * 00035 * The Advanced Encryption Standard (AES) specifies a FIPS-approved 00036 * cryptographic algorithm that can be used to protect electronic data. The AES 00037 * algorithm is a symmetric block cipher that can encrypt (encipher) and decrypt 00038 * (decipher) information. 00039 * Encryption converts data to an unintelligible form called ciphertext. 00040 * Decrypting the ciphertext converts the data back into its original form, 00041 * called plaintext. The CIPHER bit in the AES Mode Register (AES_MR) allows 00042 * selection between the encryption and the decryption processes. The AES is 00043 * capable of using cryptographic keys of 128/192/256 bits to encrypt and 00044 * decrypt data in blocks of 128 bits. 00045 * This 128-bit/192-bit/256-bit key is defined in the Key Registers (AES_KEYWRx) 00046 * and set by AES_WriteKey(). The input to the encryption processes of the CBC, 00047 * CFB, and OFB modes includes, in addition to the plaintext, a 128-bit data 00048 * block called the initialization vector (IV), 00049 * which must be set with AES_SetVector(). 00050 * The initialization vector is used in an initial step in the encryption of a 00051 * message and in the corresponding decryption of the message. 00052 * The Initialization Vector Registers are also used by the CTR mode to set the 00053 * counter value. 00054 * 00055 * To Enable a AES encryption and decryption,the user has to follow these few 00056 * steps: 00057 * <ul> 00058 * <li> A software triggered hardware reset of the AES interface is performed 00059 * by AES_SoftReset().</li> 00060 * <li> Configure AES algorithm mode, key mode, start mode and operation mode by 00061 * AES_Configure(). </li> 00062 * <li> Input AES data for encryption and decryption with function 00063 * AES_SetInput() </li> 00064 * <li> Set AES key with function AES_WriteKey(). </li> 00065 * <li> To start the encryption or the decryption process with AES_Start()</li> 00066 * <li> To get the encryption or decryption result by AES_GetOutput() </li> 00067 * </ul> 00068 * 00069 * 00070 * For more accurate information, please look at the AES section of the 00071 * Datasheet. 00072 * 00073 * Related files :\n 00074 * \ref aes.c\n 00075 * \ref aes.h\n 00076 */ 00077 /*@{*/ 00078 /*@}*/ 00079 00080 00081 /** 00082 * \file 00083 * 00084 * Implementation of Advanced Encryption Standard (AES) 00085 * 00086 */ 00087 00088 /*---------------------------------------------------------------------------- 00089 * Headers 00090 *----------------------------------------------------------------------------*/ 00091 00092 #include "chip.h" 00093 #include "aes.h" 00094 00095 /*---------------------------------------------------------------------------- 00096 * Exported functions 00097 *----------------------------------------------------------------------------*/ 00098 00099 /** 00100 * \brief Starts Manual encryption/decryption process. 00101 */ 00102 void AES_Start(void) 00103 { 00104 AES->AES_CR = AES_CR_START; 00105 } 00106 00107 /** 00108 * \brief Resets the AES. A software triggered hardware reset of the AES 00109 * interface is performed. 00110 */ 00111 void AES_SoftReset(void) 00112 { 00113 AES->AES_CR = AES_CR_SWRST; 00114 } 00115 00116 /** 00117 * \brief Configures an AES peripheral with the specified parameters. 00118 * \param mode Desired value for the AES mode register (see the datasheet). 00119 */ 00120 void AES_Configure(uint32_t mode) 00121 { 00122 AES->AES_MR = mode; 00123 } 00124 00125 /** 00126 * \brief Enables the selected interrupts sources on a AES peripheral. 00127 * \param sources Bitwise OR of selected interrupt sources. 00128 */ 00129 void AES_EnableIt(uint32_t sources) 00130 { 00131 AES->AES_IER = sources; 00132 } 00133 00134 /** 00135 * \brief Disables the selected interrupts sources on a AES peripheral. 00136 * \param sources Bitwise OR of selected interrupt sources. 00137 */ 00138 void AES_DisableIt(uint32_t sources) 00139 { 00140 AES->AES_IDR = sources; 00141 } 00142 00143 /** 00144 * \brief Get the current status register of the given AES peripheral. 00145 * \return AES status register. 00146 */ 00147 uint32_t AES_GetStatus(void) 00148 { 00149 return AES->AES_ISR; 00150 } 00151 00152 /** 00153 * \brief Set the 128-bit/192-bit/256-bit cryptographic key used for 00154 * encryption/decryption. 00155 * \param pKey Pointer to a 16/24/32 bytes cipher key. 00156 * \param keyLength length of key 00157 */ 00158 void AES_WriteKey(const uint32_t *pKey, uint32_t keyLength) 00159 { 00160 AES->AES_KEYWR[0] = pKey[0]; 00161 AES->AES_KEYWR[1] = pKey[1]; 00162 AES->AES_KEYWR[2] = pKey[2]; 00163 AES->AES_KEYWR[3] = pKey[3]; 00164 00165 if (keyLength >= 24) { 00166 AES->AES_KEYWR[4] = pKey[4]; 00167 AES->AES_KEYWR[5] = pKey[5]; 00168 } 00169 00170 if (keyLength == 32) { 00171 AES->AES_KEYWR[6] = pKey[6]; 00172 AES->AES_KEYWR[7] = pKey[7]; 00173 } 00174 } 00175 00176 /** 00177 * \brief Set the for 32-bit input Data allow to set the 128-bit data block 00178 * used for encryption/decryption. 00179 * \param data Pointer to the 16-bytes data to cipher/decipher. 00180 */ 00181 void AES_SetInput(uint32_t *data) 00182 { 00183 uint8_t i; 00184 00185 for (i = 0; i < 4; i++) 00186 AES->AES_IDATAR[i] = data[i]; 00187 } 00188 00189 /** 00190 * \brief Get the four 32-bit data contain the 128-bit data block which 00191 * has been encrypted/decrypted. 00192 * \param data pointer to the word that has been encrypted/decrypted.. 00193 */ 00194 void AES_GetOutput(uint32_t *data) 00195 { 00196 uint8_t i; 00197 00198 for (i = 0; i < 4; i++) 00199 data[i] = AES->AES_ODATAR[i]; 00200 } 00201 00202 /** 00203 * \brief Set four 64-bit initialization vector data block, which is used by 00204 * some modes of operation as an additional initial input. 00205 * \param pVector point to the word of the initialization vector. 00206 */ 00207 void AES_SetVector(const uint32_t *pVector) 00208 { 00209 AES->AES_IVR[0] = pVector[0]; 00210 AES->AES_IVR[1] = pVector[1]; 00211 AES->AES_IVR[2] = pVector[2]; 00212 AES->AES_IVR[3] = pVector[3]; 00213 } 00214 00215 /** 00216 * \brief Set Length in bytes of the AAD data that is to be processed. 00217 * \param len Length. 00218 */ 00219 void AES_SetAadLen(uint32_t len) 00220 { 00221 AES->AES_AADLENR = len; 00222 } 00223 00224 /** 00225 * \brief Set Length in bytes of the Length in bytes of the 00226 * plaintext/ciphertext (C) data that is to be processed.. 00227 * \param len Length. 00228 */ 00229 void AES_SetDataLen(uint32_t len) 00230 { 00231 AES->AES_CLENR = len; 00232 } 00233 00234 /** 00235 * \brief Set The four 32-bit Hash Word registers expose the intermediate GHASH 00236 * value. May be read to save the current GHASH value so processing can later be 00237 * resumed, presumably on a later message fragment. modes of operation as an 00238 * additional initial input. 00239 * \param hash point to the word of the hash. 00240 */ 00241 void AES_SetGcmHash(uint32_t *hash) 00242 { 00243 uint8_t i; 00244 00245 for (i = 0; i < 4; i++) 00246 AES->AES_GHASHR[i] = hash[i]; 00247 } 00248 00249 00250 /** 00251 * \brief Get The four 32-bit Tag which contain the final 128-bit GCM 00252 * Authentication tag Ħ°TĦħ when GCM processing is complete. 00253 * \param tag point to the word of the tag. 00254 */ 00255 void AES_GetGcmTag(uint32_t *tag) 00256 { 00257 uint8_t i; 00258 00259 for (i = 0; i < 4; i++) 00260 tag[i] = AES->AES_TAGR[i]; 00261 } 00262 00263 /** 00264 * \brief Reports the current value of the 32-bit GCM counter 00265 * \param counter Point to value of GCM counter. 00266 */ 00267 void AES_GetGcmCounter(uint32_t *counter) 00268 { 00269 *counter = AES->AES_CTRR; 00270 } 00271 00272 00273 /** 00274 * \brief Get the four 32-bit data contain the 128-bit H value computed from 00275 * the KEYW value 00276 * \param data point to the word that has been encrypted/decrypted. 00277 */ 00278 void AES_GetGcmH(uint32_t *h) 00279 { 00280 uint8_t i; 00281 00282 for (i = 0; i < 4; i++) 00283 h[i] = AES->AES_GCMHR[i]; 00284 } 00285 00286