stm32l4xx_hal_pka.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_pka.c
  4. * @author MCD Application Team
  5. * @brief PKA HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of public key accelerator(PKA):
  8. * + Initialization and de-initialization functions
  9. * + Start an operation
  10. * + Retrieve the operation result
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2017 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ==============================================================================
  25. ##### How to use this driver #####
  26. ==============================================================================
  27. [..]
  28. The PKA HAL driver can be used as follows:
  29. (#) Declare a PKA_HandleTypeDef handle structure, for example: PKA_HandleTypeDef hpka;
  30. (#) Initialize the PKA low level resources by implementing the HAL_PKA_MspInit() API:
  31. (##) Enable the PKA interface clock
  32. (##) NVIC configuration if you need to use interrupt process
  33. (+++) Configure the PKA interrupt priority
  34. (+++) Enable the NVIC PKA IRQ Channel
  35. (#) Initialize the PKA registers by calling the HAL_PKA_Init() API which trig
  36. HAL_PKA_MspInit().
  37. (#) Fill entirely the input structure corresponding to your operation:
  38. For instance: PKA_ModExpInTypeDef for HAL_PKA_ModExp().
  39. (#) Execute the operation (in polling or interrupt) and check the returned value.
  40. (#) Retrieve the result of the operation (For instance, HAL_PKA_ModExp_GetResult for
  41. HAL_PKA_ModExp operation). The function to gather the result is different for each
  42. kind of operation. The correspondence can be found in the following section.
  43. (#) Call the function HAL_PKA_DeInit() to restore the default configuration which trig
  44. HAL_PKA_MspDeInit().
  45. *** High level operation ***
  46. =================================
  47. [..]
  48. (+) Input structure requires buffers as uint8_t array.
  49. (+) Output structure requires buffers as uint8_t array.
  50. (+) Modular exponentiation using:
  51. (++) HAL_PKA_ModExp().
  52. (++) HAL_PKA_ModExp_IT().
  53. (++) HAL_PKA_ModExpFastMode().
  54. (++) HAL_PKA_ModExpFastMode_IT().
  55. (++) HAL_PKA_ModExp_GetResult() to retrieve the result of the operation.
  56. (+) RSA Chinese Remainder Theorem (CRT) using:
  57. (++) HAL_PKA_RSACRTExp().
  58. (++) HAL_PKA_RSACRTExp_IT().
  59. (++) HAL_PKA_RSACRTExp_GetResult() to retrieve the result of the operation.
  60. (+) ECC Point Check using:
  61. (++) HAL_PKA_PointCheck().
  62. (++) HAL_PKA_PointCheck_IT().
  63. (++) HAL_PKA_PointCheck_IsOnCurve() to retrieve the result of the operation.
  64. (+) ECDSA Sign
  65. (++) HAL_PKA_ECDSASign().
  66. (++) HAL_PKA_ECDSASign_IT().
  67. (++) HAL_PKA_ECDSASign_GetResult() to retrieve the result of the operation.
  68. (+) ECDSA Verify
  69. (++) HAL_PKA_ECDSAVerif().
  70. (++) HAL_PKA_ECDSAVerif_IT().
  71. (++) HAL_PKA_ECDSAVerif_IsValidSignature() to retrieve the result of the operation.
  72. (+) ECC Scalar Multiplication using:
  73. (++) HAL_PKA_ECCMul().
  74. (++) HAL_PKA_ECCMul_IT().
  75. (++) HAL_PKA_ECCMulFastMode().
  76. (++) HAL_PKA_ECCMulFastMode_IT().
  77. (++) HAL_PKA_ECCMul_GetResult() to retrieve the result of the operation.
  78. *** Low level operation ***
  79. =================================
  80. [..]
  81. (+) Input structure requires buffers as uint32_t array.
  82. (+) Output structure requires buffers as uint32_t array.
  83. (+) Arithmetic addition using:
  84. (++) HAL_PKA_Add().
  85. (++) HAL_PKA_Add_IT().
  86. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  87. The resulting size can be the input parameter or the input parameter size + 1 (overflow).
  88. (+) Arithmetic subtraction using:
  89. (++) HAL_PKA_Sub().
  90. (++) HAL_PKA_Sub_IT().
  91. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  92. (+) Arithmetic multiplication using:
  93. (++) HAL_PKA_Mul().
  94. (++) HAL_PKA_Mul_IT().
  95. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  96. (+) Comparison using:
  97. (++) HAL_PKA_Cmp().
  98. (++) HAL_PKA_Cmp_IT().
  99. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  100. (+) Modular addition using:
  101. (++) HAL_PKA_ModAdd().
  102. (++) HAL_PKA_ModAdd_IT().
  103. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  104. (+) Modular subtraction using:
  105. (++) HAL_PKA_ModSub().
  106. (++) HAL_PKA_ModSub_IT().
  107. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  108. (+) Modular inversion using:
  109. (++) HAL_PKA_ModInv().
  110. (++) HAL_PKA_ModInv_IT().
  111. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  112. (+) Modular reduction using:
  113. (++) HAL_PKA_ModRed().
  114. (++) HAL_PKA_ModRed_IT().
  115. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  116. (+) Montgomery multiplication using:
  117. (++) HAL_PKA_MontgomeryMul().
  118. (++) HAL_PKA_MontgomeryMul_IT().
  119. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  120. *** Montgomery parameter ***
  121. =================================
  122. (+) For some operation, the computation of the Montgomery parameter is a prerequisite.
  123. (+) Input structure requires buffers as uint8_t array.
  124. (+) Output structure requires buffers as uint32_t array.(Only used inside PKA).
  125. (+) You can compute the Montgomery parameter using:
  126. (++) HAL_PKA_MontgomeryParam().
  127. (++) HAL_PKA_MontgomeryParam_IT().
  128. (++) HAL_PKA_MontgomeryParam_GetResult() to retrieve the result of the operation.
  129. *** Polling mode operation ***
  130. ===================================
  131. [..]
  132. (+) When an operation is started in polling mode, the function returns when:
  133. (++) A timeout is encounter.
  134. (++) The operation is completed.
  135. *** Interrupt mode operation ***
  136. ===================================
  137. [..]
  138. (+) Add HAL_PKA_IRQHandler to the IRQHandler of PKA.
  139. (+) Enable the IRQ using HAL_NVIC_EnableIRQ().
  140. (+) When an operation is started in interrupt mode, the function returns immediately.
  141. (+) When the operation is completed, the callback HAL_PKA_OperationCpltCallback is called.
  142. (+) When an error is encountered, the callback HAL_PKA_ErrorCallback is called.
  143. (+) To stop any operation in interrupt mode, use HAL_PKA_Abort().
  144. *** Utilities ***
  145. ===================================
  146. [..]
  147. (+) To clear the PKA RAM, use HAL_PKA_RAMReset().
  148. (+) To get current state, use HAL_PKA_GetState().
  149. (+) To get current error, use HAL_PKA_GetError().
  150. *** Callback registration ***
  151. =============================================
  152. [..]
  153. The compilation flag USE_HAL_PKA_REGISTER_CALLBACKS, when set to 1,
  154. allows the user to configure dynamically the driver callbacks.
  155. Use Functions HAL_PKA_RegisterCallback()
  156. to register an interrupt callback.
  157. [..]
  158. Function HAL_PKA_RegisterCallback() allows to register following callbacks:
  159. (+) OperationCpltCallback : callback for End of operation.
  160. (+) ErrorCallback : callback for error detection.
  161. (+) MspInitCallback : callback for Msp Init.
  162. (+) MspDeInitCallback : callback for Msp DeInit.
  163. This function takes as parameters the HAL peripheral handle, the Callback ID
  164. and a pointer to the user callback function.
  165. [..]
  166. Use function HAL_PKA_UnRegisterCallback to reset a callback to the default
  167. weak function.
  168. [..]
  169. HAL_PKA_UnRegisterCallback takes as parameters the HAL peripheral handle,
  170. and the Callback ID.
  171. This function allows to reset following callbacks:
  172. (+) OperationCpltCallback : callback for End of operation.
  173. (+) ErrorCallback : callback for error detection.
  174. (+) MspInitCallback : callback for Msp Init.
  175. (+) MspDeInitCallback : callback for Msp DeInit.
  176. [..]
  177. By default, after the HAL_PKA_Init() and when the state is HAL_PKA_STATE_RESET
  178. all callbacks are set to the corresponding weak functions:
  179. examples HAL_PKA_OperationCpltCallback(), HAL_PKA_ErrorCallback().
  180. Exception done for MspInit and MspDeInit functions that are
  181. reset to the legacy weak functions in the HAL_PKA_Init()/ HAL_PKA_DeInit() only when
  182. these callbacks are null (not registered beforehand).
  183. [..]
  184. If MspInit or MspDeInit are not null, the HAL_PKA_Init()/ HAL_PKA_DeInit()
  185. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  186. [..]
  187. Callbacks can be registered/unregistered in HAL_PKA_STATE_READY state only.
  188. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  189. in HAL_PKA_STATE_READY or HAL_PKA_STATE_RESET state,
  190. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  191. [..]
  192. Then, the user first registers the MspInit/MspDeInit user callbacks
  193. using HAL_PKA_RegisterCallback() before calling HAL_PKA_DeInit()
  194. or HAL_PKA_Init() function.
  195. [..]
  196. When the compilation flag USE_HAL_PKA_REGISTER_CALLBACKS is set to 0 or
  197. not defined, the callback registration feature is not available and all callbacks
  198. are set to the corresponding weak functions.
  199. @endverbatim
  200. ******************************************************************************
  201. */
  202. /* Includes ------------------------------------------------------------------*/
  203. #include "stm32l4xx_hal.h"
  204. /** @addtogroup STM32L4xx_HAL_Driver
  205. * @{
  206. */
  207. #if defined(PKA) && defined(HAL_PKA_MODULE_ENABLED)
  208. /** @defgroup PKA PKA
  209. * @brief PKA HAL module driver.
  210. * @{
  211. */
  212. /* Private typedef -----------------------------------------------------------*/
  213. /* Private define ------------------------------------------------------------*/
  214. /** @defgroup PKA_Private_Define PKA Private Define
  215. * @{
  216. */
  217. #define PKA_RAM_SIZE 894U
  218. #define PKA_RAM_ERASE_TIMEOUT 1000U
  219. /* Private macro -------------------------------------------------------------*/
  220. #define __PKA_RAM_PARAM_END(TAB,INDEX) do{ \
  221. TAB[INDEX] = 0UL; \
  222. } while(0)
  223. /**
  224. * @}
  225. */
  226. /* Private variables ---------------------------------------------------------*/
  227. /* Private function prototypes -----------------------------------------------*/
  228. /** @defgroup PKA_Private_Functions PKA Private Functions
  229. * @{
  230. */
  231. uint32_t PKA_GetMode(const PKA_HandleTypeDef *hpka);
  232. HAL_StatusTypeDef PKA_PollEndOfOperation(const PKA_HandleTypeDef *hpka, uint32_t Timeout, uint32_t Tickstart);
  233. uint32_t PKA_CheckError(const PKA_HandleTypeDef *hpka, uint32_t mode);
  234. uint32_t PKA_GetBitSize_u8(uint32_t byteNumber);
  235. uint32_t PKA_GetOptBitSize_u8(uint32_t byteNumber, uint8_t msb);
  236. uint32_t PKA_GetBitSize_u32(uint32_t wordNumber);
  237. uint32_t PKA_GetArraySize_u8(uint32_t bitSize);
  238. void PKA_Memcpy_u32_to_u8(uint8_t dst[], __IO const uint32_t src[], size_t n);
  239. void PKA_Memcpy_u8_to_u32(__IO uint32_t dst[], const uint8_t src[], size_t n);
  240. void PKA_Memcpy_u32_to_u32(__IO uint32_t dst[], __IO const uint32_t src[], size_t n);
  241. HAL_StatusTypeDef PKA_Process(PKA_HandleTypeDef *hpka, uint32_t mode, uint32_t Timeout);
  242. HAL_StatusTypeDef PKA_Process_IT(PKA_HandleTypeDef *hpka, uint32_t mode);
  243. void PKA_ModExp_Set(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in);
  244. void PKA_ModExpFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in);
  245. void PKA_ECDSASign_Set(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in);
  246. void PKA_ECDSAVerif_Set(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in);
  247. void PKA_RSACRTExp_Set(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in);
  248. void PKA_PointCheck_Set(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in);
  249. void PKA_ECCMul_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in);
  250. void PKA_ECCMulFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in);
  251. void PKA_ModRed_Set(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in);
  252. void PKA_ModInv_Set(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in);
  253. void PKA_MontgomeryParam_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint8_t *pOp1);
  254. void PKA_ARI_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint32_t *pOp1, const uint32_t *pOp2,
  255. const uint8_t *pOp3);
  256. /**
  257. * @}
  258. */
  259. /* Exported functions --------------------------------------------------------*/
  260. /** @defgroup PKA_Exported_Functions PKA Exported Functions
  261. * @{
  262. */
  263. /** @defgroup PKA_Exported_Functions_Group1 Initialization and de-initialization functions
  264. * @brief Initialization and de-initialization functions
  265. *
  266. @verbatim
  267. ===============================================================================
  268. ##### Initialization and de-initialization functions #####
  269. ===============================================================================
  270. [..] This subsection provides a set of functions allowing to initialize and
  271. deinitialize the PKAx peripheral:
  272. (+) User must implement HAL_PKA_MspInit() function in which he configures
  273. all related peripherals resources (CLOCK, IT and NVIC ).
  274. (+) Call the function HAL_PKA_Init() to configure the device.
  275. (+) Call the function HAL_PKA_DeInit() to restore the default configuration
  276. of the selected PKAx peripheral.
  277. @endverbatim
  278. * @{
  279. */
  280. /**
  281. * @brief Initialize the PKA according to the specified
  282. * parameters in the PKA_InitTypeDef and initialize the associated handle.
  283. * @param hpka PKA handle
  284. * @retval HAL status
  285. */
  286. HAL_StatusTypeDef HAL_PKA_Init(PKA_HandleTypeDef *hpka)
  287. {
  288. HAL_StatusTypeDef err = HAL_OK;
  289. uint32_t tickstart;
  290. /* Check the PKA handle allocation */
  291. if (hpka != NULL)
  292. {
  293. /* Check the parameters */
  294. assert_param(IS_PKA_ALL_INSTANCE(hpka->Instance));
  295. if (hpka->State == HAL_PKA_STATE_RESET)
  296. {
  297. #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
  298. /* Init the PKA Callback settings */
  299. hpka->OperationCpltCallback = HAL_PKA_OperationCpltCallback; /* Legacy weak OperationCpltCallback */
  300. hpka->ErrorCallback = HAL_PKA_ErrorCallback; /* Legacy weak ErrorCallback */
  301. if (hpka->MspInitCallback == NULL)
  302. {
  303. hpka->MspInitCallback = HAL_PKA_MspInit; /* Legacy weak MspInit */
  304. }
  305. /* Init the low level hardware */
  306. hpka->MspInitCallback(hpka);
  307. #else
  308. /* Init the low level hardware */
  309. HAL_PKA_MspInit(hpka);
  310. #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
  311. }
  312. /* Set the state to busy */
  313. hpka->State = HAL_PKA_STATE_BUSY;
  314. /* Get current tick */
  315. tickstart = HAL_GetTick();
  316. /* Reset the control register and enable the PKA (wait the end of PKA RAM erase) */
  317. while ((hpka->Instance->CR & PKA_CR_EN) != PKA_CR_EN)
  318. {
  319. hpka->Instance->CR = PKA_CR_EN;
  320. /* Check the Timeout */
  321. if ((HAL_GetTick() - tickstart) > PKA_RAM_ERASE_TIMEOUT)
  322. {
  323. /* Set timeout status */
  324. err = HAL_TIMEOUT;
  325. break;
  326. }
  327. }
  328. if (err == HAL_OK)
  329. {
  330. /* Reset any pending flag */
  331. SET_BIT(hpka->Instance->CLRFR, PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
  332. /* Initialize the error code */
  333. hpka->ErrorCode = HAL_PKA_ERROR_NONE;
  334. /* Set the state to ready */
  335. hpka->State = HAL_PKA_STATE_READY;
  336. }
  337. else
  338. {
  339. /* Set the error code to timeout error */
  340. hpka->ErrorCode = HAL_PKA_ERROR_TIMEOUT;
  341. /* Set the state to error */
  342. hpka->State = HAL_PKA_STATE_ERROR;
  343. }
  344. }
  345. else
  346. {
  347. err = HAL_ERROR;
  348. }
  349. return err;
  350. }
  351. /**
  352. * @brief DeInitialize the PKA peripheral.
  353. * @param hpka PKA handle
  354. * @retval HAL status
  355. */
  356. HAL_StatusTypeDef HAL_PKA_DeInit(PKA_HandleTypeDef *hpka)
  357. {
  358. HAL_StatusTypeDef err = HAL_OK;
  359. /* Check the PKA handle allocation */
  360. if (hpka != NULL)
  361. {
  362. /* Check the parameters */
  363. assert_param(IS_PKA_ALL_INSTANCE(hpka->Instance));
  364. /* Set the state to busy */
  365. hpka->State = HAL_PKA_STATE_BUSY;
  366. /* Reset the control register */
  367. /* This abort any operation in progress (PKA RAM content is not guaranteed in this case) */
  368. hpka->Instance->CR = 0;
  369. /* Reset any pending flag */
  370. SET_BIT(hpka->Instance->CLRFR, PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
  371. #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
  372. if (hpka->MspDeInitCallback == NULL)
  373. {
  374. hpka->MspDeInitCallback = HAL_PKA_MspDeInit; /* Legacy weak MspDeInit */
  375. }
  376. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  377. hpka->MspDeInitCallback(hpka);
  378. #else
  379. /* DeInit the low level hardware: CLOCK, NVIC */
  380. HAL_PKA_MspDeInit(hpka);
  381. #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
  382. /* Reset the error code */
  383. hpka->ErrorCode = HAL_PKA_ERROR_NONE;
  384. /* Reset the state */
  385. hpka->State = HAL_PKA_STATE_RESET;
  386. }
  387. else
  388. {
  389. err = HAL_ERROR;
  390. }
  391. return err;
  392. }
  393. /**
  394. * @brief Initialize the PKA MSP.
  395. * @param hpka PKA handle
  396. * @retval None
  397. */
  398. __weak void HAL_PKA_MspInit(PKA_HandleTypeDef *hpka)
  399. {
  400. /* Prevent unused argument(s) compilation warning */
  401. UNUSED(hpka);
  402. /* NOTE : This function should not be modified, when the callback is needed,
  403. the HAL_PKA_MspInit can be implemented in the user file
  404. */
  405. }
  406. /**
  407. * @brief DeInitialize the PKA MSP.
  408. * @param hpka PKA handle
  409. * @retval None
  410. */
  411. __weak void HAL_PKA_MspDeInit(PKA_HandleTypeDef *hpka)
  412. {
  413. /* Prevent unused argument(s) compilation warning */
  414. UNUSED(hpka);
  415. /* NOTE : This function should not be modified, when the callback is needed,
  416. the HAL_PKA_MspDeInit can be implemented in the user file
  417. */
  418. }
  419. #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
  420. /**
  421. * @brief Register a User PKA Callback
  422. * To be used instead of the weak predefined callback
  423. * @param hpka Pointer to a PKA_HandleTypeDef structure that contains
  424. * the configuration information for the specified PKA.
  425. * @param CallbackID ID of the callback to be registered
  426. * This parameter can be one of the following values:
  427. * @arg @ref HAL_PKA_OPERATION_COMPLETE_CB_ID End of operation callback ID
  428. * @arg @ref HAL_PKA_ERROR_CB_ID Error callback ID
  429. * @arg @ref HAL_PKA_MSPINIT_CB_ID MspInit callback ID
  430. * @arg @ref HAL_PKA_MSPDEINIT_CB_ID MspDeInit callback ID
  431. * @param pCallback pointer to the Callback function
  432. * @retval HAL status
  433. */
  434. HAL_StatusTypeDef HAL_PKA_RegisterCallback(PKA_HandleTypeDef *hpka, HAL_PKA_CallbackIDTypeDef CallbackID,
  435. pPKA_CallbackTypeDef pCallback)
  436. {
  437. HAL_StatusTypeDef status = HAL_OK;
  438. if (pCallback == NULL)
  439. {
  440. /* Update the error code */
  441. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  442. return HAL_ERROR;
  443. }
  444. if (HAL_PKA_STATE_READY == hpka->State)
  445. {
  446. switch (CallbackID)
  447. {
  448. case HAL_PKA_OPERATION_COMPLETE_CB_ID :
  449. hpka->OperationCpltCallback = pCallback;
  450. break;
  451. case HAL_PKA_ERROR_CB_ID :
  452. hpka->ErrorCallback = pCallback;
  453. break;
  454. case HAL_PKA_MSPINIT_CB_ID :
  455. hpka->MspInitCallback = pCallback;
  456. break;
  457. case HAL_PKA_MSPDEINIT_CB_ID :
  458. hpka->MspDeInitCallback = pCallback;
  459. break;
  460. default :
  461. /* Update the error code */
  462. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  463. /* Return error status */
  464. status = HAL_ERROR;
  465. break;
  466. }
  467. }
  468. else if (HAL_PKA_STATE_RESET == hpka->State)
  469. {
  470. switch (CallbackID)
  471. {
  472. case HAL_PKA_MSPINIT_CB_ID :
  473. hpka->MspInitCallback = pCallback;
  474. break;
  475. case HAL_PKA_MSPDEINIT_CB_ID :
  476. hpka->MspDeInitCallback = pCallback;
  477. break;
  478. default :
  479. /* Update the error code */
  480. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  481. /* Return error status */
  482. status = HAL_ERROR;
  483. break;
  484. }
  485. }
  486. else
  487. {
  488. /* Update the error code */
  489. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  490. /* Return error status */
  491. status = HAL_ERROR;
  492. }
  493. return status;
  494. }
  495. /**
  496. * @brief Unregister a PKA Callback
  497. * PKA callback is redirected to the weak predefined callback
  498. * @param hpka Pointer to a PKA_HandleTypeDef structure that contains
  499. * the configuration information for the specified PKA.
  500. * @param CallbackID ID of the callback to be unregistered
  501. * This parameter can be one of the following values:
  502. * @arg @ref HAL_PKA_OPERATION_COMPLETE_CB_ID End of operation callback ID
  503. * @arg @ref HAL_PKA_ERROR_CB_ID Error callback ID
  504. * @arg @ref HAL_PKA_MSPINIT_CB_ID MspInit callback ID
  505. * @arg @ref HAL_PKA_MSPDEINIT_CB_ID MspDeInit callback ID
  506. * @retval HAL status
  507. */
  508. HAL_StatusTypeDef HAL_PKA_UnRegisterCallback(PKA_HandleTypeDef *hpka, HAL_PKA_CallbackIDTypeDef CallbackID)
  509. {
  510. HAL_StatusTypeDef status = HAL_OK;
  511. if (HAL_PKA_STATE_READY == hpka->State)
  512. {
  513. switch (CallbackID)
  514. {
  515. case HAL_PKA_OPERATION_COMPLETE_CB_ID :
  516. hpka->OperationCpltCallback = HAL_PKA_OperationCpltCallback; /* Legacy weak OperationCpltCallback */
  517. break;
  518. case HAL_PKA_ERROR_CB_ID :
  519. hpka->ErrorCallback = HAL_PKA_ErrorCallback; /* Legacy weak ErrorCallback */
  520. break;
  521. case HAL_PKA_MSPINIT_CB_ID :
  522. hpka->MspInitCallback = HAL_PKA_MspInit; /* Legacy weak MspInit */
  523. break;
  524. case HAL_PKA_MSPDEINIT_CB_ID :
  525. hpka->MspDeInitCallback = HAL_PKA_MspDeInit; /* Legacy weak MspDeInit */
  526. break;
  527. default :
  528. /* Update the error code */
  529. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  530. /* Return error status */
  531. status = HAL_ERROR;
  532. break;
  533. }
  534. }
  535. else if (HAL_PKA_STATE_RESET == hpka->State)
  536. {
  537. switch (CallbackID)
  538. {
  539. case HAL_PKA_MSPINIT_CB_ID :
  540. hpka->MspInitCallback = HAL_PKA_MspInit; /* Legacy weak MspInit */
  541. break;
  542. case HAL_PKA_MSPDEINIT_CB_ID :
  543. hpka->MspDeInitCallback = HAL_PKA_MspDeInit; /* Legacy weak MspDeInit */
  544. break;
  545. default :
  546. /* Update the error code */
  547. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  548. /* Return error status */
  549. status = HAL_ERROR;
  550. break;
  551. }
  552. }
  553. else
  554. {
  555. /* Update the error code */
  556. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  557. /* Return error status */
  558. status = HAL_ERROR;
  559. }
  560. return status;
  561. }
  562. #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
  563. /**
  564. * @}
  565. */
  566. /** @defgroup PKA_Exported_Functions_Group2 IO operation functions
  567. * @brief IO operation functions
  568. *
  569. @verbatim
  570. ===============================================================================
  571. ##### IO operation functions #####
  572. ===============================================================================
  573. [..]
  574. This subsection provides a set of functions allowing to manage the PKA operations.
  575. (#) There are two modes of operation:
  576. (++) Blocking mode : The operation is performed in the polling mode.
  577. These functions return when data operation is completed.
  578. (++) No-Blocking mode : The operation is performed using Interrupts.
  579. These functions return immediately.
  580. The end of the operation is indicated by HAL_PKA_ErrorCallback in case of error.
  581. The end of the operation is indicated by HAL_PKA_OperationCpltCallback in case of success.
  582. To stop any operation in interrupt mode, use HAL_PKA_Abort().
  583. (#) Blocking mode functions are :
  584. (++) HAL_PKA_ModExp()
  585. (++) HAL_PKA_ModExpFastMode()
  586. (++) HAL_PKA_ModExp_GetResult();
  587. (++) HAL_PKA_ECDSASign()
  588. (++) HAL_PKA_ECDSASign_GetResult();
  589. (++) HAL_PKA_ECDSAVerif()
  590. (++) HAL_PKA_ECDSAVerif_IsValidSignature();
  591. (++) HAL_PKA_RSACRTExp()
  592. (++) HAL_PKA_RSACRTExp_GetResult();
  593. (++) HAL_PKA_PointCheck()
  594. (++) HAL_PKA_PointCheck_IsOnCurve();
  595. (++) HAL_PKA_ECCMul()
  596. (++) HAL_PKA_ECCMulFastMode()
  597. (++) HAL_PKA_ECCMul_GetResult();
  598. (++) HAL_PKA_Add()
  599. (++) HAL_PKA_Sub()
  600. (++) HAL_PKA_Cmp()
  601. (++) HAL_PKA_Mul()
  602. (++) HAL_PKA_ModAdd()
  603. (++) HAL_PKA_ModSub()
  604. (++) HAL_PKA_ModInv()
  605. (++) HAL_PKA_ModRed()
  606. (++) HAL_PKA_MontgomeryMul()
  607. (++) HAL_PKA_Arithmetic_GetResult(P);
  608. (++) HAL_PKA_MontgomeryParam()
  609. (++) HAL_PKA_MontgomeryParam_GetResult();
  610. (#) No-Blocking mode functions with Interrupt are :
  611. (++) HAL_PKA_ModExp_IT();
  612. (++) HAL_PKA_ModExpFastMode_IT();
  613. (++) HAL_PKA_ModExp_GetResult();
  614. (++) HAL_PKA_ECDSASign_IT();
  615. (++) HAL_PKA_ECDSASign_GetResult();
  616. (++) HAL_PKA_ECDSAVerif_IT();
  617. (++) HAL_PKA_ECDSAVerif_IsValidSignature();
  618. (++) HAL_PKA_RSACRTExp_IT();
  619. (++) HAL_PKA_RSACRTExp_GetResult();
  620. (++) HAL_PKA_PointCheck_IT();
  621. (++) HAL_PKA_PointCheck_IsOnCurve();
  622. (++) HAL_PKA_ECCMul_IT();
  623. (++) HAL_PKA_ECCMulFastMode_IT();
  624. (++) HAL_PKA_ECCMul_GetResult();
  625. (++) HAL_PKA_Add_IT();
  626. (++) HAL_PKA_Sub_IT();
  627. (++) HAL_PKA_Cmp_IT();
  628. (++) HAL_PKA_Mul_IT();
  629. (++) HAL_PKA_ModAdd_IT();
  630. (++) HAL_PKA_ModSub_IT();
  631. (++) HAL_PKA_ModInv_IT();
  632. (++) HAL_PKA_ModRed_IT();
  633. (++) HAL_PKA_MontgomeryMul_IT();
  634. (++) HAL_PKA_Arithmetic_GetResult();
  635. (++) HAL_PKA_MontgomeryParam_IT();
  636. (++) HAL_PKA_MontgomeryParam_GetResult();
  637. (++) HAL_PKA_Abort();
  638. @endverbatim
  639. * @{
  640. */
  641. /**
  642. * @brief Modular exponentiation in blocking mode.
  643. * @param hpka PKA handle
  644. * @param in Input information
  645. * @param Timeout Timeout duration
  646. * @retval HAL status
  647. */
  648. HAL_StatusTypeDef HAL_PKA_ModExp(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in, uint32_t Timeout)
  649. {
  650. /* Set input parameter in PKA RAM */
  651. PKA_ModExp_Set(hpka, in);
  652. /* Start the operation */
  653. return PKA_Process(hpka, PKA_MODE_MODULAR_EXP, Timeout);
  654. }
  655. /**
  656. * @brief Modular exponentiation in non-blocking mode with Interrupt.
  657. * @param hpka PKA handle
  658. * @param in Input information
  659. * @retval HAL status
  660. */
  661. HAL_StatusTypeDef HAL_PKA_ModExp_IT(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in)
  662. {
  663. /* Set input parameter in PKA RAM */
  664. PKA_ModExp_Set(hpka, in);
  665. /* Start the operation */
  666. return PKA_Process_IT(hpka, PKA_MODE_MODULAR_EXP);
  667. }
  668. /**
  669. * @brief Modular exponentiation in blocking mode.
  670. * @param hpka PKA handle
  671. * @param in Input information
  672. * @param Timeout Timeout duration
  673. * @retval HAL status
  674. */
  675. HAL_StatusTypeDef HAL_PKA_ModExpFastMode(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in, uint32_t Timeout)
  676. {
  677. /* Set input parameter in PKA RAM */
  678. PKA_ModExpFastMode_Set(hpka, in);
  679. /* Start the operation */
  680. return PKA_Process(hpka, PKA_MODE_MODULAR_EXP_FAST_MODE, Timeout);
  681. }
  682. /**
  683. * @brief Modular exponentiation in non-blocking mode with Interrupt.
  684. * @param hpka PKA handle
  685. * @param in Input information
  686. * @retval HAL status
  687. */
  688. HAL_StatusTypeDef HAL_PKA_ModExpFastMode_IT(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in)
  689. {
  690. /* Set input parameter in PKA RAM */
  691. PKA_ModExpFastMode_Set(hpka, in);
  692. /* Start the operation */
  693. return PKA_Process_IT(hpka, PKA_MODE_MODULAR_EXP_FAST_MODE);
  694. }
  695. /**
  696. * @brief Retrieve operation result.
  697. * @param hpka PKA handle
  698. * @param pRes Output buffer
  699. * @retval HAL status
  700. */
  701. void HAL_PKA_ModExp_GetResult(PKA_HandleTypeDef *hpka, uint8_t *pRes)
  702. {
  703. uint32_t size;
  704. /* Indicate to the user the final size */
  705. size = (hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] + 7UL) / 8UL;
  706. /* Move the result to appropriate location (indicated in out parameter) */
  707. PKA_Memcpy_u32_to_u8(pRes, &hpka->Instance->RAM[PKA_MODULAR_EXP_OUT_SM_ALGO_ACC1], size);
  708. }
  709. /**
  710. * @brief Sign a message using elliptic curves over prime fields in blocking mode.
  711. * @param hpka PKA handle
  712. * @param in Input information
  713. * @param Timeout Timeout duration
  714. * @retval HAL status
  715. */
  716. HAL_StatusTypeDef HAL_PKA_ECDSASign(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in, uint32_t Timeout)
  717. {
  718. /* Set input parameter in PKA RAM */
  719. PKA_ECDSASign_Set(hpka, in);
  720. /* Start the operation */
  721. return PKA_Process(hpka, PKA_MODE_ECDSA_SIGNATURE, Timeout);
  722. }
  723. /**
  724. * @brief Sign a message using elliptic curves over prime fields in non-blocking mode with Interrupt.
  725. * @param hpka PKA handle
  726. * @param in Input information
  727. * @retval HAL status
  728. */
  729. HAL_StatusTypeDef HAL_PKA_ECDSASign_IT(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in)
  730. {
  731. /* Set input parameter in PKA RAM */
  732. PKA_ECDSASign_Set(hpka, in);
  733. /* Start the operation */
  734. return PKA_Process_IT(hpka, PKA_MODE_ECDSA_SIGNATURE);
  735. }
  736. /**
  737. * @brief Retrieve operation result.
  738. * @param hpka PKA handle
  739. * @param out Output information
  740. * @param outExt Additional Output information (facultative)
  741. */
  742. void HAL_PKA_ECDSASign_GetResult(PKA_HandleTypeDef *hpka, PKA_ECDSASignOutTypeDef *out,
  743. PKA_ECDSASignOutExtParamTypeDef *outExt)
  744. {
  745. uint32_t size;
  746. size = (hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] + 7UL) / 8UL;
  747. if (out != NULL)
  748. {
  749. PKA_Memcpy_u32_to_u8(out->RSign, &hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_SIGNATURE_R], size);
  750. PKA_Memcpy_u32_to_u8(out->SSign, &hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_SIGNATURE_S], size);
  751. }
  752. /* If user requires the additional information */
  753. if (outExt != NULL)
  754. {
  755. /* Move the result to appropriate location (indicated in outExt parameter) */
  756. PKA_Memcpy_u32_to_u8(outExt->ptX, &hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_FINAL_POINT_X], size);
  757. PKA_Memcpy_u32_to_u8(outExt->ptY, &hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_FINAL_POINT_Y], size);
  758. }
  759. }
  760. /**
  761. * @brief Verify the validity of a signature using elliptic curves over prime fields in blocking mode.
  762. * @param hpka PKA handle
  763. * @param in Input information
  764. * @param Timeout Timeout duration
  765. * @retval HAL status
  766. */
  767. HAL_StatusTypeDef HAL_PKA_ECDSAVerif(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in, uint32_t Timeout)
  768. {
  769. /* Set input parameter in PKA RAM */
  770. PKA_ECDSAVerif_Set(hpka, in);
  771. /* Start the operation */
  772. return PKA_Process(hpka, PKA_MODE_ECDSA_VERIFICATION, Timeout);
  773. }
  774. /**
  775. * @brief Verify the validity of a signature using elliptic curves
  776. * over prime fields in non-blocking mode with Interrupt.
  777. * @param hpka PKA handle
  778. * @param in Input information
  779. * @retval HAL status
  780. */
  781. HAL_StatusTypeDef HAL_PKA_ECDSAVerif_IT(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in)
  782. {
  783. /* Set input parameter in PKA RAM */
  784. PKA_ECDSAVerif_Set(hpka, in);
  785. /* Start the operation */
  786. return PKA_Process_IT(hpka, PKA_MODE_ECDSA_VERIFICATION);
  787. }
  788. /**
  789. * @brief Return the result of the ECDSA verification operation.
  790. * @param hpka PKA handle
  791. * @retval 1 if signature is verified, 0 in other case
  792. */
  793. uint32_t HAL_PKA_ECDSAVerif_IsValidSignature(PKA_HandleTypeDef const *const hpka)
  794. {
  795. /* Invert the state of the PKA RAM bit containing the result of the operation */
  796. return (hpka->Instance->RAM[PKA_ECDSA_VERIF_OUT_RESULT] == 0UL) ? 1UL : 0UL;
  797. }
  798. /**
  799. * @brief RSA CRT exponentiation in blocking mode.
  800. * @param hpka PKA handle
  801. * @param in Input information
  802. * @param Timeout Timeout duration
  803. * @retval HAL status
  804. */
  805. HAL_StatusTypeDef HAL_PKA_RSACRTExp(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in, uint32_t Timeout)
  806. {
  807. /* Set input parameter in PKA RAM */
  808. PKA_RSACRTExp_Set(hpka, in);
  809. /* Start the operation */
  810. return PKA_Process(hpka, PKA_MODE_RSA_CRT_EXP, Timeout);
  811. }
  812. /**
  813. * @brief RSA CRT exponentiation in non-blocking mode with Interrupt.
  814. * @param hpka PKA handle
  815. * @param in Input information
  816. * @retval HAL status
  817. */
  818. HAL_StatusTypeDef HAL_PKA_RSACRTExp_IT(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in)
  819. {
  820. /* Set input parameter in PKA RAM */
  821. PKA_RSACRTExp_Set(hpka, in);
  822. /* Start the operation */
  823. return PKA_Process_IT(hpka, PKA_MODE_RSA_CRT_EXP);
  824. }
  825. /**
  826. * @brief Retrieve operation result.
  827. * @param hpka PKA handle
  828. * @param pRes Pointer to memory location to receive the result of the operation
  829. * @retval HAL status
  830. */
  831. void HAL_PKA_RSACRTExp_GetResult(PKA_HandleTypeDef *hpka, uint8_t *pRes)
  832. {
  833. uint32_t size;
  834. /* Move the result to appropriate location (indicated in out parameter) */
  835. size = (hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] + 7UL) / 8UL;
  836. PKA_Memcpy_u32_to_u8(pRes, &hpka->Instance->RAM[PKA_RSA_CRT_EXP_OUT_RESULT], size);
  837. }
  838. /**
  839. * @brief Point on elliptic curve check in blocking mode.
  840. * @param hpka PKA handle
  841. * @param in Input information
  842. * @param Timeout Timeout duration
  843. * @retval HAL status
  844. */
  845. HAL_StatusTypeDef HAL_PKA_PointCheck(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in, uint32_t Timeout)
  846. {
  847. /* Set input parameter in PKA RAM */
  848. PKA_PointCheck_Set(hpka, in);
  849. /* Start the operation */
  850. return PKA_Process(hpka, PKA_MODE_POINT_CHECK, Timeout);
  851. }
  852. /**
  853. * @brief Point on elliptic curve check in non-blocking mode with Interrupt.
  854. * @param hpka PKA handle
  855. * @param in Input information
  856. * @retval HAL status
  857. */
  858. HAL_StatusTypeDef HAL_PKA_PointCheck_IT(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in)
  859. {
  860. /* Set input parameter in PKA RAM */
  861. PKA_PointCheck_Set(hpka, in);
  862. /* Start the operation */
  863. return PKA_Process_IT(hpka, PKA_MODE_POINT_CHECK);
  864. }
  865. /**
  866. * @brief Return the result of the point check operation.
  867. * @param hpka PKA handle
  868. * @retval 1 if point is on curve, 0 in other case
  869. */
  870. uint32_t HAL_PKA_PointCheck_IsOnCurve(PKA_HandleTypeDef const *const hpka)
  871. {
  872. #define PKA_POINT_IS_ON_CURVE 0UL
  873. /* Invert the value of the PKA RAM containing the result of the operation */
  874. return (hpka->Instance->RAM[PKA_POINT_CHECK_OUT_ERROR] == PKA_POINT_IS_ON_CURVE) ? 1UL : 0UL;
  875. }
  876. /**
  877. * @brief ECC scalar multiplication in blocking mode.
  878. * @param hpka PKA handle
  879. * @param in Input information
  880. * @param Timeout Timeout duration
  881. * @retval HAL status
  882. */
  883. HAL_StatusTypeDef HAL_PKA_ECCMul(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in, uint32_t Timeout)
  884. {
  885. /* Set input parameter in PKA RAM */
  886. PKA_ECCMul_Set(hpka, in);
  887. /* Start the operation */
  888. return PKA_Process(hpka, PKA_MODE_ECC_MUL, Timeout);
  889. }
  890. /**
  891. * @brief ECC scalar multiplication in non-blocking mode with Interrupt.
  892. * @param hpka PKA handle
  893. * @param in Input information
  894. * @retval HAL status
  895. */
  896. HAL_StatusTypeDef HAL_PKA_ECCMul_IT(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in)
  897. {
  898. /* Set input parameter in PKA RAM */
  899. PKA_ECCMul_Set(hpka, in);
  900. /* Start the operation */
  901. return PKA_Process_IT(hpka, PKA_MODE_ECC_MUL);
  902. }
  903. /**
  904. * @brief ECC scalar multiplication in blocking mode.
  905. * @param hpka PKA handle
  906. * @param in Input information
  907. * @param Timeout Timeout duration
  908. * @retval HAL status
  909. */
  910. HAL_StatusTypeDef HAL_PKA_ECCMulFastMode(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in, uint32_t Timeout)
  911. {
  912. /* Set input parameter in PKA RAM */
  913. PKA_ECCMulFastMode_Set(hpka, in);
  914. /* Start the operation */
  915. return PKA_Process(hpka, PKA_MODE_ECC_MUL_FAST_MODE, Timeout);
  916. }
  917. /**
  918. * @brief ECC scalar multiplication in non-blocking mode with Interrupt.
  919. * @param hpka PKA handle
  920. * @param in Input information
  921. * @retval HAL status
  922. */
  923. HAL_StatusTypeDef HAL_PKA_ECCMulFastMode_IT(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in)
  924. {
  925. /* Set input parameter in PKA RAM */
  926. PKA_ECCMulFastMode_Set(hpka, in);
  927. /* Start the operation */
  928. return PKA_Process_IT(hpka, PKA_MODE_ECC_MUL_FAST_MODE);
  929. }
  930. /**
  931. * @brief Retrieve operation result.
  932. * @param hpka PKA handle
  933. * @param out Output information
  934. * @retval HAL status
  935. */
  936. void HAL_PKA_ECCMul_GetResult(PKA_HandleTypeDef *hpka, PKA_ECCMulOutTypeDef *out)
  937. {
  938. uint32_t size;
  939. /* Retrieve the size of the array from the PKA RAM */
  940. size = (hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] + 7UL) / 8UL;
  941. /* If a destination buffer is provided */
  942. if (out != NULL)
  943. {
  944. /* Move the result to appropriate location (indicated in out parameter) */
  945. PKA_Memcpy_u32_to_u8(out->ptX, &hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_OUT_RESULT_X], size);
  946. PKA_Memcpy_u32_to_u8(out->ptY, &hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_OUT_RESULT_Y], size);
  947. }
  948. }
  949. /**
  950. * @brief Arithmetic addition in blocking mode.
  951. * @param hpka PKA handle
  952. * @param in Input information
  953. * @param Timeout Timeout duration
  954. * @retval HAL status
  955. */
  956. HAL_StatusTypeDef HAL_PKA_Add(PKA_HandleTypeDef *hpka, PKA_AddInTypeDef *in, uint32_t Timeout)
  957. {
  958. /* Set input parameter in PKA RAM */
  959. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  960. /* Start the operation */
  961. return PKA_Process(hpka, PKA_MODE_ARITHMETIC_ADD, Timeout);
  962. }
  963. /**
  964. * @brief Arithmetic addition in non-blocking mode with Interrupt.
  965. * @param hpka PKA handle
  966. * @param in Input information
  967. * @retval HAL status
  968. */
  969. HAL_StatusTypeDef HAL_PKA_Add_IT(PKA_HandleTypeDef *hpka, PKA_AddInTypeDef *in)
  970. {
  971. /* Set input parameter in PKA RAM */
  972. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  973. /* Start the operation */
  974. return PKA_Process_IT(hpka, PKA_MODE_ARITHMETIC_ADD);
  975. }
  976. /**
  977. * @brief Arithmetic subtraction in blocking mode.
  978. * @param hpka PKA handle
  979. * @param in Input information
  980. * @param Timeout Timeout duration
  981. * @retval HAL status
  982. */
  983. HAL_StatusTypeDef HAL_PKA_Sub(PKA_HandleTypeDef *hpka, PKA_SubInTypeDef *in, uint32_t Timeout)
  984. {
  985. /* Set input parameter in PKA RAM */
  986. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  987. /* Start the operation */
  988. return PKA_Process(hpka, PKA_MODE_ARITHMETIC_SUB, Timeout);
  989. }
  990. /**
  991. * @brief Arithmetic subtraction in non-blocking mode with Interrupt.
  992. * @param hpka PKA handle
  993. * @param in Input information
  994. * @retval HAL status
  995. */
  996. HAL_StatusTypeDef HAL_PKA_Sub_IT(PKA_HandleTypeDef *hpka, PKA_SubInTypeDef *in)
  997. {
  998. /* Set input parameter in PKA RAM */
  999. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  1000. /* Start the operation */
  1001. return PKA_Process_IT(hpka, PKA_MODE_ARITHMETIC_SUB);
  1002. }
  1003. /**
  1004. * @brief Arithmetic multiplication in blocking mode.
  1005. * @param hpka PKA handle
  1006. * @param in Input information
  1007. * @param Timeout Timeout duration
  1008. * @retval HAL status
  1009. */
  1010. HAL_StatusTypeDef HAL_PKA_Mul(PKA_HandleTypeDef *hpka, PKA_MulInTypeDef *in, uint32_t Timeout)
  1011. {
  1012. /* Set input parameter in PKA RAM */
  1013. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  1014. /* Start the operation */
  1015. return PKA_Process(hpka, PKA_MODE_ARITHMETIC_MUL, Timeout);
  1016. }
  1017. /**
  1018. * @brief Arithmetic multiplication in non-blocking mode with Interrupt.
  1019. * @param hpka PKA handle
  1020. * @param in Input information
  1021. * @retval HAL status
  1022. */
  1023. HAL_StatusTypeDef HAL_PKA_Mul_IT(PKA_HandleTypeDef *hpka, PKA_MulInTypeDef *in)
  1024. {
  1025. /* Set input parameter in PKA RAM */
  1026. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  1027. /* Start the operation */
  1028. return PKA_Process_IT(hpka, PKA_MODE_ARITHMETIC_MUL);
  1029. }
  1030. /**
  1031. * @brief Comparison in blocking mode.
  1032. * @param hpka PKA handle
  1033. * @param in Input information
  1034. * @param Timeout Timeout duration
  1035. * @retval HAL status
  1036. */
  1037. HAL_StatusTypeDef HAL_PKA_Cmp(PKA_HandleTypeDef *hpka, PKA_CmpInTypeDef *in, uint32_t Timeout)
  1038. {
  1039. /* Set input parameter in PKA RAM */
  1040. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  1041. /* Start the operation */
  1042. return PKA_Process(hpka, PKA_MODE_COMPARISON, Timeout);
  1043. }
  1044. /**
  1045. * @brief Comparison in non-blocking mode with Interrupt.
  1046. * @param hpka PKA handle
  1047. * @param in Input information
  1048. * @retval HAL status
  1049. */
  1050. HAL_StatusTypeDef HAL_PKA_Cmp_IT(PKA_HandleTypeDef *hpka, PKA_CmpInTypeDef *in)
  1051. {
  1052. /* Set input parameter in PKA RAM */
  1053. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  1054. /* Start the operation */
  1055. return PKA_Process_IT(hpka, PKA_MODE_COMPARISON);
  1056. }
  1057. /**
  1058. * @brief Modular addition in blocking mode.
  1059. * @param hpka PKA handle
  1060. * @param in Input information
  1061. * @param Timeout Timeout duration
  1062. * @retval HAL status
  1063. */
  1064. HAL_StatusTypeDef HAL_PKA_ModAdd(PKA_HandleTypeDef *hpka, PKA_ModAddInTypeDef *in, uint32_t Timeout)
  1065. {
  1066. /* Set input parameter in PKA RAM */
  1067. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
  1068. /* Start the operation */
  1069. return PKA_Process(hpka, PKA_MODE_MODULAR_ADD, Timeout);
  1070. }
  1071. /**
  1072. * @brief Modular addition in non-blocking mode with Interrupt.
  1073. * @param hpka PKA handle
  1074. * @param in Input information
  1075. * @retval HAL status
  1076. */
  1077. HAL_StatusTypeDef HAL_PKA_ModAdd_IT(PKA_HandleTypeDef *hpka, PKA_ModAddInTypeDef *in)
  1078. {
  1079. /* Set input parameter in PKA RAM */
  1080. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
  1081. /* Start the operation */
  1082. return PKA_Process_IT(hpka, PKA_MODE_MODULAR_ADD);
  1083. }
  1084. /**
  1085. * @brief Modular inversion in blocking mode.
  1086. * @param hpka PKA handle
  1087. * @param in Input information
  1088. * @param Timeout Timeout duration
  1089. * @retval HAL status
  1090. */
  1091. HAL_StatusTypeDef HAL_PKA_ModInv(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in, uint32_t Timeout)
  1092. {
  1093. /* Set input parameter in PKA RAM */
  1094. PKA_ModInv_Set(hpka, in);
  1095. /* Start the operation */
  1096. return PKA_Process(hpka, PKA_MODE_MODULAR_INV, Timeout);
  1097. }
  1098. /**
  1099. * @brief Modular inversion in non-blocking mode with Interrupt.
  1100. * @param hpka PKA handle
  1101. * @param in Input information
  1102. * @retval HAL status
  1103. */
  1104. HAL_StatusTypeDef HAL_PKA_ModInv_IT(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in)
  1105. {
  1106. /* Set input parameter in PKA RAM */
  1107. PKA_ModInv_Set(hpka, in);
  1108. /* Start the operation */
  1109. return PKA_Process_IT(hpka, PKA_MODE_MODULAR_INV);
  1110. }
  1111. /**
  1112. * @brief Modular subtraction in blocking mode.
  1113. * @param hpka PKA handle
  1114. * @param in Input information
  1115. * @param Timeout Timeout duration
  1116. * @retval HAL status
  1117. */
  1118. HAL_StatusTypeDef HAL_PKA_ModSub(PKA_HandleTypeDef *hpka, PKA_ModSubInTypeDef *in, uint32_t Timeout)
  1119. {
  1120. /* Set input parameter in PKA RAM */
  1121. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
  1122. /* Start the operation */
  1123. return PKA_Process(hpka, PKA_MODE_MODULAR_SUB, Timeout);
  1124. }
  1125. /**
  1126. * @brief Modular subtraction in non-blocking mode with Interrupt.
  1127. * @param hpka PKA handle
  1128. * @param in Input information
  1129. * @retval HAL status
  1130. */
  1131. HAL_StatusTypeDef HAL_PKA_ModSub_IT(PKA_HandleTypeDef *hpka, PKA_ModSubInTypeDef *in)
  1132. {
  1133. /* Set input parameter in PKA RAM */
  1134. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
  1135. /* Start the operation */
  1136. return PKA_Process_IT(hpka, PKA_MODE_MODULAR_SUB);
  1137. }
  1138. /**
  1139. * @brief Modular reduction in blocking mode.
  1140. * @param hpka PKA handle
  1141. * @param in Input information
  1142. * @param Timeout Timeout duration
  1143. * @retval HAL status
  1144. */
  1145. HAL_StatusTypeDef HAL_PKA_ModRed(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in, uint32_t Timeout)
  1146. {
  1147. /* Set input parameter in PKA RAM */
  1148. PKA_ModRed_Set(hpka, in);
  1149. /* Start the operation */
  1150. return PKA_Process(hpka, PKA_MODE_MODULAR_RED, Timeout);
  1151. }
  1152. /**
  1153. * @brief Modular reduction in non-blocking mode with Interrupt.
  1154. * @param hpka PKA handle
  1155. * @param in Input information
  1156. * @retval HAL status
  1157. */
  1158. HAL_StatusTypeDef HAL_PKA_ModRed_IT(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in)
  1159. {
  1160. /* Set input parameter in PKA RAM */
  1161. PKA_ModRed_Set(hpka, in);
  1162. /* Start the operation */
  1163. return PKA_Process_IT(hpka, PKA_MODE_MODULAR_RED);
  1164. }
  1165. /**
  1166. * @brief Montgomery multiplication in blocking mode.
  1167. * @param hpka PKA handle
  1168. * @param in Input information
  1169. * @param Timeout Timeout duration
  1170. * @retval HAL status
  1171. */
  1172. HAL_StatusTypeDef HAL_PKA_MontgomeryMul(PKA_HandleTypeDef *hpka, PKA_MontgomeryMulInTypeDef *in, uint32_t Timeout)
  1173. {
  1174. /* Set input parameter in PKA RAM */
  1175. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
  1176. /* Start the operation */
  1177. return PKA_Process(hpka, PKA_MODE_MONTGOMERY_MUL, Timeout);
  1178. }
  1179. /**
  1180. * @brief Montgomery multiplication in non-blocking mode with Interrupt.
  1181. * @param hpka PKA handle
  1182. * @param in Input information
  1183. * @retval HAL status
  1184. */
  1185. HAL_StatusTypeDef HAL_PKA_MontgomeryMul_IT(PKA_HandleTypeDef *hpka, PKA_MontgomeryMulInTypeDef *in)
  1186. {
  1187. /* Set input parameter in PKA RAM */
  1188. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
  1189. /* Start the operation */
  1190. return PKA_Process_IT(hpka, PKA_MODE_MONTGOMERY_MUL);
  1191. }
  1192. /**
  1193. * @brief Retrieve operation result.
  1194. * @param hpka PKA handle
  1195. * @param pRes Pointer to memory location to receive the result of the operation
  1196. */
  1197. void HAL_PKA_Arithmetic_GetResult(PKA_HandleTypeDef *hpka, uint32_t *pRes)
  1198. {
  1199. uint32_t mode = (hpka->Instance->CR & PKA_CR_MODE_Msk) >> PKA_CR_MODE_Pos;
  1200. uint32_t size = 0;
  1201. /* Move the result to appropriate location (indicated in pRes parameter) */
  1202. switch (mode)
  1203. {
  1204. case PKA_MODE_ARITHMETIC_SUB:
  1205. case PKA_MODE_MODULAR_ADD:
  1206. case PKA_MODE_MODULAR_RED:
  1207. case PKA_MODE_MODULAR_INV:
  1208. case PKA_MODE_MODULAR_SUB:
  1209. case PKA_MODE_MONTGOMERY_MUL:
  1210. size = hpka->Instance->RAM[1] / 32UL;
  1211. break;
  1212. case PKA_MODE_ARITHMETIC_ADD:
  1213. size = hpka->Instance->RAM[1] / 32UL;
  1214. /* Manage the overflow of the addition */
  1215. if (hpka->Instance->RAM[500U + size] != 0UL)
  1216. {
  1217. size += 1UL;
  1218. }
  1219. break;
  1220. case PKA_MODE_COMPARISON:
  1221. size = 1;
  1222. break;
  1223. case PKA_MODE_ARITHMETIC_MUL:
  1224. size = hpka->Instance->RAM[1] / 32UL * 2UL;
  1225. break;
  1226. default:
  1227. break;
  1228. }
  1229. if (pRes != NULL)
  1230. {
  1231. switch (mode)
  1232. {
  1233. case PKA_MODE_ARITHMETIC_SUB:
  1234. case PKA_MODE_MODULAR_ADD:
  1235. case PKA_MODE_MODULAR_RED:
  1236. case PKA_MODE_MODULAR_INV:
  1237. case PKA_MODE_MODULAR_SUB:
  1238. case PKA_MODE_MONTGOMERY_MUL:
  1239. case PKA_MODE_ARITHMETIC_ADD:
  1240. case PKA_MODE_COMPARISON:
  1241. case PKA_MODE_ARITHMETIC_MUL:
  1242. PKA_Memcpy_u32_to_u32(pRes, &hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_OUT_RESULT], size);
  1243. break;
  1244. default:
  1245. break;
  1246. }
  1247. }
  1248. }
  1249. /**
  1250. * @brief Montgomery parameter computation in blocking mode.
  1251. * @param hpka PKA handle
  1252. * @param in Input information
  1253. * @param Timeout Timeout duration
  1254. * @retval HAL status
  1255. */
  1256. HAL_StatusTypeDef HAL_PKA_MontgomeryParam(PKA_HandleTypeDef *hpka, PKA_MontgomeryParamInTypeDef *in, uint32_t Timeout)
  1257. {
  1258. /* Set input parameter in PKA RAM */
  1259. PKA_MontgomeryParam_Set(hpka, in->size, in->pOp1);
  1260. /* Start the operation */
  1261. return PKA_Process(hpka, PKA_MODE_MONTGOMERY_PARAM, Timeout);
  1262. }
  1263. /**
  1264. * @brief Montgomery parameter computation in non-blocking mode with Interrupt.
  1265. * @param hpka PKA handle
  1266. * @param in Input information
  1267. * @retval HAL status
  1268. */
  1269. HAL_StatusTypeDef HAL_PKA_MontgomeryParam_IT(PKA_HandleTypeDef *hpka, PKA_MontgomeryParamInTypeDef *in)
  1270. {
  1271. /* Set input parameter in PKA RAM */
  1272. PKA_MontgomeryParam_Set(hpka, in->size, in->pOp1);
  1273. /* Start the operation */
  1274. return PKA_Process_IT(hpka, PKA_MODE_MONTGOMERY_PARAM);
  1275. }
  1276. /**
  1277. * @brief Retrieve operation result.
  1278. * @param hpka PKA handle
  1279. * @param pRes pointer to buffer where the result will be copied
  1280. * @retval HAL status
  1281. */
  1282. void HAL_PKA_MontgomeryParam_GetResult(PKA_HandleTypeDef *hpka, uint32_t *pRes)
  1283. {
  1284. uint32_t size;
  1285. /* Retrieve the size of the buffer from the PKA RAM */
  1286. size = (hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MOD_NB_BITS] + 31UL) / 32UL;
  1287. /* Move the result to appropriate location (indicated in out parameter) */
  1288. PKA_Memcpy_u32_to_u32(pRes, &hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_OUT_PARAMETER], size);
  1289. }
  1290. /**
  1291. * @brief Abort any ongoing operation.
  1292. * @param hpka PKA handle
  1293. * @retval HAL status
  1294. */
  1295. HAL_StatusTypeDef HAL_PKA_Abort(PKA_HandleTypeDef *hpka)
  1296. {
  1297. HAL_StatusTypeDef err = HAL_OK;
  1298. /* Clear EN bit */
  1299. /* This abort any operation in progress (PKA RAM content is not guaranteed in this case) */
  1300. CLEAR_BIT(hpka->Instance->CR, PKA_CR_EN);
  1301. SET_BIT(hpka->Instance->CR, PKA_CR_EN);
  1302. /* Reset any pending flag */
  1303. SET_BIT(hpka->Instance->CLRFR, PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
  1304. /* Reset the error code */
  1305. hpka->ErrorCode = HAL_PKA_ERROR_NONE;
  1306. /* Reset the state */
  1307. hpka->State = HAL_PKA_STATE_READY;
  1308. return err;
  1309. }
  1310. /**
  1311. * @brief Reset the PKA RAM.
  1312. * @param hpka PKA handle
  1313. * @retval None
  1314. */
  1315. void HAL_PKA_RAMReset(PKA_HandleTypeDef *hpka)
  1316. {
  1317. uint32_t index;
  1318. /* For each element in the PKA RAM */
  1319. for (index = 0; index < PKA_RAM_SIZE; index++)
  1320. {
  1321. /* Clear the content */
  1322. hpka->Instance->RAM[index] = 0UL;
  1323. }
  1324. }
  1325. /**
  1326. * @brief This function handles PKA event interrupt request.
  1327. * @param hpka PKA handle
  1328. * @retval None
  1329. */
  1330. void HAL_PKA_IRQHandler(PKA_HandleTypeDef *hpka)
  1331. {
  1332. uint32_t mode = PKA_GetMode(hpka);
  1333. uint32_t itsource = READ_REG(hpka->Instance->CR);
  1334. uint32_t flag = READ_REG(hpka->Instance->SR);
  1335. /* Address error interrupt occurred */
  1336. if (((itsource & PKA_IT_ADDRERR) == PKA_IT_ADDRERR) && ((flag & PKA_FLAG_ADDRERR) == PKA_FLAG_ADDRERR))
  1337. {
  1338. hpka->ErrorCode |= HAL_PKA_ERROR_ADDRERR;
  1339. /* Clear ADDRERR flag */
  1340. __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_ADDRERR);
  1341. }
  1342. /* RAM access error interrupt occurred */
  1343. if (((itsource & PKA_IT_RAMERR) == PKA_IT_RAMERR) && ((flag & PKA_FLAG_RAMERR) == PKA_FLAG_RAMERR))
  1344. {
  1345. hpka->ErrorCode |= HAL_PKA_ERROR_RAMERR;
  1346. /* Clear RAMERR flag */
  1347. __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_RAMERR);
  1348. }
  1349. /* Check the operation success in case of ECDSA signature */
  1350. if (mode == PKA_MODE_ECDSA_SIGNATURE)
  1351. {
  1352. /* If error output result is different from 0, ecdsa sign operation need to be repeated */
  1353. if (hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_ERROR] != 0UL)
  1354. {
  1355. hpka->ErrorCode |= HAL_PKA_ERROR_OPERATION;
  1356. }
  1357. }
  1358. /* Trigger the error callback if an error is present */
  1359. if (hpka->ErrorCode != HAL_PKA_ERROR_NONE)
  1360. {
  1361. #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
  1362. hpka->ErrorCallback(hpka);
  1363. #else
  1364. HAL_PKA_ErrorCallback(hpka);
  1365. #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
  1366. }
  1367. /* End Of Operation interrupt occurred */
  1368. if (((itsource & PKA_IT_PROCEND) == PKA_IT_PROCEND) && ((flag & PKA_FLAG_PROCEND) == PKA_FLAG_PROCEND))
  1369. {
  1370. /* Clear PROCEND flag */
  1371. __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_PROCEND);
  1372. /* Set the state to ready */
  1373. hpka->State = HAL_PKA_STATE_READY;
  1374. #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
  1375. hpka->OperationCpltCallback(hpka);
  1376. #else
  1377. HAL_PKA_OperationCpltCallback(hpka);
  1378. #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
  1379. }
  1380. }
  1381. /**
  1382. * @brief Process completed callback.
  1383. * @param hpka PKA handle
  1384. * @retval None
  1385. */
  1386. __weak void HAL_PKA_OperationCpltCallback(PKA_HandleTypeDef *hpka)
  1387. {
  1388. /* Prevent unused argument(s) compilation warning */
  1389. UNUSED(hpka);
  1390. /* NOTE : This function should not be modified, when the callback is needed,
  1391. the HAL_PKA_OperationCpltCallback could be implemented in the user file
  1392. */
  1393. }
  1394. /**
  1395. * @brief Error callback.
  1396. * @param hpka PKA handle
  1397. * @retval None
  1398. */
  1399. __weak void HAL_PKA_ErrorCallback(PKA_HandleTypeDef *hpka)
  1400. {
  1401. /* Prevent unused argument(s) compilation warning */
  1402. UNUSED(hpka);
  1403. /* NOTE : This function should not be modified, when the callback is needed,
  1404. the HAL_PKA_ErrorCallback could be implemented in the user file
  1405. */
  1406. }
  1407. /**
  1408. * @}
  1409. */
  1410. /** @defgroup PKA_Exported_Functions_Group3 Peripheral State and Error functions
  1411. * @brief Peripheral State and Error functions
  1412. *
  1413. @verbatim
  1414. ===============================================================================
  1415. ##### Peripheral State and Error functions #####
  1416. ===============================================================================
  1417. [..]
  1418. This subsection permit to get in run-time the status of the peripheral.
  1419. @endverbatim
  1420. * @{
  1421. */
  1422. /**
  1423. * @brief Return the PKA handle state.
  1424. * @param hpka PKA handle
  1425. * @retval HAL status
  1426. */
  1427. HAL_PKA_StateTypeDef HAL_PKA_GetState(const PKA_HandleTypeDef *hpka)
  1428. {
  1429. /* Return PKA handle state */
  1430. return hpka->State;
  1431. }
  1432. /**
  1433. * @brief Return the PKA error code.
  1434. * @param hpka PKA handle
  1435. * @retval PKA error code
  1436. */
  1437. uint32_t HAL_PKA_GetError(const PKA_HandleTypeDef *hpka)
  1438. {
  1439. /* Return PKA handle error code */
  1440. return hpka->ErrorCode;
  1441. }
  1442. /**
  1443. * @}
  1444. */
  1445. /**
  1446. * @}
  1447. */
  1448. /** @addtogroup PKA_Private_Functions
  1449. * @{
  1450. */
  1451. /**
  1452. * @brief Get PKA operating mode.
  1453. * @param hpka PKA handle
  1454. * @retval Return the current mode
  1455. */
  1456. uint32_t PKA_GetMode(const PKA_HandleTypeDef *hpka)
  1457. {
  1458. /* return the shifted PKA_CR_MODE value */
  1459. return (uint32_t)(READ_BIT(hpka->Instance->CR, PKA_CR_MODE) >> PKA_CR_MODE_Pos);
  1460. }
  1461. /**
  1462. * @brief Wait for operation completion or timeout.
  1463. * @param hpka PKA handle
  1464. * @param Timeout Timeout duration in millisecond.
  1465. * @param Tickstart Tick start value
  1466. * @retval HAL status
  1467. */
  1468. HAL_StatusTypeDef PKA_PollEndOfOperation(const PKA_HandleTypeDef *hpka, uint32_t Timeout, uint32_t Tickstart)
  1469. {
  1470. /* Wait for the end of operation or timeout */
  1471. while ((hpka->Instance->SR & PKA_SR_PROCENDF) == 0UL)
  1472. {
  1473. /* Check if timeout is disabled (set to infinite wait) */
  1474. if (Timeout != HAL_MAX_DELAY)
  1475. {
  1476. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0UL))
  1477. {
  1478. return HAL_TIMEOUT;
  1479. }
  1480. }
  1481. }
  1482. return HAL_OK;
  1483. }
  1484. /**
  1485. * @brief Return a hal error code based on PKA error flags.
  1486. * @param hpka PKA handle
  1487. * @param mode PKA operating mode
  1488. * @retval error code
  1489. */
  1490. uint32_t PKA_CheckError(const PKA_HandleTypeDef *hpka, uint32_t mode)
  1491. {
  1492. uint32_t err = HAL_PKA_ERROR_NONE;
  1493. /* Check RAMERR error */
  1494. if (__HAL_PKA_GET_FLAG(hpka, PKA_FLAG_RAMERR) == SET)
  1495. {
  1496. err |= HAL_PKA_ERROR_RAMERR;
  1497. }
  1498. /* Check ADDRERR error */
  1499. if (__HAL_PKA_GET_FLAG(hpka, PKA_FLAG_ADDRERR) == SET)
  1500. {
  1501. err |= HAL_PKA_ERROR_ADDRERR;
  1502. }
  1503. /* Check the operation success in case of ECDSA signature */
  1504. if (mode == PKA_MODE_ECDSA_SIGNATURE)
  1505. {
  1506. #define EDCSA_SIGN_NOERROR 0UL
  1507. /* If error output result is different from no error, ecsa sign operation need to be repeated */
  1508. if (hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_ERROR] != EDCSA_SIGN_NOERROR)
  1509. {
  1510. err |= HAL_PKA_ERROR_OPERATION;
  1511. }
  1512. }
  1513. return err;
  1514. }
  1515. /**
  1516. * @brief Get number of bits inside an array of u8.
  1517. * @param byteNumber Number of u8 inside the array
  1518. */
  1519. uint32_t PKA_GetBitSize_u8(uint32_t byteNumber)
  1520. {
  1521. /* Convert from number of uint8_t in an array to the associated number of bits in this array */
  1522. return byteNumber * 8UL;
  1523. }
  1524. /**
  1525. * @brief Get optimal number of bits inside an array of u8.
  1526. * @param byteNumber Number of u8 inside the array
  1527. * @param msb Most significant uint8_t of the array
  1528. */
  1529. uint32_t PKA_GetOptBitSize_u8(uint32_t byteNumber, uint8_t msb)
  1530. {
  1531. uint32_t position;
  1532. position = 32UL - __CLZ(msb);
  1533. return (((byteNumber - 1UL) * 8UL) + position);
  1534. }
  1535. /**
  1536. * @brief Get number of bits inside an array of u32.
  1537. * @param wordNumber Number of u32 inside the array
  1538. */
  1539. uint32_t PKA_GetBitSize_u32(uint32_t wordNumber)
  1540. {
  1541. /* Convert from number of uint32_t in an array to the associated number of bits in this array */
  1542. return wordNumber * 32UL;
  1543. }
  1544. /**
  1545. * @brief Get number of uint8_t element in an array of bitSize bits.
  1546. * @param bitSize Number of bits in an array
  1547. */
  1548. uint32_t PKA_GetArraySize_u8(uint32_t bitSize)
  1549. {
  1550. /* Manage the non aligned on uint8_t bitsize: */
  1551. /* 512 bits requires 64 uint8_t */
  1552. /* 521 bits requires 66 uint8_t */
  1553. return ((bitSize + 7UL) / 8UL);
  1554. }
  1555. /**
  1556. * @brief Copy uint32_t array to uint8_t array to fit PKA number representation.
  1557. * @param dst Pointer to destination
  1558. * @param src Pointer to source
  1559. * @param n Number of uint8_t to copy
  1560. * @retval dst
  1561. */
  1562. void PKA_Memcpy_u32_to_u8(uint8_t dst[], __IO const uint32_t src[], size_t n)
  1563. {
  1564. if (dst != NULL)
  1565. {
  1566. if (src != NULL)
  1567. {
  1568. uint32_t index_uint32_t = 0UL; /* This index is used outside of the loop */
  1569. for (; index_uint32_t < (n / 4UL); index_uint32_t++)
  1570. {
  1571. /* Avoid casting from uint8_t* to uint32_t* by copying 4 uint8_t in a row */
  1572. /* Apply __REV equivalent */
  1573. uint32_t index_uint8_t = n - 4UL - (index_uint32_t * 4UL);
  1574. dst[index_uint8_t + 3UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
  1575. dst[index_uint8_t + 2UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
  1576. dst[index_uint8_t + 1UL] = (uint8_t)((src[index_uint32_t] & 0x00FF0000U) >> 16UL);
  1577. dst[index_uint8_t + 0UL] = (uint8_t)((src[index_uint32_t] & 0xFF000000U) >> 24UL);
  1578. }
  1579. /* Manage the buffers not aligned on uint32_t */
  1580. if ((n % 4UL) == 1UL)
  1581. {
  1582. dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
  1583. }
  1584. else if ((n % 4UL) == 2UL)
  1585. {
  1586. dst[1UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
  1587. dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
  1588. }
  1589. else if ((n % 4UL) == 3UL)
  1590. {
  1591. dst[2UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
  1592. dst[1UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
  1593. dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x00FF0000U) >> 16UL);
  1594. }
  1595. else
  1596. {
  1597. /* The last element is already handle in the loop */
  1598. }
  1599. }
  1600. }
  1601. }
  1602. /**
  1603. * @brief Copy uint8_t array to uint32_t array to fit PKA number representation.
  1604. * @param dst Pointer to destination
  1605. * @param src Pointer to source
  1606. * @param n Number of uint8_t to copy (must be multiple of 4)
  1607. * @retval dst
  1608. */
  1609. void PKA_Memcpy_u8_to_u32(__IO uint32_t dst[], const uint8_t src[], size_t n)
  1610. {
  1611. if (dst != NULL)
  1612. {
  1613. if (src != NULL)
  1614. {
  1615. uint32_t index = 0UL; /* This index is used outside of the loop */
  1616. for (; index < (n / 4UL); index++)
  1617. {
  1618. /* Apply the equivalent of __REV from uint8_t to uint32_t */
  1619. dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
  1620. | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL) \
  1621. | ((uint32_t)src[(n - (index * 4UL) - 3UL)] << 16UL) \
  1622. | ((uint32_t)src[(n - (index * 4UL) - 4UL)] << 24UL);
  1623. }
  1624. /* Manage the buffers not aligned on uint32_t */
  1625. if ((n % 4UL) == 1UL)
  1626. {
  1627. dst[index] = (uint32_t)src[(n - (index * 4UL) - 1UL)];
  1628. }
  1629. else if ((n % 4UL) == 2UL)
  1630. {
  1631. dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
  1632. | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL);
  1633. }
  1634. else if ((n % 4UL) == 3UL)
  1635. {
  1636. dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
  1637. | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL) \
  1638. | ((uint32_t)src[(n - (index * 4UL) - 3UL)] << 16UL);
  1639. }
  1640. else
  1641. {
  1642. /* The last element is already handle in the loop */
  1643. }
  1644. }
  1645. }
  1646. }
  1647. /**
  1648. * @brief Copy uint32_t array to uint32_t array.
  1649. * @param dst Pointer to destination
  1650. * @param src Pointer to source
  1651. * @param n Number of u32 to be handled
  1652. * @retval dst
  1653. */
  1654. void PKA_Memcpy_u32_to_u32(__IO uint32_t dst[], __IO const uint32_t src[], size_t n)
  1655. {
  1656. /* If a destination buffer is provided */
  1657. if (dst != NULL)
  1658. {
  1659. /* If a source buffer is provided */
  1660. if (src != NULL)
  1661. {
  1662. /* For each element in the array */
  1663. for (uint32_t index = 0UL; index < n; index++)
  1664. {
  1665. /* Copy the content */
  1666. dst[index] = src[index];
  1667. }
  1668. }
  1669. }
  1670. }
  1671. /**
  1672. * @brief Generic function to start a PKA operation in blocking mode.
  1673. * @param hpka PKA handle
  1674. * @param mode PKA operation
  1675. * @param Timeout Timeout duration
  1676. * @retval HAL status
  1677. */
  1678. HAL_StatusTypeDef PKA_Process(PKA_HandleTypeDef *hpka, uint32_t mode, uint32_t Timeout)
  1679. {
  1680. HAL_StatusTypeDef err = HAL_OK;
  1681. uint32_t tickstart;
  1682. if (hpka->State == HAL_PKA_STATE_READY)
  1683. {
  1684. /* Set the state to busy */
  1685. hpka->State = HAL_PKA_STATE_BUSY;
  1686. /* Clear any pending error */
  1687. hpka->ErrorCode = HAL_PKA_ERROR_NONE;
  1688. /* Init tickstart for timeout management*/
  1689. tickstart = HAL_GetTick();
  1690. /* Set the mode and deactivate the interrupts */
  1691. MODIFY_REG(hpka->Instance->CR, PKA_CR_MODE | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE,
  1692. mode << PKA_CR_MODE_Pos);
  1693. /* Start the computation */
  1694. hpka->Instance->CR |= PKA_CR_START;
  1695. /* Wait for the end of operation or timeout */
  1696. if (PKA_PollEndOfOperation(hpka, Timeout, tickstart) != HAL_OK)
  1697. {
  1698. /* Abort any ongoing operation */
  1699. CLEAR_BIT(hpka->Instance->CR, PKA_CR_EN);
  1700. hpka->ErrorCode |= HAL_PKA_ERROR_TIMEOUT;
  1701. /* Make ready for the next operation */
  1702. SET_BIT(hpka->Instance->CR, PKA_CR_EN);
  1703. }
  1704. /* Check error */
  1705. hpka->ErrorCode |= PKA_CheckError(hpka, mode);
  1706. /* Clear all flags */
  1707. hpka->Instance->CLRFR |= (PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
  1708. /* Set the state to ready */
  1709. hpka->State = HAL_PKA_STATE_READY;
  1710. /* Manage the result based on encountered errors */
  1711. if (hpka->ErrorCode != HAL_PKA_ERROR_NONE)
  1712. {
  1713. err = HAL_ERROR;
  1714. }
  1715. }
  1716. else
  1717. {
  1718. err = HAL_ERROR;
  1719. }
  1720. return err;
  1721. }
  1722. /**
  1723. * @brief Generic function to start a PKA operation in non-blocking mode with Interrupt.
  1724. * @param hpka PKA handle
  1725. * @param mode PKA operation
  1726. * @retval HAL status
  1727. */
  1728. HAL_StatusTypeDef PKA_Process_IT(PKA_HandleTypeDef *hpka, uint32_t mode)
  1729. {
  1730. HAL_StatusTypeDef err = HAL_OK;
  1731. if (hpka->State == HAL_PKA_STATE_READY)
  1732. {
  1733. /* Set the state to busy */
  1734. hpka->State = HAL_PKA_STATE_BUSY;
  1735. /* Clear any pending error */
  1736. hpka->ErrorCode = HAL_PKA_ERROR_NONE;
  1737. /* Set the mode and activate interrupts */
  1738. MODIFY_REG(hpka->Instance->CR, PKA_CR_MODE | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE,
  1739. (mode << PKA_CR_MODE_Pos) | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE);
  1740. /* Start the computation */
  1741. hpka->Instance->CR |= PKA_CR_START;
  1742. }
  1743. else
  1744. {
  1745. err = HAL_ERROR;
  1746. }
  1747. return err;
  1748. }
  1749. /**
  1750. * @brief Set input parameters.
  1751. * @param hpka PKA handle
  1752. * @param in Input information
  1753. */
  1754. void PKA_ModExp_Set(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in)
  1755. {
  1756. /* Get the number of bit per operand */
  1757. hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = PKA_GetBitSize_u8(in->OpSize);
  1758. /* Get the number of bit of the exponent */
  1759. hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = PKA_GetBitSize_u8(in->expSize);
  1760. /* Move the input parameters pOp1 to PKA RAM */
  1761. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT_BASE], in->pOp1, in->OpSize);
  1762. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT_BASE + ((in->OpSize + 3UL) / 4UL));
  1763. /* Move the exponent to PKA RAM */
  1764. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT], in->pExp, in->expSize);
  1765. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT + ((in->expSize + 3UL) / 4UL));
  1766. /* Move the modulus to PKA RAM */
  1767. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MODULUS], in->pMod, in->OpSize);
  1768. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MODULUS + ((in->OpSize + 3UL) / 4UL));
  1769. }
  1770. /**
  1771. * @brief Set input parameters.
  1772. * @param hpka PKA handle
  1773. * @param in Input information
  1774. */
  1775. void PKA_ModExpFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in)
  1776. {
  1777. /* Get the number of bit per operand */
  1778. hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = PKA_GetBitSize_u8(in->OpSize);
  1779. /* Get the number of bit of the exponent */
  1780. hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = PKA_GetBitSize_u8(in->expSize);
  1781. /* Move the input parameters pOp1 to PKA RAM */
  1782. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT_BASE], in->pOp1, in->OpSize);
  1783. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT_BASE + (in->OpSize / 4UL));
  1784. /* Move the exponent to PKA RAM */
  1785. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT], in->pExp, in->expSize);
  1786. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT + (in->expSize / 4UL));
  1787. /* Move the modulus to PKA RAM */
  1788. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MODULUS], in->pMod, in->OpSize);
  1789. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MODULUS + (in->OpSize / 4UL));
  1790. /* Move the Montgomery parameter to PKA RAM */
  1791. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MONTGOMERY_PARAM], in->pMontgomeryParam,
  1792. in->OpSize / 4UL);
  1793. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MONTGOMERY_PARAM + (in->OpSize / 4UL));
  1794. }
  1795. /**
  1796. * @brief Set input parameters.
  1797. * @param hpka PKA handle
  1798. * @param in Input information
  1799. * @note If the modulus size is bigger than the hash size (with a curve SECP521R1 when using a SHA256 hash
  1800. * for example)the hash value should be written at the end of the buffer with zeros padding at beginning.
  1801. */
  1802. void PKA_ECDSASign_Set(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in)
  1803. {
  1804. /* Get the prime order n length */
  1805. hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS] = PKA_GetOptBitSize_u8(in->primeOrderSize, *(in->primeOrder));
  1806. /* Get the modulus p length */
  1807. hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1808. /* Get the coefficient a sign */
  1809. hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN] = in->coefSign;
  1810. /* Move the input parameters coefficient |a| to PKA RAM */
  1811. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_A_COEFF], in->coef, in->modulusSize);
  1812. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1813. /* Move the input parameters modulus value p to PKA RAM */
  1814. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_GF], in->modulus, in->modulusSize);
  1815. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1816. /* Move the input parameters integer k to PKA RAM */
  1817. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_K], in->integer, in->primeOrderSize);
  1818. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_K + ((in->primeOrderSize + 3UL) / 4UL));
  1819. /* Move the input parameters base point G coordinate x to PKA RAM */
  1820. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_INITIAL_POINT_X], in->basePointX, in->modulusSize);
  1821. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1822. /* Move the input parameters base point G coordinate y to PKA RAM */
  1823. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y], in->basePointY, in->modulusSize);
  1824. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1825. /* Move the input parameters hash of message z to PKA RAM */
  1826. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_HASH_E], in->hash, in->primeOrderSize);
  1827. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_HASH_E + ((in->primeOrderSize + 3UL) / 4UL));
  1828. /* Move the input parameters private key d to PKA RAM */
  1829. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D], in->privateKey, in->primeOrderSize);
  1830. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D + ((in->primeOrderSize + 3UL) / 4UL));
  1831. /* Move the input parameters prime order n to PKA RAM */
  1832. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_ORDER_N], in->primeOrder, in->primeOrderSize);
  1833. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_ORDER_N + ((in->primeOrderSize + 3UL) / 4UL));
  1834. }
  1835. /**
  1836. * @brief Set input parameters.
  1837. * @param hpka PKA handle
  1838. * @param in Input information
  1839. */
  1840. void PKA_ECDSAVerif_Set(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in)
  1841. {
  1842. /* Get the prime order n length */
  1843. hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS] = PKA_GetOptBitSize_u8(in->primeOrderSize, *(in->primeOrder));
  1844. /* Get the modulus p length */
  1845. hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1846. /* Get the coefficient a sign */
  1847. hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN] = in->coefSign;
  1848. /* Move the input parameters coefficient |a| to PKA RAM */
  1849. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_A_COEFF], in->coef, in->modulusSize);
  1850. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1851. /* Move the input parameters modulus value p to PKA RAM */
  1852. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_MOD_GF], in->modulus, in->modulusSize);
  1853. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1854. /* Move the input parameters base point G coordinate x to PKA RAM */
  1855. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_INITIAL_POINT_X], in->basePointX, in->modulusSize);
  1856. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1857. /* Move the input parameters base point G coordinate y to PKA RAM */
  1858. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y], in->basePointY, in->modulusSize);
  1859. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1860. /* Move the input parameters public-key curve point Q coordinate xQ to PKA RAM */
  1861. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X], in->pPubKeyCurvePtX,
  1862. in->modulusSize);
  1863. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1864. /* Move the input parameters public-key curve point Q coordinate xQ to PKA RAM */
  1865. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y], in->pPubKeyCurvePtY,
  1866. in->modulusSize);
  1867. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1868. /* Move the input parameters signature part r to PKA RAM */
  1869. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_SIGNATURE_R], in->RSign, in->primeOrderSize);
  1870. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_SIGNATURE_R + ((in->primeOrderSize + 3UL) / 4UL));
  1871. /* Move the input parameters signature part s to PKA RAM */
  1872. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_SIGNATURE_S], in->SSign, in->primeOrderSize);
  1873. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_SIGNATURE_S + ((in->primeOrderSize + 3UL) / 4UL));
  1874. /* Move the input parameters hash of message z to PKA RAM */
  1875. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_HASH_E], in->hash, in->primeOrderSize);
  1876. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_HASH_E + ((in->primeOrderSize + 3UL) / 4UL));
  1877. /* Move the input parameters curve prime order n to PKA RAM */
  1878. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_ORDER_N], in->primeOrder, in->primeOrderSize);
  1879. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_ORDER_N + ((in->primeOrderSize + 3UL) / 4UL));
  1880. }
  1881. /**
  1882. * @brief Set input parameters.
  1883. * @param hpka PKA handle
  1884. * @param in Input information
  1885. */
  1886. void PKA_RSACRTExp_Set(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in)
  1887. {
  1888. /* Get the operand length M */
  1889. hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] = PKA_GetBitSize_u8(in->size);
  1890. /* Move the input parameters operand dP to PKA RAM */
  1891. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_DP_CRT], in->pOpDp, in->size / 2UL);
  1892. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_DP_CRT + (in->size / 8UL));
  1893. /* Move the input parameters operand dQ to PKA RAM */
  1894. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_DQ_CRT], in->pOpDq, in->size / 2UL);
  1895. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_DQ_CRT + (in->size / 8UL));
  1896. /* Move the input parameters operand qinv to PKA RAM */
  1897. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_QINV_CRT], in->pOpQinv, in->size / 2UL);
  1898. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_QINV_CRT + (in->size / 8UL));
  1899. /* Move the input parameters prime p to PKA RAM */
  1900. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_PRIME_P], in->pPrimeP, in->size / 2UL);
  1901. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_PRIME_P + (in->size / 8UL));
  1902. /* Move the input parameters prime q to PKA RAM */
  1903. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_PRIME_Q], in->pPrimeQ, in->size / 2UL);
  1904. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_PRIME_Q + (in->size / 8UL));
  1905. /* Move the input parameters operand A to PKA RAM */
  1906. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_EXPONENT_BASE], in->popA, in->size);
  1907. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_EXPONENT_BASE + (in->size / 4UL));
  1908. }
  1909. /**
  1910. * @brief Set input parameters.
  1911. * @param hpka PKA handle
  1912. * @param in Input information
  1913. */
  1914. void PKA_PointCheck_Set(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in)
  1915. {
  1916. /* Get the modulus length */
  1917. hpka->Instance->RAM[PKA_POINT_CHECK_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1918. /* Get the coefficient a sign */
  1919. hpka->Instance->RAM[PKA_POINT_CHECK_IN_A_COEFF_SIGN] = in->coefSign;
  1920. /* Move the input parameters coefficient |a| to PKA RAM */
  1921. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_A_COEFF], in->coefA, in->modulusSize);
  1922. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1923. /* Move the input parameters coefficient b to PKA RAM */
  1924. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_B_COEFF], in->coefB, in->modulusSize);
  1925. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_B_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1926. /* Move the input parameters modulus value p to PKA RAM */
  1927. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_MOD_GF], in->modulus, in->modulusSize);
  1928. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1929. /* Move the input parameters Point P coordinate x to PKA RAM */
  1930. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
  1931. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1932. /* Move the input parameters Point P coordinate y to PKA RAM */
  1933. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
  1934. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1935. }
  1936. /**
  1937. * @brief Set input parameters.
  1938. * @param hpka PKA handle
  1939. * @param in Input information
  1940. */
  1941. void PKA_ECCMul_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in)
  1942. {
  1943. /* Get the scalar multiplier k length */
  1944. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = PKA_GetOptBitSize_u8(in->scalarMulSize, *(in->scalarMul));
  1945. /* Get the modulus length */
  1946. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1947. /* Get the coefficient a sign */
  1948. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = in->coefSign;
  1949. /* Move the input parameters coefficient |a| to PKA RAM */
  1950. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF], in->coefA, in->modulusSize);
  1951. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1952. /* Move the input parameters modulus value p to PKA RAM */
  1953. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MOD_GF], in->modulus, in->modulusSize);
  1954. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1955. /* Move the input parameters scalar multiplier k to PKA RAM */
  1956. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_K], in->scalarMul, in->scalarMulSize);
  1957. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_K + ((in->scalarMulSize + 3UL) / 4UL));
  1958. /* Move the input parameters Point P coordinate x to PKA RAM */
  1959. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
  1960. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1961. /* Move the input parameters Point P coordinate y to PKA RAM */
  1962. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
  1963. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1964. }
  1965. /**
  1966. * @brief Set input parameters.
  1967. * @param hpka PKA handle
  1968. * @param in Input information
  1969. */
  1970. void PKA_ECCMulFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in)
  1971. {
  1972. /* Get the scalar multiplier k length */
  1973. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = PKA_GetOptBitSize_u8(in->scalarMulSize, *(in->scalarMul));
  1974. /* Get the modulus length */
  1975. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1976. /* Get the coefficient a sign */
  1977. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = in->coefSign;
  1978. /* Move the input parameters coefficient |a| to PKA RAM */
  1979. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF], in->coefA, in->modulusSize);
  1980. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1981. /* Move the input parameters modulus value p to PKA RAM */
  1982. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MOD_GF], in->modulus, in->modulusSize);
  1983. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1984. /* Move the input parameters scalar multiplier k to PKA RAM */
  1985. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_K], in->scalarMul, in->scalarMulSize);
  1986. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_K + ((in->scalarMulSize + 3UL) / 4UL));
  1987. /* Move the input parameters Point P coordinate x to PKA RAM */
  1988. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
  1989. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1990. /* Move the input parameters Point P coordinate y to PKA RAM */
  1991. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
  1992. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1993. /* Move the Montgomery parameter to PKA RAM */
  1994. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MONTGOMERY_PARAM], in->pMontgomeryParam,
  1995. (in->modulusSize + 3UL) / 4UL);
  1996. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MONTGOMERY_PARAM + ((in->modulusSize + 3UL) / 4UL));
  1997. }
  1998. /**
  1999. * @brief Set input parameters.
  2000. * @param hpka PKA handle
  2001. * @param in Input information
  2002. */
  2003. void PKA_ModInv_Set(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in)
  2004. {
  2005. /* Get the number of bit per operand */
  2006. hpka->Instance->RAM[PKA_MODULAR_INV_NB_BITS] = PKA_GetBitSize_u32(in->size);
  2007. /* Move the input parameters operand A to PKA RAM */
  2008. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_INV_IN_OP1], in->pOp1, in->size);
  2009. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_INV_IN_OP1 + in->size);
  2010. /* Move the input parameters modulus value n to PKA RAM */
  2011. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_INV_IN_OP2_MOD], in->pMod, in->size * 4UL);
  2012. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_INV_IN_OP2_MOD + in->size);
  2013. }
  2014. /**
  2015. * @brief Set input parameters.
  2016. * @param hpka PKA handle
  2017. * @param in Input information
  2018. */
  2019. void PKA_ModRed_Set(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in)
  2020. {
  2021. /* Get the number of bit per operand */
  2022. hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_OP_LENGTH] = PKA_GetBitSize_u32(in->OpSize);
  2023. /* Get the number of bit per modulus */
  2024. hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_MOD_LENGTH] = PKA_GetBitSize_u8(in->modSize);
  2025. /* Move the input parameters operand A to PKA RAM */
  2026. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_OPERAND], in->pOp1, in->OpSize);
  2027. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_REDUC_IN_OPERAND + in->OpSize);
  2028. /* Move the input parameters modulus value n to PKA RAM */
  2029. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_MODULUS], in->pMod, in->modSize);
  2030. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_REDUC_IN_MODULUS + ((in->modSize + 3UL) / 4UL));
  2031. }
  2032. /**
  2033. * @brief Set input parameters.
  2034. * @param hpka PKA handle
  2035. * @param size Size of the operand
  2036. * @param pOp1 Generic pointer to input data
  2037. */
  2038. void PKA_MontgomeryParam_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint8_t *pOp1)
  2039. {
  2040. uint32_t bytetoskip = 0UL;
  2041. uint32_t newSize;
  2042. if (pOp1 != NULL)
  2043. {
  2044. /* Count the number of zero bytes */
  2045. while ((bytetoskip < size) && (pOp1[bytetoskip] == 0UL))
  2046. {
  2047. bytetoskip++;
  2048. }
  2049. /* Get new size after skipping zero bytes */
  2050. newSize = size - bytetoskip;
  2051. /* Get the number of bit per operand */
  2052. hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(newSize, pOp1[bytetoskip]);
  2053. /* Move the input parameters pOp1 to PKA RAM */
  2054. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MODULUS], pOp1, size);
  2055. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MONTGOMERY_PARAM_IN_MODULUS + ((size + 3UL) / 4UL));
  2056. }
  2057. }
  2058. /**
  2059. * @brief Generic function to set input parameters.
  2060. * @param hpka PKA handle
  2061. * @param size Size of the operand
  2062. * @param pOp1 Generic pointer to input data
  2063. * @param pOp2 Generic pointer to input data
  2064. * @param pOp3 Generic pointer to input data
  2065. */
  2066. void PKA_ARI_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint32_t *pOp1, const uint32_t *pOp2,
  2067. const uint8_t *pOp3)
  2068. {
  2069. /* Get the number of bit per operand */
  2070. hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_NB_BITS] = PKA_GetBitSize_u32(size);
  2071. if (pOp1 != NULL)
  2072. {
  2073. /* Move the input parameters pOp1 to PKA RAM */
  2074. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP1], pOp1, size);
  2075. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP1 + size);
  2076. }
  2077. if (pOp2 != NULL)
  2078. {
  2079. /* Move the input parameters pOp2 to PKA RAM */
  2080. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP2], pOp2, size);
  2081. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP2 + size);
  2082. }
  2083. if (pOp3 != NULL)
  2084. {
  2085. /* Move the input parameters pOp3 to PKA RAM */
  2086. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP3], pOp3, size * 4UL);
  2087. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP3 + size);
  2088. }
  2089. }
  2090. /**
  2091. * @}
  2092. */
  2093. /**
  2094. * @}
  2095. */
  2096. #endif /* defined(PKA) && defined(HAL_PKA_MODULE_ENABLED) */
  2097. /**
  2098. * @}
  2099. */