mbed library sources
Fork of mbed-src by
targets/cmsis/TARGET_STM/TARGET_NUCLEO_L152RE/stm32l1xx_aes_util.c@106:ced8cbb51063, 2014-02-26 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Feb 26 09:45:12 2014 +0000
- Revision:
- 106:ced8cbb51063
- Parent:
- 80:66393a7b209d
Synchronized with git revision 4222735eff5868389433f0e9271976b39c8115cd
Full URL: https://github.com/mbedmicro/mbed/commit/4222735eff5868389433f0e9271976b39c8115cd/
[NUCLEO_xxx] Update STM32CubeF4 driver V1.0.0 + update license
Who changed what in which revision?
User | Revision | Line number | New 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 | 80:66393a7b209d | 5 | * @version V1.3.0 |
mbed_official | 80:66393a7b209d | 6 | * @date 31-January-2014 |
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 | 106:ced8cbb51063 | 33 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
mbed_official | 76:aeb1df146756 | 34 | * |
mbed_official | 106:ced8cbb51063 | 35 | * Redistribution and use in source and binary forms, with or without modification, |
mbed_official | 106:ced8cbb51063 | 36 | * are permitted provided that the following conditions are met: |
mbed_official | 106:ced8cbb51063 | 37 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 106:ced8cbb51063 | 38 | * this list of conditions and the following disclaimer. |
mbed_official | 106:ced8cbb51063 | 39 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 106:ced8cbb51063 | 40 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 106:ced8cbb51063 | 41 | * and/or other materials provided with the distribution. |
mbed_official | 106:ced8cbb51063 | 42 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 106:ced8cbb51063 | 43 | * may be used to endorse or promote products derived from this software |
mbed_official | 106:ced8cbb51063 | 44 | * without specific prior written permission. |
mbed_official | 76:aeb1df146756 | 45 | * |
mbed_official | 106:ced8cbb51063 | 46 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 106:ced8cbb51063 | 47 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 106:ced8cbb51063 | 48 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 106:ced8cbb51063 | 49 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 106:ced8cbb51063 | 50 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 106:ced8cbb51063 | 51 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 106:ced8cbb51063 | 52 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 106:ced8cbb51063 | 53 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 106:ced8cbb51063 | 54 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 106:ced8cbb51063 | 55 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 76:aeb1df146756 | 56 | * |
mbed_official | 76:aeb1df146756 | 57 | ****************************************************************************** |
mbed_official | 76:aeb1df146756 | 58 | */ |
mbed_official | 76:aeb1df146756 | 59 | |
mbed_official | 76:aeb1df146756 | 60 | /* Includes ------------------------------------------------------------------*/ |
mbed_official | 76:aeb1df146756 | 61 | #include "stm32l1xx_aes.h" |
mbed_official | 76:aeb1df146756 | 62 | |
mbed_official | 76:aeb1df146756 | 63 | /** @addtogroup STM32L1xx_StdPeriph_Driver |
mbed_official | 76:aeb1df146756 | 64 | * @{ |
mbed_official | 76:aeb1df146756 | 65 | */ |
mbed_official | 76:aeb1df146756 | 66 | |
mbed_official | 76:aeb1df146756 | 67 | /** @addtogroup AES |
mbed_official | 76:aeb1df146756 | 68 | * @brief AES driver modules |
mbed_official | 76:aeb1df146756 | 69 | * @{ |
mbed_official | 76:aeb1df146756 | 70 | */ |
mbed_official | 76:aeb1df146756 | 71 | |
mbed_official | 76:aeb1df146756 | 72 | /* Private typedef -----------------------------------------------------------*/ |
mbed_official | 76:aeb1df146756 | 73 | /* Private define ------------------------------------------------------------*/ |
mbed_official | 76:aeb1df146756 | 74 | #define AES_CC_TIMEOUT ((uint32_t) 0x00010000) |
mbed_official | 76:aeb1df146756 | 75 | |
mbed_official | 76:aeb1df146756 | 76 | /* Private macro -------------------------------------------------------------*/ |
mbed_official | 76:aeb1df146756 | 77 | /* Private variables ---------------------------------------------------------*/ |
mbed_official | 76:aeb1df146756 | 78 | /* Private function prototypes -----------------------------------------------*/ |
mbed_official | 76:aeb1df146756 | 79 | /* Private functions ---------------------------------------------------------*/ |
mbed_official | 76:aeb1df146756 | 80 | |
mbed_official | 76:aeb1df146756 | 81 | /** @defgroup AES_Private_Functions |
mbed_official | 76:aeb1df146756 | 82 | * @{ |
mbed_official | 76:aeb1df146756 | 83 | */ |
mbed_official | 76:aeb1df146756 | 84 | |
mbed_official | 76:aeb1df146756 | 85 | /** @defgroup AES_Group6 High Level AES functions |
mbed_official | 76:aeb1df146756 | 86 | * @brief High Level AES functions |
mbed_official | 76:aeb1df146756 | 87 | * |
mbed_official | 76:aeb1df146756 | 88 | @verbatim |
mbed_official | 76:aeb1df146756 | 89 | ================================================================================ |
mbed_official | 76:aeb1df146756 | 90 | ##### High Level AES functions ##### |
mbed_official | 76:aeb1df146756 | 91 | ================================================================================ |
mbed_official | 76:aeb1df146756 | 92 | |
mbed_official | 76:aeb1df146756 | 93 | @endverbatim |
mbed_official | 76:aeb1df146756 | 94 | * @{ |
mbed_official | 76:aeb1df146756 | 95 | */ |
mbed_official | 76:aeb1df146756 | 96 | |
mbed_official | 76:aeb1df146756 | 97 | /** |
mbed_official | 76:aeb1df146756 | 98 | * @brief Encrypt using AES in ECB Mode |
mbed_official | 76:aeb1df146756 | 99 | * @param Key: Key used for AES algorithm. |
mbed_official | 76:aeb1df146756 | 100 | * @param Input: pointer to the Input buffer. |
mbed_official | 76:aeb1df146756 | 101 | * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes. |
mbed_official | 76:aeb1df146756 | 102 | * @param Output: pointer to the returned buffer. |
mbed_official | 76:aeb1df146756 | 103 | * @retval An ErrorStatus enumeration value: |
mbed_official | 76:aeb1df146756 | 104 | * - SUCCESS: Operation done |
mbed_official | 76:aeb1df146756 | 105 | * - ERROR: Operation failed |
mbed_official | 76:aeb1df146756 | 106 | */ |
mbed_official | 76:aeb1df146756 | 107 | ErrorStatus AES_ECB_Encrypt(uint8_t* Key, uint8_t* Input, uint32_t Ilength, uint8_t* Output) |
mbed_official | 76:aeb1df146756 | 108 | { |
mbed_official | 76:aeb1df146756 | 109 | AES_InitTypeDef AES_InitStructure; |
mbed_official | 76:aeb1df146756 | 110 | AES_KeyInitTypeDef AES_KeyInitStructure; |
mbed_official | 76:aeb1df146756 | 111 | ErrorStatus status = SUCCESS; |
mbed_official | 76:aeb1df146756 | 112 | uint32_t keyaddr = (uint32_t)Key; |
mbed_official | 76:aeb1df146756 | 113 | uint32_t inputaddr = (uint32_t)Input; |
mbed_official | 76:aeb1df146756 | 114 | uint32_t outputaddr = (uint32_t)Output; |
mbed_official | 76:aeb1df146756 | 115 | __IO uint32_t counter = 0; |
mbed_official | 76:aeb1df146756 | 116 | uint32_t ccstatus = 0; |
mbed_official | 76:aeb1df146756 | 117 | uint32_t i = 0; |
mbed_official | 76:aeb1df146756 | 118 | |
mbed_official | 76:aeb1df146756 | 119 | /* AES Key initialisation */ |
mbed_official | 76:aeb1df146756 | 120 | AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 121 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 122 | AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 123 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 124 | AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 125 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 126 | AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 127 | AES_KeyInit(&AES_KeyInitStructure); |
mbed_official | 76:aeb1df146756 | 128 | |
mbed_official | 76:aeb1df146756 | 129 | /* AES configuration */ |
mbed_official | 76:aeb1df146756 | 130 | AES_InitStructure.AES_Operation = AES_Operation_Encryp; |
mbed_official | 76:aeb1df146756 | 131 | AES_InitStructure.AES_Chaining = AES_Chaining_ECB; |
mbed_official | 76:aeb1df146756 | 132 | AES_InitStructure.AES_DataType = AES_DataType_8b; |
mbed_official | 76:aeb1df146756 | 133 | AES_Init(&AES_InitStructure); |
mbed_official | 76:aeb1df146756 | 134 | |
mbed_official | 76:aeb1df146756 | 135 | /* Enable AES */ |
mbed_official | 76:aeb1df146756 | 136 | AES_Cmd(ENABLE); |
mbed_official | 76:aeb1df146756 | 137 | |
mbed_official | 76:aeb1df146756 | 138 | for(i = 0; ((i < Ilength) && (status != ERROR)); i += 16) |
mbed_official | 76:aeb1df146756 | 139 | { |
mbed_official | 76:aeb1df146756 | 140 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 141 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 142 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 143 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 144 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 145 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 146 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 147 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 148 | |
mbed_official | 76:aeb1df146756 | 149 | /* Wait for CCF flag to be set */ |
mbed_official | 76:aeb1df146756 | 150 | counter = 0; |
mbed_official | 76:aeb1df146756 | 151 | do |
mbed_official | 76:aeb1df146756 | 152 | { |
mbed_official | 76:aeb1df146756 | 153 | ccstatus = AES_GetFlagStatus(AES_FLAG_CCF); |
mbed_official | 76:aeb1df146756 | 154 | counter++; |
mbed_official | 76:aeb1df146756 | 155 | }while((counter != AES_CC_TIMEOUT) && (ccstatus == RESET)); |
mbed_official | 76:aeb1df146756 | 156 | |
mbed_official | 76:aeb1df146756 | 157 | if (ccstatus == RESET) |
mbed_official | 76:aeb1df146756 | 158 | { |
mbed_official | 76:aeb1df146756 | 159 | status = ERROR; |
mbed_official | 76:aeb1df146756 | 160 | } |
mbed_official | 76:aeb1df146756 | 161 | else |
mbed_official | 76:aeb1df146756 | 162 | { |
mbed_official | 76:aeb1df146756 | 163 | /* Clear CCF flag */ |
mbed_official | 76:aeb1df146756 | 164 | AES_ClearFlag(AES_FLAG_CCF); |
mbed_official | 76:aeb1df146756 | 165 | /* Read cipher text */ |
mbed_official | 76:aeb1df146756 | 166 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 167 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 168 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 169 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 170 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 171 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 172 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 173 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 174 | } |
mbed_official | 76:aeb1df146756 | 175 | } |
mbed_official | 76:aeb1df146756 | 176 | |
mbed_official | 76:aeb1df146756 | 177 | /* Disable AES before starting new processing */ |
mbed_official | 76:aeb1df146756 | 178 | AES_Cmd(DISABLE); |
mbed_official | 76:aeb1df146756 | 179 | |
mbed_official | 76:aeb1df146756 | 180 | return status; |
mbed_official | 76:aeb1df146756 | 181 | } |
mbed_official | 76:aeb1df146756 | 182 | |
mbed_official | 76:aeb1df146756 | 183 | /** |
mbed_official | 76:aeb1df146756 | 184 | * @brief Decrypt using AES in ECB Mode |
mbed_official | 76:aeb1df146756 | 185 | * @param Key: Key used for AES algorithm. |
mbed_official | 76:aeb1df146756 | 186 | * @param Input: pointer to the Input buffer. |
mbed_official | 76:aeb1df146756 | 187 | * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes. |
mbed_official | 76:aeb1df146756 | 188 | * @param Output: pointer to the returned buffer. |
mbed_official | 76:aeb1df146756 | 189 | * @retval An ErrorStatus enumeration value: |
mbed_official | 76:aeb1df146756 | 190 | * - SUCCESS: Operation done |
mbed_official | 76:aeb1df146756 | 191 | * - ERROR: Operation failed |
mbed_official | 76:aeb1df146756 | 192 | */ |
mbed_official | 76:aeb1df146756 | 193 | ErrorStatus AES_ECB_Decrypt(uint8_t* Key, uint8_t* Input, uint32_t Ilength, uint8_t* Output) |
mbed_official | 76:aeb1df146756 | 194 | { |
mbed_official | 76:aeb1df146756 | 195 | AES_InitTypeDef AES_InitStructure; |
mbed_official | 76:aeb1df146756 | 196 | AES_KeyInitTypeDef AES_KeyInitStructure; |
mbed_official | 76:aeb1df146756 | 197 | ErrorStatus status = SUCCESS; |
mbed_official | 76:aeb1df146756 | 198 | uint32_t keyaddr = (uint32_t)Key; |
mbed_official | 76:aeb1df146756 | 199 | uint32_t inputaddr = (uint32_t)Input; |
mbed_official | 76:aeb1df146756 | 200 | uint32_t outputaddr = (uint32_t)Output; |
mbed_official | 76:aeb1df146756 | 201 | __IO uint32_t counter = 0; |
mbed_official | 76:aeb1df146756 | 202 | uint32_t ccstatus = 0; |
mbed_official | 76:aeb1df146756 | 203 | uint32_t i = 0; |
mbed_official | 76:aeb1df146756 | 204 | |
mbed_official | 76:aeb1df146756 | 205 | /* AES Key initialisation */ |
mbed_official | 76:aeb1df146756 | 206 | AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 207 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 208 | AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 209 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 210 | AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 211 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 212 | AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 213 | AES_KeyInit(&AES_KeyInitStructure); |
mbed_official | 76:aeb1df146756 | 214 | |
mbed_official | 76:aeb1df146756 | 215 | /* AES configuration */ |
mbed_official | 76:aeb1df146756 | 216 | AES_InitStructure.AES_Operation = AES_Operation_KeyDerivAndDecryp; |
mbed_official | 76:aeb1df146756 | 217 | AES_InitStructure.AES_Chaining = AES_Chaining_ECB; |
mbed_official | 76:aeb1df146756 | 218 | AES_InitStructure.AES_DataType = AES_DataType_8b; |
mbed_official | 76:aeb1df146756 | 219 | AES_Init(&AES_InitStructure); |
mbed_official | 76:aeb1df146756 | 220 | |
mbed_official | 76:aeb1df146756 | 221 | /* Enable AES */ |
mbed_official | 76:aeb1df146756 | 222 | AES_Cmd(ENABLE); |
mbed_official | 76:aeb1df146756 | 223 | |
mbed_official | 76:aeb1df146756 | 224 | for(i = 0; ((i < Ilength) && (status != ERROR)); i += 16) |
mbed_official | 76:aeb1df146756 | 225 | { |
mbed_official | 76:aeb1df146756 | 226 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 227 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 228 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 229 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 230 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 231 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 232 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 233 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 234 | |
mbed_official | 76:aeb1df146756 | 235 | /* Wait for CCF flag to be set */ |
mbed_official | 76:aeb1df146756 | 236 | counter = 0; |
mbed_official | 76:aeb1df146756 | 237 | do |
mbed_official | 76:aeb1df146756 | 238 | { |
mbed_official | 76:aeb1df146756 | 239 | ccstatus = AES_GetFlagStatus(AES_FLAG_CCF); |
mbed_official | 76:aeb1df146756 | 240 | counter++; |
mbed_official | 76:aeb1df146756 | 241 | }while((counter != AES_CC_TIMEOUT) && (ccstatus == RESET)); |
mbed_official | 76:aeb1df146756 | 242 | |
mbed_official | 76:aeb1df146756 | 243 | if (ccstatus == RESET) |
mbed_official | 76:aeb1df146756 | 244 | { |
mbed_official | 76:aeb1df146756 | 245 | status = ERROR; |
mbed_official | 76:aeb1df146756 | 246 | } |
mbed_official | 76:aeb1df146756 | 247 | else |
mbed_official | 76:aeb1df146756 | 248 | { |
mbed_official | 76:aeb1df146756 | 249 | /* Clear CCF flag */ |
mbed_official | 76:aeb1df146756 | 250 | AES_ClearFlag(AES_FLAG_CCF); |
mbed_official | 76:aeb1df146756 | 251 | |
mbed_official | 76:aeb1df146756 | 252 | /* Read cipher text */ |
mbed_official | 76:aeb1df146756 | 253 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 254 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 255 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 256 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 257 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 258 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 259 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 260 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 261 | } |
mbed_official | 76:aeb1df146756 | 262 | } |
mbed_official | 76:aeb1df146756 | 263 | |
mbed_official | 76:aeb1df146756 | 264 | /* Disable AES before starting new processing */ |
mbed_official | 76:aeb1df146756 | 265 | AES_Cmd(DISABLE); |
mbed_official | 76:aeb1df146756 | 266 | |
mbed_official | 76:aeb1df146756 | 267 | return status; |
mbed_official | 76:aeb1df146756 | 268 | } |
mbed_official | 76:aeb1df146756 | 269 | |
mbed_official | 76:aeb1df146756 | 270 | /** |
mbed_official | 76:aeb1df146756 | 271 | * @brief Encrypt using AES in CBC Mode |
mbed_official | 76:aeb1df146756 | 272 | * @param InitVectors: Initialisation Vectors used for AES algorithm. |
mbed_official | 76:aeb1df146756 | 273 | * @param Key: Key used for AES algorithm. |
mbed_official | 76:aeb1df146756 | 274 | * @param Input: pointer to the Input buffer. |
mbed_official | 76:aeb1df146756 | 275 | * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes. |
mbed_official | 76:aeb1df146756 | 276 | * @param Output: pointer to the returned buffer. |
mbed_official | 76:aeb1df146756 | 277 | * @retval An ErrorStatus enumeration value: |
mbed_official | 76:aeb1df146756 | 278 | * - SUCCESS: Operation done |
mbed_official | 76:aeb1df146756 | 279 | * - ERROR: Operation failed |
mbed_official | 76:aeb1df146756 | 280 | */ |
mbed_official | 76:aeb1df146756 | 281 | ErrorStatus AES_CBC_Encrypt(uint8_t* Key, uint8_t InitVectors[16], uint8_t* Input, uint32_t Ilength, uint8_t* Output) |
mbed_official | 76:aeb1df146756 | 282 | { |
mbed_official | 76:aeb1df146756 | 283 | AES_InitTypeDef AES_InitStructure; |
mbed_official | 76:aeb1df146756 | 284 | AES_KeyInitTypeDef AES_KeyInitStructure; |
mbed_official | 76:aeb1df146756 | 285 | AES_IVInitTypeDef AES_IVInitStructure; |
mbed_official | 76:aeb1df146756 | 286 | ErrorStatus status = SUCCESS; |
mbed_official | 76:aeb1df146756 | 287 | uint32_t keyaddr = (uint32_t)Key; |
mbed_official | 76:aeb1df146756 | 288 | uint32_t inputaddr = (uint32_t)Input; |
mbed_official | 76:aeb1df146756 | 289 | uint32_t outputaddr = (uint32_t)Output; |
mbed_official | 76:aeb1df146756 | 290 | uint32_t ivaddr = (uint32_t)InitVectors; |
mbed_official | 76:aeb1df146756 | 291 | __IO uint32_t counter = 0; |
mbed_official | 76:aeb1df146756 | 292 | uint32_t ccstatus = 0; |
mbed_official | 76:aeb1df146756 | 293 | uint32_t i = 0; |
mbed_official | 76:aeb1df146756 | 294 | |
mbed_official | 76:aeb1df146756 | 295 | /* AES Key initialisation*/ |
mbed_official | 76:aeb1df146756 | 296 | AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 297 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 298 | AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 299 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 300 | AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 301 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 302 | AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 303 | AES_KeyInit(&AES_KeyInitStructure); |
mbed_official | 76:aeb1df146756 | 304 | |
mbed_official | 76:aeb1df146756 | 305 | /* AES Initialization Vectors */ |
mbed_official | 76:aeb1df146756 | 306 | AES_IVInitStructure.AES_IV3 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 307 | ivaddr += 4; |
mbed_official | 76:aeb1df146756 | 308 | AES_IVInitStructure.AES_IV2 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 309 | ivaddr += 4; |
mbed_official | 76:aeb1df146756 | 310 | AES_IVInitStructure.AES_IV1 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 311 | ivaddr += 4; |
mbed_official | 76:aeb1df146756 | 312 | AES_IVInitStructure.AES_IV0 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 313 | AES_IVInit(&AES_IVInitStructure); |
mbed_official | 76:aeb1df146756 | 314 | |
mbed_official | 76:aeb1df146756 | 315 | /* AES configuration */ |
mbed_official | 76:aeb1df146756 | 316 | AES_InitStructure.AES_Operation = AES_Operation_Encryp; |
mbed_official | 76:aeb1df146756 | 317 | AES_InitStructure.AES_Chaining = AES_Chaining_CBC; |
mbed_official | 76:aeb1df146756 | 318 | AES_InitStructure.AES_DataType = AES_DataType_8b; |
mbed_official | 76:aeb1df146756 | 319 | AES_Init(&AES_InitStructure); |
mbed_official | 76:aeb1df146756 | 320 | |
mbed_official | 76:aeb1df146756 | 321 | /* Enable AES */ |
mbed_official | 76:aeb1df146756 | 322 | AES_Cmd(ENABLE); |
mbed_official | 76:aeb1df146756 | 323 | |
mbed_official | 76:aeb1df146756 | 324 | for(i = 0; ((i < Ilength) && (status != ERROR)); i += 16) |
mbed_official | 76:aeb1df146756 | 325 | { |
mbed_official | 76:aeb1df146756 | 326 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 327 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 328 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 329 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 330 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 331 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 332 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 333 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 334 | |
mbed_official | 76:aeb1df146756 | 335 | /* Wait for CCF flag to be set */ |
mbed_official | 76:aeb1df146756 | 336 | counter = 0; |
mbed_official | 76:aeb1df146756 | 337 | do |
mbed_official | 76:aeb1df146756 | 338 | { |
mbed_official | 76:aeb1df146756 | 339 | ccstatus = AES_GetFlagStatus(AES_FLAG_CCF); |
mbed_official | 76:aeb1df146756 | 340 | counter++; |
mbed_official | 76:aeb1df146756 | 341 | }while((counter != AES_CC_TIMEOUT) && (ccstatus == RESET)); |
mbed_official | 76:aeb1df146756 | 342 | |
mbed_official | 76:aeb1df146756 | 343 | if (ccstatus == RESET) |
mbed_official | 76:aeb1df146756 | 344 | { |
mbed_official | 76:aeb1df146756 | 345 | status = ERROR; |
mbed_official | 76:aeb1df146756 | 346 | } |
mbed_official | 76:aeb1df146756 | 347 | else |
mbed_official | 76:aeb1df146756 | 348 | { |
mbed_official | 76:aeb1df146756 | 349 | /* Clear CCF flag */ |
mbed_official | 76:aeb1df146756 | 350 | AES_ClearFlag(AES_FLAG_CCF); |
mbed_official | 76:aeb1df146756 | 351 | |
mbed_official | 76:aeb1df146756 | 352 | /* Read cipher text */ |
mbed_official | 76:aeb1df146756 | 353 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 354 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 355 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 356 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 357 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 358 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 359 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 360 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 361 | } |
mbed_official | 76:aeb1df146756 | 362 | } |
mbed_official | 76:aeb1df146756 | 363 | |
mbed_official | 76:aeb1df146756 | 364 | /* Disable AES before starting new processing */ |
mbed_official | 76:aeb1df146756 | 365 | AES_Cmd(DISABLE); |
mbed_official | 76:aeb1df146756 | 366 | |
mbed_official | 76:aeb1df146756 | 367 | return status; |
mbed_official | 76:aeb1df146756 | 368 | } |
mbed_official | 76:aeb1df146756 | 369 | |
mbed_official | 76:aeb1df146756 | 370 | /** |
mbed_official | 76:aeb1df146756 | 371 | * @brief Decrypt using AES in CBC Mode |
mbed_official | 76:aeb1df146756 | 372 | * @param InitVectors: Initialisation Vectors used for AES algorithm. |
mbed_official | 76:aeb1df146756 | 373 | * @param Key: Key used for AES algorithm. |
mbed_official | 76:aeb1df146756 | 374 | * @param Input: pointer to the Input buffer. |
mbed_official | 76:aeb1df146756 | 375 | * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes. |
mbed_official | 76:aeb1df146756 | 376 | * @param Output: pointer to the returned buffer. |
mbed_official | 76:aeb1df146756 | 377 | * @retval An ErrorStatus enumeration value: |
mbed_official | 76:aeb1df146756 | 378 | * - SUCCESS: Operation done |
mbed_official | 76:aeb1df146756 | 379 | * - ERROR: Operation failed |
mbed_official | 76:aeb1df146756 | 380 | */ |
mbed_official | 76:aeb1df146756 | 381 | ErrorStatus AES_CBC_Decrypt(uint8_t* Key, uint8_t InitVectors[16], uint8_t* Input, uint32_t Ilength, uint8_t* Output) |
mbed_official | 76:aeb1df146756 | 382 | { |
mbed_official | 76:aeb1df146756 | 383 | AES_InitTypeDef AES_InitStructure; |
mbed_official | 76:aeb1df146756 | 384 | AES_KeyInitTypeDef AES_KeyInitStructure; |
mbed_official | 76:aeb1df146756 | 385 | AES_IVInitTypeDef AES_IVInitStructure; |
mbed_official | 76:aeb1df146756 | 386 | ErrorStatus status = SUCCESS; |
mbed_official | 76:aeb1df146756 | 387 | uint32_t keyaddr = (uint32_t)Key; |
mbed_official | 76:aeb1df146756 | 388 | uint32_t inputaddr = (uint32_t)Input; |
mbed_official | 76:aeb1df146756 | 389 | uint32_t outputaddr = (uint32_t)Output; |
mbed_official | 76:aeb1df146756 | 390 | uint32_t ivaddr = (uint32_t)InitVectors; |
mbed_official | 76:aeb1df146756 | 391 | __IO uint32_t counter = 0; |
mbed_official | 76:aeb1df146756 | 392 | uint32_t ccstatus = 0; |
mbed_official | 76:aeb1df146756 | 393 | uint32_t i = 0; |
mbed_official | 76:aeb1df146756 | 394 | |
mbed_official | 76:aeb1df146756 | 395 | /* AES Key initialisation*/ |
mbed_official | 76:aeb1df146756 | 396 | AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 397 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 398 | AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 399 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 400 | AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 401 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 402 | AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 403 | AES_KeyInit(&AES_KeyInitStructure); |
mbed_official | 76:aeb1df146756 | 404 | |
mbed_official | 76:aeb1df146756 | 405 | /* AES Initialization Vectors */ |
mbed_official | 76:aeb1df146756 | 406 | AES_IVInitStructure.AES_IV3 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 407 | ivaddr += 4; |
mbed_official | 76:aeb1df146756 | 408 | AES_IVInitStructure.AES_IV2 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 409 | ivaddr += 4; |
mbed_official | 76:aeb1df146756 | 410 | AES_IVInitStructure.AES_IV1 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 411 | ivaddr += 4; |
mbed_official | 76:aeb1df146756 | 412 | AES_IVInitStructure.AES_IV0 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 413 | AES_IVInit(&AES_IVInitStructure); |
mbed_official | 76:aeb1df146756 | 414 | |
mbed_official | 76:aeb1df146756 | 415 | /* AES configuration */ |
mbed_official | 76:aeb1df146756 | 416 | AES_InitStructure.AES_Operation = AES_Operation_KeyDerivAndDecryp; |
mbed_official | 76:aeb1df146756 | 417 | AES_InitStructure.AES_Chaining = AES_Chaining_CBC; |
mbed_official | 76:aeb1df146756 | 418 | AES_InitStructure.AES_DataType = AES_DataType_8b; |
mbed_official | 76:aeb1df146756 | 419 | AES_Init(&AES_InitStructure); |
mbed_official | 76:aeb1df146756 | 420 | |
mbed_official | 76:aeb1df146756 | 421 | /* Enable AES */ |
mbed_official | 76:aeb1df146756 | 422 | AES_Cmd(ENABLE); |
mbed_official | 76:aeb1df146756 | 423 | |
mbed_official | 76:aeb1df146756 | 424 | for(i = 0; ((i < Ilength) && (status != ERROR)); i += 16) |
mbed_official | 76:aeb1df146756 | 425 | { |
mbed_official | 76:aeb1df146756 | 426 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 427 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 428 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 429 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 430 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 431 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 432 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 433 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 434 | |
mbed_official | 76:aeb1df146756 | 435 | /* Wait for CCF flag to be set */ |
mbed_official | 76:aeb1df146756 | 436 | counter = 0; |
mbed_official | 76:aeb1df146756 | 437 | do |
mbed_official | 76:aeb1df146756 | 438 | { |
mbed_official | 76:aeb1df146756 | 439 | ccstatus = AES_GetFlagStatus(AES_FLAG_CCF); |
mbed_official | 76:aeb1df146756 | 440 | counter++; |
mbed_official | 76:aeb1df146756 | 441 | }while((counter != AES_CC_TIMEOUT) && (ccstatus == RESET)); |
mbed_official | 76:aeb1df146756 | 442 | |
mbed_official | 76:aeb1df146756 | 443 | if (ccstatus == RESET) |
mbed_official | 76:aeb1df146756 | 444 | { |
mbed_official | 76:aeb1df146756 | 445 | status = ERROR; |
mbed_official | 76:aeb1df146756 | 446 | } |
mbed_official | 76:aeb1df146756 | 447 | else |
mbed_official | 76:aeb1df146756 | 448 | { |
mbed_official | 76:aeb1df146756 | 449 | /* Clear CCF flag */ |
mbed_official | 76:aeb1df146756 | 450 | AES_ClearFlag(AES_FLAG_CCF); |
mbed_official | 76:aeb1df146756 | 451 | |
mbed_official | 76:aeb1df146756 | 452 | /* Read cipher text */ |
mbed_official | 76:aeb1df146756 | 453 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 454 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 455 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 456 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 457 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 458 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 459 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 460 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 461 | } |
mbed_official | 76:aeb1df146756 | 462 | } |
mbed_official | 76:aeb1df146756 | 463 | |
mbed_official | 76:aeb1df146756 | 464 | /* Disable AES before starting new processing */ |
mbed_official | 76:aeb1df146756 | 465 | AES_Cmd(DISABLE); |
mbed_official | 76:aeb1df146756 | 466 | |
mbed_official | 76:aeb1df146756 | 467 | return status; |
mbed_official | 76:aeb1df146756 | 468 | } |
mbed_official | 76:aeb1df146756 | 469 | |
mbed_official | 76:aeb1df146756 | 470 | /** |
mbed_official | 76:aeb1df146756 | 471 | * @brief Encrypt using AES in CTR Mode |
mbed_official | 76:aeb1df146756 | 472 | * @param InitVectors: Initialisation Vectors used for AES algorithm. |
mbed_official | 76:aeb1df146756 | 473 | * @param Key: Key used for AES algorithm. |
mbed_official | 76:aeb1df146756 | 474 | * @param Input: pointer to the Input buffer. |
mbed_official | 76:aeb1df146756 | 475 | * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes. |
mbed_official | 76:aeb1df146756 | 476 | * @param Output: pointer to the returned buffer. |
mbed_official | 76:aeb1df146756 | 477 | * @retval An ErrorStatus enumeration value: |
mbed_official | 76:aeb1df146756 | 478 | * - SUCCESS: Operation done |
mbed_official | 76:aeb1df146756 | 479 | * - ERROR: Operation failed |
mbed_official | 76:aeb1df146756 | 480 | */ |
mbed_official | 76:aeb1df146756 | 481 | ErrorStatus AES_CTR_Encrypt(uint8_t* Key, uint8_t InitVectors[16], uint8_t* Input, uint32_t Ilength, uint8_t* Output) |
mbed_official | 76:aeb1df146756 | 482 | { |
mbed_official | 76:aeb1df146756 | 483 | AES_InitTypeDef AES_InitStructure; |
mbed_official | 76:aeb1df146756 | 484 | AES_KeyInitTypeDef AES_KeyInitStructure; |
mbed_official | 76:aeb1df146756 | 485 | AES_IVInitTypeDef AES_IVInitStructure; |
mbed_official | 76:aeb1df146756 | 486 | |
mbed_official | 76:aeb1df146756 | 487 | ErrorStatus status = SUCCESS; |
mbed_official | 76:aeb1df146756 | 488 | uint32_t keyaddr = (uint32_t)Key; |
mbed_official | 76:aeb1df146756 | 489 | uint32_t inputaddr = (uint32_t)Input; |
mbed_official | 76:aeb1df146756 | 490 | uint32_t outputaddr = (uint32_t)Output; |
mbed_official | 76:aeb1df146756 | 491 | uint32_t ivaddr = (uint32_t)InitVectors; |
mbed_official | 76:aeb1df146756 | 492 | __IO uint32_t counter = 0; |
mbed_official | 76:aeb1df146756 | 493 | uint32_t ccstatus = 0; |
mbed_official | 76:aeb1df146756 | 494 | uint32_t i = 0; |
mbed_official | 76:aeb1df146756 | 495 | |
mbed_official | 76:aeb1df146756 | 496 | /* AES key initialisation*/ |
mbed_official | 76:aeb1df146756 | 497 | AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 498 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 499 | AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 500 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 501 | AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 502 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 503 | AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 504 | AES_KeyInit(&AES_KeyInitStructure); |
mbed_official | 76:aeb1df146756 | 505 | |
mbed_official | 76:aeb1df146756 | 506 | /* AES Initialization Vectors */ |
mbed_official | 76:aeb1df146756 | 507 | AES_IVInitStructure.AES_IV3 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 508 | ivaddr += 4; |
mbed_official | 76:aeb1df146756 | 509 | AES_IVInitStructure.AES_IV2= __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 510 | ivaddr += 4; |
mbed_official | 76:aeb1df146756 | 511 | AES_IVInitStructure.AES_IV1 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 512 | ivaddr += 4; |
mbed_official | 76:aeb1df146756 | 513 | AES_IVInitStructure.AES_IV0= __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 514 | AES_IVInit(&AES_IVInitStructure); |
mbed_official | 76:aeb1df146756 | 515 | |
mbed_official | 76:aeb1df146756 | 516 | /* AES configuration */ |
mbed_official | 76:aeb1df146756 | 517 | AES_InitStructure.AES_Operation = AES_Operation_Encryp; |
mbed_official | 76:aeb1df146756 | 518 | AES_InitStructure.AES_Chaining = AES_Chaining_CTR; |
mbed_official | 76:aeb1df146756 | 519 | AES_InitStructure.AES_DataType = AES_DataType_8b; |
mbed_official | 76:aeb1df146756 | 520 | AES_Init(&AES_InitStructure); |
mbed_official | 76:aeb1df146756 | 521 | |
mbed_official | 76:aeb1df146756 | 522 | /* Enable AES */ |
mbed_official | 76:aeb1df146756 | 523 | AES_Cmd(ENABLE); |
mbed_official | 76:aeb1df146756 | 524 | |
mbed_official | 76:aeb1df146756 | 525 | for(i = 0; ((i < Ilength) && (status != ERROR)); i += 16) |
mbed_official | 76:aeb1df146756 | 526 | { |
mbed_official | 76:aeb1df146756 | 527 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 528 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 529 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 530 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 531 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 532 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 533 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 534 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 535 | |
mbed_official | 76:aeb1df146756 | 536 | /* Wait for CCF flag to be set */ |
mbed_official | 76:aeb1df146756 | 537 | counter = 0; |
mbed_official | 76:aeb1df146756 | 538 | do |
mbed_official | 76:aeb1df146756 | 539 | { |
mbed_official | 76:aeb1df146756 | 540 | ccstatus = AES_GetFlagStatus(AES_FLAG_CCF); |
mbed_official | 76:aeb1df146756 | 541 | counter++; |
mbed_official | 76:aeb1df146756 | 542 | }while((counter != AES_CC_TIMEOUT) && (ccstatus == RESET)); |
mbed_official | 76:aeb1df146756 | 543 | |
mbed_official | 76:aeb1df146756 | 544 | if (ccstatus == RESET) |
mbed_official | 76:aeb1df146756 | 545 | { |
mbed_official | 76:aeb1df146756 | 546 | status = ERROR; |
mbed_official | 76:aeb1df146756 | 547 | } |
mbed_official | 76:aeb1df146756 | 548 | else |
mbed_official | 76:aeb1df146756 | 549 | { |
mbed_official | 76:aeb1df146756 | 550 | /* Clear CCF flag */ |
mbed_official | 76:aeb1df146756 | 551 | AES_ClearFlag(AES_FLAG_CCF); |
mbed_official | 76:aeb1df146756 | 552 | |
mbed_official | 76:aeb1df146756 | 553 | /* Read cipher text */ |
mbed_official | 76:aeb1df146756 | 554 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 555 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 556 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 557 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 558 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 559 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 560 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 561 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 562 | } |
mbed_official | 76:aeb1df146756 | 563 | } |
mbed_official | 76:aeb1df146756 | 564 | |
mbed_official | 76:aeb1df146756 | 565 | /* Disable AES before starting new processing */ |
mbed_official | 76:aeb1df146756 | 566 | AES_Cmd(DISABLE); |
mbed_official | 76:aeb1df146756 | 567 | |
mbed_official | 76:aeb1df146756 | 568 | return status; |
mbed_official | 76:aeb1df146756 | 569 | } |
mbed_official | 76:aeb1df146756 | 570 | |
mbed_official | 76:aeb1df146756 | 571 | /** |
mbed_official | 76:aeb1df146756 | 572 | * @brief Decrypt using AES in CTR Mode |
mbed_official | 76:aeb1df146756 | 573 | * @param InitVectors: Initialisation Vectors used for AES algorithm. |
mbed_official | 76:aeb1df146756 | 574 | * @param Key: Key used for AES algorithm. |
mbed_official | 76:aeb1df146756 | 575 | * @param Input: pointer to the Input buffer. |
mbed_official | 76:aeb1df146756 | 576 | * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes. |
mbed_official | 76:aeb1df146756 | 577 | * @param Output: pointer to the returned buffer. |
mbed_official | 76:aeb1df146756 | 578 | * @retval An ErrorStatus enumeration value: |
mbed_official | 76:aeb1df146756 | 579 | * - SUCCESS: Operation done |
mbed_official | 76:aeb1df146756 | 580 | * - ERROR: Operation failed |
mbed_official | 76:aeb1df146756 | 581 | */ |
mbed_official | 76:aeb1df146756 | 582 | ErrorStatus AES_CTR_Decrypt(uint8_t* Key, uint8_t InitVectors[16], uint8_t* Input, uint32_t Ilength, uint8_t* Output) |
mbed_official | 76:aeb1df146756 | 583 | { |
mbed_official | 76:aeb1df146756 | 584 | AES_InitTypeDef AES_InitStructure; |
mbed_official | 76:aeb1df146756 | 585 | AES_KeyInitTypeDef AES_KeyInitStructure; |
mbed_official | 76:aeb1df146756 | 586 | AES_IVInitTypeDef AES_IVInitStructure; |
mbed_official | 76:aeb1df146756 | 587 | |
mbed_official | 76:aeb1df146756 | 588 | ErrorStatus status = SUCCESS; |
mbed_official | 76:aeb1df146756 | 589 | uint32_t keyaddr = (uint32_t)Key; |
mbed_official | 76:aeb1df146756 | 590 | uint32_t inputaddr = (uint32_t)Input; |
mbed_official | 76:aeb1df146756 | 591 | uint32_t outputaddr = (uint32_t)Output; |
mbed_official | 76:aeb1df146756 | 592 | uint32_t ivaddr = (uint32_t)InitVectors; |
mbed_official | 76:aeb1df146756 | 593 | __IO uint32_t counter = 0; |
mbed_official | 76:aeb1df146756 | 594 | uint32_t ccstatus = 0; |
mbed_official | 76:aeb1df146756 | 595 | uint32_t i = 0; |
mbed_official | 76:aeb1df146756 | 596 | |
mbed_official | 76:aeb1df146756 | 597 | /* AES Key initialisation*/ |
mbed_official | 76:aeb1df146756 | 598 | AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 599 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 600 | AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 601 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 602 | AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 603 | keyaddr += 4; |
mbed_official | 76:aeb1df146756 | 604 | AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 76:aeb1df146756 | 605 | AES_KeyInit(&AES_KeyInitStructure); |
mbed_official | 76:aeb1df146756 | 606 | |
mbed_official | 76:aeb1df146756 | 607 | /* AES Initialization Vectors */ |
mbed_official | 76:aeb1df146756 | 608 | AES_IVInitStructure.AES_IV3 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 609 | ivaddr += 4; |
mbed_official | 76:aeb1df146756 | 610 | AES_IVInitStructure.AES_IV2 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 611 | ivaddr += 4; |
mbed_official | 76:aeb1df146756 | 612 | AES_IVInitStructure.AES_IV1 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 613 | ivaddr += 4; |
mbed_official | 76:aeb1df146756 | 614 | AES_IVInitStructure.AES_IV0 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 76:aeb1df146756 | 615 | AES_IVInit(&AES_IVInitStructure); |
mbed_official | 76:aeb1df146756 | 616 | |
mbed_official | 76:aeb1df146756 | 617 | /* AES configuration */ |
mbed_official | 76:aeb1df146756 | 618 | AES_InitStructure.AES_Operation = AES_Operation_KeyDerivAndDecryp; |
mbed_official | 76:aeb1df146756 | 619 | AES_InitStructure.AES_Chaining = AES_Chaining_CTR; |
mbed_official | 76:aeb1df146756 | 620 | AES_InitStructure.AES_DataType = AES_DataType_8b; |
mbed_official | 76:aeb1df146756 | 621 | AES_Init(&AES_InitStructure); |
mbed_official | 76:aeb1df146756 | 622 | |
mbed_official | 76:aeb1df146756 | 623 | /* Enable AES */ |
mbed_official | 76:aeb1df146756 | 624 | AES_Cmd(ENABLE); |
mbed_official | 76:aeb1df146756 | 625 | |
mbed_official | 76:aeb1df146756 | 626 | for(i = 0; ((i < Ilength) && (status != ERROR)); i += 16) |
mbed_official | 76:aeb1df146756 | 627 | { |
mbed_official | 76:aeb1df146756 | 628 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 629 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 630 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 631 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 632 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 633 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 634 | AES_WriteSubData(*(uint32_t*)(inputaddr)); |
mbed_official | 76:aeb1df146756 | 635 | inputaddr += 4; |
mbed_official | 76:aeb1df146756 | 636 | |
mbed_official | 76:aeb1df146756 | 637 | /* Wait for CCF flag to be set */ |
mbed_official | 76:aeb1df146756 | 638 | counter = 0; |
mbed_official | 76:aeb1df146756 | 639 | do |
mbed_official | 76:aeb1df146756 | 640 | { |
mbed_official | 76:aeb1df146756 | 641 | ccstatus = AES_GetFlagStatus(AES_FLAG_CCF); |
mbed_official | 76:aeb1df146756 | 642 | counter++; |
mbed_official | 76:aeb1df146756 | 643 | }while((counter != AES_CC_TIMEOUT) && (ccstatus == RESET)); |
mbed_official | 76:aeb1df146756 | 644 | |
mbed_official | 76:aeb1df146756 | 645 | if (ccstatus == RESET) |
mbed_official | 76:aeb1df146756 | 646 | { |
mbed_official | 76:aeb1df146756 | 647 | status = ERROR; |
mbed_official | 76:aeb1df146756 | 648 | } |
mbed_official | 76:aeb1df146756 | 649 | else |
mbed_official | 76:aeb1df146756 | 650 | { |
mbed_official | 76:aeb1df146756 | 651 | /* Clear CCF flag */ |
mbed_official | 76:aeb1df146756 | 652 | AES_ClearFlag(AES_FLAG_CCF); |
mbed_official | 76:aeb1df146756 | 653 | |
mbed_official | 76:aeb1df146756 | 654 | /* Read cipher text */ |
mbed_official | 76:aeb1df146756 | 655 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 656 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 657 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 658 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 659 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 660 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 661 | *(uint32_t*)(outputaddr) = AES_ReadSubData(); |
mbed_official | 76:aeb1df146756 | 662 | outputaddr += 4; |
mbed_official | 76:aeb1df146756 | 663 | } |
mbed_official | 76:aeb1df146756 | 664 | } |
mbed_official | 76:aeb1df146756 | 665 | |
mbed_official | 76:aeb1df146756 | 666 | /* Disable AES before starting new processing */ |
mbed_official | 76:aeb1df146756 | 667 | AES_Cmd(DISABLE); |
mbed_official | 76:aeb1df146756 | 668 | |
mbed_official | 76:aeb1df146756 | 669 | return status; |
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 | */ |
mbed_official | 76:aeb1df146756 | 679 | |
mbed_official | 76:aeb1df146756 | 680 | /** |
mbed_official | 76:aeb1df146756 | 681 | * @} |
mbed_official | 76:aeb1df146756 | 682 | */ |
mbed_official | 76:aeb1df146756 | 683 | |
mbed_official | 76:aeb1df146756 | 684 | /** |
mbed_official | 76:aeb1df146756 | 685 | * @} |
mbed_official | 76:aeb1df146756 | 686 | */ |
mbed_official | 76:aeb1df146756 | 687 | |
mbed_official | 76:aeb1df146756 | 688 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
mbed_official | 76:aeb1df146756 | 689 |