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