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.
 
 
 

5183 lines
162 KiB

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