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.
 
 
 

6040 lines
202 KiB

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