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