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