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