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(const PKA_HandleTypeDef *hpka);
281 HAL_StatusTypeDef PKA_PollEndOfOperation(const PKA_HandleTypeDef *hpka, uint32_t Timeout, uint32_t Tickstart);
282 uint32_t PKA_CheckError(const 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   /* Start the operation */
791   return PKA_Process(hpka, PKA_MODE_MODULAR_EXP, Timeout);
792 }
793 
794 /**
795   * @brief  Modular exponentiation in non-blocking mode with Interrupt.
796   * @param  hpka PKA handle
797   * @param  in Input information
798   * @retval HAL status
799   */
HAL_PKA_ModExp_IT(PKA_HandleTypeDef * hpka,PKA_ModExpInTypeDef * in)800 HAL_StatusTypeDef HAL_PKA_ModExp_IT(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in)
801 {
802   /* Set input parameter in PKA RAM */
803   PKA_ModExp_Set(hpka, in);
804   /* Start the operation */
805   return PKA_Process_IT(hpka, PKA_MODE_MODULAR_EXP);
806 }
807 
808 /**
809   * @brief  Modular exponentiation in blocking mode.
810   * @param  hpka PKA handle
811   * @param  in Input information
812   * @param  Timeout Timeout duration
813   * @retval HAL status
814   */
HAL_PKA_ModExpFastMode(PKA_HandleTypeDef * hpka,PKA_ModExpFastModeInTypeDef * in,uint32_t Timeout)815 HAL_StatusTypeDef HAL_PKA_ModExpFastMode(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in, uint32_t Timeout)
816 {
817   /* Set input parameter in PKA RAM */
818   PKA_ModExpFastMode_Set(hpka, in);
819   /* Start the operation */
820   return PKA_Process(hpka, PKA_MODE_MODULAR_EXP_FAST_MODE, Timeout);
821 }
822 
823 /**
824   * @brief  Modular exponentiation in non-blocking mode with Interrupt.
825   * @param  hpka PKA handle
826   * @param  in Input information
827   * @retval HAL status
828   */
HAL_PKA_ModExpFastMode_IT(PKA_HandleTypeDef * hpka,PKA_ModExpFastModeInTypeDef * in)829 HAL_StatusTypeDef HAL_PKA_ModExpFastMode_IT(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in)
830 {
831   /* Set input parameter in PKA RAM */
832   PKA_ModExpFastMode_Set(hpka, in);
833   /* Start the operation */
834   return PKA_Process_IT(hpka, PKA_MODE_MODULAR_EXP_FAST_MODE);
835 }
836 
837 
838 /**
839   * @brief  Retrieve operation result.
840   * @param  hpka PKA handle
841   * @param  pRes Output buffer
842   * @retval HAL status
843   */
HAL_PKA_ModExp_GetResult(PKA_HandleTypeDef * hpka,uint8_t * pRes)844 void HAL_PKA_ModExp_GetResult(PKA_HandleTypeDef *hpka, uint8_t *pRes)
845 {
846   uint32_t size;
847 
848   /* Indicate to the user the final size */
849   size = (hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] + 7UL) / 8UL;
850 
851   /* Move the result to appropriate location (indicated in out parameter) */
852   PKA_Memcpy_u32_to_u8(pRes, &hpka->Instance->RAM[PKA_MODULAR_EXP_OUT_SM_ALGO_ACC1], size);
853 }
854 
855 /**
856   * @brief  Sign a message using elliptic curves over prime fields in blocking mode.
857   * @param  hpka PKA handle
858   * @param  in Input information
859   * @param  Timeout Timeout duration
860   * @retval HAL status
861   */
HAL_PKA_ECDSASign(PKA_HandleTypeDef * hpka,PKA_ECDSASignInTypeDef * in,uint32_t Timeout)862 HAL_StatusTypeDef HAL_PKA_ECDSASign(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in, uint32_t Timeout)
863 {
864   /* Set input parameter in PKA RAM */
865   PKA_ECDSASign_Set(hpka, in);
866   /* Start the operation */
867   return PKA_Process(hpka, PKA_MODE_ECDSA_SIGNATURE, Timeout);
868 }
869 
870 /**
871   * @brief  Sign a message using elliptic curves over prime fields in non-blocking mode with Interrupt.
872   * @param  hpka PKA handle
873   * @param  in Input information
874   * @retval HAL status
875   */
HAL_PKA_ECDSASign_IT(PKA_HandleTypeDef * hpka,PKA_ECDSASignInTypeDef * in)876 HAL_StatusTypeDef HAL_PKA_ECDSASign_IT(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in)
877 {
878   /* Set input parameter in PKA RAM */
879   PKA_ECDSASign_Set(hpka, in);
880   /* Start the operation */
881   return PKA_Process_IT(hpka, PKA_MODE_ECDSA_SIGNATURE);
882 }
883 
884 /**
885   * @brief  Retrieve operation result.
886   * @param  hpka PKA handle
887   * @param  out Output information
888   * @param  outExt Additional Output information (facultative)
889   */
HAL_PKA_ECDSASign_GetResult(PKA_HandleTypeDef * hpka,PKA_ECDSASignOutTypeDef * out,PKA_ECDSASignOutExtParamTypeDef * outExt)890 void HAL_PKA_ECDSASign_GetResult(PKA_HandleTypeDef *hpka, PKA_ECDSASignOutTypeDef *out,
891                                  PKA_ECDSASignOutExtParamTypeDef *outExt)
892 {
893   uint32_t size;
894 
895   size = (hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] + 7UL) / 8UL;
896 
897   if (out != NULL)
898   {
899     PKA_Memcpy_u32_to_u8(out->RSign, &hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_SIGNATURE_R], size);
900     PKA_Memcpy_u32_to_u8(out->SSign, &hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_SIGNATURE_S], size);
901   }
902 
903   /* If user requires the additional information */
904   if (outExt != NULL)
905   {
906     /* Move the result to appropriate location (indicated in outExt parameter) */
907     PKA_Memcpy_u32_to_u8(outExt->ptX, &hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_FINAL_POINT_X], size);
908     PKA_Memcpy_u32_to_u8(outExt->ptY, &hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_FINAL_POINT_Y], size);
909   }
910 }
911 
912 /**
913   * @brief  Verify the validity of a signature using elliptic curves over prime fields in blocking mode.
914   * @param  hpka PKA handle
915   * @param  in Input information
916   * @param  Timeout Timeout duration
917   * @retval HAL status
918   */
HAL_PKA_ECDSAVerif(PKA_HandleTypeDef * hpka,PKA_ECDSAVerifInTypeDef * in,uint32_t Timeout)919 HAL_StatusTypeDef HAL_PKA_ECDSAVerif(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in, uint32_t Timeout)
920 {
921   /* Set input parameter in PKA RAM */
922   PKA_ECDSAVerif_Set(hpka, in);
923 
924   /* Start the operation */
925   return PKA_Process(hpka, PKA_MODE_ECDSA_VERIFICATION, Timeout);
926 }
927 
928 /**
929   * @brief  Verify the validity of a signature using elliptic curves
930   *         over prime fields in non-blocking mode with Interrupt.
931   * @param  hpka PKA handle
932   * @param  in Input information
933   * @retval HAL status
934   */
HAL_PKA_ECDSAVerif_IT(PKA_HandleTypeDef * hpka,PKA_ECDSAVerifInTypeDef * in)935 HAL_StatusTypeDef HAL_PKA_ECDSAVerif_IT(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in)
936 {
937   /* Set input parameter in PKA RAM */
938   PKA_ECDSAVerif_Set(hpka, in);
939 
940   /* Start the operation */
941   return PKA_Process_IT(hpka, PKA_MODE_ECDSA_VERIFICATION);
942 }
943 
944 /**
945   * @brief  Return the result of the ECDSA verification operation.
946   * @param  hpka PKA handle
947   * @retval 1 if signature is verified, 0 in other case
948   */
HAL_PKA_ECDSAVerif_IsValidSignature(PKA_HandleTypeDef const * const hpka)949 uint32_t HAL_PKA_ECDSAVerif_IsValidSignature(PKA_HandleTypeDef const *const hpka)
950 {
951   /* Invert the state of the PKA RAM bit containing the result of the operation */
952   return (hpka->Instance->RAM[PKA_ECDSA_VERIF_OUT_RESULT] == 0UL) ? 1UL : 0UL;
953 }
954 
955 /**
956   * @brief  RSA CRT exponentiation in blocking mode.
957   * @param  hpka PKA handle
958   * @param  in Input information
959   * @param  Timeout Timeout duration
960   * @retval HAL status
961   */
HAL_PKA_RSACRTExp(PKA_HandleTypeDef * hpka,PKA_RSACRTExpInTypeDef * in,uint32_t Timeout)962 HAL_StatusTypeDef HAL_PKA_RSACRTExp(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in, uint32_t Timeout)
963 {
964   /* Set input parameter in PKA RAM */
965   PKA_RSACRTExp_Set(hpka, in);
966 
967   /* Start the operation */
968   return PKA_Process(hpka, PKA_MODE_RSA_CRT_EXP, Timeout);
969 }
970 
971 /**
972   * @brief  RSA CRT exponentiation in non-blocking mode with Interrupt.
973   * @param  hpka PKA handle
974   * @param  in Input information
975   * @retval HAL status
976   */
HAL_PKA_RSACRTExp_IT(PKA_HandleTypeDef * hpka,PKA_RSACRTExpInTypeDef * in)977 HAL_StatusTypeDef HAL_PKA_RSACRTExp_IT(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in)
978 {
979   /* Set input parameter in PKA RAM */
980   PKA_RSACRTExp_Set(hpka, in);
981 
982   /* Start the operation */
983   return PKA_Process_IT(hpka, PKA_MODE_RSA_CRT_EXP);
984 }
985 
986 /**
987   * @brief  Retrieve operation result.
988   * @param  hpka PKA handle
989   * @param  pRes Pointer to memory location to receive the result of the operation
990   * @retval HAL status
991   */
HAL_PKA_RSACRTExp_GetResult(PKA_HandleTypeDef * hpka,uint8_t * pRes)992 void HAL_PKA_RSACRTExp_GetResult(PKA_HandleTypeDef *hpka, uint8_t *pRes)
993 {
994   uint32_t size;
995 
996   /* Move the result to appropriate location (indicated in out parameter) */
997   size = (hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] + 7UL) / 8UL;
998 
999   PKA_Memcpy_u32_to_u8(pRes, &hpka->Instance->RAM[PKA_RSA_CRT_EXP_OUT_RESULT], size);
1000 }
1001 
1002 /**
1003   * @brief  Point on elliptic curve check in blocking mode.
1004   * @param  hpka PKA handle
1005   * @param  in Input information
1006   * @param  Timeout Timeout duration
1007   * @retval HAL status
1008   */
HAL_PKA_PointCheck(PKA_HandleTypeDef * hpka,PKA_PointCheckInTypeDef * in,uint32_t Timeout)1009 HAL_StatusTypeDef HAL_PKA_PointCheck(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in, uint32_t Timeout)
1010 {
1011   /* Set input parameter in PKA RAM */
1012   PKA_PointCheck_Set(hpka, in);
1013 
1014   /* Start the operation */
1015   return PKA_Process(hpka, PKA_MODE_POINT_CHECK, Timeout);
1016 }
1017 
1018 /**
1019   * @brief  Point on elliptic curve check in non-blocking mode with Interrupt.
1020   * @param  hpka PKA handle
1021   * @param  in Input information
1022   * @retval HAL status
1023   */
HAL_PKA_PointCheck_IT(PKA_HandleTypeDef * hpka,PKA_PointCheckInTypeDef * in)1024 HAL_StatusTypeDef HAL_PKA_PointCheck_IT(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in)
1025 {
1026   /* Set input parameter in PKA RAM */
1027   PKA_PointCheck_Set(hpka, in);
1028 
1029   /* Start the operation */
1030   return PKA_Process_IT(hpka, PKA_MODE_POINT_CHECK);
1031 }
1032 
1033 /**
1034   * @brief  Return the result of the point check operation.
1035   * @param  hpka PKA handle
1036   * @retval 1 if point is on curve, 0 in other case
1037   */
HAL_PKA_PointCheck_IsOnCurve(PKA_HandleTypeDef const * const hpka)1038 uint32_t HAL_PKA_PointCheck_IsOnCurve(PKA_HandleTypeDef const *const hpka)
1039 {
1040 #define PKA_POINT_IS_ON_CURVE 0UL
1041   /* Invert the value of the PKA RAM containing the result of the operation */
1042   return (hpka->Instance->RAM[PKA_POINT_CHECK_OUT_ERROR] == PKA_POINT_IS_ON_CURVE) ? 1UL : 0UL;
1043 }
1044 
1045 /**
1046   * @brief  ECC scalar multiplication in blocking mode.
1047   * @param  hpka PKA handle
1048   * @param  in Input information
1049   * @param  Timeout Timeout duration
1050   * @retval HAL status
1051   */
HAL_PKA_ECCMul(PKA_HandleTypeDef * hpka,PKA_ECCMulInTypeDef * in,uint32_t Timeout)1052 HAL_StatusTypeDef HAL_PKA_ECCMul(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in, uint32_t Timeout)
1053 {
1054   /* Set input parameter in PKA RAM */
1055   PKA_ECCMul_Set(hpka, in);
1056   /* Start the operation */
1057   return PKA_Process(hpka, PKA_MODE_ECC_MUL, Timeout);
1058 }
1059 
1060 /**
1061   * @brief  ECC scalar multiplication in non-blocking mode with Interrupt.
1062   * @param  hpka PKA handle
1063   * @param  in Input information
1064   * @retval HAL status
1065   */
HAL_PKA_ECCMul_IT(PKA_HandleTypeDef * hpka,PKA_ECCMulInTypeDef * in)1066 HAL_StatusTypeDef HAL_PKA_ECCMul_IT(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in)
1067 {
1068   /* Set input parameter in PKA RAM */
1069   PKA_ECCMul_Set(hpka, in);
1070   /* Start the operation */
1071   return PKA_Process_IT(hpka, PKA_MODE_ECC_MUL);
1072 }
1073 /**
1074   * @brief  ECC scalar multiplication in blocking mode.
1075   * @param  hpka PKA handle
1076   * @param  in Input information
1077   * @param  Timeout Timeout duration
1078   * @retval HAL status
1079   */
HAL_PKA_ECCMulFastMode(PKA_HandleTypeDef * hpka,PKA_ECCMulFastModeInTypeDef * in,uint32_t Timeout)1080 HAL_StatusTypeDef HAL_PKA_ECCMulFastMode(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in, uint32_t Timeout)
1081 {
1082   /* Set input parameter in PKA RAM */
1083   PKA_ECCMulFastMode_Set(hpka, in);
1084 
1085   /* Start the operation */
1086   return PKA_Process(hpka, PKA_MODE_ECC_MUL_FAST_MODE, Timeout);
1087 }
1088 
1089 /**
1090   * @brief  ECC scalar multiplication in non-blocking mode with Interrupt.
1091   * @param  hpka PKA handle
1092   * @param  in Input information
1093   * @retval HAL status
1094   */
HAL_PKA_ECCMulFastMode_IT(PKA_HandleTypeDef * hpka,PKA_ECCMulFastModeInTypeDef * in)1095 HAL_StatusTypeDef HAL_PKA_ECCMulFastMode_IT(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in)
1096 {
1097   /* Set input parameter in PKA RAM */
1098   PKA_ECCMulFastMode_Set(hpka, in);
1099 
1100   /* Start the operation */
1101   return PKA_Process_IT(hpka, PKA_MODE_ECC_MUL_FAST_MODE);
1102 }
1103 /**
1104   * @brief  Retrieve operation result.
1105   * @param  hpka PKA handle
1106   * @param  out Output information
1107   * @retval HAL status
1108   */
HAL_PKA_ECCMul_GetResult(PKA_HandleTypeDef * hpka,PKA_ECCMulOutTypeDef * out)1109 void HAL_PKA_ECCMul_GetResult(PKA_HandleTypeDef *hpka, PKA_ECCMulOutTypeDef *out)
1110 {
1111   uint32_t size;
1112 
1113   /* Retrieve the size of the array from the PKA RAM */
1114   size = (hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] + 7UL) / 8UL;
1115 
1116   /* If a destination buffer is provided */
1117   if (out != NULL)
1118   {
1119     /* Move the result to appropriate location (indicated in out parameter) */
1120     PKA_Memcpy_u32_to_u8(out->ptX, &hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_OUT_RESULT_X], size);
1121     PKA_Memcpy_u32_to_u8(out->ptY, &hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_OUT_RESULT_Y], size);
1122   }
1123 }
1124 
1125 /**
1126   * @brief  Arithmetic addition in blocking mode.
1127   * @param  hpka PKA handle
1128   * @param  in Input information
1129   * @param  Timeout Timeout duration
1130   * @retval HAL status
1131   */
HAL_PKA_Add(PKA_HandleTypeDef * hpka,PKA_AddInTypeDef * in,uint32_t Timeout)1132 HAL_StatusTypeDef HAL_PKA_Add(PKA_HandleTypeDef *hpka, PKA_AddInTypeDef *in, uint32_t Timeout)
1133 {
1134   /* Set input parameter in PKA RAM */
1135   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1136 
1137   /* Start the operation */
1138   return PKA_Process(hpka, PKA_MODE_ARITHMETIC_ADD, Timeout);
1139 }
1140 
1141 /**
1142   * @brief  Arithmetic addition in non-blocking mode with Interrupt.
1143   * @param  hpka PKA handle
1144   * @param  in Input information
1145   * @retval HAL status
1146   */
HAL_PKA_Add_IT(PKA_HandleTypeDef * hpka,PKA_AddInTypeDef * in)1147 HAL_StatusTypeDef HAL_PKA_Add_IT(PKA_HandleTypeDef *hpka, PKA_AddInTypeDef *in)
1148 {
1149   /* Set input parameter in PKA RAM */
1150   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1151 
1152   /* Start the operation */
1153   return PKA_Process_IT(hpka, PKA_MODE_ARITHMETIC_ADD);
1154 }
1155 
1156 /**
1157   * @brief  Arithmetic subtraction in blocking mode.
1158   * @param  hpka PKA handle
1159   * @param  in Input information
1160   * @param  Timeout Timeout duration
1161   * @retval HAL status
1162   */
HAL_PKA_Sub(PKA_HandleTypeDef * hpka,PKA_SubInTypeDef * in,uint32_t Timeout)1163 HAL_StatusTypeDef HAL_PKA_Sub(PKA_HandleTypeDef *hpka, PKA_SubInTypeDef *in, uint32_t Timeout)
1164 {
1165   /* Set input parameter in PKA RAM */
1166   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1167 
1168   /* Start the operation */
1169   return PKA_Process(hpka, PKA_MODE_ARITHMETIC_SUB, Timeout);
1170 }
1171 
1172 /**
1173   * @brief  Arithmetic subtraction in non-blocking mode with Interrupt.
1174   * @param  hpka PKA handle
1175   * @param  in Input information
1176   * @retval HAL status
1177   */
HAL_PKA_Sub_IT(PKA_HandleTypeDef * hpka,PKA_SubInTypeDef * in)1178 HAL_StatusTypeDef HAL_PKA_Sub_IT(PKA_HandleTypeDef *hpka, PKA_SubInTypeDef *in)
1179 {
1180   /* Set input parameter in PKA RAM */
1181   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1182 
1183   /* Start the operation */
1184   return PKA_Process_IT(hpka, PKA_MODE_ARITHMETIC_SUB);
1185 }
1186 
1187 /**
1188   * @brief  Arithmetic multiplication in blocking mode.
1189   * @param  hpka PKA handle
1190   * @param  in Input information
1191   * @param  Timeout Timeout duration
1192   * @retval HAL status
1193   */
HAL_PKA_Mul(PKA_HandleTypeDef * hpka,PKA_MulInTypeDef * in,uint32_t Timeout)1194 HAL_StatusTypeDef HAL_PKA_Mul(PKA_HandleTypeDef *hpka, PKA_MulInTypeDef *in, uint32_t Timeout)
1195 {
1196   /* Set input parameter in PKA RAM */
1197   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1198 
1199   /* Start the operation */
1200   return PKA_Process(hpka, PKA_MODE_ARITHMETIC_MUL, Timeout);
1201 }
1202 
1203 /**
1204   * @brief  Arithmetic multiplication in non-blocking mode with Interrupt.
1205   * @param  hpka PKA handle
1206   * @param  in Input information
1207   * @retval HAL status
1208   */
HAL_PKA_Mul_IT(PKA_HandleTypeDef * hpka,PKA_MulInTypeDef * in)1209 HAL_StatusTypeDef HAL_PKA_Mul_IT(PKA_HandleTypeDef *hpka, PKA_MulInTypeDef *in)
1210 {
1211   /* Set input parameter in PKA RAM */
1212   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1213 
1214   /* Start the operation */
1215   return PKA_Process_IT(hpka, PKA_MODE_ARITHMETIC_MUL);
1216 }
1217 
1218 /**
1219   * @brief  Comparison in blocking mode.
1220   * @param  hpka PKA handle
1221   * @param  in Input information
1222   * @param  Timeout Timeout duration
1223   * @retval HAL status
1224   */
HAL_PKA_Cmp(PKA_HandleTypeDef * hpka,PKA_CmpInTypeDef * in,uint32_t Timeout)1225 HAL_StatusTypeDef HAL_PKA_Cmp(PKA_HandleTypeDef *hpka, PKA_CmpInTypeDef *in, uint32_t Timeout)
1226 {
1227   /* Set input parameter in PKA RAM */
1228   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1229 
1230   /* Start the operation */
1231   return PKA_Process(hpka, PKA_MODE_COMPARISON, Timeout);
1232 }
1233 
1234 /**
1235   * @brief  Comparison in non-blocking mode with Interrupt.
1236   * @param  hpka PKA handle
1237   * @param  in Input information
1238   * @retval HAL status
1239   */
HAL_PKA_Cmp_IT(PKA_HandleTypeDef * hpka,PKA_CmpInTypeDef * in)1240 HAL_StatusTypeDef HAL_PKA_Cmp_IT(PKA_HandleTypeDef *hpka, PKA_CmpInTypeDef *in)
1241 {
1242   /* Set input parameter in PKA RAM */
1243   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1244 
1245   /* Start the operation */
1246   return PKA_Process_IT(hpka, PKA_MODE_COMPARISON);
1247 }
1248 
1249 /**
1250   * @brief  Modular addition in blocking mode.
1251   * @param  hpka PKA handle
1252   * @param  in Input information
1253   * @param  Timeout Timeout duration
1254   * @retval HAL status
1255   */
HAL_PKA_ModAdd(PKA_HandleTypeDef * hpka,PKA_ModAddInTypeDef * in,uint32_t Timeout)1256 HAL_StatusTypeDef HAL_PKA_ModAdd(PKA_HandleTypeDef *hpka, PKA_ModAddInTypeDef *in, uint32_t Timeout)
1257 {
1258   /* Set input parameter in PKA RAM */
1259   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
1260 
1261   /* Start the operation */
1262   return PKA_Process(hpka, PKA_MODE_MODULAR_ADD, Timeout);
1263 }
1264 
1265 /**
1266   * @brief  Modular addition in non-blocking mode with Interrupt.
1267   * @param  hpka PKA handle
1268   * @param  in Input information
1269   * @retval HAL status
1270   */
HAL_PKA_ModAdd_IT(PKA_HandleTypeDef * hpka,PKA_ModAddInTypeDef * in)1271 HAL_StatusTypeDef HAL_PKA_ModAdd_IT(PKA_HandleTypeDef *hpka, PKA_ModAddInTypeDef *in)
1272 {
1273   /* Set input parameter in PKA RAM */
1274   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
1275 
1276   /* Start the operation */
1277   return PKA_Process_IT(hpka, PKA_MODE_MODULAR_ADD);
1278 }
1279 
1280 /**
1281   * @brief  Modular inversion in blocking mode.
1282   * @param  hpka PKA handle
1283   * @param  in Input information
1284   * @param  Timeout Timeout duration
1285   * @retval HAL status
1286   */
HAL_PKA_ModInv(PKA_HandleTypeDef * hpka,PKA_ModInvInTypeDef * in,uint32_t Timeout)1287 HAL_StatusTypeDef HAL_PKA_ModInv(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in, uint32_t Timeout)
1288 {
1289   /* Set input parameter in PKA RAM */
1290   PKA_ModInv_Set(hpka, in);
1291 
1292   /* Start the operation */
1293   return PKA_Process(hpka, PKA_MODE_MODULAR_INV, Timeout);
1294 }
1295 
1296 /**
1297   * @brief  Modular inversion in non-blocking mode with Interrupt.
1298   * @param  hpka PKA handle
1299   * @param  in Input information
1300   * @retval HAL status
1301   */
HAL_PKA_ModInv_IT(PKA_HandleTypeDef * hpka,PKA_ModInvInTypeDef * in)1302 HAL_StatusTypeDef HAL_PKA_ModInv_IT(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in)
1303 {
1304   /* Set input parameter in PKA RAM */
1305   PKA_ModInv_Set(hpka, in);
1306 
1307   /* Start the operation */
1308   return PKA_Process_IT(hpka, PKA_MODE_MODULAR_INV);
1309 }
1310 
1311 /**
1312   * @brief  Modular subtraction in blocking mode.
1313   * @param  hpka PKA handle
1314   * @param  in Input information
1315   * @param  Timeout Timeout duration
1316   * @retval HAL status
1317   */
HAL_PKA_ModSub(PKA_HandleTypeDef * hpka,PKA_ModSubInTypeDef * in,uint32_t Timeout)1318 HAL_StatusTypeDef HAL_PKA_ModSub(PKA_HandleTypeDef *hpka, PKA_ModSubInTypeDef *in, uint32_t Timeout)
1319 {
1320   /* Set input parameter in PKA RAM */
1321   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
1322 
1323   /* Start the operation */
1324   return PKA_Process(hpka, PKA_MODE_MODULAR_SUB, Timeout);
1325 }
1326 
1327 /**
1328   * @brief  Modular subtraction in non-blocking mode with Interrupt.
1329   * @param  hpka PKA handle
1330   * @param  in Input information
1331   * @retval HAL status
1332   */
HAL_PKA_ModSub_IT(PKA_HandleTypeDef * hpka,PKA_ModSubInTypeDef * in)1333 HAL_StatusTypeDef HAL_PKA_ModSub_IT(PKA_HandleTypeDef *hpka, PKA_ModSubInTypeDef *in)
1334 {
1335   /* Set input parameter in PKA RAM */
1336   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
1337 
1338   /* Start the operation */
1339   return PKA_Process_IT(hpka, PKA_MODE_MODULAR_SUB);
1340 }
1341 
1342 /**
1343   * @brief  Modular reduction in blocking mode.
1344   * @param  hpka PKA handle
1345   * @param  in Input information
1346   * @param  Timeout Timeout duration
1347   * @retval HAL status
1348   */
HAL_PKA_ModRed(PKA_HandleTypeDef * hpka,PKA_ModRedInTypeDef * in,uint32_t Timeout)1349 HAL_StatusTypeDef HAL_PKA_ModRed(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in, uint32_t Timeout)
1350 {
1351   /* Set input parameter in PKA RAM */
1352   PKA_ModRed_Set(hpka, in);
1353 
1354   /* Start the operation */
1355   return PKA_Process(hpka, PKA_MODE_MODULAR_RED, Timeout);
1356 }
1357 
1358 /**
1359   * @brief  Modular reduction in non-blocking mode with Interrupt.
1360   * @param  hpka PKA handle
1361   * @param  in Input information
1362   * @retval HAL status
1363   */
HAL_PKA_ModRed_IT(PKA_HandleTypeDef * hpka,PKA_ModRedInTypeDef * in)1364 HAL_StatusTypeDef HAL_PKA_ModRed_IT(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in)
1365 {
1366   /* Set input parameter in PKA RAM */
1367   PKA_ModRed_Set(hpka, in);
1368 
1369   /* Start the operation */
1370   return PKA_Process_IT(hpka, PKA_MODE_MODULAR_RED);
1371 }
1372 
1373 /**
1374   * @brief  Montgomery multiplication in blocking mode.
1375   * @param  hpka PKA handle
1376   * @param  in Input information
1377   * @param  Timeout Timeout duration
1378   * @retval HAL status
1379   */
HAL_PKA_MontgomeryMul(PKA_HandleTypeDef * hpka,PKA_MontgomeryMulInTypeDef * in,uint32_t Timeout)1380 HAL_StatusTypeDef HAL_PKA_MontgomeryMul(PKA_HandleTypeDef *hpka, PKA_MontgomeryMulInTypeDef *in, uint32_t Timeout)
1381 {
1382   /* Set input parameter in PKA RAM */
1383   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
1384 
1385   /* Start the operation */
1386   return PKA_Process(hpka, PKA_MODE_MONTGOMERY_MUL, Timeout);
1387 }
1388 
1389 /**
1390   * @brief  Montgomery multiplication in non-blocking mode with Interrupt.
1391   * @param  hpka PKA handle
1392   * @param  in Input information
1393   * @retval HAL status
1394   */
HAL_PKA_MontgomeryMul_IT(PKA_HandleTypeDef * hpka,PKA_MontgomeryMulInTypeDef * in)1395 HAL_StatusTypeDef HAL_PKA_MontgomeryMul_IT(PKA_HandleTypeDef *hpka, PKA_MontgomeryMulInTypeDef *in)
1396 {
1397   /* Set input parameter in PKA RAM */
1398   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
1399 
1400   /* Start the operation */
1401   return PKA_Process_IT(hpka, PKA_MODE_MONTGOMERY_MUL);
1402 }
1403 
1404 /**
1405   * @brief  Retrieve operation result.
1406   * @param  hpka PKA handle
1407   * @param  pRes Pointer to memory location to receive the result of the operation
1408   */
HAL_PKA_Arithmetic_GetResult(PKA_HandleTypeDef * hpka,uint32_t * pRes)1409 void HAL_PKA_Arithmetic_GetResult(PKA_HandleTypeDef *hpka, uint32_t *pRes)
1410 {
1411   uint32_t mode = (hpka->Instance->CR & PKA_CR_MODE_Msk) >> PKA_CR_MODE_Pos;
1412   uint32_t size = 0;
1413 
1414   /* Move the result to appropriate location (indicated in pRes parameter) */
1415   switch (mode)
1416   {
1417     case PKA_MODE_ARITHMETIC_SUB:
1418     case PKA_MODE_MODULAR_ADD:
1419     case PKA_MODE_MODULAR_RED:
1420     case PKA_MODE_MODULAR_INV:
1421     case PKA_MODE_MODULAR_SUB:
1422     case PKA_MODE_MONTGOMERY_MUL:
1423       size = hpka->Instance->RAM[1] / 32UL;
1424       break;
1425     case PKA_MODE_ARITHMETIC_ADD:
1426       size = hpka->Instance->RAM[1] / 32UL;
1427 
1428       /* Manage the overflow of the addition */
1429       if (hpka->Instance->RAM[500U + size] != 0UL)
1430       {
1431         size += 1UL;
1432       }
1433 
1434       break;
1435     case PKA_MODE_COMPARISON:
1436       size = 1;
1437       break;
1438     case PKA_MODE_ARITHMETIC_MUL:
1439       size = hpka->Instance->RAM[1] / 32UL * 2UL;
1440       break;
1441     default:
1442       break;
1443   }
1444 
1445   if (pRes != NULL)
1446   {
1447     switch (mode)
1448     {
1449       case PKA_MODE_ARITHMETIC_SUB:
1450       case PKA_MODE_MODULAR_ADD:
1451       case PKA_MODE_MODULAR_RED:
1452       case PKA_MODE_MODULAR_INV:
1453       case PKA_MODE_MODULAR_SUB:
1454       case PKA_MODE_MONTGOMERY_MUL:
1455       case PKA_MODE_ARITHMETIC_ADD:
1456       case PKA_MODE_COMPARISON:
1457       case PKA_MODE_ARITHMETIC_MUL:
1458         PKA_Memcpy_u32_to_u32(pRes, &hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_OUT_RESULT], size);
1459         break;
1460       default:
1461         break;
1462     }
1463   }
1464 }
1465 
1466 /**
1467   * @brief  Montgomery parameter computation in blocking mode.
1468   * @param  hpka PKA handle
1469   * @param  in Input information
1470   * @param  Timeout Timeout duration
1471   * @retval HAL status
1472   */
HAL_PKA_MontgomeryParam(PKA_HandleTypeDef * hpka,PKA_MontgomeryParamInTypeDef * in,uint32_t Timeout)1473 HAL_StatusTypeDef HAL_PKA_MontgomeryParam(PKA_HandleTypeDef *hpka, PKA_MontgomeryParamInTypeDef *in, uint32_t Timeout)
1474 {
1475   /* Set input parameter in PKA RAM */
1476   PKA_MontgomeryParam_Set(hpka, in->size, in->pOp1);
1477 
1478   /* Start the operation */
1479   return PKA_Process(hpka, PKA_MODE_MONTGOMERY_PARAM, Timeout);
1480 }
1481 
1482 /**
1483   * @brief  Montgomery parameter computation in non-blocking mode with Interrupt.
1484   * @param  hpka PKA handle
1485   * @param  in Input information
1486   * @retval HAL status
1487   */
HAL_PKA_MontgomeryParam_IT(PKA_HandleTypeDef * hpka,PKA_MontgomeryParamInTypeDef * in)1488 HAL_StatusTypeDef HAL_PKA_MontgomeryParam_IT(PKA_HandleTypeDef *hpka, PKA_MontgomeryParamInTypeDef *in)
1489 {
1490   /* Set input parameter in PKA RAM */
1491   PKA_MontgomeryParam_Set(hpka, in->size, in->pOp1);
1492 
1493   /* Start the operation */
1494   return PKA_Process_IT(hpka, PKA_MODE_MONTGOMERY_PARAM);
1495 }
1496 
1497 
1498 /**
1499   * @brief  Retrieve operation result.
1500   * @param  hpka PKA handle
1501   * @param  pRes pointer to buffer where the result will be copied
1502   * @retval HAL status
1503   */
HAL_PKA_MontgomeryParam_GetResult(PKA_HandleTypeDef * hpka,uint32_t * pRes)1504 void HAL_PKA_MontgomeryParam_GetResult(PKA_HandleTypeDef *hpka, uint32_t *pRes)
1505 {
1506   uint32_t size;
1507 
1508   /* Retrieve the size of the buffer from the PKA RAM */
1509   size = (hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MOD_NB_BITS] + 31UL) / 32UL;
1510 
1511   /* Move the result to appropriate location (indicated in out parameter) */
1512   PKA_Memcpy_u32_to_u32(pRes, &hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_OUT_PARAMETER], size);
1513 }
1514 
1515 /**
1516   * @brief  Abort any ongoing operation.
1517   * @param  hpka PKA handle
1518   * @retval HAL status
1519   */
HAL_PKA_Abort(PKA_HandleTypeDef * hpka)1520 HAL_StatusTypeDef HAL_PKA_Abort(PKA_HandleTypeDef *hpka)
1521 {
1522   HAL_StatusTypeDef err = HAL_OK;
1523 
1524   /* Clear EN bit */
1525   /* This abort any operation in progress (PKA RAM content is not guaranteed in this case) */
1526   CLEAR_BIT(hpka->Instance->CR, PKA_CR_EN);
1527   SET_BIT(hpka->Instance->CR, PKA_CR_EN);
1528 
1529   /* Reset any pending flag */
1530   SET_BIT(hpka->Instance->CLRFR, PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
1531 
1532   /* Reset the error code */
1533   hpka->ErrorCode = HAL_PKA_ERROR_NONE;
1534 
1535   /* Reset the state */
1536   hpka->State = HAL_PKA_STATE_READY;
1537 
1538   return err;
1539 }
1540 
1541 /**
1542   * @brief  Reset the PKA RAM.
1543   * @param  hpka PKA handle
1544   * @retval None
1545   */
HAL_PKA_RAMReset(PKA_HandleTypeDef * hpka)1546 void HAL_PKA_RAMReset(PKA_HandleTypeDef *hpka)
1547 {
1548   uint32_t index;
1549 
1550   /* For each element in the PKA RAM */
1551   for (index = 0; index < PKA_RAM_SIZE; index++)
1552   {
1553     /* Clear the content */
1554     hpka->Instance->RAM[index] = 0UL;
1555   }
1556 }
1557 
1558 /**
1559   * @brief  This function handles PKA event interrupt request.
1560   * @param  hpka PKA handle
1561   * @retval None
1562   */
HAL_PKA_IRQHandler(PKA_HandleTypeDef * hpka)1563 void HAL_PKA_IRQHandler(PKA_HandleTypeDef *hpka)
1564 {
1565   uint32_t mode = PKA_GetMode(hpka);
1566   uint32_t itsource = READ_REG(hpka->Instance->CR);
1567   uint32_t flag  =   READ_REG(hpka->Instance->SR);
1568 
1569   /* Address error interrupt occurred */
1570   if (((itsource & PKA_IT_ADDRERR) == PKA_IT_ADDRERR) && ((flag & PKA_FLAG_ADDRERR) == PKA_FLAG_ADDRERR))
1571   {
1572     hpka->ErrorCode |= HAL_PKA_ERROR_ADDRERR;
1573 
1574     /* Clear ADDRERR flag */
1575     __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_ADDRERR);
1576   }
1577 
1578   /* RAM access error interrupt occurred */
1579   if (((itsource & PKA_IT_RAMERR) == PKA_IT_RAMERR) && ((flag & PKA_FLAG_RAMERR) == PKA_FLAG_RAMERR))
1580   {
1581     hpka->ErrorCode |= HAL_PKA_ERROR_RAMERR;
1582 
1583     /* Clear RAMERR flag */
1584     __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_RAMERR);
1585   }
1586 
1587   /* Check the operation success in case of ECDSA signature */
1588   if (mode == PKA_MODE_ECDSA_SIGNATURE)
1589   {
1590     /* If error output result is different from 0, ecdsa sign operation need to be repeated */
1591     if (hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_ERROR] != 0UL)
1592     {
1593       hpka->ErrorCode |= HAL_PKA_ERROR_OPERATION;
1594     }
1595   }
1596   /* Trigger the error callback if an error is present */
1597   if (hpka->ErrorCode != HAL_PKA_ERROR_NONE)
1598   {
1599 #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
1600     hpka->ErrorCallback(hpka);
1601 #else
1602     HAL_PKA_ErrorCallback(hpka);
1603 #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
1604   }
1605 
1606   /* End Of Operation interrupt occurred */
1607   if (((itsource & PKA_IT_PROCEND) == PKA_IT_PROCEND) && ((flag & PKA_FLAG_PROCEND) == PKA_FLAG_PROCEND))
1608   {
1609     /* Clear PROCEND flag */
1610     __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_PROCEND);
1611 
1612     /* Set the state to ready */
1613     hpka->State = HAL_PKA_STATE_READY;
1614 
1615 #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
1616     hpka->OperationCpltCallback(hpka);
1617 #else
1618     HAL_PKA_OperationCpltCallback(hpka);
1619 #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
1620   }
1621 }
1622 
1623 /**
1624   * @brief  Process completed callback.
1625   * @param  hpka PKA handle
1626   * @retval None
1627   */
HAL_PKA_OperationCpltCallback(PKA_HandleTypeDef * hpka)1628 __weak void HAL_PKA_OperationCpltCallback(PKA_HandleTypeDef *hpka)
1629 {
1630   /* Prevent unused argument(s) compilation warning */
1631   UNUSED(hpka);
1632 
1633   /* NOTE : This function should not be modified, when the callback is needed,
1634             the HAL_PKA_OperationCpltCallback could be implemented in the user file
1635    */
1636 }
1637 
1638 /**
1639   * @brief  Error callback.
1640   * @param  hpka PKA handle
1641   * @retval None
1642   */
HAL_PKA_ErrorCallback(PKA_HandleTypeDef * hpka)1643 __weak void HAL_PKA_ErrorCallback(PKA_HandleTypeDef *hpka)
1644 {
1645   /* Prevent unused argument(s) compilation warning */
1646   UNUSED(hpka);
1647 
1648   /* NOTE : This function should not be modified, when the callback is needed,
1649             the HAL_PKA_ErrorCallback could be implemented in the user file
1650    */
1651 }
1652 
1653 /**
1654   * @}
1655   */
1656 
1657 /** @defgroup PKA_Exported_Functions_Group3 Peripheral State and Error functions
1658   * @brief    Peripheral State and Error functions
1659   *
1660   @verbatim
1661  ===============================================================================
1662             ##### Peripheral State and Error functions #####
1663  ===============================================================================
1664     [..]
1665     This subsection permit to get in run-time the status of the peripheral.
1666 
1667 @endverbatim
1668   * @{
1669   */
1670 
1671 /**
1672   * @brief  Return the PKA handle state.
1673   * @param  hpka PKA handle
1674   * @retval HAL status
1675   */
HAL_PKA_GetState(const PKA_HandleTypeDef * hpka)1676 HAL_PKA_StateTypeDef HAL_PKA_GetState(const PKA_HandleTypeDef *hpka)
1677 {
1678   /* Return PKA handle state */
1679   return hpka->State;
1680 }
1681 
1682 /**
1683   * @brief  Return the PKA error code.
1684   * @param  hpka PKA handle
1685   * @retval PKA error code
1686   */
HAL_PKA_GetError(const PKA_HandleTypeDef * hpka)1687 uint32_t HAL_PKA_GetError(const PKA_HandleTypeDef *hpka)
1688 {
1689   /* Return PKA handle error code */
1690   return hpka->ErrorCode;
1691 }
1692 
1693 /**
1694   * @}
1695   */
1696 
1697 /**
1698   * @}
1699   */
1700 
1701 /** @addtogroup PKA_Private_Functions
1702   * @{
1703   */
1704 
1705 /**
1706   * @brief  Get PKA operating mode.
1707   * @param  hpka PKA handle
1708   * @retval Return the current mode
1709   */
PKA_GetMode(const PKA_HandleTypeDef * hpka)1710 uint32_t PKA_GetMode(const PKA_HandleTypeDef *hpka)
1711 {
1712   /* return the shifted PKA_CR_MODE value */
1713   return (uint32_t)(READ_BIT(hpka->Instance->CR, PKA_CR_MODE) >> PKA_CR_MODE_Pos);
1714 }
1715 
1716 /**
1717   * @brief  Wait for operation completion or timeout.
1718   * @param  hpka PKA handle
1719   * @param  Timeout Timeout duration in millisecond.
1720   * @param  Tickstart Tick start value
1721   * @retval HAL status
1722   */
PKA_PollEndOfOperation(const PKA_HandleTypeDef * hpka,uint32_t Timeout,uint32_t Tickstart)1723 HAL_StatusTypeDef PKA_PollEndOfOperation(const PKA_HandleTypeDef *hpka, uint32_t Timeout, uint32_t Tickstart)
1724 {
1725   /* Wait for the end of operation or timeout */
1726   while ((hpka->Instance->SR & PKA_SR_PROCENDF) == 0UL)
1727   {
1728     /* Check if timeout is disabled (set to infinite wait) */
1729     if (Timeout != HAL_MAX_DELAY)
1730     {
1731       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0UL))
1732       {
1733         return HAL_TIMEOUT;
1734       }
1735     }
1736   }
1737   return HAL_OK;
1738 }
1739 
1740 /**
1741   * @brief  Return a hal error code based on PKA error flags.
1742   * @param  hpka PKA handle
1743   * @param  mode PKA operating mode
1744   * @retval error code
1745   */
PKA_CheckError(const PKA_HandleTypeDef * hpka,uint32_t mode)1746 uint32_t PKA_CheckError(const PKA_HandleTypeDef *hpka, uint32_t mode)
1747 {
1748   uint32_t err = HAL_PKA_ERROR_NONE;
1749 
1750   /* Check RAMERR error */
1751   if (__HAL_PKA_GET_FLAG(hpka, PKA_FLAG_RAMERR) == SET)
1752   {
1753     err |= HAL_PKA_ERROR_RAMERR;
1754   }
1755 
1756   /* Check ADDRERR error */
1757   if (__HAL_PKA_GET_FLAG(hpka, PKA_FLAG_ADDRERR) == SET)
1758   {
1759     err |= HAL_PKA_ERROR_ADDRERR;
1760   }
1761 
1762   /* Check the operation success in case of ECDSA signature */
1763   if (mode == PKA_MODE_ECDSA_SIGNATURE)
1764   {
1765 #define EDCSA_SIGN_NOERROR 0UL
1766     /* If error output result is different from no error, ecsa sign operation need to be repeated */
1767     if (hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_ERROR] != EDCSA_SIGN_NOERROR)
1768     {
1769       err |= HAL_PKA_ERROR_OPERATION;
1770     }
1771   }
1772 
1773   return err;
1774 }
1775 
1776 /**
1777   * @brief  Get number of bits inside an array of u8.
1778   * @param  byteNumber Number of u8 inside the array
1779   */
PKA_GetBitSize_u8(uint32_t byteNumber)1780 uint32_t PKA_GetBitSize_u8(uint32_t byteNumber)
1781 {
1782   /* Convert from number of uint8_t in an array to the associated number of bits in this array */
1783   return byteNumber * 8UL;
1784 }
1785 
1786 /**
1787   * @brief  Get optimal number of bits inside an array of u8.
1788   * @param  byteNumber Number of u8 inside the array
1789   * @param  msb Most significant uint8_t of the array
1790   */
PKA_GetOptBitSize_u8(uint32_t byteNumber,uint8_t msb)1791 uint32_t PKA_GetOptBitSize_u8(uint32_t byteNumber, uint8_t msb)
1792 {
1793   uint32_t position;
1794 
1795   position = 32UL - __CLZ(msb);
1796 
1797   return (((byteNumber - 1UL) * 8UL) + position);
1798 }
1799 
1800 /**
1801   * @brief  Get number of bits inside an array of u32.
1802   * @param  wordNumber Number of u32 inside the array
1803   */
PKA_GetBitSize_u32(uint32_t wordNumber)1804 uint32_t PKA_GetBitSize_u32(uint32_t wordNumber)
1805 {
1806   /* Convert from number of uint32_t in an array to the associated number of bits in this array */
1807   return wordNumber * 32UL;
1808 }
1809 
1810 /**
1811   * @brief  Get number of uint8_t element in an array of bitSize bits.
1812   * @param  bitSize Number of bits in an array
1813   */
PKA_GetArraySize_u8(uint32_t bitSize)1814 uint32_t PKA_GetArraySize_u8(uint32_t bitSize)
1815 {
1816   /* Manage the non aligned on uint8_t bitsize: */
1817   /*   512 bits requires 64 uint8_t             */
1818   /*   521 bits requires 66 uint8_t             */
1819   return ((bitSize + 7UL) / 8UL);
1820 }
1821 
1822 /**
1823   * @brief  Copy uint32_t array to uint8_t array to fit PKA number representation.
1824   * @param  dst Pointer to destination
1825   * @param  src Pointer to source
1826   * @param  n Number of uint8_t to copy
1827   * @retval dst
1828   */
PKA_Memcpy_u32_to_u8(uint8_t dst[],__IO const uint32_t src[],size_t n)1829 void PKA_Memcpy_u32_to_u8(uint8_t dst[], __IO const uint32_t src[], size_t n)
1830 {
1831   if (dst != NULL)
1832   {
1833     if (src != NULL)
1834     {
1835       uint32_t index_uint32_t = 0UL; /* This index is used outside of the loop */
1836 
1837       for (; index_uint32_t < (n / 4UL); index_uint32_t++)
1838       {
1839         /* Avoid casting from uint8_t* to uint32_t* by copying 4 uint8_t in a row */
1840         /* Apply __REV equivalent */
1841         uint32_t index_uint8_t = n - 4UL - (index_uint32_t * 4UL);
1842         dst[index_uint8_t + 3UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
1843         dst[index_uint8_t + 2UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
1844         dst[index_uint8_t + 1UL] = (uint8_t)((src[index_uint32_t] & 0x00FF0000U) >> 16UL);
1845         dst[index_uint8_t + 0UL] = (uint8_t)((src[index_uint32_t] & 0xFF000000U) >> 24UL);
1846       }
1847 
1848       /* Manage the buffers not aligned on uint32_t */
1849       if ((n % 4UL) == 1UL)
1850       {
1851         dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
1852       }
1853       else if ((n % 4UL) == 2UL)
1854       {
1855         dst[1UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
1856         dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
1857       }
1858       else if ((n % 4UL) == 3UL)
1859       {
1860         dst[2UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
1861         dst[1UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
1862         dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x00FF0000U) >> 16UL);
1863       }
1864       else
1865       {
1866         /* The last element is already handle in the loop */
1867       }
1868     }
1869   }
1870 }
1871 
1872 /**
1873   * @brief  Copy uint8_t array to uint32_t array to fit PKA number representation.
1874   * @param  dst Pointer to destination
1875   * @param  src Pointer to source
1876   * @param  n Number of uint8_t to copy (must be multiple of 4)
1877   * @retval dst
1878   */
PKA_Memcpy_u8_to_u32(__IO uint32_t dst[],const uint8_t src[],size_t n)1879 void PKA_Memcpy_u8_to_u32(__IO uint32_t dst[], const uint8_t src[], size_t n)
1880 {
1881   if (dst != NULL)
1882   {
1883     if (src != NULL)
1884     {
1885       uint32_t index = 0UL; /* This index is used outside of the loop */
1886 
1887       for (; index < (n / 4UL); index++)
1888       {
1889         /* Apply the equivalent of __REV from uint8_t to uint32_t */
1890         dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
1891                      | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL) \
1892                      | ((uint32_t)src[(n - (index * 4UL) - 3UL)] << 16UL) \
1893                      | ((uint32_t)src[(n - (index * 4UL) - 4UL)] << 24UL);
1894       }
1895 
1896       /* Manage the buffers not aligned on uint32_t */
1897       if ((n % 4UL) == 1UL)
1898       {
1899         dst[index] = (uint32_t)src[(n - (index * 4UL) - 1UL)];
1900       }
1901       else if ((n % 4UL) == 2UL)
1902       {
1903         dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
1904                      | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL);
1905       }
1906       else if ((n % 4UL) == 3UL)
1907       {
1908         dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
1909                      | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL) \
1910                      | ((uint32_t)src[(n - (index * 4UL) - 3UL)] << 16UL);
1911       }
1912       else
1913       {
1914         /* The last element is already handle in the loop */
1915       }
1916     }
1917   }
1918 }
1919 
1920 /**
1921   * @brief  Copy uint32_t array to uint32_t array.
1922   * @param  dst Pointer to destination
1923   * @param  src Pointer to source
1924   * @param  n Number of u32 to be handled
1925   * @retval dst
1926   */
PKA_Memcpy_u32_to_u32(__IO uint32_t dst[],__IO const uint32_t src[],size_t n)1927 void PKA_Memcpy_u32_to_u32(__IO uint32_t dst[], __IO const uint32_t src[], size_t n)
1928 {
1929   /* If a destination buffer is provided */
1930   if (dst != NULL)
1931   {
1932     /* If a source buffer is provided */
1933     if (src != NULL)
1934     {
1935       /* For each element in the array */
1936       for (uint32_t index = 0UL; index < n; index++)
1937       {
1938         /* Copy the content */
1939         dst[index] = src[index];
1940       }
1941     }
1942   }
1943 }
1944 
1945 /**
1946   * @brief  Generic function to start a PKA operation in blocking mode.
1947   * @param  hpka PKA handle
1948   * @param  mode PKA operation
1949   * @param  Timeout Timeout duration
1950   * @retval HAL status
1951   */
PKA_Process(PKA_HandleTypeDef * hpka,uint32_t mode,uint32_t Timeout)1952 HAL_StatusTypeDef PKA_Process(PKA_HandleTypeDef *hpka, uint32_t mode, uint32_t Timeout)
1953 {
1954   HAL_StatusTypeDef err = HAL_OK;
1955   uint32_t tickstart;
1956 
1957   if (hpka->State == HAL_PKA_STATE_READY)
1958   {
1959     /* Set the state to busy */
1960     hpka->State = HAL_PKA_STATE_BUSY;
1961 
1962     /* Clear any pending error */
1963     hpka->ErrorCode = HAL_PKA_ERROR_NONE;
1964 
1965     /* Init tickstart for timeout management*/
1966     tickstart = HAL_GetTick();
1967 
1968     /* Set the mode and deactivate the interrupts */
1969     MODIFY_REG(hpka->Instance->CR, PKA_CR_MODE | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE,
1970                mode << PKA_CR_MODE_Pos);
1971 
1972     /* Start the computation */
1973     hpka->Instance->CR |= PKA_CR_START;
1974 
1975     /* Wait for the end of operation or timeout */
1976     if (PKA_PollEndOfOperation(hpka, Timeout, tickstart) != HAL_OK)
1977     {
1978       /* Abort any ongoing operation */
1979       CLEAR_BIT(hpka->Instance->CR, PKA_CR_EN);
1980 
1981       hpka->ErrorCode |= HAL_PKA_ERROR_TIMEOUT;
1982 
1983       /* Make ready for the next operation */
1984       SET_BIT(hpka->Instance->CR, PKA_CR_EN);
1985     }
1986 
1987     /* Check error */
1988     hpka->ErrorCode |= PKA_CheckError(hpka, mode);
1989 
1990     /* Clear all flags */
1991     hpka->Instance->CLRFR |= (PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
1992 
1993     /* Set the state to ready */
1994     hpka->State = HAL_PKA_STATE_READY;
1995 
1996     /* Manage the result based on encountered errors */
1997     if (hpka->ErrorCode != HAL_PKA_ERROR_NONE)
1998     {
1999       err = HAL_ERROR;
2000     }
2001   }
2002   else
2003   {
2004     err = HAL_ERROR;
2005   }
2006   return err;
2007 }
2008 
2009 /**
2010   * @brief  Generic function to start a PKA operation in non-blocking mode with Interrupt.
2011   * @param  hpka PKA handle
2012   * @param  mode PKA operation
2013   * @retval HAL status
2014   */
PKA_Process_IT(PKA_HandleTypeDef * hpka,uint32_t mode)2015 HAL_StatusTypeDef PKA_Process_IT(PKA_HandleTypeDef *hpka, uint32_t mode)
2016 {
2017   HAL_StatusTypeDef err = HAL_OK;
2018 
2019   if (hpka->State == HAL_PKA_STATE_READY)
2020   {
2021     /* Set the state to busy */
2022     hpka->State = HAL_PKA_STATE_BUSY;
2023 
2024     /* Clear any pending error */
2025     hpka->ErrorCode = HAL_PKA_ERROR_NONE;
2026 
2027     /* Set the mode and activate interrupts */
2028     MODIFY_REG(hpka->Instance->CR, PKA_CR_MODE | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE,
2029                (mode << PKA_CR_MODE_Pos) | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE);
2030 
2031     /* Start the computation */
2032     hpka->Instance->CR |= PKA_CR_START;
2033   }
2034   else
2035   {
2036     err = HAL_ERROR;
2037   }
2038   return err;
2039 }
2040 
2041 /**
2042   * @brief  Set input parameters.
2043   * @param  hpka PKA handle
2044   * @param  in Input information
2045   */
PKA_ModExp_Set(PKA_HandleTypeDef * hpka,PKA_ModExpInTypeDef * in)2046 void PKA_ModExp_Set(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in)
2047 {
2048   /* Get the number of bit per operand */
2049   hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = PKA_GetBitSize_u8(in->OpSize);
2050 
2051   /* Get the number of bit of the exponent */
2052   hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = PKA_GetBitSize_u8(in->expSize);
2053 
2054   /* Move the input parameters pOp1 to PKA RAM */
2055   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT_BASE], in->pOp1, in->OpSize);
2056   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT_BASE + ((in->OpSize + 3UL) / 4UL));
2057 
2058   /* Move the exponent to PKA RAM */
2059   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT], in->pExp, in->expSize);
2060   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT + ((in->expSize + 3UL) / 4UL));
2061 
2062   /* Move the modulus to PKA RAM */
2063   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MODULUS], in->pMod, in->OpSize);
2064   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MODULUS + ((in->OpSize + 3UL) / 4UL));
2065 }
2066 
2067 /**
2068   * @brief  Set input parameters.
2069   * @param  hpka PKA handle
2070   * @param  in Input information
2071   */
PKA_ModExpFastMode_Set(PKA_HandleTypeDef * hpka,PKA_ModExpFastModeInTypeDef * in)2072 void PKA_ModExpFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in)
2073 {
2074   /* Get the number of bit per operand */
2075   hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = PKA_GetBitSize_u8(in->OpSize);
2076 
2077   /* Get the number of bit of the exponent */
2078   hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = PKA_GetBitSize_u8(in->expSize);
2079 
2080   /* Move the input parameters pOp1 to PKA RAM */
2081   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT_BASE], in->pOp1, in->OpSize);
2082   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT_BASE + (in->OpSize / 4UL));
2083 
2084   /* Move the exponent to PKA RAM */
2085   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT], in->pExp, in->expSize);
2086   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT + (in->expSize / 4UL));
2087 
2088   /* Move the modulus to PKA RAM */
2089   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MODULUS], in->pMod, in->OpSize);
2090   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MODULUS + (in->OpSize / 4UL));
2091 
2092   /* Move the Montgomery parameter to PKA RAM */
2093   PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MONTGOMERY_PARAM], in->pMontgomeryParam,
2094                         in->OpSize / 4UL);
2095   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MONTGOMERY_PARAM + (in->OpSize / 4UL));
2096 }
2097 
2098 
2099 /**
2100   * @brief  Set input parameters.
2101   * @param  hpka PKA handle
2102   * @param  in Input information
2103   * @note   If the modulus size is bigger than the hash size (with a curve SECP521R1 when using a SHA256 hash
2104   *         for example)the hash value should be written at the end of the buffer with zeros padding at beginning.
2105   */
PKA_ECDSASign_Set(PKA_HandleTypeDef * hpka,PKA_ECDSASignInTypeDef * in)2106 void PKA_ECDSASign_Set(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in)
2107 {
2108   /* Get the prime order n length */
2109   hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS] = PKA_GetOptBitSize_u8(in->primeOrderSize, *(in->primeOrder));
2110 
2111   /* Get the modulus p length */
2112   hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2113 
2114   /* Get the coefficient a sign */
2115   hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN] = in->coefSign;
2116 
2117   /* Move the input parameters coefficient |a| to PKA RAM */
2118   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_A_COEFF], in->coef, in->modulusSize);
2119   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2120 
2121   /* Move the input parameters modulus value p to PKA RAM */
2122   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_GF], in->modulus, in->modulusSize);
2123   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2124 
2125   /* Move the input parameters integer k to PKA RAM */
2126   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_K], in->integer, in->primeOrderSize);
2127   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_K + ((in->primeOrderSize + 3UL) / 4UL));
2128 
2129   /* Move the input parameters base point G coordinate x to PKA RAM */
2130   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_INITIAL_POINT_X], in->basePointX, in->modulusSize);
2131   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2132 
2133   /* Move the input parameters base point G coordinate y to PKA RAM */
2134   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y], in->basePointY, in->modulusSize);
2135   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2136 
2137   /* Move the input parameters hash of message z to PKA RAM */
2138   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_HASH_E], in->hash, in->primeOrderSize);
2139   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_HASH_E + ((in->primeOrderSize + 3UL) / 4UL));
2140 
2141   /* Move the input parameters private key d to PKA RAM */
2142   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D], in->privateKey, in->primeOrderSize);
2143   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D + ((in->primeOrderSize + 3UL) / 4UL));
2144 
2145   /* Move the input parameters prime order n to PKA RAM */
2146   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_ORDER_N], in->primeOrder, in->primeOrderSize);
2147   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_ORDER_N + ((in->primeOrderSize + 3UL) / 4UL));
2148 }
2149 
2150 /**
2151   * @brief  Set input parameters.
2152   * @param  hpka PKA handle
2153   * @param  in Input information
2154   */
PKA_ECDSAVerif_Set(PKA_HandleTypeDef * hpka,PKA_ECDSAVerifInTypeDef * in)2155 void PKA_ECDSAVerif_Set(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in)
2156 {
2157   /* Get the prime order n length */
2158   hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS] = PKA_GetOptBitSize_u8(in->primeOrderSize, *(in->primeOrder));
2159 
2160   /* Get the modulus p length */
2161   hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2162 
2163   /* Get the coefficient a sign */
2164   hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN] = in->coefSign;
2165 
2166   /* Move the input parameters coefficient |a| to PKA RAM */
2167   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_A_COEFF], in->coef, in->modulusSize);
2168   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2169 
2170   /* Move the input parameters modulus value p to PKA RAM */
2171   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_MOD_GF], in->modulus, in->modulusSize);
2172   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2173 
2174   /* Move the input parameters base point G coordinate x to PKA RAM */
2175   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_INITIAL_POINT_X], in->basePointX, in->modulusSize);
2176   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2177 
2178   /* Move the input parameters base point G coordinate y to PKA RAM */
2179   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y], in->basePointY, in->modulusSize);
2180   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2181 
2182   /* Move the input parameters public-key curve point Q coordinate xQ to PKA RAM */
2183   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X], in->pPubKeyCurvePtX,
2184                        in->modulusSize);
2185   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2186 
2187   /* Move the input parameters public-key curve point Q coordinate xQ to PKA RAM */
2188   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y], in->pPubKeyCurvePtY,
2189                        in->modulusSize);
2190   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2191 
2192   /* Move the input parameters signature part r to PKA RAM */
2193   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_SIGNATURE_R], in->RSign, in->primeOrderSize);
2194   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_SIGNATURE_R + ((in->primeOrderSize + 3UL) / 4UL));
2195 
2196   /* Move the input parameters signature part s to PKA RAM */
2197   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_SIGNATURE_S], in->SSign, in->primeOrderSize);
2198   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_SIGNATURE_S + ((in->primeOrderSize + 3UL) / 4UL));
2199 
2200   /* Move the input parameters hash of message z to PKA RAM */
2201   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_HASH_E], in->hash, in->primeOrderSize);
2202   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_HASH_E + ((in->primeOrderSize + 3UL) / 4UL));
2203 
2204   /* Move the input parameters curve prime order n to PKA RAM */
2205   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_ORDER_N], in->primeOrder, in->primeOrderSize);
2206   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_ORDER_N + ((in->primeOrderSize + 3UL) / 4UL));
2207 }
2208 
2209 /**
2210   * @brief  Set input parameters.
2211   * @param  hpka PKA handle
2212   * @param  in Input information
2213   */
PKA_RSACRTExp_Set(PKA_HandleTypeDef * hpka,PKA_RSACRTExpInTypeDef * in)2214 void PKA_RSACRTExp_Set(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in)
2215 {
2216   /* Get the operand length M */
2217   hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] = PKA_GetBitSize_u8(in->size);
2218 
2219   /* Move the input parameters operand dP to PKA RAM */
2220   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_DP_CRT], in->pOpDp, in->size / 2UL);
2221   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_DP_CRT + (in->size / 8UL));
2222 
2223   /* Move the input parameters operand dQ to PKA RAM */
2224   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_DQ_CRT], in->pOpDq, in->size / 2UL);
2225   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_DQ_CRT + (in->size / 8UL));
2226 
2227   /* Move the input parameters operand qinv to PKA RAM */
2228   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_QINV_CRT], in->pOpQinv, in->size / 2UL);
2229   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_QINV_CRT + (in->size / 8UL));
2230 
2231   /* Move the input parameters prime p to PKA RAM */
2232   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_PRIME_P], in->pPrimeP, in->size / 2UL);
2233   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_PRIME_P + (in->size / 8UL));
2234 
2235   /* Move the input parameters prime q to PKA RAM */
2236   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_PRIME_Q], in->pPrimeQ, in->size / 2UL);
2237   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_PRIME_Q + (in->size / 8UL));
2238 
2239   /* Move the input parameters operand A to PKA RAM */
2240   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_EXPONENT_BASE], in->popA, in->size);
2241   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_EXPONENT_BASE + (in->size / 4UL));
2242 }
2243 
2244 /**
2245   * @brief  Set input parameters.
2246   * @param  hpka PKA handle
2247   * @param  in Input information
2248   */
PKA_PointCheck_Set(PKA_HandleTypeDef * hpka,PKA_PointCheckInTypeDef * in)2249 void PKA_PointCheck_Set(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in)
2250 {
2251   /* Get the modulus length */
2252   hpka->Instance->RAM[PKA_POINT_CHECK_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2253 
2254   /* Get the coefficient a sign */
2255   hpka->Instance->RAM[PKA_POINT_CHECK_IN_A_COEFF_SIGN] = in->coefSign;
2256 
2257   /* Move the input parameters coefficient |a| to PKA RAM */
2258   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_A_COEFF], in->coefA, in->modulusSize);
2259   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2260 
2261   /* Move the input parameters coefficient b to PKA RAM */
2262   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_B_COEFF], in->coefB, in->modulusSize);
2263   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_B_COEFF + ((in->modulusSize + 3UL) / 4UL));
2264 
2265   /* Move the input parameters modulus value p to PKA RAM */
2266   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_MOD_GF], in->modulus, in->modulusSize);
2267   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2268 
2269   /* Move the input parameters Point P coordinate x to PKA RAM */
2270   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
2271   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2272 
2273   /* Move the input parameters Point P coordinate y to PKA RAM */
2274   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
2275   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2276 }
2277 
2278 /**
2279   * @brief  Set input parameters.
2280   * @param  hpka PKA handle
2281   * @param  in Input information
2282   */
PKA_ECCMul_Set(PKA_HandleTypeDef * hpka,PKA_ECCMulInTypeDef * in)2283 void PKA_ECCMul_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in)
2284 {
2285   /* Get the scalar multiplier k length */
2286   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = PKA_GetOptBitSize_u8(in->scalarMulSize, *(in->scalarMul));
2287 
2288   /* Get the modulus length */
2289   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2290 
2291   /* Get the coefficient a sign */
2292   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = in->coefSign;
2293 
2294   /* Move the input parameters coefficient |a| to PKA RAM */
2295   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF], in->coefA, in->modulusSize);
2296   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2297 
2298 
2299   /* Move the input parameters modulus value p to PKA RAM */
2300   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MOD_GF], in->modulus, in->modulusSize);
2301   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2302 
2303   /* Move the input parameters scalar multiplier k to PKA RAM */
2304   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_K], in->scalarMul, in->scalarMulSize);
2305   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_K + ((in->scalarMulSize + 3UL) / 4UL));
2306 
2307   /* Move the input parameters Point P coordinate x to PKA RAM */
2308   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
2309   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2310 
2311   /* Move the input parameters Point P coordinate y to PKA RAM */
2312   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
2313   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2314 
2315 }
2316 
2317 /**
2318   * @brief  Set input parameters.
2319   * @param  hpka PKA handle
2320   * @param  in Input information
2321   */
PKA_ECCMulFastMode_Set(PKA_HandleTypeDef * hpka,PKA_ECCMulFastModeInTypeDef * in)2322 void PKA_ECCMulFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in)
2323 {
2324   /* Get the scalar multiplier k length */
2325   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = PKA_GetOptBitSize_u8(in->scalarMulSize, *(in->scalarMul));
2326 
2327   /* Get the modulus length */
2328   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2329 
2330   /* Get the coefficient a sign */
2331   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = in->coefSign;
2332 
2333   /* Move the input parameters coefficient |a| to PKA RAM */
2334   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF], in->coefA, in->modulusSize);
2335   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2336 
2337   /* Move the input parameters modulus value p to PKA RAM */
2338   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MOD_GF], in->modulus, in->modulusSize);
2339   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2340 
2341   /* Move the input parameters scalar multiplier k to PKA RAM */
2342   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_K], in->scalarMul, in->scalarMulSize);
2343   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_K + ((in->scalarMulSize + 3UL) / 4UL));
2344 
2345   /* Move the input parameters Point P coordinate x to PKA RAM */
2346   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
2347   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2348 
2349   /* Move the input parameters Point P coordinate y to PKA RAM */
2350   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
2351   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2352 
2353   /* Move the Montgomery parameter to PKA RAM */
2354   PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MONTGOMERY_PARAM], in->pMontgomeryParam,
2355                         (in->modulusSize + 3UL) / 4UL);
2356   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MONTGOMERY_PARAM + ((in->modulusSize + 3UL) / 4UL));
2357 }
2358 /**
2359   * @brief  Set input parameters.
2360   * @param  hpka PKA handle
2361   * @param  in Input information
2362   */
PKA_ModInv_Set(PKA_HandleTypeDef * hpka,PKA_ModInvInTypeDef * in)2363 void PKA_ModInv_Set(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in)
2364 {
2365   /* Get the number of bit per operand */
2366   hpka->Instance->RAM[PKA_MODULAR_INV_NB_BITS] = PKA_GetBitSize_u32(in->size);
2367 
2368   /* Move the input parameters operand A to PKA RAM */
2369   PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_INV_IN_OP1], in->pOp1, in->size);
2370   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_INV_IN_OP1 + in->size);
2371 
2372   /* Move the input parameters modulus value n to PKA RAM */
2373   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_INV_IN_OP2_MOD], in->pMod, in->size * 4UL);
2374   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_INV_IN_OP2_MOD + in->size);
2375 }
2376 
2377 /**
2378   * @brief  Set input parameters.
2379   * @param  hpka PKA handle
2380   * @param  in Input information
2381   */
PKA_ModRed_Set(PKA_HandleTypeDef * hpka,PKA_ModRedInTypeDef * in)2382 void PKA_ModRed_Set(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in)
2383 {
2384   /* Get the number of bit per operand */
2385   hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_OP_LENGTH] = PKA_GetBitSize_u32(in->OpSize);
2386 
2387   /* Get the number of bit per modulus */
2388   hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_MOD_LENGTH] = PKA_GetBitSize_u8(in->modSize);
2389 
2390   /* Move the input parameters operand A to PKA RAM */
2391   PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_OPERAND], in->pOp1, in->OpSize);
2392   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_REDUC_IN_OPERAND + in->OpSize);
2393 
2394   /* Move the input parameters modulus value n to PKA RAM */
2395   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_MODULUS], in->pMod, in->modSize);
2396   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_REDUC_IN_MODULUS + ((in->modSize + 3UL) / 4UL));
2397 }
2398 
2399 /**
2400   * @brief  Set input parameters.
2401   * @param  hpka PKA handle
2402   * @param  size Size of the operand
2403   * @param  pOp1 Generic pointer to input data
2404   */
PKA_MontgomeryParam_Set(PKA_HandleTypeDef * hpka,const uint32_t size,const uint8_t * pOp1)2405 void PKA_MontgomeryParam_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint8_t *pOp1)
2406 {
2407   uint32_t bytetoskip = 0UL;
2408   uint32_t newSize;
2409 
2410   if (pOp1 != NULL)
2411   {
2412     /* Count the number of zero bytes */
2413     while ((bytetoskip < size) && (pOp1[bytetoskip] == 0UL))
2414     {
2415       bytetoskip++;
2416     }
2417 
2418     /* Get new size after skipping zero bytes */
2419     newSize = size - bytetoskip;
2420 
2421     /* Get the number of bit per operand */
2422     hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(newSize, pOp1[bytetoskip]);
2423 
2424     /* Move the input parameters pOp1 to PKA RAM */
2425     PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MODULUS], pOp1, size);
2426     __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MONTGOMERY_PARAM_IN_MODULUS + ((size + 3UL) / 4UL));
2427   }
2428 }
2429 
2430 /**
2431   * @brief  Generic function to set input parameters.
2432   * @param  hpka PKA handle
2433   * @param  size Size of the operand
2434   * @param  pOp1 Generic pointer to input data
2435   * @param  pOp2 Generic pointer to input data
2436   * @param  pOp3 Generic pointer to input data
2437   */
PKA_ARI_Set(PKA_HandleTypeDef * hpka,const uint32_t size,const uint32_t * pOp1,const uint32_t * pOp2,const uint8_t * pOp3)2438 void PKA_ARI_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint32_t *pOp1, const uint32_t *pOp2,
2439                  const uint8_t *pOp3)
2440 {
2441   /* Get the number of bit per operand */
2442   hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_NB_BITS] = PKA_GetBitSize_u32(size);
2443 
2444   if (pOp1 != NULL)
2445   {
2446     /* Move the input parameters pOp1 to PKA RAM */
2447     PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP1], pOp1, size);
2448     __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP1 + size);
2449   }
2450 
2451   if (pOp2 != NULL)
2452   {
2453     /* Move the input parameters pOp2 to PKA RAM */
2454     PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP2], pOp2, size);
2455     __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP2 + size);
2456   }
2457 
2458   if (pOp3 != NULL)
2459   {
2460     /* Move the input parameters pOp3 to PKA RAM */
2461     PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP3], pOp3, size * 4UL);
2462     __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP3 + size);
2463   }
2464 }
2465 
2466 /**
2467   * @}
2468   */
2469 
2470 /**
2471   * @}
2472   */
2473 
2474 #endif /* defined(PKA) && defined(HAL_PKA_MODULE_ENABLED) */
2475 
2476 /**
2477   * @}
2478   */
2479