mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Jan 27 14:30:07 2014 +0000
Revision:
76:aeb1df146756
Child:
80:66393a7b209d
Synchronized with git revision a31ec9c5f7bcb5c8a1b2eced103f6a1dfa921abd

Full URL: https://github.com/mbedmicro/mbed/commit/a31ec9c5f7bcb5c8a1b2eced103f6a1dfa921abd/

Add NUCLEO_L152RE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 76:aeb1df146756 1 /**
mbed_official 76:aeb1df146756 2 ******************************************************************************
mbed_official 76:aeb1df146756 3 * @file stm32l1xx_aes_util.c
mbed_official 76:aeb1df146756 4 * @author MCD Application Team
mbed_official 76:aeb1df146756 5 * @version V1.2.0
mbed_official 76:aeb1df146756 6 * @date 22-February-2013
mbed_official 76:aeb1df146756 7 * @brief This file provides high level functions to encrypt and decrypt an
mbed_official 76:aeb1df146756 8 * input message using AES in ECB/CBC/CTR modes.
mbed_official 76:aeb1df146756 9 *
mbed_official 76:aeb1df146756 10 * @verbatim
mbed_official 76:aeb1df146756 11
mbed_official 76:aeb1df146756 12 ================================================================================
mbed_official 76:aeb1df146756 13 ##### How to use this driver #####
mbed_official 76:aeb1df146756 14 ================================================================================
mbed_official 76:aeb1df146756 15 [..]
mbed_official 76:aeb1df146756 16 (#) Enable The AES controller clock using
mbed_official 76:aeb1df146756 17 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_AES, ENABLE); function.
mbed_official 76:aeb1df146756 18
mbed_official 76:aeb1df146756 19 (#) Use AES_ECB_Encrypt() function to encrypt an input message in ECB mode.
mbed_official 76:aeb1df146756 20 (#) Use AES_ECB_Decrypt() function to decrypt an input message in ECB mode.
mbed_official 76:aeb1df146756 21
mbed_official 76:aeb1df146756 22 (#) Use AES_CBC_Encrypt() function to encrypt an input message in CBC mode.
mbed_official 76:aeb1df146756 23 (#) Use AES_CBC_Decrypt() function to decrypt an input message in CBC mode.
mbed_official 76:aeb1df146756 24
mbed_official 76:aeb1df146756 25 (#) Use AES_CTR_Encrypt() function to encrypt an input message in CTR mode.
mbed_official 76:aeb1df146756 26 (#) Use AES_CTR_Decrypt() function to decrypt an input message in CTR mode.
mbed_official 76:aeb1df146756 27
mbed_official 76:aeb1df146756 28 * @endverbatim
mbed_official 76:aeb1df146756 29 *
mbed_official 76:aeb1df146756 30 ******************************************************************************
mbed_official 76:aeb1df146756 31 * @attention
mbed_official 76:aeb1df146756 32 *
mbed_official 76:aeb1df146756 33 * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
mbed_official 76:aeb1df146756 34 *
mbed_official 76:aeb1df146756 35 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
mbed_official 76:aeb1df146756 36 * You may not use this file except in compliance with the License.
mbed_official 76:aeb1df146756 37 * You may obtain a copy of the License at:
mbed_official 76:aeb1df146756 38 *
mbed_official 76:aeb1df146756 39 * http://www.st.com/software_license_agreement_liberty_v2
mbed_official 76:aeb1df146756 40 *
mbed_official 76:aeb1df146756 41 * Unless required by applicable law or agreed to in writing, software
mbed_official 76:aeb1df146756 42 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 76:aeb1df146756 43 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 76:aeb1df146756 44 * See the License for the specific language governing permissions and
mbed_official 76:aeb1df146756 45 * limitations under the License.
mbed_official 76:aeb1df146756 46 *
mbed_official 76:aeb1df146756 47 ******************************************************************************
mbed_official 76:aeb1df146756 48 */
mbed_official 76:aeb1df146756 49
mbed_official 76:aeb1df146756 50 /* Includes ------------------------------------------------------------------*/
mbed_official 76:aeb1df146756 51 #include "stm32l1xx_aes.h"
mbed_official 76:aeb1df146756 52
mbed_official 76:aeb1df146756 53 /** @addtogroup STM32L1xx_StdPeriph_Driver
mbed_official 76:aeb1df146756 54 * @{
mbed_official 76:aeb1df146756 55 */
mbed_official 76:aeb1df146756 56
mbed_official 76:aeb1df146756 57 /** @addtogroup AES
mbed_official 76:aeb1df146756 58 * @brief AES driver modules
mbed_official 76:aeb1df146756 59 * @{
mbed_official 76:aeb1df146756 60 */
mbed_official 76:aeb1df146756 61
mbed_official 76:aeb1df146756 62 /* Private typedef -----------------------------------------------------------*/
mbed_official 76:aeb1df146756 63 /* Private define ------------------------------------------------------------*/
mbed_official 76:aeb1df146756 64 #define AES_CC_TIMEOUT ((uint32_t) 0x00010000)
mbed_official 76:aeb1df146756 65
mbed_official 76:aeb1df146756 66 /* Private macro -------------------------------------------------------------*/
mbed_official 76:aeb1df146756 67 /* Private variables ---------------------------------------------------------*/
mbed_official 76:aeb1df146756 68 /* Private function prototypes -----------------------------------------------*/
mbed_official 76:aeb1df146756 69 /* Private functions ---------------------------------------------------------*/
mbed_official 76:aeb1df146756 70
mbed_official 76:aeb1df146756 71 /** @defgroup AES_Private_Functions
mbed_official 76:aeb1df146756 72 * @{
mbed_official 76:aeb1df146756 73 */
mbed_official 76:aeb1df146756 74
mbed_official 76:aeb1df146756 75 /** @defgroup AES_Group6 High Level AES functions
mbed_official 76:aeb1df146756 76 * @brief High Level AES functions
mbed_official 76:aeb1df146756 77 *
mbed_official 76:aeb1df146756 78 @verbatim
mbed_official 76:aeb1df146756 79 ================================================================================
mbed_official 76:aeb1df146756 80 ##### High Level AES functions #####
mbed_official 76:aeb1df146756 81 ================================================================================
mbed_official 76:aeb1df146756 82
mbed_official 76:aeb1df146756 83 @endverbatim
mbed_official 76:aeb1df146756 84 * @{
mbed_official 76:aeb1df146756 85 */
mbed_official 76:aeb1df146756 86
mbed_official 76:aeb1df146756 87 /**
mbed_official 76:aeb1df146756 88 * @brief Encrypt using AES in ECB Mode
mbed_official 76:aeb1df146756 89 * @param Key: Key used for AES algorithm.
mbed_official 76:aeb1df146756 90 * @param Input: pointer to the Input buffer.
mbed_official 76:aeb1df146756 91 * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes.
mbed_official 76:aeb1df146756 92 * @param Output: pointer to the returned buffer.
mbed_official 76:aeb1df146756 93 * @retval An ErrorStatus enumeration value:
mbed_official 76:aeb1df146756 94 * - SUCCESS: Operation done
mbed_official 76:aeb1df146756 95 * - ERROR: Operation failed
mbed_official 76:aeb1df146756 96 */
mbed_official 76:aeb1df146756 97 ErrorStatus AES_ECB_Encrypt(uint8_t* Key, uint8_t* Input, uint32_t Ilength, uint8_t* Output)
mbed_official 76:aeb1df146756 98 {
mbed_official 76:aeb1df146756 99 AES_InitTypeDef AES_InitStructure;
mbed_official 76:aeb1df146756 100 AES_KeyInitTypeDef AES_KeyInitStructure;
mbed_official 76:aeb1df146756 101 ErrorStatus status = SUCCESS;
mbed_official 76:aeb1df146756 102 uint32_t keyaddr = (uint32_t)Key;
mbed_official 76:aeb1df146756 103 uint32_t inputaddr = (uint32_t)Input;
mbed_official 76:aeb1df146756 104 uint32_t outputaddr = (uint32_t)Output;
mbed_official 76:aeb1df146756 105 __IO uint32_t counter = 0;
mbed_official 76:aeb1df146756 106 uint32_t ccstatus = 0;
mbed_official 76:aeb1df146756 107 uint32_t i = 0;
mbed_official 76:aeb1df146756 108
mbed_official 76:aeb1df146756 109 /* AES Key initialisation */
mbed_official 76:aeb1df146756 110 AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 111 keyaddr += 4;
mbed_official 76:aeb1df146756 112 AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 113 keyaddr += 4;
mbed_official 76:aeb1df146756 114 AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 115 keyaddr += 4;
mbed_official 76:aeb1df146756 116 AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 117 AES_KeyInit(&AES_KeyInitStructure);
mbed_official 76:aeb1df146756 118
mbed_official 76:aeb1df146756 119 /* AES configuration */
mbed_official 76:aeb1df146756 120 AES_InitStructure.AES_Operation = AES_Operation_Encryp;
mbed_official 76:aeb1df146756 121 AES_InitStructure.AES_Chaining = AES_Chaining_ECB;
mbed_official 76:aeb1df146756 122 AES_InitStructure.AES_DataType = AES_DataType_8b;
mbed_official 76:aeb1df146756 123 AES_Init(&AES_InitStructure);
mbed_official 76:aeb1df146756 124
mbed_official 76:aeb1df146756 125 /* Enable AES */
mbed_official 76:aeb1df146756 126 AES_Cmd(ENABLE);
mbed_official 76:aeb1df146756 127
mbed_official 76:aeb1df146756 128 for(i = 0; ((i < Ilength) && (status != ERROR)); i += 16)
mbed_official 76:aeb1df146756 129 {
mbed_official 76:aeb1df146756 130 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 131 inputaddr += 4;
mbed_official 76:aeb1df146756 132 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 133 inputaddr += 4;
mbed_official 76:aeb1df146756 134 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 135 inputaddr += 4;
mbed_official 76:aeb1df146756 136 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 137 inputaddr += 4;
mbed_official 76:aeb1df146756 138
mbed_official 76:aeb1df146756 139 /* Wait for CCF flag to be set */
mbed_official 76:aeb1df146756 140 counter = 0;
mbed_official 76:aeb1df146756 141 do
mbed_official 76:aeb1df146756 142 {
mbed_official 76:aeb1df146756 143 ccstatus = AES_GetFlagStatus(AES_FLAG_CCF);
mbed_official 76:aeb1df146756 144 counter++;
mbed_official 76:aeb1df146756 145 }while((counter != AES_CC_TIMEOUT) && (ccstatus == RESET));
mbed_official 76:aeb1df146756 146
mbed_official 76:aeb1df146756 147 if (ccstatus == RESET)
mbed_official 76:aeb1df146756 148 {
mbed_official 76:aeb1df146756 149 status = ERROR;
mbed_official 76:aeb1df146756 150 }
mbed_official 76:aeb1df146756 151 else
mbed_official 76:aeb1df146756 152 {
mbed_official 76:aeb1df146756 153 /* Clear CCF flag */
mbed_official 76:aeb1df146756 154 AES_ClearFlag(AES_FLAG_CCF);
mbed_official 76:aeb1df146756 155 /* Read cipher text */
mbed_official 76:aeb1df146756 156 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 157 outputaddr += 4;
mbed_official 76:aeb1df146756 158 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 159 outputaddr += 4;
mbed_official 76:aeb1df146756 160 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 161 outputaddr += 4;
mbed_official 76:aeb1df146756 162 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 163 outputaddr += 4;
mbed_official 76:aeb1df146756 164 }
mbed_official 76:aeb1df146756 165 }
mbed_official 76:aeb1df146756 166
mbed_official 76:aeb1df146756 167 /* Disable AES before starting new processing */
mbed_official 76:aeb1df146756 168 AES_Cmd(DISABLE);
mbed_official 76:aeb1df146756 169
mbed_official 76:aeb1df146756 170 return status;
mbed_official 76:aeb1df146756 171 }
mbed_official 76:aeb1df146756 172
mbed_official 76:aeb1df146756 173 /**
mbed_official 76:aeb1df146756 174 * @brief Decrypt using AES in ECB Mode
mbed_official 76:aeb1df146756 175 * @param Key: Key used for AES algorithm.
mbed_official 76:aeb1df146756 176 * @param Input: pointer to the Input buffer.
mbed_official 76:aeb1df146756 177 * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes.
mbed_official 76:aeb1df146756 178 * @param Output: pointer to the returned buffer.
mbed_official 76:aeb1df146756 179 * @retval An ErrorStatus enumeration value:
mbed_official 76:aeb1df146756 180 * - SUCCESS: Operation done
mbed_official 76:aeb1df146756 181 * - ERROR: Operation failed
mbed_official 76:aeb1df146756 182 */
mbed_official 76:aeb1df146756 183 ErrorStatus AES_ECB_Decrypt(uint8_t* Key, uint8_t* Input, uint32_t Ilength, uint8_t* Output)
mbed_official 76:aeb1df146756 184 {
mbed_official 76:aeb1df146756 185 AES_InitTypeDef AES_InitStructure;
mbed_official 76:aeb1df146756 186 AES_KeyInitTypeDef AES_KeyInitStructure;
mbed_official 76:aeb1df146756 187 ErrorStatus status = SUCCESS;
mbed_official 76:aeb1df146756 188 uint32_t keyaddr = (uint32_t)Key;
mbed_official 76:aeb1df146756 189 uint32_t inputaddr = (uint32_t)Input;
mbed_official 76:aeb1df146756 190 uint32_t outputaddr = (uint32_t)Output;
mbed_official 76:aeb1df146756 191 __IO uint32_t counter = 0;
mbed_official 76:aeb1df146756 192 uint32_t ccstatus = 0;
mbed_official 76:aeb1df146756 193 uint32_t i = 0;
mbed_official 76:aeb1df146756 194
mbed_official 76:aeb1df146756 195 /* AES Key initialisation */
mbed_official 76:aeb1df146756 196 AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 197 keyaddr += 4;
mbed_official 76:aeb1df146756 198 AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 199 keyaddr += 4;
mbed_official 76:aeb1df146756 200 AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 201 keyaddr += 4;
mbed_official 76:aeb1df146756 202 AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 203 AES_KeyInit(&AES_KeyInitStructure);
mbed_official 76:aeb1df146756 204
mbed_official 76:aeb1df146756 205 /* AES configuration */
mbed_official 76:aeb1df146756 206 AES_InitStructure.AES_Operation = AES_Operation_KeyDerivAndDecryp;
mbed_official 76:aeb1df146756 207 AES_InitStructure.AES_Chaining = AES_Chaining_ECB;
mbed_official 76:aeb1df146756 208 AES_InitStructure.AES_DataType = AES_DataType_8b;
mbed_official 76:aeb1df146756 209 AES_Init(&AES_InitStructure);
mbed_official 76:aeb1df146756 210
mbed_official 76:aeb1df146756 211 /* Enable AES */
mbed_official 76:aeb1df146756 212 AES_Cmd(ENABLE);
mbed_official 76:aeb1df146756 213
mbed_official 76:aeb1df146756 214 for(i = 0; ((i < Ilength) && (status != ERROR)); i += 16)
mbed_official 76:aeb1df146756 215 {
mbed_official 76:aeb1df146756 216 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 217 inputaddr += 4;
mbed_official 76:aeb1df146756 218 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 219 inputaddr += 4;
mbed_official 76:aeb1df146756 220 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 221 inputaddr += 4;
mbed_official 76:aeb1df146756 222 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 223 inputaddr += 4;
mbed_official 76:aeb1df146756 224
mbed_official 76:aeb1df146756 225 /* Wait for CCF flag to be set */
mbed_official 76:aeb1df146756 226 counter = 0;
mbed_official 76:aeb1df146756 227 do
mbed_official 76:aeb1df146756 228 {
mbed_official 76:aeb1df146756 229 ccstatus = AES_GetFlagStatus(AES_FLAG_CCF);
mbed_official 76:aeb1df146756 230 counter++;
mbed_official 76:aeb1df146756 231 }while((counter != AES_CC_TIMEOUT) && (ccstatus == RESET));
mbed_official 76:aeb1df146756 232
mbed_official 76:aeb1df146756 233 if (ccstatus == RESET)
mbed_official 76:aeb1df146756 234 {
mbed_official 76:aeb1df146756 235 status = ERROR;
mbed_official 76:aeb1df146756 236 }
mbed_official 76:aeb1df146756 237 else
mbed_official 76:aeb1df146756 238 {
mbed_official 76:aeb1df146756 239 /* Clear CCF flag */
mbed_official 76:aeb1df146756 240 AES_ClearFlag(AES_FLAG_CCF);
mbed_official 76:aeb1df146756 241
mbed_official 76:aeb1df146756 242 /* Read cipher text */
mbed_official 76:aeb1df146756 243 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 244 outputaddr += 4;
mbed_official 76:aeb1df146756 245 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 246 outputaddr += 4;
mbed_official 76:aeb1df146756 247 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 248 outputaddr += 4;
mbed_official 76:aeb1df146756 249 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 250 outputaddr += 4;
mbed_official 76:aeb1df146756 251 }
mbed_official 76:aeb1df146756 252 }
mbed_official 76:aeb1df146756 253
mbed_official 76:aeb1df146756 254 /* Disable AES before starting new processing */
mbed_official 76:aeb1df146756 255 AES_Cmd(DISABLE);
mbed_official 76:aeb1df146756 256
mbed_official 76:aeb1df146756 257 return status;
mbed_official 76:aeb1df146756 258 }
mbed_official 76:aeb1df146756 259
mbed_official 76:aeb1df146756 260 /**
mbed_official 76:aeb1df146756 261 * @brief Encrypt using AES in CBC Mode
mbed_official 76:aeb1df146756 262 * @param InitVectors: Initialisation Vectors used for AES algorithm.
mbed_official 76:aeb1df146756 263 * @param Key: Key used for AES algorithm.
mbed_official 76:aeb1df146756 264 * @param Input: pointer to the Input buffer.
mbed_official 76:aeb1df146756 265 * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes.
mbed_official 76:aeb1df146756 266 * @param Output: pointer to the returned buffer.
mbed_official 76:aeb1df146756 267 * @retval An ErrorStatus enumeration value:
mbed_official 76:aeb1df146756 268 * - SUCCESS: Operation done
mbed_official 76:aeb1df146756 269 * - ERROR: Operation failed
mbed_official 76:aeb1df146756 270 */
mbed_official 76:aeb1df146756 271 ErrorStatus AES_CBC_Encrypt(uint8_t* Key, uint8_t InitVectors[16], uint8_t* Input, uint32_t Ilength, uint8_t* Output)
mbed_official 76:aeb1df146756 272 {
mbed_official 76:aeb1df146756 273 AES_InitTypeDef AES_InitStructure;
mbed_official 76:aeb1df146756 274 AES_KeyInitTypeDef AES_KeyInitStructure;
mbed_official 76:aeb1df146756 275 AES_IVInitTypeDef AES_IVInitStructure;
mbed_official 76:aeb1df146756 276 ErrorStatus status = SUCCESS;
mbed_official 76:aeb1df146756 277 uint32_t keyaddr = (uint32_t)Key;
mbed_official 76:aeb1df146756 278 uint32_t inputaddr = (uint32_t)Input;
mbed_official 76:aeb1df146756 279 uint32_t outputaddr = (uint32_t)Output;
mbed_official 76:aeb1df146756 280 uint32_t ivaddr = (uint32_t)InitVectors;
mbed_official 76:aeb1df146756 281 __IO uint32_t counter = 0;
mbed_official 76:aeb1df146756 282 uint32_t ccstatus = 0;
mbed_official 76:aeb1df146756 283 uint32_t i = 0;
mbed_official 76:aeb1df146756 284
mbed_official 76:aeb1df146756 285 /* AES Key initialisation*/
mbed_official 76:aeb1df146756 286 AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 287 keyaddr += 4;
mbed_official 76:aeb1df146756 288 AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 289 keyaddr += 4;
mbed_official 76:aeb1df146756 290 AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 291 keyaddr += 4;
mbed_official 76:aeb1df146756 292 AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 293 AES_KeyInit(&AES_KeyInitStructure);
mbed_official 76:aeb1df146756 294
mbed_official 76:aeb1df146756 295 /* AES Initialization Vectors */
mbed_official 76:aeb1df146756 296 AES_IVInitStructure.AES_IV3 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 297 ivaddr += 4;
mbed_official 76:aeb1df146756 298 AES_IVInitStructure.AES_IV2 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 299 ivaddr += 4;
mbed_official 76:aeb1df146756 300 AES_IVInitStructure.AES_IV1 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 301 ivaddr += 4;
mbed_official 76:aeb1df146756 302 AES_IVInitStructure.AES_IV0 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 303 AES_IVInit(&AES_IVInitStructure);
mbed_official 76:aeb1df146756 304
mbed_official 76:aeb1df146756 305 /* AES configuration */
mbed_official 76:aeb1df146756 306 AES_InitStructure.AES_Operation = AES_Operation_Encryp;
mbed_official 76:aeb1df146756 307 AES_InitStructure.AES_Chaining = AES_Chaining_CBC;
mbed_official 76:aeb1df146756 308 AES_InitStructure.AES_DataType = AES_DataType_8b;
mbed_official 76:aeb1df146756 309 AES_Init(&AES_InitStructure);
mbed_official 76:aeb1df146756 310
mbed_official 76:aeb1df146756 311 /* Enable AES */
mbed_official 76:aeb1df146756 312 AES_Cmd(ENABLE);
mbed_official 76:aeb1df146756 313
mbed_official 76:aeb1df146756 314 for(i = 0; ((i < Ilength) && (status != ERROR)); i += 16)
mbed_official 76:aeb1df146756 315 {
mbed_official 76:aeb1df146756 316 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 317 inputaddr += 4;
mbed_official 76:aeb1df146756 318 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 319 inputaddr += 4;
mbed_official 76:aeb1df146756 320 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 321 inputaddr += 4;
mbed_official 76:aeb1df146756 322 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 323 inputaddr += 4;
mbed_official 76:aeb1df146756 324
mbed_official 76:aeb1df146756 325 /* Wait for CCF flag to be set */
mbed_official 76:aeb1df146756 326 counter = 0;
mbed_official 76:aeb1df146756 327 do
mbed_official 76:aeb1df146756 328 {
mbed_official 76:aeb1df146756 329 ccstatus = AES_GetFlagStatus(AES_FLAG_CCF);
mbed_official 76:aeb1df146756 330 counter++;
mbed_official 76:aeb1df146756 331 }while((counter != AES_CC_TIMEOUT) && (ccstatus == RESET));
mbed_official 76:aeb1df146756 332
mbed_official 76:aeb1df146756 333 if (ccstatus == RESET)
mbed_official 76:aeb1df146756 334 {
mbed_official 76:aeb1df146756 335 status = ERROR;
mbed_official 76:aeb1df146756 336 }
mbed_official 76:aeb1df146756 337 else
mbed_official 76:aeb1df146756 338 {
mbed_official 76:aeb1df146756 339 /* Clear CCF flag */
mbed_official 76:aeb1df146756 340 AES_ClearFlag(AES_FLAG_CCF);
mbed_official 76:aeb1df146756 341
mbed_official 76:aeb1df146756 342 /* Read cipher text */
mbed_official 76:aeb1df146756 343 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 344 outputaddr += 4;
mbed_official 76:aeb1df146756 345 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 346 outputaddr += 4;
mbed_official 76:aeb1df146756 347 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 348 outputaddr += 4;
mbed_official 76:aeb1df146756 349 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 350 outputaddr += 4;
mbed_official 76:aeb1df146756 351 }
mbed_official 76:aeb1df146756 352 }
mbed_official 76:aeb1df146756 353
mbed_official 76:aeb1df146756 354 /* Disable AES before starting new processing */
mbed_official 76:aeb1df146756 355 AES_Cmd(DISABLE);
mbed_official 76:aeb1df146756 356
mbed_official 76:aeb1df146756 357 return status;
mbed_official 76:aeb1df146756 358 }
mbed_official 76:aeb1df146756 359
mbed_official 76:aeb1df146756 360 /**
mbed_official 76:aeb1df146756 361 * @brief Decrypt using AES in CBC Mode
mbed_official 76:aeb1df146756 362 * @param InitVectors: Initialisation Vectors used for AES algorithm.
mbed_official 76:aeb1df146756 363 * @param Key: Key used for AES algorithm.
mbed_official 76:aeb1df146756 364 * @param Input: pointer to the Input buffer.
mbed_official 76:aeb1df146756 365 * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes.
mbed_official 76:aeb1df146756 366 * @param Output: pointer to the returned buffer.
mbed_official 76:aeb1df146756 367 * @retval An ErrorStatus enumeration value:
mbed_official 76:aeb1df146756 368 * - SUCCESS: Operation done
mbed_official 76:aeb1df146756 369 * - ERROR: Operation failed
mbed_official 76:aeb1df146756 370 */
mbed_official 76:aeb1df146756 371 ErrorStatus AES_CBC_Decrypt(uint8_t* Key, uint8_t InitVectors[16], uint8_t* Input, uint32_t Ilength, uint8_t* Output)
mbed_official 76:aeb1df146756 372 {
mbed_official 76:aeb1df146756 373 AES_InitTypeDef AES_InitStructure;
mbed_official 76:aeb1df146756 374 AES_KeyInitTypeDef AES_KeyInitStructure;
mbed_official 76:aeb1df146756 375 AES_IVInitTypeDef AES_IVInitStructure;
mbed_official 76:aeb1df146756 376 ErrorStatus status = SUCCESS;
mbed_official 76:aeb1df146756 377 uint32_t keyaddr = (uint32_t)Key;
mbed_official 76:aeb1df146756 378 uint32_t inputaddr = (uint32_t)Input;
mbed_official 76:aeb1df146756 379 uint32_t outputaddr = (uint32_t)Output;
mbed_official 76:aeb1df146756 380 uint32_t ivaddr = (uint32_t)InitVectors;
mbed_official 76:aeb1df146756 381 __IO uint32_t counter = 0;
mbed_official 76:aeb1df146756 382 uint32_t ccstatus = 0;
mbed_official 76:aeb1df146756 383 uint32_t i = 0;
mbed_official 76:aeb1df146756 384
mbed_official 76:aeb1df146756 385 /* AES Key initialisation*/
mbed_official 76:aeb1df146756 386 AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 387 keyaddr += 4;
mbed_official 76:aeb1df146756 388 AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 389 keyaddr += 4;
mbed_official 76:aeb1df146756 390 AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 391 keyaddr += 4;
mbed_official 76:aeb1df146756 392 AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 393 AES_KeyInit(&AES_KeyInitStructure);
mbed_official 76:aeb1df146756 394
mbed_official 76:aeb1df146756 395 /* AES Initialization Vectors */
mbed_official 76:aeb1df146756 396 AES_IVInitStructure.AES_IV3 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 397 ivaddr += 4;
mbed_official 76:aeb1df146756 398 AES_IVInitStructure.AES_IV2 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 399 ivaddr += 4;
mbed_official 76:aeb1df146756 400 AES_IVInitStructure.AES_IV1 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 401 ivaddr += 4;
mbed_official 76:aeb1df146756 402 AES_IVInitStructure.AES_IV0 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 403 AES_IVInit(&AES_IVInitStructure);
mbed_official 76:aeb1df146756 404
mbed_official 76:aeb1df146756 405 /* AES configuration */
mbed_official 76:aeb1df146756 406 AES_InitStructure.AES_Operation = AES_Operation_KeyDerivAndDecryp;
mbed_official 76:aeb1df146756 407 AES_InitStructure.AES_Chaining = AES_Chaining_CBC;
mbed_official 76:aeb1df146756 408 AES_InitStructure.AES_DataType = AES_DataType_8b;
mbed_official 76:aeb1df146756 409 AES_Init(&AES_InitStructure);
mbed_official 76:aeb1df146756 410
mbed_official 76:aeb1df146756 411 /* Enable AES */
mbed_official 76:aeb1df146756 412 AES_Cmd(ENABLE);
mbed_official 76:aeb1df146756 413
mbed_official 76:aeb1df146756 414 for(i = 0; ((i < Ilength) && (status != ERROR)); i += 16)
mbed_official 76:aeb1df146756 415 {
mbed_official 76:aeb1df146756 416 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 417 inputaddr += 4;
mbed_official 76:aeb1df146756 418 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 419 inputaddr += 4;
mbed_official 76:aeb1df146756 420 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 421 inputaddr += 4;
mbed_official 76:aeb1df146756 422 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 423 inputaddr += 4;
mbed_official 76:aeb1df146756 424
mbed_official 76:aeb1df146756 425 /* Wait for CCF flag to be set */
mbed_official 76:aeb1df146756 426 counter = 0;
mbed_official 76:aeb1df146756 427 do
mbed_official 76:aeb1df146756 428 {
mbed_official 76:aeb1df146756 429 ccstatus = AES_GetFlagStatus(AES_FLAG_CCF);
mbed_official 76:aeb1df146756 430 counter++;
mbed_official 76:aeb1df146756 431 }while((counter != AES_CC_TIMEOUT) && (ccstatus == RESET));
mbed_official 76:aeb1df146756 432
mbed_official 76:aeb1df146756 433 if (ccstatus == RESET)
mbed_official 76:aeb1df146756 434 {
mbed_official 76:aeb1df146756 435 status = ERROR;
mbed_official 76:aeb1df146756 436 }
mbed_official 76:aeb1df146756 437 else
mbed_official 76:aeb1df146756 438 {
mbed_official 76:aeb1df146756 439 /* Clear CCF flag */
mbed_official 76:aeb1df146756 440 AES_ClearFlag(AES_FLAG_CCF);
mbed_official 76:aeb1df146756 441
mbed_official 76:aeb1df146756 442 /* Read cipher text */
mbed_official 76:aeb1df146756 443 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 444 outputaddr += 4;
mbed_official 76:aeb1df146756 445 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 446 outputaddr += 4;
mbed_official 76:aeb1df146756 447 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 448 outputaddr += 4;
mbed_official 76:aeb1df146756 449 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 450 outputaddr += 4;
mbed_official 76:aeb1df146756 451 }
mbed_official 76:aeb1df146756 452 }
mbed_official 76:aeb1df146756 453
mbed_official 76:aeb1df146756 454 /* Disable AES before starting new processing */
mbed_official 76:aeb1df146756 455 AES_Cmd(DISABLE);
mbed_official 76:aeb1df146756 456
mbed_official 76:aeb1df146756 457 return status;
mbed_official 76:aeb1df146756 458 }
mbed_official 76:aeb1df146756 459
mbed_official 76:aeb1df146756 460 /**
mbed_official 76:aeb1df146756 461 * @brief Encrypt using AES in CTR Mode
mbed_official 76:aeb1df146756 462 * @param InitVectors: Initialisation Vectors used for AES algorithm.
mbed_official 76:aeb1df146756 463 * @param Key: Key used for AES algorithm.
mbed_official 76:aeb1df146756 464 * @param Input: pointer to the Input buffer.
mbed_official 76:aeb1df146756 465 * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes.
mbed_official 76:aeb1df146756 466 * @param Output: pointer to the returned buffer.
mbed_official 76:aeb1df146756 467 * @retval An ErrorStatus enumeration value:
mbed_official 76:aeb1df146756 468 * - SUCCESS: Operation done
mbed_official 76:aeb1df146756 469 * - ERROR: Operation failed
mbed_official 76:aeb1df146756 470 */
mbed_official 76:aeb1df146756 471 ErrorStatus AES_CTR_Encrypt(uint8_t* Key, uint8_t InitVectors[16], uint8_t* Input, uint32_t Ilength, uint8_t* Output)
mbed_official 76:aeb1df146756 472 {
mbed_official 76:aeb1df146756 473 AES_InitTypeDef AES_InitStructure;
mbed_official 76:aeb1df146756 474 AES_KeyInitTypeDef AES_KeyInitStructure;
mbed_official 76:aeb1df146756 475 AES_IVInitTypeDef AES_IVInitStructure;
mbed_official 76:aeb1df146756 476
mbed_official 76:aeb1df146756 477 ErrorStatus status = SUCCESS;
mbed_official 76:aeb1df146756 478 uint32_t keyaddr = (uint32_t)Key;
mbed_official 76:aeb1df146756 479 uint32_t inputaddr = (uint32_t)Input;
mbed_official 76:aeb1df146756 480 uint32_t outputaddr = (uint32_t)Output;
mbed_official 76:aeb1df146756 481 uint32_t ivaddr = (uint32_t)InitVectors;
mbed_official 76:aeb1df146756 482 __IO uint32_t counter = 0;
mbed_official 76:aeb1df146756 483 uint32_t ccstatus = 0;
mbed_official 76:aeb1df146756 484 uint32_t i = 0;
mbed_official 76:aeb1df146756 485
mbed_official 76:aeb1df146756 486 /* AES key initialisation*/
mbed_official 76:aeb1df146756 487 AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 488 keyaddr += 4;
mbed_official 76:aeb1df146756 489 AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 490 keyaddr += 4;
mbed_official 76:aeb1df146756 491 AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 492 keyaddr += 4;
mbed_official 76:aeb1df146756 493 AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 494 AES_KeyInit(&AES_KeyInitStructure);
mbed_official 76:aeb1df146756 495
mbed_official 76:aeb1df146756 496 /* AES Initialization Vectors */
mbed_official 76:aeb1df146756 497 AES_IVInitStructure.AES_IV3 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 498 ivaddr += 4;
mbed_official 76:aeb1df146756 499 AES_IVInitStructure.AES_IV2= __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 500 ivaddr += 4;
mbed_official 76:aeb1df146756 501 AES_IVInitStructure.AES_IV1 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 502 ivaddr += 4;
mbed_official 76:aeb1df146756 503 AES_IVInitStructure.AES_IV0= __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 504 AES_IVInit(&AES_IVInitStructure);
mbed_official 76:aeb1df146756 505
mbed_official 76:aeb1df146756 506 /* AES configuration */
mbed_official 76:aeb1df146756 507 AES_InitStructure.AES_Operation = AES_Operation_Encryp;
mbed_official 76:aeb1df146756 508 AES_InitStructure.AES_Chaining = AES_Chaining_CTR;
mbed_official 76:aeb1df146756 509 AES_InitStructure.AES_DataType = AES_DataType_8b;
mbed_official 76:aeb1df146756 510 AES_Init(&AES_InitStructure);
mbed_official 76:aeb1df146756 511
mbed_official 76:aeb1df146756 512 /* Enable AES */
mbed_official 76:aeb1df146756 513 AES_Cmd(ENABLE);
mbed_official 76:aeb1df146756 514
mbed_official 76:aeb1df146756 515 for(i = 0; ((i < Ilength) && (status != ERROR)); i += 16)
mbed_official 76:aeb1df146756 516 {
mbed_official 76:aeb1df146756 517 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 518 inputaddr += 4;
mbed_official 76:aeb1df146756 519 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 520 inputaddr += 4;
mbed_official 76:aeb1df146756 521 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 522 inputaddr += 4;
mbed_official 76:aeb1df146756 523 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 524 inputaddr += 4;
mbed_official 76:aeb1df146756 525
mbed_official 76:aeb1df146756 526 /* Wait for CCF flag to be set */
mbed_official 76:aeb1df146756 527 counter = 0;
mbed_official 76:aeb1df146756 528 do
mbed_official 76:aeb1df146756 529 {
mbed_official 76:aeb1df146756 530 ccstatus = AES_GetFlagStatus(AES_FLAG_CCF);
mbed_official 76:aeb1df146756 531 counter++;
mbed_official 76:aeb1df146756 532 }while((counter != AES_CC_TIMEOUT) && (ccstatus == RESET));
mbed_official 76:aeb1df146756 533
mbed_official 76:aeb1df146756 534 if (ccstatus == RESET)
mbed_official 76:aeb1df146756 535 {
mbed_official 76:aeb1df146756 536 status = ERROR;
mbed_official 76:aeb1df146756 537 }
mbed_official 76:aeb1df146756 538 else
mbed_official 76:aeb1df146756 539 {
mbed_official 76:aeb1df146756 540 /* Clear CCF flag */
mbed_official 76:aeb1df146756 541 AES_ClearFlag(AES_FLAG_CCF);
mbed_official 76:aeb1df146756 542
mbed_official 76:aeb1df146756 543 /* Read cipher text */
mbed_official 76:aeb1df146756 544 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 545 outputaddr += 4;
mbed_official 76:aeb1df146756 546 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 547 outputaddr += 4;
mbed_official 76:aeb1df146756 548 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 549 outputaddr += 4;
mbed_official 76:aeb1df146756 550 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 551 outputaddr += 4;
mbed_official 76:aeb1df146756 552 }
mbed_official 76:aeb1df146756 553 }
mbed_official 76:aeb1df146756 554
mbed_official 76:aeb1df146756 555 /* Disable AES before starting new processing */
mbed_official 76:aeb1df146756 556 AES_Cmd(DISABLE);
mbed_official 76:aeb1df146756 557
mbed_official 76:aeb1df146756 558 return status;
mbed_official 76:aeb1df146756 559 }
mbed_official 76:aeb1df146756 560
mbed_official 76:aeb1df146756 561 /**
mbed_official 76:aeb1df146756 562 * @brief Decrypt using AES in CTR Mode
mbed_official 76:aeb1df146756 563 * @param InitVectors: Initialisation Vectors used for AES algorithm.
mbed_official 76:aeb1df146756 564 * @param Key: Key used for AES algorithm.
mbed_official 76:aeb1df146756 565 * @param Input: pointer to the Input buffer.
mbed_official 76:aeb1df146756 566 * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes.
mbed_official 76:aeb1df146756 567 * @param Output: pointer to the returned buffer.
mbed_official 76:aeb1df146756 568 * @retval An ErrorStatus enumeration value:
mbed_official 76:aeb1df146756 569 * - SUCCESS: Operation done
mbed_official 76:aeb1df146756 570 * - ERROR: Operation failed
mbed_official 76:aeb1df146756 571 */
mbed_official 76:aeb1df146756 572 ErrorStatus AES_CTR_Decrypt(uint8_t* Key, uint8_t InitVectors[16], uint8_t* Input, uint32_t Ilength, uint8_t* Output)
mbed_official 76:aeb1df146756 573 {
mbed_official 76:aeb1df146756 574 AES_InitTypeDef AES_InitStructure;
mbed_official 76:aeb1df146756 575 AES_KeyInitTypeDef AES_KeyInitStructure;
mbed_official 76:aeb1df146756 576 AES_IVInitTypeDef AES_IVInitStructure;
mbed_official 76:aeb1df146756 577
mbed_official 76:aeb1df146756 578 ErrorStatus status = SUCCESS;
mbed_official 76:aeb1df146756 579 uint32_t keyaddr = (uint32_t)Key;
mbed_official 76:aeb1df146756 580 uint32_t inputaddr = (uint32_t)Input;
mbed_official 76:aeb1df146756 581 uint32_t outputaddr = (uint32_t)Output;
mbed_official 76:aeb1df146756 582 uint32_t ivaddr = (uint32_t)InitVectors;
mbed_official 76:aeb1df146756 583 __IO uint32_t counter = 0;
mbed_official 76:aeb1df146756 584 uint32_t ccstatus = 0;
mbed_official 76:aeb1df146756 585 uint32_t i = 0;
mbed_official 76:aeb1df146756 586
mbed_official 76:aeb1df146756 587 /* AES Key initialisation*/
mbed_official 76:aeb1df146756 588 AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 589 keyaddr += 4;
mbed_official 76:aeb1df146756 590 AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 591 keyaddr += 4;
mbed_official 76:aeb1df146756 592 AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 593 keyaddr += 4;
mbed_official 76:aeb1df146756 594 AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t*)(keyaddr));
mbed_official 76:aeb1df146756 595 AES_KeyInit(&AES_KeyInitStructure);
mbed_official 76:aeb1df146756 596
mbed_official 76:aeb1df146756 597 /* AES Initialization Vectors */
mbed_official 76:aeb1df146756 598 AES_IVInitStructure.AES_IV3 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 599 ivaddr += 4;
mbed_official 76:aeb1df146756 600 AES_IVInitStructure.AES_IV2 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 601 ivaddr += 4;
mbed_official 76:aeb1df146756 602 AES_IVInitStructure.AES_IV1 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 603 ivaddr += 4;
mbed_official 76:aeb1df146756 604 AES_IVInitStructure.AES_IV0 = __REV(*(uint32_t*)(ivaddr));
mbed_official 76:aeb1df146756 605 AES_IVInit(&AES_IVInitStructure);
mbed_official 76:aeb1df146756 606
mbed_official 76:aeb1df146756 607 /* AES configuration */
mbed_official 76:aeb1df146756 608 AES_InitStructure.AES_Operation = AES_Operation_KeyDerivAndDecryp;
mbed_official 76:aeb1df146756 609 AES_InitStructure.AES_Chaining = AES_Chaining_CTR;
mbed_official 76:aeb1df146756 610 AES_InitStructure.AES_DataType = AES_DataType_8b;
mbed_official 76:aeb1df146756 611 AES_Init(&AES_InitStructure);
mbed_official 76:aeb1df146756 612
mbed_official 76:aeb1df146756 613 /* Enable AES */
mbed_official 76:aeb1df146756 614 AES_Cmd(ENABLE);
mbed_official 76:aeb1df146756 615
mbed_official 76:aeb1df146756 616 for(i = 0; ((i < Ilength) && (status != ERROR)); i += 16)
mbed_official 76:aeb1df146756 617 {
mbed_official 76:aeb1df146756 618 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 619 inputaddr += 4;
mbed_official 76:aeb1df146756 620 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 621 inputaddr += 4;
mbed_official 76:aeb1df146756 622 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 623 inputaddr += 4;
mbed_official 76:aeb1df146756 624 AES_WriteSubData(*(uint32_t*)(inputaddr));
mbed_official 76:aeb1df146756 625 inputaddr += 4;
mbed_official 76:aeb1df146756 626
mbed_official 76:aeb1df146756 627 /* Wait for CCF flag to be set */
mbed_official 76:aeb1df146756 628 counter = 0;
mbed_official 76:aeb1df146756 629 do
mbed_official 76:aeb1df146756 630 {
mbed_official 76:aeb1df146756 631 ccstatus = AES_GetFlagStatus(AES_FLAG_CCF);
mbed_official 76:aeb1df146756 632 counter++;
mbed_official 76:aeb1df146756 633 }while((counter != AES_CC_TIMEOUT) && (ccstatus == RESET));
mbed_official 76:aeb1df146756 634
mbed_official 76:aeb1df146756 635 if (ccstatus == RESET)
mbed_official 76:aeb1df146756 636 {
mbed_official 76:aeb1df146756 637 status = ERROR;
mbed_official 76:aeb1df146756 638 }
mbed_official 76:aeb1df146756 639 else
mbed_official 76:aeb1df146756 640 {
mbed_official 76:aeb1df146756 641 /* Clear CCF flag */
mbed_official 76:aeb1df146756 642 AES_ClearFlag(AES_FLAG_CCF);
mbed_official 76:aeb1df146756 643
mbed_official 76:aeb1df146756 644 /* Read cipher text */
mbed_official 76:aeb1df146756 645 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 646 outputaddr += 4;
mbed_official 76:aeb1df146756 647 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 648 outputaddr += 4;
mbed_official 76:aeb1df146756 649 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 650 outputaddr += 4;
mbed_official 76:aeb1df146756 651 *(uint32_t*)(outputaddr) = AES_ReadSubData();
mbed_official 76:aeb1df146756 652 outputaddr += 4;
mbed_official 76:aeb1df146756 653 }
mbed_official 76:aeb1df146756 654 }
mbed_official 76:aeb1df146756 655
mbed_official 76:aeb1df146756 656 /* Disable AES before starting new processing */
mbed_official 76:aeb1df146756 657 AES_Cmd(DISABLE);
mbed_official 76:aeb1df146756 658
mbed_official 76:aeb1df146756 659 return status;
mbed_official 76:aeb1df146756 660 }
mbed_official 76:aeb1df146756 661
mbed_official 76:aeb1df146756 662 /**
mbed_official 76:aeb1df146756 663 * @}
mbed_official 76:aeb1df146756 664 */
mbed_official 76:aeb1df146756 665
mbed_official 76:aeb1df146756 666 /**
mbed_official 76:aeb1df146756 667 * @}
mbed_official 76:aeb1df146756 668 */
mbed_official 76:aeb1df146756 669
mbed_official 76:aeb1df146756 670 /**
mbed_official 76:aeb1df146756 671 * @}
mbed_official 76:aeb1df146756 672 */
mbed_official 76:aeb1df146756 673
mbed_official 76:aeb1df146756 674 /**
mbed_official 76:aeb1df146756 675 * @}
mbed_official 76:aeb1df146756 676 */
mbed_official 76:aeb1df146756 677
mbed_official 76:aeb1df146756 678 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
mbed_official 76:aeb1df146756 679