stm32l4xx_hal_cryp.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2017 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file in
  21. * the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. The CRYP HAL driver can be used as follows:
  30. (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
  31. (++) Enable the CRYP interface clock using __HAL_RCC_AES_CLK_ENABLE()
  32. (++) In case of using interrupts (e.g. HAL_CRYP_AES_IT())
  33. (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
  34. (+++) Enable the AES IRQ handler using HAL_NVIC_EnableIRQ()
  35. (+++) In AES IRQ handler, call HAL_CRYP_IRQHandler()
  36. (++) In case of using DMA to control data transfer (e.g. HAL_CRYPEx_AES_DMA())
  37. (+++) Enable the DMA2 interface clock using
  38. __HAL_RCC_DMA2_CLK_ENABLE()
  39. (+++) Configure and enable two DMA channels one for managing data transfer from
  40. memory to peripheral (input channel) and another channel for managing data
  41. transfer from peripheral to memory (output channel)
  42. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  43. using __HAL_LINKDMA()
  44. (+++) Configure the priority and enable the NVIC for the transfer complete
  45. interrupt on the two DMA channels. The output channel should have higher
  46. priority than the input channel.
  47. Resort to HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
  48. (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures:
  49. (++) The data type: 1-bit, 8-bit, 16-bit and 32-bit
  50. (++) The AES operating mode (encryption, key derivation and/or decryption)
  51. (++) The AES chaining mode (ECB, CBC, CTR, GCM, GMAC, CMAC when applicable, CCM when applicable)
  52. (++) The encryption/decryption key if so required
  53. (++) The initialization vector or nonce if applicable (not used in ECB mode).
  54. (#)Three processing (encryption/decryption) functions are available:
  55. (++) Polling mode: encryption and decryption APIs are blocking functions
  56. i.e. they process the data and wait till the processing is finished
  57. (++) Interrupt mode: encryption and decryption APIs are not blocking functions
  58. i.e. they process the data under interrupt
  59. (++) DMA mode: encryption and decryption APIs are not blocking functions
  60. i.e. the data transfer is ensured by DMA
  61. (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
  62. *** Callback registration ***
  63. ===================================
  64. [..]
  65. (#) The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS when set to 1
  66. allows the user to configure dynamically the driver callbacks.
  67. Use function @ref HAL_CRYP_RegisterCallback() to register a user callback.
  68. (#) Function @ref HAL_CRYP_RegisterCallback() allows to register following callbacks:
  69. (+) InCpltCallback : callback for input DMA transfer completion.
  70. (+) OutCpltCallback : callback for output DMA transfer completion.
  71. (+) CompCpltCallback : callback for computation completion.
  72. (+) ErrorCallback : callback for error.
  73. (+) MspInitCallback : CRYP MspInit.
  74. (+) MspDeInitCallback : CRYP MspDeInit.
  75. This function takes as parameters the HAL peripheral handle, the Callback ID
  76. and a pointer to the user callback function.
  77. (#) Use function @ref HAL_CRYP_UnRegisterCallback() to reset a callback to the default
  78. weak (surcharged) function.
  79. @ref HAL_CRYP_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  80. and the Callback ID.
  81. This function allows to reset following callbacks:
  82. (+) InCpltCallback : callback for input DMA transfer completion.
  83. (+) OutCpltCallback : callback for output DMA transfer completion.
  84. (+) CompCpltCallback : callback for computation completion.
  85. (+) ErrorCallback : callback for error.
  86. (+) MspInitCallback : CRYP MspInit.
  87. (+) MspDeInitCallback : CRYP MspDeInit.
  88. (#) By default, after the @ref HAL_CRYP_Init and if the state is HAL_CRYP_STATE_RESET
  89. all callbacks are reset to the corresponding legacy weak (surcharged) functions:
  90. examples @ref HAL_CRYP_InCpltCallback(), @ref HAL_CRYP_ErrorCallback()
  91. Exception done for MspInit and MspDeInit callbacks that are respectively
  92. reset to the legacy weak (surcharged) functions in the @ref HAL_CRYP_Init
  93. and @ref HAL_CRYP_DeInit only when these callbacks are null (not registered beforehand)
  94. If not, MspInit or MspDeInit are not null, the @ref HAL_CRYP_Init and @ref HAL_CRYP_DeInit
  95. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  96. Callbacks can be registered/unregistered in READY state only.
  97. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  98. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  99. during the Init/DeInit.
  100. In that case first register the MspInit/MspDeInit user callbacks
  101. using @ref HAL_CRYP_RegisterCallback before calling @ref HAL_CRYP_DeInit
  102. or @ref HAL_¨CRYP_Init function.
  103. When The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS is set to 0 or
  104. not defined, the callback registering feature is not available
  105. and weak (surcharged) callbacks are used.
  106. @endverbatim
  107. ******************************************************************************
  108. */
  109. /* Includes ------------------------------------------------------------------*/
  110. #include "stm32l4xx_hal.h"
  111. #ifdef HAL_CRYP_MODULE_ENABLED
  112. #if defined(AES)
  113. /** @addtogroup STM32L4xx_HAL_Driver
  114. * @{
  115. */
  116. /** @defgroup CRYP CRYP
  117. * @brief CRYP HAL module driver.
  118. * @{
  119. */
  120. /* Private typedef -----------------------------------------------------------*/
  121. /* Private define ------------------------------------------------------------*/
  122. /* Private macro -------------------------------------------------------------*/
  123. /* Private variables ---------------------------------------------------------*/
  124. /* Private functions --------------------------------------------------------*/
  125. /** @defgroup CRYP_Private_Functions CRYP Private Functions
  126. * @{
  127. */
  128. static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp);
  129. static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp);
  130. static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
  131. /**
  132. * @}
  133. */
  134. /* Exported functions ---------------------------------------------------------*/
  135. /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
  136. * @{
  137. */
  138. /** @defgroup CRYP_Exported_Functions_Group1 Initialization and deinitialization functions
  139. * @brief Initialization and Configuration functions.
  140. *
  141. @verbatim
  142. ==============================================================================
  143. ##### Initialization and deinitialization functions #####
  144. ==============================================================================
  145. [..] This section provides functions allowing to:
  146. (+) Initialize the CRYP according to the specified parameters
  147. in the CRYP_InitTypeDef and creates the associated handle
  148. (+) DeInitialize the CRYP peripheral
  149. (+) Initialize the CRYP MSP (MCU Specific Package)
  150. (+) De-Initialize the CRYP MSP
  151. [..]
  152. (@) Specific care must be taken to format the key and the Initialization Vector IV!
  153. [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where
  154. b127 is the MSB and b0 the LSB, the key must be stored in MCU memory
  155. (+) as a sequence of words where the MSB word comes first (occupies the
  156. lowest memory address)
  157. (+) where each word is byte-swapped:
  158. (++) address n+0 : 0b b103 .. b96 b111 .. b104 b119 .. b112 b127 .. b120
  159. (++) address n+4 : 0b b71 .. b64 b79 .. b72 b87 .. b80 b95 .. b88
  160. (++) address n+8 : 0b b39 .. b32 b47 .. b40 b55 .. b48 b63 .. b56
  161. (++) address n+C : 0b b7 .. b0 b15 .. b8 b23 .. b16 b31 .. b24
  162. [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}.
  163. The 4 32-bit words that make the key must be stored as follows in MCU memory:
  164. (+) address n+0 : 0x B12 B13 B14 B15
  165. (+) address n+4 : 0x B8 B9 B10 B11
  166. (+) address n+8 : 0x B4 B5 B6 B7
  167. (+) address n+C : 0x B0 B1 B2 B3
  168. [..] which leads to the expected setting
  169. (+) AES_KEYR3 = 0x B15 B14 B13 B12
  170. (+) AES_KEYR2 = 0x B11 B10 B9 B8
  171. (+) AES_KEYR1 = 0x B7 B6 B5 B4
  172. (+) AES_KEYR0 = 0x B3 B2 B1 B0
  173. [..] Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}.
  174. The 8 32-bit words that make the key must be stored as follows in MCU memory:
  175. (+) address n+00 : 0x B28 B29 B30 B31
  176. (+) address n+04 : 0x B24 B25 B26 B27
  177. (+) address n+08 : 0x B20 B21 B22 B23
  178. (+) address n+0C : 0x B16 B17 B18 B19
  179. (+) address n+10 : 0x B12 B13 B14 B15
  180. (+) address n+14 : 0x B8 B9 B10 B11
  181. (+) address n+18 : 0x B4 B5 B6 B7
  182. (+) address n+1C : 0x B0 B1 B2 B3
  183. [..] which leads to the expected setting
  184. (+) AES_KEYR7 = 0x B31 B30 B29 B28
  185. (+) AES_KEYR6 = 0x B27 B26 B25 B24
  186. (+) AES_KEYR5 = 0x B23 B22 B21 B20
  187. (+) AES_KEYR4 = 0x B19 B18 B17 B16
  188. (+) AES_KEYR3 = 0x B15 B14 B13 B12
  189. (+) AES_KEYR2 = 0x B11 B10 B9 B8
  190. (+) AES_KEYR1 = 0x B7 B6 B5 B4
  191. (+) AES_KEYR0 = 0x B3 B2 B1 B0
  192. [..] Initialization Vector IV (4 32-bit words) format must follow the same as
  193. that of a 128-bit long key.
  194. [..]
  195. @endverbatim
  196. * @{
  197. */
  198. /**
  199. * @brief Initialize the CRYP according to the specified
  200. * parameters in the CRYP_InitTypeDef and initialize the associated handle.
  201. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  202. * the configuration information for CRYP module
  203. * @note Specific care must be taken to format the key and the Initialization Vector IV
  204. * stored in the MCU memory before calling HAL_CRYP_Init(). Refer to explanations
  205. * hereabove.
  206. * @retval HAL status
  207. */
  208. HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
  209. {
  210. /* Check the CRYP handle allocation */
  211. if(hcryp == NULL)
  212. {
  213. return HAL_ERROR;
  214. }
  215. /* Check the instance */
  216. assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance));
  217. /* Check the parameters */
  218. assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
  219. assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
  220. assert_param(IS_CRYP_ALGOMODE(hcryp->Init.OperatingMode));
  221. /* ChainingMode parameter is irrelevant when mode is set to Key derivation */
  222. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  223. {
  224. assert_param(IS_CRYP_CHAINMODE(hcryp->Init.ChainingMode));
  225. }
  226. assert_param(IS_CRYP_WRITE(hcryp->Init.KeyWriteFlag));
  227. /*========================================================*/
  228. /* Check the proper operating/chaining modes combinations */
  229. /*========================================================*/
  230. /* Check the proper chaining when the operating mode is key derivation and decryption */
  231. #if defined(AES_CR_NPBLB)
  232. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
  233. ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \
  234. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \
  235. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)))
  236. #else
  237. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
  238. ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \
  239. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \
  240. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)))
  241. #endif
  242. {
  243. return HAL_ERROR;
  244. }
  245. /* Check that key derivation is not set in CMAC mode or CCM mode when applicable */
  246. #if defined(AES_CR_NPBLB)
  247. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  248. && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
  249. #else
  250. if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  251. && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
  252. #endif
  253. {
  254. return HAL_ERROR;
  255. }
  256. /*================*/
  257. /* Initialization */
  258. /*================*/
  259. /* Initialization start */
  260. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  261. if (hcryp->State == HAL_CRYP_STATE_RESET)
  262. {
  263. /* Allocate lock resource and initialize it */
  264. hcryp->Lock = HAL_UNLOCKED;
  265. /* Reset Callback pointers in HAL_CRYP_STATE_RESET only */
  266. hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak (surcharged) input DMA transfer completion callback */
  267. hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak (surcharged) output DMA transfer completion callback */
  268. hcryp->CompCpltCallback = HAL_CRYPEx_ComputationCpltCallback; /* Legacy weak (surcharged) computation completion callback */
  269. hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak (surcharged) error callback */
  270. if(hcryp->MspInitCallback == NULL)
  271. {
  272. hcryp->MspInitCallback = HAL_CRYP_MspInit;
  273. }
  274. /* Init the low level hardware */
  275. hcryp->MspInitCallback(hcryp);
  276. }
  277. #else
  278. if(hcryp->State == HAL_CRYP_STATE_RESET)
  279. {
  280. /* Allocate lock resource and initialize it */
  281. hcryp->Lock = HAL_UNLOCKED;
  282. /* Init the low level hardware */
  283. HAL_CRYP_MspInit(hcryp);
  284. }
  285. #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
  286. /* Change the CRYP state */
  287. hcryp->State = HAL_CRYP_STATE_BUSY;
  288. /* Disable the Peripheral */
  289. __HAL_CRYP_DISABLE(hcryp);
  290. /*=============================================================*/
  291. /* AES initialization common to all operating modes */
  292. /*=============================================================*/
  293. /* Set the Key size selection */
  294. MODIFY_REG(hcryp->Instance->CR, AES_CR_KEYSIZE, hcryp->Init.KeySize);
  295. /* Set the default CRYP phase when this parameter is not used.
  296. Phase is updated below in case of GCM/GMAC(/CMAC)(/CCM) setting. */
  297. hcryp->Phase = HAL_CRYP_PHASE_NOT_USED;
  298. /*=============================================================*/
  299. /* Carry on the initialization based on the AES operating mode */
  300. /*=============================================================*/
  301. /* Key derivation */
  302. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  303. {
  304. MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_ALGOMODE_KEYDERIVATION);
  305. /* Configure the Key registers */
  306. if (CRYP_SetKey(hcryp) != HAL_OK)
  307. {
  308. return HAL_ERROR;
  309. }
  310. }
  311. else
  312. /* Encryption / Decryption (with or without key derivation) / authentication */
  313. {
  314. #if !defined(AES_CR_NPBLB)
  315. /* Set data type, operating and chaining modes.
  316. In case of GCM or GMAC, data type is forced to 0b00 */
  317. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  318. {
  319. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
  320. }
  321. else
  322. #endif
  323. {
  324. MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.DataType|hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
  325. }
  326. /* Specify the encryption/decryption phase in case of Galois counter mode (GCM),
  327. Galois message authentication code (GMAC), cipher message authentication code (CMAC) when applicable
  328. or Counter with Cipher Mode (CCM) when applicable */
  329. #if defined(AES_CR_NPBLB)
  330. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  331. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
  332. #else
  333. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  334. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
  335. #endif
  336. {
  337. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, hcryp->Init.GCMCMACPhase);
  338. hcryp->Phase = HAL_CRYP_PHASE_START;
  339. }
  340. /* Configure the Key registers if no need to bypass this step */
  341. if (hcryp->Init.KeyWriteFlag == CRYP_KEY_WRITE_ENABLE)
  342. {
  343. if (CRYP_SetKey(hcryp) != HAL_OK)
  344. {
  345. return HAL_ERROR;
  346. }
  347. }
  348. /* If applicable, configure the Initialization Vector */
  349. if (hcryp->Init.ChainingMode != CRYP_CHAINMODE_AES_ECB)
  350. {
  351. if (CRYP_SetInitVector(hcryp) != HAL_OK)
  352. {
  353. return HAL_ERROR;
  354. }
  355. }
  356. }
  357. #if defined(AES_CR_NPBLB)
  358. /* Clear NPBLB field */
  359. CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
  360. #endif
  361. /* Reset CrypInCount and CrypOutCount */
  362. hcryp->CrypInCount = 0;
  363. hcryp->CrypOutCount = 0;
  364. /* Reset ErrorCode field */
  365. hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
  366. /* Reset Mode suspension request */
  367. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  368. /* Change the CRYP state */
  369. hcryp->State = HAL_CRYP_STATE_READY;
  370. /* Enable the Peripheral */
  371. __HAL_CRYP_ENABLE(hcryp);
  372. /* Return function status */
  373. return HAL_OK;
  374. }
  375. /**
  376. * @brief DeInitialize the CRYP peripheral.
  377. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  378. * the configuration information for CRYP module
  379. * @retval HAL status
  380. */
  381. HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
  382. {
  383. /* Check the CRYP handle allocation */
  384. if(hcryp == NULL)
  385. {
  386. return HAL_ERROR;
  387. }
  388. /* Change the CRYP state */
  389. hcryp->State = HAL_CRYP_STATE_BUSY;
  390. /* Set the default CRYP phase */
  391. hcryp->Phase = HAL_CRYP_PHASE_READY;
  392. /* Reset CrypInCount and CrypOutCount */
  393. hcryp->CrypInCount = 0;
  394. hcryp->CrypOutCount = 0;
  395. /* Disable the CRYP Peripheral Clock */
  396. __HAL_CRYP_DISABLE(hcryp);
  397. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  398. if(hcryp->MspDeInitCallback == NULL)
  399. {
  400. hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;
  401. }
  402. /* DeInit the low level hardware */
  403. hcryp->MspDeInitCallback(hcryp);
  404. #else
  405. /* DeInit the low level hardware: CLOCK, NVIC.*/
  406. HAL_CRYP_MspDeInit(hcryp);
  407. #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
  408. /* Change the CRYP state */
  409. hcryp->State = HAL_CRYP_STATE_RESET;
  410. /* Release Lock */
  411. __HAL_UNLOCK(hcryp);
  412. /* Return function status */
  413. return HAL_OK;
  414. }
  415. /**
  416. * @brief Initialize the CRYP MSP.
  417. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  418. * the configuration information for CRYP module
  419. * @retval None
  420. */
  421. __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
  422. {
  423. /* Prevent unused argument(s) compilation warning */
  424. UNUSED(hcryp);
  425. /* NOTE : This function should not be modified; when the callback is needed,
  426. the HAL_CRYP_MspInit can be implemented in the user file
  427. */
  428. }
  429. /**
  430. * @brief DeInitialize CRYP MSP.
  431. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  432. * the configuration information for CRYP module
  433. * @retval None
  434. */
  435. __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
  436. {
  437. /* Prevent unused argument(s) compilation warning */
  438. UNUSED(hcryp);
  439. /* NOTE : This function should not be modified; when the callback is needed,
  440. the HAL_CRYP_MspDeInit can be implemented in the user file
  441. */
  442. }
  443. /**
  444. * @}
  445. */
  446. /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
  447. * @brief Processing functions.
  448. *
  449. @verbatim
  450. ==============================================================================
  451. ##### AES processing functions #####
  452. ==============================================================================
  453. [..] This section provides functions allowing to:
  454. (+) Encrypt plaintext using AES algorithm in different chaining modes
  455. (+) Decrypt ciphertext using AES algorithm in different chaining modes
  456. [..] Three processing functions are available:
  457. (+) Polling mode
  458. (+) Interrupt mode
  459. (+) DMA mode
  460. @endverbatim
  461. * @{
  462. */
  463. /**
  464. * @brief Encrypt pPlainData in AES ECB encryption mode. The cypher data are available in pCypherData.
  465. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  466. * the configuration information for CRYP module
  467. * @param pPlainData Pointer to the plaintext buffer
  468. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  469. * @param pCypherData Pointer to the ciphertext buffer
  470. * @param Timeout Specify Timeout value
  471. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  472. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  473. * @retval HAL status
  474. */
  475. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  476. {
  477. /* Re-initialize AES IP with proper parameters */
  478. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  479. {
  480. return HAL_ERROR;
  481. }
  482. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  483. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  484. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  485. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  486. {
  487. return HAL_ERROR;
  488. }
  489. return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
  490. }
  491. /**
  492. * @brief Encrypt pPlainData in AES CBC encryption mode with key derivation. The cypher data are available in pCypherData.
  493. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  494. * the configuration information for CRYP module
  495. * @param pPlainData Pointer to the plaintext buffer
  496. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  497. * @param pCypherData Pointer to the ciphertext 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_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, 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_ENCRYPT;
  511. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  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, pPlainData, Size, pCypherData, Timeout);
  518. }
  519. /**
  520. * @brief Encrypt pPlainData in AES CTR encryption mode. The cypher data are available in pCypherData
  521. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  522. * the configuration information for CRYP module
  523. * @param pPlainData Pointer to the plaintext buffer
  524. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  525. * @param pCypherData Pointer to the ciphertext buffer
  526. * @param Timeout Specify Timeout value
  527. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  528. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  529. * @retval HAL status
  530. */
  531. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
  532. {
  533. /* Re-initialize AES IP with proper parameters */
  534. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  535. {
  536. return HAL_ERROR;
  537. }
  538. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  539. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  540. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  541. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  542. {
  543. return HAL_ERROR;
  544. }
  545. return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
  546. }
  547. /**
  548. * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation,
  549. * the deciphered data are available in pPlainData.
  550. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  551. * the configuration information for CRYP module
  552. * @param pCypherData Pointer to the ciphertext buffer
  553. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  554. * @param pPlainData Pointer to the plaintext buffer
  555. * @param Timeout Specify Timeout value
  556. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  557. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  558. * @retval HAL status
  559. */
  560. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  561. {
  562. /* Re-initialize AES IP with proper parameters */
  563. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  564. {
  565. return HAL_ERROR;
  566. }
  567. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  568. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  569. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  570. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  571. {
  572. return HAL_ERROR;
  573. }
  574. return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
  575. }
  576. /**
  577. * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation,
  578. * the deciphered data are available in pPlainData.
  579. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  580. * the configuration information for CRYP module
  581. * @param pCypherData Pointer to the ciphertext buffer
  582. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  583. * @param pPlainData Pointer to the plaintext buffer
  584. * @param Timeout Specify Timeout value
  585. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  586. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  587. * @retval HAL status
  588. */
  589. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  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_KEYDERIVATION_DECRYPT;
  597. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  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(hcryp, pCypherData, Size, pPlainData, Timeout);
  604. }
  605. /**
  606. * @brief Decrypt pCypherData in AES CTR decryption mode,
  607. * the deciphered data are available in pPlainData.
  608. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  609. * the configuration information for CRYP module
  610. * @param pCypherData Pointer to the ciphertext buffer
  611. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  612. * @param pPlainData Pointer to the plaintext buffer
  613. * @param Timeout Specify Timeout value
  614. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  615. * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
  616. * @retval HAL status
  617. */
  618. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
  619. {
  620. /* Re-initialize AES IP with proper parameters */
  621. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  622. {
  623. return HAL_ERROR;
  624. }
  625. hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  626. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  627. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  628. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  629. {
  630. return HAL_ERROR;
  631. }
  632. return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
  633. }
  634. /**
  635. * @brief Encrypt pPlainData in AES ECB encryption mode using Interrupt,
  636. * the cypher data are available in pCypherData.
  637. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  638. * the configuration information for CRYP module
  639. * @param pPlainData Pointer to the plaintext buffer
  640. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  641. * @param pCypherData Pointer to the ciphertext buffer
  642. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  643. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  644. * @retval HAL status
  645. */
  646. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  647. {
  648. /* Re-initialize AES IP with proper parameters */
  649. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  650. {
  651. return HAL_ERROR;
  652. }
  653. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  654. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  655. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  656. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  657. {
  658. return HAL_ERROR;
  659. }
  660. return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
  661. }
  662. /**
  663. * @brief Encrypt pPlainData in AES CBC encryption mode using Interrupt,
  664. * the cypher data are available in pCypherData.
  665. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  666. * the configuration information for CRYP module
  667. * @param pPlainData Pointer to the plaintext buffer
  668. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  669. * @param pCypherData Pointer to the ciphertext buffer
  670. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  671. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  672. * @retval HAL status
  673. */
  674. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  675. {
  676. /* Re-initialize AES IP with proper parameters */
  677. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  678. {
  679. return HAL_ERROR;
  680. }
  681. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  682. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  683. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  684. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  685. {
  686. return HAL_ERROR;
  687. }
  688. return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
  689. }
  690. /**
  691. * @brief Encrypt pPlainData in AES CTR encryption mode using Interrupt,
  692. * the cypher data are available in pCypherData.
  693. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  694. * the configuration information for CRYP module
  695. * @param pPlainData Pointer to the plaintext buffer
  696. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  697. * @param pCypherData Pointer to the ciphertext buffer
  698. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  699. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  700. * @retval HAL status
  701. */
  702. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  703. {
  704. /* Re-initialize AES IP with proper parameters */
  705. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  706. {
  707. return HAL_ERROR;
  708. }
  709. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  710. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  711. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  712. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  713. {
  714. return HAL_ERROR;
  715. }
  716. return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
  717. }
  718. /**
  719. * @brief Decrypt pCypherData in AES ECB decryption mode using Interrupt,
  720. * the deciphered data are available in pPlainData.
  721. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  722. * the configuration information for CRYP module
  723. * @param pCypherData Pointer to the ciphertext buffer
  724. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  725. * @param pPlainData Pointer to the plaintext buffer.
  726. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  727. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  728. * @retval HAL status
  729. */
  730. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  731. {
  732. /* Re-initialize AES IP with proper parameters */
  733. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  734. {
  735. return HAL_ERROR;
  736. }
  737. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  738. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  739. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  740. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  741. {
  742. return HAL_ERROR;
  743. }
  744. return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
  745. }
  746. /**
  747. * @brief Decrypt pCypherData in AES CBC decryption mode using Interrupt,
  748. * the deciphered data are available in pPlainData.
  749. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  750. * the configuration information for CRYP module
  751. * @param pCypherData Pointer to the ciphertext buffer
  752. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  753. * @param pPlainData Pointer to the plaintext buffer
  754. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  755. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  756. * @retval HAL status
  757. */
  758. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  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_KEYDERIVATION_DECRYPT;
  766. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  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_IT(hcryp, pCypherData, Size, pPlainData);
  773. }
  774. /**
  775. * @brief Decrypt pCypherData in AES CTR decryption mode using Interrupt,
  776. * the deciphered data are available in pPlainData.
  777. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  778. * the configuration information for CRYP module
  779. * @param pCypherData Pointer to the ciphertext buffer
  780. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  781. * @param pPlainData Pointer to the plaintext buffer
  782. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  783. * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
  784. * @retval HAL status
  785. */
  786. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  787. {
  788. /* Re-initialize AES IP with proper parameters */
  789. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  790. {
  791. return HAL_ERROR;
  792. }
  793. hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  794. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  795. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  796. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  797. {
  798. return HAL_ERROR;
  799. }
  800. return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
  801. }
  802. /**
  803. * @brief Encrypt pPlainData in AES ECB encryption mode using DMA,
  804. * the cypher data are available in pCypherData.
  805. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  806. * the configuration information for CRYP module
  807. * @param pPlainData Pointer to the plaintext buffer
  808. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  809. * @param pCypherData Pointer to the ciphertext buffer
  810. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  811. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  812. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  813. * @retval HAL status
  814. */
  815. HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  816. {
  817. /* Re-initialize AES IP with proper parameters */
  818. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  819. {
  820. return HAL_ERROR;
  821. }
  822. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  823. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  824. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  825. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  826. {
  827. return HAL_ERROR;
  828. }
  829. return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
  830. }
  831. /**
  832. * @brief Encrypt pPlainData in AES CBC encryption mode using DMA,
  833. * the cypher data are available in pCypherData.
  834. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  835. * the configuration information for CRYP module
  836. * @param pPlainData Pointer to the plaintext buffer
  837. * @param Size Length of the plaintext buffer, must be a multiple of 16.
  838. * @param pCypherData Pointer to the ciphertext buffer
  839. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  840. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  841. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  842. * @retval HAL status
  843. */
  844. HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  845. {
  846. /* Re-initialize AES IP with proper parameters */
  847. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  848. {
  849. return HAL_ERROR;
  850. }
  851. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  852. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  853. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  854. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  855. {
  856. return HAL_ERROR;
  857. }
  858. return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
  859. }
  860. /**
  861. * @brief Encrypt pPlainData in AES CTR encryption mode using DMA,
  862. * the cypher data are available in pCypherData.
  863. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  864. * the configuration information for CRYP module
  865. * @param pPlainData Pointer to the plaintext buffer
  866. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  867. * @param pCypherData Pointer to the ciphertext buffer.
  868. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  869. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  870. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  871. * @retval HAL status
  872. */
  873. HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
  874. {
  875. /* Re-initialize AES IP with proper parameters */
  876. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  877. {
  878. return HAL_ERROR;
  879. }
  880. hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
  881. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  882. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  883. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  884. {
  885. return HAL_ERROR;
  886. }
  887. return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
  888. }
  889. /**
  890. * @brief Decrypt pCypherData in AES ECB decryption mode using DMA,
  891. * the deciphered data are available in pPlainData.
  892. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  893. * the configuration information for CRYP module
  894. * @param pCypherData Pointer to the ciphertext buffer
  895. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  896. * @param pPlainData Pointer to the plaintext buffer
  897. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  898. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  899. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  900. * @retval HAL status
  901. */
  902. HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  903. {
  904. /* Re-initialize AES IP with proper parameters */
  905. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  906. {
  907. return HAL_ERROR;
  908. }
  909. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  910. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
  911. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  912. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  913. {
  914. return HAL_ERROR;
  915. }
  916. return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
  917. }
  918. /**
  919. * @brief Decrypt pCypherData in AES CBC decryption mode using DMA,
  920. * the deciphered data are available in pPlainData.
  921. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  922. * the configuration information for CRYP module
  923. * @param pCypherData Pointer to the ciphertext buffer
  924. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  925. * @param pPlainData Pointer to the plaintext buffer
  926. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  927. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  928. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  929. * @retval HAL status
  930. */
  931. HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  932. {
  933. /* Re-initialize AES IP with proper parameters */
  934. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  935. {
  936. return HAL_ERROR;
  937. }
  938. hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
  939. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
  940. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  941. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  942. {
  943. return HAL_ERROR;
  944. }
  945. return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
  946. }
  947. /**
  948. * @brief Decrypt pCypherData in AES CTR decryption mode using DMA,
  949. * the deciphered data are available in pPlainData.
  950. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  951. * the configuration information for CRYP module
  952. * @param pCypherData Pointer to the ciphertext buffer
  953. * @param Size Length of the plaintext buffer in bytes, must be a multiple of 16.
  954. * @param pPlainData Pointer to the plaintext buffer
  955. * @note This API is provided only to maintain compatibility with legacy software. Users should directly
  956. * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
  957. * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  958. * @retval HAL status
  959. */
  960. HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
  961. {
  962. /* Re-initialize AES IP with proper parameters */
  963. if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  964. {
  965. return HAL_ERROR;
  966. }
  967. hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  968. hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  969. hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  970. if (HAL_CRYP_Init(hcryp) != HAL_OK)
  971. {
  972. return HAL_ERROR;
  973. }
  974. return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
  975. }
  976. /**
  977. * @}
  978. */
  979. /** @defgroup CRYP_Exported_Functions_Group3 Callback functions
  980. * @brief Callback functions.
  981. *
  982. @verbatim
  983. ==============================================================================
  984. ##### Callback functions #####
  985. ==============================================================================
  986. [..] This section provides Interruption and DMA callback functions:
  987. (+) DMA Input data transfer complete
  988. (+) DMA Output data transfer complete
  989. (+) DMA or Interrupt error
  990. @endverbatim
  991. * @{
  992. */
  993. /**
  994. * @brief CRYP error callback.
  995. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  996. * the configuration information for CRYP module
  997. * @retval None
  998. */
  999. __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
  1000. {
  1001. /* Prevent unused argument(s) compilation warning */
  1002. UNUSED(hcryp);
  1003. /* NOTE : This function should not be modified; when the callback is needed,
  1004. the HAL_CRYP_ErrorCallback can be implemented in the user file
  1005. */
  1006. }
  1007. /**
  1008. * @brief Input DMA transfer complete callback.
  1009. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1010. * the configuration information for CRYP module
  1011. * @retval None
  1012. */
  1013. __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
  1014. {
  1015. /* Prevent unused argument(s) compilation warning */
  1016. UNUSED(hcryp);
  1017. /* NOTE : This function should not be modified; when the callback is needed,
  1018. the HAL_CRYP_InCpltCallback can be implemented in the user file
  1019. */
  1020. }
  1021. /**
  1022. * @brief Output DMA transfer complete callback.
  1023. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1024. * the configuration information for CRYP module
  1025. * @retval None
  1026. */
  1027. __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
  1028. {
  1029. /* Prevent unused argument(s) compilation warning */
  1030. UNUSED(hcryp);
  1031. /* NOTE : This function should not be modified; when the callback is needed,
  1032. the HAL_CRYP_OutCpltCallback can be implemented in the user file
  1033. */
  1034. }
  1035. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  1036. /**
  1037. * @brief Register a User CRYP Callback
  1038. * To be used instead of the weak (surcharged) predefined callback
  1039. * @param hcryp CRYP handle
  1040. * @param CallbackID ID of the callback to be registered
  1041. * This parameter can be one of the following values:
  1042. * @arg @ref HAL_CRYP_INPUTCPLT_CB_ID CRYP input DMA transfer completion Callback ID
  1043. * @arg @ref HAL_CRYP_OUTPUTCPLT_CB_ID CRYP output DMA transfer completion Callback ID
  1044. * @arg @ref HAL_CRYP_COMPCPLT_CB_ID CRYP computation completion Callback ID
  1045. * @arg @ref HAL_CRYP_ERROR_CB_ID CRYP error callback ID
  1046. * @arg @ref HAL_CRYP_MSPINIT_CB_ID CRYP MspDeInit callback ID
  1047. * @arg @ref HAL_CRYP_MSPDEINIT_CB_ID CRYP MspDeInit callback ID
  1048. * @param pCallback pointer to the Callback function
  1049. * @retval status
  1050. */
  1051. HAL_StatusTypeDef HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID, pCRYP_CallbackTypeDef pCallback)
  1052. {
  1053. HAL_StatusTypeDef status = HAL_OK;
  1054. if(pCallback == NULL)
  1055. {
  1056. /* Update the error code */
  1057. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  1058. return HAL_ERROR;
  1059. }
  1060. /* Process locked */
  1061. __HAL_LOCK(hcryp);
  1062. if(HAL_CRYP_STATE_READY == hcryp->State)
  1063. {
  1064. switch (CallbackID)
  1065. {
  1066. case HAL_CRYP_INPUTCPLT_CB_ID :
  1067. hcryp->InCpltCallback = pCallback;
  1068. break;
  1069. case HAL_CRYP_OUTPUTCPLT_CB_ID :
  1070. hcryp->OutCpltCallback = pCallback;
  1071. break;
  1072. case HAL_CRYP_COMPCPLT_CB_ID :
  1073. hcryp->CompCpltCallback = pCallback;
  1074. break;
  1075. case HAL_CRYP_ERROR_CB_ID :
  1076. hcryp->ErrorCallback = pCallback;
  1077. break;
  1078. case HAL_CRYP_MSPINIT_CB_ID :
  1079. hcryp->MspInitCallback = pCallback;
  1080. break;
  1081. case HAL_CRYP_MSPDEINIT_CB_ID :
  1082. hcryp->MspDeInitCallback = pCallback;
  1083. break;
  1084. default :
  1085. /* Update the error code */
  1086. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  1087. /* update return status */
  1088. status = HAL_ERROR;
  1089. break;
  1090. }
  1091. }
  1092. else if(HAL_CRYP_STATE_RESET == hcryp->State)
  1093. {
  1094. switch (CallbackID)
  1095. {
  1096. case HAL_CRYP_MSPINIT_CB_ID :
  1097. hcryp->MspInitCallback = pCallback;
  1098. break;
  1099. case HAL_CRYP_MSPDEINIT_CB_ID :
  1100. hcryp->MspDeInitCallback = pCallback;
  1101. break;
  1102. default :
  1103. /* Update the error code */
  1104. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  1105. /* update return status */
  1106. status = HAL_ERROR;
  1107. break;
  1108. }
  1109. }
  1110. else
  1111. {
  1112. /* Update the error code */
  1113. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  1114. /* update return status */
  1115. status = HAL_ERROR;
  1116. }
  1117. /* Release Lock */
  1118. __HAL_UNLOCK(hcryp);
  1119. return status;
  1120. }
  1121. /**
  1122. * @brief Unregister a CRYP Callback
  1123. * CRYP Callback is redirected to the weak (surcharged) predefined callback
  1124. * @param hcryp CRYP handle
  1125. * @param CallbackID ID of the callback to be unregistered
  1126. * This parameter can be one of the following values:
  1127. * @arg @ref HAL_CRYP_INPUTCPLT_CB_ID CRYP input DMA transfer completion Callback ID
  1128. * @arg @ref HAL_CRYP_OUTPUTCPLT_CB_ID CRYP output DMA transfer completion Callback ID
  1129. * @arg @ref HAL_CRYP_COMPCPLT_CB_ID CRYP computation completion Callback ID
  1130. * @arg @ref HAL_CRYP_ERROR_CB_ID CRYP error callback ID
  1131. * @arg @ref HAL_CRYP_MSPINIT_CB_ID CRYP MspDeInit callback ID
  1132. * @arg @ref HAL_CRYP_MSPDEINIT_CB_ID CRYP MspDeInit callback ID
  1133. * @retval status
  1134. */
  1135. HAL_StatusTypeDef HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID)
  1136. {
  1137. HAL_StatusTypeDef status = HAL_OK;
  1138. /* Process locked */
  1139. __HAL_LOCK(hcryp);
  1140. if(HAL_CRYP_STATE_READY == hcryp->State)
  1141. {
  1142. switch (CallbackID)
  1143. {
  1144. case HAL_CRYP_INPUTCPLT_CB_ID :
  1145. hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak (surcharged) input DMA transfer completion callback */
  1146. break;
  1147. case HAL_CRYP_OUTPUTCPLT_CB_ID :
  1148. hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak (surcharged) output DMA transfer completion callback */
  1149. break;
  1150. case HAL_CRYP_COMPCPLT_CB_ID :
  1151. hcryp->CompCpltCallback = HAL_CRYPEx_ComputationCpltCallback; /* Legacy weak (surcharged) computation completion callback */
  1152. break;
  1153. case HAL_CRYP_ERROR_CB_ID :
  1154. hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak (surcharged) error callback */
  1155. break;
  1156. case HAL_CRYP_MSPINIT_CB_ID :
  1157. hcryp->MspInitCallback = HAL_CRYP_MspInit; /* Legacy weak (surcharged) Msp DeInit */
  1158. break;
  1159. case HAL_CRYP_MSPDEINIT_CB_ID :
  1160. hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit; /* Legacy weak (surcharged) Msp DeInit */
  1161. break;
  1162. default :
  1163. /* Update the error code */
  1164. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  1165. /* update return status */
  1166. status = HAL_ERROR;
  1167. break;
  1168. }
  1169. }
  1170. else if(HAL_CRYP_STATE_RESET == hcryp->State)
  1171. {
  1172. switch (CallbackID)
  1173. {
  1174. case HAL_CRYP_MSPINIT_CB_ID :
  1175. hcryp->MspInitCallback = HAL_CRYP_MspInit; /* Legacy weak (surcharged) Msp Init */
  1176. break;
  1177. case HAL_CRYP_MSPDEINIT_CB_ID :
  1178. hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit; /* Legacy weak (surcharged) Msp DeInit */
  1179. break;
  1180. default :
  1181. /* Update the error code */
  1182. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  1183. /* update return status */
  1184. status = HAL_ERROR;
  1185. break;
  1186. }
  1187. }
  1188. else
  1189. {
  1190. /* Update the error code */
  1191. hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
  1192. /* update return status */
  1193. status = HAL_ERROR;
  1194. }
  1195. /* Release Lock */
  1196. __HAL_UNLOCK(hcryp);
  1197. return status;
  1198. }
  1199. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  1200. /**
  1201. * @}
  1202. */
  1203. /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
  1204. * @brief AES IRQ handler.
  1205. *
  1206. @verbatim
  1207. ==============================================================================
  1208. ##### AES IRQ handler management #####
  1209. ==============================================================================
  1210. [..] This section provides AES IRQ handler function.
  1211. @endverbatim
  1212. * @{
  1213. */
  1214. /**
  1215. * @brief Handle AES interrupt request.
  1216. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1217. * the configuration information for CRYP module
  1218. * @retval None
  1219. */
  1220. void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
  1221. {
  1222. /* Check if error occurred */
  1223. if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_ERRIE) != RESET)
  1224. {
  1225. /* If Write Error occurred */
  1226. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_WRERR) != RESET)
  1227. {
  1228. hcryp->ErrorCode |= HAL_CRYP_WRITE_ERROR;
  1229. hcryp->State = HAL_CRYP_STATE_ERROR;
  1230. }
  1231. /* If Read Error occurred */
  1232. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_RDERR) != RESET)
  1233. {
  1234. hcryp->ErrorCode |= HAL_CRYP_READ_ERROR;
  1235. hcryp->State = HAL_CRYP_STATE_ERROR;
  1236. }
  1237. /* If an error has been reported */
  1238. if (hcryp->State == HAL_CRYP_STATE_ERROR)
  1239. {
  1240. /* Disable Error and Computation Complete Interrupts */
  1241. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  1242. /* Clear all Interrupt flags */
  1243. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR|CRYP_CCF_CLEAR);
  1244. /* Process Unlocked */
  1245. __HAL_UNLOCK(hcryp);
  1246. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  1247. hcryp->ErrorCallback(hcryp);
  1248. #else
  1249. HAL_CRYP_ErrorCallback(hcryp);
  1250. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  1251. return;
  1252. }
  1253. }
  1254. /* Check if computation complete interrupt is enabled
  1255. and if the computation complete flag is raised */
  1256. if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_CCF) != RESET)
  1257. {
  1258. if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_CCFIE) != RESET)
  1259. {
  1260. #if defined(AES_CR_NPBLB)
  1261. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  1262. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
  1263. #else
  1264. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  1265. || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
  1266. #endif
  1267. {
  1268. /* To ensure proper suspension requests management, CCF flag
  1269. is reset in CRYP_AES_Auth_IT() according to the current
  1270. phase under handling */
  1271. if (CRYP_AES_Auth_IT(hcryp) != HAL_OK)
  1272. {
  1273. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  1274. hcryp->ErrorCallback(hcryp);
  1275. #else
  1276. HAL_CRYP_ErrorCallback(hcryp);
  1277. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  1278. }
  1279. }
  1280. else
  1281. {
  1282. /* Clear Computation Complete Flag */
  1283. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1284. if (CRYP_AES_IT(hcryp) != HAL_OK)
  1285. {
  1286. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  1287. hcryp->ErrorCallback(hcryp);
  1288. #else
  1289. HAL_CRYP_ErrorCallback(hcryp);
  1290. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  1291. }
  1292. }
  1293. }
  1294. }
  1295. }
  1296. /**
  1297. * @}
  1298. */
  1299. /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
  1300. * @brief Peripheral State functions.
  1301. *
  1302. @verbatim
  1303. ==============================================================================
  1304. ##### Peripheral State functions #####
  1305. ==============================================================================
  1306. [..]
  1307. This subsection permits to get in run-time the status of the peripheral.
  1308. @endverbatim
  1309. * @{
  1310. */
  1311. /**
  1312. * @brief Return the CRYP handle state.
  1313. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1314. * the configuration information for CRYP module
  1315. * @retval HAL state
  1316. */
  1317. HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
  1318. {
  1319. /* Return CRYP handle state */
  1320. return hcryp->State;
  1321. }
  1322. /**
  1323. * @brief Return the CRYP peripheral error.
  1324. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1325. * the configuration information for CRYP module
  1326. * @note The returned error is a bit-map combination of possible errors
  1327. * @retval Error bit-map
  1328. */
  1329. uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
  1330. {
  1331. return hcryp->ErrorCode;
  1332. }
  1333. /**
  1334. * @}
  1335. */
  1336. /**
  1337. * @}
  1338. */
  1339. /** @addtogroup CRYP_Private_Functions
  1340. * @{
  1341. */
  1342. /**
  1343. * @brief Write the Key in KeyRx registers.
  1344. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1345. * the configuration information for CRYP module
  1346. * @retval None
  1347. */
  1348. static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp)
  1349. {
  1350. uint32_t keyaddr;
  1351. if (hcryp->Init.pKey == NULL)
  1352. {
  1353. return HAL_ERROR;
  1354. }
  1355. keyaddr = (uint32_t)(hcryp->Init.pKey);
  1356. if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
  1357. {
  1358. hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
  1359. keyaddr+=4U;
  1360. hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
  1361. keyaddr+=4U;
  1362. hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
  1363. keyaddr+=4U;
  1364. hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
  1365. keyaddr+=4U;
  1366. }
  1367. hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
  1368. keyaddr+=4U;
  1369. hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
  1370. keyaddr+=4U;
  1371. hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
  1372. keyaddr+=4U;
  1373. hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
  1374. return HAL_OK;
  1375. }
  1376. /**
  1377. * @brief Write the InitVector/InitCounter in IVRx registers.
  1378. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1379. * the configuration information for CRYP module
  1380. * @retval None
  1381. */
  1382. static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp)
  1383. {
  1384. uint32_t ivaddr;
  1385. #if !defined(AES_CR_NPBLB)
  1386. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  1387. {
  1388. hcryp->Instance->IVR3 = 0;
  1389. hcryp->Instance->IVR2 = 0;
  1390. hcryp->Instance->IVR1 = 0;
  1391. hcryp->Instance->IVR0 = 0;
  1392. }
  1393. else
  1394. #endif
  1395. {
  1396. if (hcryp->Init.pInitVect == NULL)
  1397. {
  1398. return HAL_ERROR;
  1399. }
  1400. ivaddr = (uint32_t)(hcryp->Init.pInitVect);
  1401. hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
  1402. ivaddr+=4U;
  1403. hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
  1404. ivaddr+=4U;
  1405. hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
  1406. ivaddr+=4U;
  1407. hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
  1408. }
  1409. return HAL_OK;
  1410. }
  1411. /**
  1412. * @brief Handle CRYP block input/output data handling under interruption.
  1413. * @note The function is called under interruption only, once
  1414. * interruptions have been enabled by HAL_CRYPEx_AES_IT().
  1415. * @param hcryp pointer to a CRYP_HandleTypeDef structure that contains
  1416. * the configuration information for CRYP module.
  1417. * @retval HAL status
  1418. */
  1419. static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
  1420. {
  1421. uint32_t inputaddr;
  1422. uint32_t outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  1423. if(hcryp->State == HAL_CRYP_STATE_BUSY)
  1424. {
  1425. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  1426. {
  1427. /* Read the last available output block from the Data Output Register */
  1428. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  1429. outputaddr+=4U;
  1430. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  1431. outputaddr+=4U;
  1432. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  1433. outputaddr+=4U;
  1434. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  1435. hcryp->pCrypOutBuffPtr += 16;
  1436. hcryp->CrypOutCount -= 16U;
  1437. }
  1438. else
  1439. {
  1440. /* Read the derived key from the Key registers */
  1441. if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
  1442. {
  1443. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
  1444. outputaddr+=4U;
  1445. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
  1446. outputaddr+=4U;
  1447. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
  1448. outputaddr+=4U;
  1449. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
  1450. outputaddr+=4U;
  1451. }
  1452. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
  1453. outputaddr+=4U;
  1454. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
  1455. outputaddr+=4U;
  1456. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
  1457. outputaddr+=4U;
  1458. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
  1459. }
  1460. /* In case of ciphering or deciphering, check if all output text has been retrieved;
  1461. In case of key derivation, stop right there */
  1462. if ((hcryp->CrypOutCount == 0U) || (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION))
  1463. {
  1464. /* Disable Computation Complete Flag and Errors Interrupts */
  1465. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  1466. /* Change the CRYP state */
  1467. hcryp->State = HAL_CRYP_STATE_READY;
  1468. /* Process Unlocked */
  1469. __HAL_UNLOCK(hcryp);
  1470. /* Call computation complete callback */
  1471. #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
  1472. hcryp->CompCpltCallback(hcryp);
  1473. #else
  1474. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  1475. #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
  1476. return HAL_OK;
  1477. }
  1478. /* If suspension flag has been raised, suspend processing */
  1479. else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  1480. {
  1481. /* reset ModeSuspend */
  1482. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  1483. /* Disable Computation Complete Flag and Errors Interrupts */
  1484. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  1485. /* Change the CRYP state */
  1486. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  1487. /* Process Unlocked */
  1488. __HAL_UNLOCK(hcryp);
  1489. return HAL_OK;
  1490. }
  1491. else /* Process the rest of input data */
  1492. {
  1493. /* Get the Input data address */
  1494. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1495. /* Increment/decrement instance pointer/counter */
  1496. hcryp->pCrypInBuffPtr += 16;
  1497. hcryp->CrypInCount -= 16U;
  1498. /* Write the next input block in the Data Input register */
  1499. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1500. inputaddr+=4U;
  1501. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1502. inputaddr+=4U;
  1503. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1504. inputaddr+=4U;
  1505. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1506. return HAL_OK;
  1507. }
  1508. }
  1509. else
  1510. {
  1511. return HAL_BUSY;
  1512. }
  1513. }
  1514. /**
  1515. * @}
  1516. */
  1517. /**
  1518. * @}
  1519. */
  1520. /**
  1521. * @}
  1522. */
  1523. #endif /* AES */
  1524. #endif /* HAL_CRYP_MODULE_ENABLED */