Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of EV-COG-AD3029LZ by
SPIRIT_Aes.c
00001 /** 00002 ****************************************************************************** 00003 * @file SPIRIT_Aes.c 00004 * @author VMA division - AMS 00005 * @version 3.2.2 00006 * @date 08-July-2015 00007 * @brief Configuration and management of SPIRIT AES Engine. 00008 * 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 1. Redistributions of source code must retain the above copyright notice, 00016 * this list of conditions and the following disclaimer. 00017 * 2. Redistributions in binary form must reproduce the above copyright notice, 00018 * this list of conditions and the following disclaimer in the documentation 00019 * and/or other materials provided with the distribution. 00020 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 ****************************************************************************** 00036 */ 00037 00038 00039 /* Includes ------------------------------------------------------------------*/ 00040 #include "SPIRIT_Aes.h" 00041 #include "MCU_Interface.h" 00042 00043 00044 /** 00045 * @addtogroup SPIRIT_Libraries 00046 * @{ 00047 */ 00048 00049 00050 /** 00051 * @addtogroup SPIRIT_Aes 00052 * @{ 00053 */ 00054 00055 00056 /** 00057 * @defgroup Aes_Private_TypesDefinitions AES Private Types Definitions 00058 * @{ 00059 */ 00060 00061 /** 00062 * @} 00063 */ 00064 00065 00066 /** 00067 * @defgroup Aes_Private_Defines AES Private Defines 00068 * @{ 00069 */ 00070 00071 /** 00072 * @} 00073 */ 00074 00075 00076 /** 00077 * @defgroup Aes_Private_Macros AES Private Macros 00078 * @{ 00079 */ 00080 00081 /** 00082 * @} 00083 */ 00084 00085 00086 /** 00087 * @defgroup Aes_Private_Variables AES Private Variables 00088 * @{ 00089 */ 00090 00091 /** 00092 * @} 00093 */ 00094 00095 00096 /** 00097 * @defgroup Aes_Private_FunctionPrototypes AES Private Function Prototypes 00098 * @{ 00099 */ 00100 00101 /** 00102 * @} 00103 */ 00104 00105 00106 /** 00107 * @defgroup Aes_Private_Functions AES Private Functions 00108 * @{ 00109 */ 00110 00111 00112 /** 00113 * @brief Enables or Disables the AES engine. 00114 * @param xNewState new state for AES engine. 00115 * This parameter can be: S_ENABLE or S_DISABLE. 00116 * @retval None 00117 */ 00118 void SpiritAesMode(SpiritFunctionalState xNewState) 00119 { 00120 uint8_t tempRegValue = 0x00; 00121 00122 /* Check the parameters */ 00123 s_assert_param(IS_SPIRIT_FUNCTIONAL_STATE(xNewState)); 00124 00125 /* Modifies the register value */ 00126 g_xStatus = SpiritSpiReadRegisters(ANA_FUNC_CONF0_BASE, 1, &tempRegValue); 00127 if(xNewState == S_ENABLE) 00128 { 00129 tempRegValue |= AES_MASK; 00130 } 00131 else 00132 { 00133 tempRegValue &= ~AES_MASK; 00134 } 00135 00136 /* Writes the ANA_FUNC_CONF0 register to enable or disable the AES engine */ 00137 g_xStatus = SpiritSpiWriteRegisters(ANA_FUNC_CONF0_BASE, 1, &tempRegValue); 00138 00139 } 00140 00141 00142 /** 00143 * @brief Writes the data to encrypt or decrypt, or the encryption key for the 00144 * derive decryption key operation into the AES_DATA_IN registers. 00145 * @param pcBufferDataIn pointer to the user data buffer. The first byte of the array 00146 * shall be the MSB byte and it will be put in the AES_DATA_IN[0] register, while 00147 * the last one shall be the LSB and it will be put in the AES_DATA_IN[cDataLength-1] 00148 * register. If data to write are less than 16 bytes the remaining AES_DATA_IN registers 00149 * will be filled with bytes equal to 0. This parameter is an uint8_t*. 00150 * @param cDataLength length of data in bytes. 00151 * This parameter is an uint8_t. 00152 * @retval None 00153 */ 00154 void SpiritAesWriteDataIn(uint8_t* pcBufferDataIn, uint8_t cDataLength) 00155 { 00156 uint8_t i, dataInArray[16]; 00157 00158 /* Verifies that there are no more than 16 bytes */ 00159 (cDataLength>16) ? (cDataLength=16) : cDataLength; 00160 00161 /* Fill the dataInArray with the data buffer, using padding */ 00162 for(i=0;i<16;i++) 00163 { 00164 (i<(16 - cDataLength)) ? (dataInArray[i]=0):(dataInArray[i]=pcBufferDataIn[15-i]); 00165 00166 } 00167 00168 /* Writes the AES_DATA_IN registers */ 00169 g_xStatus = SpiritSpiWriteRegisters(AES_DATA_IN_15_BASE, 16, dataInArray); 00170 00171 } 00172 00173 00174 /** 00175 * @brief Returns the encrypted or decrypted data or the decription key from the AES_DATA_OUT register. 00176 * @param pcBufferDataOut pointer to the user data buffer. The AES_DATA_OUT[0] 00177 * register value will be put as first element of the buffer (MSB), while the 00178 * AES_DAT_OUT[cDataLength-1] register value will be put as last element of the buffer (LSB). 00179 * This parameter is a uint8_t*. 00180 * @param cDataLength length of data to read in bytes. 00181 * This parameter is a uint8_t. 00182 * @retval None 00183 */ 00184 void SpiritAesReadDataOut(uint8_t* pcBufferDataOut, uint8_t cDataLength) 00185 { 00186 uint8_t address, dataOutArray[16]; 00187 00188 /* Verifies that there are no more than 16 bytes */ 00189 (cDataLength>16) ? (cDataLength=16) : cDataLength; 00190 00191 /* Evaluates the address of AES_DATA_OUT from which start to read */ 00192 address = AES_DATA_OUT_15_BASE+16-cDataLength; 00193 00194 /* Reads the exact number of AES_DATA_OUT registers */ 00195 g_xStatus = (SpiritSpiReadRegisters(address, cDataLength, dataOutArray)); 00196 00197 /* Copy in the user buffer the read values changing the order */ 00198 for(int i = (cDataLength-1); i>=0; i--) 00199 { 00200 *pcBufferDataOut = dataOutArray[i]; 00201 pcBufferDataOut++; 00202 } 00203 00204 } 00205 00206 00207 /** 00208 * @brief Writes the encryption key into the AES_KEY_IN register. 00209 * @param pcKey pointer to the buffer of 4 words containing the AES key. 00210 * The first byte of the buffer shall be the most significant byte AES_KEY_0 of the AES key. 00211 * The last byte of the buffer shall be the less significant byte AES_KEY_15 of the AES key. 00212 * This parameter is an uint8_t*. 00213 * @retval None 00214 */ 00215 void SpiritAesWriteKey(uint8_t* pcKey) 00216 { 00217 uint8_t pcTempKey[16]; 00218 for (uint8_t i = 0; i < 16; i++) 00219 { 00220 pcTempKey[15-i] = pcKey[i]; 00221 } 00222 00223 /* Writes the AES_DATA_IN registers */ 00224 g_xStatus = SpiritSpiWriteRegisters(AES_KEY_IN_15_BASE, 16, pcTempKey); 00225 00226 } 00227 00228 /** 00229 * @brief Returns the encryption/decryption key from the AES_KEY_IN register. 00230 * @param pcKey pointer to the buffer of 4 words (16 bytes) containing the AES key. 00231 * The first byte of the buffer shall be the most significant byte AES_KEY_0 of the AES key. 00232 * The last byte of the buffer shall be the less significant byte AES_KEY_15 of the AES key. 00233 * This parameter is an uint8_t*. 00234 * @retval None 00235 */ 00236 void SpiritAesReadKey(uint8_t* pcKey) 00237 { 00238 uint8_t pcTempKey[16]; 00239 00240 /* Reads the AES_DATA_IN registers */ 00241 g_xStatus = SpiritSpiReadRegisters(AES_KEY_IN_15_BASE, 16, pcTempKey); 00242 00243 00244 for (uint8_t i = 0; i < 16; i++) 00245 pcKey[i] = pcTempKey[15-i]; 00246 00247 } 00248 00249 00250 00251 /** 00252 * @brief Derives the decryption key from a given encryption key. 00253 * @param None. 00254 * @retval None. 00255 */ 00256 void SpiritAesDeriveDecKeyFromEnc(void) 00257 { 00258 /* Sends the COMMAND_AES_KEY command */ 00259 g_xStatus = SpiritSpiCommandStrobes(COMMAND_AES_KEY); 00260 00261 } 00262 00263 00264 /** 00265 * @brief Executes the encryption operation. 00266 * @param None. 00267 * @retval None. 00268 */ 00269 void SpiritAesExecuteEncryption(void) 00270 { 00271 /* Sends the COMMAND_AES_ENC command */ 00272 g_xStatus = SpiritSpiCommandStrobes(COMMAND_AES_ENC); 00273 00274 } 00275 00276 00277 /** 00278 * @brief Executes the decryption operation. 00279 * @param None. 00280 * @retval None. 00281 */ 00282 void SpiritAesExecuteDecryption(void) 00283 { 00284 /* Sends the COMMAND_AES_DEC command */ 00285 g_xStatus = SpiritSpiCommandStrobes(COMMAND_AES_DEC); 00286 00287 } 00288 00289 00290 /** 00291 * @brief Executes the key derivation and the decryption operation. 00292 * @param None. 00293 * @retval None. 00294 */ 00295 void SpiritAesDeriveDecKeyExecuteDec(void) 00296 { 00297 /* Sends the COMMAND_AES_KEY_DEC command */ 00298 g_xStatus = SpiritSpiCommandStrobes(COMMAND_AES_KEY_DEC); 00299 00300 } 00301 00302 00303 /** 00304 * @} 00305 */ 00306 00307 00308 /** 00309 * @} 00310 */ 00311 00312 00313 /** 00314 * @} 00315 */ 00316 00317 00318 00319 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/
Generated on Wed Jul 13 2022 17:25:37 by
