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.
 
 
 
 
 

7156 lines
229 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_cryp.c
  4. * @author MCD Application Team
  5. * @brief CRYP HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Cryptography (CRYP) peripheral:
  8. * + Initialization, de-initialization, set config and get config functions
  9. * + DES/TDES, AES processing functions
  10. * + DMA callback functions
  11. * + CRYP IRQ handler management
  12. * + Peripheral State functions
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2016 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file
  21. * in the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. *
  24. ******************************************************************************
  25. @verbatim
  26. ==============================================================================
  27. ##### How to use this driver #####
  28. ==============================================================================
  29. [..]
  30. The CRYP HAL driver can be used in CRYP or TinyAES IP as follows:
  31. (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
  32. (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()or
  33. __HAL_RCC_AES_CLK_ENABLE for TinyAES IP
  34. (##) In case of using interrupts (e.g. HAL_CRYP_Encrypt_IT())
  35. (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
  36. (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
  37. (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
  38. (##) In case of using DMA to control data transfer (e.g. HAL_CRYP_Encrypt_DMA())
  39. (+++) Enable the DMAx interface clock using __RCC_DMAx_CLK_ENABLE()
  40. (+++) Configure and enable two DMA streams one for managing data transfer from
  41. memory to peripheral (input stream) and another stream for managing data
  42. transfer from peripheral to memory (output stream)
  43. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  44. using __HAL_LINKDMA()
  45. (+++) Configure the priority and enable the NVIC for the transfer complete
  46. interrupt on the two DMA Streams. The output stream should have higher
  47. priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
  48. (#)Initialize the CRYP according to the specified parameters :
  49. (##) The data type: 1-bit, 8-bit, 16-bit or 32-bit.
  50. (##) The key size: 128, 192 or 256.
  51. (##) The AlgoMode DES/ TDES Algorithm ECB/CBC or AES Algorithm ECB/CBC/CTR/GCM or CCM.
  52. (##) The initialization vector (counter). It is not used in ECB mode.
  53. (##) The key buffer used for encryption/decryption.
  54. (##) The Header used only in AES GCM and CCM Algorithm for authentication.
  55. (##) The HeaderSize The size of header buffer in word.
  56. (##) The B0 block is the first authentication block used only in AES CCM mode.
  57. (#)Three processing (encryption/decryption) functions are available:
  58. (##) Polling mode: encryption and decryption APIs are blocking functions
  59. i.e. they process the data and wait till the processing is finished,
  60. e.g. HAL_CRYP_Encrypt & HAL_CRYP_Decrypt
  61. (##) Interrupt mode: encryption and decryption APIs are not blocking functions
  62. i.e. they process the data under interrupt,
  63. e.g. HAL_CRYP_Encrypt_IT & HAL_CRYP_Decrypt_IT
  64. (##) DMA mode: encryption and decryption APIs are not blocking functions
  65. i.e. the data transfer is ensured by DMA,
  66. e.g. HAL_CRYP_Encrypt_DMA & HAL_CRYP_Decrypt_DMA
  67. (#)When the processing function is called at first time after HAL_CRYP_Init()
  68. the CRYP peripheral is configured and processes the buffer in input.
  69. At second call, no need to Initialize the CRYP, user have to get current configuration via
  70. HAL_CRYP_GetConfig() API, then only HAL_CRYP_SetConfig() is requested to set
  71. new parameters, finally user can start encryption/decryption.
  72. (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
  73. (#)To process a single message with consecutive calls to HAL_CRYP_Encrypt() or HAL_CRYP_Decrypt()
  74. without having to configure again the Key or the Initialization Vector between each API call,
  75. the field KeyIVConfigSkip of the initialization structure must be set to CRYP_KEYIVCONFIG_ONCE.
  76. Same is true for consecutive calls of HAL_CRYP_Encrypt_IT(), HAL_CRYP_Decrypt_IT(), HAL_CRYP_Encrypt_DMA()
  77. or HAL_CRYP_Decrypt_DMA().
  78. [..]
  79. The cryptographic processor supports following standards:
  80. (#) The data encryption standard (DES) and Triple-DES (TDES) supported only by CRYP1 IP:
  81. (##)64-bit data block processing
  82. (##) chaining modes supported :
  83. (+++) Electronic Code Book(ECB)
  84. (+++) Cipher Block Chaining (CBC)
  85. (##) keys length supported :64-bit, 128-bit and 192-bit.
  86. (#) The advanced encryption standard (AES) supported by CRYP1 & TinyAES IP:
  87. (##)128-bit data block processing
  88. (##) chaining modes supported :
  89. (+++) Electronic Code Book(ECB)
  90. (+++) Cipher Block Chaining (CBC)
  91. (+++) Counter mode (CTR)
  92. (+++) Galois/counter mode (GCM/GMAC)
  93. (+++) Counter with Cipher Block Chaining-Message(CCM)
  94. (##) keys length Supported :
  95. (+++) for CRYP1 IP: 128-bit, 192-bit and 256-bit.
  96. (+++) for TinyAES IP: 128-bit and 256-bit
  97. [..] This section describes the AES Galois/counter mode (GCM) supported by both CRYP1 IP:
  98. (#) Algorithm supported :
  99. (##) Galois/counter mode (GCM)
  100. (##) Galois message authentication code (GMAC) :is exactly the same as
  101. GCM algorithm composed only by an header.
  102. (#) Four phases are performed in GCM :
  103. (##) Init phase: IP prepares the GCM hash subkey (H) and do the IV processing
  104. (##) Header phase: IP processes the Additional Authenticated Data (AAD), with hash
  105. computation only.
  106. (##) Payload phase: IP processes the plaintext (P) with hash computation + keystream
  107. encryption + data XORing. It works in a similar way for ciphertext (C).
  108. (##) Final phase: IP generates the authenticated tag (T) using the last block of data.
  109. (#) structure of message construction in GCM is defined as below :
  110. (##) 16 bytes Initial Counter Block (ICB)composed of IV and counter
  111. (##) The authenticated header A (also knows as Additional Authentication Data AAD)
  112. this part of the message is only authenticated, not encrypted.
  113. (##) The plaintext message P is both authenticated and encrypted as ciphertext.
  114. GCM standard specifies that ciphertext has same bit length as the plaintext.
  115. (##) The last block is composed of the length of A (on 64 bits) and the length of ciphertext
  116. (on 64 bits)
  117. [..] This section describe The AES Counter with Cipher Block Chaining-Message
  118. Authentication Code (CCM) supported by both CRYP1 IP:
  119. (#) Specific parameters for CCM :
  120. (##) B0 block : According to NIST Special Publication 800-38C,
  121. The first block B0 is formatted as follows, where l(m) is encoded in
  122. most-significant-byte first order(see below table 3)
  123. (+++) Q: a bit string representation of the octet length of P (plaintext)
  124. (+++) q The octet length of the binary representation of the octet length of the payload
  125. (+++) A nonce (N), n The octet length of the where n+q=15.
  126. (+++) Flags: most significant octet containing four flags for control information,
  127. (+++) t The octet length of the MAC.
  128. (##) B1 block (header) : associated data length(a) concatenated with Associated Data (A)
  129. the associated data length expressed in bytes (a) defined as below:
  130. (+++) If 0 < a < 216-28, then it is encoded as [a]16, i.e. two octets
  131. (+++) If 216-28 < a < 232, then it is encoded as 0xff || 0xfe || [a]32, i.e. six octets
  132. (+++) If 232 < a < 264, then it is encoded as 0xff || 0xff || [a]64, i.e. ten octets
  133. (##) CTRx block : control blocks
  134. (+++) Generation of CTR1 from first block B0 information :
  135. equal to B0 with first 5 bits zeroed and most significant bits storing octet
  136. length of P also zeroed, then incremented by one ( see below Table 4)
  137. (+++) Generation of CTR0: same as CTR1 with bit[0] set to zero.
  138. (#) Four phases are performed in CCM for CRYP1 IP:
  139. (##) Init phase: IP prepares the GCM hash subkey (H) and do the IV processing
  140. (##) Header phase: IP processes the Additional Authenticated Data (AAD), with hash
  141. computation only.
  142. (##) Payload phase: IP processes the plaintext (P) with hash computation + keystream
  143. encryption + data XORing. It works in a similar way for ciphertext (C).
  144. (##) Final phase: IP generates the authenticated tag (T) using the last block of data.
  145. *** Callback registration ***
  146. =============================================
  147. The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS when set to 1
  148. allows the user to configure dynamically the driver callbacks.
  149. Use Functions HAL_CRYP_RegisterCallback() or HAL_CRYP_RegisterXXXCallback()
  150. to register an interrupt callback.
  151. Function HAL_CRYP_RegisterCallback() allows to register following callbacks:
  152. (+) InCpltCallback : Input FIFO transfer completed callback.
  153. (+) OutCpltCallback : Output FIFO transfer completed callback.
  154. (+) ErrorCallback : callback for error detection.
  155. (+) MspInitCallback : CRYP MspInit.
  156. (+) MspDeInitCallback : CRYP MspDeInit.
  157. This function takes as parameters the HAL peripheral handle, the Callback ID
  158. and a pointer to the user callback function.
  159. Use function HAL_CRYP_UnRegisterCallback() to reset a callback to the default
  160. weak function.
  161. HAL_CRYP_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  162. and the Callback ID.
  163. This function allows to reset following callbacks:
  164. (+) InCpltCallback : Input FIFO transfer completed callback.
  165. (+) OutCpltCallback : Output FIFO transfer completed callback.
  166. (+) ErrorCallback : callback for error detection.
  167. (+) MspInitCallback : CRYP MspInit.
  168. (+) MspDeInitCallback : CRYP MspDeInit.
  169. By default, after the HAL_CRYP_Init() and when the state is HAL_CRYP_STATE_RESET
  170. all callbacks are set to the corresponding weak functions :
  171. examples HAL_CRYP_InCpltCallback() , HAL_CRYP_OutCpltCallback().
  172. Exception done for MspInit and MspDeInit functions that are
  173. reset to the legacy weak function in the HAL_CRYP_Init()/ HAL_CRYP_DeInit() only when
  174. these callbacks are null (not registered beforehand).
  175. if not, MspInit or MspDeInit are not null, the HAL_CRYP_Init() / HAL_CRYP_DeInit()
  176. keep and use the user MspInit/MspDeInit functions (registered beforehand)
  177. Callbacks can be registered/unregistered in HAL_CRYP_STATE_READY state only.
  178. Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
  179. in HAL_CRYP_STATE_READY or HAL_CRYP_STATE_RESET state,
  180. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  181. In that case first register the MspInit/MspDeInit user callbacks
  182. using HAL_CRYP_RegisterCallback() before calling HAL_CRYP_DeInit()
  183. or HAL_CRYP_Init() function.
  184. When The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS is set to 0 or
  185. not defined, the callback registration feature is not available and all callbacks
  186. are set to the corresponding weak functions.
  187. Table 1. Initial Counter Block (ICB)
  188. +-------------------------------------------------------+
  189. | Initialization vector (IV) | Counter |
  190. |----------------|----------------|-----------|---------|
  191. 127 95 63 31 0
  192. Bit Number Register Contents
  193. ---------- --------------- -----------
  194. 127 ...96 CRYP_IV1R[31:0] ICB[127:96]
  195. 95 ...64 CRYP_IV1L[31:0] B0[95:64]
  196. 63 ... 32 CRYP_IV0R[31:0] ICB[63:32]
  197. 31 ... 0 CRYP_IV0L[31:0] ICB[31:0], where 32-bit counter= 0x2
  198. Table 2. GCM last block definition
  199. +-------------------------------------------------------------------+
  200. | Bit[0] | Bit[32] | Bit[64] | Bit[96] |
  201. |-----------|--------------------|-----------|----------------------|
  202. | 0x0 | Header length[31:0]| 0x0 | Payload length[31:0] |
  203. |-----------|--------------------|-----------|----------------------|
  204. Table 3. B0 block
  205. Octet Number Contents
  206. ------------ ---------
  207. 0 Flags
  208. 1 ... 15-q Nonce N
  209. 16-q ... 15 Q
  210. the Flags field is formatted as follows:
  211. Bit Number Contents
  212. ---------- ----------------------
  213. 7 Reserved (always zero)
  214. 6 Adata
  215. 5 ... 3 (t-2)/2
  216. 2 ... 0 [q-1]3
  217. Table 4. CTRx block
  218. Bit Number Register Contents
  219. ---------- --------------- -----------
  220. 127 ...96 CRYP_IV1R[31:0] B0[127:96], where Q length bits are set to 0, except for
  221. bit 0 that is set to 1
  222. 95 ...64 CRYP_IV1L[31:0] B0[95:64]
  223. 63 ... 32 CRYP_IV0R[31:0] B0[63:32]
  224. 31 ... 0 CRYP_IV0L[31:0] B0[31:0], where flag bits set to 0
  225. @endverbatim
  226. ******************************************************************************
  227. */
  228. /* Includes ------------------------------------------------------------------*/
  229. #include "stm32f4xx_hal.h"
  230. /** @addtogroup STM32F4xx_HAL_Driver
  231. * @{
  232. */
  233. #if defined (AES) || defined (CRYP)
  234. /** @defgroup CRYP CRYP
  235. * @brief CRYP HAL module driver.
  236. * @{
  237. */
  238. #ifdef HAL_CRYP_MODULE_ENABLED
  239. /* Private typedef -----------------------------------------------------------*/
  240. /* Private define ------------------------------------------------------------*/
  241. /** @addtogroup CRYP_Private_Defines
  242. * @{
  243. */
  244. #define CRYP_TIMEOUT_KEYPREPARATION 82U /*The latency of key preparation operation is 82 clock cycles.*/
  245. #define CRYP_TIMEOUT_GCMCCMINITPHASE 299U /* The latency of GCM/CCM init phase to prepare hash subkey is
  246. 299 clock cycles.*/
  247. #define CRYP_TIMEOUT_GCMCCMHEADERPHASE 290U /* The latency of GCM/CCM header phase is 290 clock cycles.*/
  248. #define CRYP_PHASE_READY 0x00000001U /*!< CRYP peripheral is ready for initialization. */
  249. #define CRYP_PHASE_PROCESS 0x00000002U /*!< CRYP peripheral is in processing phase */
  250. #if defined(AES)
  251. #define CRYP_OPERATINGMODE_ENCRYPT 0x00000000U /*!< Encryption mode(Mode 1) */
  252. #define CRYP_OPERATINGMODE_KEYDERIVATION AES_CR_MODE_0 /*!< Key derivation mode only used when performing ECB and CBC decryptions (Mode 2) */
  253. #define CRYP_OPERATINGMODE_DECRYPT AES_CR_MODE_1 /*!< Decryption (Mode 3) */
  254. #define CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT AES_CR_MODE /*!< Key derivation and decryption only used when performing ECB and CBC decryptions (Mode 4) */
  255. #define CRYP_PHASE_INIT 0x00000000U /*!< GCM/GMAC (or CCM) init phase */
  256. #define CRYP_PHASE_HEADER AES_CR_GCMPH_0 /*!< GCM/GMAC or CCM header phase */
  257. #define CRYP_PHASE_PAYLOAD AES_CR_GCMPH_1 /*!< GCM(/CCM) payload phase */
  258. #define CRYP_PHASE_FINAL AES_CR_GCMPH /*!< GCM/GMAC or CCM final phase */
  259. #else /* CRYP */
  260. #define CRYP_PHASE_INIT 0x00000000U /*!< GCM/GMAC (or CCM) init phase */
  261. #define CRYP_PHASE_HEADER CRYP_CR_GCM_CCMPH_0 /*!< GCM/GMAC or CCM header phase */
  262. #define CRYP_PHASE_PAYLOAD CRYP_CR_GCM_CCMPH_1 /*!< GCM(/CCM) payload phase */
  263. #define CRYP_PHASE_FINAL CRYP_CR_GCM_CCMPH /*!< GCM/GMAC or CCM final phase */
  264. #define CRYP_OPERATINGMODE_ENCRYPT 0x00000000U /*!< Encryption mode */
  265. #define CRYP_OPERATINGMODE_DECRYPT CRYP_CR_ALGODIR /*!< Decryption */
  266. #endif /* End CRYP or AES */
  267. /* CTR1 information to use in CCM algorithm */
  268. #define CRYP_CCM_CTR1_0 0x07FFFFFFU
  269. #define CRYP_CCM_CTR1_1 0xFFFFFF00U
  270. #define CRYP_CCM_CTR1_2 0x00000001U
  271. /**
  272. * @}
  273. */
  274. /* Private macro -------------------------------------------------------------*/
  275. /** @addtogroup CRYP_Private_Macros
  276. * @{
  277. */
  278. #if defined(CRYP)
  279. #define CRYP_SET_PHASE(__HANDLE__, __PHASE__) do{(__HANDLE__)->Instance->CR &= (uint32_t)(~CRYP_CR_GCM_CCMPH);\
  280. (__HANDLE__)->Instance->CR |= (uint32_t)(__PHASE__);\
  281. }while(0)
  282. #define HAL_CRYP_FIFO_FLUSH(__HANDLE__) ((__HANDLE__)->Instance->CR |= CRYP_CR_FFLUSH)
  283. #else /*AES*/
  284. #define CRYP_SET_PHASE(__HANDLE__, __PHASE__) do{(__HANDLE__)->Instance->CR &= (uint32_t)(~AES_CR_GCMPH);\
  285. (__HANDLE__)->Instance->CR |= (uint32_t)(__PHASE__);\
  286. }while(0)
  287. #endif /* End AES or CRYP*/
  288. /**
  289. * @}
  290. */
  291. /* Private struct -------------------------------------------------------------*/
  292. /* Private variables ---------------------------------------------------------*/
  293. /* Private function prototypes -----------------------------------------------*/
  294. /** @addtogroup CRYP_Private_Functions_prototypes
  295. * @{
  296. */
  297. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  298. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
  299. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
  300. static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
  301. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint32_t KeySize);
  302. static void CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
  303. #if defined (CRYP_CR_ALGOMODE_AES_GCM)|| defined (AES)
  304. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  305. static void CRYP_GCMCCM_SetPayloadPhase_IT(CRYP_HandleTypeDef *hcryp);
  306. static void CRYP_GCMCCM_SetHeaderPhase_IT(CRYP_HandleTypeDef *hcryp);
  307. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase_DMA(CRYP_HandleTypeDef *hcryp);
  308. static void CRYP_Workaround(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  309. static HAL_StatusTypeDef CRYP_AESGCM_Process_DMA(CRYP_HandleTypeDef *hcryp);
  310. static HAL_StatusTypeDef CRYP_AESGCM_Process_IT(CRYP_HandleTypeDef *hcryp);
  311. static HAL_StatusTypeDef CRYP_AESGCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  312. static HAL_StatusTypeDef CRYP_AESCCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  313. static HAL_StatusTypeDef CRYP_AESCCM_Process_IT(CRYP_HandleTypeDef *hcryp);
  314. static HAL_StatusTypeDef CRYP_AESCCM_Process_DMA(CRYP_HandleTypeDef *hcryp);
  315. #endif /* AES or GCM CCM defined*/
  316. static void CRYP_AES_ProcessData(CRYP_HandleTypeDef *hcrypt, uint32_t Timeout);
  317. static HAL_StatusTypeDef CRYP_AES_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  318. static HAL_StatusTypeDef CRYP_AES_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  319. static HAL_StatusTypeDef CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef *hcryp);
  320. static HAL_StatusTypeDef CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef *hcryp);
  321. static HAL_StatusTypeDef CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef *hcryp);
  322. #if defined (CRYP)
  323. static void CRYP_TDES_IT(CRYP_HandleTypeDef *hcryp);
  324. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  325. static HAL_StatusTypeDef CRYP_WaitOnIFEMFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  326. #endif /* GCM CCM defined*/
  327. static HAL_StatusTypeDef CRYP_WaitOnBUSYFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  328. static HAL_StatusTypeDef CRYP_WaitOnOFNEFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  329. static HAL_StatusTypeDef CRYP_TDES_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  330. #else /*AES*/
  331. static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  332. #endif /* End CRYP or AES */
  333. /**
  334. * @}
  335. */
  336. /* Exported functions ---------------------------------------------------------*/
  337. /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
  338. * @{
  339. */
  340. /** @defgroup CRYP_Exported_Functions_Group1 Initialization and de-initialization functions
  341. * @brief Initialization and Configuration functions.
  342. *
  343. @verbatim
  344. ========================================================================================
  345. ##### Initialization, de-initialization and Set and Get configuration functions #####
  346. ========================================================================================
  347. [..] This section provides functions allowing to:
  348. (+) Initialize the CRYP
  349. (+) DeInitialize the CRYP
  350. (+) Initialize the CRYP MSP
  351. (+) DeInitialize the CRYP MSP
  352. (+) configure CRYP (HAL_CRYP_SetConfig) with the specified parameters in the CRYP_ConfigTypeDef
  353. Parameters which are configured in This section are :
  354. (+) Key size
  355. (+) Data Type : 32,16, 8 or 1bit
  356. (+) AlgoMode :
  357. - for CRYP1 IP :
  358. ECB and CBC in DES/TDES Standard
  359. ECB,CBC,CTR,GCM/GMAC and CCM in AES Standard.
  360. - for TinyAES2 IP, only ECB,CBC,CTR,GCM/GMAC and CCM in AES Standard are supported.
  361. (+) Get CRYP configuration (HAL_CRYP_GetConfig) from the specified parameters in the CRYP_HandleTypeDef
  362. @endverbatim
  363. * @{
  364. */
  365. /**
  366. * @brief Initializes the CRYP according to the specified
  367. * parameters in the CRYP_ConfigTypeDef and creates the associated handle.
  368. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  369. * the configuration information for CRYP module
  370. * @retval HAL status
  371. */
  372. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
  373. {
  374. /* Check the CRYP handle allocation */
  375. if (hcryp == NULL)
  376. {
  377. return HAL_ERROR;
  378. }
  379. /* Check parameters */
  380. assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
  381. assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
  382. assert_param(IS_CRYP_ALGORITHM(hcryp->Init.Algorithm));
  383. assert_param(IS_CRYP_INIT(hcryp->Init.KeyIVConfigSkip));
  384. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  385. if (hcryp->State == HAL_CRYP_STATE_RESET)
  386. {
  387. /* Allocate lock resource and initialize it */
  388. hcryp->Lock = HAL_UNLOCKED;
  389. hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak InCpltCallback */
  390. hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak OutCpltCallback */
  391. hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak ErrorCallback */
  392. if (hcryp->MspInitCallback == NULL)
  393. {
  394. hcryp->MspInitCallback = HAL_CRYP_MspInit; /* Legacy weak MspInit */
  395. }
  396. /* Init the low level hardware */
  397. hcryp->MspInitCallback(hcryp);
  398. }
  399. #else
  400. if (hcryp->State == HAL_CRYP_STATE_RESET)
  401. {
  402. /* Allocate lock resource and initialize it */
  403. hcryp->Lock = HAL_UNLOCKED;
  404. /* Init the low level hardware */
  405. HAL_CRYP_MspInit(hcryp);
  406. }
  407. #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
  408. /* Set the key size(This bit field is don't care in the DES or TDES modes) data type and Algorithm */
  409. #if defined (CRYP)
  410. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_DATATYPE | CRYP_CR_KEYSIZE | CRYP_CR_ALGOMODE,
  411. hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
  412. #else /*AES*/
  413. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE | AES_CR_KEYSIZE | AES_CR_CHMOD,
  414. hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
  415. #endif /* End AES or CRYP*/
  416. /* Reset Error Code field */
  417. hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
  418. /* Change the CRYP state */
  419. hcryp->State = HAL_CRYP_STATE_READY;
  420. /* Set the default CRYP phase */
  421. hcryp->Phase = CRYP_PHASE_READY;
  422. /* Return function status */
  423. return HAL_OK;
  424. }
  425. /**
  426. * @brief De-Initializes the CRYP peripheral.
  427. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  428. * the configuration information for CRYP module
  429. * @retval HAL status
  430. */
  431. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
  432. {
  433. /* Check the CRYP handle allocation */
  434. if (hcryp == NULL)
  435. {
  436. return HAL_ERROR;
  437. }
  438. /* Set the default CRYP phase */
  439. hcryp->Phase = CRYP_PHASE_READY;
  440. /* Reset CrypInCount and CrypOutCount */
  441. hcryp->CrypInCount = 0;
  442. hcryp->CrypOutCount = 0;
  443. hcryp->CrypHeaderCount = 0;
  444. /* Disable the CRYP peripheral clock */
  445. __HAL_CRYP_DISABLE(hcryp);
  446. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  447. if (hcryp->MspDeInitCallback == NULL)
  448. {
  449. hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit; /* Legacy weak MspDeInit */
  450. }
  451. /* DeInit the low level hardware */
  452. hcryp->MspDeInitCallback(hcryp);
  453. #else
  454. /* DeInit the low level hardware: CLOCK, NVIC.*/
  455. HAL_CRYP_MspDeInit(hcryp);
  456. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  457. /* Change the CRYP state */
  458. hcryp->State = HAL_CRYP_STATE_RESET;
  459. /* Release Lock */
  460. __HAL_UNLOCK(hcryp);
  461. /* Return function status */
  462. return HAL_OK;
  463. }
  464. /**
  465. * @brief Configure the CRYP according to the specified
  466. * parameters in the CRYP_ConfigTypeDef
  467. * @param hcryp: pointer to a CRYP_HandleTypeDef structure
  468. * @param pConf: pointer to a CRYP_ConfigTypeDef structure that contains
  469. * the configuration information for CRYP module
  470. * @retval HAL status
  471. */
  472. HAL_StatusTypeDef HAL_CRYP_SetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf)
  473. {
  474. /* Check the CRYP handle allocation */
  475. if ((hcryp == NULL) || (pConf == NULL))
  476. {
  477. return HAL_ERROR;
  478. }
  479. /* Check parameters */
  480. assert_param(IS_CRYP_KEYSIZE(pConf->KeySize));
  481. assert_param(IS_CRYP_DATATYPE(pConf->DataType));
  482. assert_param(IS_CRYP_ALGORITHM(pConf->Algorithm));
  483. if (hcryp->State == HAL_CRYP_STATE_READY)
  484. {
  485. /* Change the CRYP state */
  486. hcryp->State = HAL_CRYP_STATE_BUSY;
  487. /* Process locked */
  488. __HAL_LOCK(hcryp);
  489. /* Set CRYP parameters */
  490. hcryp->Init.DataType = pConf->DataType;
  491. hcryp->Init.pKey = pConf->pKey;
  492. hcryp->Init.Algorithm = pConf->Algorithm;
  493. hcryp->Init.KeySize = pConf->KeySize;
  494. hcryp->Init.pInitVect = pConf->pInitVect;
  495. hcryp->Init.Header = pConf->Header;
  496. hcryp->Init.HeaderSize = pConf->HeaderSize;
  497. hcryp->Init.B0 = pConf->B0;
  498. hcryp->Init.DataWidthUnit = pConf->DataWidthUnit;
  499. hcryp->Init.KeyIVConfigSkip = pConf->KeyIVConfigSkip;
  500. hcryp->Init.HeaderWidthUnit = pConf->HeaderWidthUnit;
  501. /* Set the key size(This bit field is don't care in the DES or TDES modes) data type, AlgoMode and operating mode*/
  502. #if defined (CRYP)
  503. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_DATATYPE | CRYP_CR_KEYSIZE | CRYP_CR_ALGOMODE,
  504. hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
  505. #else /*AES*/
  506. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE | AES_CR_KEYSIZE | AES_CR_CHMOD,
  507. hcryp->Init.DataType | hcryp->Init.KeySize | hcryp->Init.Algorithm);
  508. /*clear error flags*/
  509. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR);
  510. #endif /* End AES or CRYP */
  511. /* Process Unlocked */
  512. __HAL_UNLOCK(hcryp);
  513. /* Reset Error Code field */
  514. hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
  515. /* Change the CRYP state */
  516. hcryp->State = HAL_CRYP_STATE_READY;
  517. /* Set the default CRYP phase */
  518. hcryp->Phase = CRYP_PHASE_READY;
  519. /* Return function status */
  520. return HAL_OK;
  521. }
  522. else
  523. {
  524. /* Process Unlocked */
  525. __HAL_UNLOCK(hcryp);
  526. /* Busy error code field */
  527. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  528. return HAL_ERROR;
  529. }
  530. }
  531. /**
  532. * @brief Get CRYP Configuration parameters in associated handle.
  533. * @param pConf: pointer to a CRYP_ConfigTypeDef structure
  534. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  535. * the configuration information for CRYP module
  536. * @retval HAL status
  537. */
  538. HAL_StatusTypeDef HAL_CRYP_GetConfig(CRYP_HandleTypeDef *hcryp, CRYP_ConfigTypeDef *pConf)
  539. {
  540. /* Check the CRYP handle allocation */
  541. if ((hcryp == NULL) || (pConf == NULL))
  542. {
  543. return HAL_ERROR;
  544. }
  545. if (hcryp->State == HAL_CRYP_STATE_READY)
  546. {
  547. /* Change the CRYP state */
  548. hcryp->State = HAL_CRYP_STATE_BUSY;
  549. /* Process locked */
  550. __HAL_LOCK(hcryp);
  551. /* Get CRYP parameters */
  552. pConf->DataType = hcryp->Init.DataType;
  553. pConf->pKey = hcryp->Init.pKey;
  554. pConf->Algorithm = hcryp->Init.Algorithm;
  555. pConf->KeySize = hcryp->Init.KeySize ;
  556. pConf->pInitVect = hcryp->Init.pInitVect;
  557. pConf->Header = hcryp->Init.Header ;
  558. pConf->HeaderSize = hcryp->Init.HeaderSize;
  559. pConf->B0 = hcryp->Init.B0;
  560. pConf->DataWidthUnit = hcryp->Init.DataWidthUnit;
  561. pConf->KeyIVConfigSkip = hcryp->Init.KeyIVConfigSkip;
  562. pConf->HeaderWidthUnit = hcryp->Init.HeaderWidthUnit;
  563. /* Process Unlocked */
  564. __HAL_UNLOCK(hcryp);
  565. /* Change the CRYP state */
  566. hcryp->State = HAL_CRYP_STATE_READY;
  567. /* Return function status */
  568. return HAL_OK;
  569. }
  570. else
  571. {
  572. /* Process Unlocked */
  573. __HAL_UNLOCK(hcryp);
  574. /* Busy error code field */
  575. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  576. return HAL_ERROR;
  577. }
  578. }
  579. /**
  580. * @brief Initializes the CRYP MSP.
  581. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  582. * the configuration information for CRYP module
  583. * @retval None
  584. */
  585. __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
  586. {
  587. /* Prevent unused argument(s) compilation warning */
  588. UNUSED(hcryp);
  589. /* NOTE : This function should not be modified, when the callback is needed,
  590. the HAL_CRYP_MspInit can be implemented in the user file
  591. */
  592. }
  593. /**
  594. * @brief DeInitializes CRYP MSP.
  595. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  596. * the configuration information for CRYP module
  597. * @retval None
  598. */
  599. __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
  600. {
  601. /* Prevent unused argument(s) compilation warning */
  602. UNUSED(hcryp);
  603. /* NOTE : This function should not be modified, when the callback is needed,
  604. the HAL_CRYP_MspDeInit can be implemented in the user file
  605. */
  606. }
  607. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  608. /**
  609. * @brief Register a User CRYP Callback
  610. * To be used instead of the weak predefined callback
  611. * @param hcryp cryp handle
  612. * @param CallbackID ID of the callback to be registered
  613. * This parameter can be one of the following values:
  614. * @arg @ref HAL_CRYP_INPUT_COMPLETE_CB_ID Input FIFO transfer completed callback ID
  615. * @arg @ref HAL_CRYP_OUTPUT_COMPLETE_CB_ID Output FIFO transfer completed callback ID
  616. * @arg @ref HAL_CRYP_ERROR_CB_ID Error callback ID
  617. * @arg @ref HAL_CRYP_MSPINIT_CB_ID MspInit callback ID
  618. * @arg @ref HAL_CRYP_MSPDEINIT_CB_ID MspDeInit callback ID
  619. * @param pCallback pointer to the Callback function
  620. * @retval status
  621. */
  622. HAL_StatusTypeDef HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID,
  623. pCRYP_CallbackTypeDef pCallback)
  624. {
  625. HAL_StatusTypeDef status = HAL_OK;
  626. if (pCallback == NULL)
  627. {
  628. /* Update the error code */
  629. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  630. return HAL_ERROR;
  631. }
  632. /* Process locked */
  633. __HAL_LOCK(hcryp);
  634. if (hcryp->State == HAL_CRYP_STATE_READY)
  635. {
  636. switch (CallbackID)
  637. {
  638. case HAL_CRYP_INPUT_COMPLETE_CB_ID :
  639. hcryp->InCpltCallback = pCallback;
  640. break;
  641. case HAL_CRYP_OUTPUT_COMPLETE_CB_ID :
  642. hcryp->OutCpltCallback = pCallback;
  643. break;
  644. case HAL_CRYP_ERROR_CB_ID :
  645. hcryp->ErrorCallback = pCallback;
  646. break;
  647. case HAL_CRYP_MSPINIT_CB_ID :
  648. hcryp->MspInitCallback = pCallback;
  649. break;
  650. case HAL_CRYP_MSPDEINIT_CB_ID :
  651. hcryp->MspDeInitCallback = pCallback;
  652. break;
  653. default :
  654. /* Update the error code */
  655. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  656. /* Return error status */
  657. status = HAL_ERROR;
  658. break;
  659. }
  660. }
  661. else if (hcryp->State == HAL_CRYP_STATE_RESET)
  662. {
  663. switch (CallbackID)
  664. {
  665. case HAL_CRYP_MSPINIT_CB_ID :
  666. hcryp->MspInitCallback = pCallback;
  667. break;
  668. case HAL_CRYP_MSPDEINIT_CB_ID :
  669. hcryp->MspDeInitCallback = pCallback;
  670. break;
  671. default :
  672. /* Update the error code */
  673. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  674. /* Return error status */
  675. status = HAL_ERROR;
  676. break;
  677. }
  678. }
  679. else
  680. {
  681. /* Update the error code */
  682. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  683. /* Return error status */
  684. status = HAL_ERROR;
  685. }
  686. /* Release Lock */
  687. __HAL_UNLOCK(hcryp);
  688. return status;
  689. }
  690. /**
  691. * @brief Unregister an CRYP Callback
  692. * CRYP callback is redirected to the weak predefined callback
  693. * @param hcryp cryp handle
  694. * @param CallbackID ID of the callback to be unregistered
  695. * This parameter can be one of the following values:
  696. * @arg @ref HAL_CRYP_INPUT_COMPLETE_CB_ID Input FIFO transfer completed callback ID
  697. * @arg @ref HAL_CRYP_OUTPUT_COMPLETE_CB_ID Output FIFO transfer completed callback ID
  698. * @arg @ref HAL_CRYP_ERROR_CB_ID Error callback ID
  699. * @arg @ref HAL_CRYP_MSPINIT_CB_ID MspInit callback ID
  700. * @arg @ref HAL_CRYP_MSPDEINIT_CB_ID MspDeInit callback ID
  701. * @retval status
  702. */
  703. HAL_StatusTypeDef HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID)
  704. {
  705. HAL_StatusTypeDef status = HAL_OK;
  706. /* Process locked */
  707. __HAL_LOCK(hcryp);
  708. if (hcryp->State == HAL_CRYP_STATE_READY)
  709. {
  710. switch (CallbackID)
  711. {
  712. case HAL_CRYP_INPUT_COMPLETE_CB_ID :
  713. hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak InCpltCallback */
  714. break;
  715. case HAL_CRYP_OUTPUT_COMPLETE_CB_ID :
  716. hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak OutCpltCallback */
  717. break;
  718. case HAL_CRYP_ERROR_CB_ID :
  719. hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak ErrorCallback */
  720. break;
  721. case HAL_CRYP_MSPINIT_CB_ID :
  722. hcryp->MspInitCallback = HAL_CRYP_MspInit;
  723. break;
  724. case HAL_CRYP_MSPDEINIT_CB_ID :
  725. hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;
  726. break;
  727. default :
  728. /* Update the error code */
  729. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  730. /* Return error status */
  731. status = HAL_ERROR;
  732. break;
  733. }
  734. }
  735. else if (hcryp->State == HAL_CRYP_STATE_RESET)
  736. {
  737. switch (CallbackID)
  738. {
  739. case HAL_CRYP_MSPINIT_CB_ID :
  740. hcryp->MspInitCallback = HAL_CRYP_MspInit;
  741. break;
  742. case HAL_CRYP_MSPDEINIT_CB_ID :
  743. hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;
  744. break;
  745. default :
  746. /* Update the error code */
  747. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  748. /* Return error status */
  749. status = HAL_ERROR;
  750. break;
  751. }
  752. }
  753. else
  754. {
  755. /* Update the error code */
  756. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  757. /* Return error status */
  758. status = HAL_ERROR;
  759. }
  760. /* Release Lock */
  761. __HAL_UNLOCK(hcryp);
  762. return status;
  763. }
  764. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  765. /**
  766. * @}
  767. */
  768. /** @defgroup CRYP_Exported_Functions_Group2 Encrypt Decrypt functions
  769. * @brief processing functions.
  770. *
  771. @verbatim
  772. ==============================================================================
  773. ##### Encrypt Decrypt functions #####
  774. ==============================================================================
  775. [..] This section provides API allowing to Encrypt/Decrypt Data following
  776. Standard DES/TDES or AES, and Algorithm configured by the user:
  777. (+) Standard DES/TDES only supported by CRYP1 IP, below list of Algorithm supported :
  778. - Electronic Code Book(ECB)
  779. - Cipher Block Chaining (CBC)
  780. (+) Standard AES supported by CRYP1 IP & TinyAES, list of Algorithm supported:
  781. - Electronic Code Book(ECB)
  782. - Cipher Block Chaining (CBC)
  783. - Counter mode (CTR)
  784. - Cipher Block Chaining (CBC)
  785. - Counter mode (CTR)
  786. - Galois/counter mode (GCM)
  787. - Counter with Cipher Block Chaining-Message(CCM)
  788. [..] Three processing functions are available:
  789. (+) Polling mode : HAL_CRYP_Encrypt & HAL_CRYP_Decrypt
  790. (+) Interrupt mode : HAL_CRYP_Encrypt_IT & HAL_CRYP_Decrypt_IT
  791. (+) DMA mode : HAL_CRYP_Encrypt_DMA & HAL_CRYP_Decrypt_DMA
  792. @endverbatim
  793. * @{
  794. */
  795. /**
  796. * @brief Encryption mode.
  797. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  798. * the configuration information for CRYP module
  799. * @param Input: Pointer to the input buffer (plaintext)
  800. * @param Size: Length of the plaintext buffer in word.
  801. * @param Output: Pointer to the output buffer(ciphertext)
  802. * @param Timeout: Specify Timeout value
  803. * @retval HAL status
  804. */
  805. HAL_StatusTypeDef HAL_CRYP_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output,
  806. uint32_t Timeout)
  807. {
  808. uint32_t algo;
  809. HAL_StatusTypeDef status;
  810. if (hcryp->State == HAL_CRYP_STATE_READY)
  811. {
  812. /* Change state Busy */
  813. hcryp->State = HAL_CRYP_STATE_BUSY;
  814. /* Process locked */
  815. __HAL_LOCK(hcryp);
  816. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  817. hcryp->CrypInCount = 0U;
  818. hcryp->CrypOutCount = 0U;
  819. hcryp->pCrypInBuffPtr = Input;
  820. hcryp->pCrypOutBuffPtr = Output;
  821. /* Calculate Size parameter in Byte*/
  822. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  823. {
  824. hcryp->Size = Size * 4U;
  825. }
  826. else
  827. {
  828. hcryp->Size = Size;
  829. }
  830. #if defined (CRYP)
  831. /* Set Encryption operating mode*/
  832. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT);
  833. /* algo get algorithm selected */
  834. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  835. switch (algo)
  836. {
  837. case CRYP_DES_ECB:
  838. case CRYP_DES_CBC:
  839. case CRYP_TDES_ECB:
  840. case CRYP_TDES_CBC:
  841. /*Set Key */
  842. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  843. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  844. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  845. {
  846. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  847. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  848. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  849. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  850. }
  851. /*Set Initialization Vector (IV)*/
  852. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  853. {
  854. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  855. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  856. }
  857. /* Flush FIFO */
  858. HAL_CRYP_FIFO_FLUSH(hcryp);
  859. /* Set the phase */
  860. hcryp->Phase = CRYP_PHASE_PROCESS;
  861. /* Start DES/TDES encryption process */
  862. status = CRYP_TDES_Process(hcryp, Timeout);
  863. break;
  864. case CRYP_AES_ECB:
  865. case CRYP_AES_CBC:
  866. case CRYP_AES_CTR:
  867. /* AES encryption */
  868. status = CRYP_AES_Encrypt(hcryp, Timeout);
  869. break;
  870. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  871. case CRYP_AES_GCM:
  872. /* AES GCM encryption */
  873. status = CRYP_AESGCM_Process(hcryp, Timeout);
  874. break;
  875. case CRYP_AES_CCM:
  876. /* AES CCM encryption */
  877. status = CRYP_AESCCM_Process(hcryp, Timeout);
  878. break;
  879. #endif /* GCM CCM defined*/
  880. default:
  881. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  882. /* Change the CRYP peripheral state */
  883. hcryp->State = HAL_CRYP_STATE_READY;
  884. /* Process unlocked */
  885. __HAL_UNLOCK(hcryp);
  886. return HAL_ERROR;
  887. }
  888. #else /*AES*/
  889. /* Set the operating mode*/
  890. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  891. /* algo get algorithm selected */
  892. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  893. switch (algo)
  894. {
  895. case CRYP_AES_ECB:
  896. case CRYP_AES_CBC:
  897. case CRYP_AES_CTR:
  898. /* AES encryption */
  899. status = CRYP_AES_Encrypt(hcryp, Timeout);
  900. break;
  901. case CRYP_AES_GCM_GMAC:
  902. /* AES GCM encryption */
  903. status = CRYP_AESGCM_Process(hcryp, Timeout) ;
  904. break;
  905. case CRYP_AES_CCM:
  906. /* AES CCM encryption */
  907. status = CRYP_AESCCM_Process(hcryp, Timeout);
  908. break;
  909. default:
  910. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  911. /* Change the CRYP peripheral state */
  912. hcryp->State = HAL_CRYP_STATE_READY;
  913. /* Process unlocked */
  914. __HAL_UNLOCK(hcryp);
  915. return HAL_ERROR;
  916. }
  917. #endif /*end AES or CRYP */
  918. if (status == HAL_OK)
  919. {
  920. /* Change the CRYP peripheral state */
  921. hcryp->State = HAL_CRYP_STATE_READY;
  922. /* Process unlocked */
  923. __HAL_UNLOCK(hcryp);
  924. }
  925. }
  926. else
  927. {
  928. /* Busy error code field */
  929. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  930. return HAL_ERROR;
  931. }
  932. /* Return function status */
  933. return HAL_OK;
  934. }
  935. /**
  936. * @brief Decryption mode.
  937. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  938. * the configuration information for CRYP module
  939. * @param Input: Pointer to the input buffer (ciphertext )
  940. * @param Size: Length of the plaintext buffer in word.
  941. * @param Output: Pointer to the output buffer(plaintext)
  942. * @param Timeout: Specify Timeout value
  943. * @retval HAL status
  944. */
  945. HAL_StatusTypeDef HAL_CRYP_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output,
  946. uint32_t Timeout)
  947. {
  948. HAL_StatusTypeDef status;
  949. uint32_t algo;
  950. if (hcryp->State == HAL_CRYP_STATE_READY)
  951. {
  952. /* Change state Busy */
  953. hcryp->State = HAL_CRYP_STATE_BUSY;
  954. /* Process locked */
  955. __HAL_LOCK(hcryp);
  956. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  957. hcryp->CrypInCount = 0U;
  958. hcryp->CrypOutCount = 0U;
  959. hcryp->pCrypInBuffPtr = Input;
  960. hcryp->pCrypOutBuffPtr = Output;
  961. /* Calculate Size parameter in Byte*/
  962. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  963. {
  964. hcryp->Size = Size * 4U;
  965. }
  966. else
  967. {
  968. hcryp->Size = Size;
  969. }
  970. #if defined (CRYP)
  971. /* Set Decryption operating mode*/
  972. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT);
  973. /* algo get algorithm selected */
  974. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  975. switch (algo)
  976. {
  977. case CRYP_DES_ECB:
  978. case CRYP_DES_CBC:
  979. case CRYP_TDES_ECB:
  980. case CRYP_TDES_CBC:
  981. /*Set Key */
  982. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  983. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  984. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  985. {
  986. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  987. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  988. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  989. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  990. }
  991. /*Set Initialization Vector (IV)*/
  992. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  993. {
  994. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  995. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  996. }
  997. /* Flush FIFO */
  998. HAL_CRYP_FIFO_FLUSH(hcryp);
  999. /* Set the phase */
  1000. hcryp->Phase = CRYP_PHASE_PROCESS;
  1001. /* Start DES/TDES decryption process */
  1002. status = CRYP_TDES_Process(hcryp, Timeout);
  1003. break;
  1004. case CRYP_AES_ECB:
  1005. case CRYP_AES_CBC:
  1006. case CRYP_AES_CTR:
  1007. /* AES decryption */
  1008. status = CRYP_AES_Decrypt(hcryp, Timeout);
  1009. break;
  1010. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  1011. case CRYP_AES_GCM:
  1012. /* AES GCM decryption */
  1013. status = CRYP_AESGCM_Process(hcryp, Timeout) ;
  1014. break;
  1015. case CRYP_AES_CCM:
  1016. /* AES CCM decryption */
  1017. status = CRYP_AESCCM_Process(hcryp, Timeout);
  1018. break;
  1019. #endif /* GCM CCM defined*/
  1020. default:
  1021. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1022. /* Change the CRYP peripheral state */
  1023. hcryp->State = HAL_CRYP_STATE_READY;
  1024. /* Process unlocked */
  1025. __HAL_UNLOCK(hcryp);
  1026. return HAL_ERROR;
  1027. }
  1028. #else /*AES*/
  1029. /* Set Decryption operating mode*/
  1030. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  1031. /* algo get algorithm selected */
  1032. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1033. switch (algo)
  1034. {
  1035. case CRYP_AES_ECB:
  1036. case CRYP_AES_CBC:
  1037. case CRYP_AES_CTR:
  1038. /* AES decryption */
  1039. status = CRYP_AES_Decrypt(hcryp, Timeout);
  1040. break;
  1041. case CRYP_AES_GCM_GMAC:
  1042. /* AES GCM decryption */
  1043. status = CRYP_AESGCM_Process(hcryp, Timeout) ;
  1044. break;
  1045. case CRYP_AES_CCM:
  1046. /* AES CCM decryption */
  1047. status = CRYP_AESCCM_Process(hcryp, Timeout);
  1048. break;
  1049. default:
  1050. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1051. /* Change the CRYP peripheral state */
  1052. hcryp->State = HAL_CRYP_STATE_READY;
  1053. /* Process unlocked */
  1054. __HAL_UNLOCK(hcryp);
  1055. return HAL_ERROR;
  1056. }
  1057. #endif /* End AES or CRYP */
  1058. if (status == HAL_OK)
  1059. {
  1060. /* Change the CRYP peripheral state */
  1061. hcryp->State = HAL_CRYP_STATE_READY;
  1062. /* Process unlocked */
  1063. __HAL_UNLOCK(hcryp);
  1064. }
  1065. }
  1066. else
  1067. {
  1068. /* Busy error code field */
  1069. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1070. return HAL_ERROR;
  1071. }
  1072. /* Return function status */
  1073. return HAL_OK;
  1074. }
  1075. /**
  1076. * @brief Encryption in interrupt mode.
  1077. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1078. * the configuration information for CRYP module
  1079. * @param Input: Pointer to the input buffer (plaintext)
  1080. * @param Size: Length of the plaintext buffer in word
  1081. * @param Output: Pointer to the output buffer(ciphertext)
  1082. * @retval HAL status
  1083. */
  1084. HAL_StatusTypeDef HAL_CRYP_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  1085. {
  1086. uint32_t algo;
  1087. HAL_StatusTypeDef status = HAL_OK;
  1088. if (hcryp->State == HAL_CRYP_STATE_READY)
  1089. {
  1090. /* Change state Busy */
  1091. hcryp->State = HAL_CRYP_STATE_BUSY;
  1092. /* Process locked */
  1093. __HAL_LOCK(hcryp);
  1094. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  1095. hcryp->CrypInCount = 0U;
  1096. hcryp->CrypOutCount = 0U;
  1097. hcryp->pCrypInBuffPtr = Input;
  1098. hcryp->pCrypOutBuffPtr = Output;
  1099. /* Calculate Size parameter in Byte*/
  1100. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1101. {
  1102. hcryp->Size = Size * 4U;
  1103. }
  1104. else
  1105. {
  1106. hcryp->Size = Size;
  1107. }
  1108. #if defined (CRYP)
  1109. /* Set encryption operating mode*/
  1110. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT);
  1111. /* algo get algorithm selected */
  1112. algo = (hcryp->Instance->CR & CRYP_CR_ALGOMODE);
  1113. switch (algo)
  1114. {
  1115. case CRYP_DES_ECB:
  1116. case CRYP_DES_CBC:
  1117. case CRYP_TDES_ECB:
  1118. case CRYP_TDES_CBC:
  1119. /*Set Key */
  1120. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  1121. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  1122. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1123. {
  1124. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  1125. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  1126. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  1127. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  1128. }
  1129. /* Set the Initialization Vector*/
  1130. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1131. {
  1132. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  1133. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  1134. }
  1135. /* Flush FIFO */
  1136. HAL_CRYP_FIFO_FLUSH(hcryp);
  1137. /* Set the phase */
  1138. hcryp->Phase = CRYP_PHASE_PROCESS;
  1139. /* Enable interrupts */
  1140. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1141. /* Enable CRYP to start DES/TDES process*/
  1142. __HAL_CRYP_ENABLE(hcryp);
  1143. break;
  1144. case CRYP_AES_ECB:
  1145. case CRYP_AES_CBC:
  1146. case CRYP_AES_CTR:
  1147. status = CRYP_AES_Encrypt_IT(hcryp);
  1148. break;
  1149. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  1150. case CRYP_AES_GCM:
  1151. status = CRYP_AESGCM_Process_IT(hcryp) ;
  1152. break;
  1153. case CRYP_AES_CCM:
  1154. status = CRYP_AESCCM_Process_IT(hcryp);
  1155. break;
  1156. #endif /* GCM CCM defined*/
  1157. default:
  1158. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1159. /* Change the CRYP peripheral state */
  1160. hcryp->State = HAL_CRYP_STATE_READY;
  1161. /* Process unlocked */
  1162. __HAL_UNLOCK(hcryp);
  1163. status = HAL_ERROR;
  1164. break;
  1165. }
  1166. #else /* AES */
  1167. /* Set encryption operating mode*/
  1168. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  1169. /* algo get algorithm selected */
  1170. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1171. switch (algo)
  1172. {
  1173. case CRYP_AES_ECB:
  1174. case CRYP_AES_CBC:
  1175. case CRYP_AES_CTR:
  1176. /* AES encryption */
  1177. status = CRYP_AES_Encrypt_IT(hcryp);
  1178. break;
  1179. case CRYP_AES_GCM_GMAC:
  1180. /* AES GCM encryption */
  1181. status = CRYP_AESGCM_Process_IT(hcryp) ;
  1182. break;
  1183. case CRYP_AES_CCM:
  1184. /* AES CCM encryption */
  1185. status = CRYP_AESCCM_Process_IT(hcryp);
  1186. break;
  1187. default:
  1188. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1189. /* Change the CRYP peripheral state */
  1190. hcryp->State = HAL_CRYP_STATE_READY;
  1191. /* Process unlocked */
  1192. __HAL_UNLOCK(hcryp);
  1193. status = HAL_ERROR;
  1194. break;
  1195. }
  1196. #endif /*end AES or CRYP*/
  1197. }
  1198. else
  1199. {
  1200. /* Busy error code field */
  1201. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1202. status = HAL_ERROR;
  1203. }
  1204. /* Return function status */
  1205. return status;
  1206. }
  1207. /**
  1208. * @brief Decryption in itnterrupt mode.
  1209. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1210. * the configuration information for CRYP module
  1211. * @param Input: Pointer to the input buffer (ciphertext )
  1212. * @param Size: Length of the plaintext buffer in word.
  1213. * @param Output: Pointer to the output buffer(plaintext)
  1214. * @retval HAL status
  1215. */
  1216. HAL_StatusTypeDef HAL_CRYP_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  1217. {
  1218. uint32_t algo;
  1219. HAL_StatusTypeDef status = HAL_OK;
  1220. if (hcryp->State == HAL_CRYP_STATE_READY)
  1221. {
  1222. /* Change state Busy */
  1223. hcryp->State = HAL_CRYP_STATE_BUSY;
  1224. /* Process locked */
  1225. __HAL_LOCK(hcryp);
  1226. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  1227. hcryp->CrypInCount = 0U;
  1228. hcryp->CrypOutCount = 0U;
  1229. hcryp->pCrypInBuffPtr = Input;
  1230. hcryp->pCrypOutBuffPtr = Output;
  1231. /* Calculate Size parameter in Byte*/
  1232. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1233. {
  1234. hcryp->Size = Size * 4U;
  1235. }
  1236. else
  1237. {
  1238. hcryp->Size = Size;
  1239. }
  1240. #if defined (CRYP)
  1241. /* Set decryption operating mode*/
  1242. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT);
  1243. /* algo get algorithm selected */
  1244. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  1245. switch (algo)
  1246. {
  1247. case CRYP_DES_ECB:
  1248. case CRYP_DES_CBC:
  1249. case CRYP_TDES_ECB:
  1250. case CRYP_TDES_CBC:
  1251. /*Set Key */
  1252. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  1253. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  1254. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1255. {
  1256. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  1257. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  1258. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  1259. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  1260. }
  1261. /* Set the Initialization Vector*/
  1262. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1263. {
  1264. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  1265. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  1266. }
  1267. /* Flush FIFO */
  1268. HAL_CRYP_FIFO_FLUSH(hcryp);
  1269. /* Set the phase */
  1270. hcryp->Phase = CRYP_PHASE_PROCESS;
  1271. /* Enable interrupts */
  1272. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1273. /* Enable CRYP and start DES/TDES process*/
  1274. __HAL_CRYP_ENABLE(hcryp);
  1275. break;
  1276. case CRYP_AES_ECB:
  1277. case CRYP_AES_CBC:
  1278. case CRYP_AES_CTR:
  1279. /* AES decryption */
  1280. status = CRYP_AES_Decrypt_IT(hcryp);
  1281. break;
  1282. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  1283. case CRYP_AES_GCM:
  1284. /* AES GCM decryption */
  1285. status = CRYP_AESGCM_Process_IT(hcryp) ;
  1286. break;
  1287. case CRYP_AES_CCM:
  1288. /* AES CCMdecryption */
  1289. status = CRYP_AESCCM_Process_IT(hcryp);
  1290. break;
  1291. #endif /* GCM CCM defined*/
  1292. default:
  1293. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1294. /* Change the CRYP peripheral state */
  1295. hcryp->State = HAL_CRYP_STATE_READY;
  1296. /* Process unlocked */
  1297. __HAL_UNLOCK(hcryp);
  1298. status = HAL_ERROR;
  1299. break;
  1300. }
  1301. #else /*AES*/
  1302. /* Set decryption operating mode*/
  1303. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  1304. /* algo get algorithm selected */
  1305. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1306. switch (algo)
  1307. {
  1308. case CRYP_AES_ECB:
  1309. case CRYP_AES_CBC:
  1310. case CRYP_AES_CTR:
  1311. /* AES decryption */
  1312. status = CRYP_AES_Decrypt_IT(hcryp);
  1313. break;
  1314. case CRYP_AES_GCM_GMAC:
  1315. /* AES GCM decryption */
  1316. status = CRYP_AESGCM_Process_IT(hcryp) ;
  1317. break;
  1318. case CRYP_AES_CCM:
  1319. /* AES CCM decryption */
  1320. status = CRYP_AESCCM_Process_IT(hcryp);
  1321. break;
  1322. default:
  1323. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1324. /* Change the CRYP peripheral state */
  1325. hcryp->State = HAL_CRYP_STATE_READY;
  1326. /* Process unlocked */
  1327. __HAL_UNLOCK(hcryp);
  1328. status = HAL_ERROR;
  1329. break;
  1330. }
  1331. #endif /* End AES or CRYP */
  1332. }
  1333. else
  1334. {
  1335. /* Busy error code field */
  1336. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1337. status = HAL_ERROR;
  1338. }
  1339. /* Return function status */
  1340. return status;
  1341. }
  1342. /**
  1343. * @brief Encryption in DMA mode.
  1344. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1345. * the configuration information for CRYP module
  1346. * @param Input: Pointer to the input buffer (plaintext)
  1347. * @param Size: Length of the plaintext buffer in word.
  1348. * @param Output: Pointer to the output buffer(ciphertext)
  1349. * @retval HAL status
  1350. */
  1351. HAL_StatusTypeDef HAL_CRYP_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  1352. {
  1353. uint32_t algo;
  1354. HAL_StatusTypeDef status = HAL_OK;
  1355. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  1356. if (hcryp->State == HAL_CRYP_STATE_READY)
  1357. {
  1358. /* Change state Busy */
  1359. hcryp->State = HAL_CRYP_STATE_BUSY;
  1360. /* Process locked */
  1361. __HAL_LOCK(hcryp);
  1362. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr and pCrypOutBuffPtr parameters*/
  1363. hcryp->CrypInCount = 0U;
  1364. hcryp->CrypOutCount = 0U;
  1365. hcryp->pCrypInBuffPtr = Input;
  1366. hcryp->pCrypOutBuffPtr = Output;
  1367. /* Calculate Size parameter in Byte*/
  1368. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1369. {
  1370. hcryp->Size = Size * 4U;
  1371. }
  1372. else
  1373. {
  1374. hcryp->Size = Size;
  1375. }
  1376. #if defined (CRYP)
  1377. /* Set encryption operating mode*/
  1378. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_ENCRYPT);
  1379. /* algo get algorithm selected */
  1380. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  1381. switch (algo)
  1382. {
  1383. case CRYP_DES_ECB:
  1384. case CRYP_DES_CBC:
  1385. case CRYP_TDES_ECB:
  1386. case CRYP_TDES_CBC:
  1387. /*Set Key */
  1388. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  1389. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  1390. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1391. {
  1392. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  1393. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  1394. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  1395. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  1396. }
  1397. /* Set the Initialization Vector*/
  1398. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1399. {
  1400. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  1401. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  1402. }
  1403. /* Flush FIFO */
  1404. HAL_CRYP_FIFO_FLUSH(hcryp);
  1405. /* Set the phase */
  1406. hcryp->Phase = CRYP_PHASE_PROCESS;
  1407. /* Start DMA process transfer for DES/TDES */
  1408. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)(hcryp->Size) / 4U),
  1409. (uint32_t)(hcryp->pCrypOutBuffPtr));
  1410. break;
  1411. case CRYP_AES_ECB:
  1412. case CRYP_AES_CBC:
  1413. case CRYP_AES_CTR:
  1414. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  1415. {
  1416. if (hcryp->KeyIVConfig == 1U)
  1417. {
  1418. /* If the Key and IV configuration has to be done only once
  1419. and if it has already been done, skip it */
  1420. DoKeyIVConfig = 0U;
  1421. }
  1422. else
  1423. {
  1424. /* If the Key and IV configuration has to be done only once
  1425. and if it has not been done already, do it and set KeyIVConfig
  1426. to keep track it won't have to be done again next time */
  1427. hcryp->KeyIVConfig = 1U;
  1428. }
  1429. }
  1430. if (DoKeyIVConfig == 1U)
  1431. {
  1432. /* Set the Key*/
  1433. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1434. /* Set the Initialization Vector*/
  1435. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1436. {
  1437. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  1438. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1U);
  1439. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2U);
  1440. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3U);
  1441. }
  1442. } /* if (DoKeyIVConfig == 1U) */
  1443. /* Set the phase */
  1444. hcryp->Phase = CRYP_PHASE_PROCESS;
  1445. /* Start DMA process transfer for AES */
  1446. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)(hcryp->Size) / 4U),
  1447. (uint32_t)(hcryp->pCrypOutBuffPtr));
  1448. break;
  1449. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  1450. case CRYP_AES_GCM:
  1451. /* AES GCM encryption */
  1452. status = CRYP_AESGCM_Process_DMA(hcryp) ;
  1453. break;
  1454. case CRYP_AES_CCM:
  1455. /* AES CCM encryption */
  1456. status = CRYP_AESCCM_Process_DMA(hcryp);
  1457. break;
  1458. #endif /* GCM CCM defined*/
  1459. default:
  1460. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1461. /* Change the CRYP peripheral state */
  1462. hcryp->State = HAL_CRYP_STATE_READY;
  1463. /* Process unlocked */
  1464. __HAL_UNLOCK(hcryp);
  1465. status = HAL_ERROR;
  1466. break;
  1467. }
  1468. #else /*AES*/
  1469. /* Set encryption operating mode*/
  1470. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  1471. /* algo get algorithm selected */
  1472. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1473. switch (algo)
  1474. {
  1475. case CRYP_AES_ECB:
  1476. case CRYP_AES_CBC:
  1477. case CRYP_AES_CTR:
  1478. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  1479. {
  1480. if (hcryp->KeyIVConfig == 1U)
  1481. {
  1482. /* If the Key and IV configuration has to be done only once
  1483. and if it has already been done, skip it */
  1484. DoKeyIVConfig = 0U;
  1485. }
  1486. else
  1487. {
  1488. /* If the Key and IV configuration has to be done only once
  1489. and if it has not been done already, do it and set KeyIVConfig
  1490. to keep track it won't have to be done again next time */
  1491. hcryp->KeyIVConfig = 1U;
  1492. }
  1493. }
  1494. if (DoKeyIVConfig == 1U)
  1495. {
  1496. /* Set the Key*/
  1497. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  1498. /* Set the Initialization Vector*/
  1499. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  1500. {
  1501. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  1502. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  1503. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  1504. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  1505. }
  1506. } /* if (DoKeyIVConfig == 1U) */
  1507. /* Set the phase */
  1508. hcryp->Phase = CRYP_PHASE_PROCESS;
  1509. /* Start DMA process transfer for AES */
  1510. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr),
  1511. (hcryp->Size / 4U), (uint32_t)(hcryp->pCrypOutBuffPtr));
  1512. break;
  1513. case CRYP_AES_GCM_GMAC:
  1514. /* AES GCM encryption */
  1515. status = CRYP_AESGCM_Process_DMA(hcryp) ;
  1516. break;
  1517. case CRYP_AES_CCM:
  1518. /* AES CCM encryption */
  1519. status = CRYP_AESCCM_Process_DMA(hcryp);
  1520. break;
  1521. default:
  1522. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1523. /* Change the CRYP peripheral state */
  1524. hcryp->State = HAL_CRYP_STATE_READY;
  1525. /* Process unlocked */
  1526. __HAL_UNLOCK(hcryp);
  1527. status = HAL_ERROR;
  1528. break;
  1529. }
  1530. #endif /* End AES or CRYP */
  1531. }
  1532. else
  1533. {
  1534. /* Busy error code field */
  1535. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1536. status = HAL_ERROR;
  1537. }
  1538. /* Return function status */
  1539. return status;
  1540. }
  1541. /**
  1542. * @brief Decryption in DMA mode.
  1543. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1544. * the configuration information for CRYP module
  1545. * @param Input: Pointer to the input buffer (ciphertext )
  1546. * @param Size: Length of the plaintext buffer in word
  1547. * @param Output: Pointer to the output buffer(plaintext)
  1548. * @retval HAL status
  1549. */
  1550. HAL_StatusTypeDef HAL_CRYP_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint32_t *Input, uint16_t Size, uint32_t *Output)
  1551. {
  1552. uint32_t algo;
  1553. HAL_StatusTypeDef status = HAL_OK;
  1554. if (hcryp->State == HAL_CRYP_STATE_READY)
  1555. {
  1556. /* Change state Busy */
  1557. hcryp->State = HAL_CRYP_STATE_BUSY;
  1558. /* Process locked */
  1559. __HAL_LOCK(hcryp);
  1560. /* Reset CrypInCount, CrypOutCount and Initialize pCrypInBuffPtr, pCrypOutBuffPtr and Size parameters*/
  1561. hcryp->CrypInCount = 0U;
  1562. hcryp->CrypOutCount = 0U;
  1563. hcryp->pCrypInBuffPtr = Input;
  1564. hcryp->pCrypOutBuffPtr = Output;
  1565. /* Calculate Size parameter in Byte*/
  1566. if (hcryp->Init.DataWidthUnit == CRYP_DATAWIDTHUNIT_WORD)
  1567. {
  1568. hcryp->Size = Size * 4U;
  1569. }
  1570. else
  1571. {
  1572. hcryp->Size = Size;
  1573. }
  1574. #if defined (CRYP)
  1575. /* Set decryption operating mode*/
  1576. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGODIR, CRYP_OPERATINGMODE_DECRYPT);
  1577. /* algo get algorithm selected */
  1578. algo = hcryp->Instance->CR & CRYP_CR_ALGOMODE;
  1579. switch (algo)
  1580. {
  1581. case CRYP_DES_ECB:
  1582. case CRYP_DES_CBC:
  1583. case CRYP_TDES_ECB:
  1584. case CRYP_TDES_CBC:
  1585. /*Set Key */
  1586. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  1587. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  1588. if ((hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1589. {
  1590. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  1591. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  1592. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  1593. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  1594. }
  1595. /* Set the Initialization Vector*/
  1596. if ((hcryp->Init.Algorithm == CRYP_DES_CBC) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1597. {
  1598. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  1599. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  1600. }
  1601. /* Flush FIFO */
  1602. HAL_CRYP_FIFO_FLUSH(hcryp);
  1603. /* Set the phase */
  1604. hcryp->Phase = CRYP_PHASE_PROCESS;
  1605. /* Start DMA process transfer for DES/TDES */
  1606. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)(hcryp->Size) / 4U),
  1607. (uint32_t)(hcryp->pCrypOutBuffPtr));
  1608. break;
  1609. case CRYP_AES_ECB:
  1610. case CRYP_AES_CBC:
  1611. case CRYP_AES_CTR:
  1612. /* AES decryption */
  1613. status = CRYP_AES_Decrypt_DMA(hcryp);
  1614. break;
  1615. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  1616. case CRYP_AES_GCM:
  1617. /* AES GCM decryption */
  1618. status = CRYP_AESGCM_Process_DMA(hcryp) ;
  1619. break;
  1620. case CRYP_AES_CCM:
  1621. /* AES CCM decryption */
  1622. status = CRYP_AESCCM_Process_DMA(hcryp);
  1623. break;
  1624. #endif /* GCM CCM defined*/
  1625. default:
  1626. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1627. /* Change the CRYP peripheral state */
  1628. hcryp->State = HAL_CRYP_STATE_READY;
  1629. /* Process unlocked */
  1630. __HAL_UNLOCK(hcryp);
  1631. status = HAL_ERROR;
  1632. break;
  1633. }
  1634. #else /*AES*/
  1635. /* Set decryption operating mode*/
  1636. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  1637. /* algo get algorithm selected */
  1638. algo = hcryp->Instance->CR & AES_CR_CHMOD;
  1639. switch (algo)
  1640. {
  1641. case CRYP_AES_ECB:
  1642. case CRYP_AES_CBC:
  1643. case CRYP_AES_CTR:
  1644. /* AES decryption */
  1645. status = CRYP_AES_Decrypt_DMA(hcryp);
  1646. break;
  1647. case CRYP_AES_GCM_GMAC:
  1648. /* AES GCM decryption */
  1649. status = CRYP_AESGCM_Process_DMA(hcryp) ;
  1650. break;
  1651. case CRYP_AES_CCM:
  1652. /* AES CCM decryption */
  1653. status = CRYP_AESCCM_Process_DMA(hcryp);
  1654. break;
  1655. default:
  1656. hcryp->ErrorCode |= HAL_CRYP_ERROR_NOT_SUPPORTED;
  1657. /* Change the CRYP peripheral state */
  1658. hcryp->State = HAL_CRYP_STATE_READY;
  1659. /* Process unlocked */
  1660. __HAL_UNLOCK(hcryp);
  1661. status = HAL_ERROR;
  1662. break;
  1663. }
  1664. #endif /* End AES or CRYP */
  1665. }
  1666. else
  1667. {
  1668. /* Busy error code field */
  1669. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  1670. status = HAL_ERROR;
  1671. }
  1672. /* Return function status */
  1673. return status;
  1674. }
  1675. /**
  1676. * @}
  1677. */
  1678. /** @defgroup CRYP_Exported_Functions_Group3 CRYP IRQ handler management
  1679. * @brief CRYP IRQ handler.
  1680. *
  1681. @verbatim
  1682. ==============================================================================
  1683. ##### CRYP IRQ handler management #####
  1684. ==============================================================================
  1685. [..] This section provides CRYP IRQ handler and callback functions.
  1686. (+) HAL_CRYP_IRQHandler CRYP interrupt request
  1687. (+) HAL_CRYP_InCpltCallback input data transfer complete callback
  1688. (+) HAL_CRYP_OutCpltCallback output data transfer complete callback
  1689. (+) HAL_CRYP_ErrorCallback CRYP error callback
  1690. (+) HAL_CRYP_GetState return the CRYP state
  1691. (+) HAL_CRYP_GetError return the CRYP error code
  1692. @endverbatim
  1693. * @{
  1694. */
  1695. /**
  1696. * @brief This function handles cryptographic interrupt request.
  1697. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1698. * the configuration information for CRYP module
  1699. * @retval None
  1700. */
  1701. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
  1702. {
  1703. #if defined (CRYP)
  1704. uint32_t itstatus = hcryp->Instance->MISR;
  1705. if ((itstatus & (CRYP_IT_INI | CRYP_IT_OUTI)) != 0U)
  1706. {
  1707. if ((hcryp->Init.Algorithm == CRYP_DES_ECB) || (hcryp->Init.Algorithm == CRYP_DES_CBC)
  1708. || (hcryp->Init.Algorithm == CRYP_TDES_ECB) || (hcryp->Init.Algorithm == CRYP_TDES_CBC))
  1709. {
  1710. CRYP_TDES_IT(hcryp); /* DES or TDES*/
  1711. }
  1712. else if ((hcryp->Init.Algorithm == CRYP_AES_ECB) || (hcryp->Init.Algorithm == CRYP_AES_CBC)
  1713. || (hcryp->Init.Algorithm == CRYP_AES_CTR))
  1714. {
  1715. CRYP_AES_IT(hcryp); /*AES*/
  1716. }
  1717. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  1718. else if ((hcryp->Init.Algorithm == CRYP_AES_GCM) || (hcryp->Init.Algorithm == CRYP_CR_ALGOMODE_AES_CCM))
  1719. {
  1720. /* if header phase */
  1721. if ((hcryp->Instance->CR & CRYP_PHASE_HEADER) == CRYP_PHASE_HEADER)
  1722. {
  1723. CRYP_GCMCCM_SetHeaderPhase_IT(hcryp);
  1724. }
  1725. else /* if payload phase */
  1726. {
  1727. CRYP_GCMCCM_SetPayloadPhase_IT(hcryp);
  1728. }
  1729. }
  1730. #endif /* GCM CCM defined*/
  1731. else
  1732. {
  1733. /* Nothing to do */
  1734. }
  1735. }
  1736. #else /*AES*/
  1737. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_CCF) != RESET)
  1738. {
  1739. if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_CCFIE) != RESET)
  1740. {
  1741. /* Clear computation complete flag */
  1742. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1743. if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)
  1744. {
  1745. /* if header phase */
  1746. if ((hcryp->Instance->CR & CRYP_PHASE_HEADER) == CRYP_PHASE_HEADER)
  1747. {
  1748. CRYP_GCMCCM_SetHeaderPhase_IT(hcryp);
  1749. }
  1750. else /* if payload phase */
  1751. {
  1752. CRYP_GCMCCM_SetPayloadPhase_IT(hcryp);
  1753. }
  1754. }
  1755. else if (hcryp->Init.Algorithm == CRYP_AES_CCM)
  1756. {
  1757. /* if header phase */
  1758. if (hcryp->Init.HeaderSize >= hcryp->CrypHeaderCount)
  1759. {
  1760. CRYP_GCMCCM_SetHeaderPhase_IT(hcryp);
  1761. }
  1762. else /* if payload phase */
  1763. {
  1764. CRYP_GCMCCM_SetPayloadPhase_IT(hcryp);
  1765. }
  1766. }
  1767. else /* AES Algorithm ECB,CBC or CTR*/
  1768. {
  1769. CRYP_AES_IT(hcryp);
  1770. }
  1771. }
  1772. }
  1773. /* Check if error occurred */
  1774. if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_ERRIE) != RESET)
  1775. {
  1776. /* If write Error occurred */
  1777. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_WRERR) != RESET)
  1778. {
  1779. hcryp->ErrorCode |= HAL_CRYP_ERROR_WRITE;
  1780. }
  1781. /* If read Error occurred */
  1782. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_RDERR) != RESET)
  1783. {
  1784. hcryp->ErrorCode |= HAL_CRYP_ERROR_READ;
  1785. }
  1786. }
  1787. #endif /* End AES or CRYP */
  1788. }
  1789. /**
  1790. * @brief Return the CRYP error code.
  1791. * @param hcryp : pointer to a CRYP_HandleTypeDef structure that contains
  1792. * the configuration information for the CRYP IP
  1793. * @retval CRYP error code
  1794. */
  1795. uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
  1796. {
  1797. return hcryp->ErrorCode;
  1798. }
  1799. /**
  1800. * @brief Returns the CRYP state.
  1801. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1802. * the configuration information for CRYP module.
  1803. * @retval HAL state
  1804. */
  1805. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
  1806. {
  1807. return hcryp->State;
  1808. }
  1809. /**
  1810. * @brief Input FIFO transfer completed callback.
  1811. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1812. * the configuration information for CRYP module.
  1813. * @retval None
  1814. */
  1815. __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
  1816. {
  1817. /* Prevent unused argument(s) compilation warning */
  1818. UNUSED(hcryp);
  1819. /* NOTE : This function should not be modified, when the callback is needed,
  1820. the HAL_CRYP_InCpltCallback can be implemented in the user file
  1821. */
  1822. }
  1823. /**
  1824. * @brief Output FIFO transfer completed callback.
  1825. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1826. * the configuration information for CRYP module.
  1827. * @retval None
  1828. */
  1829. __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
  1830. {
  1831. /* Prevent unused argument(s) compilation warning */
  1832. UNUSED(hcryp);
  1833. /* NOTE : This function should not be modified, when the callback is needed,
  1834. the HAL_CRYP_OutCpltCallback can be implemented in the user file
  1835. */
  1836. }
  1837. /**
  1838. * @brief CRYP error callback.
  1839. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1840. * the configuration information for CRYP module.
  1841. * @retval None
  1842. */
  1843. __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
  1844. {
  1845. /* Prevent unused argument(s) compilation warning */
  1846. UNUSED(hcryp);
  1847. /* NOTE : This function Should not be modified, when the callback is needed,
  1848. the HAL_CRYP_ErrorCallback could be implemented in the user file
  1849. */
  1850. }
  1851. /**
  1852. * @}
  1853. */
  1854. /* Private functions ---------------------------------------------------------*/
  1855. /** @addtogroup CRYP_Private_Functions
  1856. * @{
  1857. */
  1858. #if defined (CRYP)
  1859. /**
  1860. * @brief Encryption in ECB/CBC Algorithm with DES/TDES standard.
  1861. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1862. * the configuration information for CRYP module
  1863. * @param Timeout: specify Timeout value
  1864. * @retval HAL status
  1865. */
  1866. static HAL_StatusTypeDef CRYP_TDES_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  1867. {
  1868. uint32_t temp[2]; /* Temporary CrypOutBuff */
  1869. uint16_t incount; /* Temporary CrypInCount Value */
  1870. uint16_t outcount; /* Temporary CrypOutCount Value */
  1871. uint32_t i;
  1872. /* Enable CRYP */
  1873. __HAL_CRYP_ENABLE(hcryp);
  1874. /*Temporary CrypOutCount Value*/
  1875. outcount = hcryp->CrypOutCount;
  1876. /*Start processing*/
  1877. while ((hcryp->CrypInCount < (hcryp->Size / 4U)) && (outcount < (hcryp->Size / 4U)))
  1878. {
  1879. /* Temporary CrypInCount Value */
  1880. incount = hcryp->CrypInCount;
  1881. /* Write plain data and get cipher data */
  1882. if (((hcryp->Instance->SR & CRYP_FLAG_IFNF) != 0x0U) && (incount < (hcryp->Size / 4U)))
  1883. {
  1884. /* Write the input block in the IN FIFO */
  1885. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  1886. hcryp->CrypInCount++;
  1887. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  1888. hcryp->CrypInCount++;
  1889. }
  1890. /* Wait for OFNE flag to be raised */
  1891. if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  1892. {
  1893. /* Disable the CRYP peripheral clock */
  1894. __HAL_CRYP_DISABLE(hcryp);
  1895. /* Change state & errorCode*/
  1896. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  1897. hcryp->State = HAL_CRYP_STATE_READY;
  1898. /* Process unlocked */
  1899. __HAL_UNLOCK(hcryp);
  1900. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  1901. /*Call registered error callback*/
  1902. hcryp->ErrorCallback(hcryp);
  1903. #else
  1904. /*Call legacy weak error callback*/
  1905. HAL_CRYP_ErrorCallback(hcryp);
  1906. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  1907. }
  1908. /*Temporary CrypOutCount Value*/
  1909. outcount = hcryp->CrypOutCount;
  1910. if (((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) && (outcount < (hcryp->Size / 4U)))
  1911. {
  1912. /* Read the output block from the Output FIFO and put them in temporary Buffer
  1913. then get CrypOutBuff from temporary buffer */
  1914. for (i = 0U; i < 2U; i++)
  1915. {
  1916. temp[i] = hcryp->Instance->DOUT;
  1917. }
  1918. i = 0U;
  1919. while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 2U))
  1920. {
  1921. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  1922. hcryp->CrypOutCount++;
  1923. i++;
  1924. }
  1925. }
  1926. /*Temporary CrypOutCount Value*/
  1927. outcount = hcryp->CrypOutCount;
  1928. }
  1929. /* Disable CRYP */
  1930. __HAL_CRYP_DISABLE(hcryp);
  1931. /* Change the CRYP state */
  1932. hcryp->State = HAL_CRYP_STATE_READY;
  1933. /* Return function status */
  1934. return HAL_OK;
  1935. }
  1936. /**
  1937. * @brief CRYP block input/output data handling under interruption with DES/TDES standard.
  1938. * @note The function is called under interruption only, once
  1939. * interruptions have been enabled by CRYP_Decrypt_IT() and CRYP_Encrypt_IT().
  1940. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1941. * the configuration information for CRYP module.
  1942. * @retval none
  1943. */
  1944. static void CRYP_TDES_IT(CRYP_HandleTypeDef *hcryp)
  1945. {
  1946. uint32_t temp[2]; /* Temporary CrypOutBuff */
  1947. uint32_t i;
  1948. if (hcryp->State == HAL_CRYP_STATE_BUSY)
  1949. {
  1950. if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI) != 0x0U)
  1951. {
  1952. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_FLAG_INRIS) != 0x0U)
  1953. {
  1954. /* Write input block in the IN FIFO */
  1955. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  1956. hcryp->CrypInCount++;
  1957. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  1958. hcryp->CrypInCount++;
  1959. if (hcryp->CrypInCount == ((uint16_t)(hcryp->Size) / 4U))
  1960. {
  1961. /* Disable interruption */
  1962. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1963. /* Call the input data transfer complete callback */
  1964. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  1965. /*Call registered Input complete callback*/
  1966. hcryp->InCpltCallback(hcryp);
  1967. #else
  1968. /*Call legacy weak Input complete callback*/
  1969. HAL_CRYP_InCpltCallback(hcryp);
  1970. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  1971. }
  1972. }
  1973. }
  1974. if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI) != 0x0U)
  1975. {
  1976. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_FLAG_OUTRIS) != 0x0U)
  1977. {
  1978. /* Read the output block from the Output FIFO and put them in temporary Buffer
  1979. then get CrypOutBuff from temporary buffer */
  1980. for (i = 0U; i < 2U; i++)
  1981. {
  1982. temp[i] = hcryp->Instance->DOUT;
  1983. }
  1984. i = 0U;
  1985. while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 2U))
  1986. {
  1987. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  1988. hcryp->CrypOutCount++;
  1989. i++;
  1990. }
  1991. if (hcryp->CrypOutCount == ((uint16_t)(hcryp->Size) / 4U))
  1992. {
  1993. /* Disable interruption */
  1994. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1995. /* Disable CRYP */
  1996. __HAL_CRYP_DISABLE(hcryp);
  1997. /* Process unlocked */
  1998. __HAL_UNLOCK(hcryp);
  1999. /* Change the CRYP state */
  2000. hcryp->State = HAL_CRYP_STATE_READY;
  2001. /* Call output transfer complete callback */
  2002. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2003. /*Call registered Output complete callback*/
  2004. hcryp->OutCpltCallback(hcryp);
  2005. #else
  2006. /*Call legacy weak Output complete callback*/
  2007. HAL_CRYP_OutCpltCallback(hcryp);
  2008. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2009. }
  2010. }
  2011. }
  2012. }
  2013. else
  2014. {
  2015. /* Process unlocked */
  2016. __HAL_UNLOCK(hcryp);
  2017. /* Busy error code field */
  2018. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  2019. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2020. /*Call registered error callback*/
  2021. hcryp->ErrorCallback(hcryp);
  2022. #else
  2023. /*Call legacy weak error callback*/
  2024. HAL_CRYP_ErrorCallback(hcryp);
  2025. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2026. }
  2027. }
  2028. #endif /* CRYP */
  2029. /**
  2030. * @brief Encryption in ECB/CBC & CTR Algorithm with AES Standard
  2031. * @param hcryp: pointer to a CRYP_HandleTypeDef structure
  2032. * @param Timeout: specify Timeout value
  2033. * @retval HAL status
  2034. */
  2035. static HAL_StatusTypeDef CRYP_AES_Encrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  2036. {
  2037. uint16_t outcount; /* Temporary CrypOutCount Value */
  2038. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  2039. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  2040. {
  2041. if (hcryp->KeyIVConfig == 1U)
  2042. {
  2043. /* If the Key and IV configuration has to be done only once
  2044. and if it has already been done, skip it */
  2045. DoKeyIVConfig = 0U;
  2046. }
  2047. else
  2048. {
  2049. /* If the Key and IV configuration has to be done only once
  2050. and if it has not been done already, do it and set KeyIVConfig
  2051. to keep track it won't have to be done again next time */
  2052. hcryp->KeyIVConfig = 1U;
  2053. }
  2054. }
  2055. if (DoKeyIVConfig == 1U)
  2056. {
  2057. /* Set the Key*/
  2058. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2059. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  2060. {
  2061. /* Set the Initialization Vector*/
  2062. #if defined (AES)
  2063. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2064. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2065. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2066. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2067. #else /* CRYP */
  2068. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  2069. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2070. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2071. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2072. #endif /* End AES or CRYP */
  2073. }
  2074. } /* if (DoKeyIVConfig == 1U) */
  2075. /* Set the phase */
  2076. hcryp->Phase = CRYP_PHASE_PROCESS;
  2077. /* Enable CRYP */
  2078. __HAL_CRYP_ENABLE(hcryp);
  2079. /*Temporary CrypOutCount Value*/
  2080. outcount = hcryp->CrypOutCount;
  2081. while ((hcryp->CrypInCount < (hcryp->Size / 4U)) && (outcount < (hcryp->Size / 4U)))
  2082. {
  2083. /* Write plain Ddta and get cipher data */
  2084. CRYP_AES_ProcessData(hcryp, Timeout);
  2085. /*Temporary CrypOutCount Value*/
  2086. outcount = hcryp->CrypOutCount;
  2087. }
  2088. /* Disable CRYP */
  2089. __HAL_CRYP_DISABLE(hcryp);
  2090. /* Change the CRYP state */
  2091. hcryp->State = HAL_CRYP_STATE_READY;
  2092. /* Return function status */
  2093. return HAL_OK;
  2094. }
  2095. /**
  2096. * @brief Encryption in ECB/CBC & CTR mode with AES Standard using interrupt mode
  2097. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2098. * the configuration information for CRYP module
  2099. * @retval HAL status
  2100. */
  2101. static HAL_StatusTypeDef CRYP_AES_Encrypt_IT(CRYP_HandleTypeDef *hcryp)
  2102. {
  2103. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  2104. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  2105. {
  2106. if (hcryp->KeyIVConfig == 1U)
  2107. {
  2108. /* If the Key and IV configuration has to be done only once
  2109. and if it has already been done, skip it */
  2110. DoKeyIVConfig = 0U;
  2111. }
  2112. else
  2113. {
  2114. /* If the Key and IV configuration has to be done only once
  2115. and if it has not been done already, do it and set KeyIVConfig
  2116. to keep track it won't have to be done again next time */
  2117. hcryp->KeyIVConfig = 1U;
  2118. }
  2119. }
  2120. if (DoKeyIVConfig == 1U)
  2121. {
  2122. /* Set the Key*/
  2123. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2124. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  2125. {
  2126. /* Set the Initialization Vector*/
  2127. #if defined (AES)
  2128. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2129. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2130. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2131. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2132. #else /* CRYP */
  2133. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  2134. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2135. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2136. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2137. #endif /* End AES or CRYP */
  2138. }
  2139. } /* if (DoKeyIVConfig == 1U) */
  2140. /* Set the phase */
  2141. hcryp->Phase = CRYP_PHASE_PROCESS;
  2142. if (hcryp->Size != 0U)
  2143. {
  2144. #if defined (AES)
  2145. /* Enable computation complete flag and error interrupts */
  2146. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  2147. /* Enable CRYP */
  2148. __HAL_CRYP_ENABLE(hcryp);
  2149. /* Increment the pointer before writing the input block in the IN FIFO to make sure that
  2150. when Computation Completed IRQ fires, the hcryp->CrypInCount has always a consistent value
  2151. and it is ready for the next operation. */
  2152. hcryp->CrypInCount++;
  2153. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
  2154. hcryp->CrypInCount++;
  2155. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
  2156. hcryp->CrypInCount++;
  2157. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
  2158. hcryp->CrypInCount++;
  2159. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
  2160. #else /* CRYP */
  2161. /* Enable interrupts */
  2162. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2163. /* Enable CRYP */
  2164. __HAL_CRYP_ENABLE(hcryp);
  2165. #endif /* End AES or CRYP */
  2166. }
  2167. else
  2168. {
  2169. /* Change the CRYP state */
  2170. hcryp->State = HAL_CRYP_STATE_READY;
  2171. /* Process unlocked */
  2172. __HAL_UNLOCK(hcryp);
  2173. }
  2174. /* Return function status */
  2175. return HAL_OK;
  2176. }
  2177. /**
  2178. * @brief Decryption in ECB/CBC & CTR mode with AES Standard
  2179. * @param hcryp: pointer to a CRYP_HandleTypeDef structure
  2180. * @param Timeout: Specify Timeout value
  2181. * @retval HAL status
  2182. */
  2183. static HAL_StatusTypeDef CRYP_AES_Decrypt(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  2184. {
  2185. uint16_t outcount; /* Temporary CrypOutCount Value */
  2186. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  2187. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  2188. {
  2189. if (hcryp->KeyIVConfig == 1U)
  2190. {
  2191. /* If the Key and IV configuration has to be done only once
  2192. and if it has already been done, skip it */
  2193. DoKeyIVConfig = 0U;
  2194. }
  2195. else
  2196. {
  2197. /* If the Key and IV configuration has to be done only once
  2198. and if it has not been done already, do it and set KeyIVConfig
  2199. to keep track it won't have to be done again next time */
  2200. hcryp->KeyIVConfig = 1U;
  2201. }
  2202. }
  2203. if (DoKeyIVConfig == 1U)
  2204. {
  2205. /* Key preparation for ECB/CBC */
  2206. if (hcryp->Init.Algorithm != CRYP_AES_CTR)
  2207. {
  2208. #if defined (AES)
  2209. if (hcryp->AutoKeyDerivation == DISABLE)/*Mode 2 Key preparation*/
  2210. {
  2211. /* Set key preparation for decryption operating mode*/
  2212. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION);
  2213. /* Set the Key*/
  2214. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2215. /* Enable CRYP */
  2216. __HAL_CRYP_ENABLE(hcryp);
  2217. /* Wait for CCF flag to be raised */
  2218. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  2219. {
  2220. /* Disable the CRYP peripheral clock */
  2221. __HAL_CRYP_DISABLE(hcryp);
  2222. /* Change state & error code*/
  2223. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2224. hcryp->State = HAL_CRYP_STATE_READY;
  2225. /* Process unlocked */
  2226. __HAL_UNLOCK(hcryp);
  2227. return HAL_ERROR;
  2228. }
  2229. /* Clear CCF Flag */
  2230. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2231. /* Return to decryption operating mode(Mode 3)*/
  2232. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  2233. }
  2234. else /*Mode 4 : decryption & Key preparation*/
  2235. {
  2236. /* Set the Key*/
  2237. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2238. /* Set decryption & Key preparation operating mode*/
  2239. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT);
  2240. }
  2241. #else /* CRYP */
  2242. /* change ALGOMODE to key preparation for decryption*/
  2243. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY);
  2244. /* Set the Key*/
  2245. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2246. /* Enable CRYP */
  2247. __HAL_CRYP_ENABLE(hcryp);
  2248. /* Wait for BUSY flag to be raised */
  2249. if (CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK)
  2250. {
  2251. /* Disable the CRYP peripheral clock */
  2252. __HAL_CRYP_DISABLE(hcryp);
  2253. /* Change state */
  2254. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2255. hcryp->State = HAL_CRYP_STATE_READY;
  2256. /* Process unlocked */
  2257. __HAL_UNLOCK(hcryp);
  2258. return HAL_ERROR;
  2259. }
  2260. /* Turn back to ALGOMODE of the configuration */
  2261. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm);
  2262. #endif /* End AES or CRYP */
  2263. }
  2264. else /*Algorithm CTR */
  2265. {
  2266. /* Set the Key*/
  2267. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2268. }
  2269. /* Set IV */
  2270. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  2271. {
  2272. /* Set the Initialization Vector*/
  2273. #if defined (AES)
  2274. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2275. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2276. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2277. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2278. #else /* CRYP */
  2279. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  2280. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2281. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2282. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2283. #endif /* End AES or CRYP */
  2284. }
  2285. } /* if (DoKeyIVConfig == 1U) */
  2286. /* Set the phase */
  2287. hcryp->Phase = CRYP_PHASE_PROCESS;
  2288. /* Enable CRYP */
  2289. __HAL_CRYP_ENABLE(hcryp);
  2290. /*Temporary CrypOutCount Value*/
  2291. outcount = hcryp->CrypOutCount;
  2292. while ((hcryp->CrypInCount < (hcryp->Size / 4U)) && (outcount < (hcryp->Size / 4U)))
  2293. {
  2294. /* Write plain data and get cipher data */
  2295. CRYP_AES_ProcessData(hcryp, Timeout);
  2296. /*Temporary CrypOutCount Value*/
  2297. outcount = hcryp->CrypOutCount;
  2298. }
  2299. /* Disable CRYP */
  2300. __HAL_CRYP_DISABLE(hcryp);
  2301. /* Change the CRYP state */
  2302. hcryp->State = HAL_CRYP_STATE_READY;
  2303. /* Return function status */
  2304. return HAL_OK;
  2305. }
  2306. /**
  2307. * @brief Decryption in ECB/CBC & CTR mode with AES Standard using interrupt mode
  2308. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2309. * the configuration information for CRYP module
  2310. * @retval HAL status
  2311. */
  2312. static HAL_StatusTypeDef CRYP_AES_Decrypt_IT(CRYP_HandleTypeDef *hcryp)
  2313. {
  2314. __IO uint32_t count = 0U;
  2315. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  2316. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  2317. {
  2318. if (hcryp->KeyIVConfig == 1U)
  2319. {
  2320. /* If the Key and IV configuration has to be done only once
  2321. and if it has already been done, skip it */
  2322. DoKeyIVConfig = 0U;
  2323. }
  2324. else
  2325. {
  2326. /* If the Key and IV configuration has to be done only once
  2327. and if it has not been done already, do it and set KeyIVConfig
  2328. to keep track it won't have to be done again next time */
  2329. hcryp->KeyIVConfig = 1U;
  2330. }
  2331. }
  2332. if (DoKeyIVConfig == 1U)
  2333. {
  2334. /* Key preparation for ECB/CBC */
  2335. if (hcryp->Init.Algorithm != CRYP_AES_CTR)
  2336. {
  2337. #if defined (AES)
  2338. if (hcryp->AutoKeyDerivation == DISABLE)/*Mode 2 Key preparation*/
  2339. {
  2340. /* Set key preparation for decryption operating mode*/
  2341. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION);
  2342. /* Set the Key*/
  2343. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2344. /* Enable CRYP */
  2345. __HAL_CRYP_ENABLE(hcryp);
  2346. /* Wait for CCF flag to be raised */
  2347. count = CRYP_TIMEOUT_KEYPREPARATION;
  2348. do
  2349. {
  2350. count-- ;
  2351. if (count == 0U)
  2352. {
  2353. /* Disable the CRYP peripheral clock */
  2354. __HAL_CRYP_DISABLE(hcryp);
  2355. /* Change state */
  2356. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2357. hcryp->State = HAL_CRYP_STATE_READY;
  2358. /* Process unlocked */
  2359. __HAL_UNLOCK(hcryp);
  2360. return HAL_ERROR;
  2361. }
  2362. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  2363. /* Clear CCF Flag */
  2364. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2365. /* Return to decryption operating mode(Mode 3)*/
  2366. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  2367. }
  2368. else /*Mode 4 : decryption & key preparation*/
  2369. {
  2370. /* Set the Key*/
  2371. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2372. /* Set decryption & key preparation operating mode*/
  2373. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT);
  2374. }
  2375. #else /* CRYP */
  2376. /* change ALGOMODE to key preparation for decryption*/
  2377. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY);
  2378. /* Set the Key*/
  2379. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2380. /* Enable CRYP */
  2381. __HAL_CRYP_ENABLE(hcryp);
  2382. /* Wait for BUSY flag to be raised */
  2383. count = CRYP_TIMEOUT_KEYPREPARATION;
  2384. do
  2385. {
  2386. count-- ;
  2387. if (count == 0U)
  2388. {
  2389. /* Change state */
  2390. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2391. hcryp->State = HAL_CRYP_STATE_READY;
  2392. /* Process unlocked */
  2393. __HAL_UNLOCK(hcryp);
  2394. return HAL_ERROR;
  2395. }
  2396. } while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
  2397. /* Turn back to ALGOMODE of the configuration */
  2398. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm);
  2399. #endif /* End AES or CRYP */
  2400. }
  2401. else /*Algorithm CTR */
  2402. {
  2403. /* Set the Key*/
  2404. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2405. }
  2406. /* Set IV */
  2407. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  2408. {
  2409. /* Set the Initialization Vector*/
  2410. #if defined (AES)
  2411. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2412. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2413. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2414. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2415. #else /* CRYP */
  2416. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  2417. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2418. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2419. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2420. #endif /* End AES or CRYP */
  2421. }
  2422. } /* if (DoKeyIVConfig == 1U) */
  2423. /* Set the phase */
  2424. hcryp->Phase = CRYP_PHASE_PROCESS;
  2425. if (hcryp->Size != 0U)
  2426. {
  2427. #if defined (AES)
  2428. /* Enable computation complete flag and error interrupts */
  2429. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  2430. /* Enable CRYP */
  2431. __HAL_CRYP_ENABLE(hcryp);
  2432. /* Increment the pointer before writing the input block in the IN FIFO to make sure that
  2433. when Computation Completed IRQ fires, the hcryp->CrypInCount has always a consistent value
  2434. and it is ready for the next operation. */
  2435. hcryp->CrypInCount++;
  2436. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
  2437. hcryp->CrypInCount++;
  2438. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
  2439. hcryp->CrypInCount++;
  2440. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
  2441. hcryp->CrypInCount++;
  2442. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
  2443. #else /* CRYP */
  2444. /* Enable interrupts */
  2445. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  2446. /* Enable CRYP */
  2447. __HAL_CRYP_ENABLE(hcryp);
  2448. #endif /* End AES or CRYP */
  2449. }
  2450. else
  2451. {
  2452. /* Process locked */
  2453. __HAL_UNLOCK(hcryp);
  2454. /* Change the CRYP state */
  2455. hcryp->State = HAL_CRYP_STATE_READY;
  2456. }
  2457. /* Return function status */
  2458. return HAL_OK;
  2459. }
  2460. /**
  2461. * @brief Decryption in ECB/CBC & CTR mode with AES Standard using DMA mode
  2462. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2463. * the configuration information for CRYP module
  2464. * @retval HAL status
  2465. */
  2466. static HAL_StatusTypeDef CRYP_AES_Decrypt_DMA(CRYP_HandleTypeDef *hcryp)
  2467. {
  2468. __IO uint32_t count = 0U;
  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. }
  2478. else
  2479. {
  2480. /* If the Key and IV configuration has to be done only once
  2481. and if it has not been done already, do it and set KeyIVConfig
  2482. to keep track it won't have to be done again next time */
  2483. hcryp->KeyIVConfig = 1U;
  2484. }
  2485. }
  2486. if (DoKeyIVConfig == 1U)
  2487. {
  2488. /* Key preparation for ECB/CBC */
  2489. if (hcryp->Init.Algorithm != CRYP_AES_CTR)
  2490. {
  2491. #if defined (AES)
  2492. if (hcryp->AutoKeyDerivation == DISABLE)/*Mode 2 key preparation*/
  2493. {
  2494. /* Set key preparation for decryption operating mode*/
  2495. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION);
  2496. /* Set the Key*/
  2497. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2498. /* Enable CRYP */
  2499. __HAL_CRYP_ENABLE(hcryp);
  2500. /* Wait for CCF flag to be raised */
  2501. count = CRYP_TIMEOUT_KEYPREPARATION;
  2502. do
  2503. {
  2504. count-- ;
  2505. if (count == 0U)
  2506. {
  2507. /* Disable the CRYP peripheral clock */
  2508. __HAL_CRYP_DISABLE(hcryp);
  2509. /* Change state */
  2510. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2511. hcryp->State = HAL_CRYP_STATE_READY;
  2512. /* Process unlocked */
  2513. __HAL_UNLOCK(hcryp);
  2514. return HAL_ERROR;
  2515. }
  2516. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  2517. /* Clear CCF Flag */
  2518. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2519. /* Return to decryption operating mode(Mode 3)*/
  2520. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_DECRYPT);
  2521. }
  2522. else /*Mode 4 : decryption & key preparation*/
  2523. {
  2524. /* Set the Key*/
  2525. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2526. /* Set decryption & Key preparation operating mode*/
  2527. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT);
  2528. }
  2529. #else /* CRYP */
  2530. /* change ALGOMODE to key preparation for decryption*/
  2531. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_CR_ALGOMODE_AES_KEY);
  2532. /* Set the Key*/
  2533. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2534. /* Enable CRYP */
  2535. __HAL_CRYP_ENABLE(hcryp);
  2536. /* Wait for BUSY flag to be raised */
  2537. count = CRYP_TIMEOUT_KEYPREPARATION;
  2538. do
  2539. {
  2540. count-- ;
  2541. if (count == 0U)
  2542. {
  2543. /* Disable the CRYP peripheral clock */
  2544. __HAL_CRYP_DISABLE(hcryp);
  2545. /* Change state */
  2546. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2547. hcryp->State = HAL_CRYP_STATE_READY;
  2548. /* Process unlocked */
  2549. __HAL_UNLOCK(hcryp);
  2550. return HAL_ERROR;
  2551. }
  2552. } while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
  2553. /* Turn back to ALGOMODE of the configuration */
  2554. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm);
  2555. #endif /* End AES or CRYP */
  2556. }
  2557. else /*Algorithm CTR */
  2558. {
  2559. /* Set the Key*/
  2560. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  2561. }
  2562. if (hcryp->Init.Algorithm != CRYP_AES_ECB)
  2563. {
  2564. /* Set the Initialization Vector*/
  2565. #if defined (AES)
  2566. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  2567. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2568. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2569. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2570. #else /* CRYP */
  2571. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  2572. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  2573. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  2574. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  2575. #endif /* End AES or CRYP */
  2576. }
  2577. } /* if (DoKeyIVConfig == 1U) */
  2578. /* Set the phase */
  2579. hcryp->Phase = CRYP_PHASE_PROCESS;
  2580. if (hcryp->Size != 0U)
  2581. {
  2582. /* Set the input and output addresses and start DMA transfer */
  2583. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U), (uint32_t)(hcryp->pCrypOutBuffPtr));
  2584. }
  2585. else
  2586. {
  2587. /* Process unlocked */
  2588. __HAL_UNLOCK(hcryp);
  2589. /* Change the CRYP state */
  2590. hcryp->State = HAL_CRYP_STATE_READY;
  2591. }
  2592. /* Return function status */
  2593. return HAL_OK;
  2594. }
  2595. /**
  2596. * @brief DMA CRYP input data process complete callback.
  2597. * @param hdma: DMA handle
  2598. * @retval None
  2599. */
  2600. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
  2601. {
  2602. CRYP_HandleTypeDef *hcryp = (CRYP_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2603. /* Disable the DMA transfer for input FIFO request by resetting the DIEN bit
  2604. in the DMACR register */
  2605. #if defined (CRYP)
  2606. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN);
  2607. #else /* AES */
  2608. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  2609. /* TinyAES2, No output on CCM AES, unlock should be done when input data process complete */
  2610. if ((hcryp->Init.Algorithm & CRYP_AES_CCM) == CRYP_AES_CCM)
  2611. {
  2612. /* Clear CCF flag */
  2613. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2614. /* Change the CRYP state to ready */
  2615. hcryp->State = HAL_CRYP_STATE_READY;
  2616. /* Process Unlocked */
  2617. __HAL_UNLOCK(hcryp);
  2618. }
  2619. #endif /* End AES or CRYP */
  2620. /* Call input data transfer complete callback */
  2621. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2622. /*Call registered Input complete callback*/
  2623. hcryp->InCpltCallback(hcryp);
  2624. #else
  2625. /*Call legacy weak Input complete callback*/
  2626. HAL_CRYP_InCpltCallback(hcryp);
  2627. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2628. }
  2629. /**
  2630. * @brief DMA CRYP output data process complete callback.
  2631. * @param hdma: DMA handle
  2632. * @retval None
  2633. */
  2634. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
  2635. {
  2636. CRYP_HandleTypeDef *hcryp = (CRYP_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2637. /* Disable the DMA transfer for output FIFO request by resetting
  2638. the DOEN bit in the DMACR register */
  2639. #if defined (CRYP)
  2640. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN);
  2641. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  2642. if ((hcryp->Init.Algorithm & CRYP_AES_GCM) != CRYP_AES_GCM)
  2643. {
  2644. /* Disable CRYP (not allowed in GCM)*/
  2645. __HAL_CRYP_DISABLE(hcryp);
  2646. }
  2647. #else /*NO GCM CCM */
  2648. /* Disable CRYP */
  2649. __HAL_CRYP_DISABLE(hcryp);
  2650. #endif /* GCM CCM defined*/
  2651. #else /* AES */
  2652. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
  2653. /* Clear CCF flag */
  2654. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2655. if ((hcryp->Init.Algorithm & CRYP_AES_GCM_GMAC) != CRYP_AES_GCM_GMAC)
  2656. {
  2657. /* Disable CRYP (not allowed in GCM)*/
  2658. __HAL_CRYP_DISABLE(hcryp);
  2659. }
  2660. #endif /* End AES or CRYP */
  2661. /* Change the CRYP state to ready */
  2662. hcryp->State = HAL_CRYP_STATE_READY;
  2663. /* Process unlocked */
  2664. __HAL_UNLOCK(hcryp);
  2665. /* Call output data transfer complete callback */
  2666. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2667. /*Call registered Output complete callback*/
  2668. hcryp->OutCpltCallback(hcryp);
  2669. #else
  2670. /*Call legacy weak Output complete callback*/
  2671. HAL_CRYP_OutCpltCallback(hcryp);
  2672. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2673. }
  2674. /**
  2675. * @brief DMA CRYP communication error callback.
  2676. * @param hdma: DMA handle
  2677. * @retval None
  2678. */
  2679. static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
  2680. {
  2681. CRYP_HandleTypeDef *hcryp = (CRYP_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2682. /* Change the CRYP peripheral state */
  2683. hcryp->State = HAL_CRYP_STATE_READY;
  2684. /* DMA error code field */
  2685. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2686. #if defined (AES)
  2687. /* Clear CCF flag */
  2688. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2689. #endif /* AES */
  2690. /* Call error callback */
  2691. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2692. /*Call registered error callback*/
  2693. hcryp->ErrorCallback(hcryp);
  2694. #else
  2695. /*Call legacy weak error callback*/
  2696. HAL_CRYP_ErrorCallback(hcryp);
  2697. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2698. }
  2699. /**
  2700. * @brief Set the DMA configuration and start the DMA transfer
  2701. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2702. * the configuration information for CRYP module
  2703. * @param inputaddr: address of the input buffer
  2704. * @param Size: size of the input buffer, must be a multiple of 16.
  2705. * @param outputaddr: address of the output buffer
  2706. * @retval None
  2707. */
  2708. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  2709. {
  2710. /* Set the CRYP DMA transfer complete callback */
  2711. hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
  2712. /* Set the DMA input error callback */
  2713. hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
  2714. /* Set the CRYP DMA transfer complete callback */
  2715. hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
  2716. /* Set the DMA output error callback */
  2717. hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
  2718. #if defined (CRYP)
  2719. /* Enable CRYP */
  2720. __HAL_CRYP_ENABLE(hcryp);
  2721. /* Enable the input DMA Stream */
  2722. if (HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DIN, Size) != HAL_OK)
  2723. {
  2724. /* DMA error code field */
  2725. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2726. /* Call error callback */
  2727. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2728. /*Call registered error callback*/
  2729. hcryp->ErrorCallback(hcryp);
  2730. #else
  2731. /*Call legacy weak error callback*/
  2732. HAL_CRYP_ErrorCallback(hcryp);
  2733. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2734. }
  2735. /* Enable the output DMA Stream */
  2736. if (HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size) != HAL_OK)
  2737. {
  2738. /* DMA error code field */
  2739. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2740. /* Call error callback */
  2741. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2742. /*Call registered error callback*/
  2743. hcryp->ErrorCallback(hcryp);
  2744. #else
  2745. /*Call legacy weak error callback*/
  2746. HAL_CRYP_ErrorCallback(hcryp);
  2747. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2748. }
  2749. /* Enable In/Out DMA request */
  2750. hcryp->Instance->DMACR = CRYP_DMACR_DOEN | CRYP_DMACR_DIEN;
  2751. #else /* AES */
  2752. if (((hcryp->Init.Algorithm & CRYP_AES_GCM_GMAC) != CRYP_AES_GCM_GMAC)
  2753. && ((hcryp->Init.Algorithm & CRYP_AES_CCM) != CRYP_AES_CCM))
  2754. {
  2755. /* Enable CRYP (not allowed in GCM & CCM)*/
  2756. __HAL_CRYP_ENABLE(hcryp);
  2757. }
  2758. /* Enable the DMA input stream */
  2759. if (HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size) != HAL_OK)
  2760. {
  2761. /* DMA error code field */
  2762. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2763. /* Call error callback */
  2764. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2765. /*Call registered error callback*/
  2766. hcryp->ErrorCallback(hcryp);
  2767. #else
  2768. /*Call legacy weak error callback*/
  2769. HAL_CRYP_ErrorCallback(hcryp);
  2770. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2771. }
  2772. /* Enable the DMA output stream */
  2773. if (HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size) != HAL_OK)
  2774. {
  2775. /* DMA error code field */
  2776. hcryp->ErrorCode |= HAL_CRYP_ERROR_DMA;
  2777. /* Call error callback */
  2778. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2779. /*Call registered error callback*/
  2780. hcryp->ErrorCallback(hcryp);
  2781. #else
  2782. /*Call legacy weak error callback*/
  2783. HAL_CRYP_ErrorCallback(hcryp);
  2784. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2785. }
  2786. /*AES2v1.1.1 : CCM authentication : no init phase, only header and final phase */
  2787. /* Enable In and Out DMA requests */
  2788. if ((hcryp->Init.Algorithm & CRYP_AES_CCM) == CRYP_AES_CCM)
  2789. {
  2790. /* Enable only In DMA requests for CCM*/
  2791. SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN));
  2792. }
  2793. else
  2794. {
  2795. /* Enable In and Out DMA requests */
  2796. SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN));
  2797. }
  2798. #endif /* End AES or CRYP */
  2799. }
  2800. /**
  2801. * @brief Process Data: Write Input data in polling mode and used in AES functions.
  2802. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2803. * the configuration information for CRYP module
  2804. * @param Timeout: Specify Timeout value
  2805. * @retval None
  2806. */
  2807. static void CRYP_AES_ProcessData(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  2808. {
  2809. uint32_t temp[4]; /* Temporary CrypOutBuff */
  2810. uint32_t i;
  2811. #if defined (CRYP)
  2812. uint16_t incount; /* Temporary CrypInCount Value */
  2813. uint16_t outcount; /* Temporary CrypOutCount Value */
  2814. #endif /* CRYP */
  2815. #if defined (CRYP)
  2816. /*Temporary CrypOutCount Value*/
  2817. incount = hcryp->CrypInCount;
  2818. if (((hcryp->Instance->SR & CRYP_FLAG_IFNF) != 0x0U) && (incount < (hcryp->Size / 4U)))
  2819. {
  2820. /* Write the input block in the IN FIFO */
  2821. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2822. hcryp->CrypInCount++;
  2823. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2824. hcryp->CrypInCount++;
  2825. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2826. hcryp->CrypInCount++;
  2827. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2828. hcryp->CrypInCount++;
  2829. }
  2830. /* Wait for OFNE flag to be raised */
  2831. if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  2832. {
  2833. /* Disable the CRYP peripheral clock */
  2834. __HAL_CRYP_DISABLE(hcryp);
  2835. /* Change state & error code*/
  2836. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2837. hcryp->State = HAL_CRYP_STATE_READY;
  2838. /* Process unlocked */
  2839. __HAL_UNLOCK(hcryp);
  2840. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2841. /*Call registered error callback*/
  2842. hcryp->ErrorCallback(hcryp);
  2843. #else
  2844. /*Call legacy weak error callback*/
  2845. HAL_CRYP_ErrorCallback(hcryp);
  2846. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2847. }
  2848. /*Temporary CrypOutCount Value*/
  2849. outcount = hcryp->CrypOutCount;
  2850. if (((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) && (outcount < (hcryp->Size / 4U)))
  2851. {
  2852. /* Read the output block from the Output FIFO and put them in temporary buffer
  2853. then get CrypOutBuff from temporary buffer */
  2854. for (i = 0U; i < 4U; i++)
  2855. {
  2856. temp[i] = hcryp->Instance->DOUT;
  2857. }
  2858. i = 0U;
  2859. while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 4U))
  2860. {
  2861. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  2862. hcryp->CrypOutCount++;
  2863. i++;
  2864. }
  2865. }
  2866. #else /* AES */
  2867. /* Write the input block in the IN FIFO */
  2868. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2869. hcryp->CrypInCount++;
  2870. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2871. hcryp->CrypInCount++;
  2872. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2873. hcryp->CrypInCount++;
  2874. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2875. hcryp->CrypInCount++;
  2876. /* Wait for CCF flag to be raised */
  2877. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  2878. {
  2879. /* Disable the CRYP peripheral clock */
  2880. __HAL_CRYP_DISABLE(hcryp);
  2881. /* Change state */
  2882. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  2883. hcryp->State = HAL_CRYP_STATE_READY;
  2884. /* Process unlocked */
  2885. __HAL_UNLOCK(hcryp);
  2886. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2887. /*Call registered error callback*/
  2888. hcryp->ErrorCallback(hcryp);
  2889. #else
  2890. /*Call legacy weak error callback*/
  2891. HAL_CRYP_ErrorCallback(hcryp);
  2892. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2893. }
  2894. /* Clear CCF Flag */
  2895. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2896. /* Read the output block from the output FIFO and put them in temporary buffer
  2897. then get CrypOutBuff from temporary buffer*/
  2898. for (i = 0U; i < 4U; i++)
  2899. {
  2900. temp[i] = hcryp->Instance->DOUTR;
  2901. }
  2902. i = 0U;
  2903. while ((hcryp->CrypOutCount < ((hcryp->Size + 3U) / 4U)) && (i < 4U))
  2904. {
  2905. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  2906. hcryp->CrypOutCount++;
  2907. i++;
  2908. }
  2909. #endif /* End AES or CRYP */
  2910. }
  2911. /**
  2912. * @brief Handle CRYP block input/output data handling under interruption.
  2913. * @note The function is called under interruption only, once
  2914. * interruptions have been enabled by HAL_CRYP_Encrypt_IT or HAL_CRYP_Decrypt_IT.
  2915. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2916. * the configuration information for CRYP module.
  2917. * @retval HAL status
  2918. */
  2919. static void CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
  2920. {
  2921. uint32_t temp[4]; /* Temporary CrypOutBuff */
  2922. uint32_t i;
  2923. #if defined (CRYP)
  2924. uint16_t incount; /* Temporary CrypInCount Value */
  2925. uint16_t outcount; /* Temporary CrypOutCount Value */
  2926. #endif /* CRYP */
  2927. if (hcryp->State == HAL_CRYP_STATE_BUSY)
  2928. {
  2929. #if defined (CRYP)
  2930. /*Temporary CrypOutCount Value*/
  2931. incount = hcryp->CrypInCount;
  2932. if (((hcryp->Instance->SR & CRYP_FLAG_IFNF) != 0x0U) && (incount < (hcryp->Size / 4U)))
  2933. {
  2934. /* Write the input block in the IN FIFO */
  2935. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2936. hcryp->CrypInCount++;
  2937. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2938. hcryp->CrypInCount++;
  2939. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2940. hcryp->CrypInCount++;
  2941. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  2942. hcryp->CrypInCount++;
  2943. if (hcryp->CrypInCount == ((uint16_t)(hcryp->Size) / 4U))
  2944. {
  2945. /* Disable interrupts */
  2946. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  2947. /* Call the input data transfer complete callback */
  2948. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2949. /*Call registered Input complete callback*/
  2950. hcryp->InCpltCallback(hcryp);
  2951. #else
  2952. /*Call legacy weak Input complete callback*/
  2953. HAL_CRYP_InCpltCallback(hcryp);
  2954. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2955. }
  2956. }
  2957. /*Temporary CrypOutCount Value*/
  2958. outcount = hcryp->CrypOutCount;
  2959. if (((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U) && (outcount < (hcryp->Size / 4U)))
  2960. {
  2961. /* Read the output block from the output FIFO and put them in temporary buffer
  2962. then get CrypOutBuff from temporary buffer */
  2963. for (i = 0U; i < 4U; i++)
  2964. {
  2965. temp[i] = hcryp->Instance->DOUT;
  2966. }
  2967. i = 0U;
  2968. while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 4U))
  2969. {
  2970. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  2971. hcryp->CrypOutCount++;
  2972. i++;
  2973. }
  2974. if (hcryp->CrypOutCount == ((uint16_t)(hcryp->Size) / 4U))
  2975. {
  2976. /* Disable interrupts */
  2977. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  2978. /* Change the CRYP state */
  2979. hcryp->State = HAL_CRYP_STATE_READY;
  2980. /* Disable CRYP */
  2981. __HAL_CRYP_DISABLE(hcryp);
  2982. /* Process unlocked */
  2983. __HAL_UNLOCK(hcryp);
  2984. /* Call Output transfer complete callback */
  2985. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  2986. /*Call registered Output complete callback*/
  2987. hcryp->OutCpltCallback(hcryp);
  2988. #else
  2989. /*Call legacy weak Output complete callback*/
  2990. HAL_CRYP_OutCpltCallback(hcryp);
  2991. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  2992. }
  2993. }
  2994. #else /*AES*/
  2995. /* Read the output block from the output FIFO and put them in temporary buffer
  2996. then get CrypOutBuff from temporary buffer*/
  2997. for (i = 0U; i < 4U; i++)
  2998. {
  2999. temp[i] = hcryp->Instance->DOUTR;
  3000. }
  3001. i = 0U;
  3002. while ((hcryp->CrypOutCount < ((hcryp->Size + 3U) / 4U)) && (i < 4U))
  3003. {
  3004. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  3005. hcryp->CrypOutCount++;
  3006. i++;
  3007. }
  3008. if (hcryp->CrypOutCount == (hcryp->Size / 4U))
  3009. {
  3010. /* Disable Computation Complete flag and errors interrupts */
  3011. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  3012. /* Change the CRYP state */
  3013. hcryp->State = HAL_CRYP_STATE_READY;
  3014. /* Disable CRYP */
  3015. __HAL_CRYP_DISABLE(hcryp);
  3016. /* Process Unlocked */
  3017. __HAL_UNLOCK(hcryp);
  3018. /* Call Output transfer complete callback */
  3019. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  3020. /*Call registered Output complete callback*/
  3021. hcryp->OutCpltCallback(hcryp);
  3022. #else
  3023. /*Call legacy weak Output complete callback*/
  3024. HAL_CRYP_OutCpltCallback(hcryp);
  3025. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3026. }
  3027. else
  3028. {
  3029. /* Write the input block in the IN FIFO */
  3030. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3031. hcryp->CrypInCount++;
  3032. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3033. hcryp->CrypInCount++;
  3034. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3035. hcryp->CrypInCount++;
  3036. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3037. hcryp->CrypInCount++;
  3038. if (hcryp->CrypInCount == (hcryp->Size / 4U))
  3039. {
  3040. /* Call Input transfer complete callback */
  3041. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  3042. /*Call registered Input complete callback*/
  3043. hcryp->InCpltCallback(hcryp);
  3044. #else
  3045. /*Call legacy weak Input complete callback*/
  3046. HAL_CRYP_InCpltCallback(hcryp);
  3047. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3048. }
  3049. }
  3050. #endif /* End AES or CRYP */
  3051. }
  3052. else
  3053. {
  3054. /* Busy error code field */
  3055. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  3056. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  3057. /*Call registered error callback*/
  3058. hcryp->ErrorCallback(hcryp);
  3059. #else
  3060. /*Call legacy weak error callback*/
  3061. HAL_CRYP_ErrorCallback(hcryp);
  3062. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3063. }
  3064. }
  3065. /**
  3066. * @brief Writes Key in Key registers.
  3067. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3068. * the configuration information for CRYP module
  3069. * @param KeySize: Size of Key
  3070. * @retval None
  3071. */
  3072. static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint32_t KeySize)
  3073. {
  3074. #if defined (CRYP)
  3075. switch (KeySize)
  3076. {
  3077. case CRYP_KEYSIZE_256B:
  3078. hcryp->Instance->K0LR = *(uint32_t *)(hcryp->Init.pKey);
  3079. hcryp->Instance->K0RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  3080. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  3081. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  3082. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  3083. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  3084. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 6);
  3085. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 7);
  3086. break;
  3087. case CRYP_KEYSIZE_192B:
  3088. hcryp->Instance->K1LR = *(uint32_t *)(hcryp->Init.pKey);
  3089. hcryp->Instance->K1RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  3090. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  3091. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  3092. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 4);
  3093. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 5);
  3094. break;
  3095. case CRYP_KEYSIZE_128B:
  3096. hcryp->Instance->K2LR = *(uint32_t *)(hcryp->Init.pKey);
  3097. hcryp->Instance->K2RR = *(uint32_t *)(hcryp->Init.pKey + 1);
  3098. hcryp->Instance->K3LR = *(uint32_t *)(hcryp->Init.pKey + 2);
  3099. hcryp->Instance->K3RR = *(uint32_t *)(hcryp->Init.pKey + 3);
  3100. break;
  3101. default:
  3102. break;
  3103. }
  3104. #else /*AES*/
  3105. switch (KeySize)
  3106. {
  3107. case CRYP_KEYSIZE_256B:
  3108. hcryp->Instance->KEYR7 = *(uint32_t *)(hcryp->Init.pKey);
  3109. hcryp->Instance->KEYR6 = *(uint32_t *)(hcryp->Init.pKey + 1);
  3110. hcryp->Instance->KEYR5 = *(uint32_t *)(hcryp->Init.pKey + 2);
  3111. hcryp->Instance->KEYR4 = *(uint32_t *)(hcryp->Init.pKey + 3);
  3112. hcryp->Instance->KEYR3 = *(uint32_t *)(hcryp->Init.pKey + 4);
  3113. hcryp->Instance->KEYR2 = *(uint32_t *)(hcryp->Init.pKey + 5);
  3114. hcryp->Instance->KEYR1 = *(uint32_t *)(hcryp->Init.pKey + 6);
  3115. hcryp->Instance->KEYR0 = *(uint32_t *)(hcryp->Init.pKey + 7);
  3116. break;
  3117. case CRYP_KEYSIZE_128B:
  3118. hcryp->Instance->KEYR3 = *(uint32_t *)(hcryp->Init.pKey);
  3119. hcryp->Instance->KEYR2 = *(uint32_t *)(hcryp->Init.pKey + 1);
  3120. hcryp->Instance->KEYR1 = *(uint32_t *)(hcryp->Init.pKey + 2);
  3121. hcryp->Instance->KEYR0 = *(uint32_t *)(hcryp->Init.pKey + 3);
  3122. break;
  3123. default:
  3124. break;
  3125. }
  3126. #endif /* End AES or CRYP */
  3127. }
  3128. #if defined (CRYP_CR_ALGOMODE_AES_GCM)|| defined (AES)
  3129. /**
  3130. * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG
  3131. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3132. * the configuration information for CRYP module
  3133. * @param Timeout: Timeout duration
  3134. * @retval HAL status
  3135. */
  3136. static HAL_StatusTypeDef CRYP_AESGCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  3137. {
  3138. uint32_t tickstart;
  3139. uint32_t wordsize = (uint32_t)(hcryp->Size) / 4U ;
  3140. uint16_t outcount; /* Temporary CrypOutCount Value */
  3141. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  3142. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  3143. {
  3144. if (hcryp->KeyIVConfig == 1U)
  3145. {
  3146. /* If the Key and IV configuration has to be done only once
  3147. and if it has already been done, skip it */
  3148. DoKeyIVConfig = 0U;
  3149. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  3150. }
  3151. else
  3152. {
  3153. /* If the Key and IV configuration has to be done only once
  3154. and if it has not been done already, do it and set KeyIVConfig
  3155. to keep track it won't have to be done again next time */
  3156. hcryp->KeyIVConfig = 1U;
  3157. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  3158. }
  3159. }
  3160. else
  3161. {
  3162. hcryp->SizesSum = hcryp->Size;
  3163. }
  3164. if (DoKeyIVConfig == 1U)
  3165. {
  3166. /* Reset CrypHeaderCount */
  3167. hcryp->CrypHeaderCount = 0U;
  3168. /****************************** Init phase **********************************/
  3169. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  3170. /* Set the key */
  3171. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3172. #if defined(CRYP)
  3173. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3174. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  3175. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  3176. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  3177. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  3178. /* Enable the CRYP peripheral */
  3179. __HAL_CRYP_ENABLE(hcryp);
  3180. /* Get tick */
  3181. tickstart = HAL_GetTick();
  3182. /*Wait for the CRYPEN bit to be cleared*/
  3183. while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  3184. {
  3185. /* Check for the Timeout */
  3186. if (Timeout != HAL_MAX_DELAY)
  3187. {
  3188. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  3189. {
  3190. /* Disable the CRYP peripheral clock */
  3191. __HAL_CRYP_DISABLE(hcryp);
  3192. /* Change state */
  3193. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3194. hcryp->State = HAL_CRYP_STATE_READY;
  3195. /* Process unlocked */
  3196. __HAL_UNLOCK(hcryp);
  3197. return HAL_ERROR;
  3198. }
  3199. }
  3200. }
  3201. #else /* AES */
  3202. /* Workaround 1 : only AES.
  3203. Datatype configuration must be 32 bits during Init phase. Only, after Init, and before re
  3204. enabling the IP, datatype different from 32 bits can be configured.*/
  3205. /* Select DATATYPE 32 */
  3206. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, CRYP_DATATYPE_32B);
  3207. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3208. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  3209. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  3210. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  3211. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  3212. /* Enable the CRYP peripheral */
  3213. __HAL_CRYP_ENABLE(hcryp);
  3214. /* just wait for hash computation */
  3215. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3216. {
  3217. /* Change state */
  3218. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3219. hcryp->State = HAL_CRYP_STATE_READY;
  3220. /* Process unlocked & return error */
  3221. __HAL_UNLOCK(hcryp);
  3222. return HAL_ERROR;
  3223. }
  3224. /* Clear CCF flag */
  3225. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3226. #endif /* End AES or CRYP */
  3227. /************************ Header phase *************************************/
  3228. if (CRYP_GCMCCM_SetHeaderPhase(hcryp, Timeout) != HAL_OK)
  3229. {
  3230. return HAL_ERROR;
  3231. }
  3232. /*************************Payload phase ************************************/
  3233. /* Set the phase */
  3234. hcryp->Phase = CRYP_PHASE_PROCESS;
  3235. #if defined(CRYP)
  3236. /* Disable the CRYP peripheral */
  3237. __HAL_CRYP_DISABLE(hcryp);
  3238. /* Select payload phase once the header phase is performed */
  3239. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  3240. /* Enable the CRYP peripheral */
  3241. __HAL_CRYP_ENABLE(hcryp);
  3242. #else /* AES */
  3243. /* Select payload phase once the header phase is performed */
  3244. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  3245. #endif /* End AES or CRYP */
  3246. } /* if (DoKeyIVConfig == 1U) */
  3247. if ((hcryp->Size % 16U) != 0U)
  3248. {
  3249. /* recalculate wordsize */
  3250. wordsize = ((wordsize / 4U) * 4U) ;
  3251. }
  3252. /* Get tick */
  3253. tickstart = HAL_GetTick();
  3254. /*Temporary CrypOutCount Value*/
  3255. outcount = hcryp->CrypOutCount;
  3256. /* Write input data and get output Data */
  3257. while ((hcryp->CrypInCount < wordsize) && (outcount < wordsize))
  3258. {
  3259. /* Write plain data and get cipher data */
  3260. CRYP_AES_ProcessData(hcryp, Timeout);
  3261. /*Temporary CrypOutCount Value*/
  3262. outcount = hcryp->CrypOutCount;
  3263. /* Check for the Timeout */
  3264. if (Timeout != HAL_MAX_DELAY)
  3265. {
  3266. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  3267. {
  3268. /* Disable the CRYP peripheral clock */
  3269. __HAL_CRYP_DISABLE(hcryp);
  3270. /* Change state & error code */
  3271. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3272. hcryp->State = HAL_CRYP_STATE_READY;
  3273. /* Process unlocked */
  3274. __HAL_UNLOCK(hcryp);
  3275. return HAL_ERROR;
  3276. }
  3277. }
  3278. }
  3279. if ((hcryp->Size % 16U) != 0U)
  3280. {
  3281. /* Workaround 2 : CRYP1 & AES generates correct TAG for GCM mode only when input block size is multiple of
  3282. 128 bits. If lthe size of the last block of payload is inferior to 128 bits, when GCM encryption
  3283. is selected, then the TAG message will be wrong.*/
  3284. CRYP_Workaround(hcryp, Timeout);
  3285. }
  3286. /* Return function status */
  3287. return HAL_OK;
  3288. }
  3289. /**
  3290. * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG in interrupt mode
  3291. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3292. * the configuration information for CRYP module
  3293. * @retval HAL status
  3294. */
  3295. static HAL_StatusTypeDef CRYP_AESGCM_Process_IT(CRYP_HandleTypeDef *hcryp)
  3296. {
  3297. __IO uint32_t count = 0U;
  3298. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  3299. #if defined(AES)
  3300. uint32_t loopcounter;
  3301. uint32_t lastwordsize;
  3302. uint32_t npblb;
  3303. #endif /* AES */
  3304. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  3305. {
  3306. if (hcryp->KeyIVConfig == 1U)
  3307. {
  3308. /* If the Key and IV configuration has to be done only once
  3309. and if it has already been done, skip it */
  3310. DoKeyIVConfig = 0U;
  3311. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  3312. }
  3313. else
  3314. {
  3315. /* If the Key and IV configuration has to be done only once
  3316. and if it has not been done already, do it and set KeyIVConfig
  3317. to keep track it won't have to be done again next time */
  3318. hcryp->KeyIVConfig = 1U;
  3319. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  3320. }
  3321. }
  3322. else
  3323. {
  3324. hcryp->SizesSum = hcryp->Size;
  3325. }
  3326. /* Configure Key, IV and process message (header and payload) */
  3327. if (DoKeyIVConfig == 1U)
  3328. {
  3329. /* Reset CrypHeaderCount */
  3330. hcryp->CrypHeaderCount = 0U;
  3331. /******************************* Init phase *********************************/
  3332. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  3333. /* Set the key */
  3334. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3335. #if defined(CRYP)
  3336. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3337. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  3338. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  3339. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  3340. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  3341. /* Enable the CRYP peripheral */
  3342. __HAL_CRYP_ENABLE(hcryp);
  3343. /*Wait for the CRYPEN bit to be cleared*/
  3344. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  3345. do
  3346. {
  3347. count-- ;
  3348. if (count == 0U)
  3349. {
  3350. /* Disable the CRYP peripheral clock */
  3351. __HAL_CRYP_DISABLE(hcryp);
  3352. /* Change state */
  3353. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3354. hcryp->State = HAL_CRYP_STATE_READY;
  3355. /* Process unlocked */
  3356. __HAL_UNLOCK(hcryp);
  3357. return HAL_ERROR;
  3358. }
  3359. } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
  3360. #else /* AES */
  3361. /* Workaround 1 : only AES
  3362. Datatype configuration must be 32 bits during INIT phase. Only, after INIT, and before re
  3363. enabling the IP, datatype different from 32 bits can be configured.*/
  3364. /* Select DATATYPE 32 */
  3365. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, CRYP_DATATYPE_32B);
  3366. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3367. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  3368. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  3369. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  3370. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  3371. /* Enable the CRYP peripheral */
  3372. __HAL_CRYP_ENABLE(hcryp);
  3373. /* just wait for hash computation */
  3374. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  3375. do
  3376. {
  3377. count-- ;
  3378. if (count == 0U)
  3379. {
  3380. /* Disable the CRYP peripheral clock */
  3381. __HAL_CRYP_DISABLE(hcryp);
  3382. /* Change state */
  3383. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3384. hcryp->State = HAL_CRYP_STATE_READY;
  3385. /* Process unlocked */
  3386. __HAL_UNLOCK(hcryp);
  3387. return HAL_ERROR;
  3388. }
  3389. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  3390. /* Clear CCF flag */
  3391. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3392. #endif /* End AES or CRYP */
  3393. /***************************** Header phase *********************************/
  3394. #if defined(CRYP)
  3395. /* Select header phase */
  3396. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  3397. /* Enable interrupts */
  3398. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI);
  3399. /* Enable CRYP */
  3400. __HAL_CRYP_ENABLE(hcryp);
  3401. #else /* AES */
  3402. /* Workaround 1: only AES , before re-enabling the IP, datatype can be configured*/
  3403. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
  3404. /* Select header phase */
  3405. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  3406. /* Enable computation complete flag and error interrupts */
  3407. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  3408. /* Enable the CRYP peripheral */
  3409. __HAL_CRYP_ENABLE(hcryp);
  3410. if (hcryp->Init.HeaderSize == 0U) /*header phase is skipped*/
  3411. {
  3412. /* Set the phase */
  3413. hcryp->Phase = CRYP_PHASE_PROCESS;
  3414. /* Select payload phase once the header phase is performed */
  3415. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_PAYLOAD);
  3416. /* Write the payload Input block in the IN FIFO */
  3417. if (hcryp->Size == 0U)
  3418. {
  3419. /* Disable interrupts */
  3420. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  3421. /* Change the CRYP state */
  3422. hcryp->State = HAL_CRYP_STATE_READY;
  3423. /* Process unlocked */
  3424. __HAL_UNLOCK(hcryp);
  3425. }
  3426. else if (hcryp->Size >= 16U)
  3427. {
  3428. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3429. hcryp->CrypInCount++;
  3430. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3431. hcryp->CrypInCount++;
  3432. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3433. hcryp->CrypInCount++;
  3434. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3435. hcryp->CrypInCount++;
  3436. if (hcryp->CrypInCount == (hcryp->Size / 4U))
  3437. {
  3438. /* Call Input transfer complete callback */
  3439. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  3440. /*Call registered Input complete callback*/
  3441. hcryp->InCpltCallback(hcryp);
  3442. #else
  3443. /*Call legacy weak Input complete callback*/
  3444. HAL_CRYP_InCpltCallback(hcryp);
  3445. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3446. }
  3447. }
  3448. else /* Size < 16Bytes : first block is the last block*/
  3449. {
  3450. /* Workaround not implemented*/
  3451. /* Size should be %4 otherwise Tag will be incorrectly generated for GCM Encryption:
  3452. Workaround is implemented in polling mode, so if last block of
  3453. payload <128bit don't use CRYP_Encrypt_IT otherwise TAG is incorrectly generated for GCM Encryption. */
  3454. /* Compute the number of padding bytes in last block of payload */
  3455. npblb = 16U - (uint32_t)(hcryp->Size);
  3456. /* Number of valid words (lastwordsize) in last block */
  3457. if ((npblb % 4U) == 0U)
  3458. {
  3459. lastwordsize = (16U - npblb) / 4U;
  3460. }
  3461. else
  3462. {
  3463. lastwordsize = ((16U - npblb) / 4U) + 1U;
  3464. }
  3465. /* last block optionally pad the data with zeros*/
  3466. for (loopcounter = 0U; loopcounter < lastwordsize ; loopcounter++)
  3467. {
  3468. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3469. hcryp->CrypInCount++;
  3470. }
  3471. while (loopcounter < 4U)
  3472. {
  3473. /* pad the data with zeros to have a complete block */
  3474. hcryp->Instance->DINR = 0x0U;
  3475. loopcounter++;
  3476. }
  3477. }
  3478. }
  3479. else if ((hcryp->Init.HeaderSize) < 4U)
  3480. {
  3481. for (loopcounter = 0U; loopcounter < hcryp->Init.HeaderSize ; loopcounter++)
  3482. {
  3483. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3484. hcryp->CrypHeaderCount++ ;
  3485. }
  3486. while (loopcounter < 4U)
  3487. {
  3488. /* pad the data with zeros to have a complete block */
  3489. hcryp->Instance->DINR = 0x0U;
  3490. loopcounter++;
  3491. }
  3492. /* Set the phase */
  3493. hcryp->Phase = CRYP_PHASE_PROCESS;
  3494. /* Select payload phase once the header phase is performed */
  3495. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  3496. /* Call Input transfer complete callback */
  3497. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  3498. /*Call registered Input complete callback*/
  3499. hcryp->InCpltCallback(hcryp);
  3500. #else
  3501. /*Call legacy weak Input complete callback*/
  3502. HAL_CRYP_InCpltCallback(hcryp);
  3503. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  3504. }
  3505. else if ((hcryp->Init.HeaderSize) >= 4U)
  3506. {
  3507. /* Write the input block in the IN FIFO */
  3508. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3509. hcryp->CrypHeaderCount++;
  3510. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3511. hcryp->CrypHeaderCount++;
  3512. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3513. hcryp->CrypHeaderCount++;
  3514. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3515. hcryp->CrypHeaderCount++;
  3516. }
  3517. else
  3518. {
  3519. /* Nothing to do */
  3520. }
  3521. #endif /* End AES or CRYP */
  3522. } /* end of if (DoKeyIVConfig == 1U) */
  3523. /* Return function status */
  3524. return HAL_OK;
  3525. }
  3526. /**
  3527. * @brief Encryption/Decryption process in AES GCM mode and prepare the authentication TAG using DMA
  3528. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3529. * the configuration information for CRYP module
  3530. * @retval HAL status
  3531. */
  3532. static HAL_StatusTypeDef CRYP_AESGCM_Process_DMA(CRYP_HandleTypeDef *hcryp)
  3533. {
  3534. __IO uint32_t count = 0U;
  3535. uint32_t wordsize;
  3536. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  3537. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  3538. {
  3539. if (hcryp->KeyIVConfig == 1U)
  3540. {
  3541. /* If the Key and IV configuration has to be done only once
  3542. and if it has already been done, skip it */
  3543. DoKeyIVConfig = 0U;
  3544. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  3545. }
  3546. else
  3547. {
  3548. /* If the Key and IV configuration has to be done only once
  3549. and if it has not been done already, do it and set KeyIVConfig
  3550. to keep track it won't have to be done again next time */
  3551. hcryp->KeyIVConfig = 1U;
  3552. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  3553. }
  3554. }
  3555. else
  3556. {
  3557. hcryp->SizesSum = hcryp->Size;
  3558. }
  3559. if (DoKeyIVConfig == 1U)
  3560. {
  3561. /* Reset CrypHeaderCount */
  3562. hcryp->CrypHeaderCount = 0U;
  3563. /*************************** Init phase ************************************/
  3564. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  3565. /* Set the key */
  3566. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3567. #if defined(CRYP)
  3568. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3569. hcryp->Instance->IV0LR = *(uint32_t *)(hcryp->Init.pInitVect);
  3570. hcryp->Instance->IV0RR = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  3571. hcryp->Instance->IV1LR = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  3572. hcryp->Instance->IV1RR = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  3573. /* Enable the CRYP peripheral */
  3574. __HAL_CRYP_ENABLE(hcryp);
  3575. /*Wait for the CRYPEN bit to be cleared*/
  3576. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  3577. do
  3578. {
  3579. count-- ;
  3580. if (count == 0U)
  3581. {
  3582. /* Disable the CRYP peripheral clock */
  3583. __HAL_CRYP_DISABLE(hcryp);
  3584. /* Change state */
  3585. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3586. hcryp->State = HAL_CRYP_STATE_READY;
  3587. /* Process unlocked */
  3588. __HAL_UNLOCK(hcryp);
  3589. return HAL_ERROR;
  3590. }
  3591. } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
  3592. #else /* AES */
  3593. /*Workaround 1 : only AES
  3594. Datatype configuration must be 32 bits during Init phase. Only, after Init, and before re
  3595. enabling the IP, datatype different from 32 bits can be configured.*/
  3596. /* Select DATATYPE 32 */
  3597. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, CRYP_DATATYPE_32B);
  3598. /* Set the initialization vector and the counter : Initial Counter Block (ICB)*/
  3599. hcryp->Instance->IVR3 = *(uint32_t *)(hcryp->Init.pInitVect);
  3600. hcryp->Instance->IVR2 = *(uint32_t *)(hcryp->Init.pInitVect + 1);
  3601. hcryp->Instance->IVR1 = *(uint32_t *)(hcryp->Init.pInitVect + 2);
  3602. hcryp->Instance->IVR0 = *(uint32_t *)(hcryp->Init.pInitVect + 3);
  3603. /* Enable the CRYP peripheral */
  3604. __HAL_CRYP_ENABLE(hcryp);
  3605. /* just wait for hash computation */
  3606. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  3607. do
  3608. {
  3609. count-- ;
  3610. if (count == 0U)
  3611. {
  3612. /* Disable the CRYP peripheral clock */
  3613. __HAL_CRYP_DISABLE(hcryp);
  3614. /* Change state */
  3615. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3616. hcryp->State = HAL_CRYP_STATE_READY;
  3617. /* Process unlocked */
  3618. __HAL_UNLOCK(hcryp);
  3619. return HAL_ERROR;
  3620. }
  3621. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  3622. /* Clear CCF flag */
  3623. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3624. #endif /* End AES or CRYP */
  3625. /************************ Header phase *************************************/
  3626. if (CRYP_GCMCCM_SetHeaderPhase_DMA(hcryp) != HAL_OK)
  3627. {
  3628. return HAL_ERROR;
  3629. }
  3630. /************************ Payload phase ************************************/
  3631. /* Set the phase */
  3632. hcryp->Phase = CRYP_PHASE_PROCESS;
  3633. #if defined(CRYP)
  3634. /* Disable the CRYP peripheral */
  3635. __HAL_CRYP_DISABLE(hcryp);
  3636. #endif /* CRYP */
  3637. /* Select payload phase once the header phase is performed */
  3638. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  3639. } /* if (DoKeyIVConfig == 1U) */
  3640. if (hcryp->Size != 0U)
  3641. {
  3642. /* CRYP1 IP V < 2.2.1 Size should be %4 otherwise Tag will
  3643. be incorrectly generated for GCM Encryption:
  3644. Workaround is implemented in polling mode, so if last block of
  3645. payload <128bit don't use DMA mode otherwise TAG is incorrectly generated . */
  3646. /* Set the input and output addresses and start DMA transfer */
  3647. if ((hcryp->Size % 16U) == 0U)
  3648. {
  3649. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U),
  3650. (uint32_t)(hcryp->pCrypOutBuffPtr));
  3651. }
  3652. else /*to compute last word<128bits, otherwise it will not be encrypted/decrypted */
  3653. {
  3654. wordsize = (uint32_t)(hcryp->Size) + (16U - ((uint32_t)(hcryp->Size) % 16U)) ;
  3655. /* Set the input and output addresses and start DMA transfer, pCrypOutBuffPtr size should be %4 */
  3656. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)wordsize / 4U),
  3657. (uint32_t)(hcryp->pCrypOutBuffPtr));
  3658. }
  3659. }
  3660. else
  3661. {
  3662. /* Process unLocked */
  3663. __HAL_UNLOCK(hcryp);
  3664. /* Change the CRYP state and phase */
  3665. hcryp->State = HAL_CRYP_STATE_READY;
  3666. }
  3667. /* Return function status */
  3668. return HAL_OK;
  3669. }
  3670. /**
  3671. * @brief AES CCM encryption/decryption processing in polling mode
  3672. * for TinyAES IP, no encrypt/decrypt performed, only authentication preparation.
  3673. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3674. * the configuration information for CRYP module
  3675. * @param Timeout: Timeout duration
  3676. * @retval HAL status
  3677. */
  3678. static HAL_StatusTypeDef CRYP_AESCCM_Process(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  3679. {
  3680. uint32_t tickstart;
  3681. uint32_t wordsize = (uint32_t)(hcryp->Size) / 4U;
  3682. uint16_t outcount; /* Temporary CrypOutCount Value */
  3683. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  3684. #if defined(AES)
  3685. uint32_t loopcounter;
  3686. uint32_t npblb;
  3687. uint32_t lastwordsize;
  3688. #endif /* AES */
  3689. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  3690. {
  3691. if (hcryp->KeyIVConfig == 1U)
  3692. {
  3693. /* If the Key and IV configuration has to be done only once
  3694. and if it has already been done, skip it */
  3695. DoKeyIVConfig = 0U;
  3696. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  3697. }
  3698. else
  3699. {
  3700. /* If the Key and IV configuration has to be done only once
  3701. and if it has not been done already, do it and set KeyIVConfig
  3702. to keep track it won't have to be done again next time */
  3703. hcryp->KeyIVConfig = 1U;
  3704. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  3705. }
  3706. }
  3707. else
  3708. {
  3709. hcryp->SizesSum = hcryp->Size;
  3710. }
  3711. if (DoKeyIVConfig == 1U)
  3712. {
  3713. /* Reset CrypHeaderCount */
  3714. hcryp->CrypHeaderCount = 0U;
  3715. #if defined(CRYP)
  3716. /********************** Init phase ******************************************/
  3717. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  3718. /* Set the key */
  3719. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3720. /* Set the initialization vector (IV) with CTR1 information */
  3721. hcryp->Instance->IV0LR = (hcryp->Init.B0[0]) & CRYP_CCM_CTR1_0;
  3722. hcryp->Instance->IV0RR = hcryp->Init.B0[1];
  3723. hcryp->Instance->IV1LR = hcryp->Init.B0[2];
  3724. hcryp->Instance->IV1RR = (hcryp->Init.B0[3] & CRYP_CCM_CTR1_1) | CRYP_CCM_CTR1_2;
  3725. /* Enable the CRYP peripheral */
  3726. __HAL_CRYP_ENABLE(hcryp);
  3727. /*Write B0 packet into CRYP_DIN Register*/
  3728. if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
  3729. {
  3730. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0));
  3731. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 1));
  3732. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 2));
  3733. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 3));
  3734. }
  3735. else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
  3736. {
  3737. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0), 16);
  3738. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 1), 16);
  3739. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 2), 16);
  3740. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 3), 16);
  3741. }
  3742. else if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
  3743. {
  3744. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0));
  3745. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 1));
  3746. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 2));
  3747. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 3));
  3748. }
  3749. else
  3750. {
  3751. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0);
  3752. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 1);
  3753. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 2);
  3754. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 3);
  3755. }
  3756. /* Get tick */
  3757. tickstart = HAL_GetTick();
  3758. /*Wait for the CRYPEN bit to be cleared*/
  3759. while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  3760. {
  3761. /* Check for the Timeout */
  3762. if (Timeout != HAL_MAX_DELAY)
  3763. {
  3764. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  3765. {
  3766. /* Disable the CRYP peripheral clock */
  3767. __HAL_CRYP_DISABLE(hcryp);
  3768. /* Change state */
  3769. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3770. hcryp->State = HAL_CRYP_STATE_READY;
  3771. /* Process unlocked */
  3772. __HAL_UNLOCK(hcryp);
  3773. return HAL_ERROR;
  3774. }
  3775. }
  3776. }
  3777. #else /* AES */
  3778. /*AES2v1.1.1 : CCM authentication : no init phase, only header and final phase */
  3779. /* Select header phase */
  3780. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  3781. /* configured encryption mode */
  3782. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  3783. /* Set the key */
  3784. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  3785. /* Set the initialization vector with zero values*/
  3786. hcryp->Instance->IVR3 = 0U;
  3787. hcryp->Instance->IVR2 = 0U;
  3788. hcryp->Instance->IVR1 = 0U;
  3789. hcryp->Instance->IVR0 = 0U;
  3790. /* Enable the CRYP peripheral */
  3791. __HAL_CRYP_ENABLE(hcryp);
  3792. /*Write the B0 packet into CRYP_DIN*/
  3793. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0);
  3794. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 1);
  3795. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 2);
  3796. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 3);
  3797. /* wait until the end of computation */
  3798. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3799. {
  3800. /* Change state */
  3801. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3802. hcryp->State = HAL_CRYP_STATE_READY;
  3803. /* Process unlocked & return error */
  3804. __HAL_UNLOCK(hcryp);
  3805. return HAL_ERROR;
  3806. }
  3807. /* Clear CCF flag */
  3808. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3809. /* Set the phase */
  3810. hcryp->Phase = CRYP_PHASE_PROCESS;
  3811. /* From that point the whole message must be processed, first the Header then the payload.
  3812. First the Header block(B1) : associated data length expressed in bytes concatenated with Associated Data (A)*/
  3813. if (hcryp->Init.HeaderSize != 0U)
  3814. {
  3815. if ((hcryp->Init.HeaderSize % 4U) == 0U)
  3816. {
  3817. /* HeaderSize %4, no padding */
  3818. for (loopcounter = 0U; (loopcounter < hcryp->Init.HeaderSize); loopcounter += 4U)
  3819. {
  3820. /* Write the Input block in the Data Input register */
  3821. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3822. hcryp->CrypHeaderCount++ ;
  3823. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3824. hcryp->CrypHeaderCount++ ;
  3825. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3826. hcryp->CrypHeaderCount++ ;
  3827. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3828. hcryp->CrypHeaderCount++ ;
  3829. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3830. {
  3831. /* Disable the CRYP peripheral clock */
  3832. __HAL_CRYP_DISABLE(hcryp);
  3833. /* Change state */
  3834. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3835. hcryp->State = HAL_CRYP_STATE_READY;
  3836. /* Process unlocked */
  3837. __HAL_UNLOCK(hcryp);
  3838. return HAL_ERROR;
  3839. }
  3840. /* Clear CCF Flag */
  3841. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3842. }
  3843. }
  3844. else
  3845. {
  3846. /*Write Header block in the IN FIFO without last block */
  3847. for (loopcounter = 0U; (loopcounter < ((hcryp->Init.HeaderSize) - (hcryp->Init.HeaderSize % 4U)));
  3848. loopcounter += 4U)
  3849. {
  3850. /* Write the input block in the data input register */
  3851. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3852. hcryp->CrypHeaderCount++ ;
  3853. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3854. hcryp->CrypHeaderCount++ ;
  3855. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3856. hcryp->CrypHeaderCount++ ;
  3857. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3858. hcryp->CrypHeaderCount++ ;
  3859. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3860. {
  3861. /* Disable the CRYP peripheral clock */
  3862. __HAL_CRYP_DISABLE(hcryp);
  3863. /* Change state */
  3864. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3865. hcryp->State = HAL_CRYP_STATE_READY;
  3866. /* Process unlocked */
  3867. __HAL_UNLOCK(hcryp);
  3868. return HAL_ERROR;
  3869. }
  3870. /* Clear CCF Flag */
  3871. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3872. }
  3873. /* Last block optionally pad the data with zeros*/
  3874. for (loopcounter = 0U; (loopcounter < (hcryp->Init.HeaderSize % 4U)); loopcounter++)
  3875. {
  3876. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  3877. hcryp->CrypHeaderCount++ ;
  3878. }
  3879. while (loopcounter < 4U)
  3880. {
  3881. /* Pad the data with zeros to have a complete block */
  3882. hcryp->Instance->DINR = 0x0U;
  3883. loopcounter++;
  3884. }
  3885. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3886. {
  3887. /* Disable the CRYP peripheral clock */
  3888. __HAL_CRYP_DISABLE(hcryp);
  3889. /* Change state */
  3890. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3891. hcryp->State = HAL_CRYP_STATE_READY;
  3892. /* Process unlocked */
  3893. __HAL_UNLOCK(hcryp);
  3894. return HAL_ERROR;
  3895. }
  3896. /* Clear CCF flag */
  3897. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3898. }
  3899. }
  3900. } /* if (DoKeyIVConfig == 1U) */
  3901. /* Then the payload: cleartext payload (not the ciphertext payload).
  3902. Write input Data, no output Data to get */
  3903. if (hcryp->Size != 0U)
  3904. {
  3905. if ((hcryp->Size % 16U) != 0U)
  3906. {
  3907. /* recalculate wordsize */
  3908. wordsize = ((wordsize / 4U) * 4U) ;
  3909. }
  3910. /* Get tick */
  3911. tickstart = HAL_GetTick();
  3912. /*Temporary CrypOutCount Value*/
  3913. outcount = hcryp->CrypOutCount;
  3914. while ((hcryp->CrypInCount < wordsize) && (outcount < wordsize))
  3915. {
  3916. /* Write plain data and get cipher data */
  3917. CRYP_AES_ProcessData(hcryp, Timeout);
  3918. /*Temporary CrypOutCount Value*/
  3919. outcount = hcryp->CrypOutCount;
  3920. /* Check for the Timeout */
  3921. if (Timeout != HAL_MAX_DELAY)
  3922. {
  3923. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  3924. {
  3925. /* Disable the CRYP peripheral clock */
  3926. __HAL_CRYP_DISABLE(hcryp);
  3927. /* Change state */
  3928. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3929. hcryp->State = HAL_CRYP_STATE_READY;
  3930. /* Process unlocked */
  3931. __HAL_UNLOCK(hcryp);
  3932. return HAL_ERROR;
  3933. }
  3934. }
  3935. }
  3936. if ((hcryp->Size % 16U) != 0U)
  3937. {
  3938. /* Compute the number of padding bytes in last block of payload */
  3939. npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size);
  3940. /* Number of valid words (lastwordsize) in last block */
  3941. if ((npblb % 4U) == 0U)
  3942. {
  3943. lastwordsize = (16U - npblb) / 4U;
  3944. }
  3945. else
  3946. {
  3947. lastwordsize = ((16U - npblb) / 4U) + 1U;
  3948. }
  3949. /* Last block optionally pad the data with zeros*/
  3950. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter ++)
  3951. {
  3952. /* Write the last input block in the IN FIFO */
  3953. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  3954. hcryp->CrypInCount++;
  3955. }
  3956. while (loopcounter < 4U)
  3957. {
  3958. /* Pad the data with zeros to have a complete block */
  3959. hcryp->Instance->DINR = 0U;
  3960. loopcounter++;
  3961. }
  3962. /* Wait for CCF flag to be raised */
  3963. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3964. {
  3965. /* Disable the CRYP peripheral clock */
  3966. __HAL_CRYP_DISABLE(hcryp);
  3967. /* Change state */
  3968. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  3969. hcryp->State = HAL_CRYP_STATE_READY;
  3970. /* Process unlocked */
  3971. __HAL_UNLOCK(hcryp);
  3972. return HAL_ERROR;
  3973. }
  3974. /* Clear CCF flag */
  3975. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  3976. }
  3977. }
  3978. #endif /* End AES or CRYP */
  3979. #if defined(CRYP)
  3980. /************************* Header phase *************************************/
  3981. /* Header block(B1) : associated data length expressed in bytes concatenated
  3982. with Associated Data (A)*/
  3983. if (CRYP_GCMCCM_SetHeaderPhase(hcryp, Timeout) != HAL_OK)
  3984. {
  3985. return HAL_ERROR;
  3986. }
  3987. /********************** Payload phase ***************************************/
  3988. /* Set the phase */
  3989. hcryp->Phase = CRYP_PHASE_PROCESS;
  3990. /* Disable the CRYP peripheral */
  3991. __HAL_CRYP_DISABLE(hcryp);
  3992. /* Select payload phase once the header phase is performed */
  3993. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  3994. /* Enable the CRYP peripheral */
  3995. __HAL_CRYP_ENABLE(hcryp);
  3996. } /* if (DoKeyIVConfig == 1U) */
  3997. if ((hcryp->Size % 16U) != 0U)
  3998. {
  3999. /* recalculate wordsize */
  4000. wordsize = ((wordsize / 4U) * 4U) ;
  4001. }
  4002. /* Get tick */
  4003. tickstart = HAL_GetTick();
  4004. /*Temporary CrypOutCount Value*/
  4005. outcount = hcryp->CrypOutCount;
  4006. /* Write input data and get output data */
  4007. while ((hcryp->CrypInCount < wordsize) && (outcount < wordsize))
  4008. {
  4009. /* Write plain data and get cipher data */
  4010. CRYP_AES_ProcessData(hcryp, Timeout);
  4011. /* Check for the Timeout */
  4012. if (Timeout != HAL_MAX_DELAY)
  4013. {
  4014. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  4015. {
  4016. /* Disable the CRYP peripheral clock */
  4017. __HAL_CRYP_DISABLE(hcryp);
  4018. /* Change state */
  4019. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4020. hcryp->State = HAL_CRYP_STATE_READY;
  4021. /* Process unlocked */
  4022. __HAL_UNLOCK(hcryp);
  4023. return HAL_ERROR;
  4024. }
  4025. }
  4026. }
  4027. if ((hcryp->Size % 16U) != 0U)
  4028. {
  4029. /* CRYP Workaround : CRYP1 generates correct TAG during CCM decryption
  4030. only when ciphertext blocks size is multiple of 128 bits. If lthe size of
  4031. the last block of payload is inferior to 128 bits, when CCM decryption
  4032. is selected, then the TAG message will be wrong.*/
  4033. CRYP_Workaround(hcryp, Timeout);
  4034. }
  4035. #endif /* CRYP */
  4036. /* Return function status */
  4037. return HAL_OK;
  4038. }
  4039. /**
  4040. * @brief AES CCM encryption/decryption process in interrupt mode
  4041. * for TinyAES IP, no encrypt/decrypt performed, only authentication preparation.
  4042. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4043. * the configuration information for CRYP module
  4044. * @retval HAL status
  4045. */
  4046. static HAL_StatusTypeDef CRYP_AESCCM_Process_IT(CRYP_HandleTypeDef *hcryp)
  4047. {
  4048. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  4049. #if defined(CRYP)
  4050. __IO uint32_t count = 0U;
  4051. #endif /* CRYP */
  4052. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  4053. {
  4054. if (hcryp->KeyIVConfig == 1U)
  4055. {
  4056. /* If the Key and IV configuration has to be done only once
  4057. and if it has already been done, skip it */
  4058. DoKeyIVConfig = 0U;
  4059. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  4060. }
  4061. else
  4062. {
  4063. /* If the Key and IV configuration has to be done only once
  4064. and if it has not been done already, do it and set KeyIVConfig
  4065. to keep track it won't have to be done again next time */
  4066. hcryp->KeyIVConfig = 1U;
  4067. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  4068. }
  4069. }
  4070. else
  4071. {
  4072. hcryp->SizesSum = hcryp->Size;
  4073. }
  4074. /* Configure Key, IV and process message (header and payload) */
  4075. if (DoKeyIVConfig == 1U)
  4076. {
  4077. /* Reset CrypHeaderCount */
  4078. hcryp->CrypHeaderCount = 0U;
  4079. #if defined(CRYP)
  4080. /************ Init phase ************/
  4081. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  4082. /* Set the key */
  4083. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  4084. /* Set the initialization vector (IV) with CTR1 information */
  4085. hcryp->Instance->IV0LR = (hcryp->Init.B0[0]) & CRYP_CCM_CTR1_0;
  4086. hcryp->Instance->IV0RR = hcryp->Init.B0[1];
  4087. hcryp->Instance->IV1LR = hcryp->Init.B0[2];
  4088. hcryp->Instance->IV1RR = (hcryp->Init.B0[3] & CRYP_CCM_CTR1_1) | CRYP_CCM_CTR1_2;
  4089. /* Enable the CRYP peripheral */
  4090. __HAL_CRYP_ENABLE(hcryp);
  4091. /*Write the B0 packet into CRYP_DIN Register*/
  4092. if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
  4093. {
  4094. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0));
  4095. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 1));
  4096. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 2));
  4097. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 3));
  4098. }
  4099. else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
  4100. {
  4101. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0), 16);
  4102. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 1), 16);
  4103. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 2), 16);
  4104. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 3), 16);
  4105. }
  4106. else if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
  4107. {
  4108. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0));
  4109. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 1));
  4110. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 2));
  4111. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 3));
  4112. }
  4113. else
  4114. {
  4115. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0);
  4116. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 1);
  4117. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 2);
  4118. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 3);
  4119. }
  4120. /*Wait for the CRYPEN bit to be cleared*/
  4121. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4122. do
  4123. {
  4124. count-- ;
  4125. if (count == 0U)
  4126. {
  4127. /* Disable the CRYP peripheral clock */
  4128. __HAL_CRYP_DISABLE(hcryp);
  4129. /* Change state */
  4130. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4131. hcryp->State = HAL_CRYP_STATE_READY;
  4132. /* Process unlocked */
  4133. __HAL_UNLOCK(hcryp);
  4134. return HAL_ERROR;
  4135. }
  4136. } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
  4137. /* Select header phase */
  4138. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4139. } /* end of if (DoKeyIVConfig == 1U) */
  4140. /* Enable interrupts */
  4141. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI);
  4142. /* Enable CRYP */
  4143. __HAL_CRYP_ENABLE(hcryp);
  4144. #else /* AES */
  4145. /*AES2v1.1.1 : CCM authentication : no init phase, only header and final phase */
  4146. /* Select header phase */
  4147. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4148. /* configured mode and encryption mode */
  4149. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  4150. /* Set the key */
  4151. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  4152. /* Set the initialization vector with zero values*/
  4153. hcryp->Instance->IVR3 = 0U;
  4154. hcryp->Instance->IVR2 = 0U;
  4155. hcryp->Instance->IVR1 = 0U;
  4156. hcryp->Instance->IVR0 = 0U;
  4157. /* Enable interrupts */
  4158. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  4159. /* Enable the CRYP peripheral */
  4160. __HAL_CRYP_ENABLE(hcryp);
  4161. /*Write the B0 packet into CRYP_DIN*/
  4162. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0);
  4163. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 1);
  4164. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 2);
  4165. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 3);
  4166. } /* end of if (DoKeyIVConfig == 1U) */
  4167. #endif /* End AES or CRYP */
  4168. /* Return function status */
  4169. return HAL_OK;
  4170. }
  4171. /**
  4172. * @brief AES CCM encryption/decryption process in DMA mode
  4173. * for TinyAES IP, no encrypt/decrypt performed, only authentication preparation.
  4174. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4175. * the configuration information for CRYP module
  4176. * @retval HAL status
  4177. */
  4178. static HAL_StatusTypeDef CRYP_AESCCM_Process_DMA(CRYP_HandleTypeDef *hcryp)
  4179. {
  4180. uint32_t wordsize;
  4181. __IO uint32_t count = 0U;
  4182. uint32_t DoKeyIVConfig = 1U; /* By default, carry out peripheral Key and IV configuration */
  4183. #if defined(AES)
  4184. uint32_t loopcounter;
  4185. uint32_t npblb;
  4186. uint32_t lastwordsize;
  4187. #endif /* AES */
  4188. if (hcryp->Init.KeyIVConfigSkip == CRYP_KEYIVCONFIG_ONCE)
  4189. {
  4190. if (hcryp->KeyIVConfig == 1U)
  4191. {
  4192. /* If the Key and IV configuration has to be done only once
  4193. and if it has already been done, skip it */
  4194. DoKeyIVConfig = 0U;
  4195. hcryp->SizesSum += hcryp->Size; /* Compute message total payload length */
  4196. }
  4197. else
  4198. {
  4199. /* If the Key and IV configuration has to be done only once
  4200. and if it has not been done already, do it and set KeyIVConfig
  4201. to keep track it won't have to be done again next time */
  4202. hcryp->KeyIVConfig = 1U;
  4203. hcryp->SizesSum = hcryp->Size; /* Merely store payload length */
  4204. }
  4205. }
  4206. else
  4207. {
  4208. hcryp->SizesSum = hcryp->Size;
  4209. }
  4210. if (DoKeyIVConfig == 1U)
  4211. {
  4212. /* Reset CrypHeaderCount */
  4213. hcryp->CrypHeaderCount = 0U;
  4214. #if defined(CRYP)
  4215. /************************** Init phase **************************************/
  4216. CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  4217. /* Set the key */
  4218. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  4219. /* Set the initialization vector (IV) with CTR1 information */
  4220. hcryp->Instance->IV0LR = (hcryp->Init.B0[0]) & CRYP_CCM_CTR1_0;
  4221. hcryp->Instance->IV0RR = hcryp->Init.B0[1];
  4222. hcryp->Instance->IV1LR = hcryp->Init.B0[2];
  4223. hcryp->Instance->IV1RR = (hcryp->Init.B0[3] & CRYP_CCM_CTR1_1) | CRYP_CCM_CTR1_2;
  4224. /* Enable the CRYP peripheral */
  4225. __HAL_CRYP_ENABLE(hcryp);
  4226. /*Write the B0 packet into CRYP_DIN Register*/
  4227. if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
  4228. {
  4229. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0));
  4230. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 1));
  4231. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 2));
  4232. hcryp->Instance->DIN = __REV(*(uint32_t *)(hcryp->Init.B0 + 3));
  4233. }
  4234. else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
  4235. {
  4236. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0), 16);
  4237. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 1), 16);
  4238. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 2), 16);
  4239. hcryp->Instance->DIN = __ROR(*(uint32_t *)(hcryp->Init.B0 + 3), 16);
  4240. }
  4241. else if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
  4242. {
  4243. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0));
  4244. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 1));
  4245. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 2));
  4246. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(hcryp->Init.B0 + 3));
  4247. }
  4248. else
  4249. {
  4250. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0);
  4251. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 1);
  4252. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 2);
  4253. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.B0 + 3);
  4254. }
  4255. /*Wait for the CRYPEN bit to be cleared*/
  4256. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4257. do
  4258. {
  4259. count-- ;
  4260. if (count == 0U)
  4261. {
  4262. /* Disable the CRYP peripheral clock */
  4263. __HAL_CRYP_DISABLE(hcryp);
  4264. /* Change state */
  4265. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4266. hcryp->State = HAL_CRYP_STATE_READY;
  4267. /* Process unlocked */
  4268. __HAL_UNLOCK(hcryp);
  4269. return HAL_ERROR;
  4270. }
  4271. } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
  4272. #else /* AES */
  4273. /*AES2v1.1.1 : CCM authentication : no init phase, only header and final phase */
  4274. /* Select header phase */
  4275. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4276. /* configured encryption mode */
  4277. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_OPERATINGMODE_ENCRYPT);
  4278. /* Set the key */
  4279. CRYP_SetKey(hcryp, hcryp->Init.KeySize);
  4280. /* Set the initialization vector with zero values*/
  4281. hcryp->Instance->IVR3 = 0U;
  4282. hcryp->Instance->IVR2 = 0U;
  4283. hcryp->Instance->IVR1 = 0U;
  4284. hcryp->Instance->IVR0 = 0U;
  4285. /* Enable the CRYP peripheral */
  4286. __HAL_CRYP_ENABLE(hcryp);
  4287. /*Write the B0 packet into CRYP_DIN*/
  4288. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0);
  4289. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 1);
  4290. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 2);
  4291. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.B0 + 3);
  4292. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4293. do
  4294. {
  4295. count-- ;
  4296. if (count == 0U)
  4297. {
  4298. /* Disable the CRYP peripheral clock */
  4299. __HAL_CRYP_DISABLE(hcryp);
  4300. /* Change state */
  4301. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4302. hcryp->State = HAL_CRYP_STATE_READY;
  4303. /* Process Unlocked */
  4304. __HAL_UNLOCK(hcryp);
  4305. return HAL_ERROR;
  4306. }
  4307. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  4308. /* Clear CCF flag */
  4309. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4310. /* Set the phase */
  4311. hcryp->Phase = CRYP_PHASE_PROCESS;
  4312. /* From that point the whole message must be processed, first the Header then the payload.
  4313. First the Header block(B1) : associated data length expressed in bytes concatenated with Associated Data (A)*/
  4314. if (hcryp->Init.HeaderSize != 0U)
  4315. {
  4316. if ((hcryp->Init.HeaderSize % 4U) == 0U)
  4317. {
  4318. /* HeaderSize %4, no padding */
  4319. for (loopcounter = 0U; (loopcounter < hcryp->Init.HeaderSize); loopcounter += 4U)
  4320. {
  4321. /* Write the Input block in the Data Input register */
  4322. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4323. hcryp->CrypHeaderCount++ ;
  4324. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4325. hcryp->CrypHeaderCount++ ;
  4326. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4327. hcryp->CrypHeaderCount++ ;
  4328. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4329. hcryp->CrypHeaderCount++ ;
  4330. /* wait until the end of computation */
  4331. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4332. do
  4333. {
  4334. count-- ;
  4335. if (count == 0U)
  4336. {
  4337. /* Disable the CRYP peripheral clock */
  4338. __HAL_CRYP_DISABLE(hcryp);
  4339. /* Change state */
  4340. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4341. hcryp->State = HAL_CRYP_STATE_READY;
  4342. /* Process Unlocked */
  4343. __HAL_UNLOCK(hcryp);
  4344. return HAL_ERROR;
  4345. }
  4346. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  4347. /* Clear CCF flag */
  4348. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4349. }
  4350. }
  4351. else
  4352. {
  4353. /*Write Header block in the IN FIFO without last block */
  4354. for (loopcounter = 0U; (loopcounter < ((hcryp->Init.HeaderSize) - (hcryp->Init.HeaderSize % 4U)));
  4355. loopcounter += 4U)
  4356. {
  4357. /* Write the input block in the data input register */
  4358. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4359. hcryp->CrypHeaderCount++ ;
  4360. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4361. hcryp->CrypHeaderCount++ ;
  4362. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4363. hcryp->CrypHeaderCount++ ;
  4364. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4365. hcryp->CrypHeaderCount++ ;
  4366. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4367. do
  4368. {
  4369. count-- ;
  4370. if (count == 0U)
  4371. {
  4372. /* Disable the CRYP peripheral clock */
  4373. __HAL_CRYP_DISABLE(hcryp);
  4374. /* Change state */
  4375. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4376. hcryp->State = HAL_CRYP_STATE_READY;
  4377. /* Process Unlocked */
  4378. __HAL_UNLOCK(hcryp);
  4379. return HAL_ERROR;
  4380. }
  4381. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  4382. /* Clear CCF flag */
  4383. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4384. }
  4385. /* Last block optionally pad the data with zeros*/
  4386. for (loopcounter = 0U; (loopcounter < (hcryp->Init.HeaderSize % 4U)); loopcounter++)
  4387. {
  4388. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4389. hcryp->CrypHeaderCount++ ;
  4390. }
  4391. while (loopcounter < 4U)
  4392. {
  4393. /* Pad the data with zeros to have a complete block */
  4394. hcryp->Instance->DINR = 0x0U;
  4395. loopcounter++;
  4396. }
  4397. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4398. do
  4399. {
  4400. count-- ;
  4401. if (count == 0U)
  4402. {
  4403. /* Disable the CRYP peripheral clock */
  4404. __HAL_CRYP_DISABLE(hcryp);
  4405. /* Change state */
  4406. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4407. hcryp->State = HAL_CRYP_STATE_READY;
  4408. /* Process Unlocked */
  4409. __HAL_UNLOCK(hcryp);
  4410. return HAL_ERROR;
  4411. }
  4412. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  4413. /* Clear CCF flag */
  4414. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4415. }
  4416. }
  4417. } /* if (DoKeyIVConfig == 1U) */
  4418. /* Then the payload: cleartext payload (not the ciphertext payload).
  4419. Write input Data, no output Data to get */
  4420. if (hcryp->Size != 0U)
  4421. {
  4422. if (hcryp->Size >= 16U)
  4423. {
  4424. if ((hcryp->Size % 16U) == 0U)
  4425. {
  4426. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (hcryp->Size / 4U),
  4427. (uint32_t)(hcryp->pCrypOutBuffPtr));
  4428. }
  4429. else /*to compute last word<128bits, otherwise it will not be encrypted/decrypted */
  4430. {
  4431. wordsize = (uint32_t)(hcryp->Size) + (16U - ((uint32_t)(hcryp->Size) % 16U)) ;
  4432. /* Set the input and output addresses and start DMA transfer, pCrypOutBuffPtr size should be %4 */
  4433. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), ((uint16_t)wordsize / 4U),
  4434. (uint32_t)(hcryp->pCrypOutBuffPtr));
  4435. }
  4436. }
  4437. if ((hcryp->Size < 16U) != 0U)
  4438. {
  4439. /* Compute the number of padding bytes in last block of payload */
  4440. npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size);
  4441. /* Number of valid words (lastwordsize) in last block */
  4442. if ((npblb % 4U) == 0U)
  4443. {
  4444. lastwordsize = (16U - npblb) / 4U;
  4445. }
  4446. else
  4447. {
  4448. lastwordsize = ((16U - npblb) / 4U) + 1U;
  4449. }
  4450. /* Last block optionally pad the data with zeros*/
  4451. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter ++)
  4452. {
  4453. /* Write the last input block in the IN FIFO */
  4454. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4455. hcryp->CrypInCount++;
  4456. }
  4457. while (loopcounter < 4U)
  4458. {
  4459. /* Pad the data with zeros to have a complete block */
  4460. hcryp->Instance->DINR = 0U;
  4461. loopcounter++;
  4462. }
  4463. count = CRYP_TIMEOUT_GCMCCMINITPHASE;
  4464. do
  4465. {
  4466. count-- ;
  4467. if (count == 0U)
  4468. {
  4469. /* Disable the CRYP peripheral clock */
  4470. __HAL_CRYP_DISABLE(hcryp);
  4471. /* Change state */
  4472. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4473. hcryp->State = HAL_CRYP_STATE_READY;
  4474. /* Process Unlocked */
  4475. __HAL_UNLOCK(hcryp);
  4476. return HAL_ERROR;
  4477. }
  4478. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  4479. /* Clear CCF flag */
  4480. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4481. /* Process unlocked */
  4482. __HAL_UNLOCK(hcryp);
  4483. /* Change the CRYP state and phase */
  4484. hcryp->State = HAL_CRYP_STATE_READY;
  4485. }
  4486. }
  4487. else
  4488. {
  4489. /* Process unLocked */
  4490. __HAL_UNLOCK(hcryp);
  4491. /* Change the CRYP state and phase */
  4492. hcryp->State = HAL_CRYP_STATE_READY;
  4493. }
  4494. #endif /* AES */
  4495. #if defined(CRYP)
  4496. /********************* Header phase *****************************************/
  4497. if (CRYP_GCMCCM_SetHeaderPhase_DMA(hcryp) != HAL_OK)
  4498. {
  4499. return HAL_ERROR;
  4500. }
  4501. /******************** Payload phase *****************************************/
  4502. /* Set the phase */
  4503. hcryp->Phase = CRYP_PHASE_PROCESS;
  4504. /* Disable the CRYP peripheral */
  4505. __HAL_CRYP_DISABLE(hcryp);
  4506. /* Select payload phase once the header phase is performed */
  4507. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  4508. } /* if (DoKeyIVConfig == 1U) */
  4509. if (hcryp->Size != 0U)
  4510. {
  4511. /* Size should be %4 otherwise Tag will be incorrectly generated for GCM Encryption & CCM Decryption
  4512. Workaround is implemented in polling mode, so if last block of
  4513. payload <128bit don't use HAL_CRYP_AESGCM_DMA otherwise TAG is incorrectly generated for GCM Encryption. */
  4514. /* Set the input and output addresses and start DMA transfer */
  4515. if ((hcryp->Size % 16U) == 0U)
  4516. {
  4517. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), hcryp->Size / 4U, (uint32_t)(hcryp->pCrypOutBuffPtr));
  4518. }
  4519. else
  4520. {
  4521. wordsize = (uint32_t)(hcryp->Size) + 16U - ((uint32_t)(hcryp->Size) % 16U) ;
  4522. /* Set the input and output addresses and start DMA transfer, pCrypOutBuffPtr size should be %4*/
  4523. CRYP_SetDMAConfig(hcryp, (uint32_t)(hcryp->pCrypInBuffPtr), (uint16_t)wordsize / 4U,
  4524. (uint32_t)(hcryp->pCrypOutBuffPtr));
  4525. }
  4526. }
  4527. else /*Size = 0*/
  4528. {
  4529. /* Process unlocked */
  4530. __HAL_UNLOCK(hcryp);
  4531. /* Change the CRYP state and phase */
  4532. hcryp->State = HAL_CRYP_STATE_READY;
  4533. }
  4534. #endif /* CRYP */
  4535. /* Return function status */
  4536. return HAL_OK;
  4537. }
  4538. /**
  4539. * @brief Sets the payload phase in interrupt mode
  4540. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4541. * the configuration information for CRYP module
  4542. * @retval state
  4543. */
  4544. static void CRYP_GCMCCM_SetPayloadPhase_IT(CRYP_HandleTypeDef *hcryp)
  4545. {
  4546. uint32_t loopcounter;
  4547. uint32_t temp[4]; /* Temporary CrypOutBuff */
  4548. uint32_t lastwordsize;
  4549. uint32_t npblb;
  4550. uint32_t i;
  4551. #if defined(AES)
  4552. uint16_t outcount; /* Temporary CrypOutCount Value */
  4553. #endif /* AES */
  4554. /***************************** Payload phase *******************************/
  4555. #if defined(CRYP)
  4556. if (hcryp->Size == 0U)
  4557. {
  4558. /* Disable interrupts */
  4559. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  4560. /* Process unlocked */
  4561. __HAL_UNLOCK(hcryp);
  4562. /* Change the CRYP state */
  4563. hcryp->State = HAL_CRYP_STATE_READY;
  4564. }
  4565. else if (((hcryp->Size / 4U) - (hcryp->CrypInCount)) >= 4U)
  4566. {
  4567. /* Write the input block in the IN FIFO */
  4568. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4569. hcryp->CrypInCount++;
  4570. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4571. hcryp->CrypInCount++;
  4572. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4573. hcryp->CrypInCount++;
  4574. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4575. hcryp->CrypInCount++;
  4576. if (((hcryp->Size / 4U) == hcryp->CrypInCount) && ((hcryp->Size % 16U) == 0U))
  4577. {
  4578. /* Disable interrupts */
  4579. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  4580. /* Call the input data transfer complete callback */
  4581. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  4582. /*Call registered Input complete callback*/
  4583. hcryp->InCpltCallback(hcryp);
  4584. #else
  4585. /*Call legacy weak Input complete callback*/
  4586. HAL_CRYP_InCpltCallback(hcryp);
  4587. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4588. }
  4589. if (hcryp->CrypOutCount < (hcryp->Size / 4U))
  4590. {
  4591. /* Read the output block from the Output FIFO and put them in temporary buffer
  4592. then get CrypOutBuff from temporary buffer */
  4593. for (i = 0U; i < 4U; i++)
  4594. {
  4595. temp[i] = hcryp->Instance->DOUT;
  4596. }
  4597. i = 0U;
  4598. while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 4U))
  4599. {
  4600. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  4601. hcryp->CrypOutCount++;
  4602. i++;
  4603. }
  4604. if (((hcryp->Size / 4U) == hcryp->CrypOutCount) && ((hcryp->Size % 16U) == 0U))
  4605. {
  4606. /* Disable interrupts */
  4607. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  4608. /* Change the CRYP state */
  4609. hcryp->State = HAL_CRYP_STATE_READY;
  4610. /* Disable CRYP */
  4611. __HAL_CRYP_DISABLE(hcryp);
  4612. /* Process unlocked */
  4613. __HAL_UNLOCK(hcryp);
  4614. /* Call output transfer complete callback */
  4615. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  4616. /*Call registered Output complete callback*/
  4617. hcryp->OutCpltCallback(hcryp);
  4618. #else
  4619. /*Call legacy weak Output complete callback*/
  4620. HAL_CRYP_OutCpltCallback(hcryp);
  4621. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4622. }
  4623. }
  4624. }
  4625. else if ((hcryp->Size % 16U) != 0U)
  4626. {
  4627. /* Size should be %4 in word and %16 in byte otherwise TAG will
  4628. be incorrectly generated for GCM Encryption & CCM Decryption
  4629. Workaround is implemented in polling mode, so if last block of
  4630. payload <128bit don't use CRYP_AESGCM_Encrypt_IT otherwise TAG is incorrectly generated. */
  4631. /* Compute the number of padding bytes in last block of payload */
  4632. npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size);
  4633. /* Number of valid words (lastwordsize) in last block */
  4634. if ((npblb % 4U) == 0U)
  4635. {
  4636. lastwordsize = (16U - npblb) / 4U;
  4637. }
  4638. else
  4639. {
  4640. lastwordsize = ((16U - npblb) / 4U) + 1U;
  4641. }
  4642. /* Last block optionally pad the data with zeros*/
  4643. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++)
  4644. {
  4645. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4646. hcryp->CrypInCount++;
  4647. }
  4648. while (loopcounter < 4U)
  4649. {
  4650. /* Pad the data with zeros to have a complete block */
  4651. hcryp->Instance->DIN = 0x0U;
  4652. loopcounter++;
  4653. }
  4654. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  4655. if ((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U)
  4656. {
  4657. for (i = 0U; i < 4U; i++)
  4658. {
  4659. temp[i] = hcryp->Instance->DOUT;
  4660. }
  4661. if (((hcryp->Size) / 4U) == 0U)
  4662. {
  4663. for (i = 0U; i < ((uint32_t)(hcryp->Size) % 4U); i++)
  4664. {
  4665. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  4666. hcryp->CrypOutCount++;
  4667. }
  4668. }
  4669. i = 0x0U;
  4670. while (((hcryp->CrypOutCount < ((hcryp->Size) / 4U))) && (i < 4U))
  4671. {
  4672. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  4673. hcryp->CrypOutCount++;
  4674. i++;
  4675. }
  4676. }
  4677. if (hcryp->CrypOutCount >= (hcryp->Size / 4U))
  4678. {
  4679. /* Disable interrupts */
  4680. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI | CRYP_IT_INI);
  4681. /* Change the CRYP peripheral state */
  4682. hcryp->State = HAL_CRYP_STATE_READY;
  4683. /* Process unlocked */
  4684. __HAL_UNLOCK(hcryp);
  4685. /* Call output transfer complete callback */
  4686. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  4687. /*Call registered Output complete callback*/
  4688. hcryp->OutCpltCallback(hcryp);
  4689. #else
  4690. /*Call legacy weak Output complete callback*/
  4691. HAL_CRYP_OutCpltCallback(hcryp);
  4692. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4693. }
  4694. }
  4695. else
  4696. {
  4697. /* Nothing to do */
  4698. }
  4699. #else /* AES */
  4700. /* Read the output block from the output FIFO and put them in temporary buffer
  4701. then get CrypOutBuff from temporary buffer*/
  4702. for (i = 0U; i < 4U; i++)
  4703. {
  4704. temp[i] = hcryp->Instance->DOUTR;
  4705. }
  4706. i = 0U;
  4707. while ((hcryp->CrypOutCount < ((hcryp->Size + 3U) / 4U)) && (i < 4U))
  4708. {
  4709. *(uint32_t *)(hcryp->pCrypOutBuffPtr + hcryp->CrypOutCount) = temp[i];
  4710. hcryp->CrypOutCount++;
  4711. i++;
  4712. }
  4713. /*Temporary CrypOutCount Value*/
  4714. outcount = hcryp->CrypOutCount;
  4715. if ((hcryp->CrypOutCount >= (hcryp->Size / 4U)) && ((outcount * 4U) >= hcryp->Size))
  4716. {
  4717. /* Disable computation complete flag and errors interrupts */
  4718. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  4719. /* Change the CRYP state */
  4720. hcryp->State = HAL_CRYP_STATE_READY;
  4721. /* Process unlocked */
  4722. __HAL_UNLOCK(hcryp);
  4723. /* Call output transfer complete callback */
  4724. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  4725. /*Call registered Output complete callback*/
  4726. hcryp->OutCpltCallback(hcryp);
  4727. #else
  4728. /*Call legacy weak Output complete callback*/
  4729. HAL_CRYP_OutCpltCallback(hcryp);
  4730. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4731. }
  4732. else if (((hcryp->Size / 4U) - (hcryp->CrypInCount)) >= 4U)
  4733. {
  4734. /* Write the input block in the IN FIFO */
  4735. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4736. hcryp->CrypInCount++;
  4737. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4738. hcryp->CrypInCount++;
  4739. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4740. hcryp->CrypInCount++;
  4741. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4742. hcryp->CrypInCount++;
  4743. if ((hcryp->CrypInCount == (hcryp->Size / 4U)) && ((hcryp->Size % 16U) == 0U))
  4744. {
  4745. /* Call Input transfer complete callback */
  4746. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  4747. /*Call registered Input complete callback*/
  4748. hcryp->InCpltCallback(hcryp);
  4749. #else
  4750. /*Call legacy weak Input complete callback*/
  4751. HAL_CRYP_InCpltCallback(hcryp);
  4752. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  4753. }
  4754. }
  4755. else /* Last block of payload < 128bit*/
  4756. {
  4757. /* Workaround not implemented, Size should be %4 otherwise Tag will be incorrectly
  4758. generated for GCM Encryption & CCM Decryption. Workaround is implemented in polling mode,
  4759. so if last block of payload <128bit don't use CRYP_Encrypt_IT otherwise TAG is incorrectly
  4760. generated for GCM Encryption & CCM Decryption. */
  4761. /* Compute the number of padding bytes in last block of payload */
  4762. npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size);
  4763. /* Number of valid words (lastwordsize) in last block */
  4764. if ((npblb % 4U) == 0U)
  4765. {
  4766. lastwordsize = (16U - npblb) / 4U;
  4767. }
  4768. else
  4769. {
  4770. lastwordsize = ((16U - npblb) / 4U) + 1U;
  4771. }
  4772. /* Last block optionally pad the data with zeros*/
  4773. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++)
  4774. {
  4775. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  4776. hcryp->CrypInCount++;
  4777. }
  4778. while (loopcounter < 4U)
  4779. {
  4780. /* pad the data with zeros to have a complete block */
  4781. hcryp->Instance->DINR = 0x0U;
  4782. loopcounter++;
  4783. }
  4784. }
  4785. #endif /* AES */
  4786. }
  4787. /**
  4788. * @brief Sets the header phase in polling mode
  4789. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4790. * the configuration information for CRYP module(Header & HeaderSize)
  4791. * @param Timeout: Timeout value
  4792. * @retval state
  4793. */
  4794. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  4795. {
  4796. uint32_t loopcounter;
  4797. uint32_t size_in_bytes;
  4798. uint32_t tmp;
  4799. uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */
  4800. 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */
  4801. 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU
  4802. }; /* 8-bit data type */
  4803. /***************************** Header phase for GCM/GMAC or CCM *********************************/
  4804. if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD)
  4805. {
  4806. size_in_bytes = hcryp->Init.HeaderSize * 4U;
  4807. }
  4808. else
  4809. {
  4810. size_in_bytes = hcryp->Init.HeaderSize;
  4811. }
  4812. if (size_in_bytes != 0U)
  4813. {
  4814. #if defined(CRYP)
  4815. /* Select header phase */
  4816. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4817. /* Enable the CRYP peripheral */
  4818. __HAL_CRYP_ENABLE(hcryp);
  4819. if ((size_in_bytes % 16U) == 0U)
  4820. {
  4821. /* HeaderSize %4, no padding */
  4822. for (loopcounter = 0U; (loopcounter < (size_in_bytes / 4U)); loopcounter += 4U)
  4823. {
  4824. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4825. hcryp->CrypHeaderCount++ ;
  4826. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4827. hcryp->CrypHeaderCount++ ;
  4828. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4829. hcryp->CrypHeaderCount++ ;
  4830. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4831. hcryp->CrypHeaderCount++ ;
  4832. /* Wait for IFEM to be raised */
  4833. if (CRYP_WaitOnIFEMFlag(hcryp, Timeout) != HAL_OK)
  4834. {
  4835. /* Disable the CRYP peripheral clock */
  4836. __HAL_CRYP_DISABLE(hcryp);
  4837. /* Change state */
  4838. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4839. hcryp->State = HAL_CRYP_STATE_READY;
  4840. /* Process unlocked */
  4841. __HAL_UNLOCK(hcryp);
  4842. return HAL_ERROR;
  4843. }
  4844. }
  4845. }
  4846. else
  4847. {
  4848. /*Write header block in the IN FIFO without last block */
  4849. for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 16U) * 4U)); loopcounter += 4U)
  4850. {
  4851. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4852. hcryp->CrypHeaderCount++ ;
  4853. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4854. hcryp->CrypHeaderCount++ ;
  4855. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4856. hcryp->CrypHeaderCount++ ;
  4857. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4858. hcryp->CrypHeaderCount++ ;
  4859. /* Wait for IFEM to be raised */
  4860. if (CRYP_WaitOnIFEMFlag(hcryp, Timeout) != HAL_OK)
  4861. {
  4862. /* Disable the CRYP peripheral clock */
  4863. __HAL_CRYP_DISABLE(hcryp);
  4864. /* Change state */
  4865. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4866. hcryp->State = HAL_CRYP_STATE_READY;
  4867. /* Process unlocked */
  4868. __HAL_UNLOCK(hcryp);
  4869. return HAL_ERROR;
  4870. }
  4871. }
  4872. /* Last block optionally pad the data with zeros*/
  4873. for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 4U) % 4U)); loopcounter++)
  4874. {
  4875. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4876. hcryp->CrypHeaderCount++ ;
  4877. }
  4878. /* If the header size is a multiple of words */
  4879. if ((size_in_bytes % 4U) == 0U)
  4880. {
  4881. /* Pad the data with zeros to have a complete block */
  4882. while (loopcounter < 4U)
  4883. {
  4884. hcryp->Instance->DIN = 0x0U;
  4885. loopcounter++;
  4886. }
  4887. }
  4888. else
  4889. {
  4890. /* Enter last bytes, padded with zeroes */
  4891. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4892. tmp &= mask[(((hcryp->Init.DataType) >> 5) * 2U) + (size_in_bytes % 4U)];
  4893. hcryp->Instance->DIN = tmp;
  4894. loopcounter++;
  4895. /* Pad the data with zeros to have a complete block */
  4896. while (loopcounter < 4U)
  4897. {
  4898. hcryp->Instance->DIN = 0x0U;
  4899. loopcounter++;
  4900. }
  4901. }
  4902. /* Wait for CCF IFEM to be raised */
  4903. if (CRYP_WaitOnIFEMFlag(hcryp, Timeout) != HAL_OK)
  4904. {
  4905. /* Disable the CRYP peripheral clock */
  4906. __HAL_CRYP_DISABLE(hcryp);
  4907. /* Change state */
  4908. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4909. hcryp->State = HAL_CRYP_STATE_READY;
  4910. /* Process unlocked */
  4911. __HAL_UNLOCK(hcryp);
  4912. return HAL_ERROR;
  4913. }
  4914. }
  4915. /* Wait until the complete message has been processed */
  4916. if (CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK)
  4917. {
  4918. /* Disable the CRYP peripheral clock */
  4919. __HAL_CRYP_DISABLE(hcryp);
  4920. /* Change state */
  4921. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4922. hcryp->State = HAL_CRYP_STATE_READY;
  4923. /* Process unlocked & return error */
  4924. __HAL_UNLOCK(hcryp);
  4925. return HAL_ERROR;
  4926. }
  4927. #else /* AES */
  4928. if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)
  4929. {
  4930. /* Workaround 1 :only AES before re-enabling the IP, datatype can be configured.*/
  4931. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
  4932. /* Select header phase */
  4933. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  4934. /* Enable the CRYP peripheral */
  4935. __HAL_CRYP_ENABLE(hcryp);
  4936. }
  4937. /* If size_in_bytes is a multiple of blocks (a multiple of four 32-bits words ) */
  4938. if ((size_in_bytes % 16U) == 0U)
  4939. {
  4940. /* No padding */
  4941. for (loopcounter = 0U; (loopcounter < (size_in_bytes / 4U)); loopcounter += 4U)
  4942. {
  4943. /* Write the input block in the data input register */
  4944. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4945. hcryp->CrypHeaderCount++ ;
  4946. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4947. hcryp->CrypHeaderCount++ ;
  4948. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4949. hcryp->CrypHeaderCount++ ;
  4950. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4951. hcryp->CrypHeaderCount++ ;
  4952. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  4953. {
  4954. /* Disable the CRYP peripheral clock */
  4955. __HAL_CRYP_DISABLE(hcryp);
  4956. /* Change state */
  4957. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4958. hcryp->State = HAL_CRYP_STATE_READY;
  4959. /* Process unlocked */
  4960. __HAL_UNLOCK(hcryp);
  4961. return HAL_ERROR;
  4962. }
  4963. /* Clear CCF flag */
  4964. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4965. }
  4966. }
  4967. else
  4968. {
  4969. /*Write header block in the IN FIFO without last block */
  4970. for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 16U) * 4U)); loopcounter += 4U)
  4971. {
  4972. /* Write the input block in the data input register */
  4973. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4974. hcryp->CrypHeaderCount++ ;
  4975. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4976. hcryp->CrypHeaderCount++ ;
  4977. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4978. hcryp->CrypHeaderCount++ ;
  4979. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4980. hcryp->CrypHeaderCount++ ;
  4981. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  4982. {
  4983. /* Disable the CRYP peripheral clock */
  4984. __HAL_CRYP_DISABLE(hcryp);
  4985. /* Change state */
  4986. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  4987. hcryp->State = HAL_CRYP_STATE_READY;
  4988. /* Process unlocked */
  4989. __HAL_UNLOCK(hcryp);
  4990. return HAL_ERROR;
  4991. }
  4992. /* Clear CCF flag */
  4993. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  4994. }
  4995. /* Write last complete words */
  4996. for (loopcounter = 0U; (loopcounter < ((size_in_bytes / 4U) % 4U)); loopcounter++)
  4997. {
  4998. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  4999. hcryp->CrypHeaderCount++ ;
  5000. }
  5001. /* If the header size is a multiple of words */
  5002. if ((size_in_bytes % 4U) == 0U)
  5003. {
  5004. /* Pad the data with zeros to have a complete block */
  5005. while (loopcounter < 4U)
  5006. {
  5007. hcryp->Instance->DINR = 0x0U;
  5008. loopcounter++;
  5009. }
  5010. }
  5011. else
  5012. {
  5013. /* Enter last bytes, padded with zeroes */
  5014. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5015. tmp &= mask[(hcryp->Init.DataType * 2U) + (size_in_bytes % 4U)];
  5016. hcryp->Instance->DINR = tmp;
  5017. loopcounter++;
  5018. /* Pad the data with zeros to have a complete block */
  5019. while (loopcounter < 4U)
  5020. {
  5021. hcryp->Instance->DINR = 0x0U;
  5022. loopcounter++;
  5023. }
  5024. }
  5025. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  5026. {
  5027. /* Disable the CRYP peripheral clock */
  5028. __HAL_CRYP_DISABLE(hcryp);
  5029. /* Change state */
  5030. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5031. hcryp->State = HAL_CRYP_STATE_READY;
  5032. /* Process unlocked */
  5033. __HAL_UNLOCK(hcryp);
  5034. return HAL_ERROR;
  5035. }
  5036. /* Clear CCF flag */
  5037. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  5038. }
  5039. #endif /* End AES or CRYP */
  5040. }
  5041. else
  5042. {
  5043. #if defined(AES)
  5044. if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)
  5045. {
  5046. /*Workaround 1: only AES, before re-enabling the IP, datatype can be configured.*/
  5047. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
  5048. /* Select header phase */
  5049. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  5050. /* Enable the CRYP peripheral */
  5051. __HAL_CRYP_ENABLE(hcryp);
  5052. }
  5053. #endif /* AES */
  5054. }
  5055. /* Return function status */
  5056. return HAL_OK;
  5057. }
  5058. /**
  5059. * @brief Sets the header phase when using DMA in process
  5060. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  5061. * the configuration information for CRYP module(Header & HeaderSize)
  5062. * @retval None
  5063. */
  5064. static HAL_StatusTypeDef CRYP_GCMCCM_SetHeaderPhase_DMA(CRYP_HandleTypeDef *hcryp)
  5065. {
  5066. __IO uint32_t count = 0U;
  5067. uint32_t loopcounter;
  5068. uint32_t headersize_in_bytes;
  5069. uint32_t tmp;
  5070. uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */
  5071. 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */
  5072. 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU
  5073. }; /* 8-bit data type */
  5074. /***************************** Header phase for GCM/GMAC or CCM *********************************/
  5075. if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD)
  5076. {
  5077. headersize_in_bytes = hcryp->Init.HeaderSize * 4U;
  5078. }
  5079. else
  5080. {
  5081. headersize_in_bytes = hcryp->Init.HeaderSize;
  5082. }
  5083. if (headersize_in_bytes != 0U)
  5084. {
  5085. #if defined(CRYP)
  5086. /* Select header phase */
  5087. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  5088. /* Enable the CRYP peripheral */
  5089. __HAL_CRYP_ENABLE(hcryp);
  5090. if ((headersize_in_bytes % 16U) == 0U)
  5091. {
  5092. /* HeaderSize %4, no padding */
  5093. for (loopcounter = 0U; (loopcounter < (headersize_in_bytes / 4U)); loopcounter += 4U)
  5094. {
  5095. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5096. hcryp->CrypHeaderCount++ ;
  5097. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5098. hcryp->CrypHeaderCount++ ;
  5099. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5100. hcryp->CrypHeaderCount++ ;
  5101. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5102. hcryp->CrypHeaderCount++ ;
  5103. /* Wait for IFEM to be raised */
  5104. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5105. do
  5106. {
  5107. count-- ;
  5108. if (count == 0U)
  5109. {
  5110. /* Disable the CRYP peripheral clock */
  5111. __HAL_CRYP_DISABLE(hcryp);
  5112. /* Change state */
  5113. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5114. hcryp->State = HAL_CRYP_STATE_READY;
  5115. /* Process unlocked */
  5116. __HAL_UNLOCK(hcryp);
  5117. return HAL_ERROR;
  5118. }
  5119. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
  5120. }
  5121. }
  5122. else
  5123. {
  5124. /*Write header block in the IN FIFO without last block */
  5125. for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 16U) * 4U)); loopcounter += 4U)
  5126. {
  5127. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5128. hcryp->CrypHeaderCount++ ;
  5129. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5130. hcryp->CrypHeaderCount++ ;
  5131. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5132. hcryp->CrypHeaderCount++ ;
  5133. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5134. hcryp->CrypHeaderCount++ ;
  5135. /* Wait for IFEM to be raised */
  5136. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5137. do
  5138. {
  5139. count-- ;
  5140. if (count == 0U)
  5141. {
  5142. /* Disable the CRYP peripheral clock */
  5143. __HAL_CRYP_DISABLE(hcryp);
  5144. /* Change state */
  5145. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5146. hcryp->State = HAL_CRYP_STATE_READY;
  5147. /* Process unlocked */
  5148. __HAL_UNLOCK(hcryp);
  5149. return HAL_ERROR;
  5150. }
  5151. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
  5152. }
  5153. /* Last block optionally pad the data with zeros*/
  5154. for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 4U) % 4U)); loopcounter++)
  5155. {
  5156. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5157. hcryp->CrypHeaderCount++ ;
  5158. }
  5159. /* If the header size is a multiple of words */
  5160. if ((headersize_in_bytes % 4U) == 0U)
  5161. {
  5162. /* Pad the data with zeros to have a complete block */
  5163. while (loopcounter < 4U)
  5164. {
  5165. hcryp->Instance->DIN = 0x0U;
  5166. loopcounter++;
  5167. }
  5168. }
  5169. else
  5170. {
  5171. /* Enter last bytes, padded with zeroes */
  5172. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5173. tmp &= mask[(((hcryp->Init.DataType) >> 5) * 2U) + (headersize_in_bytes % 4U)];
  5174. hcryp->Instance->DIN = tmp;
  5175. loopcounter++;
  5176. /* Pad the data with zeros to have a complete block */
  5177. while (loopcounter < 4U)
  5178. {
  5179. hcryp->Instance->DIN = 0x0U;
  5180. loopcounter++;
  5181. }
  5182. }
  5183. /* Wait for IFEM to be raised */
  5184. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5185. do
  5186. {
  5187. count-- ;
  5188. if (count == 0U)
  5189. {
  5190. /* Disable the CRYP peripheral clock */
  5191. __HAL_CRYP_DISABLE(hcryp);
  5192. /* Change state */
  5193. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5194. hcryp->State = HAL_CRYP_STATE_READY;
  5195. /* Process unlocked */
  5196. __HAL_UNLOCK(hcryp);
  5197. return HAL_ERROR;
  5198. }
  5199. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
  5200. }
  5201. /* Wait until the complete message has been processed */
  5202. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5203. do
  5204. {
  5205. count-- ;
  5206. if (count == 0U)
  5207. {
  5208. /* Disable the CRYP peripheral clock */
  5209. __HAL_CRYP_DISABLE(hcryp);
  5210. /* Change state */
  5211. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5212. hcryp->State = HAL_CRYP_STATE_READY;
  5213. /* Process unlocked */
  5214. __HAL_UNLOCK(hcryp);
  5215. return HAL_ERROR;
  5216. }
  5217. } while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
  5218. #else /* AES */
  5219. if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)
  5220. {
  5221. /* Workaround 1: only AES, before re-enabling the IP, datatype can be configured.*/
  5222. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
  5223. /* Select header phase */
  5224. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  5225. /* Enable the CRYP peripheral */
  5226. __HAL_CRYP_ENABLE(hcryp);
  5227. }
  5228. if ((headersize_in_bytes % 16U) == 0U)
  5229. {
  5230. /* HeaderSize %4, no padding */
  5231. for (loopcounter = 0U; (loopcounter < (headersize_in_bytes / 4U)); loopcounter += 4U)
  5232. {
  5233. /* Write the input block in the data input register */
  5234. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5235. hcryp->CrypHeaderCount++ ;
  5236. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5237. hcryp->CrypHeaderCount++ ;
  5238. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5239. hcryp->CrypHeaderCount++ ;
  5240. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5241. hcryp->CrypHeaderCount++ ;
  5242. /*Wait on CCF flag*/
  5243. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5244. do
  5245. {
  5246. count-- ;
  5247. if (count == 0U)
  5248. {
  5249. /* Disable the CRYP peripheral clock */
  5250. __HAL_CRYP_DISABLE(hcryp);
  5251. /* Change state */
  5252. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5253. hcryp->State = HAL_CRYP_STATE_READY;
  5254. /* Process unlocked */
  5255. __HAL_UNLOCK(hcryp);
  5256. return HAL_ERROR;
  5257. }
  5258. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  5259. /* Clear CCF flag */
  5260. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  5261. }
  5262. }
  5263. else
  5264. {
  5265. /*Write header block in the IN FIFO without last block */
  5266. for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 16U) * 4U)); loopcounter += 4U)
  5267. {
  5268. /* Write the Input block in the Data Input register */
  5269. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5270. hcryp->CrypHeaderCount++ ;
  5271. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5272. hcryp->CrypHeaderCount++ ;
  5273. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5274. hcryp->CrypHeaderCount++ ;
  5275. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5276. hcryp->CrypHeaderCount++ ;
  5277. /*Wait on CCF flag*/
  5278. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5279. do
  5280. {
  5281. count-- ;
  5282. if (count == 0U)
  5283. {
  5284. /* Disable the CRYP peripheral clock */
  5285. __HAL_CRYP_DISABLE(hcryp);
  5286. /* Change state */
  5287. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5288. hcryp->State = HAL_CRYP_STATE_READY;
  5289. /* Process unlocked */
  5290. __HAL_UNLOCK(hcryp);
  5291. return HAL_ERROR;
  5292. }
  5293. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  5294. /* Clear CCF flag */
  5295. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  5296. }
  5297. /* Last block optionally pad the data with zeros*/
  5298. for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 4U) % 4U)); loopcounter++)
  5299. {
  5300. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5301. hcryp->CrypHeaderCount++ ;
  5302. }
  5303. /* If the header size is a multiple of words */
  5304. if ((headersize_in_bytes % 4U) == 0U)
  5305. {
  5306. /* Pad the data with zeros to have a complete block */
  5307. while (loopcounter < 4U)
  5308. {
  5309. hcryp->Instance->DINR = 0x0U;
  5310. loopcounter++;
  5311. }
  5312. }
  5313. else
  5314. {
  5315. /* Enter last bytes, padded with zeroes */
  5316. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5317. tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)];
  5318. hcryp->Instance->DINR = tmp;
  5319. loopcounter++;
  5320. /* Pad the data with zeros to have a complete block */
  5321. while (loopcounter < 4U)
  5322. {
  5323. hcryp->Instance->DINR = 0x0U;
  5324. loopcounter++;
  5325. }
  5326. }
  5327. /*Wait on CCF flag*/
  5328. count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
  5329. do
  5330. {
  5331. count-- ;
  5332. if (count == 0U)
  5333. {
  5334. /* Disable the CRYP peripheral clock */
  5335. __HAL_CRYP_DISABLE(hcryp);
  5336. /* Change state */
  5337. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5338. hcryp->State = HAL_CRYP_STATE_READY;
  5339. /* Process unlocked */
  5340. __HAL_UNLOCK(hcryp);
  5341. return HAL_ERROR;
  5342. }
  5343. } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
  5344. /* Clear CCF flag */
  5345. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  5346. }
  5347. #endif /* End AES or CRYP */
  5348. }
  5349. else
  5350. {
  5351. #if defined(AES)
  5352. if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)
  5353. {
  5354. /*Workaround 1: only AES, before re-enabling the IP, datatype can be configured.*/
  5355. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE, hcryp->Init.DataType);
  5356. /* Select header phase */
  5357. CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  5358. /* Enable the CRYP peripheral */
  5359. __HAL_CRYP_ENABLE(hcryp);
  5360. }
  5361. #endif /* AES */
  5362. }
  5363. /* Return function status */
  5364. return HAL_OK;
  5365. }
  5366. /**
  5367. * @brief Sets the header phase in interrupt mode
  5368. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  5369. * the configuration information for CRYP module(Header & HeaderSize)
  5370. * @retval None
  5371. */
  5372. static void CRYP_GCMCCM_SetHeaderPhase_IT(CRYP_HandleTypeDef *hcryp)
  5373. {
  5374. uint32_t loopcounter;
  5375. uint32_t headersize_in_bytes;
  5376. #if defined(AES)
  5377. uint32_t lastwordsize;
  5378. uint32_t npblb;
  5379. #endif /* AES */
  5380. uint32_t tmp;
  5381. uint32_t mask[12] = {0x0U, 0xFF000000U, 0xFFFF0000U, 0xFFFFFF00U, /* 32-bit data type */
  5382. 0x0U, 0x0000FF00U, 0x0000FFFFU, 0xFF00FFFFU, /* 16-bit data type */
  5383. 0x0U, 0x000000FFU, 0x0000FFFFU, 0x00FFFFFFU
  5384. }; /* 8-bit data type */
  5385. if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_WORD)
  5386. {
  5387. headersize_in_bytes = hcryp->Init.HeaderSize * 4U;
  5388. }
  5389. else
  5390. {
  5391. headersize_in_bytes = hcryp->Init.HeaderSize;
  5392. }
  5393. /***************************** Header phase *********************************/
  5394. #if defined(CRYP)
  5395. if (headersize_in_bytes <= ((uint32_t)(hcryp->CrypHeaderCount) * 4U))
  5396. {
  5397. /* Disable interrupts */
  5398. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  5399. /* Disable the CRYP peripheral */
  5400. __HAL_CRYP_DISABLE(hcryp);
  5401. /* Set the phase */
  5402. hcryp->Phase = CRYP_PHASE_PROCESS;
  5403. /* Select payload phase once the header phase is performed */
  5404. CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  5405. /* Enable Interrupts */
  5406. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  5407. /* Enable the CRYP peripheral */
  5408. __HAL_CRYP_ENABLE(hcryp);
  5409. }
  5410. else if (((headersize_in_bytes / 4U) - (hcryp->CrypHeaderCount)) >= 4U)
  5411. {
  5412. /* HeaderSize %4, no padding */
  5413. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5414. hcryp->CrypHeaderCount++ ;
  5415. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5416. hcryp->CrypHeaderCount++ ;
  5417. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5418. hcryp->CrypHeaderCount++ ;
  5419. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5420. hcryp->CrypHeaderCount++ ;
  5421. }
  5422. else
  5423. {
  5424. /* Last block optionally pad the data with zeros*/
  5425. for (loopcounter = 0U; loopcounter < ((headersize_in_bytes / 4U) % 4U); loopcounter++)
  5426. {
  5427. hcryp->Instance->DIN = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5428. hcryp->CrypHeaderCount++ ;
  5429. }
  5430. if ((headersize_in_bytes % 4U) == 0U)
  5431. {
  5432. /* Pad the data with zeros to have a complete block */
  5433. while (loopcounter < 4U)
  5434. {
  5435. hcryp->Instance->DIN = 0x0U;
  5436. loopcounter++;
  5437. hcryp->CrypHeaderCount++;
  5438. }
  5439. }
  5440. else
  5441. {
  5442. /* Enter last bytes, padded with zeros */
  5443. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5444. tmp &= mask[(((hcryp->Init.DataType) >> 5) * 2U) + (headersize_in_bytes % 4U)];
  5445. hcryp->Instance->DIN = tmp;
  5446. loopcounter++;
  5447. hcryp->CrypHeaderCount++;
  5448. /* Pad the data with zeros to have a complete block */
  5449. while (loopcounter < 4U)
  5450. {
  5451. hcryp->Instance->DIN = 0x0U;
  5452. loopcounter++;
  5453. hcryp->CrypHeaderCount++;
  5454. }
  5455. }
  5456. }
  5457. #else /* AES */
  5458. if (headersize_in_bytes <= ((uint32_t)(hcryp->CrypHeaderCount) * 4U))
  5459. {
  5460. /* Set the phase */
  5461. hcryp->Phase = CRYP_PHASE_PROCESS;
  5462. /* Payload phase not supported in CCM AES2 */
  5463. if (hcryp->Init.Algorithm == CRYP_AES_GCM_GMAC)
  5464. {
  5465. /* Select payload phase once the header phase is performed */
  5466. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_PAYLOAD);
  5467. }
  5468. if (hcryp->Init.Algorithm == CRYP_AES_CCM)
  5469. {
  5470. /* Increment CrypHeaderCount to pass in CRYP_GCMCCM_SetPayloadPhase_IT */
  5471. hcryp->CrypHeaderCount++;
  5472. }
  5473. /* Write the payload Input block in the IN FIFO */
  5474. if (hcryp->Size == 0U)
  5475. {
  5476. /* Disable interrupts */
  5477. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE | CRYP_IT_ERRIE);
  5478. /* Change the CRYP state */
  5479. hcryp->State = HAL_CRYP_STATE_READY;
  5480. /* Process unlocked */
  5481. __HAL_UNLOCK(hcryp);
  5482. }
  5483. else if (hcryp->Size >= 16U)
  5484. {
  5485. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5486. hcryp->CrypInCount++;
  5487. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5488. hcryp->CrypInCount++;
  5489. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5490. hcryp->CrypInCount++;
  5491. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5492. hcryp->CrypInCount++;
  5493. if ((hcryp->CrypInCount == (hcryp->Size / 4U)) && ((hcryp->Size % 16U) == 0U))
  5494. {
  5495. /* Call the input data transfer complete callback */
  5496. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  5497. /*Call registered Input complete callback*/
  5498. hcryp->InCpltCallback(hcryp);
  5499. #else
  5500. /*Call legacy weak Input complete callback*/
  5501. HAL_CRYP_InCpltCallback(hcryp);
  5502. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  5503. }
  5504. }
  5505. else /* Size < 4 words : first block is the last block*/
  5506. {
  5507. /* Workaround not implemented, Size should be %4 otherwise Tag will be incorrectly
  5508. generated for GCM Encryption. Workaround is implemented in polling mode, so if last block of
  5509. payload <128bit don't use CRYP_Encrypt_IT otherwise TAG is incorrectly generated for GCM Encryption. */
  5510. /* Compute the number of padding bytes in last block of payload */
  5511. npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size);
  5512. /* Number of valid words (lastwordsize) in last block */
  5513. if ((npblb % 4U) == 0U)
  5514. {
  5515. lastwordsize = (16U - npblb) / 4U;
  5516. }
  5517. else
  5518. {
  5519. lastwordsize = ((16U - npblb) / 4U) + 1U;
  5520. }
  5521. /* Last block optionally pad the data with zeros*/
  5522. for (loopcounter = 0U; loopcounter < lastwordsize; loopcounter++)
  5523. {
  5524. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5525. hcryp->CrypInCount++;
  5526. }
  5527. while (loopcounter < 4U)
  5528. {
  5529. /* Pad the data with zeros to have a complete block */
  5530. hcryp->Instance->DINR = 0x0U;
  5531. loopcounter++;
  5532. }
  5533. }
  5534. }
  5535. else if (((headersize_in_bytes / 4U) - (hcryp->CrypHeaderCount)) >= 4U)
  5536. {
  5537. /* Write the input block in the IN FIFO */
  5538. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5539. hcryp->CrypHeaderCount++;
  5540. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5541. hcryp->CrypHeaderCount++;
  5542. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5543. hcryp->CrypHeaderCount++;
  5544. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5545. hcryp->CrypHeaderCount++;
  5546. }
  5547. else /*HeaderSize < 4 or HeaderSize >4 & HeaderSize %4 != 0*/
  5548. {
  5549. /* Last block optionally pad the data with zeros*/
  5550. for (loopcounter = 0U; loopcounter < ((headersize_in_bytes / 4U) % 4U); loopcounter++)
  5551. {
  5552. hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5553. hcryp->CrypHeaderCount++ ;
  5554. }
  5555. /* If the header size is a multiple of words */
  5556. if ((headersize_in_bytes % 4U) == 0U)
  5557. {
  5558. /* Pad the data with zeros to have a complete block */
  5559. while (loopcounter < 4U)
  5560. {
  5561. hcryp->Instance->DINR = 0x0U;
  5562. loopcounter++;
  5563. hcryp->CrypHeaderCount++;
  5564. }
  5565. }
  5566. else
  5567. {
  5568. /* Enter last bytes, padded with zeros */
  5569. tmp = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
  5570. tmp &= mask[(hcryp->Init.DataType * 2U) + (headersize_in_bytes % 4U)];
  5571. hcryp->Instance->DINR = tmp;
  5572. loopcounter++;
  5573. hcryp->CrypHeaderCount++;
  5574. /* Pad the data with zeros to have a complete block */
  5575. while (loopcounter < 4U)
  5576. {
  5577. hcryp->Instance->DINR = 0x0U;
  5578. loopcounter++;
  5579. hcryp->CrypHeaderCount++;
  5580. }
  5581. }
  5582. }
  5583. #endif /* End AES or CRYP */
  5584. }
  5585. /**
  5586. * @brief Workaround used for GCM/CCM mode.
  5587. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  5588. * the configuration information for CRYP module
  5589. * @param Timeout: specify Timeout value
  5590. * @retval None
  5591. */
  5592. static void CRYP_Workaround(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  5593. {
  5594. uint32_t lastwordsize;
  5595. uint32_t npblb;
  5596. #if defined(CRYP)
  5597. uint32_t iv1temp;
  5598. uint32_t temp[4] = {0};
  5599. uint32_t temp2[4] = {0};
  5600. #endif /* CRYP */
  5601. uint32_t intermediate_data[4] = {0};
  5602. uint32_t index;
  5603. /* Compute the number of padding bytes in last block of payload */
  5604. npblb = ((((uint32_t)(hcryp->Size) / 16U) + 1U) * 16U) - (uint32_t)(hcryp->Size);
  5605. /* Number of valid words (lastwordsize) in last block */
  5606. if ((npblb % 4U) == 0U)
  5607. {
  5608. lastwordsize = (16U - npblb) / 4U;
  5609. }
  5610. else
  5611. {
  5612. lastwordsize = ((16U - npblb) / 4U) + 1U;
  5613. }
  5614. #if defined(CRYP)
  5615. /* Workaround 2, case GCM encryption */
  5616. if (hcryp->Init.Algorithm == CRYP_AES_GCM)
  5617. {
  5618. if ((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_ENCRYPT)
  5619. {
  5620. /*Workaround in order to properly compute authentication tags while doing
  5621. a GCM encryption with the last block of payload size inferior to 128 bits*/
  5622. /* Disable CRYP to start the final phase */
  5623. __HAL_CRYP_DISABLE(hcryp);
  5624. /*Update CRYP_IV1R register and ALGOMODE*/
  5625. hcryp->Instance->IV1RR = ((hcryp->Instance->CSGCMCCM7R) - 1U);
  5626. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_CTR);
  5627. /* Enable CRYP to start the final phase */
  5628. __HAL_CRYP_ENABLE(hcryp);
  5629. }
  5630. /* Last block optionally pad the data with zeros*/
  5631. for (index = 0; index < lastwordsize; index ++)
  5632. {
  5633. /* Write the last input block in the IN FIFO */
  5634. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5635. hcryp->CrypInCount++;
  5636. }
  5637. while (index < 4U)
  5638. {
  5639. /* Pad the data with zeros to have a complete block */
  5640. hcryp->Instance->DIN = 0U;
  5641. index++;
  5642. }
  5643. /* Wait for OFNE flag to be raised */
  5644. if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  5645. {
  5646. /* Disable the CRYP peripheral clock */
  5647. __HAL_CRYP_DISABLE(hcryp);
  5648. /* Change state */
  5649. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5650. hcryp->State = HAL_CRYP_STATE_READY;
  5651. /* Process Unlocked */
  5652. __HAL_UNLOCK(hcryp);
  5653. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  5654. /*Call registered error callback*/
  5655. hcryp->ErrorCallback(hcryp);
  5656. #else
  5657. /*Call legacy weak error callback*/
  5658. HAL_CRYP_ErrorCallback(hcryp);
  5659. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  5660. }
  5661. if ((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U)
  5662. {
  5663. for (index = 0U; index < 4U; index++)
  5664. {
  5665. /* Read the output block from the output FIFO */
  5666. intermediate_data[index] = hcryp->Instance->DOUT;
  5667. /* Intermediate data buffer to be used in for the workaround*/
  5668. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = intermediate_data[index];
  5669. hcryp->CrypOutCount++;
  5670. }
  5671. }
  5672. if ((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_ENCRYPT)
  5673. {
  5674. /*workaround in order to properly compute authentication tags while doing
  5675. a GCM encryption with the last block of payload size inferior to 128 bits*/
  5676. /* Change the AES mode to GCM mode and Select Final phase */
  5677. /* configured CHMOD GCM */
  5678. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_GCM);
  5679. /* configured final phase */
  5680. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_GCM_CCMPH, CRYP_PHASE_FINAL);
  5681. if ((hcryp->Instance->CR & CRYP_CR_DATATYPE) == CRYP_DATATYPE_32B)
  5682. {
  5683. if ((npblb % 4U) == 1U)
  5684. {
  5685. intermediate_data[lastwordsize - 1U] &= 0xFFFFFF00U;
  5686. }
  5687. if ((npblb % 4U) == 2U)
  5688. {
  5689. intermediate_data[lastwordsize - 1U] &= 0xFFFF0000U;
  5690. }
  5691. if ((npblb % 4U) == 3U)
  5692. {
  5693. intermediate_data[lastwordsize - 1U] &= 0xFF000000U;
  5694. }
  5695. }
  5696. else if ((hcryp->Instance->CR & CRYP_CR_DATATYPE) == CRYP_DATATYPE_8B)
  5697. {
  5698. if ((npblb % 4U) == 1U)
  5699. {
  5700. intermediate_data[lastwordsize - 1U] &= __REV(0xFFFFFF00U);
  5701. }
  5702. if ((npblb % 4U) == 2U)
  5703. {
  5704. intermediate_data[lastwordsize - 1U] &= __REV(0xFFFF0000U);
  5705. }
  5706. if ((npblb % 4U) == 3U)
  5707. {
  5708. intermediate_data[lastwordsize - 1U] &= __REV(0xFF000000U);
  5709. }
  5710. }
  5711. else if ((hcryp->Instance->CR & CRYP_CR_DATATYPE) == CRYP_DATATYPE_16B)
  5712. {
  5713. if ((npblb % 4U) == 1U)
  5714. {
  5715. intermediate_data[lastwordsize - 1U] &= __ROR((0xFFFFFF00U), 16);
  5716. }
  5717. if ((npblb % 4U) == 2U)
  5718. {
  5719. intermediate_data[lastwordsize - 1U] &= __ROR((0xFFFF0000U), 16);
  5720. }
  5721. if ((npblb % 4U) == 3U)
  5722. {
  5723. intermediate_data[lastwordsize - 1U] &= __ROR((0xFF000000U), 16);
  5724. }
  5725. }
  5726. else /*CRYP_DATATYPE_1B*/
  5727. {
  5728. if ((npblb % 4U) == 1U)
  5729. {
  5730. intermediate_data[lastwordsize - 1U] &= __RBIT(0xFFFFFF00U);
  5731. }
  5732. if ((npblb % 4U) == 2U)
  5733. {
  5734. intermediate_data[lastwordsize - 1U] &= __RBIT(0xFFFF0000U);
  5735. }
  5736. if ((npblb % 4U) == 3U)
  5737. {
  5738. intermediate_data[lastwordsize - 1U] &= __RBIT(0xFF000000U);
  5739. }
  5740. }
  5741. for (index = 0U; index < lastwordsize; index ++)
  5742. {
  5743. /*Write the intermediate_data in the IN FIFO */
  5744. hcryp->Instance->DIN = intermediate_data[index];
  5745. }
  5746. while (index < 4U)
  5747. {
  5748. /* Pad the data with zeros to have a complete block */
  5749. hcryp->Instance->DIN = 0x0U;
  5750. index++;
  5751. }
  5752. /* Wait for OFNE flag to be raised */
  5753. if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  5754. {
  5755. /* Disable the CRYP peripheral clock */
  5756. __HAL_CRYP_DISABLE(hcryp);
  5757. /* Change state */
  5758. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5759. hcryp->State = HAL_CRYP_STATE_READY;
  5760. /* Process unlocked */
  5761. __HAL_UNLOCK(hcryp);
  5762. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  5763. /*Call registered error callback*/
  5764. hcryp->ErrorCallback(hcryp);
  5765. #else
  5766. /*Call legacy weak error callback*/
  5767. HAL_CRYP_ErrorCallback(hcryp);
  5768. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  5769. }
  5770. if ((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U)
  5771. {
  5772. for (index = 0U; index < 4U; index++)
  5773. {
  5774. intermediate_data[index] = hcryp->Instance->DOUT;
  5775. }
  5776. }
  5777. }
  5778. } /* End of GCM encryption */
  5779. else
  5780. {
  5781. /* Workaround 2, case CCM decryption, in order to properly compute
  5782. authentication tags while doing a CCM decryption with the last block
  5783. of payload size inferior to 128 bits*/
  5784. if ((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_DECRYPT)
  5785. {
  5786. iv1temp = hcryp->Instance->CSGCMCCM7R;
  5787. /* Disable CRYP to start the final phase */
  5788. __HAL_CRYP_DISABLE(hcryp);
  5789. temp[0] = hcryp->Instance->CSGCMCCM0R;
  5790. temp[1] = hcryp->Instance->CSGCMCCM1R;
  5791. temp[2] = hcryp->Instance->CSGCMCCM2R;
  5792. temp[3] = hcryp->Instance->CSGCMCCM3R;
  5793. hcryp->Instance->IV1RR = iv1temp;
  5794. /* Configured CHMOD CTR */
  5795. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_CTR);
  5796. /* Enable CRYP to start the final phase */
  5797. __HAL_CRYP_ENABLE(hcryp);
  5798. }
  5799. /* Last block optionally pad the data with zeros*/
  5800. for (index = 0; index < lastwordsize; index ++)
  5801. {
  5802. /* Write the last Input block in the IN FIFO */
  5803. hcryp->Instance->DIN = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5804. hcryp->CrypInCount++;
  5805. }
  5806. while (index < 4U)
  5807. {
  5808. /* Pad the data with zeros to have a complete block */
  5809. hcryp->Instance->DIN = 0U;
  5810. index++;
  5811. }
  5812. /* Wait for OFNE flag to be raised */
  5813. if (CRYP_WaitOnOFNEFlag(hcryp, Timeout) != HAL_OK)
  5814. {
  5815. /* Disable the CRYP peripheral clock */
  5816. __HAL_CRYP_DISABLE(hcryp);
  5817. /* Change state */
  5818. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5819. hcryp->State = HAL_CRYP_STATE_READY;
  5820. /* Process Unlocked */
  5821. __HAL_UNLOCK(hcryp);
  5822. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  5823. /*Call registered error callback*/
  5824. hcryp->ErrorCallback(hcryp);
  5825. #else
  5826. /*Call legacy weak error callback*/
  5827. HAL_CRYP_ErrorCallback(hcryp);
  5828. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  5829. }
  5830. if ((hcryp->Instance->SR & CRYP_FLAG_OFNE) != 0x0U)
  5831. {
  5832. for (index = 0U; index < 4U; index++)
  5833. {
  5834. /* Read the Output block from the Output FIFO */
  5835. intermediate_data[index] = hcryp->Instance->DOUT;
  5836. /*intermediate data buffer to be used in for the workaround*/
  5837. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = intermediate_data[index];
  5838. hcryp->CrypOutCount++;
  5839. }
  5840. }
  5841. if ((hcryp->Instance->CR & CRYP_CR_ALGODIR) == CRYP_OPERATINGMODE_DECRYPT)
  5842. {
  5843. temp2[0] = hcryp->Instance->CSGCMCCM0R;
  5844. temp2[1] = hcryp->Instance->CSGCMCCM1R;
  5845. temp2[2] = hcryp->Instance->CSGCMCCM2R;
  5846. temp2[3] = hcryp->Instance->CSGCMCCM3R;
  5847. /* configured CHMOD CCM */
  5848. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, CRYP_AES_CCM);
  5849. /* configured Header phase */
  5850. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_GCM_CCMPH, CRYP_PHASE_HEADER);
  5851. /*set to zero the bits corresponding to the padded bits*/
  5852. for (index = lastwordsize; index < 4U; index ++)
  5853. {
  5854. intermediate_data[index] = 0U;
  5855. }
  5856. if ((npblb % 4U) == 1U)
  5857. {
  5858. intermediate_data[lastwordsize - 1U] &= 0xFFFFFF00U;
  5859. }
  5860. if ((npblb % 4U) == 2U)
  5861. {
  5862. intermediate_data[lastwordsize - 1U] &= 0xFFFF0000U;
  5863. }
  5864. if ((npblb % 4U) == 3U)
  5865. {
  5866. intermediate_data[lastwordsize - 1U] &= 0xFF000000U;
  5867. }
  5868. for (index = 0U; index < 4U ; index ++)
  5869. {
  5870. intermediate_data[index] ^= temp[index];
  5871. intermediate_data[index] ^= temp2[index];
  5872. }
  5873. for (index = 0U; index < 4U; index ++)
  5874. {
  5875. /* Write the last Input block in the IN FIFO */
  5876. hcryp->Instance->DIN = intermediate_data[index] ;
  5877. }
  5878. /* Wait for BUSY flag to be raised */
  5879. if (CRYP_WaitOnBUSYFlag(hcryp, Timeout) != HAL_OK)
  5880. {
  5881. /* Disable the CRYP peripheral clock */
  5882. __HAL_CRYP_DISABLE(hcryp);
  5883. /* Change state */
  5884. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  5885. hcryp->State = HAL_CRYP_STATE_READY;
  5886. /* Process Unlocked */
  5887. __HAL_UNLOCK(hcryp);
  5888. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  5889. /*Call registered error callback*/
  5890. hcryp->ErrorCallback(hcryp);
  5891. #else
  5892. /*Call legacy weak error callback*/
  5893. HAL_CRYP_ErrorCallback(hcryp);
  5894. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  5895. }
  5896. }
  5897. } /* End of CCM WKA*/
  5898. /* Process Unlocked */
  5899. __HAL_UNLOCK(hcryp);
  5900. #else /* AES */
  5901. /*Workaround 2: case GCM encryption, during payload phase and before inserting
  5902. the last block of paylaod, which size is inferior to 128 bits */
  5903. if ((hcryp->Instance->CR & AES_CR_MODE) == CRYP_OPERATINGMODE_ENCRYPT)
  5904. {
  5905. /* configured CHMOD CTR */
  5906. MODIFY_REG(hcryp->Instance->CR, AES_CR_CHMOD, CRYP_AES_CTR);
  5907. }
  5908. /* last block optionally pad the data with zeros*/
  5909. for (index = 0U; index < lastwordsize; index ++)
  5910. {
  5911. /* Write the last Input block in the IN FIFO */
  5912. hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
  5913. hcryp->CrypInCount++;
  5914. }
  5915. while (index < 4U)
  5916. {
  5917. /* pad the data with zeros to have a complete block */
  5918. hcryp->Instance->DINR = 0U;
  5919. index++;
  5920. }
  5921. /* Wait for CCF flag to be raised */
  5922. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  5923. {
  5924. hcryp->State = HAL_CRYP_STATE_READY;
  5925. __HAL_UNLOCK(hcryp);
  5926. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  5927. /*Call registered error callback*/
  5928. hcryp->ErrorCallback(hcryp);
  5929. #else
  5930. /*Call legacy weak error callback*/
  5931. HAL_CRYP_ErrorCallback(hcryp);
  5932. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  5933. }
  5934. /* Clear CCF Flag */
  5935. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  5936. for (index = 0U; index < 4U; index++)
  5937. {
  5938. /* Read the Output block from the Output FIFO */
  5939. intermediate_data[index] = hcryp->Instance->DOUTR;
  5940. /*intermediate data buffer to be used in the workaround*/
  5941. *(uint32_t *)(hcryp->pCrypOutBuffPtr + (hcryp->CrypOutCount)) = intermediate_data[index];
  5942. hcryp->CrypOutCount++;
  5943. }
  5944. if ((hcryp->Instance->CR & AES_CR_MODE) == CRYP_OPERATINGMODE_ENCRYPT)
  5945. {
  5946. /* configured CHMOD GCM */
  5947. MODIFY_REG(hcryp->Instance->CR, AES_CR_CHMOD, CRYP_AES_GCM_GMAC);
  5948. /* Select final phase */
  5949. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_FINAL);
  5950. if ((hcryp->Instance->CR & AES_CR_DATATYPE) == CRYP_DATATYPE_32B)
  5951. {
  5952. if ((npblb % 4U) == 1U)
  5953. {
  5954. intermediate_data[lastwordsize - 1U] &= 0xFFFFFF00U;
  5955. }
  5956. if ((npblb % 4U) == 2U)
  5957. {
  5958. intermediate_data[lastwordsize - 1U] &= 0xFFFF0000U;
  5959. }
  5960. if ((npblb % 4U) == 3U)
  5961. {
  5962. intermediate_data[lastwordsize - 1U] &= 0xFF000000U;
  5963. }
  5964. }
  5965. else if ((hcryp->Instance->CR & AES_CR_DATATYPE) == CRYP_DATATYPE_8B)
  5966. {
  5967. if ((npblb % 4U) == 1U)
  5968. {
  5969. intermediate_data[lastwordsize - 1U] &= __REV(0xFFFFFF00U);
  5970. }
  5971. if ((npblb % 4U) == 2U)
  5972. {
  5973. intermediate_data[lastwordsize - 1U] &= __REV(0xFFFF0000U);
  5974. }
  5975. if ((npblb % 4U) == 3U)
  5976. {
  5977. intermediate_data[lastwordsize - 1U] &= __REV(0xFF000000U);
  5978. }
  5979. }
  5980. else if ((hcryp->Instance->CR & AES_CR_DATATYPE) == CRYP_DATATYPE_16B)
  5981. {
  5982. if ((npblb % 4U) == 1U)
  5983. {
  5984. intermediate_data[lastwordsize - 1U] &= __ROR((0xFFFFFF00U), 16);
  5985. }
  5986. if ((npblb % 4U) == 2U)
  5987. {
  5988. intermediate_data[lastwordsize - 1U] &= __ROR((0xFFFF0000U), 16);
  5989. }
  5990. if ((npblb % 4U) == 3U)
  5991. {
  5992. intermediate_data[lastwordsize - 1U] &= __ROR((0xFF000000U), 16);
  5993. }
  5994. }
  5995. else /*CRYP_DATATYPE_1B*/
  5996. {
  5997. if ((npblb % 4U) == 1U)
  5998. {
  5999. intermediate_data[lastwordsize - 1U] &= __RBIT(0xFFFFFF00U);
  6000. }
  6001. if ((npblb % 4U) == 2U)
  6002. {
  6003. intermediate_data[lastwordsize - 1U] &= __RBIT(0xFFFF0000U);
  6004. }
  6005. if ((npblb % 4U) == 3U)
  6006. {
  6007. intermediate_data[lastwordsize - 1U] &= __RBIT(0xFF000000U);
  6008. }
  6009. }
  6010. /*Write the intermediate_data in the IN FIFO */
  6011. for (index = 0U; index < lastwordsize; index ++)
  6012. {
  6013. hcryp->Instance->DINR = intermediate_data[index];
  6014. }
  6015. while (index < 4U)
  6016. {
  6017. /* pad the data with zeros to have a complete block */
  6018. hcryp->Instance->DINR = 0U;
  6019. index++;
  6020. }
  6021. /* Wait for CCF flag to be raised */
  6022. if (CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  6023. {
  6024. /* Disable the CRYP peripheral clock */
  6025. __HAL_CRYP_DISABLE(hcryp);
  6026. /* Change state */
  6027. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  6028. hcryp->State = HAL_CRYP_STATE_READY;
  6029. /* Process Unlocked */
  6030. __HAL_UNLOCK(hcryp);
  6031. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1U)
  6032. /*Call registered error callback*/
  6033. hcryp->ErrorCallback(hcryp);
  6034. #else
  6035. /*Call legacy weak error callback*/
  6036. HAL_CRYP_ErrorCallback(hcryp);
  6037. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  6038. }
  6039. /* Clear CCF Flag */
  6040. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  6041. for (index = 0U; index < 4U; index++)
  6042. {
  6043. intermediate_data[index] = hcryp->Instance->DOUTR;
  6044. }
  6045. }/*End of Workaround 2*/
  6046. #endif /* End AES or CRYP */
  6047. }
  6048. #endif /* AES or GCM CCM defined*/
  6049. #if defined (CRYP)
  6050. #if defined (CRYP_CR_ALGOMODE_AES_GCM)
  6051. /**
  6052. * @brief Handle CRYP hardware block Timeout when waiting for IFEM flag to be raised.
  6053. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  6054. * the configuration information for CRYP module.
  6055. * @param Timeout: Timeout duration.
  6056. * @retval HAL status
  6057. */
  6058. static HAL_StatusTypeDef CRYP_WaitOnIFEMFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  6059. {
  6060. uint32_t tickstart;
  6061. /* Get timeout */
  6062. tickstart = HAL_GetTick();
  6063. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  6064. {
  6065. /* Check for the Timeout */
  6066. if (Timeout != HAL_MAX_DELAY)
  6067. {
  6068. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  6069. {
  6070. return HAL_ERROR;
  6071. }
  6072. }
  6073. }
  6074. return HAL_OK;
  6075. }
  6076. #endif /* GCM CCM defined*/
  6077. /**
  6078. * @brief Handle CRYP hardware block Timeout when waiting for BUSY flag to be raised.
  6079. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  6080. * the configuration information for CRYP module.
  6081. * @param Timeout: Timeout duration.
  6082. * @retval HAL status
  6083. */
  6084. static HAL_StatusTypeDef CRYP_WaitOnBUSYFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  6085. {
  6086. uint32_t tickstart;
  6087. /* Get timeout */
  6088. tickstart = HAL_GetTick();
  6089. while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY))
  6090. {
  6091. /* Check for the Timeout */
  6092. if (Timeout != HAL_MAX_DELAY)
  6093. {
  6094. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  6095. {
  6096. return HAL_ERROR;
  6097. }
  6098. }
  6099. }
  6100. return HAL_OK;
  6101. }
  6102. /**
  6103. * @brief Handle CRYP hardware block Timeout when waiting for OFNE flag to be raised.
  6104. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  6105. * the configuration information for CRYP module.
  6106. * @param Timeout: Timeout duration.
  6107. * @retval HAL status
  6108. */
  6109. static HAL_StatusTypeDef CRYP_WaitOnOFNEFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  6110. {
  6111. uint32_t tickstart;
  6112. /* Get timeout */
  6113. tickstart = HAL_GetTick();
  6114. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  6115. {
  6116. /* Check for the Timeout */
  6117. if (Timeout != HAL_MAX_DELAY)
  6118. {
  6119. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  6120. {
  6121. return HAL_ERROR;
  6122. }
  6123. }
  6124. }
  6125. return HAL_OK;
  6126. }
  6127. #else /* AES */
  6128. /**
  6129. * @brief Handle CRYP hardware block Timeout when waiting for CCF flag to be raised.
  6130. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  6131. * the configuration information for CRYP module.
  6132. * @param Timeout: Timeout duration.
  6133. * @retval HAL status
  6134. */
  6135. static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  6136. {
  6137. uint32_t tickstart;
  6138. /* Get timeout */
  6139. tickstart = HAL_GetTick();
  6140. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
  6141. {
  6142. /* Check for the Timeout */
  6143. if (Timeout != HAL_MAX_DELAY)
  6144. {
  6145. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  6146. {
  6147. return HAL_ERROR;
  6148. }
  6149. }
  6150. }
  6151. return HAL_OK;
  6152. }
  6153. #endif /* End AES or CRYP */
  6154. /**
  6155. * @}
  6156. */
  6157. /**
  6158. * @}
  6159. */
  6160. /**
  6161. * @}
  6162. */
  6163. #endif /* HAL_CRYP_MODULE_ENABLED */
  6164. /**
  6165. * @}
  6166. */
  6167. #endif /* TinyAES or CRYP*/
  6168. /**
  6169. * @}
  6170. */