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