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.
 
 
 

5146 lines
164 KiB

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