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