stm32l4xx_hal_cryp.c 53 KB

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