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