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