mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Wed Jul 01 09:45:11 2015 +0100
Revision:
579:53297373a894
Parent:
394:83f921546702
Synchronized with git revision d5b4d2ab9c47edb4dc5776e7177b0c2263459081

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

Initial version of drivers for SAMR21

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 354:e67efb2aab0e 1 /**
mbed_official 354:e67efb2aab0e 2 ******************************************************************************
mbed_official 354:e67efb2aab0e 3 * @file stm32l1xx_hal_cryp.c
mbed_official 354:e67efb2aab0e 4 * @author MCD Application Team
mbed_official 354:e67efb2aab0e 5 * @version V1.0.0
mbed_official 354:e67efb2aab0e 6 * @date 5-September-2014
mbed_official 354:e67efb2aab0e 7 * @brief CRYP HAL module driver.
mbed_official 354:e67efb2aab0e 8 *
mbed_official 354:e67efb2aab0e 9 * This file provides firmware functions to manage the following
mbed_official 354:e67efb2aab0e 10 * functionalities of the Cryptography (CRYP) peripheral:
mbed_official 354:e67efb2aab0e 11 * + Initialization and de-initialization functions
mbed_official 354:e67efb2aab0e 12 * + Processing functions by algorithm using polling mode
mbed_official 354:e67efb2aab0e 13 * + Processing functions by algorithm using interrupt mode
mbed_official 354:e67efb2aab0e 14 * + Processing functions by algorithm using DMA mode
mbed_official 354:e67efb2aab0e 15 * + Peripheral State functions
mbed_official 354:e67efb2aab0e 16 *
mbed_official 354:e67efb2aab0e 17 @verbatim
mbed_official 354:e67efb2aab0e 18 ==============================================================================
mbed_official 354:e67efb2aab0e 19 ##### How to use this driver #####
mbed_official 354:e67efb2aab0e 20 ==============================================================================
mbed_official 354:e67efb2aab0e 21 [..]
mbed_official 354:e67efb2aab0e 22 The CRYP HAL driver can be used as follows:
mbed_official 354:e67efb2aab0e 23
mbed_official 354:e67efb2aab0e 24 (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
mbed_official 354:e67efb2aab0e 25 (##) Enable the CRYP interface clock using __CRYP_CLK_ENABLE()
mbed_official 354:e67efb2aab0e 26 (##) In case of using interrupts (e.g. HAL_CRYP_AESECB_Encrypt_IT())
mbed_official 354:e67efb2aab0e 27 (+) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
mbed_official 354:e67efb2aab0e 28 (+) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
mbed_official 354:e67efb2aab0e 29 (+) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
mbed_official 354:e67efb2aab0e 30 (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_AESECB_Encrypt_DMA())
mbed_official 354:e67efb2aab0e 31 (+) Enable the DMA2 interface clock using
mbed_official 354:e67efb2aab0e 32 (++) __DMA2_CLK_ENABLE()
mbed_official 354:e67efb2aab0e 33 (+) Configure and enable two DMA Channels one for managing data transfer from
mbed_official 354:e67efb2aab0e 34 memory to peripheral (input channel) and another channel for managing data
mbed_official 354:e67efb2aab0e 35 transfer from peripheral to memory (output channel)
mbed_official 354:e67efb2aab0e 36 (+) Associate the initialized DMA handle to the CRYP DMA handle
mbed_official 354:e67efb2aab0e 37 using __HAL_LINKDMA()
mbed_official 354:e67efb2aab0e 38 (+) Configure the priority and enable the NVIC for the transfer complete
mbed_official 354:e67efb2aab0e 39 interrupt on the two DMA Streams. The output stream should have higher
mbed_official 354:e67efb2aab0e 40 priority than the input stream.
mbed_official 354:e67efb2aab0e 41 (++) HAL_NVIC_SetPriority()
mbed_official 354:e67efb2aab0e 42 (++) HAL_NVIC_EnableIRQ()
mbed_official 354:e67efb2aab0e 43
mbed_official 354:e67efb2aab0e 44 (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly:
mbed_official 354:e67efb2aab0e 45 (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit
mbed_official 354:e67efb2aab0e 46 (##) The encryption/decryption key.
mbed_official 354:e67efb2aab0e 47 (##) The initialization vector (counter). It is not used ECB mode.
mbed_official 354:e67efb2aab0e 48
mbed_official 354:e67efb2aab0e 49 (#)Three processing (encryption/decryption) functions are available:
mbed_official 354:e67efb2aab0e 50 (##) Polling mode: encryption and decryption APIs are blocking functions
mbed_official 354:e67efb2aab0e 51 i.e. they process the data and wait till the processing is finished
mbed_official 354:e67efb2aab0e 52 e.g. HAL_CRYP_AESCBC_Encrypt()
mbed_official 354:e67efb2aab0e 53 (##) Interrupt mode: encryption and decryption APIs are not blocking functions
mbed_official 354:e67efb2aab0e 54 i.e. they process the data under interrupt
mbed_official 354:e67efb2aab0e 55 e.g. HAL_CRYP_AESCBC_Encrypt_IT()
mbed_official 354:e67efb2aab0e 56 (##) DMA mode: encryption and decryption APIs are not blocking functions
mbed_official 354:e67efb2aab0e 57 i.e. the data transfer is ensured by DMA
mbed_official 354:e67efb2aab0e 58 e.g. HAL_CRYP_AESCBC_Encrypt_DMA()
mbed_official 354:e67efb2aab0e 59
mbed_official 354:e67efb2aab0e 60 (#)When the processing function is called for the first time after HAL_CRYP_Init()
mbed_official 354:e67efb2aab0e 61 the CRYP peripheral is initialized and processes the buffer in input.
mbed_official 354:e67efb2aab0e 62 At second call, the processing function performs an append of the already
mbed_official 354:e67efb2aab0e 63 processed buffer.
mbed_official 354:e67efb2aab0e 64 When a new data block is to be processed, call HAL_CRYP_Init() then the
mbed_official 354:e67efb2aab0e 65 processing function.
mbed_official 354:e67efb2aab0e 66
mbed_official 354:e67efb2aab0e 67 (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
mbed_official 354:e67efb2aab0e 68
mbed_official 354:e67efb2aab0e 69 @endverbatim
mbed_official 354:e67efb2aab0e 70 ******************************************************************************
mbed_official 354:e67efb2aab0e 71 * @attention
mbed_official 354:e67efb2aab0e 72 *
mbed_official 354:e67efb2aab0e 73 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 354:e67efb2aab0e 74 *
mbed_official 354:e67efb2aab0e 75 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 354:e67efb2aab0e 76 * are permitted provided that the following conditions are met:
mbed_official 354:e67efb2aab0e 77 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 354:e67efb2aab0e 78 * this list of conditions and the following disclaimer.
mbed_official 354:e67efb2aab0e 79 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 354:e67efb2aab0e 80 * this list of conditions and the following disclaimer in the documentation
mbed_official 354:e67efb2aab0e 81 * and/or other materials provided with the distribution.
mbed_official 354:e67efb2aab0e 82 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 354:e67efb2aab0e 83 * may be used to endorse or promote products derived from this software
mbed_official 354:e67efb2aab0e 84 * without specific prior written permission.
mbed_official 354:e67efb2aab0e 85 *
mbed_official 354:e67efb2aab0e 86 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 354:e67efb2aab0e 87 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 354:e67efb2aab0e 88 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 354:e67efb2aab0e 89 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 354:e67efb2aab0e 90 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 354:e67efb2aab0e 91 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 354:e67efb2aab0e 92 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 354:e67efb2aab0e 93 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 354:e67efb2aab0e 94 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 354:e67efb2aab0e 95 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 354:e67efb2aab0e 96 *
mbed_official 354:e67efb2aab0e 97 ******************************************************************************
mbed_official 354:e67efb2aab0e 98 */
mbed_official 354:e67efb2aab0e 99
mbed_official 354:e67efb2aab0e 100 /* Includes ------------------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 101 #include "stm32l1xx_hal.h"
mbed_official 354:e67efb2aab0e 102
mbed_official 354:e67efb2aab0e 103 #ifdef HAL_CRYP_MODULE_ENABLED
mbed_official 354:e67efb2aab0e 104
mbed_official 354:e67efb2aab0e 105 /** @addtogroup STM32L1xx_HAL_Driver
mbed_official 354:e67efb2aab0e 106 * @{
mbed_official 354:e67efb2aab0e 107 */
mbed_official 354:e67efb2aab0e 108
mbed_official 354:e67efb2aab0e 109 /** @defgroup CRYP CRYP
mbed_official 354:e67efb2aab0e 110 * @brief CRYP HAL module driver.
mbed_official 354:e67efb2aab0e 111 * @{
mbed_official 354:e67efb2aab0e 112 */
mbed_official 354:e67efb2aab0e 113
mbed_official 354:e67efb2aab0e 114 #if defined(STM32L162xC) || defined(STM32L162xCA) || defined(STM32L162xD) || defined(STM32L162xE)
mbed_official 354:e67efb2aab0e 115
mbed_official 354:e67efb2aab0e 116 /* Private typedef -----------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 117 /* Private define ------------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 118
mbed_official 354:e67efb2aab0e 119 /** @defgroup CRYP_Private_Defines CRYP Private Defines
mbed_official 354:e67efb2aab0e 120 * @{
mbed_official 354:e67efb2aab0e 121 */
mbed_official 354:e67efb2aab0e 122
mbed_official 354:e67efb2aab0e 123 #define CRYP_ALGO_CHAIN_MASK (AES_CR_MODE | AES_CR_CHMOD)
mbed_official 354:e67efb2aab0e 124
mbed_official 354:e67efb2aab0e 125 /**
mbed_official 354:e67efb2aab0e 126 * @}
mbed_official 354:e67efb2aab0e 127 */
mbed_official 354:e67efb2aab0e 128
mbed_official 354:e67efb2aab0e 129 /* Private macro -------------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 130 /* Private variables ---------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 131 /* Private function prototypes -----------------------------------------------*/
mbed_official 354:e67efb2aab0e 132
mbed_official 354:e67efb2aab0e 133 /** @defgroup CRYP_Private_Functions CRYP Private Functions
mbed_official 354:e67efb2aab0e 134 * @{
mbed_official 354:e67efb2aab0e 135 */
mbed_official 354:e67efb2aab0e 136
mbed_official 354:e67efb2aab0e 137 static HAL_StatusTypeDef CRYP_EncryptDecrypt_IT(CRYP_HandleTypeDef *hcryp);
mbed_official 354:e67efb2aab0e 138 static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector);
mbed_official 354:e67efb2aab0e 139 static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key);
mbed_official 354:e67efb2aab0e 140 static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
mbed_official 354:e67efb2aab0e 141 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
mbed_official 354:e67efb2aab0e 142 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
mbed_official 354:e67efb2aab0e 143 static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
mbed_official 354:e67efb2aab0e 144 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
mbed_official 354:e67efb2aab0e 145
mbed_official 354:e67efb2aab0e 146 /**
mbed_official 354:e67efb2aab0e 147 * @}
mbed_official 354:e67efb2aab0e 148 */
mbed_official 354:e67efb2aab0e 149
mbed_official 354:e67efb2aab0e 150 /* Private functions ---------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 151
mbed_official 354:e67efb2aab0e 152 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
mbed_official 354:e67efb2aab0e 153 * @{
mbed_official 354:e67efb2aab0e 154 */
mbed_official 354:e67efb2aab0e 155
mbed_official 354:e67efb2aab0e 156 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
mbed_official 354:e67efb2aab0e 157 * @brief Initialization and Configuration functions.
mbed_official 354:e67efb2aab0e 158 *
mbed_official 354:e67efb2aab0e 159 @verbatim
mbed_official 354:e67efb2aab0e 160 ==============================================================================
mbed_official 354:e67efb2aab0e 161 ##### Initialization and de-initialization functions #####
mbed_official 354:e67efb2aab0e 162 ==============================================================================
mbed_official 354:e67efb2aab0e 163 [..] This section provides functions allowing to:
mbed_official 354:e67efb2aab0e 164 (+) Initialize the CRYP according to the specified parameters
mbed_official 354:e67efb2aab0e 165 in the CRYP_InitTypeDef and creates the associated handle
mbed_official 354:e67efb2aab0e 166 (+) DeInitialize the CRYP peripheral
mbed_official 354:e67efb2aab0e 167 (+) Initialize the CRYP MSP
mbed_official 354:e67efb2aab0e 168 (+) DeInitialize CRYP MSP
mbed_official 354:e67efb2aab0e 169
mbed_official 354:e67efb2aab0e 170 @endverbatim
mbed_official 354:e67efb2aab0e 171 * @{
mbed_official 354:e67efb2aab0e 172 */
mbed_official 354:e67efb2aab0e 173
mbed_official 354:e67efb2aab0e 174 /**
mbed_official 354:e67efb2aab0e 175 * @brief Initializes the CRYP according to the specified
mbed_official 354:e67efb2aab0e 176 * parameters in the CRYP_InitTypeDef and creates the associated handle.
mbed_official 354:e67efb2aab0e 177 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 178 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 179 * @retval HAL status
mbed_official 354:e67efb2aab0e 180 */
mbed_official 354:e67efb2aab0e 181 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
mbed_official 354:e67efb2aab0e 182 {
mbed_official 354:e67efb2aab0e 183 /* Check the CRYP handle allocation */
mbed_official 354:e67efb2aab0e 184 if(hcryp == HAL_NULL)
mbed_official 354:e67efb2aab0e 185 {
mbed_official 354:e67efb2aab0e 186 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 187 }
mbed_official 354:e67efb2aab0e 188
mbed_official 354:e67efb2aab0e 189 /* Check the parameters */
mbed_official 354:e67efb2aab0e 190 assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
mbed_official 354:e67efb2aab0e 191
mbed_official 354:e67efb2aab0e 192 if(hcryp->State == HAL_CRYP_STATE_RESET)
mbed_official 354:e67efb2aab0e 193 {
mbed_official 354:e67efb2aab0e 194 /* Init the low level hardware */
mbed_official 354:e67efb2aab0e 195 HAL_CRYP_MspInit(hcryp);
mbed_official 354:e67efb2aab0e 196 }
mbed_official 354:e67efb2aab0e 197
mbed_official 354:e67efb2aab0e 198 /* Check if AES already enabled */
mbed_official 354:e67efb2aab0e 199 if (HAL_IS_BIT_CLR(AES->CR, AES_CR_EN))
mbed_official 354:e67efb2aab0e 200 {
mbed_official 354:e67efb2aab0e 201 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 202 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 203
mbed_official 354:e67efb2aab0e 204 /* Set the data type*/
mbed_official 354:e67efb2aab0e 205 MODIFY_REG(AES->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
mbed_official 354:e67efb2aab0e 206
mbed_official 354:e67efb2aab0e 207 /* Reset CrypInCount and CrypOutCount */
mbed_official 354:e67efb2aab0e 208 hcryp->CrypInCount = 0;
mbed_official 354:e67efb2aab0e 209 hcryp->CrypOutCount = 0;
mbed_official 354:e67efb2aab0e 210
mbed_official 354:e67efb2aab0e 211 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 212 hcryp->State = HAL_CRYP_STATE_READY;
mbed_official 354:e67efb2aab0e 213
mbed_official 354:e67efb2aab0e 214 /* Set the default CRYP phase */
mbed_official 354:e67efb2aab0e 215 hcryp->Phase = HAL_CRYP_PHASE_READY;
mbed_official 354:e67efb2aab0e 216
mbed_official 354:e67efb2aab0e 217 /* Return function status */
mbed_official 354:e67efb2aab0e 218 return HAL_OK;
mbed_official 354:e67efb2aab0e 219 }
mbed_official 354:e67efb2aab0e 220 else
mbed_official 354:e67efb2aab0e 221 {
mbed_official 354:e67efb2aab0e 222 /* The Datatype selection must be changed if the AES is disabled. Writing these bits while the AES is */
mbed_official 354:e67efb2aab0e 223 /* enabled is forbidden to avoid unpredictable AES behavior.*/
mbed_official 354:e67efb2aab0e 224
mbed_official 354:e67efb2aab0e 225 /* Return function status */
mbed_official 354:e67efb2aab0e 226 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 227 }
mbed_official 354:e67efb2aab0e 228
mbed_official 354:e67efb2aab0e 229 }
mbed_official 354:e67efb2aab0e 230
mbed_official 354:e67efb2aab0e 231 /**
mbed_official 354:e67efb2aab0e 232 * @brief DeInitializes the CRYP peripheral.
mbed_official 354:e67efb2aab0e 233 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 234 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 235 * @retval HAL status
mbed_official 354:e67efb2aab0e 236 */
mbed_official 354:e67efb2aab0e 237 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
mbed_official 354:e67efb2aab0e 238 {
mbed_official 354:e67efb2aab0e 239 /* Check the CRYP handle allocation */
mbed_official 354:e67efb2aab0e 240 if(hcryp == HAL_NULL)
mbed_official 354:e67efb2aab0e 241 {
mbed_official 354:e67efb2aab0e 242 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 243 }
mbed_official 354:e67efb2aab0e 244
mbed_official 354:e67efb2aab0e 245 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 246 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 247
mbed_official 354:e67efb2aab0e 248 /* Set the default CRYP phase */
mbed_official 354:e67efb2aab0e 249 hcryp->Phase = HAL_CRYP_PHASE_READY;
mbed_official 354:e67efb2aab0e 250
mbed_official 354:e67efb2aab0e 251 /* Reset CrypInCount and CrypOutCount */
mbed_official 354:e67efb2aab0e 252 hcryp->CrypInCount = 0;
mbed_official 354:e67efb2aab0e 253 hcryp->CrypOutCount = 0;
mbed_official 354:e67efb2aab0e 254
mbed_official 354:e67efb2aab0e 255 /* Disable the CRYP Peripheral Clock */
mbed_official 354:e67efb2aab0e 256 __HAL_CRYP_DISABLE();
mbed_official 354:e67efb2aab0e 257
mbed_official 354:e67efb2aab0e 258 /* DeInit the low level hardware: CLOCK, NVIC.*/
mbed_official 354:e67efb2aab0e 259 HAL_CRYP_MspDeInit(hcryp);
mbed_official 354:e67efb2aab0e 260
mbed_official 354:e67efb2aab0e 261 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 262 hcryp->State = HAL_CRYP_STATE_RESET;
mbed_official 354:e67efb2aab0e 263
mbed_official 354:e67efb2aab0e 264 /* Release Lock */
mbed_official 354:e67efb2aab0e 265 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 266
mbed_official 354:e67efb2aab0e 267 /* Return function status */
mbed_official 354:e67efb2aab0e 268 return HAL_OK;
mbed_official 354:e67efb2aab0e 269 }
mbed_official 354:e67efb2aab0e 270
mbed_official 354:e67efb2aab0e 271 /**
mbed_official 354:e67efb2aab0e 272 * @brief Initializes the CRYP MSP.
mbed_official 354:e67efb2aab0e 273 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 274 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 275 * @retval None
mbed_official 354:e67efb2aab0e 276 */
mbed_official 354:e67efb2aab0e 277 __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
mbed_official 354:e67efb2aab0e 278 {
mbed_official 354:e67efb2aab0e 279 /* NOTE : This function should not be modified; when the callback is needed,
mbed_official 354:e67efb2aab0e 280 the HAL_CRYP_MspInit can be implemented in the user file */
mbed_official 354:e67efb2aab0e 281 }
mbed_official 354:e67efb2aab0e 282
mbed_official 354:e67efb2aab0e 283 /**
mbed_official 354:e67efb2aab0e 284 * @brief DeInitializes CRYP MSP.
mbed_official 354:e67efb2aab0e 285 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 286 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 287 * @retval None
mbed_official 354:e67efb2aab0e 288 */
mbed_official 354:e67efb2aab0e 289 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
mbed_official 354:e67efb2aab0e 290 {
mbed_official 354:e67efb2aab0e 291 /* NOTE : This function should not be modified; when the callback is needed,
mbed_official 354:e67efb2aab0e 292 the HAL_CRYP_MspDeInit can be implemented in the user file */
mbed_official 354:e67efb2aab0e 293 }
mbed_official 354:e67efb2aab0e 294
mbed_official 354:e67efb2aab0e 295 /**
mbed_official 354:e67efb2aab0e 296 * @}
mbed_official 354:e67efb2aab0e 297 */
mbed_official 354:e67efb2aab0e 298
mbed_official 354:e67efb2aab0e 299 /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
mbed_official 354:e67efb2aab0e 300 * @brief processing functions.
mbed_official 354:e67efb2aab0e 301 *
mbed_official 354:e67efb2aab0e 302 @verbatim
mbed_official 354:e67efb2aab0e 303 ==============================================================================
mbed_official 354:e67efb2aab0e 304 ##### AES processing functions #####
mbed_official 354:e67efb2aab0e 305 ==============================================================================
mbed_official 354:e67efb2aab0e 306 [..] This section provides functions allowing to:
mbed_official 354:e67efb2aab0e 307 (+) Encrypt plaintext using AES algorithm in different chaining modes
mbed_official 354:e67efb2aab0e 308 (+) Decrypt cyphertext using AES algorithm in different chaining modes
mbed_official 354:e67efb2aab0e 309 [..] Three processing functions are available:
mbed_official 354:e67efb2aab0e 310 (+) Polling mode
mbed_official 354:e67efb2aab0e 311 (+) Interrupt mode
mbed_official 354:e67efb2aab0e 312 (+) DMA mode
mbed_official 354:e67efb2aab0e 313
mbed_official 354:e67efb2aab0e 314 @endverbatim
mbed_official 354:e67efb2aab0e 315 * @{
mbed_official 354:e67efb2aab0e 316 */
mbed_official 354:e67efb2aab0e 317
mbed_official 354:e67efb2aab0e 318 /**
mbed_official 354:e67efb2aab0e 319 * @brief Initializes the CRYP peripheral in AES ECB encryption mode
mbed_official 354:e67efb2aab0e 320 * then encrypt pPlainData. The cypher data are available in pCypherData
mbed_official 354:e67efb2aab0e 321 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 322 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 323 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 324 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
mbed_official 354:e67efb2aab0e 325 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 326 * @param Timeout: Specify Timeout value
mbed_official 354:e67efb2aab0e 327 * @retval HAL status
mbed_official 354:e67efb2aab0e 328 */
mbed_official 354:e67efb2aab0e 329 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
mbed_official 354:e67efb2aab0e 330 {
mbed_official 354:e67efb2aab0e 331 /* Process Locked */
mbed_official 354:e67efb2aab0e 332 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 333
mbed_official 354:e67efb2aab0e 334 /* Check that data aligned on u32 and Size multiple of 16*/
mbed_official 354:e67efb2aab0e 335 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 336 {
mbed_official 354:e67efb2aab0e 337 /* Process Locked */
mbed_official 354:e67efb2aab0e 338 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 339
mbed_official 354:e67efb2aab0e 340 /* Return function status */
mbed_official 354:e67efb2aab0e 341 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 342 }
mbed_official 354:e67efb2aab0e 343
mbed_official 354:e67efb2aab0e 344 /* Check if HAL_CRYP_Init has been called */
mbed_official 354:e67efb2aab0e 345 if(hcryp->State != HAL_CRYP_STATE_RESET)
mbed_official 354:e67efb2aab0e 346 {
mbed_official 354:e67efb2aab0e 347 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 348 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 349
mbed_official 354:e67efb2aab0e 350 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 351 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 352 {
mbed_official 354:e67efb2aab0e 353 /* Set the key */
mbed_official 354:e67efb2aab0e 354 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 355
mbed_official 354:e67efb2aab0e 356 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 357 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 358
mbed_official 354:e67efb2aab0e 359 /* Set the CRYP peripheral in AES ECB mode */
mbed_official 354:e67efb2aab0e 360 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT);
mbed_official 354:e67efb2aab0e 361
mbed_official 354:e67efb2aab0e 362 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 363 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 364
mbed_official 354:e67efb2aab0e 365 /* Set the phase */
mbed_official 354:e67efb2aab0e 366 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 367 }
mbed_official 354:e67efb2aab0e 368
mbed_official 354:e67efb2aab0e 369 /* Write Plain Data and Get Cypher Data */
mbed_official 354:e67efb2aab0e 370 if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
mbed_official 354:e67efb2aab0e 371 {
mbed_official 354:e67efb2aab0e 372 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 373 }
mbed_official 354:e67efb2aab0e 374
mbed_official 354:e67efb2aab0e 375 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 376 hcryp->State = HAL_CRYP_STATE_READY;
mbed_official 354:e67efb2aab0e 377
mbed_official 354:e67efb2aab0e 378 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 379 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 380
mbed_official 354:e67efb2aab0e 381 /* Return function status */
mbed_official 354:e67efb2aab0e 382 return HAL_OK;
mbed_official 354:e67efb2aab0e 383 }
mbed_official 354:e67efb2aab0e 384 else
mbed_official 354:e67efb2aab0e 385 {
mbed_official 354:e67efb2aab0e 386 /* Return function status */
mbed_official 354:e67efb2aab0e 387 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 388 }
mbed_official 354:e67efb2aab0e 389 }
mbed_official 354:e67efb2aab0e 390
mbed_official 354:e67efb2aab0e 391 /**
mbed_official 354:e67efb2aab0e 392 * @brief Initializes the CRYP peripheral in AES CBC encryption mode
mbed_official 354:e67efb2aab0e 393 * then encrypt pPlainData. The cypher data are available in pCypherData
mbed_official 354:e67efb2aab0e 394 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 395 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 396 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 397 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
mbed_official 354:e67efb2aab0e 398 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 399 * @param Timeout: Specify Timeout value
mbed_official 354:e67efb2aab0e 400 * @retval HAL status
mbed_official 354:e67efb2aab0e 401 */
mbed_official 354:e67efb2aab0e 402 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
mbed_official 354:e67efb2aab0e 403 {
mbed_official 354:e67efb2aab0e 404 /* Process Locked */
mbed_official 354:e67efb2aab0e 405 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 406
mbed_official 354:e67efb2aab0e 407 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 408 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 409 {
mbed_official 354:e67efb2aab0e 410 /* Process Locked */
mbed_official 354:e67efb2aab0e 411 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 412
mbed_official 354:e67efb2aab0e 413 /* Return function status */
mbed_official 354:e67efb2aab0e 414 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 415 }
mbed_official 354:e67efb2aab0e 416
mbed_official 354:e67efb2aab0e 417 /* Check if HAL_CRYP_Init has been called */
mbed_official 354:e67efb2aab0e 418 if(hcryp->State != HAL_CRYP_STATE_RESET)
mbed_official 354:e67efb2aab0e 419 {
mbed_official 354:e67efb2aab0e 420 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 421 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 422
mbed_official 354:e67efb2aab0e 423 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 424 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 425 {
mbed_official 354:e67efb2aab0e 426 /* Set the key */
mbed_official 354:e67efb2aab0e 427 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 428
mbed_official 354:e67efb2aab0e 429 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 430 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 431
mbed_official 354:e67efb2aab0e 432 /* Set the CRYP peripheral in AES CBC mode */
mbed_official 354:e67efb2aab0e 433 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT);
mbed_official 354:e67efb2aab0e 434
mbed_official 354:e67efb2aab0e 435 /* Set the Initialization Vector */
mbed_official 354:e67efb2aab0e 436 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
mbed_official 354:e67efb2aab0e 437
mbed_official 354:e67efb2aab0e 438 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 439 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 440
mbed_official 354:e67efb2aab0e 441 /* Set the phase */
mbed_official 354:e67efb2aab0e 442 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 443 }
mbed_official 354:e67efb2aab0e 444
mbed_official 354:e67efb2aab0e 445 /* Write Plain Data and Get Cypher Data */
mbed_official 354:e67efb2aab0e 446 if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
mbed_official 354:e67efb2aab0e 447 {
mbed_official 354:e67efb2aab0e 448 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 449 }
mbed_official 354:e67efb2aab0e 450
mbed_official 354:e67efb2aab0e 451 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 452 hcryp->State = HAL_CRYP_STATE_READY;
mbed_official 354:e67efb2aab0e 453
mbed_official 354:e67efb2aab0e 454 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 455 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 456
mbed_official 354:e67efb2aab0e 457 /* Return function status */
mbed_official 354:e67efb2aab0e 458 return HAL_OK;
mbed_official 354:e67efb2aab0e 459 }
mbed_official 354:e67efb2aab0e 460 else
mbed_official 354:e67efb2aab0e 461 {
mbed_official 354:e67efb2aab0e 462 /* Return function status */
mbed_official 354:e67efb2aab0e 463 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 464 }
mbed_official 354:e67efb2aab0e 465 }
mbed_official 354:e67efb2aab0e 466
mbed_official 354:e67efb2aab0e 467 /**
mbed_official 354:e67efb2aab0e 468 * @brief Initializes the CRYP peripheral in AES CTR encryption mode
mbed_official 354:e67efb2aab0e 469 * then encrypt pPlainData. The cypher data are available in pCypherData
mbed_official 354:e67efb2aab0e 470 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 471 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 472 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 473 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
mbed_official 354:e67efb2aab0e 474 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 475 * @param Timeout: Specify Timeout value
mbed_official 354:e67efb2aab0e 476 * @retval HAL status
mbed_official 354:e67efb2aab0e 477 */
mbed_official 354:e67efb2aab0e 478 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
mbed_official 354:e67efb2aab0e 479 {
mbed_official 354:e67efb2aab0e 480 /* Process Locked */
mbed_official 354:e67efb2aab0e 481 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 482
mbed_official 354:e67efb2aab0e 483 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 484 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 485 {
mbed_official 354:e67efb2aab0e 486 /* Process Locked */
mbed_official 354:e67efb2aab0e 487 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 488
mbed_official 354:e67efb2aab0e 489 /* Return function status */
mbed_official 354:e67efb2aab0e 490 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 491 }
mbed_official 354:e67efb2aab0e 492
mbed_official 354:e67efb2aab0e 493 /* Check if HAL_CRYP_Init has been called */
mbed_official 354:e67efb2aab0e 494 if(hcryp->State != HAL_CRYP_STATE_RESET)
mbed_official 354:e67efb2aab0e 495 {
mbed_official 354:e67efb2aab0e 496 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 497 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 498
mbed_official 354:e67efb2aab0e 499 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 500 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 501 {
mbed_official 354:e67efb2aab0e 502 /* Set the key */
mbed_official 354:e67efb2aab0e 503 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 504
mbed_official 354:e67efb2aab0e 505 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 506 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 507
mbed_official 354:e67efb2aab0e 508 /* Set the CRYP peripheral in AES CTR mode */
mbed_official 354:e67efb2aab0e 509 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT);
mbed_official 354:e67efb2aab0e 510
mbed_official 354:e67efb2aab0e 511 /* Set the Initialization Vector */
mbed_official 354:e67efb2aab0e 512 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
mbed_official 354:e67efb2aab0e 513
mbed_official 354:e67efb2aab0e 514 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 515 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 516
mbed_official 354:e67efb2aab0e 517 /* Set the phase */
mbed_official 354:e67efb2aab0e 518 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 519 }
mbed_official 354:e67efb2aab0e 520
mbed_official 354:e67efb2aab0e 521 /* Write Plain Data and Get Cypher Data */
mbed_official 354:e67efb2aab0e 522 if(CRYP_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
mbed_official 354:e67efb2aab0e 523 {
mbed_official 354:e67efb2aab0e 524 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 525 }
mbed_official 354:e67efb2aab0e 526
mbed_official 354:e67efb2aab0e 527 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 528 hcryp->State = HAL_CRYP_STATE_READY;
mbed_official 354:e67efb2aab0e 529
mbed_official 354:e67efb2aab0e 530 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 531 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 532
mbed_official 354:e67efb2aab0e 533 /* Return function status */
mbed_official 354:e67efb2aab0e 534 return HAL_OK;
mbed_official 354:e67efb2aab0e 535 }
mbed_official 354:e67efb2aab0e 536 else
mbed_official 354:e67efb2aab0e 537 {
mbed_official 354:e67efb2aab0e 538 /* Return function status */
mbed_official 354:e67efb2aab0e 539 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 540 }
mbed_official 354:e67efb2aab0e 541 }
mbed_official 354:e67efb2aab0e 542
mbed_official 354:e67efb2aab0e 543 /**
mbed_official 354:e67efb2aab0e 544 * @brief Initializes the CRYP peripheral in AES ECB decryption mode
mbed_official 354:e67efb2aab0e 545 * then decrypted pCypherData. The cypher data are available in pPlainData
mbed_official 354:e67efb2aab0e 546 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 547 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 548 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 549 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
mbed_official 354:e67efb2aab0e 550 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 551 * @param Timeout: Specify Timeout value
mbed_official 354:e67efb2aab0e 552 * @retval HAL status
mbed_official 354:e67efb2aab0e 553 */
mbed_official 354:e67efb2aab0e 554 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
mbed_official 354:e67efb2aab0e 555 {
mbed_official 354:e67efb2aab0e 556 /* Process Locked */
mbed_official 354:e67efb2aab0e 557 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 558
mbed_official 354:e67efb2aab0e 559 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 560 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 561 {
mbed_official 354:e67efb2aab0e 562 /* Process Locked */
mbed_official 354:e67efb2aab0e 563 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 564
mbed_official 354:e67efb2aab0e 565 /* Return function status */
mbed_official 354:e67efb2aab0e 566 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 567 }
mbed_official 354:e67efb2aab0e 568
mbed_official 354:e67efb2aab0e 569 /* Check if HAL_CRYP_Init has been called */
mbed_official 354:e67efb2aab0e 570 if(hcryp->State != HAL_CRYP_STATE_RESET)
mbed_official 354:e67efb2aab0e 571 {
mbed_official 354:e67efb2aab0e 572 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 573 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 574
mbed_official 354:e67efb2aab0e 575 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 576 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 577 {
mbed_official 354:e67efb2aab0e 578 /* Set the key */
mbed_official 354:e67efb2aab0e 579 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 580
mbed_official 354:e67efb2aab0e 581 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 582 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 583
mbed_official 354:e67efb2aab0e 584 /* Set the CRYP peripheral in AES ECB decryption mode (with key derivation) */
mbed_official 354:e67efb2aab0e 585 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT);
mbed_official 354:e67efb2aab0e 586
mbed_official 354:e67efb2aab0e 587 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 588 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 589
mbed_official 354:e67efb2aab0e 590 /* Set the phase */
mbed_official 354:e67efb2aab0e 591 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 592 }
mbed_official 354:e67efb2aab0e 593
mbed_official 354:e67efb2aab0e 594 /* Write Cypher Data and Get Plain Data */
mbed_official 354:e67efb2aab0e 595 if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
mbed_official 354:e67efb2aab0e 596 {
mbed_official 354:e67efb2aab0e 597 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 598 }
mbed_official 354:e67efb2aab0e 599
mbed_official 354:e67efb2aab0e 600 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 601 hcryp->State = HAL_CRYP_STATE_READY;
mbed_official 354:e67efb2aab0e 602
mbed_official 354:e67efb2aab0e 603 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 604 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 605
mbed_official 354:e67efb2aab0e 606 /* Return function status */
mbed_official 354:e67efb2aab0e 607 return HAL_OK;
mbed_official 354:e67efb2aab0e 608 }
mbed_official 354:e67efb2aab0e 609 else
mbed_official 354:e67efb2aab0e 610 {
mbed_official 354:e67efb2aab0e 611 /* Return function status */
mbed_official 354:e67efb2aab0e 612 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 613 }
mbed_official 354:e67efb2aab0e 614 }
mbed_official 354:e67efb2aab0e 615
mbed_official 354:e67efb2aab0e 616 /**
mbed_official 354:e67efb2aab0e 617 * @brief Initializes the CRYP peripheral in AES ECB decryption mode
mbed_official 354:e67efb2aab0e 618 * then decrypted pCypherData. The cypher data are available in pPlainData
mbed_official 354:e67efb2aab0e 619 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 620 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 621 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 622 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
mbed_official 354:e67efb2aab0e 623 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 624 * @param Timeout: Specify Timeout value
mbed_official 354:e67efb2aab0e 625 * @retval HAL status
mbed_official 354:e67efb2aab0e 626 */
mbed_official 354:e67efb2aab0e 627 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
mbed_official 354:e67efb2aab0e 628 {
mbed_official 354:e67efb2aab0e 629 /* Process Locked */
mbed_official 354:e67efb2aab0e 630 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 631
mbed_official 354:e67efb2aab0e 632 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 633 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 634 {
mbed_official 354:e67efb2aab0e 635 /* Process Locked */
mbed_official 354:e67efb2aab0e 636 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 637
mbed_official 354:e67efb2aab0e 638 /* Return function status */
mbed_official 354:e67efb2aab0e 639 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 640 }
mbed_official 354:e67efb2aab0e 641
mbed_official 354:e67efb2aab0e 642 /* Check if HAL_CRYP_Init has been called */
mbed_official 354:e67efb2aab0e 643 if(hcryp->State != HAL_CRYP_STATE_RESET)
mbed_official 354:e67efb2aab0e 644 {
mbed_official 354:e67efb2aab0e 645 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 646 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 647
mbed_official 354:e67efb2aab0e 648 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 649 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 650 {
mbed_official 354:e67efb2aab0e 651 /* Set the key */
mbed_official 354:e67efb2aab0e 652 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 653
mbed_official 354:e67efb2aab0e 654 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 655 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 656
mbed_official 354:e67efb2aab0e 657 /* Set the CRYP peripheral in AES CBC decryption mode (with key derivation) */
mbed_official 354:e67efb2aab0e 658 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT);
mbed_official 354:e67efb2aab0e 659
mbed_official 354:e67efb2aab0e 660 /* Set the Initialization Vector */
mbed_official 354:e67efb2aab0e 661 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
mbed_official 354:e67efb2aab0e 662
mbed_official 354:e67efb2aab0e 663 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 664 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 665
mbed_official 354:e67efb2aab0e 666 /* Set the phase */
mbed_official 354:e67efb2aab0e 667 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 668 }
mbed_official 354:e67efb2aab0e 669
mbed_official 354:e67efb2aab0e 670 /* Write Cypher Data and Get Plain Data */
mbed_official 354:e67efb2aab0e 671 if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
mbed_official 354:e67efb2aab0e 672 {
mbed_official 354:e67efb2aab0e 673 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 674 }
mbed_official 354:e67efb2aab0e 675
mbed_official 354:e67efb2aab0e 676 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 677 hcryp->State = HAL_CRYP_STATE_READY;
mbed_official 354:e67efb2aab0e 678
mbed_official 354:e67efb2aab0e 679 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 680 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 681
mbed_official 354:e67efb2aab0e 682 /* Return function status */
mbed_official 354:e67efb2aab0e 683 return HAL_OK;
mbed_official 354:e67efb2aab0e 684 }
mbed_official 354:e67efb2aab0e 685 else
mbed_official 354:e67efb2aab0e 686 {
mbed_official 354:e67efb2aab0e 687 /* Return function status */
mbed_official 354:e67efb2aab0e 688 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 689 }
mbed_official 354:e67efb2aab0e 690 }
mbed_official 354:e67efb2aab0e 691
mbed_official 354:e67efb2aab0e 692 /**
mbed_official 354:e67efb2aab0e 693 * @brief Initializes the CRYP peripheral in AES CTR decryption mode
mbed_official 354:e67efb2aab0e 694 * then decrypted pCypherData. The cypher data are available in pPlainData
mbed_official 354:e67efb2aab0e 695 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 696 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 697 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 698 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
mbed_official 354:e67efb2aab0e 699 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 700 * @param Timeout: Specify Timeout value
mbed_official 354:e67efb2aab0e 701 * @retval HAL status
mbed_official 354:e67efb2aab0e 702 */
mbed_official 354:e67efb2aab0e 703 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
mbed_official 354:e67efb2aab0e 704 {
mbed_official 354:e67efb2aab0e 705 /* Process Locked */
mbed_official 354:e67efb2aab0e 706 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 707
mbed_official 354:e67efb2aab0e 708 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 709 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 710 {
mbed_official 354:e67efb2aab0e 711 /* Process Locked */
mbed_official 354:e67efb2aab0e 712 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 713
mbed_official 354:e67efb2aab0e 714 /* Return function status */
mbed_official 354:e67efb2aab0e 715 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 716 }
mbed_official 354:e67efb2aab0e 717
mbed_official 354:e67efb2aab0e 718 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 719 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->Phase == HAL_CRYP_PHASE_READY))
mbed_official 354:e67efb2aab0e 720 {
mbed_official 354:e67efb2aab0e 721 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 722 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 723
mbed_official 354:e67efb2aab0e 724 /* Set the key */
mbed_official 354:e67efb2aab0e 725 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 726
mbed_official 354:e67efb2aab0e 727 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 728 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 729
mbed_official 354:e67efb2aab0e 730 /* Set the CRYP peripheral in AES CTR decryption mode */
mbed_official 354:e67efb2aab0e 731 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_DECRYPT);
mbed_official 354:e67efb2aab0e 732
mbed_official 354:e67efb2aab0e 733 /* Set the Initialization Vector */
mbed_official 354:e67efb2aab0e 734 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
mbed_official 354:e67efb2aab0e 735
mbed_official 354:e67efb2aab0e 736 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 737 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 738
mbed_official 354:e67efb2aab0e 739 /* Set the phase */
mbed_official 354:e67efb2aab0e 740 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 741 }
mbed_official 354:e67efb2aab0e 742
mbed_official 354:e67efb2aab0e 743 /* Write Cypher Data and Get Plain Data */
mbed_official 354:e67efb2aab0e 744 if(CRYP_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
mbed_official 354:e67efb2aab0e 745 {
mbed_official 354:e67efb2aab0e 746 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 747 }
mbed_official 354:e67efb2aab0e 748
mbed_official 354:e67efb2aab0e 749 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 750 hcryp->State = HAL_CRYP_STATE_READY;
mbed_official 354:e67efb2aab0e 751
mbed_official 354:e67efb2aab0e 752 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 753 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 754
mbed_official 354:e67efb2aab0e 755 /* Return function status */
mbed_official 354:e67efb2aab0e 756 return HAL_OK;
mbed_official 354:e67efb2aab0e 757 }
mbed_official 354:e67efb2aab0e 758
mbed_official 354:e67efb2aab0e 759 /**
mbed_official 354:e67efb2aab0e 760 * @brief Initializes the CRYP peripheral in AES ECB encryption mode using Interrupt.
mbed_official 354:e67efb2aab0e 761 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 762 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 763 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 764 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
mbed_official 354:e67efb2aab0e 765 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 766 * @retval HAL status
mbed_official 354:e67efb2aab0e 767 */
mbed_official 354:e67efb2aab0e 768 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
mbed_official 354:e67efb2aab0e 769 {
mbed_official 354:e67efb2aab0e 770 uint32_t inputaddr = 0;
mbed_official 354:e67efb2aab0e 771
mbed_official 354:e67efb2aab0e 772 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 773 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 774 {
mbed_official 354:e67efb2aab0e 775 /* Process Locked */
mbed_official 354:e67efb2aab0e 776 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 777
mbed_official 354:e67efb2aab0e 778 /* Return function status */
mbed_official 354:e67efb2aab0e 779 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 780 }
mbed_official 354:e67efb2aab0e 781
mbed_official 354:e67efb2aab0e 782 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
mbed_official 354:e67efb2aab0e 783 {
mbed_official 354:e67efb2aab0e 784 /* Process Locked */
mbed_official 354:e67efb2aab0e 785 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 786
mbed_official 354:e67efb2aab0e 787 /* Get the buffer addresses and sizes */
mbed_official 354:e67efb2aab0e 788 hcryp->CrypInCount = Size;
mbed_official 354:e67efb2aab0e 789 hcryp->pCrypInBuffPtr = pPlainData;
mbed_official 354:e67efb2aab0e 790 hcryp->pCrypOutBuffPtr = pCypherData;
mbed_official 354:e67efb2aab0e 791 hcryp->CrypOutCount = Size;
mbed_official 354:e67efb2aab0e 792
mbed_official 354:e67efb2aab0e 793 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 794 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 795
mbed_official 354:e67efb2aab0e 796 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 797 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 798 {
mbed_official 354:e67efb2aab0e 799 /* Set the key */
mbed_official 354:e67efb2aab0e 800 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 801
mbed_official 354:e67efb2aab0e 802 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 803 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 804
mbed_official 354:e67efb2aab0e 805 /* Set the CRYP peripheral in AES ECB mode */
mbed_official 354:e67efb2aab0e 806 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT);
mbed_official 354:e67efb2aab0e 807
mbed_official 354:e67efb2aab0e 808 /* Set the phase */
mbed_official 354:e67efb2aab0e 809 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 810 }
mbed_official 354:e67efb2aab0e 811
mbed_official 354:e67efb2aab0e 812 /* Enable Interrupts */
mbed_official 354:e67efb2aab0e 813 __HAL_CRYP_ENABLE_IT(AES_IT_CC);
mbed_official 354:e67efb2aab0e 814
mbed_official 354:e67efb2aab0e 815 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 816 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 817
mbed_official 354:e67efb2aab0e 818 /* Get the last input data adress */
mbed_official 354:e67efb2aab0e 819 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
mbed_official 354:e67efb2aab0e 820
mbed_official 354:e67efb2aab0e 821 /* Write the Input block in the Data Input register */
mbed_official 354:e67efb2aab0e 822 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 823 inputaddr+=4;
mbed_official 354:e67efb2aab0e 824 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 825 inputaddr+=4;
mbed_official 354:e67efb2aab0e 826 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 827 inputaddr+=4;
mbed_official 354:e67efb2aab0e 828 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 829 hcryp->pCrypInBuffPtr += 16;
mbed_official 354:e67efb2aab0e 830 hcryp->CrypInCount -= 16;
mbed_official 354:e67efb2aab0e 831
mbed_official 354:e67efb2aab0e 832 /* Return function status */
mbed_official 354:e67efb2aab0e 833 return HAL_OK;
mbed_official 354:e67efb2aab0e 834 }
mbed_official 354:e67efb2aab0e 835 else
mbed_official 354:e67efb2aab0e 836 {
mbed_official 354:e67efb2aab0e 837 /* Return function status */
mbed_official 354:e67efb2aab0e 838 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 839 }
mbed_official 354:e67efb2aab0e 840 }
mbed_official 354:e67efb2aab0e 841
mbed_official 354:e67efb2aab0e 842 /**
mbed_official 354:e67efb2aab0e 843 * @brief Initializes the CRYP peripheral in AES CBC encryption mode using Interrupt.
mbed_official 354:e67efb2aab0e 844 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 845 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 846 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 847 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
mbed_official 354:e67efb2aab0e 848 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 849 * @retval HAL status
mbed_official 354:e67efb2aab0e 850 */
mbed_official 354:e67efb2aab0e 851 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
mbed_official 354:e67efb2aab0e 852 {
mbed_official 354:e67efb2aab0e 853 uint32_t inputaddr = 0;
mbed_official 354:e67efb2aab0e 854
mbed_official 354:e67efb2aab0e 855 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 856 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 857 {
mbed_official 354:e67efb2aab0e 858 /* Process Locked */
mbed_official 354:e67efb2aab0e 859 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 860
mbed_official 354:e67efb2aab0e 861 /* Return function status */
mbed_official 354:e67efb2aab0e 862 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 863 }
mbed_official 354:e67efb2aab0e 864
mbed_official 354:e67efb2aab0e 865 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
mbed_official 354:e67efb2aab0e 866 {
mbed_official 354:e67efb2aab0e 867 /* Process Locked */
mbed_official 354:e67efb2aab0e 868 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 869
mbed_official 354:e67efb2aab0e 870 /* Get the buffer addresses and sizes */
mbed_official 354:e67efb2aab0e 871 hcryp->CrypInCount = Size;
mbed_official 354:e67efb2aab0e 872 hcryp->pCrypInBuffPtr = pPlainData;
mbed_official 354:e67efb2aab0e 873 hcryp->pCrypOutBuffPtr = pCypherData;
mbed_official 354:e67efb2aab0e 874 hcryp->CrypOutCount = Size;
mbed_official 354:e67efb2aab0e 875
mbed_official 354:e67efb2aab0e 876 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 877 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 878
mbed_official 354:e67efb2aab0e 879 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 880 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 881 {
mbed_official 354:e67efb2aab0e 882 /* Set the key */
mbed_official 354:e67efb2aab0e 883 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 884
mbed_official 354:e67efb2aab0e 885 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 886 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 887
mbed_official 354:e67efb2aab0e 888 /* Set the CRYP peripheral in AES CBC mode */
mbed_official 354:e67efb2aab0e 889 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT);
mbed_official 354:e67efb2aab0e 890
mbed_official 354:e67efb2aab0e 891 /* Set the Initialization Vector */
mbed_official 354:e67efb2aab0e 892 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
mbed_official 354:e67efb2aab0e 893
mbed_official 354:e67efb2aab0e 894 /* Set the phase */
mbed_official 354:e67efb2aab0e 895 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 896 }
mbed_official 354:e67efb2aab0e 897
mbed_official 354:e67efb2aab0e 898 /* Enable Interrupts */
mbed_official 354:e67efb2aab0e 899 __HAL_CRYP_ENABLE_IT(AES_IT_CC);
mbed_official 354:e67efb2aab0e 900
mbed_official 354:e67efb2aab0e 901 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 902 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 903
mbed_official 354:e67efb2aab0e 904 /* Get the last input data adress */
mbed_official 354:e67efb2aab0e 905 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
mbed_official 354:e67efb2aab0e 906
mbed_official 354:e67efb2aab0e 907 /* Write the Input block in the Data Input register */
mbed_official 354:e67efb2aab0e 908 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 909 inputaddr+=4;
mbed_official 354:e67efb2aab0e 910 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 911 inputaddr+=4;
mbed_official 354:e67efb2aab0e 912 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 913 inputaddr+=4;
mbed_official 354:e67efb2aab0e 914 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 915 hcryp->pCrypInBuffPtr += 16;
mbed_official 354:e67efb2aab0e 916 hcryp->CrypInCount -= 16;
mbed_official 354:e67efb2aab0e 917
mbed_official 354:e67efb2aab0e 918 /* Return function status */
mbed_official 354:e67efb2aab0e 919 return HAL_OK;
mbed_official 354:e67efb2aab0e 920 }
mbed_official 354:e67efb2aab0e 921 else
mbed_official 354:e67efb2aab0e 922 {
mbed_official 354:e67efb2aab0e 923 /* Return function status */
mbed_official 354:e67efb2aab0e 924 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 925 }
mbed_official 354:e67efb2aab0e 926 }
mbed_official 354:e67efb2aab0e 927
mbed_official 354:e67efb2aab0e 928 /**
mbed_official 354:e67efb2aab0e 929 * @brief Initializes the CRYP peripheral in AES CTR encryption mode using Interrupt.
mbed_official 354:e67efb2aab0e 930 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 931 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 932 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 933 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
mbed_official 354:e67efb2aab0e 934 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 935 * @retval HAL status
mbed_official 354:e67efb2aab0e 936 */
mbed_official 354:e67efb2aab0e 937 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
mbed_official 354:e67efb2aab0e 938 {
mbed_official 354:e67efb2aab0e 939 uint32_t inputaddr = 0;
mbed_official 354:e67efb2aab0e 940
mbed_official 354:e67efb2aab0e 941 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 942 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 943 {
mbed_official 354:e67efb2aab0e 944 /* Process Locked */
mbed_official 354:e67efb2aab0e 945 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 946
mbed_official 354:e67efb2aab0e 947 /* Return function status */
mbed_official 354:e67efb2aab0e 948 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 949 }
mbed_official 354:e67efb2aab0e 950
mbed_official 354:e67efb2aab0e 951 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
mbed_official 354:e67efb2aab0e 952 {
mbed_official 354:e67efb2aab0e 953 /* Process Locked */
mbed_official 354:e67efb2aab0e 954 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 955
mbed_official 354:e67efb2aab0e 956 /* Get the buffer addresses and sizes */
mbed_official 354:e67efb2aab0e 957 hcryp->CrypInCount = Size;
mbed_official 354:e67efb2aab0e 958 hcryp->pCrypInBuffPtr = pPlainData;
mbed_official 354:e67efb2aab0e 959 hcryp->pCrypOutBuffPtr = pCypherData;
mbed_official 354:e67efb2aab0e 960 hcryp->CrypOutCount = Size;
mbed_official 354:e67efb2aab0e 961
mbed_official 354:e67efb2aab0e 962 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 963 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 964
mbed_official 354:e67efb2aab0e 965 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 966 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 967 {
mbed_official 354:e67efb2aab0e 968 /* Set the key */
mbed_official 354:e67efb2aab0e 969 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 970
mbed_official 354:e67efb2aab0e 971 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 972 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 973
mbed_official 354:e67efb2aab0e 974 /* Set the CRYP peripheral in AES CTR mode */
mbed_official 354:e67efb2aab0e 975 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT);
mbed_official 354:e67efb2aab0e 976
mbed_official 354:e67efb2aab0e 977 /* Set the Initialization Vector */
mbed_official 354:e67efb2aab0e 978 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
mbed_official 354:e67efb2aab0e 979
mbed_official 354:e67efb2aab0e 980 /* Set the phase */
mbed_official 354:e67efb2aab0e 981 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 982 }
mbed_official 354:e67efb2aab0e 983
mbed_official 354:e67efb2aab0e 984 /* Enable Interrupts */
mbed_official 354:e67efb2aab0e 985 __HAL_CRYP_ENABLE_IT(AES_IT_CC);
mbed_official 354:e67efb2aab0e 986
mbed_official 354:e67efb2aab0e 987 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 988 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 989
mbed_official 354:e67efb2aab0e 990 /* Get the last input data adress */
mbed_official 354:e67efb2aab0e 991 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
mbed_official 354:e67efb2aab0e 992
mbed_official 354:e67efb2aab0e 993 /* Write the Input block in the Data Input register */
mbed_official 354:e67efb2aab0e 994 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 995 inputaddr+=4;
mbed_official 354:e67efb2aab0e 996 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 997 inputaddr+=4;
mbed_official 354:e67efb2aab0e 998 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 999 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1000 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1001 hcryp->pCrypInBuffPtr += 16;
mbed_official 354:e67efb2aab0e 1002 hcryp->CrypInCount -= 16;
mbed_official 354:e67efb2aab0e 1003
mbed_official 354:e67efb2aab0e 1004 /* Return function status */
mbed_official 354:e67efb2aab0e 1005 return HAL_OK;
mbed_official 354:e67efb2aab0e 1006 }
mbed_official 354:e67efb2aab0e 1007 else
mbed_official 354:e67efb2aab0e 1008 {
mbed_official 354:e67efb2aab0e 1009 /* Return function status */
mbed_official 354:e67efb2aab0e 1010 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1011 }
mbed_official 354:e67efb2aab0e 1012 }
mbed_official 354:e67efb2aab0e 1013
mbed_official 354:e67efb2aab0e 1014 /**
mbed_official 354:e67efb2aab0e 1015 * @brief Initializes the CRYP peripheral in AES ECB decryption mode using Interrupt.
mbed_official 354:e67efb2aab0e 1016 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1017 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1018 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1019 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
mbed_official 354:e67efb2aab0e 1020 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1021 * @retval HAL status
mbed_official 354:e67efb2aab0e 1022 */
mbed_official 354:e67efb2aab0e 1023 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
mbed_official 354:e67efb2aab0e 1024 {
mbed_official 354:e67efb2aab0e 1025 uint32_t inputaddr = 0;
mbed_official 354:e67efb2aab0e 1026
mbed_official 354:e67efb2aab0e 1027 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 1028 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 1029 {
mbed_official 354:e67efb2aab0e 1030 /* Process Locked */
mbed_official 354:e67efb2aab0e 1031 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1032
mbed_official 354:e67efb2aab0e 1033 /* Return function status */
mbed_official 354:e67efb2aab0e 1034 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1035 }
mbed_official 354:e67efb2aab0e 1036
mbed_official 354:e67efb2aab0e 1037 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
mbed_official 354:e67efb2aab0e 1038 {
mbed_official 354:e67efb2aab0e 1039 /* Process Locked */
mbed_official 354:e67efb2aab0e 1040 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 1041
mbed_official 354:e67efb2aab0e 1042 /* Get the buffer addresses and sizes */
mbed_official 354:e67efb2aab0e 1043 hcryp->CrypInCount = Size;
mbed_official 354:e67efb2aab0e 1044 hcryp->pCrypInBuffPtr = pCypherData;
mbed_official 354:e67efb2aab0e 1045 hcryp->pCrypOutBuffPtr = pPlainData;
mbed_official 354:e67efb2aab0e 1046 hcryp->CrypOutCount = Size;
mbed_official 354:e67efb2aab0e 1047
mbed_official 354:e67efb2aab0e 1048 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 1049 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 1050
mbed_official 354:e67efb2aab0e 1051 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 1052 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 1053 {
mbed_official 354:e67efb2aab0e 1054 /* Set the key */
mbed_official 354:e67efb2aab0e 1055 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 1056
mbed_official 354:e67efb2aab0e 1057 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 1058 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 1059
mbed_official 354:e67efb2aab0e 1060 /* Set the CRYP peripheral in AES ECB decryption mode (with key derivation) */
mbed_official 354:e67efb2aab0e 1061 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT);
mbed_official 354:e67efb2aab0e 1062
mbed_official 354:e67efb2aab0e 1063 /* Set the phase */
mbed_official 354:e67efb2aab0e 1064 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 1065 }
mbed_official 354:e67efb2aab0e 1066
mbed_official 354:e67efb2aab0e 1067 /* Enable Interrupts */
mbed_official 354:e67efb2aab0e 1068 __HAL_CRYP_ENABLE_IT(AES_IT_CC);
mbed_official 354:e67efb2aab0e 1069
mbed_official 354:e67efb2aab0e 1070 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 1071 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 1072
mbed_official 354:e67efb2aab0e 1073 /* Get the last input data adress */
mbed_official 354:e67efb2aab0e 1074 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
mbed_official 354:e67efb2aab0e 1075
mbed_official 354:e67efb2aab0e 1076 /* Write the Input block in the Data Input register */
mbed_official 354:e67efb2aab0e 1077 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1078 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1079 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1080 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1081 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1082 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1083 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1084 hcryp->pCrypInBuffPtr += 16;
mbed_official 354:e67efb2aab0e 1085 hcryp->CrypInCount -= 16;
mbed_official 354:e67efb2aab0e 1086
mbed_official 354:e67efb2aab0e 1087 /* Return function status */
mbed_official 354:e67efb2aab0e 1088 return HAL_OK;
mbed_official 354:e67efb2aab0e 1089 }
mbed_official 354:e67efb2aab0e 1090 else
mbed_official 354:e67efb2aab0e 1091 {
mbed_official 354:e67efb2aab0e 1092 /* Return function status */
mbed_official 354:e67efb2aab0e 1093 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1094 }
mbed_official 354:e67efb2aab0e 1095 }
mbed_official 354:e67efb2aab0e 1096
mbed_official 354:e67efb2aab0e 1097 /**
mbed_official 354:e67efb2aab0e 1098 * @brief Initializes the CRYP peripheral in AES CBC decryption mode using IT.
mbed_official 354:e67efb2aab0e 1099 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1100 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1101 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1102 * @param Size: Length of the plaintext buffer, must be a multiple of 16
mbed_official 354:e67efb2aab0e 1103 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1104 * @retval HAL status
mbed_official 354:e67efb2aab0e 1105 */
mbed_official 354:e67efb2aab0e 1106 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
mbed_official 354:e67efb2aab0e 1107 {
mbed_official 354:e67efb2aab0e 1108 uint32_t inputaddr = 0;
mbed_official 354:e67efb2aab0e 1109
mbed_official 354:e67efb2aab0e 1110 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 1111 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 1112 {
mbed_official 354:e67efb2aab0e 1113 /* Process Locked */
mbed_official 354:e67efb2aab0e 1114 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1115
mbed_official 354:e67efb2aab0e 1116 /* Return function status */
mbed_official 354:e67efb2aab0e 1117 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1118 }
mbed_official 354:e67efb2aab0e 1119
mbed_official 354:e67efb2aab0e 1120 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
mbed_official 354:e67efb2aab0e 1121 {
mbed_official 354:e67efb2aab0e 1122 /* Process Locked */
mbed_official 354:e67efb2aab0e 1123 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 1124
mbed_official 354:e67efb2aab0e 1125 /* Get the buffer addresses and sizes */
mbed_official 354:e67efb2aab0e 1126 hcryp->CrypInCount = Size;
mbed_official 354:e67efb2aab0e 1127 hcryp->pCrypInBuffPtr = pCypherData;
mbed_official 354:e67efb2aab0e 1128 hcryp->pCrypOutBuffPtr = pPlainData;
mbed_official 354:e67efb2aab0e 1129 hcryp->CrypOutCount = Size;
mbed_official 354:e67efb2aab0e 1130
mbed_official 354:e67efb2aab0e 1131 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 1132 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 1133
mbed_official 354:e67efb2aab0e 1134 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 1135 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 1136 {
mbed_official 354:e67efb2aab0e 1137 /* Set the key */
mbed_official 354:e67efb2aab0e 1138 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 1139
mbed_official 354:e67efb2aab0e 1140 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 1141 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 1142
mbed_official 354:e67efb2aab0e 1143 /* Set the CRYP peripheral in AES CBC decryption mode (with key derivation) */
mbed_official 354:e67efb2aab0e 1144 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT);
mbed_official 354:e67efb2aab0e 1145
mbed_official 354:e67efb2aab0e 1146 /* Set the Initialization Vector */
mbed_official 354:e67efb2aab0e 1147 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
mbed_official 354:e67efb2aab0e 1148
mbed_official 354:e67efb2aab0e 1149 /* Set the phase */
mbed_official 354:e67efb2aab0e 1150 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 1151 }
mbed_official 354:e67efb2aab0e 1152
mbed_official 354:e67efb2aab0e 1153 /* Enable Interrupts */
mbed_official 354:e67efb2aab0e 1154 __HAL_CRYP_ENABLE_IT(AES_IT_CC);
mbed_official 354:e67efb2aab0e 1155
mbed_official 354:e67efb2aab0e 1156 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 1157 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 1158
mbed_official 354:e67efb2aab0e 1159 /* Get the last input data adress */
mbed_official 354:e67efb2aab0e 1160 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
mbed_official 354:e67efb2aab0e 1161
mbed_official 354:e67efb2aab0e 1162 /* Write the Input block in the Data Input register */
mbed_official 354:e67efb2aab0e 1163 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1164 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1165 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1166 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1167 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1168 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1169 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1170 hcryp->pCrypInBuffPtr += 16;
mbed_official 354:e67efb2aab0e 1171 hcryp->CrypInCount -= 16;
mbed_official 354:e67efb2aab0e 1172
mbed_official 354:e67efb2aab0e 1173 /* Return function status */
mbed_official 354:e67efb2aab0e 1174 return HAL_OK;
mbed_official 354:e67efb2aab0e 1175 }
mbed_official 354:e67efb2aab0e 1176 else
mbed_official 354:e67efb2aab0e 1177 {
mbed_official 354:e67efb2aab0e 1178 /* Return function status */
mbed_official 354:e67efb2aab0e 1179 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1180 }
mbed_official 354:e67efb2aab0e 1181 }
mbed_official 354:e67efb2aab0e 1182
mbed_official 354:e67efb2aab0e 1183 /**
mbed_official 354:e67efb2aab0e 1184 * @brief Initializes the CRYP peripheral in AES CTR decryption mode using Interrupt.
mbed_official 354:e67efb2aab0e 1185 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1186 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1187 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1188 * @param Size: Length of the plaintext buffer, must be a multiple of 16
mbed_official 354:e67efb2aab0e 1189 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1190 * @retval HAL status
mbed_official 354:e67efb2aab0e 1191 */
mbed_official 354:e67efb2aab0e 1192 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
mbed_official 354:e67efb2aab0e 1193 {
mbed_official 354:e67efb2aab0e 1194 uint32_t inputaddr = 0;
mbed_official 354:e67efb2aab0e 1195
mbed_official 354:e67efb2aab0e 1196 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 1197 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 1198 {
mbed_official 354:e67efb2aab0e 1199 /* Process Locked */
mbed_official 354:e67efb2aab0e 1200 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1201
mbed_official 354:e67efb2aab0e 1202 /* Return function status */
mbed_official 354:e67efb2aab0e 1203 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1204 }
mbed_official 354:e67efb2aab0e 1205
mbed_official 354:e67efb2aab0e 1206 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
mbed_official 354:e67efb2aab0e 1207 {
mbed_official 354:e67efb2aab0e 1208 /* Process Locked */
mbed_official 354:e67efb2aab0e 1209 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 1210
mbed_official 354:e67efb2aab0e 1211 /* Get the buffer addresses and sizes */
mbed_official 354:e67efb2aab0e 1212 hcryp->CrypInCount = Size;
mbed_official 354:e67efb2aab0e 1213 hcryp->pCrypInBuffPtr = pCypherData;
mbed_official 354:e67efb2aab0e 1214 hcryp->pCrypOutBuffPtr = pPlainData;
mbed_official 354:e67efb2aab0e 1215 hcryp->CrypOutCount = Size;
mbed_official 354:e67efb2aab0e 1216
mbed_official 354:e67efb2aab0e 1217 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 1218 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 1219
mbed_official 354:e67efb2aab0e 1220 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 1221 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 1222 {
mbed_official 354:e67efb2aab0e 1223 /* Set the key */
mbed_official 354:e67efb2aab0e 1224 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 1225
mbed_official 354:e67efb2aab0e 1226 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 1227 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 1228
mbed_official 354:e67efb2aab0e 1229 /* Set the CRYP peripheral in AES CTR decryption mode */
mbed_official 354:e67efb2aab0e 1230 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_DECRYPT);
mbed_official 354:e67efb2aab0e 1231
mbed_official 354:e67efb2aab0e 1232 /* Set the Initialization Vector */
mbed_official 354:e67efb2aab0e 1233 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
mbed_official 354:e67efb2aab0e 1234
mbed_official 354:e67efb2aab0e 1235 /* Set the phase */
mbed_official 354:e67efb2aab0e 1236 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 1237 }
mbed_official 354:e67efb2aab0e 1238
mbed_official 354:e67efb2aab0e 1239 /* Enable Interrupts */
mbed_official 354:e67efb2aab0e 1240 __HAL_CRYP_ENABLE_IT(AES_IT_CC);
mbed_official 354:e67efb2aab0e 1241
mbed_official 354:e67efb2aab0e 1242 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 1243 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 1244
mbed_official 354:e67efb2aab0e 1245 /* Get the last input data adress */
mbed_official 354:e67efb2aab0e 1246 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
mbed_official 354:e67efb2aab0e 1247
mbed_official 354:e67efb2aab0e 1248 /* Write the Input block in the Data Input register */
mbed_official 354:e67efb2aab0e 1249 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1250 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1251 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1252 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1253 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1254 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1255 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1256 hcryp->pCrypInBuffPtr += 16;
mbed_official 354:e67efb2aab0e 1257 hcryp->CrypInCount -= 16;
mbed_official 354:e67efb2aab0e 1258
mbed_official 354:e67efb2aab0e 1259 /* Return function status */
mbed_official 354:e67efb2aab0e 1260 return HAL_OK;
mbed_official 354:e67efb2aab0e 1261 }
mbed_official 354:e67efb2aab0e 1262 else
mbed_official 354:e67efb2aab0e 1263 {
mbed_official 354:e67efb2aab0e 1264 /* Return function status */
mbed_official 354:e67efb2aab0e 1265 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1266 }
mbed_official 354:e67efb2aab0e 1267 }
mbed_official 354:e67efb2aab0e 1268
mbed_official 354:e67efb2aab0e 1269 /**
mbed_official 354:e67efb2aab0e 1270 * @brief Initializes the CRYP peripheral in AES ECB encryption mode using DMA.
mbed_official 354:e67efb2aab0e 1271 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1272 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1273 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1274 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
mbed_official 354:e67efb2aab0e 1275 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1276 * @retval HAL status
mbed_official 354:e67efb2aab0e 1277 */
mbed_official 354:e67efb2aab0e 1278 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
mbed_official 354:e67efb2aab0e 1279 {
mbed_official 354:e67efb2aab0e 1280 uint32_t inputaddr = 0, outputaddr = 0;
mbed_official 354:e67efb2aab0e 1281
mbed_official 354:e67efb2aab0e 1282 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 1283 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 1284 {
mbed_official 354:e67efb2aab0e 1285 /* Process Locked */
mbed_official 354:e67efb2aab0e 1286 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1287
mbed_official 354:e67efb2aab0e 1288 /* Return function status */
mbed_official 354:e67efb2aab0e 1289 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1290 }
mbed_official 354:e67efb2aab0e 1291
mbed_official 354:e67efb2aab0e 1292 /* Check if HAL_CRYP_Init has been called */
mbed_official 354:e67efb2aab0e 1293 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
mbed_official 354:e67efb2aab0e 1294 {
mbed_official 354:e67efb2aab0e 1295 /* Process Locked */
mbed_official 354:e67efb2aab0e 1296 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 1297
mbed_official 354:e67efb2aab0e 1298 inputaddr = (uint32_t)pPlainData;
mbed_official 354:e67efb2aab0e 1299 outputaddr = (uint32_t)pCypherData;
mbed_official 354:e67efb2aab0e 1300
mbed_official 354:e67efb2aab0e 1301 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 1302 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 1303
mbed_official 354:e67efb2aab0e 1304 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 1305 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 1306 {
mbed_official 354:e67efb2aab0e 1307 /* Set the key */
mbed_official 354:e67efb2aab0e 1308 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 1309
mbed_official 354:e67efb2aab0e 1310 /* Set the CRYP peripheral in AES ECB mode */
mbed_official 354:e67efb2aab0e 1311 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_ENCRYPT);
mbed_official 354:e67efb2aab0e 1312
mbed_official 354:e67efb2aab0e 1313 /* Set the phase */
mbed_official 354:e67efb2aab0e 1314 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 1315 }
mbed_official 354:e67efb2aab0e 1316 /* Set the input and output addresses and start DMA transfer */
mbed_official 354:e67efb2aab0e 1317 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
mbed_official 354:e67efb2aab0e 1318
mbed_official 354:e67efb2aab0e 1319 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 1320 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1321
mbed_official 354:e67efb2aab0e 1322 /* Return function status */
mbed_official 354:e67efb2aab0e 1323 return HAL_OK;
mbed_official 354:e67efb2aab0e 1324 }
mbed_official 354:e67efb2aab0e 1325 else
mbed_official 354:e67efb2aab0e 1326 {
mbed_official 354:e67efb2aab0e 1327 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1328 }
mbed_official 354:e67efb2aab0e 1329 }
mbed_official 354:e67efb2aab0e 1330
mbed_official 354:e67efb2aab0e 1331 /**
mbed_official 354:e67efb2aab0e 1332 * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
mbed_official 354:e67efb2aab0e 1333 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1334 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1335 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1336 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
mbed_official 354:e67efb2aab0e 1337 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1338 * @retval HAL status
mbed_official 354:e67efb2aab0e 1339 */
mbed_official 354:e67efb2aab0e 1340 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
mbed_official 354:e67efb2aab0e 1341 {
mbed_official 354:e67efb2aab0e 1342 uint32_t inputaddr = 0, outputaddr = 0;
mbed_official 354:e67efb2aab0e 1343
mbed_official 354:e67efb2aab0e 1344 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 1345 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 1346 {
mbed_official 354:e67efb2aab0e 1347 /* Process Locked */
mbed_official 354:e67efb2aab0e 1348 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1349
mbed_official 354:e67efb2aab0e 1350 /* Return function status */
mbed_official 354:e67efb2aab0e 1351 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1352 }
mbed_official 354:e67efb2aab0e 1353
mbed_official 354:e67efb2aab0e 1354 /* Check if HAL_CRYP_Init has been called */
mbed_official 354:e67efb2aab0e 1355 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
mbed_official 354:e67efb2aab0e 1356 {
mbed_official 354:e67efb2aab0e 1357 /* Process Locked */
mbed_official 354:e67efb2aab0e 1358 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 1359
mbed_official 354:e67efb2aab0e 1360 inputaddr = (uint32_t)pPlainData;
mbed_official 354:e67efb2aab0e 1361 outputaddr = (uint32_t)pCypherData;
mbed_official 354:e67efb2aab0e 1362
mbed_official 354:e67efb2aab0e 1363 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 1364 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 1365
mbed_official 354:e67efb2aab0e 1366 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 1367 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 1368 {
mbed_official 354:e67efb2aab0e 1369 /* Set the key */
mbed_official 354:e67efb2aab0e 1370 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 1371
mbed_official 354:e67efb2aab0e 1372 /* Set the CRYP peripheral in AES CBC mode */
mbed_official 354:e67efb2aab0e 1373 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_ENCRYPT);
mbed_official 354:e67efb2aab0e 1374
mbed_official 354:e67efb2aab0e 1375 /* Set the Initialization Vector */
mbed_official 354:e67efb2aab0e 1376 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
mbed_official 354:e67efb2aab0e 1377
mbed_official 354:e67efb2aab0e 1378 /* Set the phase */
mbed_official 354:e67efb2aab0e 1379 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 1380 }
mbed_official 354:e67efb2aab0e 1381 /* Set the input and output addresses and start DMA transfer */
mbed_official 354:e67efb2aab0e 1382 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
mbed_official 354:e67efb2aab0e 1383
mbed_official 354:e67efb2aab0e 1384 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 1385 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1386
mbed_official 354:e67efb2aab0e 1387 /* Return function status */
mbed_official 354:e67efb2aab0e 1388 return HAL_OK;
mbed_official 354:e67efb2aab0e 1389 }
mbed_official 354:e67efb2aab0e 1390 else
mbed_official 354:e67efb2aab0e 1391 {
mbed_official 354:e67efb2aab0e 1392 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1393 }
mbed_official 354:e67efb2aab0e 1394 }
mbed_official 354:e67efb2aab0e 1395
mbed_official 354:e67efb2aab0e 1396 /**
mbed_official 354:e67efb2aab0e 1397 * @brief Initializes the CRYP peripheral in AES CTR encryption mode using DMA.
mbed_official 354:e67efb2aab0e 1398 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1399 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1400 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1401 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
mbed_official 354:e67efb2aab0e 1402 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1403 * @retval HAL status
mbed_official 354:e67efb2aab0e 1404 */
mbed_official 354:e67efb2aab0e 1405 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
mbed_official 354:e67efb2aab0e 1406 {
mbed_official 354:e67efb2aab0e 1407 uint32_t inputaddr = 0, outputaddr = 0;
mbed_official 354:e67efb2aab0e 1408
mbed_official 354:e67efb2aab0e 1409 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 1410 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 1411 {
mbed_official 354:e67efb2aab0e 1412 /* Process Locked */
mbed_official 354:e67efb2aab0e 1413 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1414
mbed_official 354:e67efb2aab0e 1415 /* Return function status */
mbed_official 354:e67efb2aab0e 1416 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1417 }
mbed_official 354:e67efb2aab0e 1418
mbed_official 354:e67efb2aab0e 1419 /* Check if HAL_CRYP_Init has been called */
mbed_official 354:e67efb2aab0e 1420 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
mbed_official 354:e67efb2aab0e 1421 {
mbed_official 354:e67efb2aab0e 1422 /* Process Locked */
mbed_official 354:e67efb2aab0e 1423 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 1424
mbed_official 354:e67efb2aab0e 1425 inputaddr = (uint32_t)pPlainData;
mbed_official 354:e67efb2aab0e 1426 outputaddr = (uint32_t)pCypherData;
mbed_official 354:e67efb2aab0e 1427
mbed_official 354:e67efb2aab0e 1428 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 1429 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 1430
mbed_official 354:e67efb2aab0e 1431 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 1432 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 1433 {
mbed_official 354:e67efb2aab0e 1434 /* Set the key */
mbed_official 354:e67efb2aab0e 1435 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 1436
mbed_official 354:e67efb2aab0e 1437 /* Set the CRYP peripheral in AES CTR mode */
mbed_official 354:e67efb2aab0e 1438 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_ENCRYPT);
mbed_official 354:e67efb2aab0e 1439
mbed_official 354:e67efb2aab0e 1440 /* Set the Initialization Vector */
mbed_official 354:e67efb2aab0e 1441 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
mbed_official 354:e67efb2aab0e 1442
mbed_official 354:e67efb2aab0e 1443 /* Set the phase */
mbed_official 354:e67efb2aab0e 1444 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 1445 }
mbed_official 354:e67efb2aab0e 1446
mbed_official 354:e67efb2aab0e 1447 /* Set the input and output addresses and start DMA transfer */
mbed_official 354:e67efb2aab0e 1448 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
mbed_official 354:e67efb2aab0e 1449
mbed_official 354:e67efb2aab0e 1450 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 1451 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1452
mbed_official 354:e67efb2aab0e 1453 /* Return function status */
mbed_official 354:e67efb2aab0e 1454 return HAL_OK;
mbed_official 354:e67efb2aab0e 1455 }
mbed_official 354:e67efb2aab0e 1456 else
mbed_official 354:e67efb2aab0e 1457 {
mbed_official 354:e67efb2aab0e 1458 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1459 }
mbed_official 354:e67efb2aab0e 1460 }
mbed_official 354:e67efb2aab0e 1461
mbed_official 354:e67efb2aab0e 1462 /**
mbed_official 354:e67efb2aab0e 1463 * @brief Initializes the CRYP peripheral in AES ECB decryption mode using DMA.
mbed_official 354:e67efb2aab0e 1464 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1465 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1466 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1467 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
mbed_official 354:e67efb2aab0e 1468 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1469 * @retval HAL status
mbed_official 354:e67efb2aab0e 1470 */
mbed_official 354:e67efb2aab0e 1471 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
mbed_official 354:e67efb2aab0e 1472 {
mbed_official 354:e67efb2aab0e 1473 uint32_t inputaddr = 0, outputaddr = 0;
mbed_official 354:e67efb2aab0e 1474
mbed_official 354:e67efb2aab0e 1475 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 1476 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 1477 {
mbed_official 354:e67efb2aab0e 1478 /* Process Locked */
mbed_official 354:e67efb2aab0e 1479 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1480
mbed_official 354:e67efb2aab0e 1481 /* Return function status */
mbed_official 354:e67efb2aab0e 1482 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1483 }
mbed_official 354:e67efb2aab0e 1484
mbed_official 354:e67efb2aab0e 1485 /* Check if HAL_CRYP_Init has been called */
mbed_official 354:e67efb2aab0e 1486 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
mbed_official 354:e67efb2aab0e 1487 {
mbed_official 354:e67efb2aab0e 1488 /* Process Locked */
mbed_official 354:e67efb2aab0e 1489 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 1490
mbed_official 354:e67efb2aab0e 1491 inputaddr = (uint32_t)pCypherData;
mbed_official 354:e67efb2aab0e 1492 outputaddr = (uint32_t)pPlainData;
mbed_official 354:e67efb2aab0e 1493
mbed_official 354:e67efb2aab0e 1494 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 1495 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 1496
mbed_official 354:e67efb2aab0e 1497 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 1498 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 1499 {
mbed_official 354:e67efb2aab0e 1500 /* Set the key */
mbed_official 354:e67efb2aab0e 1501 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 1502
mbed_official 354:e67efb2aab0e 1503 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 1504 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 1505
mbed_official 354:e67efb2aab0e 1506 /* Set the CRYP peripheral in AES ECB decryption mode (with key derivation) */
mbed_official 354:e67efb2aab0e 1507 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_ECB_KEYDERDECRYPT);
mbed_official 354:e67efb2aab0e 1508
mbed_official 354:e67efb2aab0e 1509 /* Set the phase */
mbed_official 354:e67efb2aab0e 1510 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 1511 }
mbed_official 354:e67efb2aab0e 1512
mbed_official 354:e67efb2aab0e 1513 /* Set the input and output addresses and start DMA transfer */
mbed_official 354:e67efb2aab0e 1514 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
mbed_official 354:e67efb2aab0e 1515
mbed_official 354:e67efb2aab0e 1516 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 1517 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1518
mbed_official 354:e67efb2aab0e 1519 /* Return function status */
mbed_official 354:e67efb2aab0e 1520 return HAL_OK;
mbed_official 354:e67efb2aab0e 1521 }
mbed_official 354:e67efb2aab0e 1522 else
mbed_official 354:e67efb2aab0e 1523 {
mbed_official 354:e67efb2aab0e 1524 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1525 }
mbed_official 354:e67efb2aab0e 1526 }
mbed_official 354:e67efb2aab0e 1527
mbed_official 354:e67efb2aab0e 1528 /**
mbed_official 354:e67efb2aab0e 1529 * @brief Initializes the CRYP peripheral in AES CBC encryption mode using DMA.
mbed_official 354:e67efb2aab0e 1530 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1531 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1532 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1533 * @param Size: Length of the plaintext buffer, must be a multiple of 16 bytes
mbed_official 354:e67efb2aab0e 1534 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1535 * @retval HAL status
mbed_official 354:e67efb2aab0e 1536 */
mbed_official 354:e67efb2aab0e 1537 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
mbed_official 354:e67efb2aab0e 1538 {
mbed_official 354:e67efb2aab0e 1539 uint32_t inputaddr = 0, outputaddr = 0;
mbed_official 354:e67efb2aab0e 1540
mbed_official 354:e67efb2aab0e 1541 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 1542 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 1543 {
mbed_official 354:e67efb2aab0e 1544 /* Process Locked */
mbed_official 354:e67efb2aab0e 1545 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1546
mbed_official 354:e67efb2aab0e 1547 /* Return function status */
mbed_official 354:e67efb2aab0e 1548 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1549 }
mbed_official 354:e67efb2aab0e 1550
mbed_official 354:e67efb2aab0e 1551 /* Check if HAL_CRYP_Init has been called */
mbed_official 354:e67efb2aab0e 1552 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
mbed_official 354:e67efb2aab0e 1553 {
mbed_official 354:e67efb2aab0e 1554 /* Process Locked */
mbed_official 354:e67efb2aab0e 1555 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 1556
mbed_official 354:e67efb2aab0e 1557 inputaddr = (uint32_t)pCypherData;
mbed_official 354:e67efb2aab0e 1558 outputaddr = (uint32_t)pPlainData;
mbed_official 354:e67efb2aab0e 1559
mbed_official 354:e67efb2aab0e 1560 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 1561 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 1562
mbed_official 354:e67efb2aab0e 1563 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 1564 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 1565 {
mbed_official 354:e67efb2aab0e 1566 /* Set the key */
mbed_official 354:e67efb2aab0e 1567 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 1568
mbed_official 354:e67efb2aab0e 1569 /* Reset the CHMOD & MODE bits */
mbed_official 354:e67efb2aab0e 1570 CLEAR_BIT(AES->CR, CRYP_ALGO_CHAIN_MASK);
mbed_official 354:e67efb2aab0e 1571
mbed_official 354:e67efb2aab0e 1572 /* Set the CRYP peripheral in AES CBC decryption mode (with key derivation) */
mbed_official 354:e67efb2aab0e 1573 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CBC_KEYDERDECRYPT);
mbed_official 354:e67efb2aab0e 1574
mbed_official 354:e67efb2aab0e 1575 /* Set the Initialization Vector */
mbed_official 354:e67efb2aab0e 1576 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
mbed_official 354:e67efb2aab0e 1577
mbed_official 354:e67efb2aab0e 1578 /* Set the phase */
mbed_official 354:e67efb2aab0e 1579 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 1580 }
mbed_official 354:e67efb2aab0e 1581
mbed_official 354:e67efb2aab0e 1582 /* Set the input and output addresses and start DMA transfer */
mbed_official 354:e67efb2aab0e 1583 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
mbed_official 354:e67efb2aab0e 1584
mbed_official 354:e67efb2aab0e 1585 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 1586 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1587
mbed_official 354:e67efb2aab0e 1588 /* Return function status */
mbed_official 354:e67efb2aab0e 1589 return HAL_OK;
mbed_official 354:e67efb2aab0e 1590 }
mbed_official 354:e67efb2aab0e 1591 else
mbed_official 354:e67efb2aab0e 1592 {
mbed_official 354:e67efb2aab0e 1593 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1594 }
mbed_official 354:e67efb2aab0e 1595 }
mbed_official 354:e67efb2aab0e 1596
mbed_official 354:e67efb2aab0e 1597 /**
mbed_official 354:e67efb2aab0e 1598 * @brief Initializes the CRYP peripheral in AES CTR decryption mode using DMA.
mbed_official 354:e67efb2aab0e 1599 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1600 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1601 * @param pCypherData: Pointer to the cyphertext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1602 * @param Size: Length of the plaintext buffer, must be a multiple of 16
mbed_official 354:e67efb2aab0e 1603 * @param pPlainData: Pointer to the plaintext buffer (aligned on u32)
mbed_official 354:e67efb2aab0e 1604 * @retval HAL status
mbed_official 354:e67efb2aab0e 1605 */
mbed_official 354:e67efb2aab0e 1606 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
mbed_official 354:e67efb2aab0e 1607 {
mbed_official 354:e67efb2aab0e 1608 uint32_t inputaddr = 0, outputaddr = 0;
mbed_official 354:e67efb2aab0e 1609
mbed_official 354:e67efb2aab0e 1610 /* Check that data aligned on u32 */
mbed_official 354:e67efb2aab0e 1611 if((((uint32_t)pPlainData & (uint32_t)0x00000003) != 0) || (((uint32_t)pCypherData & (uint32_t)0x00000003) != 0) || ((Size & (uint16_t)0x000F) != 0))
mbed_official 354:e67efb2aab0e 1612 {
mbed_official 354:e67efb2aab0e 1613 /* Process Locked */
mbed_official 354:e67efb2aab0e 1614 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1615
mbed_official 354:e67efb2aab0e 1616 /* Return function status */
mbed_official 354:e67efb2aab0e 1617 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1618 }
mbed_official 354:e67efb2aab0e 1619
mbed_official 354:e67efb2aab0e 1620 /* Check if HAL_CRYP_Init has been called */
mbed_official 354:e67efb2aab0e 1621 if ((hcryp->State != HAL_CRYP_STATE_RESET) && (hcryp->State == HAL_CRYP_STATE_READY))
mbed_official 354:e67efb2aab0e 1622 {
mbed_official 354:e67efb2aab0e 1623 /* Process Locked */
mbed_official 354:e67efb2aab0e 1624 __HAL_LOCK(hcryp);
mbed_official 354:e67efb2aab0e 1625
mbed_official 354:e67efb2aab0e 1626 inputaddr = (uint32_t)pCypherData;
mbed_official 354:e67efb2aab0e 1627 outputaddr = (uint32_t)pPlainData;
mbed_official 354:e67efb2aab0e 1628
mbed_official 354:e67efb2aab0e 1629 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 1630 hcryp->State = HAL_CRYP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 1631
mbed_official 354:e67efb2aab0e 1632 /* Check if initialization phase has already been performed */
mbed_official 354:e67efb2aab0e 1633 if(hcryp->Phase == HAL_CRYP_PHASE_READY)
mbed_official 354:e67efb2aab0e 1634 {
mbed_official 354:e67efb2aab0e 1635 /* Set the key */
mbed_official 354:e67efb2aab0e 1636 CRYP_SetKey(hcryp, hcryp->Init.pKey);
mbed_official 354:e67efb2aab0e 1637
mbed_official 354:e67efb2aab0e 1638 /* Set the CRYP peripheral in AES CTR mode */
mbed_official 354:e67efb2aab0e 1639 __HAL_CRYP_SET_MODE(CRYP_CR_ALGOMODE_AES_CTR_DECRYPT);
mbed_official 354:e67efb2aab0e 1640
mbed_official 354:e67efb2aab0e 1641 /* Set the Initialization Vector */
mbed_official 354:e67efb2aab0e 1642 CRYP_SetInitVector(hcryp, hcryp->Init.pInitVect);
mbed_official 354:e67efb2aab0e 1643
mbed_official 354:e67efb2aab0e 1644 /* Set the phase */
mbed_official 354:e67efb2aab0e 1645 hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
mbed_official 354:e67efb2aab0e 1646 }
mbed_official 354:e67efb2aab0e 1647
mbed_official 354:e67efb2aab0e 1648 /* Set the input and output addresses and start DMA transfer */
mbed_official 354:e67efb2aab0e 1649 CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
mbed_official 354:e67efb2aab0e 1650
mbed_official 354:e67efb2aab0e 1651 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 1652 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1653
mbed_official 354:e67efb2aab0e 1654 /* Return function status */
mbed_official 354:e67efb2aab0e 1655 return HAL_OK;
mbed_official 354:e67efb2aab0e 1656 }
mbed_official 354:e67efb2aab0e 1657 else
mbed_official 354:e67efb2aab0e 1658 {
mbed_official 354:e67efb2aab0e 1659 return HAL_ERROR;
mbed_official 354:e67efb2aab0e 1660 }
mbed_official 354:e67efb2aab0e 1661 }
mbed_official 354:e67efb2aab0e 1662
mbed_official 354:e67efb2aab0e 1663 /**
mbed_official 354:e67efb2aab0e 1664 * @}
mbed_official 354:e67efb2aab0e 1665 */
mbed_official 354:e67efb2aab0e 1666
mbed_official 354:e67efb2aab0e 1667 /** @defgroup CRYP_Exported_Functions_Group3 DMA callback functions
mbed_official 354:e67efb2aab0e 1668 * @brief DMA callback functions.
mbed_official 354:e67efb2aab0e 1669 *
mbed_official 354:e67efb2aab0e 1670 @verbatim
mbed_official 354:e67efb2aab0e 1671 ==============================================================================
mbed_official 354:e67efb2aab0e 1672 ##### DMA callback functions #####
mbed_official 354:e67efb2aab0e 1673 ==============================================================================
mbed_official 354:e67efb2aab0e 1674 [..] This section provides DMA callback functions:
mbed_official 354:e67efb2aab0e 1675 (+) DMA Input data transfer complete
mbed_official 354:e67efb2aab0e 1676 (+) DMA Output data transfer complete
mbed_official 354:e67efb2aab0e 1677 (+) DMA error
mbed_official 354:e67efb2aab0e 1678
mbed_official 354:e67efb2aab0e 1679 @endverbatim
mbed_official 354:e67efb2aab0e 1680 * @{
mbed_official 354:e67efb2aab0e 1681 */
mbed_official 354:e67efb2aab0e 1682
mbed_official 354:e67efb2aab0e 1683 /**
mbed_official 354:e67efb2aab0e 1684 * @brief CRYP error callback.
mbed_official 354:e67efb2aab0e 1685 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1686 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1687 * @retval None
mbed_official 354:e67efb2aab0e 1688 */
mbed_official 354:e67efb2aab0e 1689 __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
mbed_official 354:e67efb2aab0e 1690 {
mbed_official 354:e67efb2aab0e 1691 /* NOTE : This function should not be modified; when the callback is needed,
mbed_official 354:e67efb2aab0e 1692 the HAL_CRYP_ErrorCallback can be implemented in the user file
mbed_official 354:e67efb2aab0e 1693 */
mbed_official 354:e67efb2aab0e 1694 }
mbed_official 354:e67efb2aab0e 1695
mbed_official 354:e67efb2aab0e 1696 /**
mbed_official 354:e67efb2aab0e 1697 * @brief Input transfer completed callback.
mbed_official 354:e67efb2aab0e 1698 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1699 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1700 * @retval None
mbed_official 354:e67efb2aab0e 1701 */
mbed_official 354:e67efb2aab0e 1702 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
mbed_official 354:e67efb2aab0e 1703 {
mbed_official 354:e67efb2aab0e 1704 /* NOTE : This function should not be modified; when the callback is needed,
mbed_official 354:e67efb2aab0e 1705 the HAL_CRYP_InCpltCallback can be implemented in the user file
mbed_official 354:e67efb2aab0e 1706 */
mbed_official 354:e67efb2aab0e 1707 }
mbed_official 354:e67efb2aab0e 1708
mbed_official 354:e67efb2aab0e 1709 /**
mbed_official 354:e67efb2aab0e 1710 * @brief Output transfer completed callback.
mbed_official 354:e67efb2aab0e 1711 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1712 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1713 * @retval None
mbed_official 354:e67efb2aab0e 1714 */
mbed_official 354:e67efb2aab0e 1715 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
mbed_official 354:e67efb2aab0e 1716 {
mbed_official 354:e67efb2aab0e 1717 /* NOTE : This function should not be modified; when the callback is needed,
mbed_official 354:e67efb2aab0e 1718 the HAL_CRYP_OutCpltCallback can be implemented in the user file
mbed_official 354:e67efb2aab0e 1719 */
mbed_official 354:e67efb2aab0e 1720 }
mbed_official 354:e67efb2aab0e 1721
mbed_official 354:e67efb2aab0e 1722 /**
mbed_official 354:e67efb2aab0e 1723 * @}
mbed_official 354:e67efb2aab0e 1724 */
mbed_official 354:e67efb2aab0e 1725
mbed_official 354:e67efb2aab0e 1726 /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
mbed_official 354:e67efb2aab0e 1727 * @brief CRYP IRQ handler.
mbed_official 354:e67efb2aab0e 1728 *
mbed_official 354:e67efb2aab0e 1729 @verbatim
mbed_official 354:e67efb2aab0e 1730 ==============================================================================
mbed_official 354:e67efb2aab0e 1731 ##### CRYP IRQ handler management #####
mbed_official 354:e67efb2aab0e 1732 ==============================================================================
mbed_official 354:e67efb2aab0e 1733 [..] This section provides CRYP IRQ handler function.
mbed_official 354:e67efb2aab0e 1734
mbed_official 354:e67efb2aab0e 1735 @endverbatim
mbed_official 354:e67efb2aab0e 1736 * @{
mbed_official 354:e67efb2aab0e 1737 */
mbed_official 354:e67efb2aab0e 1738
mbed_official 354:e67efb2aab0e 1739 /**
mbed_official 354:e67efb2aab0e 1740 * @brief This function handles CRYP interrupt request.
mbed_official 354:e67efb2aab0e 1741 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1742 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1743 * @retval None
mbed_official 354:e67efb2aab0e 1744 */
mbed_official 354:e67efb2aab0e 1745 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
mbed_official 354:e67efb2aab0e 1746 {
mbed_official 354:e67efb2aab0e 1747 /* Check if error occurred*/
mbed_official 354:e67efb2aab0e 1748 if (__HAL_CRYP_GET_IT_SOURCE(hcryp, AES_IT_ERR) != RESET)
mbed_official 354:e67efb2aab0e 1749 {
mbed_official 354:e67efb2aab0e 1750 if (__HAL_CRYP_GET_FLAG(AES_FLAG_RDERR) != RESET)
mbed_official 354:e67efb2aab0e 1751 {
mbed_official 354:e67efb2aab0e 1752 __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_RDERR);
mbed_official 354:e67efb2aab0e 1753 }
mbed_official 354:e67efb2aab0e 1754
mbed_official 354:e67efb2aab0e 1755 if (__HAL_CRYP_GET_FLAG(AES_FLAG_WRERR) != RESET)
mbed_official 354:e67efb2aab0e 1756 {
mbed_official 354:e67efb2aab0e 1757 __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_WRERR);
mbed_official 354:e67efb2aab0e 1758 }
mbed_official 354:e67efb2aab0e 1759
mbed_official 354:e67efb2aab0e 1760 if (__HAL_CRYP_GET_FLAG(AES_FLAG_CCF) != RESET)
mbed_official 354:e67efb2aab0e 1761 {
mbed_official 354:e67efb2aab0e 1762 __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_CCF);
mbed_official 354:e67efb2aab0e 1763 }
mbed_official 354:e67efb2aab0e 1764
mbed_official 354:e67efb2aab0e 1765 hcryp->State= HAL_CRYP_STATE_ERROR;
mbed_official 354:e67efb2aab0e 1766 /* Disable Computation Complete Interrupt */
mbed_official 354:e67efb2aab0e 1767 __HAL_CRYP_DISABLE_IT(AES_IT_CC);
mbed_official 354:e67efb2aab0e 1768 __HAL_CRYP_DISABLE_IT(AES_IT_ERR);
mbed_official 354:e67efb2aab0e 1769
mbed_official 354:e67efb2aab0e 1770 HAL_CRYP_ErrorCallback(hcryp);
mbed_official 354:e67efb2aab0e 1771
mbed_official 354:e67efb2aab0e 1772 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 1773 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1774
mbed_official 354:e67efb2aab0e 1775 return;
mbed_official 354:e67efb2aab0e 1776 }
mbed_official 354:e67efb2aab0e 1777
mbed_official 354:e67efb2aab0e 1778 /* Check if computation complete interrupt was enabled*/
mbed_official 354:e67efb2aab0e 1779 if (__HAL_CRYP_GET_IT_SOURCE(hcryp, AES_IT_CC) != RESET)
mbed_official 354:e67efb2aab0e 1780 {
mbed_official 354:e67efb2aab0e 1781 /* Clear CCF Flag */
mbed_official 354:e67efb2aab0e 1782 __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_CCF);
mbed_official 354:e67efb2aab0e 1783
mbed_official 354:e67efb2aab0e 1784 CRYP_EncryptDecrypt_IT(hcryp);
mbed_official 354:e67efb2aab0e 1785 }
mbed_official 354:e67efb2aab0e 1786 }
mbed_official 354:e67efb2aab0e 1787
mbed_official 354:e67efb2aab0e 1788 /**
mbed_official 354:e67efb2aab0e 1789 * @}
mbed_official 354:e67efb2aab0e 1790 */
mbed_official 354:e67efb2aab0e 1791
mbed_official 354:e67efb2aab0e 1792 /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
mbed_official 354:e67efb2aab0e 1793 * @brief Peripheral State functions.
mbed_official 354:e67efb2aab0e 1794 *
mbed_official 354:e67efb2aab0e 1795 @verbatim
mbed_official 354:e67efb2aab0e 1796 ==============================================================================
mbed_official 354:e67efb2aab0e 1797 ##### Peripheral State functions #####
mbed_official 354:e67efb2aab0e 1798 ==============================================================================
mbed_official 354:e67efb2aab0e 1799 [..]
mbed_official 354:e67efb2aab0e 1800 This subsection permits to get in run-time the status of the peripheral.
mbed_official 354:e67efb2aab0e 1801
mbed_official 354:e67efb2aab0e 1802 @endverbatim
mbed_official 354:e67efb2aab0e 1803 * @{
mbed_official 354:e67efb2aab0e 1804 */
mbed_official 354:e67efb2aab0e 1805
mbed_official 354:e67efb2aab0e 1806 /**
mbed_official 354:e67efb2aab0e 1807 * @brief Returns the CRYP state.
mbed_official 354:e67efb2aab0e 1808 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1809 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1810 * @retval HAL state
mbed_official 354:e67efb2aab0e 1811 */
mbed_official 354:e67efb2aab0e 1812 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
mbed_official 354:e67efb2aab0e 1813 {
mbed_official 354:e67efb2aab0e 1814 return hcryp->State;
mbed_official 354:e67efb2aab0e 1815 }
mbed_official 354:e67efb2aab0e 1816
mbed_official 354:e67efb2aab0e 1817 /**
mbed_official 354:e67efb2aab0e 1818 * @}
mbed_official 354:e67efb2aab0e 1819 */
mbed_official 354:e67efb2aab0e 1820
mbed_official 354:e67efb2aab0e 1821 /**
mbed_official 354:e67efb2aab0e 1822 * @}
mbed_official 354:e67efb2aab0e 1823 */
mbed_official 354:e67efb2aab0e 1824
mbed_official 354:e67efb2aab0e 1825 /** @addtogroup CRYP_Private_Functions
mbed_official 354:e67efb2aab0e 1826 * @{
mbed_official 354:e67efb2aab0e 1827 */
mbed_official 354:e67efb2aab0e 1828
mbed_official 354:e67efb2aab0e 1829 /**
mbed_official 354:e67efb2aab0e 1830 * @brief IT function called under interruption context to continue encryption or decryption
mbed_official 354:e67efb2aab0e 1831 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1832 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1833 * @retval HAL status
mbed_official 354:e67efb2aab0e 1834 */
mbed_official 354:e67efb2aab0e 1835 static HAL_StatusTypeDef CRYP_EncryptDecrypt_IT(CRYP_HandleTypeDef *hcryp)
mbed_official 354:e67efb2aab0e 1836 {
mbed_official 354:e67efb2aab0e 1837 uint32_t inputaddr = 0, outputaddr = 0;
mbed_official 354:e67efb2aab0e 1838
mbed_official 354:e67efb2aab0e 1839 /* Get the last Output data adress */
mbed_official 354:e67efb2aab0e 1840 outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
mbed_official 354:e67efb2aab0e 1841
mbed_official 354:e67efb2aab0e 1842 /* Read the Output block from the Output Register */
mbed_official 354:e67efb2aab0e 1843 *(uint32_t*)(outputaddr) = AES->DOUTR;
mbed_official 354:e67efb2aab0e 1844 outputaddr+=4;
mbed_official 354:e67efb2aab0e 1845 *(uint32_t*)(outputaddr) = AES->DOUTR;
mbed_official 354:e67efb2aab0e 1846 outputaddr+=4;
mbed_official 354:e67efb2aab0e 1847 *(uint32_t*)(outputaddr) = AES->DOUTR;
mbed_official 354:e67efb2aab0e 1848 outputaddr+=4;
mbed_official 354:e67efb2aab0e 1849 *(uint32_t*)(outputaddr) = AES->DOUTR;
mbed_official 354:e67efb2aab0e 1850
mbed_official 354:e67efb2aab0e 1851 hcryp->pCrypOutBuffPtr += 16;
mbed_official 354:e67efb2aab0e 1852 hcryp->CrypOutCount -= 16;
mbed_official 354:e67efb2aab0e 1853
mbed_official 354:e67efb2aab0e 1854 /* Check if all input text is encrypted or decrypted */
mbed_official 354:e67efb2aab0e 1855 if(hcryp->CrypOutCount == 0)
mbed_official 354:e67efb2aab0e 1856 {
mbed_official 354:e67efb2aab0e 1857 /* Disable Computation Complete Interrupt */
mbed_official 354:e67efb2aab0e 1858 __HAL_CRYP_DISABLE_IT(AES_IT_CC);
mbed_official 354:e67efb2aab0e 1859 __HAL_CRYP_DISABLE_IT(AES_IT_ERR);
mbed_official 354:e67efb2aab0e 1860
mbed_official 354:e67efb2aab0e 1861 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 1862 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 1863
mbed_official 354:e67efb2aab0e 1864 /* Change the CRYP state */
mbed_official 354:e67efb2aab0e 1865 hcryp->State = HAL_CRYP_STATE_READY;
mbed_official 354:e67efb2aab0e 1866
mbed_official 354:e67efb2aab0e 1867 /* Call computation complete callback */
mbed_official 354:e67efb2aab0e 1868 HAL_CRYPEx_ComputationCpltCallback(hcryp);
mbed_official 354:e67efb2aab0e 1869 }
mbed_official 354:e67efb2aab0e 1870 else /* Process the rest of input text */
mbed_official 354:e67efb2aab0e 1871 {
mbed_official 354:e67efb2aab0e 1872 /* Get the last Intput data adress */
mbed_official 354:e67efb2aab0e 1873 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
mbed_official 354:e67efb2aab0e 1874
mbed_official 354:e67efb2aab0e 1875 /* Write the Input block in the Data Input register */
mbed_official 354:e67efb2aab0e 1876 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1877 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1878 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1879 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1880 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1881 inputaddr+=4;
mbed_official 354:e67efb2aab0e 1882 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 1883 hcryp->pCrypInBuffPtr += 16;
mbed_official 354:e67efb2aab0e 1884 hcryp->CrypInCount -= 16;
mbed_official 354:e67efb2aab0e 1885 }
mbed_official 354:e67efb2aab0e 1886 return HAL_OK;
mbed_official 354:e67efb2aab0e 1887 }
mbed_official 354:e67efb2aab0e 1888 /**
mbed_official 354:e67efb2aab0e 1889 * @brief DMA CRYP Input Data process complete callback.
mbed_official 354:e67efb2aab0e 1890 * @param hdma: DMA handle
mbed_official 354:e67efb2aab0e 1891 * @retval None
mbed_official 354:e67efb2aab0e 1892 */
mbed_official 354:e67efb2aab0e 1893 static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
mbed_official 354:e67efb2aab0e 1894 {
mbed_official 354:e67efb2aab0e 1895 CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 354:e67efb2aab0e 1896
mbed_official 354:e67efb2aab0e 1897 /* Disable the DMA transfer for input request */
mbed_official 354:e67efb2aab0e 1898 CLEAR_BIT(AES->CR, AES_CR_DMAINEN);
mbed_official 354:e67efb2aab0e 1899
mbed_official 354:e67efb2aab0e 1900 /* Call input data transfer complete callback */
mbed_official 354:e67efb2aab0e 1901 HAL_CRYP_InCpltCallback(hcryp);
mbed_official 354:e67efb2aab0e 1902 }
mbed_official 354:e67efb2aab0e 1903
mbed_official 354:e67efb2aab0e 1904 /**
mbed_official 354:e67efb2aab0e 1905 * @brief DMA CRYP Output Data process complete callback.
mbed_official 354:e67efb2aab0e 1906 * @param hdma: DMA handle
mbed_official 354:e67efb2aab0e 1907 * @retval None
mbed_official 354:e67efb2aab0e 1908 */
mbed_official 354:e67efb2aab0e 1909 static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
mbed_official 354:e67efb2aab0e 1910 {
mbed_official 354:e67efb2aab0e 1911 CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 354:e67efb2aab0e 1912
mbed_official 354:e67efb2aab0e 1913 /* Disable the DMA transfer for output request by resetting the DMAOUTEN bit
mbed_official 354:e67efb2aab0e 1914 in the DMACR register */
mbed_official 354:e67efb2aab0e 1915 CLEAR_BIT(AES->CR, AES_CR_DMAOUTEN);
mbed_official 354:e67efb2aab0e 1916
mbed_official 354:e67efb2aab0e 1917 /* Clear CCF Flag */
mbed_official 354:e67efb2aab0e 1918 __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_CCF);
mbed_official 354:e67efb2aab0e 1919
mbed_official 354:e67efb2aab0e 1920 /* Disable CRYP */
mbed_official 354:e67efb2aab0e 1921 __HAL_CRYP_DISABLE();
mbed_official 354:e67efb2aab0e 1922
mbed_official 354:e67efb2aab0e 1923 /* Change the CRYP state to ready */
mbed_official 354:e67efb2aab0e 1924 hcryp->State = HAL_CRYP_STATE_READY;
mbed_official 354:e67efb2aab0e 1925
mbed_official 354:e67efb2aab0e 1926 /* Call output data transfer complete callback */
mbed_official 354:e67efb2aab0e 1927 HAL_CRYP_OutCpltCallback(hcryp);
mbed_official 354:e67efb2aab0e 1928 }
mbed_official 354:e67efb2aab0e 1929
mbed_official 354:e67efb2aab0e 1930 /**
mbed_official 354:e67efb2aab0e 1931 * @brief DMA CRYP communication error callback.
mbed_official 354:e67efb2aab0e 1932 * @param hdma: DMA handle
mbed_official 354:e67efb2aab0e 1933 * @retval None
mbed_official 354:e67efb2aab0e 1934 */
mbed_official 354:e67efb2aab0e 1935 static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
mbed_official 354:e67efb2aab0e 1936 {
mbed_official 354:e67efb2aab0e 1937 CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 354:e67efb2aab0e 1938 hcryp->State= HAL_CRYP_STATE_ERROR;
mbed_official 354:e67efb2aab0e 1939 HAL_CRYP_ErrorCallback(hcryp);
mbed_official 354:e67efb2aab0e 1940 }
mbed_official 354:e67efb2aab0e 1941
mbed_official 354:e67efb2aab0e 1942 /**
mbed_official 354:e67efb2aab0e 1943 * @brief Writes the Key in Key registers.
mbed_official 354:e67efb2aab0e 1944 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1945 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1946 * @param Key: Pointer to Key buffer
mbed_official 354:e67efb2aab0e 1947 * @note Key must be written as little endian.
mbed_official 354:e67efb2aab0e 1948 * If Key pointer points at address n,
mbed_official 354:e67efb2aab0e 1949 * n[15:0] contains key[96:127],
mbed_official 354:e67efb2aab0e 1950 * (n+4)[15:0] contains key[64:95],
mbed_official 354:e67efb2aab0e 1951 * (n+8)[15:0] contains key[32:63] and
mbed_official 354:e67efb2aab0e 1952 * (n+12)[15:0] contains key[0:31]
mbed_official 354:e67efb2aab0e 1953 * @retval None
mbed_official 354:e67efb2aab0e 1954 */
mbed_official 354:e67efb2aab0e 1955 static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key)
mbed_official 354:e67efb2aab0e 1956 {
mbed_official 354:e67efb2aab0e 1957 uint32_t keyaddr = (uint32_t)Key;
mbed_official 354:e67efb2aab0e 1958
mbed_official 354:e67efb2aab0e 1959 AES->KEYR3 = __REV(*(uint32_t*)(keyaddr));
mbed_official 354:e67efb2aab0e 1960 keyaddr+=4;
mbed_official 354:e67efb2aab0e 1961 AES->KEYR2 = __REV(*(uint32_t*)(keyaddr));
mbed_official 354:e67efb2aab0e 1962 keyaddr+=4;
mbed_official 354:e67efb2aab0e 1963 AES->KEYR1 = __REV(*(uint32_t*)(keyaddr));
mbed_official 354:e67efb2aab0e 1964 keyaddr+=4;
mbed_official 354:e67efb2aab0e 1965 AES->KEYR0 = __REV(*(uint32_t*)(keyaddr));
mbed_official 354:e67efb2aab0e 1966 }
mbed_official 354:e67efb2aab0e 1967
mbed_official 354:e67efb2aab0e 1968 /**
mbed_official 354:e67efb2aab0e 1969 * @brief Writes the InitVector/InitCounter in IV registers.
mbed_official 354:e67efb2aab0e 1970 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1971 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1972 * @param InitVector: Pointer to InitVector/InitCounter buffer
mbed_official 354:e67efb2aab0e 1973 * @note Init Vector must be written as little endian.
mbed_official 354:e67efb2aab0e 1974 * If Init Vector pointer points at address n,
mbed_official 354:e67efb2aab0e 1975 * n[15:0] contains Vector[96:127],
mbed_official 354:e67efb2aab0e 1976 * (n+4)[15:0] contains Vector[64:95],
mbed_official 354:e67efb2aab0e 1977 * (n+8)[15:0] contains Vector[32:63] and
mbed_official 354:e67efb2aab0e 1978 * (n+12)[15:0] contains Vector[0:31]
mbed_official 354:e67efb2aab0e 1979 * @retval None
mbed_official 354:e67efb2aab0e 1980 */
mbed_official 354:e67efb2aab0e 1981 static void CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector)
mbed_official 354:e67efb2aab0e 1982 {
mbed_official 354:e67efb2aab0e 1983 uint32_t ivaddr = (uint32_t)InitVector;
mbed_official 354:e67efb2aab0e 1984
mbed_official 354:e67efb2aab0e 1985 AES->IVR3 = __REV(*(uint32_t*)(ivaddr));
mbed_official 354:e67efb2aab0e 1986 ivaddr+=4;
mbed_official 354:e67efb2aab0e 1987 AES->IVR2 = __REV(*(uint32_t*)(ivaddr));
mbed_official 354:e67efb2aab0e 1988 ivaddr+=4;
mbed_official 354:e67efb2aab0e 1989 AES->IVR1 = __REV(*(uint32_t*)(ivaddr));
mbed_official 354:e67efb2aab0e 1990 ivaddr+=4;
mbed_official 354:e67efb2aab0e 1991 AES->IVR0 = __REV(*(uint32_t*)(ivaddr));
mbed_official 354:e67efb2aab0e 1992 }
mbed_official 354:e67efb2aab0e 1993
mbed_official 354:e67efb2aab0e 1994 /**
mbed_official 354:e67efb2aab0e 1995 * @brief Process Data: Writes Input data in polling mode and reads the output data
mbed_official 354:e67efb2aab0e 1996 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 1997 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 1998 * @param Input: Pointer to the Input buffer
mbed_official 354:e67efb2aab0e 1999 * @param Ilength: Length of the Input buffer, must be a multiple of 16.
mbed_official 354:e67efb2aab0e 2000 * @param Output: Pointer to the returned buffer
mbed_official 354:e67efb2aab0e 2001 * @param Timeout: Specify Timeout value
mbed_official 354:e67efb2aab0e 2002 * @retval None
mbed_official 354:e67efb2aab0e 2003 */
mbed_official 354:e67efb2aab0e 2004 static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
mbed_official 354:e67efb2aab0e 2005 {
mbed_official 354:e67efb2aab0e 2006 uint32_t tickstart = 0;
mbed_official 354:e67efb2aab0e 2007
mbed_official 354:e67efb2aab0e 2008 uint32_t index = 0;
mbed_official 354:e67efb2aab0e 2009 uint32_t inputaddr = (uint32_t)Input;
mbed_official 354:e67efb2aab0e 2010 uint32_t outputaddr = (uint32_t)Output;
mbed_official 354:e67efb2aab0e 2011
mbed_official 354:e67efb2aab0e 2012 for(index=0; (index < Ilength); index += 16)
mbed_official 354:e67efb2aab0e 2013 {
mbed_official 354:e67efb2aab0e 2014 /* Write the Input block in the Data Input register */
mbed_official 354:e67efb2aab0e 2015 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 2016 inputaddr+=4;
mbed_official 354:e67efb2aab0e 2017 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 2018 inputaddr+=4;
mbed_official 354:e67efb2aab0e 2019 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 2020 inputaddr+=4;
mbed_official 354:e67efb2aab0e 2021 AES->DINR = *(uint32_t*)(inputaddr);
mbed_official 354:e67efb2aab0e 2022 inputaddr+=4;
mbed_official 354:e67efb2aab0e 2023
mbed_official 354:e67efb2aab0e 2024 /* Get timeout */
mbed_official 354:e67efb2aab0e 2025 tickstart = HAL_GetTick();
mbed_official 354:e67efb2aab0e 2026
mbed_official 354:e67efb2aab0e 2027 while(HAL_IS_BIT_CLR(AES->SR, AES_SR_CCF))
mbed_official 354:e67efb2aab0e 2028 {
mbed_official 354:e67efb2aab0e 2029 /* Check for the Timeout */
mbed_official 354:e67efb2aab0e 2030 if(Timeout != HAL_MAX_DELAY)
mbed_official 354:e67efb2aab0e 2031 {
mbed_official 354:e67efb2aab0e 2032 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
mbed_official 354:e67efb2aab0e 2033 {
mbed_official 354:e67efb2aab0e 2034 /* Change state */
mbed_official 354:e67efb2aab0e 2035 hcryp->State = HAL_CRYP_STATE_TIMEOUT;
mbed_official 354:e67efb2aab0e 2036
mbed_official 354:e67efb2aab0e 2037 /* Process Unlocked */
mbed_official 354:e67efb2aab0e 2038 __HAL_UNLOCK(hcryp);
mbed_official 354:e67efb2aab0e 2039
mbed_official 354:e67efb2aab0e 2040 return HAL_TIMEOUT;
mbed_official 354:e67efb2aab0e 2041 }
mbed_official 354:e67efb2aab0e 2042 }
mbed_official 354:e67efb2aab0e 2043 }
mbed_official 354:e67efb2aab0e 2044 /* Clear CCF Flag */
mbed_official 354:e67efb2aab0e 2045 __HAL_CRYP_CLEAR_FLAG(hcryp, AES_CLEARFLAG_CCF);
mbed_official 354:e67efb2aab0e 2046
mbed_official 354:e67efb2aab0e 2047 /* Read the Output block from the Data Output Register */
mbed_official 354:e67efb2aab0e 2048 *(uint32_t*)(outputaddr) = AES->DOUTR;
mbed_official 354:e67efb2aab0e 2049 outputaddr+=4;
mbed_official 354:e67efb2aab0e 2050 *(uint32_t*)(outputaddr) = AES->DOUTR;
mbed_official 354:e67efb2aab0e 2051 outputaddr+=4;
mbed_official 354:e67efb2aab0e 2052 *(uint32_t*)(outputaddr) = AES->DOUTR;
mbed_official 354:e67efb2aab0e 2053 outputaddr+=4;
mbed_official 354:e67efb2aab0e 2054 *(uint32_t*)(outputaddr) = AES->DOUTR;
mbed_official 354:e67efb2aab0e 2055 outputaddr+=4;
mbed_official 354:e67efb2aab0e 2056 }
mbed_official 354:e67efb2aab0e 2057 /* Return function status */
mbed_official 354:e67efb2aab0e 2058 return HAL_OK;
mbed_official 354:e67efb2aab0e 2059 }
mbed_official 354:e67efb2aab0e 2060
mbed_official 354:e67efb2aab0e 2061 /**
mbed_official 354:e67efb2aab0e 2062 * @brief Set the DMA configuration and start the DMA transfer
mbed_official 354:e67efb2aab0e 2063 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
mbed_official 354:e67efb2aab0e 2064 * the configuration information for CRYP module
mbed_official 354:e67efb2aab0e 2065 * @param inputaddr: address of the Input buffer
mbed_official 354:e67efb2aab0e 2066 * @param Size: Size of the Input buffer, must be a multiple of 16.
mbed_official 354:e67efb2aab0e 2067 * @param outputaddr: address of the Output buffer
mbed_official 354:e67efb2aab0e 2068 * @retval None
mbed_official 354:e67efb2aab0e 2069 */
mbed_official 354:e67efb2aab0e 2070 static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
mbed_official 354:e67efb2aab0e 2071 {
mbed_official 354:e67efb2aab0e 2072 /* Set the CRYP DMA transfer complete callback */
mbed_official 354:e67efb2aab0e 2073 hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
mbed_official 354:e67efb2aab0e 2074 /* Set the DMA error callback */
mbed_official 354:e67efb2aab0e 2075 hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
mbed_official 354:e67efb2aab0e 2076
mbed_official 354:e67efb2aab0e 2077 /* Set the CRYP DMA transfer complete callback */
mbed_official 354:e67efb2aab0e 2078 hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
mbed_official 354:e67efb2aab0e 2079 /* Set the DMA error callback */
mbed_official 354:e67efb2aab0e 2080 hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
mbed_official 354:e67efb2aab0e 2081
mbed_official 354:e67efb2aab0e 2082 /* Enable the DMA In DMA Stream */
mbed_official 354:e67efb2aab0e 2083 HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&AES->DINR, Size/4);
mbed_official 354:e67efb2aab0e 2084
mbed_official 354:e67efb2aab0e 2085 /* Enable the DMA Out DMA Stream */
mbed_official 354:e67efb2aab0e 2086 HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&AES->DOUTR, outputaddr, Size/4);
mbed_official 354:e67efb2aab0e 2087
mbed_official 354:e67efb2aab0e 2088 /* Enable In and Out DMA requests */
mbed_official 354:e67efb2aab0e 2089 SET_BIT(AES->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN));
mbed_official 354:e67efb2aab0e 2090
mbed_official 354:e67efb2aab0e 2091 /* Enable CRYP */
mbed_official 354:e67efb2aab0e 2092 __HAL_CRYP_ENABLE();
mbed_official 354:e67efb2aab0e 2093 }
mbed_official 354:e67efb2aab0e 2094
mbed_official 354:e67efb2aab0e 2095 /**
mbed_official 354:e67efb2aab0e 2096 * @}
mbed_official 354:e67efb2aab0e 2097 */
mbed_official 354:e67efb2aab0e 2098
mbed_official 354:e67efb2aab0e 2099 #endif /* STM32L162xC || STM32L162xCA || STM32L162xD || STM32L162xE*/
mbed_official 354:e67efb2aab0e 2100
mbed_official 354:e67efb2aab0e 2101 /**
mbed_official 354:e67efb2aab0e 2102 * @}
mbed_official 354:e67efb2aab0e 2103 */
mbed_official 354:e67efb2aab0e 2104
mbed_official 354:e67efb2aab0e 2105 /**
mbed_official 354:e67efb2aab0e 2106 * @}
mbed_official 354:e67efb2aab0e 2107 */
mbed_official 354:e67efb2aab0e 2108
mbed_official 354:e67efb2aab0e 2109 #endif /* HAL_CRYP_MODULE_ENABLED */
mbed_official 354:e67efb2aab0e 2110
mbed_official 354:e67efb2aab0e 2111 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/