SAMV71 Xplained Ultra Software Package 1.4

aes.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2013, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
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 
00094 /*----------------------------------------------------------------------------
00095  *        Exported functions
00096  *----------------------------------------------------------------------------*/
00097 
00098 /**
00099  * \brief Starts Manual encryption/decryption process.
00100  */
00101 void AES_Start(void)
00102 {
00103     AES->AES_CR = AES_CR_START;
00104 }
00105 
00106 /**
00107  * \brief Resets the AES. A software triggered hardware reset of the AES
00108  *  interface is performed.
00109  */
00110 void AES_SoftReset(void)
00111 {
00112     AES->AES_CR = AES_CR_SWRST;
00113 }
00114 
00115 /**
00116  * \brief Configures an AES peripheral with the specified parameters.
00117  *  \param mode  Desired value for the AES mode register (see the datasheet).
00118  */
00119 void AES_Configure(uint32_t mode)
00120 {
00121     AES->AES_MR = mode; 
00122 }
00123 
00124 /**
00125  * \brief Enables the selected interrupts sources on a AES peripheral.
00126  * \param sources  Bitwise OR of selected interrupt sources.
00127  */
00128 void AES_EnableIt(uint32_t sources)
00129 {
00130     AES->AES_IER = sources;
00131 }
00132 
00133 /**
00134  * \brief Disables the selected interrupts sources on a AES peripheral.
00135  * \param sources  Bitwise OR of selected interrupt sources.
00136  */
00137 void AES_DisableIt(uint32_t sources)
00138 {
00139     AES->AES_IDR = sources;
00140 }
00141 
00142 /**
00143  * \brief Get the current status register of the given AES peripheral.
00144  * \return  AES status register.
00145  */
00146 uint32_t AES_GetStatus(void)
00147 {
00148     return AES->AES_ISR;
00149 }
00150 
00151 /**
00152  * \brief Set the 128-bit/192-bit/256-bit cryptographic key used for 
00153  * encryption/decryption.
00154  * \param pKey Pointer to a 16/24/32 bytes cipher key.
00155  * \param keyLength length of key
00156  */
00157 void AES_WriteKey(const uint32_t *pKey, uint32_t keyLength)
00158 {
00159     AES->AES_KEYWR[0] = pKey[0];
00160     AES->AES_KEYWR[1] = pKey[1];
00161     AES->AES_KEYWR[2] = pKey[2];
00162     AES->AES_KEYWR[3] = pKey[3];
00163 
00164     if( keyLength >= 24 ) {
00165         AES->AES_KEYWR[4] = pKey[4];
00166         AES->AES_KEYWR[5] = pKey[5];
00167     }
00168     if( keyLength == 32 ) {
00169         AES->AES_KEYWR[6] = pKey[6];
00170         AES->AES_KEYWR[7] = pKey[7];
00171     }
00172 }
00173 
00174 /**
00175  * \brief Set the for 32-bit input Data allow to set the 128-bit data block
00176  * used for encryption/decryption.
00177  * \param data Pointer to the 16-bytes data to cipher/decipher.
00178  */
00179 void AES_SetInput(uint32_t *data)
00180 {
00181     uint8_t i;
00182     for (i = 0; i< 4; i++)
00183         AES->AES_IDATAR[i] = data[i];
00184 }
00185 
00186 /**
00187  * \brief Get the four 32-bit data contain the 128-bit data block which 
00188  * has been encrypted/decrypted.
00189  * \param data pointer to the word that has been encrypted/decrypted..
00190  */
00191 void AES_GetOutput(uint32_t *data)
00192 {
00193     uint8_t i;
00194     for (i = 0; i< 4; i++) 
00195         data[i] = AES->AES_ODATAR[i];
00196 }
00197 
00198 /**
00199  * \brief Set four 64-bit initialization vector data block, which is used by 
00200  * some modes of operation as an additional initial input.
00201  * \param pVector point to the word of the initialization vector.
00202  */
00203 void AES_SetVector(const uint32_t *pVector)
00204 {
00205     AES->AES_IVR[0] = pVector[0];
00206     AES->AES_IVR[1] = pVector[1];
00207     AES->AES_IVR[2] = pVector[2];
00208     AES->AES_IVR[3] = pVector[3];
00209 }
00210 
00211 /**
00212  * \brief Set Length in bytes of the AAD data that is to be processed.
00213  * \param len Length.
00214  */
00215 void AES_SetAadLen(uint32_t len)
00216 {
00217     AES->AES_AADLENR = len;
00218 }
00219 
00220 /**
00221  * \brief Set Length in bytes of the Length in bytes of the 
00222  * plaintext/ciphertext (C) data that is to be processed..
00223  * \param len Length.
00224  */
00225 void AES_SetDataLen(uint32_t len)
00226 {
00227     AES->AES_CLENR = len;
00228 }
00229 
00230 /**
00231  * \brief Set The four 32-bit Hash Word registers expose the intermediate GHASH 
00232  * value. May be read to save the current GHASH value so processing can later be 
00233  * resumed, presumably on a later message fragment. modes of operation as an 
00234  * additional initial input.
00235  * \param hash point to the word of the hash.
00236  */
00237 void AES_SetGcmHash(uint32_t * hash)
00238 {
00239     uint8_t i;
00240     for (i = 0; i< 4; i++) 
00241         AES->AES_GHASHR[i] = hash[i];
00242 }
00243 
00244 
00245 /**
00246  * \brief Get The four 32-bit Tag which contain the final 128-bit GCM 
00247  * Authentication tag Ħ°TĦħ when GCM processing is complete.
00248  * \param tag point to the word of the tag.
00249  */
00250 void AES_GetGcmTag(uint32_t * tag)
00251 {
00252     uint8_t i;
00253     for (i = 0; i< 4; i++) 
00254         tag[i] = AES->AES_TAGR[i] ;
00255 }
00256 
00257 /**
00258  * \brief Reports the current value of the 32-bit GCM counter
00259  * \param counter Point to value of GCM counter.
00260  */
00261 void AES_GetGcmCounter(uint32_t * counter)
00262 {
00263     *counter = AES->AES_CTRR;
00264 }
00265 
00266 
00267 /**
00268  * \brief Get the four 32-bit data contain the 128-bit H value computed from 
00269  * the KEYW value
00270  * \param data point to the word that has been encrypted/decrypted.
00271  */
00272 void AES_GetGcmH(uint32_t *h)
00273 {
00274     uint8_t i;
00275     for (i = 0; i< 4; i++) 
00276         h[i] = AES->AES_GCMHR[i];
00277 }
00278 
00279 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines