mbed library sources
Fork of mbed-src by
targets/cmsis/TARGET_STM/TARGET_STM32L4/stm32l4xx_hal_cryp_ex.c@637:ed69428d4850, 2015-12-22 (annotated)
- Committer:
- jaerts
- Date:
- Tue Dec 22 13:22:16 2015 +0000
- Revision:
- 637:ed69428d4850
- Parent:
- 610:813dcc80987e
Add very shady LPC1768 CAN Filter implementation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 610:813dcc80987e | 1 | /** |
mbed_official | 610:813dcc80987e | 2 | ****************************************************************************** |
mbed_official | 610:813dcc80987e | 3 | * @file stm32l4xx_hal_cryp_ex.c |
mbed_official | 610:813dcc80987e | 4 | * @author MCD Application Team |
mbed_official | 610:813dcc80987e | 5 | * @version V1.0.0 |
mbed_official | 610:813dcc80987e | 6 | * @date 26-June-2015 |
mbed_official | 610:813dcc80987e | 7 | * @brief CRYPEx HAL module driver. |
mbed_official | 610:813dcc80987e | 8 | * This file provides firmware functions to manage the extended |
mbed_official | 610:813dcc80987e | 9 | * functionalities of the Cryptography (CRYP) peripheral. |
mbed_official | 610:813dcc80987e | 10 | * |
mbed_official | 610:813dcc80987e | 11 | ****************************************************************************** |
mbed_official | 610:813dcc80987e | 12 | * @attention |
mbed_official | 610:813dcc80987e | 13 | * |
mbed_official | 610:813dcc80987e | 14 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
mbed_official | 610:813dcc80987e | 15 | * |
mbed_official | 610:813dcc80987e | 16 | * Redistribution and use in source and binary forms, with or without modification, |
mbed_official | 610:813dcc80987e | 17 | * are permitted provided that the following conditions are met: |
mbed_official | 610:813dcc80987e | 18 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 610:813dcc80987e | 19 | * this list of conditions and the following disclaimer. |
mbed_official | 610:813dcc80987e | 20 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 610:813dcc80987e | 21 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 610:813dcc80987e | 22 | * and/or other materials provided with the distribution. |
mbed_official | 610:813dcc80987e | 23 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 610:813dcc80987e | 24 | * may be used to endorse or promote products derived from this software |
mbed_official | 610:813dcc80987e | 25 | * without specific prior written permission. |
mbed_official | 610:813dcc80987e | 26 | * |
mbed_official | 610:813dcc80987e | 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 610:813dcc80987e | 28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 610:813dcc80987e | 29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 610:813dcc80987e | 30 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 610:813dcc80987e | 31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 610:813dcc80987e | 32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 610:813dcc80987e | 33 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 610:813dcc80987e | 34 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 610:813dcc80987e | 35 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 610:813dcc80987e | 36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 610:813dcc80987e | 37 | * |
mbed_official | 610:813dcc80987e | 38 | ****************************************************************************** |
mbed_official | 610:813dcc80987e | 39 | */ |
mbed_official | 610:813dcc80987e | 40 | |
mbed_official | 610:813dcc80987e | 41 | /* Includes ------------------------------------------------------------------*/ |
mbed_official | 610:813dcc80987e | 42 | #include "stm32l4xx_hal.h" |
mbed_official | 610:813dcc80987e | 43 | |
mbed_official | 610:813dcc80987e | 44 | #ifdef HAL_CRYP_MODULE_ENABLED |
mbed_official | 610:813dcc80987e | 45 | |
mbed_official | 610:813dcc80987e | 46 | #if defined(STM32L485xx) || defined(STM32L486xx) |
mbed_official | 610:813dcc80987e | 47 | |
mbed_official | 610:813dcc80987e | 48 | /** @addtogroup STM32L4xx_HAL_Driver |
mbed_official | 610:813dcc80987e | 49 | * @{ |
mbed_official | 610:813dcc80987e | 50 | */ |
mbed_official | 610:813dcc80987e | 51 | |
mbed_official | 610:813dcc80987e | 52 | /** @defgroup CRYPEx CRYPEx |
mbed_official | 610:813dcc80987e | 53 | * @brief CRYP Extended HAL module driver |
mbed_official | 610:813dcc80987e | 54 | * @{ |
mbed_official | 610:813dcc80987e | 55 | */ |
mbed_official | 610:813dcc80987e | 56 | |
mbed_official | 610:813dcc80987e | 57 | /* Private typedef -----------------------------------------------------------*/ |
mbed_official | 610:813dcc80987e | 58 | /* Private define ------------------------------------------------------------*/ |
mbed_official | 610:813dcc80987e | 59 | /** @defgroup CRYPEx_Private_Constants CRYPEx Private Constants |
mbed_official | 610:813dcc80987e | 60 | * @{ |
mbed_official | 610:813dcc80987e | 61 | */ |
mbed_official | 610:813dcc80987e | 62 | #define CRYP_CCF_TIMEOUTVALUE 22000 /*!< CCF flag raising time-out value */ |
mbed_official | 610:813dcc80987e | 63 | #define CRYP_BUSY_TIMEOUTVALUE 22000 /*!< BUSY flag reset time-out value */ |
mbed_official | 610:813dcc80987e | 64 | /** |
mbed_official | 610:813dcc80987e | 65 | * @} |
mbed_official | 610:813dcc80987e | 66 | */ |
mbed_official | 610:813dcc80987e | 67 | |
mbed_official | 610:813dcc80987e | 68 | /* Private macro -------------------------------------------------------------*/ |
mbed_official | 610:813dcc80987e | 69 | /* Private variables ---------------------------------------------------------*/ |
mbed_official | 610:813dcc80987e | 70 | /* Private function prototypes -----------------------------------------------*/ |
mbed_official | 610:813dcc80987e | 71 | /** @defgroup CRYPEx_Private_Functions CRYPEx Private Functions |
mbed_official | 610:813dcc80987e | 72 | * @{ |
mbed_official | 610:813dcc80987e | 73 | */ |
mbed_official | 610:813dcc80987e | 74 | static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout); |
mbed_official | 610:813dcc80987e | 75 | static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout); |
mbed_official | 610:813dcc80987e | 76 | static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr); |
mbed_official | 610:813dcc80987e | 77 | static void CRYP_GCMCMAC_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr); |
mbed_official | 610:813dcc80987e | 78 | static void CRYP_GCMCMAC_DMAInCplt(DMA_HandleTypeDef *hdma); |
mbed_official | 610:813dcc80987e | 79 | static void CRYP_GCMCMAC_DMAError(DMA_HandleTypeDef *hdma); |
mbed_official | 610:813dcc80987e | 80 | static void CRYP_GCMCMAC_DMAOutCplt(DMA_HandleTypeDef *hdma); |
mbed_official | 610:813dcc80987e | 81 | static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); |
mbed_official | 610:813dcc80987e | 82 | static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef *hcryp, uint32_t Timeout); |
mbed_official | 610:813dcc80987e | 83 | static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma); |
mbed_official | 610:813dcc80987e | 84 | static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma); |
mbed_official | 610:813dcc80987e | 85 | static void CRYP_DMAError(DMA_HandleTypeDef *hdma); |
mbed_official | 610:813dcc80987e | 86 | /** |
mbed_official | 610:813dcc80987e | 87 | * @} |
mbed_official | 610:813dcc80987e | 88 | */ |
mbed_official | 610:813dcc80987e | 89 | |
mbed_official | 610:813dcc80987e | 90 | /* Exported functions ---------------------------------------------------------*/ |
mbed_official | 610:813dcc80987e | 91 | |
mbed_official | 610:813dcc80987e | 92 | /** @defgroup CRYPEx_Exported_Functions CRYPEx Exported Functions |
mbed_official | 610:813dcc80987e | 93 | * @{ |
mbed_official | 610:813dcc80987e | 94 | */ |
mbed_official | 610:813dcc80987e | 95 | |
mbed_official | 610:813dcc80987e | 96 | |
mbed_official | 610:813dcc80987e | 97 | /** @defgroup CRYPEx_Exported_Functions_Group1 Extended callback function |
mbed_official | 610:813dcc80987e | 98 | * @brief Extended callback functions. |
mbed_official | 610:813dcc80987e | 99 | * |
mbed_official | 610:813dcc80987e | 100 | @verbatim |
mbed_official | 610:813dcc80987e | 101 | =============================================================================== |
mbed_official | 610:813dcc80987e | 102 | ##### Extended callback functions ##### |
mbed_official | 610:813dcc80987e | 103 | =============================================================================== |
mbed_official | 610:813dcc80987e | 104 | [..] This section provides callback function: |
mbed_official | 610:813dcc80987e | 105 | (+) Computation completed. |
mbed_official | 610:813dcc80987e | 106 | |
mbed_official | 610:813dcc80987e | 107 | @endverbatim |
mbed_official | 610:813dcc80987e | 108 | * @{ |
mbed_official | 610:813dcc80987e | 109 | */ |
mbed_official | 610:813dcc80987e | 110 | |
mbed_official | 610:813dcc80987e | 111 | |
mbed_official | 610:813dcc80987e | 112 | /** |
mbed_official | 610:813dcc80987e | 113 | * @brief Computation completed callbacks. |
mbed_official | 610:813dcc80987e | 114 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 115 | * the configuration information for CRYP module |
mbed_official | 610:813dcc80987e | 116 | * @retval None |
mbed_official | 610:813dcc80987e | 117 | */ |
mbed_official | 610:813dcc80987e | 118 | __weak void HAL_CRYPEx_ComputationCpltCallback(CRYP_HandleTypeDef *hcryp) |
mbed_official | 610:813dcc80987e | 119 | { |
mbed_official | 610:813dcc80987e | 120 | /* NOTE : This function should not be modified; when the callback is needed, |
mbed_official | 610:813dcc80987e | 121 | the HAL_CRYP_ErrorCallback can be implemented in the user file |
mbed_official | 610:813dcc80987e | 122 | */ |
mbed_official | 610:813dcc80987e | 123 | } |
mbed_official | 610:813dcc80987e | 124 | |
mbed_official | 610:813dcc80987e | 125 | /** |
mbed_official | 610:813dcc80987e | 126 | * @} |
mbed_official | 610:813dcc80987e | 127 | */ |
mbed_official | 610:813dcc80987e | 128 | |
mbed_official | 610:813dcc80987e | 129 | /** @defgroup CRYPEx_Exported_Functions_Group2 AES extended processing functions |
mbed_official | 610:813dcc80987e | 130 | * @brief Extended processing functions. |
mbed_official | 610:813dcc80987e | 131 | * |
mbed_official | 610:813dcc80987e | 132 | @verbatim |
mbed_official | 610:813dcc80987e | 133 | ============================================================================== |
mbed_official | 610:813dcc80987e | 134 | ##### AES extended processing functions ##### |
mbed_official | 610:813dcc80987e | 135 | ============================================================================== |
mbed_official | 610:813dcc80987e | 136 | [..] This section provides functions allowing to: |
mbed_official | 610:813dcc80987e | 137 | (+) Encrypt plaintext or decrypt cipher text using AES algorithm in different chaining modes. |
mbed_official | 610:813dcc80987e | 138 | Functions are generic (handles ECB, CBC and CTR and all modes) and are only differentiated |
mbed_official | 610:813dcc80987e | 139 | based on the processing type. Three processing types are available: |
mbed_official | 610:813dcc80987e | 140 | (++) Polling mode |
mbed_official | 610:813dcc80987e | 141 | (++) Interrupt mode |
mbed_official | 610:813dcc80987e | 142 | (++) DMA mode |
mbed_official | 610:813dcc80987e | 143 | (+) Generate and authentication tag in addition to encrypt/decrypt a plain/cipher text using AES |
mbed_official | 610:813dcc80987e | 144 | algorithm in different chaining modes. |
mbed_official | 610:813dcc80987e | 145 | Functions are generic (handles GCM, GMAC and CMAC) and process only one phase so that steps |
mbed_official | 610:813dcc80987e | 146 | can be skipped if so required. Functions are only differentiated based on the processing type. |
mbed_official | 610:813dcc80987e | 147 | Three processing types are available: |
mbed_official | 610:813dcc80987e | 148 | (++) Polling mode |
mbed_official | 610:813dcc80987e | 149 | (++) Interrupt mode |
mbed_official | 610:813dcc80987e | 150 | (++) DMA mode |
mbed_official | 610:813dcc80987e | 151 | |
mbed_official | 610:813dcc80987e | 152 | @endverbatim |
mbed_official | 610:813dcc80987e | 153 | * @{ |
mbed_official | 610:813dcc80987e | 154 | */ |
mbed_official | 610:813dcc80987e | 155 | |
mbed_official | 610:813dcc80987e | 156 | /** |
mbed_official | 610:813dcc80987e | 157 | * @brief Carry out in polling mode the ciphering or deciphering operation according to |
mbed_official | 610:813dcc80987e | 158 | * hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and |
mbed_official | 610:813dcc80987e | 159 | * chaining modes ECB, CBC and CTR are managed by this function in polling mode. |
mbed_official | 610:813dcc80987e | 160 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 161 | * the configuration information for CRYP module |
mbed_official | 610:813dcc80987e | 162 | * @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption |
mbed_official | 610:813dcc80987e | 163 | * or key derivation+decryption. |
mbed_official | 610:813dcc80987e | 164 | * Parameter is meaningless in case of key derivation. |
mbed_official | 610:813dcc80987e | 165 | * @param Size: Length of the input data buffer in bytes, must be a multiple of 16. |
mbed_official | 610:813dcc80987e | 166 | * Parameter is meaningless in case of key derivation. |
mbed_official | 610:813dcc80987e | 167 | * @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of |
mbed_official | 610:813dcc80987e | 168 | * decryption/key derivation+decryption, or pointer to the derivative keys in |
mbed_official | 610:813dcc80987e | 169 | * case of key derivation only. |
mbed_official | 610:813dcc80987e | 170 | * @param Timeout: Specify Timeout value |
mbed_official | 610:813dcc80987e | 171 | * @retval HAL status |
mbed_official | 610:813dcc80987e | 172 | */ |
mbed_official | 610:813dcc80987e | 173 | HAL_StatusTypeDef HAL_CRYPEx_AES(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData, uint32_t Timeout) |
mbed_official | 610:813dcc80987e | 174 | { |
mbed_official | 610:813dcc80987e | 175 | |
mbed_official | 610:813dcc80987e | 176 | if (hcryp->State == HAL_CRYP_STATE_READY) |
mbed_official | 610:813dcc80987e | 177 | { |
mbed_official | 610:813dcc80987e | 178 | /* Check parameters setting */ |
mbed_official | 610:813dcc80987e | 179 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
mbed_official | 610:813dcc80987e | 180 | { |
mbed_official | 610:813dcc80987e | 181 | if (pOutputData == NULL) |
mbed_official | 610:813dcc80987e | 182 | { |
mbed_official | 610:813dcc80987e | 183 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 184 | } |
mbed_official | 610:813dcc80987e | 185 | } |
mbed_official | 610:813dcc80987e | 186 | else |
mbed_official | 610:813dcc80987e | 187 | { |
mbed_official | 610:813dcc80987e | 188 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0)) |
mbed_official | 610:813dcc80987e | 189 | { |
mbed_official | 610:813dcc80987e | 190 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 191 | } |
mbed_official | 610:813dcc80987e | 192 | } |
mbed_official | 610:813dcc80987e | 193 | |
mbed_official | 610:813dcc80987e | 194 | /* Process Locked */ |
mbed_official | 610:813dcc80987e | 195 | __HAL_LOCK(hcryp); |
mbed_official | 610:813dcc80987e | 196 | |
mbed_official | 610:813dcc80987e | 197 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 198 | hcryp->State = HAL_CRYP_STATE_BUSY; |
mbed_official | 610:813dcc80987e | 199 | |
mbed_official | 610:813dcc80987e | 200 | /* Call CRYP_ReadKey() API if the operating mode is set to |
mbed_official | 610:813dcc80987e | 201 | key derivation, CRYP_ProcessData() otherwise */ |
mbed_official | 610:813dcc80987e | 202 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
mbed_official | 610:813dcc80987e | 203 | { |
mbed_official | 610:813dcc80987e | 204 | if(CRYP_ReadKey(hcryp, pOutputData, Timeout) != HAL_OK) |
mbed_official | 610:813dcc80987e | 205 | { |
mbed_official | 610:813dcc80987e | 206 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 207 | } |
mbed_official | 610:813dcc80987e | 208 | } |
mbed_official | 610:813dcc80987e | 209 | else |
mbed_official | 610:813dcc80987e | 210 | { |
mbed_official | 610:813dcc80987e | 211 | if(CRYP_ProcessData(hcryp, pInputData, Size, pOutputData, Timeout) != HAL_OK) |
mbed_official | 610:813dcc80987e | 212 | { |
mbed_official | 610:813dcc80987e | 213 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 214 | } |
mbed_official | 610:813dcc80987e | 215 | } |
mbed_official | 610:813dcc80987e | 216 | |
mbed_official | 610:813dcc80987e | 217 | /* If the state has not been set to SUSPENDED, set it to |
mbed_official | 610:813dcc80987e | 218 | READY, otherwise keep it as it is */ |
mbed_official | 610:813dcc80987e | 219 | if (hcryp->State != HAL_CRYP_STATE_SUSPENDED) |
mbed_official | 610:813dcc80987e | 220 | { |
mbed_official | 610:813dcc80987e | 221 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 222 | } |
mbed_official | 610:813dcc80987e | 223 | |
mbed_official | 610:813dcc80987e | 224 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 225 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 226 | |
mbed_official | 610:813dcc80987e | 227 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 228 | } |
mbed_official | 610:813dcc80987e | 229 | else |
mbed_official | 610:813dcc80987e | 230 | { |
mbed_official | 610:813dcc80987e | 231 | return HAL_BUSY; |
mbed_official | 610:813dcc80987e | 232 | } |
mbed_official | 610:813dcc80987e | 233 | } |
mbed_official | 610:813dcc80987e | 234 | |
mbed_official | 610:813dcc80987e | 235 | |
mbed_official | 610:813dcc80987e | 236 | |
mbed_official | 610:813dcc80987e | 237 | /** |
mbed_official | 610:813dcc80987e | 238 | * @brief Carry out in interrupt mode the ciphering or deciphering operation according to |
mbed_official | 610:813dcc80987e | 239 | * hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and |
mbed_official | 610:813dcc80987e | 240 | * chaining modes ECB, CBC and CTR are managed by this function in interrupt mode. |
mbed_official | 610:813dcc80987e | 241 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 242 | * the configuration information for CRYP module |
mbed_official | 610:813dcc80987e | 243 | * @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption |
mbed_official | 610:813dcc80987e | 244 | * or key derivation+decryption. |
mbed_official | 610:813dcc80987e | 245 | * Parameter is meaningless in case of key derivation. |
mbed_official | 610:813dcc80987e | 246 | * @param Size: Length of the input data buffer in bytes, must be a multiple of 16. |
mbed_official | 610:813dcc80987e | 247 | * Parameter is meaningless in case of key derivation. |
mbed_official | 610:813dcc80987e | 248 | * @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of |
mbed_official | 610:813dcc80987e | 249 | * decryption/key derivation+decryption, or pointer to the derivative keys in |
mbed_official | 610:813dcc80987e | 250 | * case of key derivation only. |
mbed_official | 610:813dcc80987e | 251 | * @retval HAL status |
mbed_official | 610:813dcc80987e | 252 | */ |
mbed_official | 610:813dcc80987e | 253 | HAL_StatusTypeDef HAL_CRYPEx_AES_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData) |
mbed_official | 610:813dcc80987e | 254 | { |
mbed_official | 610:813dcc80987e | 255 | uint32_t inputaddr = 0; |
mbed_official | 610:813dcc80987e | 256 | |
mbed_official | 610:813dcc80987e | 257 | if(hcryp->State == HAL_CRYP_STATE_READY) |
mbed_official | 610:813dcc80987e | 258 | { |
mbed_official | 610:813dcc80987e | 259 | /* Check parameters setting */ |
mbed_official | 610:813dcc80987e | 260 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
mbed_official | 610:813dcc80987e | 261 | { |
mbed_official | 610:813dcc80987e | 262 | if (pOutputData == NULL) |
mbed_official | 610:813dcc80987e | 263 | { |
mbed_official | 610:813dcc80987e | 264 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 265 | } |
mbed_official | 610:813dcc80987e | 266 | } |
mbed_official | 610:813dcc80987e | 267 | else |
mbed_official | 610:813dcc80987e | 268 | { |
mbed_official | 610:813dcc80987e | 269 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0)) |
mbed_official | 610:813dcc80987e | 270 | { |
mbed_official | 610:813dcc80987e | 271 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 272 | } |
mbed_official | 610:813dcc80987e | 273 | } |
mbed_official | 610:813dcc80987e | 274 | /* Process Locked */ |
mbed_official | 610:813dcc80987e | 275 | __HAL_LOCK(hcryp); |
mbed_official | 610:813dcc80987e | 276 | |
mbed_official | 610:813dcc80987e | 277 | /* If operating mode is not limited to key derivation only, |
mbed_official | 610:813dcc80987e | 278 | get the buffers addresses and sizes */ |
mbed_official | 610:813dcc80987e | 279 | if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION) |
mbed_official | 610:813dcc80987e | 280 | { |
mbed_official | 610:813dcc80987e | 281 | |
mbed_official | 610:813dcc80987e | 282 | hcryp->CrypInCount = Size; |
mbed_official | 610:813dcc80987e | 283 | hcryp->pCrypInBuffPtr = pInputData; |
mbed_official | 610:813dcc80987e | 284 | hcryp->pCrypOutBuffPtr = pOutputData; |
mbed_official | 610:813dcc80987e | 285 | hcryp->CrypOutCount = Size; |
mbed_official | 610:813dcc80987e | 286 | } |
mbed_official | 610:813dcc80987e | 287 | |
mbed_official | 610:813dcc80987e | 288 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 289 | hcryp->State = HAL_CRYP_STATE_BUSY; |
mbed_official | 610:813dcc80987e | 290 | |
mbed_official | 610:813dcc80987e | 291 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 292 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 293 | |
mbed_official | 610:813dcc80987e | 294 | /* Enable Computation Complete Flag and Error Interrupts */ |
mbed_official | 610:813dcc80987e | 295 | __HAL_CRYP_ENABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
mbed_official | 610:813dcc80987e | 296 | |
mbed_official | 610:813dcc80987e | 297 | |
mbed_official | 610:813dcc80987e | 298 | /* If operating mode is key derivation only, the input data have |
mbed_official | 610:813dcc80987e | 299 | already been entered during the initialization process. For |
mbed_official | 610:813dcc80987e | 300 | the other operating modes, they are fed to the CRYP hardware |
mbed_official | 610:813dcc80987e | 301 | block at this point. */ |
mbed_official | 610:813dcc80987e | 302 | if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION) |
mbed_official | 610:813dcc80987e | 303 | { |
mbed_official | 610:813dcc80987e | 304 | /* Initiate the processing under interrupt in entering |
mbed_official | 610:813dcc80987e | 305 | the first input data */ |
mbed_official | 610:813dcc80987e | 306 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
mbed_official | 610:813dcc80987e | 307 | /* Increment/decrement instance pointer/counter */ |
mbed_official | 610:813dcc80987e | 308 | hcryp->pCrypInBuffPtr += 16; |
mbed_official | 610:813dcc80987e | 309 | hcryp->CrypInCount -= 16; |
mbed_official | 610:813dcc80987e | 310 | /* Write the first input block in the Data Input register */ |
mbed_official | 610:813dcc80987e | 311 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 312 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 313 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 314 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 315 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 316 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 317 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 318 | } |
mbed_official | 610:813dcc80987e | 319 | |
mbed_official | 610:813dcc80987e | 320 | /* Return function status */ |
mbed_official | 610:813dcc80987e | 321 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 322 | } |
mbed_official | 610:813dcc80987e | 323 | else |
mbed_official | 610:813dcc80987e | 324 | { |
mbed_official | 610:813dcc80987e | 325 | return HAL_BUSY; |
mbed_official | 610:813dcc80987e | 326 | } |
mbed_official | 610:813dcc80987e | 327 | } |
mbed_official | 610:813dcc80987e | 328 | |
mbed_official | 610:813dcc80987e | 329 | |
mbed_official | 610:813dcc80987e | 330 | |
mbed_official | 610:813dcc80987e | 331 | |
mbed_official | 610:813dcc80987e | 332 | |
mbed_official | 610:813dcc80987e | 333 | /** |
mbed_official | 610:813dcc80987e | 334 | * @brief Carry out in DMA mode the ciphering or deciphering operation according to |
mbed_official | 610:813dcc80987e | 335 | * hcryp->Init structure fields. |
mbed_official | 610:813dcc80987e | 336 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 337 | * the configuration information for CRYP module |
mbed_official | 610:813dcc80987e | 338 | * @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption |
mbed_official | 610:813dcc80987e | 339 | * or key derivation+decryption. |
mbed_official | 610:813dcc80987e | 340 | * @param Size: Length of the input data buffer in bytes, must be a multiple of 16. |
mbed_official | 610:813dcc80987e | 341 | * @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of |
mbed_official | 610:813dcc80987e | 342 | * decryption/key derivation+decryption. |
mbed_official | 610:813dcc80987e | 343 | * @note Chaining modes ECB, CBC and CTR are managed by this function in DMA mode. |
mbed_official | 610:813dcc80987e | 344 | * @note Supported operating modes are encryption, decryption and key derivation with decryption. |
mbed_official | 610:813dcc80987e | 345 | * @note No DMA channel is provided for key derivation only and therefore, access to AES_KEYRx |
mbed_official | 610:813dcc80987e | 346 | * registers must be done by software. |
mbed_official | 610:813dcc80987e | 347 | * @note This API is not applicable to key derivation only; for such a mode, access to AES_KEYRx |
mbed_official | 610:813dcc80987e | 348 | * registers must be done by software thru HAL_CRYPEx_AES() or HAL_CRYPEx_AES_IT() APIs. |
mbed_official | 610:813dcc80987e | 349 | * @retval HAL status |
mbed_official | 610:813dcc80987e | 350 | */ |
mbed_official | 610:813dcc80987e | 351 | HAL_StatusTypeDef HAL_CRYPEx_AES_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData) |
mbed_official | 610:813dcc80987e | 352 | { |
mbed_official | 610:813dcc80987e | 353 | uint32_t inputaddr = 0; |
mbed_official | 610:813dcc80987e | 354 | uint32_t outputaddr = 0; |
mbed_official | 610:813dcc80987e | 355 | |
mbed_official | 610:813dcc80987e | 356 | if (hcryp->State == HAL_CRYP_STATE_READY) |
mbed_official | 610:813dcc80987e | 357 | { |
mbed_official | 610:813dcc80987e | 358 | /* Check parameters setting */ |
mbed_official | 610:813dcc80987e | 359 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION) |
mbed_official | 610:813dcc80987e | 360 | { |
mbed_official | 610:813dcc80987e | 361 | /* no DMA channel is provided for key derivation operating mode, |
mbed_official | 610:813dcc80987e | 362 | access to AES_KEYRx registers must be done by software */ |
mbed_official | 610:813dcc80987e | 363 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 364 | } |
mbed_official | 610:813dcc80987e | 365 | else |
mbed_official | 610:813dcc80987e | 366 | { |
mbed_official | 610:813dcc80987e | 367 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0)) |
mbed_official | 610:813dcc80987e | 368 | { |
mbed_official | 610:813dcc80987e | 369 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 370 | } |
mbed_official | 610:813dcc80987e | 371 | } |
mbed_official | 610:813dcc80987e | 372 | |
mbed_official | 610:813dcc80987e | 373 | |
mbed_official | 610:813dcc80987e | 374 | /* Process Locked */ |
mbed_official | 610:813dcc80987e | 375 | __HAL_LOCK(hcryp); |
mbed_official | 610:813dcc80987e | 376 | |
mbed_official | 610:813dcc80987e | 377 | inputaddr = (uint32_t)pInputData; |
mbed_official | 610:813dcc80987e | 378 | outputaddr = (uint32_t)pOutputData; |
mbed_official | 610:813dcc80987e | 379 | |
mbed_official | 610:813dcc80987e | 380 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 381 | hcryp->State = HAL_CRYP_STATE_BUSY; |
mbed_official | 610:813dcc80987e | 382 | |
mbed_official | 610:813dcc80987e | 383 | /* Set the input and output addresses and start DMA transfer */ |
mbed_official | 610:813dcc80987e | 384 | CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
mbed_official | 610:813dcc80987e | 385 | |
mbed_official | 610:813dcc80987e | 386 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 387 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 388 | |
mbed_official | 610:813dcc80987e | 389 | /* Return function status */ |
mbed_official | 610:813dcc80987e | 390 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 391 | } |
mbed_official | 610:813dcc80987e | 392 | else |
mbed_official | 610:813dcc80987e | 393 | { |
mbed_official | 610:813dcc80987e | 394 | return HAL_BUSY; |
mbed_official | 610:813dcc80987e | 395 | } |
mbed_official | 610:813dcc80987e | 396 | } |
mbed_official | 610:813dcc80987e | 397 | |
mbed_official | 610:813dcc80987e | 398 | |
mbed_official | 610:813dcc80987e | 399 | |
mbed_official | 610:813dcc80987e | 400 | |
mbed_official | 610:813dcc80987e | 401 | |
mbed_official | 610:813dcc80987e | 402 | |
mbed_official | 610:813dcc80987e | 403 | /** |
mbed_official | 610:813dcc80987e | 404 | * @brief Carry out in polling mode the authentication tag generation as well as the ciphering or deciphering |
mbed_official | 610:813dcc80987e | 405 | * operation according to hcryp->Init structure fields. |
mbed_official | 610:813dcc80987e | 406 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 407 | * the configuration information for CRYP module |
mbed_official | 610:813dcc80987e | 408 | * @param pInputData: Pointer to payload data in GCM payload phase, |
mbed_official | 610:813dcc80987e | 409 | * Parameter is meaningless in case of GCM/GMAC init, header and final phases, |
mbed_official | 610:813dcc80987e | 410 | * Pointer to B0 blocks in CMAC header phase, |
mbed_official | 610:813dcc80987e | 411 | * Pointer to C block in CMAC final phase. |
mbed_official | 610:813dcc80987e | 412 | * @param Size: Length of the input payload data buffer in bytes, must be a multiple of 16, |
mbed_official | 610:813dcc80987e | 413 | * Parameter is meaningless in case of GCM/GMAC init and header phases, |
mbed_official | 610:813dcc80987e | 414 | * Length of B blocks (in bytes, must be a multiple of 16) in CMAC header phase, |
mbed_official | 610:813dcc80987e | 415 | * Length of C block (in bytes) in CMAC final phase. |
mbed_official | 610:813dcc80987e | 416 | * @param pOutputData: Pointer to plain or cipher text in GCM payload phase, |
mbed_official | 610:813dcc80987e | 417 | * pointer to authentication tag in GCM/GMAC and CMAC final phases. |
mbed_official | 610:813dcc80987e | 418 | * Parameter is meaningless in case of GCM/GMAC init and header phases |
mbed_official | 610:813dcc80987e | 419 | * and in case of CMAC header phase. |
mbed_official | 610:813dcc80987e | 420 | * @param Timeout: Specify Timeout value |
mbed_official | 610:813dcc80987e | 421 | * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC. |
mbed_official | 610:813dcc80987e | 422 | * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes |
mbed_official | 610:813dcc80987e | 423 | * can be skipped by the user if so required. |
mbed_official | 610:813dcc80987e | 424 | * @retval HAL status |
mbed_official | 610:813dcc80987e | 425 | */ |
mbed_official | 610:813dcc80987e | 426 | HAL_StatusTypeDef HAL_CRYPEx_AES_Auth(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData, uint32_t Timeout) |
mbed_official | 610:813dcc80987e | 427 | { |
mbed_official | 610:813dcc80987e | 428 | uint32_t index = 0; |
mbed_official | 610:813dcc80987e | 429 | uint32_t inputaddr = 0; |
mbed_official | 610:813dcc80987e | 430 | uint32_t outputaddr = 0; |
mbed_official | 610:813dcc80987e | 431 | uint32_t tagaddr = 0; |
mbed_official | 610:813dcc80987e | 432 | uint64_t headerlength = 0; |
mbed_official | 610:813dcc80987e | 433 | uint64_t inputlength = 0; |
mbed_official | 610:813dcc80987e | 434 | |
mbed_official | 610:813dcc80987e | 435 | if (hcryp->State == HAL_CRYP_STATE_READY) |
mbed_official | 610:813dcc80987e | 436 | { |
mbed_official | 610:813dcc80987e | 437 | /* input/output parameters check */ |
mbed_official | 610:813dcc80987e | 438 | if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_HEADER_PHASE) |
mbed_official | 610:813dcc80987e | 439 | { |
mbed_official | 610:813dcc80987e | 440 | if ((hcryp->Init.Header == NULL) || (hcryp->Init.HeaderSize == 0)) |
mbed_official | 610:813dcc80987e | 441 | { |
mbed_official | 610:813dcc80987e | 442 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 443 | } |
mbed_official | 610:813dcc80987e | 444 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
mbed_official | 610:813dcc80987e | 445 | { |
mbed_official | 610:813dcc80987e | 446 | /* In case of CMAC header phase resumption, we can have pInputData = NULL and Size = 0 */ |
mbed_official | 610:813dcc80987e | 447 | if (((pInputData != NULL) && (Size == 0)) || ((pInputData == NULL) && (Size != 0))) |
mbed_official | 610:813dcc80987e | 448 | { |
mbed_official | 610:813dcc80987e | 449 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 450 | } |
mbed_official | 610:813dcc80987e | 451 | } |
mbed_official | 610:813dcc80987e | 452 | } |
mbed_official | 610:813dcc80987e | 453 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) |
mbed_official | 610:813dcc80987e | 454 | { |
mbed_official | 610:813dcc80987e | 455 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0)) |
mbed_official | 610:813dcc80987e | 456 | { |
mbed_official | 610:813dcc80987e | 457 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 458 | } |
mbed_official | 610:813dcc80987e | 459 | } |
mbed_official | 610:813dcc80987e | 460 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_FINAL_PHASE) |
mbed_official | 610:813dcc80987e | 461 | { |
mbed_official | 610:813dcc80987e | 462 | if (pOutputData == NULL) |
mbed_official | 610:813dcc80987e | 463 | { |
mbed_official | 610:813dcc80987e | 464 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 465 | } |
mbed_official | 610:813dcc80987e | 466 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL)) |
mbed_official | 610:813dcc80987e | 467 | { |
mbed_official | 610:813dcc80987e | 468 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 469 | } |
mbed_official | 610:813dcc80987e | 470 | } |
mbed_official | 610:813dcc80987e | 471 | |
mbed_official | 610:813dcc80987e | 472 | |
mbed_official | 610:813dcc80987e | 473 | /* Process Locked */ |
mbed_official | 610:813dcc80987e | 474 | __HAL_LOCK(hcryp); |
mbed_official | 610:813dcc80987e | 475 | |
mbed_official | 610:813dcc80987e | 476 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 477 | hcryp->State = HAL_CRYP_STATE_BUSY; |
mbed_official | 610:813dcc80987e | 478 | |
mbed_official | 610:813dcc80987e | 479 | /*=====================*/ |
mbed_official | 610:813dcc80987e | 480 | /* GCM/GMAC init phase */ |
mbed_official | 610:813dcc80987e | 481 | /*=====================*/ |
mbed_official | 610:813dcc80987e | 482 | /* In case of init phase, the input data (Key and Initialization Vector) have |
mbed_official | 610:813dcc80987e | 483 | already been entered during the initialization process. Therefore, the |
mbed_official | 610:813dcc80987e | 484 | API just waits for the CCF flag to be set. */ |
mbed_official | 610:813dcc80987e | 485 | if (hcryp->Init.GCMCMACPhase == CRYP_GCM_INIT_PHASE) |
mbed_official | 610:813dcc80987e | 486 | { |
mbed_official | 610:813dcc80987e | 487 | /* just wait for hash computation */ |
mbed_official | 610:813dcc80987e | 488 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
mbed_official | 610:813dcc80987e | 489 | { |
mbed_official | 610:813dcc80987e | 490 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 491 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 492 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 493 | } |
mbed_official | 610:813dcc80987e | 494 | |
mbed_official | 610:813dcc80987e | 495 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 496 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 497 | /* Mark that the initialization phase is over */ |
mbed_official | 610:813dcc80987e | 498 | hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER; |
mbed_official | 610:813dcc80987e | 499 | } |
mbed_official | 610:813dcc80987e | 500 | /*===============================*/ |
mbed_official | 610:813dcc80987e | 501 | /* GCM/GMAC or CMAC header phase */ |
mbed_official | 610:813dcc80987e | 502 | /*===============================*/ |
mbed_official | 610:813dcc80987e | 503 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_HEADER_PHASE) |
mbed_official | 610:813dcc80987e | 504 | { |
mbed_official | 610:813dcc80987e | 505 | /* Set header phase; for GCM or GMAC, set data-byte at this point */ |
mbed_official | 610:813dcc80987e | 506 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
mbed_official | 610:813dcc80987e | 507 | { |
mbed_official | 610:813dcc80987e | 508 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_GCMCMAC_HEADER_PHASE|hcryp->Init.DataType); |
mbed_official | 610:813dcc80987e | 509 | } |
mbed_official | 610:813dcc80987e | 510 | else |
mbed_official | 610:813dcc80987e | 511 | { |
mbed_official | 610:813dcc80987e | 512 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_HEADER_PHASE); |
mbed_official | 610:813dcc80987e | 513 | } |
mbed_official | 610:813dcc80987e | 514 | |
mbed_official | 610:813dcc80987e | 515 | /* Enable the Peripheral */ |
mbed_official | 610:813dcc80987e | 516 | __HAL_CRYP_ENABLE(); |
mbed_official | 610:813dcc80987e | 517 | |
mbed_official | 610:813dcc80987e | 518 | /* in case of CMAC, enter B0 block in header phase, before the header itself. */ |
mbed_official | 610:813dcc80987e | 519 | /* If Size = 0 (possible case of resumption after CMAC header phase suspension), |
mbed_official | 610:813dcc80987e | 520 | skip these steps and go directly to header buffer feeding to the HW */ |
mbed_official | 610:813dcc80987e | 521 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (Size != 0)) |
mbed_official | 610:813dcc80987e | 522 | { |
mbed_official | 610:813dcc80987e | 523 | inputaddr = (uint32_t)pInputData; |
mbed_official | 610:813dcc80987e | 524 | |
mbed_official | 610:813dcc80987e | 525 | for(index=0; (index < Size); index += 16) |
mbed_official | 610:813dcc80987e | 526 | { |
mbed_official | 610:813dcc80987e | 527 | /* Write the Input block in the Data Input register */ |
mbed_official | 610:813dcc80987e | 528 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 529 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 530 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 531 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 532 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 533 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 534 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 535 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 536 | |
mbed_official | 610:813dcc80987e | 537 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
mbed_official | 610:813dcc80987e | 538 | { |
mbed_official | 610:813dcc80987e | 539 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 540 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 541 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 542 | } |
mbed_official | 610:813dcc80987e | 543 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 544 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 545 | |
mbed_official | 610:813dcc80987e | 546 | /* If the suspension flag has been raised and if the processing is not about |
mbed_official | 610:813dcc80987e | 547 | to end, suspend processing */ |
mbed_official | 610:813dcc80987e | 548 | if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16) < Size)) |
mbed_official | 610:813dcc80987e | 549 | { |
mbed_official | 610:813dcc80987e | 550 | /* reset SuspendRequest */ |
mbed_official | 610:813dcc80987e | 551 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
mbed_official | 610:813dcc80987e | 552 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 553 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
mbed_official | 610:813dcc80987e | 554 | /* Mark that the header phase is over */ |
mbed_official | 610:813dcc80987e | 555 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
mbed_official | 610:813dcc80987e | 556 | |
mbed_official | 610:813dcc80987e | 557 | /* Save current reading and writing locations of Input and Output buffers */ |
mbed_official | 610:813dcc80987e | 558 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
mbed_official | 610:813dcc80987e | 559 | /* Save the total number of bytes (B blocks + header) that remain to be |
mbed_official | 610:813dcc80987e | 560 | processed at this point */ |
mbed_official | 610:813dcc80987e | 561 | hcryp->CrypInCount = hcryp->Init.HeaderSize + Size - (index+16); |
mbed_official | 610:813dcc80987e | 562 | |
mbed_official | 610:813dcc80987e | 563 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 564 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 565 | |
mbed_official | 610:813dcc80987e | 566 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 567 | } |
mbed_official | 610:813dcc80987e | 568 | } /* for(index=0; (index < Size); index += 16) */ |
mbed_official | 610:813dcc80987e | 569 | } |
mbed_official | 610:813dcc80987e | 570 | |
mbed_official | 610:813dcc80987e | 571 | /* Enter header */ |
mbed_official | 610:813dcc80987e | 572 | inputaddr = (uint32_t)hcryp->Init.Header; |
mbed_official | 610:813dcc80987e | 573 | for(index=0; (index < hcryp->Init.HeaderSize); index += 16) |
mbed_official | 610:813dcc80987e | 574 | { |
mbed_official | 610:813dcc80987e | 575 | /* Write the Input block in the Data Input register */ |
mbed_official | 610:813dcc80987e | 576 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 577 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 578 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 579 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 580 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 581 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 582 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 583 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 584 | |
mbed_official | 610:813dcc80987e | 585 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
mbed_official | 610:813dcc80987e | 586 | { |
mbed_official | 610:813dcc80987e | 587 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 588 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 589 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 590 | } |
mbed_official | 610:813dcc80987e | 591 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 592 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 593 | |
mbed_official | 610:813dcc80987e | 594 | /* If the suspension flag has been raised and if the processing is not about |
mbed_official | 610:813dcc80987e | 595 | to end, suspend processing */ |
mbed_official | 610:813dcc80987e | 596 | if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16) < hcryp->Init.HeaderSize)) |
mbed_official | 610:813dcc80987e | 597 | { |
mbed_official | 610:813dcc80987e | 598 | /* reset SuspendRequest */ |
mbed_official | 610:813dcc80987e | 599 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
mbed_official | 610:813dcc80987e | 600 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 601 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
mbed_official | 610:813dcc80987e | 602 | /* Mark that the header phase is over */ |
mbed_official | 610:813dcc80987e | 603 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
mbed_official | 610:813dcc80987e | 604 | |
mbed_official | 610:813dcc80987e | 605 | /* Save current reading and writing locations of Input and Output buffers */ |
mbed_official | 610:813dcc80987e | 606 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
mbed_official | 610:813dcc80987e | 607 | /* Save the total number of bytes that remain to be processed at this point */ |
mbed_official | 610:813dcc80987e | 608 | hcryp->CrypInCount = hcryp->Init.HeaderSize - (index+16); |
mbed_official | 610:813dcc80987e | 609 | |
mbed_official | 610:813dcc80987e | 610 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 611 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 612 | |
mbed_official | 610:813dcc80987e | 613 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 614 | } |
mbed_official | 610:813dcc80987e | 615 | } |
mbed_official | 610:813dcc80987e | 616 | /* Mark that the header phase is over */ |
mbed_official | 610:813dcc80987e | 617 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
mbed_official | 610:813dcc80987e | 618 | } |
mbed_official | 610:813dcc80987e | 619 | /*========================*/ |
mbed_official | 610:813dcc80987e | 620 | /* GCM/GMAC payload phase */ |
mbed_official | 610:813dcc80987e | 621 | /*========================*/ |
mbed_official | 610:813dcc80987e | 622 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) |
mbed_official | 610:813dcc80987e | 623 | { |
mbed_official | 610:813dcc80987e | 624 | |
mbed_official | 610:813dcc80987e | 625 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCM_PAYLOAD_PHASE); |
mbed_official | 610:813dcc80987e | 626 | |
mbed_official | 610:813dcc80987e | 627 | /* if the header phase has been bypassed, AES must be enabled again */ |
mbed_official | 610:813dcc80987e | 628 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
mbed_official | 610:813dcc80987e | 629 | { |
mbed_official | 610:813dcc80987e | 630 | __HAL_CRYP_ENABLE(); |
mbed_official | 610:813dcc80987e | 631 | } |
mbed_official | 610:813dcc80987e | 632 | |
mbed_official | 610:813dcc80987e | 633 | inputaddr = (uint32_t)pInputData; |
mbed_official | 610:813dcc80987e | 634 | outputaddr = (uint32_t)pOutputData; |
mbed_official | 610:813dcc80987e | 635 | |
mbed_official | 610:813dcc80987e | 636 | /* Enter payload */ |
mbed_official | 610:813dcc80987e | 637 | for(index=0; (index < Size); index += 16) |
mbed_official | 610:813dcc80987e | 638 | { |
mbed_official | 610:813dcc80987e | 639 | /* Write the Input block in the Data Input register */ |
mbed_official | 610:813dcc80987e | 640 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 641 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 642 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 643 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 644 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 645 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 646 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 647 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 648 | |
mbed_official | 610:813dcc80987e | 649 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
mbed_official | 610:813dcc80987e | 650 | { |
mbed_official | 610:813dcc80987e | 651 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 652 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 653 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 654 | } |
mbed_official | 610:813dcc80987e | 655 | |
mbed_official | 610:813dcc80987e | 656 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 657 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 658 | |
mbed_official | 610:813dcc80987e | 659 | /* Retrieve output data: read the output block |
mbed_official | 610:813dcc80987e | 660 | from the Data Output Register */ |
mbed_official | 610:813dcc80987e | 661 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 662 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 663 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 664 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 665 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 666 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 667 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 668 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 669 | |
mbed_official | 610:813dcc80987e | 670 | /* If the suspension flag has been raised and if the processing is not about |
mbed_official | 610:813dcc80987e | 671 | to end, suspend processing */ |
mbed_official | 610:813dcc80987e | 672 | if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16) < Size)) |
mbed_official | 610:813dcc80987e | 673 | { |
mbed_official | 610:813dcc80987e | 674 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) |
mbed_official | 610:813dcc80987e | 675 | { |
mbed_official | 610:813dcc80987e | 676 | /* Ensure that Busy flag is reset */ |
mbed_official | 610:813dcc80987e | 677 | if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK) |
mbed_official | 610:813dcc80987e | 678 | { |
mbed_official | 610:813dcc80987e | 679 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 680 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 681 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 682 | } |
mbed_official | 610:813dcc80987e | 683 | } |
mbed_official | 610:813dcc80987e | 684 | /* reset SuspendRequest */ |
mbed_official | 610:813dcc80987e | 685 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
mbed_official | 610:813dcc80987e | 686 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 687 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
mbed_official | 610:813dcc80987e | 688 | /* Mark that the header phase is over */ |
mbed_official | 610:813dcc80987e | 689 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
mbed_official | 610:813dcc80987e | 690 | |
mbed_official | 610:813dcc80987e | 691 | /* Save current reading and writing locations of Input and Output buffers */ |
mbed_official | 610:813dcc80987e | 692 | hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr; |
mbed_official | 610:813dcc80987e | 693 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
mbed_official | 610:813dcc80987e | 694 | /* Save the number of bytes that remain to be processed at this point */ |
mbed_official | 610:813dcc80987e | 695 | hcryp->CrypInCount = Size - (index+16); |
mbed_official | 610:813dcc80987e | 696 | |
mbed_official | 610:813dcc80987e | 697 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 698 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 699 | |
mbed_official | 610:813dcc80987e | 700 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 701 | } |
mbed_official | 610:813dcc80987e | 702 | |
mbed_official | 610:813dcc80987e | 703 | } |
mbed_official | 610:813dcc80987e | 704 | /* Mark that the payload phase is over */ |
mbed_official | 610:813dcc80987e | 705 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
mbed_official | 610:813dcc80987e | 706 | } |
mbed_official | 610:813dcc80987e | 707 | /*==============================*/ |
mbed_official | 610:813dcc80987e | 708 | /* GCM/GMAC or CMAC final phase */ |
mbed_official | 610:813dcc80987e | 709 | /*==============================*/ |
mbed_official | 610:813dcc80987e | 710 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_FINAL_PHASE) |
mbed_official | 610:813dcc80987e | 711 | { |
mbed_official | 610:813dcc80987e | 712 | tagaddr = (uint32_t)pOutputData; |
mbed_official | 610:813dcc80987e | 713 | |
mbed_official | 610:813dcc80987e | 714 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_FINAL_PHASE); |
mbed_official | 610:813dcc80987e | 715 | |
mbed_official | 610:813dcc80987e | 716 | /* if the header and payload phases have been bypassed, AES must be enabled again */ |
mbed_official | 610:813dcc80987e | 717 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
mbed_official | 610:813dcc80987e | 718 | { |
mbed_official | 610:813dcc80987e | 719 | __HAL_CRYP_ENABLE(); |
mbed_official | 610:813dcc80987e | 720 | } |
mbed_official | 610:813dcc80987e | 721 | |
mbed_official | 610:813dcc80987e | 722 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
mbed_official | 610:813dcc80987e | 723 | { |
mbed_official | 610:813dcc80987e | 724 | headerlength = hcryp->Init.HeaderSize * 8; /* Header length in bits */ |
mbed_official | 610:813dcc80987e | 725 | inputlength = Size * 8; /* input length in bits */ |
mbed_official | 610:813dcc80987e | 726 | |
mbed_official | 610:813dcc80987e | 727 | |
mbed_official | 610:813dcc80987e | 728 | if(hcryp->Init.DataType == CRYP_DATATYPE_1B) |
mbed_official | 610:813dcc80987e | 729 | { |
mbed_official | 610:813dcc80987e | 730 | hcryp->Instance->DINR = __RBIT((headerlength)>>32); |
mbed_official | 610:813dcc80987e | 731 | hcryp->Instance->DINR = __RBIT(headerlength); |
mbed_official | 610:813dcc80987e | 732 | hcryp->Instance->DINR = __RBIT((inputlength)>>32); |
mbed_official | 610:813dcc80987e | 733 | hcryp->Instance->DINR = __RBIT(inputlength); |
mbed_official | 610:813dcc80987e | 734 | } |
mbed_official | 610:813dcc80987e | 735 | else if(hcryp->Init.DataType == CRYP_DATATYPE_8B) |
mbed_official | 610:813dcc80987e | 736 | { |
mbed_official | 610:813dcc80987e | 737 | hcryp->Instance->DINR = __REV((headerlength)>>32); |
mbed_official | 610:813dcc80987e | 738 | hcryp->Instance->DINR = __REV(headerlength); |
mbed_official | 610:813dcc80987e | 739 | hcryp->Instance->DINR = __REV((inputlength)>>32); |
mbed_official | 610:813dcc80987e | 740 | hcryp->Instance->DINR = __REV(inputlength); |
mbed_official | 610:813dcc80987e | 741 | } |
mbed_official | 610:813dcc80987e | 742 | else if(hcryp->Init.DataType == CRYP_DATATYPE_16B) |
mbed_official | 610:813dcc80987e | 743 | { |
mbed_official | 610:813dcc80987e | 744 | hcryp->Instance->DINR = __ROR((headerlength)>>32, 16); |
mbed_official | 610:813dcc80987e | 745 | hcryp->Instance->DINR = __ROR(headerlength, 16); |
mbed_official | 610:813dcc80987e | 746 | hcryp->Instance->DINR = __ROR((inputlength)>>32, 16); |
mbed_official | 610:813dcc80987e | 747 | hcryp->Instance->DINR = __ROR(inputlength, 16); |
mbed_official | 610:813dcc80987e | 748 | } |
mbed_official | 610:813dcc80987e | 749 | else if(hcryp->Init.DataType == CRYP_DATATYPE_32B) |
mbed_official | 610:813dcc80987e | 750 | { |
mbed_official | 610:813dcc80987e | 751 | hcryp->Instance->DINR = (uint32_t)(headerlength>>32); |
mbed_official | 610:813dcc80987e | 752 | hcryp->Instance->DINR = (uint32_t)(headerlength); |
mbed_official | 610:813dcc80987e | 753 | hcryp->Instance->DINR = (uint32_t)(inputlength>>32); |
mbed_official | 610:813dcc80987e | 754 | hcryp->Instance->DINR = (uint32_t)(inputlength); |
mbed_official | 610:813dcc80987e | 755 | } |
mbed_official | 610:813dcc80987e | 756 | } |
mbed_official | 610:813dcc80987e | 757 | else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
mbed_official | 610:813dcc80987e | 758 | { |
mbed_official | 610:813dcc80987e | 759 | inputaddr = (uint32_t)pInputData; |
mbed_official | 610:813dcc80987e | 760 | /* Enter the last block made of a 128-bit value formatted |
mbed_official | 610:813dcc80987e | 761 | from the original B0 packet. */ |
mbed_official | 610:813dcc80987e | 762 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 763 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 764 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 765 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 766 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 767 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 768 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 769 | } |
mbed_official | 610:813dcc80987e | 770 | |
mbed_official | 610:813dcc80987e | 771 | |
mbed_official | 610:813dcc80987e | 772 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
mbed_official | 610:813dcc80987e | 773 | { |
mbed_official | 610:813dcc80987e | 774 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 775 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 776 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 777 | } |
mbed_official | 610:813dcc80987e | 778 | |
mbed_official | 610:813dcc80987e | 779 | /* Read the Auth TAG in the Data Out register */ |
mbed_official | 610:813dcc80987e | 780 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 781 | tagaddr+=4; |
mbed_official | 610:813dcc80987e | 782 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 783 | tagaddr+=4; |
mbed_official | 610:813dcc80987e | 784 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 785 | tagaddr+=4; |
mbed_official | 610:813dcc80987e | 786 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 787 | |
mbed_official | 610:813dcc80987e | 788 | |
mbed_official | 610:813dcc80987e | 789 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 790 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 791 | /* Mark that the final phase is over */ |
mbed_official | 610:813dcc80987e | 792 | hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER; |
mbed_official | 610:813dcc80987e | 793 | /* Disable the Peripheral */ |
mbed_official | 610:813dcc80987e | 794 | __HAL_CRYP_DISABLE(); |
mbed_official | 610:813dcc80987e | 795 | } |
mbed_official | 610:813dcc80987e | 796 | /*=================================================*/ |
mbed_official | 610:813dcc80987e | 797 | /* case incorrect hcryp->Init.GCMCMACPhase setting */ |
mbed_official | 610:813dcc80987e | 798 | /*=================================================*/ |
mbed_official | 610:813dcc80987e | 799 | else |
mbed_official | 610:813dcc80987e | 800 | { |
mbed_official | 610:813dcc80987e | 801 | hcryp->State = HAL_CRYP_STATE_ERROR; |
mbed_official | 610:813dcc80987e | 802 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 803 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 804 | } |
mbed_official | 610:813dcc80987e | 805 | |
mbed_official | 610:813dcc80987e | 806 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 807 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 808 | |
mbed_official | 610:813dcc80987e | 809 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 810 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 811 | |
mbed_official | 610:813dcc80987e | 812 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 813 | } |
mbed_official | 610:813dcc80987e | 814 | else |
mbed_official | 610:813dcc80987e | 815 | { |
mbed_official | 610:813dcc80987e | 816 | return HAL_BUSY; |
mbed_official | 610:813dcc80987e | 817 | } |
mbed_official | 610:813dcc80987e | 818 | } |
mbed_official | 610:813dcc80987e | 819 | |
mbed_official | 610:813dcc80987e | 820 | |
mbed_official | 610:813dcc80987e | 821 | |
mbed_official | 610:813dcc80987e | 822 | |
mbed_official | 610:813dcc80987e | 823 | /** |
mbed_official | 610:813dcc80987e | 824 | * @brief Carry out in interrupt mode the authentication tag generation as well as the ciphering or deciphering |
mbed_official | 610:813dcc80987e | 825 | * operation according to hcryp->Init structure fields. |
mbed_official | 610:813dcc80987e | 826 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 827 | * the configuration information for CRYP module |
mbed_official | 610:813dcc80987e | 828 | * @param pInputData: Pointer to payload data in GCM payload phase, |
mbed_official | 610:813dcc80987e | 829 | * Parameter is meaningless in case of GCM/GMAC init, header and final phases, |
mbed_official | 610:813dcc80987e | 830 | * Pointer to B0 blocks in CMAC header phase, |
mbed_official | 610:813dcc80987e | 831 | * Pointer to C block in CMAC final phase. |
mbed_official | 610:813dcc80987e | 832 | * @param Size: Length of the input payload data buffer in bytes, must be a multiple of 16, |
mbed_official | 610:813dcc80987e | 833 | * Parameter is meaningless in case of GCM/GMAC init and header phases, |
mbed_official | 610:813dcc80987e | 834 | * Length of B blocks (in bytes, must be a multiple of 16) in CMAC header phase, |
mbed_official | 610:813dcc80987e | 835 | * Length of C block (in bytes) in CMAC final phase. |
mbed_official | 610:813dcc80987e | 836 | * @param pOutputData: Pointer to plain or cipher text in GCM payload phase, |
mbed_official | 610:813dcc80987e | 837 | * pointer to authentication tag in GCM/GMAC and CMAC final phases. |
mbed_official | 610:813dcc80987e | 838 | * Parameter is meaningless in case of GCM/GMAC init and header phases |
mbed_official | 610:813dcc80987e | 839 | * and in case of CMAC header phase. |
mbed_official | 610:813dcc80987e | 840 | * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC. |
mbed_official | 610:813dcc80987e | 841 | * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes |
mbed_official | 610:813dcc80987e | 842 | * can be skipped by the user if so required. |
mbed_official | 610:813dcc80987e | 843 | * @retval HAL status |
mbed_official | 610:813dcc80987e | 844 | */ |
mbed_official | 610:813dcc80987e | 845 | HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData) |
mbed_official | 610:813dcc80987e | 846 | { |
mbed_official | 610:813dcc80987e | 847 | |
mbed_official | 610:813dcc80987e | 848 | uint32_t inputaddr = 0; |
mbed_official | 610:813dcc80987e | 849 | uint64_t headerlength = 0; |
mbed_official | 610:813dcc80987e | 850 | uint64_t inputlength = 0; |
mbed_official | 610:813dcc80987e | 851 | |
mbed_official | 610:813dcc80987e | 852 | |
mbed_official | 610:813dcc80987e | 853 | if (hcryp->State == HAL_CRYP_STATE_READY) |
mbed_official | 610:813dcc80987e | 854 | { |
mbed_official | 610:813dcc80987e | 855 | /* input/output parameters check */ |
mbed_official | 610:813dcc80987e | 856 | if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_HEADER_PHASE) |
mbed_official | 610:813dcc80987e | 857 | { |
mbed_official | 610:813dcc80987e | 858 | if ((hcryp->Init.Header == NULL) || (hcryp->Init.HeaderSize == 0)) |
mbed_official | 610:813dcc80987e | 859 | { |
mbed_official | 610:813dcc80987e | 860 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 861 | } |
mbed_official | 610:813dcc80987e | 862 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
mbed_official | 610:813dcc80987e | 863 | { |
mbed_official | 610:813dcc80987e | 864 | /* In case of CMAC header phase resumption, we can have pInputData = NULL and Size = 0 */ |
mbed_official | 610:813dcc80987e | 865 | if (((pInputData != NULL) && (Size == 0)) || ((pInputData == NULL) && (Size != 0))) |
mbed_official | 610:813dcc80987e | 866 | { |
mbed_official | 610:813dcc80987e | 867 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 868 | } |
mbed_official | 610:813dcc80987e | 869 | } |
mbed_official | 610:813dcc80987e | 870 | } |
mbed_official | 610:813dcc80987e | 871 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) |
mbed_official | 610:813dcc80987e | 872 | { |
mbed_official | 610:813dcc80987e | 873 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0)) |
mbed_official | 610:813dcc80987e | 874 | { |
mbed_official | 610:813dcc80987e | 875 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 876 | } |
mbed_official | 610:813dcc80987e | 877 | } |
mbed_official | 610:813dcc80987e | 878 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_FINAL_PHASE) |
mbed_official | 610:813dcc80987e | 879 | { |
mbed_official | 610:813dcc80987e | 880 | if (pOutputData == NULL) |
mbed_official | 610:813dcc80987e | 881 | { |
mbed_official | 610:813dcc80987e | 882 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 883 | } |
mbed_official | 610:813dcc80987e | 884 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL)) |
mbed_official | 610:813dcc80987e | 885 | { |
mbed_official | 610:813dcc80987e | 886 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 887 | } |
mbed_official | 610:813dcc80987e | 888 | } |
mbed_official | 610:813dcc80987e | 889 | |
mbed_official | 610:813dcc80987e | 890 | |
mbed_official | 610:813dcc80987e | 891 | /* Process Locked */ |
mbed_official | 610:813dcc80987e | 892 | __HAL_LOCK(hcryp); |
mbed_official | 610:813dcc80987e | 893 | |
mbed_official | 610:813dcc80987e | 894 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 895 | hcryp->State = HAL_CRYP_STATE_BUSY; |
mbed_official | 610:813dcc80987e | 896 | |
mbed_official | 610:813dcc80987e | 897 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 898 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 899 | |
mbed_official | 610:813dcc80987e | 900 | /* Enable Computation Complete Flag and Error Interrupts */ |
mbed_official | 610:813dcc80987e | 901 | __HAL_CRYP_ENABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
mbed_official | 610:813dcc80987e | 902 | |
mbed_official | 610:813dcc80987e | 903 | |
mbed_official | 610:813dcc80987e | 904 | |
mbed_official | 610:813dcc80987e | 905 | /*=====================*/ |
mbed_official | 610:813dcc80987e | 906 | /* GCM/GMAC init phase */ |
mbed_official | 610:813dcc80987e | 907 | /*=====================*/ |
mbed_official | 610:813dcc80987e | 908 | if (hcryp->Init.GCMCMACPhase == CRYP_GCM_INIT_PHASE) |
mbed_official | 610:813dcc80987e | 909 | { |
mbed_official | 610:813dcc80987e | 910 | /* In case of init phase, the input data (Key and Initialization Vector) have |
mbed_official | 610:813dcc80987e | 911 | already been entered during the initialization process. Therefore, the |
mbed_official | 610:813dcc80987e | 912 | software just waits for the CCF interrupt to be raised and which will |
mbed_official | 610:813dcc80987e | 913 | be handled by CRYP_AES_Auth_IT() API. */ |
mbed_official | 610:813dcc80987e | 914 | } |
mbed_official | 610:813dcc80987e | 915 | /*===============================*/ |
mbed_official | 610:813dcc80987e | 916 | /* GCM/GMAC or CMAC header phase */ |
mbed_official | 610:813dcc80987e | 917 | /*===============================*/ |
mbed_official | 610:813dcc80987e | 918 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_HEADER_PHASE) |
mbed_official | 610:813dcc80987e | 919 | { |
mbed_official | 610:813dcc80987e | 920 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
mbed_official | 610:813dcc80987e | 921 | { |
mbed_official | 610:813dcc80987e | 922 | /* In case of CMAC, B blocks are first entered, before the header. |
mbed_official | 610:813dcc80987e | 923 | Therefore, B blocks and the header are entered back-to-back |
mbed_official | 610:813dcc80987e | 924 | as if it was only one single block. |
mbed_official | 610:813dcc80987e | 925 | However, in case of resumption after suspension, if all the |
mbed_official | 610:813dcc80987e | 926 | B blocks have been entered (in that case, Size = 0), only the |
mbed_official | 610:813dcc80987e | 927 | remainder of the non-processed header bytes are entered. */ |
mbed_official | 610:813dcc80987e | 928 | if (Size != 0) |
mbed_official | 610:813dcc80987e | 929 | { |
mbed_official | 610:813dcc80987e | 930 | hcryp->CrypInCount = Size + hcryp->Init.HeaderSize; |
mbed_official | 610:813dcc80987e | 931 | hcryp->pCrypInBuffPtr = pInputData; |
mbed_official | 610:813dcc80987e | 932 | } |
mbed_official | 610:813dcc80987e | 933 | else |
mbed_official | 610:813dcc80987e | 934 | { |
mbed_official | 610:813dcc80987e | 935 | hcryp->CrypInCount = hcryp->Init.HeaderSize; |
mbed_official | 610:813dcc80987e | 936 | hcryp->pCrypInBuffPtr = hcryp->Init.Header; |
mbed_official | 610:813dcc80987e | 937 | } |
mbed_official | 610:813dcc80987e | 938 | } |
mbed_official | 610:813dcc80987e | 939 | else |
mbed_official | 610:813dcc80987e | 940 | { |
mbed_official | 610:813dcc80987e | 941 | /* Get the header addresses and sizes */ |
mbed_official | 610:813dcc80987e | 942 | hcryp->CrypInCount = hcryp->Init.HeaderSize; |
mbed_official | 610:813dcc80987e | 943 | hcryp->pCrypInBuffPtr = hcryp->Init.Header; |
mbed_official | 610:813dcc80987e | 944 | } |
mbed_official | 610:813dcc80987e | 945 | |
mbed_official | 610:813dcc80987e | 946 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
mbed_official | 610:813dcc80987e | 947 | |
mbed_official | 610:813dcc80987e | 948 | /* Set header phase; for GCM or GMAC, set data-byte at this point */ |
mbed_official | 610:813dcc80987e | 949 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
mbed_official | 610:813dcc80987e | 950 | { |
mbed_official | 610:813dcc80987e | 951 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_GCMCMAC_HEADER_PHASE|hcryp->Init.DataType); |
mbed_official | 610:813dcc80987e | 952 | } |
mbed_official | 610:813dcc80987e | 953 | else |
mbed_official | 610:813dcc80987e | 954 | { |
mbed_official | 610:813dcc80987e | 955 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_HEADER_PHASE); |
mbed_official | 610:813dcc80987e | 956 | } |
mbed_official | 610:813dcc80987e | 957 | |
mbed_official | 610:813dcc80987e | 958 | /* Enable the Peripheral */ |
mbed_official | 610:813dcc80987e | 959 | __HAL_CRYP_ENABLE(); |
mbed_official | 610:813dcc80987e | 960 | |
mbed_official | 610:813dcc80987e | 961 | /* Increment/decrement instance pointer/counter */ |
mbed_official | 610:813dcc80987e | 962 | hcryp->pCrypInBuffPtr += 16; |
mbed_official | 610:813dcc80987e | 963 | hcryp->CrypInCount -= 16; |
mbed_official | 610:813dcc80987e | 964 | |
mbed_official | 610:813dcc80987e | 965 | |
mbed_official | 610:813dcc80987e | 966 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
mbed_official | 610:813dcc80987e | 967 | { |
mbed_official | 610:813dcc80987e | 968 | if (hcryp->CrypInCount == hcryp->Init.HeaderSize) |
mbed_official | 610:813dcc80987e | 969 | { |
mbed_official | 610:813dcc80987e | 970 | /* All B blocks will have been entered after the next |
mbed_official | 610:813dcc80987e | 971 | four DINR writing, so point at header buffer for |
mbed_official | 610:813dcc80987e | 972 | the next iteration */ |
mbed_official | 610:813dcc80987e | 973 | hcryp->pCrypInBuffPtr = hcryp->Init.Header; |
mbed_official | 610:813dcc80987e | 974 | } |
mbed_official | 610:813dcc80987e | 975 | } |
mbed_official | 610:813dcc80987e | 976 | |
mbed_official | 610:813dcc80987e | 977 | /* Enter header first block to initiate the process |
mbed_official | 610:813dcc80987e | 978 | in the Data Input register */ |
mbed_official | 610:813dcc80987e | 979 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 980 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 981 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 982 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 983 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 984 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 985 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 986 | } |
mbed_official | 610:813dcc80987e | 987 | /*========================*/ |
mbed_official | 610:813dcc80987e | 988 | /* GCM/GMAC payload phase */ |
mbed_official | 610:813dcc80987e | 989 | /*========================*/ |
mbed_official | 610:813dcc80987e | 990 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) |
mbed_official | 610:813dcc80987e | 991 | { |
mbed_official | 610:813dcc80987e | 992 | /* Get the buffer addresses and sizes */ |
mbed_official | 610:813dcc80987e | 993 | hcryp->CrypInCount = Size; |
mbed_official | 610:813dcc80987e | 994 | hcryp->pCrypInBuffPtr = pInputData; |
mbed_official | 610:813dcc80987e | 995 | hcryp->pCrypOutBuffPtr = pOutputData; |
mbed_official | 610:813dcc80987e | 996 | hcryp->CrypOutCount = Size; |
mbed_official | 610:813dcc80987e | 997 | |
mbed_official | 610:813dcc80987e | 998 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
mbed_official | 610:813dcc80987e | 999 | |
mbed_official | 610:813dcc80987e | 1000 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCM_PAYLOAD_PHASE); |
mbed_official | 610:813dcc80987e | 1001 | |
mbed_official | 610:813dcc80987e | 1002 | /* if the header phase has been bypassed, AES must be enabled again */ |
mbed_official | 610:813dcc80987e | 1003 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
mbed_official | 610:813dcc80987e | 1004 | { |
mbed_official | 610:813dcc80987e | 1005 | __HAL_CRYP_ENABLE(); |
mbed_official | 610:813dcc80987e | 1006 | } |
mbed_official | 610:813dcc80987e | 1007 | |
mbed_official | 610:813dcc80987e | 1008 | /* Increment/decrement instance pointer/counter */ |
mbed_official | 610:813dcc80987e | 1009 | hcryp->pCrypInBuffPtr += 16; |
mbed_official | 610:813dcc80987e | 1010 | hcryp->CrypInCount -= 16; |
mbed_official | 610:813dcc80987e | 1011 | |
mbed_official | 610:813dcc80987e | 1012 | /* Enter payload first block to initiate the process |
mbed_official | 610:813dcc80987e | 1013 | in the Data Input register */ |
mbed_official | 610:813dcc80987e | 1014 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1015 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1016 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1017 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1018 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1019 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1020 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1021 | } |
mbed_official | 610:813dcc80987e | 1022 | /*==============================*/ |
mbed_official | 610:813dcc80987e | 1023 | /* GCM/GMAC or CMAC final phase */ |
mbed_official | 610:813dcc80987e | 1024 | /*==============================*/ |
mbed_official | 610:813dcc80987e | 1025 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_FINAL_PHASE) |
mbed_official | 610:813dcc80987e | 1026 | { |
mbed_official | 610:813dcc80987e | 1027 | hcryp->pCrypOutBuffPtr = pOutputData; |
mbed_official | 610:813dcc80987e | 1028 | |
mbed_official | 610:813dcc80987e | 1029 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_FINAL_PHASE); |
mbed_official | 610:813dcc80987e | 1030 | |
mbed_official | 610:813dcc80987e | 1031 | /* if the header and payload phases have been bypassed, AES must be enabled again */ |
mbed_official | 610:813dcc80987e | 1032 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
mbed_official | 610:813dcc80987e | 1033 | { |
mbed_official | 610:813dcc80987e | 1034 | __HAL_CRYP_ENABLE(); |
mbed_official | 610:813dcc80987e | 1035 | } |
mbed_official | 610:813dcc80987e | 1036 | |
mbed_official | 610:813dcc80987e | 1037 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
mbed_official | 610:813dcc80987e | 1038 | { |
mbed_official | 610:813dcc80987e | 1039 | headerlength = hcryp->Init.HeaderSize * 8; /* Header length in bits */ |
mbed_official | 610:813dcc80987e | 1040 | inputlength = Size * 8; /* input length in bits */ |
mbed_official | 610:813dcc80987e | 1041 | /* Write the number of bits in the header on 64 bits followed by the number |
mbed_official | 610:813dcc80987e | 1042 | of bits in the payload on 64 bits as well */ |
mbed_official | 610:813dcc80987e | 1043 | if(hcryp->Init.DataType == CRYP_DATATYPE_1B) |
mbed_official | 610:813dcc80987e | 1044 | { |
mbed_official | 610:813dcc80987e | 1045 | hcryp->Instance->DINR = __RBIT((headerlength)>>32); |
mbed_official | 610:813dcc80987e | 1046 | hcryp->Instance->DINR = __RBIT(headerlength); |
mbed_official | 610:813dcc80987e | 1047 | hcryp->Instance->DINR = __RBIT((inputlength)>>32); |
mbed_official | 610:813dcc80987e | 1048 | hcryp->Instance->DINR = __RBIT(inputlength); |
mbed_official | 610:813dcc80987e | 1049 | } |
mbed_official | 610:813dcc80987e | 1050 | else if(hcryp->Init.DataType == CRYP_DATATYPE_8B) |
mbed_official | 610:813dcc80987e | 1051 | { |
mbed_official | 610:813dcc80987e | 1052 | hcryp->Instance->DINR = __REV((headerlength)>>32); |
mbed_official | 610:813dcc80987e | 1053 | hcryp->Instance->DINR = __REV(headerlength); |
mbed_official | 610:813dcc80987e | 1054 | hcryp->Instance->DINR = __REV((inputlength)>>32); |
mbed_official | 610:813dcc80987e | 1055 | hcryp->Instance->DINR = __REV(inputlength); |
mbed_official | 610:813dcc80987e | 1056 | } |
mbed_official | 610:813dcc80987e | 1057 | else if(hcryp->Init.DataType == CRYP_DATATYPE_16B) |
mbed_official | 610:813dcc80987e | 1058 | { |
mbed_official | 610:813dcc80987e | 1059 | hcryp->Instance->DINR = __ROR((headerlength)>>32, 16); |
mbed_official | 610:813dcc80987e | 1060 | hcryp->Instance->DINR = __ROR(headerlength, 16); |
mbed_official | 610:813dcc80987e | 1061 | hcryp->Instance->DINR = __ROR((inputlength)>>32, 16); |
mbed_official | 610:813dcc80987e | 1062 | hcryp->Instance->DINR = __ROR(inputlength, 16); |
mbed_official | 610:813dcc80987e | 1063 | } |
mbed_official | 610:813dcc80987e | 1064 | else if(hcryp->Init.DataType == CRYP_DATATYPE_32B) |
mbed_official | 610:813dcc80987e | 1065 | { |
mbed_official | 610:813dcc80987e | 1066 | hcryp->Instance->DINR = (uint32_t)(headerlength>>32); |
mbed_official | 610:813dcc80987e | 1067 | hcryp->Instance->DINR = (uint32_t)(headerlength); |
mbed_official | 610:813dcc80987e | 1068 | hcryp->Instance->DINR = (uint32_t)(inputlength>>32); |
mbed_official | 610:813dcc80987e | 1069 | hcryp->Instance->DINR = (uint32_t)(inputlength); |
mbed_official | 610:813dcc80987e | 1070 | } |
mbed_official | 610:813dcc80987e | 1071 | } |
mbed_official | 610:813dcc80987e | 1072 | else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
mbed_official | 610:813dcc80987e | 1073 | { |
mbed_official | 610:813dcc80987e | 1074 | inputaddr = (uint32_t)pInputData; |
mbed_official | 610:813dcc80987e | 1075 | /* Enter the last block made of a 128-bit value formatted |
mbed_official | 610:813dcc80987e | 1076 | from the original B0 packet. */ |
mbed_official | 610:813dcc80987e | 1077 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1078 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1079 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1080 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1081 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1082 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1083 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1084 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1085 | } |
mbed_official | 610:813dcc80987e | 1086 | } |
mbed_official | 610:813dcc80987e | 1087 | /*=================================================*/ |
mbed_official | 610:813dcc80987e | 1088 | /* case incorrect hcryp->Init.GCMCMACPhase setting */ |
mbed_official | 610:813dcc80987e | 1089 | /*=================================================*/ |
mbed_official | 610:813dcc80987e | 1090 | else |
mbed_official | 610:813dcc80987e | 1091 | { |
mbed_official | 610:813dcc80987e | 1092 | hcryp->State = HAL_CRYP_STATE_ERROR; |
mbed_official | 610:813dcc80987e | 1093 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 1094 | } |
mbed_official | 610:813dcc80987e | 1095 | |
mbed_official | 610:813dcc80987e | 1096 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 1097 | } |
mbed_official | 610:813dcc80987e | 1098 | else |
mbed_official | 610:813dcc80987e | 1099 | { |
mbed_official | 610:813dcc80987e | 1100 | return HAL_BUSY; |
mbed_official | 610:813dcc80987e | 1101 | } |
mbed_official | 610:813dcc80987e | 1102 | } |
mbed_official | 610:813dcc80987e | 1103 | |
mbed_official | 610:813dcc80987e | 1104 | |
mbed_official | 610:813dcc80987e | 1105 | |
mbed_official | 610:813dcc80987e | 1106 | |
mbed_official | 610:813dcc80987e | 1107 | /** |
mbed_official | 610:813dcc80987e | 1108 | * @brief Carry out in DMA mode the authentication tag generation as well as the ciphering or deciphering |
mbed_official | 610:813dcc80987e | 1109 | * operation according to hcryp->Init structure fields. |
mbed_official | 610:813dcc80987e | 1110 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 1111 | * the configuration information for CRYP module |
mbed_official | 610:813dcc80987e | 1112 | * @param pInputData: Pointer to payload data in GCM payload phase, |
mbed_official | 610:813dcc80987e | 1113 | * Parameter is meaningless in case of GCM/GMAC init, header and final phases, |
mbed_official | 610:813dcc80987e | 1114 | * Pointer to B0 blocks in CMAC header phase, |
mbed_official | 610:813dcc80987e | 1115 | * Pointer to C block in CMAC final phase. |
mbed_official | 610:813dcc80987e | 1116 | * @param Size: Length of the input payload data buffer in bytes, must be a multiple of 16, |
mbed_official | 610:813dcc80987e | 1117 | * Parameter is meaningless in case of GCM/GMAC init and header phases, |
mbed_official | 610:813dcc80987e | 1118 | * Length of B blocks (in bytes, must be a multiple of 16) in CMAC header phase, |
mbed_official | 610:813dcc80987e | 1119 | * Length of C block (in bytes) in CMAC final phase. |
mbed_official | 610:813dcc80987e | 1120 | * @param pOutputData: Pointer to plain or cipher text in GCM payload phase, |
mbed_official | 610:813dcc80987e | 1121 | * pointer to authentication tag in GCM/GMAC and CMAC final phases. |
mbed_official | 610:813dcc80987e | 1122 | * Parameter is meaningless in case of GCM/GMAC init and header phases |
mbed_official | 610:813dcc80987e | 1123 | * and in case of CMAC header phase. |
mbed_official | 610:813dcc80987e | 1124 | * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC. |
mbed_official | 610:813dcc80987e | 1125 | * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes |
mbed_official | 610:813dcc80987e | 1126 | * can be skipped by the user if so required. |
mbed_official | 610:813dcc80987e | 1127 | * @retval HAL status |
mbed_official | 610:813dcc80987e | 1128 | */ |
mbed_official | 610:813dcc80987e | 1129 | HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData) |
mbed_official | 610:813dcc80987e | 1130 | { |
mbed_official | 610:813dcc80987e | 1131 | uint32_t inputaddr = 0; |
mbed_official | 610:813dcc80987e | 1132 | uint32_t outputaddr = 0; |
mbed_official | 610:813dcc80987e | 1133 | uint32_t tagaddr = 0; |
mbed_official | 610:813dcc80987e | 1134 | uint64_t headerlength = 0; |
mbed_official | 610:813dcc80987e | 1135 | uint64_t inputlength = 0; |
mbed_official | 610:813dcc80987e | 1136 | |
mbed_official | 610:813dcc80987e | 1137 | |
mbed_official | 610:813dcc80987e | 1138 | if (hcryp->State == HAL_CRYP_STATE_READY) |
mbed_official | 610:813dcc80987e | 1139 | { |
mbed_official | 610:813dcc80987e | 1140 | /* input/output parameters check */ |
mbed_official | 610:813dcc80987e | 1141 | if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_HEADER_PHASE) |
mbed_official | 610:813dcc80987e | 1142 | { |
mbed_official | 610:813dcc80987e | 1143 | if ((hcryp->Init.Header == NULL) || (hcryp->Init.HeaderSize == 0)) |
mbed_official | 610:813dcc80987e | 1144 | { |
mbed_official | 610:813dcc80987e | 1145 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 1146 | } |
mbed_official | 610:813dcc80987e | 1147 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
mbed_official | 610:813dcc80987e | 1148 | { |
mbed_official | 610:813dcc80987e | 1149 | if ((pInputData == NULL) || (Size == 0)) |
mbed_official | 610:813dcc80987e | 1150 | { |
mbed_official | 610:813dcc80987e | 1151 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 1152 | } |
mbed_official | 610:813dcc80987e | 1153 | } |
mbed_official | 610:813dcc80987e | 1154 | } |
mbed_official | 610:813dcc80987e | 1155 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) |
mbed_official | 610:813dcc80987e | 1156 | { |
mbed_official | 610:813dcc80987e | 1157 | if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0)) |
mbed_official | 610:813dcc80987e | 1158 | { |
mbed_official | 610:813dcc80987e | 1159 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 1160 | } |
mbed_official | 610:813dcc80987e | 1161 | } |
mbed_official | 610:813dcc80987e | 1162 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_FINAL_PHASE) |
mbed_official | 610:813dcc80987e | 1163 | { |
mbed_official | 610:813dcc80987e | 1164 | if (pOutputData == NULL) |
mbed_official | 610:813dcc80987e | 1165 | { |
mbed_official | 610:813dcc80987e | 1166 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 1167 | } |
mbed_official | 610:813dcc80987e | 1168 | if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL)) |
mbed_official | 610:813dcc80987e | 1169 | { |
mbed_official | 610:813dcc80987e | 1170 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 1171 | } |
mbed_official | 610:813dcc80987e | 1172 | } |
mbed_official | 610:813dcc80987e | 1173 | |
mbed_official | 610:813dcc80987e | 1174 | |
mbed_official | 610:813dcc80987e | 1175 | /* Process Locked */ |
mbed_official | 610:813dcc80987e | 1176 | __HAL_LOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1177 | |
mbed_official | 610:813dcc80987e | 1178 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 1179 | hcryp->State = HAL_CRYP_STATE_BUSY; |
mbed_official | 610:813dcc80987e | 1180 | |
mbed_official | 610:813dcc80987e | 1181 | /*=====================*/ |
mbed_official | 610:813dcc80987e | 1182 | /* GCM/GMAC init phase */ |
mbed_official | 610:813dcc80987e | 1183 | /*=====================*/ |
mbed_official | 610:813dcc80987e | 1184 | /* In case of init phase, the input data (Key and Initialization Vector) have |
mbed_official | 610:813dcc80987e | 1185 | already been entered during the initialization process. No DMA transfer is |
mbed_official | 610:813dcc80987e | 1186 | required at that point therefore, the software just waits for the CCF flag |
mbed_official | 610:813dcc80987e | 1187 | to be raised. */ |
mbed_official | 610:813dcc80987e | 1188 | if (hcryp->Init.GCMCMACPhase == CRYP_GCM_INIT_PHASE) |
mbed_official | 610:813dcc80987e | 1189 | { |
mbed_official | 610:813dcc80987e | 1190 | /* just wait for hash computation */ |
mbed_official | 610:813dcc80987e | 1191 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
mbed_official | 610:813dcc80987e | 1192 | { |
mbed_official | 610:813dcc80987e | 1193 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1194 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1195 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 1196 | } |
mbed_official | 610:813dcc80987e | 1197 | |
mbed_official | 610:813dcc80987e | 1198 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 1199 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1200 | /* Mark that the initialization phase is over */ |
mbed_official | 610:813dcc80987e | 1201 | hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER; |
mbed_official | 610:813dcc80987e | 1202 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1203 | } |
mbed_official | 610:813dcc80987e | 1204 | /*===============================*/ |
mbed_official | 610:813dcc80987e | 1205 | /* GCM/GMAC or CMAC header phase */ |
mbed_official | 610:813dcc80987e | 1206 | /*===============================*/ |
mbed_official | 610:813dcc80987e | 1207 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_HEADER_PHASE) |
mbed_official | 610:813dcc80987e | 1208 | { |
mbed_official | 610:813dcc80987e | 1209 | /* Set header phase; for GCM or GMAC, set data-byte at this point */ |
mbed_official | 610:813dcc80987e | 1210 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
mbed_official | 610:813dcc80987e | 1211 | { |
mbed_official | 610:813dcc80987e | 1212 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_GCMCMAC_HEADER_PHASE|hcryp->Init.DataType); |
mbed_official | 610:813dcc80987e | 1213 | } |
mbed_official | 610:813dcc80987e | 1214 | else |
mbed_official | 610:813dcc80987e | 1215 | { |
mbed_official | 610:813dcc80987e | 1216 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_HEADER_PHASE); |
mbed_official | 610:813dcc80987e | 1217 | } |
mbed_official | 610:813dcc80987e | 1218 | |
mbed_official | 610:813dcc80987e | 1219 | /* enter first B0 block in polling mode (no DMA transfer for B0) */ |
mbed_official | 610:813dcc80987e | 1220 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
mbed_official | 610:813dcc80987e | 1221 | { |
mbed_official | 610:813dcc80987e | 1222 | /* Enable the CRYP peripheral */ |
mbed_official | 610:813dcc80987e | 1223 | __HAL_CRYP_ENABLE(); |
mbed_official | 610:813dcc80987e | 1224 | |
mbed_official | 610:813dcc80987e | 1225 | inputaddr = (uint32_t)pInputData; |
mbed_official | 610:813dcc80987e | 1226 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1227 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1228 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1229 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1230 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1231 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1232 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1233 | |
mbed_official | 610:813dcc80987e | 1234 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
mbed_official | 610:813dcc80987e | 1235 | { |
mbed_official | 610:813dcc80987e | 1236 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1237 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1238 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 1239 | } |
mbed_official | 610:813dcc80987e | 1240 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 1241 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1242 | } |
mbed_official | 610:813dcc80987e | 1243 | |
mbed_official | 610:813dcc80987e | 1244 | |
mbed_official | 610:813dcc80987e | 1245 | inputaddr = (uint32_t)hcryp->Init.Header; |
mbed_official | 610:813dcc80987e | 1246 | /* Set the input address and start DMA transfer */ |
mbed_official | 610:813dcc80987e | 1247 | CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, hcryp->Init.HeaderSize, 0); |
mbed_official | 610:813dcc80987e | 1248 | } |
mbed_official | 610:813dcc80987e | 1249 | /*========================*/ |
mbed_official | 610:813dcc80987e | 1250 | /* GCM/GMAC payload phase */ |
mbed_official | 610:813dcc80987e | 1251 | /*========================*/ |
mbed_official | 610:813dcc80987e | 1252 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) |
mbed_official | 610:813dcc80987e | 1253 | { |
mbed_official | 610:813dcc80987e | 1254 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCM_PAYLOAD_PHASE); |
mbed_official | 610:813dcc80987e | 1255 | |
mbed_official | 610:813dcc80987e | 1256 | inputaddr = (uint32_t)pInputData; |
mbed_official | 610:813dcc80987e | 1257 | outputaddr = (uint32_t)pOutputData; |
mbed_official | 610:813dcc80987e | 1258 | /* Set the input and output addresses and start DMA transfer */ |
mbed_official | 610:813dcc80987e | 1259 | CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, Size, outputaddr); |
mbed_official | 610:813dcc80987e | 1260 | } |
mbed_official | 610:813dcc80987e | 1261 | /*==============================*/ |
mbed_official | 610:813dcc80987e | 1262 | /* GCM/GMAC or CMAC final phase */ |
mbed_official | 610:813dcc80987e | 1263 | /*==============================*/ |
mbed_official | 610:813dcc80987e | 1264 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_FINAL_PHASE) |
mbed_official | 610:813dcc80987e | 1265 | { |
mbed_official | 610:813dcc80987e | 1266 | tagaddr = (uint32_t)pOutputData; |
mbed_official | 610:813dcc80987e | 1267 | |
mbed_official | 610:813dcc80987e | 1268 | MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_FINAL_PHASE); |
mbed_official | 610:813dcc80987e | 1269 | |
mbed_official | 610:813dcc80987e | 1270 | /* if the header and payload phases have been bypassed, AES must be enabled again */ |
mbed_official | 610:813dcc80987e | 1271 | if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER) |
mbed_official | 610:813dcc80987e | 1272 | { |
mbed_official | 610:813dcc80987e | 1273 | __HAL_CRYP_ENABLE(); |
mbed_official | 610:813dcc80987e | 1274 | } |
mbed_official | 610:813dcc80987e | 1275 | |
mbed_official | 610:813dcc80987e | 1276 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) |
mbed_official | 610:813dcc80987e | 1277 | { |
mbed_official | 610:813dcc80987e | 1278 | headerlength = hcryp->Init.HeaderSize * 8; /* Header length in bits */ |
mbed_official | 610:813dcc80987e | 1279 | inputlength = Size * 8; /* input length in bits */ |
mbed_official | 610:813dcc80987e | 1280 | /* Write the number of bits in the header on 64 bits followed by the number |
mbed_official | 610:813dcc80987e | 1281 | of bits in the payload on 64 bits as well */ |
mbed_official | 610:813dcc80987e | 1282 | if(hcryp->Init.DataType == CRYP_DATATYPE_1B) |
mbed_official | 610:813dcc80987e | 1283 | { |
mbed_official | 610:813dcc80987e | 1284 | hcryp->Instance->DINR = __RBIT((headerlength)>>32); |
mbed_official | 610:813dcc80987e | 1285 | hcryp->Instance->DINR = __RBIT(headerlength); |
mbed_official | 610:813dcc80987e | 1286 | hcryp->Instance->DINR = __RBIT((inputlength)>>32); |
mbed_official | 610:813dcc80987e | 1287 | hcryp->Instance->DINR = __RBIT(inputlength); |
mbed_official | 610:813dcc80987e | 1288 | } |
mbed_official | 610:813dcc80987e | 1289 | else if(hcryp->Init.DataType == CRYP_DATATYPE_8B) |
mbed_official | 610:813dcc80987e | 1290 | { |
mbed_official | 610:813dcc80987e | 1291 | hcryp->Instance->DINR = __REV((headerlength)>>32); |
mbed_official | 610:813dcc80987e | 1292 | hcryp->Instance->DINR = __REV(headerlength); |
mbed_official | 610:813dcc80987e | 1293 | hcryp->Instance->DINR = __REV((inputlength)>>32); |
mbed_official | 610:813dcc80987e | 1294 | hcryp->Instance->DINR = __REV(inputlength); |
mbed_official | 610:813dcc80987e | 1295 | } |
mbed_official | 610:813dcc80987e | 1296 | else if(hcryp->Init.DataType == CRYP_DATATYPE_16B) |
mbed_official | 610:813dcc80987e | 1297 | { |
mbed_official | 610:813dcc80987e | 1298 | hcryp->Instance->DINR = __ROR((headerlength)>>32, 16); |
mbed_official | 610:813dcc80987e | 1299 | hcryp->Instance->DINR = __ROR(headerlength, 16); |
mbed_official | 610:813dcc80987e | 1300 | hcryp->Instance->DINR = __ROR((inputlength)>>32, 16); |
mbed_official | 610:813dcc80987e | 1301 | hcryp->Instance->DINR = __ROR(inputlength, 16); |
mbed_official | 610:813dcc80987e | 1302 | } |
mbed_official | 610:813dcc80987e | 1303 | else if(hcryp->Init.DataType == CRYP_DATATYPE_32B) |
mbed_official | 610:813dcc80987e | 1304 | { |
mbed_official | 610:813dcc80987e | 1305 | hcryp->Instance->DINR = (uint32_t)(headerlength>>32); |
mbed_official | 610:813dcc80987e | 1306 | hcryp->Instance->DINR = (uint32_t)(headerlength); |
mbed_official | 610:813dcc80987e | 1307 | hcryp->Instance->DINR = (uint32_t)(inputlength>>32); |
mbed_official | 610:813dcc80987e | 1308 | hcryp->Instance->DINR = (uint32_t)(inputlength); |
mbed_official | 610:813dcc80987e | 1309 | } |
mbed_official | 610:813dcc80987e | 1310 | } |
mbed_official | 610:813dcc80987e | 1311 | else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
mbed_official | 610:813dcc80987e | 1312 | { |
mbed_official | 610:813dcc80987e | 1313 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1314 | |
mbed_official | 610:813dcc80987e | 1315 | inputaddr = (uint32_t)pInputData; |
mbed_official | 610:813dcc80987e | 1316 | /* Enter the last block made of a 128-bit value formatted |
mbed_official | 610:813dcc80987e | 1317 | from the original B0 packet. */ |
mbed_official | 610:813dcc80987e | 1318 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1319 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1320 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1321 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1322 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1323 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1324 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1325 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1326 | } |
mbed_official | 610:813dcc80987e | 1327 | |
mbed_official | 610:813dcc80987e | 1328 | /* No DMA transfer is required at that point therefore, the software |
mbed_official | 610:813dcc80987e | 1329 | just waits for the CCF flag to be raised. */ |
mbed_official | 610:813dcc80987e | 1330 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
mbed_official | 610:813dcc80987e | 1331 | { |
mbed_official | 610:813dcc80987e | 1332 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1333 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1334 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 1335 | } |
mbed_official | 610:813dcc80987e | 1336 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 1337 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1338 | /* Read the Auth TAG in the IN FIFO */ |
mbed_official | 610:813dcc80987e | 1339 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 1340 | tagaddr+=4; |
mbed_official | 610:813dcc80987e | 1341 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 1342 | tagaddr+=4; |
mbed_official | 610:813dcc80987e | 1343 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 1344 | tagaddr+=4; |
mbed_official | 610:813dcc80987e | 1345 | *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 1346 | |
mbed_official | 610:813dcc80987e | 1347 | /* Mark that the final phase is over */ |
mbed_official | 610:813dcc80987e | 1348 | hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER; |
mbed_official | 610:813dcc80987e | 1349 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1350 | /* Disable the Peripheral */ |
mbed_official | 610:813dcc80987e | 1351 | __HAL_CRYP_DISABLE(); |
mbed_official | 610:813dcc80987e | 1352 | } |
mbed_official | 610:813dcc80987e | 1353 | /*=================================================*/ |
mbed_official | 610:813dcc80987e | 1354 | /* case incorrect hcryp->Init.GCMCMACPhase setting */ |
mbed_official | 610:813dcc80987e | 1355 | /*=================================================*/ |
mbed_official | 610:813dcc80987e | 1356 | else |
mbed_official | 610:813dcc80987e | 1357 | { |
mbed_official | 610:813dcc80987e | 1358 | hcryp->State = HAL_CRYP_STATE_ERROR; |
mbed_official | 610:813dcc80987e | 1359 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1360 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 1361 | } |
mbed_official | 610:813dcc80987e | 1362 | |
mbed_official | 610:813dcc80987e | 1363 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 1364 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1365 | |
mbed_official | 610:813dcc80987e | 1366 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 1367 | } |
mbed_official | 610:813dcc80987e | 1368 | else |
mbed_official | 610:813dcc80987e | 1369 | { |
mbed_official | 610:813dcc80987e | 1370 | return HAL_BUSY; |
mbed_official | 610:813dcc80987e | 1371 | } |
mbed_official | 610:813dcc80987e | 1372 | } |
mbed_official | 610:813dcc80987e | 1373 | |
mbed_official | 610:813dcc80987e | 1374 | /** |
mbed_official | 610:813dcc80987e | 1375 | * @} |
mbed_official | 610:813dcc80987e | 1376 | */ |
mbed_official | 610:813dcc80987e | 1377 | |
mbed_official | 610:813dcc80987e | 1378 | /** @defgroup CRYPEx_Exported_Functions_Group3 AES suspension/resumption functions |
mbed_official | 610:813dcc80987e | 1379 | * @brief Extended processing functions. |
mbed_official | 610:813dcc80987e | 1380 | * |
mbed_official | 610:813dcc80987e | 1381 | @verbatim |
mbed_official | 610:813dcc80987e | 1382 | ============================================================================== |
mbed_official | 610:813dcc80987e | 1383 | ##### AES extended suspension and resumption functions ##### |
mbed_official | 610:813dcc80987e | 1384 | ============================================================================== |
mbed_official | 610:813dcc80987e | 1385 | [..] This section provides functions allowing to: |
mbed_official | 610:813dcc80987e | 1386 | (+) save in memory the Initialization Vector, the Key registers, the Control register or |
mbed_official | 610:813dcc80987e | 1387 | the Suspend registers when a process is suspended by a higher priority message |
mbed_official | 610:813dcc80987e | 1388 | (+) write back in CRYP hardware block the saved values listed above when the suspended |
mbed_official | 610:813dcc80987e | 1389 | lower priority message processing is resumed. |
mbed_official | 610:813dcc80987e | 1390 | |
mbed_official | 610:813dcc80987e | 1391 | @endverbatim |
mbed_official | 610:813dcc80987e | 1392 | * @{ |
mbed_official | 610:813dcc80987e | 1393 | */ |
mbed_official | 610:813dcc80987e | 1394 | |
mbed_official | 610:813dcc80987e | 1395 | |
mbed_official | 610:813dcc80987e | 1396 | /** |
mbed_official | 610:813dcc80987e | 1397 | * @brief In case of message processing suspension, read the Initialization Vector. |
mbed_official | 610:813dcc80987e | 1398 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 1399 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 1400 | * @param Output: Pointer to the buffer containing the saved Initialization Vector. |
mbed_official | 610:813dcc80987e | 1401 | * @note This value has to be stored for reuse by writing the AES_IVRx registers |
mbed_official | 610:813dcc80987e | 1402 | * as soon as the interrupted processing has to be resumed. |
mbed_official | 610:813dcc80987e | 1403 | * Applicable to all chaining modes. |
mbed_official | 610:813dcc80987e | 1404 | * @note AES must be disabled when reading or resetting the IV values. |
mbed_official | 610:813dcc80987e | 1405 | * @retval None |
mbed_official | 610:813dcc80987e | 1406 | */ |
mbed_official | 610:813dcc80987e | 1407 | void HAL_CRYPEx_Read_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output) |
mbed_official | 610:813dcc80987e | 1408 | { |
mbed_official | 610:813dcc80987e | 1409 | uint32_t outputaddr = (uint32_t)Output; |
mbed_official | 610:813dcc80987e | 1410 | |
mbed_official | 610:813dcc80987e | 1411 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR3); |
mbed_official | 610:813dcc80987e | 1412 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1413 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR2); |
mbed_official | 610:813dcc80987e | 1414 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1415 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR1); |
mbed_official | 610:813dcc80987e | 1416 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1417 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR0); |
mbed_official | 610:813dcc80987e | 1418 | } |
mbed_official | 610:813dcc80987e | 1419 | |
mbed_official | 610:813dcc80987e | 1420 | /** |
mbed_official | 610:813dcc80987e | 1421 | * @brief In case of message processing resumption, rewrite the Initialization |
mbed_official | 610:813dcc80987e | 1422 | * Vector in the AES_IVRx registers. |
mbed_official | 610:813dcc80987e | 1423 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 1424 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 1425 | * @param Input: Pointer to the buffer containing the saved Initialization Vector to |
mbed_official | 610:813dcc80987e | 1426 | * write back in the CRYP hardware block. |
mbed_official | 610:813dcc80987e | 1427 | * @note Applicable to all chaining modes. |
mbed_official | 610:813dcc80987e | 1428 | * @note AES must be disabled when reading or resetting the IV values. |
mbed_official | 610:813dcc80987e | 1429 | * @retval None |
mbed_official | 610:813dcc80987e | 1430 | */ |
mbed_official | 610:813dcc80987e | 1431 | void HAL_CRYPEx_Write_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input) |
mbed_official | 610:813dcc80987e | 1432 | { |
mbed_official | 610:813dcc80987e | 1433 | uint32_t ivaddr = (uint32_t)Input; |
mbed_official | 610:813dcc80987e | 1434 | |
mbed_official | 610:813dcc80987e | 1435 | hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 610:813dcc80987e | 1436 | ivaddr+=4; |
mbed_official | 610:813dcc80987e | 1437 | hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 610:813dcc80987e | 1438 | ivaddr+=4; |
mbed_official | 610:813dcc80987e | 1439 | hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 610:813dcc80987e | 1440 | ivaddr+=4; |
mbed_official | 610:813dcc80987e | 1441 | hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 610:813dcc80987e | 1442 | } |
mbed_official | 610:813dcc80987e | 1443 | |
mbed_official | 610:813dcc80987e | 1444 | |
mbed_official | 610:813dcc80987e | 1445 | /** |
mbed_official | 610:813dcc80987e | 1446 | * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Suspend Registers. |
mbed_official | 610:813dcc80987e | 1447 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 1448 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 1449 | * @param Output: Pointer to the buffer containing the saved Suspend Registers. |
mbed_official | 610:813dcc80987e | 1450 | * @note These values have to be stored for reuse by writing back the AES_SUSPxR registers |
mbed_official | 610:813dcc80987e | 1451 | * as soon as the interrupted processing has to be resumed. |
mbed_official | 610:813dcc80987e | 1452 | * @retval None |
mbed_official | 610:813dcc80987e | 1453 | */ |
mbed_official | 610:813dcc80987e | 1454 | void HAL_CRYPEx_Read_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output) |
mbed_official | 610:813dcc80987e | 1455 | { |
mbed_official | 610:813dcc80987e | 1456 | uint32_t outputaddr = (uint32_t)Output; |
mbed_official | 610:813dcc80987e | 1457 | |
mbed_official | 610:813dcc80987e | 1458 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP7R); |
mbed_official | 610:813dcc80987e | 1459 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1460 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP6R); |
mbed_official | 610:813dcc80987e | 1461 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1462 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP5R); |
mbed_official | 610:813dcc80987e | 1463 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1464 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP4R); |
mbed_official | 610:813dcc80987e | 1465 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1466 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP3R); |
mbed_official | 610:813dcc80987e | 1467 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1468 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP2R); |
mbed_official | 610:813dcc80987e | 1469 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1470 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP1R); |
mbed_official | 610:813dcc80987e | 1471 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1472 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP0R); |
mbed_official | 610:813dcc80987e | 1473 | } |
mbed_official | 610:813dcc80987e | 1474 | |
mbed_official | 610:813dcc80987e | 1475 | /** |
mbed_official | 610:813dcc80987e | 1476 | * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Suspend |
mbed_official | 610:813dcc80987e | 1477 | * Registers in the AES_SUSPxR registers. |
mbed_official | 610:813dcc80987e | 1478 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 1479 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 1480 | * @param Input: Pointer to the buffer containing the saved suspend registers to |
mbed_official | 610:813dcc80987e | 1481 | * write back in the CRYP hardware block. |
mbed_official | 610:813dcc80987e | 1482 | * @retval None |
mbed_official | 610:813dcc80987e | 1483 | */ |
mbed_official | 610:813dcc80987e | 1484 | void HAL_CRYPEx_Write_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input) |
mbed_official | 610:813dcc80987e | 1485 | { |
mbed_official | 610:813dcc80987e | 1486 | uint32_t ivaddr = (uint32_t)Input; |
mbed_official | 610:813dcc80987e | 1487 | |
mbed_official | 610:813dcc80987e | 1488 | hcryp->Instance->SUSP7R = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 610:813dcc80987e | 1489 | ivaddr+=4; |
mbed_official | 610:813dcc80987e | 1490 | hcryp->Instance->SUSP6R = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 610:813dcc80987e | 1491 | ivaddr+=4; |
mbed_official | 610:813dcc80987e | 1492 | hcryp->Instance->SUSP5R = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 610:813dcc80987e | 1493 | ivaddr+=4; |
mbed_official | 610:813dcc80987e | 1494 | hcryp->Instance->SUSP4R = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 610:813dcc80987e | 1495 | ivaddr+=4; |
mbed_official | 610:813dcc80987e | 1496 | hcryp->Instance->SUSP3R = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 610:813dcc80987e | 1497 | ivaddr+=4; |
mbed_official | 610:813dcc80987e | 1498 | hcryp->Instance->SUSP2R = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 610:813dcc80987e | 1499 | ivaddr+=4; |
mbed_official | 610:813dcc80987e | 1500 | hcryp->Instance->SUSP1R = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 610:813dcc80987e | 1501 | ivaddr+=4; |
mbed_official | 610:813dcc80987e | 1502 | hcryp->Instance->SUSP0R = __REV(*(uint32_t*)(ivaddr)); |
mbed_official | 610:813dcc80987e | 1503 | } |
mbed_official | 610:813dcc80987e | 1504 | |
mbed_official | 610:813dcc80987e | 1505 | |
mbed_official | 610:813dcc80987e | 1506 | /** |
mbed_official | 610:813dcc80987e | 1507 | * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Key Registers. |
mbed_official | 610:813dcc80987e | 1508 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 1509 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 1510 | * @param Output: Pointer to the buffer containing the saved Key Registers. |
mbed_official | 610:813dcc80987e | 1511 | * @param KeySize: Indicates the key size (128 or 256 bits). |
mbed_official | 610:813dcc80987e | 1512 | * @note These values have to be stored for reuse by writing back the AES_KEYRx registers |
mbed_official | 610:813dcc80987e | 1513 | * as soon as the interrupted processing has to be resumed. |
mbed_official | 610:813dcc80987e | 1514 | * @retval None |
mbed_official | 610:813dcc80987e | 1515 | */ |
mbed_official | 610:813dcc80987e | 1516 | void HAL_CRYPEx_Read_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t KeySize) |
mbed_official | 610:813dcc80987e | 1517 | { |
mbed_official | 610:813dcc80987e | 1518 | uint32_t keyaddr = (uint32_t)Output; |
mbed_official | 610:813dcc80987e | 1519 | |
mbed_official | 610:813dcc80987e | 1520 | if (KeySize == CRYP_KEYSIZE_256B) |
mbed_official | 610:813dcc80987e | 1521 | { |
mbed_official | 610:813dcc80987e | 1522 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR7); |
mbed_official | 610:813dcc80987e | 1523 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1524 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR6); |
mbed_official | 610:813dcc80987e | 1525 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1526 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR5); |
mbed_official | 610:813dcc80987e | 1527 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1528 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR4); |
mbed_official | 610:813dcc80987e | 1529 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1530 | } |
mbed_official | 610:813dcc80987e | 1531 | |
mbed_official | 610:813dcc80987e | 1532 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR3); |
mbed_official | 610:813dcc80987e | 1533 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1534 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR2); |
mbed_official | 610:813dcc80987e | 1535 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1536 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR1); |
mbed_official | 610:813dcc80987e | 1537 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1538 | *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR0); |
mbed_official | 610:813dcc80987e | 1539 | } |
mbed_official | 610:813dcc80987e | 1540 | |
mbed_official | 610:813dcc80987e | 1541 | /** |
mbed_official | 610:813dcc80987e | 1542 | * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Key |
mbed_official | 610:813dcc80987e | 1543 | * Registers in the AES_KEYRx registers. |
mbed_official | 610:813dcc80987e | 1544 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 1545 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 1546 | * @param Input: Pointer to the buffer containing the saved key registers to |
mbed_official | 610:813dcc80987e | 1547 | * write back in the CRYP hardware block. |
mbed_official | 610:813dcc80987e | 1548 | * @param KeySize: Indicates the key size (128 or 256 bits) |
mbed_official | 610:813dcc80987e | 1549 | * @retval None |
mbed_official | 610:813dcc80987e | 1550 | */ |
mbed_official | 610:813dcc80987e | 1551 | void HAL_CRYPEx_Write_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint32_t KeySize) |
mbed_official | 610:813dcc80987e | 1552 | { |
mbed_official | 610:813dcc80987e | 1553 | uint32_t keyaddr = (uint32_t)Input; |
mbed_official | 610:813dcc80987e | 1554 | |
mbed_official | 610:813dcc80987e | 1555 | if (KeySize == CRYP_KEYSIZE_256B) |
mbed_official | 610:813dcc80987e | 1556 | { |
mbed_official | 610:813dcc80987e | 1557 | hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 610:813dcc80987e | 1558 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1559 | hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 610:813dcc80987e | 1560 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1561 | hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 610:813dcc80987e | 1562 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1563 | hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 610:813dcc80987e | 1564 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1565 | } |
mbed_official | 610:813dcc80987e | 1566 | |
mbed_official | 610:813dcc80987e | 1567 | hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 610:813dcc80987e | 1568 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1569 | hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 610:813dcc80987e | 1570 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1571 | hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 610:813dcc80987e | 1572 | keyaddr+=4; |
mbed_official | 610:813dcc80987e | 1573 | hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr)); |
mbed_official | 610:813dcc80987e | 1574 | } |
mbed_official | 610:813dcc80987e | 1575 | |
mbed_official | 610:813dcc80987e | 1576 | |
mbed_official | 610:813dcc80987e | 1577 | /** |
mbed_official | 610:813dcc80987e | 1578 | * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Control Register. |
mbed_official | 610:813dcc80987e | 1579 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 1580 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 1581 | * @param Output: Pointer to the buffer containing the saved Control Register. |
mbed_official | 610:813dcc80987e | 1582 | * @note This values has to be stored for reuse by writing back the AES_CR register |
mbed_official | 610:813dcc80987e | 1583 | * as soon as the interrupted processing has to be resumed. |
mbed_official | 610:813dcc80987e | 1584 | * @retval None |
mbed_official | 610:813dcc80987e | 1585 | */ |
mbed_official | 610:813dcc80987e | 1586 | void HAL_CRYPEx_Read_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Output) |
mbed_official | 610:813dcc80987e | 1587 | { |
mbed_official | 610:813dcc80987e | 1588 | *(uint32_t*)(Output) = hcryp->Instance->CR; |
mbed_official | 610:813dcc80987e | 1589 | } |
mbed_official | 610:813dcc80987e | 1590 | |
mbed_official | 610:813dcc80987e | 1591 | /** |
mbed_official | 610:813dcc80987e | 1592 | * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Control |
mbed_official | 610:813dcc80987e | 1593 | * Registers in the AES_CR register. |
mbed_official | 610:813dcc80987e | 1594 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 1595 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 1596 | * @param Input: Pointer to the buffer containing the saved Control Register to |
mbed_official | 610:813dcc80987e | 1597 | * write back in the CRYP hardware block. |
mbed_official | 610:813dcc80987e | 1598 | * @retval None |
mbed_official | 610:813dcc80987e | 1599 | */ |
mbed_official | 610:813dcc80987e | 1600 | void HAL_CRYPEx_Write_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Input) |
mbed_official | 610:813dcc80987e | 1601 | { |
mbed_official | 610:813dcc80987e | 1602 | hcryp->Instance->CR = *(uint32_t*)(Input); |
mbed_official | 610:813dcc80987e | 1603 | } |
mbed_official | 610:813dcc80987e | 1604 | |
mbed_official | 610:813dcc80987e | 1605 | /** |
mbed_official | 610:813dcc80987e | 1606 | * @} |
mbed_official | 610:813dcc80987e | 1607 | */ |
mbed_official | 610:813dcc80987e | 1608 | |
mbed_official | 610:813dcc80987e | 1609 | /** |
mbed_official | 610:813dcc80987e | 1610 | * @} |
mbed_official | 610:813dcc80987e | 1611 | */ |
mbed_official | 610:813dcc80987e | 1612 | |
mbed_official | 610:813dcc80987e | 1613 | /** @addtogroup CRYPEx_Private_Functions |
mbed_official | 610:813dcc80987e | 1614 | * @{ |
mbed_official | 610:813dcc80987e | 1615 | */ |
mbed_official | 610:813dcc80987e | 1616 | |
mbed_official | 610:813dcc80987e | 1617 | /** |
mbed_official | 610:813dcc80987e | 1618 | * @brief DMA CRYP Input Data process complete callback |
mbed_official | 610:813dcc80987e | 1619 | * for GCM, GMAC or CMAC chainging modes. |
mbed_official | 610:813dcc80987e | 1620 | * @note Specific setting of hcryp fields are required only |
mbed_official | 610:813dcc80987e | 1621 | * in the case of header phase where no output data DMA |
mbed_official | 610:813dcc80987e | 1622 | * transfer is on-going (only input data transfer is enabled |
mbed_official | 610:813dcc80987e | 1623 | * in such a case). |
mbed_official | 610:813dcc80987e | 1624 | * @param hdma: DMA handle. |
mbed_official | 610:813dcc80987e | 1625 | * @retval None |
mbed_official | 610:813dcc80987e | 1626 | */ |
mbed_official | 610:813dcc80987e | 1627 | static void CRYP_GCMCMAC_DMAInCplt(DMA_HandleTypeDef *hdma) |
mbed_official | 610:813dcc80987e | 1628 | { |
mbed_official | 610:813dcc80987e | 1629 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
mbed_official | 610:813dcc80987e | 1630 | |
mbed_official | 610:813dcc80987e | 1631 | /* Disable the DMA transfer for input request */ |
mbed_official | 610:813dcc80987e | 1632 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN); |
mbed_official | 610:813dcc80987e | 1633 | |
mbed_official | 610:813dcc80987e | 1634 | if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_HEADER_PHASE) |
mbed_official | 610:813dcc80987e | 1635 | { |
mbed_official | 610:813dcc80987e | 1636 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 1637 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1638 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 1639 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1640 | |
mbed_official | 610:813dcc80987e | 1641 | /* Mark that the header phase is over */ |
mbed_official | 610:813dcc80987e | 1642 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
mbed_official | 610:813dcc80987e | 1643 | } |
mbed_official | 610:813dcc80987e | 1644 | |
mbed_official | 610:813dcc80987e | 1645 | /* Call input data transfer complete callback */ |
mbed_official | 610:813dcc80987e | 1646 | HAL_CRYP_InCpltCallback(hcryp); |
mbed_official | 610:813dcc80987e | 1647 | } |
mbed_official | 610:813dcc80987e | 1648 | |
mbed_official | 610:813dcc80987e | 1649 | /** |
mbed_official | 610:813dcc80987e | 1650 | * @brief DMA CRYP Output Data process complete callback |
mbed_official | 610:813dcc80987e | 1651 | * for GCM, GMAC or CMAC chainging modes. |
mbed_official | 610:813dcc80987e | 1652 | * @note This callback is called only in the payload phase. |
mbed_official | 610:813dcc80987e | 1653 | * @param hdma: DMA handle. |
mbed_official | 610:813dcc80987e | 1654 | * @retval None |
mbed_official | 610:813dcc80987e | 1655 | */ |
mbed_official | 610:813dcc80987e | 1656 | static void CRYP_GCMCMAC_DMAOutCplt(DMA_HandleTypeDef *hdma) |
mbed_official | 610:813dcc80987e | 1657 | { |
mbed_official | 610:813dcc80987e | 1658 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
mbed_official | 610:813dcc80987e | 1659 | |
mbed_official | 610:813dcc80987e | 1660 | /* Disable the DMA transfer for output request */ |
mbed_official | 610:813dcc80987e | 1661 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN); |
mbed_official | 610:813dcc80987e | 1662 | |
mbed_official | 610:813dcc80987e | 1663 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 1664 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1665 | |
mbed_official | 610:813dcc80987e | 1666 | /* Change the CRYP state to ready */ |
mbed_official | 610:813dcc80987e | 1667 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1668 | /* Mark that the payload phase is over */ |
mbed_official | 610:813dcc80987e | 1669 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
mbed_official | 610:813dcc80987e | 1670 | |
mbed_official | 610:813dcc80987e | 1671 | /* Call output data transfer complete callback */ |
mbed_official | 610:813dcc80987e | 1672 | HAL_CRYP_OutCpltCallback(hcryp); |
mbed_official | 610:813dcc80987e | 1673 | } |
mbed_official | 610:813dcc80987e | 1674 | |
mbed_official | 610:813dcc80987e | 1675 | /** |
mbed_official | 610:813dcc80987e | 1676 | * @brief DMA CRYP communication error callback |
mbed_official | 610:813dcc80987e | 1677 | * for GCM, GMAC or CMAC chainging modes. |
mbed_official | 610:813dcc80987e | 1678 | * @param hdma: DMA handle |
mbed_official | 610:813dcc80987e | 1679 | * @retval None |
mbed_official | 610:813dcc80987e | 1680 | */ |
mbed_official | 610:813dcc80987e | 1681 | static void CRYP_GCMCMAC_DMAError(DMA_HandleTypeDef *hdma) |
mbed_official | 610:813dcc80987e | 1682 | { |
mbed_official | 610:813dcc80987e | 1683 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
mbed_official | 610:813dcc80987e | 1684 | |
mbed_official | 610:813dcc80987e | 1685 | hcryp->State= HAL_CRYP_STATE_ERROR; |
mbed_official | 610:813dcc80987e | 1686 | hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR; |
mbed_official | 610:813dcc80987e | 1687 | HAL_CRYP_ErrorCallback(hcryp); |
mbed_official | 610:813dcc80987e | 1688 | /* Clear Error Flag */ |
mbed_official | 610:813dcc80987e | 1689 | __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR); |
mbed_official | 610:813dcc80987e | 1690 | } |
mbed_official | 610:813dcc80987e | 1691 | |
mbed_official | 610:813dcc80987e | 1692 | |
mbed_official | 610:813dcc80987e | 1693 | |
mbed_official | 610:813dcc80987e | 1694 | /** |
mbed_official | 610:813dcc80987e | 1695 | * @brief Handle CRYP block input/output data handling under interruption |
mbed_official | 610:813dcc80987e | 1696 | * for GCM, GMAC or CMAC chainging modes. |
mbed_official | 610:813dcc80987e | 1697 | * @note The function is called under interruption only, once |
mbed_official | 610:813dcc80987e | 1698 | * interruptions have been enabled by HAL_CRYPEx_AES_Auth_IT(). |
mbed_official | 610:813dcc80987e | 1699 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 1700 | * the configuration information for CRYP module |
mbed_official | 610:813dcc80987e | 1701 | * @retval HAL status |
mbed_official | 610:813dcc80987e | 1702 | */ |
mbed_official | 610:813dcc80987e | 1703 | HAL_StatusTypeDef CRYP_AES_Auth_IT(CRYP_HandleTypeDef *hcryp) |
mbed_official | 610:813dcc80987e | 1704 | { |
mbed_official | 610:813dcc80987e | 1705 | uint32_t inputaddr = 0x0; |
mbed_official | 610:813dcc80987e | 1706 | uint32_t outputaddr = 0x0; |
mbed_official | 610:813dcc80987e | 1707 | |
mbed_official | 610:813dcc80987e | 1708 | if(hcryp->State == HAL_CRYP_STATE_BUSY) |
mbed_official | 610:813dcc80987e | 1709 | { |
mbed_official | 610:813dcc80987e | 1710 | /*=====================*/ |
mbed_official | 610:813dcc80987e | 1711 | /* GCM/GMAC init phase */ |
mbed_official | 610:813dcc80987e | 1712 | /*=====================*/ |
mbed_official | 610:813dcc80987e | 1713 | if (hcryp->Init.GCMCMACPhase == CRYP_GCM_INIT_PHASE) |
mbed_official | 610:813dcc80987e | 1714 | { |
mbed_official | 610:813dcc80987e | 1715 | /* Clear Computation Complete Flag */ |
mbed_official | 610:813dcc80987e | 1716 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1717 | /* Disable Computation Complete Flag and Errors Interrupts */ |
mbed_official | 610:813dcc80987e | 1718 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
mbed_official | 610:813dcc80987e | 1719 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 1720 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1721 | |
mbed_official | 610:813dcc80987e | 1722 | /* Mark that the initialization phase is over */ |
mbed_official | 610:813dcc80987e | 1723 | hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER; |
mbed_official | 610:813dcc80987e | 1724 | |
mbed_official | 610:813dcc80987e | 1725 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 1726 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1727 | /* Call computation complete callback */ |
mbed_official | 610:813dcc80987e | 1728 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
mbed_official | 610:813dcc80987e | 1729 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 1730 | } |
mbed_official | 610:813dcc80987e | 1731 | /*===============================*/ |
mbed_official | 610:813dcc80987e | 1732 | /* GCM/GMAC or CMAC header phase */ |
mbed_official | 610:813dcc80987e | 1733 | /*===============================*/ |
mbed_official | 610:813dcc80987e | 1734 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_HEADER_PHASE) |
mbed_official | 610:813dcc80987e | 1735 | { |
mbed_official | 610:813dcc80987e | 1736 | /* Check if all input header data have been entered */ |
mbed_official | 610:813dcc80987e | 1737 | if (hcryp->CrypInCount == 0) |
mbed_official | 610:813dcc80987e | 1738 | { |
mbed_official | 610:813dcc80987e | 1739 | /* Clear Computation Complete Flag */ |
mbed_official | 610:813dcc80987e | 1740 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1741 | /* Disable Computation Complete Flag and Errors Interrupts */ |
mbed_official | 610:813dcc80987e | 1742 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
mbed_official | 610:813dcc80987e | 1743 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 1744 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1745 | /* Mark that the header phase is over */ |
mbed_official | 610:813dcc80987e | 1746 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER; |
mbed_official | 610:813dcc80987e | 1747 | |
mbed_official | 610:813dcc80987e | 1748 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 1749 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1750 | |
mbed_official | 610:813dcc80987e | 1751 | /* Call computation complete callback */ |
mbed_official | 610:813dcc80987e | 1752 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
mbed_official | 610:813dcc80987e | 1753 | |
mbed_official | 610:813dcc80987e | 1754 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 1755 | } |
mbed_official | 610:813dcc80987e | 1756 | /* If suspension flag has been raised, suspend processing */ |
mbed_official | 610:813dcc80987e | 1757 | else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND) |
mbed_official | 610:813dcc80987e | 1758 | { |
mbed_official | 610:813dcc80987e | 1759 | /* Ensure that CCF flag is set */ |
mbed_official | 610:813dcc80987e | 1760 | if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK) |
mbed_official | 610:813dcc80987e | 1761 | { |
mbed_official | 610:813dcc80987e | 1762 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1763 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1764 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 1765 | } |
mbed_official | 610:813dcc80987e | 1766 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 1767 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1768 | |
mbed_official | 610:813dcc80987e | 1769 | /* reset SuspendRequest */ |
mbed_official | 610:813dcc80987e | 1770 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
mbed_official | 610:813dcc80987e | 1771 | /* Disable Computation Complete Flag and Errors Interrupts */ |
mbed_official | 610:813dcc80987e | 1772 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
mbed_official | 610:813dcc80987e | 1773 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 1774 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
mbed_official | 610:813dcc80987e | 1775 | /* Mark that the header phase is over */ |
mbed_official | 610:813dcc80987e | 1776 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
mbed_official | 610:813dcc80987e | 1777 | |
mbed_official | 610:813dcc80987e | 1778 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 1779 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1780 | |
mbed_official | 610:813dcc80987e | 1781 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 1782 | } |
mbed_official | 610:813dcc80987e | 1783 | else /* Carry on feeding input data to the CRYP hardware block */ |
mbed_official | 610:813dcc80987e | 1784 | { |
mbed_official | 610:813dcc80987e | 1785 | /* Clear Computation Complete Flag */ |
mbed_official | 610:813dcc80987e | 1786 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1787 | /* Get the last Input data address */ |
mbed_official | 610:813dcc80987e | 1788 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
mbed_official | 610:813dcc80987e | 1789 | |
mbed_official | 610:813dcc80987e | 1790 | /* Increment/decrement instance pointer/counter */ |
mbed_official | 610:813dcc80987e | 1791 | hcryp->pCrypInBuffPtr += 16; |
mbed_official | 610:813dcc80987e | 1792 | hcryp->CrypInCount -= 16; |
mbed_official | 610:813dcc80987e | 1793 | |
mbed_official | 610:813dcc80987e | 1794 | if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) |
mbed_official | 610:813dcc80987e | 1795 | { |
mbed_official | 610:813dcc80987e | 1796 | if (hcryp->CrypInCount == hcryp->Init.HeaderSize) |
mbed_official | 610:813dcc80987e | 1797 | { |
mbed_official | 610:813dcc80987e | 1798 | /* All B blocks will have been entered after the next |
mbed_official | 610:813dcc80987e | 1799 | four DINR writing, so point at header buffer for |
mbed_official | 610:813dcc80987e | 1800 | the next iteration */ |
mbed_official | 610:813dcc80987e | 1801 | hcryp->pCrypInBuffPtr = hcryp->Init.Header; |
mbed_official | 610:813dcc80987e | 1802 | } |
mbed_official | 610:813dcc80987e | 1803 | } |
mbed_official | 610:813dcc80987e | 1804 | |
mbed_official | 610:813dcc80987e | 1805 | /* Write the Input block in the Data Input register */ |
mbed_official | 610:813dcc80987e | 1806 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1807 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1808 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1809 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1810 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1811 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1812 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1813 | |
mbed_official | 610:813dcc80987e | 1814 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 1815 | } |
mbed_official | 610:813dcc80987e | 1816 | } |
mbed_official | 610:813dcc80987e | 1817 | /*========================*/ |
mbed_official | 610:813dcc80987e | 1818 | /* GCM/GMAC payload phase */ |
mbed_official | 610:813dcc80987e | 1819 | /*========================*/ |
mbed_official | 610:813dcc80987e | 1820 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) |
mbed_official | 610:813dcc80987e | 1821 | { |
mbed_official | 610:813dcc80987e | 1822 | /* Get the last output data address */ |
mbed_official | 610:813dcc80987e | 1823 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
mbed_official | 610:813dcc80987e | 1824 | |
mbed_official | 610:813dcc80987e | 1825 | /* Retrieve the last block available from the CRYP hardware block: |
mbed_official | 610:813dcc80987e | 1826 | read the output block from the Data Output Register */ |
mbed_official | 610:813dcc80987e | 1827 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 1828 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1829 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 1830 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1831 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 1832 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1833 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 1834 | |
mbed_official | 610:813dcc80987e | 1835 | /* Increment/decrement instance pointer/counter */ |
mbed_official | 610:813dcc80987e | 1836 | hcryp->pCrypOutBuffPtr += 16; |
mbed_official | 610:813dcc80987e | 1837 | hcryp->CrypOutCount -= 16; |
mbed_official | 610:813dcc80987e | 1838 | |
mbed_official | 610:813dcc80987e | 1839 | /* Check if all output text has been retrieved */ |
mbed_official | 610:813dcc80987e | 1840 | if (hcryp->CrypOutCount == 0) |
mbed_official | 610:813dcc80987e | 1841 | { |
mbed_official | 610:813dcc80987e | 1842 | /* Clear Computation Complete Flag */ |
mbed_official | 610:813dcc80987e | 1843 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1844 | /* Disable Computation Complete Flag and Errors Interrupts */ |
mbed_official | 610:813dcc80987e | 1845 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
mbed_official | 610:813dcc80987e | 1846 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 1847 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1848 | /* Mark that the payload phase is over */ |
mbed_official | 610:813dcc80987e | 1849 | hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER; |
mbed_official | 610:813dcc80987e | 1850 | |
mbed_official | 610:813dcc80987e | 1851 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 1852 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1853 | |
mbed_official | 610:813dcc80987e | 1854 | /* Call computation complete callback */ |
mbed_official | 610:813dcc80987e | 1855 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
mbed_official | 610:813dcc80987e | 1856 | |
mbed_official | 610:813dcc80987e | 1857 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 1858 | } |
mbed_official | 610:813dcc80987e | 1859 | /* If suspension flag has been raised, suspend processing */ |
mbed_official | 610:813dcc80987e | 1860 | else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND) |
mbed_official | 610:813dcc80987e | 1861 | { |
mbed_official | 610:813dcc80987e | 1862 | if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) |
mbed_official | 610:813dcc80987e | 1863 | { |
mbed_official | 610:813dcc80987e | 1864 | /* Ensure that Busy flag is reset */ |
mbed_official | 610:813dcc80987e | 1865 | if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK) |
mbed_official | 610:813dcc80987e | 1866 | { |
mbed_official | 610:813dcc80987e | 1867 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1868 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1869 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 1870 | } |
mbed_official | 610:813dcc80987e | 1871 | } |
mbed_official | 610:813dcc80987e | 1872 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 1873 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1874 | |
mbed_official | 610:813dcc80987e | 1875 | /* reset SuspendRequest */ |
mbed_official | 610:813dcc80987e | 1876 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
mbed_official | 610:813dcc80987e | 1877 | /* Disable Computation Complete Flag and Errors Interrupts */ |
mbed_official | 610:813dcc80987e | 1878 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
mbed_official | 610:813dcc80987e | 1879 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 1880 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
mbed_official | 610:813dcc80987e | 1881 | /* Mark that the header phase is over */ |
mbed_official | 610:813dcc80987e | 1882 | hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED; |
mbed_official | 610:813dcc80987e | 1883 | |
mbed_official | 610:813dcc80987e | 1884 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 1885 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1886 | |
mbed_official | 610:813dcc80987e | 1887 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 1888 | } |
mbed_official | 610:813dcc80987e | 1889 | else /* Output data are still expected, carry on feeding the CRYP |
mbed_official | 610:813dcc80987e | 1890 | hardware block with input data */ |
mbed_official | 610:813dcc80987e | 1891 | { |
mbed_official | 610:813dcc80987e | 1892 | /* Clear Computation Complete Flag */ |
mbed_official | 610:813dcc80987e | 1893 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1894 | /* Get the last Input data address */ |
mbed_official | 610:813dcc80987e | 1895 | inputaddr = (uint32_t)hcryp->pCrypInBuffPtr; |
mbed_official | 610:813dcc80987e | 1896 | |
mbed_official | 610:813dcc80987e | 1897 | /* Increment/decrement instance pointer/counter */ |
mbed_official | 610:813dcc80987e | 1898 | hcryp->pCrypInBuffPtr += 16; |
mbed_official | 610:813dcc80987e | 1899 | hcryp->CrypInCount -= 16; |
mbed_official | 610:813dcc80987e | 1900 | |
mbed_official | 610:813dcc80987e | 1901 | /* Write the Input block in the Data Input register */ |
mbed_official | 610:813dcc80987e | 1902 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1903 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1904 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1905 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1906 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1907 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 1908 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 1909 | |
mbed_official | 610:813dcc80987e | 1910 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 1911 | } |
mbed_official | 610:813dcc80987e | 1912 | } |
mbed_official | 610:813dcc80987e | 1913 | /*==============================*/ |
mbed_official | 610:813dcc80987e | 1914 | /* GCM/GMAC or CMAC final phase */ |
mbed_official | 610:813dcc80987e | 1915 | /*==============================*/ |
mbed_official | 610:813dcc80987e | 1916 | else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_FINAL_PHASE) |
mbed_official | 610:813dcc80987e | 1917 | { |
mbed_official | 610:813dcc80987e | 1918 | /* Clear Computation Complete Flag */ |
mbed_official | 610:813dcc80987e | 1919 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1920 | |
mbed_official | 610:813dcc80987e | 1921 | /* Get the last output data address */ |
mbed_official | 610:813dcc80987e | 1922 | outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr; |
mbed_official | 610:813dcc80987e | 1923 | |
mbed_official | 610:813dcc80987e | 1924 | /* Retrieve the last expected data from the CRYP hardware block: |
mbed_official | 610:813dcc80987e | 1925 | read the output block from the Data Output Register */ |
mbed_official | 610:813dcc80987e | 1926 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 1927 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1928 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 1929 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1930 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 1931 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 1932 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 1933 | |
mbed_official | 610:813dcc80987e | 1934 | /* Disable Computation Complete Flag and Errors Interrupts */ |
mbed_official | 610:813dcc80987e | 1935 | __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE); |
mbed_official | 610:813dcc80987e | 1936 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 1937 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 1938 | /* Mark that the header phase is over */ |
mbed_official | 610:813dcc80987e | 1939 | hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER; |
mbed_official | 610:813dcc80987e | 1940 | |
mbed_official | 610:813dcc80987e | 1941 | /* Disable the Peripheral */ |
mbed_official | 610:813dcc80987e | 1942 | __HAL_CRYP_DISABLE(); |
mbed_official | 610:813dcc80987e | 1943 | /* Process Unlocked */ |
mbed_official | 610:813dcc80987e | 1944 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1945 | |
mbed_official | 610:813dcc80987e | 1946 | /* Call computation complete callback */ |
mbed_official | 610:813dcc80987e | 1947 | HAL_CRYPEx_ComputationCpltCallback(hcryp); |
mbed_official | 610:813dcc80987e | 1948 | |
mbed_official | 610:813dcc80987e | 1949 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 1950 | } |
mbed_official | 610:813dcc80987e | 1951 | else |
mbed_official | 610:813dcc80987e | 1952 | { |
mbed_official | 610:813dcc80987e | 1953 | /* Clear Computation Complete Flag */ |
mbed_official | 610:813dcc80987e | 1954 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 1955 | hcryp->State = HAL_CRYP_STATE_ERROR; |
mbed_official | 610:813dcc80987e | 1956 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 1957 | return HAL_ERROR; |
mbed_official | 610:813dcc80987e | 1958 | } |
mbed_official | 610:813dcc80987e | 1959 | } |
mbed_official | 610:813dcc80987e | 1960 | else |
mbed_official | 610:813dcc80987e | 1961 | { |
mbed_official | 610:813dcc80987e | 1962 | return HAL_BUSY; |
mbed_official | 610:813dcc80987e | 1963 | } |
mbed_official | 610:813dcc80987e | 1964 | } |
mbed_official | 610:813dcc80987e | 1965 | |
mbed_official | 610:813dcc80987e | 1966 | |
mbed_official | 610:813dcc80987e | 1967 | |
mbed_official | 610:813dcc80987e | 1968 | /** |
mbed_official | 610:813dcc80987e | 1969 | * @brief Set the DMA configuration and start the DMA transfer |
mbed_official | 610:813dcc80987e | 1970 | * for GCM, GMAC or CMAC chainging modes. |
mbed_official | 610:813dcc80987e | 1971 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 1972 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 1973 | * @param inputaddr: Address of the Input buffer. |
mbed_official | 610:813dcc80987e | 1974 | * @param Size: Size of the Input buffer un bytes, must be a multiple of 16. |
mbed_official | 610:813dcc80987e | 1975 | * @param outputaddr: Address of the Output buffer, null pointer when no output DMA stream |
mbed_official | 610:813dcc80987e | 1976 | * has to be configured. |
mbed_official | 610:813dcc80987e | 1977 | * @retval None |
mbed_official | 610:813dcc80987e | 1978 | */ |
mbed_official | 610:813dcc80987e | 1979 | static void CRYP_GCMCMAC_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr) |
mbed_official | 610:813dcc80987e | 1980 | { |
mbed_official | 610:813dcc80987e | 1981 | |
mbed_official | 610:813dcc80987e | 1982 | /* Set the input CRYP DMA transfer complete callback */ |
mbed_official | 610:813dcc80987e | 1983 | hcryp->hdmain->XferCpltCallback = CRYP_GCMCMAC_DMAInCplt; |
mbed_official | 610:813dcc80987e | 1984 | /* Set the DMA error callback */ |
mbed_official | 610:813dcc80987e | 1985 | hcryp->hdmain->XferErrorCallback = CRYP_GCMCMAC_DMAError; |
mbed_official | 610:813dcc80987e | 1986 | |
mbed_official | 610:813dcc80987e | 1987 | if (outputaddr != 0) |
mbed_official | 610:813dcc80987e | 1988 | { |
mbed_official | 610:813dcc80987e | 1989 | /* Set the output CRYP DMA transfer complete callback */ |
mbed_official | 610:813dcc80987e | 1990 | hcryp->hdmaout->XferCpltCallback = CRYP_GCMCMAC_DMAOutCplt; |
mbed_official | 610:813dcc80987e | 1991 | /* Set the DMA error callback */ |
mbed_official | 610:813dcc80987e | 1992 | hcryp->hdmaout->XferErrorCallback = CRYP_GCMCMAC_DMAError; |
mbed_official | 610:813dcc80987e | 1993 | } |
mbed_official | 610:813dcc80987e | 1994 | |
mbed_official | 610:813dcc80987e | 1995 | /* Enable the CRYP peripheral */ |
mbed_official | 610:813dcc80987e | 1996 | __HAL_CRYP_ENABLE(); |
mbed_official | 610:813dcc80987e | 1997 | |
mbed_official | 610:813dcc80987e | 1998 | /* Enable the DMA input stream */ |
mbed_official | 610:813dcc80987e | 1999 | HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4); |
mbed_official | 610:813dcc80987e | 2000 | |
mbed_official | 610:813dcc80987e | 2001 | /* Enable the DMA input request */ |
mbed_official | 610:813dcc80987e | 2002 | SET_BIT(hcryp->Instance->CR, AES_CR_DMAINEN); |
mbed_official | 610:813dcc80987e | 2003 | |
mbed_official | 610:813dcc80987e | 2004 | |
mbed_official | 610:813dcc80987e | 2005 | if (outputaddr != 0) |
mbed_official | 610:813dcc80987e | 2006 | { |
mbed_official | 610:813dcc80987e | 2007 | /* Enable the DMA output stream */ |
mbed_official | 610:813dcc80987e | 2008 | HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4); |
mbed_official | 610:813dcc80987e | 2009 | |
mbed_official | 610:813dcc80987e | 2010 | /* Enable the DMA output request */ |
mbed_official | 610:813dcc80987e | 2011 | SET_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN); |
mbed_official | 610:813dcc80987e | 2012 | } |
mbed_official | 610:813dcc80987e | 2013 | } |
mbed_official | 610:813dcc80987e | 2014 | |
mbed_official | 610:813dcc80987e | 2015 | |
mbed_official | 610:813dcc80987e | 2016 | |
mbed_official | 610:813dcc80987e | 2017 | /** |
mbed_official | 610:813dcc80987e | 2018 | * @brief Write/read input/output data in polling mode. |
mbed_official | 610:813dcc80987e | 2019 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 2020 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 2021 | * @param Input: Pointer to the Input buffer. |
mbed_official | 610:813dcc80987e | 2022 | * @param Ilength: Length of the Input buffer in bytes, must be a multiple of 16. |
mbed_official | 610:813dcc80987e | 2023 | * @param Output: Pointer to the returned buffer. |
mbed_official | 610:813dcc80987e | 2024 | * @param Timeout: Specify Timeout value. |
mbed_official | 610:813dcc80987e | 2025 | * @retval HAL status |
mbed_official | 610:813dcc80987e | 2026 | */ |
mbed_official | 610:813dcc80987e | 2027 | static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout) |
mbed_official | 610:813dcc80987e | 2028 | { |
mbed_official | 610:813dcc80987e | 2029 | uint32_t index = 0; |
mbed_official | 610:813dcc80987e | 2030 | uint32_t inputaddr = (uint32_t)Input; |
mbed_official | 610:813dcc80987e | 2031 | uint32_t outputaddr = (uint32_t)Output; |
mbed_official | 610:813dcc80987e | 2032 | |
mbed_official | 610:813dcc80987e | 2033 | |
mbed_official | 610:813dcc80987e | 2034 | for(index=0; (index < Ilength); index += 16) |
mbed_official | 610:813dcc80987e | 2035 | { |
mbed_official | 610:813dcc80987e | 2036 | /* Write the Input block in the Data Input register */ |
mbed_official | 610:813dcc80987e | 2037 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 2038 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 2039 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 2040 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 2041 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 2042 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 2043 | hcryp->Instance->DINR = *(uint32_t*)(inputaddr); |
mbed_official | 610:813dcc80987e | 2044 | inputaddr+=4; |
mbed_official | 610:813dcc80987e | 2045 | |
mbed_official | 610:813dcc80987e | 2046 | /* Wait for CCF flag to be raised */ |
mbed_official | 610:813dcc80987e | 2047 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
mbed_official | 610:813dcc80987e | 2048 | { |
mbed_official | 610:813dcc80987e | 2049 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 2050 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 2051 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 2052 | } |
mbed_official | 610:813dcc80987e | 2053 | |
mbed_official | 610:813dcc80987e | 2054 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 2055 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 2056 | |
mbed_official | 610:813dcc80987e | 2057 | /* Read the Output block from the Data Output Register */ |
mbed_official | 610:813dcc80987e | 2058 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 2059 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 2060 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 2061 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 2062 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 2063 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 2064 | *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR; |
mbed_official | 610:813dcc80987e | 2065 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 2066 | |
mbed_official | 610:813dcc80987e | 2067 | /* If the suspension flag has been raised and if the processing is not about |
mbed_official | 610:813dcc80987e | 2068 | to end, suspend processing */ |
mbed_official | 610:813dcc80987e | 2069 | if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16) < Ilength)) |
mbed_official | 610:813dcc80987e | 2070 | { |
mbed_official | 610:813dcc80987e | 2071 | /* Reset SuspendRequest */ |
mbed_official | 610:813dcc80987e | 2072 | hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE; |
mbed_official | 610:813dcc80987e | 2073 | |
mbed_official | 610:813dcc80987e | 2074 | /* Save current reading and writing locations of Input and Output buffers */ |
mbed_official | 610:813dcc80987e | 2075 | hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr; |
mbed_official | 610:813dcc80987e | 2076 | hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr; |
mbed_official | 610:813dcc80987e | 2077 | /* Save the number of bytes that remain to be processed at this point */ |
mbed_official | 610:813dcc80987e | 2078 | hcryp->CrypInCount = Ilength - (index+16); |
mbed_official | 610:813dcc80987e | 2079 | |
mbed_official | 610:813dcc80987e | 2080 | /* Change the CRYP state */ |
mbed_official | 610:813dcc80987e | 2081 | hcryp->State = HAL_CRYP_STATE_SUSPENDED; |
mbed_official | 610:813dcc80987e | 2082 | |
mbed_official | 610:813dcc80987e | 2083 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 2084 | } |
mbed_official | 610:813dcc80987e | 2085 | |
mbed_official | 610:813dcc80987e | 2086 | |
mbed_official | 610:813dcc80987e | 2087 | } |
mbed_official | 610:813dcc80987e | 2088 | /* Return function status */ |
mbed_official | 610:813dcc80987e | 2089 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 2090 | |
mbed_official | 610:813dcc80987e | 2091 | } |
mbed_official | 610:813dcc80987e | 2092 | |
mbed_official | 610:813dcc80987e | 2093 | |
mbed_official | 610:813dcc80987e | 2094 | |
mbed_official | 610:813dcc80987e | 2095 | |
mbed_official | 610:813dcc80987e | 2096 | |
mbed_official | 610:813dcc80987e | 2097 | /** |
mbed_official | 610:813dcc80987e | 2098 | * @brief Read derivative key in polling mode when CRYP hardware block is set |
mbed_official | 610:813dcc80987e | 2099 | * in key derivation operating mode (mode 2). |
mbed_official | 610:813dcc80987e | 2100 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 2101 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 2102 | * @param Output: Pointer to the returned buffer. |
mbed_official | 610:813dcc80987e | 2103 | * @param Timeout: Specify Timeout value. |
mbed_official | 610:813dcc80987e | 2104 | * @retval HAL status |
mbed_official | 610:813dcc80987e | 2105 | */ |
mbed_official | 610:813dcc80987e | 2106 | static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout) |
mbed_official | 610:813dcc80987e | 2107 | { |
mbed_official | 610:813dcc80987e | 2108 | uint32_t outputaddr = (uint32_t)Output; |
mbed_official | 610:813dcc80987e | 2109 | |
mbed_official | 610:813dcc80987e | 2110 | /* Wait for CCF flag to be raised */ |
mbed_official | 610:813dcc80987e | 2111 | if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK) |
mbed_official | 610:813dcc80987e | 2112 | { |
mbed_official | 610:813dcc80987e | 2113 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 2114 | __HAL_UNLOCK(hcryp); |
mbed_official | 610:813dcc80987e | 2115 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 2116 | } |
mbed_official | 610:813dcc80987e | 2117 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 2118 | __HAL_CRYP_CLEAR_FLAG( CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 2119 | |
mbed_official | 610:813dcc80987e | 2120 | /* Read the derivative key from the AES_KEYRx registers */ |
mbed_official | 610:813dcc80987e | 2121 | if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B) |
mbed_official | 610:813dcc80987e | 2122 | { |
mbed_official | 610:813dcc80987e | 2123 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7); |
mbed_official | 610:813dcc80987e | 2124 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 2125 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6); |
mbed_official | 610:813dcc80987e | 2126 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 2127 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5); |
mbed_official | 610:813dcc80987e | 2128 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 2129 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4); |
mbed_official | 610:813dcc80987e | 2130 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 2131 | } |
mbed_official | 610:813dcc80987e | 2132 | |
mbed_official | 610:813dcc80987e | 2133 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3); |
mbed_official | 610:813dcc80987e | 2134 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 2135 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2); |
mbed_official | 610:813dcc80987e | 2136 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 2137 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1); |
mbed_official | 610:813dcc80987e | 2138 | outputaddr+=4; |
mbed_official | 610:813dcc80987e | 2139 | *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0); |
mbed_official | 610:813dcc80987e | 2140 | |
mbed_official | 610:813dcc80987e | 2141 | |
mbed_official | 610:813dcc80987e | 2142 | /* Return function status */ |
mbed_official | 610:813dcc80987e | 2143 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 2144 | } |
mbed_official | 610:813dcc80987e | 2145 | |
mbed_official | 610:813dcc80987e | 2146 | /** |
mbed_official | 610:813dcc80987e | 2147 | * @brief Set the DMA configuration and start the DMA transfer. |
mbed_official | 610:813dcc80987e | 2148 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 2149 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 2150 | * @param inputaddr: Address of the Input buffer. |
mbed_official | 610:813dcc80987e | 2151 | * @param Size: Size of the Input buffer in bytes, must be a multiple of 16. |
mbed_official | 610:813dcc80987e | 2152 | * @param outputaddr: Address of the Output buffer. |
mbed_official | 610:813dcc80987e | 2153 | * @retval None |
mbed_official | 610:813dcc80987e | 2154 | */ |
mbed_official | 610:813dcc80987e | 2155 | static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr) |
mbed_official | 610:813dcc80987e | 2156 | { |
mbed_official | 610:813dcc80987e | 2157 | /* Set the CRYP DMA transfer complete callback */ |
mbed_official | 610:813dcc80987e | 2158 | hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt; |
mbed_official | 610:813dcc80987e | 2159 | /* Set the DMA error callback */ |
mbed_official | 610:813dcc80987e | 2160 | hcryp->hdmain->XferErrorCallback = CRYP_DMAError; |
mbed_official | 610:813dcc80987e | 2161 | |
mbed_official | 610:813dcc80987e | 2162 | /* Set the CRYP DMA transfer complete callback */ |
mbed_official | 610:813dcc80987e | 2163 | hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt; |
mbed_official | 610:813dcc80987e | 2164 | /* Set the DMA error callback */ |
mbed_official | 610:813dcc80987e | 2165 | hcryp->hdmaout->XferErrorCallback = CRYP_DMAError; |
mbed_official | 610:813dcc80987e | 2166 | |
mbed_official | 610:813dcc80987e | 2167 | /* Enable the DMA input stream */ |
mbed_official | 610:813dcc80987e | 2168 | HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4); |
mbed_official | 610:813dcc80987e | 2169 | |
mbed_official | 610:813dcc80987e | 2170 | /* Enable the DMA output stream */ |
mbed_official | 610:813dcc80987e | 2171 | HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4); |
mbed_official | 610:813dcc80987e | 2172 | |
mbed_official | 610:813dcc80987e | 2173 | /* Enable In and Out DMA requests */ |
mbed_official | 610:813dcc80987e | 2174 | SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN)); |
mbed_official | 610:813dcc80987e | 2175 | |
mbed_official | 610:813dcc80987e | 2176 | /* Enable the CRYP peripheral */ |
mbed_official | 610:813dcc80987e | 2177 | __HAL_CRYP_ENABLE(); |
mbed_official | 610:813dcc80987e | 2178 | } |
mbed_official | 610:813dcc80987e | 2179 | |
mbed_official | 610:813dcc80987e | 2180 | |
mbed_official | 610:813dcc80987e | 2181 | /** |
mbed_official | 610:813dcc80987e | 2182 | * @brief Handle CRYP hardware block Timeout when waiting for CCF flag to be raised. |
mbed_official | 610:813dcc80987e | 2183 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 2184 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 2185 | * @param Timeout: Timeout duration. |
mbed_official | 610:813dcc80987e | 2186 | * @retval HAL status |
mbed_official | 610:813dcc80987e | 2187 | */ |
mbed_official | 610:813dcc80987e | 2188 | static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) |
mbed_official | 610:813dcc80987e | 2189 | { |
mbed_official | 610:813dcc80987e | 2190 | uint32_t tickstart = 0; |
mbed_official | 610:813dcc80987e | 2191 | |
mbed_official | 610:813dcc80987e | 2192 | /* Get timeout */ |
mbed_official | 610:813dcc80987e | 2193 | tickstart = HAL_GetTick(); |
mbed_official | 610:813dcc80987e | 2194 | |
mbed_official | 610:813dcc80987e | 2195 | while(HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF)) |
mbed_official | 610:813dcc80987e | 2196 | { |
mbed_official | 610:813dcc80987e | 2197 | /* Check for the Timeout */ |
mbed_official | 610:813dcc80987e | 2198 | if(Timeout != HAL_MAX_DELAY) |
mbed_official | 610:813dcc80987e | 2199 | { |
mbed_official | 610:813dcc80987e | 2200 | if((HAL_GetTick() - tickstart ) > Timeout) |
mbed_official | 610:813dcc80987e | 2201 | { |
mbed_official | 610:813dcc80987e | 2202 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 2203 | } |
mbed_official | 610:813dcc80987e | 2204 | } |
mbed_official | 610:813dcc80987e | 2205 | } |
mbed_official | 610:813dcc80987e | 2206 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 2207 | } |
mbed_official | 610:813dcc80987e | 2208 | |
mbed_official | 610:813dcc80987e | 2209 | /** |
mbed_official | 610:813dcc80987e | 2210 | * @brief Wait for Busy Flag to be reset during a GCM payload encryption process suspension. |
mbed_official | 610:813dcc80987e | 2211 | * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains |
mbed_official | 610:813dcc80987e | 2212 | * the configuration information for CRYP module. |
mbed_official | 610:813dcc80987e | 2213 | * @param Timeout: Timeout duration. |
mbed_official | 610:813dcc80987e | 2214 | * @retval HAL status |
mbed_official | 610:813dcc80987e | 2215 | */ |
mbed_official | 610:813dcc80987e | 2216 | static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef *hcryp, uint32_t Timeout) |
mbed_official | 610:813dcc80987e | 2217 | { |
mbed_official | 610:813dcc80987e | 2218 | uint32_t tickstart = 0; |
mbed_official | 610:813dcc80987e | 2219 | |
mbed_official | 610:813dcc80987e | 2220 | /* Get timeout */ |
mbed_official | 610:813dcc80987e | 2221 | tickstart = HAL_GetTick(); |
mbed_official | 610:813dcc80987e | 2222 | |
mbed_official | 610:813dcc80987e | 2223 | while(HAL_IS_BIT_SET(hcryp->Instance->SR, AES_SR_BUSY)) |
mbed_official | 610:813dcc80987e | 2224 | { |
mbed_official | 610:813dcc80987e | 2225 | /* Check for the Timeout */ |
mbed_official | 610:813dcc80987e | 2226 | if(Timeout != HAL_MAX_DELAY) |
mbed_official | 610:813dcc80987e | 2227 | { |
mbed_official | 610:813dcc80987e | 2228 | if((HAL_GetTick() - tickstart ) > Timeout) |
mbed_official | 610:813dcc80987e | 2229 | { |
mbed_official | 610:813dcc80987e | 2230 | return HAL_TIMEOUT; |
mbed_official | 610:813dcc80987e | 2231 | } |
mbed_official | 610:813dcc80987e | 2232 | } |
mbed_official | 610:813dcc80987e | 2233 | } |
mbed_official | 610:813dcc80987e | 2234 | return HAL_OK; |
mbed_official | 610:813dcc80987e | 2235 | } |
mbed_official | 610:813dcc80987e | 2236 | |
mbed_official | 610:813dcc80987e | 2237 | |
mbed_official | 610:813dcc80987e | 2238 | /** |
mbed_official | 610:813dcc80987e | 2239 | * @brief DMA CRYP Input Data process complete callback. |
mbed_official | 610:813dcc80987e | 2240 | * @param hdma: DMA handle. |
mbed_official | 610:813dcc80987e | 2241 | * @retval None |
mbed_official | 610:813dcc80987e | 2242 | */ |
mbed_official | 610:813dcc80987e | 2243 | static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma) |
mbed_official | 610:813dcc80987e | 2244 | { |
mbed_official | 610:813dcc80987e | 2245 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
mbed_official | 610:813dcc80987e | 2246 | |
mbed_official | 610:813dcc80987e | 2247 | /* Disable the DMA transfer for input request */ |
mbed_official | 610:813dcc80987e | 2248 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN); |
mbed_official | 610:813dcc80987e | 2249 | |
mbed_official | 610:813dcc80987e | 2250 | /* Call input data transfer complete callback */ |
mbed_official | 610:813dcc80987e | 2251 | HAL_CRYP_InCpltCallback(hcryp); |
mbed_official | 610:813dcc80987e | 2252 | } |
mbed_official | 610:813dcc80987e | 2253 | |
mbed_official | 610:813dcc80987e | 2254 | /** |
mbed_official | 610:813dcc80987e | 2255 | * @brief DMA CRYP Output Data process complete callback. |
mbed_official | 610:813dcc80987e | 2256 | * @param hdma: DMA handle. |
mbed_official | 610:813dcc80987e | 2257 | * @retval None |
mbed_official | 610:813dcc80987e | 2258 | */ |
mbed_official | 610:813dcc80987e | 2259 | static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma) |
mbed_official | 610:813dcc80987e | 2260 | { |
mbed_official | 610:813dcc80987e | 2261 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
mbed_official | 610:813dcc80987e | 2262 | |
mbed_official | 610:813dcc80987e | 2263 | /* Disable the DMA transfer for output request */ |
mbed_official | 610:813dcc80987e | 2264 | CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN); |
mbed_official | 610:813dcc80987e | 2265 | |
mbed_official | 610:813dcc80987e | 2266 | /* Clear CCF Flag */ |
mbed_official | 610:813dcc80987e | 2267 | __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR); |
mbed_official | 610:813dcc80987e | 2268 | |
mbed_official | 610:813dcc80987e | 2269 | /* Disable CRYP */ |
mbed_official | 610:813dcc80987e | 2270 | __HAL_CRYP_DISABLE(); |
mbed_official | 610:813dcc80987e | 2271 | |
mbed_official | 610:813dcc80987e | 2272 | /* Change the CRYP state to ready */ |
mbed_official | 610:813dcc80987e | 2273 | hcryp->State = HAL_CRYP_STATE_READY; |
mbed_official | 610:813dcc80987e | 2274 | |
mbed_official | 610:813dcc80987e | 2275 | /* Call output data transfer complete callback */ |
mbed_official | 610:813dcc80987e | 2276 | HAL_CRYP_OutCpltCallback(hcryp); |
mbed_official | 610:813dcc80987e | 2277 | } |
mbed_official | 610:813dcc80987e | 2278 | |
mbed_official | 610:813dcc80987e | 2279 | /** |
mbed_official | 610:813dcc80987e | 2280 | * @brief DMA CRYP communication error callback. |
mbed_official | 610:813dcc80987e | 2281 | * @param hdma: DMA handle. |
mbed_official | 610:813dcc80987e | 2282 | * @retval None |
mbed_official | 610:813dcc80987e | 2283 | */ |
mbed_official | 610:813dcc80987e | 2284 | static void CRYP_DMAError(DMA_HandleTypeDef *hdma) |
mbed_official | 610:813dcc80987e | 2285 | { |
mbed_official | 610:813dcc80987e | 2286 | CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
mbed_official | 610:813dcc80987e | 2287 | |
mbed_official | 610:813dcc80987e | 2288 | hcryp->State= HAL_CRYP_STATE_ERROR; |
mbed_official | 610:813dcc80987e | 2289 | hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR; |
mbed_official | 610:813dcc80987e | 2290 | HAL_CRYP_ErrorCallback(hcryp); |
mbed_official | 610:813dcc80987e | 2291 | /* Clear Error Flag */ |
mbed_official | 610:813dcc80987e | 2292 | __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR); |
mbed_official | 610:813dcc80987e | 2293 | } |
mbed_official | 610:813dcc80987e | 2294 | |
mbed_official | 610:813dcc80987e | 2295 | |
mbed_official | 610:813dcc80987e | 2296 | /** |
mbed_official | 610:813dcc80987e | 2297 | * @} |
mbed_official | 610:813dcc80987e | 2298 | */ |
mbed_official | 610:813dcc80987e | 2299 | |
mbed_official | 610:813dcc80987e | 2300 | /** |
mbed_official | 610:813dcc80987e | 2301 | * @} |
mbed_official | 610:813dcc80987e | 2302 | */ |
mbed_official | 610:813dcc80987e | 2303 | |
mbed_official | 610:813dcc80987e | 2304 | /** |
mbed_official | 610:813dcc80987e | 2305 | * @} |
mbed_official | 610:813dcc80987e | 2306 | */ |
mbed_official | 610:813dcc80987e | 2307 | |
mbed_official | 610:813dcc80987e | 2308 | #endif /* defined(STM32L485xx) || defined(STM32L486xx) */ |
mbed_official | 610:813dcc80987e | 2309 | |
mbed_official | 610:813dcc80987e | 2310 | #endif /* HAL_CRYP_MODULE_ENABLED */ |
mbed_official | 610:813dcc80987e | 2311 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |