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.
 
 
 

5991 lines
203 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_cryp_ex.c
  4. * @author MCD Application Team
  5. * @version V1.7.1
  6. * @date 14-April-2017
  7. * @brief Extended CRYP HAL module driver
  8. * This file provides firmware functions to manage the following
  9. * functionalities of CRYP extension peripheral:
  10. * + Extended AES processing functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. The CRYP Extension HAL driver can be used as follows:
  18. (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
  19. (##) Enable the CRYP interface clock using __HAL_RCC_CRYP_CLK_ENABLE()
  20. (##) In case of using interrupts (e.g. HAL_CRYPEx_AESGCM_Encrypt_IT())
  21. (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
  22. (+++) Enable the CRYP IRQ handler using HAL_NVIC_EnableIRQ()
  23. (+++) In CRYP IRQ handler, call HAL_CRYP_IRQHandler()
  24. (##) In case of using DMA to control data transfer (e.g. HAL_AES_ECB_Encrypt_DMA())
  25. (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE()
  26. (+++) Configure and enable two DMA streams one for managing data transfer from
  27. memory to peripheral (input stream) and another stream for managing data
  28. transfer from peripheral to memory (output stream)
  29. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  30. using __HAL_LINKDMA()
  31. (+++) Configure the priority and enable the NVIC for the transfer complete
  32. interrupt on the two DMA Streams. The output stream should have higher
  33. priority than the input stream HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
  34. (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures mainly:
  35. (##) The data type: 1-bit, 8-bit, 16-bit and 32-bit
  36. (##) The key size: 128, 192 and 256. This parameter is relevant only for AES
  37. (##) The encryption/decryption key. Its size depends on the algorithm
  38. used for encryption/decryption
  39. (##) The initialization vector (counter). It is not used ECB mode.
  40. (#)Three processing (encryption/decryption) functions are available:
  41. (##) Polling mode: encryption and decryption APIs are blocking functions
  42. i.e. they process the data and wait till the processing is finished
  43. e.g. HAL_CRYPEx_AESGCM_Encrypt()
  44. (##) Interrupt mode: encryption and decryption APIs are not blocking functions
  45. i.e. they process the data under interrupt
  46. e.g. HAL_CRYPEx_AESGCM_Encrypt_IT()
  47. (##) DMA mode: encryption and decryption APIs are not blocking functions
  48. i.e. the data transfer is ensured by DMA
  49. e.g. HAL_CRYPEx_AESGCM_Encrypt_DMA()
  50. (#)When the processing function is called at first time after HAL_CRYP_Init()
  51. the CRYP peripheral is initialized and processes the buffer in input.
  52. At second call, the processing function performs an append of the already
  53. processed buffer.
  54. When a new data block is to be processed, call HAL_CRYP_Init() then the
  55. processing function.
  56. (#)In AES-GCM and AES-CCM modes are an authenticated encryption algorithms
  57. which provide authentication messages.
  58. HAL_AES_GCM_Finish() and HAL_AES_CCM_Finish() are used to provide those
  59. authentication messages.
  60. Call those functions after the processing ones (polling, interrupt or DMA).
  61. e.g. in AES-CCM mode call HAL_CRYPEx_AESCCM_Encrypt() to encrypt the plain data
  62. then call HAL_CRYPEx_AESCCM_Finish() to get the authentication message
  63. -@- For CCM Encrypt/Decrypt API's, only DataType = 8-bit is supported by this version.
  64. -@- The HAL_CRYPEx_AESGCM_xxxx() implementation is limited to 32bits inputs data length
  65. (Plain/Cyphertext, Header) compared with GCM standards specifications (800-38D).
  66. (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
  67. @endverbatim
  68. ******************************************************************************
  69. * @attention
  70. *
  71. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  72. *
  73. * Redistribution and use in source and binary forms, with or without modification,
  74. * are permitted provided that the following conditions are met:
  75. * 1. Redistributions of source code must retain the above copyright notice,
  76. * this list of conditions and the following disclaimer.
  77. * 2. Redistributions in binary form must reproduce the above copyright notice,
  78. * this list of conditions and the following disclaimer in the documentation
  79. * and/or other materials provided with the distribution.
  80. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  81. * may be used to endorse or promote products derived from this software
  82. * without specific prior written permission.
  83. *
  84. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  85. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  86. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  87. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  88. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  89. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  90. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  91. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  92. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  93. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  94. *
  95. ******************************************************************************
  96. */
  97. /* Includes ------------------------------------------------------------------*/
  98. #include "stm32f4xx_hal.h"
  99. /** @addtogroup STM32F4xx_HAL_Driver
  100. * @{
  101. */
  102. /** @defgroup CRYPEx CRYPEx
  103. * @brief CRYP Extension HAL module driver.
  104. * @{
  105. */
  106. #ifdef HAL_CRYP_MODULE_ENABLED
  107. #if defined(CRYP)
  108. /* Private typedef -----------------------------------------------------------*/
  109. /* Private define ------------------------------------------------------------*/
  110. /** @addtogroup CRYPEx_Private_define
  111. * @{
  112. */
  113. #define CRYPEx_TIMEOUT_VALUE 1U
  114. /**
  115. * @}
  116. */
  117. /* Private macro -------------------------------------------------------------*/
  118. /* Private variables ---------------------------------------------------------*/
  119. /* Private function prototypes -----------------------------------------------*/
  120. /** @defgroup CRYPEx_Private_Functions_prototypes CRYP Private Functions Prototypes
  121. * @{
  122. */
  123. static void CRYPEx_GCMCCM_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector);
  124. static void CRYPEx_GCMCCM_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize);
  125. static HAL_StatusTypeDef CRYPEx_GCMCCM_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t *Input, uint16_t Ilength, uint8_t *Output, uint32_t Timeout);
  126. static HAL_StatusTypeDef CRYPEx_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint32_t Timeout);
  127. static void CRYPEx_GCMCCM_DMAInCplt(DMA_HandleTypeDef *hdma);
  128. static void CRYPEx_GCMCCM_DMAOutCplt(DMA_HandleTypeDef *hdma);
  129. static void CRYPEx_GCMCCM_DMAError(DMA_HandleTypeDef *hdma);
  130. static void CRYPEx_GCMCCM_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  131. /**
  132. * @}
  133. */
  134. /* Private functions ---------------------------------------------------------*/
  135. /** @addtogroup CRYPEx_Private_Functions
  136. * @{
  137. */
  138. /**
  139. * @brief DMA CRYP Input Data process complete callback.
  140. * @param hdma: DMA handle
  141. * @retval None
  142. */
  143. static void CRYPEx_GCMCCM_DMAInCplt(DMA_HandleTypeDef *hdma)
  144. {
  145. CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  146. /* Disable the DMA transfer for input Fifo request by resetting the DIEN bit
  147. in the DMACR register */
  148. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DIEN);
  149. /* Call input data transfer complete callback */
  150. HAL_CRYP_InCpltCallback(hcryp);
  151. }
  152. /**
  153. * @brief DMA CRYP Output Data process complete callback.
  154. * @param hdma: DMA handle
  155. * @retval None
  156. */
  157. static void CRYPEx_GCMCCM_DMAOutCplt(DMA_HandleTypeDef *hdma)
  158. {
  159. CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  160. /* Disable the DMA transfer for output Fifo request by resetting the DOEN bit
  161. in the DMACR register */
  162. hcryp->Instance->DMACR &= (uint32_t)(~CRYP_DMACR_DOEN);
  163. /* Enable the CRYP peripheral */
  164. __HAL_CRYP_DISABLE(hcryp);
  165. /* Change the CRYP peripheral state */
  166. hcryp->State = HAL_CRYP_STATE_READY;
  167. /* Call output data transfer complete callback */
  168. HAL_CRYP_OutCpltCallback(hcryp);
  169. }
  170. /**
  171. * @brief DMA CRYP communication error callback.
  172. * @param hdma: DMA handle
  173. * @retval None
  174. */
  175. static void CRYPEx_GCMCCM_DMAError(DMA_HandleTypeDef *hdma)
  176. {
  177. CRYP_HandleTypeDef* hcryp = ( CRYP_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  178. hcryp->State= HAL_CRYP_STATE_READY;
  179. HAL_CRYP_ErrorCallback(hcryp);
  180. }
  181. /**
  182. * @brief Writes the Key in Key registers.
  183. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  184. * the configuration information for CRYP module
  185. * @param Key: Pointer to Key buffer
  186. * @param KeySize: Size of Key
  187. * @retval None
  188. */
  189. static void CRYPEx_GCMCCM_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key, uint32_t KeySize)
  190. {
  191. uint32_t keyaddr = (uint32_t)Key;
  192. switch(KeySize)
  193. {
  194. case CRYP_KEYSIZE_256B:
  195. /* Key Initialisation */
  196. hcryp->Instance->K0LR = __REV(*(uint32_t*)(keyaddr));
  197. keyaddr+=4U;
  198. hcryp->Instance->K0RR = __REV(*(uint32_t*)(keyaddr));
  199. keyaddr+=4U;
  200. hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));
  201. keyaddr+=4U;
  202. hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));
  203. keyaddr+=4U;
  204. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  205. keyaddr+=4U;
  206. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  207. keyaddr+=4U;
  208. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  209. keyaddr+=4U;
  210. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  211. break;
  212. case CRYP_KEYSIZE_192B:
  213. hcryp->Instance->K1LR = __REV(*(uint32_t*)(keyaddr));
  214. keyaddr+=4U;
  215. hcryp->Instance->K1RR = __REV(*(uint32_t*)(keyaddr));
  216. keyaddr+=4U;
  217. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  218. keyaddr+=4U;
  219. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  220. keyaddr+=4U;
  221. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  222. keyaddr+=4U;
  223. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  224. break;
  225. case CRYP_KEYSIZE_128B:
  226. hcryp->Instance->K2LR = __REV(*(uint32_t*)(keyaddr));
  227. keyaddr+=4U;
  228. hcryp->Instance->K2RR = __REV(*(uint32_t*)(keyaddr));
  229. keyaddr+=4U;
  230. hcryp->Instance->K3LR = __REV(*(uint32_t*)(keyaddr));
  231. keyaddr+=4U;
  232. hcryp->Instance->K3RR = __REV(*(uint32_t*)(keyaddr));
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. /**
  239. * @brief Writes the InitVector/InitCounter in IV registers.
  240. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  241. * the configuration information for CRYP module
  242. * @param InitVector: Pointer to InitVector/InitCounter buffer
  243. * @retval None
  244. */
  245. static void CRYPEx_GCMCCM_SetInitVector(CRYP_HandleTypeDef *hcryp, uint8_t *InitVector)
  246. {
  247. uint32_t ivaddr = (uint32_t)InitVector;
  248. hcryp->Instance->IV0LR = __REV(*(uint32_t*)(ivaddr));
  249. ivaddr+=4U;
  250. hcryp->Instance->IV0RR = __REV(*(uint32_t*)(ivaddr));
  251. ivaddr+=4U;
  252. hcryp->Instance->IV1LR = __REV(*(uint32_t*)(ivaddr));
  253. ivaddr+=4U;
  254. hcryp->Instance->IV1RR = __REV(*(uint32_t*)(ivaddr));
  255. }
  256. /**
  257. * @brief Process Data: Writes Input data in polling mode and read the Output data.
  258. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  259. * the configuration information for CRYP module
  260. * @param Input: Pointer to the Input buffer.
  261. * @param Ilength: Length of the Input buffer, must be a multiple of 16
  262. * @param Output: Pointer to the returned buffer
  263. * @param Timeout: Timeout value
  264. * @retval None
  265. */
  266. static HAL_StatusTypeDef CRYPEx_GCMCCM_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t *Input, uint16_t Ilength, uint8_t *Output, uint32_t Timeout)
  267. {
  268. uint32_t tickstart = 0U;
  269. uint32_t i = 0U;
  270. uint32_t inputaddr = (uint32_t)Input;
  271. uint32_t outputaddr = (uint32_t)Output;
  272. for(i=0U; (i < Ilength); i+=16U)
  273. {
  274. /* Write the Input block in the IN FIFO */
  275. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  276. inputaddr+=4U;
  277. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  278. inputaddr+=4U;
  279. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  280. inputaddr+=4U;
  281. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  282. inputaddr+=4U;
  283. /* Get tick */
  284. tickstart = HAL_GetTick();
  285. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  286. {
  287. /* Check for the Timeout */
  288. if(Timeout != HAL_MAX_DELAY)
  289. {
  290. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  291. {
  292. /* Change state */
  293. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  294. /* Process Unlocked */
  295. __HAL_UNLOCK(hcryp);
  296. return HAL_TIMEOUT;
  297. }
  298. }
  299. }
  300. /* Read the Output block from the OUT FIFO */
  301. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  302. outputaddr+=4U;
  303. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  304. outputaddr+=4U;
  305. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  306. outputaddr+=4U;
  307. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  308. outputaddr+=4U;
  309. }
  310. /* Return function status */
  311. return HAL_OK;
  312. }
  313. /**
  314. * @brief Sets the header phase
  315. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  316. * the configuration information for CRYP module
  317. * @param Input: Pointer to the Input buffer.
  318. * @param Ilength: Length of the Input buffer, must be a multiple of 16
  319. * @param Timeout: Timeout value
  320. * @retval None
  321. */
  322. static HAL_StatusTypeDef CRYPEx_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint32_t Timeout)
  323. {
  324. uint32_t tickstart = 0U;
  325. uint32_t loopcounter = 0U;
  326. uint32_t headeraddr = (uint32_t)Input;
  327. /* Prevent unused argument(s) compilation warning */
  328. UNUSED(Ilength);
  329. /***************************** Header phase *********************************/
  330. if(hcryp->Init.HeaderSize != 0U)
  331. {
  332. /* Select header phase */
  333. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  334. /* Enable the CRYP peripheral */
  335. __HAL_CRYP_ENABLE(hcryp);
  336. for(loopcounter = 0U; (loopcounter < hcryp->Init.HeaderSize); loopcounter+=16U)
  337. {
  338. /* Get tick */
  339. tickstart = HAL_GetTick();
  340. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  341. {
  342. /* Check for the Timeout */
  343. if(Timeout != HAL_MAX_DELAY)
  344. {
  345. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  346. {
  347. /* Change state */
  348. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  349. /* Process Unlocked */
  350. __HAL_UNLOCK(hcryp);
  351. return HAL_TIMEOUT;
  352. }
  353. }
  354. }
  355. /* Write the Input block in the IN FIFO */
  356. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  357. headeraddr+=4U;
  358. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  359. headeraddr+=4U;
  360. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  361. headeraddr+=4U;
  362. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  363. headeraddr+=4U;
  364. }
  365. /* Wait until the complete message has been processed */
  366. /* Get tick */
  367. tickstart = HAL_GetTick();
  368. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  369. {
  370. /* Check for the Timeout */
  371. if(Timeout != HAL_MAX_DELAY)
  372. {
  373. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  374. {
  375. /* Change state */
  376. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  377. /* Process Unlocked */
  378. __HAL_UNLOCK(hcryp);
  379. return HAL_TIMEOUT;
  380. }
  381. }
  382. }
  383. }
  384. /* Return function status */
  385. return HAL_OK;
  386. }
  387. /**
  388. * @brief Sets the DMA configuration and start the DMA transfer.
  389. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  390. * the configuration information for CRYP module
  391. * @param inputaddr: Address of the Input buffer
  392. * @param Size: Size of the Input buffer, must be a multiple of 16
  393. * @param outputaddr: Address of the Output buffer
  394. * @retval None
  395. */
  396. static void CRYPEx_GCMCCM_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  397. {
  398. /* Set the CRYP DMA transfer complete callback */
  399. hcryp->hdmain->XferCpltCallback = CRYPEx_GCMCCM_DMAInCplt;
  400. /* Set the DMA error callback */
  401. hcryp->hdmain->XferErrorCallback = CRYPEx_GCMCCM_DMAError;
  402. /* Set the CRYP DMA transfer complete callback */
  403. hcryp->hdmaout->XferCpltCallback = CRYPEx_GCMCCM_DMAOutCplt;
  404. /* Set the DMA error callback */
  405. hcryp->hdmaout->XferErrorCallback = CRYPEx_GCMCCM_DMAError;
  406. /* Enable the CRYP peripheral */
  407. __HAL_CRYP_ENABLE(hcryp);
  408. /* Enable the DMA In DMA Stream */
  409. HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DR, Size/4U);
  410. /* Enable In DMA request */
  411. hcryp->Instance->DMACR = CRYP_DMACR_DIEN;
  412. /* Enable the DMA Out DMA Stream */
  413. HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUT, outputaddr, Size/4U);
  414. /* Enable Out DMA request */
  415. hcryp->Instance->DMACR |= CRYP_DMACR_DOEN;
  416. }
  417. /**
  418. * @}
  419. */
  420. /* Exported functions---------------------------------------------------------*/
  421. /** @addtogroup CRYPEx_Exported_Functions
  422. * @{
  423. */
  424. /** @defgroup CRYPEx_Exported_Functions_Group1 Extended AES processing functions
  425. * @brief Extended processing functions.
  426. *
  427. @verbatim
  428. ==============================================================================
  429. ##### Extended AES processing functions #####
  430. ==============================================================================
  431. [..] This section provides functions allowing to:
  432. (+) Encrypt plaintext using AES-128/192/256 using GCM and CCM chaining modes
  433. (+) Decrypt cyphertext using AES-128/192/256 using GCM and CCM chaining modes
  434. (+) Finish the processing. This function is available only for GCM and CCM
  435. [..] Three processing methods are available:
  436. (+) Polling mode
  437. (+) Interrupt mode
  438. (+) DMA mode
  439. @endverbatim
  440. * @{
  441. */
  442. /**
  443. * @brief Initializes the CRYP peripheral in AES CCM encryption mode then
  444. * encrypt pPlainData. The cypher data are available in pCypherData.
  445. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  446. * the configuration information for CRYP module
  447. * @param pPlainData: Pointer to the plaintext buffer
  448. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  449. * @param pCypherData: Pointer to the cyphertext buffer
  450. * @param Timeout: Timeout duration
  451. * @retval HAL status
  452. */
  453. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  454. {
  455. uint32_t tickstart = 0U;
  456. uint32_t headersize = hcryp->Init.HeaderSize;
  457. uint32_t headeraddr = (uint32_t)hcryp->Init.Header;
  458. uint32_t loopcounter = 0U;
  459. uint32_t bufferidx = 0U;
  460. uint8_t blockb0[16U] = {0};/* Block B0 */
  461. uint8_t ctr[16U] = {0}; /* Counter */
  462. uint32_t b0addr = (uint32_t)blockb0;
  463. /* Process Locked */
  464. __HAL_LOCK(hcryp);
  465. /* Change the CRYP peripheral state */
  466. hcryp->State = HAL_CRYP_STATE_BUSY;
  467. /* Check if initialization phase has already been performed */
  468. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  469. {
  470. /************************ Formatting the header block *********************/
  471. if(headersize != 0U)
  472. {
  473. /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
  474. if(headersize < 65280U)
  475. {
  476. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
  477. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
  478. headersize += 2U;
  479. }
  480. else
  481. {
  482. /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
  483. hcryp->Init.pScratch[bufferidx++] = 0xFFU;
  484. hcryp->Init.pScratch[bufferidx++] = 0xFEU;
  485. hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U;
  486. hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U;
  487. hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U;
  488. hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU;
  489. headersize += 6U;
  490. }
  491. /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */
  492. for(loopcounter = 0U; loopcounter < headersize; loopcounter++)
  493. {
  494. hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];
  495. }
  496. /* Check if the header size is modulo 16 */
  497. if ((headersize % 16U) != 0U)
  498. {
  499. /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */
  500. for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++)
  501. {
  502. hcryp->Init.pScratch[loopcounter] = 0U;
  503. }
  504. /* Set the header size to modulo 16 */
  505. headersize = ((headersize/16U) + 1U) * 16U;
  506. }
  507. /* Set the pointer headeraddr to hcryp->Init.pScratch */
  508. headeraddr = (uint32_t)hcryp->Init.pScratch;
  509. }
  510. /*********************** Formatting the block B0 **************************/
  511. if(headersize != 0U)
  512. {
  513. blockb0[0U] = 0x40U;
  514. }
  515. /* Flags byte */
  516. /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */
  517. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1U) & (uint8_t)0x07) << 3U);
  518. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);
  519. for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++)
  520. {
  521. blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter];
  522. }
  523. for ( ; loopcounter < 13U; loopcounter++)
  524. {
  525. blockb0[loopcounter+1U] = 0U;
  526. }
  527. blockb0[14U] = (Size >> 8U);
  528. blockb0[15U] = (Size & 0xFFU);
  529. /************************* Formatting the initial counter *****************/
  530. /* Byte 0:
  531. Bits 7 and 6 are reserved and shall be set to 0
  532. Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter blocks
  533. are distinct from B0
  534. Bits 0, 1, and 2 contain the same encoding of q as in B0
  535. */
  536. ctr[0U] = blockb0[0U] & 0x07U;
  537. /* byte 1 to NonceSize is the IV (Nonce) */
  538. for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++)
  539. {
  540. ctr[loopcounter] = blockb0[loopcounter];
  541. }
  542. /* Set the LSB to 1 */
  543. ctr[15U] |= 0x01U;
  544. /* Set the key */
  545. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  546. /* Set the CRYP peripheral in AES CCM mode */
  547. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT);
  548. /* Set the Initialization Vector */
  549. CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);
  550. /* Select init phase */
  551. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  552. b0addr = (uint32_t)blockb0;
  553. /* Write the blockb0 block in the IN FIFO */
  554. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  555. b0addr+=4U;
  556. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  557. b0addr+=4U;
  558. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  559. b0addr+=4U;
  560. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  561. /* Enable the CRYP peripheral */
  562. __HAL_CRYP_ENABLE(hcryp);
  563. /* Get tick */
  564. tickstart = HAL_GetTick();
  565. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  566. {
  567. /* Check for the Timeout */
  568. if(Timeout != HAL_MAX_DELAY)
  569. {
  570. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  571. {
  572. /* Change state */
  573. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  574. /* Process Unlocked */
  575. __HAL_UNLOCK(hcryp);
  576. return HAL_TIMEOUT;
  577. }
  578. }
  579. }
  580. /***************************** Header phase *******************************/
  581. if(headersize != 0U)
  582. {
  583. /* Select header phase */
  584. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  585. /* Enable the CRYP peripheral */
  586. __HAL_CRYP_ENABLE(hcryp);
  587. for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U)
  588. {
  589. /* Get tick */
  590. tickstart = HAL_GetTick();
  591. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  592. {
  593. {
  594. /* Check for the Timeout */
  595. if(Timeout != HAL_MAX_DELAY)
  596. {
  597. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  598. {
  599. /* Change state */
  600. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  601. /* Process Unlocked */
  602. __HAL_UNLOCK(hcryp);
  603. return HAL_TIMEOUT;
  604. }
  605. }
  606. }
  607. }
  608. /* Write the header block in the IN FIFO */
  609. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  610. headeraddr+=4U;
  611. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  612. headeraddr+=4U;
  613. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  614. headeraddr+=4U;
  615. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  616. headeraddr+=4U;
  617. }
  618. /* Get tick */
  619. tickstart = HAL_GetTick();
  620. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  621. {
  622. /* Check for the Timeout */
  623. if(Timeout != HAL_MAX_DELAY)
  624. {
  625. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  626. {
  627. /* Change state */
  628. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  629. /* Process Unlocked */
  630. __HAL_UNLOCK(hcryp);
  631. return HAL_TIMEOUT;
  632. }
  633. }
  634. }
  635. }
  636. /* Save formatted counter into the scratch buffer pScratch */
  637. for(loopcounter = 0U; (loopcounter < 16U); loopcounter++)
  638. {
  639. hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];
  640. }
  641. /* Reset bit 0 */
  642. hcryp->Init.pScratch[15U] &= 0xFEU;
  643. /* Select payload phase once the header phase is performed */
  644. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  645. /* Flush FIFO */
  646. __HAL_CRYP_FIFO_FLUSH(hcryp);
  647. /* Enable the CRYP peripheral */
  648. __HAL_CRYP_ENABLE(hcryp);
  649. /* Set the phase */
  650. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  651. }
  652. /* Write Plain Data and Get Cypher Data */
  653. if(CRYPEx_GCMCCM_ProcessData(hcryp,pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  654. {
  655. return HAL_TIMEOUT;
  656. }
  657. /* Change the CRYP peripheral state */
  658. hcryp->State = HAL_CRYP_STATE_READY;
  659. /* Process Unlocked */
  660. __HAL_UNLOCK(hcryp);
  661. /* Return function status */
  662. return HAL_OK;
  663. }
  664. /**
  665. * @brief Initializes the CRYP peripheral in AES GCM encryption mode then
  666. * encrypt pPlainData. The cypher data are available in pCypherData.
  667. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  668. * the configuration information for CRYP module
  669. * @param pPlainData: Pointer to the plaintext buffer
  670. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  671. * @param pCypherData: Pointer to the cyphertext buffer
  672. * @param Timeout: Timeout duration
  673. * @retval HAL status
  674. */
  675. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  676. {
  677. uint32_t tickstart = 0U;
  678. /* Process Locked */
  679. __HAL_LOCK(hcryp);
  680. /* Change the CRYP peripheral state */
  681. hcryp->State = HAL_CRYP_STATE_BUSY;
  682. /* Check if initialization phase has already been performed */
  683. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  684. {
  685. /* Set the key */
  686. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  687. /* Set the CRYP peripheral in AES GCM mode */
  688. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT);
  689. /* Set the Initialization Vector */
  690. CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);
  691. /* Flush FIFO */
  692. __HAL_CRYP_FIFO_FLUSH(hcryp);
  693. /* Enable the CRYP peripheral */
  694. __HAL_CRYP_ENABLE(hcryp);
  695. /* Get tick */
  696. tickstart = HAL_GetTick();
  697. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  698. {
  699. /* Check for the Timeout */
  700. if(Timeout != HAL_MAX_DELAY)
  701. {
  702. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  703. {
  704. /* Change state */
  705. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  706. /* Process Unlocked */
  707. __HAL_UNLOCK(hcryp);
  708. return HAL_TIMEOUT;
  709. }
  710. }
  711. }
  712. /* Set the header phase */
  713. if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, Timeout) != HAL_OK)
  714. {
  715. return HAL_TIMEOUT;
  716. }
  717. /* Disable the CRYP peripheral */
  718. __HAL_CRYP_DISABLE(hcryp);
  719. /* Select payload phase once the header phase is performed */
  720. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  721. /* Flush FIFO */
  722. __HAL_CRYP_FIFO_FLUSH(hcryp);
  723. /* Enable the CRYP peripheral */
  724. __HAL_CRYP_ENABLE(hcryp);
  725. /* Set the phase */
  726. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  727. }
  728. /* Write Plain Data and Get Cypher Data */
  729. if(CRYPEx_GCMCCM_ProcessData(hcryp, pPlainData, Size, pCypherData, Timeout) != HAL_OK)
  730. {
  731. return HAL_TIMEOUT;
  732. }
  733. /* Change the CRYP peripheral state */
  734. hcryp->State = HAL_CRYP_STATE_READY;
  735. /* Process Unlocked */
  736. __HAL_UNLOCK(hcryp);
  737. /* Return function status */
  738. return HAL_OK;
  739. }
  740. /**
  741. * @brief Initializes the CRYP peripheral in AES GCM decryption mode then
  742. * decrypted pCypherData. The cypher data are available in pPlainData.
  743. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  744. * the configuration information for CRYP module
  745. * @param pCypherData: Pointer to the cyphertext buffer
  746. * @param Size: Length of the cyphertext buffer, must be a multiple of 16
  747. * @param pPlainData: Pointer to the plaintext buffer
  748. * @param Timeout: Timeout duration
  749. * @retval HAL status
  750. */
  751. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  752. {
  753. uint32_t tickstart = 0U;
  754. /* Process Locked */
  755. __HAL_LOCK(hcryp);
  756. /* Change the CRYP peripheral state */
  757. hcryp->State = HAL_CRYP_STATE_BUSY;
  758. /* Check if initialization phase has already been performed */
  759. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  760. {
  761. /* Set the key */
  762. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  763. /* Set the CRYP peripheral in AES GCM decryption mode */
  764. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT);
  765. /* Set the Initialization Vector */
  766. CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);
  767. /* Flush FIFO */
  768. __HAL_CRYP_FIFO_FLUSH(hcryp);
  769. /* Enable the CRYP peripheral */
  770. __HAL_CRYP_ENABLE(hcryp);
  771. /* Get tick */
  772. tickstart = HAL_GetTick();
  773. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  774. {
  775. /* Check for the Timeout */
  776. if(Timeout != HAL_MAX_DELAY)
  777. {
  778. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  779. {
  780. /* Change state */
  781. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  782. /* Process Unlocked */
  783. __HAL_UNLOCK(hcryp);
  784. return HAL_TIMEOUT;
  785. }
  786. }
  787. }
  788. /* Set the header phase */
  789. if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, Timeout) != HAL_OK)
  790. {
  791. return HAL_TIMEOUT;
  792. }
  793. /* Disable the CRYP peripheral */
  794. __HAL_CRYP_DISABLE(hcryp);
  795. /* Select payload phase once the header phase is performed */
  796. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  797. /* Enable the CRYP peripheral */
  798. __HAL_CRYP_ENABLE(hcryp);
  799. /* Set the phase */
  800. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  801. }
  802. /* Write Plain Data and Get Cypher Data */
  803. if(CRYPEx_GCMCCM_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  804. {
  805. return HAL_TIMEOUT;
  806. }
  807. /* Change the CRYP peripheral state */
  808. hcryp->State = HAL_CRYP_STATE_READY;
  809. /* Process Unlocked */
  810. __HAL_UNLOCK(hcryp);
  811. /* Return function status */
  812. return HAL_OK;
  813. }
  814. /**
  815. * @brief Computes the authentication TAG.
  816. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  817. * the configuration information for CRYP module
  818. * @param Size: Total length of the plain/cyphertext buffer
  819. * @param AuthTag: Pointer to the authentication buffer
  820. * @param Timeout: Timeout duration
  821. * @retval HAL status
  822. */
  823. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Finish(CRYP_HandleTypeDef *hcryp, uint32_t Size, uint8_t *AuthTag, uint32_t Timeout)
  824. {
  825. uint32_t tickstart = 0U;
  826. uint64_t headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
  827. uint64_t inputlength = Size * 8U; /* input length in bits */
  828. uint32_t tagaddr = (uint32_t)AuthTag;
  829. /* Process Locked */
  830. __HAL_LOCK(hcryp);
  831. /* Change the CRYP peripheral state */
  832. hcryp->State = HAL_CRYP_STATE_BUSY;
  833. /* Check if initialization phase has already been performed */
  834. if(hcryp->Phase == HAL_CRYP_PHASE_PROCESS)
  835. {
  836. /* Change the CRYP phase */
  837. hcryp->Phase = HAL_CRYP_PHASE_FINAL;
  838. /* Disable CRYP to start the final phase */
  839. __HAL_CRYP_DISABLE(hcryp);
  840. /* Select final phase */
  841. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_FINAL);
  842. /* Enable the CRYP peripheral */
  843. __HAL_CRYP_ENABLE(hcryp);
  844. /* Write the number of bits in header (64 bits) followed by the number of bits
  845. in the payload */
  846. if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  847. {
  848. hcryp->Instance->DR = __RBIT(headerlength >> 32U);
  849. hcryp->Instance->DR = __RBIT(headerlength);
  850. hcryp->Instance->DR = __RBIT(inputlength >> 32U);
  851. hcryp->Instance->DR = __RBIT(inputlength);
  852. }
  853. else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  854. {
  855. hcryp->Instance->DR = __REV(headerlength >> 32U);
  856. hcryp->Instance->DR = __REV(headerlength);
  857. hcryp->Instance->DR = __REV(inputlength >> 32U);
  858. hcryp->Instance->DR = __REV(inputlength);
  859. }
  860. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  861. {
  862. hcryp->Instance->DR = __ROR((uint32_t)(headerlength >> 32U), 16U);
  863. hcryp->Instance->DR = __ROR((uint32_t)headerlength, 16U);
  864. hcryp->Instance->DR = __ROR((uint32_t)(inputlength >> 32U), 16U);
  865. hcryp->Instance->DR = __ROR((uint32_t)inputlength, 16U);
  866. }
  867. else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
  868. {
  869. hcryp->Instance->DR = (uint32_t)(headerlength >> 32U);
  870. hcryp->Instance->DR = (uint32_t)(headerlength);
  871. hcryp->Instance->DR = (uint32_t)(inputlength >> 32U);
  872. hcryp->Instance->DR = (uint32_t)(inputlength);
  873. }
  874. /* Get tick */
  875. tickstart = HAL_GetTick();
  876. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  877. {
  878. /* Check for the Timeout */
  879. if(Timeout != HAL_MAX_DELAY)
  880. {
  881. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  882. {
  883. /* Change state */
  884. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  885. /* Process Unlocked */
  886. __HAL_UNLOCK(hcryp);
  887. return HAL_TIMEOUT;
  888. }
  889. }
  890. }
  891. /* Read the Auth TAG in the IN FIFO */
  892. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT;
  893. tagaddr+=4U;
  894. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT;
  895. tagaddr+=4U;
  896. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT;
  897. tagaddr+=4U;
  898. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUT;
  899. }
  900. /* Change the CRYP peripheral state */
  901. hcryp->State = HAL_CRYP_STATE_READY;
  902. /* Process Unlocked */
  903. __HAL_UNLOCK(hcryp);
  904. /* Return function status */
  905. return HAL_OK;
  906. }
  907. /**
  908. * @brief Computes the authentication TAG for AES CCM mode.
  909. * @note This API is called after HAL_AES_CCM_Encrypt()/HAL_AES_CCM_Decrypt()
  910. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  911. * the configuration information for CRYP module
  912. * @param AuthTag: Pointer to the authentication buffer
  913. * @param Timeout: Timeout duration
  914. * @retval HAL status
  915. */
  916. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Finish(CRYP_HandleTypeDef *hcryp, uint8_t *AuthTag, uint32_t Timeout)
  917. {
  918. uint32_t tickstart = 0U;
  919. uint32_t tagaddr = (uint32_t)AuthTag;
  920. uint32_t ctraddr = (uint32_t)hcryp->Init.pScratch;
  921. uint32_t temptag[4U] = {0U}; /* Temporary TAG (MAC) */
  922. uint32_t loopcounter;
  923. /* Process Locked */
  924. __HAL_LOCK(hcryp);
  925. /* Change the CRYP peripheral state */
  926. hcryp->State = HAL_CRYP_STATE_BUSY;
  927. /* Check if initialization phase has already been performed */
  928. if(hcryp->Phase == HAL_CRYP_PHASE_PROCESS)
  929. {
  930. /* Change the CRYP phase */
  931. hcryp->Phase = HAL_CRYP_PHASE_FINAL;
  932. /* Disable CRYP to start the final phase */
  933. __HAL_CRYP_DISABLE(hcryp);
  934. /* Select final phase */
  935. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_FINAL);
  936. /* Enable the CRYP peripheral */
  937. __HAL_CRYP_ENABLE(hcryp);
  938. /* Write the counter block in the IN FIFO */
  939. hcryp->Instance->DR = *(uint32_t*)ctraddr;
  940. ctraddr+=4U;
  941. hcryp->Instance->DR = *(uint32_t*)ctraddr;
  942. ctraddr+=4U;
  943. hcryp->Instance->DR = *(uint32_t*)ctraddr;
  944. ctraddr+=4U;
  945. hcryp->Instance->DR = *(uint32_t*)ctraddr;
  946. /* Get tick */
  947. tickstart = HAL_GetTick();
  948. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  949. {
  950. /* Check for the Timeout */
  951. if(Timeout != HAL_MAX_DELAY)
  952. {
  953. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  954. {
  955. /* Change state */
  956. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  957. /* Process Unlocked */
  958. __HAL_UNLOCK(hcryp);
  959. return HAL_TIMEOUT;
  960. }
  961. }
  962. }
  963. /* Read the Auth TAG in the IN FIFO */
  964. temptag[0U] = hcryp->Instance->DOUT;
  965. temptag[1U] = hcryp->Instance->DOUT;
  966. temptag[2U] = hcryp->Instance->DOUT;
  967. temptag[3U] = hcryp->Instance->DOUT;
  968. }
  969. /* Copy temporary authentication TAG in user TAG buffer */
  970. for(loopcounter = 0U; loopcounter < hcryp->Init.TagSize ; loopcounter++)
  971. {
  972. /* Set the authentication TAG buffer */
  973. *((uint8_t*)tagaddr+loopcounter) = *((uint8_t*)temptag+loopcounter);
  974. }
  975. /* Change the CRYP peripheral state */
  976. hcryp->State = HAL_CRYP_STATE_READY;
  977. /* Process Unlocked */
  978. __HAL_UNLOCK(hcryp);
  979. /* Return function status */
  980. return HAL_OK;
  981. }
  982. /**
  983. * @brief Initializes the CRYP peripheral in AES CCM decryption mode then
  984. * decrypted pCypherData. The cypher data are available in pPlainData.
  985. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  986. * the configuration information for CRYP module
  987. * @param pPlainData: Pointer to the plaintext buffer
  988. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  989. * @param pCypherData: Pointer to the cyphertext buffer
  990. * @param Timeout: Timeout duration
  991. * @retval HAL status
  992. */
  993. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  994. {
  995. uint32_t tickstart = 0U;
  996. uint32_t headersize = hcryp->Init.HeaderSize;
  997. uint32_t headeraddr = (uint32_t)hcryp->Init.Header;
  998. uint32_t loopcounter = 0U;
  999. uint32_t bufferidx = 0U;
  1000. uint8_t blockb0[16U] = {0};/* Block B0 */
  1001. uint8_t ctr[16U] = {0}; /* Counter */
  1002. uint32_t b0addr = (uint32_t)blockb0;
  1003. /* Process Locked */
  1004. __HAL_LOCK(hcryp);
  1005. /* Change the CRYP peripheral state */
  1006. hcryp->State = HAL_CRYP_STATE_BUSY;
  1007. /* Check if initialization phase has already been performed */
  1008. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1009. {
  1010. /************************ Formatting the header block *********************/
  1011. if(headersize != 0U)
  1012. {
  1013. /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
  1014. if(headersize < 65280U)
  1015. {
  1016. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
  1017. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
  1018. headersize += 2U;
  1019. }
  1020. else
  1021. {
  1022. /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
  1023. hcryp->Init.pScratch[bufferidx++] = 0xFFU;
  1024. hcryp->Init.pScratch[bufferidx++] = 0xFEU;
  1025. hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U;
  1026. hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U;
  1027. hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U;
  1028. hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU;
  1029. headersize += 6U;
  1030. }
  1031. /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */
  1032. for(loopcounter = 0U; loopcounter < headersize; loopcounter++)
  1033. {
  1034. hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];
  1035. }
  1036. /* Check if the header size is modulo 16 */
  1037. if ((headersize % 16U) != 0U)
  1038. {
  1039. /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */
  1040. for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++)
  1041. {
  1042. hcryp->Init.pScratch[loopcounter] = 0U;
  1043. }
  1044. /* Set the header size to modulo 16 */
  1045. headersize = ((headersize/16U) + 1U) * 16U;
  1046. }
  1047. /* Set the pointer headeraddr to hcryp->Init.pScratch */
  1048. headeraddr = (uint32_t)hcryp->Init.pScratch;
  1049. }
  1050. /*********************** Formatting the block B0 **************************/
  1051. if(headersize != 0U)
  1052. {
  1053. blockb0[0U] = 0x40U;
  1054. }
  1055. /* Flags byte */
  1056. /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */
  1057. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2U))) >> 1U) & (uint8_t)0x07U) << 3U);
  1058. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15U) - hcryp->Init.IVSize) - (uint8_t)1U) & (uint8_t)0x07U);
  1059. for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++)
  1060. {
  1061. blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter];
  1062. }
  1063. for ( ; loopcounter < 13U; loopcounter++)
  1064. {
  1065. blockb0[loopcounter+1U] = 0U;
  1066. }
  1067. blockb0[14U] = (Size >> 8U);
  1068. blockb0[15U] = (Size & 0xFFU);
  1069. /************************* Formatting the initial counter *****************/
  1070. /* Byte 0:
  1071. Bits 7 and 6 are reserved and shall be set to 0
  1072. Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter
  1073. blocks are distinct from B0
  1074. Bits 0, 1, and 2 contain the same encoding of q as in B0
  1075. */
  1076. ctr[0U] = blockb0[0U] & 0x07U;
  1077. /* byte 1 to NonceSize is the IV (Nonce) */
  1078. for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++)
  1079. {
  1080. ctr[loopcounter] = blockb0[loopcounter];
  1081. }
  1082. /* Set the LSB to 1 */
  1083. ctr[15U] |= 0x01U;
  1084. /* Set the key */
  1085. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1086. /* Set the CRYP peripheral in AES CCM mode */
  1087. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT);
  1088. /* Set the Initialization Vector */
  1089. CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);
  1090. /* Select init phase */
  1091. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  1092. b0addr = (uint32_t)blockb0;
  1093. /* Write the blockb0 block in the IN FIFO */
  1094. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1095. b0addr+=4U;
  1096. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1097. b0addr+=4U;
  1098. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1099. b0addr+=4U;
  1100. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1101. /* Enable the CRYP peripheral */
  1102. __HAL_CRYP_ENABLE(hcryp);
  1103. /* Get tick */
  1104. tickstart = HAL_GetTick();
  1105. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  1106. {
  1107. /* Check for the Timeout */
  1108. if(Timeout != HAL_MAX_DELAY)
  1109. {
  1110. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1111. {
  1112. /* Change state */
  1113. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1114. /* Process Unlocked */
  1115. __HAL_UNLOCK(hcryp);
  1116. return HAL_TIMEOUT;
  1117. }
  1118. }
  1119. }
  1120. /***************************** Header phase *******************************/
  1121. if(headersize != 0U)
  1122. {
  1123. /* Select header phase */
  1124. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  1125. /* Enable Crypto processor */
  1126. __HAL_CRYP_ENABLE(hcryp);
  1127. for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U)
  1128. {
  1129. /* Get tick */
  1130. tickstart = HAL_GetTick();
  1131. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  1132. {
  1133. /* Check for the Timeout */
  1134. if(Timeout != HAL_MAX_DELAY)
  1135. {
  1136. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1137. {
  1138. /* Change state */
  1139. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1140. /* Process Unlocked */
  1141. __HAL_UNLOCK(hcryp);
  1142. return HAL_TIMEOUT;
  1143. }
  1144. }
  1145. }
  1146. /* Write the header block in the IN FIFO */
  1147. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1148. headeraddr+=4U;
  1149. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1150. headeraddr+=4U;
  1151. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1152. headeraddr+=4U;
  1153. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1154. headeraddr+=4U;
  1155. }
  1156. /* Get tick */
  1157. tickstart = HAL_GetTick();
  1158. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  1159. {
  1160. /* Check for the Timeout */
  1161. if(Timeout != HAL_MAX_DELAY)
  1162. {
  1163. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1164. {
  1165. /* Change state */
  1166. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1167. /* Process Unlocked */
  1168. __HAL_UNLOCK(hcryp);
  1169. return HAL_TIMEOUT;
  1170. }
  1171. }
  1172. }
  1173. }
  1174. /* Save formatted counter into the scratch buffer pScratch */
  1175. for(loopcounter = 0U; (loopcounter < 16U); loopcounter++)
  1176. {
  1177. hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];
  1178. }
  1179. /* Reset bit 0 */
  1180. hcryp->Init.pScratch[15U] &= 0xFEU;
  1181. /* Select payload phase once the header phase is performed */
  1182. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  1183. /* Flush FIFO */
  1184. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1185. /* Enable the CRYP peripheral */
  1186. __HAL_CRYP_ENABLE(hcryp);
  1187. /* Set the phase */
  1188. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1189. }
  1190. /* Write Plain Data and Get Cypher Data */
  1191. if(CRYPEx_GCMCCM_ProcessData(hcryp, pCypherData, Size, pPlainData, Timeout) != HAL_OK)
  1192. {
  1193. return HAL_TIMEOUT;
  1194. }
  1195. /* Change the CRYP peripheral state */
  1196. hcryp->State = HAL_CRYP_STATE_READY;
  1197. /* Process Unlocked */
  1198. __HAL_UNLOCK(hcryp);
  1199. /* Return function status */
  1200. return HAL_OK;
  1201. }
  1202. /**
  1203. * @brief Initializes the CRYP peripheral in AES GCM encryption mode using IT.
  1204. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1205. * the configuration information for CRYP module
  1206. * @param pPlainData: Pointer to the plaintext buffer
  1207. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  1208. * @param pCypherData: Pointer to the cyphertext buffer
  1209. * @retval HAL status
  1210. */
  1211. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1212. {
  1213. uint32_t tickstart = 0U;
  1214. uint32_t inputaddr;
  1215. uint32_t outputaddr;
  1216. if(hcryp->State == HAL_CRYP_STATE_READY)
  1217. {
  1218. /* Process Locked */
  1219. __HAL_LOCK(hcryp);
  1220. /* Get the buffer addresses and sizes */
  1221. hcryp->CrypInCount = Size;
  1222. hcryp->pCrypInBuffPtr = pPlainData;
  1223. hcryp->pCrypOutBuffPtr = pCypherData;
  1224. hcryp->CrypOutCount = Size;
  1225. /* Change the CRYP peripheral state */
  1226. hcryp->State = HAL_CRYP_STATE_BUSY;
  1227. /* Check if initialization phase has already been performed */
  1228. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1229. {
  1230. /* Set the key */
  1231. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1232. /* Set the CRYP peripheral in AES GCM mode */
  1233. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT);
  1234. /* Set the Initialization Vector */
  1235. CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);
  1236. /* Flush FIFO */
  1237. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1238. /* Enable CRYP to start the init phase */
  1239. __HAL_CRYP_ENABLE(hcryp);
  1240. /* Get tick */
  1241. tickstart = HAL_GetTick();
  1242. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  1243. {
  1244. /* Check for the Timeout */
  1245. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1246. {
  1247. /* Change state */
  1248. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1249. /* Process Unlocked */
  1250. __HAL_UNLOCK(hcryp);
  1251. return HAL_TIMEOUT;
  1252. }
  1253. }
  1254. /* Set the header phase */
  1255. if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK)
  1256. {
  1257. return HAL_TIMEOUT;
  1258. }
  1259. /* Disable the CRYP peripheral */
  1260. __HAL_CRYP_DISABLE(hcryp);
  1261. /* Select payload phase once the header phase is performed */
  1262. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  1263. /* Flush FIFO */
  1264. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1265. /* Set the phase */
  1266. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1267. }
  1268. if(Size != 0U)
  1269. {
  1270. /* Enable Interrupts */
  1271. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1272. /* Enable the CRYP peripheral */
  1273. __HAL_CRYP_ENABLE(hcryp);
  1274. }
  1275. else
  1276. {
  1277. /* Process Locked */
  1278. __HAL_UNLOCK(hcryp);
  1279. /* Change the CRYP state and phase */
  1280. hcryp->State = HAL_CRYP_STATE_READY;
  1281. }
  1282. /* Return function status */
  1283. return HAL_OK;
  1284. }
  1285. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1286. {
  1287. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1288. /* Write the Input block in the IN FIFO */
  1289. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1290. inputaddr+=4U;
  1291. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1292. inputaddr+=4U;
  1293. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1294. inputaddr+=4U;
  1295. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1296. hcryp->pCrypInBuffPtr += 16U;
  1297. hcryp->CrypInCount -= 16U;
  1298. if(hcryp->CrypInCount == 0U)
  1299. {
  1300. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1301. /* Call the Input data transfer complete callback */
  1302. HAL_CRYP_InCpltCallback(hcryp);
  1303. }
  1304. }
  1305. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1306. {
  1307. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1308. /* Read the Output block from the Output FIFO */
  1309. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1310. outputaddr+=4U;
  1311. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1312. outputaddr+=4U;
  1313. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1314. outputaddr+=4U;
  1315. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1316. hcryp->pCrypOutBuffPtr += 16U;
  1317. hcryp->CrypOutCount -= 16U;
  1318. if(hcryp->CrypOutCount == 0U)
  1319. {
  1320. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1321. /* Process Unlocked */
  1322. __HAL_UNLOCK(hcryp);
  1323. /* Change the CRYP peripheral state */
  1324. hcryp->State = HAL_CRYP_STATE_READY;
  1325. /* Call Input transfer complete callback */
  1326. HAL_CRYP_OutCpltCallback(hcryp);
  1327. }
  1328. }
  1329. /* Return function status */
  1330. return HAL_OK;
  1331. }
  1332. /**
  1333. * @brief Initializes the CRYP peripheral in AES CCM encryption mode using interrupt.
  1334. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1335. * the configuration information for CRYP module
  1336. * @param pPlainData: Pointer to the plaintext buffer
  1337. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  1338. * @param pCypherData: Pointer to the cyphertext buffer
  1339. * @retval HAL status
  1340. */
  1341. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1342. {
  1343. uint32_t tickstart = 0U;
  1344. uint32_t inputaddr;
  1345. uint32_t outputaddr;
  1346. uint32_t headersize = hcryp->Init.HeaderSize;
  1347. uint32_t headeraddr = (uint32_t)hcryp->Init.Header;
  1348. uint32_t loopcounter = 0U;
  1349. uint32_t bufferidx = 0U;
  1350. uint8_t blockb0[16U] = {0};/* Block B0 */
  1351. uint8_t ctr[16U] = {0}; /* Counter */
  1352. uint32_t b0addr = (uint32_t)blockb0;
  1353. if(hcryp->State == HAL_CRYP_STATE_READY)
  1354. {
  1355. /* Process Locked */
  1356. __HAL_LOCK(hcryp);
  1357. hcryp->CrypInCount = Size;
  1358. hcryp->pCrypInBuffPtr = pPlainData;
  1359. hcryp->pCrypOutBuffPtr = pCypherData;
  1360. hcryp->CrypOutCount = Size;
  1361. /* Change the CRYP peripheral state */
  1362. hcryp->State = HAL_CRYP_STATE_BUSY;
  1363. /* Check if initialization phase has already been performed */
  1364. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1365. {
  1366. /************************ Formatting the header block *******************/
  1367. if(headersize != 0U)
  1368. {
  1369. /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
  1370. if(headersize < 65280U)
  1371. {
  1372. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
  1373. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
  1374. headersize += 2U;
  1375. }
  1376. else
  1377. {
  1378. /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
  1379. hcryp->Init.pScratch[bufferidx++] = 0xFFU;
  1380. hcryp->Init.pScratch[bufferidx++] = 0xFEU;
  1381. hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U;
  1382. hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U;
  1383. hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U;
  1384. hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU;
  1385. headersize += 6U;
  1386. }
  1387. /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */
  1388. for(loopcounter = 0U; loopcounter < headersize; loopcounter++)
  1389. {
  1390. hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];
  1391. }
  1392. /* Check if the header size is modulo 16 */
  1393. if ((headersize % 16U) != 0U)
  1394. {
  1395. /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */
  1396. for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++)
  1397. {
  1398. hcryp->Init.pScratch[loopcounter] = 0U;
  1399. }
  1400. /* Set the header size to modulo 16 */
  1401. headersize = ((headersize/16U) + 1U) * 16U;
  1402. }
  1403. /* Set the pointer headeraddr to hcryp->Init.pScratch */
  1404. headeraddr = (uint32_t)hcryp->Init.pScratch;
  1405. }
  1406. /*********************** Formatting the block B0 ************************/
  1407. if(headersize != 0U)
  1408. {
  1409. blockb0[0U] = 0x40U;
  1410. }
  1411. /* Flags byte */
  1412. /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */
  1413. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1U) & (uint8_t)0x07) << 3U);
  1414. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);
  1415. for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++)
  1416. {
  1417. blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter];
  1418. }
  1419. for ( ; loopcounter < 13U; loopcounter++)
  1420. {
  1421. blockb0[loopcounter+1U] = 0U;
  1422. }
  1423. blockb0[14U] = (Size >> 8U);
  1424. blockb0[15U] = (Size & 0xFFU);
  1425. /************************* Formatting the initial counter ***************/
  1426. /* Byte 0:
  1427. Bits 7 and 6 are reserved and shall be set to 0
  1428. Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter
  1429. blocks are distinct from B0
  1430. Bits 0, 1, and 2 contain the same encoding of q as in B0
  1431. */
  1432. ctr[0U] = blockb0[0U] & 0x07U;
  1433. /* byte 1 to NonceSize is the IV (Nonce) */
  1434. for(loopcounter = 1; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++)
  1435. {
  1436. ctr[loopcounter] = blockb0[loopcounter];
  1437. }
  1438. /* Set the LSB to 1 */
  1439. ctr[15U] |= 0x01U;
  1440. /* Set the key */
  1441. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1442. /* Set the CRYP peripheral in AES CCM mode */
  1443. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT);
  1444. /* Set the Initialization Vector */
  1445. CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);
  1446. /* Select init phase */
  1447. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  1448. b0addr = (uint32_t)blockb0;
  1449. /* Write the blockb0 block in the IN FIFO */
  1450. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1451. b0addr+=4U;
  1452. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1453. b0addr+=4U;
  1454. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1455. b0addr+=4U;
  1456. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1457. /* Enable the CRYP peripheral */
  1458. __HAL_CRYP_ENABLE(hcryp);
  1459. /* Get tick */
  1460. tickstart = HAL_GetTick();
  1461. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  1462. {
  1463. /* Check for the Timeout */
  1464. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1465. {
  1466. /* Change state */
  1467. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1468. /* Process Unlocked */
  1469. __HAL_UNLOCK(hcryp);
  1470. return HAL_TIMEOUT;
  1471. }
  1472. }
  1473. /***************************** Header phase *****************************/
  1474. if(headersize != 0U)
  1475. {
  1476. /* Select header phase */
  1477. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  1478. /* Enable Crypto processor */
  1479. __HAL_CRYP_ENABLE(hcryp);
  1480. for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U)
  1481. {
  1482. /* Get tick */
  1483. tickstart = HAL_GetTick();
  1484. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  1485. {
  1486. /* Check for the Timeout */
  1487. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1488. {
  1489. /* Change state */
  1490. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1491. /* Process Unlocked */
  1492. __HAL_UNLOCK(hcryp);
  1493. return HAL_TIMEOUT;
  1494. }
  1495. }
  1496. /* Write the header block in the IN FIFO */
  1497. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1498. headeraddr+=4U;
  1499. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1500. headeraddr+=4U;
  1501. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1502. headeraddr+=4U;
  1503. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1504. headeraddr+=4U;
  1505. }
  1506. /* Get tick */
  1507. tickstart = HAL_GetTick();
  1508. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  1509. {
  1510. /* Check for the Timeout */
  1511. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1512. {
  1513. /* Change state */
  1514. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1515. /* Process Unlocked */
  1516. __HAL_UNLOCK(hcryp);
  1517. return HAL_TIMEOUT;
  1518. }
  1519. }
  1520. }
  1521. /* Save formatted counter into the scratch buffer pScratch */
  1522. for(loopcounter = 0U; (loopcounter < 16U); loopcounter++)
  1523. {
  1524. hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];
  1525. }
  1526. /* Reset bit 0 */
  1527. hcryp->Init.pScratch[15U] &= 0xFEU;
  1528. /* Select payload phase once the header phase is performed */
  1529. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  1530. /* Flush FIFO */
  1531. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1532. /* Set the phase */
  1533. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1534. }
  1535. if(Size != 0U)
  1536. {
  1537. /* Enable Interrupts */
  1538. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1539. /* Enable the CRYP peripheral */
  1540. __HAL_CRYP_ENABLE(hcryp);
  1541. }
  1542. else
  1543. {
  1544. /* Change the CRYP state and phase */
  1545. hcryp->State = HAL_CRYP_STATE_READY;
  1546. }
  1547. /* Return function status */
  1548. return HAL_OK;
  1549. }
  1550. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1551. {
  1552. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1553. /* Write the Input block in the IN FIFO */
  1554. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1555. inputaddr+=4U;
  1556. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1557. inputaddr+=4U;
  1558. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1559. inputaddr+=4U;
  1560. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1561. hcryp->pCrypInBuffPtr += 16U;
  1562. hcryp->CrypInCount -= 16U;
  1563. if(hcryp->CrypInCount == 0U)
  1564. {
  1565. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1566. /* Call Input transfer complete callback */
  1567. HAL_CRYP_InCpltCallback(hcryp);
  1568. }
  1569. }
  1570. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1571. {
  1572. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1573. /* Read the Output block from the Output FIFO */
  1574. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1575. outputaddr+=4U;
  1576. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1577. outputaddr+=4U;
  1578. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1579. outputaddr+=4U;
  1580. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1581. hcryp->pCrypOutBuffPtr += 16U;
  1582. hcryp->CrypOutCount -= 16U;
  1583. if(hcryp->CrypOutCount == 0U)
  1584. {
  1585. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1586. /* Process Unlocked */
  1587. __HAL_UNLOCK(hcryp);
  1588. /* Change the CRYP peripheral state */
  1589. hcryp->State = HAL_CRYP_STATE_READY;
  1590. /* Call Input transfer complete callback */
  1591. HAL_CRYP_OutCpltCallback(hcryp);
  1592. }
  1593. }
  1594. /* Return function status */
  1595. return HAL_OK;
  1596. }
  1597. /**
  1598. * @brief Initializes the CRYP peripheral in AES GCM decryption mode using IT.
  1599. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1600. * the configuration information for CRYP module
  1601. * @param pCypherData: Pointer to the cyphertext buffer
  1602. * @param Size: Length of the cyphertext buffer, must be a multiple of 16
  1603. * @param pPlainData: Pointer to the plaintext buffer
  1604. * @retval HAL status
  1605. */
  1606. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1607. {
  1608. uint32_t tickstart = 0U;
  1609. uint32_t inputaddr;
  1610. uint32_t outputaddr;
  1611. if(hcryp->State == HAL_CRYP_STATE_READY)
  1612. {
  1613. /* Process Locked */
  1614. __HAL_LOCK(hcryp);
  1615. /* Get the buffer addresses and sizes */
  1616. hcryp->CrypInCount = Size;
  1617. hcryp->pCrypInBuffPtr = pCypherData;
  1618. hcryp->pCrypOutBuffPtr = pPlainData;
  1619. hcryp->CrypOutCount = Size;
  1620. /* Change the CRYP peripheral state */
  1621. hcryp->State = HAL_CRYP_STATE_BUSY;
  1622. /* Check if initialization phase has already been performed */
  1623. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1624. {
  1625. /* Set the key */
  1626. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1627. /* Set the CRYP peripheral in AES GCM decryption mode */
  1628. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT);
  1629. /* Set the Initialization Vector */
  1630. CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);
  1631. /* Flush FIFO */
  1632. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1633. /* Enable CRYP to start the init phase */
  1634. __HAL_CRYP_ENABLE(hcryp);
  1635. /* Get tick */
  1636. tickstart = HAL_GetTick();
  1637. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  1638. {
  1639. /* Check for the Timeout */
  1640. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1641. {
  1642. /* Change state */
  1643. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1644. /* Process Unlocked */
  1645. __HAL_UNLOCK(hcryp);
  1646. return HAL_TIMEOUT;
  1647. }
  1648. }
  1649. /* Set the header phase */
  1650. if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK)
  1651. {
  1652. return HAL_TIMEOUT;
  1653. }
  1654. /* Disable the CRYP peripheral */
  1655. __HAL_CRYP_DISABLE(hcryp);
  1656. /* Select payload phase once the header phase is performed */
  1657. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  1658. /* Set the phase */
  1659. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1660. }
  1661. if(Size != 0U)
  1662. {
  1663. /* Enable Interrupts */
  1664. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1665. /* Enable the CRYP peripheral */
  1666. __HAL_CRYP_ENABLE(hcryp);
  1667. }
  1668. else
  1669. {
  1670. /* Process Locked */
  1671. __HAL_UNLOCK(hcryp);
  1672. /* Change the CRYP state and phase */
  1673. hcryp->State = HAL_CRYP_STATE_READY;
  1674. }
  1675. /* Return function status */
  1676. return HAL_OK;
  1677. }
  1678. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1679. {
  1680. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1681. /* Write the Input block in the IN FIFO */
  1682. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1683. inputaddr+=4U;
  1684. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1685. inputaddr+=4U;
  1686. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1687. inputaddr+=4U;
  1688. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1689. hcryp->pCrypInBuffPtr += 16U;
  1690. hcryp->CrypInCount -= 16U;
  1691. if(hcryp->CrypInCount == 0U)
  1692. {
  1693. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1694. /* Call the Input data transfer complete callback */
  1695. HAL_CRYP_InCpltCallback(hcryp);
  1696. }
  1697. }
  1698. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1699. {
  1700. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1701. /* Read the Output block from the Output FIFO */
  1702. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1703. outputaddr+=4U;
  1704. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1705. outputaddr+=4U;
  1706. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1707. outputaddr+=4U;
  1708. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1709. hcryp->pCrypOutBuffPtr += 16U;
  1710. hcryp->CrypOutCount -= 16U;
  1711. if(hcryp->CrypOutCount == 0U)
  1712. {
  1713. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1714. /* Process Unlocked */
  1715. __HAL_UNLOCK(hcryp);
  1716. /* Change the CRYP peripheral state */
  1717. hcryp->State = HAL_CRYP_STATE_READY;
  1718. /* Call Input transfer complete callback */
  1719. HAL_CRYP_OutCpltCallback(hcryp);
  1720. }
  1721. }
  1722. /* Return function status */
  1723. return HAL_OK;
  1724. }
  1725. /**
  1726. * @brief Initializes the CRYP peripheral in AES CCM decryption mode using interrupt
  1727. * then decrypted pCypherData. The cypher data are available in pPlainData.
  1728. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1729. * the configuration information for CRYP module
  1730. * @param pCypherData: Pointer to the cyphertext buffer
  1731. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  1732. * @param pPlainData: Pointer to the plaintext buffer
  1733. * @retval HAL status
  1734. */
  1735. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  1736. {
  1737. uint32_t inputaddr;
  1738. uint32_t outputaddr;
  1739. uint32_t tickstart = 0U;
  1740. uint32_t headersize = hcryp->Init.HeaderSize;
  1741. uint32_t headeraddr = (uint32_t)hcryp->Init.Header;
  1742. uint32_t loopcounter = 0U;
  1743. uint32_t bufferidx = 0U;
  1744. uint8_t blockb0[16U] = {0};/* Block B0 */
  1745. uint8_t ctr[16U] = {0}; /* Counter */
  1746. uint32_t b0addr = (uint32_t)blockb0;
  1747. if(hcryp->State == HAL_CRYP_STATE_READY)
  1748. {
  1749. /* Process Locked */
  1750. __HAL_LOCK(hcryp);
  1751. hcryp->CrypInCount = Size;
  1752. hcryp->pCrypInBuffPtr = pCypherData;
  1753. hcryp->pCrypOutBuffPtr = pPlainData;
  1754. hcryp->CrypOutCount = Size;
  1755. /* Change the CRYP peripheral state */
  1756. hcryp->State = HAL_CRYP_STATE_BUSY;
  1757. /* Check if initialization phase has already been performed */
  1758. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  1759. {
  1760. /************************ Formatting the header block *******************/
  1761. if(headersize != 0U)
  1762. {
  1763. /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
  1764. if(headersize < 65280U)
  1765. {
  1766. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
  1767. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
  1768. headersize += 2U;
  1769. }
  1770. else
  1771. {
  1772. /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
  1773. hcryp->Init.pScratch[bufferidx++] = 0xFFU;
  1774. hcryp->Init.pScratch[bufferidx++] = 0xFEU;
  1775. hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U;
  1776. hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U;
  1777. hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U;
  1778. hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU;
  1779. headersize += 6U;
  1780. }
  1781. /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */
  1782. for(loopcounter = 0U; loopcounter < headersize; loopcounter++)
  1783. {
  1784. hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];
  1785. }
  1786. /* Check if the header size is modulo 16 */
  1787. if ((headersize % 16U) != 0U)
  1788. {
  1789. /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */
  1790. for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++)
  1791. {
  1792. hcryp->Init.pScratch[loopcounter] = 0U;
  1793. }
  1794. /* Set the header size to modulo 16 */
  1795. headersize = ((headersize/16U) + 1U) * 16U;
  1796. }
  1797. /* Set the pointer headeraddr to hcryp->Init.pScratch */
  1798. headeraddr = (uint32_t)hcryp->Init.pScratch;
  1799. }
  1800. /*********************** Formatting the block B0 ************************/
  1801. if(headersize != 0U)
  1802. {
  1803. blockb0[0U] = 0x40U;
  1804. }
  1805. /* Flags byte */
  1806. /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */
  1807. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1U) & (uint8_t)0x07) << 3U);
  1808. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);
  1809. for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++)
  1810. {
  1811. blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter];
  1812. }
  1813. for ( ; loopcounter < 13U; loopcounter++)
  1814. {
  1815. blockb0[loopcounter+1U] = 0U;
  1816. }
  1817. blockb0[14U] = (Size >> 8U);
  1818. blockb0[15U] = (Size & 0xFFU);
  1819. /************************* Formatting the initial counter ***************/
  1820. /* Byte 0:
  1821. Bits 7 and 6 are reserved and shall be set to 0
  1822. Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter
  1823. blocks are distinct from B0
  1824. Bits 0, 1, and 2 contain the same encoding of q as in B0
  1825. */
  1826. ctr[0U] = blockb0[0U] & 0x07U;
  1827. /* byte 1 to NonceSize is the IV (Nonce) */
  1828. for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++)
  1829. {
  1830. ctr[loopcounter] = blockb0[loopcounter];
  1831. }
  1832. /* Set the LSB to 1 */
  1833. ctr[15U] |= 0x01U;
  1834. /* Set the key */
  1835. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  1836. /* Set the CRYP peripheral in AES CCM mode */
  1837. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT);
  1838. /* Set the Initialization Vector */
  1839. CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);
  1840. /* Select init phase */
  1841. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  1842. b0addr = (uint32_t)blockb0;
  1843. /* Write the blockb0 block in the IN FIFO */
  1844. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1845. b0addr+=4U;
  1846. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1847. b0addr+=4U;
  1848. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1849. b0addr+=4U;
  1850. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  1851. /* Enable the CRYP peripheral */
  1852. __HAL_CRYP_ENABLE(hcryp);
  1853. /* Get tick */
  1854. tickstart = HAL_GetTick();
  1855. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  1856. {
  1857. /* Check for the Timeout */
  1858. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1859. {
  1860. /* Change state */
  1861. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1862. /* Process Unlocked */
  1863. __HAL_UNLOCK(hcryp);
  1864. return HAL_TIMEOUT;
  1865. }
  1866. }
  1867. /***************************** Header phase *****************************/
  1868. if(headersize != 0U)
  1869. {
  1870. /* Select header phase */
  1871. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  1872. /* Enable Crypto processor */
  1873. __HAL_CRYP_ENABLE(hcryp);
  1874. for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U)
  1875. {
  1876. /* Get tick */
  1877. tickstart = HAL_GetTick();
  1878. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  1879. {
  1880. /* Check for the Timeout */
  1881. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1882. {
  1883. /* Change state */
  1884. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1885. /* Process Unlocked */
  1886. __HAL_UNLOCK(hcryp);
  1887. return HAL_TIMEOUT;
  1888. }
  1889. }
  1890. /* Write the header block in the IN FIFO */
  1891. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1892. headeraddr+=4U;
  1893. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1894. headeraddr+=4U;
  1895. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1896. headeraddr+=4U;
  1897. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  1898. headeraddr+=4U;
  1899. }
  1900. /* Get tick */
  1901. tickstart = HAL_GetTick();
  1902. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  1903. {
  1904. /* Check for the Timeout */
  1905. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  1906. {
  1907. /* Change state */
  1908. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  1909. /* Process Unlocked */
  1910. __HAL_UNLOCK(hcryp);
  1911. return HAL_TIMEOUT;
  1912. }
  1913. }
  1914. }
  1915. /* Save formatted counter into the scratch buffer pScratch */
  1916. for(loopcounter = 0U; (loopcounter < 16U); loopcounter++)
  1917. {
  1918. hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];
  1919. }
  1920. /* Reset bit 0 */
  1921. hcryp->Init.pScratch[15U] &= 0xFEU;
  1922. /* Select payload phase once the header phase is performed */
  1923. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  1924. /* Flush FIFO */
  1925. __HAL_CRYP_FIFO_FLUSH(hcryp);
  1926. /* Set the phase */
  1927. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  1928. }
  1929. /* Enable Interrupts */
  1930. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_INI | CRYP_IT_OUTI);
  1931. /* Enable the CRYP peripheral */
  1932. __HAL_CRYP_ENABLE(hcryp);
  1933. /* Return function status */
  1934. return HAL_OK;
  1935. }
  1936. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_INI))
  1937. {
  1938. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1939. /* Write the Input block in the IN FIFO */
  1940. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1941. inputaddr+=4U;
  1942. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1943. inputaddr+=4U;
  1944. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1945. inputaddr+=4U;
  1946. hcryp->Instance->DR = *(uint32_t*)(inputaddr);
  1947. hcryp->pCrypInBuffPtr += 16U;
  1948. hcryp->CrypInCount -= 16U;
  1949. if(hcryp->CrypInCount == 0U)
  1950. {
  1951. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_INI);
  1952. /* Call the Input data transfer complete callback */
  1953. HAL_CRYP_InCpltCallback(hcryp);
  1954. }
  1955. }
  1956. else if (__HAL_CRYP_GET_IT(hcryp, CRYP_IT_OUTI))
  1957. {
  1958. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1959. /* Read the Output block from the Output FIFO */
  1960. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1961. outputaddr+=4U;
  1962. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1963. outputaddr+=4U;
  1964. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1965. outputaddr+=4U;
  1966. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUT;
  1967. hcryp->pCrypOutBuffPtr += 16U;
  1968. hcryp->CrypOutCount -= 16U;
  1969. if(hcryp->CrypOutCount == 0U)
  1970. {
  1971. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_OUTI);
  1972. /* Process Unlocked */
  1973. __HAL_UNLOCK(hcryp);
  1974. /* Change the CRYP peripheral state */
  1975. hcryp->State = HAL_CRYP_STATE_READY;
  1976. /* Call Input transfer complete callback */
  1977. HAL_CRYP_OutCpltCallback(hcryp);
  1978. }
  1979. }
  1980. /* Return function status */
  1981. return HAL_OK;
  1982. }
  1983. /**
  1984. * @brief Initializes the CRYP peripheral in AES GCM encryption mode using DMA.
  1985. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1986. * the configuration information for CRYP module
  1987. * @param pPlainData: Pointer to the plaintext buffer
  1988. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  1989. * @param pCypherData: Pointer to the cyphertext buffer
  1990. * @retval HAL status
  1991. */
  1992. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  1993. {
  1994. uint32_t tickstart = 0U;
  1995. uint32_t inputaddr;
  1996. uint32_t outputaddr;
  1997. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  1998. {
  1999. /* Process Locked */
  2000. __HAL_LOCK(hcryp);
  2001. inputaddr = (uint32_t)pPlainData;
  2002. outputaddr = (uint32_t)pCypherData;
  2003. /* Change the CRYP peripheral state */
  2004. hcryp->State = HAL_CRYP_STATE_BUSY;
  2005. /* Check if initialization phase has already been performed */
  2006. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  2007. {
  2008. /* Set the key */
  2009. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  2010. /* Set the CRYP peripheral in AES GCM mode */
  2011. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT);
  2012. /* Set the Initialization Vector */
  2013. CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);
  2014. /* Flush FIFO */
  2015. __HAL_CRYP_FIFO_FLUSH(hcryp);
  2016. /* Enable CRYP to start the init phase */
  2017. __HAL_CRYP_ENABLE(hcryp);
  2018. /* Get tick */
  2019. tickstart = HAL_GetTick();
  2020. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  2021. {
  2022. /* Check for the Timeout */
  2023. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2024. {
  2025. /* Change state */
  2026. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2027. /* Process Unlocked */
  2028. __HAL_UNLOCK(hcryp);
  2029. return HAL_TIMEOUT;
  2030. }
  2031. }
  2032. /* Flush FIFO */
  2033. __HAL_CRYP_FIFO_FLUSH(hcryp);
  2034. /* Set the header phase */
  2035. if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK)
  2036. {
  2037. return HAL_TIMEOUT;
  2038. }
  2039. /* Disable the CRYP peripheral */
  2040. __HAL_CRYP_DISABLE(hcryp);
  2041. /* Select payload phase once the header phase is performed */
  2042. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2043. /* Flush FIFO */
  2044. __HAL_CRYP_FIFO_FLUSH(hcryp);
  2045. /* Set the phase */
  2046. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  2047. }
  2048. /* Set the input and output addresses and start DMA transfer */
  2049. CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2050. /* Unlock process */
  2051. __HAL_UNLOCK(hcryp);
  2052. /* Return function status */
  2053. return HAL_OK;
  2054. }
  2055. else
  2056. {
  2057. return HAL_ERROR;
  2058. }
  2059. }
  2060. /**
  2061. * @brief Initializes the CRYP peripheral in AES CCM encryption mode using interrupt.
  2062. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2063. * the configuration information for CRYP module
  2064. * @param pPlainData: Pointer to the plaintext buffer
  2065. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  2066. * @param pCypherData: Pointer to the cyphertext buffer
  2067. * @retval HAL status
  2068. */
  2069. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  2070. {
  2071. uint32_t tickstart = 0U;
  2072. uint32_t inputaddr;
  2073. uint32_t outputaddr;
  2074. uint32_t headersize;
  2075. uint32_t headeraddr;
  2076. uint32_t loopcounter = 0U;
  2077. uint32_t bufferidx = 0U;
  2078. uint8_t blockb0[16U] = {0};/* Block B0 */
  2079. uint8_t ctr[16U] = {0}; /* Counter */
  2080. uint32_t b0addr = (uint32_t)blockb0;
  2081. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2082. {
  2083. /* Process Locked */
  2084. __HAL_LOCK(hcryp);
  2085. inputaddr = (uint32_t)pPlainData;
  2086. outputaddr = (uint32_t)pCypherData;
  2087. headersize = hcryp->Init.HeaderSize;
  2088. headeraddr = (uint32_t)hcryp->Init.Header;
  2089. hcryp->CrypInCount = Size;
  2090. hcryp->pCrypInBuffPtr = pPlainData;
  2091. hcryp->pCrypOutBuffPtr = pCypherData;
  2092. hcryp->CrypOutCount = Size;
  2093. /* Change the CRYP peripheral state */
  2094. hcryp->State = HAL_CRYP_STATE_BUSY;
  2095. /* Check if initialization phase has already been performed */
  2096. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  2097. {
  2098. /************************ Formatting the header block *******************/
  2099. if(headersize != 0U)
  2100. {
  2101. /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
  2102. if(headersize < 65280U)
  2103. {
  2104. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
  2105. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
  2106. headersize += 2U;
  2107. }
  2108. else
  2109. {
  2110. /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
  2111. hcryp->Init.pScratch[bufferidx++] = 0xFFU;
  2112. hcryp->Init.pScratch[bufferidx++] = 0xFEU;
  2113. hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U;
  2114. hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U;
  2115. hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U;
  2116. hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU;
  2117. headersize += 6U;
  2118. }
  2119. /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */
  2120. for(loopcounter = 0U; loopcounter < headersize; loopcounter++)
  2121. {
  2122. hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];
  2123. }
  2124. /* Check if the header size is modulo 16 */
  2125. if ((headersize % 16U) != 0U)
  2126. {
  2127. /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */
  2128. for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++)
  2129. {
  2130. hcryp->Init.pScratch[loopcounter] = 0U;
  2131. }
  2132. /* Set the header size to modulo 16 */
  2133. headersize = ((headersize/16U) + 1U) * 16U;
  2134. }
  2135. /* Set the pointer headeraddr to hcryp->Init.pScratch */
  2136. headeraddr = (uint32_t)hcryp->Init.pScratch;
  2137. }
  2138. /*********************** Formatting the block B0 ************************/
  2139. if(headersize != 0U)
  2140. {
  2141. blockb0[0U] = 0x40U;
  2142. }
  2143. /* Flags byte */
  2144. /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */
  2145. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07) << 3);
  2146. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);
  2147. for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++)
  2148. {
  2149. blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter];
  2150. }
  2151. for ( ; loopcounter < 13U; loopcounter++)
  2152. {
  2153. blockb0[loopcounter+1U] = 0U;
  2154. }
  2155. blockb0[14U] = (Size >> 8U);
  2156. blockb0[15U] = (Size & 0xFFU);
  2157. /************************* Formatting the initial counter ***************/
  2158. /* Byte 0:
  2159. Bits 7 and 6 are reserved and shall be set to 0
  2160. Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter
  2161. blocks are distinct from B0
  2162. Bits 0, 1, and 2 contain the same encoding of q as in B0
  2163. */
  2164. ctr[0U] = blockb0[0U] & 0x07U;
  2165. /* byte 1 to NonceSize is the IV (Nonce) */
  2166. for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++)
  2167. {
  2168. ctr[loopcounter] = blockb0[loopcounter];
  2169. }
  2170. /* Set the LSB to 1 */
  2171. ctr[15U] |= 0x01U;
  2172. /* Set the key */
  2173. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  2174. /* Set the CRYP peripheral in AES CCM mode */
  2175. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT);
  2176. /* Set the Initialization Vector */
  2177. CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);
  2178. /* Select init phase */
  2179. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  2180. b0addr = (uint32_t)blockb0;
  2181. /* Write the blockb0 block in the IN FIFO */
  2182. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2183. b0addr+=4U;
  2184. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2185. b0addr+=4U;
  2186. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2187. b0addr+=4U;
  2188. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2189. /* Enable the CRYP peripheral */
  2190. __HAL_CRYP_ENABLE(hcryp);
  2191. /* Get tick */
  2192. tickstart = HAL_GetTick();
  2193. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  2194. {
  2195. /* Check for the Timeout */
  2196. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2197. {
  2198. /* Change state */
  2199. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2200. /* Process Unlocked */
  2201. __HAL_UNLOCK(hcryp);
  2202. return HAL_TIMEOUT;
  2203. }
  2204. }
  2205. /***************************** Header phase *****************************/
  2206. if(headersize != 0U)
  2207. {
  2208. /* Select header phase */
  2209. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  2210. /* Enable Crypto processor */
  2211. __HAL_CRYP_ENABLE(hcryp);
  2212. for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U)
  2213. {
  2214. /* Get tick */
  2215. tickstart = HAL_GetTick();
  2216. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  2217. {
  2218. /* Check for the Timeout */
  2219. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2220. {
  2221. /* Change state */
  2222. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2223. /* Process Unlocked */
  2224. __HAL_UNLOCK(hcryp);
  2225. return HAL_TIMEOUT;
  2226. }
  2227. }
  2228. /* Write the header block in the IN FIFO */
  2229. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2230. headeraddr+=4U;
  2231. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2232. headeraddr+=4U;
  2233. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2234. headeraddr+=4U;
  2235. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2236. headeraddr+=4U;
  2237. }
  2238. /* Get tick */
  2239. tickstart = HAL_GetTick();
  2240. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  2241. {
  2242. /* Check for the Timeout */
  2243. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2244. {
  2245. /* Change state */
  2246. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2247. /* Process Unlocked */
  2248. __HAL_UNLOCK(hcryp);
  2249. return HAL_TIMEOUT;
  2250. }
  2251. }
  2252. }
  2253. /* Save formatted counter into the scratch buffer pScratch */
  2254. for(loopcounter = 0U; (loopcounter < 16U); loopcounter++)
  2255. {
  2256. hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];
  2257. }
  2258. /* Reset bit 0 */
  2259. hcryp->Init.pScratch[15U] &= 0xFEU;
  2260. /* Select payload phase once the header phase is performed */
  2261. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2262. /* Flush FIFO */
  2263. __HAL_CRYP_FIFO_FLUSH(hcryp);
  2264. /* Set the phase */
  2265. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  2266. }
  2267. /* Set the input and output addresses and start DMA transfer */
  2268. CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2269. /* Unlock process */
  2270. __HAL_UNLOCK(hcryp);
  2271. /* Return function status */
  2272. return HAL_OK;
  2273. }
  2274. else
  2275. {
  2276. return HAL_ERROR;
  2277. }
  2278. }
  2279. /**
  2280. * @brief Initializes the CRYP peripheral in AES GCM decryption mode using DMA.
  2281. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2282. * the configuration information for CRYP module
  2283. * @param pCypherData: Pointer to the cyphertext buffer.
  2284. * @param Size: Length of the cyphertext buffer, must be a multiple of 16
  2285. * @param pPlainData: Pointer to the plaintext buffer
  2286. * @retval HAL status
  2287. */
  2288. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2289. {
  2290. uint32_t tickstart = 0U;
  2291. uint32_t inputaddr;
  2292. uint32_t outputaddr;
  2293. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2294. {
  2295. /* Process Locked */
  2296. __HAL_LOCK(hcryp);
  2297. inputaddr = (uint32_t)pCypherData;
  2298. outputaddr = (uint32_t)pPlainData;
  2299. /* Change the CRYP peripheral state */
  2300. hcryp->State = HAL_CRYP_STATE_BUSY;
  2301. /* Check if initialization phase has already been performed */
  2302. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  2303. {
  2304. /* Set the key */
  2305. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  2306. /* Set the CRYP peripheral in AES GCM decryption mode */
  2307. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_GCM_DECRYPT);
  2308. /* Set the Initialization Vector */
  2309. CRYPEx_GCMCCM_SetInitVector(hcryp, hcryp->Init.pInitVect);
  2310. /* Enable CRYP to start the init phase */
  2311. __HAL_CRYP_ENABLE(hcryp);
  2312. /* Get tick */
  2313. tickstart = HAL_GetTick();
  2314. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  2315. {
  2316. /* Check for the Timeout */
  2317. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2318. {
  2319. /* Change state */
  2320. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2321. /* Process Unlocked */
  2322. __HAL_UNLOCK(hcryp);
  2323. return HAL_TIMEOUT;
  2324. }
  2325. }
  2326. /* Set the header phase */
  2327. if(CRYPEx_GCMCCM_SetHeaderPhase(hcryp, hcryp->Init.Header, hcryp->Init.HeaderSize, 1U) != HAL_OK)
  2328. {
  2329. return HAL_TIMEOUT;
  2330. }
  2331. /* Disable the CRYP peripheral */
  2332. __HAL_CRYP_DISABLE(hcryp);
  2333. /* Select payload phase once the header phase is performed */
  2334. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2335. /* Set the phase */
  2336. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  2337. }
  2338. /* Set the input and output addresses and start DMA transfer */
  2339. CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2340. /* Unlock process */
  2341. __HAL_UNLOCK(hcryp);
  2342. /* Return function status */
  2343. return HAL_OK;
  2344. }
  2345. else
  2346. {
  2347. return HAL_ERROR;
  2348. }
  2349. }
  2350. /**
  2351. * @brief Initializes the CRYP peripheral in AES CCM decryption mode using DMA
  2352. * then decrypted pCypherData. The cypher data are available in pPlainData.
  2353. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2354. * the configuration information for CRYP module
  2355. * @param pCypherData: Pointer to the cyphertext buffer
  2356. * @param Size: Length of the plaintext buffer, must be a multiple of 16
  2357. * @param pPlainData: Pointer to the plaintext buffer
  2358. * @retval HAL status
  2359. */
  2360. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  2361. {
  2362. uint32_t tickstart = 0U;
  2363. uint32_t inputaddr;
  2364. uint32_t outputaddr;
  2365. uint32_t headersize;
  2366. uint32_t headeraddr;
  2367. uint32_t loopcounter = 0U;
  2368. uint32_t bufferidx = 0U;
  2369. uint8_t blockb0[16U] = {0};/* Block B0 */
  2370. uint8_t ctr[16U] = {0}; /* Counter */
  2371. uint32_t b0addr = (uint32_t)blockb0;
  2372. if((hcryp->State == HAL_CRYP_STATE_READY) || (hcryp->Phase == HAL_CRYP_PHASE_PROCESS))
  2373. {
  2374. /* Process Locked */
  2375. __HAL_LOCK(hcryp);
  2376. inputaddr = (uint32_t)pCypherData;
  2377. outputaddr = (uint32_t)pPlainData;
  2378. headersize = hcryp->Init.HeaderSize;
  2379. headeraddr = (uint32_t)hcryp->Init.Header;
  2380. hcryp->CrypInCount = Size;
  2381. hcryp->pCrypInBuffPtr = pCypherData;
  2382. hcryp->pCrypOutBuffPtr = pPlainData;
  2383. hcryp->CrypOutCount = Size;
  2384. /* Change the CRYP peripheral state */
  2385. hcryp->State = HAL_CRYP_STATE_BUSY;
  2386. /* Check if initialization phase has already been performed */
  2387. if(hcryp->Phase == HAL_CRYP_PHASE_READY)
  2388. {
  2389. /************************ Formatting the header block *******************/
  2390. if(headersize != 0U)
  2391. {
  2392. /* Check that the associated data (or header) length is lower than 2^16 - 2^8 = 65536 - 256 = 65280 */
  2393. if(headersize < 65280U)
  2394. {
  2395. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize >> 8) & 0xFF);
  2396. hcryp->Init.pScratch[bufferidx++] = (uint8_t) ((headersize) & 0xFF);
  2397. headersize += 2U;
  2398. }
  2399. else
  2400. {
  2401. /* Header is encoded as 0xff || 0xfe || [headersize]32, i.e., six octets */
  2402. hcryp->Init.pScratch[bufferidx++] = 0xFFU;
  2403. hcryp->Init.pScratch[bufferidx++] = 0xFEU;
  2404. hcryp->Init.pScratch[bufferidx++] = headersize & 0xff000000U;
  2405. hcryp->Init.pScratch[bufferidx++] = headersize & 0x00ff0000U;
  2406. hcryp->Init.pScratch[bufferidx++] = headersize & 0x0000ff00U;
  2407. hcryp->Init.pScratch[bufferidx++] = headersize & 0x000000ffU;
  2408. headersize += 6U;
  2409. }
  2410. /* Copy the header buffer in internal buffer "hcryp->Init.pScratch" */
  2411. for(loopcounter = 0U; loopcounter < headersize; loopcounter++)
  2412. {
  2413. hcryp->Init.pScratch[bufferidx++] = hcryp->Init.Header[loopcounter];
  2414. }
  2415. /* Check if the header size is modulo 16 */
  2416. if ((headersize % 16U) != 0U)
  2417. {
  2418. /* Padd the header buffer with 0s till the hcryp->Init.pScratch length is modulo 16 */
  2419. for(loopcounter = headersize; loopcounter <= ((headersize/16U) + 1U) * 16U; loopcounter++)
  2420. {
  2421. hcryp->Init.pScratch[loopcounter] = 0U;
  2422. }
  2423. /* Set the header size to modulo 16 */
  2424. headersize = ((headersize/16U) + 1U) * 16U;
  2425. }
  2426. /* Set the pointer headeraddr to hcryp->Init.pScratch */
  2427. headeraddr = (uint32_t)hcryp->Init.pScratch;
  2428. }
  2429. /*********************** Formatting the block B0 ************************/
  2430. if(headersize != 0U)
  2431. {
  2432. blockb0[0U] = 0x40U;
  2433. }
  2434. /* Flags byte */
  2435. /* blockb0[0] |= 0u | (((( (uint8_t) hcryp->Init.TagSize - 2) / 2) & 0x07 ) << 3 ) | ( ( (uint8_t) (15 - hcryp->Init.IVSize) - 1) & 0x07U) */
  2436. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)(((uint8_t)(hcryp->Init.TagSize - (uint8_t)(2))) >> 1) & (uint8_t)0x07) << 3);
  2437. blockb0[0U] |= (uint8_t)((uint8_t)((uint8_t)((uint8_t)(15) - hcryp->Init.IVSize) - (uint8_t)1) & (uint8_t)0x07);
  2438. for (loopcounter = 0U; loopcounter < hcryp->Init.IVSize; loopcounter++)
  2439. {
  2440. blockb0[loopcounter+1U] = hcryp->Init.pInitVect[loopcounter];
  2441. }
  2442. for ( ; loopcounter < 13U; loopcounter++)
  2443. {
  2444. blockb0[loopcounter+1U] = 0U;
  2445. }
  2446. blockb0[14U] = (Size >> 8U);
  2447. blockb0[15U] = (Size & 0xFFU);
  2448. /************************* Formatting the initial counter ***************/
  2449. /* Byte 0:
  2450. Bits 7 and 6 are reserved and shall be set to 0
  2451. Bits 3, 4, and 5 shall also be set to 0, to ensure that all the counter
  2452. blocks are distinct from B0
  2453. Bits 0, 1, and 2 contain the same encoding of q as in B0
  2454. */
  2455. ctr[0U] = blockb0[0U] & 0x07U;
  2456. /* byte 1 to NonceSize is the IV (Nonce) */
  2457. for(loopcounter = 1U; loopcounter < hcryp->Init.IVSize + 1U; loopcounter++)
  2458. {
  2459. ctr[loopcounter] = blockb0[loopcounter];
  2460. }
  2461. /* Set the LSB to 1 */
  2462. ctr[15U] |= 0x01U;
  2463. /* Set the key */
  2464. CRYPEx_GCMCCM_SetKey(hcryp, hcryp->Init.pKey, hcryp->Init.KeySize);
  2465. /* Set the CRYP peripheral in AES CCM mode */
  2466. __HAL_CRYP_SET_MODE(hcryp, CRYP_CR_ALGOMODE_AES_CCM_DECRYPT);
  2467. /* Set the Initialization Vector */
  2468. CRYPEx_GCMCCM_SetInitVector(hcryp, ctr);
  2469. /* Select init phase */
  2470. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_INIT);
  2471. b0addr = (uint32_t)blockb0;
  2472. /* Write the blockb0 block in the IN FIFO */
  2473. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2474. b0addr+=4U;
  2475. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2476. b0addr+=4U;
  2477. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2478. b0addr+=4U;
  2479. hcryp->Instance->DR = *(uint32_t*)(b0addr);
  2480. /* Enable the CRYP peripheral */
  2481. __HAL_CRYP_ENABLE(hcryp);
  2482. /* Get tick */
  2483. tickstart = HAL_GetTick();
  2484. while((CRYP->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN)
  2485. {
  2486. /* Check for the Timeout */
  2487. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2488. {
  2489. /* Change state */
  2490. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2491. /* Process Unlocked */
  2492. __HAL_UNLOCK(hcryp);
  2493. return HAL_TIMEOUT;
  2494. }
  2495. }
  2496. /***************************** Header phase *****************************/
  2497. if(headersize != 0U)
  2498. {
  2499. /* Select header phase */
  2500. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
  2501. /* Enable Crypto processor */
  2502. __HAL_CRYP_ENABLE(hcryp);
  2503. for(loopcounter = 0U; (loopcounter < headersize); loopcounter+=16U)
  2504. {
  2505. /* Get tick */
  2506. tickstart = HAL_GetTick();
  2507. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM))
  2508. {
  2509. /* Check for the Timeout */
  2510. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2511. {
  2512. /* Change state */
  2513. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2514. /* Process Unlocked */
  2515. __HAL_UNLOCK(hcryp);
  2516. return HAL_TIMEOUT;
  2517. }
  2518. }
  2519. /* Write the header block in the IN FIFO */
  2520. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2521. headeraddr+=4U;
  2522. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2523. headeraddr+=4U;
  2524. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2525. headeraddr+=4U;
  2526. hcryp->Instance->DR = *(uint32_t*)(headeraddr);
  2527. headeraddr+=4U;
  2528. }
  2529. /* Get tick */
  2530. tickstart = HAL_GetTick();
  2531. while((hcryp->Instance->SR & CRYP_FLAG_BUSY) == CRYP_FLAG_BUSY)
  2532. {
  2533. /* Check for the Timeout */
  2534. if((HAL_GetTick() - tickstart ) > CRYPEx_TIMEOUT_VALUE)
  2535. {
  2536. /* Change state */
  2537. hcryp->State = HAL_CRYP_STATE_TIMEOUT;
  2538. /* Process Unlocked */
  2539. __HAL_UNLOCK(hcryp);
  2540. return HAL_TIMEOUT;
  2541. }
  2542. }
  2543. }
  2544. /* Save formatted counter into the scratch buffer pScratch */
  2545. for(loopcounter = 0U; (loopcounter < 16U); loopcounter++)
  2546. {
  2547. hcryp->Init.pScratch[loopcounter] = ctr[loopcounter];
  2548. }
  2549. /* Reset bit 0 */
  2550. hcryp->Init.pScratch[15U] &= 0xFEU;
  2551. /* Select payload phase once the header phase is performed */
  2552. __HAL_CRYP_SET_PHASE(hcryp, CRYP_PHASE_PAYLOAD);
  2553. /* Flush FIFO */
  2554. __HAL_CRYP_FIFO_FLUSH(hcryp);
  2555. /* Set the phase */
  2556. hcryp->Phase = HAL_CRYP_PHASE_PROCESS;
  2557. }
  2558. /* Set the input and output addresses and start DMA transfer */
  2559. CRYPEx_GCMCCM_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2560. /* Unlock process */
  2561. __HAL_UNLOCK(hcryp);
  2562. /* Return function status */
  2563. return HAL_OK;
  2564. }
  2565. else
  2566. {
  2567. return HAL_ERROR;
  2568. }
  2569. }
  2570. /**
  2571. * @}
  2572. */
  2573. /** @defgroup CRYPEx_Exported_Functions_Group2 CRYPEx IRQ handler management
  2574. * @brief CRYPEx IRQ handler.
  2575. *
  2576. @verbatim
  2577. ==============================================================================
  2578. ##### CRYPEx IRQ handler management #####
  2579. ==============================================================================
  2580. [..] This section provides CRYPEx IRQ handler function.
  2581. @endverbatim
  2582. * @{
  2583. */
  2584. /**
  2585. * @brief This function handles CRYPEx interrupt request.
  2586. * @param hcryp: pointer to a CRYPEx_HandleTypeDef structure that contains
  2587. * the configuration information for CRYP module
  2588. * @retval None
  2589. */
  2590. void HAL_CRYPEx_GCMCCM_IRQHandler(CRYP_HandleTypeDef *hcryp)
  2591. {
  2592. switch(CRYP->CR & CRYP_CR_ALGOMODE_DIRECTION)
  2593. {
  2594. case CRYP_CR_ALGOMODE_AES_GCM_ENCRYPT:
  2595. HAL_CRYPEx_AESGCM_Encrypt_IT(hcryp, NULL, 0U, NULL);
  2596. break;
  2597. case CRYP_CR_ALGOMODE_AES_GCM_DECRYPT:
  2598. HAL_CRYPEx_AESGCM_Decrypt_IT(hcryp, NULL, 0U, NULL);
  2599. break;
  2600. case CRYP_CR_ALGOMODE_AES_CCM_ENCRYPT:
  2601. HAL_CRYPEx_AESCCM_Encrypt_IT(hcryp, NULL, 0U, NULL);
  2602. break;
  2603. case CRYP_CR_ALGOMODE_AES_CCM_DECRYPT:
  2604. HAL_CRYPEx_AESCCM_Decrypt_IT(hcryp, NULL, 0U, NULL);
  2605. break;
  2606. default:
  2607. break;
  2608. }
  2609. }
  2610. /**
  2611. * @}
  2612. */
  2613. /**
  2614. * @}
  2615. */
  2616. #endif /* CRYP */
  2617. #if defined (AES)
  2618. /** @defgroup CRYPEx_Private_Constants CRYPEx Private Constants
  2619. * @{
  2620. */
  2621. #define CRYP_CCF_TIMEOUTVALUE 22000U /*!< CCF flag raising time-out value */
  2622. #define CRYP_BUSY_TIMEOUTVALUE 22000U /*!< BUSY flag reset time-out value */
  2623. #define CRYP_POLLING_OFF 0x0U /*!< No polling when padding */
  2624. #define CRYP_POLLING_ON 0x1U /*!< Polling when padding */
  2625. /**
  2626. * @}
  2627. */
  2628. /* Private macro -------------------------------------------------------------*/
  2629. /* Private variables ---------------------------------------------------------*/
  2630. /* Private function prototypes -----------------------------------------------*/
  2631. /** @defgroup CRYPEx_Private_Functions CRYPEx Private Functions
  2632. * @{
  2633. */
  2634. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
  2635. static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout);
  2636. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  2637. static void CRYP_GCMCMAC_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  2638. static void CRYP_GCMCMAC_DMAInCplt(DMA_HandleTypeDef *hdma);
  2639. static void CRYP_GCMCMAC_DMAError(DMA_HandleTypeDef *hdma);
  2640. static void CRYP_GCMCMAC_DMAOutCplt(DMA_HandleTypeDef *hdma);
  2641. static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  2642. static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  2643. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
  2644. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
  2645. static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
  2646. static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling);
  2647. /**
  2648. * @}
  2649. */
  2650. /* Exported functions ---------------------------------------------------------*/
  2651. /** @defgroup CRYPEx_Exported_Functions CRYPEx Exported Functions
  2652. * @{
  2653. */
  2654. /** @defgroup CRYPEx_Exported_Functions_Group1 Extended callback function
  2655. * @brief Extended callback functions.
  2656. *
  2657. @verbatim
  2658. ===============================================================================
  2659. ##### Extended callback functions #####
  2660. ===============================================================================
  2661. [..] This section provides callback function:
  2662. (+) Computation completed.
  2663. @endverbatim
  2664. * @{
  2665. */
  2666. /**
  2667. * @brief Computation completed callbacks.
  2668. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2669. * the configuration information for CRYP module
  2670. * @retval None
  2671. */
  2672. __weak void HAL_CRYPEx_ComputationCpltCallback(CRYP_HandleTypeDef *hcryp)
  2673. {
  2674. /* Prevent unused argument(s) compilation warning */
  2675. UNUSED(hcryp);
  2676. /* NOTE : This function should not be modified; when the callback is needed,
  2677. the HAL_CRYPEx_ComputationCpltCallback can be implemented in the user file
  2678. */
  2679. }
  2680. /**
  2681. * @}
  2682. */
  2683. /** @defgroup CRYPEx_Exported_Functions_Group2 AES extended processing functions
  2684. * @brief Extended processing functions.
  2685. *
  2686. @verbatim
  2687. ==============================================================================
  2688. ##### AES extended processing functions #####
  2689. ==============================================================================
  2690. [..] This section provides functions allowing to:
  2691. (+) Encrypt plaintext or decrypt cipher text using AES algorithm in different chaining modes.
  2692. Functions are generic (handles ECB, CBC and CTR and all modes) and are only differentiated
  2693. based on the processing type. Three processing types are available:
  2694. (++) Polling mode
  2695. (++) Interrupt mode
  2696. (++) DMA mode
  2697. (+) Generate and authentication tag in addition to encrypt/decrypt a plain/cipher text using AES
  2698. algorithm in different chaining modes.
  2699. Functions are generic (handles GCM, GMAC, CMAC and CCM when applicable) and process only one phase
  2700. so that steps can be skipped if so required. Functions are only differentiated based on the processing type.
  2701. Three processing types are available:
  2702. (++) Polling mode
  2703. (++) Interrupt mode
  2704. (++) DMA mode
  2705. @endverbatim
  2706. * @{
  2707. */
  2708. /**
  2709. * @brief Carry out in polling mode the ciphering or deciphering operation according to
  2710. * hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and
  2711. * chaining modes ECB, CBC and CTR are managed by this function in polling mode.
  2712. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2713. * the configuration information for CRYP module
  2714. * @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption
  2715. * or key derivation+decryption.
  2716. * Parameter is meaningless in case of key derivation.
  2717. * @param Size: Length of the input data buffer in bytes, must be a multiple of 16.
  2718. * Parameter is meaningless in case of key derivation.
  2719. * @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of
  2720. * decryption/key derivation+decryption, or pointer to the derivative keys in
  2721. * case of key derivation only.
  2722. * @param Timeout: Specify Timeout value
  2723. * @retval HAL status
  2724. */
  2725. HAL_StatusTypeDef HAL_CRYPEx_AES(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData, uint32_t Timeout)
  2726. {
  2727. if (hcryp->State == HAL_CRYP_STATE_READY)
  2728. {
  2729. /* Check parameters setting */
  2730. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  2731. {
  2732. if (pOutputData == NULL)
  2733. {
  2734. return HAL_ERROR;
  2735. }
  2736. }
  2737. else
  2738. {
  2739. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
  2740. {
  2741. return HAL_ERROR;
  2742. }
  2743. }
  2744. /* Process Locked */
  2745. __HAL_LOCK(hcryp);
  2746. /* Change the CRYP state */
  2747. hcryp->State = HAL_CRYP_STATE_BUSY;
  2748. /* Call CRYP_ReadKey() API if the operating mode is set to
  2749. key derivation, CRYP_ProcessData() otherwise */
  2750. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  2751. {
  2752. if(CRYP_ReadKey(hcryp, pOutputData, Timeout) != HAL_OK)
  2753. {
  2754. return HAL_TIMEOUT;
  2755. }
  2756. }
  2757. else
  2758. {
  2759. if(CRYP_ProcessData(hcryp, pInputData, Size, pOutputData, Timeout) != HAL_OK)
  2760. {
  2761. return HAL_TIMEOUT;
  2762. }
  2763. }
  2764. /* If the state has not been set to SUSPENDED, set it to
  2765. READY, otherwise keep it as it is */
  2766. if (hcryp->State != HAL_CRYP_STATE_SUSPENDED)
  2767. {
  2768. hcryp->State = HAL_CRYP_STATE_READY;
  2769. }
  2770. /* Process Unlocked */
  2771. __HAL_UNLOCK(hcryp);
  2772. return HAL_OK;
  2773. }
  2774. else
  2775. {
  2776. return HAL_BUSY;
  2777. }
  2778. }
  2779. /**
  2780. * @brief Carry out in interrupt mode the ciphering or deciphering operation according to
  2781. * hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and
  2782. * chaining modes ECB, CBC and CTR are managed by this function in interrupt mode.
  2783. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2784. * the configuration information for CRYP module
  2785. * @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption
  2786. * or key derivation+decryption.
  2787. * Parameter is meaningless in case of key derivation.
  2788. * @param Size: Length of the input data buffer in bytes, must be a multiple of 16.
  2789. * Parameter is meaningless in case of key derivation.
  2790. * @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of
  2791. * decryption/key derivation+decryption, or pointer to the derivative keys in
  2792. * case of key derivation only.
  2793. * @retval HAL status
  2794. */
  2795. HAL_StatusTypeDef HAL_CRYPEx_AES_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData)
  2796. {
  2797. uint32_t inputaddr = 0U;
  2798. if(hcryp->State == HAL_CRYP_STATE_READY)
  2799. {
  2800. /* Check parameters setting */
  2801. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  2802. {
  2803. if (pOutputData == NULL)
  2804. {
  2805. return HAL_ERROR;
  2806. }
  2807. }
  2808. else
  2809. {
  2810. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
  2811. {
  2812. return HAL_ERROR;
  2813. }
  2814. }
  2815. /* Process Locked */
  2816. __HAL_LOCK(hcryp);
  2817. /* If operating mode is not limited to key derivation only,
  2818. get the buffers addresses and sizes */
  2819. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  2820. {
  2821. hcryp->CrypInCount = Size;
  2822. hcryp->pCrypInBuffPtr = pInputData;
  2823. hcryp->pCrypOutBuffPtr = pOutputData;
  2824. hcryp->CrypOutCount = Size;
  2825. }
  2826. /* Change the CRYP state */
  2827. hcryp->State = HAL_CRYP_STATE_BUSY;
  2828. /* Process Unlocked */
  2829. __HAL_UNLOCK(hcryp);
  2830. /* Enable Computation Complete Flag and Error Interrupts */
  2831. __HAL_CRYP_ENABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  2832. /* If operating mode is key derivation only, the input data have
  2833. already been entered during the initialization process. For
  2834. the other operating modes, they are fed to the CRYP hardware
  2835. block at this point. */
  2836. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  2837. {
  2838. /* Initiate the processing under interrupt in entering
  2839. the first input data */
  2840. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2841. /* Increment/decrement instance pointer/counter */
  2842. hcryp->pCrypInBuffPtr += 16U;
  2843. hcryp->CrypInCount -= 16U;
  2844. /* Write the first input block in the Data Input register */
  2845. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2846. inputaddr+=4U;
  2847. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2848. inputaddr+=4U;
  2849. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2850. inputaddr+=4U;
  2851. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2852. }
  2853. /* Return function status */
  2854. return HAL_OK;
  2855. }
  2856. else
  2857. {
  2858. return HAL_BUSY;
  2859. }
  2860. }
  2861. /**
  2862. * @brief Carry out in DMA mode the ciphering or deciphering operation according to
  2863. * hcryp->Init structure fields.
  2864. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2865. * the configuration information for CRYP module
  2866. * @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption
  2867. * or key derivation+decryption.
  2868. * @param Size: Length of the input data buffer in bytes, must be a multiple of 16.
  2869. * @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of
  2870. * decryption/key derivation+decryption.
  2871. * @note Chaining modes ECB, CBC and CTR are managed by this function in DMA mode.
  2872. * @note Supported operating modes are encryption, decryption and key derivation with decryption.
  2873. * @note No DMA channel is provided for key derivation only and therefore, access to AES_KEYRx
  2874. * registers must be done by software.
  2875. * @note This API is not applicable to key derivation only; for such a mode, access to AES_KEYRx
  2876. * registers must be done by software thru HAL_CRYPEx_AES() or HAL_CRYPEx_AES_IT() APIs.
  2877. * @note pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  2878. * @retval HAL status
  2879. */
  2880. HAL_StatusTypeDef HAL_CRYPEx_AES_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData)
  2881. {
  2882. uint32_t inputaddr = 0U;
  2883. uint32_t outputaddr = 0U;
  2884. if (hcryp->State == HAL_CRYP_STATE_READY)
  2885. {
  2886. /* Check parameters setting */
  2887. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  2888. {
  2889. /* no DMA channel is provided for key derivation operating mode,
  2890. access to AES_KEYRx registers must be done by software */
  2891. return HAL_ERROR;
  2892. }
  2893. else
  2894. {
  2895. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
  2896. {
  2897. return HAL_ERROR;
  2898. }
  2899. }
  2900. /* Process Locked */
  2901. __HAL_LOCK(hcryp);
  2902. inputaddr = (uint32_t)pInputData;
  2903. outputaddr = (uint32_t)pOutputData;
  2904. /* Change the CRYP state */
  2905. hcryp->State = HAL_CRYP_STATE_BUSY;
  2906. /* Set the input and output addresses and start DMA transfer */
  2907. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  2908. /* Process Unlocked */
  2909. __HAL_UNLOCK(hcryp);
  2910. /* Return function status */
  2911. return HAL_OK;
  2912. }
  2913. else
  2914. {
  2915. return HAL_BUSY;
  2916. }
  2917. }
  2918. /**
  2919. * @brief Carry out in polling mode the authentication tag generation as well as the ciphering or deciphering
  2920. * operation according to hcryp->Init structure fields.
  2921. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2922. * the configuration information for CRYP module
  2923. * @param pInputData:
  2924. * - pointer to payload data in GCM payload phase,
  2925. * - pointer to B0 block in CMAC header phase,
  2926. * - pointer to C block in CMAC final phase.
  2927. * - Parameter is meaningless in case of GCM/GMAC init, header and final phases.
  2928. * @param Size:
  2929. * - length of the input payload data buffer in bytes,
  2930. * - length of B0 block (in bytes) in CMAC header phase,
  2931. * - length of C block (in bytes) in CMAC final phase.
  2932. * - Parameter is meaningless in case of GCM/GMAC init and header phases.
  2933. * @param pOutputData:
  2934. * - pointer to plain or cipher text in GCM payload phase,
  2935. * - pointer to authentication tag in GCM/GMAC and CMAC final phases.
  2936. * - Parameter is meaningless in case of GCM/GMAC init and header phases
  2937. * and in case of CMAC header phase.
  2938. * @param Timeout: Specify Timeout value
  2939. * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC, CMAC and CCM when the latter is applicable.
  2940. * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes
  2941. * can be skipped by the user if so required.
  2942. * @retval HAL status
  2943. */
  2944. HAL_StatusTypeDef HAL_CRYPEx_AES_Auth(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData, uint32_t Timeout)
  2945. {
  2946. uint32_t index = 0U;
  2947. uint32_t inputaddr = 0U;
  2948. uint32_t outputaddr = 0U;
  2949. uint32_t tagaddr = 0U;
  2950. uint64_t headerlength = 0U;
  2951. uint64_t inputlength = 0U;
  2952. uint64_t payloadlength = 0U;
  2953. uint32_t difflength = 0U;
  2954. uint32_t addhoc_process = 0U;
  2955. if (hcryp->State == HAL_CRYP_STATE_READY)
  2956. {
  2957. /* input/output parameters check */
  2958. if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  2959. {
  2960. if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U))
  2961. {
  2962. return HAL_ERROR;
  2963. }
  2964. #if defined(AES_CR_NPBLB)
  2965. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)
  2966. #else
  2967. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  2968. #endif
  2969. {
  2970. /* In case of CMAC (or CCM) header phase resumption, we can have pInputData = NULL and Size = 0 */
  2971. if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U)))
  2972. {
  2973. return HAL_ERROR;
  2974. }
  2975. }
  2976. }
  2977. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  2978. {
  2979. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
  2980. {
  2981. return HAL_ERROR;
  2982. }
  2983. }
  2984. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  2985. {
  2986. if (pOutputData == NULL)
  2987. {
  2988. return HAL_ERROR;
  2989. }
  2990. #if defined(AES_CR_NPBLB)
  2991. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) && (pInputData == NULL))
  2992. #else
  2993. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
  2994. #endif
  2995. {
  2996. return HAL_ERROR;
  2997. }
  2998. }
  2999. /* Process Locked */
  3000. __HAL_LOCK(hcryp);
  3001. /* Change the CRYP state */
  3002. hcryp->State = HAL_CRYP_STATE_BUSY;
  3003. /*==============================================*/
  3004. /* GCM/GMAC (or CCM when applicable) init phase */
  3005. /*==============================================*/
  3006. /* In case of init phase, the input data (Key and Initialization Vector) have
  3007. already been entered during the initialization process. Therefore, the
  3008. API just waits for the CCF flag to be set. */
  3009. if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
  3010. {
  3011. /* just wait for hash computation */
  3012. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3013. {
  3014. hcryp->State = HAL_CRYP_STATE_READY;
  3015. __HAL_UNLOCK(hcryp);
  3016. return HAL_TIMEOUT;
  3017. }
  3018. /* Clear CCF Flag */
  3019. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3020. /* Mark that the initialization phase is over */
  3021. hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER;
  3022. }
  3023. /*=====================================*/
  3024. /* GCM/GMAC or (CCM/)CMAC header phase */
  3025. /*=====================================*/
  3026. else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  3027. {
  3028. /* Set header phase; for GCM or GMAC, set data-byte at this point */
  3029. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3030. {
  3031. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
  3032. }
  3033. else
  3034. {
  3035. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
  3036. }
  3037. /* Enable the Peripheral */
  3038. __HAL_CRYP_ENABLE();
  3039. #if !defined(AES_CR_NPBLB)
  3040. /* in case of CMAC, enter B0 block in header phase, before the header itself. */
  3041. /* If Size = 0 (possible case of resumption after CMAC header phase suspension),
  3042. skip these steps and go directly to header buffer feeding to the HW */
  3043. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (Size != 0U))
  3044. {
  3045. inputaddr = (uint32_t)pInputData;
  3046. for(index=0U; (index < Size); index += 16U)
  3047. {
  3048. /* Write the Input block in the Data Input register */
  3049. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3050. inputaddr+=4U;
  3051. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3052. inputaddr+=4U;
  3053. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3054. inputaddr+=4U;
  3055. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3056. inputaddr+=4U;
  3057. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3058. {
  3059. hcryp->State = HAL_CRYP_STATE_READY;
  3060. __HAL_UNLOCK(hcryp);
  3061. return HAL_TIMEOUT;
  3062. }
  3063. /* Clear CCF Flag */
  3064. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3065. /* If the suspension flag has been raised and if the processing is not about
  3066. to end, suspend processing */
  3067. if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < Size))
  3068. {
  3069. /* reset SuspendRequest */
  3070. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  3071. /* Change the CRYP state */
  3072. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  3073. /* Mark that the header phase is over */
  3074. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  3075. /* Save current reading and writing locations of Input and Output buffers */
  3076. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  3077. /* Save the total number of bytes (B blocks + header) that remain to be
  3078. processed at this point */
  3079. hcryp->CrypInCount = hcryp->Init.HeaderSize + Size - (index+16U);
  3080. /* Process Unlocked */
  3081. __HAL_UNLOCK(hcryp);
  3082. return HAL_OK;
  3083. }
  3084. } /* for(index=0; (index < Size); index += 16) */
  3085. }
  3086. #endif /* !defined(AES_CR_NPBLB) */
  3087. /* Enter header */
  3088. inputaddr = (uint32_t)hcryp->Init.Header;
  3089. /* Local variable headerlength is a number of bytes multiple of 128 bits,
  3090. remaining header data (if any) are handled after this loop */
  3091. headerlength = (((hcryp->Init.HeaderSize)/16U)*16U) ;
  3092. if ((hcryp->Init.HeaderSize % 16U) != 0U)
  3093. {
  3094. difflength = (uint32_t) (hcryp->Init.HeaderSize - headerlength);
  3095. }
  3096. for(index=0U; index < headerlength; index += 16U)
  3097. {
  3098. /* Write the Input block in the Data Input register */
  3099. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3100. inputaddr+=4U;
  3101. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3102. inputaddr+=4U;
  3103. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3104. inputaddr+=4U;
  3105. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3106. inputaddr+=4U;
  3107. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3108. {
  3109. hcryp->State = HAL_CRYP_STATE_READY;
  3110. __HAL_UNLOCK(hcryp);
  3111. return HAL_TIMEOUT;
  3112. }
  3113. /* Clear CCF Flag */
  3114. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3115. /* If the suspension flag has been raised and if the processing is not about
  3116. to end, suspend processing */
  3117. if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < headerlength))
  3118. {
  3119. /* reset SuspendRequest */
  3120. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  3121. /* Change the CRYP state */
  3122. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  3123. /* Mark that the header phase is over */
  3124. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  3125. /* Save current reading and writing locations of Input and Output buffers */
  3126. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  3127. /* Save the total number of bytes that remain to be processed at this point */
  3128. hcryp->CrypInCount = hcryp->Init.HeaderSize - (index+16U);
  3129. /* Process Unlocked */
  3130. __HAL_UNLOCK(hcryp);
  3131. return HAL_OK;
  3132. }
  3133. }
  3134. /* Case header length is not a multiple of 16 bytes */
  3135. if (difflength != 0U)
  3136. {
  3137. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  3138. CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
  3139. }
  3140. /* Mark that the header phase is over */
  3141. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  3142. }
  3143. /*============================================*/
  3144. /* GCM (or CCM when applicable) payload phase */
  3145. /*============================================*/
  3146. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  3147. {
  3148. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
  3149. /* if the header phase has been bypassed, AES must be enabled again */
  3150. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  3151. {
  3152. __HAL_CRYP_ENABLE();
  3153. }
  3154. inputaddr = (uint32_t)pInputData;
  3155. outputaddr = (uint32_t)pOutputData;
  3156. /* Enter payload */
  3157. /* Specific handling to manage payload last block size less than 128 bits */
  3158. if ((Size % 16U) != 0U)
  3159. {
  3160. payloadlength = (Size/16U) * 16U;
  3161. difflength = (uint32_t) (Size - payloadlength);
  3162. addhoc_process = 1U;
  3163. }
  3164. else
  3165. {
  3166. payloadlength = Size;
  3167. addhoc_process = 0U;
  3168. }
  3169. /* Feed payload */
  3170. for(index=0U; index < payloadlength; index += 16U)
  3171. {
  3172. /* Write the Input block in the Data Input register */
  3173. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3174. inputaddr+=4U;
  3175. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3176. inputaddr+=4U;
  3177. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3178. inputaddr+=4U;
  3179. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3180. inputaddr+=4U;
  3181. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3182. {
  3183. hcryp->State = HAL_CRYP_STATE_READY;
  3184. __HAL_UNLOCK(hcryp);
  3185. return HAL_TIMEOUT;
  3186. }
  3187. /* Clear CCF Flag */
  3188. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3189. /* Retrieve output data: read the output block
  3190. from the Data Output Register */
  3191. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  3192. outputaddr+=4U;
  3193. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  3194. outputaddr+=4U;
  3195. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  3196. outputaddr+=4U;
  3197. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  3198. outputaddr+=4U;
  3199. /* If the suspension flag has been raised and if the processing is not about
  3200. to end, suspend processing */
  3201. if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < payloadlength))
  3202. {
  3203. /* no flag waiting under IRQ handling */
  3204. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  3205. {
  3206. /* Ensure that Busy flag is reset */
  3207. if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK)
  3208. {
  3209. hcryp->State = HAL_CRYP_STATE_READY;
  3210. __HAL_UNLOCK(hcryp);
  3211. return HAL_TIMEOUT;
  3212. }
  3213. }
  3214. /* reset SuspendRequest */
  3215. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  3216. /* Change the CRYP state */
  3217. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  3218. /* Mark that the header phase is over */
  3219. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  3220. /* Save current reading and writing locations of Input and Output buffers */
  3221. hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
  3222. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  3223. /* Save the number of bytes that remain to be processed at this point */
  3224. hcryp->CrypInCount = Size - (index+16U);
  3225. /* Process Unlocked */
  3226. __HAL_UNLOCK(hcryp);
  3227. return HAL_OK;
  3228. }
  3229. }
  3230. /* Additional processing to manage GCM(/CCM) encryption and decryption cases when
  3231. payload last block size less than 128 bits */
  3232. if (addhoc_process == 1U)
  3233. {
  3234. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  3235. hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
  3236. CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
  3237. } /* (addhoc_process == 1) */
  3238. /* Mark that the payload phase is over */
  3239. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  3240. }
  3241. /*====================================*/
  3242. /* GCM/GMAC or (CCM/)CMAC final phase */
  3243. /*====================================*/
  3244. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  3245. {
  3246. tagaddr = (uint32_t)pOutputData;
  3247. #if defined(AES_CR_NPBLB)
  3248. /* By default, clear NPBLB field */
  3249. CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
  3250. #endif
  3251. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
  3252. /* if the header and payload phases have been bypassed, AES must be enabled again */
  3253. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  3254. {
  3255. __HAL_CRYP_ENABLE();
  3256. }
  3257. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3258. {
  3259. headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
  3260. inputlength = Size * 8U; /* input length in bits */
  3261. if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  3262. {
  3263. hcryp->Instance->DINR = __RBIT((headerlength)>>32U);
  3264. hcryp->Instance->DINR = __RBIT(headerlength);
  3265. hcryp->Instance->DINR = __RBIT((inputlength)>>32U);
  3266. hcryp->Instance->DINR = __RBIT(inputlength);
  3267. }
  3268. else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  3269. {
  3270. hcryp->Instance->DINR = __REV((headerlength)>>32U);
  3271. hcryp->Instance->DINR = __REV(headerlength);
  3272. hcryp->Instance->DINR = __REV((inputlength)>>32U);
  3273. hcryp->Instance->DINR = __REV(inputlength);
  3274. }
  3275. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  3276. {
  3277. hcryp->Instance->DINR = __ROR((headerlength)>>32U, 16U);
  3278. hcryp->Instance->DINR = __ROR(headerlength, 16U);
  3279. hcryp->Instance->DINR = __ROR((inputlength)>>32U, 16U);
  3280. hcryp->Instance->DINR = __ROR(inputlength, 16U);
  3281. }
  3282. else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
  3283. {
  3284. hcryp->Instance->DINR = (uint32_t)(headerlength>>32U);
  3285. hcryp->Instance->DINR = (uint32_t)(headerlength);
  3286. hcryp->Instance->DINR = (uint32_t)(inputlength>>32U);
  3287. hcryp->Instance->DINR = (uint32_t)(inputlength);
  3288. }
  3289. }
  3290. #if !defined(AES_CR_NPBLB)
  3291. else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3292. {
  3293. inputaddr = (uint32_t)pInputData;
  3294. /* Enter the last block made of a 128-bit value formatted
  3295. from the original B0 packet. */
  3296. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3297. inputaddr+=4U;
  3298. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3299. inputaddr+=4U;
  3300. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3301. inputaddr+=4U;
  3302. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3303. }
  3304. #endif
  3305. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  3306. {
  3307. hcryp->State = HAL_CRYP_STATE_READY;
  3308. __HAL_UNLOCK(hcryp);
  3309. return HAL_TIMEOUT;
  3310. }
  3311. /* Read the Auth TAG in the Data Out register */
  3312. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  3313. tagaddr+=4U;
  3314. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  3315. tagaddr+=4U;
  3316. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  3317. tagaddr+=4U;
  3318. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  3319. /* Clear CCF Flag */
  3320. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3321. /* Mark that the final phase is over */
  3322. hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER;
  3323. /* Disable the Peripheral */
  3324. __HAL_CRYP_DISABLE();
  3325. }
  3326. /*=================================================*/
  3327. /* case incorrect hcryp->Init.GCMCMACPhase setting */
  3328. /*=================================================*/
  3329. else
  3330. {
  3331. hcryp->State = HAL_CRYP_STATE_ERROR;
  3332. __HAL_UNLOCK(hcryp);
  3333. return HAL_ERROR;
  3334. }
  3335. /* Change the CRYP state */
  3336. hcryp->State = HAL_CRYP_STATE_READY;
  3337. /* Process Unlocked */
  3338. __HAL_UNLOCK(hcryp);
  3339. return HAL_OK;
  3340. }
  3341. else
  3342. {
  3343. return HAL_BUSY;
  3344. }
  3345. }
  3346. /**
  3347. * @brief Carry out in interrupt mode the authentication tag generation as well as the ciphering or deciphering
  3348. * operation according to hcryp->Init structure fields.
  3349. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3350. * the configuration information for CRYP module
  3351. * @param pInputData:
  3352. * - pointer to payload data in GCM payload phase,
  3353. * - pointer to B0 block in CMAC header phase,
  3354. * - pointer to C block in CMAC final phase.
  3355. * Parameter is meaningless in case of GCM/GMAC init, header and final phases.
  3356. * @param Size:
  3357. * - length of the input payload data buffer in bytes,
  3358. * - length of B0 block (in bytes) in CMAC header phase,
  3359. * - length of C block (in bytes) in CMAC final phase.
  3360. * - Parameter is meaningless in case of GCM/GMAC init and header phases.
  3361. * @param pOutputData:
  3362. * - pointer to plain or cipher text in GCM payload phase,
  3363. * - pointer to authentication tag in GCM/GMAC and CMAC final phases.
  3364. * - Parameter is meaningless in case of GCM/GMAC init and header phases
  3365. * and in case of CMAC header phase.
  3366. * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC.
  3367. * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes
  3368. * can be skipped by the user if so required.
  3369. * @retval HAL status
  3370. */
  3371. HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData)
  3372. {
  3373. uint32_t inputaddr = 0U;
  3374. uint64_t headerlength = 0U;
  3375. uint64_t inputlength = 0U;
  3376. uint32_t index = 0U;
  3377. uint32_t addhoc_process = 0U;
  3378. uint32_t difflength = 0U;
  3379. uint32_t difflengthmod4 = 0U;
  3380. uint32_t mask[3U] = {0x0FFU, 0x0FFFFU, 0x0FFFFFFU};
  3381. if (hcryp->State == HAL_CRYP_STATE_READY)
  3382. {
  3383. /* input/output parameters check */
  3384. if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  3385. {
  3386. if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U))
  3387. {
  3388. return HAL_ERROR;
  3389. }
  3390. #if defined(AES_CR_NPBLB)
  3391. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)
  3392. #else
  3393. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3394. #endif
  3395. {
  3396. /* In case of CMAC header phase resumption, we can have pInputData = NULL and Size = 0 */
  3397. if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U)))
  3398. {
  3399. return HAL_ERROR;
  3400. }
  3401. }
  3402. }
  3403. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  3404. {
  3405. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
  3406. {
  3407. return HAL_ERROR;
  3408. }
  3409. }
  3410. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  3411. {
  3412. if (pOutputData == NULL)
  3413. {
  3414. return HAL_ERROR;
  3415. }
  3416. #if defined(AES_CR_NPBLB)
  3417. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) && (pInputData == NULL))
  3418. #else
  3419. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
  3420. #endif
  3421. {
  3422. return HAL_ERROR;
  3423. }
  3424. }
  3425. /* Process Locked */
  3426. __HAL_LOCK(hcryp);
  3427. /* Change the CRYP state */
  3428. hcryp->State = HAL_CRYP_STATE_BUSY;
  3429. /* Process Unlocked */
  3430. __HAL_UNLOCK(hcryp);
  3431. /* Enable Computation Complete Flag and Error Interrupts */
  3432. __HAL_CRYP_ENABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  3433. /*==============================================*/
  3434. /* GCM/GMAC (or CCM when applicable) init phase */
  3435. /*==============================================*/
  3436. if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
  3437. {
  3438. /* In case of init phase, the input data (Key and Initialization Vector) have
  3439. already been entered during the initialization process. Therefore, the
  3440. software just waits for the CCF interrupt to be raised and which will
  3441. be handled by CRYP_AES_Auth_IT() API. */
  3442. }
  3443. /*=====================================*/
  3444. /* GCM/GMAC or (CCM/)CMAC header phase */
  3445. /*=====================================*/
  3446. else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  3447. {
  3448. #if defined(AES_CR_NPBLB)
  3449. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)
  3450. #else
  3451. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3452. #endif
  3453. {
  3454. /* In case of CMAC, B blocks are first entered, before the header.
  3455. Therefore, B blocks and the header are entered back-to-back
  3456. as if it was only one single block.
  3457. However, in case of resumption after suspension, if all the
  3458. B blocks have been entered (in that case, Size = 0), only the
  3459. remainder of the non-processed header bytes are entered. */
  3460. if (Size != 0U)
  3461. {
  3462. hcryp->CrypInCount = Size + hcryp->Init.HeaderSize;
  3463. hcryp->pCrypInBuffPtr = pInputData;
  3464. }
  3465. else
  3466. {
  3467. hcryp->CrypInCount = hcryp->Init.HeaderSize;
  3468. hcryp->pCrypInBuffPtr = hcryp->Init.Header;
  3469. }
  3470. }
  3471. else
  3472. {
  3473. /* Get the header addresses and sizes */
  3474. hcryp->CrypInCount = hcryp->Init.HeaderSize;
  3475. hcryp->pCrypInBuffPtr = hcryp->Init.Header;
  3476. }
  3477. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  3478. /* Set header phase; for GCM or GMAC, set data-byte at this point */
  3479. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3480. {
  3481. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
  3482. }
  3483. else
  3484. {
  3485. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
  3486. }
  3487. /* Enable the Peripheral */
  3488. __HAL_CRYP_ENABLE();
  3489. /* Increment/decrement instance pointer/counter */
  3490. if (hcryp->CrypInCount == 0U)
  3491. {
  3492. /* Case of no header */
  3493. hcryp->State = HAL_CRYP_STATE_READY;
  3494. return HAL_OK;
  3495. }
  3496. else if (hcryp->CrypInCount < 16U)
  3497. {
  3498. hcryp->CrypInCount = 0U;
  3499. addhoc_process = 1U;
  3500. difflength = (uint32_t) (hcryp->Init.HeaderSize);
  3501. difflengthmod4 = difflength%4U;
  3502. }
  3503. else
  3504. {
  3505. hcryp->pCrypInBuffPtr += 16U;
  3506. hcryp->CrypInCount -= 16U;
  3507. }
  3508. #if defined(AES_CR_NPBLB)
  3509. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)
  3510. #else
  3511. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3512. #endif
  3513. {
  3514. if (hcryp->CrypInCount == hcryp->Init.HeaderSize)
  3515. {
  3516. /* All B blocks will have been entered after the next
  3517. four DINR writing, so point at header buffer for
  3518. the next iteration */
  3519. hcryp->pCrypInBuffPtr = hcryp->Init.Header;
  3520. }
  3521. }
  3522. /* Enter header first block to initiate the process
  3523. in the Data Input register */
  3524. if (addhoc_process == 0U)
  3525. {
  3526. /* Header has size equal or larger than 128 bits */
  3527. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3528. inputaddr+=4U;
  3529. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3530. inputaddr+=4U;
  3531. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3532. inputaddr+=4U;
  3533. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3534. }
  3535. else
  3536. {
  3537. /* Header has size less than 128 bits */
  3538. /* Enter complete words when possible */
  3539. for(index=0U; index < (difflength/4U); index ++)
  3540. {
  3541. /* Write the Input block in the Data Input register */
  3542. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3543. inputaddr+=4U;
  3544. }
  3545. /* Enter incomplete word padded with zeroes if applicable
  3546. (case of header length not a multiple of 32-bits) */
  3547. if (difflengthmod4 != 0U)
  3548. {
  3549. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1U]);
  3550. }
  3551. /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
  3552. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  3553. {
  3554. hcryp->Instance->DINR = 0U;
  3555. }
  3556. }
  3557. }
  3558. /*============================================*/
  3559. /* GCM (or CCM when applicable) payload phase */
  3560. /*============================================*/
  3561. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  3562. {
  3563. /* Get the buffer addresses and sizes */
  3564. hcryp->CrypInCount = Size;
  3565. hcryp->pCrypInBuffPtr = pInputData;
  3566. hcryp->pCrypOutBuffPtr = pOutputData;
  3567. hcryp->CrypOutCount = Size;
  3568. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  3569. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCM_PAYLOAD_PHASE);
  3570. /* if the header phase has been bypassed, AES must be enabled again */
  3571. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  3572. {
  3573. __HAL_CRYP_ENABLE();
  3574. }
  3575. /* Specific handling to manage payload size less than 128 bits */
  3576. if (Size < 16U)
  3577. {
  3578. #if defined(AES_CR_NPBLB)
  3579. /* In case of GCM encryption or CCM decryption, specify the number of padding
  3580. bytes in last block of payload */
  3581. if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE)
  3582. {
  3583. if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC)
  3584. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT))
  3585. || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM_CMAC)
  3586. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT)))
  3587. {
  3588. /* Set NPBLB field in writing the number of padding bytes
  3589. for the last block of payload */
  3590. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 16U - difflength);
  3591. }
  3592. }
  3593. #else
  3594. /* Software workaround applied to GCM encryption only */
  3595. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  3596. {
  3597. /* Change the mode configured in CHMOD bits of CR register to select CTR mode */
  3598. __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_CTR);
  3599. }
  3600. #endif
  3601. /* Set hcryp->CrypInCount to 0 (no more data to enter) */
  3602. hcryp->CrypInCount = 0U;
  3603. /* Insert the last block (which size is inferior to 128 bits) padded with zeroes,
  3604. to have a complete block of 128 bits */
  3605. difflength = (uint32_t) (Size);
  3606. difflengthmod4 = difflength%4U;
  3607. /* Insert the last block (which size is inferior to 128 bits) padded with zeroes
  3608. to have a complete block of 128 bits */
  3609. for(index=0U; index < (difflength/4U); index ++)
  3610. {
  3611. /* Write the Input block in the Data Input register */
  3612. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3613. inputaddr+=4U;
  3614. }
  3615. /* If required, manage input data size not multiple of 32 bits */
  3616. if (difflengthmod4 != 0U)
  3617. {
  3618. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1U]);
  3619. }
  3620. /* Wrap-up in padding with zero-words if applicable */
  3621. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  3622. {
  3623. hcryp->Instance->DINR = 0U;
  3624. }
  3625. }
  3626. else
  3627. {
  3628. /* Increment/decrement instance pointer/counter */
  3629. hcryp->pCrypInBuffPtr += 16U;
  3630. hcryp->CrypInCount -= 16U;
  3631. /* Enter payload first block to initiate the process
  3632. in the Data Input register */
  3633. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3634. inputaddr+=4U;
  3635. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3636. inputaddr+=4U;
  3637. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3638. inputaddr+=4U;
  3639. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3640. }
  3641. }
  3642. /*====================================*/
  3643. /* GCM/GMAC or (CCM/)CMAC final phase */
  3644. /*====================================*/
  3645. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  3646. {
  3647. hcryp->pCrypOutBuffPtr = pOutputData;
  3648. #if defined(AES_CR_NPBLB)
  3649. /* By default, clear NPBLB field */
  3650. CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
  3651. #endif
  3652. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
  3653. /* if the header and payload phases have been bypassed, AES must be enabled again */
  3654. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  3655. {
  3656. __HAL_CRYP_ENABLE();
  3657. }
  3658. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3659. {
  3660. headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
  3661. inputlength = Size * 8U; /* Input length in bits */
  3662. /* Write the number of bits in the header on 64 bits followed by the number
  3663. of bits in the payload on 64 bits as well */
  3664. if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  3665. {
  3666. hcryp->Instance->DINR = __RBIT((headerlength)>>32U);
  3667. hcryp->Instance->DINR = __RBIT(headerlength);
  3668. hcryp->Instance->DINR = __RBIT((inputlength)>>32U);
  3669. hcryp->Instance->DINR = __RBIT(inputlength);
  3670. }
  3671. else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  3672. {
  3673. hcryp->Instance->DINR = __REV((headerlength)>>32U);
  3674. hcryp->Instance->DINR = __REV(headerlength);
  3675. hcryp->Instance->DINR = __REV((inputlength)>>32U);
  3676. hcryp->Instance->DINR = __REV(inputlength);
  3677. }
  3678. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  3679. {
  3680. hcryp->Instance->DINR = __ROR((headerlength)>>32U, 16U);
  3681. hcryp->Instance->DINR = __ROR(headerlength, 16U);
  3682. hcryp->Instance->DINR = __ROR((inputlength)>>32U, 16U);
  3683. hcryp->Instance->DINR = __ROR(inputlength, 16U);
  3684. }
  3685. else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
  3686. {
  3687. hcryp->Instance->DINR = (uint32_t)(headerlength>>32U);
  3688. hcryp->Instance->DINR = (uint32_t)(headerlength);
  3689. hcryp->Instance->DINR = (uint32_t)(inputlength>>32U);
  3690. hcryp->Instance->DINR = (uint32_t)(inputlength);
  3691. }
  3692. }
  3693. #if !defined(AES_CR_NPBLB)
  3694. else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3695. {
  3696. inputaddr = (uint32_t)pInputData;
  3697. /* Enter the last block made of a 128-bit value formatted
  3698. from the original B0 packet. */
  3699. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3700. inputaddr+=4U;
  3701. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3702. inputaddr+=4U;
  3703. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3704. inputaddr+=4U;
  3705. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3706. inputaddr+=4U;
  3707. }
  3708. #endif
  3709. }
  3710. /*=================================================*/
  3711. /* case incorrect hcryp->Init.GCMCMACPhase setting */
  3712. /*=================================================*/
  3713. else
  3714. {
  3715. hcryp->State = HAL_CRYP_STATE_ERROR;
  3716. return HAL_ERROR;
  3717. }
  3718. return HAL_OK;
  3719. }
  3720. else
  3721. {
  3722. return HAL_BUSY;
  3723. }
  3724. }
  3725. /**
  3726. * @brief Carry out in DMA mode the authentication tag generation as well as the ciphering or deciphering
  3727. * operation according to hcryp->Init structure fields.
  3728. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  3729. * the configuration information for CRYP module
  3730. * @param pInputData:
  3731. * - pointer to payload data in GCM payload phase,
  3732. * - pointer to B0 block in CMAC header phase,
  3733. * - pointer to C block in CMAC final phase.
  3734. * - Parameter is meaningless in case of GCM/GMAC init, header and final phases.
  3735. * @param Size:
  3736. * - length of the input payload data buffer in bytes,
  3737. * - length of B block (in bytes) in CMAC header phase,
  3738. * - length of C block (in bytes) in CMAC final phase.
  3739. * - Parameter is meaningless in case of GCM/GMAC init and header phases.
  3740. * @param pOutputData:
  3741. * - pointer to plain or cipher text in GCM payload phase,
  3742. * - pointer to authentication tag in GCM/GMAC and CMAC final phases.
  3743. * - Parameter is meaningless in case of GCM/GMAC init and header phases
  3744. * and in case of CMAC header phase.
  3745. * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC.
  3746. * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes
  3747. * can be skipped by the user if so required.
  3748. * @note pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  3749. * @retval HAL status
  3750. */
  3751. HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData)
  3752. {
  3753. uint32_t inputaddr = 0U;
  3754. uint32_t outputaddr = 0U;
  3755. uint32_t tagaddr = 0U;
  3756. uint64_t headerlength = 0U;
  3757. uint64_t inputlength = 0U;
  3758. uint64_t payloadlength = 0U;
  3759. if (hcryp->State == HAL_CRYP_STATE_READY)
  3760. {
  3761. /* input/output parameters check */
  3762. if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  3763. {
  3764. if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U))
  3765. {
  3766. return HAL_ERROR;
  3767. }
  3768. #if defined(AES_CR_NPBLB)
  3769. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)
  3770. #else
  3771. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3772. #endif
  3773. {
  3774. if ((pInputData == NULL) || (Size == 0U))
  3775. {
  3776. return HAL_ERROR;
  3777. }
  3778. }
  3779. }
  3780. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  3781. {
  3782. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
  3783. {
  3784. return HAL_ERROR;
  3785. }
  3786. }
  3787. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  3788. {
  3789. if (pOutputData == NULL)
  3790. {
  3791. return HAL_ERROR;
  3792. }
  3793. #if defined(AES_CR_NPBLB)
  3794. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC) && (pInputData == NULL))
  3795. #else
  3796. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
  3797. #endif
  3798. {
  3799. return HAL_ERROR;
  3800. }
  3801. }
  3802. /* Process Locked */
  3803. __HAL_LOCK(hcryp);
  3804. /* Change the CRYP state */
  3805. hcryp->State = HAL_CRYP_STATE_BUSY;
  3806. /*==============================================*/
  3807. /* GCM/GMAC (or CCM when applicable) init phase */
  3808. /*==============================================*/
  3809. /* In case of init phase, the input data (Key and Initialization Vector) have
  3810. already been entered during the initialization process. No DMA transfer is
  3811. required at that point therefore, the software just waits for the CCF flag
  3812. to be raised. */
  3813. if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
  3814. {
  3815. /* just wait for hash computation */
  3816. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  3817. {
  3818. hcryp->State = HAL_CRYP_STATE_READY;
  3819. __HAL_UNLOCK(hcryp);
  3820. return HAL_TIMEOUT;
  3821. }
  3822. /* Clear CCF Flag */
  3823. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3824. /* Mark that the initialization phase is over */
  3825. hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER;
  3826. hcryp->State = HAL_CRYP_STATE_READY;
  3827. }
  3828. /*===============================*/
  3829. /* GCM/GMAC or CMAC header phase */
  3830. /*===============================*/
  3831. else if (hcryp->Init.GCMCMACPhase == CRYP_GCMCMAC_HEADER_PHASE)
  3832. {
  3833. /* Set header phase; for GCM or GMAC, set data-byte at this point */
  3834. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  3835. {
  3836. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_GCMCMAC_HEADER_PHASE|hcryp->Init.DataType);
  3837. }
  3838. else
  3839. {
  3840. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_HEADER_PHASE);
  3841. }
  3842. #if !defined(AES_CR_NPBLB)
  3843. /* enter first B0 block in polling mode (no DMA transfer for B0) */
  3844. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  3845. {
  3846. /* Enable the CRYP peripheral */
  3847. __HAL_CRYP_ENABLE();
  3848. inputaddr = (uint32_t)pInputData;
  3849. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3850. inputaddr+=4U;
  3851. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3852. inputaddr+=4U;
  3853. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3854. inputaddr+=4U;
  3855. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  3856. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  3857. {
  3858. hcryp->State = HAL_CRYP_STATE_READY;
  3859. __HAL_UNLOCK(hcryp);
  3860. return HAL_TIMEOUT;
  3861. }
  3862. /* Clear CCF Flag */
  3863. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3864. }
  3865. #endif
  3866. /* No header case */
  3867. if (hcryp->Init.Header == NULL)
  3868. {
  3869. hcryp->State = HAL_CRYP_STATE_READY;
  3870. /* Mark that the header phase is over */
  3871. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  3872. /* Process Unlocked */
  3873. __HAL_UNLOCK(hcryp);
  3874. return HAL_OK;
  3875. }
  3876. inputaddr = (uint32_t)hcryp->Init.Header;
  3877. if ((hcryp->Init.HeaderSize % 16U) != 0U)
  3878. {
  3879. if (hcryp->Init.HeaderSize < 16U)
  3880. {
  3881. CRYP_Padding(hcryp, (uint32_t) (hcryp->Init.HeaderSize), CRYP_POLLING_OFF);
  3882. hcryp->State = HAL_CRYP_STATE_READY;
  3883. /* Mark that the header phase is over */
  3884. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  3885. /* CCF flag indicating header phase AES processing completion
  3886. will be checked at the start of the next phase:
  3887. - payload phase (GCM / CCM when applicable)
  3888. - final phase (GMAC or CMAC). */
  3889. }
  3890. else
  3891. {
  3892. /* Local variable headerlength is a number of bytes multiple of 128 bits,
  3893. remaining header data (if any) are handled after this loop */
  3894. headerlength = (((hcryp->Init.HeaderSize)/16U)*16U) ;
  3895. /* Store the ending transfer point */
  3896. hcryp->pCrypInBuffPtr = hcryp->Init.Header + headerlength;
  3897. hcryp->CrypInCount = (uint32_t)(hcryp->Init.HeaderSize - headerlength); /* remainder */
  3898. /* Set the input and output addresses and start DMA transfer */
  3899. /* (incomplete DMA transfer, will be wrapped up after completion of
  3900. the first one (initiated here) with data padding */
  3901. CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, headerlength, 0U);
  3902. }
  3903. }
  3904. else
  3905. {
  3906. hcryp->CrypInCount = 0U;
  3907. /* Set the input address and start DMA transfer */
  3908. CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, hcryp->Init.HeaderSize, 0U);
  3909. }
  3910. }
  3911. /*============================================*/
  3912. /* GCM (or CCM when applicable) payload phase */
  3913. /*============================================*/
  3914. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  3915. {
  3916. /* Coming from header phase, wait for CCF flag to be raised
  3917. if header present and fed to the IP in the previous phase */
  3918. if (hcryp->Init.Header != NULL)
  3919. {
  3920. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  3921. {
  3922. hcryp->State = HAL_CRYP_STATE_READY;
  3923. __HAL_UNLOCK(hcryp);
  3924. return HAL_TIMEOUT;
  3925. }
  3926. }
  3927. else
  3928. {
  3929. /* Enable the Peripheral since wasn't in header phase (no header case) */
  3930. __HAL_CRYP_ENABLE();
  3931. }
  3932. /* Clear CCF Flag */
  3933. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3934. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
  3935. /* Specific handling to manage payload size less than 128 bits */
  3936. if ((Size % 16U) != 0U)
  3937. {
  3938. inputaddr = (uint32_t)pInputData;
  3939. outputaddr = (uint32_t)pOutputData;
  3940. if (Size < 16U)
  3941. {
  3942. /* Block is now entered in polling mode, no actual gain in resorting to DMA */
  3943. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  3944. hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
  3945. CRYP_Padding(hcryp, (uint32_t)Size, CRYP_POLLING_ON);
  3946. /* Change the CRYP state to ready */
  3947. hcryp->State = HAL_CRYP_STATE_READY;
  3948. /* Mark that the payload phase is over */
  3949. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  3950. /* Call output data transfer complete callback */
  3951. HAL_CRYP_OutCpltCallback(hcryp);
  3952. }
  3953. else
  3954. {
  3955. payloadlength = (Size/16U) * 16U;
  3956. /* Store the ending transfer points */
  3957. hcryp->pCrypInBuffPtr = pInputData + payloadlength;
  3958. hcryp->pCrypOutBuffPtr = pOutputData + payloadlength;
  3959. hcryp->CrypInCount = (uint32_t)(Size - payloadlength); /* remainder */
  3960. /* Set the input and output addresses and start DMA transfer */
  3961. /* (incomplete DMA transfer, will be wrapped up with data padding
  3962. after completion of the one initiated here) */
  3963. CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, payloadlength, outputaddr);
  3964. }
  3965. }
  3966. else
  3967. {
  3968. hcryp->CrypInCount = 0U;
  3969. inputaddr = (uint32_t)pInputData;
  3970. outputaddr = (uint32_t)pOutputData;
  3971. /* Set the input and output addresses and start DMA transfer */
  3972. CRYP_GCMCMAC_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  3973. }
  3974. }
  3975. /*====================================*/
  3976. /* GCM/GMAC or (CCM/)CMAC final phase */
  3977. /*====================================*/
  3978. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  3979. {
  3980. /* If coming from header phase (GMAC or CMAC case),
  3981. wait for CCF flag to be raised */
  3982. if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_HEADER_PHASE)
  3983. {
  3984. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  3985. {
  3986. hcryp->State = HAL_CRYP_STATE_READY;
  3987. __HAL_UNLOCK(hcryp);
  3988. return HAL_TIMEOUT;
  3989. }
  3990. /* Clear CCF Flag */
  3991. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  3992. }
  3993. tagaddr = (uint32_t)pOutputData;
  3994. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
  3995. /* if the header and payload phases have been bypassed, AES must be enabled again */
  3996. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  3997. {
  3998. __HAL_CRYP_ENABLE();
  3999. }
  4000. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  4001. {
  4002. headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
  4003. inputlength = Size * 8U; /* input length in bits */
  4004. /* Write the number of bits in the header on 64 bits followed by the number
  4005. of bits in the payload on 64 bits as well */
  4006. if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  4007. {
  4008. hcryp->Instance->DINR = __RBIT((headerlength)>>32U);
  4009. hcryp->Instance->DINR = __RBIT(headerlength);
  4010. hcryp->Instance->DINR = __RBIT((inputlength)>>32U);
  4011. hcryp->Instance->DINR = __RBIT(inputlength);
  4012. }
  4013. else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  4014. {
  4015. hcryp->Instance->DINR = __REV((headerlength)>>32U);
  4016. hcryp->Instance->DINR = __REV(headerlength);
  4017. hcryp->Instance->DINR = __REV((inputlength)>>32U);
  4018. hcryp->Instance->DINR = __REV(inputlength);
  4019. }
  4020. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  4021. {
  4022. hcryp->Instance->DINR = __ROR((headerlength)>>32U, 16U);
  4023. hcryp->Instance->DINR = __ROR(headerlength, 16U);
  4024. hcryp->Instance->DINR = __ROR((inputlength)>>32U, 16U);
  4025. hcryp->Instance->DINR = __ROR(inputlength, 16U);
  4026. }
  4027. else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
  4028. {
  4029. hcryp->Instance->DINR = (uint32_t)(headerlength>>32U);
  4030. hcryp->Instance->DINR = (uint32_t)(headerlength);
  4031. hcryp->Instance->DINR = (uint32_t)(inputlength>>32U);
  4032. hcryp->Instance->DINR = (uint32_t)(inputlength);
  4033. }
  4034. }
  4035. #if !defined(AES_CR_NPBLB)
  4036. else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  4037. {
  4038. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4039. inputaddr = (uint32_t)pInputData;
  4040. /* Enter the last block made of a 128-bit value formatted
  4041. from the original B0 packet. */
  4042. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4043. inputaddr+=4U;
  4044. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4045. inputaddr+=4U;
  4046. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4047. inputaddr+=4U;
  4048. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4049. inputaddr+=4U;
  4050. }
  4051. #endif
  4052. /* No DMA transfer is required at that point therefore, the software
  4053. just waits for the CCF flag to be raised. */
  4054. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  4055. {
  4056. hcryp->State = HAL_CRYP_STATE_READY;
  4057. __HAL_UNLOCK(hcryp);
  4058. return HAL_TIMEOUT;
  4059. }
  4060. /* Clear CCF Flag */
  4061. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4062. /* Read the Auth TAG in the IN FIFO */
  4063. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  4064. tagaddr+=4U;
  4065. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  4066. tagaddr+=4U;
  4067. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  4068. tagaddr+=4U;
  4069. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  4070. /* Mark that the final phase is over */
  4071. hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER;
  4072. hcryp->State = HAL_CRYP_STATE_READY;
  4073. /* Disable the Peripheral */
  4074. __HAL_CRYP_DISABLE();
  4075. }
  4076. /*=================================================*/
  4077. /* case incorrect hcryp->Init.GCMCMACPhase setting */
  4078. /*=================================================*/
  4079. else
  4080. {
  4081. hcryp->State = HAL_CRYP_STATE_ERROR;
  4082. __HAL_UNLOCK(hcryp);
  4083. return HAL_ERROR;
  4084. }
  4085. /* Process Unlocked */
  4086. __HAL_UNLOCK(hcryp);
  4087. return HAL_OK;
  4088. }
  4089. else
  4090. {
  4091. return HAL_BUSY;
  4092. }
  4093. }
  4094. /**
  4095. * @}
  4096. */
  4097. /** @defgroup CRYPEx_Exported_Functions_Group3 AES suspension/resumption functions
  4098. * @brief Extended processing functions.
  4099. *
  4100. @verbatim
  4101. ==============================================================================
  4102. ##### AES extended suspension and resumption functions #####
  4103. ==============================================================================
  4104. [..] This section provides functions allowing to:
  4105. (+) save in memory the Initialization Vector, the Key registers, the Control register or
  4106. the Suspend registers when a process is suspended by a higher priority message
  4107. (+) write back in CRYP hardware block the saved values listed above when the suspended
  4108. lower priority message processing is resumed.
  4109. @endverbatim
  4110. * @{
  4111. */
  4112. /**
  4113. * @brief In case of message processing suspension, read the Initialization Vector.
  4114. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4115. * the configuration information for CRYP module.
  4116. * @param Output: Pointer to the buffer containing the saved Initialization Vector.
  4117. * @note This value has to be stored for reuse by writing the AES_IVRx registers
  4118. * as soon as the interrupted processing has to be resumed.
  4119. * Applicable to all chaining modes.
  4120. * @note AES must be disabled when reading or resetting the IV values.
  4121. * @retval None
  4122. */
  4123. void HAL_CRYPEx_Read_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output)
  4124. {
  4125. uint32_t outputaddr = (uint32_t)Output;
  4126. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR3);
  4127. outputaddr+=4U;
  4128. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR2);
  4129. outputaddr+=4U;
  4130. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR1);
  4131. outputaddr+=4U;
  4132. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR0);
  4133. }
  4134. /**
  4135. * @brief In case of message processing resumption, rewrite the Initialization
  4136. * Vector in the AES_IVRx registers.
  4137. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4138. * the configuration information for CRYP module.
  4139. * @param Input: Pointer to the buffer containing the saved Initialization Vector to
  4140. * write back in the CRYP hardware block.
  4141. * @note Applicable to all chaining modes.
  4142. * @note AES must be disabled when reading or resetting the IV values.
  4143. * @retval None
  4144. */
  4145. void HAL_CRYPEx_Write_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input)
  4146. {
  4147. uint32_t ivaddr = (uint32_t)Input;
  4148. hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
  4149. ivaddr+=4U;
  4150. hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
  4151. ivaddr+=4U;
  4152. hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
  4153. ivaddr+=4U;
  4154. hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
  4155. }
  4156. /**
  4157. * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Suspend Registers.
  4158. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4159. * the configuration information for CRYP module.
  4160. * @param Output: Pointer to the buffer containing the saved Suspend Registers.
  4161. * @note These values have to be stored for reuse by writing back the AES_SUSPxR registers
  4162. * as soon as the interrupted processing has to be resumed.
  4163. * @retval None
  4164. */
  4165. void HAL_CRYPEx_Read_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output)
  4166. {
  4167. uint32_t outputaddr = (uint32_t)Output;
  4168. /* In case of GCM payload phase encryption, check that suspension can be carried out */
  4169. if (READ_BIT(hcryp->Instance->CR, (AES_CR_GCMPH|AES_CR_MODE)) == (CRYP_GCM_PAYLOAD_PHASE|CRYP_ALGOMODE_ENCRYPT))
  4170. {
  4171. /* Ensure that Busy flag is reset */
  4172. if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK)
  4173. {
  4174. hcryp->ErrorCode |= HAL_CRYP_BUSY_ERROR;
  4175. hcryp->State = HAL_CRYP_STATE_ERROR;
  4176. /* Process Unlocked */
  4177. __HAL_UNLOCK(hcryp);
  4178. HAL_CRYP_ErrorCallback(hcryp);
  4179. return ;
  4180. }
  4181. }
  4182. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP7R);
  4183. outputaddr+=4U;
  4184. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP6R);
  4185. outputaddr+=4U;
  4186. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP5R);
  4187. outputaddr+=4U;
  4188. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP4R);
  4189. outputaddr+=4U;
  4190. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP3R);
  4191. outputaddr+=4U;
  4192. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP2R);
  4193. outputaddr+=4U;
  4194. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP1R);
  4195. outputaddr+=4U;
  4196. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP0R);
  4197. }
  4198. /**
  4199. * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Suspend
  4200. * Registers in the AES_SUSPxR registers.
  4201. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4202. * the configuration information for CRYP module.
  4203. * @param Input: Pointer to the buffer containing the saved suspend registers to
  4204. * write back in the CRYP hardware block.
  4205. * @retval None
  4206. */
  4207. void HAL_CRYPEx_Write_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input)
  4208. {
  4209. uint32_t ivaddr = (uint32_t)Input;
  4210. hcryp->Instance->SUSP7R = __REV(*(uint32_t*)(ivaddr));
  4211. ivaddr+=4U;
  4212. hcryp->Instance->SUSP6R = __REV(*(uint32_t*)(ivaddr));
  4213. ivaddr+=4U;
  4214. hcryp->Instance->SUSP5R = __REV(*(uint32_t*)(ivaddr));
  4215. ivaddr+=4U;
  4216. hcryp->Instance->SUSP4R = __REV(*(uint32_t*)(ivaddr));
  4217. ivaddr+=4U;
  4218. hcryp->Instance->SUSP3R = __REV(*(uint32_t*)(ivaddr));
  4219. ivaddr+=4U;
  4220. hcryp->Instance->SUSP2R = __REV(*(uint32_t*)(ivaddr));
  4221. ivaddr+=4U;
  4222. hcryp->Instance->SUSP1R = __REV(*(uint32_t*)(ivaddr));
  4223. ivaddr+=4U;
  4224. hcryp->Instance->SUSP0R = __REV(*(uint32_t*)(ivaddr));
  4225. }
  4226. /**
  4227. * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Key Registers.
  4228. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4229. * the configuration information for CRYP module.
  4230. * @param Output: Pointer to the buffer containing the saved Key Registers.
  4231. * @param KeySize: Indicates the key size (128 or 256 bits).
  4232. * @note These values have to be stored for reuse by writing back the AES_KEYRx registers
  4233. * as soon as the interrupted processing has to be resumed.
  4234. * @retval None
  4235. */
  4236. void HAL_CRYPEx_Read_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t KeySize)
  4237. {
  4238. uint32_t keyaddr = (uint32_t)Output;
  4239. if (KeySize == CRYP_KEYSIZE_256B)
  4240. {
  4241. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR7);
  4242. keyaddr+=4U;
  4243. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR6);
  4244. keyaddr+=4U;
  4245. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR5);
  4246. keyaddr+=4U;
  4247. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR4);
  4248. keyaddr+=4U;
  4249. }
  4250. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR3);
  4251. keyaddr+=4U;
  4252. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR2);
  4253. keyaddr+=4U;
  4254. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR1);
  4255. keyaddr+=4U;
  4256. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR0);
  4257. }
  4258. /**
  4259. * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Key
  4260. * Registers in the AES_KEYRx registers.
  4261. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4262. * the configuration information for CRYP module.
  4263. * @param Input: Pointer to the buffer containing the saved key registers to
  4264. * write back in the CRYP hardware block.
  4265. * @param KeySize: Indicates the key size (128 or 256 bits)
  4266. * @retval None
  4267. */
  4268. void HAL_CRYPEx_Write_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint32_t KeySize)
  4269. {
  4270. uint32_t keyaddr = (uint32_t)Input;
  4271. if (KeySize == CRYP_KEYSIZE_256B)
  4272. {
  4273. hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
  4274. keyaddr+=4U;
  4275. hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
  4276. keyaddr+=4U;
  4277. hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
  4278. keyaddr+=4U;
  4279. hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
  4280. keyaddr+=4U;
  4281. }
  4282. hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
  4283. keyaddr+=4U;
  4284. hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
  4285. keyaddr+=4U;
  4286. hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
  4287. keyaddr+=4U;
  4288. hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
  4289. }
  4290. /**
  4291. * @brief In case of message GCM/GMAC or CMAC processing suspension, read the Control Register.
  4292. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4293. * the configuration information for CRYP module.
  4294. * @param Output: Pointer to the buffer containing the saved Control Register.
  4295. * @note This values has to be stored for reuse by writing back the AES_CR register
  4296. * as soon as the interrupted processing has to be resumed.
  4297. * @retval None
  4298. */
  4299. void HAL_CRYPEx_Read_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Output)
  4300. {
  4301. *(uint32_t*)(Output) = hcryp->Instance->CR;
  4302. }
  4303. /**
  4304. * @brief In case of message GCM/GMAC or CMAC processing resumption, rewrite the Control
  4305. * Registers in the AES_CR register.
  4306. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4307. * the configuration information for CRYP module.
  4308. * @param Input: Pointer to the buffer containing the saved Control Register to
  4309. * write back in the CRYP hardware block.
  4310. * @retval None
  4311. */
  4312. void HAL_CRYPEx_Write_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Input)
  4313. {
  4314. hcryp->Instance->CR = *(uint32_t*)(Input);
  4315. /* At the same time, set handle state back to READY to be able to resume the AES calculations
  4316. without the processing APIs returning HAL_BUSY when called. */
  4317. hcryp->State = HAL_CRYP_STATE_READY;
  4318. }
  4319. /**
  4320. * @brief Request CRYP processing suspension when in polling or interruption mode.
  4321. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4322. * the configuration information for CRYP module.
  4323. * @note Set the handle field SuspendRequest to the appropriate value so that
  4324. * the on-going CRYP processing is suspended as soon as the required
  4325. * conditions are met.
  4326. * @note It is advised not to suspend the CRYP processing when the DMA controller
  4327. * is managing the data transfer
  4328. * @retval None
  4329. */
  4330. void HAL_CRYPEx_ProcessSuspend(CRYP_HandleTypeDef *hcryp)
  4331. {
  4332. /* Set Handle Suspend Request field */
  4333. hcryp->SuspendRequest = HAL_CRYP_SUSPEND;
  4334. }
  4335. /**
  4336. * @}
  4337. */
  4338. /**
  4339. * @}
  4340. */
  4341. /** @addtogroup CRYPEx_Private_Functions
  4342. * @{
  4343. */
  4344. /**
  4345. * @brief DMA CRYP Input Data process complete callback
  4346. * for GCM, GMAC or CMAC chainging modes.
  4347. * @note Specific setting of hcryp fields are required only
  4348. * in the case of header phase where no output data DMA
  4349. * transfer is on-going (only input data transfer is enabled
  4350. * in such a case).
  4351. * @param hdma: DMA handle.
  4352. * @retval None
  4353. */
  4354. static void CRYP_GCMCMAC_DMAInCplt(DMA_HandleTypeDef *hdma)
  4355. {
  4356. uint32_t difflength = 0U;
  4357. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  4358. /* Disable the DMA transfer for input request */
  4359. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  4360. if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  4361. {
  4362. if (hcryp->CrypInCount != 0U)
  4363. {
  4364. /* Last block is now entered in polling mode, no actual gain in resorting to DMA */
  4365. difflength = hcryp->CrypInCount;
  4366. hcryp->CrypInCount = 0U;
  4367. CRYP_Padding(hcryp, difflength, CRYP_POLLING_OFF);
  4368. }
  4369. hcryp->State = HAL_CRYP_STATE_READY;
  4370. /* Mark that the header phase is over */
  4371. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  4372. }
  4373. /* CCF flag indicating header phase AES processing completion
  4374. will be checked at the start of the next phase:
  4375. - payload phase (GCM or CCM when applicable)
  4376. - final phase (GMAC or CMAC).
  4377. This allows to avoid the Wait on Flag within the IRQ handling. */
  4378. /* Call input data transfer complete callback */
  4379. HAL_CRYP_InCpltCallback(hcryp);
  4380. }
  4381. /**
  4382. * @brief DMA CRYP Output Data process complete callback
  4383. * for GCM, GMAC or CMAC chainging modes.
  4384. * @note This callback is called only in the payload phase.
  4385. * @param hdma: DMA handle.
  4386. * @retval None
  4387. */
  4388. static void CRYP_GCMCMAC_DMAOutCplt(DMA_HandleTypeDef *hdma)
  4389. {
  4390. uint32_t difflength = 0U;
  4391. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  4392. /* Disable the DMA transfer for output request */
  4393. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
  4394. /* Clear CCF Flag */
  4395. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4396. /* Initiate additional transfer to wrap-up data feeding to the IP */
  4397. if (hcryp->CrypInCount != 0U)
  4398. {
  4399. /* Last block is now entered in polling mode, no actual gain in resorting to DMA */
  4400. difflength = hcryp->CrypInCount;
  4401. hcryp->CrypInCount = 0U;
  4402. CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
  4403. }
  4404. /* Change the CRYP state to ready */
  4405. hcryp->State = HAL_CRYP_STATE_READY;
  4406. /* Mark that the payload phase is over */
  4407. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  4408. /* Call output data transfer complete callback */
  4409. HAL_CRYP_OutCpltCallback(hcryp);
  4410. }
  4411. /**
  4412. * @brief DMA CRYP communication error callback
  4413. * for GCM, GMAC or CMAC chainging modes.
  4414. * @param hdma: DMA handle
  4415. * @retval None
  4416. */
  4417. static void CRYP_GCMCMAC_DMAError(DMA_HandleTypeDef *hdma)
  4418. {
  4419. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  4420. hcryp->State= HAL_CRYP_STATE_ERROR;
  4421. hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR;
  4422. HAL_CRYP_ErrorCallback(hcryp);
  4423. /* Clear Error Flag */
  4424. __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR);
  4425. }
  4426. /**
  4427. * @brief Handle CRYP block input/output data handling under interruption
  4428. * for GCM, GMAC or CMAC chaining modes.
  4429. * @note The function is called under interruption only, once
  4430. * interruptions have been enabled by HAL_CRYPEx_AES_Auth_IT().
  4431. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4432. * the configuration information for CRYP module
  4433. * @retval HAL status
  4434. */
  4435. HAL_StatusTypeDef CRYP_AES_Auth_IT(CRYP_HandleTypeDef *hcryp)
  4436. {
  4437. uint32_t inputaddr = 0x0U;
  4438. uint32_t outputaddr = 0x0U;
  4439. uint32_t index = 0x0U;
  4440. uint32_t addhoc_process = 0U;
  4441. uint32_t difflength = 0U;
  4442. uint32_t difflengthmod4 = 0U;
  4443. uint32_t mask[3] = {0x0FFU, 0x0FFFFU, 0x0FFFFFFU};
  4444. uint32_t intermediate_data[4U] = {0U};
  4445. if(hcryp->State == HAL_CRYP_STATE_BUSY)
  4446. {
  4447. /*===========================*/
  4448. /* GCM/GMAC(/CCM) init phase */
  4449. /*===========================*/
  4450. if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
  4451. {
  4452. /* Clear Computation Complete Flag */
  4453. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4454. /* Disable Computation Complete Flag and Errors Interrupts */
  4455. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4456. /* Change the CRYP state */
  4457. hcryp->State = HAL_CRYP_STATE_READY;
  4458. /* Mark that the initialization phase is over */
  4459. hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER;
  4460. /* Process Unlocked */
  4461. __HAL_UNLOCK(hcryp);
  4462. /* Call computation complete callback */
  4463. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  4464. return HAL_OK;
  4465. }
  4466. /*=====================================*/
  4467. /* GCM/GMAC or (CCM/)CMAC header phase */
  4468. /*=====================================*/
  4469. else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  4470. {
  4471. /* Check if all input header data have been entered */
  4472. if (hcryp->CrypInCount == 0U)
  4473. {
  4474. /* Clear Computation Complete Flag */
  4475. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4476. /* Disable Computation Complete Flag and Errors Interrupts */
  4477. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4478. /* Change the CRYP state */
  4479. hcryp->State = HAL_CRYP_STATE_READY;
  4480. /* Mark that the header phase is over */
  4481. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  4482. /* Process Unlocked */
  4483. __HAL_UNLOCK(hcryp);
  4484. /* Call computation complete callback */
  4485. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  4486. return HAL_OK;
  4487. }
  4488. /* If suspension flag has been raised, suspend processing */
  4489. else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  4490. {
  4491. /* Clear CCF Flag */
  4492. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4493. /* reset SuspendRequest */
  4494. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  4495. /* Disable Computation Complete Flag and Errors Interrupts */
  4496. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4497. /* Change the CRYP state */
  4498. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  4499. /* Mark that the header phase is over */
  4500. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  4501. /* Process Unlocked */
  4502. __HAL_UNLOCK(hcryp);
  4503. return HAL_OK;
  4504. }
  4505. else /* Carry on feeding input data to the CRYP hardware block */
  4506. {
  4507. /* Clear Computation Complete Flag */
  4508. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4509. /* Get the last Input data address */
  4510. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  4511. /* Increment/decrement instance pointer/counter */
  4512. if (hcryp->CrypInCount < 16U)
  4513. {
  4514. difflength = hcryp->CrypInCount;
  4515. hcryp->CrypInCount = 0U;
  4516. addhoc_process = 1U;
  4517. difflengthmod4 = difflength%4U;
  4518. }
  4519. else
  4520. {
  4521. hcryp->pCrypInBuffPtr += 16U;
  4522. hcryp->CrypInCount -= 16U;
  4523. }
  4524. #if defined(AES_CR_NPBLB)
  4525. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM_CMAC)
  4526. #else
  4527. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  4528. #endif
  4529. {
  4530. if (hcryp->CrypInCount == hcryp->Init.HeaderSize)
  4531. {
  4532. /* All B blocks will have been entered after the next
  4533. four DINR writing, so point at header buffer for
  4534. the next iteration */
  4535. hcryp->pCrypInBuffPtr = hcryp->Init.Header;
  4536. }
  4537. }
  4538. /* Write the Input block in the Data Input register */
  4539. if (addhoc_process == 0U)
  4540. {
  4541. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4542. inputaddr+=4U;
  4543. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4544. inputaddr+=4U;
  4545. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4546. inputaddr+=4U;
  4547. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4548. }
  4549. else
  4550. {
  4551. /* Header remainder has size less than 128 bits */
  4552. /* Enter complete words when possible */
  4553. for(index=0U; index < (difflength/4U); index ++)
  4554. {
  4555. /* Write the Input block in the Data Input register */
  4556. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4557. inputaddr+=4U;
  4558. }
  4559. /* Enter incomplete word padded with zeroes if applicable
  4560. (case of header length not a multiple of 32-bits) */
  4561. if (difflengthmod4 != 0U)
  4562. {
  4563. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1]);
  4564. }
  4565. /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
  4566. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  4567. {
  4568. hcryp->Instance->DINR = 0U;
  4569. }
  4570. }
  4571. return HAL_OK;
  4572. }
  4573. }
  4574. /*=======================*/
  4575. /* GCM/CCM payload phase */
  4576. /*=======================*/
  4577. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  4578. {
  4579. /* Get the last output data address */
  4580. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  4581. /* Specific handling to manage payload size less than 128 bits
  4582. when GCM (or CCM when applicable) encryption or decryption is selected.
  4583. Check here if the last block output data are read */
  4584. #if defined(AES_CR_NPBLB)
  4585. if ((hcryp->CrypOutCount < 16U) && \
  4586. (hcryp->CrypOutCount > 0U))
  4587. #else
  4588. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) && \
  4589. (hcryp->CrypOutCount < 16U) && \
  4590. (hcryp->CrypOutCount > 0U))
  4591. #endif
  4592. {
  4593. addhoc_process = 1U;
  4594. difflength = hcryp->CrypOutCount;
  4595. difflengthmod4 = difflength%4U;
  4596. hcryp->CrypOutCount = 0U; /* mark that no more output data will be needed */
  4597. /* Retrieve intermediate data */
  4598. for(index=0U; index < 4U; index ++)
  4599. {
  4600. intermediate_data[index] = hcryp->Instance->DOUTR;
  4601. }
  4602. /* Retrieve last words of cyphered data */
  4603. /* First, retrieve complete output words */
  4604. for(index=0U; index < (difflength/4U); index ++)
  4605. {
  4606. *(uint32_t*)(outputaddr) = intermediate_data[index];
  4607. outputaddr+=4U;
  4608. }
  4609. /* Next, retrieve partial output word if applicable;
  4610. at the same time, start masking intermediate data
  4611. with a mask of zeros of same size than the padding
  4612. applied to the last block of payload */
  4613. if (difflengthmod4 != 0U)
  4614. {
  4615. intermediate_data[difflength/4U] &= mask[difflengthmod4-1U];
  4616. *(uint32_t*)(outputaddr) = intermediate_data[difflength/4U];
  4617. }
  4618. #if !defined(AES_CR_NPBLB)
  4619. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  4620. {
  4621. /* Change again CHMOD configuration to GCM mode */
  4622. __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_GCM_GMAC);
  4623. /* Select FINAL phase */
  4624. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_FINAL_PHASE);
  4625. /* Before inserting the intermediate data, carry on masking operation
  4626. with a mask of zeros of same size than the padding applied to the last block of payload */
  4627. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  4628. {
  4629. intermediate_data[(difflength+3U)/4U+index] = 0U;
  4630. }
  4631. /* Insert intermediate data to trigger an additional DOUTR reading round */
  4632. /* Clear Computation Complete Flag before entering new block */
  4633. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4634. for(index=0U; index < 4U; index ++)
  4635. {
  4636. hcryp->Instance->DINR = intermediate_data[index];
  4637. }
  4638. }
  4639. else
  4640. #endif
  4641. {
  4642. /* Payload phase is now over */
  4643. /* Clear Computation Complete Flag */
  4644. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4645. /* Disable Computation Complete Flag and Errors Interrupts */
  4646. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4647. /* Change the CRYP state */
  4648. hcryp->State = HAL_CRYP_STATE_READY;
  4649. /* Mark that the payload phase is over */
  4650. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  4651. /* Process Unlocked */
  4652. __HAL_UNLOCK(hcryp);
  4653. /* Call computation complete callback */
  4654. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  4655. }
  4656. return HAL_OK;
  4657. }
  4658. else
  4659. {
  4660. if (hcryp->CrypOutCount != 0U)
  4661. {
  4662. /* Usual case (different than GCM/CCM last block < 128 bits ciphering) */
  4663. /* Retrieve the last block available from the CRYP hardware block:
  4664. read the output block from the Data Output Register */
  4665. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4666. outputaddr+=4U;
  4667. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4668. outputaddr+=4U;
  4669. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4670. outputaddr+=4U;
  4671. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4672. /* Increment/decrement instance pointer/counter */
  4673. hcryp->pCrypOutBuffPtr += 16U;
  4674. hcryp->CrypOutCount -= 16U;
  4675. }
  4676. #if !defined(AES_CR_NPBLB)
  4677. else
  4678. {
  4679. /* Software work-around: additional DOUTR reading round to discard the data */
  4680. for(index=0U; index < 4U; index ++)
  4681. {
  4682. intermediate_data[index] = hcryp->Instance->DOUTR;
  4683. }
  4684. }
  4685. #endif
  4686. }
  4687. /* Check if all output text has been retrieved */
  4688. if (hcryp->CrypOutCount == 0U)
  4689. {
  4690. #if !defined(AES_CR_NPBLB)
  4691. /* Make sure that software-work around is not running before disabling
  4692. the interruptions (indeed, if software work-around is running, the
  4693. interruptions must not be disabled to allow the additional DOUTR
  4694. reading round */
  4695. if (addhoc_process == 0U)
  4696. #endif
  4697. {
  4698. /* Clear Computation Complete Flag */
  4699. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4700. /* Disable Computation Complete Flag and Errors Interrupts */
  4701. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4702. /* Change the CRYP state */
  4703. hcryp->State = HAL_CRYP_STATE_READY;
  4704. /* Mark that the payload phase is over */
  4705. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  4706. /* Process Unlocked */
  4707. __HAL_UNLOCK(hcryp);
  4708. /* Call computation complete callback */
  4709. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  4710. }
  4711. return HAL_OK;
  4712. }
  4713. /* If suspension flag has been raised, suspend processing */
  4714. else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  4715. {
  4716. /* Clear CCF Flag */
  4717. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4718. /* reset SuspendRequest */
  4719. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  4720. /* Disable Computation Complete Flag and Errors Interrupts */
  4721. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4722. /* Change the CRYP state */
  4723. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  4724. /* Mark that the header phase is over */
  4725. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  4726. /* Process Unlocked */
  4727. __HAL_UNLOCK(hcryp);
  4728. return HAL_OK;
  4729. }
  4730. else /* Output data are still expected, carry on feeding the CRYP
  4731. hardware block with input data */
  4732. {
  4733. /* Clear Computation Complete Flag */
  4734. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4735. /* Get the last Input data address */
  4736. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  4737. /* Usual input data feeding case */
  4738. if (hcryp->CrypInCount < 16U)
  4739. {
  4740. difflength = (uint32_t) (hcryp->CrypInCount);
  4741. difflengthmod4 = difflength%4U;
  4742. hcryp->CrypInCount = 0U;
  4743. #if defined(AES_CR_NPBLB)
  4744. /* In case of GCM encryption or CCM decryption, specify the number of padding
  4745. bytes in last block of payload */
  4746. if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC)
  4747. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT))
  4748. || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM_CMAC)
  4749. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT)))
  4750. {
  4751. /* Set NPBLB field in writing the number of padding bytes
  4752. for the last block of payload */
  4753. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 16U - difflength);
  4754. }
  4755. #else
  4756. /* Software workaround applied to GCM encryption only */
  4757. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  4758. {
  4759. /* Change the mode configured in CHMOD bits of CR register to select CTR mode */
  4760. __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_CTR);
  4761. }
  4762. #endif
  4763. /* Insert the last block (which size is inferior to 128 bits) padded with zeroes
  4764. to have a complete block of 128 bits */
  4765. for(index=0U; index < (difflength/4U); index ++)
  4766. {
  4767. /* Write the Input block in the Data Input register */
  4768. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4769. inputaddr+=4U;
  4770. }
  4771. /* If required, manage input data size not multiple of 32 bits */
  4772. if (difflengthmod4 != 0U)
  4773. {
  4774. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1U]);
  4775. }
  4776. /* Wrap-up in padding with zero-words if applicable */
  4777. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  4778. {
  4779. hcryp->Instance->DINR = 0U;
  4780. }
  4781. }
  4782. else
  4783. {
  4784. hcryp->pCrypInBuffPtr += 16U;
  4785. hcryp->CrypInCount -= 16U;
  4786. /* Write the Input block in the Data Input register */
  4787. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4788. inputaddr+=4U;
  4789. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4790. inputaddr+=4U;
  4791. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4792. inputaddr+=4U;
  4793. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4794. }
  4795. return HAL_OK;
  4796. }
  4797. }
  4798. /*====================================*/
  4799. /* GCM/GMAC or (CCM/)CMAC final phase */
  4800. /*====================================*/
  4801. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  4802. {
  4803. /* Clear Computation Complete Flag */
  4804. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4805. /* Get the last output data address */
  4806. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  4807. /* Retrieve the last expected data from the CRYP hardware block:
  4808. read the output block from the Data Output Register */
  4809. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4810. outputaddr+=4U;
  4811. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4812. outputaddr+=4U;
  4813. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4814. outputaddr+=4U;
  4815. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4816. /* Disable Computation Complete Flag and Errors Interrupts */
  4817. __HAL_CRYP_DISABLE_IT(CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  4818. /* Change the CRYP state */
  4819. hcryp->State = HAL_CRYP_STATE_READY;
  4820. /* Mark that the header phase is over */
  4821. hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER;
  4822. /* Disable the Peripheral */
  4823. __HAL_CRYP_DISABLE();
  4824. /* Process Unlocked */
  4825. __HAL_UNLOCK(hcryp);
  4826. /* Call computation complete callback */
  4827. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  4828. return HAL_OK;
  4829. }
  4830. else
  4831. {
  4832. /* Clear Computation Complete Flag */
  4833. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4834. hcryp->State = HAL_CRYP_STATE_ERROR;
  4835. __HAL_UNLOCK(hcryp);
  4836. return HAL_ERROR;
  4837. }
  4838. }
  4839. else
  4840. {
  4841. return HAL_BUSY;
  4842. }
  4843. }
  4844. /**
  4845. * @brief Set the DMA configuration and start the DMA transfer
  4846. * for GCM, GMAC or CMAC chainging modes.
  4847. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4848. * the configuration information for CRYP module.
  4849. * @param inputaddr: Address of the Input buffer.
  4850. * @param Size: Size of the Input buffer un bytes, must be a multiple of 16.
  4851. * @param outputaddr: Address of the Output buffer, null pointer when no output DMA stream
  4852. * has to be configured.
  4853. * @retval None
  4854. */
  4855. static void CRYP_GCMCMAC_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  4856. {
  4857. /* Set the input CRYP DMA transfer complete callback */
  4858. hcryp->hdmain->XferCpltCallback = CRYP_GCMCMAC_DMAInCplt;
  4859. /* Set the DMA error callback */
  4860. hcryp->hdmain->XferErrorCallback = CRYP_GCMCMAC_DMAError;
  4861. if (outputaddr != 0U)
  4862. {
  4863. /* Set the output CRYP DMA transfer complete callback */
  4864. hcryp->hdmaout->XferCpltCallback = CRYP_GCMCMAC_DMAOutCplt;
  4865. /* Set the DMA error callback */
  4866. hcryp->hdmaout->XferErrorCallback = CRYP_GCMCMAC_DMAError;
  4867. }
  4868. /* Enable the CRYP peripheral */
  4869. __HAL_CRYP_ENABLE();
  4870. /* Enable the DMA input stream */
  4871. HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4U);
  4872. /* Enable the DMA input request */
  4873. SET_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  4874. if (outputaddr != 0U)
  4875. {
  4876. /* Enable the DMA output stream */
  4877. HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4U);
  4878. /* Enable the DMA output request */
  4879. SET_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
  4880. }
  4881. }
  4882. /**
  4883. * @brief Write/read input/output data in polling mode.
  4884. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4885. * the configuration information for CRYP module.
  4886. * @param Input: Pointer to the Input buffer.
  4887. * @param Ilength: Length of the Input buffer in bytes, must be a multiple of 16.
  4888. * @param Output: Pointer to the returned buffer.
  4889. * @param Timeout: Specify Timeout value.
  4890. * @retval HAL status
  4891. */
  4892. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
  4893. {
  4894. uint32_t index = 0U;
  4895. uint32_t inputaddr = (uint32_t)Input;
  4896. uint32_t outputaddr = (uint32_t)Output;
  4897. for(index=0U; (index < Ilength); index += 16U)
  4898. {
  4899. /* Write the Input block in the Data Input register */
  4900. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4901. inputaddr+=4U;
  4902. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4903. inputaddr+=4U;
  4904. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4905. inputaddr+=4U;
  4906. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  4907. inputaddr+=4U;
  4908. /* Wait for CCF flag to be raised */
  4909. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  4910. {
  4911. hcryp->State = HAL_CRYP_STATE_READY;
  4912. __HAL_UNLOCK(hcryp);
  4913. return HAL_TIMEOUT;
  4914. }
  4915. /* Clear CCF Flag */
  4916. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  4917. /* Read the Output block from the Data Output Register */
  4918. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4919. outputaddr+=4U;
  4920. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4921. outputaddr+=4U;
  4922. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4923. outputaddr+=4U;
  4924. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  4925. outputaddr+=4U;
  4926. /* If the suspension flag has been raised and if the processing is not about
  4927. to end, suspend processing */
  4928. if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < Ilength))
  4929. {
  4930. /* Reset SuspendRequest */
  4931. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  4932. /* Save current reading and writing locations of Input and Output buffers */
  4933. hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
  4934. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  4935. /* Save the number of bytes that remain to be processed at this point */
  4936. hcryp->CrypInCount = Ilength - (index+16U);
  4937. /* Change the CRYP state */
  4938. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  4939. return HAL_OK;
  4940. }
  4941. }
  4942. /* Return function status */
  4943. return HAL_OK;
  4944. }
  4945. /**
  4946. * @brief Read derivative key in polling mode when CRYP hardware block is set
  4947. * in key derivation operating mode (mode 2).
  4948. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4949. * the configuration information for CRYP module.
  4950. * @param Output: Pointer to the returned buffer.
  4951. * @param Timeout: Specify Timeout value.
  4952. * @retval HAL status
  4953. */
  4954. static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout)
  4955. {
  4956. uint32_t outputaddr = (uint32_t)Output;
  4957. /* Wait for CCF flag to be raised */
  4958. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  4959. {
  4960. hcryp->State = HAL_CRYP_STATE_READY;
  4961. __HAL_UNLOCK(hcryp);
  4962. return HAL_TIMEOUT;
  4963. }
  4964. /* Clear CCF Flag */
  4965. __HAL_CRYP_CLEAR_FLAG( CRYP_CCF_CLEAR);
  4966. /* Read the derivative key from the AES_KEYRx registers */
  4967. if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
  4968. {
  4969. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
  4970. outputaddr+=4U;
  4971. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
  4972. outputaddr+=4U;
  4973. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
  4974. outputaddr+=4U;
  4975. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
  4976. outputaddr+=4U;
  4977. }
  4978. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
  4979. outputaddr+=4U;
  4980. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
  4981. outputaddr+=4U;
  4982. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
  4983. outputaddr+=4U;
  4984. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
  4985. /* Return function status */
  4986. return HAL_OK;
  4987. }
  4988. /**
  4989. * @brief Set the DMA configuration and start the DMA transfer.
  4990. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  4991. * the configuration information for CRYP module.
  4992. * @param inputaddr: Address of the Input buffer.
  4993. * @param Size: Size of the Input buffer in bytes, must be a multiple of 16.
  4994. * @param outputaddr: Address of the Output buffer.
  4995. * @retval None
  4996. */
  4997. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  4998. {
  4999. /* Set the CRYP DMA transfer complete callback */
  5000. hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
  5001. /* Set the DMA error callback */
  5002. hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
  5003. /* Set the CRYP DMA transfer complete callback */
  5004. hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
  5005. /* Set the DMA error callback */
  5006. hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
  5007. /* Enable the DMA input stream */
  5008. HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4U);
  5009. /* Enable the DMA output stream */
  5010. HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4U);
  5011. /* Enable In and Out DMA requests */
  5012. SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN));
  5013. /* Enable the CRYP peripheral */
  5014. __HAL_CRYP_ENABLE();
  5015. }
  5016. /**
  5017. * @brief Handle CRYP hardware block Timeout when waiting for CCF flag to be raised.
  5018. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  5019. * the configuration information for CRYP module.
  5020. * @param Timeout: Timeout duration.
  5021. * @retval HAL status
  5022. */
  5023. static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  5024. {
  5025. uint32_t tickstart = 0U;
  5026. /* Get timeout */
  5027. tickstart = HAL_GetTick();
  5028. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
  5029. {
  5030. /* Check for the Timeout */
  5031. if(Timeout != HAL_MAX_DELAY)
  5032. {
  5033. if((HAL_GetTick() - tickstart ) > Timeout)
  5034. {
  5035. return HAL_TIMEOUT;
  5036. }
  5037. }
  5038. }
  5039. return HAL_OK;
  5040. }
  5041. /**
  5042. * @brief Wait for Busy Flag to be reset during a GCM payload encryption process suspension.
  5043. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  5044. * the configuration information for CRYP module.
  5045. * @param Timeout: Timeout duration.
  5046. * @retval HAL status
  5047. */
  5048. static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  5049. {
  5050. uint32_t tickstart = 0U;
  5051. /* Get timeout */
  5052. tickstart = HAL_GetTick();
  5053. while(HAL_IS_BIT_SET(hcryp->Instance->SR, AES_SR_BUSY))
  5054. {
  5055. /* Check for the Timeout */
  5056. if(Timeout != HAL_MAX_DELAY)
  5057. {
  5058. if((HAL_GetTick() - tickstart ) > Timeout)
  5059. {
  5060. return HAL_TIMEOUT;
  5061. }
  5062. }
  5063. }
  5064. return HAL_OK;
  5065. }
  5066. /**
  5067. * @brief DMA CRYP Input Data process complete callback.
  5068. * @param hdma: DMA handle.
  5069. * @retval None
  5070. */
  5071. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
  5072. {
  5073. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  5074. /* Disable the DMA transfer for input request */
  5075. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  5076. /* Call input data transfer complete callback */
  5077. HAL_CRYP_InCpltCallback(hcryp);
  5078. }
  5079. /**
  5080. * @brief DMA CRYP Output Data process complete callback.
  5081. * @param hdma: DMA handle.
  5082. * @retval None
  5083. */
  5084. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
  5085. {
  5086. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  5087. /* Disable the DMA transfer for output request */
  5088. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
  5089. /* Clear CCF Flag */
  5090. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  5091. /* Disable CRYP */
  5092. __HAL_CRYP_DISABLE();
  5093. /* Change the CRYP state to ready */
  5094. hcryp->State = HAL_CRYP_STATE_READY;
  5095. /* Call output data transfer complete callback */
  5096. HAL_CRYP_OutCpltCallback(hcryp);
  5097. }
  5098. /**
  5099. * @brief DMA CRYP communication error callback.
  5100. * @param hdma: DMA handle.
  5101. * @retval None
  5102. */
  5103. static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
  5104. {
  5105. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  5106. hcryp->State= HAL_CRYP_STATE_ERROR;
  5107. hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR;
  5108. HAL_CRYP_ErrorCallback(hcryp);
  5109. /* Clear Error Flag */
  5110. __HAL_CRYP_CLEAR_FLAG(CRYP_ERR_CLEAR);
  5111. }
  5112. /**
  5113. * @brief Last header or payload block padding when size is not a multiple of 128 bits.
  5114. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  5115. * the configuration information for CRYP module.
  5116. * @param difflength: size remainder after having fed all complete 128-bit blocks.
  5117. * @param polling: specifies whether or not polling on CCF must be done after having
  5118. * entered a complete block.
  5119. * @retval None
  5120. */
  5121. static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling)
  5122. {
  5123. uint32_t index = 0U;
  5124. uint32_t difflengthmod4 = difflength%4U;
  5125. uint32_t inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  5126. uint32_t outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  5127. uint32_t mask[3U] = {0x0FFU, 0x0FFFFU, 0x0FFFFFFU};
  5128. uint32_t intermediate_data[4U] = {0U};
  5129. #if defined(AES_CR_NPBLB)
  5130. /* In case of GCM encryption or CCM decryption, specify the number of padding
  5131. bytes in last block of payload */
  5132. if (READ_BIT(hcryp->Instance->CR,AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE)
  5133. {
  5134. if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC)
  5135. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT))
  5136. || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM_CMAC)
  5137. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT)))
  5138. {
  5139. /* Set NPBLB field in writing the number of padding bytes
  5140. for the last block of payload */
  5141. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, 16U - difflength);
  5142. }
  5143. }
  5144. #else
  5145. /* Software workaround applied to GCM encryption only */
  5146. if ((hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) &&
  5147. (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT))
  5148. {
  5149. /* Change the mode configured in CHMOD bits of CR register to select CTR mode */
  5150. __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_CTR);
  5151. }
  5152. #endif
  5153. /* Wrap-up entering header or payload data */
  5154. /* Enter complete words when possible */
  5155. for(index=0U; index < (difflength/4U); index ++)
  5156. {
  5157. /* Write the Input block in the Data Input register */
  5158. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  5159. inputaddr+=4U;
  5160. }
  5161. /* Enter incomplete word padded with zeroes if applicable
  5162. (case of header length not a multiple of 32-bits) */
  5163. if (difflengthmod4 != 0U)
  5164. {
  5165. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[difflengthmod4-1]);
  5166. }
  5167. /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
  5168. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  5169. {
  5170. hcryp->Instance->DINR = 0U;
  5171. }
  5172. if (polling == CRYP_POLLING_ON)
  5173. {
  5174. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  5175. {
  5176. hcryp->State = HAL_CRYP_STATE_READY;
  5177. __HAL_UNLOCK(hcryp);
  5178. HAL_CRYP_ErrorCallback(hcryp);
  5179. }
  5180. /* Clear CCF Flag */
  5181. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  5182. }
  5183. /* if payload */
  5184. if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE)
  5185. {
  5186. /* Retrieve intermediate data */
  5187. for(index=0U; index < 4U; index ++)
  5188. {
  5189. intermediate_data[index] = hcryp->Instance->DOUTR;
  5190. }
  5191. /* Retrieve last words of cyphered data */
  5192. /* First, retrieve complete output words */
  5193. for(index=0U; index < (difflength/4U); index ++)
  5194. {
  5195. *(uint32_t*)(outputaddr) = intermediate_data[index];
  5196. outputaddr+=4U;
  5197. }
  5198. /* Next, retrieve partial output word if applicable;
  5199. at the same time, start masking intermediate data
  5200. with a mask of zeros of same size than the padding
  5201. applied to the last block of payload */
  5202. if (difflengthmod4 != 0U)
  5203. {
  5204. intermediate_data[difflength/4U] &= mask[difflengthmod4-1U];
  5205. *(uint32_t*)(outputaddr) = intermediate_data[difflength/4U];
  5206. }
  5207. #if !defined(AES_CR_NPBLB)
  5208. /* Software workaround applied to GCM encryption only,
  5209. applicable for AES IP v2 version (where NPBLB is not defined) */
  5210. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  5211. {
  5212. /* Change again CHMOD configuration to GCM mode */
  5213. __HAL_CRYP_SET_CHAININGMODE(CRYP_CHAINMODE_AES_GCM_GMAC);
  5214. /* Select FINAL phase */
  5215. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_GCMCMAC_FINAL_PHASE);
  5216. /* Before inserting the intermediate data, carry on masking operation
  5217. with a mask of zeros of same size than the padding applied to the last block of payload */
  5218. for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
  5219. {
  5220. intermediate_data[(difflength+3U)/4U+index] = 0U;
  5221. }
  5222. /* Insert intermediate data */
  5223. for(index=0U; index < 4U; index ++)
  5224. {
  5225. hcryp->Instance->DINR = intermediate_data[index];
  5226. }
  5227. /* Wait for completion, and read data on DOUT. This data is to discard. */
  5228. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  5229. {
  5230. hcryp->State = HAL_CRYP_STATE_READY;
  5231. __HAL_UNLOCK(hcryp);
  5232. HAL_CRYP_ErrorCallback(hcryp);
  5233. }
  5234. /* Read data to discard */
  5235. /* Clear CCF Flag */
  5236. __HAL_CRYP_CLEAR_FLAG(CRYP_CCF_CLEAR);
  5237. for(index=0U; index < 4U; index ++)
  5238. {
  5239. intermediate_data[index] = hcryp->Instance->DOUTR;
  5240. }
  5241. } /* if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) */
  5242. #endif /* !defined(AES_CR_NPBLB) */
  5243. } /* if (hcryp->Init.GCMCMACPhase == CRYP_GCM_PAYLOAD_PHASE) */
  5244. }
  5245. /**
  5246. * @}
  5247. */
  5248. #endif /* AES */
  5249. #endif /* HAL_CRYP_MODULE_ENABLED */
  5250. /**
  5251. * @}
  5252. */
  5253. /**
  5254. * @}
  5255. */
  5256. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/