stm32l4xx_hal_cryp.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_cryp.c
  4. * @author MCD Application Team
  5. * @brief CRYP HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Cryptography (CRYP) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + Processing functions using polling mode
  10. * + Processing functions using interrupt mode
  11. * + Processing functions using DMA mode
  12. * + Peripheral State functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. The CRYP HAL driver can be used as follows:
  20. (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
  21. (++) Enable the CRYP interface clock using __HAL_RCC_AES_CLK_ENABLE()
  22. (++) In case of using interrupts (e.g. HAL_CRYP_AES_IT())
  23. (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
  24. (+++) Enable the AES IRQ handler using HAL_NVIC_EnableIRQ()
  25. (+++) In AES IRQ handler, call HAL_CRYP_IRQHandler()
  26. (++) In case of using DMA to control data transfer (e.g. HAL_CRYPEx_AES_DMA())
  27. (+++) Enable the DMA2 interface clock using
  28. __HAL_RCC_DMA2_CLK_ENABLE()
  29. (+++) Configure and enable two DMA channels one for managing data transfer from
  30. memory to peripheral (input channel) and another channel for managing data
  31. transfer from peripheral to memory (output channel)
  32. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  33. using __HAL_LINKDMA()
  34. (+++) Configure the priority and enable the NVIC for the transfer complete
  35. interrupt on the two DMA channels. The output channel should have higher
  36. priority than the input channel.
  37. Resort to HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
  38. (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures:
  39. (++) The data type: 1-bit, 8-bit, 16-bit and 32-bit
  40. (++) The AES operating mode (encryption, key derivation and/or decryption)
  41. (++) The AES chaining mode (ECB, CBC, CTR, GCM, GMAC, CMAC when applicable, CCM when applicable)
  42. (++) The encryption/decryption key if so required
  43. (++) The initialization vector or nonce if applicable (not used in ECB mode).
  44. (#)Three processing (encryption/decryption) functions are available:
  45. (++) Polling mode: encryption and decryption APIs are blocking functions
  46. i.e. they process the data and wait till the processing is finished
  47. (++) Interrupt mode: encryption and decryption APIs are not blocking functions
  48. i.e. they process the data under interrupt
  49. (++) DMA mode: encryption and decryption APIs are not blocking functions
  50. i.e. the data transfer is ensured by DMA
  51. (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
  52. @endverbatim
  53. ******************************************************************************
  54. * @attention
  55. *
  56. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  57. *
  58. * Redistribution and use in source and binary forms, with or without modification,
  59. * are permitted provided that the following conditions are met:
  60. * 1. Redistributions of source code must retain the above copyright notice,
  61. * this list of conditions and the following disclaimer.
  62. * 2. Redistributions in binary form must reproduce the above copyright notice,
  63. * this list of conditions and the following disclaimer in the documentation
  64. * and/or other materials provided with the distribution.
  65. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  66. * may be used to endorse or promote products derived from this software
  67. * without specific prior written permission.
  68. *
  69. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  70. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  71. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  72. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  73. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  74. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  75. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  76. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  77. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  78. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  79. *
  80. ******************************************************************************
  81. */
  82. /* Includes ------------------------------------------------------------------*/
  83. #include "stm32l4xx_hal.h"
  84. #ifdef HAL_CRYP_MODULE_ENABLED
  85. #if defined (STM32L442xx) || defined (STM32L443xx) || defined (STM32L462xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L4A6xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  86. /** @addtogroup STM32L4xx_HAL_Driver
  87. * @{
  88. */
  89. /** @defgroup CRYP CRYP
  90. * @brief CRYP HAL module driver.
  91. * @{
  92. */
  93. /* Private typedef -----------------------------------------------------------*/
  94. /* Private define ------------------------------------------------------------*/
  95. /* Private macro -------------------------------------------------------------*/
  96. /* Private variables ---------------------------------------------------------*/
  97. /* Private functions --------------------------------------------------------*/
  98. /** @defgroup CRYP_Private_Functions CRYP Private Functions
  99. * @{
  100. */
  101. static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp);
  102. static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp);
  103. static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
  104. /**
  105. * @}
  106. */
  107. /* Exported functions ---------------------------------------------------------*/
  108. /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
  109. * @{
  110. */
  111. /** @defgroup CRYP_Exported_Functions_Group1 Initialization and deinitialization functions
  112. * @brief Initialization and Configuration functions.
  113. *
  114. @verbatim
  115. ==============================================================================
  116. ##### Initialization and deinitialization functions #####
  117. ==============================================================================
  118. [..] This section provides functions allowing to:
  119. (+) Initialize the CRYP according to the specified parameters
  120. in the CRYP_InitTypeDef and creates the associated handle
  121. (+) DeInitialize the CRYP peripheral
  122. (+) Initialize the CRYP MSP (MCU Specific Package)
  123. (+) De-Initialize the CRYP MSP
  124. [..]
  125. (@) Specific care must be taken to format the key and the Initialization Vector IV!
  126. [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where
  127. b127 is the MSB and b0 the LSB, the key must be stored in MCU memory
  128. (+) as a sequence of words where the MSB word comes first (occupies the
  129. lowest memory address)
  130. (+) where each word is byte-swapped:
  131. (++) address n+0 : 0b b103 .. b96 b111 .. b104 b119 .. b112 b127 .. b120
  132. (++) address n+4 : 0b b71 .. b64 b79 .. b72 b87 .. b80 b95 .. b88
  133. (++) address n+8 : 0b b39 .. b32 b47 .. b40 b55 .. b48 b63 .. b56
  134. (++) address n+C : 0b b7 .. b0 b15 .. b8 b23 .. b16 b31 .. b24
  135. [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}.
  136. The 4 32-bit words that make the key must be stored as follows in MCU memory:
  137. (+) address n+0 : 0x B12 B13 B14 B15
  138. (+) address n+4 : 0x B8 B9 B10 B11
  139. (+) address n+8 : 0x B4 B5 B6 B7
  140. (+) address n+C : 0x B0 B1 B2 B3
  141. [..] which leads to the expected setting
  142. (+) AES_KEYR3 = 0x B15 B14 B13 B12
  143. (+) AES_KEYR2 = 0x B11 B10 B9 B8
  144. (+) AES_KEYR1 = 0x B7 B6 B5 B4
  145. (+) AES_KEYR0 = 0x B3 B2 B1 B0
  146. [..] Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}.
  147. The 8 32-bit words that make the key must be stored as follows in MCU memory:
  148. (+) address n+00 : 0x B28 B29 B30 B31
  149. (+) address n+04 : 0x B24 B25 B26 B27
  150. (+) address n+08 : 0x B20 B21 B22 B23
  151. (+) address n+0C : 0x B16 B17 B18 B19
  152. (+) address n+10 : 0x B12 B13 B14 B15
  153. (+) address n+14 : 0x B8 B9 B10 B11
  154. (+) address n+18 : 0x B4 B5 B6 B7
  155. (+) address n+1C : 0x B0 B1 B2 B3
  156. [..] which leads to the expected setting
  157. (+) AES_KEYR7 = 0x B31 B30 B29 B28
  158. (+) AES_KEYR6 = 0x B27 B26 B25 B24
  159. (+) AES_KEYR5 = 0x B23 B22 B21 B20
  160. (+) AES_KEYR4 = 0x B19 B18 B17 B16
  161. (+) AES_KEYR3 = 0x B15 B14 B13 B12
  162. (+) AES_KEYR2 = 0x B11 B10 B9 B8
  163. (+) AES_KEYR1 = 0x B7 B6 B5 B4
  164. (+) AES_KEYR0 = 0x B3 B2 B1 B0
  165. [..] Initialization Vector IV (4 32-bit words) format must follow the same as
  166. that of a 128-bit long key.
  167. [..]
  168. @endverbatim
  169. * @{
  170. */
  171. /**
  172. * @brief Initialize the CRYP according to the specified
  173. * parameters in the CRYP_InitTypeDef and initialize the associated handle.
  174. * @note Specific care must be taken to format the key and the Initialization Vector IV
  175. * stored in the MCU memory before calling HAL_CRYP_Init(). Refer to explanations
  176. * hereabove.
  177. * @retval HAL status
  178. */
  179. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
  180. {
  181. /* Check the CRYP handle allocation */
  182. if(hcryp == NULL)
  183. {
  184. return HAL_ERROR;
  185. }
  186. /* Check the instance */
  187. assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance));
  188. /* Check the parameters */
  189. assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
  190. assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
  191. assert_param(IS_CRYP_ALGOMODE(hcryp->Init.OperatingMode));
  192. /* ChainingMode parameter is irrelevant when mode is set to Key derivation */
  193. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  194. {
  195. assert_param(IS_CRYP_CHAINMODE(hcryp->Init.ChainingMode));
  196. }
  197. assert_param(IS_CRYP_WRITE(hcryp->Init.KeyWriteFlag));
  198. /*========================================================*/
  199. /* Check the proper operating/chaining modes combinations */
  200. /*========================================================*/
  201. /* Check the proper chaining when the operating mode is key derivation and decryption */
  202. #if defined(AES_CR_NPBLB)
  203. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
  204. ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \
  205. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \
  206. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)))
  207. #else
  208. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
  209. ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \
  210. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \
  211. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)))
  212. #endif
  213. {
  214. return HAL_ERROR;
  215. }
  216. /* Check that key derivation is not set in CMAC mode or CCM mode when applicable */
  217. #if defined(AES_CR_NPBLB)
  218. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  219. && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
  220. #else
  221. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  222. && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
  223. #endif
  224. {
  225. return HAL_ERROR;
  226. }
  227. /*================*/
  228. /* Initialization */
  229. /*================*/
  230. /* Initialization start */
  231. if(hcryp->State == HAL_CRYP_STATE_RESET)
  232. {
  233. /* Allocate lock resource and initialize it */
  234. hcryp->Lock = HAL_UNLOCKED;
  235. /* Init the low level hardware */
  236. HAL_CRYP_MspInit(hcryp);
  237. }
  238. /* Change the CRYP state */
  239. hcryp->State = HAL_CRYP_STATE_BUSY;
  240. /* Disable the Peripheral */
  241. __HAL_CRYP_DISABLE(hcryp);
  242. /*=============================================================*/
  243. /* AES initialization common to all operating modes */
  244. /*=============================================================*/
  245. /* Set the Key size selection */
  246. MODIFY_REG(hcryp->Instance->CR, AES_CR_KEYSIZE, hcryp->Init.KeySize);
  247. /* Set the default CRYP phase when this parameter is not used.
  248. Phase is updated below in case of GCM/GMAC(/CMAC)(/CCM) setting. */
  249. hcryp->Phase = HAL_CRYP_PHASE_NOT_USED;
  250. /*=============================================================*/
  251. /* Carry on the initialization based on the AES operating mode */
  252. /*=============================================================*/
  253. /* Key derivation */
  254. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  255. {
  256. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_ALGOMODE_KEYDERIVATION);
  257. /* Configure the Key registers */
  258. if (CRYP_SetKey(hcryp) != HAL_OK)
  259. {
  260. return HAL_ERROR;
  261. }
  262. }
  263. else
  264. /* Encryption / Decryption (with or without key derivation) / authentication */
  265. {
  266. #if !defined(AES_CR_NPBLB)
  267. /* Set data type, operating and chaining modes.
  268. In case of GCM or GMAC, data type is forced to 0b00 */
  269. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  270. {
  271. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
  272. }
  273. else
  274. #endif
  275. {
  276. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.DataType|hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
  277. }
  278. /* Specify the encryption/decryption phase in case of Galois counter mode (GCM),
  279. Galois message authentication code (GMAC), cipher message authentication code (CMAC) when applicable
  280. or Counter with Cipher Mode (CCM) when applicable */
  281. #if defined(AES_CR_NPBLB)
  282. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  283. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
  284. #else
  285. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  286. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
  287. #endif
  288. {
  289. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, hcryp->Init.GCMCMACPhase);
  290. hcryp->Phase = HAL_CRYP_PHASE_START;
  291. }
  292. /* Configure the Key registers if no need to bypass this step */
  293. if (hcryp->Init.KeyWriteFlag == CRYP_KEY_WRITE_ENABLE)
  294. {
  295. if (CRYP_SetKey(hcryp) != HAL_OK)
  296. {
  297. return HAL_ERROR;
  298. }
  299. }
  300. /* If applicable, configure the Initialization Vector */
  301. if (hcryp->Init.ChainingMode != CRYP_CHAINMODE_AES_ECB)
  302. {
  303. if (CRYP_SetInitVector(hcryp) != HAL_OK)
  304. {
  305. return HAL_ERROR;
  306. }
  307. }
  308. }
  309. #if defined(AES_CR_NPBLB)
  310. /* Clear NPBLB field */
  311. CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
  312. #endif
  313. /* Reset CrypInCount and CrypOutCount */
  314. hcryp->CrypInCount = 0;
  315. hcryp->CrypOutCount = 0;
  316. /* Reset ErrorCode field */
  317. hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
  318. /* Reset Mode suspension request */
  319. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  320. /* Change the CRYP state */
  321. hcryp->State = HAL_CRYP_STATE_READY;
  322. /* Enable the Peripheral */
  323. __HAL_CRYP_ENABLE(hcryp);
  324. /* Return function status */
  325. return HAL_OK;
  326. }
  327. /**
  328. * @brief DeInitialize the CRYP peripheral.
  329. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  330. * the configuration information for CRYP module
  331. * @retval HAL status
  332. */
  333. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
  334. {
  335. /* Check the CRYP handle allocation */
  336. if(hcryp == NULL)
  337. {
  338. return HAL_ERROR;
  339. }
  340. /* Change the CRYP state */
  341. hcryp->State = HAL_CRYP_STATE_BUSY;
  342. /* Set the default CRYP phase */
  343. hcryp->Phase = HAL_CRYP_PHASE_READY;
  344. /* Reset CrypInCount and CrypOutCount */
  345. hcryp->CrypInCount = 0;
  346. hcryp->CrypOutCount = 0;
  347. /* Disable the CRYP Peripheral Clock */
  348. __HAL_CRYP_DISABLE(hcryp);
  349. /* DeInit the low level hardware: CLOCK, NVIC.*/
  350. HAL_CRYP_MspDeInit(hcryp);
  351. /* Change the CRYP state */
  352. hcryp->State = HAL_CRYP_STATE_RESET;
  353. /* Release Lock */
  354. __HAL_UNLOCK(hcryp);
  355. /* Return function status */
  356. return HAL_OK;
  357. }
  358. /**
  359. * @brief Initialize the CRYP MSP.
  360. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  361. * the configuration information for CRYP module
  362. * @retval None
  363. */
  364. __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
  365. {
  366. /* Prevent unused argument(s) compilation warning */
  367. UNUSED(hcryp);
  368. /* NOTE : This function should not be modified; when the callback is needed,
  369. the HAL_CRYP_MspInit can be implemented in the user file
  370. */
  371. }
  372. /**
  373. * @brief DeInitialize CRYP MSP.
  374. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  375. * the configuration information for CRYP module
  376. * @retval None
  377. */
  378. __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
  379. {
  380. /* Prevent unused argument(s) compilation warning */
  381. UNUSED(hcryp);
  382. /* NOTE : This function should not be modified; when the callback is needed,
  383. the HAL_CRYP_MspDeInit can be implemented in the user file
  384. */
  385. }
  386. /**
  387. * @}
  388. */
  389. /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
  390. * @brief Processing functions.
  391. *
  392. @verbatim
  393. ==============================================================================
  394. ##### AES processing functions #####
  395. ==============================================================================
  396. [..] This section provides functions allowing to:
  397. (+) Encrypt plaintext using AES algorithm in different chaining modes
  398. (+) Decrypt cyphertext using AES algorithm in different chaining modes
  399. [..] Three processing functions are available:
  400. (+) Polling mode
  401. (+) Interrupt mode
  402. (+) DMA mode
  403. @endverbatim
  404. * @{
  405. */
  406. /**
  407. * @brief Encrypt pPlainData in AES ECB encryption mode. The cypher data are available in pCypherData.
  408. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  409. * the configuration information for CRYP module
  410. * @param pPlainData: Pointer to the plaintext buffer
  411. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  412. * @param pCypherData: Pointer to the cyphertext buffer
  413. * @param Timeout: Specify Timeout value
  414. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  415. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  416. * @retval HAL status
  417. */
  418. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  419. {
  420. /* Re-initialize AES IP with proper parameters */
  421. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  422. {
  423. return HAL_ERROR;
  424. }
  425. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  426. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  427. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  428. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  429. {
  430. return HAL_ERROR;
  431. }
  432. return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
  433. }
  434. /**
  435. * @brief Encrypt pPlainData in AES CBC encryption mode with key derivation. The cypher data are available in pCypherData.
  436. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  437. * the configuration information for CRYP module
  438. * @param pPlainData: Pointer to the plaintext buffer
  439. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  440. * @param pCypherData: Pointer to the cyphertext buffer
  441. * @param Timeout: Specify Timeout value
  442. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  443. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  444. * @retval HAL status
  445. */
  446. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  447. {
  448. /* Re-initialize AES IP with proper parameters */
  449. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  450. {
  451. return HAL_ERROR;
  452. }
  453. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  454. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  455. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  456. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  457. {
  458. return HAL_ERROR;
  459. }
  460. return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
  461. }
  462. /**
  463. * @brief Encrypt pPlainData in AES CTR encryption mode. The cypher data are available in pCypherData
  464. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  465. * the configuration information for CRYP module
  466. * @param pPlainData: Pointer to the plaintext buffer
  467. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  468. * @param pCypherData: Pointer to the cyphertext buffer
  469. * @param Timeout: Specify Timeout value
  470. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  471. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  472. * @retval HAL status
  473. */
  474. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  475. {
  476. /* Re-initialize AES IP with proper parameters */
  477. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  478. {
  479. return HAL_ERROR;
  480. }
  481. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  482. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  483. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  484. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  485. {
  486. return HAL_ERROR;
  487. }
  488. return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
  489. }
  490. /**
  491. * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation,
  492. * the decyphered data are available in pPlainData.
  493. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  494. * the configuration information for CRYP module
  495. * @param pCypherData: Pointer to the cyphertext buffer
  496. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  497. * @param pPlainData: Pointer to the plaintext buffer
  498. * @param Timeout: Specify Timeout value
  499. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  500. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  501. * @retval HAL status
  502. */
  503. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  504. {
  505. /* Re-initialize AES IP with proper parameters */
  506. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  507. {
  508. return HAL_ERROR;
  509. }
  510. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  511. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  512. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  513. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  514. {
  515. return HAL_ERROR;
  516. }
  517. return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
  518. }
  519. /**
  520. * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation,
  521. * the decyphered data are available in pPlainData.
  522. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  523. * the configuration information for CRYP module
  524. * @param pCypherData: Pointer to the cyphertext buffer
  525. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  526. * @param pPlainData: Pointer to the plaintext buffer
  527. * @param Timeout: Specify Timeout value
  528. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  529. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  530. * @retval HAL status
  531. */
  532. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  533. {
  534. /* Re-initialize AES IP with proper parameters */
  535. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  536. {
  537. return HAL_ERROR;
  538. }
  539. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  540. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  541. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  542. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  543. {
  544. return HAL_ERROR;
  545. }
  546. return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
  547. }
  548. /**
  549. * @brief Decrypt pCypherData in AES CTR decryption mode,
  550. * the decyphered data are available in pPlainData.
  551. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  552. * the configuration information for CRYP module
  553. * @param pCypherData: Pointer to the cyphertext buffer
  554. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  555. * @param pPlainData: Pointer to the plaintext buffer
  556. * @param Timeout: Specify Timeout value
  557. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  558. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  559. * @retval HAL status
  560. */
  561. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  562. {
  563. /* Re-initialize AES IP with proper parameters */
  564. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  565. {
  566. return HAL_ERROR;
  567. }
  568. hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  569. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  570. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  571. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  572. {
  573. return HAL_ERROR;
  574. }
  575. return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
  576. }
  577. /**
  578. * @brief Encrypt pPlainData in AES ECB encryption mode using Interrupt,
  579. * the cypher data are available in pCypherData.
  580. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  581. * the configuration information for CRYP module
  582. * @param pPlainData: Pointer to the plaintext buffer
  583. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  584. * @param pCypherData: Pointer to the cyphertext buffer
  585. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  586. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  587. * @retval HAL status
  588. */
  589. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  590. {
  591. /* Re-initialize AES IP with proper parameters */
  592. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  593. {
  594. return HAL_ERROR;
  595. }
  596. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  597. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  598. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  599. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  600. {
  601. return HAL_ERROR;
  602. }
  603. return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
  604. }
  605. /**
  606. * @brief Encrypt pPlainData in AES CBC encryption mode using Interrupt,
  607. * the cypher data are available in pCypherData.
  608. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  609. * the configuration information for CRYP module
  610. * @param pPlainData: Pointer to the plaintext buffer
  611. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  612. * @param pCypherData: Pointer to the cyphertext buffer
  613. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  614. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  615. * @retval HAL status
  616. */
  617. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  618. {
  619. /* Re-initialize AES IP with proper parameters */
  620. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  621. {
  622. return HAL_ERROR;
  623. }
  624. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  625. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  626. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  627. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  628. {
  629. return HAL_ERROR;
  630. }
  631. return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
  632. }
  633. /**
  634. * @brief Encrypt pPlainData in AES CTR encryption mode using Interrupt,
  635. * the cypher data are available in pCypherData.
  636. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  637. * the configuration information for CRYP module
  638. * @param pPlainData: Pointer to the plaintext buffer
  639. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  640. * @param pCypherData: Pointer to the cyphertext buffer
  641. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  642. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  643. * @retval HAL status
  644. */
  645. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  646. {
  647. /* Re-initialize AES IP with proper parameters */
  648. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  649. {
  650. return HAL_ERROR;
  651. }
  652. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  653. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  654. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  655. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  656. {
  657. return HAL_ERROR;
  658. }
  659. return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
  660. }
  661. /**
  662. * @brief Decrypt pCypherData in AES ECB decryption mode using Interrupt,
  663. * the decyphered data are available in pPlainData.
  664. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  665. * the configuration information for CRYP module
  666. * @param pCypherData: Pointer to the cyphertext buffer
  667. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  668. * @param pPlainData: Pointer to the plaintext buffer.
  669. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  670. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  671. * @retval HAL status
  672. */
  673. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  674. {
  675. /* Re-initialize AES IP with proper parameters */
  676. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  677. {
  678. return HAL_ERROR;
  679. }
  680. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  681. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  682. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  683. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  684. {
  685. return HAL_ERROR;
  686. }
  687. return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
  688. }
  689. /**
  690. * @brief Decrypt pCypherData in AES CBC decryption mode using Interrupt,
  691. * the decyphered data are available in pPlainData.
  692. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  693. * the configuration information for CRYP module
  694. * @param pCypherData: Pointer to the cyphertext buffer
  695. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  696. * @param pPlainData: Pointer to the plaintext buffer
  697. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  698. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  699. * @retval HAL status
  700. */
  701. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  702. {
  703. /* Re-initialize AES IP with proper parameters */
  704. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  705. {
  706. return HAL_ERROR;
  707. }
  708. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  709. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  710. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  711. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  712. {
  713. return HAL_ERROR;
  714. }
  715. return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
  716. }
  717. /**
  718. * @brief Decrypt pCypherData in AES CTR decryption mode using Interrupt,
  719. * the decyphered data are available in pPlainData.
  720. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  721. * the configuration information for CRYP module
  722. * @param pCypherData: Pointer to the cyphertext buffer
  723. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  724. * @param pPlainData: Pointer to the plaintext buffer
  725. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  726. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  727. * @retval HAL status
  728. */
  729. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  730. {
  731. /* Re-initialize AES IP with proper parameters */
  732. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  733. {
  734. return HAL_ERROR;
  735. }
  736. hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  737. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  738. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  739. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  740. {
  741. return HAL_ERROR;
  742. }
  743. return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
  744. }
  745. /**
  746. * @brief Encrypt pPlainData in AES ECB encryption mode using DMA,
  747. * the cypher data are available in pCypherData.
  748. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  749. * the configuration information for CRYP module
  750. * @param pPlainData: Pointer to the plaintext buffer
  751. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  752. * @param pCypherData: Pointer to the cyphertext buffer
  753. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  754. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  755. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  756. * @retval HAL status
  757. */
  758. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  759. {
  760. /* Re-initialize AES IP with proper parameters */
  761. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  762. {
  763. return HAL_ERROR;
  764. }
  765. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  766. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  767. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  768. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  769. {
  770. return HAL_ERROR;
  771. }
  772. return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
  773. }
  774. /**
  775. * @brief Encrypt pPlainData in AES CBC encryption mode using DMA,
  776. * the cypher data are available in pCypherData.
  777. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  778. * the configuration information for CRYP module
  779. * @param pPlainData: Pointer to the plaintext buffer
  780. * @param Size: Length of the plaintext buffer, must be a multiple of 16.
  781. * @param pCypherData: Pointer to the cyphertext buffer
  782. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  783. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  784. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  785. * @retval HAL status
  786. */
  787. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  788. {
  789. /* Re-initialize AES IP with proper parameters */
  790. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  791. {
  792. return HAL_ERROR;
  793. }
  794. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  795. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  796. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  797. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  798. {
  799. return HAL_ERROR;
  800. }
  801. return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
  802. }
  803. /**
  804. * @brief Encrypt pPlainData in AES CTR encryption mode using DMA,
  805. * the cypher data are available in pCypherData.
  806. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  807. * the configuration information for CRYP module
  808. * @param pPlainData: Pointer to the plaintext buffer
  809. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  810. * @param pCypherData: Pointer to the cyphertext buffer.
  811. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  812. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  813. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  814. * @retval HAL status
  815. */
  816. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  817. {
  818. /* Re-initialize AES IP with proper parameters */
  819. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  820. {
  821. return HAL_ERROR;
  822. }
  823. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  824. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  825. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  826. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  827. {
  828. return HAL_ERROR;
  829. }
  830. return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
  831. }
  832. /**
  833. * @brief Decrypt pCypherData in AES ECB decryption mode using DMA,
  834. * the decyphered data are available in pPlainData.
  835. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  836. * the configuration information for CRYP module
  837. * @param pCypherData: Pointer to the cyphertext buffer
  838. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  839. * @param pPlainData: Pointer to the plaintext buffer
  840. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  841. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  842. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  843. * @retval HAL status
  844. */
  845. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  846. {
  847. /* Re-initialize AES IP with proper parameters */
  848. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  849. {
  850. return HAL_ERROR;
  851. }
  852. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  853. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  854. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  855. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  856. {
  857. return HAL_ERROR;
  858. }
  859. return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
  860. }
  861. /**
  862. * @brief Decrypt pCypherData in AES CBC decryption mode using DMA,
  863. * the decyphered data are available in pPlainData.
  864. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  865. * the configuration information for CRYP module
  866. * @param pCypherData: Pointer to the cyphertext buffer
  867. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  868. * @param pPlainData: Pointer to the plaintext buffer
  869. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  870. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  871. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  872. * @retval HAL status
  873. */
  874. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  875. {
  876. /* Re-initialize AES IP with proper parameters */
  877. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  878. {
  879. return HAL_ERROR;
  880. }
  881. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  882. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  883. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  884. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  885. {
  886. return HAL_ERROR;
  887. }
  888. return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
  889. }
  890. /**
  891. * @brief Decrypt pCypherData in AES CTR decryption mode using DMA,
  892. * the decyphered data are available in pPlainData.
  893. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  894. * the configuration information for CRYP module
  895. * @param pCypherData: Pointer to the cyphertext buffer
  896. * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  897. * @param pPlainData: Pointer to the plaintext buffer
  898. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  899. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  900. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  901. * @retval HAL status
  902. */
  903. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  904. {
  905. /* Re-initialize AES IP with proper parameters */
  906. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  907. {
  908. return HAL_ERROR;
  909. }
  910. hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  911. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  912. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  913. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  914. {
  915. return HAL_ERROR;
  916. }
  917. return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
  918. }
  919. /**
  920. * @}
  921. */
  922. /** @defgroup CRYP_Exported_Functions_Group3 Callback functions
  923. * @brief Callback functions.
  924. *
  925. @verbatim
  926. ==============================================================================
  927. ##### Callback functions #####
  928. ==============================================================================
  929. [..] This section provides Interruption and DMA callback functions:
  930. (+) DMA Input data transfer complete
  931. (+) DMA Output data transfer complete
  932. (+) DMA or Interrupt error
  933. @endverbatim
  934. * @{
  935. */
  936. /**
  937. * @brief CRYP error callback.
  938. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  939. * the configuration information for CRYP module
  940. * @retval None
  941. */
  942. __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
  943. {
  944. /* Prevent unused argument(s) compilation warning */
  945. UNUSED(hcryp);
  946. /* NOTE : This function should not be modified; when the callback is needed,
  947. the HAL_CRYP_ErrorCallback can be implemented in the user file
  948. */
  949. }
  950. /**
  951. * @brief Input DMA transfer complete callback.
  952. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  953. * the configuration information for CRYP module
  954. * @retval None
  955. */
  956. __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
  957. {
  958. /* Prevent unused argument(s) compilation warning */
  959. UNUSED(hcryp);
  960. /* NOTE : This function should not be modified; when the callback is needed,
  961. the HAL_CRYP_InCpltCallback can be implemented in the user file
  962. */
  963. }
  964. /**
  965. * @brief Output DMA transfer complete callback.
  966. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  967. * the configuration information for CRYP module
  968. * @retval None
  969. */
  970. __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
  971. {
  972. /* Prevent unused argument(s) compilation warning */
  973. UNUSED(hcryp);
  974. /* NOTE : This function should not be modified; when the callback is needed,
  975. the HAL_CRYP_OutCpltCallback can be implemented in the user file
  976. */
  977. }
  978. /**
  979. * @}
  980. */
  981. /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
  982. * @brief AES IRQ handler.
  983. *
  984. @verbatim
  985. ==============================================================================
  986. ##### AES IRQ handler management #####
  987. ==============================================================================
  988. [..] This section provides AES IRQ handler function.
  989. @endverbatim
  990. * @{
  991. */
  992. /**
  993. * @brief Handle AES interrupt request.
  994. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  995. * the configuration information for CRYP module
  996. * @retval None
  997. */
  998. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
  999. {
  1000. /* Check if error occurred */
  1001. if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_ERRIE) != RESET)
  1002. {
  1003. /* If Write Error occurred */
  1004. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_WRERR) != RESET)
  1005. {
  1006. hcryp->ErrorCode |= HAL_CRYP_WRITE_ERROR;
  1007. hcryp->State = HAL_CRYP_STATE_ERROR;
  1008. }
  1009. /* If Read Error occurred */
  1010. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_RDERR) != RESET)
  1011. {
  1012. hcryp->ErrorCode |= HAL_CRYP_READ_ERROR;
  1013. hcryp->State = HAL_CRYP_STATE_ERROR;
  1014. }
  1015. /* If an error has been reported */
  1016. if (hcryp->State == HAL_CRYP_STATE_ERROR)
  1017. {
  1018. /* Disable Error and Computation Complete Interrupts */
  1019. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  1020. /* Clear all Interrupt flags */
  1021. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR|CRYP_CCF_CLEAR);
  1022. /* Process Unlocked */
  1023. __HAL_UNLOCK(hcryp);
  1024. HAL_CRYP_ErrorCallback(hcryp);
  1025. return;
  1026. }
  1027. }
  1028. /* Check if computation complete interrupt is enabled
  1029. and if the computation complete flag is raised */
  1030. if((__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_CCF) != RESET) && (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_CCFIE) != RESET))
  1031. {
  1032. #if defined(AES_CR_NPBLB)
  1033. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  1034. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
  1035. #else
  1036. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  1037. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
  1038. #endif
  1039. {
  1040. /* To ensure proper suspension requests management, CCF flag
  1041. is reset in CRYP_AES_Auth_IT() according to the current
  1042. phase under handling */
  1043. CRYP_AES_Auth_IT(hcryp);
  1044. }
  1045. else
  1046. {
  1047. /* Clear Computation Complete Flag */
  1048. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1049. CRYP_AES_IT(hcryp);
  1050. }
  1051. }
  1052. }
  1053. /**
  1054. * @}
  1055. */
  1056. /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
  1057. * @brief Peripheral State functions.
  1058. *
  1059. @verbatim
  1060. ==============================================================================
  1061. ##### Peripheral State functions #####
  1062. ==============================================================================
  1063. [..]
  1064. This subsection permits to get in run-time the status of the peripheral.
  1065. @endverbatim
  1066. * @{
  1067. */
  1068. /**
  1069. * @brief Return the CRYP handle state.
  1070. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1071. * the configuration information for CRYP module
  1072. * @retval HAL state
  1073. */
  1074. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
  1075. {
  1076. /* Return CRYP handle state */
  1077. return hcryp->State;
  1078. }
  1079. /**
  1080. * @brief Return the CRYP peripheral error.
  1081. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1082. * the configuration information for CRYP module
  1083. * @note The returned error is a bit-map combination of possible errors
  1084. * @retval Error bit-map
  1085. */
  1086. uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
  1087. {
  1088. return hcryp->ErrorCode;
  1089. }
  1090. /**
  1091. * @}
  1092. */
  1093. /**
  1094. * @}
  1095. */
  1096. /** @addtogroup CRYP_Private_Functions
  1097. * @{
  1098. */
  1099. /**
  1100. * @brief Write the Key in KeyRx registers.
  1101. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1102. * the configuration information for CRYP module
  1103. * @retval None
  1104. */
  1105. static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp)
  1106. {
  1107. uint32_t keyaddr = 0x0;
  1108. if ((uint32_t)(hcryp->Init.pKey == NULL))
  1109. {
  1110. return HAL_ERROR;
  1111. }
  1112. keyaddr = (uint32_t)(hcryp->Init.pKey);
  1113. if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
  1114. {
  1115. hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
  1116. keyaddr+=4;
  1117. hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
  1118. keyaddr+=4;
  1119. hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
  1120. keyaddr+=4;
  1121. hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
  1122. keyaddr+=4;
  1123. }
  1124. hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
  1125. keyaddr+=4;
  1126. hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
  1127. keyaddr+=4;
  1128. hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
  1129. keyaddr+=4;
  1130. hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
  1131. return HAL_OK;
  1132. }
  1133. /**
  1134. * @brief Write the InitVector/InitCounter in IVRx registers.
  1135. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1136. * the configuration information for CRYP module
  1137. * @retval None
  1138. */
  1139. static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp)
  1140. {
  1141. uint32_t ivaddr = 0x0;
  1142. #if !defined(AES_CR_NPBLB)
  1143. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  1144. {
  1145. hcryp->Instance->IVR3 = 0;
  1146. hcryp->Instance->IVR2 = 0;
  1147. hcryp->Instance->IVR1 = 0;
  1148. hcryp->Instance->IVR0 = 0;
  1149. }
  1150. else
  1151. #endif
  1152. {
  1153. if (hcryp->Init.pInitVect == NULL)
  1154. {
  1155. return HAL_ERROR;
  1156. }
  1157. ivaddr = (uint32_t)(hcryp->Init.pInitVect);
  1158. hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
  1159. ivaddr+=4;
  1160. hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
  1161. ivaddr+=4;
  1162. hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
  1163. ivaddr+=4;
  1164. hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
  1165. }
  1166. return HAL_OK;
  1167. }
  1168. /**
  1169. * @brief Handle CRYP block input/output data handling under interruption.
  1170. * @note The function is called under interruption only, once
  1171. * interruptions have been enabled by HAL_CRYPEx_AES_IT().
  1172. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1173. * the configuration information for CRYP module.
  1174. * @retval HAL status
  1175. */
  1176. static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
  1177. {
  1178. uint32_t inputaddr = 0;
  1179. uint32_t outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1180. if(hcryp->State == HAL_CRYP_STATE_BUSY)
  1181. {
  1182. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  1183. {
  1184. /* Read the last available output block from the Data Output Register */
  1185. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  1186. outputaddr+=4;
  1187. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  1188. outputaddr+=4;
  1189. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  1190. outputaddr+=4;
  1191. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  1192. hcryp->pCrypOutBuffPtr += 16;
  1193. hcryp->CrypOutCount -= 16;
  1194. }
  1195. else
  1196. {
  1197. /* Read the derived key from the Key registers */
  1198. if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
  1199. {
  1200. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
  1201. outputaddr+=4;
  1202. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
  1203. outputaddr+=4;
  1204. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
  1205. outputaddr+=4;
  1206. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
  1207. outputaddr+=4;
  1208. }
  1209. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
  1210. outputaddr+=4;
  1211. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
  1212. outputaddr+=4;
  1213. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
  1214. outputaddr+=4;
  1215. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
  1216. }
  1217. /* In case of ciphering or deciphering, check if all output text has been retrieved;
  1218. In case of key derivation, stop right there */
  1219. if ((hcryp->CrypOutCount == 0) || (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION))
  1220. {
  1221. /* Disable Computation Complete Flag and Errors Interrupts */
  1222. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  1223. /* Change the CRYP state */
  1224. hcryp->State = HAL_CRYP_STATE_READY;
  1225. /* Process Unlocked */
  1226. __HAL_UNLOCK(hcryp);
  1227. /* Call computation complete callback */
  1228. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  1229. return HAL_OK;
  1230. }
  1231. /* If suspension flag has been raised, suspend processing */
  1232. else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  1233. {
  1234. /* reset ModeSuspend */
  1235. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  1236. /* Disable Computation Complete Flag and Errors Interrupts */
  1237. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  1238. /* Change the CRYP state */
  1239. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  1240. /* Process Unlocked */
  1241. __HAL_UNLOCK(hcryp);
  1242. return HAL_OK;
  1243. }
  1244. else /* Process the rest of input data */
  1245. {
  1246. /* Get the Intput data address */
  1247. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1248. /* Increment/decrement instance pointer/counter */
  1249. hcryp->pCrypInBuffPtr += 16;
  1250. hcryp->CrypInCount -= 16;
  1251. /* Write the next input block in the Data Input register */
  1252. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1253. inputaddr+=4;
  1254. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1255. inputaddr+=4;
  1256. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1257. inputaddr+=4;
  1258. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1259. return HAL_OK;
  1260. }
  1261. }
  1262. else
  1263. {
  1264. return HAL_BUSY;
  1265. }
  1266. }
  1267. /**
  1268. * @}
  1269. */
  1270. /**
  1271. * @}
  1272. */
  1273. /**
  1274. * @}
  1275. */
  1276. #endif /* defined (STM32L442xx) || defined (STM32L443xx) || defined (STM32L462xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L4A6xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) */
  1277. #endif /* HAL_CRYP_MODULE_ENABLED */
  1278. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/