训练营PLSR题目
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.
 
 
 
 
 
 

681 line
21 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_cryp_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended CRYP HAL module driver
  6. * This file provides firmware functions to manage the following
  7. * functionalities of CRYP extension peripheral:
  8. * + Extended AES processing functions
  9. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * Copyright (c) 2016 STMicroelectronics.
  14. * All rights reserved.
  15. *
  16. * This software is licensed under terms that can be found in the LICENSE file
  17. * in the root directory of this software component.
  18. * If no LICENSE file comes with this software, it is provided AS-IS.
  19. *
  20. ******************************************************************************
  21. @verbatim
  22. ==============================================================================
  23. ##### How to use this driver #####
  24. ==============================================================================
  25. [..]
  26. The CRYP extension HAL driver can be used as follows:
  27. (#)After AES-GCM or AES-CCM Encryption/Decryption user can start following API
  28. to get the authentication messages :
  29. (##) HAL_CRYPEx_AESGCM_GenerateAuthTAG
  30. (##) HAL_CRYPEx_AESCCM_GenerateAuthTAG
  31. @endverbatim
  32. ******************************************************************************
  33. */
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm32f4xx_hal.h"
  36. /** @addtogroup STM32F4xx_HAL_Driver
  37. * @{
  38. */
  39. #if defined (AES) || defined (CRYP)
  40. #if defined (CRYP_CR_ALGOMODE_AES_GCM)|| defined (AES)
  41. /** @defgroup CRYPEx CRYPEx
  42. * @brief CRYP Extension HAL module driver.
  43. * @{
  44. */
  45. #ifdef HAL_CRYP_MODULE_ENABLED
  46. /* Private typedef -----------------------------------------------------------*/
  47. /* Private define ------------------------------------------------------------*/
  48. /** @addtogroup CRYPEx_Private_Defines
  49. * @{
  50. */
  51. #if defined(AES)
  52. #define CRYP_PHASE_INIT 0x00000000U /*!< GCM/GMAC (or CCM) init phase */
  53. #define CRYP_PHASE_HEADER AES_CR_GCMPH_0 /*!< GCM/GMAC or CCM header phase */
  54. #define CRYP_PHASE_PAYLOAD AES_CR_GCMPH_1 /*!< GCM(/CCM) payload phase */
  55. #define CRYP_PHASE_FINAL AES_CR_GCMPH /*!< GCM/GMAC or CCM final phase */
  56. #define CRYP_OPERATINGMODE_ENCRYPT 0x00000000U /*!< Encryption mode */
  57. #define CRYP_OPERATINGMODE_KEYDERIVATION AES_CR_MODE_0 /*!< Key derivation mode only used when performing ECB and CBC decryptions */
  58. #define CRYP_OPERATINGMODE_DECRYPT AES_CR_MODE_1 /*!< Decryption */
  59. #define CRYP_OPERATINGMODE_KEYDERIVATION_DECRYPT AES_CR_MODE /*!< Key derivation and decryption only used when performing ECB and CBC decryptions */
  60. #else /* CRYP */
  61. #define CRYP_PHASE_INIT 0x00000000U
  62. #define CRYP_PHASE_HEADER CRYP_CR_GCM_CCMPH_0
  63. #define CRYP_PHASE_PAYLOAD CRYP_CR_GCM_CCMPH_1
  64. #define CRYP_PHASE_FINAL CRYP_CR_GCM_CCMPH
  65. #define CRYP_OPERATINGMODE_ENCRYPT 0x00000000U
  66. #define CRYP_OPERATINGMODE_DECRYPT CRYP_CR_ALGODIR
  67. #endif /* End AES or CRYP */
  68. #define CRYPEx_PHASE_PROCESS 0x02U /*!< CRYP peripheral is in processing phase */
  69. #define CRYPEx_PHASE_FINAL 0x03U /*!< CRYP peripheral is in final phase this is relevant only with CCM and GCM modes */
  70. /* CTR0 information to use in CCM algorithm */
  71. #define CRYP_CCM_CTR0_0 0x07FFFFFFU
  72. #define CRYP_CCM_CTR0_3 0xFFFFFF00U
  73. /**
  74. * @}
  75. */
  76. /* Private macro -------------------------------------------------------------*/
  77. /* Private variables ---------------------------------------------------------*/
  78. /* Private function prototypes -----------------------------------------------*/
  79. /* Exported functions---------------------------------------------------------*/
  80. /** @addtogroup CRYPEx_Exported_Functions
  81. * @{
  82. */
  83. /** @defgroup CRYPEx_Exported_Functions_Group1 Extended AES processing functions
  84. * @brief Extended processing functions.
  85. *
  86. @verbatim
  87. ==============================================================================
  88. ##### Extended AES processing functions #####
  89. ==============================================================================
  90. [..] This section provides functions allowing to generate the authentication
  91. TAG in Polling mode
  92. (#)HAL_CRYPEx_AESGCM_GenerateAuthTAG
  93. (#)HAL_CRYPEx_AESCCM_GenerateAuthTAG
  94. they should be used after Encrypt/Decrypt operation.
  95. @endverbatim
  96. * @{
  97. */
  98. /**
  99. * @brief generate the GCM authentication TAG.
  100. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  101. * the configuration information for CRYP module
  102. * @param AuthTag: Pointer to the authentication buffer
  103. * @param Timeout: Timeout duration
  104. * @retval HAL status
  105. */
  106. HAL_StatusTypeDef HAL_CRYPEx_AESGCM_GenerateAuthTAG(CRYP_HandleTypeDef *hcryp, uint32_t *AuthTag, uint32_t Timeout)
  107. {
  108. uint32_t tickstart;
  109. /* Assume first Init.HeaderSize is in words */
  110. uint64_t headerlength = (uint64_t)(hcryp->Init.HeaderSize) * 32U; /* Header length in bits */
  111. uint64_t inputlength = (uint64_t)hcryp->SizesSum * 8U; /* Input length in bits */
  112. uint32_t tagaddr = (uint32_t)AuthTag;
  113. /* Correct headerlength if Init.HeaderSize is actually in bytes */
  114. if (hcryp->Init.HeaderWidthUnit == CRYP_HEADERWIDTHUNIT_BYTE)
  115. {
  116. headerlength /= 4U;
  117. }
  118. if (hcryp->State == HAL_CRYP_STATE_READY)
  119. {
  120. /* Process locked */
  121. __HAL_LOCK(hcryp);
  122. /* Change the CRYP peripheral state */
  123. hcryp->State = HAL_CRYP_STATE_BUSY;
  124. /* Check if initialization phase has already been performed */
  125. if (hcryp->Phase == CRYPEx_PHASE_PROCESS)
  126. {
  127. /* Change the CRYP phase */
  128. hcryp->Phase = CRYPEx_PHASE_FINAL;
  129. }
  130. else /* Initialization phase has not been performed*/
  131. {
  132. /* Disable the Peripheral */
  133. __HAL_CRYP_DISABLE(hcryp);
  134. /* Sequence error code field */
  135. hcryp->ErrorCode |= HAL_CRYP_ERROR_AUTH_TAG_SEQUENCE;
  136. /* Change the CRYP peripheral state */
  137. hcryp->State = HAL_CRYP_STATE_READY;
  138. /* Process unlocked */
  139. __HAL_UNLOCK(hcryp);
  140. return HAL_ERROR;
  141. }
  142. #if defined(CRYP)
  143. /* Disable CRYP to start the final phase */
  144. __HAL_CRYP_DISABLE(hcryp);
  145. /* Select final phase */
  146. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_GCM_CCMPH, CRYP_PHASE_FINAL);
  147. /*ALGODIR bit must be set to '0'.*/
  148. hcryp->Instance->CR &= ~CRYP_CR_ALGODIR;
  149. /* Enable the CRYP peripheral */
  150. __HAL_CRYP_ENABLE(hcryp);
  151. /* Write the number of bits in header (64 bits) followed by the number of bits
  152. in the payload */
  153. if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
  154. {
  155. hcryp->Instance->DIN = 0U;
  156. hcryp->Instance->DIN = __RBIT((uint32_t)(headerlength));
  157. hcryp->Instance->DIN = 0U;
  158. hcryp->Instance->DIN = __RBIT((uint32_t)(inputlength));
  159. }
  160. else if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
  161. {
  162. hcryp->Instance->DIN = 0U;
  163. hcryp->Instance->DIN = __REV((uint32_t)(headerlength));
  164. hcryp->Instance->DIN = 0U;
  165. hcryp->Instance->DIN = __REV((uint32_t)(inputlength));
  166. }
  167. else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
  168. {
  169. hcryp->Instance->DIN = 0U;
  170. hcryp->Instance->DIN = __ROR((uint32_t)headerlength, 16U);
  171. hcryp->Instance->DIN = 0U;
  172. hcryp->Instance->DIN = __ROR((uint32_t)inputlength, 16U);
  173. }
  174. else if (hcryp->Init.DataType == CRYP_DATATYPE_32B)
  175. {
  176. hcryp->Instance->DIN = 0U;
  177. hcryp->Instance->DIN = (uint32_t)(headerlength);
  178. hcryp->Instance->DIN = 0U;
  179. hcryp->Instance->DIN = (uint32_t)(inputlength);
  180. }
  181. else
  182. {
  183. /* Nothing to do */
  184. }
  185. /* Wait for OFNE flag to be raised */
  186. tickstart = HAL_GetTick();
  187. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  188. {
  189. /* Check for the Timeout */
  190. if (Timeout != HAL_MAX_DELAY)
  191. {
  192. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  193. {
  194. /* Disable the CRYP Peripheral Clock */
  195. __HAL_CRYP_DISABLE(hcryp);
  196. /* Change state */
  197. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  198. hcryp->State = HAL_CRYP_STATE_READY;
  199. /* Process unlocked */
  200. __HAL_UNLOCK(hcryp);
  201. return HAL_ERROR;
  202. }
  203. }
  204. }
  205. /* Read the authentication TAG in the output FIFO */
  206. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
  207. tagaddr += 4U;
  208. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
  209. tagaddr += 4U;
  210. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
  211. tagaddr += 4U;
  212. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
  213. #else /* AES*/
  214. /* Select final phase */
  215. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_FINAL);
  216. /* Write the number of bits in header (64 bits) followed by the number of bits
  217. in the payload */
  218. if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
  219. {
  220. hcryp->Instance->DINR = 0U;
  221. hcryp->Instance->DINR = __RBIT((uint32_t)(headerlength));
  222. hcryp->Instance->DINR = 0U;
  223. hcryp->Instance->DINR = __RBIT((uint32_t)(inputlength));
  224. }
  225. else if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
  226. {
  227. hcryp->Instance->DINR = 0U;
  228. hcryp->Instance->DINR = __REV((uint32_t)(headerlength));
  229. hcryp->Instance->DINR = 0U;
  230. hcryp->Instance->DINR = __REV((uint32_t)(inputlength));
  231. }
  232. else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
  233. {
  234. hcryp->Instance->DINR = 0U;
  235. hcryp->Instance->DINR = __ROR((uint32_t)headerlength, 16U);
  236. hcryp->Instance->DINR = 0U;
  237. hcryp->Instance->DINR = __ROR((uint32_t)inputlength, 16U);
  238. }
  239. else if (hcryp->Init.DataType == CRYP_DATATYPE_32B)
  240. {
  241. hcryp->Instance->DINR = 0U;
  242. hcryp->Instance->DINR = (uint32_t)(headerlength);
  243. hcryp->Instance->DINR = 0U;
  244. hcryp->Instance->DINR = (uint32_t)(inputlength);
  245. }
  246. else
  247. {
  248. /* Nothing to do */
  249. }
  250. /* Wait for CCF flag to be raised */
  251. tickstart = HAL_GetTick();
  252. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
  253. {
  254. /* Check for the Timeout */
  255. if (Timeout != HAL_MAX_DELAY)
  256. {
  257. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  258. {
  259. /* Disable the CRYP peripheral clock */
  260. __HAL_CRYP_DISABLE(hcryp);
  261. /* Change state */
  262. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  263. hcryp->State = HAL_CRYP_STATE_READY;
  264. /* Process unlocked */
  265. __HAL_UNLOCK(hcryp);
  266. return HAL_ERROR;
  267. }
  268. }
  269. }
  270. /* Read the authentication TAG in the output FIFO */
  271. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
  272. tagaddr += 4U;
  273. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
  274. tagaddr += 4U;
  275. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
  276. tagaddr += 4U;
  277. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
  278. /* Clear CCF flag */
  279. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  280. #endif /* End AES or CRYP */
  281. /* Disable the peripheral */
  282. __HAL_CRYP_DISABLE(hcryp);
  283. /* Change the CRYP peripheral state */
  284. hcryp->State = HAL_CRYP_STATE_READY;
  285. /* Process unlocked */
  286. __HAL_UNLOCK(hcryp);
  287. }
  288. else
  289. {
  290. /* Busy error code field */
  291. hcryp->ErrorCode |= HAL_CRYP_ERROR_BUSY;
  292. return HAL_ERROR;
  293. }
  294. /* Return function status */
  295. return HAL_OK;
  296. }
  297. /**
  298. * @brief AES CCM Authentication TAG generation.
  299. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  300. * the configuration information for CRYP module
  301. * @param AuthTag: Pointer to the authentication buffer
  302. * @param Timeout: Timeout duration
  303. * @retval HAL status
  304. */
  305. HAL_StatusTypeDef HAL_CRYPEx_AESCCM_GenerateAuthTAG(CRYP_HandleTypeDef *hcryp, uint32_t *AuthTag, uint32_t Timeout)
  306. {
  307. uint32_t tagaddr = (uint32_t)AuthTag;
  308. uint32_t ctr0 [4] = {0};
  309. uint32_t ctr0addr = (uint32_t)ctr0;
  310. uint32_t tickstart;
  311. if (hcryp->State == HAL_CRYP_STATE_READY)
  312. {
  313. /* Process locked */
  314. __HAL_LOCK(hcryp);
  315. /* Change the CRYP peripheral state */
  316. hcryp->State = HAL_CRYP_STATE_BUSY;
  317. /* Check if initialization phase has already been performed */
  318. if (hcryp->Phase == CRYPEx_PHASE_PROCESS)
  319. {
  320. /* Change the CRYP phase */
  321. hcryp->Phase = CRYPEx_PHASE_FINAL;
  322. }
  323. else /* Initialization phase has not been performed*/
  324. {
  325. /* Disable the peripheral */
  326. __HAL_CRYP_DISABLE(hcryp);
  327. /* Sequence error code field */
  328. hcryp->ErrorCode |= HAL_CRYP_ERROR_AUTH_TAG_SEQUENCE;
  329. /* Change the CRYP peripheral state */
  330. hcryp->State = HAL_CRYP_STATE_READY;
  331. /* Process unlocked */
  332. __HAL_UNLOCK(hcryp);
  333. return HAL_ERROR;
  334. }
  335. #if defined(CRYP)
  336. /* Disable CRYP to start the final phase */
  337. __HAL_CRYP_DISABLE(hcryp);
  338. /* Select final phase & ALGODIR bit must be set to '0'. */
  339. MODIFY_REG(hcryp->Instance->CR, CRYP_CR_GCM_CCMPH | CRYP_CR_ALGODIR, CRYP_PHASE_FINAL | CRYP_OPERATINGMODE_ENCRYPT);
  340. /* Enable the CRYP peripheral */
  341. __HAL_CRYP_ENABLE(hcryp);
  342. /* Write the counter block in the IN FIFO, CTR0 information from B0
  343. data has to be swapped according to the DATATYPE*/
  344. ctr0[0] = (hcryp->Init.B0[0]) & CRYP_CCM_CTR0_0;
  345. ctr0[1] = hcryp->Init.B0[1];
  346. ctr0[2] = hcryp->Init.B0[2];
  347. ctr0[3] = hcryp->Init.B0[3] & CRYP_CCM_CTR0_3;
  348. if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
  349. {
  350. hcryp->Instance->DIN = __REV(*(uint32_t *)(ctr0addr));
  351. ctr0addr += 4U;
  352. hcryp->Instance->DIN = __REV(*(uint32_t *)(ctr0addr));
  353. ctr0addr += 4U;
  354. hcryp->Instance->DIN = __REV(*(uint32_t *)(ctr0addr));
  355. ctr0addr += 4U;
  356. hcryp->Instance->DIN = __REV(*(uint32_t *)(ctr0addr));
  357. }
  358. else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
  359. {
  360. hcryp->Instance->DIN = __ROR(*(uint32_t *)(ctr0addr), 16U);
  361. ctr0addr += 4U;
  362. hcryp->Instance->DIN = __ROR(*(uint32_t *)(ctr0addr), 16U);
  363. ctr0addr += 4U;
  364. hcryp->Instance->DIN = __ROR(*(uint32_t *)(ctr0addr), 16U);
  365. ctr0addr += 4U;
  366. hcryp->Instance->DIN = __ROR(*(uint32_t *)(ctr0addr), 16U);
  367. }
  368. else if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
  369. {
  370. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(ctr0addr));
  371. ctr0addr += 4U;
  372. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(ctr0addr));
  373. ctr0addr += 4U;
  374. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(ctr0addr));
  375. ctr0addr += 4U;
  376. hcryp->Instance->DIN = __RBIT(*(uint32_t *)(ctr0addr));
  377. }
  378. else
  379. {
  380. hcryp->Instance->DIN = *(uint32_t *)(ctr0addr);
  381. ctr0addr += 4U;
  382. hcryp->Instance->DIN = *(uint32_t *)(ctr0addr);
  383. ctr0addr += 4U;
  384. hcryp->Instance->DIN = *(uint32_t *)(ctr0addr);
  385. ctr0addr += 4U;
  386. hcryp->Instance->DIN = *(uint32_t *)(ctr0addr);
  387. }
  388. /* Wait for OFNE flag to be raised */
  389. tickstart = HAL_GetTick();
  390. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_OFNE))
  391. {
  392. /* Check for the Timeout */
  393. if (Timeout != HAL_MAX_DELAY)
  394. {
  395. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  396. {
  397. /* Disable the CRYP peripheral Clock */
  398. __HAL_CRYP_DISABLE(hcryp);
  399. /* Change state */
  400. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  401. hcryp->State = HAL_CRYP_STATE_READY;
  402. /* Process unlocked */
  403. __HAL_UNLOCK(hcryp);
  404. return HAL_ERROR;
  405. }
  406. }
  407. }
  408. /* Read the Auth TAG in the IN FIFO */
  409. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
  410. tagaddr += 4U;
  411. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
  412. tagaddr += 4U;
  413. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
  414. tagaddr += 4U;
  415. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUT;
  416. #else /* AES */
  417. /* Select final phase */
  418. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PHASE_FINAL);
  419. /* Write the counter block in the IN FIFO, CTR0 information from B0
  420. data has to be swapped according to the DATATYPE*/
  421. if (hcryp->Init.DataType == CRYP_DATATYPE_8B)
  422. {
  423. ctr0[0] = (__REV(hcryp->Init.B0[0]) & CRYP_CCM_CTR0_0);
  424. ctr0[1] = __REV(hcryp->Init.B0[1]);
  425. ctr0[2] = __REV(hcryp->Init.B0[2]);
  426. ctr0[3] = (__REV(hcryp->Init.B0[3])& CRYP_CCM_CTR0_3);
  427. hcryp->Instance->DINR = __REV(*(uint32_t *)(ctr0addr));
  428. ctr0addr += 4U;
  429. hcryp->Instance->DINR = __REV(*(uint32_t *)(ctr0addr));
  430. ctr0addr += 4U;
  431. hcryp->Instance->DINR = __REV(*(uint32_t *)(ctr0addr));
  432. ctr0addr += 4U;
  433. hcryp->Instance->DINR = __REV(*(uint32_t *)(ctr0addr));
  434. }
  435. else if (hcryp->Init.DataType == CRYP_DATATYPE_16B)
  436. {
  437. ctr0[0] = (__ROR((hcryp->Init.B0[0]), 16U)& CRYP_CCM_CTR0_0);
  438. ctr0[1] = __ROR((hcryp->Init.B0[1]), 16U);
  439. ctr0[2] = __ROR((hcryp->Init.B0[2]), 16U);
  440. ctr0[3] = (__ROR((hcryp->Init.B0[3]), 16U)& CRYP_CCM_CTR0_3);
  441. hcryp->Instance->DINR = __ROR(*(uint32_t *)(ctr0addr), 16U);
  442. ctr0addr += 4U;
  443. hcryp->Instance->DINR = __ROR(*(uint32_t *)(ctr0addr), 16U);
  444. ctr0addr += 4U;
  445. hcryp->Instance->DINR = __ROR(*(uint32_t *)(ctr0addr), 16U);
  446. ctr0addr += 4U;
  447. hcryp->Instance->DINR = __ROR(*(uint32_t *)(ctr0addr), 16U);
  448. }
  449. else if (hcryp->Init.DataType == CRYP_DATATYPE_1B)
  450. {
  451. ctr0[0] = (__RBIT(hcryp->Init.B0[0])& CRYP_CCM_CTR0_0);
  452. ctr0[1] = __RBIT(hcryp->Init.B0[1]);
  453. ctr0[2] = __RBIT(hcryp->Init.B0[2]);
  454. ctr0[3] = (__RBIT(hcryp->Init.B0[3])& CRYP_CCM_CTR0_3);
  455. hcryp->Instance->DINR = __RBIT(*(uint32_t *)(ctr0addr));
  456. ctr0addr += 4U;
  457. hcryp->Instance->DINR = __RBIT(*(uint32_t *)(ctr0addr));
  458. ctr0addr += 4U;
  459. hcryp->Instance->DINR = __RBIT(*(uint32_t *)(ctr0addr));
  460. ctr0addr += 4U;
  461. hcryp->Instance->DINR = __RBIT(*(uint32_t *)(ctr0addr));
  462. }
  463. else
  464. {
  465. ctr0[0] = (hcryp->Init.B0[0]) & CRYP_CCM_CTR0_0;
  466. ctr0[1] = hcryp->Init.B0[1];
  467. ctr0[2] = hcryp->Init.B0[2];
  468. ctr0[3] = hcryp->Init.B0[3] & CRYP_CCM_CTR0_3;
  469. hcryp->Instance->DINR = *(uint32_t *)(ctr0addr);
  470. ctr0addr += 4U;
  471. hcryp->Instance->DINR = *(uint32_t *)(ctr0addr);
  472. ctr0addr += 4U;
  473. hcryp->Instance->DINR = *(uint32_t *)(ctr0addr);
  474. ctr0addr += 4U;
  475. hcryp->Instance->DINR = *(uint32_t *)(ctr0addr);
  476. }
  477. /* Wait for CCF flag to be raised */
  478. tickstart = HAL_GetTick();
  479. while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
  480. {
  481. /* Check for the Timeout */
  482. if (Timeout != HAL_MAX_DELAY)
  483. {
  484. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  485. {
  486. /* Disable the CRYP peripheral Clock */
  487. __HAL_CRYP_DISABLE(hcryp);
  488. /* Change state */
  489. hcryp->ErrorCode |= HAL_CRYP_ERROR_TIMEOUT;
  490. hcryp->State = HAL_CRYP_STATE_READY;
  491. /* Process unlocked */
  492. __HAL_UNLOCK(hcryp);
  493. return HAL_ERROR;
  494. }
  495. }
  496. }
  497. /* Read the authentication TAG in the output FIFO */
  498. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
  499. tagaddr += 4U;
  500. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
  501. tagaddr += 4U;
  502. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
  503. tagaddr += 4U;
  504. *(uint32_t *)(tagaddr) = hcryp->Instance->DOUTR;
  505. /* Clear CCF Flag */
  506. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  507. #endif /* End of AES || CRYP */
  508. /* Change the CRYP peripheral state */
  509. hcryp->State = HAL_CRYP_STATE_READY;
  510. /* Process unlocked */
  511. __HAL_UNLOCK(hcryp);
  512. /* Disable CRYP */
  513. __HAL_CRYP_DISABLE(hcryp);
  514. }
  515. else
  516. {
  517. /* Busy error code field */
  518. hcryp->ErrorCode = HAL_CRYP_ERROR_BUSY;
  519. return HAL_ERROR;
  520. }
  521. /* Return function status */
  522. return HAL_OK;
  523. }
  524. /**
  525. * @}
  526. */
  527. #if defined (AES)
  528. /** @defgroup CRYPEx_Exported_Functions_Group2 Key Derivation functions
  529. * @brief AutoKeyDerivation functions
  530. *
  531. @verbatim
  532. ==============================================================================
  533. ##### Key Derivation functions #####
  534. ==============================================================================
  535. [..] This section provides functions allowing to Enable or Disable the
  536. the AutoKeyDerivation parameter in CRYP_HandleTypeDef structure
  537. These function are allowed only in TinyAES IP.
  538. @endverbatim
  539. * @{
  540. */
  541. /**
  542. * @brief AES enable key derivation functions
  543. * @param hcryp: pointer to a CRYP_HandleTypeDef structure.
  544. * @retval None
  545. */
  546. void HAL_CRYPEx_EnableAutoKeyDerivation(CRYP_HandleTypeDef *hcryp)
  547. {
  548. if (hcryp->State == HAL_CRYP_STATE_READY)
  549. {
  550. hcryp->AutoKeyDerivation = ENABLE;
  551. }
  552. else
  553. {
  554. /* Busy error code field */
  555. hcryp->ErrorCode = HAL_CRYP_ERROR_BUSY;
  556. }
  557. }
  558. /**
  559. * @brief AES disable key derivation functions
  560. * @param hcryp: pointer to a CRYP_HandleTypeDef structure.
  561. * @retval None
  562. */
  563. void HAL_CRYPEx_DisableAutoKeyDerivation(CRYP_HandleTypeDef *hcryp)
  564. {
  565. if (hcryp->State == HAL_CRYP_STATE_READY)
  566. {
  567. hcryp->AutoKeyDerivation = DISABLE;
  568. }
  569. else
  570. {
  571. /* Busy error code field */
  572. hcryp->ErrorCode = HAL_CRYP_ERROR_BUSY;
  573. }
  574. }
  575. /**
  576. * @}
  577. */
  578. #endif /* AES or GCM CCM defined*/
  579. #endif /* AES */
  580. #endif /* HAL_CRYP_MODULE_ENABLED */
  581. /**
  582. * @}
  583. */
  584. #endif /* TinyAES or CRYP*/
  585. /**
  586. * @}
  587. */
  588. /**
  589. * @}
  590. */