You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

5468 lines
182 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_hal_cryp.c
  4. * @author MCD Application Team
  5. * @brief CRYP HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Cryptography (CRYP) peripheral:
  8. * + Initialization, de-initialization, set config and get config functions
  9. * + AES processing functions
  10. * + DMA callback functions
  11. * + CRYP IRQ handler management
  12. * + Peripheral State functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. The CRYP HAL driver can be used in CRYP or TinyAES peripheral as follows:
  20. (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
  21. (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()or __HAL_RCC_AES_CLK_ENABLE for TinyAES peripheral
  22. (##) In case of using interrupts (e.g. HAL_CRYP_Encrypt_IT())
  23. (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
  24. (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
  25. (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
  26. (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_Encrypt_DMA())
  27. (+++) Enable the DMAx interface clock using __RCC_DMAx_CLK_ENABLE()
  28. (+++) Configure and enable two DMA streams one for managing data transfer from
  29. memory to peripheral (input stream) and another stream for managing data
  30. transfer from peripheral to memory (output stream)
  31. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  32. using __HAL_LINKDMA()
  33. (+++) Configure the priority and enable the NVIC for the transfer complete
  34. interrupt on the two DMA channels. The output channel should have higher
  35. priority than the input channel HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
  36. (#)Initialize the CRYP according to the specified parameters :
  37. (##) The data type: 1-bit, 8-bit, 16-bit or 32-bit.
  38. (##) The key size: 128, 192 or 256.
  39. (##) The AlgoMode DES/ TDES Algorithm ECB/CBC or AES Algorithm ECB/CBC/CTR/GCM or CCM.
  40. (##) The initialization vector (counter). It is not used in ECB mode.
  41. (##) The key buffer used for encryption/decryption.
  42. (+++) In some specific configurations, the key is written by the application
  43. code out of the HAL scope. In that case, user can still resort to the
  44. HAL APIs as usual but must make sure that pKey pointer is set to NULL.
  45. (##) The DataWidthUnit field. It specifies whether the data length (or the payload length for authentication
  46. algorithms) is in words or bytes.
  47. (##) The Header used only in AES GCM and CCM Algorithm for authentication.
  48. (##) The HeaderSize providing the size of the header buffer in words or bytes, depending upon HeaderWidthUnit field.
  49. (##) The HeaderWidthUnit field. It specifies whether the header length (for authentication algorithms) is in words or bytes.
  50. (##) The B0 block is the first authentication block used only in AES CCM mode.
  51. (##) The KeyIVConfigSkip used to process several messages in a row (please see more information below).
  52. (#)Three processing (encryption/decryption) functions are available:
  53. (##) Polling mode: encryption and decryption APIs are blocking functions
  54. i.e. they process the data and wait till the processing is finished,
  55. e.g. HAL_CRYP_Encrypt & HAL_CRYP_Decrypt
  56. (##) Interrupt mode: encryption and decryption APIs are not blocking functions
  57. i.e. they process the data under interrupt,
  58. e.g. HAL_CRYP_Encrypt_IT & HAL_CRYP_Decrypt_IT
  59. (##) DMA mode: encryption and decryption APIs are not blocking functions
  60. i.e. the data transfer is ensured by DMA,
  61. e.g. HAL_CRYP_Encrypt_DMA & HAL_CRYP_Decrypt_DMA
  62. (#)When the processing function is called at first time after HAL_CRYP_Init()
  63. the CRYP peripheral is configured and processes the buffer in input.
  64. At second call, no need to Initialize the CRYP, user have to get current configuration via
  65. HAL_CRYP_GetConfig() API, then only HAL_CRYP_SetConfig() is requested to set
  66. new parametres, finally user can start encryption/decryption.
  67. (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
  68. (#)To process a single message with consecutive calls to HAL_CRYP_Encrypt() or HAL_CRYP_Decrypt()
  69. without having to configure again the Key or the Initialization Vector between each API call,
  70. the field KeyIVConfigSkip of the initialization structure must be set to CRYP_KEYIVCONFIG_ONCE.
  71. Same is true for consecutive calls of HAL_CRYP_Encrypt_IT(), HAL_CRYP_Decrypt_IT(), HAL_CRYP_Encrypt_DMA()
  72. or HAL_CRYP_Decrypt_DMA().
  73. [..]
  74. The cryptographic processor supports following standards:
  75. (#) The data encryption standard (DES) and Triple-DES (TDES) supported only by CRYP1 peripheral:
  76. (##)64-bit data block processing
  77. (##) chaining modes supported :
  78. (+++) Electronic Code Book(ECB)
  79. (+++) Cipher Block Chaining (CBC)
  80. (##) keys length supported :64-bit, 128-bit and 192-bit.
  81. (#) The advanced encryption standard (AES) supported by CRYP1 & TinyAES peripheral:
  82. (##)128-bit data block processing
  83. (##) chaining modes supported :
  84. (+++) Electronic Code Book(ECB)
  85. (+++) Cipher Block Chaining (CBC)
  86. (+++) Counter mode (CTR)
  87. (+++) Galois/counter mode (GCM/GMAC)
  88. (+++) Counter with Cipher Block Chaining-Message(CCM)
  89. (##) keys length Supported :
  90. (+++) for CRYP1 peripheral: 128-bit, 192-bit and 256-bit.
  91. (+++) for TinyAES peripheral: 128-bit and 256-bit
  92. [..]
  93. (@) Specific care must be taken to format the key and the Initialization Vector IV!
  94. [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where
  95. b127 is the MSB and b0 the LSB, the key must be stored in MCU memory
  96. (+) as a sequence of words where the MSB word comes first (occupies the
  97. lowest memory address)
  98. (++) address n+0 : 0b b127 .. b120 b119 .. b112 b111 .. b104 b103 .. b96
  99. (++) address n+4 : 0b b95 .. b88 b87 .. b80 b79 .. b72 b71 .. b64
  100. (++) address n+8 : 0b b63 .. b56 b55 .. b48 b47 .. b40 b39 .. b32
  101. (++) address n+C : 0b b31 .. b24 b23 .. b16 b15 .. b8 b7 .. b0
  102. [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}.
  103. The 4 32-bit words that make the key must be stored as follows in MCU memory:
  104. (+) address n+0 : 0x B15 B14 B13 B12
  105. (+) address n+4 : 0x B11 B10 B9 B8
  106. (+) address n+8 : 0x B7 B6 B5 B4
  107. (+) address n+C : 0x B3 B2 B1 B0
  108. [..] which leads to the expected setting
  109. (+) AES_KEYR3 = 0x B15 B14 B13 B12
  110. (+) AES_KEYR2 = 0x B11 B10 B9 B8
  111. (+) AES_KEYR1 = 0x B7 B6 B5 B4
  112. (+) AES_KEYR0 = 0x B3 B2 B1 B0
  113. [..] Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}.
  114. The 8 32-bit words that make the key must be stored as follows in MCU memory:
  115. (+) address n+00 : 0x B31 B30 B29 B28
  116. (+) address n+04 : 0x B27 B26 B25 B24
  117. (+) address n+08 : 0x B23 B22 B21 B20
  118. (+) address n+0C : 0x B19 B18 B17 B16
  119. (+) address n+10 : 0x B15 B14 B13 B12
  120. (+) address n+14 : 0x B11 B10 B9 B8
  121. (+) address n+18 : 0x B7 B6 B5 B4
  122. (+) address n+1C : 0x B3 B2 B1 B0
  123. [..] which leads to the expected setting
  124. (+) AES_KEYR7 = 0x B31 B30 B29 B28
  125. (+) AES_KEYR6 = 0x B27 B26 B25 B24
  126. (+) AES_KEYR5 = 0x B23 B22 B21 B20
  127. (+) AES_KEYR4 = 0x B19 B18 B17 B16
  128. (+) AES_KEYR3 = 0x B15 B14 B13 B12
  129. (+) AES_KEYR2 = 0x B11 B10 B9 B8
  130. (+) AES_KEYR1 = 0x B7 B6 B5 B4
  131. (+) AES_KEYR0 = 0x B3 B2 B1 B0
  132. [..] Initialization Vector IV (4 32-bit words) format must follow the same as
  133. that of a 128-bit long key.
  134. [..] Note that key and IV registers are not sensitive to swap mode selection.
  135. [..] This section describes the AES Galois/counter mode (GCM) supported by both CRYP1 and TinyAES peripherals:
  136. (#) Algorithm supported :
  137. (##) Galois/counter mode (GCM)
  138. (##) Galois message authentication code (GMAC) :is exactly the same as
  139. GCM algorithm composed only by an header.
  140. (#) Four phases are performed in GCM :
  141. (##) Init phase: peripheral prepares the GCM hash subkey (H) and do the IV processing
  142. (##) Header phase: peripheral processes the Additional Authenticated Data (AAD), with hash
  143. computation only.
  144. (##) Payload phase: peripheral processes the plaintext (P) with hash computation + keystream
  145. encryption + data XORing. It works in a similar way for ciphertext (C).
  146. (##) Final phase: peripheral generates the authenticated tag (T) using the last block of data.
  147. (#) structure of message construction in GCM is defined as below :
  148. (##) 16 bytes Initial Counter Block (ICB)composed of IV and counter
  149. (##) The authenticated header A (also knows as Additional Authentication Data AAD)
  150. this part of the message is only authenticated, not encrypted.
  151. (##) The plaintext message P is both authenticated and encrypted as ciphertext.
  152. GCM standard specifies that ciphertext has same bit length as the plaintext.
  153. (##) The last block is composed of the length of A (on 64 bits) and the length of ciphertext
  154. (on 64 bits)
  155. [..] A more detailed description of the GCM message structure is available below.
  156. [..] This section describe The AES Counter with Cipher Block Chaining-Message
  157. Authentication Code (CCM) supported by both CRYP1 and TinyAES peripheral:
  158. (#) Specific parameters for CCM :
  159. (##) B0 block : follows NIST Special Publication 800-38C,
  160. (##) B1 block (header)
  161. (##) CTRx block : control blocks
  162. [..] A detailed description of the CCM message structure is available below.
  163. (#) Four phases are performed in CCM for CRYP1 peripheral:
  164. (##) Init phase: peripheral prepares the GCM hash subkey (H) and do the IV processing
  165. (##) Header phase: peripheral processes the Additional Authenticated Data (AAD), with hash
  166. computation only.
  167. (##) Payload phase: peripheral processes the plaintext (P) with hash computation + keystream
  168. encryption + data XORing. It works in a similar way for ciphertext (C).
  169. (##) Final phase: peripheral generates the authenticated tag (T) using the last block of data.
  170. (#) CCM in TinyAES peripheral:
  171. (##) To perform message payload encryption or decryption AES is configured in CTR mode.
  172. (##) For authentication two phases are performed :
  173. - Header phase: peripheral processes the Additional Authenticated Data (AAD) first, then the cleartext message
  174. only cleartext payload (not the ciphertext payload) is used and no outpout.
  175. (##) Final phase: peripheral generates the authenticated tag (T) using the last block of data.
  176. *** Callback registration ***
  177. =============================
  178. [..]
  179. The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS when set to 1
  180. allows the user to configure dynamically the driver callbacks.
  181. Use Functions HAL_CRYP_RegisterCallback() or HAL_CRYP_RegisterXXXCallback()
  182. to register an interrupt callback.
  183. [..]
  184. Function HAL_CRYP_RegisterCallback() allows to register following callbacks:
  185. (+) InCpltCallback : Input FIFO transfer completed callback.
  186. (+) OutCpltCallback : Output FIFO transfer completed callback.
  187. (+) ErrorCallback : callback for error detection.
  188. (+) MspInitCallback : CRYP MspInit.
  189. (+) MspDeInitCallback : CRYP MspDeInit.
  190. This function takes as parameters the HAL peripheral handle, the Callback ID
  191. and a pointer to the user callback function.
  192. [..]
  193. Use function HAL_CRYP_UnRegisterCallback() to reset a callback to the default
  194. weak function.
  195. HAL_CRYP_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  196. and the Callback ID.
  197. This function allows to reset following callbacks:
  198. (+) InCpltCallback : Input FIFO transfer completed callback.
  199. (+) OutCpltCallback : Output FIFO transfer completed callback.
  200. (+) ErrorCallback : callback for error detection.
  201. (+) MspInitCallback : CRYP MspInit.
  202. (+) MspDeInitCallback : CRYP MspDeInit.
  203. [..]
  204. By default, after the HAL_CRYP_Init() and when the state is HAL_CRYP_STATE_RESET
  205. all callbacks are set to the corresponding weak functions :
  206. examples HAL_CRYP_InCpltCallback() , HAL_CRYP_OutCpltCallback().
  207. Exception done for MspInit and MspDeInit functions that are
  208. reset to the legacy weak function in the HAL_CRYP_Init()/ HAL_CRYP_DeInit() only when
  209. these callbacks are null (not registered beforehand).
  210. if not, MspInit or MspDeInit are not null, the HAL_CRYP_Init() / HAL_CRYP_DeInit()
  211. keep and use the user MspInit/MspDeInit functions (registered beforehand)
  212. [..]
  213. Callbacks can be registered/unregistered in HAL_CRYP_STATE_READY state only.
  214. Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
  215. in HAL_CRYP_STATE_READY or HAL_CRYP_STATE_RESET state,
  216. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  217. In that case first register the MspInit/MspDeInit user callbacks
  218. using HAL_CRYP_RegisterCallback() before calling HAL_CRYP_DeInit()
  219. or HAL_CRYP_Init() function.
  220. [..]
  221. When The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS is set to 0 or
  222. not defined, the callback registration feature is not available and all callbacks
  223. are set to the corresponding weak functions.
  224. *** Suspend/Resume feature ***
  225. ==============================
  226. [..]
  227. The compilation define USE_HAL_CRYP_SUSPEND_RESUME when set to 1
  228. allows the user to resort to the suspend/resume feature.
  229. A low priority block processing can be suspended to process a high priority block
  230. instead. When the high priority block processing is over, the low priority block
  231. processing can be resumed, restarting from the point where it was suspended. This
  232. feature is applicable only in non-blocking interrupt mode.
  233. [..] User must resort to HAL_CRYP_Suspend() to suspend the low priority block
  234. processing. This API manages the hardware block processing suspension and saves all the
  235. internal data that will be needed to restart later on. Upon HAL_CRYP_Suspend() completion,
  236. the user can launch the processing of any other block (high priority block processing).
  237. [..] When the high priority block processing is over, user must invoke HAL_CRYP_Resume()
  238. to resume the low priority block processing. Ciphering (or deciphering) restarts from
  239. the suspension point and ends as usual.
  240. [..] HAL_CRYP_Suspend() reports an error when the suspension request is sent too late
  241. (i.e when the low priority block processing is about to end). There is no use to
  242. suspend the tag generation processing for authentication algorithms.
  243. [..]
  244. (@) If the key is written out of HAL scope (case pKey pointer set to NULL by the user),
  245. the block processing suspension/resumption mechanism is NOT applicable.
  246. [..]
  247. (@) If the Key and Initialization Vector are configured only once and configuration is
  248. skipped for consecutive processings (case KeyIVConfigSkip set to CRYP_KEYIVCONFIG_ONCE),
  249. the block processing suspension/resumption mechanism is NOT applicable.
  250. @endverbatim
  251. ******************************************************************************
  252. * @attention
  253. *
  254. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  255. * All rights reserved.</center></h2>
  256. *
  257. * This software component is licensed by ST under BSD 3-Clause license,
  258. * the "License"; You may not use this file except in compliance with the
  259. * License. You may obtain a copy of the License at:
  260. * opensource.org/licenses/BSD-3-Clause
  261. *
  262. ******************************************************************************
  263. */
  264. /* Includes ------------------------------------------------------------------*/
  265. #include "stm32wbxx_hal.h"
  266. /** @addtogroup STM32WBxx_HAL_Driver
  267. * @{
  268. */
  269. /** @addtogroup CRYP
  270. * @{
  271. */
  272. #ifdef HAL_CRYP_MODULE_ENABLED
  273. /* Private typedef -----------------------------------------------------------*/
  274. /* Private define ------------------------------------------------------------*/
  275. /** @addtogroup CRYP_Private_Defines
  276. * @{
  277. */
  278. #define CRYP_TIMEOUT_KEYPREPARATION 82U /* The latency of key preparation operation is 82 clock cycles.*/
  279. #define CRYP_TIMEOUT_GCMCCMINITPHASE 299U /* The latency of GCM/CCM init phase to prepare hash subkey is 299 clock cycles.*/
  280. #define CRYP_TIMEOUT_GCMCCMHEADERPHASE 290U /* The latency of GCM/CCM header phase is 290 clock cycles.*/
  281. #define CRYP_PHASE_READY 0x00000001U /*!< CRYP peripheral is ready for initialization. */
  282. #define CRYP_PHASE_PROCESS 0x00000002U /*!< CRYP peripheral is in processing phase */
  283. #if (USE_HAL_CRYP_SUSPEND_RESUME == 1U)
  284. #define CRYP_PHASE_HEADER_SUSPENDED 0x00000004U /*!< GCM/GMAC/CCM header phase is suspended */
  285. #define CRYP_PHASE_PAYLOAD_SUSPENDED 0x00000005U /*!< GCM/CCM payload phase is suspended */
  286. #endif /* USE_HAL_CRYP_SUSPEND_RESUME */
  287. #define CRYP_PHASE_HEADER_DMA_FEED 0x00000006U /*!< GCM/GMAC/CCM header is fed to the peripheral in DMA mode */
  288. #define CRYP_OPERATINGMODE_ENCRYPT 0x00000000U /*!< Encryption mode(Mode 1) */
  289. #define CRYP_OPERATINGMODE_KEYDERIVATION AES_CR_MODE_0 /*!< Key derivation mode only used when performing ECB and CBC decryptions (Mode 2) */
  290. #define CRYP_OPERATINGMODE_DECRYPT AES_CR_MODE_1 /*!< Decryption (Mode 3) */
  291. #define CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT AES_CR_MODE /*!< Key derivation and decryption only used when performing ECB and CBC decryptions (Mode 4) */
  292. #define CRYP_PHASE_INIT 0x00000000U /*!< GCM/GMAC (or CCM) init phase */
  293. #define CRYP_PHASE_HEADER AES_CR_GCMPH_0 /*!< GCM/GMAC or CCM header phase */
  294. #define CRYP_PHASE_PAYLOAD AES_CR_GCMPH_1 /*!< GCM(/CCM) payload phase */
  295. #define CRYP_PHASE_FINAL AES_CR_GCMPH /*!< GCM/GMAC or CCM final phase */
  296. /* CTR1 information to use in CCM algorithm */
  297. #define CRYP_CCM_CTR1_0 0x07FFFFFFU
  298. #define CRYP_CCM_CTR1_1 0xFFFFFF00U
  299. #define CRYP_CCM_CTR1_2 0x00000001U
  300. /**
  301. * @}
  302. */
  303. /* Private macro -------------------------------------------------------------*/
  304. /** @addtogroup CRYP_Private_Macros
  305. * @{
  306. */
  307. #define CRYP_SET_PHASE(__HANDLE__, __PHASE__) MODIFY_REG((__HANDLE__)->Instance->CR, AES_CR_GCMPH, (uint32_t)(__PHASE__))
  308. /**
  309. * @}
  310. */
  311. /* Private struct -------------------------------------------------------------*/
  312. /* Private variables ---------------------------------------------------------*/
  313. /* Private function prototypes -----------------------------------------------*/
  314. /** @addtogroup CRYP_Private_Functions
  315. * @{
  316. */
  317. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  318. static HAL_StatusTypeDef CRYP_SetHeaderDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size);
  319. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
  320. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
  321. static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
  322. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint32_t KeySize);
  323. static void CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
  324. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  325. static void CRYP_GCMCCM_SetPayloadPhase_IT(CRYP_HandleTypeDef *hcryp);
  326. static void CRYP_GCMCCM_SetHeaderPhase_IT(CRYP_HandleTypeDef *hcryp);
  327. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase_DMA(CRYP_HandleTypeDef *hcryp);
  328. static HAL_StatusTypeDef CRYP_GCMCCM_SetPayloadPhase_DMA(CRYP_HandleTypeDef *hcryp);
  329. static HAL_StatusTypeDef CRYP_AESGCM_Process_DMA(CRYP_HandleTypeDef *hcryp);
  330. static HAL_StatusTypeDef CRYP_AESGCM_Process_IT(CRYP_HandleTypeDef *hcryp);
  331. static HAL_StatusTypeDef CRYP_AESGCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  332. static HAL_StatusTypeDef CRYP_AESCCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  333. static HAL_StatusTypeDef CRYP_AESCCM_Process_IT(CRYP_HandleTypeDef *hcryp);
  334. static HAL_StatusTypeDef CRYP_AESCCM_Process_DMA(CRYP_HandleTypeDef *hcryp);
  335. static void CRYP_AES_ProcessData(CRYP_HandleTypeDef *hcrypt, uint32_t Timeout);
  336. static HAL_StatusTypeDef CRYP_AES_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  337. static HAL_StatusTypeDef CRYP_AES_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  338. static HAL_StatusTypeDef CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef *hcryp);
  339. static HAL_StatusTypeDef CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef *hcryp);
  340. static HAL_StatusTypeDef CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef *hcryp);
  341. static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  342. static void CRYP_ClearCCFlagWhenHigh(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  343. #if (USE_HAL_CRYP_SUSPEND_RESUME == 1U)
  344. static void CRYP_Read_IVRegisters(CRYP_HandleTypeDef *hcryp, uint32_t* Output);
  345. static void CRYP_Write_IVRegisters(CRYP_HandleTypeDef *hcryp, uint32_t* Input);
  346. static void CRYP_Read_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint32_t* Output);
  347. static void CRYP_Write_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint32_t* Input);
  348. static void CRYP_Read_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint32_t* Output, uint32_t KeySize);
  349. static void CRYP_Write_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint32_t* Input, uint32_t KeySize);
  350. static void CRYP_PhaseProcessingResume(CRYP_HandleTypeDef *hcryp);
  351. #endif /* USE_HAL_CRYP_SUSPEND_RESUME */
  352. /**
  353. * @}
  354. */
  355. /* Exported functions ---------------------------------------------------------*/
  356. /** @addtogroup CRYP_Exported_Functions
  357. * @{
  358. */
  359. /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
  360. * @brief Initialization and Configuration functions.
  361. *
  362. @verbatim
  363. ========================================================================================
  364. ##### Initialization, de-initialization and Set and Get configuration functions #####
  365. ========================================================================================
  366. [..] This section provides functions allowing to:
  367. (+) Initialize the CRYP
  368. (+) DeInitialize the CRYP
  369. (+) Initialize the CRYP MSP
  370. (+) DeInitialize the CRYP MSP
  371. (+) configure CRYP (HAL_CRYP_SetConfig) with the specified parameters in the CRYP_ConfigTypeDef
  372. Parameters which are configured in This section are :
  373. (++) Key size
  374. (++) Data Type : 32,16, 8 or 1bit
  375. (++) AlgoMode :
  376. (+++) for CRYP1 peripheral :
  377. ECB and CBC in DES/TDES Standard
  378. ECB,CBC,CTR,GCM/GMAC and CCM in AES Standard.
  379. (+++) for TinyAES2 peripheral, only ECB,CBC,CTR,GCM/GMAC and CCM in AES Standard are supported.
  380. (+) Get CRYP configuration (HAL_CRYP_GetConfig) from the specified parameters in the CRYP_HandleTypeDef
  381. @endverbatim
  382. * @{
  383. */
  384. /**
  385. * @brief Initializes the CRYP according to the specified
  386. * parameters in the CRYP_ConfigTypeDef and creates the associated handle.
  387. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  388. * the configuration information for CRYP module
  389. * @retval HAL status
  390. */
  391. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
  392. {
  393. /* Check the CRYP handle allocation */
  394. if (hcryp == NULL)
  395. {
  396. return HAL_ERROR;
  397. }
  398. /* Check parameters */
  399. assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
  400. assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
  401. assert_param(IS_CRYP_ALGORITHM(hcryp->Init.Algorithm));
  402. assert_param(IS_CRYP_INIT(hcryp->Init.KeyIVConfigSkip));
  403. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  404. if (hcryp->State == HAL_CRYP_STATE_RESET)
  405. {
  406. /* Allocate lock resource and initialize it */
  407. hcryp->Lock = HAL_UNLOCKED;
  408. hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak InCpltCallback */
  409. hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak OutCpltCallback */
  410. hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak ErrorCallback */
  411. if (hcryp->MspInitCallback == NULL)
  412. {
  413. hcryp->MspInitCallback = HAL_CRYP_MspInit; /* Legacy weak MspInit */
  414. }
  415. /* Init the low level hardware */
  416. hcryp->MspInitCallback(hcryp);
  417. }
  418. #else
  419. if (hcryp->State == HAL_CRYP_STATE_RESET)
  420. {
  421. /* Allocate lock resource and initialize it */
  422. hcryp->Lock = HAL_UNLOCKED;
  423. /* Init the low level hardware */
  424. HAL_CRYP_MspInit(hcryp);
  425. }
  426. #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
  427. /* Set the key size (This bit field is do not care in the DES or TDES modes), data type and Algorithm */
  428. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE | AES_CR_KEYSIZE | AES_CR_CHMOD, hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
  429. /* Reset Error Code field */
  430. hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
  431. /* Reset peripheral Key and IV configuration flag */
  432. hcryp->KeyIVConfig = 0U;
  433. /* Change the CRYP state */
  434. hcryp->State = HAL_CRYP_STATE_READY;
  435. /* Set the default CRYP phase */
  436. hcryp->Phase = CRYP_PHASE_READY;
  437. /* Return function status */
  438. return HAL_OK;
  439. }
  440. /**
  441. * @brief De-Initializes the CRYP peripheral.
  442. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  443. * the configuration information for CRYP module
  444. * @retval HAL status
  445. */
  446. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
  447. {
  448. /* Check the CRYP handle allocation */
  449. if (hcryp == NULL)
  450. {
  451. return HAL_ERROR;
  452. }
  453. /* Set the default CRYP phase */
  454. hcryp->Phase = CRYP_PHASE_READY;
  455. /* Reset CrypInCount and CrypOutCount */
  456. hcryp->CrypInCount = 0;
  457. hcryp->CrypOutCount = 0;
  458. hcryp->CrypHeaderCount = 0;
  459. /* Disable the CRYP peripheral clock */
  460. __HAL_CRYP_DISABLE(hcryp);
  461. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  462. if (hcryp->MspDeInitCallback == NULL)
  463. {
  464. hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit; /* Legacy weak MspDeInit */
  465. }
  466. /* DeInit the low level hardware */
  467. hcryp->MspDeInitCallback(hcryp);
  468. #else
  469. /* DeInit the low level hardware: CLOCK, NVIC.*/
  470. HAL_CRYP_MspDeInit(hcryp);
  471. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  472. /* Change the CRYP state */
  473. hcryp->State = HAL_CRYP_STATE_RESET;
  474. /* Release Lock */
  475. __HAL_UNLOCK(hcryp);
  476. /* Return function status */
  477. return HAL_OK;
  478. }
  479. /**
  480. * @brief Configure the CRYP according to the specified
  481. * parameters in the CRYP_ConfigTypeDef
  482. * @param hcryp pointer to a CRYP_HandleTypeDef structure
  483. * @param pConf pointer to a CRYP_ConfigTypeDef structure that contains
  484. * the configuration information for CRYP module
  485. * @retval HAL status
  486. */
  487. HAL_StatusTypeDef HAL_CRYP_SetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf)
  488. {
  489. /* Check the CRYP handle allocation */
  490. if ((hcryp == NULL) || (pConf == NULL))
  491. {
  492. return HAL_ERROR;
  493. }
  494. /* Check parameters */
  495. assert_param(IS_CRYP_KEYSIZE(pConf->KeySize));
  496. assert_param(IS_CRYP_DATATYPE(pConf->DataType));
  497. assert_param(IS_CRYP_ALGORITHM(pConf->Algorithm));
  498. if (hcryp->State == HAL_CRYP_STATE_READY)
  499. {
  500. /* Change the CRYP state */
  501. hcryp->State = HAL_CRYP_STATE_BUSY;
  502. /* Process locked */
  503. __HAL_LOCK(hcryp);
  504. /* Set CRYP parameters */
  505. hcryp->Init.DataType = pConf->DataType;
  506. hcryp->Init.pKey = pConf->pKey;
  507. hcryp->Init.Algorithm = pConf->Algorithm;
  508. hcryp->Init.KeySize = pConf->KeySize;
  509. hcryp->Init.pInitVect = pConf->pInitVect;
  510. hcryp->Init.Header = pConf->Header;
  511. hcryp->Init.HeaderSize = pConf->HeaderSize;
  512. hcryp->Init.B0 = pConf->B0;
  513. hcryp->Init.DataWidthUnit = pConf->DataWidthUnit;
  514. /* Set the key size (This bit field is do not care in the DES or TDES modes), data type and operating mode*/
  515. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE | AES_CR_KEYSIZE | AES_CR_CHMOD, hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
  516. /*clear error flags*/
  517. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR);
  518. /* Process Unlocked */
  519. __HAL_UNLOCK(hcryp);
  520. /* Reset Error Code field */
  521. hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
  522. /* Change the CRYP state */
  523. hcryp->State = HAL_CRYP_STATE_READY;
  524. /* Set the default CRYP phase */
  525. hcryp->Phase = CRYP_PHASE_READY;
  526. /* Return function status */
  527. return HAL_OK;
  528. }
  529. else
  530. {
  531. /* Process Unlocked */
  532. __HAL_UNLOCK(hcryp);
  533. /* Busy error code field */
  534. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  535. return HAL_ERROR;
  536. }
  537. }
  538. /**
  539. * @brief Get CRYP Configuration parameters in associated handle.
  540. * @param pConf pointer to a CRYP_ConfigTypeDef structure
  541. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  542. * the configuration information for CRYP module
  543. * @retval HAL status
  544. */
  545. HAL_StatusTypeDef HAL_CRYP_GetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf)
  546. {
  547. /* Check the CRYP handle allocation */
  548. if ((hcryp == NULL) || (pConf == NULL))
  549. {
  550. return HAL_ERROR;
  551. }
  552. if (hcryp->State == HAL_CRYP_STATE_READY)
  553. {
  554. /* Change the CRYP state */
  555. hcryp->State = HAL_CRYP_STATE_BUSY;
  556. /* Process locked */
  557. __HAL_LOCK(hcryp);
  558. /* Get CRYP parameters */
  559. pConf->DataType = hcryp->Init.DataType;
  560. pConf->pKey = hcryp->Init.pKey;
  561. pConf->Algorithm = hcryp->Init.Algorithm;
  562. pConf->KeySize = hcryp->Init.KeySize ;
  563. pConf->pInitVect = hcryp->Init.pInitVect;
  564. pConf->Header = hcryp->Init.Header ;
  565. pConf->HeaderSize = hcryp->Init.HeaderSize;
  566. pConf->B0 = hcryp->Init.B0;
  567. pConf->DataWidthUnit = hcryp->Init.DataWidthUnit;
  568. /* Process Unlocked */
  569. __HAL_UNLOCK(hcryp);
  570. /* Change the CRYP state */
  571. hcryp->State = HAL_CRYP_STATE_READY;
  572. /* Return function status */
  573. return HAL_OK;
  574. }
  575. else
  576. {
  577. /* Process Unlocked */
  578. __HAL_UNLOCK(hcryp);
  579. /* Busy error code field */
  580. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  581. return HAL_ERROR;
  582. }
  583. }
  584. /**
  585. * @brief Initializes the CRYP MSP.
  586. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  587. * the configuration information for CRYP module
  588. * @retval None
  589. */
  590. __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
  591. {
  592. /* Prevent unused argument(s) compilation warning */
  593. UNUSED(hcryp);
  594. /* NOTE : This function Should not be modified, when the callback is needed,
  595. the HAL_CRYP_MspInit could be implemented in the user file
  596. */
  597. }
  598. /**
  599. * @brief DeInitializes CRYP MSP.
  600. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  601. * the configuration information for CRYP module
  602. * @retval None
  603. */
  604. __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
  605. {
  606. /* Prevent unused argument(s) compilation warning */
  607. UNUSED(hcryp);
  608. /* NOTE : This function Should not be modified, when the callback is needed,
  609. the HAL_CRYP_MspDeInit could be implemented in the user file
  610. */
  611. }
  612. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  613. /**
  614. * @brief Register a User CRYP Callback
  615. * To be used instead of the weak predefined callback
  616. * @param hcryp cryp handle
  617. * @param CallbackID ID of the callback to be registered
  618. * This parameter can be one of the following values:
  619. * @arg @ref HAL_CRYP_INPUT_COMPLETE_CB_ID Input FIFO transfer completed callback ID
  620. * @arg @ref HAL_CRYP_OUTPUT_COMPLETE_CB_ID Output FIFO transfer completed callback ID
  621. * @arg @ref HAL_CRYP_ERROR_CB_ID Error callback ID
  622. * @arg @ref HAL_CRYP_MSPINIT_CB_ID MspInit callback ID
  623. * @arg @ref HAL_CRYP_MSPDEINIT_CB_ID MspDeInit callback ID
  624. * @param pCallback pointer to the Callback function
  625. * @retval status
  626. */
  627. HAL_StatusTypeDef HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID, pCRYP_CallbackTypeDef pCallback)
  628. {
  629. HAL_StatusTypeDef status = HAL_OK;
  630. if (pCallback == NULL)
  631. {
  632. /* Update the error code */
  633. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  634. return HAL_ERROR;
  635. }
  636. /* Process locked */
  637. __HAL_LOCK(hcryp);
  638. if (hcryp->State == HAL_CRYP_STATE_READY)
  639. {
  640. switch (CallbackID)
  641. {
  642. case HAL_CRYP_INPUT_COMPLETE_CB_ID :
  643. hcryp->InCpltCallback = pCallback;
  644. break;
  645. case HAL_CRYP_OUTPUT_COMPLETE_CB_ID :
  646. hcryp->OutCpltCallback = pCallback;
  647. break;
  648. case HAL_CRYP_ERROR_CB_ID :
  649. hcryp->ErrorCallback = pCallback;
  650. break;
  651. case HAL_CRYP_MSPINIT_CB_ID :
  652. hcryp->MspInitCallback = pCallback;
  653. break;
  654. case HAL_CRYP_MSPDEINIT_CB_ID :
  655. hcryp->MspDeInitCallback = pCallback;
  656. break;
  657. default :
  658. /* Update the error code */
  659. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  660. /* Return error status */
  661. status = HAL_ERROR;
  662. break;
  663. }
  664. }
  665. else if (hcryp->State == HAL_CRYP_STATE_RESET)
  666. {
  667. switch (CallbackID)
  668. {
  669. case HAL_CRYP_MSPINIT_CB_ID :
  670. hcryp->MspInitCallback = pCallback;
  671. break;
  672. case HAL_CRYP_MSPDEINIT_CB_ID :
  673. hcryp->MspDeInitCallback = pCallback;
  674. break;
  675. default :
  676. /* Update the error code */
  677. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  678. /* Return error status */
  679. status = HAL_ERROR;
  680. break;
  681. }
  682. }
  683. else
  684. {
  685. /* Update the error code */
  686. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  687. /* Return error status */
  688. status = HAL_ERROR;
  689. }
  690. /* Release Lock */
  691. __HAL_UNLOCK(hcryp);
  692. return status;
  693. }
  694. /**
  695. * @brief Unregister an CRYP Callback
  696. * CRYP callback is redirected to the weak predefined callback
  697. * @param hcryp cryp handle
  698. * @param CallbackID ID of the callback to be unregistered
  699. * This parameter can be one of the following values:
  700. * @arg @ref HAL_CRYP_INPUT_COMPLETE_CB_ID Input FIFO transfer completed callback ID
  701. * @arg @ref HAL_CRYP_OUTPUT_COMPLETE_CB_ID Output FIFO transfer completed callback ID
  702. * @arg @ref HAL_CRYP_ERROR_CB_ID Error callback ID
  703. * @arg @ref HAL_CRYP_MSPINIT_CB_ID MspInit callback ID
  704. * @arg @ref HAL_CRYP_MSPDEINIT_CB_ID MspDeInit callback ID
  705. * @retval status
  706. */
  707. HAL_StatusTypeDef HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID)
  708. {
  709. HAL_StatusTypeDef status = HAL_OK;
  710. /* Process locked */
  711. __HAL_LOCK(hcryp);
  712. if (hcryp->State == HAL_CRYP_STATE_READY)
  713. {
  714. switch (CallbackID)
  715. {
  716. case HAL_CRYP_INPUT_COMPLETE_CB_ID :
  717. hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak InCpltCallback */
  718. break;
  719. case HAL_CRYP_OUTPUT_COMPLETE_CB_ID :
  720. hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak OutCpltCallback */
  721. break;
  722. case HAL_CRYP_ERROR_CB_ID :
  723. hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak ErrorCallback */
  724. break;
  725. case HAL_CRYP_MSPINIT_CB_ID :
  726. hcryp->MspInitCallback = HAL_CRYP_MspInit;
  727. break;
  728. case HAL_CRYP_MSPDEINIT_CB_ID :
  729. hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;
  730. break;
  731. default :
  732. /* Update the error code */
  733. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  734. /* Return error status */
  735. status = HAL_ERROR;
  736. break;
  737. }
  738. }
  739. else if (hcryp->State == HAL_CRYP_STATE_RESET)
  740. {
  741. switch (CallbackID)
  742. {
  743. case HAL_CRYP_MSPINIT_CB_ID :
  744. hcryp->MspInitCallback = HAL_CRYP_MspInit;
  745. break;
  746. case HAL_CRYP_MSPDEINIT_CB_ID :
  747. hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;
  748. break;
  749. default :
  750. /* Update the error code */
  751. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  752. /* Return error status */
  753. status = HAL_ERROR;
  754. break;
  755. }
  756. }
  757. else
  758. {
  759. /* Update the error code */
  760. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;;
  761. /* Return error status */
  762. status = HAL_ERROR;
  763. }
  764. /* Release Lock */
  765. __HAL_UNLOCK(hcryp);
  766. return status;
  767. }
  768. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  769. #if (USE_HAL_CRYP_SUSPEND_RESUME == 1U)
  770. /**
  771. * @brief Request CRYP processing suspension when in interruption mode.
  772. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  773. * the configuration information for CRYP module.
  774. * @note Set the handle field SuspendRequest to the appropriate value so that
  775. * the on-going CRYP processing is suspended as soon as the required
  776. * conditions are met.
  777. * @note HAL_CRYP_ProcessSuspend() can only be invoked when the processing is done
  778. * in non-blocking interrupt mode.
  779. * @note It is advised not to suspend the CRYP processing when the DMA controller
  780. * is managing the data transfer.
  781. * @retval None
  782. */
  783. void HAL_CRYP_ProcessSuspend(CRYP_HandleTypeDef *hcryp)
  784. {
  785. /* Set Handle SuspendRequest field */
  786. hcryp->SuspendRequest = HAL_CRYP_SUSPEND;
  787. }
  788. /**
  789. * @brief CRYP processing suspension and peripheral internal parameters storage.
  790. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  791. * the configuration information for CRYP module
  792. * @note peripheral internal parameters are stored to be readily available when
  793. * suspended processing is resumed later on.
  794. * @retval HAL status
  795. */
  796. HAL_StatusTypeDef HAL_CRYP_Suspend(CRYP_HandleTypeDef *hcryp)
  797. {
  798. HAL_CRYP_STATETypeDef state;
  799. /* Request suspension */
  800. HAL_CRYP_ProcessSuspend(hcryp);
  801. do
  802. {
  803. state = HAL_CRYP_GetState(hcryp);
  804. } while ((state != HAL_CRYP_STATE_SUSPENDED) && (state != HAL_CRYP_STATE_READY));
  805. if (HAL_CRYP_GetState(hcryp) == HAL_CRYP_STATE_READY)
  806. {
  807. /* Processing was already over or was about to end. No suspension done */
  808. return HAL_ERROR;
  809. }
  810. else
  811. {
  812. /* Suspend Processing */
  813. /* If authentication algorithms on-going, carry out first saving steps
  814. before disable the peripheral */
  815. if ((hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC) || \
  816. (hcryp->Init.Algorithm == CRYP_AES_CCM))
  817. {
  818. /* Save Suspension registers */
  819. CRYP_Read_SuspendRegisters(hcryp, hcryp->SUSPxR_saved);
  820. /* Save Key */
  821. CRYP_Read_KeyRegisters(hcryp, hcryp->Key_saved, hcryp->Init.KeySize);
  822. /* Save IV */
  823. CRYP_Read_IVRegisters(hcryp, hcryp->IV_saved);
  824. }
  825. /* Disable AES */
  826. __HAL_CRYP_DISABLE(hcryp);
  827. /* Save low-priority block CRYP handle parameters */
  828. hcryp->Init_saved = hcryp->Init;
  829. hcryp->pCrypInBuffPtr_saved = hcryp->pCrypInBuffPtr;
  830. hcryp->pCrypOutBuffPtr_saved = hcryp->pCrypOutBuffPtr;
  831. hcryp->CrypInCount_saved = hcryp->CrypInCount;
  832. hcryp->CrypOutCount_saved = hcryp->CrypOutCount;
  833. hcryp->Phase_saved = hcryp->Phase;
  834. hcryp->State_saved = hcryp->State;
  835. hcryp->Size_saved = ( (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD) ? (hcryp->Size /4U) : hcryp->Size);
  836. hcryp->SizesSum_saved = hcryp->SizesSum;
  837. hcryp->AutoKeyDerivation_saved = hcryp->AutoKeyDerivation;
  838. hcryp->CrypHeaderCount_saved = hcryp->CrypHeaderCount;
  839. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  840. if ((hcryp->Init.Algorithm == CRYP_AES_CBC) || \
  841. (hcryp->Init.Algorithm == CRYP_AES_CTR))
  842. {
  843. /* Save Initialisation Vector registers */
  844. CRYP_Read_IVRegisters(hcryp, hcryp->IV_saved);
  845. }
  846. /* Save Control register */
  847. hcryp->CR_saved = hcryp->Instance->CR;
  848. }
  849. return HAL_OK;
  850. }
  851. /**
  852. * @brief CRYP processing resumption.
  853. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  854. * the configuration information for CRYP module
  855. * @note Processing restarts at the exact point where it was suspended, based
  856. * on the parameters saved at suspension time.
  857. * @retval HAL status
  858. */
  859. HAL_StatusTypeDef HAL_CRYP_Resume(CRYP_HandleTypeDef *hcryp)
  860. {
  861. /* Check the CRYP handle allocation */
  862. if (hcryp == NULL)
  863. {
  864. return HAL_ERROR;
  865. }
  866. if (hcryp->State_saved != HAL_CRYP_STATE_SUSPENDED)
  867. {
  868. /* CRYP was not suspended */
  869. return HAL_ERROR;
  870. }
  871. else
  872. {
  873. /* Restore low-priority block CRYP handle parameters */
  874. hcryp->Init = hcryp->Init_saved;
  875. hcryp->State = hcryp->State_saved;
  876. /* Chaining algorithms case */
  877. if ((hcryp->Init_saved.Algorithm == CRYP_AES_ECB) || \
  878. (hcryp->Init_saved.Algorithm == CRYP_AES_CBC) || \
  879. (hcryp->Init_saved.Algorithm == CRYP_AES_CTR))
  880. {
  881. /* Restore low-priority block CRYP handle parameters */
  882. hcryp->AutoKeyDerivation = hcryp->AutoKeyDerivation_saved;
  883. if ((hcryp->Init.Algorithm == CRYP_AES_CBC) || \
  884. (hcryp->Init.Algorithm == CRYP_AES_CTR))
  885. {
  886. hcryp->Init.pInitVect = hcryp->IV_saved;
  887. }
  888. __HAL_CRYP_DISABLE(hcryp);
  889. (void) HAL_CRYP_Init(hcryp);
  890. }
  891. else /* Authentication algorithms case */
  892. {
  893. /* Restore low-priority block CRYP handle parameters */
  894. hcryp->Phase = hcryp->Phase_saved;
  895. hcryp->CrypHeaderCount = hcryp->CrypHeaderCount_saved;
  896. hcryp->SizesSum = hcryp->SizesSum_saved;
  897. /* Disable AES and write-back SUSPxR registers */;
  898. __HAL_CRYP_DISABLE(hcryp);
  899. /* Restore AES Suspend Registers */
  900. CRYP_Write_SuspendRegisters(hcryp, hcryp->SUSPxR_saved);
  901. /* Restore Control, Key and IV Registers, then enable AES */
  902. hcryp->Instance->CR = hcryp->CR_saved;
  903. CRYP_Write_KeyRegisters(hcryp, hcryp->Key_saved, hcryp->Init.KeySize);
  904. CRYP_Write_IVRegisters(hcryp, hcryp->IV_saved);
  905. /* At the same time, set handle state back to READY to be able to resume the AES calculations
  906. without the processing APIs returning HAL_BUSY when called. */
  907. hcryp->State = HAL_CRYP_STATE_READY;
  908. }
  909. /* Resume low-priority block processing under IT */
  910. hcryp->ResumingFlag = 1U;
  911. if (READ_BIT(hcryp->CR_saved, AES_CR_MODE) == CRYP_OPERATINGMODE_ENCRYPT)
  912. {
  913. if (HAL_CRYP_Encrypt_IT(hcryp, hcryp->pCrypInBuffPtr_saved, hcryp->Size_saved, hcryp->pCrypOutBuffPtr_saved) != HAL_OK)
  914. {
  915. return HAL_ERROR;
  916. }
  917. }
  918. else
  919. {
  920. if (HAL_CRYP_Decrypt_IT(hcryp, hcryp->pCrypInBuffPtr_saved, hcryp->Size_saved, hcryp->pCrypOutBuffPtr_saved) != HAL_OK)
  921. {
  922. return HAL_ERROR;
  923. }
  924. }
  925. }
  926. return HAL_OK;
  927. }
  928. #endif /* defined (USE_HAL_CRYP_SUSPEND_RESUME) */
  929. /**
  930. * @}
  931. */
  932. /** @defgroup CRYP_Exported_Functions_Group2 Encryption Decryption functions
  933. * @brief Encryption Decryption functions.
  934. *
  935. @verbatim
  936. ==============================================================================
  937. ##### Encrypt Decrypt functions #####
  938. ==============================================================================
  939. [..] This section provides API allowing to Encrypt/Decrypt Data following
  940. Standard DES/TDES or AES, and Algorithm configured by the user:
  941. (+) Standard DES/TDES only supported by CRYP1 peripheral, below list of Algorithm supported :
  942. - Electronic Code Book(ECB)
  943. - Cipher Block Chaining (CBC)
  944. (+) Standard AES supported by CRYP1 peripheral & TinyAES, list of Algorithm supported:
  945. - Electronic Code Book(ECB)
  946. - Cipher Block Chaining (CBC)
  947. - Counter mode (CTR)
  948. - Cipher Block Chaining (CBC)
  949. - Counter mode (CTR)
  950. - Galois/counter mode (GCM)
  951. - Counter with Cipher Block Chaining-Message(CCM)
  952. [..] Three processing functions are available:
  953. (+) Polling mode : HAL_CRYP_Encrypt & HAL_CRYP_Decrypt
  954. (+) Interrupt mode : HAL_CRYP_Encrypt_IT & HAL_CRYP_Decrypt_IT
  955. (+) DMA mode : HAL_CRYP_Encrypt_DMA & HAL_CRYP_Decrypt_DMA
  956. @endverbatim
  957. * @{
  958. */
  959. /* GCM message structure additional details
  960. ICB
  961. +-------------------------------------------------------+
  962. | Initialization vector (IV) | Counter |
  963. |----------------|----------------|-----------|---------|
  964. 127 95 63 31 0
  965. Bit Number Register Contents
  966. ---------- --------------- -----------
  967. 127 ...96 CRYP_IV1R[31:0] ICB[127:96]
  968. 95 ...64 CRYP_IV1L[31:0] B0[95:64]
  969. 63 ... 32 CRYP_IV0R[31:0] ICB[63:32]
  970. 31 ... 0 CRYP_IV0L[31:0] ICB[31:0], where 32-bit counter= 0x2
  971. GCM last block definition
  972. +-------------------------------------------------------------------+
  973. | Bit[0] | Bit[32] | Bit[64] | Bit[96] |
  974. |-----------|--------------------|-----------|----------------------|
  975. | 0x0 | Header length[31:0]| 0x0 | Payload length[31:0] |
  976. |-----------|--------------------|-----------|----------------------|
  977. */
  978. /* CCM message blocks description
  979. (##) B0 block : According to NIST Special Publication 800-38C,
  980. The first block B0 is formatted as follows, where l(m) is encoded in
  981. most-significant-byte first order:
  982. Octet Number Contents
  983. ------------ ---------
  984. 0 Flags
  985. 1 ... 15-q Nonce N
  986. 16-q ... 15 Q
  987. the Flags field is formatted as follows:
  988. Bit Number Contents
  989. ---------- ----------------------
  990. 7 Reserved (always zero)
  991. 6 Adata
  992. 5 ... 3 (t-2)/2
  993. 2 ... 0 [q-1]3
  994. - Q: a bit string representation of the octet length of P (plaintext)
  995. - q The octet length of the binary representation of the octet length of the payload
  996. - A nonce (N), n The octet length of the where n+q=15.
  997. - Flags: most significant octet containing four flags for control information,
  998. - t The octet length of the MAC.
  999. (##) B1 block (header) : associated data length(a) concatenated with Associated Data (A)
  1000. the associated data length expressed in bytes (a) defined as below:
  1001. - If 0 < a < 216-28, then it is encoded as [a]16, i.e. two octets
  1002. - If 216-28 < a < 232, then it is encoded as 0xff || 0xfe || [a]32, i.e. six octets
  1003. - If 232 < a < 264, then it is encoded as 0xff || 0xff || [a]64, i.e. ten octets
  1004. (##) CTRx block : control blocks
  1005. - Generation of CTR1 from first block B0 information :
  1006. equal to B0 with first 5 bits zeroed and most significant bits storing octet
  1007. length of P also zeroed, then incremented by one
  1008. Bit Number Register Contents
  1009. ---------- --------------- -----------
  1010. 127 ...96 CRYP_IV1R[31:0] B0[127:96], where Q length bits are set to 0, except for
  1011. bit 0 that is set to 1
  1012. 95 ...64 CRYP_IV1L[31:0] B0[95:64]
  1013. 63 ... 32 CRYP_IV0R[31:0] B0[63:32]
  1014. 31 ... 0 CRYP_IV0L[31:0] B0[31:0], where flag bits set to 0
  1015. - Generation of CTR0: same as CTR1 with bit[0] set to zero.
  1016. */
  1017. /**
  1018. * @brief Encryption mode.
  1019. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1020. * the configuration information for CRYP module
  1021. * @param Input Pointer to the input buffer (plaintext)
  1022. * @param Size Length of the plaintext buffer in bytes or words (depending upon DataWidthUnit field)
  1023. * @param Output Pointer to the output buffer(ciphertext)
  1024. * @param Timeout Specify Timeout value
  1025. * @retval HAL status
  1026. */
  1027. HAL_StatusTypeDef HAL_CRYP_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output, uint32_t Timeout)
  1028. {
  1029. uint32_t algo;
  1030. HAL_StatusTypeDef status;
  1031. #ifdef USE_FULL_ASSERT
  1032. uint32_t algo_assert = (hcryp->Instance->CR) & AES_CR_CHMOD;
  1033. /* Check input buffer size */
  1034. assert_param(IS_CRYP_BUFFERSIZE(algo_assert, hcryp->Init.DataWidthUnit, Size));
  1035. #endif
  1036. if (hcryp->State == HAL_CRYP_STATE_READY)
  1037. {
  1038. /* Change state Busy */
  1039. hcryp->State = HAL_CRYP_STATE_BUSY;
  1040. /* Process locked */
  1041. __HAL_LOCK(hcryp);
  1042. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  1043. hcryp->CrypInCount = 0U;
  1044. hcryp->CrypOutCount = 0U;
  1045. hcryp->pCrypInBuffPtr = Input;
  1046. hcryp->pCrypOutBuffPtr = Output;
  1047. /* Calculate Size parameter in Byte*/
  1048. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1049. {
  1050. hcryp->Size = Size * 4U;
  1051. }
  1052. else
  1053. {
  1054. hcryp->Size = Size;
  1055. }
  1056. /* Set the operating mode*/
  1057. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  1058. /* algo get algorithm selected */
  1059. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1060. switch (algo)
  1061. {
  1062. case CRYP_AES_ECB:
  1063. case CRYP_AES_CBC:
  1064. case CRYP_AES_CTR:
  1065. /* AES encryption */
  1066. status = CRYP_AES_Encrypt(hcryp, Timeout);
  1067. break;
  1068. case CRYP_AES_GCM_GMAC:
  1069. /* AES GCM encryption */
  1070. status = CRYP_AESGCM_Process(hcryp, Timeout) ;
  1071. break;
  1072. case CRYP_AES_CCM:
  1073. /* AES CCM encryption */
  1074. status = CRYP_AESCCM_Process(hcryp, Timeout);
  1075. break;
  1076. default:
  1077. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1078. status = HAL_ERROR;
  1079. break;
  1080. }
  1081. if (status == HAL_OK)
  1082. {
  1083. /* Change the CRYP peripheral state */
  1084. hcryp->State = HAL_CRYP_STATE_READY;
  1085. /* Process unlocked */
  1086. __HAL_UNLOCK(hcryp);
  1087. }
  1088. }
  1089. else
  1090. {
  1091. /* Busy error code field */
  1092. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1093. status = HAL_ERROR;
  1094. }
  1095. /* Return function status */
  1096. return status;
  1097. }
  1098. /**
  1099. * @brief Decryption mode.
  1100. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1101. * the configuration information for CRYP module
  1102. * @param Input Pointer to the input buffer (ciphertext )
  1103. * @param Size Length of the plaintext buffer in bytes or words (depending upon DataWidthUnit field)
  1104. * @param Output Pointer to the output buffer(plaintext)
  1105. * @param Timeout Specify Timeout value
  1106. * @retval HAL status
  1107. */
  1108. HAL_StatusTypeDef HAL_CRYP_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output, uint32_t Timeout)
  1109. {
  1110. HAL_StatusTypeDef status;
  1111. uint32_t algo;
  1112. #ifdef USE_FULL_ASSERT
  1113. uint32_t algo_assert = (hcryp->Instance->CR) & AES_CR_CHMOD;
  1114. /* Check input buffer size */
  1115. assert_param(IS_CRYP_BUFFERSIZE(algo_assert, hcryp->Init.DataWidthUnit, Size));
  1116. #endif
  1117. if (hcryp->State == HAL_CRYP_STATE_READY)
  1118. {
  1119. /* Change state Busy */
  1120. hcryp->State = HAL_CRYP_STATE_BUSY;
  1121. /* Process locked */
  1122. __HAL_LOCK(hcryp);
  1123. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  1124. hcryp->CrypInCount = 0U;
  1125. hcryp->CrypOutCount = 0U;
  1126. hcryp->pCrypInBuffPtr = Input;
  1127. hcryp->pCrypOutBuffPtr = Output;
  1128. /* Calculate Size parameter in Byte*/
  1129. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1130. {
  1131. hcryp->Size = Size * 4U;
  1132. }
  1133. else
  1134. {
  1135. hcryp->Size = Size;
  1136. }
  1137. /* Set Decryption operating mode*/
  1138. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  1139. /* algo get algorithm selected */
  1140. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1141. switch (algo)
  1142. {
  1143. case CRYP_AES_ECB:
  1144. case CRYP_AES_CBC:
  1145. case CRYP_AES_CTR:
  1146. /* AES decryption */
  1147. status = CRYP_AES_Decrypt(hcryp, Timeout);
  1148. break;
  1149. case CRYP_AES_GCM_GMAC:
  1150. /* AES GCM decryption */
  1151. status = CRYP_AESGCM_Process(hcryp, Timeout) ;
  1152. break;
  1153. case CRYP_AES_CCM:
  1154. /* AES CCM decryption */
  1155. status = CRYP_AESCCM_Process(hcryp, Timeout);
  1156. break;
  1157. default:
  1158. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1159. status = HAL_ERROR;
  1160. break;
  1161. }
  1162. if (status == HAL_OK)
  1163. {
  1164. /* Change the CRYP peripheral state */
  1165. hcryp->State = HAL_CRYP_STATE_READY;
  1166. /* Process unlocked */
  1167. __HAL_UNLOCK(hcryp);
  1168. }
  1169. }
  1170. else
  1171. {
  1172. /* Busy error code field */
  1173. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1174. status = HAL_ERROR;
  1175. }
  1176. /* Return function status */
  1177. return status;
  1178. }
  1179. /**
  1180. * @brief Encryption in interrupt mode.
  1181. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1182. * the configuration information for CRYP module
  1183. * @param Input Pointer to the input buffer (plaintext)
  1184. * @param Size Length of the plaintext buffer in bytes or words (depending upon DataWidthUnit field)
  1185. * @param Output Pointer to the output buffer(ciphertext)
  1186. * @retval HAL status
  1187. */
  1188. HAL_StatusTypeDef HAL_CRYP_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  1189. {
  1190. HAL_StatusTypeDef status;
  1191. uint32_t algo;
  1192. #ifdef USE_FULL_ASSERT
  1193. uint32_t algo_assert = (hcryp->Instance->CR) & AES_CR_CHMOD;
  1194. /* Check input buffer size */
  1195. assert_param(IS_CRYP_BUFFERSIZE(algo_assert, hcryp->Init.DataWidthUnit, Size));
  1196. #endif
  1197. if (hcryp->State == HAL_CRYP_STATE_READY)
  1198. {
  1199. /* Change state Busy */
  1200. hcryp->State = HAL_CRYP_STATE_BUSY;
  1201. /* Process locked */
  1202. __HAL_LOCK(hcryp);
  1203. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  1204. #if (USE_HAL_CRYP_SUSPEND_RESUME == 1U)
  1205. if (hcryp->ResumingFlag == 1U)
  1206. {
  1207. hcryp->ResumingFlag = 0U;
  1208. if (hcryp->Phase != CRYP_PHASE_HEADER_SUSPENDED)
  1209. {
  1210. hcryp->CrypInCount = (uint16_t) hcryp->CrypInCount_saved;
  1211. hcryp->CrypOutCount = (uint16_t) hcryp->CrypOutCount_saved;
  1212. }
  1213. else
  1214. {
  1215. hcryp->CrypInCount = 0U;
  1216. hcryp->CrypOutCount = 0U;
  1217. }
  1218. }
  1219. else
  1220. #endif /* USE_HAL_CRYP_SUSPEND_RESUME */
  1221. {
  1222. hcryp->CrypInCount = 0U;
  1223. hcryp->CrypOutCount = 0U;
  1224. }
  1225. hcryp->pCrypInBuffPtr = Input;
  1226. hcryp->pCrypOutBuffPtr = Output;
  1227. /* Calculate Size parameter in Byte*/
  1228. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1229. {
  1230. hcryp->Size = Size * 4U;
  1231. }
  1232. else
  1233. {
  1234. hcryp->Size = Size;
  1235. }
  1236. /* Set encryption operating mode*/
  1237. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  1238. /* algo get algorithm selected */
  1239. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1240. switch (algo)
  1241. {
  1242. case CRYP_AES_ECB:
  1243. case CRYP_AES_CBC:
  1244. case CRYP_AES_CTR:
  1245. /* AES encryption */
  1246. status = CRYP_AES_Encrypt_IT(hcryp);
  1247. break;
  1248. case CRYP_AES_GCM_GMAC:
  1249. /* AES GCM encryption */
  1250. status = CRYP_AESGCM_Process_IT(hcryp) ;
  1251. break;
  1252. case CRYP_AES_CCM:
  1253. /* AES CCM encryption */
  1254. status = CRYP_AESCCM_Process_IT(hcryp);
  1255. break;
  1256. default:
  1257. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1258. status = HAL_ERROR;
  1259. break;
  1260. }
  1261. }
  1262. else
  1263. {
  1264. /* Busy error code field */
  1265. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1266. status = HAL_ERROR;
  1267. }
  1268. /* Return function status */
  1269. return status;
  1270. }
  1271. /**
  1272. * @brief Decryption in interrupt mode.
  1273. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1274. * the configuration information for CRYP module
  1275. * @param Input Pointer to the input buffer (ciphertext )
  1276. * @param Size Length of the plaintext buffer in bytes or words (depending upon DataWidthUnit field)
  1277. * @param Output Pointer to the output buffer(plaintext)
  1278. * @retval HAL status
  1279. */
  1280. HAL_StatusTypeDef HAL_CRYP_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  1281. {
  1282. HAL_StatusTypeDef status;
  1283. uint32_t algo;
  1284. #ifdef USE_FULL_ASSERT
  1285. uint32_t algo_assert = (hcryp->Instance->CR) & AES_CR_CHMOD;
  1286. /* Check input buffer size */
  1287. assert_param(IS_CRYP_BUFFERSIZE(algo_assert, hcryp->Init.DataWidthUnit, Size));
  1288. #endif
  1289. if (hcryp->State == HAL_CRYP_STATE_READY)
  1290. {
  1291. /* Change state Busy */
  1292. hcryp->State = HAL_CRYP_STATE_BUSY;
  1293. /* Process locked */
  1294. __HAL_LOCK(hcryp);
  1295. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  1296. #if (USE_HAL_CRYP_SUSPEND_RESUME == 1U)
  1297. if (hcryp->ResumingFlag == 1U)
  1298. {
  1299. hcryp->ResumingFlag = 0U;
  1300. if (hcryp->Phase != CRYP_PHASE_HEADER_SUSPENDED)
  1301. {
  1302. hcryp->CrypInCount = (uint16_t) hcryp->CrypInCount_saved;
  1303. hcryp->CrypOutCount = (uint16_t) hcryp->CrypOutCount_saved;
  1304. }
  1305. else
  1306. {
  1307. hcryp->CrypInCount = 0U;
  1308. hcryp->CrypOutCount = 0U;
  1309. }
  1310. }
  1311. else
  1312. #endif /* USE_HAL_CRYP_SUSPEND_RESUME */
  1313. {
  1314. hcryp->CrypInCount = 0U;
  1315. hcryp->CrypOutCount = 0U;
  1316. }
  1317. hcryp->pCrypInBuffPtr = Input;
  1318. hcryp->pCrypOutBuffPtr = Output;
  1319. /* Calculate Size parameter in Byte*/
  1320. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1321. {
  1322. hcryp->Size = Size * 4U;
  1323. }
  1324. else
  1325. {
  1326. hcryp->Size = Size;
  1327. }
  1328. /* Set decryption operating mode*/
  1329. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  1330. /* algo get algorithm selected */
  1331. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1332. switch (algo)
  1333. {
  1334. case CRYP_AES_ECB:
  1335. case CRYP_AES_CBC:
  1336. case CRYP_AES_CTR:
  1337. /* AES decryption */
  1338. status = CRYP_AES_Decrypt_IT(hcryp);
  1339. break;
  1340. case CRYP_AES_GCM_GMAC:
  1341. /* AES GCM decryption */
  1342. status = CRYP_AESGCM_Process_IT(hcryp) ;
  1343. break;
  1344. case CRYP_AES_CCM:
  1345. /* AES CCM decryption */
  1346. status = CRYP_AESCCM_Process_IT(hcryp);
  1347. break;
  1348. default:
  1349. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1350. status = HAL_ERROR;
  1351. break;
  1352. }
  1353. }
  1354. else
  1355. {
  1356. /* Busy error code field */
  1357. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1358. status = HAL_ERROR;
  1359. }
  1360. /* Return function status */
  1361. return status;
  1362. }
  1363. /**
  1364. * @brief Encryption in DMA mode.
  1365. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1366. * the configuration information for CRYP module
  1367. * @param Input Pointer to the input buffer (plaintext)
  1368. * @param Size Length of the plaintext buffer in bytes or words (depending upon DataWidthUnit field)
  1369. * @param Output Pointer to the output buffer(ciphertext)
  1370. * @retval HAL status
  1371. */
  1372. HAL_StatusTypeDef HAL_CRYP_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  1373. {
  1374. HAL_StatusTypeDef status;
  1375. uint32_t algo;
  1376. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  1377. #ifdef USE_FULL_ASSERT
  1378. uint32_t algo_assert = (hcryp->Instance->CR) & AES_CR_CHMOD;
  1379. /* Check input buffer size */
  1380. assert_param(IS_CRYP_BUFFERSIZE(algo_assert, hcryp->Init.DataWidthUnit, Size));
  1381. #endif
  1382. if (hcryp->State == HAL_CRYP_STATE_READY)
  1383. {
  1384. /* Change state Busy */
  1385. hcryp->State = HAL_CRYP_STATE_BUSY;
  1386. /* Process locked */
  1387. __HAL_LOCK(hcryp);
  1388. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  1389. hcryp->CrypInCount = 0U;
  1390. hcryp->CrypOutCount = 0U;
  1391. hcryp->pCrypInBuffPtr = Input;
  1392. hcryp->pCrypOutBuffPtr = Output;
  1393. /* Calculate Size parameter in Byte*/
  1394. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1395. {
  1396. hcryp->Size = Size * 4U;
  1397. }
  1398. else
  1399. {
  1400. hcryp->Size = Size;
  1401. }
  1402. /* Set encryption operating mode*/
  1403. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  1404. /* algo get algorithm selected */
  1405. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1406. switch (algo)
  1407. {
  1408. case CRYP_AES_ECB:
  1409. case CRYP_AES_CBC:
  1410. case CRYP_AES_CTR:
  1411. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  1412. {
  1413. if (hcryp->KeyIVConfig == 1U)
  1414. {
  1415. /* If the Key and IV configuration has to be done only once
  1416. and if it has already been done, skip it */
  1417. DoKeyIVConfig = 0U;
  1418. }
  1419. else
  1420. {
  1421. /* If the Key and IV configuration has to be done only once
  1422. and if it has not been done already, do it and set KeyIVConfig
  1423. to keep track it won't have to be done again next time */
  1424. hcryp->KeyIVConfig = 1U;
  1425. }
  1426. }
  1427. if (DoKeyIVConfig == 1U)
  1428. {
  1429. /* Set the Key*/
  1430. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1431. /* Set the Initialization Vector*/
  1432. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1433. {
  1434. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  1435. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1U);
  1436. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2U);
  1437. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3U);
  1438. }
  1439. } /* if (DoKeyIVConfig == 1U) */
  1440. /* Set the phase */
  1441. hcryp->Phase = CRYP_PHASE_PROCESS;
  1442. /* Start DMA process transfer for AES */
  1443. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U), (uint32_t)(hcryp->pCrypOutBuffPtr));
  1444. status = HAL_OK;
  1445. break;
  1446. case CRYP_AES_GCM_GMAC:
  1447. /* AES GCM encryption */
  1448. status = CRYP_AESGCM_Process_DMA(hcryp) ;
  1449. break;
  1450. case CRYP_AES_CCM:
  1451. /* AES CCM encryption */
  1452. status = CRYP_AESCCM_Process_DMA(hcryp);
  1453. break;
  1454. default:
  1455. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1456. status = HAL_ERROR;
  1457. break;
  1458. }
  1459. }
  1460. else
  1461. {
  1462. /* Busy error code field */
  1463. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1464. status = HAL_ERROR;
  1465. }
  1466. /* Return function status */
  1467. return status;
  1468. }
  1469. /**
  1470. * @brief Decryption in DMA mode.
  1471. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1472. * the configuration information for CRYP module
  1473. * @param Input Pointer to the input buffer (ciphertext )
  1474. * @param Size Length of the plaintext buffer in bytes or words (depending upon DataWidthUnit field)
  1475. * @param Output Pointer to the output buffer(plaintext)
  1476. * @retval HAL status
  1477. */
  1478. HAL_StatusTypeDef HAL_CRYP_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  1479. {
  1480. HAL_StatusTypeDef status;
  1481. uint32_t algo;
  1482. #ifdef USE_FULL_ASSERT
  1483. uint32_t algo_assert = (hcryp->Instance->CR) & AES_CR_CHMOD;
  1484. /* Check input buffer size */
  1485. assert_param(IS_CRYP_BUFFERSIZE(algo_assert, hcryp->Init.DataWidthUnit, Size));
  1486. #endif
  1487. if (hcryp->State == HAL_CRYP_STATE_READY)
  1488. {
  1489. /* Change state Busy */
  1490. hcryp->State = HAL_CRYP_STATE_BUSY;
  1491. /* Process locked */
  1492. __HAL_LOCK(hcryp);
  1493. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
  1494. hcryp->CrypInCount = 0U;
  1495. hcryp->CrypOutCount = 0U;
  1496. hcryp->pCrypInBuffPtr = Input;
  1497. hcryp->pCrypOutBuffPtr = Output;
  1498. /* Calculate Size parameter in Byte*/
  1499. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1500. {
  1501. hcryp->Size = Size * 4U;
  1502. }
  1503. else
  1504. {
  1505. hcryp->Size = Size;
  1506. }
  1507. /* Set decryption operating mode*/
  1508. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  1509. /* algo get algorithm selected */
  1510. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1511. switch (algo)
  1512. {
  1513. case CRYP_AES_ECB:
  1514. case CRYP_AES_CBC:
  1515. case CRYP_AES_CTR:
  1516. /* AES decryption */
  1517. status = CRYP_AES_Decrypt_DMA(hcryp);
  1518. break;
  1519. case CRYP_AES_GCM_GMAC:
  1520. /* AES GCM decryption */
  1521. status = CRYP_AESGCM_Process_DMA(hcryp) ;
  1522. break;
  1523. case CRYP_AES_CCM:
  1524. /* AES CCM decryption */
  1525. status = CRYP_AESCCM_Process_DMA(hcryp);
  1526. break;
  1527. default:
  1528. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1529. status = HAL_ERROR;
  1530. break;
  1531. }
  1532. }
  1533. else
  1534. {
  1535. /* Busy error code field */
  1536. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1537. status = HAL_ERROR;
  1538. }
  1539. /* Return function status */
  1540. return status;
  1541. }
  1542. /**
  1543. * @}
  1544. */
  1545. /** @defgroup CRYP_Exported_Functions_Group3 CRYP IRQ handler management
  1546. * @brief CRYP IRQ handler.
  1547. *
  1548. @verbatim
  1549. ==============================================================================
  1550. ##### CRYP IRQ handler management #####
  1551. ==============================================================================
  1552. [..] This section provides CRYP IRQ handler and callback functions.
  1553. (+) HAL_CRYP_IRQHandler CRYP interrupt request
  1554. (+) HAL_CRYP_InCpltCallback input data transfer complete callback
  1555. (+) HAL_CRYP_OutCpltCallback output data transfer complete callback
  1556. (+) HAL_CRYP_ErrorCallback CRYP error callback
  1557. (+) HAL_CRYP_GetState return the CRYP state
  1558. (+) HAL_CRYP_GetError return the CRYP error code
  1559. @endverbatim
  1560. * @{
  1561. */
  1562. /**
  1563. * @brief This function handles cryptographic interrupt request.
  1564. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1565. * the configuration information for CRYP module
  1566. * @retval None
  1567. */
  1568. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
  1569. {
  1570. /* Check if error occurred */
  1571. if (__HAL_CRYP_GET_IT_SOURCE(hcryp,CRYP_IT_ERRIE) != RESET)
  1572. {
  1573. /* If write Error occurred */
  1574. if (__HAL_CRYP_GET_FLAG(hcryp,CRYP_IT_WRERR) != RESET)
  1575. {
  1576. hcryp->ErrorCode |= HAL_CRYP_ERROR_WRITE;
  1577. }
  1578. /* If read Error occurred */
  1579. if (__HAL_CRYP_GET_FLAG(hcryp,CRYP_IT_RDERR) != RESET)
  1580. {
  1581. hcryp->ErrorCode |= HAL_CRYP_ERROR_READ;
  1582. }
  1583. }
  1584. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_CCF) != RESET)
  1585. {
  1586. if(__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_CCFIE) != RESET)
  1587. {
  1588. /* Clear computation complete flag */
  1589. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1590. if ((hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC) || (hcryp->Init.Algorithm == CRYP_AES_CCM))
  1591. {
  1592. /* if header phase */
  1593. if ((hcryp->Instance->CR & CRYP_PHASE_HEADER) == CRYP_PHASE_HEADER)
  1594. {
  1595. CRYP_GCMCCM_SetHeaderPhase_IT(hcryp);
  1596. }
  1597. else /* if payload phase */
  1598. {
  1599. CRYP_GCMCCM_SetPayloadPhase_IT(hcryp);
  1600. }
  1601. }
  1602. else /* AES Algorithm ECB,CBC or CTR*/
  1603. {
  1604. CRYP_AES_IT(hcryp);
  1605. }
  1606. }
  1607. }
  1608. }
  1609. /**
  1610. * @brief Return the CRYP error code.
  1611. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1612. * the configuration information for the CRYP peripheral
  1613. * @retval CRYP error code
  1614. */
  1615. uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
  1616. {
  1617. return hcryp->ErrorCode;
  1618. }
  1619. /**
  1620. * @brief Returns the CRYP state.
  1621. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1622. * the configuration information for CRYP module.
  1623. * @retval HAL state
  1624. */
  1625. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
  1626. {
  1627. return hcryp->State;
  1628. }
  1629. /**
  1630. * @brief Input FIFO transfer completed callback.
  1631. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1632. * the configuration information for CRYP module.
  1633. * @retval None
  1634. */
  1635. __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
  1636. {
  1637. /* Prevent unused argument(s) compilation warning */
  1638. UNUSED(hcryp);
  1639. /* NOTE : This function Should not be modified, when the callback is needed,
  1640. the HAL_CRYP_InCpltCallback could be implemented in the user file
  1641. */
  1642. }
  1643. /**
  1644. * @brief Output FIFO transfer completed callback.
  1645. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1646. * the configuration information for CRYP module.
  1647. * @retval None
  1648. */
  1649. __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
  1650. {
  1651. /* Prevent unused argument(s) compilation warning */
  1652. UNUSED(hcryp);
  1653. /* NOTE : This function Should not be modified, when the callback is needed,
  1654. the HAL_CRYP_OutCpltCallback could be implemented in the user file
  1655. */
  1656. }
  1657. /**
  1658. * @brief CRYP error callback.
  1659. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1660. * the configuration information for CRYP module.
  1661. * @retval None
  1662. */
  1663. __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
  1664. {
  1665. /* Prevent unused argument(s) compilation warning */
  1666. UNUSED(hcryp);
  1667. /* NOTE : This function Should not be modified, when the callback is needed,
  1668. the HAL_CRYP_ErrorCallback could be implemented in the user file
  1669. */
  1670. }
  1671. /**
  1672. * @}
  1673. */
  1674. /**
  1675. * @}
  1676. */
  1677. /* Private functions ---------------------------------------------------------*/
  1678. /** @addtogroup CRYP_Private_Functions
  1679. * @{
  1680. */
  1681. /**
  1682. * @brief Encryption in ECB/CBC & CTR Algorithm with AES Standard
  1683. * @param hcryp pointer to a CRYP_HandleTypeDef structure
  1684. * @param Timeout specify Timeout value
  1685. * @retval HAL status
  1686. */
  1687. static HAL_StatusTypeDef CRYP_AES_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  1688. {
  1689. uint16_t incount; /* Temporary CrypInCount Value */
  1690. uint16_t outcount; /* Temporary CrypOutCount Value */
  1691. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  1692. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  1693. {
  1694. if (hcryp->KeyIVConfig == 1U)
  1695. {
  1696. /* If the Key and IV configuration has to be done only once
  1697. and if it has already been done, skip it */
  1698. DoKeyIVConfig = 0U;
  1699. }
  1700. else
  1701. {
  1702. /* If the Key and IV configuration has to be done only once
  1703. and if it has not been done already, do it and set KeyIVConfig
  1704. to keep track it won't have to be done again next time */
  1705. hcryp->KeyIVConfig = 1U;
  1706. }
  1707. }
  1708. if (DoKeyIVConfig == 1U)
  1709. {
  1710. /* Set the Key*/
  1711. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1712. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1713. {
  1714. /* Set the Initialization Vector*/
  1715. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  1716. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1U);
  1717. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2U);
  1718. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3U);
  1719. }
  1720. } /* if (DoKeyIVConfig == 1U) */
  1721. /* Set the phase */
  1722. hcryp->Phase = CRYP_PHASE_PROCESS;
  1723. /* Enable CRYP */
  1724. __HAL_CRYP_ENABLE(hcryp);
  1725. incount = hcryp->CrypInCount;
  1726. outcount = hcryp->CrypOutCount;
  1727. while ((incount < (hcryp->Size / 4U)) && (outcount < (hcryp->Size / 4U)))
  1728. {
  1729. /* Write plain Ddta and get cipher data */
  1730. CRYP_AES_ProcessData(hcryp, Timeout);
  1731. incount = hcryp->CrypInCount;
  1732. outcount = hcryp->CrypOutCount;
  1733. }
  1734. /* Disable CRYP */
  1735. __HAL_CRYP_DISABLE(hcryp);
  1736. /* Change the CRYP state */
  1737. hcryp->State = HAL_CRYP_STATE_READY;
  1738. /* Return function status */
  1739. return HAL_OK;
  1740. }
  1741. /**
  1742. * @brief Encryption in ECB/CBC & CTR mode with AES Standard using interrupt mode
  1743. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1744. * the configuration information for CRYP module
  1745. * @retval HAL status
  1746. */
  1747. static HAL_StatusTypeDef CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef *hcryp)
  1748. {
  1749. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  1750. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  1751. {
  1752. if (hcryp->KeyIVConfig == 1U)
  1753. {
  1754. /* If the Key and IV configuration has to be done only once
  1755. and if it has already been done, skip it */
  1756. DoKeyIVConfig = 0U;
  1757. }
  1758. else
  1759. {
  1760. /* If the Key and IV configuration has to be done only once
  1761. and if it has not been done already, do it and set KeyIVConfig
  1762. to keep track it won't have to be done again next time */
  1763. hcryp->KeyIVConfig = 1U;
  1764. }
  1765. }
  1766. if (DoKeyIVConfig == 1U)
  1767. {
  1768. /* Set the Key*/
  1769. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1770. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1771. {
  1772. /* Set the Initialization Vector*/
  1773. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  1774. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1U);
  1775. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2U);
  1776. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3U);
  1777. }
  1778. } /* if (DoKeyIVConfig == 1U) */
  1779. /* Set the phase */
  1780. hcryp->Phase = CRYP_PHASE_PROCESS;
  1781. if (hcryp->Size != 0U)
  1782. {
  1783. /* Enable computation complete flag and error interrupts */
  1784. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  1785. /* Enable CRYP */
  1786. __HAL_CRYP_ENABLE(hcryp);
  1787. /* Write the input block in the IN FIFO */
  1788. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  1789. hcryp->CrypInCount++;
  1790. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  1791. hcryp->CrypInCount++;
  1792. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  1793. hcryp->CrypInCount++;
  1794. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  1795. hcryp->CrypInCount++;
  1796. }
  1797. else
  1798. {
  1799. /* Change the CRYP state */
  1800. hcryp->State = HAL_CRYP_STATE_READY;
  1801. /* Process unlocked */
  1802. __HAL_UNLOCK(hcryp);
  1803. }
  1804. /* Return function status */
  1805. return HAL_OK;
  1806. }
  1807. /**
  1808. * @brief Decryption in ECB/CBC & CTR mode with AES Standard
  1809. * @param hcryp pointer to a CRYP_HandleTypeDef structure
  1810. * @param Timeout Specify Timeout value
  1811. * @retval HAL status
  1812. */
  1813. static HAL_StatusTypeDef CRYP_AES_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  1814. {
  1815. uint16_t incount; /* Temporary CrypInCount Value */
  1816. uint16_t outcount; /* Temporary CrypOutCount Value */
  1817. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  1818. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  1819. {
  1820. if (hcryp->KeyIVConfig == 1U)
  1821. {
  1822. /* If the Key and IV configuration has to be done only once
  1823. and if it has already been done, skip it */
  1824. DoKeyIVConfig = 0U;
  1825. }
  1826. else
  1827. {
  1828. /* If the Key and IV configuration has to be done only once
  1829. and if it has not been done already, do it and set KeyIVConfig
  1830. to keep track it won't have to be done again next time */
  1831. hcryp->KeyIVConfig = 1U;
  1832. }
  1833. }
  1834. if (DoKeyIVConfig == 1U)
  1835. {
  1836. /* Key preparation for ECB/CBC */
  1837. if (hcryp->Init.Algorithm != CRYP_AES_CTR) /*ECB or CBC*/
  1838. {
  1839. if (hcryp->AutoKeyDerivation == DISABLE)/*Mode 2 Key preparation*/
  1840. {
  1841. /* Set key preparation for decryption operating mode*/
  1842. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION);
  1843. /* Set the Key*/
  1844. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1845. /* Enable CRYP */
  1846. __HAL_CRYP_ENABLE(hcryp);
  1847. /* Wait for CCF flag to be raised */
  1848. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  1849. {
  1850. /* Disable the CRYP peripheral clock */
  1851. __HAL_CRYP_DISABLE(hcryp);
  1852. /* Change state & error code*/
  1853. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1854. hcryp->State = HAL_CRYP_STATE_READY;
  1855. /* Process unlocked */
  1856. __HAL_UNLOCK(hcryp);
  1857. return HAL_ERROR;
  1858. }
  1859. /* Clear CCF Flag */
  1860. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1861. /* Return to decryption operating mode(Mode 3)*/
  1862. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  1863. }
  1864. else /*Mode 4 : decryption & Key preparation*/
  1865. {
  1866. /* Set the Key*/
  1867. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1868. /* Set decryption & Key preparation operating mode*/
  1869. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT);
  1870. }
  1871. }
  1872. else /*Algorithm CTR */
  1873. {
  1874. /* Set the Key*/
  1875. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1876. }
  1877. /* Set IV */
  1878. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1879. {
  1880. /* Set the Initialization Vector*/
  1881. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  1882. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1U);
  1883. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2U);
  1884. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3U);
  1885. }
  1886. } /* if (DoKeyIVConfig == 1U) */
  1887. /* Set the phase */
  1888. hcryp->Phase = CRYP_PHASE_PROCESS;
  1889. /* Enable CRYP */
  1890. __HAL_CRYP_ENABLE(hcryp);
  1891. incount = hcryp->CrypInCount;
  1892. outcount = hcryp->CrypOutCount;
  1893. while ((incount < (hcryp->Size / 4U)) && (outcount < (hcryp->Size / 4U)))
  1894. {
  1895. /* Write plain data and get cipher data */
  1896. CRYP_AES_ProcessData(hcryp, Timeout);
  1897. incount = hcryp->CrypInCount;
  1898. outcount = hcryp->CrypOutCount;
  1899. }
  1900. /* Disable CRYP */
  1901. __HAL_CRYP_DISABLE(hcryp);
  1902. /* Change the CRYP state */
  1903. hcryp->State = HAL_CRYP_STATE_READY;
  1904. /* Return function status */
  1905. return HAL_OK;
  1906. }
  1907. /**
  1908. * @brief Decryption in ECB/CBC & CTR mode with AES Standard using interrupt mode
  1909. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1910. * the configuration information for CRYP module
  1911. * @retval HAL status
  1912. */
  1913. static HAL_StatusTypeDef CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef *hcryp)
  1914. {
  1915. __IO uint32_t count = 0U;
  1916. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  1917. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  1918. {
  1919. if (hcryp->KeyIVConfig == 1U)
  1920. {
  1921. /* If the Key and IV configuration has to be done only once
  1922. and if it has already been done, skip it */
  1923. DoKeyIVConfig = 0U;
  1924. }
  1925. else
  1926. {
  1927. /* If the Key and IV configuration has to be done only once
  1928. and if it has not been done already, do it and set KeyIVConfig
  1929. to keep track it won't have to be done again next time */
  1930. hcryp->KeyIVConfig = 1U;
  1931. }
  1932. }
  1933. if (DoKeyIVConfig == 1U)
  1934. {
  1935. /* Key preparation for ECB/CBC */
  1936. if (hcryp->Init.Algorithm != CRYP_AES_CTR)
  1937. {
  1938. if (hcryp->AutoKeyDerivation == DISABLE)/*Mode 2 Key preparation*/
  1939. {
  1940. /* Set key preparation for decryption operating mode*/
  1941. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION);
  1942. /* Set the Key*/
  1943. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1944. /* Enable CRYP */
  1945. __HAL_CRYP_ENABLE(hcryp);
  1946. /* Wait for CCF flag to be raised */
  1947. count = CRYP_TIMEOUT_KEYPREPARATION;
  1948. do
  1949. {
  1950. count-- ;
  1951. if (count == 0U)
  1952. {
  1953. /* Disable the CRYP peripheral clock */
  1954. __HAL_CRYP_DISABLE(hcryp);
  1955. /* Change state */
  1956. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1957. hcryp->State = HAL_CRYP_STATE_READY;
  1958. /* Process unlocked */
  1959. __HAL_UNLOCK(hcryp);
  1960. return HAL_ERROR;
  1961. }
  1962. }
  1963. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  1964. /* Clear CCF Flag */
  1965. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1966. /* Return to decryption operating mode(Mode 3)*/
  1967. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  1968. }
  1969. else /*Mode 4 : decryption & key preparation*/
  1970. {
  1971. /* Set the Key*/
  1972. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1973. /* Set decryption & key preparation operating mode*/
  1974. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT);
  1975. }
  1976. }
  1977. else /*Algorithm CTR */
  1978. {
  1979. /* Set the Key*/
  1980. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1981. }
  1982. /* Set IV */
  1983. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1984. {
  1985. /* Set the Initialization Vector*/
  1986. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  1987. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1U);
  1988. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2U);
  1989. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3U);
  1990. }
  1991. } /* if (DoKeyIVConfig == 1U) */
  1992. /* Set the phase */
  1993. hcryp->Phase = CRYP_PHASE_PROCESS;
  1994. if (hcryp->Size != 0U)
  1995. {
  1996. /* Enable computation complete flag and error interrupts */
  1997. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  1998. /* Enable CRYP */
  1999. __HAL_CRYP_ENABLE(hcryp);
  2000. /* Write the input block in the IN FIFO */
  2001. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2002. hcryp->CrypInCount++;
  2003. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2004. hcryp->CrypInCount++;
  2005. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2006. hcryp->CrypInCount++;
  2007. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2008. hcryp->CrypInCount++;
  2009. }
  2010. else
  2011. {
  2012. /* Process locked */
  2013. __HAL_UNLOCK(hcryp);
  2014. /* Change the CRYP state */
  2015. hcryp->State = HAL_CRYP_STATE_READY;
  2016. }
  2017. /* Return function status */
  2018. return HAL_OK;
  2019. }
  2020. /**
  2021. * @brief Decryption in ECB/CBC & CTR mode with AES Standard using DMA mode
  2022. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2023. * the configuration information for CRYP module
  2024. * @retval HAL status
  2025. */
  2026. static HAL_StatusTypeDef CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef *hcryp)
  2027. {
  2028. __IO uint32_t count = 0U;
  2029. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  2030. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  2031. {
  2032. if (hcryp->KeyIVConfig == 1U)
  2033. {
  2034. /* If the Key and IV configuration has to be done only once
  2035. and if it has already been done, skip it */
  2036. DoKeyIVConfig = 0U;
  2037. }
  2038. else
  2039. {
  2040. /* If the Key and IV configuration has to be done only once
  2041. and if it has not been done already, do it and set KeyIVConfig
  2042. to keep track it won't have to be done again next time */
  2043. hcryp->KeyIVConfig = 1U;
  2044. }
  2045. }
  2046. if (DoKeyIVConfig == 1U)
  2047. {
  2048. /* Key preparation for ECB/CBC */
  2049. if (hcryp->Init.Algorithm != CRYP_AES_CTR)
  2050. {
  2051. if (hcryp->AutoKeyDerivation == DISABLE)/*Mode 2 key preparation*/
  2052. {
  2053. /* Set key preparation for decryption operating mode*/
  2054. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION);
  2055. /* Set the Key*/
  2056. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2057. /* Enable CRYP */
  2058. __HAL_CRYP_ENABLE(hcryp);
  2059. /* Wait for CCF flag to be raised */
  2060. count = CRYP_TIMEOUT_KEYPREPARATION;
  2061. do
  2062. {
  2063. count-- ;
  2064. if (count == 0U)
  2065. {
  2066. /* Disable the CRYP peripheral clock */
  2067. __HAL_CRYP_DISABLE(hcryp);
  2068. /* Change state */
  2069. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2070. hcryp->State = HAL_CRYP_STATE_READY;
  2071. /* Process unlocked */
  2072. __HAL_UNLOCK(hcryp);
  2073. return HAL_ERROR;
  2074. }
  2075. }
  2076. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  2077. /* Clear CCF Flag */
  2078. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2079. /* Return to decryption operating mode(Mode 3)*/
  2080. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  2081. }
  2082. else /*Mode 4 : decryption & key preparation*/
  2083. {
  2084. /* Set the Key*/
  2085. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2086. /* Set decryption & Key preparation operating mode*/
  2087. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT);
  2088. }
  2089. }
  2090. else /*Algorithm CTR */
  2091. {
  2092. /* Set the Key*/
  2093. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2094. }
  2095. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  2096. {
  2097. /* Set the Initialization Vector*/
  2098. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2099. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1U);
  2100. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2U);
  2101. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3U);
  2102. }
  2103. } /* if (DoKeyIVConfig == 1U) */
  2104. /* Set the phase */
  2105. hcryp->Phase = CRYP_PHASE_PROCESS;
  2106. if (hcryp->Size != 0U)
  2107. {
  2108. /* Set the input and output addresses and start DMA transfer */
  2109. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U), (uint32_t)(hcryp->pCrypOutBuffPtr));
  2110. }
  2111. else
  2112. {
  2113. /* Process unlocked */
  2114. __HAL_UNLOCK(hcryp);
  2115. /* Change the CRYP state */
  2116. hcryp->State = HAL_CRYP_STATE_READY;
  2117. }
  2118. /* Return function status */
  2119. return HAL_OK;
  2120. }
  2121. /**
  2122. * @brief DMA CRYP input data process complete callback.
  2123. * @param hdma DMA handle
  2124. * @retval None
  2125. */
  2126. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
  2127. {
  2128. CRYP_HandleTypeDef *hcryp = (CRYP_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2129. uint32_t loopcounter;
  2130. uint32_t headersize_in_bytes;
  2131. uint32_t tmp;
  2132. uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */
  2133. 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */
  2134. 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU}; /* 8-bit data type */
  2135. /* Stop the DMA transfers to the IN FIFO by clearing to "0" the DMAINEN */
  2136. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  2137. if (hcryp->Phase == CRYP_PHASE_HEADER_DMA_FEED)
  2138. {
  2139. /* DMA is disabled, CCF is meaningful. Wait for computation completion before moving forward */
  2140. CRYP_ClearCCFlagWhenHigh(hcryp, CRYP_TIMEOUT_GCMCCMHEADERPHASE);
  2141. /* Set the phase */
  2142. hcryp->Phase = CRYP_PHASE_PROCESS;
  2143. if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD)
  2144. {
  2145. headersize_in_bytes = hcryp->Init.HeaderSize * 4U;
  2146. }
  2147. else
  2148. {
  2149. headersize_in_bytes = hcryp->Init.HeaderSize;
  2150. }
  2151. if ((headersize_in_bytes % 16U) != 0U)
  2152. {
  2153. /* Write last words that couldn't be fed by DMA */
  2154. hcryp->CrypHeaderCount = (uint16_t)((headersize_in_bytes / 16U) * 4U);
  2155. for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 4U) % 4U)); loopcounter++)
  2156. {
  2157. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2158. hcryp->CrypHeaderCount++ ;
  2159. }
  2160. /* If the header size is a multiple of words */
  2161. if ((headersize_in_bytes % 4U) == 0U)
  2162. {
  2163. /* Pad the data with zeros to have a complete block */
  2164. while (loopcounter < 4U)
  2165. {
  2166. hcryp->Instance->DINR = 0x0U;
  2167. loopcounter++;
  2168. }
  2169. }
  2170. else
  2171. {
  2172. /* Enter last bytes, padded with zeros */
  2173. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2174. tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)];
  2175. hcryp->Instance->DINR = tmp;
  2176. loopcounter++;
  2177. /* Pad the data with zeros to have a complete block */
  2178. while (loopcounter < 4U)
  2179. {
  2180. hcryp->Instance->DINR = 0x0U;
  2181. loopcounter++;
  2182. }
  2183. }
  2184. /* Wait for computation completion before moving forward */
  2185. CRYP_ClearCCFlagWhenHigh(hcryp, CRYP_TIMEOUT_GCMCCMHEADERPHASE);
  2186. } /* if ((headersize_in_bytes % 16U) != 0U) */
  2187. /* Set to 0 the number of non-valid bytes using NPBLB register*/
  2188. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 0U);
  2189. /* Select payload phase once the header phase is performed */
  2190. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2191. /* Initiate payload DMA IN and processed data DMA OUT transfers */
  2192. (void)CRYP_GCMCCM_SetPayloadPhase_DMA(hcryp);
  2193. }
  2194. /* Call input data transfer complete callback */
  2195. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  2196. /*Call registered Input complete callback*/
  2197. hcryp->InCpltCallback(hcryp);
  2198. #else
  2199. /*Call legacy weak Input complete callback*/
  2200. HAL_CRYP_InCpltCallback(hcryp);
  2201. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2202. }
  2203. /**
  2204. * @brief DMA CRYP output data process complete callback.
  2205. * @param hdma DMA handle
  2206. * @retval None
  2207. */
  2208. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
  2209. {
  2210. uint32_t count;
  2211. uint32_t npblb;
  2212. uint32_t lastwordsize;
  2213. uint32_t temp[4]; /* Temporary CrypOutBuff */
  2214. uint32_t mode;
  2215. CRYP_HandleTypeDef *hcryp = (CRYP_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2216. /* Stop the DMA transfers to the OUT FIFO by clearing to "0" the DMAOUTEN */
  2217. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
  2218. /* Clear CCF flag */
  2219. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2220. /* Last block transfer in case of GCM or CCM with Size not %16*/
  2221. if (((hcryp->Size) % 16U) != 0U)
  2222. {
  2223. /* set CrypInCount and CrypOutCount to exact number of word already computed via DMA */
  2224. hcryp->CrypInCount = (hcryp->Size / 16U) * 4U;
  2225. hcryp->CrypOutCount = hcryp->CrypInCount;
  2226. /* Compute the number of padding bytes in last block of payload */
  2227. npblb = ((((uint32_t)hcryp->Size / 16U) + 1U) * 16U) - ((uint32_t)hcryp->Size);
  2228. mode = hcryp->Instance->CR & AES_CR_MODE;
  2229. if (((mode == CRYP_OPERATINGMODE_ENCRYPT) && (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)) ||
  2230. ((mode == CRYP_OPERATINGMODE_DECRYPT) && (hcryp->Init.Algorithm == CRYP_AES_CCM)))
  2231. {
  2232. /* Specify the number of non-valid bytes using NPBLB register*/
  2233. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, npblb << 20U);
  2234. }
  2235. /* Number of valid words (lastwordsize) in last block */
  2236. if ((npblb % 4U) == 0U)
  2237. {
  2238. lastwordsize = (16U - npblb) / 4U;
  2239. }
  2240. else
  2241. {
  2242. lastwordsize = ((16U - npblb) / 4U) + 1U;
  2243. }
  2244. /* Last block optionally pad the data with zeros*/
  2245. for (count = 0U; count < lastwordsize; count++)
  2246. {
  2247. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2248. hcryp->CrypInCount++;
  2249. }
  2250. while (count < 4U)
  2251. {
  2252. /* Pad the data with zeros to have a complete block */
  2253. hcryp->Instance->DINR = 0x0U;
  2254. count++;
  2255. }
  2256. /*Wait on CCF flag*/
  2257. CRYP_ClearCCFlagWhenHigh(hcryp, CRYP_TIMEOUT_GCMCCMHEADERPHASE);
  2258. /*Read the output block from the output FIFO */
  2259. for (count = 0U; count < 4U; count++)
  2260. {
  2261. /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */
  2262. temp[count] = hcryp->Instance->DOUTR;
  2263. }
  2264. count = 0U;
  2265. while((hcryp->CrypOutCount < ((hcryp->Size + 3U)/4U)) && (count<4U))
  2266. {
  2267. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[count];
  2268. hcryp->CrypOutCount++;
  2269. count++;
  2270. }
  2271. }
  2272. if (((hcryp->Init.Algorithm & CRYP_AES_GCM_GMAC) != CRYP_AES_GCM_GMAC) && ((hcryp->Init.Algorithm & CRYP_AES_CCM) != CRYP_AES_CCM))
  2273. {
  2274. /* Disable CRYP (not allowed in GCM)*/
  2275. __HAL_CRYP_DISABLE(hcryp);
  2276. }
  2277. /* Change the CRYP state to ready */
  2278. hcryp->State = HAL_CRYP_STATE_READY;
  2279. /* Process unlocked */
  2280. __HAL_UNLOCK(hcryp);
  2281. /* Call output data transfer complete callback */
  2282. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  2283. /*Call registered Output complete callback*/
  2284. hcryp->OutCpltCallback(hcryp);
  2285. #else
  2286. /*Call legacy weak Output complete callback*/
  2287. HAL_CRYP_OutCpltCallback(hcryp);
  2288. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2289. }
  2290. /**
  2291. * @brief DMA CRYP communication error callback.
  2292. * @param hdma DMA handle
  2293. * @retval None
  2294. */
  2295. static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
  2296. {
  2297. CRYP_HandleTypeDef *hcryp = (CRYP_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2298. /* Change the CRYP peripheral state */
  2299. hcryp->State = HAL_CRYP_STATE_READY;
  2300. /* DMA error code field */
  2301. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2302. /* Clear CCF flag */
  2303. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2304. /* Call error callback */
  2305. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  2306. /*Call registered error callback*/
  2307. hcryp->ErrorCallback(hcryp);
  2308. #else
  2309. /*Call legacy weak error callback*/
  2310. HAL_CRYP_ErrorCallback(hcryp);
  2311. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2312. }
  2313. /**
  2314. * @brief Set the DMA configuration and start the DMA transfer
  2315. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2316. * the configuration information for CRYP module
  2317. * @param inputaddr address of the input buffer
  2318. * @param Size size of the input and output buffers in words, must be a multiple of 4
  2319. * @param outputaddr address of the output buffer
  2320. * @retval None
  2321. */
  2322. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  2323. {
  2324. /* Set the CRYP DMA transfer complete callback */
  2325. hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
  2326. /* Set the DMA input error callback */
  2327. hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
  2328. /* Set the CRYP DMA transfer complete callback */
  2329. hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
  2330. /* Set the DMA output error callback */
  2331. hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
  2332. if ((hcryp->Init.Algorithm & CRYP_AES_GCM_GMAC) != CRYP_AES_GCM_GMAC)
  2333. {
  2334. /* Enable CRYP (not allowed in GCM & CCM)*/
  2335. __HAL_CRYP_ENABLE(hcryp);
  2336. }
  2337. /* Enable the DMA input stream */
  2338. if (HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size) != HAL_OK)
  2339. {
  2340. /* DMA error code field */
  2341. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2342. /* Call error callback */
  2343. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  2344. /*Call registered error callback*/
  2345. hcryp->ErrorCallback(hcryp);
  2346. #else
  2347. /*Call legacy weak error callback*/
  2348. HAL_CRYP_ErrorCallback(hcryp);
  2349. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2350. }
  2351. /* Enable the DMA output stream */
  2352. if (HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size) != HAL_OK)
  2353. {
  2354. /* DMA error code field */
  2355. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2356. /* Call error callback */
  2357. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  2358. /*Call registered error callback*/
  2359. hcryp->ErrorCallback(hcryp);
  2360. #else
  2361. /*Call legacy weak error callback*/
  2362. HAL_CRYP_ErrorCallback(hcryp);
  2363. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2364. }
  2365. /* Enable In and Out DMA requests */
  2366. SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN));
  2367. }
  2368. /**
  2369. * @brief Set the DMA configuration and start the header DMA transfer
  2370. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2371. * the configuration information for CRYP module
  2372. * @param inputaddr address of the input buffer
  2373. * @param Size size of the input buffer in words, must be a multiple of 4
  2374. * @retval None
  2375. */
  2376. static HAL_StatusTypeDef CRYP_SetHeaderDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size)
  2377. {
  2378. /* Set the CRYP DMA transfer complete callback */
  2379. hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
  2380. /* Set the DMA input error callback */
  2381. hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
  2382. /* Mark that header is fed to the peripheral in DMA mode */
  2383. hcryp->Phase = CRYP_PHASE_HEADER_DMA_FEED;
  2384. /* Enable the DMA input stream */
  2385. if (HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size) != HAL_OK)
  2386. {
  2387. /* DMA error code field */
  2388. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2389. hcryp->State = HAL_CRYP_STATE_READY;
  2390. /* Process unlocked */
  2391. __HAL_UNLOCK(hcryp);
  2392. return HAL_ERROR;
  2393. /* Call error callback */
  2394. }
  2395. /* Enable IN DMA requests */
  2396. SET_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  2397. return HAL_OK;
  2398. }
  2399. /**
  2400. * @brief Process Data: Write Input data in polling mode and used in AES functions.
  2401. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2402. * the configuration information for CRYP module
  2403. * @param Timeout Specify Timeout value
  2404. * @retval None
  2405. */
  2406. static void CRYP_AES_ProcessData(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  2407. {
  2408. uint32_t temp[4]; /* Temporary CrypOutBuff */
  2409. uint32_t i;
  2410. /* Write the input block in the IN FIFO */
  2411. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2412. hcryp->CrypInCount++;
  2413. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2414. hcryp->CrypInCount++;
  2415. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2416. hcryp->CrypInCount++;
  2417. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2418. hcryp->CrypInCount++;
  2419. /* Wait for CCF flag to be raised */
  2420. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  2421. {
  2422. /* Disable the CRYP peripheral clock */
  2423. __HAL_CRYP_DISABLE(hcryp);
  2424. /* Change state */
  2425. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2426. hcryp->State = HAL_CRYP_STATE_READY;
  2427. /* Process unlocked */
  2428. __HAL_UNLOCK(hcryp);
  2429. /*Call registered error callback*/
  2430. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  2431. hcryp->ErrorCallback(hcryp);
  2432. #else
  2433. /*Call legacy weak error callback*/
  2434. HAL_CRYP_ErrorCallback(hcryp);
  2435. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2436. }
  2437. /* Clear CCF Flag */
  2438. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2439. /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer*/
  2440. for (i = 0U; i < 4U; i++)
  2441. {
  2442. temp[i] = hcryp->Instance->DOUTR;
  2443. }
  2444. i= 0U;
  2445. while((hcryp->CrypOutCount < ((hcryp->Size + 3U)/4U)) && (i<4U))
  2446. {
  2447. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  2448. hcryp->CrypOutCount++;
  2449. i++;
  2450. }
  2451. }
  2452. /**
  2453. * @brief Handle CRYP block input/output data handling under interruption.
  2454. * @note The function is called under interruption only, once
  2455. * interruptions have been enabled by HAL_CRYP_Encrypt_IT or HAL_CRYP_Decrypt_IT.
  2456. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2457. * the configuration information for CRYP module.
  2458. * @retval HAL status
  2459. */
  2460. static void CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
  2461. {
  2462. uint32_t temp[4]; /* Temporary CrypOutBuff */
  2463. uint32_t i;
  2464. if (hcryp->State == HAL_CRYP_STATE_BUSY)
  2465. {
  2466. /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer*/
  2467. for (i = 0U; i < 4U; i++)
  2468. {
  2469. temp[i] = hcryp->Instance->DOUTR;
  2470. }
  2471. i= 0U;
  2472. while((hcryp->CrypOutCount < ((hcryp->Size + 3U)/4U)) && (i<4U))
  2473. {
  2474. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  2475. hcryp->CrypOutCount++;
  2476. i++;
  2477. }
  2478. if (hcryp->CrypOutCount == (hcryp->Size / 4U))
  2479. {
  2480. /* Disable Computation Complete flag and errors interrupts */
  2481. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  2482. /* Change the CRYP state */
  2483. hcryp->State = HAL_CRYP_STATE_READY;
  2484. /* Disable CRYP */
  2485. __HAL_CRYP_DISABLE(hcryp);
  2486. /* Process Unlocked */
  2487. __HAL_UNLOCK(hcryp);
  2488. /* Call Output transfer complete callback */
  2489. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  2490. /*Call registered Output complete callback*/
  2491. hcryp->OutCpltCallback(hcryp);
  2492. #else
  2493. /*Call legacy weak Output complete callback*/
  2494. HAL_CRYP_OutCpltCallback(hcryp);
  2495. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2496. }
  2497. else
  2498. {
  2499. #if (USE_HAL_CRYP_SUSPEND_RESUME == 1U)
  2500. /* If suspension flag has been raised, suspend processing
  2501. only if not already at the end of the payload */
  2502. if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  2503. {
  2504. /* Clear CCF Flag */
  2505. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2506. /* reset SuspendRequest */
  2507. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  2508. /* Disable Computation Complete Flag and Errors Interrupts */
  2509. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  2510. /* Change the CRYP state */
  2511. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  2512. /* Mark that the payload phase is suspended */
  2513. hcryp->Phase = CRYP_PHASE_PAYLOAD_SUSPENDED;
  2514. /* Process Unlocked */
  2515. __HAL_UNLOCK(hcryp);
  2516. }
  2517. else
  2518. #endif /* USE_HAL_CRYP_SUSPEND_RESUME */
  2519. {
  2520. /* Write the input block in the IN FIFO */
  2521. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2522. hcryp->CrypInCount++;
  2523. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2524. hcryp->CrypInCount++;
  2525. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2526. hcryp->CrypInCount++;
  2527. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2528. hcryp->CrypInCount++;
  2529. if (hcryp->CrypInCount == (hcryp->Size / 4U))
  2530. {
  2531. /* Call Input transfer complete callback */
  2532. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  2533. /*Call registered Input complete callback*/
  2534. hcryp->InCpltCallback(hcryp);
  2535. #else
  2536. /*Call legacy weak Input complete callback*/
  2537. HAL_CRYP_InCpltCallback(hcryp);
  2538. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2539. }
  2540. }
  2541. }
  2542. }
  2543. else
  2544. {
  2545. /* Busy error code field */
  2546. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  2547. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  2548. /*Call registered error callback*/
  2549. hcryp->ErrorCallback(hcryp);
  2550. #else
  2551. /*Call legacy weak error callback*/
  2552. HAL_CRYP_ErrorCallback(hcryp);
  2553. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2554. }
  2555. }
  2556. /**
  2557. * @brief Writes Key in Key registers.
  2558. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2559. * the configuration information for CRYP module
  2560. * @param KeySize Size of Key
  2561. * @note If pKey is NULL, the Key registers are not written. This configuration
  2562. * occurs when the key is written out of HAL scope.
  2563. * @retval None
  2564. */
  2565. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint32_t KeySize)
  2566. {
  2567. if (hcryp->Init.pKey != NULL)
  2568. {
  2569. switch (KeySize)
  2570. {
  2571. case CRYP_KEYSIZE_256B:
  2572. hcryp->Instance->KEYR7 = *(uint32_t *)(hcryp->Init.pKey);
  2573. hcryp->Instance->KEYR6 = *(uint32_t *)(hcryp->Init.pKey + 1U);
  2574. hcryp->Instance->KEYR5 = *(uint32_t *)(hcryp->Init.pKey + 2U);
  2575. hcryp->Instance->KEYR4 = *(uint32_t *)(hcryp->Init.pKey + 3U);
  2576. hcryp->Instance->KEYR3 = *(uint32_t *)(hcryp->Init.pKey + 4U);
  2577. hcryp->Instance->KEYR2 = *(uint32_t *)(hcryp->Init.pKey + 5U);
  2578. hcryp->Instance->KEYR1 = *(uint32_t *)(hcryp->Init.pKey + 6U);
  2579. hcryp->Instance->KEYR0 = *(uint32_t *)(hcryp->Init.pKey + 7U);
  2580. break;
  2581. case CRYP_KEYSIZE_128B:
  2582. hcryp->Instance->KEYR3 = *(uint32_t *)(hcryp->Init.pKey);
  2583. hcryp->Instance->KEYR2 = *(uint32_t *)(hcryp->Init.pKey + 1U);
  2584. hcryp->Instance->KEYR1 = *(uint32_t *)(hcryp->Init.pKey + 2U);
  2585. hcryp->Instance->KEYR0 = *(uint32_t *)(hcryp->Init.pKey + 3U);
  2586. break;
  2587. default:
  2588. break;
  2589. }
  2590. }
  2591. }
  2592. /**
  2593. * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG
  2594. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2595. * the configuration information for CRYP module
  2596. * @param Timeout Timeout duration
  2597. * @retval HAL status
  2598. */
  2599. static HAL_StatusTypeDef CRYP_AESGCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  2600. {
  2601. uint32_t tickstart;
  2602. uint32_t wordsize = ((uint32_t)hcryp->Size / 4U) ;
  2603. uint32_t npblb;
  2604. uint32_t temp[4]; /* Temporary CrypOutBuff */
  2605. uint32_t index;
  2606. uint32_t lastwordsize;
  2607. uint32_t incount; /* Temporary CrypInCount Value */
  2608. uint32_t outcount; /* Temporary CrypOutCount Value */
  2609. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  2610. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  2611. {
  2612. if (hcryp->KeyIVConfig == 1U)
  2613. {
  2614. /* If the Key and IV configuration has to be done only once
  2615. and if it has already been done, skip it */
  2616. DoKeyIVConfig = 0U;
  2617. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  2618. }
  2619. else
  2620. {
  2621. /* If the Key and IV configuration has to be done only once
  2622. and if it has not been done already, do it and set KeyIVConfig
  2623. to keep track it won't have to be done again next time */
  2624. hcryp->KeyIVConfig = 1U;
  2625. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  2626. }
  2627. }
  2628. else
  2629. {
  2630. hcryp->SizesSum = hcryp->Size;
  2631. }
  2632. if (DoKeyIVConfig == 1U)
  2633. {
  2634. /* Reset CrypHeaderCount */
  2635. hcryp->CrypHeaderCount = 0U;
  2636. /****************************** Init phase **********************************/
  2637. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  2638. /* Set the key */
  2639. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2640. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  2641. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2642. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1U);
  2643. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2U);
  2644. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3U);
  2645. /* Enable the CRYP peripheral */
  2646. __HAL_CRYP_ENABLE(hcryp);
  2647. /* just wait for hash computation */
  2648. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  2649. {
  2650. /* Change state */
  2651. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2652. hcryp->State = HAL_CRYP_STATE_READY;
  2653. /* Process unlocked & return error */
  2654. __HAL_UNLOCK(hcryp);
  2655. return HAL_ERROR;
  2656. }
  2657. /* Clear CCF flag */
  2658. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2659. /************************ Header phase *************************************/
  2660. if (CRYP_GCMCCM_SetHeaderPhase(hcryp, Timeout) != HAL_OK)
  2661. {
  2662. return HAL_ERROR;
  2663. }
  2664. /*************************Payload phase ************************************/
  2665. /* Set the phase */
  2666. hcryp->Phase = CRYP_PHASE_PROCESS;
  2667. /* Select payload phase once the header phase is performed */
  2668. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2669. /* Set to 0 the number of non-valid bytes using NPBLB register*/
  2670. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 0U);
  2671. } /* if (DoKeyIVConfig == 1U) */
  2672. if ((hcryp->Size % 16U) != 0U)
  2673. {
  2674. /* recalculate wordsize */
  2675. wordsize = ((wordsize / 4U) * 4U) ;
  2676. }
  2677. /* Get tick */
  2678. tickstart = HAL_GetTick();
  2679. /* Write input data and get output Data */
  2680. incount = hcryp->CrypInCount;
  2681. outcount = hcryp->CrypOutCount;
  2682. while ((incount < wordsize) && (outcount < wordsize))
  2683. {
  2684. /* Write plain data and get cipher data */
  2685. CRYP_AES_ProcessData(hcryp, Timeout);
  2686. /* Check for the Timeout */
  2687. if (Timeout != HAL_MAX_DELAY)
  2688. {
  2689. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  2690. {
  2691. /* Disable the CRYP peripheral clock */
  2692. __HAL_CRYP_DISABLE(hcryp);
  2693. /* Change state & error code */
  2694. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2695. hcryp->State = HAL_CRYP_STATE_READY;
  2696. /* Process unlocked */
  2697. __HAL_UNLOCK(hcryp);
  2698. return HAL_ERROR;
  2699. }
  2700. }
  2701. incount = hcryp->CrypInCount;
  2702. outcount = hcryp->CrypOutCount;
  2703. }
  2704. if ((hcryp->Size % 16U) != 0U)
  2705. {
  2706. /* Compute the number of padding bytes in last block of payload */
  2707. npblb = ((((uint32_t)hcryp->Size / 16U) + 1U) * 16U) - ((uint32_t)hcryp->Size);
  2708. /* Set Npblb in case of AES GCM payload encryption to get right tag*/
  2709. if ((hcryp->Instance->CR & AES_CR_MODE) == CRYP_OPERATINGMODE_ENCRYPT)
  2710. {
  2711. /* Set to 0 the number of non-valid bytes using NPBLB register*/
  2712. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, npblb << 20U);
  2713. }
  2714. /* Number of valid words (lastwordsize) in last block */
  2715. if ((npblb % 4U) == 0U)
  2716. {
  2717. lastwordsize = (16U - npblb) / 4U;
  2718. }
  2719. else
  2720. {
  2721. lastwordsize = ((16U - npblb) / 4U) + 1U;
  2722. }
  2723. /* last block optionally pad the data with zeros*/
  2724. for (index = 0U; index < lastwordsize; index ++)
  2725. {
  2726. /* Write the last Input block in the IN FIFO */
  2727. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2728. hcryp->CrypInCount++;
  2729. }
  2730. while (index < 4U)
  2731. {
  2732. /* pad the data with zeros to have a complete block */
  2733. hcryp->Instance->DINR = 0U;
  2734. index++;
  2735. }
  2736. /* Wait for CCF flag to be raised */
  2737. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  2738. {
  2739. hcryp->State = HAL_CRYP_STATE_READY;
  2740. __HAL_UNLOCK(hcryp);
  2741. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  2742. /*Call registered error callback*/
  2743. hcryp->ErrorCallback(hcryp);
  2744. #else
  2745. /*Call legacy weak error callback*/
  2746. HAL_CRYP_ErrorCallback(hcryp);
  2747. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2748. }
  2749. /* Clear CCF Flag */
  2750. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2751. /*Read the output block from the output FIFO */
  2752. for (index = 0U; index < 4U; index++)
  2753. {
  2754. /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */
  2755. temp[index] = hcryp->Instance->DOUTR;
  2756. }
  2757. for (index = 0U; index < lastwordsize; index++)
  2758. {
  2759. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = temp[index];
  2760. hcryp->CrypOutCount++;
  2761. }
  2762. }
  2763. /* Return function status */
  2764. return HAL_OK;
  2765. }
  2766. /**
  2767. * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG in interrupt mode
  2768. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  2769. * the configuration information for CRYP module
  2770. * @retval HAL status
  2771. */
  2772. static HAL_StatusTypeDef CRYP_AESGCM_Process_IT(CRYP_HandleTypeDef *hcryp)
  2773. {
  2774. __IO uint32_t count = 0U;
  2775. uint32_t loopcounter;
  2776. uint32_t lastwordsize;
  2777. uint32_t npblb;
  2778. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  2779. uint32_t headersize_in_bytes;
  2780. uint32_t tmp;
  2781. uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */
  2782. 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */
  2783. 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU}; /* 8-bit data type */
  2784. #if (USE_HAL_CRYP_SUSPEND_RESUME == 1U)
  2785. if ((hcryp->Phase == CRYP_PHASE_HEADER_SUSPENDED) || (hcryp->Phase == CRYP_PHASE_PAYLOAD_SUSPENDED))
  2786. {
  2787. CRYP_PhaseProcessingResume(hcryp);
  2788. return HAL_OK;
  2789. }
  2790. #endif /* USE_HAL_CRYP_SUSPEND_RESUME */
  2791. /* Manage header size given in bytes to handle cases where
  2792. header size is not a multiple of 4 bytes */
  2793. if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD)
  2794. {
  2795. headersize_in_bytes = hcryp->Init.HeaderSize * 4U;
  2796. }
  2797. else
  2798. {
  2799. headersize_in_bytes = hcryp->Init.HeaderSize;
  2800. }
  2801. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  2802. {
  2803. if (hcryp->KeyIVConfig == 1U)
  2804. {
  2805. /* If the Key and IV configuration has to be done only once
  2806. and if it has already been done, skip it */
  2807. DoKeyIVConfig = 0U;
  2808. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  2809. }
  2810. else
  2811. {
  2812. /* If the Key and IV configuration has to be done only once
  2813. and if it has not been done already, do it and set KeyIVConfig
  2814. to keep track it won't have to be done again next time */
  2815. hcryp->KeyIVConfig = 1U;
  2816. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  2817. }
  2818. }
  2819. else
  2820. {
  2821. hcryp->SizesSum = hcryp->Size;
  2822. }
  2823. /* Configure Key, IV and process message (header and payload) */
  2824. if (DoKeyIVConfig == 1U)
  2825. {
  2826. /* Reset CrypHeaderCount */
  2827. hcryp->CrypHeaderCount = 0U;
  2828. /******************************* Init phase *********************************/
  2829. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  2830. /* Set the key */
  2831. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2832. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  2833. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2834. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1U);
  2835. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2U);
  2836. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3U);
  2837. /* Enable the CRYP peripheral */
  2838. __HAL_CRYP_ENABLE(hcryp);
  2839. /* just wait for hash computation */
  2840. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  2841. do
  2842. {
  2843. count-- ;
  2844. if (count == 0U)
  2845. {
  2846. /* Disable the CRYP peripheral clock */
  2847. __HAL_CRYP_DISABLE(hcryp);
  2848. /* Change state */
  2849. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2850. hcryp->State = HAL_CRYP_STATE_READY;
  2851. /* Process unlocked */
  2852. __HAL_UNLOCK(hcryp);
  2853. return HAL_ERROR;
  2854. }
  2855. }
  2856. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  2857. /* Clear CCF flag */
  2858. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2859. /***************************** Header phase *********************************/
  2860. /* Select header phase */
  2861. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  2862. /* Enable computation complete flag and error interrupts */
  2863. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  2864. /* Enable the CRYP peripheral */
  2865. __HAL_CRYP_ENABLE(hcryp);
  2866. if (hcryp->Init.HeaderSize == 0U) /*header phase is skipped*/
  2867. {
  2868. /* Set the phase */
  2869. hcryp->Phase = CRYP_PHASE_PROCESS;
  2870. /* Select payload phase once the header phase is performed */
  2871. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_PAYLOAD);
  2872. /* Set to 0 the number of non-valid bytes using NPBLB register*/
  2873. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 0U);
  2874. /* Write the payload Input block in the IN FIFO */
  2875. if (hcryp->Size == 0U)
  2876. {
  2877. /* Disable interrupts */
  2878. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  2879. /* Change the CRYP state */
  2880. hcryp->State = HAL_CRYP_STATE_READY;
  2881. /* Process unlocked */
  2882. __HAL_UNLOCK(hcryp);
  2883. }
  2884. else if (hcryp->Size >= 16U)
  2885. {
  2886. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2887. hcryp->CrypInCount++;
  2888. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2889. hcryp->CrypInCount++;
  2890. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2891. hcryp->CrypInCount++;
  2892. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2893. hcryp->CrypInCount++;
  2894. if (hcryp->CrypInCount == (hcryp->Size / 4U))
  2895. {
  2896. /* Call Input transfer complete callback */
  2897. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  2898. /*Call registered Input complete callback*/
  2899. hcryp->InCpltCallback(hcryp);
  2900. #else
  2901. /*Call legacy weak Input complete callback*/
  2902. HAL_CRYP_InCpltCallback(hcryp);
  2903. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2904. }
  2905. }
  2906. else /* Size < 16Bytes : first block is the last block*/
  2907. {
  2908. /* Workaround not implemented for TinyAES2*/
  2909. /* Size should be %4 otherwise Tag will be incorrectly generated for GCM Encryption:
  2910. Workaround is implemented in polling mode, so if last block of
  2911. payload <128bit do not use CRYP_Encrypt_IT otherwise TAG is incorrectly generated for GCM Encryption. */
  2912. /* Compute the number of padding bytes in last block of payload */
  2913. npblb = 16U - ((uint32_t)hcryp->Size);
  2914. if ((hcryp->Instance->CR & AES_CR_MODE) == CRYP_OPERATINGMODE_ENCRYPT)
  2915. {
  2916. /* Set to 0 the number of non-valid bytes using NPBLB register*/
  2917. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, npblb << 20U);
  2918. }
  2919. /* Number of valid words (lastwordsize) in last block */
  2920. if ((npblb % 4U) == 0U)
  2921. {
  2922. lastwordsize = (16U - npblb) / 4U;
  2923. }
  2924. else
  2925. {
  2926. lastwordsize = ((16U - npblb) / 4U) + 1U;
  2927. }
  2928. /* last block optionally pad the data with zeros*/
  2929. for (loopcounter = 0U; loopcounter < lastwordsize ; loopcounter++)
  2930. {
  2931. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2932. hcryp->CrypInCount++;
  2933. }
  2934. while (loopcounter < 4U)
  2935. {
  2936. /* pad the data with zeros to have a complete block */
  2937. hcryp->Instance->DINR = 0x0U;
  2938. loopcounter++;
  2939. }
  2940. }
  2941. }
  2942. /* Enter header data */
  2943. /* Cher first whether header length is small enough to enter the full header in one shot */
  2944. else if (headersize_in_bytes <= 16U)
  2945. {
  2946. /* Write header data, padded with zeros if need be */
  2947. for (loopcounter = 0U; (loopcounter < (headersize_in_bytes / 4U)); loopcounter++)
  2948. {
  2949. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2950. hcryp->CrypHeaderCount++ ;
  2951. }
  2952. /* If the header size is a multiple of words */
  2953. if ((headersize_in_bytes % 4U) == 0U)
  2954. {
  2955. /* Pad the data with zeros to have a complete block */
  2956. while (loopcounter < 4U)
  2957. {
  2958. hcryp->Instance->DINR = 0x0U;
  2959. loopcounter++;
  2960. hcryp->CrypHeaderCount++;
  2961. }
  2962. }
  2963. else
  2964. {
  2965. /* Enter last bytes, padded with zeros */
  2966. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2967. tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)];
  2968. hcryp->Instance->DINR = tmp;
  2969. loopcounter++;
  2970. hcryp->CrypHeaderCount++ ;
  2971. /* Pad the data with zeros to have a complete block */
  2972. while (loopcounter < 4U)
  2973. {
  2974. hcryp->Instance->DINR = 0x0U;
  2975. loopcounter++;
  2976. hcryp->CrypHeaderCount++;
  2977. }
  2978. }
  2979. /* Call Input transfer complete callback */
  2980. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  2981. /*Call registered Input complete callback*/
  2982. hcryp->InCpltCallback(hcryp);
  2983. #else
  2984. /*Call legacy weak Input complete callback*/
  2985. HAL_CRYP_InCpltCallback(hcryp);
  2986. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2987. }
  2988. else
  2989. {
  2990. /* Write the first input header block in the Input FIFO,
  2991. the following header data will be fed after interrupt occurrence */
  2992. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2993. hcryp->CrypHeaderCount++;
  2994. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2995. hcryp->CrypHeaderCount++;
  2996. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2997. hcryp->CrypHeaderCount++;
  2998. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  2999. hcryp->CrypHeaderCount++;
  3000. }
  3001. } /* end of if (DoKeyIVConfig == 1U) */
  3002. else /* Key and IV have already been configured,
  3003. header has already been processed;
  3004. only process here message payload */
  3005. {
  3006. /* Enable computation complete flag and error interrupts */
  3007. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  3008. /* Set to 0 the number of non-valid bytes using NPBLB register*/
  3009. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 0U);
  3010. /* Write the payload Input block in the IN FIFO */
  3011. if (hcryp->Size == 0U)
  3012. {
  3013. /* Disable interrupts */
  3014. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  3015. /* Change the CRYP state */
  3016. hcryp->State = HAL_CRYP_STATE_READY;
  3017. /* Process unlocked */
  3018. __HAL_UNLOCK(hcryp);
  3019. }
  3020. else if (hcryp->Size >= 16U)
  3021. {
  3022. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3023. hcryp->CrypInCount++;
  3024. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3025. hcryp->CrypInCount++;
  3026. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3027. hcryp->CrypInCount++;
  3028. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3029. hcryp->CrypInCount++;
  3030. if (hcryp->CrypInCount == (hcryp->Size / 4U))
  3031. {
  3032. /* Call Input transfer complete callback */
  3033. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  3034. /*Call registered Input complete callback*/
  3035. hcryp->InCpltCallback(hcryp);
  3036. #else
  3037. /*Call legacy weak Input complete callback*/
  3038. HAL_CRYP_InCpltCallback(hcryp);
  3039. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3040. }
  3041. }
  3042. else /* Size < 16Bytes : first block is the last block*/
  3043. {
  3044. /* Workaround not implemented for TinyAES2*/
  3045. /* Size should be %4 otherwise Tag will be incorrectly generated for GCM Encryption:
  3046. Workaround is implemented in polling mode, so if last block of
  3047. payload <128bit do not use CRYP_Encrypt_IT otherwise TAG is incorrectly generated for GCM Encryption. */
  3048. /* Compute the number of padding bytes in last block of payload */
  3049. npblb = 16U - ((uint32_t)hcryp->Size);
  3050. if ((hcryp->Instance->CR & AES_CR_MODE) == CRYP_OPERATINGMODE_ENCRYPT)
  3051. {
  3052. /* Set to 0 the number of non-valid bytes using NPBLB register*/
  3053. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, npblb << 20U);
  3054. }
  3055. /* Number of valid words (lastwordsize) in last block */
  3056. if ((npblb % 4U) == 0U)
  3057. {
  3058. lastwordsize = (16U - npblb) / 4U;
  3059. }
  3060. else
  3061. {
  3062. lastwordsize = ((16U - npblb) / 4U) + 1U;
  3063. }
  3064. /* last block optionally pad the data with zeros*/
  3065. for (loopcounter = 0U; loopcounter < lastwordsize ; loopcounter++)
  3066. {
  3067. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3068. hcryp->CrypInCount++;
  3069. }
  3070. while (loopcounter < 4U)
  3071. {
  3072. /* pad the data with zeros to have a complete block */
  3073. hcryp->Instance->DINR = 0x0U;
  3074. loopcounter++;
  3075. }
  3076. }
  3077. }
  3078. /* Return function status */
  3079. return HAL_OK;
  3080. }
  3081. /**
  3082. * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG using DMA
  3083. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3084. * the configuration information for CRYP module
  3085. * @retval HAL status
  3086. */
  3087. static HAL_StatusTypeDef CRYP_AESGCM_Process_DMA(CRYP_HandleTypeDef *hcryp)
  3088. {
  3089. uint32_t count;
  3090. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  3091. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  3092. {
  3093. if (hcryp->KeyIVConfig == 1U)
  3094. {
  3095. /* If the Key and IV configuration has to be done only once
  3096. and if it has already been done, skip it */
  3097. DoKeyIVConfig = 0U;
  3098. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  3099. }
  3100. else
  3101. {
  3102. /* If the Key and IV configuration has to be done only once
  3103. and if it has not been done already, do it and set KeyIVConfig
  3104. to keep track it won't have to be done again next time */
  3105. hcryp->KeyIVConfig = 1U;
  3106. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  3107. }
  3108. }
  3109. else
  3110. {
  3111. hcryp->SizesSum = hcryp->Size;
  3112. }
  3113. if (DoKeyIVConfig == 1U)
  3114. {
  3115. /* Reset CrypHeaderCount */
  3116. hcryp->CrypHeaderCount = 0U;
  3117. /*************************** Init phase ************************************/
  3118. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  3119. /* Set the key */
  3120. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3121. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3122. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  3123. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1U);
  3124. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2U);
  3125. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3U);
  3126. /* Enable the CRYP peripheral */
  3127. __HAL_CRYP_ENABLE(hcryp);
  3128. /* just wait for hash computation */
  3129. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  3130. do
  3131. {
  3132. count-- ;
  3133. if (count == 0U)
  3134. {
  3135. /* Disable the CRYP peripheral clock */
  3136. __HAL_CRYP_DISABLE(hcryp);
  3137. /* Change state */
  3138. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3139. hcryp->State = HAL_CRYP_STATE_READY;
  3140. /* Process unlocked */
  3141. __HAL_UNLOCK(hcryp);
  3142. return HAL_ERROR;
  3143. }
  3144. }
  3145. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  3146. /* Clear CCF flag */
  3147. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3148. /************************ Header phase *************************************/
  3149. if (CRYP_GCMCCM_SetHeaderPhase_DMA(hcryp) != HAL_OK)
  3150. {
  3151. return HAL_ERROR;
  3152. }
  3153. }
  3154. else
  3155. {
  3156. /* Initialization and header phases already done, only do payload phase */
  3157. if (CRYP_GCMCCM_SetPayloadPhase_DMA(hcryp) != HAL_OK)
  3158. {
  3159. return HAL_ERROR;
  3160. }
  3161. } /* if (DoKeyIVConfig == 1U) */
  3162. /* Return function status */
  3163. return HAL_OK;
  3164. }
  3165. /**
  3166. * @brief AES CCM encryption/decryption processing in polling mode
  3167. * for TinyAES peripheral, no encrypt/decrypt performed, only authentication preparation.
  3168. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3169. * the configuration information for CRYP module
  3170. * @param Timeout Timeout duration
  3171. * @retval HAL status
  3172. */
  3173. static HAL_StatusTypeDef CRYP_AESCCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  3174. {
  3175. uint32_t tickstart;
  3176. uint32_t wordsize = ((uint32_t)hcryp->Size / 4U) ;
  3177. uint32_t loopcounter;
  3178. uint32_t npblb;
  3179. uint32_t lastwordsize;
  3180. uint32_t temp[4] ; /* Temporary CrypOutBuff */
  3181. uint32_t incount; /* Temporary CrypInCount Value */
  3182. uint32_t outcount; /* Temporary CrypOutCount Value */
  3183. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  3184. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  3185. {
  3186. if (hcryp->KeyIVConfig == 1U)
  3187. {
  3188. /* If the Key and IV configuration has to be done only once
  3189. and if it has already been done, skip it */
  3190. DoKeyIVConfig = 0U;
  3191. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  3192. }
  3193. else
  3194. {
  3195. /* If the Key and IV configuration has to be done only once
  3196. and if it has not been done already, do it and set KeyIVConfig
  3197. to keep track it won't have to be done again next time */
  3198. hcryp->KeyIVConfig = 1U;
  3199. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  3200. }
  3201. }
  3202. else
  3203. {
  3204. hcryp->SizesSum = hcryp->Size;
  3205. }
  3206. if (DoKeyIVConfig == 1U)
  3207. {
  3208. /* Reset CrypHeaderCount */
  3209. hcryp->CrypHeaderCount = 0U;
  3210. /********************** Init phase ******************************************/
  3211. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  3212. /* Set the key */
  3213. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3214. /* Set the initialization vector (IV) with B0 */
  3215. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.B0);
  3216. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.B0 + 1U);
  3217. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.B0 + 2U);
  3218. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.B0 + 3U);
  3219. /* Enable the CRYP peripheral */
  3220. __HAL_CRYP_ENABLE(hcryp);
  3221. /* just wait for hash computation */
  3222. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3223. {
  3224. /* Change state */
  3225. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3226. hcryp->State = HAL_CRYP_STATE_READY;
  3227. /* Process unlocked & return error */
  3228. __HAL_UNLOCK(hcryp);
  3229. return HAL_ERROR;
  3230. }
  3231. /* Clear CCF flag */
  3232. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3233. /************************ Header phase *************************************/
  3234. /* Header block(B1) : associated data length expressed in bytes concatenated
  3235. with Associated Data (A)*/
  3236. if (CRYP_GCMCCM_SetHeaderPhase(hcryp, Timeout) != HAL_OK)
  3237. {
  3238. return HAL_ERROR;
  3239. }
  3240. /*************************Payload phase ************************************/
  3241. /* Set the phase */
  3242. hcryp->Phase = CRYP_PHASE_PROCESS;
  3243. /* Select payload phase once the header phase is performed */
  3244. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_PAYLOAD);
  3245. /* Set to 0 the number of non-valid bytes using NPBLB register*/
  3246. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 0U);
  3247. } /* if (DoKeyIVConfig == 1U) */
  3248. if ((hcryp->Size % 16U) != 0U)
  3249. {
  3250. /* recalculate wordsize */
  3251. wordsize = ((wordsize / 4U) * 4U) ;
  3252. }
  3253. /* Get tick */
  3254. tickstart = HAL_GetTick();
  3255. /* Write input data and get output data */
  3256. incount = hcryp->CrypInCount;
  3257. outcount = hcryp->CrypOutCount;
  3258. while ((incount < wordsize) && (outcount < wordsize))
  3259. {
  3260. /* Write plain data and get cipher data */
  3261. CRYP_AES_ProcessData(hcryp, Timeout);
  3262. /* Check for the Timeout */
  3263. if (Timeout != HAL_MAX_DELAY)
  3264. {
  3265. if (((HAL_GetTick() - tickstart) > Timeout) ||(Timeout == 0U))
  3266. {
  3267. /* Disable the CRYP peripheral clock */
  3268. __HAL_CRYP_DISABLE(hcryp);
  3269. /* Change state */
  3270. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3271. hcryp->State = HAL_CRYP_STATE_READY;
  3272. /* Process unlocked */
  3273. __HAL_UNLOCK(hcryp);
  3274. return HAL_ERROR;
  3275. }
  3276. }
  3277. incount = hcryp->CrypInCount;
  3278. outcount = hcryp->CrypOutCount;
  3279. }
  3280. if ((hcryp->Size % 16U) != 0U)
  3281. {
  3282. /* Compute the number of padding bytes in last block of payload */
  3283. npblb = ((((uint32_t)hcryp->Size / 16U) + 1U) * 16U) - ((uint32_t)hcryp->Size);
  3284. if ((hcryp->Instance->CR & AES_CR_MODE) == CRYP_OPERATINGMODE_DECRYPT)
  3285. {
  3286. /* Set Npblb in case of AES CCM payload decryption to get right tag */
  3287. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, npblb << 20);
  3288. }
  3289. /* Number of valid words (lastwordsize) in last block */
  3290. if ((npblb % 4U) == 0U)
  3291. {
  3292. lastwordsize = (16U - npblb) / 4U;
  3293. }
  3294. else
  3295. {
  3296. lastwordsize = ((16U - npblb) / 4U) + 1U;
  3297. }
  3298. /* Write the last input block in the IN FIFO */
  3299. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter ++)
  3300. {
  3301. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3302. hcryp->CrypInCount++;
  3303. }
  3304. /* Pad the data with zeros to have a complete block */
  3305. while (loopcounter < 4U)
  3306. {
  3307. hcryp->Instance->DINR = 0U;
  3308. loopcounter++;
  3309. }
  3310. /* just wait for hash computation */
  3311. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3312. {
  3313. /* Change state */
  3314. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3315. hcryp->State = HAL_CRYP_STATE_READY;
  3316. /* Process unlocked & return error */
  3317. __HAL_UNLOCK(hcryp);
  3318. return HAL_ERROR;
  3319. }
  3320. /* Clear CCF flag */
  3321. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3322. for (loopcounter = 0U; loopcounter < 4U; loopcounter++)
  3323. {
  3324. /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */
  3325. temp[loopcounter] = hcryp->Instance->DOUTR;
  3326. }
  3327. for (loopcounter = 0U; loopcounter<lastwordsize; loopcounter++)
  3328. {
  3329. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[loopcounter];
  3330. hcryp->CrypOutCount++;
  3331. }
  3332. }
  3333. /* Return function status */
  3334. return HAL_OK;
  3335. }
  3336. /**
  3337. * @brief AES CCM encryption/decryption process in interrupt mode
  3338. * for TinyAES peripheral, no encrypt/decrypt performed, only authentication preparation.
  3339. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3340. * the configuration information for CRYP module
  3341. * @retval HAL status
  3342. */
  3343. static HAL_StatusTypeDef CRYP_AESCCM_Process_IT(CRYP_HandleTypeDef *hcryp)
  3344. {
  3345. __IO uint32_t count = 0U;
  3346. uint32_t loopcounter;
  3347. uint32_t lastwordsize;
  3348. uint32_t npblb;
  3349. uint32_t mode;
  3350. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  3351. #if (USE_HAL_CRYP_SUSPEND_RESUME == 1U)
  3352. if ((hcryp->Phase == CRYP_PHASE_HEADER_SUSPENDED) || (hcryp->Phase == CRYP_PHASE_PAYLOAD_SUSPENDED))
  3353. {
  3354. CRYP_PhaseProcessingResume(hcryp);
  3355. return HAL_OK;
  3356. }
  3357. #endif /* USE_HAL_CRYP_SUSPEND_RESUME */
  3358. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  3359. {
  3360. if (hcryp->KeyIVConfig == 1U)
  3361. {
  3362. /* If the Key and IV configuration has to be done only once
  3363. and if it has already been done, skip it */
  3364. DoKeyIVConfig = 0U;
  3365. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  3366. }
  3367. else
  3368. {
  3369. /* If the Key and IV configuration has to be done only once
  3370. and if it has not been done already, do it and set KeyIVConfig
  3371. to keep track it won't have to be done again next time */
  3372. hcryp->KeyIVConfig = 1U;
  3373. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  3374. }
  3375. }
  3376. else
  3377. {
  3378. hcryp->SizesSum = hcryp->Size;
  3379. }
  3380. /* Configure Key, IV and process message (header and payload) */
  3381. if (DoKeyIVConfig == 1U)
  3382. {
  3383. /* Reset CrypHeaderCount */
  3384. hcryp->CrypHeaderCount = 0U;
  3385. /********************** Init phase ******************************************/
  3386. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  3387. /* Set the key */
  3388. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3389. /* Set the initialization vector (IV) with B0 */
  3390. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.B0);
  3391. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.B0 + 1U);
  3392. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.B0 + 2U);
  3393. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.B0 + 3U);
  3394. /* Enable the CRYP peripheral */
  3395. __HAL_CRYP_ENABLE(hcryp);
  3396. /* just wait for hash computation */
  3397. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  3398. do
  3399. {
  3400. count-- ;
  3401. if (count == 0U)
  3402. {
  3403. /* Disable the CRYP peripheral clock */
  3404. __HAL_CRYP_DISABLE(hcryp);
  3405. /* Change state */
  3406. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3407. hcryp->State = HAL_CRYP_STATE_READY;
  3408. /* Process unlocked */
  3409. __HAL_UNLOCK(hcryp);
  3410. return HAL_ERROR;
  3411. }
  3412. }
  3413. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  3414. /* Clear CCF flag */
  3415. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3416. /***************************** Header phase *********************************/
  3417. /* Select header phase */
  3418. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  3419. /* Enable computation complete flag and error interrupts */
  3420. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  3421. /* Enable the CRYP peripheral */
  3422. __HAL_CRYP_ENABLE(hcryp);
  3423. if (hcryp->Init.HeaderSize == 0U) /*header phase is skipped*/
  3424. {
  3425. /* Set the phase */
  3426. hcryp->Phase = CRYP_PHASE_PROCESS;
  3427. /* Select payload phase once the header phase is performed */
  3428. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  3429. /* Set to 0 the number of non-valid bytes using NPBLB register*/
  3430. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 0U);
  3431. if (hcryp->Init.Algorithm == CRYP_AES_CCM)
  3432. {
  3433. /* Increment CrypHeaderCount to pass in CRYP_GCMCCM_SetPayloadPhase_IT */
  3434. hcryp->CrypHeaderCount++;
  3435. }
  3436. /* Write the payload Input block in the IN FIFO */
  3437. if (hcryp->Size == 0U)
  3438. {
  3439. /* Disable interrupts */
  3440. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  3441. /* Change the CRYP state */
  3442. hcryp->State = HAL_CRYP_STATE_READY;
  3443. /* Process unlocked */
  3444. __HAL_UNLOCK(hcryp);
  3445. }
  3446. else if (hcryp->Size >= 16U)
  3447. {
  3448. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3449. hcryp->CrypInCount++;
  3450. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3451. hcryp->CrypInCount++;
  3452. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3453. hcryp->CrypInCount++;
  3454. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3455. hcryp->CrypInCount++;
  3456. if ((hcryp->CrypInCount == (hcryp->Size / 4U)) && ((hcryp->Size % 16U) == 0U))
  3457. {
  3458. /* Call Input transfer complete callback */
  3459. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  3460. /*Call registered Input complete callback*/
  3461. hcryp->InCpltCallback(hcryp);
  3462. #else
  3463. /*Call legacy weak Input complete callback*/
  3464. HAL_CRYP_InCpltCallback(hcryp);
  3465. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3466. }
  3467. }
  3468. else /* Size < 4 words : first block is the last block*/
  3469. {
  3470. /* Compute the number of padding bytes in last block of payload */
  3471. npblb = 16U - (uint32_t)hcryp->Size;
  3472. mode = hcryp->Instance->CR & AES_CR_MODE;
  3473. if (((mode == CRYP_OPERATINGMODE_ENCRYPT) && (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)) ||
  3474. ((mode == CRYP_OPERATINGMODE_DECRYPT) && (hcryp->Init.Algorithm == CRYP_AES_CCM)))
  3475. {
  3476. /* Specify the number of non-valid bytes using NPBLB register*/
  3477. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, npblb << 20U);
  3478. }
  3479. /* Number of valid words (lastwordsize) in last block */
  3480. if ((npblb % 4U) == 0U)
  3481. {
  3482. lastwordsize = (16U - npblb) / 4U;
  3483. }
  3484. else
  3485. {
  3486. lastwordsize = ((16U - npblb) / 4U) + 1U;
  3487. }
  3488. /* Last block optionally pad the data with zeros*/
  3489. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++)
  3490. {
  3491. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3492. hcryp->CrypInCount++;
  3493. }
  3494. while (loopcounter < 4U)
  3495. {
  3496. /* Pad the data with zeros to have a complete block */
  3497. hcryp->Instance->DINR = 0x0U;
  3498. loopcounter++;
  3499. }
  3500. }
  3501. }
  3502. else if ((hcryp->Init.HeaderSize) < 4U) /*HeaderSize < 4 */
  3503. {
  3504. /* Last block optionally pad the data with zeros*/
  3505. for (loopcounter = 0U; loopcounter < (hcryp->Init.HeaderSize % 4U); loopcounter++)
  3506. {
  3507. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3508. hcryp->CrypHeaderCount++ ;
  3509. }
  3510. while (loopcounter < 4U)
  3511. {
  3512. /* pad the data with zeros to have a complete block */
  3513. hcryp->Instance->DINR = 0x0U;
  3514. loopcounter++;
  3515. }
  3516. }
  3517. else
  3518. {
  3519. /* Write the input block in the IN FIFO */
  3520. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3521. hcryp->CrypHeaderCount++;
  3522. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3523. hcryp->CrypHeaderCount++;
  3524. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3525. hcryp->CrypHeaderCount++;
  3526. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3527. hcryp->CrypHeaderCount++;
  3528. }
  3529. } /* end of if (DoKeyIVConfig == 1U) */
  3530. else /* Key and IV have already been configured,
  3531. header has already been processed;
  3532. only process here message payload */
  3533. {
  3534. /* Write the payload Input block in the IN FIFO */
  3535. if (hcryp->Size == 0U)
  3536. {
  3537. /* Disable interrupts */
  3538. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  3539. /* Change the CRYP state */
  3540. hcryp->State = HAL_CRYP_STATE_READY;
  3541. /* Process unlocked */
  3542. __HAL_UNLOCK(hcryp);
  3543. }
  3544. else if (hcryp->Size >= 16U)
  3545. {
  3546. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3547. hcryp->CrypInCount++;
  3548. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3549. hcryp->CrypInCount++;
  3550. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3551. hcryp->CrypInCount++;
  3552. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3553. hcryp->CrypInCount++;
  3554. if ((hcryp->CrypInCount == (hcryp->Size / 4U)) && ((hcryp->Size % 16U) == 0U))
  3555. {
  3556. /* Call Input transfer complete callback */
  3557. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  3558. /*Call registered Input complete callback*/
  3559. hcryp->InCpltCallback(hcryp);
  3560. #else
  3561. /*Call legacy weak Input complete callback*/
  3562. HAL_CRYP_InCpltCallback(hcryp);
  3563. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3564. }
  3565. }
  3566. else /* Size < 4 words : first block is the last block*/
  3567. {
  3568. /* Compute the number of padding bytes in last block of payload */
  3569. npblb = 16U - (uint32_t)hcryp->Size;
  3570. mode = hcryp->Instance->CR & AES_CR_MODE;
  3571. if (((mode == CRYP_OPERATINGMODE_ENCRYPT) && (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)) ||
  3572. ((mode == CRYP_OPERATINGMODE_DECRYPT) && (hcryp->Init.Algorithm == CRYP_AES_CCM)))
  3573. {
  3574. /* Specify the number of non-valid bytes using NPBLB register*/
  3575. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, npblb << 20U);
  3576. }
  3577. /* Number of valid words (lastwordsize) in last block */
  3578. if ((npblb % 4U) == 0U)
  3579. {
  3580. lastwordsize = (16U - npblb) / 4U;
  3581. }
  3582. else
  3583. {
  3584. lastwordsize = ((16U - npblb) / 4U) + 1U;
  3585. }
  3586. /* Last block optionally pad the data with zeros*/
  3587. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++)
  3588. {
  3589. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3590. hcryp->CrypInCount++;
  3591. }
  3592. while (loopcounter < 4U)
  3593. {
  3594. /* Pad the data with zeros to have a complete block */
  3595. hcryp->Instance->DINR = 0x0U;
  3596. loopcounter++;
  3597. }
  3598. }
  3599. }
  3600. /* Return function status */
  3601. return HAL_OK;
  3602. }
  3603. /**
  3604. * @brief AES CCM encryption/decryption process in DMA mode
  3605. * for TinyAES peripheral, no encrypt/decrypt performed, only authentication preparation.
  3606. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3607. * the configuration information for CRYP module
  3608. * @retval HAL status
  3609. */
  3610. static HAL_StatusTypeDef CRYP_AESCCM_Process_DMA(CRYP_HandleTypeDef *hcryp)
  3611. {
  3612. uint32_t count;
  3613. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  3614. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  3615. {
  3616. if (hcryp->KeyIVConfig == 1U)
  3617. {
  3618. /* If the Key and IV configuration has to be done only once
  3619. and if it has already been done, skip it */
  3620. DoKeyIVConfig = 0U;
  3621. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  3622. }
  3623. else
  3624. {
  3625. /* If the Key and IV configuration has to be done only once
  3626. and if it has not been done already, do it and set KeyIVConfig
  3627. to keep track it won't have to be done again next time */
  3628. hcryp->KeyIVConfig = 1U;
  3629. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  3630. }
  3631. }
  3632. else
  3633. {
  3634. hcryp->SizesSum = hcryp->Size;
  3635. }
  3636. if (DoKeyIVConfig == 1U)
  3637. {
  3638. /* Reset CrypHeaderCount */
  3639. hcryp->CrypHeaderCount = 0U;
  3640. /********************** Init phase ******************************************/
  3641. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  3642. /* Set the key */
  3643. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3644. /* Set the initialization vector (IV) with B0 */
  3645. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.B0);
  3646. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.B0 + 1U);
  3647. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.B0 + 2U);
  3648. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.B0 + 3U);
  3649. /* Enable the CRYP peripheral */
  3650. __HAL_CRYP_ENABLE(hcryp);
  3651. /* just wait for hash computation */
  3652. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  3653. do
  3654. {
  3655. count-- ;
  3656. if (count == 0U)
  3657. {
  3658. /* Disable the CRYP peripheral clock */
  3659. __HAL_CRYP_DISABLE(hcryp);
  3660. /* Change state */
  3661. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3662. hcryp->State = HAL_CRYP_STATE_READY;
  3663. /* Process unlocked */
  3664. __HAL_UNLOCK(hcryp);
  3665. return HAL_ERROR;
  3666. }
  3667. }
  3668. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  3669. /* Clear CCF flag */
  3670. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3671. /********************* Header phase *****************************************/
  3672. if (CRYP_GCMCCM_SetHeaderPhase_DMA(hcryp) != HAL_OK)
  3673. {
  3674. return HAL_ERROR;
  3675. }
  3676. }
  3677. else
  3678. {
  3679. /* Initialization and header phases already done, only do payload phase */
  3680. if (CRYP_GCMCCM_SetPayloadPhase_DMA(hcryp) != HAL_OK)
  3681. {
  3682. return HAL_ERROR;
  3683. }
  3684. } /* if (DoKeyIVConfig == 1U) */
  3685. /* Return function status */
  3686. return HAL_OK;
  3687. }
  3688. /**
  3689. * @brief Sets the payload phase in interrupt mode
  3690. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3691. * the configuration information for CRYP module
  3692. * @retval state
  3693. */
  3694. static void CRYP_GCMCCM_SetPayloadPhase_IT(CRYP_HandleTypeDef *hcryp)
  3695. {
  3696. uint32_t loopcounter;
  3697. uint32_t temp[4]; /* Temporary CrypOutBuff */
  3698. uint32_t lastwordsize;
  3699. uint32_t npblb;
  3700. uint32_t mode;
  3701. uint16_t incount; /* Temporary CrypInCount Value */
  3702. uint16_t outcount; /* Temporary CrypOutCount Value */
  3703. uint32_t i;
  3704. /***************************** Payload phase *******************************/
  3705. /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer*/
  3706. for (i = 0U; i < 4U; i++)
  3707. {
  3708. temp[i] = hcryp->Instance->DOUTR;
  3709. }
  3710. i= 0U;
  3711. while((hcryp->CrypOutCount < ((hcryp->Size + 3U)/4U)) && (i<4U))
  3712. {
  3713. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  3714. hcryp->CrypOutCount++;
  3715. i++;
  3716. }
  3717. incount = hcryp->CrypInCount;
  3718. outcount = hcryp->CrypOutCount;
  3719. if ((outcount >= (hcryp->Size / 4U)) && ((incount * 4U) >= hcryp->Size))
  3720. {
  3721. /* When in CCM with Key and IV configuration skipped, don't disable interruptions */
  3722. if (!((hcryp->Init.Algorithm == CRYP_AES_CCM) && (hcryp->KeyIVConfig == 1U)))
  3723. {
  3724. /* Disable computation complete flag and errors interrupts */
  3725. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  3726. }
  3727. /* Change the CRYP state */
  3728. hcryp->State = HAL_CRYP_STATE_READY;
  3729. /* Process unlocked */
  3730. __HAL_UNLOCK(hcryp);
  3731. /* Call output transfer complete callback */
  3732. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  3733. /*Call registered Output complete callback*/
  3734. hcryp->OutCpltCallback(hcryp);
  3735. #else
  3736. /*Call legacy weak Output complete callback*/
  3737. HAL_CRYP_OutCpltCallback(hcryp);
  3738. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3739. }
  3740. else if (((hcryp->Size / 4U) - (hcryp->CrypInCount)) >= 4U)
  3741. {
  3742. #if (USE_HAL_CRYP_SUSPEND_RESUME == 1U)
  3743. /* If suspension flag has been raised, suspend processing
  3744. only if not already at the end of the payload */
  3745. if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  3746. {
  3747. /* Clear CCF Flag */
  3748. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3749. /* reset SuspendRequest */
  3750. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  3751. /* Disable Computation Complete Flag and Errors Interrupts */
  3752. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  3753. /* Change the CRYP state */
  3754. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  3755. /* Mark that the payload phase is suspended */
  3756. hcryp->Phase = CRYP_PHASE_PAYLOAD_SUSPENDED;
  3757. /* Process Unlocked */
  3758. __HAL_UNLOCK(hcryp);
  3759. }
  3760. else
  3761. #endif /* USE_HAL_CRYP_SUSPEND_RESUME */
  3762. {
  3763. /* Write the input block in the IN FIFO */
  3764. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3765. hcryp->CrypInCount++;
  3766. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3767. hcryp->CrypInCount++;
  3768. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3769. hcryp->CrypInCount++;
  3770. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3771. hcryp->CrypInCount++;
  3772. if ((hcryp->CrypInCount == hcryp->Size) && (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC))
  3773. {
  3774. /* Call output transfer complete callback */
  3775. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  3776. /*Call registered Input complete callback*/
  3777. hcryp->InCpltCallback(hcryp);
  3778. #else
  3779. /*Call legacy weak Input complete callback*/
  3780. HAL_CRYP_InCpltCallback(hcryp);
  3781. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3782. }
  3783. }
  3784. }
  3785. else /* Last block of payload < 128bit*/
  3786. {
  3787. /* Compute the number of padding bytes in last block of payload */
  3788. npblb = ((((uint32_t)hcryp->Size / 16U) + 1U) * 16U) - ((uint32_t)hcryp->Size);
  3789. mode = hcryp->Instance->CR & AES_CR_MODE;
  3790. if (((mode == CRYP_OPERATINGMODE_ENCRYPT) && (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)) ||
  3791. ((mode == CRYP_OPERATINGMODE_DECRYPT) && (hcryp->Init.Algorithm == CRYP_AES_CCM)))
  3792. {
  3793. /* Specify the number of non-valid bytes using NPBLB register*/
  3794. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, npblb << 20U);
  3795. }
  3796. /* Number of valid words (lastwordsize) in last block */
  3797. if ((npblb % 4U) == 0U)
  3798. {
  3799. lastwordsize = (16U - npblb) / 4U;
  3800. }
  3801. else
  3802. {
  3803. lastwordsize = ((16U - npblb) / 4U) + 1U;
  3804. }
  3805. /* Last block optionally pad the data with zeros*/
  3806. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++)
  3807. {
  3808. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3809. hcryp->CrypInCount++;
  3810. }
  3811. while (loopcounter < 4U)
  3812. {
  3813. /* pad the data with zeros to have a complete block */
  3814. hcryp->Instance->DINR = 0x0U;
  3815. loopcounter++;
  3816. }
  3817. }
  3818. }
  3819. /**
  3820. * @brief Sets the payload phase in DMA mode
  3821. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3822. * the configuration information for CRYP module
  3823. * @retval state
  3824. */
  3825. static HAL_StatusTypeDef CRYP_GCMCCM_SetPayloadPhase_DMA(CRYP_HandleTypeDef *hcryp)
  3826. {
  3827. uint16_t wordsize = hcryp->Size / 4U ;
  3828. uint32_t index;
  3829. uint32_t npblb;
  3830. uint32_t lastwordsize;
  3831. uint32_t temp[4]; /* Temporary CrypOutBuff */
  3832. uint32_t count;
  3833. uint32_t reg;
  3834. /************************ Payload phase ************************************/
  3835. if (hcryp->Size == 0U)
  3836. {
  3837. /* Process unLocked */
  3838. __HAL_UNLOCK(hcryp);
  3839. /* Change the CRYP state and phase */
  3840. hcryp->State = HAL_CRYP_STATE_READY;
  3841. }
  3842. else if (hcryp->Size >= 16U)
  3843. {
  3844. /*DMA transfer must not include the last block in case of Size is not %16 */
  3845. wordsize = wordsize - (wordsize % 4U);
  3846. /*DMA transfer */
  3847. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), wordsize, (uint32_t)(hcryp->pCrypOutBuffPtr));
  3848. }
  3849. else /* length of input data is < 16 */
  3850. {
  3851. /* Compute the number of padding bytes in last block of payload */
  3852. npblb = 16U - (uint32_t)hcryp->Size;
  3853. /* Set Npblb in case of AES GCM payload encryption or AES CCM payload decryption to get right tag*/
  3854. reg = hcryp->Instance->CR & (AES_CR_CHMOD|AES_CR_MODE);
  3855. if ((reg == (CRYP_AES_GCM_GMAC|CRYP_OPERATINGMODE_ENCRYPT)) ||\
  3856. (reg == (CRYP_AES_CCM|CRYP_OPERATINGMODE_DECRYPT)))
  3857. {
  3858. /* Specify the number of non-valid bytes using NPBLB register*/
  3859. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, npblb << 20U);
  3860. }
  3861. /* Number of valid words (lastwordsize) in last block */
  3862. if ((npblb % 4U) == 0U)
  3863. {
  3864. lastwordsize = (16U - npblb) / 4U;
  3865. }
  3866. else
  3867. {
  3868. lastwordsize = ((16U - npblb) / 4U) + 1U;
  3869. }
  3870. /* last block optionally pad the data with zeros*/
  3871. for (index = 0U; index < lastwordsize; index ++)
  3872. {
  3873. /* Write the last Input block in the IN FIFO */
  3874. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3875. hcryp->CrypInCount++;
  3876. }
  3877. while (index < 4U)
  3878. {
  3879. /* pad the data with zeros to have a complete block */
  3880. hcryp->Instance->DINR = 0U;
  3881. index++;
  3882. }
  3883. /* Wait for CCF flag to be raised */
  3884. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  3885. do
  3886. {
  3887. count-- ;
  3888. if (count == 0U)
  3889. {
  3890. /* Disable the CRYP peripheral clock */
  3891. __HAL_CRYP_DISABLE(hcryp);
  3892. /* Change state */
  3893. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3894. hcryp->State = HAL_CRYP_STATE_READY;
  3895. /* Process unlocked */
  3896. __HAL_UNLOCK(hcryp);
  3897. return HAL_ERROR;
  3898. }
  3899. }
  3900. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  3901. /* Clear CCF Flag */
  3902. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3903. /*Read the output block from the output FIFO */
  3904. for (index = 0U; index < 4U; index++)
  3905. {
  3906. /* Read the output block from the output FIFO and put them in temporary buffer then get CrypOutBuff from temporary buffer */
  3907. temp[index] = hcryp->Instance->DOUTR;
  3908. }
  3909. for (index = 0U; index < lastwordsize; index++)
  3910. {
  3911. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[index];
  3912. hcryp->CrypOutCount++;
  3913. }
  3914. /* Change the CRYP state to ready */
  3915. hcryp->State = HAL_CRYP_STATE_READY;
  3916. /* Process unlocked */
  3917. __HAL_UNLOCK(hcryp);
  3918. }
  3919. return HAL_OK;
  3920. }
  3921. /**
  3922. * @brief Sets the header phase in polling mode
  3923. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  3924. * the configuration information for CRYP module(Header & HeaderSize)
  3925. * @param Timeout Timeout value
  3926. * @retval state
  3927. */
  3928. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  3929. {
  3930. uint32_t loopcounter;
  3931. uint32_t size_in_bytes;
  3932. uint32_t tmp;
  3933. uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */
  3934. 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */
  3935. 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU}; /* 8-bit data type */
  3936. /***************************** Header phase for GCM/GMAC or CCM *********************************/
  3937. if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD)
  3938. {
  3939. size_in_bytes = hcryp->Init.HeaderSize * 4U;
  3940. }
  3941. else
  3942. {
  3943. size_in_bytes = hcryp->Init.HeaderSize;
  3944. }
  3945. if ((size_in_bytes != 0U))
  3946. {
  3947. /* Select header phase */
  3948. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  3949. /* Enable the CRYP peripheral */
  3950. __HAL_CRYP_ENABLE(hcryp);
  3951. /* If size_in_bytes is a multiple of blocks (a multiple of four 32-bits words ) */
  3952. if ((size_in_bytes % 16U) == 0U)
  3953. {
  3954. /* No padding */
  3955. for (loopcounter = 0U; (loopcounter < (size_in_bytes / 4U)); loopcounter += 4U)
  3956. {
  3957. /* Write the input block in the data input register */
  3958. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3959. hcryp->CrypHeaderCount++ ;
  3960. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3961. hcryp->CrypHeaderCount++ ;
  3962. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3963. hcryp->CrypHeaderCount++ ;
  3964. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3965. hcryp->CrypHeaderCount++ ;
  3966. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3967. {
  3968. /* Disable the CRYP peripheral clock */
  3969. __HAL_CRYP_DISABLE(hcryp);
  3970. /* Change state */
  3971. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3972. hcryp->State = HAL_CRYP_STATE_READY;
  3973. /* Process unlocked */
  3974. __HAL_UNLOCK(hcryp);
  3975. return HAL_ERROR;
  3976. }
  3977. /* Clear CCF flag */
  3978. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3979. }
  3980. }
  3981. else
  3982. {
  3983. /* Write header block in the IN FIFO without last block */
  3984. for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 16U) * 4U)); loopcounter += 4U)
  3985. {
  3986. /* Write the input block in the data input register */
  3987. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3988. hcryp->CrypHeaderCount++ ;
  3989. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3990. hcryp->CrypHeaderCount++ ;
  3991. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3992. hcryp->CrypHeaderCount++ ;
  3993. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3994. hcryp->CrypHeaderCount++ ;
  3995. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3996. {
  3997. /* Disable the CRYP peripheral clock */
  3998. __HAL_CRYP_DISABLE(hcryp);
  3999. /* Change state */
  4000. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4001. hcryp->State = HAL_CRYP_STATE_READY;
  4002. /* Process unlocked */
  4003. __HAL_UNLOCK(hcryp);
  4004. return HAL_ERROR;
  4005. }
  4006. /* Clear CCF flag */
  4007. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4008. }
  4009. /* Write last complete words */
  4010. for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 4U) % 4U)); loopcounter++)
  4011. {
  4012. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4013. hcryp->CrypHeaderCount++ ;
  4014. }
  4015. /* If the header size is a multiple of words */
  4016. if ((size_in_bytes % 4U) == 0U)
  4017. {
  4018. /* Pad the data with zeros to have a complete block */
  4019. while (loopcounter < 4U)
  4020. {
  4021. hcryp->Instance->DINR = 0x0U;
  4022. loopcounter++;
  4023. }
  4024. }
  4025. else
  4026. {
  4027. /* Enter last bytes, padded with zeros */
  4028. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4029. tmp &= mask[(hcryp->Init.DataType * 2U) + (size_in_bytes % 4U)];
  4030. hcryp->Instance->DINR = tmp;
  4031. loopcounter++;
  4032. /* Pad the data with zeros to have a complete block */
  4033. while (loopcounter < 4U)
  4034. {
  4035. hcryp->Instance->DINR = 0x0U;
  4036. loopcounter++;
  4037. }
  4038. }
  4039. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  4040. {
  4041. /* Disable the CRYP peripheral clock */
  4042. __HAL_CRYP_DISABLE(hcryp);
  4043. /* Change state */
  4044. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4045. hcryp->State = HAL_CRYP_STATE_READY;
  4046. /* Process unlocked */
  4047. __HAL_UNLOCK(hcryp);
  4048. return HAL_ERROR;
  4049. }
  4050. /* Clear CCF flag */
  4051. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4052. }
  4053. }
  4054. else
  4055. {
  4056. /*Workaround 1: only AES, before re-enabling the peripheral, datatype can be configured.*/
  4057. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
  4058. /* Select header phase */
  4059. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4060. /* Enable the CRYP peripheral */
  4061. __HAL_CRYP_ENABLE(hcryp);
  4062. }
  4063. /* Return function status */
  4064. return HAL_OK;
  4065. }
  4066. /**
  4067. * @brief Sets the header phase when using DMA in process
  4068. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4069. * the configuration information for CRYP module(Header & HeaderSize)
  4070. * @retval None
  4071. */
  4072. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase_DMA(CRYP_HandleTypeDef *hcryp)
  4073. {
  4074. uint32_t loopcounter;
  4075. uint32_t headersize_in_bytes;
  4076. uint32_t tmp;
  4077. uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */
  4078. 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */
  4079. 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU}; /* 8-bit data type */
  4080. /***************************** Header phase for GCM/GMAC or CCM *********************************/
  4081. if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD)
  4082. {
  4083. headersize_in_bytes = hcryp->Init.HeaderSize * 4U;
  4084. }
  4085. else
  4086. {
  4087. headersize_in_bytes = hcryp->Init.HeaderSize;
  4088. }
  4089. /* Select header phase */
  4090. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4091. /* Enable the CRYP peripheral */
  4092. __HAL_CRYP_ENABLE(hcryp);
  4093. /* Set the phase */
  4094. hcryp->Phase = CRYP_PHASE_PROCESS;
  4095. /* If header size is at least equal to 16 bytes, feed the header through DMA.
  4096. If size_in_bytes is not a multiple of blocks (is not a multiple of four 32-bit words ),
  4097. last bytes feeding and padding will be done in CRYP_DMAInCplt() */
  4098. if (headersize_in_bytes >= 16U)
  4099. {
  4100. /* Initiate header DMA transfer */
  4101. if (CRYP_SetHeaderDMAConfig(hcryp, (uint32_t)(hcryp->Init.Header), (uint16_t)((headersize_in_bytes / 16U) * 4U)) != HAL_OK)
  4102. {
  4103. return HAL_ERROR;
  4104. }
  4105. }
  4106. else
  4107. {
  4108. if (headersize_in_bytes != 0U)
  4109. {
  4110. /* Header length is larger than 0 and strictly less than 16 bytes */
  4111. /* Write last complete words */
  4112. for (loopcounter = 0U; (loopcounter < (headersize_in_bytes / 4U)); loopcounter++)
  4113. {
  4114. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4115. hcryp->CrypHeaderCount++ ;
  4116. }
  4117. /* If the header size is a multiple of words */
  4118. if ((headersize_in_bytes % 4U) == 0U)
  4119. {
  4120. /* Pad the data with zeros to have a complete block */
  4121. while (loopcounter < 4U)
  4122. {
  4123. hcryp->Instance->DINR = 0x0U;
  4124. loopcounter++;
  4125. }
  4126. }
  4127. else
  4128. {
  4129. /* Enter last bytes, padded with zeros */
  4130. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4131. tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)];
  4132. hcryp->Instance->DINR = tmp;
  4133. loopcounter++;
  4134. /* Pad the data with zeros to have a complete block */
  4135. while (loopcounter < 4U)
  4136. {
  4137. hcryp->Instance->DINR = 0x0U;
  4138. loopcounter++;
  4139. }
  4140. }
  4141. if (CRYP_WaitOnCCFlag(hcryp, CRYP_TIMEOUT_GCMCCMHEADERPHASE) != HAL_OK)
  4142. {
  4143. /* Disable the CRYP peripheral clock */
  4144. __HAL_CRYP_DISABLE(hcryp);
  4145. /* Change state */
  4146. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4147. hcryp->State = HAL_CRYP_STATE_READY;
  4148. /* Process unlocked */
  4149. __HAL_UNLOCK(hcryp);
  4150. return HAL_ERROR;
  4151. }
  4152. /* Clear CCF flag */
  4153. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4154. } /* if (headersize_in_bytes != 0U) */
  4155. /* Move to payload phase if header length is null or
  4156. if the header length was less than 16 and header written by software instead of DMA */
  4157. /* Set to 0 the number of non-valid bytes using NPBLB register*/
  4158. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 0U);
  4159. /* Select payload phase once the header phase is performed */
  4160. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  4161. /* Initiate payload DMA IN and processed data DMA OUT transfers */
  4162. if (CRYP_GCMCCM_SetPayloadPhase_DMA(hcryp) != HAL_OK)
  4163. {
  4164. return HAL_ERROR;
  4165. }
  4166. } /* if (headersize_in_bytes >= 16U) */
  4167. /* Return function status */
  4168. return HAL_OK;
  4169. }
  4170. /**
  4171. * @brief Sets the header phase in interrupt mode
  4172. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4173. * the configuration information for CRYP module(Header & HeaderSize)
  4174. * @retval None
  4175. */
  4176. static void CRYP_GCMCCM_SetHeaderPhase_IT(CRYP_HandleTypeDef *hcryp)
  4177. {
  4178. uint32_t loopcounter;
  4179. uint32_t lastwordsize;
  4180. uint32_t npblb;
  4181. uint32_t mode;
  4182. uint32_t headersize_in_bytes;
  4183. uint32_t tmp;
  4184. uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */
  4185. 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */
  4186. 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU}; /* 8-bit data type */
  4187. if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD)
  4188. {
  4189. headersize_in_bytes = hcryp->Init.HeaderSize * 4U;
  4190. }
  4191. else
  4192. {
  4193. headersize_in_bytes = hcryp->Init.HeaderSize;
  4194. }
  4195. /***************************** Header phase *********************************/
  4196. /* Test whether or not the header phase is over.
  4197. If the test below is true, move to payload phase */
  4198. if (headersize_in_bytes <= ((uint32_t)(hcryp->CrypHeaderCount) * 4U))
  4199. {
  4200. /* Set the phase */
  4201. hcryp->Phase = CRYP_PHASE_PROCESS;
  4202. /* Select payload phase */
  4203. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_PAYLOAD);
  4204. /* Set to 0 the number of non-valid bytes using NPBLB register*/
  4205. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 0U);
  4206. if (hcryp->Init.Algorithm == CRYP_AES_CCM)
  4207. {
  4208. /* Increment CrypHeaderCount to pass in CRYP_GCMCCM_SetPayloadPhase_IT */
  4209. hcryp->CrypHeaderCount++;
  4210. }
  4211. /* Write the payload Input block in the IN FIFO */
  4212. if (hcryp->Size == 0U)
  4213. {
  4214. /* Disable interrupts */
  4215. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  4216. /* Change the CRYP state */
  4217. hcryp->State = HAL_CRYP_STATE_READY;
  4218. /* Process unlocked */
  4219. __HAL_UNLOCK(hcryp);
  4220. }
  4221. else if (hcryp->Size >= 16U)
  4222. {
  4223. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4224. hcryp->CrypInCount++;
  4225. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4226. hcryp->CrypInCount++;
  4227. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4228. hcryp->CrypInCount++;
  4229. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4230. hcryp->CrypInCount++;
  4231. if ((hcryp->CrypInCount == (hcryp->Size / 4U)) && ((hcryp->Size % 16U) == 0U))
  4232. {
  4233. /* Call the input data transfer complete callback */
  4234. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  4235. /*Call registered Input complete callback*/
  4236. hcryp->InCpltCallback(hcryp);
  4237. #else
  4238. /*Call legacy weak Input complete callback*/
  4239. HAL_CRYP_InCpltCallback(hcryp);
  4240. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4241. }
  4242. }
  4243. else /* Size < 4 words : first block is the last block*/
  4244. {
  4245. /* Compute the number of padding bytes in last block of payload */
  4246. npblb = 16U - ((uint32_t)hcryp->Size);
  4247. mode = hcryp->Instance->CR & AES_CR_MODE;
  4248. if (((mode == CRYP_OPERATINGMODE_ENCRYPT) && (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)) ||
  4249. ((mode == CRYP_OPERATINGMODE_DECRYPT) && (hcryp->Init.Algorithm == CRYP_AES_CCM)))
  4250. {
  4251. /* Specify the number of non-valid bytes using NPBLB register*/
  4252. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, npblb << 20U);
  4253. }
  4254. /* Number of valid words (lastwordsize) in last block */
  4255. if ((npblb % 4U) == 0U)
  4256. {
  4257. lastwordsize = (16U - npblb) / 4U;
  4258. }
  4259. else
  4260. {
  4261. lastwordsize = ((16U - npblb) / 4U) + 1U;
  4262. }
  4263. /* Last block optionally pad the data with zeros*/
  4264. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++)
  4265. {
  4266. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4267. hcryp->CrypInCount++;
  4268. }
  4269. while (loopcounter < 4U)
  4270. {
  4271. /* Pad the data with zeros to have a complete block */
  4272. hcryp->Instance->DINR = 0x0U;
  4273. loopcounter++;
  4274. }
  4275. }
  4276. }
  4277. else if ((((headersize_in_bytes / 4U) - (hcryp->CrypHeaderCount)) >= 4U))
  4278. {
  4279. /* Can enter full 4 header words */
  4280. #if (USE_HAL_CRYP_SUSPEND_RESUME == 1U)
  4281. /* If suspension flag has been raised, suspend processing
  4282. only if not already at the end of the header */
  4283. if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  4284. {
  4285. /* Clear CCF Flag */
  4286. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4287. /* reset SuspendRequest */
  4288. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  4289. /* Disable Computation Complete Flag and Errors Interrupts */
  4290. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4291. /* Change the CRYP state */
  4292. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  4293. /* Mark that the payload phase is suspended */
  4294. hcryp->Phase = CRYP_PHASE_HEADER_SUSPENDED;
  4295. /* Process Unlocked */
  4296. __HAL_UNLOCK(hcryp);
  4297. }
  4298. else
  4299. #endif /* USE_HAL_CRYP_SUSPEND_RESUME */
  4300. {
  4301. /* Write the input block in the IN FIFO */
  4302. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4303. hcryp->CrypHeaderCount++;
  4304. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4305. hcryp->CrypHeaderCount++;
  4306. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4307. hcryp->CrypHeaderCount++;
  4308. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4309. hcryp->CrypHeaderCount++;
  4310. }
  4311. }
  4312. else /* Write last header block (4 words), padded with zeros if needed */
  4313. {
  4314. for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 4U) % 4U)); loopcounter++)
  4315. {
  4316. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4317. hcryp->CrypHeaderCount++ ;
  4318. }
  4319. /* If the header size is a multiple of words */
  4320. if ((headersize_in_bytes % 4U) == 0U)
  4321. {
  4322. /* Pad the data with zeros to have a complete block */
  4323. while (loopcounter < 4U)
  4324. {
  4325. hcryp->Instance->DINR = 0x0U;
  4326. loopcounter++;
  4327. hcryp->CrypHeaderCount++;
  4328. }
  4329. }
  4330. else
  4331. {
  4332. /* Enter last bytes, padded with zeros */
  4333. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4334. tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)];
  4335. hcryp->Instance->DINR = tmp;
  4336. loopcounter++;
  4337. hcryp->CrypHeaderCount++;
  4338. /* Pad the data with zeros to have a complete block */
  4339. while (loopcounter < 4U)
  4340. {
  4341. hcryp->Instance->DINR = 0x0U;
  4342. loopcounter++;
  4343. hcryp->CrypHeaderCount++;
  4344. }
  4345. }
  4346. }
  4347. }
  4348. /**
  4349. * @brief Handle CRYP hardware block Timeout when waiting for CCF flag to be raised.
  4350. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4351. * the configuration information for CRYP module.
  4352. * @param Timeout Timeout duration.
  4353. * @note This function can only be used in thread mode.
  4354. * @retval HAL status
  4355. */
  4356. static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  4357. {
  4358. uint32_t tickstart;
  4359. /* Get timeout */
  4360. tickstart = HAL_GetTick();
  4361. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
  4362. {
  4363. /* Check for the Timeout */
  4364. if (Timeout != HAL_MAX_DELAY)
  4365. {
  4366. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  4367. {
  4368. return HAL_ERROR;
  4369. }
  4370. }
  4371. }
  4372. return HAL_OK;
  4373. }
  4374. /**
  4375. * @brief Wait for Computation Complete Flag (CCF) to raise then clear it.
  4376. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4377. * the configuration information for CRYP module.
  4378. * @param Timeout Timeout duration.
  4379. * @note This function can be used in thread or handler mode.
  4380. * @retval HAL status
  4381. */
  4382. static void CRYP_ClearCCFlagWhenHigh(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  4383. {
  4384. uint32_t count = Timeout;
  4385. do
  4386. {
  4387. count-- ;
  4388. if (count == 0U)
  4389. {
  4390. /* Disable the CRYP peripheral clock */
  4391. __HAL_CRYP_DISABLE(hcryp);
  4392. /* Change state */
  4393. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4394. /* Process unlocked */
  4395. __HAL_UNLOCK(hcryp);
  4396. hcryp->State = HAL_CRYP_STATE_READY;
  4397. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  4398. /*Call registered error callback*/
  4399. hcryp->ErrorCallback(hcryp);
  4400. #else
  4401. /*Call legacy weak error callback*/
  4402. HAL_CRYP_ErrorCallback(hcryp);
  4403. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4404. }
  4405. }
  4406. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  4407. /* Clear CCF flag */
  4408. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4409. }
  4410. #if (USE_HAL_CRYP_SUSPEND_RESUME == 1U)
  4411. /**
  4412. * @brief In case of message processing suspension, read the Initialization Vector.
  4413. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4414. * the configuration information for CRYP module.
  4415. * @param Output Pointer to the buffer containing the saved Initialization Vector.
  4416. * @note This value has to be stored for reuse by writing the AES_IVRx registers
  4417. * as soon as the suspended processing has to be resumed.
  4418. * @retval None
  4419. */
  4420. static void CRYP_Read_IVRegisters(CRYP_HandleTypeDef *hcryp, uint32_t* Output)
  4421. {
  4422. uint32_t outputaddr = (uint32_t)Output;
  4423. *(uint32_t*)(outputaddr) = hcryp->Instance->IVR3;
  4424. outputaddr+=4U;
  4425. *(uint32_t*)(outputaddr) = hcryp->Instance->IVR2;
  4426. outputaddr+=4U;
  4427. *(uint32_t*)(outputaddr) = hcryp->Instance->IVR1;
  4428. outputaddr+=4U;
  4429. *(uint32_t*)(outputaddr) = hcryp->Instance->IVR0;
  4430. }
  4431. /**
  4432. * @brief In case of message processing resumption, rewrite the Initialization
  4433. * Vector in the AES_IVRx registers.
  4434. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4435. * the configuration information for CRYP module.
  4436. * @param Input Pointer to the buffer containing the saved Initialization Vector to
  4437. * write back in the CRYP hardware block.
  4438. * @note AES must be disabled when reconfiguring the IV values.
  4439. * @retval None
  4440. */
  4441. static void CRYP_Write_IVRegisters(CRYP_HandleTypeDef *hcryp, uint32_t* Input)
  4442. {
  4443. uint32_t ivaddr = (uint32_t)Input;
  4444. hcryp->Instance->IVR3 = *(uint32_t*)(ivaddr);
  4445. ivaddr+=4U;
  4446. hcryp->Instance->IVR2 = *(uint32_t*)(ivaddr);
  4447. ivaddr+=4U;
  4448. hcryp->Instance->IVR1 = *(uint32_t*)(ivaddr);
  4449. ivaddr+=4U;
  4450. hcryp->Instance->IVR0 = *(uint32_t*)(ivaddr);
  4451. }
  4452. /**
  4453. * @brief In case of message GCM/GMAC/CCM processing suspension,
  4454. * read the Suspend Registers.
  4455. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4456. * the configuration information for CRYP module.
  4457. * @param Output Pointer to the buffer containing the saved Suspend Registers.
  4458. * @note These values have to be stored for reuse by writing back the AES_SUSPxR registers
  4459. * as soon as the suspended processing has to be resumed.
  4460. * @retval None
  4461. */
  4462. static void CRYP_Read_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint32_t* Output)
  4463. {
  4464. uint32_t outputaddr = (uint32_t)Output;
  4465. __IO uint32_t count = 0U;
  4466. /* In case of GCM payload phase encryption, check that suspension can be carried out */
  4467. if (READ_BIT(hcryp->Instance->CR, (AES_CR_CHMOD|AES_CR_GCMPH|AES_CR_MODE)) == (CRYP_AES_GCM_GMAC|AES_CR_GCMPH_1|0x0U))
  4468. {
  4469. /* Wait for BUSY flag to be cleared */
  4470. count = 0xFFF;
  4471. do
  4472. {
  4473. count-- ;
  4474. if(count == 0U)
  4475. {
  4476. /* Change state */
  4477. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4478. hcryp->State = HAL_CRYP_STATE_READY;
  4479. /* Process unlocked */
  4480. __HAL_UNLOCK(hcryp);
  4481. HAL_CRYP_ErrorCallback(hcryp);
  4482. return;
  4483. }
  4484. }
  4485. while(HAL_IS_BIT_SET(hcryp->Instance->SR, AES_SR_BUSY));
  4486. }
  4487. *(uint32_t*)(outputaddr) = hcryp->Instance->SUSP7R;
  4488. outputaddr+=4U;
  4489. *(uint32_t*)(outputaddr) = hcryp->Instance->SUSP6R;
  4490. outputaddr+=4U;
  4491. *(uint32_t*)(outputaddr) = hcryp->Instance->SUSP5R;
  4492. outputaddr+=4U;
  4493. *(uint32_t*)(outputaddr) = hcryp->Instance->SUSP4R;
  4494. outputaddr+=4U;
  4495. *(uint32_t*)(outputaddr) = hcryp->Instance->SUSP3R;
  4496. outputaddr+=4U;
  4497. *(uint32_t*)(outputaddr) = hcryp->Instance->SUSP2R;
  4498. outputaddr+=4U;
  4499. *(uint32_t*)(outputaddr) = hcryp->Instance->SUSP1R;
  4500. outputaddr+=4U;
  4501. *(uint32_t*)(outputaddr) = hcryp->Instance->SUSP0R;
  4502. }
  4503. /**
  4504. * @brief In case of message GCM/GMAC/CCM processing resumption, rewrite the Suspend
  4505. * Registers in the AES_SUSPxR registers.
  4506. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4507. * the configuration information for CRYP module.
  4508. * @param Input Pointer to the buffer containing the saved suspend registers to
  4509. * write back in the CRYP hardware block.
  4510. * @note AES must be disabled when reconfiguring the suspend registers.
  4511. * @retval None
  4512. */
  4513. static void CRYP_Write_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint32_t* Input)
  4514. {
  4515. uint32_t ivaddr = (uint32_t)Input;
  4516. hcryp->Instance->SUSP7R = *(uint32_t*)(ivaddr);
  4517. ivaddr+=4U;
  4518. hcryp->Instance->SUSP6R = *(uint32_t*)(ivaddr);
  4519. ivaddr+=4U;
  4520. hcryp->Instance->SUSP5R = *(uint32_t*)(ivaddr);
  4521. ivaddr+=4U;
  4522. hcryp->Instance->SUSP4R = *(uint32_t*)(ivaddr);
  4523. ivaddr+=4U;
  4524. hcryp->Instance->SUSP3R = *(uint32_t*)(ivaddr);
  4525. ivaddr+=4U;
  4526. hcryp->Instance->SUSP2R = *(uint32_t*)(ivaddr);
  4527. ivaddr+=4U;
  4528. hcryp->Instance->SUSP1R = *(uint32_t*)(ivaddr);
  4529. ivaddr+=4U;
  4530. hcryp->Instance->SUSP0R = *(uint32_t*)(ivaddr);
  4531. }
  4532. /**
  4533. * @brief In case of message GCM/GMAC/CCM processing suspension, read the Key Registers.
  4534. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4535. * the configuration information for CRYP module.
  4536. * @param Output Pointer to the buffer containing the saved Key Registers.
  4537. * @param KeySize Indicates the key size (128 or 256 bits).
  4538. * @note These values have to be stored for reuse by writing back the AES_KEYRx registers
  4539. * as soon as the suspended processing has to be resumed.
  4540. * @retval None
  4541. */
  4542. static void CRYP_Read_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint32_t* Output, uint32_t KeySize)
  4543. {
  4544. uint32_t keyaddr = (uint32_t)Output;
  4545. switch (KeySize)
  4546. {
  4547. case CRYP_KEYSIZE_256B:
  4548. *(uint32_t*)(keyaddr) = *(uint32_t *)(hcryp->Init.pKey);
  4549. keyaddr+=4U;
  4550. *(uint32_t*)(keyaddr) = *(uint32_t *)(hcryp->Init.pKey + 1U);
  4551. keyaddr+=4U;
  4552. *(uint32_t*)(keyaddr) = *(uint32_t *)(hcryp->Init.pKey + 2U);
  4553. keyaddr+=4U;
  4554. *(uint32_t*)(keyaddr) = *(uint32_t *)(hcryp->Init.pKey + 3U);
  4555. keyaddr+=4U;
  4556. *(uint32_t*)(keyaddr) = *(uint32_t *)(hcryp->Init.pKey + 4U);
  4557. keyaddr+=4U;
  4558. *(uint32_t*)(keyaddr) = *(uint32_t *)(hcryp->Init.pKey + 5U);
  4559. keyaddr+=4U;
  4560. *(uint32_t*)(keyaddr) = *(uint32_t *)(hcryp->Init.pKey + 6U);
  4561. keyaddr+=4U;
  4562. *(uint32_t*)(keyaddr) = *(uint32_t *)(hcryp->Init.pKey + 7U);
  4563. break;
  4564. case CRYP_KEYSIZE_128B:
  4565. *(uint32_t*)(keyaddr) = *(uint32_t *)(hcryp->Init.pKey);
  4566. keyaddr+=4U;
  4567. *(uint32_t*)(keyaddr) = *(uint32_t *)(hcryp->Init.pKey + 1U);
  4568. keyaddr+=4U;
  4569. *(uint32_t*)(keyaddr) = *(uint32_t *)(hcryp->Init.pKey + 2U);
  4570. keyaddr+=4U;
  4571. *(uint32_t*)(keyaddr) = *(uint32_t *)(hcryp->Init.pKey + 3U);
  4572. break;
  4573. default:
  4574. break;
  4575. }
  4576. }
  4577. /**
  4578. * @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing resumption, rewrite the Key
  4579. * Registers in the AES_KEYRx registers.
  4580. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4581. * the configuration information for CRYP module.
  4582. * @param Input Pointer to the buffer containing the saved key registers to
  4583. * write back in the CRYP hardware block.
  4584. * @param KeySize Indicates the key size (128 or 256 bits)
  4585. * @note AES must be disabled when reconfiguring the Key registers.
  4586. * @retval None
  4587. */
  4588. static void CRYP_Write_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint32_t* Input, uint32_t KeySize)
  4589. {
  4590. uint32_t keyaddr = (uint32_t)Input;
  4591. if (KeySize == CRYP_KEYSIZE_256B)
  4592. {
  4593. hcryp->Instance->KEYR7 = *(uint32_t*)(keyaddr);
  4594. keyaddr+=4U;
  4595. hcryp->Instance->KEYR6 = *(uint32_t*)(keyaddr);
  4596. keyaddr+=4U;
  4597. hcryp->Instance->KEYR5 = *(uint32_t*)(keyaddr);
  4598. keyaddr+=4U;
  4599. hcryp->Instance->KEYR4 = *(uint32_t*)(keyaddr);
  4600. keyaddr+=4U;
  4601. }
  4602. hcryp->Instance->KEYR3 = *(uint32_t*)(keyaddr);
  4603. keyaddr+=4U;
  4604. hcryp->Instance->KEYR2 = *(uint32_t*)(keyaddr);
  4605. keyaddr+=4U;
  4606. hcryp->Instance->KEYR1 = *(uint32_t*)(keyaddr);
  4607. keyaddr+=4U;
  4608. hcryp->Instance->KEYR0 = *(uint32_t*)(keyaddr);
  4609. }
  4610. /**
  4611. * @brief Authentication phase resumption in case of GCM/GMAC/CCM process in interrupt mode
  4612. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  4613. * the configuration information for CRYP module(Header & HeaderSize)
  4614. * @retval None
  4615. */
  4616. static void CRYP_PhaseProcessingResume(CRYP_HandleTypeDef *hcryp)
  4617. {
  4618. uint32_t loopcounter;
  4619. uint16_t lastwordsize;
  4620. uint16_t npblb;
  4621. uint32_t cr_temp;
  4622. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR | CRYP_CCF_CLEAR);
  4623. /* Enable computation complete flag and error interrupts */
  4624. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  4625. /* Enable the CRYP peripheral */
  4626. __HAL_CRYP_ENABLE(hcryp);
  4627. /* Case of header phase resumption =================================================*/
  4628. if (hcryp->Phase == CRYP_PHASE_HEADER_SUSPENDED)
  4629. {
  4630. /* Set the phase */
  4631. hcryp->Phase = CRYP_PHASE_PROCESS;
  4632. /* Select header phase */
  4633. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4634. if ((((hcryp->Init.HeaderSize) - (hcryp->CrypHeaderCount)) >= 4U))
  4635. {
  4636. /* Write the input block in the IN FIFO */
  4637. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount );
  4638. hcryp->CrypHeaderCount++;
  4639. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount );
  4640. hcryp->CrypHeaderCount++;
  4641. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount );
  4642. hcryp->CrypHeaderCount++;
  4643. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount );
  4644. hcryp->CrypHeaderCount++;
  4645. }
  4646. else /*HeaderSize < 4 or HeaderSize >4 & HeaderSize %4 != 0*/
  4647. {
  4648. /* Last block optionally pad the data with zeros*/
  4649. for(loopcounter = 0U; loopcounter < (hcryp->Init.HeaderSize %4U ); loopcounter++)
  4650. {
  4651. hcryp->Instance->DINR = *(uint32_t*)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4652. hcryp->CrypHeaderCount++ ;
  4653. }
  4654. while(loopcounter <4U )
  4655. {
  4656. /* pad the data with zeros to have a complete block */
  4657. hcryp->Instance->DINR = 0x0U;
  4658. loopcounter++;
  4659. }
  4660. }
  4661. }
  4662. /* Case of payload phase resumption =================================================*/
  4663. else
  4664. {
  4665. if (hcryp->Phase == CRYP_PHASE_PAYLOAD_SUSPENDED)
  4666. {
  4667. /* Set the phase */
  4668. hcryp->Phase = CRYP_PHASE_PROCESS;
  4669. /* Select payload phase once the header phase is performed */
  4670. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_PAYLOAD);
  4671. /* Set to 0 the number of non-valid bytes using NPBLB register*/
  4672. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 0U);
  4673. if (((hcryp->Size/4U) - (hcryp->CrypInCount)) >= 4U)
  4674. {
  4675. /* Write the input block in the IN FIFO */
  4676. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  4677. hcryp->CrypInCount++;
  4678. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  4679. hcryp->CrypInCount++;
  4680. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  4681. hcryp->CrypInCount++;
  4682. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  4683. hcryp->CrypInCount++;
  4684. if((hcryp->CrypInCount == hcryp->Size) && (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC))
  4685. {
  4686. /* Call output transfer complete callback */
  4687. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  4688. /*Call registered Input complete callback*/
  4689. hcryp->InCpltCallback(hcryp);
  4690. #else
  4691. /*Call legacy weak Input complete callback*/
  4692. HAL_CRYP_InCpltCallback(hcryp);
  4693. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4694. }
  4695. }
  4696. else /* Last block of payload < 128bit*/
  4697. {
  4698. /* Compute the number of padding bytes in last block of payload */
  4699. npblb = (((hcryp->Size/16U)+1U)*16U) - (hcryp->Size);
  4700. cr_temp = hcryp->Instance->CR;
  4701. if((((cr_temp & AES_CR_MODE) == CRYP_OPERATINGMODE_ENCRYPT) && (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)) ||
  4702. (((cr_temp& AES_CR_MODE) == CRYP_OPERATINGMODE_DECRYPT) && (hcryp->Init.Algorithm == CRYP_AES_CCM)))
  4703. {
  4704. /* Specify the number of non-valid bytes using NPBLB register*/
  4705. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, ((uint32_t)npblb)<< 20U);
  4706. }
  4707. /* Number of valid words (lastwordsize) in last block */
  4708. if ((npblb % 4U) ==0U)
  4709. {
  4710. lastwordsize = (16U-npblb)/4U;
  4711. }
  4712. else
  4713. {
  4714. lastwordsize = ((16U-npblb)/4U) +1U;
  4715. }
  4716. /* Last block optionally pad the data with zeros*/
  4717. for(loopcounter = 0U; loopcounter < lastwordsize; loopcounter++)
  4718. {
  4719. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount );
  4720. hcryp->CrypInCount++;
  4721. }
  4722. while(loopcounter < 4U )
  4723. {
  4724. /* pad the data with zeros to have a complete block */
  4725. hcryp->Instance->DINR = 0x0U;
  4726. loopcounter++;
  4727. }
  4728. }
  4729. }
  4730. }
  4731. }
  4732. #endif /* defined (USE_HAL_CRYP_SUSPEND_RESUME) */
  4733. /**
  4734. * @}
  4735. */
  4736. #endif /* HAL_CRYP_MODULE_ENABLED */
  4737. /**
  4738. * @}
  4739. */
  4740. /**
  4741. * @}
  4742. */
  4743. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/