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