xref: /btstack/port/stm32-l451-miromico-sx1280/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_pka.c (revision 2fd737d36a1de5d778cacc671d4b4d8c4f3fed82)
1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_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 substraction 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 substraction 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 immediatly.
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) 2017 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 "stm32l4xx_hal.h"
247 
248 /** @addtogroup STM32L4xx_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 guaranted 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 immediatly.
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   #define PKA_POINT_IS_ON_CURVE 0UL
1045   /* Invert the value of the PKA RAM containig the result of the operation */
1046   return (hpka->Instance->RAM[PKA_POINT_CHECK_OUT_ERROR] == PKA_POINT_IS_ON_CURVE) ? 1UL : 0UL;
1047 }
1048 
1049 /**
1050   * @brief  ECC scalar multiplication in blocking mode.
1051   * @param  hpka PKA handle
1052   * @param  in Input information
1053   * @param  Timeout Timeout duration
1054   * @retval HAL status
1055   */
HAL_PKA_ECCMul(PKA_HandleTypeDef * hpka,PKA_ECCMulInTypeDef * in,uint32_t Timeout)1056 HAL_StatusTypeDef HAL_PKA_ECCMul(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in, uint32_t Timeout)
1057 {
1058   /* Set input parameter in PKA RAM */
1059   PKA_ECCMul_Set(hpka, in);
1060 
1061   /* Start the operation */
1062   return PKA_Process(hpka, PKA_MODE_ECC_MUL, Timeout);
1063 }
1064 
1065 /**
1066   * @brief  ECC scalar multiplication in non-blocking mode with Interrupt.
1067   * @param  hpka PKA handle
1068   * @param  in Input information
1069   * @retval HAL status
1070   */
HAL_PKA_ECCMul_IT(PKA_HandleTypeDef * hpka,PKA_ECCMulInTypeDef * in)1071 HAL_StatusTypeDef HAL_PKA_ECCMul_IT(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in)
1072 {
1073   /* Set input parameter in PKA RAM */
1074   PKA_ECCMul_Set(hpka, in);
1075 
1076   /* Start the operation */
1077   return PKA_Process_IT(hpka, PKA_MODE_ECC_MUL);
1078 }
1079 /**
1080   * @brief  ECC scalar multiplication in blocking mode.
1081   * @param  hpka PKA handle
1082   * @param  in Input information
1083   * @param  Timeout Timeout duration
1084   * @retval HAL status
1085   */
HAL_PKA_ECCMulFastMode(PKA_HandleTypeDef * hpka,PKA_ECCMulFastModeInTypeDef * in,uint32_t Timeout)1086 HAL_StatusTypeDef HAL_PKA_ECCMulFastMode(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in, uint32_t Timeout)
1087 {
1088   /* Set input parameter in PKA RAM */
1089   PKA_ECCMulFastMode_Set(hpka, in);
1090 
1091   /* Start the operation */
1092   return PKA_Process(hpka, PKA_MODE_ECC_MUL_FAST_MODE, Timeout);
1093 }
1094 
1095 /**
1096   * @brief  ECC scalar multiplication in non-blocking mode with Interrupt.
1097   * @param  hpka PKA handle
1098   * @param  in Input information
1099   * @retval HAL status
1100   */
HAL_PKA_ECCMulFastMode_IT(PKA_HandleTypeDef * hpka,PKA_ECCMulFastModeInTypeDef * in)1101 HAL_StatusTypeDef HAL_PKA_ECCMulFastMode_IT(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in)
1102 {
1103   /* Set input parameter in PKA RAM */
1104   PKA_ECCMulFastMode_Set(hpka, in);
1105 
1106   /* Start the operation */
1107   return PKA_Process_IT(hpka, PKA_MODE_ECC_MUL_FAST_MODE);
1108 }
1109 /**
1110   * @brief  Retrieve operation result.
1111   * @param  hpka PKA handle
1112   * @param  out Output information
1113   * @retval HAL status
1114   */
HAL_PKA_ECCMul_GetResult(PKA_HandleTypeDef * hpka,PKA_ECCMulOutTypeDef * out)1115 void HAL_PKA_ECCMul_GetResult(PKA_HandleTypeDef *hpka, PKA_ECCMulOutTypeDef *out)
1116 {
1117   uint32_t size;
1118 
1119   /* Retrieve the size of the array from the PKA RAM */
1120   size = (hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] + 7UL) / 8UL;
1121 
1122   /* If a destination buffer is provided */
1123   if (out != NULL)
1124   {
1125     /* Move the result to appropriate location (indicated in out parameter) */
1126     PKA_Memcpy_u32_to_u8(out->ptX, &hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_OUT_RESULT_X], size);
1127     PKA_Memcpy_u32_to_u8(out->ptY, &hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_OUT_RESULT_Y], size);
1128   }
1129 }
1130 
1131 /**
1132   * @brief  Arithmetic addition in blocking mode.
1133   * @param  hpka PKA handle
1134   * @param  in Input information
1135   * @param  Timeout Timeout duration
1136   * @retval HAL status
1137   */
HAL_PKA_Add(PKA_HandleTypeDef * hpka,PKA_AddInTypeDef * in,uint32_t Timeout)1138 HAL_StatusTypeDef HAL_PKA_Add(PKA_HandleTypeDef *hpka, PKA_AddInTypeDef *in, uint32_t Timeout)
1139 {
1140   /* Set input parameter in PKA RAM */
1141   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1142 
1143   /* Start the operation */
1144   return PKA_Process(hpka, PKA_MODE_ARITHMETIC_ADD, Timeout);
1145 }
1146 
1147 /**
1148   * @brief  Arithmetic addition in non-blocking mode with Interrupt.
1149   * @param  hpka PKA handle
1150   * @param  in Input information
1151   * @retval HAL status
1152   */
HAL_PKA_Add_IT(PKA_HandleTypeDef * hpka,PKA_AddInTypeDef * in)1153 HAL_StatusTypeDef HAL_PKA_Add_IT(PKA_HandleTypeDef *hpka, PKA_AddInTypeDef *in)
1154 {
1155   /* Set input parameter in PKA RAM */
1156   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1157 
1158   /* Start the operation */
1159   return PKA_Process_IT(hpka, PKA_MODE_ARITHMETIC_ADD);
1160 }
1161 
1162 /**
1163   * @brief  Arithmetic substraction in blocking mode.
1164   * @param  hpka PKA handle
1165   * @param  in Input information
1166   * @param  Timeout Timeout duration
1167   * @retval HAL status
1168   */
HAL_PKA_Sub(PKA_HandleTypeDef * hpka,PKA_SubInTypeDef * in,uint32_t Timeout)1169 HAL_StatusTypeDef HAL_PKA_Sub(PKA_HandleTypeDef *hpka, PKA_SubInTypeDef *in, uint32_t Timeout)
1170 {
1171   /* Set input parameter in PKA RAM */
1172   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1173 
1174   /* Start the operation */
1175   return PKA_Process(hpka, PKA_MODE_ARITHMETIC_SUB, Timeout);
1176 }
1177 
1178 /**
1179   * @brief  Arithmetic substraction in non-blocking mode with Interrupt.
1180   * @param  hpka PKA handle
1181   * @param  in Input information
1182   * @retval HAL status
1183   */
HAL_PKA_Sub_IT(PKA_HandleTypeDef * hpka,PKA_SubInTypeDef * in)1184 HAL_StatusTypeDef HAL_PKA_Sub_IT(PKA_HandleTypeDef *hpka, PKA_SubInTypeDef *in)
1185 {
1186   /* Set input parameter in PKA RAM */
1187   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1188 
1189   /* Start the operation */
1190   return PKA_Process_IT(hpka, PKA_MODE_ARITHMETIC_SUB);
1191 }
1192 
1193 /**
1194   * @brief  Arithmetic multiplication in blocking mode.
1195   * @param  hpka PKA handle
1196   * @param  in Input information
1197   * @param  Timeout Timeout duration
1198   * @retval HAL status
1199   */
HAL_PKA_Mul(PKA_HandleTypeDef * hpka,PKA_MulInTypeDef * in,uint32_t Timeout)1200 HAL_StatusTypeDef HAL_PKA_Mul(PKA_HandleTypeDef *hpka, PKA_MulInTypeDef *in, uint32_t Timeout)
1201 {
1202   /* Set input parameter in PKA RAM */
1203   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1204 
1205   /* Start the operation */
1206   return PKA_Process(hpka, PKA_MODE_ARITHMETIC_MUL, Timeout);
1207 }
1208 
1209 /**
1210   * @brief  Arithmetic multiplication in non-blocking mode with Interrupt.
1211   * @param  hpka PKA handle
1212   * @param  in Input information
1213   * @retval HAL status
1214   */
HAL_PKA_Mul_IT(PKA_HandleTypeDef * hpka,PKA_MulInTypeDef * in)1215 HAL_StatusTypeDef HAL_PKA_Mul_IT(PKA_HandleTypeDef *hpka, PKA_MulInTypeDef *in)
1216 {
1217   /* Set input parameter in PKA RAM */
1218   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1219 
1220   /* Start the operation */
1221   return PKA_Process_IT(hpka, PKA_MODE_ARITHMETIC_MUL);
1222 }
1223 
1224 /**
1225   * @brief  Comparison in blocking mode.
1226   * @param  hpka PKA handle
1227   * @param  in Input information
1228   * @param  Timeout Timeout duration
1229   * @retval HAL status
1230   */
HAL_PKA_Cmp(PKA_HandleTypeDef * hpka,PKA_CmpInTypeDef * in,uint32_t Timeout)1231 HAL_StatusTypeDef HAL_PKA_Cmp(PKA_HandleTypeDef *hpka, PKA_CmpInTypeDef *in, uint32_t Timeout)
1232 {
1233   /* Set input parameter in PKA RAM */
1234   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1235 
1236   /* Start the operation */
1237   return PKA_Process(hpka, PKA_MODE_COMPARISON, Timeout);
1238 }
1239 
1240 /**
1241   * @brief  Comparison in non-blocking mode with Interrupt.
1242   * @param  hpka PKA handle
1243   * @param  in Input information
1244   * @retval HAL status
1245   */
HAL_PKA_Cmp_IT(PKA_HandleTypeDef * hpka,PKA_CmpInTypeDef * in)1246 HAL_StatusTypeDef HAL_PKA_Cmp_IT(PKA_HandleTypeDef *hpka, PKA_CmpInTypeDef *in)
1247 {
1248   /* Set input parameter in PKA RAM */
1249   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
1250 
1251   /* Start the operation */
1252   return PKA_Process_IT(hpka, PKA_MODE_COMPARISON);
1253 }
1254 
1255 /**
1256   * @brief  Modular addition in blocking mode.
1257   * @param  hpka PKA handle
1258   * @param  in Input information
1259   * @param  Timeout Timeout duration
1260   * @retval HAL status
1261   */
HAL_PKA_ModAdd(PKA_HandleTypeDef * hpka,PKA_ModAddInTypeDef * in,uint32_t Timeout)1262 HAL_StatusTypeDef HAL_PKA_ModAdd(PKA_HandleTypeDef *hpka, PKA_ModAddInTypeDef *in, uint32_t Timeout)
1263 {
1264   /* Set input parameter in PKA RAM */
1265   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
1266 
1267   /* Start the operation */
1268   return PKA_Process(hpka, PKA_MODE_MODULAR_ADD, Timeout);
1269 }
1270 
1271 /**
1272   * @brief  Modular addition in non-blocking mode with Interrupt.
1273   * @param  hpka PKA handle
1274   * @param  in Input information
1275   * @retval HAL status
1276   */
HAL_PKA_ModAdd_IT(PKA_HandleTypeDef * hpka,PKA_ModAddInTypeDef * in)1277 HAL_StatusTypeDef HAL_PKA_ModAdd_IT(PKA_HandleTypeDef *hpka, PKA_ModAddInTypeDef *in)
1278 {
1279   /* Set input parameter in PKA RAM */
1280   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
1281 
1282   /* Start the operation */
1283   return PKA_Process_IT(hpka, PKA_MODE_MODULAR_ADD);
1284 }
1285 
1286 /**
1287   * @brief  Modular inversion in blocking mode.
1288   * @param  hpka PKA handle
1289   * @param  in Input information
1290   * @param  Timeout Timeout duration
1291   * @retval HAL status
1292   */
HAL_PKA_ModInv(PKA_HandleTypeDef * hpka,PKA_ModInvInTypeDef * in,uint32_t Timeout)1293 HAL_StatusTypeDef HAL_PKA_ModInv(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in, uint32_t Timeout)
1294 {
1295   /* Set input parameter in PKA RAM */
1296   PKA_ModInv_Set(hpka, in);
1297 
1298   /* Start the operation */
1299   return PKA_Process(hpka, PKA_MODE_MODULAR_INV, Timeout);
1300 }
1301 
1302 /**
1303   * @brief  Modular inversion in non-blocking mode with Interrupt.
1304   * @param  hpka PKA handle
1305   * @param  in Input information
1306   * @retval HAL status
1307   */
HAL_PKA_ModInv_IT(PKA_HandleTypeDef * hpka,PKA_ModInvInTypeDef * in)1308 HAL_StatusTypeDef HAL_PKA_ModInv_IT(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in)
1309 {
1310   /* Set input parameter in PKA RAM */
1311   PKA_ModInv_Set(hpka, in);
1312 
1313   /* Start the operation */
1314   return PKA_Process_IT(hpka, PKA_MODE_MODULAR_INV);
1315 }
1316 
1317 /**
1318   * @brief  Modular substraction in blocking mode.
1319   * @param  hpka PKA handle
1320   * @param  in Input information
1321   * @param  Timeout Timeout duration
1322   * @retval HAL status
1323   */
HAL_PKA_ModSub(PKA_HandleTypeDef * hpka,PKA_ModSubInTypeDef * in,uint32_t Timeout)1324 HAL_StatusTypeDef HAL_PKA_ModSub(PKA_HandleTypeDef *hpka, PKA_ModSubInTypeDef *in, uint32_t Timeout)
1325 {
1326   /* Set input parameter in PKA RAM */
1327   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
1328 
1329   /* Start the operation */
1330   return PKA_Process(hpka, PKA_MODE_MODULAR_SUB, Timeout);
1331 }
1332 
1333 /**
1334   * @brief  Modular substraction in non-blocking mode with Interrupt.
1335   * @param  hpka PKA handle
1336   * @param  in Input information
1337   * @retval HAL status
1338   */
HAL_PKA_ModSub_IT(PKA_HandleTypeDef * hpka,PKA_ModSubInTypeDef * in)1339 HAL_StatusTypeDef HAL_PKA_ModSub_IT(PKA_HandleTypeDef *hpka, PKA_ModSubInTypeDef *in)
1340 {
1341   /* Set input parameter in PKA RAM */
1342   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
1343 
1344   /* Start the operation */
1345   return PKA_Process_IT(hpka, PKA_MODE_MODULAR_SUB);
1346 }
1347 
1348 /**
1349   * @brief  Modular reduction in blocking mode.
1350   * @param  hpka PKA handle
1351   * @param  in Input information
1352   * @param  Timeout Timeout duration
1353   * @retval HAL status
1354   */
HAL_PKA_ModRed(PKA_HandleTypeDef * hpka,PKA_ModRedInTypeDef * in,uint32_t Timeout)1355 HAL_StatusTypeDef HAL_PKA_ModRed(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in, uint32_t Timeout)
1356 {
1357   /* Set input parameter in PKA RAM */
1358   PKA_ModRed_Set(hpka, in);
1359 
1360   /* Start the operation */
1361   return PKA_Process(hpka, PKA_MODE_MODULAR_RED, Timeout);
1362 }
1363 
1364 /**
1365   * @brief  Modular reduction in non-blocking mode with Interrupt.
1366   * @param  hpka PKA handle
1367   * @param  in Input information
1368   * @retval HAL status
1369   */
HAL_PKA_ModRed_IT(PKA_HandleTypeDef * hpka,PKA_ModRedInTypeDef * in)1370 HAL_StatusTypeDef HAL_PKA_ModRed_IT(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in)
1371 {
1372   /* Set input parameter in PKA RAM */
1373   PKA_ModRed_Set(hpka, in);
1374 
1375   /* Start the operation */
1376   return PKA_Process_IT(hpka, PKA_MODE_MODULAR_RED);
1377 }
1378 
1379 /**
1380   * @brief  Montgomery multiplication in blocking mode.
1381   * @param  hpka PKA handle
1382   * @param  in Input information
1383   * @param  Timeout Timeout duration
1384   * @retval HAL status
1385   */
HAL_PKA_MontgomeryMul(PKA_HandleTypeDef * hpka,PKA_MontgomeryMulInTypeDef * in,uint32_t Timeout)1386 HAL_StatusTypeDef HAL_PKA_MontgomeryMul(PKA_HandleTypeDef *hpka, PKA_MontgomeryMulInTypeDef *in, uint32_t Timeout)
1387 {
1388   /* Set input parameter in PKA RAM */
1389   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
1390 
1391   /* Start the operation */
1392   return PKA_Process(hpka, PKA_MODE_MONTGOMERY_MUL, Timeout);
1393 }
1394 
1395 /**
1396   * @brief  Montgomery multiplication in non-blocking mode with Interrupt.
1397   * @param  hpka PKA handle
1398   * @param  in Input information
1399   * @retval HAL status
1400   */
HAL_PKA_MontgomeryMul_IT(PKA_HandleTypeDef * hpka,PKA_MontgomeryMulInTypeDef * in)1401 HAL_StatusTypeDef HAL_PKA_MontgomeryMul_IT(PKA_HandleTypeDef *hpka, PKA_MontgomeryMulInTypeDef *in)
1402 {
1403   /* Set input parameter in PKA RAM */
1404   PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
1405 
1406   /* Start the operation */
1407   return PKA_Process_IT(hpka, PKA_MODE_MONTGOMERY_MUL);
1408 }
1409 
1410 /**
1411   * @brief  Retrieve operation result.
1412   * @param  hpka PKA handle
1413   * @param  pRes Pointer to memory location to receive the result of the operation
1414   */
HAL_PKA_Arithmetic_GetResult(PKA_HandleTypeDef * hpka,uint32_t * pRes)1415 void HAL_PKA_Arithmetic_GetResult(PKA_HandleTypeDef *hpka, uint32_t *pRes)
1416 {
1417   uint32_t mode = (hpka->Instance->CR & PKA_CR_MODE_Msk) >> PKA_CR_MODE_Pos;
1418   uint32_t size = 0;
1419 
1420   /* Move the result to appropriate location (indicated in pRes parameter) */
1421   switch (mode)
1422   {
1423     case PKA_MODE_ARITHMETIC_SUB:
1424     case PKA_MODE_MODULAR_ADD:
1425     case PKA_MODE_MODULAR_RED:
1426     case PKA_MODE_MODULAR_INV:
1427     case PKA_MODE_MODULAR_SUB:
1428     case PKA_MODE_MONTGOMERY_MUL:
1429       size = hpka->Instance->RAM[1] / 32UL;
1430       break;
1431     case PKA_MODE_ARITHMETIC_ADD:
1432       size = hpka->Instance->RAM[1] / 32UL;
1433 
1434       /* Manage the overflow of the addition */
1435       if (hpka->Instance->RAM[500U + size] != 0UL)
1436       {
1437         size += 1UL;
1438       }
1439 
1440       break;
1441     case PKA_MODE_COMPARISON:
1442       size = 1;
1443       break;
1444     case PKA_MODE_ARITHMETIC_MUL:
1445       size = hpka->Instance->RAM[1] / 32UL * 2UL;
1446       break;
1447     default:
1448       break;
1449   }
1450 
1451   if (pRes != NULL)
1452   {
1453     switch (mode)
1454     {
1455       case PKA_MODE_ARITHMETIC_SUB:
1456       case PKA_MODE_MODULAR_ADD:
1457       case PKA_MODE_MODULAR_RED:
1458       case PKA_MODE_MODULAR_INV:
1459       case PKA_MODE_MODULAR_SUB:
1460       case PKA_MODE_MONTGOMERY_MUL:
1461       case PKA_MODE_ARITHMETIC_ADD:
1462       case PKA_MODE_COMPARISON:
1463       case PKA_MODE_ARITHMETIC_MUL:
1464         PKA_Memcpy_u32_to_u32(pRes, &hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_OUT_RESULT], size);
1465         break;
1466       default:
1467         break;
1468     }
1469   }
1470 }
1471 
1472 /**
1473   * @brief  Montgomery parameter computation in blocking mode.
1474   * @param  hpka PKA handle
1475   * @param  in Input information
1476   * @param  Timeout Timeout duration
1477   * @retval HAL status
1478   */
HAL_PKA_MontgomeryParam(PKA_HandleTypeDef * hpka,PKA_MontgomeryParamInTypeDef * in,uint32_t Timeout)1479 HAL_StatusTypeDef HAL_PKA_MontgomeryParam(PKA_HandleTypeDef *hpka, PKA_MontgomeryParamInTypeDef *in, uint32_t Timeout)
1480 {
1481   /* Set input parameter in PKA RAM */
1482   PKA_MontgomeryParam_Set(hpka, in->size, in->pOp1);
1483 
1484   /* Start the operation */
1485   return PKA_Process(hpka, PKA_MODE_MONTGOMERY_PARAM, Timeout);
1486 }
1487 
1488 /**
1489   * @brief  Montgomery parameter computation in non-blocking mode with Interrupt.
1490   * @param  hpka PKA handle
1491   * @param  in Input information
1492   * @retval HAL status
1493   */
HAL_PKA_MontgomeryParam_IT(PKA_HandleTypeDef * hpka,PKA_MontgomeryParamInTypeDef * in)1494 HAL_StatusTypeDef HAL_PKA_MontgomeryParam_IT(PKA_HandleTypeDef *hpka, PKA_MontgomeryParamInTypeDef *in)
1495 {
1496   /* Set input parameter in PKA RAM */
1497   PKA_MontgomeryParam_Set(hpka, in->size, in->pOp1);
1498 
1499   /* Start the operation */
1500   return PKA_Process_IT(hpka, PKA_MODE_MONTGOMERY_PARAM);
1501 }
1502 
1503 
1504 /**
1505   * @brief  Retrieve operation result.
1506   * @param  hpka PKA handle
1507   * @param  pRes pointer to buffer where the result will be copied
1508   * @retval HAL status
1509   */
HAL_PKA_MontgomeryParam_GetResult(PKA_HandleTypeDef * hpka,uint32_t * pRes)1510 void HAL_PKA_MontgomeryParam_GetResult(PKA_HandleTypeDef *hpka, uint32_t *pRes)
1511 {
1512   uint32_t size;
1513 
1514   /* Retrieve the size of the buffer from the PKA RAM */
1515   size = (hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MOD_NB_BITS] + 31UL) / 32UL;
1516 
1517   /* Move the result to appropriate location (indicated in out parameter) */
1518   PKA_Memcpy_u32_to_u32(pRes, &hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_OUT_PARAMETER], size);
1519 }
1520 
1521 /**
1522   * @brief  Abort any ongoing operation.
1523   * @param  hpka PKA handle
1524   * @retval HAL status
1525   */
HAL_PKA_Abort(PKA_HandleTypeDef * hpka)1526 HAL_StatusTypeDef HAL_PKA_Abort(PKA_HandleTypeDef *hpka)
1527 {
1528   HAL_StatusTypeDef err = HAL_OK;
1529 
1530   /* Clear EN bit */
1531   /* This abort any operation in progress (PKA RAM content is not guaranted in this case) */
1532   CLEAR_BIT(hpka->Instance->CR, PKA_CR_EN);
1533   SET_BIT(hpka->Instance->CR, PKA_CR_EN);
1534 
1535   /* Reset any pending flag */
1536   SET_BIT(hpka->Instance->CLRFR, PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
1537 
1538   /* Reset the error code */
1539   hpka->ErrorCode = HAL_PKA_ERROR_NONE;
1540 
1541   /* Reset the state */
1542   hpka->State = HAL_PKA_STATE_READY;
1543 
1544   return err;
1545 }
1546 
1547 /**
1548   * @brief  Reset the PKA RAM.
1549   * @param  hpka PKA handle
1550   * @retval None
1551   */
HAL_PKA_RAMReset(PKA_HandleTypeDef * hpka)1552 void HAL_PKA_RAMReset(PKA_HandleTypeDef *hpka)
1553 {
1554   uint32_t index;
1555 
1556   /* For each element in the PKA RAM */
1557   for (index = 0; index < PKA_RAM_SIZE; index++)
1558   {
1559     /* Clear the content */
1560     hpka->Instance->RAM[index] = 0UL;
1561   }
1562 }
1563 
1564 /**
1565   * @brief  This function handles PKA event interrupt request.
1566   * @param  hpka PKA handle
1567   * @retval None
1568   */
HAL_PKA_IRQHandler(PKA_HandleTypeDef * hpka)1569 void HAL_PKA_IRQHandler(PKA_HandleTypeDef *hpka)
1570 {
1571   uint32_t mode = PKA_GetMode(hpka);
1572   FlagStatus addErrFlag = __HAL_PKA_GET_FLAG(hpka, PKA_FLAG_ADDRERR);
1573   FlagStatus ramErrFlag = __HAL_PKA_GET_FLAG(hpka, PKA_FLAG_RAMERR);
1574   FlagStatus procEndFlag = __HAL_PKA_GET_FLAG(hpka, PKA_FLAG_PROCEND);
1575 
1576   /* Address error interrupt occurred */
1577   if ((__HAL_PKA_GET_IT_SOURCE(hpka, PKA_IT_ADDRERR) == SET) && (addErrFlag == SET))
1578   {
1579     hpka->ErrorCode |= HAL_PKA_ERROR_ADDRERR;
1580 
1581     /* Clear ADDRERR flag */
1582     __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_ADDRERR);
1583   }
1584 
1585   /* RAM access error interrupt occurred */
1586   if ((__HAL_PKA_GET_IT_SOURCE(hpka, PKA_IT_RAMERR) == SET) && (ramErrFlag == SET))
1587   {
1588     hpka->ErrorCode |= HAL_PKA_ERROR_RAMERR;
1589 
1590     /* Clear RAMERR flag */
1591     __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_RAMERR);
1592   }
1593 
1594   /* Check the operation success in case of ECDSA signature */
1595   if (mode == PKA_MODE_ECDSA_SIGNATURE)
1596   {
1597     /* If error output result is different from 0, ecdsa sign operation need to be repeated */
1598     if (hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_ERROR] != 0UL)
1599     {
1600       hpka->ErrorCode |= HAL_PKA_ERROR_OPERATION;
1601     }
1602   }
1603   /* Trigger the error callback if an error is present */
1604   if (hpka->ErrorCode != HAL_PKA_ERROR_NONE)
1605   {
1606 #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
1607     hpka->ErrorCallback(hpka);
1608 #else
1609     HAL_PKA_ErrorCallback(hpka);
1610 #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
1611   }
1612 
1613   /* End Of Operation interrupt occurred */
1614   if ((__HAL_PKA_GET_IT_SOURCE(hpka, PKA_IT_PROCEND) == SET) && (procEndFlag == SET))
1615   {
1616     /* Clear PROCEND flag */
1617     __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_PROCEND);
1618 
1619     /* Set the state to ready */
1620     hpka->State = HAL_PKA_STATE_READY;
1621 
1622 #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
1623     hpka->OperationCpltCallback(hpka);
1624 #else
1625     HAL_PKA_OperationCpltCallback(hpka);
1626 #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
1627   }
1628 }
1629 
1630 /**
1631   * @brief  Process completed callback.
1632   * @param  hpka PKA handle
1633   * @retval None
1634   */
HAL_PKA_OperationCpltCallback(PKA_HandleTypeDef * hpka)1635 __weak void HAL_PKA_OperationCpltCallback(PKA_HandleTypeDef *hpka)
1636 {
1637   /* Prevent unused argument(s) compilation warning */
1638   UNUSED(hpka);
1639 
1640   /* NOTE : This function should not be modified, when the callback is needed,
1641             the HAL_PKA_OperationCpltCallback could be implemented in the user file
1642    */
1643 }
1644 
1645 /**
1646   * @brief  Error callback.
1647   * @param  hpka PKA handle
1648   * @retval None
1649   */
HAL_PKA_ErrorCallback(PKA_HandleTypeDef * hpka)1650 __weak void HAL_PKA_ErrorCallback(PKA_HandleTypeDef *hpka)
1651 {
1652   /* Prevent unused argument(s) compilation warning */
1653   UNUSED(hpka);
1654 
1655   /* NOTE : This function should not be modified, when the callback is needed,
1656             the HAL_PKA_ErrorCallback could be implemented in the user file
1657    */
1658 }
1659 
1660 /**
1661   * @}
1662   */
1663 
1664 /** @defgroup PKA_Exported_Functions_Group3 Peripheral State and Error functions
1665  *  @brief   Peripheral State and Error functions
1666  *
1667   @verbatim
1668  ===============================================================================
1669             ##### Peripheral State and Error functions #####
1670  ===============================================================================
1671     [..]
1672     This subsection permit to get in run-time the status of the peripheral.
1673 
1674 @endverbatim
1675   * @{
1676   */
1677 
1678 /**
1679   * @brief  Return the PKA handle state.
1680   * @param  hpka PKA handle
1681   * @retval HAL status
1682   */
HAL_PKA_GetState(PKA_HandleTypeDef * hpka)1683 HAL_PKA_StateTypeDef HAL_PKA_GetState(PKA_HandleTypeDef *hpka)
1684 {
1685   /* Return PKA handle state */
1686   return hpka->State;
1687 }
1688 
1689 /**
1690   * @brief  Return the PKA error code.
1691   * @param  hpka PKA handle
1692   * @retval PKA error code
1693 */
HAL_PKA_GetError(PKA_HandleTypeDef * hpka)1694 uint32_t HAL_PKA_GetError(PKA_HandleTypeDef *hpka)
1695 {
1696   /* Return PKA handle error code */
1697   return hpka->ErrorCode;
1698 }
1699 
1700 /**
1701   * @}
1702   */
1703 
1704 /**
1705   * @}
1706   */
1707 
1708 /** @addtogroup PKA_Private_Functions
1709   * @{
1710   */
1711 
1712 /**
1713   * @brief  Get PKA operating mode.
1714   * @param  hpka PKA handle
1715   * @retval Return the current mode
1716   */
PKA_GetMode(PKA_HandleTypeDef * hpka)1717 uint32_t PKA_GetMode(PKA_HandleTypeDef *hpka)
1718 {
1719   /* return the shifted PKA_CR_MODE value */
1720   return (uint32_t)(READ_BIT(hpka->Instance->CR, PKA_CR_MODE) >> PKA_CR_MODE_Pos);
1721 }
1722 
1723 /**
1724   * @brief  Wait for operation completion or timeout.
1725   * @param  hpka PKA handle
1726   * @param  Timeout Timeout duration in millisecond.
1727   * @param  Tickstart Tick start value
1728   * @retval HAL status
1729   */
PKA_PollEndOfOperation(PKA_HandleTypeDef * hpka,uint32_t Timeout,uint32_t Tickstart)1730 HAL_StatusTypeDef PKA_PollEndOfOperation(PKA_HandleTypeDef *hpka, uint32_t Timeout, uint32_t Tickstart)
1731 {
1732   /* Wait for the end of operation or timeout */
1733   while ((hpka->Instance->SR & PKA_SR_PROCENDF) == 0UL)
1734   {
1735     /* Check if timeout is disabled (set to infinite wait) */
1736     if (Timeout != HAL_MAX_DELAY)
1737     {
1738       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0UL))
1739       {
1740         return HAL_TIMEOUT;
1741       }
1742     }
1743   }
1744   return HAL_OK;
1745 }
1746 
1747 /**
1748   * @brief  Return a hal error code based on PKA error flags.
1749   * @param  hpka PKA handle
1750   * @param  mode PKA operating mode
1751   * @retval error code
1752   */
PKA_CheckError(PKA_HandleTypeDef * hpka,uint32_t mode)1753 uint32_t PKA_CheckError(PKA_HandleTypeDef *hpka, uint32_t mode)
1754 {
1755   uint32_t err = HAL_PKA_ERROR_NONE;
1756 
1757   /* Check RAMERR error */
1758   if (__HAL_PKA_GET_FLAG(hpka, PKA_FLAG_RAMERR) == SET)
1759   {
1760     err |= HAL_PKA_ERROR_RAMERR;
1761   }
1762 
1763   /* Check ADDRERR error */
1764   if (__HAL_PKA_GET_FLAG(hpka, PKA_FLAG_ADDRERR) == SET)
1765   {
1766     err |= HAL_PKA_ERROR_ADDRERR;
1767   }
1768 
1769   /* Check the operation success in case of ECDSA signature */
1770   if (mode == PKA_MODE_ECDSA_SIGNATURE)
1771   {
1772 #define EDCSA_SIGN_NOERROR 0UL
1773     /* If error output result is different from no error, ecsa sign operation need to be repeated */
1774     if (hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_ERROR] != EDCSA_SIGN_NOERROR)
1775     {
1776       err |= HAL_PKA_ERROR_OPERATION;
1777     }
1778   }
1779 
1780   return err;
1781 }
1782 
1783 /**
1784   * @brief  Get number of bits inside an array of u8.
1785   * @param  byteNumber Number of u8 inside the array
1786   */
PKA_GetBitSize_u8(uint32_t byteNumber)1787 uint32_t PKA_GetBitSize_u8(uint32_t byteNumber)
1788 {
1789   /* Convert from number of uint8_t in an array to the associated number of bits in this array */
1790   return byteNumber * 8UL;
1791 }
1792 
1793 /**
1794   * @brief  Get optimal number of bits inside an array of u8.
1795   * @param  byteNumber Number of u8 inside the array
1796   * @param  msb Most significant uint8_t of the array
1797   */
PKA_GetOptBitSize_u8(uint32_t byteNumber,uint8_t msb)1798 uint32_t PKA_GetOptBitSize_u8(uint32_t byteNumber, uint8_t msb)
1799 {
1800   uint32_t position;
1801 
1802   position = 32UL - __CLZ(msb);
1803 
1804   return (((byteNumber - 1UL) * 8UL) + position);
1805 }
1806 
1807 /**
1808   * @brief  Get number of bits inside an array of u32.
1809   * @param  wordNumber Number of u32 inside the array
1810   */
PKA_GetBitSize_u32(uint32_t wordNumber)1811 uint32_t PKA_GetBitSize_u32(uint32_t wordNumber)
1812 {
1813   /* Convert from number of uint32_t in an array to the associated number of bits in this array */
1814   return wordNumber * 32UL;
1815 }
1816 
1817 /**
1818   * @brief  Get number of uint8_t element in an array of bitSize bits.
1819   * @param  bitSize Number of bits in an array
1820   */
PKA_GetArraySize_u8(uint32_t bitSize)1821 uint32_t PKA_GetArraySize_u8(uint32_t bitSize)
1822 {
1823   /* Manage the non aligned on uint8_t bitsize: */
1824   /*   512 bits requires 64 uint8_t             */
1825   /*   521 bits requires 66 uint8_t             */
1826   return ((bitSize + 7UL) / 8UL);
1827 }
1828 
1829 /**
1830   * @brief  Copy uint32_t array to uint8_t array to fit PKA number representation.
1831   * @param  dst Pointer to destination
1832   * @param  src Pointer to source
1833   * @param  n Number of uint8_t to copy
1834   * @retval dst
1835   */
PKA_Memcpy_u32_to_u8(uint8_t dst[],__IO const uint32_t src[],size_t n)1836 void PKA_Memcpy_u32_to_u8(uint8_t dst[], __IO const uint32_t src[], size_t n)
1837 {
1838   if (dst != NULL)
1839   {
1840     if (src != NULL)
1841     {
1842       uint32_t index_uint32_t = 0UL; /* This index is used outside of the loop */
1843 
1844       for (; index_uint32_t < (n / 4UL); index_uint32_t++)
1845       {
1846         /* Avoid casting from uint8_t* to uint32_t* by copying 4 uint8_t in a row */
1847         /* Apply __REV equivalent */
1848         uint32_t index_uint8_t = n - 4UL - (index_uint32_t * 4UL);
1849         dst[index_uint8_t + 3UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
1850         dst[index_uint8_t + 2UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
1851         dst[index_uint8_t + 1UL] = (uint8_t)((src[index_uint32_t] & 0x00FF0000U) >> 16UL);
1852         dst[index_uint8_t + 0UL] = (uint8_t)((src[index_uint32_t] & 0xFF000000U) >> 24UL);
1853       }
1854 
1855       /* Manage the buffers not aligned on uint32_t */
1856       if ((n % 4UL) == 1UL)
1857       {
1858         dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
1859       }
1860       else if ((n % 4UL) == 2UL)
1861       {
1862         dst[1UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
1863         dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
1864       }
1865       else if ((n % 4UL) == 3UL)
1866       {
1867         dst[2UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
1868         dst[1UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
1869         dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x00FF0000U) >> 16UL);
1870       }
1871       else
1872       {
1873         /* The last element is already handle in the loop */
1874       }
1875     }
1876   }
1877 }
1878 
1879 /**
1880   * @brief  Copy uint8_t array to uint32_t array to fit PKA number representation.
1881   * @param  dst Pointer to destination
1882   * @param  src Pointer to source
1883   * @param  n Number of uint8_t to copy (must be multiple of 4)
1884   * @retval dst
1885   */
PKA_Memcpy_u8_to_u32(__IO uint32_t dst[],const uint8_t src[],size_t n)1886 void PKA_Memcpy_u8_to_u32(__IO uint32_t dst[], const uint8_t src[], size_t n)
1887 {
1888   if (dst != NULL)
1889   {
1890     if (src != NULL)
1891     {
1892       uint32_t index = 0UL; /* This index is used outside of the loop */
1893 
1894       for (; index < (n / 4UL); index++)
1895       {
1896         /* Apply the equivalent of __REV from uint8_t to uint32_t */
1897         dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
1898                      | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL) \
1899                      | ((uint32_t)src[(n - (index * 4UL) - 3UL)] << 16UL) \
1900                      | ((uint32_t)src[(n - (index * 4UL) - 4UL)] << 24UL);
1901       }
1902 
1903       /* Manage the buffers not aligned on uint32_t */
1904       if ((n % 4UL) == 1UL)
1905       {
1906         dst[index] = (uint32_t)src[(n - (index * 4UL) - 1UL)];
1907       }
1908       else if ((n % 4UL) == 2UL)
1909       {
1910         dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
1911                      | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL);
1912       }
1913       else if ((n % 4UL) == 3UL)
1914       {
1915         dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
1916                      | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL) \
1917                      | ((uint32_t)src[(n - (index * 4UL) - 3UL)] << 16UL);
1918       }
1919       else
1920       {
1921         /* The last element is already handle in the loop */
1922       }
1923     }
1924   }
1925 }
1926 
1927 /**
1928   * @brief  Copy uint32_t array to uint32_t array.
1929   * @param  dst Pointer to destination
1930   * @param  src Pointer to source
1931   * @param  n Number of u32 to be handled
1932   * @retval dst
1933   */
PKA_Memcpy_u32_to_u32(__IO uint32_t dst[],__IO const uint32_t src[],size_t n)1934 void PKA_Memcpy_u32_to_u32(__IO uint32_t dst[], __IO const uint32_t src[], size_t n)
1935 {
1936   /* If a destination buffer is provided */
1937   if (dst != NULL)
1938   {
1939     /* If a source buffer is provided */
1940     if (src != NULL)
1941     {
1942       /* For each element in the array */
1943       for (uint32_t index = 0UL; index < n; index++)
1944       {
1945         /* Copy the content */
1946         dst[index] = src[index];
1947       }
1948     }
1949   }
1950 }
1951 
1952 /**
1953   * @brief  Generic function to start a PKA operation in blocking mode.
1954   * @param  hpka PKA handle
1955   * @param  mode PKA operation
1956   * @param  Timeout Timeout duration
1957   * @retval HAL status
1958   */
PKA_Process(PKA_HandleTypeDef * hpka,uint32_t mode,uint32_t Timeout)1959 HAL_StatusTypeDef PKA_Process(PKA_HandleTypeDef *hpka, uint32_t mode, uint32_t Timeout)
1960 {
1961   HAL_StatusTypeDef err = HAL_OK;
1962   uint32_t tickstart;
1963 
1964   if (hpka->State == HAL_PKA_STATE_READY)
1965   {
1966     /* Set the state to busy */
1967     hpka->State = HAL_PKA_STATE_BUSY;
1968 
1969     /* Clear any pending error */
1970     hpka->ErrorCode = HAL_PKA_ERROR_NONE;
1971 
1972     /* Init tickstart for timeout management*/
1973     tickstart = HAL_GetTick();
1974 
1975     /* Set the mode and deactivate the interrupts */
1976     MODIFY_REG(hpka->Instance->CR, PKA_CR_MODE | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE, mode << PKA_CR_MODE_Pos);
1977 
1978     /* Start the computation */
1979     hpka->Instance->CR |= PKA_CR_START;
1980 
1981     /* Wait for the end of operation or timeout */
1982     if (PKA_PollEndOfOperation(hpka, Timeout, tickstart) != HAL_OK)
1983     {
1984       /* Abort any ongoing operation */
1985       CLEAR_BIT(hpka->Instance->CR, PKA_CR_EN);
1986 
1987       hpka->ErrorCode |= HAL_PKA_ERROR_TIMEOUT;
1988 
1989       /* Make ready for the next operation */
1990       SET_BIT(hpka->Instance->CR, PKA_CR_EN);
1991     }
1992 
1993     /* Check error */
1994     hpka->ErrorCode |= PKA_CheckError(hpka, mode);
1995 
1996     /* Clear all flags */
1997     hpka->Instance->CLRFR |= (PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
1998 
1999     /* Set the state to ready */
2000     hpka->State = HAL_PKA_STATE_READY;
2001 
2002     /* Manage the result based on encountered errors */
2003     if (hpka->ErrorCode != HAL_PKA_ERROR_NONE)
2004     {
2005       err = HAL_ERROR;
2006     }
2007   }
2008   else
2009   {
2010     err = HAL_ERROR;
2011   }
2012   return err;
2013 }
2014 
2015 /**
2016   * @brief  Generic function to start a PKA operation in non-blocking mode with Interrupt.
2017   * @param  hpka PKA handle
2018   * @param  mode PKA operation
2019   * @retval HAL status
2020   */
PKA_Process_IT(PKA_HandleTypeDef * hpka,uint32_t mode)2021 HAL_StatusTypeDef PKA_Process_IT(PKA_HandleTypeDef *hpka, uint32_t mode)
2022 {
2023   HAL_StatusTypeDef err = HAL_OK;
2024 
2025   if (hpka->State == HAL_PKA_STATE_READY)
2026   {
2027     /* Set the state to busy */
2028     hpka->State = HAL_PKA_STATE_BUSY;
2029 
2030     /* Clear any pending error */
2031     hpka->ErrorCode = HAL_PKA_ERROR_NONE;
2032 
2033     /* Set the mode and activate interrupts */
2034     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);
2035 
2036     /* Start the computation */
2037     hpka->Instance->CR |= PKA_CR_START;
2038   }
2039   else
2040   {
2041     err = HAL_ERROR;
2042   }
2043   return err;
2044 }
2045 
2046 /**
2047   * @brief  Set input parameters.
2048   * @param  hpka PKA handle
2049   * @param  in Input information
2050   */
PKA_ModExp_Set(PKA_HandleTypeDef * hpka,PKA_ModExpInTypeDef * in)2051 void PKA_ModExp_Set(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in)
2052 {
2053   /* Get the number of bit per operand */
2054   hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = PKA_GetBitSize_u8(in->OpSize);
2055 
2056   /* Get the number of bit of the exponent */
2057   hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = PKA_GetBitSize_u8(in->expSize);
2058 
2059   /* Move the input parameters pOp1 to PKA RAM */
2060   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT_BASE], in->pOp1, in->OpSize);
2061   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT_BASE + (in->OpSize / 4UL));
2062 
2063   /* Move the exponent to PKA RAM */
2064   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT], in->pExp, in->expSize);
2065   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT + (in->expSize / 4UL));
2066 
2067   /* Move the modulus to PKA RAM */
2068   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MODULUS], in->pMod, in->OpSize);
2069   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MODULUS + (in->OpSize / 4UL));
2070 }
2071 
2072 /**
2073   * @brief  Set input parameters.
2074   * @param  hpka PKA handle
2075   * @param  in Input information
2076   */
PKA_ModExpFastMode_Set(PKA_HandleTypeDef * hpka,PKA_ModExpFastModeInTypeDef * in)2077 void PKA_ModExpFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in)
2078 {
2079   /* Get the number of bit per operand */
2080   hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = PKA_GetBitSize_u8(in->OpSize);
2081 
2082   /* Get the number of bit of the exponent */
2083   hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = PKA_GetBitSize_u8(in->expSize);
2084 
2085   /* Move the input parameters pOp1 to PKA RAM */
2086   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT_BASE], in->pOp1, in->OpSize);
2087   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT_BASE + (in->OpSize / 4UL));
2088 
2089   /* Move the exponent to PKA RAM */
2090   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT], in->pExp, in->expSize);
2091   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT + (in->expSize / 4UL));
2092 
2093   /* Move the modulus to PKA RAM */
2094   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MODULUS], in->pMod, in->OpSize);
2095   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MODULUS + (in->OpSize / 4UL));
2096 
2097   /* Move the Montgomery parameter to PKA RAM */
2098   PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MONTGOMERY_PARAM], in->pMontgomeryParam, in->expSize / 4UL);
2099   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MONTGOMERY_PARAM + (in->expSize / 4UL));
2100 }
2101 
2102 
2103 /**
2104   * @brief  Set input parameters.
2105   * @param  hpka PKA handle
2106   * @param  in Input information
2107   */
PKA_ECDSASign_Set(PKA_HandleTypeDef * hpka,PKA_ECDSASignInTypeDef * in)2108 void PKA_ECDSASign_Set(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in)
2109 {
2110   /* Get the prime order n length */
2111   hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS] = PKA_GetOptBitSize_u8(in->primeOrderSize, *(in->primeOrder));
2112 
2113   /* Get the modulus p length */
2114   hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2115 
2116   /* Get the coefficient a sign */
2117   hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN] = in->coefSign;
2118 
2119   /* Move the input parameters coefficient |a| to PKA RAM */
2120   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_A_COEFF], in->coef, in->modulusSize);
2121   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2122 
2123   /* Move the input parameters modulus value p to PKA RAM */
2124   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_GF], in->modulus, in->modulusSize);
2125   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2126 
2127   /* Move the input parameters integer k to PKA RAM */
2128   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_K], in->integer, in->primeOrderSize);
2129   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_K + ((in->primeOrderSize + 3UL) / 4UL));
2130 
2131   /* Move the input parameters base point G coordinate x to PKA RAM */
2132   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_INITIAL_POINT_X], in->basePointX, in->modulusSize);
2133   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2134 
2135   /* Move the input parameters base point G coordinate y to PKA RAM */
2136   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y], in->basePointY, in->modulusSize);
2137   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2138 
2139   /* Move the input parameters hash of message z to PKA RAM */
2140   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_HASH_E], in->hash, in->primeOrderSize);
2141   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_HASH_E + ((in->primeOrderSize + 3UL) / 4UL));
2142 
2143   /* Move the input parameters private key d to PKA RAM */
2144   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D], in->privateKey, in->primeOrderSize);
2145   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D + ((in->primeOrderSize + 3UL) / 4UL));
2146 
2147   /* Move the input parameters prime order n to PKA RAM */
2148   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_ORDER_N], in->primeOrder, in->primeOrderSize);
2149   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_ORDER_N + ((in->primeOrderSize + 3UL) / 4UL));
2150 }
2151 
2152 /**
2153   * @brief  Set input parameters.
2154   * @param  hpka PKA handle
2155   * @param  in Input information
2156   */
PKA_ECDSAVerif_Set(PKA_HandleTypeDef * hpka,PKA_ECDSAVerifInTypeDef * in)2157 void PKA_ECDSAVerif_Set(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in)
2158 {
2159   /* Get the prime order n length */
2160   hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS] = PKA_GetOptBitSize_u8(in->primeOrderSize, *(in->primeOrder));
2161 
2162   /* Get the modulus p length */
2163   hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2164 
2165   /* Get the coefficient a sign */
2166   hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN] = in->coefSign;
2167 
2168   /* Move the input parameters coefficient |a| to PKA RAM */
2169   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_A_COEFF], in->coef, in->modulusSize);
2170   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2171 
2172   /* Move the input parameters modulus value p to PKA RAM */
2173   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_MOD_GF], in->modulus, in->modulusSize);
2174   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2175 
2176   /* Move the input parameters base point G coordinate x to PKA RAM */
2177   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_INITIAL_POINT_X], in->basePointX, in->modulusSize);
2178   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2179 
2180   /* Move the input parameters base point G coordinate y to PKA RAM */
2181   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y], in->basePointY, in->modulusSize);
2182   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2183 
2184   /* Move the input parameters public-key curve point Q coordinate xQ to PKA RAM */
2185   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X], in->pPubKeyCurvePtX, in->modulusSize);
2186   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2187 
2188   /* Move the input parameters public-key curve point Q coordinate xQ to PKA RAM */
2189   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y], in->pPubKeyCurvePtY, in->modulusSize);
2190   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2191 
2192   /* Move the input parameters signature part r to PKA RAM */
2193   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_SIGNATURE_R], in->RSign, in->primeOrderSize);
2194   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_SIGNATURE_R + ((in->primeOrderSize + 3UL) / 4UL));
2195 
2196   /* Move the input parameters signature part s to PKA RAM */
2197   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_SIGNATURE_S], in->SSign, in->primeOrderSize);
2198   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_SIGNATURE_S + ((in->primeOrderSize + 3UL) / 4UL));
2199 
2200   /* Move the input parameters hash of message z to PKA RAM */
2201   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_HASH_E], in->hash, in->primeOrderSize);
2202   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_HASH_E + ((in->primeOrderSize + 3UL) / 4UL));
2203 
2204   /* Move the input parameters curve prime order n to PKA RAM */
2205   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_ORDER_N], in->primeOrder, in->primeOrderSize);
2206   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_ORDER_N + ((in->primeOrderSize + 3UL) / 4UL));
2207 }
2208 
2209 /**
2210   * @brief  Set input parameters.
2211   * @param  hpka PKA handle
2212   * @param  in Input information
2213   */
PKA_RSACRTExp_Set(PKA_HandleTypeDef * hpka,PKA_RSACRTExpInTypeDef * in)2214 void PKA_RSACRTExp_Set(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in)
2215 {
2216   /* Get the operand length M */
2217   hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] = PKA_GetBitSize_u8(in->size);
2218 
2219   /* Move the input parameters operand dP to PKA RAM */
2220   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_DP_CRT], in->pOpDp, in->size / 2UL);
2221   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_DP_CRT + (in->size / 8UL));
2222 
2223   /* Move the input parameters operand dQ to PKA RAM */
2224   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_DQ_CRT], in->pOpDq, in->size / 2UL);
2225   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_DQ_CRT + (in->size / 8UL));
2226 
2227   /* Move the input parameters operand qinv to PKA RAM */
2228   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_QINV_CRT], in->pOpQinv, in->size / 2UL);
2229   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_QINV_CRT + (in->size / 8UL));
2230 
2231   /* Move the input parameters prime p to PKA RAM */
2232   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_PRIME_P], in->pPrimeP, in->size / 2UL);
2233   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_PRIME_P + (in->size / 8UL));
2234 
2235   /* Move the input parameters prime q to PKA RAM */
2236   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_PRIME_Q], in->pPrimeQ, in->size / 2UL);
2237   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_PRIME_Q + (in->size / 8UL));
2238 
2239   /* Move the input parameters operand A to PKA RAM */
2240   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_EXPONENT_BASE], in->popA, in->size);
2241   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_EXPONENT_BASE + (in->size / 4UL));
2242 }
2243 
2244 /**
2245   * @brief  Set input parameters.
2246   * @param  hpka PKA handle
2247   * @param  in Input information
2248   */
PKA_PointCheck_Set(PKA_HandleTypeDef * hpka,PKA_PointCheckInTypeDef * in)2249 void PKA_PointCheck_Set(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in)
2250 {
2251   /* Get the modulus length */
2252   hpka->Instance->RAM[PKA_POINT_CHECK_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2253 
2254   /* Get the coefficient a sign */
2255   hpka->Instance->RAM[PKA_POINT_CHECK_IN_A_COEFF_SIGN] = in->coefSign;
2256 
2257   /* Move the input parameters coefficient |a| to PKA RAM */
2258   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_A_COEFF], in->coefA, in->modulusSize);
2259   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2260 
2261   /* Move the input parameters coefficient b to PKA RAM */
2262   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_B_COEFF], in->coefB, in->modulusSize);
2263   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_B_COEFF + ((in->modulusSize + 3UL) / 4UL));
2264 
2265   /* Move the input parameters modulus value p to PKA RAM */
2266   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_MOD_GF], in->modulus, in->modulusSize);
2267   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2268 
2269   /* Move the input parameters Point P coordinate x to PKA RAM */
2270   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
2271   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2272 
2273   /* Move the input parameters Point P coordinate y to PKA RAM */
2274   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
2275   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2276 }
2277 
2278 /**
2279   * @brief  Set input parameters.
2280   * @param  hpka PKA handle
2281   * @param  in Input information
2282   */
PKA_ECCMul_Set(PKA_HandleTypeDef * hpka,PKA_ECCMulInTypeDef * in)2283 void PKA_ECCMul_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in)
2284 {
2285   /* Get the scalar multiplier k length */
2286   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = PKA_GetOptBitSize_u8(in->scalarMulSize, *(in->scalarMul));
2287 
2288   /* Get the modulus length */
2289   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2290 
2291   /* Get the coefficient a sign */
2292   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = in->coefSign;
2293 
2294   /* Move the input parameters coefficient |a| to PKA RAM */
2295   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF], in->coefA, in->modulusSize);
2296   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2297 
2298 
2299   /* Move the input parameters modulus value p to PKA RAM */
2300   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MOD_GF], in->modulus, in->modulusSize);
2301   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2302 
2303   /* Move the input parameters scalar multiplier k to PKA RAM */
2304   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_K], in->scalarMul, in->scalarMulSize);
2305   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_K + ((in->scalarMulSize + 3UL) / 4UL));
2306 
2307   /* Move the input parameters Point P coordinate x to PKA RAM */
2308   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
2309   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2310 
2311   /* Move the input parameters Point P coordinate y to PKA RAM */
2312   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
2313   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2314 
2315 }
2316 
2317 
2318 /**
2319   * @brief  Set input parameters.
2320   * @param  hpka PKA handle
2321   * @param  in Input information
2322   */
PKA_ECCMulFastMode_Set(PKA_HandleTypeDef * hpka,PKA_ECCMulFastModeInTypeDef * in)2323 void PKA_ECCMulFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in)
2324 {
2325   /* Get the scalar multiplier k length */
2326   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = PKA_GetOptBitSize_u8(in->scalarMulSize, *(in->scalarMul));
2327 
2328   /* Get the modulus length */
2329   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
2330 
2331   /* Get the coefficient a sign */
2332   hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = in->coefSign;
2333 
2334   /* Move the input parameters coefficient |a| to PKA RAM */
2335   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF], in->coefA, in->modulusSize);
2336   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
2337 
2338   /* Move the input parameters modulus value p to PKA RAM */
2339   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MOD_GF], in->modulus, in->modulusSize);
2340   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
2341 
2342   /* Move the input parameters scalar multiplier k to PKA RAM */
2343   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_K], in->scalarMul, in->scalarMulSize);
2344   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_K + ((in->scalarMulSize + 3UL) / 4UL));
2345 
2346   /* Move the input parameters Point P coordinate x to PKA RAM */
2347   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
2348   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
2349 
2350   /* Move the input parameters Point P coordinate y to PKA RAM */
2351   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
2352   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
2353 
2354   /* Move the Montgomery parameter to PKA RAM */
2355   PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MONTGOMERY_PARAM], in->pMontgomeryParam, (in->modulusSize + 3UL) / 4UL);
2356   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MONTGOMERY_PARAM + ((in->modulusSize + 3UL) / 4UL));
2357 }
2358 /**
2359   * @brief  Set input parameters.
2360   * @param  hpka PKA handle
2361   * @param  in Input information
2362   */
PKA_ModInv_Set(PKA_HandleTypeDef * hpka,PKA_ModInvInTypeDef * in)2363 void PKA_ModInv_Set(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in)
2364 {
2365   /* Get the number of bit per operand */
2366   hpka->Instance->RAM[PKA_MODULAR_INV_NB_BITS] = PKA_GetBitSize_u32(in->size);
2367 
2368   /* Move the input parameters operand A to PKA RAM */
2369   PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_INV_IN_OP1], in->pOp1, in->size);
2370   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_INV_IN_OP1 + in->size);
2371 
2372   /* Move the input parameters modulus value n to PKA RAM */
2373   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_INV_IN_OP2_MOD], in->pMod, in->size * 4UL);
2374   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_INV_IN_OP2_MOD + in->size);
2375 }
2376 
2377 /**
2378   * @brief  Set input parameters.
2379   * @param  hpka PKA handle
2380   * @param  in Input information
2381   */
PKA_ModRed_Set(PKA_HandleTypeDef * hpka,PKA_ModRedInTypeDef * in)2382 void PKA_ModRed_Set(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in)
2383 {
2384   /* Get the number of bit per operand */
2385   hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_OP_LENGTH] = PKA_GetBitSize_u32(in->OpSize);
2386 
2387   /* Get the number of bit per modulus */
2388   hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_MOD_LENGTH] = PKA_GetBitSize_u8(in->modSize);
2389 
2390   /* Move the input parameters operand A to PKA RAM */
2391   PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_OPERAND], in->pOp1, in->OpSize);
2392   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_REDUC_IN_OPERAND + in->OpSize);
2393 
2394   /* Move the input parameters modulus value n to PKA RAM */
2395   PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_MODULUS], in->pMod, in->modSize);
2396   __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_REDUC_IN_MODULUS + (in->modSize / 4UL));
2397 }
2398 
2399 /**
2400   * @brief  Set input parameters.
2401   * @param  hpka PKA handle
2402   * @param  size Size of the operand
2403   * @param  pOp1 Generic pointer to input data
2404   */
PKA_MontgomeryParam_Set(PKA_HandleTypeDef * hpka,const uint32_t size,const uint8_t * pOp1)2405 void PKA_MontgomeryParam_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint8_t *pOp1)
2406 {
2407   if (pOp1 != NULL)
2408   {
2409     /* Get the number of bit per operand */
2410     hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(size, *pOp1);
2411 
2412     /* Move the input parameters pOp1 to PKA RAM */
2413     PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MODULUS], pOp1, size);
2414     __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MONTGOMERY_PARAM_IN_MODULUS + ((size + 3UL) / 4UL));
2415   }
2416 }
2417 
2418 /**
2419   * @brief  Generic function to set input parameters.
2420   * @param  hpka PKA handle
2421   * @param  size Size of the operand
2422   * @param  pOp1 Generic pointer to input data
2423   * @param  pOp2 Generic pointer to input data
2424   * @param  pOp3 Generic pointer to input data
2425   */
PKA_ARI_Set(PKA_HandleTypeDef * hpka,const uint32_t size,const uint32_t * pOp1,const uint32_t * pOp2,const uint8_t * pOp3)2426 void PKA_ARI_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint32_t *pOp1, const uint32_t *pOp2, const uint8_t *pOp3)
2427 {
2428   /* Get the number of bit per operand */
2429   hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_NB_BITS] = PKA_GetBitSize_u32(size);
2430 
2431   if (pOp1 != NULL)
2432   {
2433     /* Move the input parameters pOp1 to PKA RAM */
2434     PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP1], pOp1, size);
2435     __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP1 + size);
2436   }
2437 
2438   if (pOp2 != NULL)
2439   {
2440     /* Move the input parameters pOp2 to PKA RAM */
2441     PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP2], pOp2, size);
2442     __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP2 + size);
2443   }
2444 
2445   if (pOp3 != NULL)
2446   {
2447     /* Move the input parameters pOp3 to PKA RAM */
2448     PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP3], pOp3, size * 4UL);
2449     __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP3 + size);
2450   }
2451 }
2452 
2453 /**
2454   * @}
2455   */
2456 
2457 /**
2458   * @}
2459   */
2460 
2461 #endif /* defined(PKA) && defined(HAL_PKA_MODULE_ENABLED) */
2462 
2463 /**
2464   * @}
2465   */
2466 
2467 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2468