1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_hal_pka.c
4   * @author  MCD Application Team
5   * @brief   PKA HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of public key accelerator(PKA):
8   *           + Initialization and de-initialization functions
9   *           + Start an operation
10   *           + Retrieve the operation result
11   *
12   ******************************************************************************
13   * @attention
14   *
15   * Copyright (c) 2017 STMicroelectronics.
16   * All rights reserved.
17   *
18   * This software is licensed under terms that can be found in the LICENSE file
19   * in the root directory of this software component.
20   * If no LICENSE file comes with this software, it is provided AS-IS.
21   *
22   ******************************************************************************
23   @verbatim
24   ==============================================================================
25                         ##### How to use this driver #####
26   ==============================================================================
27     [..]
28     The PKA HAL driver can be used as follows:
29 
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 "stm32l4xx_hal.h"
247 
248 /** @addtogroup STM32L4xx_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   position = 32UL - __CLZ(msb);
1805 
1806   return (((byteNumber - 1UL) * 8UL) + position);
1807 }
1808 
1809 /**
1810   * @brief  Get number of bits inside an array of u32.
1811   * @param  wordNumber Number of u32 inside the array
1812   */
PKA_GetBitSize_u32(uint32_t wordNumber)1813 uint32_t PKA_GetBitSize_u32(uint32_t wordNumber)
1814 {
1815   /* Convert from number of uint32_t in an array to the associated number of bits in this array */
1816   return wordNumber * 32UL;
1817 }
1818 
1819 /**
1820   * @brief  Get number of uint8_t element in an array of bitSize bits.
1821   * @param  bitSize Number of bits in an array
1822   */
PKA_GetArraySize_u8(uint32_t bitSize)1823 uint32_t PKA_GetArraySize_u8(uint32_t bitSize)
1824 {
1825   /* Manage the non aligned on uint8_t bitsize: */
1826   /*   512 bits requires 64 uint8_t             */
1827   /*   521 bits requires 66 uint8_t             */
1828   return ((bitSize + 7UL) / 8UL);
1829 }
1830 
1831 /**
1832   * @brief  Copy uint32_t array to uint8_t array to fit PKA number representation.
1833   * @param  dst Pointer to destination
1834   * @param  src Pointer to source
1835   * @param  n Number of uint8_t to copy
1836   * @retval dst
1837   */
PKA_Memcpy_u32_to_u8(uint8_t dst[],__IO const uint32_t src[],size_t n)1838 void PKA_Memcpy_u32_to_u8(uint8_t dst[], __IO const uint32_t src[], size_t n)
1839 {
1840   if (dst != NULL)
1841   {
1842     if (src != NULL)
1843     {
1844       uint32_t index_uint32_t = 0UL; /* This index is used outside of the loop */
1845 
1846       for (; index_uint32_t < (n / 4UL); index_uint32_t++)
1847       {
1848         /* Avoid casting from uint8_t* to uint32_t* by copying 4 uint8_t in a row */
1849         /* Apply __REV equivalent */
1850         uint32_t index_uint8_t = n - 4UL - (index_uint32_t * 4UL);
1851         dst[index_uint8_t + 3UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
1852         dst[index_uint8_t + 2UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
1853         dst[index_uint8_t + 1UL] = (uint8_t)((src[index_uint32_t] & 0x00FF0000U) >> 16UL);
1854         dst[index_uint8_t + 0UL] = (uint8_t)((src[index_uint32_t] & 0xFF000000U) >> 24UL);
1855       }
1856 
1857       /* Manage the buffers not aligned on uint32_t */
1858       if ((n % 4UL) == 1UL)
1859       {
1860         dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
1861       }
1862       else if ((n % 4UL) == 2UL)
1863       {
1864         dst[1UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
1865         dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
1866       }
1867       else if ((n % 4UL) == 3UL)
1868       {
1869         dst[2UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
1870         dst[1UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
1871         dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x00FF0000U) >> 16UL);
1872       }
1873       else
1874       {
1875         /* The last element is already handle in the loop */
1876       }
1877     }
1878   }
1879 }
1880 
1881 /**
1882   * @brief  Copy uint8_t array to uint32_t array to fit PKA number representation.
1883   * @param  dst Pointer to destination
1884   * @param  src Pointer to source
1885   * @param  n Number of uint8_t to copy (must be multiple of 4)
1886   * @retval dst
1887   */
PKA_Memcpy_u8_to_u32(__IO uint32_t dst[],const uint8_t src[],size_t n)1888 void PKA_Memcpy_u8_to_u32(__IO uint32_t dst[], const uint8_t src[], size_t n)
1889 {
1890   if (dst != NULL)
1891   {
1892     if (src != NULL)
1893     {
1894       uint32_t index = 0UL; /* This index is used outside of the loop */
1895 
1896       for (; index < (n / 4UL); index++)
1897       {
1898         /* Apply the equivalent of __REV from uint8_t to uint32_t */
1899         dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
1900                      | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL) \
1901                      | ((uint32_t)src[(n - (index * 4UL) - 3UL)] << 16UL) \
1902                      | ((uint32_t)src[(n - (index * 4UL) - 4UL)] << 24UL);
1903       }
1904 
1905       /* Manage the buffers not aligned on uint32_t */
1906       if ((n % 4UL) == 1UL)
1907       {
1908         dst[index] = (uint32_t)src[(n - (index * 4UL) - 1UL)];
1909       }
1910       else if ((n % 4UL) == 2UL)
1911       {
1912         dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
1913                      | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL);
1914       }
1915       else if ((n % 4UL) == 3UL)
1916       {
1917         dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
1918                      | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL) \
1919                      | ((uint32_t)src[(n - (index * 4UL) - 3UL)] << 16UL);
1920       }
1921       else
1922       {
1923         /* The last element is already handle in the loop */
1924       }
1925     }
1926   }
1927 }
1928 
1929 /**
1930   * @brief  Copy uint32_t array to uint32_t array.
1931   * @param  dst Pointer to destination
1932   * @param  src Pointer to source
1933   * @param  n Number of u32 to be handled
1934   * @retval dst
1935   */
PKA_Memcpy_u32_to_u32(__IO uint32_t dst[],__IO const uint32_t src[],size_t n)1936 void PKA_Memcpy_u32_to_u32(__IO uint32_t dst[], __IO const uint32_t src[], size_t n)
1937 {
1938   /* If a destination buffer is provided */
1939   if (dst != NULL)
1940   {
1941     /* If a source buffer is provided */
1942     if (src != NULL)
1943     {
1944       /* For each element in the array */
1945       for (uint32_t index = 0UL; index < n; index++)
1946       {
1947         /* Copy the content */
1948         dst[index] = src[index];
1949       }
1950     }
1951   }
1952 }
1953 
1954 /**
1955   * @brief  Generic function to start a PKA operation in blocking mode.
1956   * @param  hpka PKA handle
1957   * @param  mode PKA operation
1958   * @param  Timeout Timeout duration
1959   * @retval HAL status
1960   */
PKA_Process(PKA_HandleTypeDef * hpka,uint32_t mode,uint32_t Timeout)1961 HAL_StatusTypeDef PKA_Process(PKA_HandleTypeDef *hpka, uint32_t mode, uint32_t Timeout)
1962 {
1963   HAL_StatusTypeDef err = HAL_OK;
1964   uint32_t tickstart;
1965 
1966   if (hpka->State == HAL_PKA_STATE_READY)
1967   {
1968     /* Set the state to busy */
1969     hpka->State = HAL_PKA_STATE_BUSY;
1970 
1971     /* Clear any pending error */
1972     hpka->ErrorCode = HAL_PKA_ERROR_NONE;
1973 
1974     /* Init tickstart for timeout management*/
1975     tickstart = HAL_GetTick();
1976 
1977     /* Set the mode and deactivate the interrupts */
1978     MODIFY_REG(hpka->Instance->CR, PKA_CR_MODE | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE,
1979                mode << PKA_CR_MODE_Pos);
1980 
1981     /* Start the computation */
1982     hpka->Instance->CR |= PKA_CR_START;
1983 
1984     /* Wait for the end of operation or timeout */
1985     if (PKA_PollEndOfOperation(hpka, Timeout, tickstart) != HAL_OK)
1986     {
1987       /* Abort any ongoing operation */
1988       CLEAR_BIT(hpka->Instance->CR, PKA_CR_EN);
1989 
1990       hpka->ErrorCode |= HAL_PKA_ERROR_TIMEOUT;
1991 
1992       /* Make ready for the next operation */
1993       SET_BIT(hpka->Instance->CR, PKA_CR_EN);
1994     }
1995 
1996     /* Check error */
1997     hpka->ErrorCode |= PKA_CheckError(hpka, mode);
1998 
1999     /* Clear all flags */
2000     hpka->Instance->CLRFR |= (PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
2001 
2002     /* Set the state to ready */
2003     hpka->State = HAL_PKA_STATE_READY;
2004 
2005     /* Manage the result based on encountered errors */
2006     if (hpka->ErrorCode != HAL_PKA_ERROR_NONE)
2007     {
2008       err = HAL_ERROR;
2009     }
2010   }
2011   else
2012   {
2013     err = HAL_ERROR;
2014   }
2015   return err;
2016 }
2017 
2018 /**
2019   * @brief  Generic function to start a PKA operation in non-blocking mode with Interrupt.
2020   * @param  hpka PKA handle
2021   * @param  mode PKA operation
2022   * @retval HAL status
2023   */
PKA_Process_IT(PKA_HandleTypeDef * hpka,uint32_t mode)2024 HAL_StatusTypeDef PKA_Process_IT(PKA_HandleTypeDef *hpka, uint32_t mode)
2025 {
2026   HAL_StatusTypeDef err = HAL_OK;
2027 
2028   if (hpka->State == HAL_PKA_STATE_READY)
2029   {
2030     /* Set the state to busy */
2031     hpka->State = HAL_PKA_STATE_BUSY;
2032 
2033     /* Clear any pending error */
2034     hpka->ErrorCode = HAL_PKA_ERROR_NONE;
2035 
2036     /* Set the mode and activate interrupts */
2037     MODIFY_REG(hpka->Instance->CR, PKA_CR_MODE | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE,
2038                (mode << PKA_CR_MODE_Pos) | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE);
2039 
2040     /* Start the computation */
2041     hpka->Instance->CR |= PKA_CR_START;
2042   }
2043   else
2044   {
2045     err = HAL_ERROR;
2046   }
2047   return err;
2048 }
2049 
2050 /**
2051   * @brief  Set input parameters.
2052   * @param  hpka PKA handle
2053   * @param  in Input information
2054   */
PKA_ModExp_Set(PKA_HandleTypeDef * hpka,PKA_ModExpInTypeDef * in)2055 void PKA_ModExp_Set(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in)
2056 {
2057   /* Get the number of bit per operand */
2058   hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = PKA_GetBitSize_u8(in->OpSize);
2059 
2060   /* Get the number of bit of the exponent */
2061   hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = PKA_GetBitSize_u8(in->expSize);
2062 
2063   /* Move the input parameters pOp1 to PKA RAM */
2064   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT_BASE], in->pOp1, in->OpSize);
2065   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT_BASE + ((in->OpSize + 3UL) / 4UL));
2066 
2067   /* Move the exponent to PKA RAM */
2068   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT], in->pExp, in->expSize);
2069   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT + ((in->expSize + 3UL) / 4UL));
2070 
2071   /* Move the modulus to PKA RAM */
2072   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MODULUS], in->pMod, in->OpSize);
2073   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MODULUS + ((in->OpSize + 3UL) / 4UL));
2074 }
2075 
2076 /**
2077   * @brief  Set input parameters.
2078   * @param  hpka PKA handle
2079   * @param  in Input information
2080   */
PKA_ModExpFastMode_Set(PKA_HandleTypeDef * hpka,PKA_ModExpFastModeInTypeDef * in)2081 void PKA_ModExpFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in)
2082 {
2083   /* Get the number of bit per operand */
2084   hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = PKA_GetBitSize_u8(in->OpSize);
2085 
2086   /* Get the number of bit of the exponent */
2087   hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = PKA_GetBitSize_u8(in->expSize);
2088 
2089   /* Move the input parameters pOp1 to PKA RAM */
2090   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT_BASE], in->pOp1, in->OpSize);
2091   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT_BASE + (in->OpSize / 4UL));
2092 
2093   /* Move the exponent to PKA RAM */
2094   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT], in->pExp, in->expSize);
2095   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT + (in->expSize / 4UL));
2096 
2097   /* Move the modulus to PKA RAM */
2098   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MODULUS], in->pMod, in->OpSize);
2099   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MODULUS + (in->OpSize / 4UL));
2100 
2101   /* Move the Montgomery parameter to PKA RAM */
2102   PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MONTGOMERY_PARAM], in->pMontgomeryParam,
2103                         in->OpSize / 4UL);
2104   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MONTGOMERY_PARAM + (in->OpSize / 4UL));
2105 }
2106 
2107 
2108 /**
2109   * @brief  Set input parameters.
2110   * @param  hpka PKA handle
2111   * @param  in Input information
2112   */
PKA_ECDSASign_Set(PKA_HandleTypeDef * hpka,PKA_ECDSASignInTypeDef * in)2113 void PKA_ECDSASign_Set(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in)
2114 {
2115   /* Get the prime order n length */
2116   hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS] = PKA_GetOptBitSize_u8(in->primeOrderSize, *(in->primeOrder));
2117 
2118   /* Get the modulus p length */
2119   hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2120 
2121   /* Get the coefficient a sign */
2122   hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN] = in->coefSign;
2123 
2124   /* Move the input parameters coefficient |a| to PKA RAM */
2125   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_A_COEFF], in->coef, in->modulusSize);
2126   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2127 
2128   /* Move the input parameters modulus value p to PKA RAM */
2129   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_GF], in->modulus, in->modulusSize);
2130   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2131 
2132   /* Move the input parameters integer k to PKA RAM */
2133   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_K], in->integer, in->primeOrderSize);
2134   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_K + ((in->primeOrderSize + 3UL) / 4UL));
2135 
2136   /* Move the input parameters base point G coordinate x to PKA RAM */
2137   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_INITIAL_POINT_X], in->basePointX, in->modulusSize);
2138   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2139 
2140   /* Move the input parameters base point G coordinate y to PKA RAM */
2141   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y], in->basePointY, in->modulusSize);
2142   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2143 
2144   /* Move the input parameters hash of message z to PKA RAM */
2145   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_HASH_E], in->hash, in->primeOrderSize);
2146   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_HASH_E + ((in->primeOrderSize + 3UL) / 4UL));
2147 
2148   /* Move the input parameters private key d to PKA RAM */
2149   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D], in->privateKey, in->primeOrderSize);
2150   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D + ((in->primeOrderSize + 3UL) / 4UL));
2151 
2152   /* Move the input parameters prime order n to PKA RAM */
2153   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_ORDER_N], in->primeOrder, in->primeOrderSize);
2154   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_ORDER_N + ((in->primeOrderSize + 3UL) / 4UL));
2155 }
2156 
2157 /**
2158   * @brief  Set input parameters.
2159   * @param  hpka PKA handle
2160   * @param  in Input information
2161   */
PKA_ECDSAVerif_Set(PKA_HandleTypeDef * hpka,PKA_ECDSAVerifInTypeDef * in)2162 void PKA_ECDSAVerif_Set(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in)
2163 {
2164   /* Get the prime order n length */
2165   hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS] = PKA_GetOptBitSize_u8(in->primeOrderSize, *(in->primeOrder));
2166 
2167   /* Get the modulus p length */
2168   hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2169 
2170   /* Get the coefficient a sign */
2171   hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN] = in->coefSign;
2172 
2173   /* Move the input parameters coefficient |a| to PKA RAM */
2174   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_A_COEFF], in->coef, in->modulusSize);
2175   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2176 
2177   /* Move the input parameters modulus value p to PKA RAM */
2178   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_MOD_GF], in->modulus, in->modulusSize);
2179   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2180 
2181   /* Move the input parameters base point G coordinate x to PKA RAM */
2182   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_INITIAL_POINT_X], in->basePointX, in->modulusSize);
2183   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2184 
2185   /* Move the input parameters base point G coordinate y to PKA RAM */
2186   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y], in->basePointY, in->modulusSize);
2187   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2188 
2189   /* Move the input parameters public-key curve point Q coordinate xQ to PKA RAM */
2190   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X], in->pPubKeyCurvePtX,
2191                        in->modulusSize);
2192   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2193 
2194   /* Move the input parameters public-key curve point Q coordinate xQ to PKA RAM */
2195   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y], in->pPubKeyCurvePtY,
2196                        in->modulusSize);
2197   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2198 
2199   /* Move the input parameters signature part r to PKA RAM */
2200   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_SIGNATURE_R], in->RSign, in->primeOrderSize);
2201   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_SIGNATURE_R + ((in->primeOrderSize + 3UL) / 4UL));
2202 
2203   /* Move the input parameters signature part s to PKA RAM */
2204   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_SIGNATURE_S], in->SSign, in->primeOrderSize);
2205   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_SIGNATURE_S + ((in->primeOrderSize + 3UL) / 4UL));
2206 
2207   /* Move the input parameters hash of message z to PKA RAM */
2208   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_HASH_E], in->hash, in->primeOrderSize);
2209   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_HASH_E + ((in->primeOrderSize + 3UL) / 4UL));
2210 
2211   /* Move the input parameters curve prime order n to PKA RAM */
2212   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_ORDER_N], in->primeOrder, in->primeOrderSize);
2213   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_ORDER_N + ((in->primeOrderSize + 3UL) / 4UL));
2214 }
2215 
2216 /**
2217   * @brief  Set input parameters.
2218   * @param  hpka PKA handle
2219   * @param  in Input information
2220   */
PKA_RSACRTExp_Set(PKA_HandleTypeDef * hpka,PKA_RSACRTExpInTypeDef * in)2221 void PKA_RSACRTExp_Set(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in)
2222 {
2223   /* Get the operand length M */
2224   hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] = PKA_GetBitSize_u8(in->size);
2225 
2226   /* Move the input parameters operand dP to PKA RAM */
2227   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_DP_CRT], in->pOpDp, in->size / 2UL);
2228   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_DP_CRT + (in->size / 8UL));
2229 
2230   /* Move the input parameters operand dQ to PKA RAM */
2231   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_DQ_CRT], in->pOpDq, in->size / 2UL);
2232   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_DQ_CRT + (in->size / 8UL));
2233 
2234   /* Move the input parameters operand qinv to PKA RAM */
2235   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_QINV_CRT], in->pOpQinv, in->size / 2UL);
2236   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_QINV_CRT + (in->size / 8UL));
2237 
2238   /* Move the input parameters prime p to PKA RAM */
2239   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_PRIME_P], in->pPrimeP, in->size / 2UL);
2240   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_PRIME_P + (in->size / 8UL));
2241 
2242   /* Move the input parameters prime q to PKA RAM */
2243   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_PRIME_Q], in->pPrimeQ, in->size / 2UL);
2244   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_PRIME_Q + (in->size / 8UL));
2245 
2246   /* Move the input parameters operand A to PKA RAM */
2247   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_EXPONENT_BASE], in->popA, in->size);
2248   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_EXPONENT_BASE + (in->size / 4UL));
2249 }
2250 
2251 /**
2252   * @brief  Set input parameters.
2253   * @param  hpka PKA handle
2254   * @param  in Input information
2255   */
PKA_PointCheck_Set(PKA_HandleTypeDef * hpka,PKA_PointCheckInTypeDef * in)2256 void PKA_PointCheck_Set(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in)
2257 {
2258   /* Get the modulus length */
2259   hpka->Instance->RAM[PKA_POINT_CHECK_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2260 
2261   /* Get the coefficient a sign */
2262   hpka->Instance->RAM[PKA_POINT_CHECK_IN_A_COEFF_SIGN] = in->coefSign;
2263 
2264   /* Move the input parameters coefficient |a| to PKA RAM */
2265   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_A_COEFF], in->coefA, in->modulusSize);
2266   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2267 
2268   /* Move the input parameters coefficient b to PKA RAM */
2269   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_B_COEFF], in->coefB, in->modulusSize);
2270   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_B_COEFF + ((in->modulusSize + 3UL) / 4UL));
2271 
2272   /* Move the input parameters modulus value p to PKA RAM */
2273   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_MOD_GF], in->modulus, in->modulusSize);
2274   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2275 
2276   /* Move the input parameters Point P coordinate x to PKA RAM */
2277   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
2278   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2279 
2280   /* Move the input parameters Point P coordinate y to PKA RAM */
2281   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
2282   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2283 }
2284 
2285 /**
2286   * @brief  Set input parameters.
2287   * @param  hpka PKA handle
2288   * @param  in Input information
2289   */
PKA_ECCMul_Set(PKA_HandleTypeDef * hpka,PKA_ECCMulInTypeDef * in)2290 void PKA_ECCMul_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in)
2291 {
2292   /* Get the scalar multiplier k length */
2293   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = PKA_GetOptBitSize_u8(in->scalarMulSize, *(in->scalarMul));
2294 
2295   /* Get the modulus length */
2296   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2297 
2298   /* Get the coefficient a sign */
2299   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = in->coefSign;
2300 
2301   /* Move the input parameters coefficient |a| to PKA RAM */
2302   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF], in->coefA, in->modulusSize);
2303   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2304 
2305 
2306   /* Move the input parameters modulus value p to PKA RAM */
2307   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MOD_GF], in->modulus, in->modulusSize);
2308   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2309 
2310   /* Move the input parameters scalar multiplier k to PKA RAM */
2311   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_K], in->scalarMul, in->scalarMulSize);
2312   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_K + ((in->scalarMulSize + 3UL) / 4UL));
2313 
2314   /* Move the input parameters Point P coordinate x to PKA RAM */
2315   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
2316   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2317 
2318   /* Move the input parameters Point P coordinate y to PKA RAM */
2319   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
2320   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2321 
2322 }
2323 
2324 
2325 /**
2326   * @brief  Set input parameters.
2327   * @param  hpka PKA handle
2328   * @param  in Input information
2329   */
PKA_ECCMulFastMode_Set(PKA_HandleTypeDef * hpka,PKA_ECCMulFastModeInTypeDef * in)2330 void PKA_ECCMulFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in)
2331 {
2332   /* Get the scalar multiplier k length */
2333   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = PKA_GetOptBitSize_u8(in->scalarMulSize, *(in->scalarMul));
2334 
2335   /* Get the modulus length */
2336   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2337 
2338   /* Get the coefficient a sign */
2339   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = in->coefSign;
2340 
2341   /* Move the input parameters coefficient |a| to PKA RAM */
2342   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF], in->coefA, in->modulusSize);
2343   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
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_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
2355   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_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_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
2359   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2360 
2361   /* Move the Montgomery parameter to PKA RAM */
2362   PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MONTGOMERY_PARAM], in->pMontgomeryParam,
2363                         (in->modulusSize + 3UL) / 4UL);
2364   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MONTGOMERY_PARAM + ((in->modulusSize + 3UL) / 4UL));
2365 }
2366 /**
2367   * @brief  Set input parameters.
2368   * @param  hpka PKA handle
2369   * @param  in Input information
2370   */
PKA_ModInv_Set(PKA_HandleTypeDef * hpka,PKA_ModInvInTypeDef * in)2371 void PKA_ModInv_Set(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in)
2372 {
2373   /* Get the number of bit per operand */
2374   hpka->Instance->RAM[PKA_MODULAR_INV_NB_BITS] = PKA_GetBitSize_u32(in->size);
2375 
2376   /* Move the input parameters operand A to PKA RAM */
2377   PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_INV_IN_OP1], in->pOp1, in->size);
2378   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_INV_IN_OP1 + in->size);
2379 
2380   /* Move the input parameters modulus value n to PKA RAM */
2381   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_INV_IN_OP2_MOD], in->pMod, in->size * 4UL);
2382   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_INV_IN_OP2_MOD + in->size);
2383 }
2384 
2385 /**
2386   * @brief  Set input parameters.
2387   * @param  hpka PKA handle
2388   * @param  in Input information
2389   */
PKA_ModRed_Set(PKA_HandleTypeDef * hpka,PKA_ModRedInTypeDef * in)2390 void PKA_ModRed_Set(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in)
2391 {
2392   /* Get the number of bit per operand */
2393   hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_OP_LENGTH] = PKA_GetBitSize_u32(in->OpSize);
2394 
2395   /* Get the number of bit per modulus */
2396   hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_MOD_LENGTH] = PKA_GetBitSize_u8(in->modSize);
2397 
2398   /* Move the input parameters operand A to PKA RAM */
2399   PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_OPERAND], in->pOp1, in->OpSize);
2400   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_REDUC_IN_OPERAND + in->OpSize);
2401 
2402   /* Move the input parameters modulus value n to PKA RAM */
2403   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_MODULUS], in->pMod, in->modSize);
2404   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_REDUC_IN_MODULUS + (in->modSize / 4UL));
2405 }
2406 
2407 /**
2408   * @brief  Set input parameters.
2409   * @param  hpka PKA handle
2410   * @param  size Size of the operand
2411   * @param  pOp1 Generic pointer to input data
2412   */
PKA_MontgomeryParam_Set(PKA_HandleTypeDef * hpka,const uint32_t size,const uint8_t * pOp1)2413 void PKA_MontgomeryParam_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint8_t *pOp1)
2414 {
2415   uint32_t bytetoskip = 0UL;
2416   uint32_t newSize;
2417 
2418   if (pOp1 != NULL)
2419   {
2420     /* Count the number of zero bytes */
2421     while ((bytetoskip < size) && (pOp1[bytetoskip] == 0UL))
2422     {
2423       bytetoskip++;
2424     }
2425 
2426     /* Get new size after skipping zero bytes */
2427     newSize = size - bytetoskip;
2428 
2429     /* Get the number of bit per operand */
2430     hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(newSize, pOp1[bytetoskip]);
2431 
2432     /* Move the input parameters pOp1 to PKA RAM */
2433     PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MODULUS], pOp1, size);
2434     __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MONTGOMERY_PARAM_IN_MODULUS + ((size + 3UL) / 4UL));
2435   }
2436 }
2437 
2438 /**
2439   * @brief  Generic function to set input parameters.
2440   * @param  hpka PKA handle
2441   * @param  size Size of the operand
2442   * @param  pOp1 Generic pointer to input data
2443   * @param  pOp2 Generic pointer to input data
2444   * @param  pOp3 Generic pointer to input data
2445   */
PKA_ARI_Set(PKA_HandleTypeDef * hpka,const uint32_t size,const uint32_t * pOp1,const uint32_t * pOp2,const uint8_t * pOp3)2446 void PKA_ARI_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint32_t *pOp1, const uint32_t *pOp2,
2447                  const uint8_t *pOp3)
2448 {
2449   /* Get the number of bit per operand */
2450   hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_NB_BITS] = PKA_GetBitSize_u32(size);
2451 
2452   if (pOp1 != NULL)
2453   {
2454     /* Move the input parameters pOp1 to PKA RAM */
2455     PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP1], pOp1, size);
2456     __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP1 + size);
2457   }
2458 
2459   if (pOp2 != NULL)
2460   {
2461     /* Move the input parameters pOp2 to PKA RAM */
2462     PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP2], pOp2, size);
2463     __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP2 + size);
2464   }
2465 
2466   if (pOp3 != NULL)
2467   {
2468     /* Move the input parameters pOp3 to PKA RAM */
2469     PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP3], pOp3, size * 4UL);
2470     __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP3 + size);
2471   }
2472 }
2473 
2474 /**
2475   * @}
2476   */
2477 
2478 /**
2479   * @}
2480   */
2481 
2482 #endif /* defined(PKA) && defined(HAL_PKA_MODULE_ENABLED) */
2483 
2484 /**
2485   * @}
2486   */
2487