stm32l4xx_hal_cryp_ex.c 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_cryp_ex.c
  4. * @author MCD Application Team
  5. * @brief CRYPEx HAL module driver.
  6. * This file provides firmware functions to manage the extended
  7. * functionalities of the Cryptography (CRYP) peripheral.
  8. *
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ******************************************************************************
  37. */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "stm32l4xx_hal.h"
  40. #ifdef HAL_CRYP_MODULE_ENABLED
  41. #if defined (STM32L442xx) || defined (STM32L443xx) || defined (STM32L462xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L4A6xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  42. /** @addtogroup STM32L4xx_HAL_Driver
  43. * @{
  44. */
  45. /** @defgroup CRYPEx CRYPEx
  46. * @brief CRYP Extended HAL module driver
  47. * @{
  48. */
  49. /* Private typedef -----------------------------------------------------------*/
  50. /* Private define ------------------------------------------------------------*/
  51. /** @defgroup CRYPEx_Private_Constants CRYPEx Private Constants
  52. * @{
  53. */
  54. #define CRYP_CCF_TIMEOUTVALUE 22000 /*!< CCF flag raising time-out value */
  55. #define CRYP_BUSY_TIMEOUTVALUE 22000 /*!< BUSY flag reset time-out value */
  56. #define CRYP_POLLING_OFF 0x0 /*!< No polling when padding */
  57. #define CRYP_POLLING_ON 0x1 /*!< Polling when padding */
  58. #if defined(AES_CR_NPBLB)
  59. #define AES_POSITION_CR_NPBLB (uint32_t)POSITION_VAL(AES_CR_NPBLB) /*!< Required left shift to set background CLUT size */
  60. #endif
  61. /**
  62. * @}
  63. */
  64. /* Private macro -------------------------------------------------------------*/
  65. /* Private variables ---------------------------------------------------------*/
  66. /* Private function prototypes -----------------------------------------------*/
  67. /** @defgroup CRYPEx_Private_Functions CRYPEx Private Functions
  68. * @{
  69. */
  70. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
  71. static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout);
  72. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  73. static void CRYP_Authentication_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
  74. static void CRYP_Authentication_DMAInCplt(DMA_HandleTypeDef *hdma);
  75. static void CRYP_Authentication_DMAError(DMA_HandleTypeDef *hdma);
  76. static void CRYP_Authentication_DMAOutCplt(DMA_HandleTypeDef *hdma);
  77. static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  78. static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef *hcryp, uint32_t Timeout);
  79. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
  80. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
  81. static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
  82. static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling);
  83. /**
  84. * @}
  85. */
  86. /* Exported functions ---------------------------------------------------------*/
  87. /** @defgroup CRYPEx_Exported_Functions CRYPEx Exported Functions
  88. * @{
  89. */
  90. /** @defgroup CRYPEx_Exported_Functions_Group1 Extended callback function
  91. * @brief Extended callback functions.
  92. *
  93. @verbatim
  94. ===============================================================================
  95. ##### Extended callback functions #####
  96. ===============================================================================
  97. [..] This section provides callback function:
  98. (+) Computation completed.
  99. @endverbatim
  100. * @{
  101. */
  102. /**
  103. * @brief Computation completed callbacks.
  104. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  105. * the configuration information for CRYP module
  106. * @retval None
  107. */
  108. __weak void HAL_CRYPEx_ComputationCpltCallback(CRYP_HandleTypeDef *hcryp)
  109. {
  110. /* Prevent unused argument(s) compilation warning */
  111. UNUSED(hcryp);
  112. /* NOTE : This function should not be modified; when the callback is needed,
  113. the HAL_CRYPEx_ComputationCpltCallback can be implemented in the user file
  114. */
  115. }
  116. /**
  117. * @}
  118. */
  119. /** @defgroup CRYPEx_Exported_Functions_Group2 AES extended processing functions
  120. * @brief Extended processing functions.
  121. *
  122. @verbatim
  123. ==============================================================================
  124. ##### AES extended processing functions #####
  125. ==============================================================================
  126. [..] This section provides functions allowing to:
  127. (+) Encrypt plaintext or decrypt cipher text using AES algorithm in different chaining modes.
  128. Functions are generic (handles ECB, CBC and CTR and all modes) and are only differentiated
  129. based on the processing type. Three processing types are available:
  130. (++) Polling mode
  131. (++) Interrupt mode
  132. (++) DMA mode
  133. (+) Generate and authentication tag in addition to encrypt/decrypt a plain/cipher text using AES
  134. algorithm in different chaining modes.
  135. Functions are generic (handles GCM, GMAC, CMAC and CCM when applicable) and process only one phase
  136. so that steps can be skipped if so required. Functions are only differentiated based on the processing type.
  137. Three processing types are available:
  138. (++) Polling mode
  139. (++) Interrupt mode
  140. (++) DMA mode
  141. @endverbatim
  142. * @{
  143. */
  144. /**
  145. * @brief Carry out in polling mode the ciphering or deciphering operation according to
  146. * hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and
  147. * chaining modes ECB, CBC and CTR are managed by this function in polling mode.
  148. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  149. * the configuration information for CRYP module
  150. * @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption
  151. * or key derivation+decryption.
  152. * Parameter is meaningless in case of key derivation.
  153. * @param Size: Length of the input data buffer in bytes, must be a multiple of 16.
  154. * Parameter is meaningless in case of key derivation.
  155. * @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of
  156. * decryption/key derivation+decryption, or pointer to the derivative keys in
  157. * case of key derivation only.
  158. * @param Timeout: Specify Timeout value
  159. * @retval HAL status
  160. */
  161. HAL_StatusTypeDef HAL_CRYPEx_AES(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData, uint32_t Timeout)
  162. {
  163. if (hcryp->State == HAL_CRYP_STATE_READY)
  164. {
  165. /* Check parameters setting */
  166. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  167. {
  168. if (pOutputData == NULL)
  169. {
  170. return HAL_ERROR;
  171. }
  172. }
  173. else
  174. {
  175. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0))
  176. {
  177. return HAL_ERROR;
  178. }
  179. }
  180. /* Process Locked */
  181. __HAL_LOCK(hcryp);
  182. /* Change the CRYP state */
  183. hcryp->State = HAL_CRYP_STATE_BUSY;
  184. /* Call CRYP_ReadKey() API if the operating mode is set to
  185. key derivation, CRYP_ProcessData() otherwise */
  186. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  187. {
  188. if(CRYP_ReadKey(hcryp, pOutputData, Timeout) != HAL_OK)
  189. {
  190. return HAL_TIMEOUT;
  191. }
  192. }
  193. else
  194. {
  195. if(CRYP_ProcessData(hcryp, pInputData, Size, pOutputData, Timeout) != HAL_OK)
  196. {
  197. return HAL_TIMEOUT;
  198. }
  199. }
  200. /* If the state has not been set to SUSPENDED, set it to
  201. READY, otherwise keep it as it is */
  202. if (hcryp->State != HAL_CRYP_STATE_SUSPENDED)
  203. {
  204. hcryp->State = HAL_CRYP_STATE_READY;
  205. }
  206. /* Process Unlocked */
  207. __HAL_UNLOCK(hcryp);
  208. return HAL_OK;
  209. }
  210. else
  211. {
  212. return HAL_BUSY;
  213. }
  214. }
  215. /**
  216. * @brief Carry out in interrupt mode the ciphering or deciphering operation according to
  217. * hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and
  218. * chaining modes ECB, CBC and CTR are managed by this function in interrupt mode.
  219. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  220. * the configuration information for CRYP module
  221. * @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption
  222. * or key derivation+decryption.
  223. * Parameter is meaningless in case of key derivation.
  224. * @param Size: Length of the input data buffer in bytes, must be a multiple of 16.
  225. * Parameter is meaningless in case of key derivation.
  226. * @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of
  227. * decryption/key derivation+decryption, or pointer to the derivative keys in
  228. * case of key derivation only.
  229. * @retval HAL status
  230. */
  231. HAL_StatusTypeDef HAL_CRYPEx_AES_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData)
  232. {
  233. uint32_t inputaddr = 0;
  234. if(hcryp->State == HAL_CRYP_STATE_READY)
  235. {
  236. /* Check parameters setting */
  237. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  238. {
  239. if (pOutputData == NULL)
  240. {
  241. return HAL_ERROR;
  242. }
  243. }
  244. else
  245. {
  246. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0))
  247. {
  248. return HAL_ERROR;
  249. }
  250. }
  251. /* Process Locked */
  252. __HAL_LOCK(hcryp);
  253. /* If operating mode is not limited to key derivation only,
  254. get the buffers addresses and sizes */
  255. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  256. {
  257. hcryp->CrypInCount = Size;
  258. hcryp->pCrypInBuffPtr = pInputData;
  259. hcryp->pCrypOutBuffPtr = pOutputData;
  260. hcryp->CrypOutCount = Size;
  261. }
  262. else
  263. {
  264. /* For key derivation, set output buffer only
  265. (will point at derivated key) */
  266. hcryp->pCrypOutBuffPtr = pOutputData;
  267. }
  268. /* Change the CRYP state */
  269. hcryp->State = HAL_CRYP_STATE_BUSY;
  270. /* Process Unlocked */
  271. __HAL_UNLOCK(hcryp);
  272. /* Enable Computation Complete Flag and Error Interrupts */
  273. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  274. /* If operating mode is key derivation only, the input data have
  275. already been entered during the initialization process. For
  276. the other operating modes, they are fed to the CRYP hardware
  277. block at this point. */
  278. if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
  279. {
  280. /* Initiate the processing under interrupt in entering
  281. the first input data */
  282. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  283. /* Increment/decrement instance pointer/counter */
  284. hcryp->pCrypInBuffPtr += 16;
  285. hcryp->CrypInCount -= 16;
  286. /* Write the first input block in the Data Input register */
  287. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  288. inputaddr+=4;
  289. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  290. inputaddr+=4;
  291. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  292. inputaddr+=4;
  293. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  294. }
  295. /* Return function status */
  296. return HAL_OK;
  297. }
  298. else
  299. {
  300. return HAL_BUSY;
  301. }
  302. }
  303. /**
  304. * @brief Carry out in DMA mode the ciphering or deciphering operation according to
  305. * hcryp->Init structure fields.
  306. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  307. * the configuration information for CRYP module
  308. * @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption
  309. * or key derivation+decryption.
  310. * @param Size: Length of the input data buffer in bytes, must be a multiple of 16.
  311. * @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of
  312. * decryption/key derivation+decryption.
  313. * @note Chaining modes ECB, CBC and CTR are managed by this function in DMA mode.
  314. * @note Supported operating modes are encryption, decryption and key derivation with decryption.
  315. * @note No DMA channel is provided for key derivation only and therefore, access to AES_KEYRx
  316. * registers must be done by software.
  317. * @note This API is not applicable to key derivation only; for such a mode, access to AES_KEYRx
  318. * registers must be done by software thru HAL_CRYPEx_AES() or HAL_CRYPEx_AES_IT() APIs.
  319. * @note pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  320. * @retval HAL status
  321. */
  322. HAL_StatusTypeDef HAL_CRYPEx_AES_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData)
  323. {
  324. uint32_t inputaddr = 0;
  325. uint32_t outputaddr = 0;
  326. if (hcryp->State == HAL_CRYP_STATE_READY)
  327. {
  328. /* Check parameters setting */
  329. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
  330. {
  331. /* no DMA channel is provided for key derivation operating mode,
  332. access to AES_KEYRx registers must be done by software */
  333. return HAL_ERROR;
  334. }
  335. else
  336. {
  337. if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0))
  338. {
  339. return HAL_ERROR;
  340. }
  341. }
  342. /* Process Locked */
  343. __HAL_LOCK(hcryp);
  344. inputaddr = (uint32_t)pInputData;
  345. outputaddr = (uint32_t)pOutputData;
  346. /* Change the CRYP state */
  347. hcryp->State = HAL_CRYP_STATE_BUSY;
  348. /* Set the input and output addresses and start DMA transfer */
  349. CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  350. /* Process Unlocked */
  351. __HAL_UNLOCK(hcryp);
  352. /* Return function status */
  353. return HAL_OK;
  354. }
  355. else
  356. {
  357. return HAL_BUSY;
  358. }
  359. }
  360. /**
  361. * @brief Carry out in polling mode the authentication tag generation as well as the ciphering or deciphering
  362. * operation according to hcryp->Init structure fields.
  363. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  364. * the configuration information for CRYP module
  365. * @param pInputData:
  366. * - pointer to payload data in GCM or CCM payload phase,
  367. * - pointer to B0 block in CMAC header phase,
  368. * - pointer to C block in CMAC final phase.
  369. * - Parameter is meaningless in case of GCM/GMAC/CCM init, header and final phases.
  370. * @param Size:
  371. * - length of the input payload data buffer in bytes in GCM or CCM payload phase,
  372. * - length of B0 block (in bytes) in CMAC header phase,
  373. * - length of C block (in bytes) in CMAC final phase.
  374. * - Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
  375. * - Parameter is meaningless in case of CCM final phase.
  376. * - Parameter is message length in bytes in case of GCM final phase.
  377. * - Parameter must be set to zero in case of GMAC final phase.
  378. * @param pOutputData:
  379. * - pointer to plain or cipher text in GCM/CCM payload phase,
  380. * - pointer to authentication tag in GCM/GMAC/CCM/CMAC final phase.
  381. * - Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
  382. * - Parameter is meaningless in case of CMAC header phase.
  383. * @param Timeout: Specify Timeout value
  384. * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC, CMAC and CCM when the latter is applicable.
  385. * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes
  386. * can be skipped by the user if so required.
  387. * @retval HAL status
  388. */
  389. HAL_StatusTypeDef HAL_CRYPEx_AES_Auth(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData, uint32_t Timeout)
  390. {
  391. uint32_t index = 0;
  392. uint32_t inputaddr = 0;
  393. uint32_t outputaddr = 0;
  394. uint32_t tagaddr = 0;
  395. uint64_t headerlength = 0;
  396. uint64_t inputlength = 0;
  397. uint64_t payloadlength = 0;
  398. uint32_t difflength = 0;
  399. uint32_t addhoc_process = 0;
  400. if (hcryp->State == HAL_CRYP_STATE_READY)
  401. {
  402. /* input/output parameters check */
  403. if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  404. {
  405. if (((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0)) ||
  406. ((hcryp->Init.Header == NULL) && (hcryp->Init.HeaderSize != 0)))
  407. {
  408. return HAL_ERROR;
  409. }
  410. #if defined(AES_CR_NPBLB)
  411. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
  412. #else
  413. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  414. #endif
  415. {
  416. /* In case of CMAC or CCM (when applicable) header phase resumption, we can have pInputData = NULL and Size = 0 */
  417. if (((pInputData != NULL) && (Size == 0)) || ((pInputData == NULL) && (Size != 0)))
  418. {
  419. return HAL_ERROR;
  420. }
  421. }
  422. }
  423. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  424. {
  425. if (((pInputData == NULL) && (Size != 0)) || \
  426. ((pInputData != NULL) && (Size == 0)) || \
  427. ((pInputData != NULL) && (Size != 0) && (pOutputData == NULL)))
  428. {
  429. return HAL_ERROR;
  430. }
  431. }
  432. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  433. {
  434. if (pOutputData == NULL)
  435. {
  436. return HAL_ERROR;
  437. }
  438. #if !defined(AES_CR_NPBLB)
  439. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
  440. {
  441. return HAL_ERROR;
  442. }
  443. #endif
  444. }
  445. /* Process Locked */
  446. __HAL_LOCK(hcryp);
  447. /* Change the CRYP state */
  448. hcryp->State = HAL_CRYP_STATE_BUSY;
  449. /*==============================================*/
  450. /* GCM/GMAC (or CCM when applicable) init phase */
  451. /*==============================================*/
  452. /* In case of init phase, the input data (Key and Initialization Vector) have
  453. already been entered during the initialization process. Therefore, the
  454. API just waits for the CCF flag to be set. */
  455. if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
  456. {
  457. /* just wait for hash computation */
  458. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  459. {
  460. hcryp->State = HAL_CRYP_STATE_READY;
  461. __HAL_UNLOCK(hcryp);
  462. return HAL_TIMEOUT;
  463. }
  464. /* Clear CCF Flag */
  465. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  466. /* Mark that the initialization phase is over */
  467. hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER;
  468. }
  469. /*=======================================================*/
  470. /* GCM/GMAC or (CCM / CMAC when applicable) header phase */
  471. /*=======================================================*/
  472. else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  473. {
  474. #if !defined(AES_CR_NPBLB)
  475. /* Set header phase; for GCM or GMAC, set data-byte at this point */
  476. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  477. {
  478. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
  479. }
  480. else
  481. #endif
  482. {
  483. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
  484. }
  485. /* Enable the Peripheral */
  486. __HAL_CRYP_ENABLE(hcryp);
  487. #if !defined(AES_CR_NPBLB)
  488. /* in case of CMAC, enter B0 block in header phase, before the header itself. */
  489. /* If Size = 0 (possible case of resumption after CMAC header phase suspension),
  490. skip these steps and go directly to header buffer feeding to the HW */
  491. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (Size != 0))
  492. {
  493. inputaddr = (uint32_t)pInputData;
  494. for( ; (index < Size); index += 16)
  495. {
  496. /* Write the Input block in the Data Input register */
  497. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  498. inputaddr+=4;
  499. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  500. inputaddr+=4;
  501. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  502. inputaddr+=4;
  503. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  504. inputaddr+=4;
  505. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  506. {
  507. hcryp->State = HAL_CRYP_STATE_READY;
  508. __HAL_UNLOCK(hcryp);
  509. return HAL_TIMEOUT;
  510. }
  511. /* Clear CCF Flag */
  512. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  513. /* If the suspension flag has been raised and if the processing is not about
  514. to end, suspend processing */
  515. if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16) < Size))
  516. {
  517. /* reset SuspendRequest */
  518. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  519. /* Change the CRYP state */
  520. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  521. /* Mark that the header phase is over */
  522. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  523. /* Save current reading and writing locations of Input and Output buffers */
  524. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  525. /* Save the total number of bytes (B blocks + header) that remain to be
  526. processed at this point */
  527. hcryp->CrypInCount = hcryp->Init.HeaderSize + Size - (index+16);
  528. /* Process Unlocked */
  529. __HAL_UNLOCK(hcryp);
  530. return HAL_OK;
  531. }
  532. } /* for(index=0; (index < Size); index += 16) */
  533. }
  534. #endif /* !defined(AES_CR_NPBLB) */
  535. /* Enter header */
  536. inputaddr = (uint32_t)hcryp->Init.Header;
  537. /* Local variable headerlength is a number of bytes multiple of 128 bits,
  538. remaining header data (if any) are handled after this loop */
  539. headerlength = (((hcryp->Init.HeaderSize)/16)*16) ;
  540. if ((hcryp->Init.HeaderSize % 16) != 0)
  541. {
  542. difflength = (uint32_t) (hcryp->Init.HeaderSize - headerlength);
  543. }
  544. for(index=0; index < headerlength; index += 16)
  545. {
  546. /* Write the Input block in the Data Input register */
  547. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  548. inputaddr+=4;
  549. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  550. inputaddr+=4;
  551. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  552. inputaddr+=4;
  553. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  554. inputaddr+=4;
  555. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  556. {
  557. hcryp->State = HAL_CRYP_STATE_READY;
  558. __HAL_UNLOCK(hcryp);
  559. return HAL_TIMEOUT;
  560. }
  561. /* Clear CCF Flag */
  562. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  563. /* If the suspension flag has been raised and if the processing is not about
  564. to end, suspend processing */
  565. if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16) < headerlength))
  566. {
  567. /* reset SuspendRequest */
  568. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  569. /* Change the CRYP state */
  570. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  571. /* Mark that the header phase is over */
  572. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  573. /* Save current reading and writing locations of Input and Output buffers */
  574. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  575. /* Save the total number of bytes that remain to be processed at this point */
  576. hcryp->CrypInCount = hcryp->Init.HeaderSize - (index+16);
  577. /* Process Unlocked */
  578. __HAL_UNLOCK(hcryp);
  579. return HAL_OK;
  580. }
  581. }
  582. /* Case header length is not a multiple of 16 bytes */
  583. if (difflength != 0)
  584. {
  585. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  586. CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
  587. }
  588. /* Mark that the header phase is over */
  589. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  590. }
  591. /*============================================*/
  592. /* GCM (or CCM when applicable) payload phase */
  593. /*============================================*/
  594. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  595. {
  596. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
  597. /* if the header phase has been bypassed, AES must be enabled again */
  598. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  599. {
  600. __HAL_CRYP_ENABLE(hcryp);
  601. }
  602. inputaddr = (uint32_t)pInputData;
  603. outputaddr = (uint32_t)pOutputData;
  604. /* Enter payload */
  605. /* Specific handling to manage payload last block size less than 128 bits */
  606. if ((Size % 16) != 0)
  607. {
  608. payloadlength = (Size/16) * 16;
  609. difflength = (uint32_t) (Size - payloadlength);
  610. addhoc_process = 1;
  611. }
  612. else
  613. {
  614. payloadlength = Size;
  615. }
  616. /* Feed payload */
  617. for( ; index < payloadlength; index += 16)
  618. {
  619. /* Write the Input block in the Data Input register */
  620. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  621. inputaddr+=4;
  622. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  623. inputaddr+=4;
  624. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  625. inputaddr+=4;
  626. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  627. inputaddr+=4;
  628. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  629. {
  630. hcryp->State = HAL_CRYP_STATE_READY;
  631. __HAL_UNLOCK(hcryp);
  632. return HAL_TIMEOUT;
  633. }
  634. /* Clear CCF Flag */
  635. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  636. /* Retrieve output data: read the output block
  637. from the Data Output Register */
  638. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  639. outputaddr+=4;
  640. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  641. outputaddr+=4;
  642. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  643. outputaddr+=4;
  644. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  645. outputaddr+=4;
  646. /* If the suspension flag has been raised and if the processing is not about
  647. to end, suspend processing */
  648. if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16) < payloadlength))
  649. {
  650. /* no flag waiting under IRQ handling */
  651. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  652. {
  653. /* Ensure that Busy flag is reset */
  654. if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK)
  655. {
  656. hcryp->State = HAL_CRYP_STATE_READY;
  657. __HAL_UNLOCK(hcryp);
  658. return HAL_TIMEOUT;
  659. }
  660. }
  661. /* reset SuspendRequest */
  662. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  663. /* Change the CRYP state */
  664. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  665. /* Mark that the header phase is over */
  666. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  667. /* Save current reading and writing locations of Input and Output buffers */
  668. hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
  669. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  670. /* Save the number of bytes that remain to be processed at this point */
  671. hcryp->CrypInCount = Size - (index+16);
  672. /* Process Unlocked */
  673. __HAL_UNLOCK(hcryp);
  674. return HAL_OK;
  675. }
  676. }
  677. /* Additional processing to manage GCM(/CCM) encryption and decryption cases when
  678. payload last block size less than 128 bits */
  679. if (addhoc_process == 1)
  680. {
  681. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  682. hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
  683. CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
  684. } /* (addhoc_process == 1) */
  685. /* Mark that the payload phase is over */
  686. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  687. }
  688. /*==================================*/
  689. /* GCM/GMAC/CCM or CMAC final phase */
  690. /*==================================*/
  691. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  692. {
  693. tagaddr = (uint32_t)pOutputData;
  694. #if defined(AES_CR_NPBLB)
  695. /* By default, clear NPBLB field */
  696. CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
  697. #endif
  698. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
  699. /* if the header and payload phases have been bypassed, AES must be enabled again */
  700. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  701. {
  702. __HAL_CRYP_ENABLE(hcryp);
  703. }
  704. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  705. {
  706. headerlength = hcryp->Init.HeaderSize * 8; /* Header length in bits */
  707. inputlength = Size * 8; /* input length in bits */
  708. #if !defined(AES_CR_NPBLB)
  709. if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  710. {
  711. hcryp->Instance->DINR = __RBIT((headerlength)>>32);
  712. hcryp->Instance->DINR = __RBIT(headerlength);
  713. hcryp->Instance->DINR = __RBIT((inputlength)>>32);
  714. hcryp->Instance->DINR = __RBIT(inputlength);
  715. }
  716. else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  717. {
  718. hcryp->Instance->DINR = __REV((headerlength)>>32);
  719. hcryp->Instance->DINR = __REV(headerlength);
  720. hcryp->Instance->DINR = __REV((inputlength)>>32);
  721. hcryp->Instance->DINR = __REV(inputlength);
  722. }
  723. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  724. {
  725. hcryp->Instance->DINR = __ROR((headerlength)>>32, 16);
  726. hcryp->Instance->DINR = __ROR(headerlength, 16);
  727. hcryp->Instance->DINR = __ROR((inputlength)>>32, 16);
  728. hcryp->Instance->DINR = __ROR(inputlength, 16);
  729. }
  730. else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
  731. {
  732. hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
  733. hcryp->Instance->DINR = (uint32_t)(headerlength);
  734. hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
  735. hcryp->Instance->DINR = (uint32_t)(inputlength);
  736. }
  737. #else
  738. hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
  739. hcryp->Instance->DINR = (uint32_t)(headerlength);
  740. hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
  741. hcryp->Instance->DINR = (uint32_t)(inputlength);
  742. #endif
  743. }
  744. #if !defined(AES_CR_NPBLB)
  745. else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  746. {
  747. inputaddr = (uint32_t)pInputData;
  748. /* Enter the last block made of a 128-bit value formatted
  749. from the original B0 packet. */
  750. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  751. inputaddr+=4;
  752. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  753. inputaddr+=4;
  754. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  755. inputaddr+=4;
  756. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  757. }
  758. #endif
  759. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  760. {
  761. hcryp->State = HAL_CRYP_STATE_READY;
  762. __HAL_UNLOCK(hcryp);
  763. return HAL_TIMEOUT;
  764. }
  765. /* Read the Auth TAG in the Data Out register */
  766. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  767. tagaddr+=4;
  768. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  769. tagaddr+=4;
  770. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  771. tagaddr+=4;
  772. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  773. /* Clear CCF Flag */
  774. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  775. /* Mark that the final phase is over */
  776. hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER;
  777. /* Disable the Peripheral */
  778. __HAL_CRYP_DISABLE(hcryp);
  779. }
  780. /*=================================================*/
  781. /* case incorrect hcryp->Init.GCMCMACPhase setting */
  782. /*=================================================*/
  783. else
  784. {
  785. hcryp->State = HAL_CRYP_STATE_ERROR;
  786. __HAL_UNLOCK(hcryp);
  787. return HAL_ERROR;
  788. }
  789. /* Change the CRYP state */
  790. hcryp->State = HAL_CRYP_STATE_READY;
  791. /* Process Unlocked */
  792. __HAL_UNLOCK(hcryp);
  793. return HAL_OK;
  794. }
  795. else
  796. {
  797. return HAL_BUSY;
  798. }
  799. }
  800. /**
  801. * @brief Carry out in interrupt mode the authentication tag generation as well as the ciphering or deciphering
  802. * operation according to hcryp->Init structure fields.
  803. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  804. * the configuration information for CRYP module
  805. * @param pInputData:
  806. * - pointer to payload data in GCM or CCM payload phase,
  807. * - pointer to B0 block in CMAC header phase,
  808. * - pointer to C block in CMAC final phase.
  809. * - Parameter is meaningless in case of GCM/GMAC/CCM init, header and final phases.
  810. * @param Size:
  811. * - length of the input payload data buffer in bytes in GCM or CCM payload phase,
  812. * - length of B0 block (in bytes) in CMAC header phase,
  813. * - length of C block (in bytes) in CMAC final phase.
  814. * - Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
  815. * - Parameter is meaningless in case of CCM final phase.
  816. * - Parameter is message length in bytes in case of GCM final phase.
  817. * - Parameter must be set to zero in case of GMAC final phase.
  818. * @param pOutputData:
  819. * - pointer to plain or cipher text in GCM/CCM payload phase,
  820. * - pointer to authentication tag in GCM/GMAC/CCM/CMAC final phase.
  821. * - Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
  822. * - Parameter is meaningless in case of CMAC header phase.
  823. * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC.
  824. * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes
  825. * can be skipped by the user if so required.
  826. * @retval HAL status
  827. */
  828. HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData)
  829. {
  830. uint32_t inputaddr = 0;
  831. uint64_t headerlength = 0;
  832. uint64_t inputlength = 0;
  833. uint32_t index = 0;
  834. uint32_t addhoc_process = 0;
  835. uint32_t difflength = 0;
  836. uint32_t difflengthmod4 = 0;
  837. uint32_t mask[4][3] = { {0xFF000000, 0xFFFF0000, 0xFFFFFF00}, /* 32-bit data */
  838. {0x0000FF00, 0x0000FFFF, 0xFF00FFFF}, /* 16-bit data */
  839. {0x000000FF, 0x0000FFFF, 0x00FFFFFF}, /* 8-bit data */
  840. {0x000000FF, 0x0000FFFF, 0x00FFFFFF}}; /* Bit data */
  841. uint32_t mask_index = hcryp->Init.DataType >> AES_CR_DATATYPE_Pos;
  842. if (hcryp->State == HAL_CRYP_STATE_READY)
  843. {
  844. /* input/output parameters check */
  845. if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  846. {
  847. if (((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0)) ||
  848. ((hcryp->Init.Header == NULL) && (hcryp->Init.HeaderSize != 0)))
  849. {
  850. return HAL_ERROR;
  851. }
  852. #if defined(AES_CR_NPBLB)
  853. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
  854. #else
  855. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  856. #endif
  857. {
  858. /* In case of CMAC or CCM header phase resumption, we can have pInputData = NULL and Size = 0 */
  859. if (((pInputData != NULL) && (Size == 0)) || ((pInputData == NULL) && (Size != 0)))
  860. {
  861. return HAL_ERROR;
  862. }
  863. }
  864. }
  865. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  866. {
  867. if ((pInputData != NULL) && (Size != 0) && (pOutputData == NULL))
  868. {
  869. return HAL_ERROR;
  870. }
  871. }
  872. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  873. {
  874. if (pOutputData == NULL)
  875. {
  876. return HAL_ERROR;
  877. }
  878. #if !defined(AES_CR_NPBLB)
  879. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
  880. {
  881. return HAL_ERROR;
  882. }
  883. #endif
  884. }
  885. /* Process Locked */
  886. __HAL_LOCK(hcryp);
  887. /* Change the CRYP state */
  888. hcryp->State = HAL_CRYP_STATE_BUSY;
  889. /* Process Unlocked */
  890. __HAL_UNLOCK(hcryp);
  891. /* Enable Computation Complete Flag and Error Interrupts */
  892. __HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  893. /*==============================================*/
  894. /* GCM/GMAC (or CCM when applicable) init phase */
  895. /*==============================================*/
  896. if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
  897. {
  898. /* In case of init phase, the input data (Key and Initialization Vector) have
  899. already been entered during the initialization process. Therefore, the
  900. software just waits for the CCF interrupt to be raised and which will
  901. be handled by CRYP_AES_Auth_IT() API. */
  902. }
  903. /*===================================*/
  904. /* GCM/GMAC/CCM or CMAC header phase */
  905. /*===================================*/
  906. else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  907. {
  908. #if defined(AES_CR_NPBLB)
  909. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
  910. #else
  911. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  912. #endif
  913. {
  914. /* In case of CMAC, B blocks are first entered, before the header.
  915. Therefore, B blocks and the header are entered back-to-back
  916. as if it was only one single block.
  917. However, in case of resumption after suspension, if all the
  918. B blocks have been entered (in that case, Size = 0), only the
  919. remainder of the non-processed header bytes are entered. */
  920. if (Size != 0)
  921. {
  922. hcryp->CrypInCount = Size + hcryp->Init.HeaderSize;
  923. hcryp->pCrypInBuffPtr = pInputData;
  924. }
  925. else
  926. {
  927. hcryp->CrypInCount = hcryp->Init.HeaderSize;
  928. hcryp->pCrypInBuffPtr = hcryp->Init.Header;
  929. }
  930. }
  931. else
  932. {
  933. /* Get the header addresses and sizes */
  934. hcryp->CrypInCount = hcryp->Init.HeaderSize;
  935. hcryp->pCrypInBuffPtr = hcryp->Init.Header;
  936. }
  937. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  938. #if !defined(AES_CR_NPBLB)
  939. /* Set header phase; for GCM or GMAC, set data-byte at this point */
  940. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  941. {
  942. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
  943. }
  944. else
  945. #endif
  946. {
  947. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
  948. }
  949. /* Enable the Peripheral */
  950. __HAL_CRYP_ENABLE(hcryp);
  951. /* Increment/decrement instance pointer/counter */
  952. if (hcryp->CrypInCount == 0)
  953. {
  954. /* Case of no header */
  955. hcryp->State = HAL_CRYP_STATE_READY;
  956. /* Mark that the header phase is over */
  957. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  958. return HAL_OK;
  959. }
  960. else if (hcryp->CrypInCount < 16)
  961. {
  962. hcryp->CrypInCount = 0;
  963. addhoc_process = 1;
  964. difflength = (uint32_t) (hcryp->Init.HeaderSize);
  965. difflengthmod4 = difflength%4;
  966. }
  967. else
  968. {
  969. hcryp->pCrypInBuffPtr += 16;
  970. hcryp->CrypInCount -= 16;
  971. }
  972. #if defined(AES_CR_NPBLB)
  973. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
  974. #else
  975. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  976. #endif
  977. {
  978. if (hcryp->CrypInCount == hcryp->Init.HeaderSize)
  979. {
  980. /* All B blocks will have been entered after the next
  981. four DINR writing, so point at header buffer for
  982. the next iteration */
  983. hcryp->pCrypInBuffPtr = hcryp->Init.Header;
  984. }
  985. }
  986. /* Enter header first block to initiate the process
  987. in the Data Input register */
  988. if (addhoc_process == 0)
  989. {
  990. /* Header has size equal or larger than 128 bits */
  991. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  992. inputaddr+=4;
  993. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  994. inputaddr+=4;
  995. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  996. inputaddr+=4;
  997. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  998. }
  999. else
  1000. {
  1001. /* Header has size less than 128 bits */
  1002. /* Enter complete words when possible */
  1003. for( ; index < (difflength/4); index ++)
  1004. {
  1005. /* Write the Input block in the Data Input register */
  1006. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1007. inputaddr+=4;
  1008. }
  1009. /* Enter incomplete word padded with zeroes if applicable
  1010. (case of header length not a multiple of 32-bits) */
  1011. if (difflengthmod4 != 0)
  1012. {
  1013. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[mask_index][difflengthmod4-1]);
  1014. }
  1015. /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
  1016. for(index=0; index < (4 - ((difflength+3)/4)); index ++)
  1017. {
  1018. hcryp->Instance->DINR = 0;
  1019. }
  1020. }
  1021. }
  1022. /*============================================*/
  1023. /* GCM (or CCM when applicable) payload phase */
  1024. /*============================================*/
  1025. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  1026. {
  1027. /* Get the buffer addresses and sizes */
  1028. hcryp->CrypInCount = Size;
  1029. hcryp->pCrypInBuffPtr = pInputData;
  1030. hcryp->pCrypOutBuffPtr = pOutputData;
  1031. hcryp->CrypOutCount = Size;
  1032. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  1033. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
  1034. /* if the header phase has been bypassed, AES must be enabled again */
  1035. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  1036. {
  1037. __HAL_CRYP_ENABLE(hcryp);
  1038. }
  1039. /* No payload case */
  1040. if (pInputData == NULL)
  1041. {
  1042. hcryp->State = HAL_CRYP_STATE_READY;
  1043. /* Mark that the header phase is over */
  1044. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  1045. /* Process Unlocked */
  1046. __HAL_UNLOCK(hcryp);
  1047. return HAL_OK;
  1048. }
  1049. /* Specific handling to manage payload size less than 128 bits */
  1050. if (Size < 16)
  1051. {
  1052. difflength = (uint32_t) (Size);
  1053. #if defined(AES_CR_NPBLB)
  1054. /* In case of GCM encryption or CCM decryption, specify the number of padding
  1055. bytes in last block of payload */
  1056. if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE)
  1057. {
  1058. if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC)
  1059. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT))
  1060. || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM)
  1061. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT)))
  1062. {
  1063. /* Set NPBLB field in writing the number of padding bytes
  1064. for the last block of payload */
  1065. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, (16 - difflength) << AES_POSITION_CR_NPBLB);
  1066. }
  1067. }
  1068. #else
  1069. /* Software workaround applied to GCM encryption only */
  1070. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  1071. {
  1072. /* Change the mode configured in CHMOD bits of CR register to select CTR mode */
  1073. __HAL_CRYP_SET_CHAININGMODE(hcryp, CRYP_CHAINMODE_AES_CTR);
  1074. }
  1075. #endif
  1076. /* Set hcryp->CrypInCount to 0 (no more data to enter) */
  1077. hcryp->CrypInCount = 0;
  1078. /* Insert the last block (which size is inferior to 128 bits) padded with zeroes,
  1079. to have a complete block of 128 bits */
  1080. difflengthmod4 = difflength%4;
  1081. /* Insert the last block (which size is inferior to 128 bits) padded with zeroes
  1082. to have a complete block of 128 bits */
  1083. for(index=0; index < (difflength/4); index ++)
  1084. {
  1085. /* Write the Input block in the Data Input register */
  1086. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1087. inputaddr+=4;
  1088. }
  1089. /* If required, manage input data size not multiple of 32 bits */
  1090. if (difflengthmod4 != 0)
  1091. {
  1092. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[mask_index][difflengthmod4-1]);
  1093. }
  1094. /* Wrap-up in padding with zero-words if applicable */
  1095. for(index=0; index < (4 - ((difflength+3)/4)); index ++)
  1096. {
  1097. hcryp->Instance->DINR = 0;
  1098. }
  1099. }
  1100. else
  1101. {
  1102. /* Increment/decrement instance pointer/counter */
  1103. hcryp->pCrypInBuffPtr += 16;
  1104. hcryp->CrypInCount -= 16;
  1105. /* Enter payload first block to initiate the process
  1106. in the Data Input register */
  1107. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1108. inputaddr+=4;
  1109. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1110. inputaddr+=4;
  1111. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1112. inputaddr+=4;
  1113. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1114. }
  1115. }
  1116. /*==================================*/
  1117. /* GCM/GMAC/CCM or CMAC final phase */
  1118. /*==================================*/
  1119. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  1120. {
  1121. hcryp->pCrypOutBuffPtr = pOutputData;
  1122. #if defined(AES_CR_NPBLB)
  1123. /* By default, clear NPBLB field */
  1124. CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
  1125. #endif
  1126. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
  1127. /* if the header and payload phases have been bypassed, AES must be enabled again */
  1128. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  1129. {
  1130. __HAL_CRYP_ENABLE(hcryp);
  1131. }
  1132. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  1133. {
  1134. headerlength = hcryp->Init.HeaderSize * 8; /* Header length in bits */
  1135. inputlength = Size * 8; /* Input length in bits */
  1136. /* Write the number of bits in the header on 64 bits followed by the number
  1137. of bits in the payload on 64 bits as well */
  1138. #if !defined(AES_CR_NPBLB)
  1139. if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  1140. {
  1141. hcryp->Instance->DINR = __RBIT((headerlength)>>32);
  1142. hcryp->Instance->DINR = __RBIT(headerlength);
  1143. hcryp->Instance->DINR = __RBIT((inputlength)>>32);
  1144. hcryp->Instance->DINR = __RBIT(inputlength);
  1145. }
  1146. else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  1147. {
  1148. hcryp->Instance->DINR = __REV((headerlength)>>32);
  1149. hcryp->Instance->DINR = __REV(headerlength);
  1150. hcryp->Instance->DINR = __REV((inputlength)>>32);
  1151. hcryp->Instance->DINR = __REV(inputlength);
  1152. }
  1153. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  1154. {
  1155. hcryp->Instance->DINR = __ROR((headerlength)>>32, 16);
  1156. hcryp->Instance->DINR = __ROR(headerlength, 16);
  1157. hcryp->Instance->DINR = __ROR((inputlength)>>32, 16);
  1158. hcryp->Instance->DINR = __ROR(inputlength, 16);
  1159. }
  1160. else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
  1161. {
  1162. hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
  1163. hcryp->Instance->DINR = (uint32_t)(headerlength);
  1164. hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
  1165. hcryp->Instance->DINR = (uint32_t)(inputlength);
  1166. }
  1167. #else
  1168. hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
  1169. hcryp->Instance->DINR = (uint32_t)(headerlength);
  1170. hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
  1171. hcryp->Instance->DINR = (uint32_t)(inputlength);
  1172. #endif
  1173. }
  1174. #if !defined(AES_CR_NPBLB)
  1175. else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  1176. {
  1177. inputaddr = (uint32_t)pInputData;
  1178. /* Enter the last block made of a 128-bit value formatted
  1179. from the original B0 packet. */
  1180. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1181. inputaddr+=4;
  1182. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1183. inputaddr+=4;
  1184. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1185. inputaddr+=4;
  1186. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1187. }
  1188. #endif
  1189. }
  1190. /*=================================================*/
  1191. /* case incorrect hcryp->Init.GCMCMACPhase setting */
  1192. /*=================================================*/
  1193. else
  1194. {
  1195. hcryp->State = HAL_CRYP_STATE_ERROR;
  1196. return HAL_ERROR;
  1197. }
  1198. return HAL_OK;
  1199. }
  1200. else
  1201. {
  1202. return HAL_BUSY;
  1203. }
  1204. }
  1205. /**
  1206. * @brief Carry out in DMA mode the authentication tag generation as well as the ciphering or deciphering
  1207. * operation according to hcryp->Init structure fields.
  1208. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1209. * the configuration information for CRYP module
  1210. * @param pInputData:
  1211. * - pointer to payload data in GCM or CCM payload phase,
  1212. * - pointer to B0 block in CMAC header phase,
  1213. * - pointer to C block in CMAC final phase.
  1214. * - Parameter is meaningless in case of GCM/GMAC/CCM init, header and final phases.
  1215. * @param Size:
  1216. * - length of the input payload data buffer in bytes in GCM or CCM payload phase,
  1217. * - length of B0 block (in bytes) in CMAC header phase,
  1218. * - length of C block (in bytes) in CMAC final phase.
  1219. * - Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
  1220. * - Parameter is meaningless in case of CCM final phase.
  1221. * - Parameter is message length in bytes in case of GCM final phase.
  1222. * - Parameter must be set to zero in case of GMAC final phase.
  1223. * @param pOutputData:
  1224. * - pointer to plain or cipher text in GCM/CCM payload phase,
  1225. * - pointer to authentication tag in GCM/GMAC/CCM/CMAC final phase.
  1226. * - Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
  1227. * - Parameter is meaningless in case of CMAC header phase.
  1228. * @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC.
  1229. * @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes
  1230. * can be skipped by the user if so required.
  1231. * @note pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
  1232. * @retval HAL status
  1233. */
  1234. HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData)
  1235. {
  1236. uint32_t inputaddr = 0;
  1237. uint32_t outputaddr = 0;
  1238. uint32_t tagaddr = 0;
  1239. uint64_t headerlength = 0;
  1240. uint64_t inputlength = 0;
  1241. uint64_t payloadlength = 0;
  1242. if (hcryp->State == HAL_CRYP_STATE_READY)
  1243. {
  1244. /* input/output parameters check */
  1245. if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  1246. {
  1247. if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0))
  1248. {
  1249. return HAL_ERROR;
  1250. }
  1251. #if defined(AES_CR_NPBLB)
  1252. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
  1253. {
  1254. /* In case of CMAC or CCM header phase resumption, we can have pInputData = NULL and Size = 0 */
  1255. if (((pInputData != NULL) && (Size == 0)) || ((pInputData == NULL) && (Size != 0)))
  1256. {
  1257. return HAL_ERROR;
  1258. }
  1259. }
  1260. #else
  1261. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  1262. {
  1263. if ((pInputData == NULL) || (Size == 0))
  1264. {
  1265. return HAL_ERROR;
  1266. }
  1267. }
  1268. #endif
  1269. }
  1270. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  1271. {
  1272. if ((pInputData != NULL) && (Size != 0) && (pOutputData == NULL))
  1273. {
  1274. return HAL_ERROR;
  1275. }
  1276. }
  1277. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  1278. {
  1279. if (pOutputData == NULL)
  1280. {
  1281. return HAL_ERROR;
  1282. }
  1283. #if !defined(AES_CR_NPBLB)
  1284. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
  1285. {
  1286. return HAL_ERROR;
  1287. }
  1288. #endif
  1289. }
  1290. /* Process Locked */
  1291. __HAL_LOCK(hcryp);
  1292. /* Change the CRYP state */
  1293. hcryp->State = HAL_CRYP_STATE_BUSY;
  1294. /*==============================================*/
  1295. /* GCM/GMAC (or CCM when applicable) init phase */
  1296. /*==============================================*/
  1297. /* In case of init phase, the input data (Key and Initialization Vector) have
  1298. already been entered during the initialization process. No DMA transfer is
  1299. required at that point therefore, the software just waits for the CCF flag
  1300. to be raised. */
  1301. if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
  1302. {
  1303. /* just wait for hash computation */
  1304. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  1305. {
  1306. hcryp->State = HAL_CRYP_STATE_READY;
  1307. __HAL_UNLOCK(hcryp);
  1308. return HAL_TIMEOUT;
  1309. }
  1310. /* Clear CCF Flag */
  1311. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1312. /* Mark that the initialization phase is over */
  1313. hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER;
  1314. hcryp->State = HAL_CRYP_STATE_READY;
  1315. }
  1316. /*====================================*/
  1317. /* GCM/GMAC/ CCM or CMAC header phase */
  1318. /*====================================*/
  1319. else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  1320. {
  1321. #if !defined(AES_CR_NPBLB)
  1322. /* Set header phase; for GCM or GMAC, set data-byte at this point */
  1323. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  1324. {
  1325. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
  1326. }
  1327. else
  1328. #endif
  1329. {
  1330. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
  1331. }
  1332. /* Enable the CRYP peripheral */
  1333. __HAL_CRYP_ENABLE(hcryp);
  1334. #if !defined(AES_CR_NPBLB)
  1335. /* enter first B0 block in polling mode (no DMA transfer for B0) */
  1336. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  1337. {
  1338. inputaddr = (uint32_t)pInputData;
  1339. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1340. inputaddr+=4;
  1341. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1342. inputaddr+=4;
  1343. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1344. inputaddr+=4;
  1345. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1346. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  1347. {
  1348. hcryp->State = HAL_CRYP_STATE_READY;
  1349. __HAL_UNLOCK(hcryp);
  1350. return HAL_TIMEOUT;
  1351. }
  1352. /* Clear CCF Flag */
  1353. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1354. }
  1355. #endif
  1356. /* No header case */
  1357. if (hcryp->Init.Header == NULL)
  1358. {
  1359. hcryp->State = HAL_CRYP_STATE_READY;
  1360. /* Mark that the header phase is over */
  1361. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  1362. /* Process Unlocked */
  1363. __HAL_UNLOCK(hcryp);
  1364. return HAL_OK;
  1365. }
  1366. inputaddr = (uint32_t)hcryp->Init.Header;
  1367. if ((hcryp->Init.HeaderSize % 16) != 0)
  1368. {
  1369. if (hcryp->Init.HeaderSize < 16)
  1370. {
  1371. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  1372. CRYP_Padding(hcryp, (uint32_t) (hcryp->Init.HeaderSize), CRYP_POLLING_OFF);
  1373. hcryp->State = HAL_CRYP_STATE_READY;
  1374. /* Mark that the header phase is over */
  1375. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  1376. /* CCF flag indicating header phase AES processing completion
  1377. will be checked at the start of the next phase:
  1378. - payload phase (GCM / CCM when applicable)
  1379. - final phase (GMAC or CMAC when applicable). */
  1380. }
  1381. else
  1382. {
  1383. /* Local variable headerlength is a number of bytes multiple of 128 bits,
  1384. remaining header data (if any) are handled after this loop */
  1385. headerlength = (((hcryp->Init.HeaderSize)/16)*16) ;
  1386. /* Store the ending transfer point */
  1387. hcryp->pCrypInBuffPtr = hcryp->Init.Header + headerlength;
  1388. hcryp->CrypInCount = (uint32_t)(hcryp->Init.HeaderSize - headerlength); /* remainder */
  1389. /* Set the input and output addresses and start DMA transfer */
  1390. /* (incomplete DMA transfer, will be wrapped up after completion of
  1391. the first one (initiated here) with data padding */
  1392. CRYP_Authentication_SetDMAConfig(hcryp, inputaddr, headerlength, 0);
  1393. }
  1394. }
  1395. else
  1396. {
  1397. hcryp->CrypInCount = 0;
  1398. /* Set the input address and start DMA transfer */
  1399. CRYP_Authentication_SetDMAConfig(hcryp, inputaddr, hcryp->Init.HeaderSize, 0);
  1400. }
  1401. }
  1402. /*============================================*/
  1403. /* GCM (or CCM when applicable) payload phase */
  1404. /*============================================*/
  1405. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  1406. {
  1407. /* Coming from header phase, wait for CCF flag to be raised
  1408. if header present and fed to the IP in the previous phase */
  1409. if (hcryp->Init.Header != NULL)
  1410. {
  1411. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  1412. {
  1413. hcryp->State = HAL_CRYP_STATE_READY;
  1414. __HAL_UNLOCK(hcryp);
  1415. return HAL_TIMEOUT;
  1416. }
  1417. }
  1418. else
  1419. {
  1420. /* Enable the Peripheral since wasn't in header phase (no header case) */
  1421. __HAL_CRYP_ENABLE(hcryp);
  1422. }
  1423. /* Clear CCF Flag */
  1424. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1425. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
  1426. /* No payload case */
  1427. if (pInputData == NULL)
  1428. {
  1429. hcryp->State = HAL_CRYP_STATE_READY;
  1430. /* Mark that the header phase is over */
  1431. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  1432. /* Process Unlocked */
  1433. __HAL_UNLOCK(hcryp);
  1434. return HAL_OK;
  1435. }
  1436. /* Specific handling to manage payload size less than 128 bits */
  1437. if ((Size % 16) != 0)
  1438. {
  1439. inputaddr = (uint32_t)pInputData;
  1440. outputaddr = (uint32_t)pOutputData;
  1441. if (Size < 16)
  1442. {
  1443. /* Block is now entered in polling mode, no actual gain in resorting to DMA */
  1444. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  1445. hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
  1446. CRYP_Padding(hcryp, (uint32_t)Size, CRYP_POLLING_ON);
  1447. /* Change the CRYP state to ready */
  1448. hcryp->State = HAL_CRYP_STATE_READY;
  1449. /* Mark that the payload phase is over */
  1450. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  1451. /* Call output data transfer complete callback */
  1452. HAL_CRYP_OutCpltCallback(hcryp);
  1453. }
  1454. else
  1455. {
  1456. payloadlength = (Size/16) * 16;
  1457. /* Store the ending transfer points */
  1458. hcryp->pCrypInBuffPtr = pInputData + payloadlength;
  1459. hcryp->pCrypOutBuffPtr = pOutputData + payloadlength;
  1460. hcryp->CrypInCount = (uint32_t)(Size - payloadlength); /* remainder */
  1461. /* Set the input and output addresses and start DMA transfer */
  1462. /* (incomplete DMA transfer, will be wrapped up with data padding
  1463. after completion of the one initiated here) */
  1464. CRYP_Authentication_SetDMAConfig(hcryp, inputaddr, payloadlength, outputaddr);
  1465. }
  1466. }
  1467. else
  1468. {
  1469. hcryp->CrypInCount = 0;
  1470. inputaddr = (uint32_t)pInputData;
  1471. outputaddr = (uint32_t)pOutputData;
  1472. /* Set the input and output addresses and start DMA transfer */
  1473. CRYP_Authentication_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
  1474. }
  1475. }
  1476. /*==================================*/
  1477. /* GCM/GMAC/CCM or CMAC final phase */
  1478. /*==================================*/
  1479. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  1480. {
  1481. /* If coming from header phase (GMAC or CMAC case when applicable),
  1482. wait for CCF flag to be raised */
  1483. if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_HEADER_PHASE)
  1484. {
  1485. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  1486. {
  1487. hcryp->State = HAL_CRYP_STATE_READY;
  1488. __HAL_UNLOCK(hcryp);
  1489. return HAL_TIMEOUT;
  1490. }
  1491. /* Clear CCF Flag */
  1492. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1493. }
  1494. tagaddr = (uint32_t)pOutputData;
  1495. #if defined(AES_CR_NPBLB)
  1496. /* By default, clear NPBLB field */
  1497. CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
  1498. #endif
  1499. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
  1500. /* if the header and payload phases have been bypassed, AES must be enabled again */
  1501. if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
  1502. {
  1503. __HAL_CRYP_ENABLE(hcryp);
  1504. }
  1505. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
  1506. {
  1507. headerlength = hcryp->Init.HeaderSize * 8; /* Header length in bits */
  1508. inputlength = Size * 8; /* input length in bits */
  1509. /* Write the number of bits in the header on 64 bits followed by the number
  1510. of bits in the payload on 64 bits as well */
  1511. #if !defined(AES_CR_NPBLB)
  1512. if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
  1513. {
  1514. hcryp->Instance->DINR = __RBIT((headerlength)>>32);
  1515. hcryp->Instance->DINR = __RBIT(headerlength);
  1516. hcryp->Instance->DINR = __RBIT((inputlength)>>32);
  1517. hcryp->Instance->DINR = __RBIT(inputlength);
  1518. }
  1519. else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
  1520. {
  1521. hcryp->Instance->DINR = __REV((headerlength)>>32);
  1522. hcryp->Instance->DINR = __REV(headerlength);
  1523. hcryp->Instance->DINR = __REV((inputlength)>>32);
  1524. hcryp->Instance->DINR = __REV(inputlength);
  1525. }
  1526. else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
  1527. {
  1528. hcryp->Instance->DINR = __ROR((headerlength)>>32, 16);
  1529. hcryp->Instance->DINR = __ROR(headerlength, 16);
  1530. hcryp->Instance->DINR = __ROR((inputlength)>>32, 16);
  1531. hcryp->Instance->DINR = __ROR(inputlength, 16);
  1532. }
  1533. else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
  1534. {
  1535. hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
  1536. hcryp->Instance->DINR = (uint32_t)(headerlength);
  1537. hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
  1538. hcryp->Instance->DINR = (uint32_t)(inputlength);
  1539. }
  1540. #else
  1541. hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
  1542. hcryp->Instance->DINR = (uint32_t)(headerlength);
  1543. hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
  1544. hcryp->Instance->DINR = (uint32_t)(inputlength);
  1545. #endif
  1546. }
  1547. #if !defined(AES_CR_NPBLB)
  1548. else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  1549. {
  1550. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1551. inputaddr = (uint32_t)pInputData;
  1552. /* Enter the last block made of a 128-bit value formatted
  1553. from the original B0 packet. */
  1554. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1555. inputaddr+=4;
  1556. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1557. inputaddr+=4;
  1558. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1559. inputaddr+=4;
  1560. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  1561. }
  1562. #endif
  1563. /* No DMA transfer is required at that point therefore, the software
  1564. just waits for the CCF flag to be raised. */
  1565. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  1566. {
  1567. hcryp->State = HAL_CRYP_STATE_READY;
  1568. __HAL_UNLOCK(hcryp);
  1569. return HAL_TIMEOUT;
  1570. }
  1571. /* Read the Auth TAG in the IN FIFO */
  1572. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  1573. tagaddr+=4;
  1574. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  1575. tagaddr+=4;
  1576. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  1577. tagaddr+=4;
  1578. *(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
  1579. /* Clear CCF Flag */
  1580. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1581. /* Mark that the final phase is over */
  1582. hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER;
  1583. hcryp->State = HAL_CRYP_STATE_READY;
  1584. /* Disable the Peripheral */
  1585. __HAL_CRYP_DISABLE(hcryp);
  1586. }
  1587. /*=================================================*/
  1588. /* case incorrect hcryp->Init.GCMCMACPhase setting */
  1589. /*=================================================*/
  1590. else
  1591. {
  1592. hcryp->State = HAL_CRYP_STATE_ERROR;
  1593. __HAL_UNLOCK(hcryp);
  1594. return HAL_ERROR;
  1595. }
  1596. /* Process Unlocked */
  1597. __HAL_UNLOCK(hcryp);
  1598. return HAL_OK;
  1599. }
  1600. else
  1601. {
  1602. return HAL_BUSY;
  1603. }
  1604. }
  1605. /**
  1606. * @}
  1607. */
  1608. /** @defgroup CRYPEx_Exported_Functions_Group3 AES suspension/resumption functions
  1609. * @brief Extended processing functions.
  1610. *
  1611. @verbatim
  1612. ==============================================================================
  1613. ##### AES extended suspension and resumption functions #####
  1614. ==============================================================================
  1615. [..] This section provides functions allowing to:
  1616. (+) save in memory the Initialization Vector, the Key registers, the Control register or
  1617. the Suspend registers when a process is suspended by a higher priority message
  1618. (+) write back in CRYP hardware block the saved values listed above when the suspended
  1619. lower priority message processing is resumed.
  1620. @endverbatim
  1621. * @{
  1622. */
  1623. /**
  1624. * @brief In case of message processing suspension, read the Initialization Vector.
  1625. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1626. * the configuration information for CRYP module.
  1627. * @param Output: Pointer to the buffer containing the saved Initialization Vector.
  1628. * @note This value has to be stored for reuse by writing the AES_IVRx registers
  1629. * as soon as the interrupted processing has to be resumed.
  1630. * Applicable to all chaining modes.
  1631. * @note AES must be disabled when reading or resetting the IV values.
  1632. * @retval None
  1633. */
  1634. void HAL_CRYPEx_Read_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output)
  1635. {
  1636. uint32_t outputaddr = (uint32_t)Output;
  1637. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR3);
  1638. outputaddr+=4;
  1639. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR2);
  1640. outputaddr+=4;
  1641. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR1);
  1642. outputaddr+=4;
  1643. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR0);
  1644. }
  1645. /**
  1646. * @brief In case of message processing resumption, rewrite the Initialization
  1647. * Vector in the AES_IVRx registers.
  1648. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1649. * the configuration information for CRYP module.
  1650. * @param Input: Pointer to the buffer containing the saved Initialization Vector to
  1651. * write back in the CRYP hardware block.
  1652. * @note Applicable to all chaining modes.
  1653. * @note AES must be disabled when reading or resetting the IV values.
  1654. * @retval None
  1655. */
  1656. void HAL_CRYPEx_Write_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input)
  1657. {
  1658. uint32_t ivaddr = (uint32_t)Input;
  1659. hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
  1660. ivaddr+=4;
  1661. hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
  1662. ivaddr+=4;
  1663. hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
  1664. ivaddr+=4;
  1665. hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
  1666. }
  1667. /**
  1668. * @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing suspension,
  1669. * read the Suspend Registers.
  1670. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1671. * the configuration information for CRYP module.
  1672. * @param Output: Pointer to the buffer containing the saved Suspend Registers.
  1673. * @note These values have to be stored for reuse by writing back the AES_SUSPxR registers
  1674. * as soon as the interrupted processing has to be resumed.
  1675. * @retval None
  1676. */
  1677. void HAL_CRYPEx_Read_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output)
  1678. {
  1679. uint32_t outputaddr = (uint32_t)Output;
  1680. /* In case of GCM payload phase encryption, check that suspension can be carried out */
  1681. if (READ_BIT(hcryp->Instance->CR, (AES_CR_GCMPH|AES_CR_MODE)) == (CRYP_PAYLOAD_PHASE|CRYP_ALGOMODE_ENCRYPT))
  1682. {
  1683. /* Ensure that Busy flag is reset */
  1684. if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK)
  1685. {
  1686. hcryp->ErrorCode |= HAL_CRYP_BUSY_ERROR;
  1687. hcryp->State = HAL_CRYP_STATE_ERROR;
  1688. /* Process Unlocked */
  1689. __HAL_UNLOCK(hcryp);
  1690. HAL_CRYP_ErrorCallback(hcryp);
  1691. return ;
  1692. }
  1693. }
  1694. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP7R);
  1695. outputaddr+=4;
  1696. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP6R);
  1697. outputaddr+=4;
  1698. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP5R);
  1699. outputaddr+=4;
  1700. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP4R);
  1701. outputaddr+=4;
  1702. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP3R);
  1703. outputaddr+=4;
  1704. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP2R);
  1705. outputaddr+=4;
  1706. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP1R);
  1707. outputaddr+=4;
  1708. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP0R);
  1709. }
  1710. /**
  1711. * @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing resumption, rewrite the Suspend
  1712. * Registers in the AES_SUSPxR registers.
  1713. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1714. * the configuration information for CRYP module.
  1715. * @param Input: Pointer to the buffer containing the saved suspend registers to
  1716. * write back in the CRYP hardware block.
  1717. * @retval None
  1718. */
  1719. void HAL_CRYPEx_Write_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input)
  1720. {
  1721. uint32_t ivaddr = (uint32_t)Input;
  1722. hcryp->Instance->SUSP7R = __REV(*(uint32_t*)(ivaddr));
  1723. ivaddr+=4;
  1724. hcryp->Instance->SUSP6R = __REV(*(uint32_t*)(ivaddr));
  1725. ivaddr+=4;
  1726. hcryp->Instance->SUSP5R = __REV(*(uint32_t*)(ivaddr));
  1727. ivaddr+=4;
  1728. hcryp->Instance->SUSP4R = __REV(*(uint32_t*)(ivaddr));
  1729. ivaddr+=4;
  1730. hcryp->Instance->SUSP3R = __REV(*(uint32_t*)(ivaddr));
  1731. ivaddr+=4;
  1732. hcryp->Instance->SUSP2R = __REV(*(uint32_t*)(ivaddr));
  1733. ivaddr+=4;
  1734. hcryp->Instance->SUSP1R = __REV(*(uint32_t*)(ivaddr));
  1735. ivaddr+=4;
  1736. hcryp->Instance->SUSP0R = __REV(*(uint32_t*)(ivaddr));
  1737. }
  1738. /**
  1739. * @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing suspension, read the Key Registers.
  1740. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1741. * the configuration information for CRYP module.
  1742. * @param Output: Pointer to the buffer containing the saved Key Registers.
  1743. * @param KeySize: Indicates the key size (128 or 256 bits).
  1744. * @note These values have to be stored for reuse by writing back the AES_KEYRx registers
  1745. * as soon as the interrupted processing has to be resumed.
  1746. * @retval None
  1747. */
  1748. void HAL_CRYPEx_Read_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t KeySize)
  1749. {
  1750. uint32_t keyaddr = (uint32_t)Output;
  1751. if (KeySize == CRYP_KEYSIZE_256B)
  1752. {
  1753. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR7);
  1754. keyaddr+=4;
  1755. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR6);
  1756. keyaddr+=4;
  1757. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR5);
  1758. keyaddr+=4;
  1759. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR4);
  1760. keyaddr+=4;
  1761. }
  1762. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR3);
  1763. keyaddr+=4;
  1764. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR2);
  1765. keyaddr+=4;
  1766. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR1);
  1767. keyaddr+=4;
  1768. *(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR0);
  1769. }
  1770. /**
  1771. * @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing resumption, rewrite the Key
  1772. * Registers in the AES_KEYRx registers.
  1773. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1774. * the configuration information for CRYP module.
  1775. * @param Input: Pointer to the buffer containing the saved key registers to
  1776. * write back in the CRYP hardware block.
  1777. * @param KeySize: Indicates the key size (128 or 256 bits)
  1778. * @retval None
  1779. */
  1780. void HAL_CRYPEx_Write_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint32_t KeySize)
  1781. {
  1782. uint32_t keyaddr = (uint32_t)Input;
  1783. if (KeySize == CRYP_KEYSIZE_256B)
  1784. {
  1785. hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
  1786. keyaddr+=4;
  1787. hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
  1788. keyaddr+=4;
  1789. hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
  1790. keyaddr+=4;
  1791. hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
  1792. keyaddr+=4;
  1793. }
  1794. hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
  1795. keyaddr+=4;
  1796. hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
  1797. keyaddr+=4;
  1798. hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
  1799. keyaddr+=4;
  1800. hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
  1801. }
  1802. /**
  1803. * @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing suspension, read the Control Register.
  1804. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1805. * the configuration information for CRYP module.
  1806. * @param Output: Pointer to the buffer containing the saved Control Register.
  1807. * @note This values has to be stored for reuse by writing back the AES_CR register
  1808. * as soon as the interrupted processing has to be resumed.
  1809. * @retval None
  1810. */
  1811. void HAL_CRYPEx_Read_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Output)
  1812. {
  1813. *(uint32_t*)(Output) = hcryp->Instance->CR;
  1814. }
  1815. /**
  1816. * @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing resumption, rewrite the Control
  1817. * Registers in the AES_CR register.
  1818. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1819. * the configuration information for CRYP module.
  1820. * @param Input: Pointer to the buffer containing the saved Control Register to
  1821. * write back in the CRYP hardware block.
  1822. * @retval None
  1823. */
  1824. void HAL_CRYPEx_Write_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Input)
  1825. {
  1826. hcryp->Instance->CR = *(uint32_t*)(Input);
  1827. /* At the same time, set handle state back to READY to be able to resume the AES calculations
  1828. without the processing APIs returning HAL_BUSY when called. */
  1829. hcryp->State = HAL_CRYP_STATE_READY;
  1830. }
  1831. /**
  1832. * @brief Request CRYP processing suspension when in polling or interruption mode.
  1833. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1834. * the configuration information for CRYP module.
  1835. * @note Set the handle field SuspendRequest to the appropriate value so that
  1836. * the on-going CRYP processing is suspended as soon as the required
  1837. * conditions are met.
  1838. * @note It is advised not to suspend the CRYP processing when the DMA controller
  1839. * is managing the data transfer
  1840. * @retval None
  1841. */
  1842. void HAL_CRYPEx_ProcessSuspend(CRYP_HandleTypeDef *hcryp)
  1843. {
  1844. /* Set Handle Suspend Request field */
  1845. hcryp->SuspendRequest = HAL_CRYP_SUSPEND;
  1846. }
  1847. /**
  1848. * @}
  1849. */
  1850. /**
  1851. * @}
  1852. */
  1853. /** @addtogroup CRYPEx_Private_Functions
  1854. * @{
  1855. */
  1856. /**
  1857. * @brief DMA CRYP Input Data process complete callback
  1858. * for GCM, GMAC, CCM or CMAC chaining modes.
  1859. * @note Specific setting of hcryp fields are required only
  1860. * in the case of header phase where no output data DMA
  1861. * transfer is on-going (only input data transfer is enabled
  1862. * in such a case).
  1863. * @param hdma: DMA handle.
  1864. * @retval None
  1865. */
  1866. static void CRYP_Authentication_DMAInCplt(DMA_HandleTypeDef *hdma)
  1867. {
  1868. uint32_t difflength = 0;
  1869. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1870. /* Disable the DMA transfer for input request */
  1871. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  1872. if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  1873. {
  1874. if (hcryp->CrypInCount != 0)
  1875. {
  1876. /* Last block is now entered in polling mode, no actual gain in resorting to DMA */
  1877. difflength = hcryp->CrypInCount;
  1878. hcryp->CrypInCount = 0;
  1879. CRYP_Padding(hcryp, difflength, CRYP_POLLING_OFF);
  1880. }
  1881. hcryp->State = HAL_CRYP_STATE_READY;
  1882. /* Mark that the header phase is over */
  1883. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  1884. }
  1885. /* CCF flag indicating header phase AES processing completion
  1886. will be checked at the start of the next phase:
  1887. - payload phase (GCM or CCM when applicable)
  1888. - final phase (GMAC or CMAC).
  1889. This allows to avoid the Wait on Flag within the IRQ handling. */
  1890. /* Call input data transfer complete callback */
  1891. HAL_CRYP_InCpltCallback(hcryp);
  1892. }
  1893. /**
  1894. * @brief DMA CRYP Output Data process complete callback
  1895. * for GCM, GMAC, CCM or CMAC chaining modes.
  1896. * @note This callback is called only in the payload phase.
  1897. * @param hdma: DMA handle.
  1898. * @retval None
  1899. */
  1900. static void CRYP_Authentication_DMAOutCplt(DMA_HandleTypeDef *hdma)
  1901. {
  1902. uint32_t difflength = 0;
  1903. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1904. /* Disable the DMA transfer for output request */
  1905. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
  1906. /* Clear CCF Flag */
  1907. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1908. /* Initiate additional transfer to wrap-up data feeding to the IP */
  1909. if (hcryp->CrypInCount != 0)
  1910. {
  1911. /* Last block is now entered in polling mode, no actual gain in resorting to DMA */
  1912. difflength = hcryp->CrypInCount;
  1913. hcryp->CrypInCount = 0;
  1914. CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
  1915. }
  1916. /* Change the CRYP state to ready */
  1917. hcryp->State = HAL_CRYP_STATE_READY;
  1918. /* Mark that the payload phase is over */
  1919. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  1920. /* Call output data transfer complete callback */
  1921. HAL_CRYP_OutCpltCallback(hcryp);
  1922. }
  1923. /**
  1924. * @brief DMA CRYP communication error callback
  1925. * for GCM, GMAC, CCM or CMAC chaining modes.
  1926. * @param hdma: DMA handle
  1927. * @retval None
  1928. */
  1929. static void CRYP_Authentication_DMAError(DMA_HandleTypeDef *hdma)
  1930. {
  1931. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1932. hcryp->State= HAL_CRYP_STATE_ERROR;
  1933. hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR;
  1934. HAL_CRYP_ErrorCallback(hcryp);
  1935. /* Clear Error Flag */
  1936. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR);
  1937. }
  1938. /**
  1939. * @brief Handle CRYP block input/output data handling under interruption
  1940. * for GCM, GMAC, CCM or CMAC chaining modes.
  1941. * @note The function is called under interruption only, once
  1942. * interruptions have been enabled by HAL_CRYPEx_AES_Auth_IT().
  1943. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  1944. * the configuration information for CRYP module
  1945. * @retval HAL status
  1946. */
  1947. HAL_StatusTypeDef CRYP_AES_Auth_IT(CRYP_HandleTypeDef *hcryp)
  1948. {
  1949. uint32_t inputaddr = 0x0;
  1950. uint32_t outputaddr = 0x0;
  1951. uint32_t index = 0x0;
  1952. uint32_t addhoc_process = 0;
  1953. uint32_t difflength = 0;
  1954. uint32_t difflengthmod4 = 0;
  1955. uint32_t mask[4][3] = { {0xFF000000, 0xFFFF0000, 0xFFFFFF00}, /* 32-bit data */
  1956. {0x0000FF00, 0x0000FFFF, 0xFF00FFFF}, /* 16-bit data */
  1957. {0x000000FF, 0x0000FFFF, 0x00FFFFFF}, /* 8-bit data */
  1958. {0x000000FF, 0x0000FFFF, 0x00FFFFFF}}; /* Bit data */
  1959. uint32_t mask_index = hcryp->Init.DataType >> AES_CR_DATATYPE_Pos;
  1960. uint32_t intermediate_data[4] = {0};
  1961. if(hcryp->State == HAL_CRYP_STATE_BUSY)
  1962. {
  1963. /*===========================*/
  1964. /* GCM/GMAC(/CCM) init phase */
  1965. /*===========================*/
  1966. if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
  1967. {
  1968. /* Clear Computation Complete Flag */
  1969. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1970. /* Disable Computation Complete Flag and Errors Interrupts */
  1971. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  1972. /* Change the CRYP state */
  1973. hcryp->State = HAL_CRYP_STATE_READY;
  1974. /* Mark that the initialization phase is over */
  1975. hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER;
  1976. /* Process Unlocked */
  1977. __HAL_UNLOCK(hcryp);
  1978. /* Call computation complete callback */
  1979. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  1980. return HAL_OK;
  1981. }
  1982. /*========================================*/
  1983. /* GCM/GMAC (or CCM or CMAC) header phase */
  1984. /*========================================*/
  1985. else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
  1986. {
  1987. /* Check if all input header data have been entered */
  1988. if (hcryp->CrypInCount == 0)
  1989. {
  1990. /* Clear Computation Complete Flag */
  1991. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  1992. /* Disable Computation Complete Flag and Errors Interrupts */
  1993. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  1994. /* Change the CRYP state */
  1995. hcryp->State = HAL_CRYP_STATE_READY;
  1996. /* Mark that the header phase is over */
  1997. hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
  1998. /* Process Unlocked */
  1999. __HAL_UNLOCK(hcryp);
  2000. /* Call computation complete callback */
  2001. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  2002. return HAL_OK;
  2003. }
  2004. /* If suspension flag has been raised, suspend processing */
  2005. else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  2006. {
  2007. /* Clear CCF Flag */
  2008. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2009. /* reset SuspendRequest */
  2010. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  2011. /* Disable Computation Complete Flag and Errors Interrupts */
  2012. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  2013. /* Change the CRYP state */
  2014. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  2015. /* Mark that the header phase is suspended */
  2016. hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
  2017. /* Process Unlocked */
  2018. __HAL_UNLOCK(hcryp);
  2019. return HAL_OK;
  2020. }
  2021. else /* Carry on feeding input data to the CRYP hardware block */
  2022. {
  2023. /* Clear Computation Complete Flag */
  2024. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2025. /* Get the last Input data address */
  2026. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2027. /* Increment/decrement instance pointer/counter */
  2028. if (hcryp->CrypInCount < 16)
  2029. {
  2030. difflength = hcryp->CrypInCount;
  2031. hcryp->CrypInCount = 0;
  2032. addhoc_process = 1;
  2033. difflengthmod4 = difflength%4;
  2034. }
  2035. else
  2036. {
  2037. hcryp->pCrypInBuffPtr += 16;
  2038. hcryp->CrypInCount -= 16;
  2039. }
  2040. #if defined(AES_CR_NPBLB)
  2041. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
  2042. #else
  2043. if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
  2044. #endif
  2045. {
  2046. if (hcryp->CrypInCount == hcryp->Init.HeaderSize)
  2047. {
  2048. /* All B blocks will have been entered after the next
  2049. four DINR writing, so point at header buffer for
  2050. the next iteration */
  2051. hcryp->pCrypInBuffPtr = hcryp->Init.Header;
  2052. }
  2053. }
  2054. /* Write the Input block in the Data Input register */
  2055. if (addhoc_process == 0)
  2056. {
  2057. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2058. inputaddr+=4;
  2059. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2060. inputaddr+=4;
  2061. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2062. inputaddr+=4;
  2063. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2064. }
  2065. else
  2066. {
  2067. /* Header remainder has size less than 128 bits */
  2068. /* Enter complete words when possible */
  2069. for( ; index < (difflength/4); index ++)
  2070. {
  2071. /* Write the Input block in the Data Input register */
  2072. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2073. inputaddr+=4;
  2074. }
  2075. /* Enter incomplete word padded with zeroes if applicable
  2076. (case of header length not a multiple of 32-bits) */
  2077. if (difflengthmod4 != 0)
  2078. {
  2079. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[mask_index][difflengthmod4-1]);
  2080. }
  2081. /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
  2082. for(index=0; index < (4 - ((difflength+3)/4)); index ++)
  2083. {
  2084. hcryp->Instance->DINR = 0;
  2085. }
  2086. }
  2087. return HAL_OK;
  2088. }
  2089. }
  2090. /*=======================*/
  2091. /* GCM/CCM payload phase */
  2092. /*=======================*/
  2093. else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  2094. {
  2095. /* Get the last output data address */
  2096. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2097. /* Specific handling to manage payload size less than 128 bits
  2098. when GCM (or CCM when applicable) encryption or decryption is selected.
  2099. Check here if the last block output data are read */
  2100. #if defined(AES_CR_NPBLB)
  2101. if ((hcryp->CrypOutCount < 16) && \
  2102. (hcryp->CrypOutCount > 0))
  2103. #else
  2104. if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) && \
  2105. (hcryp->CrypOutCount < 16) && \
  2106. (hcryp->CrypOutCount > 0))
  2107. #endif
  2108. {
  2109. addhoc_process = 1;
  2110. difflength = hcryp->CrypOutCount;
  2111. difflengthmod4 = difflength%4;
  2112. hcryp->CrypOutCount = 0; /* mark that no more output data will be needed */
  2113. /* Retrieve intermediate data */
  2114. for(index=0; index < 4; index ++)
  2115. {
  2116. intermediate_data[index] = hcryp->Instance->DOUTR;
  2117. }
  2118. /* Retrieve last words of cyphered data */
  2119. /* First, retrieve complete output words */
  2120. for(index=0; index < (difflength/4); index ++)
  2121. {
  2122. *(uint32_t*)(outputaddr) = intermediate_data[index];
  2123. outputaddr+=4;
  2124. }
  2125. /* Next, retrieve partial output word if applicable;
  2126. at the same time, start masking intermediate data
  2127. with a mask of zeros of same size than the padding
  2128. applied to the last block of payload */
  2129. if (difflengthmod4 != 0)
  2130. {
  2131. intermediate_data[difflength/4] &= mask[mask_index][difflengthmod4-1];
  2132. *(uint32_t*)(outputaddr) = intermediate_data[difflength/4];
  2133. }
  2134. #if !defined(AES_CR_NPBLB)
  2135. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  2136. {
  2137. /* Change again CHMOD configuration to GCM mode */
  2138. __HAL_CRYP_SET_CHAININGMODE(hcryp, CRYP_CHAINMODE_AES_GCM_GMAC);
  2139. /* Select FINAL phase */
  2140. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
  2141. /* Before inserting the intermediate data, carry on masking operation
  2142. with a mask of zeros of same size than the padding applied to the last block of payload */
  2143. for(index=0; index < (4 - ((difflength+3)/4)); index ++)
  2144. {
  2145. intermediate_data[(difflength+3)/4+index] = 0;
  2146. }
  2147. /* Insert intermediate data to trigger an additional DOUTR reading round */
  2148. /* Clear Computation Complete Flag before entering new block */
  2149. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2150. for(index=0; index < 4; index ++)
  2151. {
  2152. hcryp->Instance->DINR = intermediate_data[index];
  2153. }
  2154. }
  2155. else
  2156. #endif
  2157. {
  2158. /* Payload phase is now over */
  2159. /* Clear Computation Complete Flag */
  2160. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2161. /* Disable Computation Complete Flag and Errors Interrupts */
  2162. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  2163. /* Change the CRYP state */
  2164. hcryp->State = HAL_CRYP_STATE_READY;
  2165. /* Mark that the payload phase is over */
  2166. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  2167. /* Process Unlocked */
  2168. __HAL_UNLOCK(hcryp);
  2169. /* Call computation complete callback */
  2170. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  2171. }
  2172. return HAL_OK;
  2173. }
  2174. else
  2175. {
  2176. if (hcryp->CrypOutCount != 0)
  2177. {
  2178. /* Usual case (different than GCM/CCM last block < 128 bits ciphering) */
  2179. /* Retrieve the last block available from the CRYP hardware block:
  2180. read the output block from the Data Output Register */
  2181. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  2182. outputaddr+=4;
  2183. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  2184. outputaddr+=4;
  2185. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  2186. outputaddr+=4;
  2187. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  2188. /* Increment/decrement instance pointer/counter */
  2189. hcryp->pCrypOutBuffPtr += 16;
  2190. hcryp->CrypOutCount -= 16;
  2191. }
  2192. #if !defined(AES_CR_NPBLB)
  2193. else
  2194. {
  2195. /* Software work-around: additional DOUTR reading round to discard the data */
  2196. for(index=0; index < 4; index ++)
  2197. {
  2198. intermediate_data[index] = hcryp->Instance->DOUTR;
  2199. }
  2200. }
  2201. #endif
  2202. }
  2203. /* Check if all output text has been retrieved */
  2204. if (hcryp->CrypOutCount == 0)
  2205. {
  2206. /* Clear Computation Complete Flag */
  2207. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2208. /* Disable Computation Complete Flag and Errors Interrupts */
  2209. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  2210. /* Change the CRYP state */
  2211. hcryp->State = HAL_CRYP_STATE_READY;
  2212. /* Mark that the payload phase is over */
  2213. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
  2214. /* Process Unlocked */
  2215. __HAL_UNLOCK(hcryp);
  2216. /* Call computation complete callback */
  2217. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  2218. return HAL_OK;
  2219. }
  2220. /* If suspension flag has been raised, suspend processing */
  2221. else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
  2222. {
  2223. /* Clear CCF Flag */
  2224. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2225. /* reset SuspendRequest */
  2226. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  2227. /* Disable Computation Complete Flag and Errors Interrupts */
  2228. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  2229. /* Change the CRYP state */
  2230. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  2231. /* Mark that the payload phase is suspended */
  2232. hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_SUSPENDED;
  2233. /* Process Unlocked */
  2234. __HAL_UNLOCK(hcryp);
  2235. return HAL_OK;
  2236. }
  2237. else /* Output data are still expected, carry on feeding the CRYP
  2238. hardware block with input data */
  2239. {
  2240. /* Clear Computation Complete Flag */
  2241. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2242. /* Get the last Input data address */
  2243. inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2244. /* Usual input data feeding case */
  2245. if (hcryp->CrypInCount < 16)
  2246. {
  2247. difflength = (uint32_t) (hcryp->CrypInCount);
  2248. difflengthmod4 = difflength%4;
  2249. hcryp->CrypInCount = 0;
  2250. #if defined(AES_CR_NPBLB)
  2251. /* In case of GCM encryption or CCM decryption, specify the number of padding
  2252. bytes in last block of payload */
  2253. if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC)
  2254. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT))
  2255. || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM)
  2256. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT)))
  2257. {
  2258. /* Set NPBLB field in writing the number of padding bytes
  2259. for the last block of payload */
  2260. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, (16 - difflength) << AES_POSITION_CR_NPBLB);
  2261. }
  2262. #else
  2263. /* Software workaround applied to GCM encryption only */
  2264. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  2265. {
  2266. /* Change the mode configured in CHMOD bits of CR register to select CTR mode */
  2267. __HAL_CRYP_SET_CHAININGMODE(hcryp, CRYP_CHAINMODE_AES_CTR);
  2268. }
  2269. #endif
  2270. /* Insert the last block (which size is inferior to 128 bits) padded with zeroes
  2271. to have a complete block of 128 bits */
  2272. for(index=0; index < (difflength/4); index ++)
  2273. {
  2274. /* Write the Input block in the Data Input register */
  2275. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2276. inputaddr+=4;
  2277. }
  2278. /* If required, manage input data size not multiple of 32 bits */
  2279. if (difflengthmod4 != 0)
  2280. {
  2281. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[mask_index][difflengthmod4-1]);
  2282. }
  2283. /* Wrap-up in padding with zero-words if applicable */
  2284. for(index=0; index < (4 - ((difflength+3)/4)); index ++)
  2285. {
  2286. hcryp->Instance->DINR = 0;
  2287. }
  2288. }
  2289. else
  2290. {
  2291. hcryp->pCrypInBuffPtr += 16;
  2292. hcryp->CrypInCount -= 16;
  2293. /* Write the Input block in the Data Input register */
  2294. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2295. inputaddr+=4;
  2296. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2297. inputaddr+=4;
  2298. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2299. inputaddr+=4;
  2300. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2301. }
  2302. return HAL_OK;
  2303. }
  2304. }
  2305. /*=======================================*/
  2306. /* GCM/GMAC (or CCM or CMAC) final phase */
  2307. /*=======================================*/
  2308. else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
  2309. {
  2310. /* Clear Computation Complete Flag */
  2311. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2312. /* Get the last output data address */
  2313. outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2314. /* Retrieve the last expected data from the CRYP hardware block:
  2315. read the output block from the Data Output Register */
  2316. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  2317. outputaddr+=4;
  2318. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  2319. outputaddr+=4;
  2320. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  2321. outputaddr+=4;
  2322. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  2323. /* Disable Computation Complete Flag and Errors Interrupts */
  2324. __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
  2325. /* Change the CRYP state */
  2326. hcryp->State = HAL_CRYP_STATE_READY;
  2327. /* Mark that the header phase is over */
  2328. hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER;
  2329. /* Disable the Peripheral */
  2330. __HAL_CRYP_DISABLE(hcryp);
  2331. /* Process Unlocked */
  2332. __HAL_UNLOCK(hcryp);
  2333. /* Call computation complete callback */
  2334. HAL_CRYPEx_ComputationCpltCallback(hcryp);
  2335. return HAL_OK;
  2336. }
  2337. else
  2338. {
  2339. /* Clear Computation Complete Flag */
  2340. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2341. hcryp->State = HAL_CRYP_STATE_ERROR;
  2342. __HAL_UNLOCK(hcryp);
  2343. return HAL_ERROR;
  2344. }
  2345. }
  2346. else
  2347. {
  2348. return HAL_BUSY;
  2349. }
  2350. }
  2351. /**
  2352. * @brief Set the DMA configuration and start the DMA transfer
  2353. * for GCM, GMAC, CCM or CMAC chaining modes.
  2354. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2355. * the configuration information for CRYP module.
  2356. * @param inputaddr: Address of the Input buffer.
  2357. * @param Size: Size of the Input buffer un bytes, must be a multiple of 16.
  2358. * @param outputaddr: Address of the Output buffer, null pointer when no output DMA stream
  2359. * has to be configured.
  2360. * @retval None
  2361. */
  2362. static void CRYP_Authentication_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  2363. {
  2364. /* Set the input CRYP DMA transfer complete callback */
  2365. hcryp->hdmain->XferCpltCallback = CRYP_Authentication_DMAInCplt;
  2366. /* Set the DMA error callback */
  2367. hcryp->hdmain->XferErrorCallback = CRYP_Authentication_DMAError;
  2368. if (outputaddr != 0)
  2369. {
  2370. /* Set the output CRYP DMA transfer complete callback */
  2371. hcryp->hdmaout->XferCpltCallback = CRYP_Authentication_DMAOutCplt;
  2372. /* Set the DMA error callback */
  2373. hcryp->hdmaout->XferErrorCallback = CRYP_Authentication_DMAError;
  2374. }
  2375. /* Enable the CRYP peripheral */
  2376. __HAL_CRYP_ENABLE(hcryp);
  2377. /* Enable the DMA input stream */
  2378. HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4);
  2379. /* Enable the DMA input request */
  2380. SET_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  2381. if (outputaddr != 0)
  2382. {
  2383. /* Enable the DMA output stream */
  2384. HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4);
  2385. /* Enable the DMA output request */
  2386. SET_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
  2387. }
  2388. }
  2389. /**
  2390. * @brief Write/read input/output data in polling mode.
  2391. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2392. * the configuration information for CRYP module.
  2393. * @param Input: Pointer to the Input buffer.
  2394. * @param Ilength: Length of the Input buffer in bytes, must be a multiple of 16.
  2395. * @param Output: Pointer to the returned buffer.
  2396. * @param Timeout: Specify Timeout value.
  2397. * @retval HAL status
  2398. */
  2399. static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
  2400. {
  2401. uint32_t index = 0;
  2402. uint32_t inputaddr = (uint32_t)Input;
  2403. uint32_t outputaddr = (uint32_t)Output;
  2404. for(index=0; (index < Ilength); index += 16)
  2405. {
  2406. /* Write the Input block in the Data Input register */
  2407. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2408. inputaddr+=4;
  2409. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2410. inputaddr+=4;
  2411. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2412. inputaddr+=4;
  2413. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2414. inputaddr+=4;
  2415. /* Wait for CCF flag to be raised */
  2416. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  2417. {
  2418. hcryp->State = HAL_CRYP_STATE_READY;
  2419. __HAL_UNLOCK(hcryp);
  2420. return HAL_TIMEOUT;
  2421. }
  2422. /* Clear CCF Flag */
  2423. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2424. /* Read the Output block from the Data Output Register */
  2425. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  2426. outputaddr+=4;
  2427. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  2428. outputaddr+=4;
  2429. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  2430. outputaddr+=4;
  2431. *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
  2432. outputaddr+=4;
  2433. /* If the suspension flag has been raised and if the processing is not about
  2434. to end, suspend processing */
  2435. if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16) < Ilength))
  2436. {
  2437. /* Reset SuspendRequest */
  2438. hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
  2439. /* Save current reading and writing locations of Input and Output buffers */
  2440. hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
  2441. hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
  2442. /* Save the number of bytes that remain to be processed at this point */
  2443. hcryp->CrypInCount = Ilength - (index+16);
  2444. /* Change the CRYP state */
  2445. hcryp->State = HAL_CRYP_STATE_SUSPENDED;
  2446. return HAL_OK;
  2447. }
  2448. }
  2449. /* Return function status */
  2450. return HAL_OK;
  2451. }
  2452. /**
  2453. * @brief Read derivative key in polling mode when CRYP hardware block is set
  2454. * in key derivation operating mode (mode 2).
  2455. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2456. * the configuration information for CRYP module.
  2457. * @param Output: Pointer to the returned buffer.
  2458. * @param Timeout: Specify Timeout value.
  2459. * @retval HAL status
  2460. */
  2461. static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout)
  2462. {
  2463. uint32_t outputaddr = (uint32_t)Output;
  2464. /* Wait for CCF flag to be raised */
  2465. if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
  2466. {
  2467. hcryp->State = HAL_CRYP_STATE_READY;
  2468. __HAL_UNLOCK(hcryp);
  2469. return HAL_TIMEOUT;
  2470. }
  2471. /* Clear CCF Flag */
  2472. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2473. /* Read the derivative key from the AES_KEYRx registers */
  2474. if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
  2475. {
  2476. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
  2477. outputaddr+=4;
  2478. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
  2479. outputaddr+=4;
  2480. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
  2481. outputaddr+=4;
  2482. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
  2483. outputaddr+=4;
  2484. }
  2485. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
  2486. outputaddr+=4;
  2487. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
  2488. outputaddr+=4;
  2489. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
  2490. outputaddr+=4;
  2491. *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
  2492. /* Return function status */
  2493. return HAL_OK;
  2494. }
  2495. /**
  2496. * @brief Set the DMA configuration and start the DMA transfer.
  2497. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2498. * the configuration information for CRYP module.
  2499. * @param inputaddr: Address of the Input buffer.
  2500. * @param Size: Size of the Input buffer in bytes, must be a multiple of 16.
  2501. * @param outputaddr: Address of the Output buffer.
  2502. * @retval None
  2503. */
  2504. static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
  2505. {
  2506. /* Set the CRYP DMA transfer complete callback */
  2507. hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
  2508. /* Set the DMA error callback */
  2509. hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
  2510. /* Set the CRYP DMA transfer complete callback */
  2511. hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
  2512. /* Set the DMA error callback */
  2513. hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
  2514. /* Enable the DMA input stream */
  2515. HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, Size/4);
  2516. /* Enable the DMA output stream */
  2517. HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, Size/4);
  2518. /* Enable In and Out DMA requests */
  2519. SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN));
  2520. /* Enable the CRYP peripheral */
  2521. __HAL_CRYP_ENABLE(hcryp);
  2522. }
  2523. /**
  2524. * @brief Handle CRYP hardware block Timeout when waiting for CCF flag to be raised.
  2525. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2526. * the configuration information for CRYP module.
  2527. * @param Timeout: Timeout duration.
  2528. * @retval HAL status
  2529. */
  2530. static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  2531. {
  2532. uint32_t tickstart = 0;
  2533. /* Get timeout */
  2534. tickstart = HAL_GetTick();
  2535. while(HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
  2536. {
  2537. /* Check for the Timeout */
  2538. if(Timeout != HAL_MAX_DELAY)
  2539. {
  2540. if((HAL_GetTick() - tickstart ) > Timeout)
  2541. {
  2542. return HAL_TIMEOUT;
  2543. }
  2544. }
  2545. }
  2546. return HAL_OK;
  2547. }
  2548. /**
  2549. * @brief Wait for Busy Flag to be reset during a GCM payload encryption process suspension.
  2550. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2551. * the configuration information for CRYP module.
  2552. * @param Timeout: Timeout duration.
  2553. * @retval HAL status
  2554. */
  2555. static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef *hcryp, uint32_t Timeout)
  2556. {
  2557. uint32_t tickstart = 0;
  2558. /* Get timeout */
  2559. tickstart = HAL_GetTick();
  2560. while(HAL_IS_BIT_SET(hcryp->Instance->SR, AES_SR_BUSY))
  2561. {
  2562. /* Check for the Timeout */
  2563. if(Timeout != HAL_MAX_DELAY)
  2564. {
  2565. if((HAL_GetTick() - tickstart ) > Timeout)
  2566. {
  2567. return HAL_TIMEOUT;
  2568. }
  2569. }
  2570. }
  2571. return HAL_OK;
  2572. }
  2573. /**
  2574. * @brief DMA CRYP Input Data process complete callback.
  2575. * @param hdma: DMA handle.
  2576. * @retval None
  2577. */
  2578. static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
  2579. {
  2580. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2581. /* Disable the DMA transfer for input request */
  2582. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
  2583. /* Call input data transfer complete callback */
  2584. HAL_CRYP_InCpltCallback(hcryp);
  2585. }
  2586. /**
  2587. * @brief DMA CRYP Output Data process complete callback.
  2588. * @param hdma: DMA handle.
  2589. * @retval None
  2590. */
  2591. static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
  2592. {
  2593. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2594. /* Disable the DMA transfer for output request */
  2595. CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
  2596. /* Clear CCF Flag */
  2597. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2598. /* Disable CRYP */
  2599. __HAL_CRYP_DISABLE(hcryp);
  2600. /* Change the CRYP state to ready */
  2601. hcryp->State = HAL_CRYP_STATE_READY;
  2602. /* Call output data transfer complete callback */
  2603. HAL_CRYP_OutCpltCallback(hcryp);
  2604. }
  2605. /**
  2606. * @brief DMA CRYP communication error callback.
  2607. * @param hdma: DMA handle.
  2608. * @retval None
  2609. */
  2610. static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
  2611. {
  2612. CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2613. hcryp->State= HAL_CRYP_STATE_ERROR;
  2614. hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR;
  2615. HAL_CRYP_ErrorCallback(hcryp);
  2616. /* Clear Error Flag */
  2617. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR);
  2618. }
  2619. /**
  2620. * @brief Last header or payload block padding when size is not a multiple of 128 bits.
  2621. * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  2622. * the configuration information for CRYP module.
  2623. * @param difflength: size remainder after having fed all complete 128-bit blocks.
  2624. * @param polling: specifies whether or not polling on CCF must be done after having
  2625. * entered a complete block.
  2626. * @retval None
  2627. */
  2628. static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling)
  2629. {
  2630. uint32_t index = 0;
  2631. uint32_t difflengthmod4 = difflength%4;
  2632. uint32_t inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
  2633. uint32_t outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
  2634. uint32_t mask[4][3] = { {0xFF000000, 0xFFFF0000, 0xFFFFFF00}, /* 32-bit data */
  2635. {0x0000FF00, 0x0000FFFF, 0xFF00FFFF}, /* 16-bit data */
  2636. {0x000000FF, 0x0000FFFF, 0x00FFFFFF}, /* 8-bit data */
  2637. {0x000000FF, 0x0000FFFF, 0x00FFFFFF}}; /* Bit data */
  2638. uint32_t mask_index = hcryp->Init.DataType >> AES_CR_DATATYPE_Pos;
  2639. uint32_t intermediate_data[4] = {0};
  2640. #if defined(AES_CR_NPBLB)
  2641. /* In case of GCM encryption or CCM decryption, specify the number of padding
  2642. bytes in last block of payload */
  2643. if (READ_BIT(hcryp->Instance->CR,AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE)
  2644. {
  2645. if (((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_GCM_GMAC)
  2646. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_ENCRYPT))
  2647. || ((READ_BIT(hcryp->Instance->CR, AES_CR_CHMOD) == CRYP_CHAINMODE_AES_CCM)
  2648. && (READ_BIT(hcryp->Instance->CR, AES_CR_MODE) == CRYP_ALGOMODE_DECRYPT)))
  2649. {
  2650. /* Set NPBLB field in writing the number of padding bytes
  2651. for the last block of payload */
  2652. MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, (16 - difflength) << AES_POSITION_CR_NPBLB);
  2653. }
  2654. }
  2655. #else
  2656. /* Software workaround applied to GCM encryption only */
  2657. if ((hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) &&
  2658. (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT))
  2659. {
  2660. /* Change the mode configured in CHMOD bits of CR register to select CTR mode */
  2661. __HAL_CRYP_SET_CHAININGMODE(hcryp, CRYP_CHAINMODE_AES_CTR);
  2662. }
  2663. #endif
  2664. /* Wrap-up entering header or payload data */
  2665. /* Enter complete words when possible */
  2666. for(index=0; index < (difflength/4); index ++)
  2667. {
  2668. /* Write the Input block in the Data Input register */
  2669. hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
  2670. inputaddr+=4;
  2671. }
  2672. /* Enter incomplete word padded with zeroes if applicable
  2673. (case of header length not a multiple of 32-bits) */
  2674. if (difflengthmod4 != 0)
  2675. {
  2676. hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[mask_index][difflengthmod4-1]);
  2677. }
  2678. /* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
  2679. for(index=0; index < (4 - ((difflength+3)/4)); index ++)
  2680. {
  2681. hcryp->Instance->DINR = 0;
  2682. }
  2683. if (polling == CRYP_POLLING_ON)
  2684. {
  2685. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  2686. {
  2687. hcryp->State = HAL_CRYP_STATE_READY;
  2688. __HAL_UNLOCK(hcryp);
  2689. HAL_CRYP_ErrorCallback(hcryp);
  2690. }
  2691. /* Clear CCF Flag */
  2692. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2693. }
  2694. /* if payload */
  2695. if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
  2696. {
  2697. /* Retrieve intermediate data */
  2698. for(index=0; index < 4; index ++)
  2699. {
  2700. intermediate_data[index] = hcryp->Instance->DOUTR;
  2701. }
  2702. /* Retrieve last words of cyphered data */
  2703. /* First, retrieve complete output words */
  2704. for(index=0; index < (difflength/4); index ++)
  2705. {
  2706. *(uint32_t*)(outputaddr) = intermediate_data[index];
  2707. outputaddr+=4;
  2708. }
  2709. /* Next, retrieve partial output word if applicable;
  2710. at the same time, start masking intermediate data
  2711. with a mask of zeros of same size than the padding
  2712. applied to the last block of payload */
  2713. if (difflengthmod4 != 0)
  2714. {
  2715. intermediate_data[difflength/4] &= mask[mask_index][difflengthmod4-1];
  2716. *(uint32_t*)(outputaddr) = intermediate_data[difflength/4];
  2717. }
  2718. #if !defined(AES_CR_NPBLB)
  2719. /* Software workaround applied to GCM encryption only,
  2720. applicable for AES IP v2 version (where NPBLB is not defined) */
  2721. if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
  2722. {
  2723. /* Change again CHMOD configuration to GCM mode */
  2724. __HAL_CRYP_SET_CHAININGMODE(hcryp, CRYP_CHAINMODE_AES_GCM_GMAC);
  2725. /* Select FINAL phase */
  2726. MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
  2727. /* Before inserting the intermediate data, carry on masking operation
  2728. with a mask of zeros of same size than the padding applied to the last block of payload */
  2729. for(index=0; index < (4 - ((difflength+3)/4)); index ++)
  2730. {
  2731. intermediate_data[(difflength+3)/4+index] = 0;
  2732. }
  2733. /* Insert intermediate data */
  2734. for(index=0; index < 4; index ++)
  2735. {
  2736. hcryp->Instance->DINR = intermediate_data[index];
  2737. }
  2738. /* Wait for completion, and read data on DOUT. This data is to discard. */
  2739. if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
  2740. {
  2741. hcryp->State = HAL_CRYP_STATE_READY;
  2742. __HAL_UNLOCK(hcryp);
  2743. HAL_CRYP_ErrorCallback(hcryp);
  2744. }
  2745. /* Read data to discard */
  2746. /* Clear CCF Flag */
  2747. __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
  2748. for(index=0; index < 4; index ++)
  2749. {
  2750. intermediate_data[index] = hcryp->Instance->DOUTR;
  2751. }
  2752. } /* if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) */
  2753. #endif /* !defined(AES_CR_NPBLB) */
  2754. } /* if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) */
  2755. }
  2756. /**
  2757. * @}
  2758. */
  2759. /**
  2760. * @}
  2761. */
  2762. /**
  2763. * @}
  2764. */
  2765. #endif /* defined (STM32L442xx) || defined (STM32L443xx) || defined (STM32L462xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L4A6xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) */
  2766. #endif /* HAL_CRYP_MODULE_ENABLED */
  2767. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/