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