stm32l4xx_hal_pka.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487
  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(PKA_HandleTypeDef *hpka);
  232. HAL_StatusTypeDef PKA_PollEndOfOperation(PKA_HandleTypeDef *hpka, uint32_t Timeout, uint32_t Tickstart);
  233. uint32_t PKA_CheckError(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. FlagStatus addErrFlag = __HAL_PKA_GET_FLAG(hpka, PKA_FLAG_ADDRERR);
  1334. FlagStatus ramErrFlag = __HAL_PKA_GET_FLAG(hpka, PKA_FLAG_RAMERR);
  1335. FlagStatus procEndFlag = __HAL_PKA_GET_FLAG(hpka, PKA_FLAG_PROCEND);
  1336. /* Address error interrupt occurred */
  1337. if ((__HAL_PKA_GET_IT_SOURCE(hpka, PKA_IT_ADDRERR) == SET) && (addErrFlag == SET))
  1338. {
  1339. hpka->ErrorCode |= HAL_PKA_ERROR_ADDRERR;
  1340. /* Clear ADDRERR flag */
  1341. __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_ADDRERR);
  1342. }
  1343. /* RAM access error interrupt occurred */
  1344. if ((__HAL_PKA_GET_IT_SOURCE(hpka, PKA_IT_RAMERR) == SET) && (ramErrFlag == SET))
  1345. {
  1346. hpka->ErrorCode |= HAL_PKA_ERROR_RAMERR;
  1347. /* Clear RAMERR flag */
  1348. __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_RAMERR);
  1349. }
  1350. /* Check the operation success in case of ECDSA signature */
  1351. if (mode == PKA_MODE_ECDSA_SIGNATURE)
  1352. {
  1353. /* If error output result is different from 0, ecdsa sign operation need to be repeated */
  1354. if (hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_ERROR] != 0UL)
  1355. {
  1356. hpka->ErrorCode |= HAL_PKA_ERROR_OPERATION;
  1357. }
  1358. }
  1359. /* Trigger the error callback if an error is present */
  1360. if (hpka->ErrorCode != HAL_PKA_ERROR_NONE)
  1361. {
  1362. #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
  1363. hpka->ErrorCallback(hpka);
  1364. #else
  1365. HAL_PKA_ErrorCallback(hpka);
  1366. #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
  1367. }
  1368. /* End Of Operation interrupt occurred */
  1369. if ((__HAL_PKA_GET_IT_SOURCE(hpka, PKA_IT_PROCEND) == SET) && (procEndFlag == SET))
  1370. {
  1371. /* Clear PROCEND flag */
  1372. __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_PROCEND);
  1373. /* Set the state to ready */
  1374. hpka->State = HAL_PKA_STATE_READY;
  1375. #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
  1376. hpka->OperationCpltCallback(hpka);
  1377. #else
  1378. HAL_PKA_OperationCpltCallback(hpka);
  1379. #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
  1380. }
  1381. }
  1382. /**
  1383. * @brief Process completed callback.
  1384. * @param hpka PKA handle
  1385. * @retval None
  1386. */
  1387. __weak void HAL_PKA_OperationCpltCallback(PKA_HandleTypeDef *hpka)
  1388. {
  1389. /* Prevent unused argument(s) compilation warning */
  1390. UNUSED(hpka);
  1391. /* NOTE : This function should not be modified, when the callback is needed,
  1392. the HAL_PKA_OperationCpltCallback could be implemented in the user file
  1393. */
  1394. }
  1395. /**
  1396. * @brief Error callback.
  1397. * @param hpka PKA handle
  1398. * @retval None
  1399. */
  1400. __weak void HAL_PKA_ErrorCallback(PKA_HandleTypeDef *hpka)
  1401. {
  1402. /* Prevent unused argument(s) compilation warning */
  1403. UNUSED(hpka);
  1404. /* NOTE : This function should not be modified, when the callback is needed,
  1405. the HAL_PKA_ErrorCallback could be implemented in the user file
  1406. */
  1407. }
  1408. /**
  1409. * @}
  1410. */
  1411. /** @defgroup PKA_Exported_Functions_Group3 Peripheral State and Error functions
  1412. * @brief Peripheral State and Error functions
  1413. *
  1414. @verbatim
  1415. ===============================================================================
  1416. ##### Peripheral State and Error functions #####
  1417. ===============================================================================
  1418. [..]
  1419. This subsection permit to get in run-time the status of the peripheral.
  1420. @endverbatim
  1421. * @{
  1422. */
  1423. /**
  1424. * @brief Return the PKA handle state.
  1425. * @param hpka PKA handle
  1426. * @retval HAL status
  1427. */
  1428. HAL_PKA_StateTypeDef HAL_PKA_GetState(PKA_HandleTypeDef *hpka)
  1429. {
  1430. /* Return PKA handle state */
  1431. return hpka->State;
  1432. }
  1433. /**
  1434. * @brief Return the PKA error code.
  1435. * @param hpka PKA handle
  1436. * @retval PKA error code
  1437. */
  1438. uint32_t HAL_PKA_GetError(PKA_HandleTypeDef *hpka)
  1439. {
  1440. /* Return PKA handle error code */
  1441. return hpka->ErrorCode;
  1442. }
  1443. /**
  1444. * @}
  1445. */
  1446. /**
  1447. * @}
  1448. */
  1449. /** @addtogroup PKA_Private_Functions
  1450. * @{
  1451. */
  1452. /**
  1453. * @brief Get PKA operating mode.
  1454. * @param hpka PKA handle
  1455. * @retval Return the current mode
  1456. */
  1457. uint32_t PKA_GetMode(PKA_HandleTypeDef *hpka)
  1458. {
  1459. /* return the shifted PKA_CR_MODE value */
  1460. return (uint32_t)(READ_BIT(hpka->Instance->CR, PKA_CR_MODE) >> PKA_CR_MODE_Pos);
  1461. }
  1462. /**
  1463. * @brief Wait for operation completion or timeout.
  1464. * @param hpka PKA handle
  1465. * @param Timeout Timeout duration in millisecond.
  1466. * @param Tickstart Tick start value
  1467. * @retval HAL status
  1468. */
  1469. HAL_StatusTypeDef PKA_PollEndOfOperation(PKA_HandleTypeDef *hpka, uint32_t Timeout, uint32_t Tickstart)
  1470. {
  1471. /* Wait for the end of operation or timeout */
  1472. while ((hpka->Instance->SR & PKA_SR_PROCENDF) == 0UL)
  1473. {
  1474. /* Check if timeout is disabled (set to infinite wait) */
  1475. if (Timeout != HAL_MAX_DELAY)
  1476. {
  1477. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0UL))
  1478. {
  1479. return HAL_TIMEOUT;
  1480. }
  1481. }
  1482. }
  1483. return HAL_OK;
  1484. }
  1485. /**
  1486. * @brief Return a hal error code based on PKA error flags.
  1487. * @param hpka PKA handle
  1488. * @param mode PKA operating mode
  1489. * @retval error code
  1490. */
  1491. uint32_t PKA_CheckError(PKA_HandleTypeDef *hpka, uint32_t mode)
  1492. {
  1493. uint32_t err = HAL_PKA_ERROR_NONE;
  1494. /* Check RAMERR error */
  1495. if (__HAL_PKA_GET_FLAG(hpka, PKA_FLAG_RAMERR) == SET)
  1496. {
  1497. err |= HAL_PKA_ERROR_RAMERR;
  1498. }
  1499. /* Check ADDRERR error */
  1500. if (__HAL_PKA_GET_FLAG(hpka, PKA_FLAG_ADDRERR) == SET)
  1501. {
  1502. err |= HAL_PKA_ERROR_ADDRERR;
  1503. }
  1504. /* Check the operation success in case of ECDSA signature */
  1505. if (mode == PKA_MODE_ECDSA_SIGNATURE)
  1506. {
  1507. #define EDCSA_SIGN_NOERROR 0UL
  1508. /* If error output result is different from no error, ecsa sign operation need to be repeated */
  1509. if (hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_ERROR] != EDCSA_SIGN_NOERROR)
  1510. {
  1511. err |= HAL_PKA_ERROR_OPERATION;
  1512. }
  1513. }
  1514. return err;
  1515. }
  1516. /**
  1517. * @brief Get number of bits inside an array of u8.
  1518. * @param byteNumber Number of u8 inside the array
  1519. */
  1520. uint32_t PKA_GetBitSize_u8(uint32_t byteNumber)
  1521. {
  1522. /* Convert from number of uint8_t in an array to the associated number of bits in this array */
  1523. return byteNumber * 8UL;
  1524. }
  1525. /**
  1526. * @brief Get optimal number of bits inside an array of u8.
  1527. * @param byteNumber Number of u8 inside the array
  1528. * @param msb Most significant uint8_t of the array
  1529. */
  1530. uint32_t PKA_GetOptBitSize_u8(uint32_t byteNumber, uint8_t msb)
  1531. {
  1532. uint32_t position;
  1533. position = 32UL - __CLZ(msb);
  1534. return (((byteNumber - 1UL) * 8UL) + position);
  1535. }
  1536. /**
  1537. * @brief Get number of bits inside an array of u32.
  1538. * @param wordNumber Number of u32 inside the array
  1539. */
  1540. uint32_t PKA_GetBitSize_u32(uint32_t wordNumber)
  1541. {
  1542. /* Convert from number of uint32_t in an array to the associated number of bits in this array */
  1543. return wordNumber * 32UL;
  1544. }
  1545. /**
  1546. * @brief Get number of uint8_t element in an array of bitSize bits.
  1547. * @param bitSize Number of bits in an array
  1548. */
  1549. uint32_t PKA_GetArraySize_u8(uint32_t bitSize)
  1550. {
  1551. /* Manage the non aligned on uint8_t bitsize: */
  1552. /* 512 bits requires 64 uint8_t */
  1553. /* 521 bits requires 66 uint8_t */
  1554. return ((bitSize + 7UL) / 8UL);
  1555. }
  1556. /**
  1557. * @brief Copy uint32_t array to uint8_t array to fit PKA number representation.
  1558. * @param dst Pointer to destination
  1559. * @param src Pointer to source
  1560. * @param n Number of uint8_t to copy
  1561. * @retval dst
  1562. */
  1563. void PKA_Memcpy_u32_to_u8(uint8_t dst[], __IO const uint32_t src[], size_t n)
  1564. {
  1565. if (dst != NULL)
  1566. {
  1567. if (src != NULL)
  1568. {
  1569. uint32_t index_uint32_t = 0UL; /* This index is used outside of the loop */
  1570. for (; index_uint32_t < (n / 4UL); index_uint32_t++)
  1571. {
  1572. /* Avoid casting from uint8_t* to uint32_t* by copying 4 uint8_t in a row */
  1573. /* Apply __REV equivalent */
  1574. uint32_t index_uint8_t = n - 4UL - (index_uint32_t * 4UL);
  1575. dst[index_uint8_t + 3UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
  1576. dst[index_uint8_t + 2UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
  1577. dst[index_uint8_t + 1UL] = (uint8_t)((src[index_uint32_t] & 0x00FF0000U) >> 16UL);
  1578. dst[index_uint8_t + 0UL] = (uint8_t)((src[index_uint32_t] & 0xFF000000U) >> 24UL);
  1579. }
  1580. /* Manage the buffers not aligned on uint32_t */
  1581. if ((n % 4UL) == 1UL)
  1582. {
  1583. dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
  1584. }
  1585. else if ((n % 4UL) == 2UL)
  1586. {
  1587. dst[1UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
  1588. dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
  1589. }
  1590. else if ((n % 4UL) == 3UL)
  1591. {
  1592. dst[2UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
  1593. dst[1UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
  1594. dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x00FF0000U) >> 16UL);
  1595. }
  1596. else
  1597. {
  1598. /* The last element is already handle in the loop */
  1599. }
  1600. }
  1601. }
  1602. }
  1603. /**
  1604. * @brief Copy uint8_t array to uint32_t array to fit PKA number representation.
  1605. * @param dst Pointer to destination
  1606. * @param src Pointer to source
  1607. * @param n Number of uint8_t to copy (must be multiple of 4)
  1608. * @retval dst
  1609. */
  1610. void PKA_Memcpy_u8_to_u32(__IO uint32_t dst[], const uint8_t src[], size_t n)
  1611. {
  1612. if (dst != NULL)
  1613. {
  1614. if (src != NULL)
  1615. {
  1616. uint32_t index = 0UL; /* This index is used outside of the loop */
  1617. for (; index < (n / 4UL); index++)
  1618. {
  1619. /* Apply the equivalent of __REV from uint8_t to uint32_t */
  1620. dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
  1621. | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL) \
  1622. | ((uint32_t)src[(n - (index * 4UL) - 3UL)] << 16UL) \
  1623. | ((uint32_t)src[(n - (index * 4UL) - 4UL)] << 24UL);
  1624. }
  1625. /* Manage the buffers not aligned on uint32_t */
  1626. if ((n % 4UL) == 1UL)
  1627. {
  1628. dst[index] = (uint32_t)src[(n - (index * 4UL) - 1UL)];
  1629. }
  1630. else if ((n % 4UL) == 2UL)
  1631. {
  1632. dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
  1633. | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL);
  1634. }
  1635. else if ((n % 4UL) == 3UL)
  1636. {
  1637. dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
  1638. | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL) \
  1639. | ((uint32_t)src[(n - (index * 4UL) - 3UL)] << 16UL);
  1640. }
  1641. else
  1642. {
  1643. /* The last element is already handle in the loop */
  1644. }
  1645. }
  1646. }
  1647. }
  1648. /**
  1649. * @brief Copy uint32_t array to uint32_t array.
  1650. * @param dst Pointer to destination
  1651. * @param src Pointer to source
  1652. * @param n Number of u32 to be handled
  1653. * @retval dst
  1654. */
  1655. void PKA_Memcpy_u32_to_u32(__IO uint32_t dst[], __IO const uint32_t src[], size_t n)
  1656. {
  1657. /* If a destination buffer is provided */
  1658. if (dst != NULL)
  1659. {
  1660. /* If a source buffer is provided */
  1661. if (src != NULL)
  1662. {
  1663. /* For each element in the array */
  1664. for (uint32_t index = 0UL; index < n; index++)
  1665. {
  1666. /* Copy the content */
  1667. dst[index] = src[index];
  1668. }
  1669. }
  1670. }
  1671. }
  1672. /**
  1673. * @brief Generic function to start a PKA operation in blocking mode.
  1674. * @param hpka PKA handle
  1675. * @param mode PKA operation
  1676. * @param Timeout Timeout duration
  1677. * @retval HAL status
  1678. */
  1679. HAL_StatusTypeDef PKA_Process(PKA_HandleTypeDef *hpka, uint32_t mode, uint32_t Timeout)
  1680. {
  1681. HAL_StatusTypeDef err = HAL_OK;
  1682. uint32_t tickstart;
  1683. if (hpka->State == HAL_PKA_STATE_READY)
  1684. {
  1685. /* Set the state to busy */
  1686. hpka->State = HAL_PKA_STATE_BUSY;
  1687. /* Clear any pending error */
  1688. hpka->ErrorCode = HAL_PKA_ERROR_NONE;
  1689. /* Init tickstart for timeout management*/
  1690. tickstart = HAL_GetTick();
  1691. /* Set the mode and deactivate the interrupts */
  1692. MODIFY_REG(hpka->Instance->CR, PKA_CR_MODE | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE,
  1693. mode << PKA_CR_MODE_Pos);
  1694. /* Start the computation */
  1695. hpka->Instance->CR |= PKA_CR_START;
  1696. /* Wait for the end of operation or timeout */
  1697. if (PKA_PollEndOfOperation(hpka, Timeout, tickstart) != HAL_OK)
  1698. {
  1699. /* Abort any ongoing operation */
  1700. CLEAR_BIT(hpka->Instance->CR, PKA_CR_EN);
  1701. hpka->ErrorCode |= HAL_PKA_ERROR_TIMEOUT;
  1702. /* Make ready for the next operation */
  1703. SET_BIT(hpka->Instance->CR, PKA_CR_EN);
  1704. }
  1705. /* Check error */
  1706. hpka->ErrorCode |= PKA_CheckError(hpka, mode);
  1707. /* Clear all flags */
  1708. hpka->Instance->CLRFR |= (PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
  1709. /* Set the state to ready */
  1710. hpka->State = HAL_PKA_STATE_READY;
  1711. /* Manage the result based on encountered errors */
  1712. if (hpka->ErrorCode != HAL_PKA_ERROR_NONE)
  1713. {
  1714. err = HAL_ERROR;
  1715. }
  1716. }
  1717. else
  1718. {
  1719. err = HAL_ERROR;
  1720. }
  1721. return err;
  1722. }
  1723. /**
  1724. * @brief Generic function to start a PKA operation in non-blocking mode with Interrupt.
  1725. * @param hpka PKA handle
  1726. * @param mode PKA operation
  1727. * @retval HAL status
  1728. */
  1729. HAL_StatusTypeDef PKA_Process_IT(PKA_HandleTypeDef *hpka, uint32_t mode)
  1730. {
  1731. HAL_StatusTypeDef err = HAL_OK;
  1732. if (hpka->State == HAL_PKA_STATE_READY)
  1733. {
  1734. /* Set the state to busy */
  1735. hpka->State = HAL_PKA_STATE_BUSY;
  1736. /* Clear any pending error */
  1737. hpka->ErrorCode = HAL_PKA_ERROR_NONE;
  1738. /* Set the mode and activate interrupts */
  1739. MODIFY_REG(hpka->Instance->CR, PKA_CR_MODE | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE,
  1740. (mode << PKA_CR_MODE_Pos) | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE);
  1741. /* Start the computation */
  1742. hpka->Instance->CR |= PKA_CR_START;
  1743. }
  1744. else
  1745. {
  1746. err = HAL_ERROR;
  1747. }
  1748. return err;
  1749. }
  1750. /**
  1751. * @brief Set input parameters.
  1752. * @param hpka PKA handle
  1753. * @param in Input information
  1754. */
  1755. void PKA_ModExp_Set(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in)
  1756. {
  1757. /* Get the number of bit per operand */
  1758. hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = PKA_GetBitSize_u8(in->OpSize);
  1759. /* Get the number of bit of the exponent */
  1760. hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = PKA_GetBitSize_u8(in->expSize);
  1761. /* Move the input parameters pOp1 to PKA RAM */
  1762. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT_BASE], in->pOp1, in->OpSize);
  1763. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT_BASE + ((in->OpSize + 3UL) / 4UL));
  1764. /* Move the exponent to PKA RAM */
  1765. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT], in->pExp, in->expSize);
  1766. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT + ((in->expSize + 3UL) / 4UL));
  1767. /* Move the modulus to PKA RAM */
  1768. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MODULUS], in->pMod, in->OpSize);
  1769. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MODULUS + ((in->OpSize + 3UL) / 4UL));
  1770. }
  1771. /**
  1772. * @brief Set input parameters.
  1773. * @param hpka PKA handle
  1774. * @param in Input information
  1775. */
  1776. void PKA_ModExpFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in)
  1777. {
  1778. /* Get the number of bit per operand */
  1779. hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = PKA_GetBitSize_u8(in->OpSize);
  1780. /* Get the number of bit of the exponent */
  1781. hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = PKA_GetBitSize_u8(in->expSize);
  1782. /* Move the input parameters pOp1 to PKA RAM */
  1783. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT_BASE], in->pOp1, in->OpSize);
  1784. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT_BASE + (in->OpSize / 4UL));
  1785. /* Move the exponent to PKA RAM */
  1786. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT], in->pExp, in->expSize);
  1787. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT + (in->expSize / 4UL));
  1788. /* Move the modulus to PKA RAM */
  1789. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MODULUS], in->pMod, in->OpSize);
  1790. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MODULUS + (in->OpSize / 4UL));
  1791. /* Move the Montgomery parameter to PKA RAM */
  1792. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MONTGOMERY_PARAM], in->pMontgomeryParam,
  1793. in->OpSize / 4UL);
  1794. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MONTGOMERY_PARAM + (in->OpSize / 4UL));
  1795. }
  1796. /**
  1797. * @brief Set input parameters.
  1798. * @param hpka PKA handle
  1799. * @param in Input information
  1800. */
  1801. void PKA_ECDSASign_Set(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in)
  1802. {
  1803. /* Get the prime order n length */
  1804. hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS] = PKA_GetOptBitSize_u8(in->primeOrderSize, *(in->primeOrder));
  1805. /* Get the modulus p length */
  1806. hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1807. /* Get the coefficient a sign */
  1808. hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN] = in->coefSign;
  1809. /* Move the input parameters coefficient |a| to PKA RAM */
  1810. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_A_COEFF], in->coef, in->modulusSize);
  1811. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1812. /* Move the input parameters modulus value p to PKA RAM */
  1813. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_GF], in->modulus, in->modulusSize);
  1814. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1815. /* Move the input parameters integer k to PKA RAM */
  1816. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_K], in->integer, in->primeOrderSize);
  1817. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_K + ((in->primeOrderSize + 3UL) / 4UL));
  1818. /* Move the input parameters base point G coordinate x to PKA RAM */
  1819. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_INITIAL_POINT_X], in->basePointX, in->modulusSize);
  1820. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1821. /* Move the input parameters base point G coordinate y to PKA RAM */
  1822. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y], in->basePointY, in->modulusSize);
  1823. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1824. /* Move the input parameters hash of message z to PKA RAM */
  1825. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_HASH_E], in->hash, in->primeOrderSize);
  1826. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_HASH_E + ((in->primeOrderSize + 3UL) / 4UL));
  1827. /* Move the input parameters private key d to PKA RAM */
  1828. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D], in->privateKey, in->primeOrderSize);
  1829. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D + ((in->primeOrderSize + 3UL) / 4UL));
  1830. /* Move the input parameters prime order n to PKA RAM */
  1831. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_ORDER_N], in->primeOrder, in->primeOrderSize);
  1832. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_ORDER_N + ((in->primeOrderSize + 3UL) / 4UL));
  1833. }
  1834. /**
  1835. * @brief Set input parameters.
  1836. * @param hpka PKA handle
  1837. * @param in Input information
  1838. */
  1839. void PKA_ECDSAVerif_Set(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in)
  1840. {
  1841. /* Get the prime order n length */
  1842. hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS] = PKA_GetOptBitSize_u8(in->primeOrderSize, *(in->primeOrder));
  1843. /* Get the modulus p length */
  1844. hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1845. /* Get the coefficient a sign */
  1846. hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN] = in->coefSign;
  1847. /* Move the input parameters coefficient |a| to PKA RAM */
  1848. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_A_COEFF], in->coef, in->modulusSize);
  1849. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1850. /* Move the input parameters modulus value p to PKA RAM */
  1851. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_MOD_GF], in->modulus, in->modulusSize);
  1852. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1853. /* Move the input parameters base point G coordinate x to PKA RAM */
  1854. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_INITIAL_POINT_X], in->basePointX, in->modulusSize);
  1855. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1856. /* Move the input parameters base point G coordinate y to PKA RAM */
  1857. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y], in->basePointY, in->modulusSize);
  1858. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1859. /* Move the input parameters public-key curve point Q coordinate xQ to PKA RAM */
  1860. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X], in->pPubKeyCurvePtX,
  1861. in->modulusSize);
  1862. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1863. /* Move the input parameters public-key curve point Q coordinate xQ to PKA RAM */
  1864. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y], in->pPubKeyCurvePtY,
  1865. in->modulusSize);
  1866. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1867. /* Move the input parameters signature part r to PKA RAM */
  1868. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_SIGNATURE_R], in->RSign, in->primeOrderSize);
  1869. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_SIGNATURE_R + ((in->primeOrderSize + 3UL) / 4UL));
  1870. /* Move the input parameters signature part s to PKA RAM */
  1871. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_SIGNATURE_S], in->SSign, in->primeOrderSize);
  1872. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_SIGNATURE_S + ((in->primeOrderSize + 3UL) / 4UL));
  1873. /* Move the input parameters hash of message z to PKA RAM */
  1874. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_HASH_E], in->hash, in->primeOrderSize);
  1875. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_HASH_E + ((in->primeOrderSize + 3UL) / 4UL));
  1876. /* Move the input parameters curve prime order n to PKA RAM */
  1877. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_ORDER_N], in->primeOrder, in->primeOrderSize);
  1878. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_ORDER_N + ((in->primeOrderSize + 3UL) / 4UL));
  1879. }
  1880. /**
  1881. * @brief Set input parameters.
  1882. * @param hpka PKA handle
  1883. * @param in Input information
  1884. */
  1885. void PKA_RSACRTExp_Set(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in)
  1886. {
  1887. /* Get the operand length M */
  1888. hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] = PKA_GetBitSize_u8(in->size);
  1889. /* Move the input parameters operand dP to PKA RAM */
  1890. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_DP_CRT], in->pOpDp, in->size / 2UL);
  1891. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_DP_CRT + (in->size / 8UL));
  1892. /* Move the input parameters operand dQ to PKA RAM */
  1893. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_DQ_CRT], in->pOpDq, in->size / 2UL);
  1894. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_DQ_CRT + (in->size / 8UL));
  1895. /* Move the input parameters operand qinv to PKA RAM */
  1896. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_QINV_CRT], in->pOpQinv, in->size / 2UL);
  1897. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_QINV_CRT + (in->size / 8UL));
  1898. /* Move the input parameters prime p to PKA RAM */
  1899. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_PRIME_P], in->pPrimeP, in->size / 2UL);
  1900. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_PRIME_P + (in->size / 8UL));
  1901. /* Move the input parameters prime q to PKA RAM */
  1902. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_PRIME_Q], in->pPrimeQ, in->size / 2UL);
  1903. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_PRIME_Q + (in->size / 8UL));
  1904. /* Move the input parameters operand A to PKA RAM */
  1905. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_EXPONENT_BASE], in->popA, in->size);
  1906. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_EXPONENT_BASE + (in->size / 4UL));
  1907. }
  1908. /**
  1909. * @brief Set input parameters.
  1910. * @param hpka PKA handle
  1911. * @param in Input information
  1912. */
  1913. void PKA_PointCheck_Set(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in)
  1914. {
  1915. /* Get the modulus length */
  1916. hpka->Instance->RAM[PKA_POINT_CHECK_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1917. /* Get the coefficient a sign */
  1918. hpka->Instance->RAM[PKA_POINT_CHECK_IN_A_COEFF_SIGN] = in->coefSign;
  1919. /* Move the input parameters coefficient |a| to PKA RAM */
  1920. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_A_COEFF], in->coefA, in->modulusSize);
  1921. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1922. /* Move the input parameters coefficient b to PKA RAM */
  1923. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_B_COEFF], in->coefB, in->modulusSize);
  1924. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_B_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1925. /* Move the input parameters modulus value p to PKA RAM */
  1926. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_MOD_GF], in->modulus, in->modulusSize);
  1927. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1928. /* Move the input parameters Point P coordinate x to PKA RAM */
  1929. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
  1930. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1931. /* Move the input parameters Point P coordinate y to PKA RAM */
  1932. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
  1933. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1934. }
  1935. /**
  1936. * @brief Set input parameters.
  1937. * @param hpka PKA handle
  1938. * @param in Input information
  1939. */
  1940. void PKA_ECCMul_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in)
  1941. {
  1942. /* Get the scalar multiplier k length */
  1943. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = PKA_GetOptBitSize_u8(in->scalarMulSize, *(in->scalarMul));
  1944. /* Get the modulus length */
  1945. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1946. /* Get the coefficient a sign */
  1947. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = in->coefSign;
  1948. /* Move the input parameters coefficient |a| to PKA RAM */
  1949. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF], in->coefA, in->modulusSize);
  1950. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1951. /* Move the input parameters modulus value p to PKA RAM */
  1952. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MOD_GF], in->modulus, in->modulusSize);
  1953. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1954. /* Move the input parameters scalar multiplier k to PKA RAM */
  1955. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_K], in->scalarMul, in->scalarMulSize);
  1956. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_K + ((in->scalarMulSize + 3UL) / 4UL));
  1957. /* Move the input parameters Point P coordinate x to PKA RAM */
  1958. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
  1959. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1960. /* Move the input parameters Point P coordinate y to PKA RAM */
  1961. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
  1962. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1963. }
  1964. /**
  1965. * @brief Set input parameters.
  1966. * @param hpka PKA handle
  1967. * @param in Input information
  1968. */
  1969. void PKA_ECCMulFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in)
  1970. {
  1971. /* Get the scalar multiplier k length */
  1972. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = PKA_GetOptBitSize_u8(in->scalarMulSize, *(in->scalarMul));
  1973. /* Get the modulus length */
  1974. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1975. /* Get the coefficient a sign */
  1976. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = in->coefSign;
  1977. /* Move the input parameters coefficient |a| to PKA RAM */
  1978. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF], in->coefA, in->modulusSize);
  1979. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1980. /* Move the input parameters modulus value p to PKA RAM */
  1981. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MOD_GF], in->modulus, in->modulusSize);
  1982. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1983. /* Move the input parameters scalar multiplier k to PKA RAM */
  1984. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_K], in->scalarMul, in->scalarMulSize);
  1985. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_K + ((in->scalarMulSize + 3UL) / 4UL));
  1986. /* Move the input parameters Point P coordinate x to PKA RAM */
  1987. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
  1988. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1989. /* Move the input parameters Point P coordinate y to PKA RAM */
  1990. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
  1991. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1992. /* Move the Montgomery parameter to PKA RAM */
  1993. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MONTGOMERY_PARAM], in->pMontgomeryParam,
  1994. (in->modulusSize + 3UL) / 4UL);
  1995. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MONTGOMERY_PARAM + ((in->modulusSize + 3UL) / 4UL));
  1996. }
  1997. /**
  1998. * @brief Set input parameters.
  1999. * @param hpka PKA handle
  2000. * @param in Input information
  2001. */
  2002. void PKA_ModInv_Set(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in)
  2003. {
  2004. /* Get the number of bit per operand */
  2005. hpka->Instance->RAM[PKA_MODULAR_INV_NB_BITS] = PKA_GetBitSize_u32(in->size);
  2006. /* Move the input parameters operand A to PKA RAM */
  2007. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_INV_IN_OP1], in->pOp1, in->size);
  2008. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_INV_IN_OP1 + in->size);
  2009. /* Move the input parameters modulus value n to PKA RAM */
  2010. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_INV_IN_OP2_MOD], in->pMod, in->size * 4UL);
  2011. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_INV_IN_OP2_MOD + in->size);
  2012. }
  2013. /**
  2014. * @brief Set input parameters.
  2015. * @param hpka PKA handle
  2016. * @param in Input information
  2017. */
  2018. void PKA_ModRed_Set(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in)
  2019. {
  2020. /* Get the number of bit per operand */
  2021. hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_OP_LENGTH] = PKA_GetBitSize_u32(in->OpSize);
  2022. /* Get the number of bit per modulus */
  2023. hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_MOD_LENGTH] = PKA_GetBitSize_u8(in->modSize);
  2024. /* Move the input parameters operand A to PKA RAM */
  2025. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_OPERAND], in->pOp1, in->OpSize);
  2026. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_REDUC_IN_OPERAND + in->OpSize);
  2027. /* Move the input parameters modulus value n to PKA RAM */
  2028. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_MODULUS], in->pMod, in->modSize);
  2029. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_REDUC_IN_MODULUS + (in->modSize / 4UL));
  2030. }
  2031. /**
  2032. * @brief Set input parameters.
  2033. * @param hpka PKA handle
  2034. * @param size Size of the operand
  2035. * @param pOp1 Generic pointer to input data
  2036. */
  2037. void PKA_MontgomeryParam_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint8_t *pOp1)
  2038. {
  2039. uint32_t bytetoskip = 0UL;
  2040. uint32_t newSize;
  2041. if (pOp1 != NULL)
  2042. {
  2043. /* Count the number of zero bytes */
  2044. while ((bytetoskip < size) && (pOp1[bytetoskip] == 0UL))
  2045. {
  2046. bytetoskip++;
  2047. }
  2048. /* Get new size after skipping zero bytes */
  2049. newSize = size - bytetoskip;
  2050. /* Get the number of bit per operand */
  2051. hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(newSize, pOp1[bytetoskip]);
  2052. /* Move the input parameters pOp1 to PKA RAM */
  2053. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MODULUS], pOp1, size);
  2054. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MONTGOMERY_PARAM_IN_MODULUS + ((size + 3UL) / 4UL));
  2055. }
  2056. }
  2057. /**
  2058. * @brief Generic function to set input parameters.
  2059. * @param hpka PKA handle
  2060. * @param size Size of the operand
  2061. * @param pOp1 Generic pointer to input data
  2062. * @param pOp2 Generic pointer to input data
  2063. * @param pOp3 Generic pointer to input data
  2064. */
  2065. void PKA_ARI_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint32_t *pOp1, const uint32_t *pOp2,
  2066. const uint8_t *pOp3)
  2067. {
  2068. /* Get the number of bit per operand */
  2069. hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_NB_BITS] = PKA_GetBitSize_u32(size);
  2070. if (pOp1 != NULL)
  2071. {
  2072. /* Move the input parameters pOp1 to PKA RAM */
  2073. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP1], pOp1, size);
  2074. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP1 + size);
  2075. }
  2076. if (pOp2 != NULL)
  2077. {
  2078. /* Move the input parameters pOp2 to PKA RAM */
  2079. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP2], pOp2, size);
  2080. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP2 + size);
  2081. }
  2082. if (pOp3 != NULL)
  2083. {
  2084. /* Move the input parameters pOp3 to PKA RAM */
  2085. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP3], pOp3, size * 4UL);
  2086. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP3 + size);
  2087. }
  2088. }
  2089. /**
  2090. * @}
  2091. */
  2092. /**
  2093. * @}
  2094. */
  2095. #endif /* defined(PKA) && defined(HAL_PKA_MODULE_ENABLED) */
  2096. /**
  2097. * @}
  2098. */