You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2439 lines
81 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_hal_pka.c
  4. * @author MCD Application Team
  5. * @brief PKA HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of public key accelerator(PKA):
  8. * + Initialization and de-initialization functions
  9. * + Start an operation
  10. * + Retrieve the operation result
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. The PKA HAL driver can be used as follows:
  18. (#) Declare a PKA_HandleTypeDef handle structure, for example: PKA_HandleTypeDef hpka;
  19. (#) Initialize the PKA low level resources by implementing the HAL_PKA_MspInit() API:
  20. (##) Enable the PKA interface clock
  21. (##) NVIC configuration if you need to use interrupt process
  22. (+++) Configure the PKA interrupt priority
  23. (+++) Enable the NVIC PKA IRQ Channel
  24. (#) Initialize the PKA registers by calling the HAL_PKA_Init() API which trig
  25. HAL_PKA_MspInit().
  26. (#) Fill entirely the input structure corresponding to your operation:
  27. For instance: PKA_ModExpInTypeDef for HAL_PKA_ModExp().
  28. (#) Execute the operation (in polling or interrupt) and check the returned value.
  29. (#) Retrieve the result of the operation (For instance, HAL_PKA_ModExp_GetResult for
  30. HAL_PKA_ModExp operation). The function to gather the result is different for each
  31. kind of operation. The correspondence can be found in the following section.
  32. (#) Call the function HAL_PKA_DeInit() to restore the default configuration which trig
  33. HAL_PKA_MspDeInit().
  34. *** High level operation ***
  35. =================================
  36. [..]
  37. (+) Input structure requires buffers as uint8_t array.
  38. (+) Output structure requires buffers as uint8_t array.
  39. (+) Modular exponentiation using:
  40. (++) HAL_PKA_ModExp().
  41. (++) HAL_PKA_ModExp_IT().
  42. (++) HAL_PKA_ModExpFastMode().
  43. (++) HAL_PKA_ModExpFastMode_IT().
  44. (++) HAL_PKA_ModExp_GetResult() to retrieve the result of the operation.
  45. (+) RSA Chinese Remainder Theorem (CRT) using:
  46. (++) HAL_PKA_RSACRTExp().
  47. (++) HAL_PKA_RSACRTExp_IT().
  48. (++) HAL_PKA_RSACRTExp_GetResult() to retrieve the result of the operation.
  49. (+) ECC Point Check using:
  50. (++) HAL_PKA_PointCheck().
  51. (++) HAL_PKA_PointCheck_IT().
  52. (++) HAL_PKA_PointCheck_IsOnCurve() to retrieve the result of the operation.
  53. (+) ECDSA Sign
  54. (++) HAL_PKA_ECDSASign().
  55. (++) HAL_PKA_ECDSASign_IT().
  56. (++) HAL_PKA_ECDSASign_GetResult() to retrieve the result of the operation.
  57. (+) ECDSA Verify
  58. (++) HAL_PKA_ECDSAVerif().
  59. (++) HAL_PKA_ECDSAVerif_IT().
  60. (++) HAL_PKA_ECDSAVerif_IsValidSignature() to retrieve the result of the operation.
  61. (+) ECC Scalar Multiplication using:
  62. (++) HAL_PKA_ECCMul().
  63. (++) HAL_PKA_ECCMul_IT().
  64. (++) HAL_PKA_ECCMulFastMode().
  65. (++) HAL_PKA_ECCMulFastMode_IT().
  66. (++) HAL_PKA_ECCMul_GetResult() to retrieve the result of the operation.
  67. *** Low level operation ***
  68. =================================
  69. [..]
  70. (+) Input structure requires buffers as uint32_t array.
  71. (+) Output structure requires buffers as uint32_t array.
  72. (+) Arithmetic addition using:
  73. (++) HAL_PKA_Add().
  74. (++) HAL_PKA_Add_IT().
  75. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  76. The resulting size can be the input parameter or the input parameter size + 1 (overflow).
  77. (+) Arithmetic substraction using:
  78. (++) HAL_PKA_Sub().
  79. (++) HAL_PKA_Sub_IT().
  80. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  81. (+) Arithmetic multiplication using:
  82. (++) HAL_PKA_Mul().
  83. (++) HAL_PKA_Mul_IT().
  84. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  85. (+) Comparison using:
  86. (++) HAL_PKA_Cmp().
  87. (++) HAL_PKA_Cmp_IT().
  88. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  89. (+) Modular addition using:
  90. (++) HAL_PKA_ModAdd().
  91. (++) HAL_PKA_ModAdd_IT().
  92. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  93. (+) Modular substraction using:
  94. (++) HAL_PKA_ModSub().
  95. (++) HAL_PKA_ModSub_IT().
  96. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  97. (+) Modular inversion using:
  98. (++) HAL_PKA_ModInv().
  99. (++) HAL_PKA_ModInv_IT().
  100. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  101. (+) Modular reduction using:
  102. (++) HAL_PKA_ModRed().
  103. (++) HAL_PKA_ModRed_IT().
  104. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  105. (+) Montgomery multiplication using:
  106. (++) HAL_PKA_MontgomeryMul().
  107. (++) HAL_PKA_MontgomeryMul_IT().
  108. (++) HAL_PKA_Arithmetic_GetResult() to retrieve the result of the operation.
  109. *** Montgomery parameter ***
  110. =================================
  111. (+) For some operation, the computation of the Montgomery parameter is a prerequisite.
  112. (+) Input structure requires buffers as uint8_t array.
  113. (+) Output structure requires buffers as uint32_t array.(Only used inside PKA).
  114. (+) You can compute the Montgomery parameter using:
  115. (++) HAL_PKA_MontgomeryParam().
  116. (++) HAL_PKA_MontgomeryParam_IT().
  117. (++) HAL_PKA_MontgomeryParam_GetResult() to retrieve the result of the operation.
  118. *** Polling mode operation ***
  119. ===================================
  120. [..]
  121. (+) When an operation is started in polling mode, the function returns when:
  122. (++) A timeout is encounter.
  123. (++) The operation is completed.
  124. *** Interrupt mode operation ***
  125. ===================================
  126. [..]
  127. (+) Add HAL_PKA_IRQHandler to the IRQHandler of PKA.
  128. (+) Enable the IRQ using HAL_NVIC_EnableIRQ().
  129. (+) When an operation is started in interrupt mode, the function returns immediatly.
  130. (+) When the operation is completed, the callback HAL_PKA_OperationCpltCallback is called.
  131. (+) When an error is encountered, the callback HAL_PKA_ErrorCallback is called.
  132. (+) To stop any operation in interrupt mode, use HAL_PKA_Abort().
  133. *** Utilities ***
  134. ===================================
  135. [..]
  136. (+) To clear the PKA RAM, use HAL_PKA_RAMReset().
  137. (+) To get current state, use HAL_PKA_GetState().
  138. (+) To get current error, use HAL_PKA_GetError().
  139. *** Callback registration ***
  140. =============================================
  141. [..]
  142. The compilation flag USE_HAL_PKA_REGISTER_CALLBACKS, when set to 1,
  143. allows the user to configure dynamically the driver callbacks.
  144. Use Functions @ref HAL_PKA_RegisterCallback()
  145. to register an interrupt callback.
  146. [..]
  147. Function @ref HAL_PKA_RegisterCallback() allows to register following callbacks:
  148. (+) OperationCpltCallback : callback for End of operation.
  149. (+) ErrorCallback : callback for error detection.
  150. (+) MspInitCallback : callback for Msp Init.
  151. (+) MspDeInitCallback : callback for Msp DeInit.
  152. This function takes as parameters the HAL peripheral handle, the Callback ID
  153. and a pointer to the user callback function.
  154. [..]
  155. Use function @ref HAL_PKA_UnRegisterCallback to reset a callback to the default
  156. weak function.
  157. [..]
  158. @ref HAL_PKA_UnRegisterCallback takes as parameters the HAL peripheral handle,
  159. and the Callback ID.
  160. This function allows to reset following callbacks:
  161. (+) OperationCpltCallback : callback for End of operation.
  162. (+) ErrorCallback : callback for error detection.
  163. (+) MspInitCallback : callback for Msp Init.
  164. (+) MspDeInitCallback : callback for Msp DeInit.
  165. [..]
  166. By default, after the @ref HAL_PKA_Init() and when the state is @ref HAL_PKA_STATE_RESET
  167. all callbacks are set to the corresponding weak functions:
  168. examples @ref HAL_PKA_OperationCpltCallback(), @ref HAL_PKA_ErrorCallback().
  169. Exception done for MspInit and MspDeInit functions that are
  170. reset to the legacy weak functions in the @ref HAL_PKA_Init()/ @ref HAL_PKA_DeInit() only when
  171. these callbacks are null (not registered beforehand).
  172. [..]
  173. If MspInit or MspDeInit are not null, the @ref HAL_PKA_Init()/ @ref HAL_PKA_DeInit()
  174. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  175. [..]
  176. Callbacks can be registered/unregistered in @ref HAL_PKA_STATE_READY state only.
  177. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  178. in @ref HAL_PKA_STATE_READY or @ref HAL_PKA_STATE_RESET state,
  179. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  180. [..]
  181. Then, the user first registers the MspInit/MspDeInit user callbacks
  182. using @ref HAL_PKA_RegisterCallback() before calling @ref HAL_PKA_DeInit()
  183. or @ref HAL_PKA_Init() function.
  184. [..]
  185. When the compilation flag USE_HAL_PKA_REGISTER_CALLBACKS is set to 0 or
  186. not defined, the callback registration feature is not available and all callbacks
  187. are set to the corresponding weak functions.
  188. @endverbatim
  189. ******************************************************************************
  190. * @attention
  191. *
  192. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  193. * All rights reserved.</center></h2>
  194. *
  195. * This software component is licensed by ST under BSD 3-Clause license,
  196. * the "License"; You may not use this file except in compliance with the
  197. * License. You may obtain a copy of the License at:
  198. * opensource.org/licenses/BSD-3-Clause
  199. *
  200. ******************************************************************************
  201. */
  202. /* Includes ------------------------------------------------------------------*/
  203. #include "stm32wbxx_hal.h"
  204. /** @addtogroup STM32WBxx_HAL_Driver
  205. * @{
  206. */
  207. #if defined(PKA) && defined(HAL_PKA_MODULE_ENABLED)
  208. /** @defgroup PKA PKA
  209. * @brief PKA HAL module driver.
  210. * @{
  211. */
  212. /* Private typedef -----------------------------------------------------------*/
  213. /* Private define ------------------------------------------------------------*/
  214. /** @defgroup PKA_Private_Define PKA Private Define
  215. * @{
  216. */
  217. #define PKA_RAM_SIZE 894U
  218. /* Private macro -------------------------------------------------------------*/
  219. #define __PKA_RAM_PARAM_END(TAB,INDEX) do{ \
  220. TAB[INDEX] = 0UL; \
  221. } while(0)
  222. /**
  223. * @}
  224. */
  225. /* Private variables ---------------------------------------------------------*/
  226. /* Private function prototypes -----------------------------------------------*/
  227. /** @defgroup PKA_Private_Functions PKA Private Functions
  228. * @{
  229. */
  230. uint32_t PKA_GetMode(PKA_HandleTypeDef *hpka);
  231. HAL_StatusTypeDef PKA_PollEndOfOperation(PKA_HandleTypeDef *hpka, uint32_t Timeout, uint32_t Tickstart);
  232. uint32_t PKA_CheckError(PKA_HandleTypeDef *hpka, uint32_t mode);
  233. uint32_t PKA_GetBitSize_u8(uint32_t byteNumber);
  234. uint32_t PKA_GetOptBitSize_u8(uint32_t byteNumber, uint8_t msb);
  235. uint32_t PKA_GetBitSize_u32(uint32_t wordNumber);
  236. uint32_t PKA_GetArraySize_u8(uint32_t bitSize);
  237. void PKA_Memcpy_u32_to_u8(uint8_t dst[], __IO const uint32_t src[], size_t n);
  238. void PKA_Memcpy_u8_to_u32(__IO uint32_t dst[], const uint8_t src[], size_t n);
  239. void PKA_Memcpy_u32_to_u32(__IO uint32_t dst[], __IO const uint32_t src[], size_t n);
  240. HAL_StatusTypeDef PKA_Process(PKA_HandleTypeDef *hpka, uint32_t mode, uint32_t Timeout);
  241. HAL_StatusTypeDef PKA_Process_IT(PKA_HandleTypeDef *hpka, uint32_t mode);
  242. void PKA_ModExp_Set(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in);
  243. void PKA_ModExpFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in);
  244. void PKA_ECDSASign_Set(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in);
  245. void PKA_ECDSAVerif_Set(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in);
  246. void PKA_RSACRTExp_Set(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in);
  247. void PKA_PointCheck_Set(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in);
  248. void PKA_ECCMul_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in);
  249. void PKA_ECCMulFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in);
  250. void PKA_ModRed_Set(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in);
  251. void PKA_ModInv_Set(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in);
  252. void PKA_MontgomeryParam_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint8_t *pOp1);
  253. void PKA_ARI_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint32_t *pOp1, const uint32_t *pOp2, const uint8_t *pOp3);
  254. /**
  255. * @}
  256. */
  257. /* Exported functions --------------------------------------------------------*/
  258. /** @defgroup PKA_Exported_Functions PKA Exported Functions
  259. * @{
  260. */
  261. /** @defgroup PKA_Exported_Functions_Group1 Initialization and de-initialization functions
  262. * @brief Initialization and de-initialization functions
  263. *
  264. @verbatim
  265. ===============================================================================
  266. ##### Initialization and de-initialization functions #####
  267. ===============================================================================
  268. [..] This subsection provides a set of functions allowing to initialize and
  269. deinitialize the PKAx peripheral:
  270. (+) User must implement HAL_PKA_MspInit() function in which he configures
  271. all related peripherals resources (CLOCK, IT and NVIC ).
  272. (+) Call the function HAL_PKA_Init() to configure the device.
  273. (+) Call the function HAL_PKA_DeInit() to restore the default configuration
  274. of the selected PKAx peripheral.
  275. @endverbatim
  276. * @{
  277. */
  278. /**
  279. * @brief Initialize the PKA according to the specified
  280. * parameters in the PKA_InitTypeDef and initialize the associated handle.
  281. * @param hpka PKA handle
  282. * @retval HAL status
  283. */
  284. HAL_StatusTypeDef HAL_PKA_Init(PKA_HandleTypeDef *hpka)
  285. {
  286. HAL_StatusTypeDef err = HAL_OK;
  287. /* Check the PKA handle allocation */
  288. if (hpka != NULL)
  289. {
  290. /* Check the parameters */
  291. assert_param(IS_PKA_ALL_INSTANCE(hpka->Instance));
  292. if (hpka->State == HAL_PKA_STATE_RESET)
  293. {
  294. #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
  295. /* Init the PKA Callback settings */
  296. hpka->OperationCpltCallback = HAL_PKA_OperationCpltCallback; /* Legacy weak OperationCpltCallback */
  297. hpka->ErrorCallback = HAL_PKA_ErrorCallback; /* Legacy weak ErrorCallback */
  298. if (hpka->MspInitCallback == NULL)
  299. {
  300. hpka->MspInitCallback = HAL_PKA_MspInit; /* Legacy weak MspInit */
  301. }
  302. /* Init the low level hardware */
  303. hpka->MspInitCallback(hpka);
  304. #else
  305. /* Init the low level hardware */
  306. HAL_PKA_MspInit(hpka);
  307. #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
  308. }
  309. /* Set the state to busy */
  310. hpka->State = HAL_PKA_STATE_BUSY;
  311. /* Reset the control register and enable the PKA */
  312. hpka->Instance->CR = PKA_CR_EN;
  313. /* Reset any pending flag */
  314. SET_BIT(hpka->Instance->CLRFR, PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
  315. /* Initialize the error code */
  316. hpka->ErrorCode = HAL_PKA_ERROR_NONE;
  317. /* Set the state to ready */
  318. hpka->State = HAL_PKA_STATE_READY;
  319. }
  320. else
  321. {
  322. err = HAL_ERROR;
  323. }
  324. return err;
  325. }
  326. /**
  327. * @brief DeInitialize the PKA peripheral.
  328. * @param hpka PKA handle
  329. * @retval HAL status
  330. */
  331. HAL_StatusTypeDef HAL_PKA_DeInit(PKA_HandleTypeDef *hpka)
  332. {
  333. HAL_StatusTypeDef err = HAL_OK;
  334. /* Check the PKA handle allocation */
  335. if (hpka != NULL)
  336. {
  337. /* Check the parameters */
  338. assert_param(IS_PKA_ALL_INSTANCE(hpka->Instance));
  339. /* Set the state to busy */
  340. hpka->State = HAL_PKA_STATE_BUSY;
  341. /* Reset the control register */
  342. /* This abort any operation in progress (PKA RAM content is not guaranted in this case) */
  343. hpka->Instance->CR = 0;
  344. /* Reset any pending flag */
  345. SET_BIT(hpka->Instance->CLRFR, PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
  346. #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
  347. if (hpka->MspDeInitCallback == NULL)
  348. {
  349. hpka->MspDeInitCallback = HAL_PKA_MspDeInit; /* Legacy weak MspDeInit */
  350. }
  351. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  352. hpka->MspDeInitCallback(hpka);
  353. #else
  354. /* DeInit the low level hardware: CLOCK, NVIC */
  355. HAL_PKA_MspDeInit(hpka);
  356. #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
  357. /* Reset the error code */
  358. hpka->ErrorCode = HAL_PKA_ERROR_NONE;
  359. /* Reset the state */
  360. hpka->State = HAL_PKA_STATE_RESET;
  361. }
  362. else
  363. {
  364. err = HAL_ERROR;
  365. }
  366. return err;
  367. }
  368. /**
  369. * @brief Initialize the PKA MSP.
  370. * @param hpka PKA handle
  371. * @retval None
  372. */
  373. __weak void HAL_PKA_MspInit(PKA_HandleTypeDef *hpka)
  374. {
  375. /* Prevent unused argument(s) compilation warning */
  376. UNUSED(hpka);
  377. /* NOTE : This function should not be modified, when the callback is needed,
  378. the HAL_PKA_MspInit can be implemented in the user file
  379. */
  380. }
  381. /**
  382. * @brief DeInitialize the PKA MSP.
  383. * @param hpka PKA handle
  384. * @retval None
  385. */
  386. __weak void HAL_PKA_MspDeInit(PKA_HandleTypeDef *hpka)
  387. {
  388. /* Prevent unused argument(s) compilation warning */
  389. UNUSED(hpka);
  390. /* NOTE : This function should not be modified, when the callback is needed,
  391. the HAL_PKA_MspDeInit can be implemented in the user file
  392. */
  393. }
  394. #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
  395. /**
  396. * @brief Register a User PKA Callback
  397. * To be used instead of the weak predefined callback
  398. * @param hpka Pointer to a PKA_HandleTypeDef structure that contains
  399. * the configuration information for the specified PKA.
  400. * @param CallbackID ID of the callback to be registered
  401. * This parameter can be one of the following values:
  402. * @arg @ref HAL_PKA_OPERATION_COMPLETE_CB_ID End of operation callback ID
  403. * @arg @ref HAL_PKA_ERROR_CB_ID Error callback ID
  404. * @arg @ref HAL_PKA_MSPINIT_CB_ID MspInit callback ID
  405. * @arg @ref HAL_PKA_MSPDEINIT_CB_ID MspDeInit callback ID
  406. * @param pCallback pointer to the Callback function
  407. * @retval HAL status
  408. */
  409. HAL_StatusTypeDef HAL_PKA_RegisterCallback(PKA_HandleTypeDef *hpka, HAL_PKA_CallbackIDTypeDef CallbackID, pPKA_CallbackTypeDef pCallback)
  410. {
  411. HAL_StatusTypeDef status = HAL_OK;
  412. if (pCallback == NULL)
  413. {
  414. /* Update the error code */
  415. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  416. return HAL_ERROR;
  417. }
  418. if (HAL_PKA_STATE_READY == hpka->State)
  419. {
  420. switch (CallbackID)
  421. {
  422. case HAL_PKA_OPERATION_COMPLETE_CB_ID :
  423. hpka->OperationCpltCallback = pCallback;
  424. break;
  425. case HAL_PKA_ERROR_CB_ID :
  426. hpka->ErrorCallback = pCallback;
  427. break;
  428. case HAL_PKA_MSPINIT_CB_ID :
  429. hpka->MspInitCallback = pCallback;
  430. break;
  431. case HAL_PKA_MSPDEINIT_CB_ID :
  432. hpka->MspDeInitCallback = pCallback;
  433. break;
  434. default :
  435. /* Update the error code */
  436. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  437. /* Return error status */
  438. status = HAL_ERROR;
  439. break;
  440. }
  441. }
  442. else if (HAL_PKA_STATE_RESET == hpka->State)
  443. {
  444. switch (CallbackID)
  445. {
  446. case HAL_PKA_MSPINIT_CB_ID :
  447. hpka->MspInitCallback = pCallback;
  448. break;
  449. case HAL_PKA_MSPDEINIT_CB_ID :
  450. hpka->MspDeInitCallback = pCallback;
  451. break;
  452. default :
  453. /* Update the error code */
  454. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  455. /* Return error status */
  456. status = HAL_ERROR;
  457. break;
  458. }
  459. }
  460. else
  461. {
  462. /* Update the error code */
  463. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  464. /* Return error status */
  465. status = HAL_ERROR;
  466. }
  467. return status;
  468. }
  469. /**
  470. * @brief Unregister a PKA Callback
  471. * PKA callback is redirected to the weak predefined callback
  472. * @param hpka Pointer to a PKA_HandleTypeDef structure that contains
  473. * the configuration information for the specified PKA.
  474. * @param CallbackID ID of the callback to be unregistered
  475. * This parameter can be one of the following values:
  476. * @arg @ref HAL_PKA_OPERATION_COMPLETE_CB_ID End of operation callback ID
  477. * @arg @ref HAL_PKA_ERROR_CB_ID Error callback ID
  478. * @arg @ref HAL_PKA_MSPINIT_CB_ID MspInit callback ID
  479. * @arg @ref HAL_PKA_MSPDEINIT_CB_ID MspDeInit callback ID
  480. * @retval HAL status
  481. */
  482. HAL_StatusTypeDef HAL_PKA_UnRegisterCallback(PKA_HandleTypeDef *hpka, HAL_PKA_CallbackIDTypeDef CallbackID)
  483. {
  484. HAL_StatusTypeDef status = HAL_OK;
  485. if (HAL_PKA_STATE_READY == hpka->State)
  486. {
  487. switch (CallbackID)
  488. {
  489. case HAL_PKA_OPERATION_COMPLETE_CB_ID :
  490. hpka->OperationCpltCallback = HAL_PKA_OperationCpltCallback; /* Legacy weak OperationCpltCallback */
  491. break;
  492. case HAL_PKA_ERROR_CB_ID :
  493. hpka->ErrorCallback = HAL_PKA_ErrorCallback; /* Legacy weak ErrorCallback */
  494. break;
  495. case HAL_PKA_MSPINIT_CB_ID :
  496. hpka->MspInitCallback = HAL_PKA_MspInit; /* Legacy weak MspInit */
  497. break;
  498. case HAL_PKA_MSPDEINIT_CB_ID :
  499. hpka->MspDeInitCallback = HAL_PKA_MspDeInit; /* Legacy weak MspDeInit */
  500. break;
  501. default :
  502. /* Update the error code */
  503. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  504. /* Return error status */
  505. status = HAL_ERROR;
  506. break;
  507. }
  508. }
  509. else if (HAL_PKA_STATE_RESET == hpka->State)
  510. {
  511. switch (CallbackID)
  512. {
  513. case HAL_PKA_MSPINIT_CB_ID :
  514. hpka->MspInitCallback = HAL_PKA_MspInit; /* Legacy weak MspInit */
  515. break;
  516. case HAL_PKA_MSPDEINIT_CB_ID :
  517. hpka->MspDeInitCallback = HAL_PKA_MspDeInit; /* Legacy weak MspDeInit */
  518. break;
  519. default :
  520. /* Update the error code */
  521. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  522. /* Return error status */
  523. status = HAL_ERROR;
  524. break;
  525. }
  526. }
  527. else
  528. {
  529. /* Update the error code */
  530. hpka->ErrorCode |= HAL_PKA_ERROR_INVALID_CALLBACK;
  531. /* Return error status */
  532. status = HAL_ERROR;
  533. }
  534. return status;
  535. }
  536. #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
  537. /**
  538. * @}
  539. */
  540. /** @defgroup PKA_Exported_Functions_Group2 IO operation functions
  541. * @brief IO operation functions
  542. *
  543. @verbatim
  544. ===============================================================================
  545. ##### IO operation functions #####
  546. ===============================================================================
  547. [..]
  548. This subsection provides a set of functions allowing to manage the PKA operations.
  549. (#) There are two modes of operation:
  550. (++) Blocking mode : The operation is performed in the polling mode.
  551. These functions return when data operation is completed.
  552. (++) No-Blocking mode : The operation is performed using Interrupts.
  553. These functions return immediatly.
  554. The end of the operation is indicated by HAL_PKA_ErrorCallback in case of error.
  555. The end of the operation is indicated by HAL_PKA_OperationCpltCallback in case of success.
  556. To stop any operation in interrupt mode, use HAL_PKA_Abort().
  557. (#) Blocking mode functions are :
  558. (++) HAL_PKA_ModExp()
  559. (++) HAL_PKA_ModExpFastMode()
  560. (++) HAL_PKA_ModExp_GetResult();
  561. (++) HAL_PKA_ECDSASign()
  562. (++) HAL_PKA_ECDSASign_GetResult();
  563. (++) HAL_PKA_ECDSAVerif()
  564. (++) HAL_PKA_ECDSAVerif_IsValidSignature();
  565. (++) HAL_PKA_RSACRTExp()
  566. (++) HAL_PKA_RSACRTExp_GetResult();
  567. (++) HAL_PKA_PointCheck()
  568. (++) HAL_PKA_PointCheck_IsOnCurve();
  569. (++) HAL_PKA_ECCMul()
  570. (++) HAL_PKA_ECCMulFastMode()
  571. (++) HAL_PKA_ECCMul_GetResult();
  572. (++) HAL_PKA_Add()
  573. (++) HAL_PKA_Sub()
  574. (++) HAL_PKA_Cmp()
  575. (++) HAL_PKA_Mul()
  576. (++) HAL_PKA_ModAdd()
  577. (++) HAL_PKA_ModSub()
  578. (++) HAL_PKA_ModInv()
  579. (++) HAL_PKA_ModRed()
  580. (++) HAL_PKA_MontgomeryMul()
  581. (++) HAL_PKA_Arithmetic_GetResult(P);
  582. (++) HAL_PKA_MontgomeryParam()
  583. (++) HAL_PKA_MontgomeryParam_GetResult();
  584. (#) No-Blocking mode functions with Interrupt are :
  585. (++) HAL_PKA_ModExp_IT();
  586. (++) HAL_PKA_ModExpFastMode_IT();
  587. (++) HAL_PKA_ModExp_GetResult();
  588. (++) HAL_PKA_ECDSASign_IT();
  589. (++) HAL_PKA_ECDSASign_GetResult();
  590. (++) HAL_PKA_ECDSAVerif_IT();
  591. (++) HAL_PKA_ECDSAVerif_IsValidSignature();
  592. (++) HAL_PKA_RSACRTExp_IT();
  593. (++) HAL_PKA_RSACRTExp_GetResult();
  594. (++) HAL_PKA_PointCheck_IT();
  595. (++) HAL_PKA_PointCheck_IsOnCurve();
  596. (++) HAL_PKA_ECCMul_IT();
  597. (++) HAL_PKA_ECCMulFastMode_IT();
  598. (++) HAL_PKA_ECCMul_GetResult();
  599. (++) HAL_PKA_Add_IT();
  600. (++) HAL_PKA_Sub_IT();
  601. (++) HAL_PKA_Cmp_IT();
  602. (++) HAL_PKA_Mul_IT();
  603. (++) HAL_PKA_ModAdd_IT();
  604. (++) HAL_PKA_ModSub_IT();
  605. (++) HAL_PKA_ModInv_IT();
  606. (++) HAL_PKA_ModRed_IT();
  607. (++) HAL_PKA_MontgomeryMul_IT();
  608. (++) HAL_PKA_Arithmetic_GetResult();
  609. (++) HAL_PKA_MontgomeryParam_IT();
  610. (++) HAL_PKA_MontgomeryParam_GetResult();
  611. (++) HAL_PKA_Abort();
  612. @endverbatim
  613. * @{
  614. */
  615. /**
  616. * @brief Modular exponentiation in blocking mode.
  617. * @param hpka PKA handle
  618. * @param in Input information
  619. * @param Timeout Timeout duration
  620. * @retval HAL status
  621. */
  622. HAL_StatusTypeDef HAL_PKA_ModExp(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in, uint32_t Timeout)
  623. {
  624. /* Set input parameter in PKA RAM */
  625. PKA_ModExp_Set(hpka, in);
  626. /* Start the operation */
  627. return PKA_Process(hpka, PKA_MODE_MODULAR_EXP, Timeout);
  628. }
  629. /**
  630. * @brief Modular exponentiation in non-blocking mode with Interrupt.
  631. * @param hpka PKA handle
  632. * @param in Input information
  633. * @retval HAL status
  634. */
  635. HAL_StatusTypeDef HAL_PKA_ModExp_IT(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in)
  636. {
  637. /* Set input parameter in PKA RAM */
  638. PKA_ModExp_Set(hpka, in);
  639. /* Start the operation */
  640. return PKA_Process_IT(hpka, PKA_MODE_MODULAR_EXP);
  641. }
  642. /**
  643. * @brief Modular exponentiation in blocking mode.
  644. * @param hpka PKA handle
  645. * @param in Input information
  646. * @param Timeout Timeout duration
  647. * @retval HAL status
  648. */
  649. HAL_StatusTypeDef HAL_PKA_ModExpFastMode(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in, uint32_t Timeout)
  650. {
  651. /* Set input parameter in PKA RAM */
  652. PKA_ModExpFastMode_Set(hpka, in);
  653. /* Start the operation */
  654. return PKA_Process(hpka, PKA_MODE_MODULAR_EXP_FAST_MODE, Timeout);
  655. }
  656. /**
  657. * @brief Modular exponentiation in non-blocking mode with Interrupt.
  658. * @param hpka PKA handle
  659. * @param in Input information
  660. * @retval HAL status
  661. */
  662. HAL_StatusTypeDef HAL_PKA_ModExpFastMode_IT(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in)
  663. {
  664. /* Set input parameter in PKA RAM */
  665. PKA_ModExpFastMode_Set(hpka, in);
  666. /* Start the operation */
  667. return PKA_Process_IT(hpka, PKA_MODE_MODULAR_EXP_FAST_MODE);
  668. }
  669. /**
  670. * @brief Retrieve operation result.
  671. * @param hpka PKA handle
  672. * @param pRes Output buffer
  673. * @retval HAL status
  674. */
  675. void HAL_PKA_ModExp_GetResult(PKA_HandleTypeDef *hpka, uint8_t *pRes)
  676. {
  677. uint32_t size;
  678. /* Indicate to the user the final size */
  679. size = (hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] + 7UL) / 8UL;
  680. /* Move the result to appropriate location (indicated in out parameter) */
  681. PKA_Memcpy_u32_to_u8(pRes, &hpka->Instance->RAM[PKA_MODULAR_EXP_OUT_SM_ALGO_ACC1], size);
  682. }
  683. /**
  684. * @brief Sign a message using elliptic curves over prime fields in blocking mode.
  685. * @param hpka PKA handle
  686. * @param in Input information
  687. * @param Timeout Timeout duration
  688. * @retval HAL status
  689. */
  690. HAL_StatusTypeDef HAL_PKA_ECDSASign(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in, uint32_t Timeout)
  691. {
  692. /* Set input parameter in PKA RAM */
  693. PKA_ECDSASign_Set(hpka, in);
  694. /* Start the operation */
  695. return PKA_Process(hpka, PKA_MODE_ECDSA_SIGNATURE, Timeout);
  696. }
  697. /**
  698. * @brief Sign a message using elliptic curves over prime fields in non-blocking mode with Interrupt.
  699. * @param hpka PKA handle
  700. * @param in Input information
  701. * @retval HAL status
  702. */
  703. HAL_StatusTypeDef HAL_PKA_ECDSASign_IT(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in)
  704. {
  705. /* Set input parameter in PKA RAM */
  706. PKA_ECDSASign_Set(hpka, in);
  707. /* Start the operation */
  708. return PKA_Process_IT(hpka, PKA_MODE_ECDSA_SIGNATURE);
  709. }
  710. /**
  711. * @brief Retrieve operation result.
  712. * @param hpka PKA handle
  713. * @param out Output information
  714. * @param outExt Additional Output information (facultative)
  715. */
  716. void HAL_PKA_ECDSASign_GetResult(PKA_HandleTypeDef *hpka, PKA_ECDSASignOutTypeDef *out, PKA_ECDSASignOutExtParamTypeDef *outExt)
  717. {
  718. uint32_t size;
  719. size = (hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] + 7UL) / 8UL;
  720. if (out != NULL)
  721. {
  722. PKA_Memcpy_u32_to_u8(out->RSign, &hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_SIGNATURE_R], size);
  723. PKA_Memcpy_u32_to_u8(out->SSign, &hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_SIGNATURE_S], size);
  724. }
  725. /* If user requires the additional information */
  726. if (outExt != NULL)
  727. {
  728. /* Move the result to appropriate location (indicated in outExt parameter) */
  729. PKA_Memcpy_u32_to_u8(outExt->ptX, &hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_FINAL_POINT_X], size);
  730. PKA_Memcpy_u32_to_u8(outExt->ptY, &hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_FINAL_POINT_Y], size);
  731. }
  732. }
  733. /**
  734. * @brief Verify the validity of a signature using elliptic curves over prime fields in blocking mode.
  735. * @param hpka PKA handle
  736. * @param in Input information
  737. * @param Timeout Timeout duration
  738. * @retval HAL status
  739. */
  740. HAL_StatusTypeDef HAL_PKA_ECDSAVerif(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in, uint32_t Timeout)
  741. {
  742. /* Set input parameter in PKA RAM */
  743. PKA_ECDSAVerif_Set(hpka, in);
  744. /* Start the operation */
  745. return PKA_Process(hpka, PKA_MODE_ECDSA_VERIFICATION, Timeout);
  746. }
  747. /**
  748. * @brief Verify the validity of a signature using elliptic curves over prime fields in non-blocking mode with Interrupt.
  749. * @param hpka PKA handle
  750. * @param in Input information
  751. * @retval HAL status
  752. */
  753. HAL_StatusTypeDef HAL_PKA_ECDSAVerif_IT(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in)
  754. {
  755. /* Set input parameter in PKA RAM */
  756. PKA_ECDSAVerif_Set(hpka, in);
  757. /* Start the operation */
  758. return PKA_Process_IT(hpka, PKA_MODE_ECDSA_VERIFICATION);
  759. }
  760. /**
  761. * @brief Return the result of the ECDSA verification operation.
  762. * @param hpka PKA handle
  763. * @retval 1 if signature is verified, 0 in other case
  764. */
  765. uint32_t HAL_PKA_ECDSAVerif_IsValidSignature(PKA_HandleTypeDef const *const hpka)
  766. {
  767. /* Invert the state of the PKA RAM bit containing the result of the operation */
  768. return (hpka->Instance->RAM[PKA_ECDSA_VERIF_OUT_RESULT] == 0UL) ? 1UL : 0UL;
  769. }
  770. /**
  771. * @brief RSA CRT exponentiation in blocking mode.
  772. * @param hpka PKA handle
  773. * @param in Input information
  774. * @param Timeout Timeout duration
  775. * @retval HAL status
  776. */
  777. HAL_StatusTypeDef HAL_PKA_RSACRTExp(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in, uint32_t Timeout)
  778. {
  779. /* Set input parameter in PKA RAM */
  780. PKA_RSACRTExp_Set(hpka, in);
  781. /* Start the operation */
  782. return PKA_Process(hpka, PKA_MODE_RSA_CRT_EXP, Timeout);
  783. }
  784. /**
  785. * @brief RSA CRT exponentiation in non-blocking mode with Interrupt.
  786. * @param hpka PKA handle
  787. * @param in Input information
  788. * @retval HAL status
  789. */
  790. HAL_StatusTypeDef HAL_PKA_RSACRTExp_IT(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in)
  791. {
  792. /* Set input parameter in PKA RAM */
  793. PKA_RSACRTExp_Set(hpka, in);
  794. /* Start the operation */
  795. return PKA_Process_IT(hpka, PKA_MODE_RSA_CRT_EXP);
  796. }
  797. /**
  798. * @brief Retrieve operation result.
  799. * @param hpka PKA handle
  800. * @param pRes Pointer to memory location to receive the result of the operation
  801. * @retval HAL status
  802. */
  803. void HAL_PKA_RSACRTExp_GetResult(PKA_HandleTypeDef *hpka, uint8_t *pRes)
  804. {
  805. uint32_t size;
  806. /* Move the result to appropriate location (indicated in out parameter) */
  807. size = (hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] + 7UL) / 8UL;
  808. PKA_Memcpy_u32_to_u8(pRes, &hpka->Instance->RAM[PKA_RSA_CRT_EXP_OUT_RESULT], size);
  809. }
  810. /**
  811. * @brief Point on elliptic curve check in blocking mode.
  812. * @param hpka PKA handle
  813. * @param in Input information
  814. * @param Timeout Timeout duration
  815. * @retval HAL status
  816. */
  817. HAL_StatusTypeDef HAL_PKA_PointCheck(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in, uint32_t Timeout)
  818. {
  819. /* Set input parameter in PKA RAM */
  820. PKA_PointCheck_Set(hpka, in);
  821. /* Start the operation */
  822. return PKA_Process(hpka, PKA_MODE_POINT_CHECK, Timeout);
  823. }
  824. /**
  825. * @brief Point on elliptic curve check in non-blocking mode with Interrupt.
  826. * @param hpka PKA handle
  827. * @param in Input information
  828. * @retval HAL status
  829. */
  830. HAL_StatusTypeDef HAL_PKA_PointCheck_IT(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in)
  831. {
  832. /* Set input parameter in PKA RAM */
  833. PKA_PointCheck_Set(hpka, in);
  834. /* Start the operation */
  835. return PKA_Process_IT(hpka, PKA_MODE_POINT_CHECK);
  836. }
  837. /**
  838. * @brief Return the result of the point check operation.
  839. * @param hpka PKA handle
  840. * @retval 1 if point is on curve, 0 in other case
  841. */
  842. uint32_t HAL_PKA_PointCheck_IsOnCurve(PKA_HandleTypeDef const *const hpka)
  843. {
  844. #define PKA_POINT_IS_ON_CURVE 0UL
  845. /* Invert the value of the PKA RAM containig the result of the operation */
  846. return (hpka->Instance->RAM[PKA_POINT_CHECK_OUT_ERROR] == PKA_POINT_IS_ON_CURVE) ? 1UL : 0UL;
  847. }
  848. /**
  849. * @brief ECC scalar multiplication in blocking mode.
  850. * @param hpka PKA handle
  851. * @param in Input information
  852. * @param Timeout Timeout duration
  853. * @retval HAL status
  854. */
  855. HAL_StatusTypeDef HAL_PKA_ECCMul(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in, uint32_t Timeout)
  856. {
  857. /* Set input parameter in PKA RAM */
  858. PKA_ECCMul_Set(hpka, in);
  859. /* Start the operation */
  860. return PKA_Process(hpka, PKA_MODE_ECC_MUL, Timeout);
  861. }
  862. /**
  863. * @brief ECC scalar multiplication in non-blocking mode with Interrupt.
  864. * @param hpka PKA handle
  865. * @param in Input information
  866. * @retval HAL status
  867. */
  868. HAL_StatusTypeDef HAL_PKA_ECCMul_IT(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in)
  869. {
  870. /* Set input parameter in PKA RAM */
  871. PKA_ECCMul_Set(hpka, in);
  872. /* Start the operation */
  873. return PKA_Process_IT(hpka, PKA_MODE_ECC_MUL);
  874. }
  875. /**
  876. * @brief ECC scalar multiplication in blocking mode.
  877. * @param hpka PKA handle
  878. * @param in Input information
  879. * @param Timeout Timeout duration
  880. * @retval HAL status
  881. */
  882. HAL_StatusTypeDef HAL_PKA_ECCMulFastMode(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in, uint32_t Timeout)
  883. {
  884. /* Set input parameter in PKA RAM */
  885. PKA_ECCMulFastMode_Set(hpka, in);
  886. /* Start the operation */
  887. return PKA_Process(hpka, PKA_MODE_ECC_MUL_FAST_MODE, Timeout);
  888. }
  889. /**
  890. * @brief ECC scalar multiplication in non-blocking mode with Interrupt.
  891. * @param hpka PKA handle
  892. * @param in Input information
  893. * @retval HAL status
  894. */
  895. HAL_StatusTypeDef HAL_PKA_ECCMulFastMode_IT(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in)
  896. {
  897. /* Set input parameter in PKA RAM */
  898. PKA_ECCMulFastMode_Set(hpka, in);
  899. /* Start the operation */
  900. return PKA_Process_IT(hpka, PKA_MODE_ECC_MUL_FAST_MODE);
  901. }
  902. /**
  903. * @brief Retrieve operation result.
  904. * @param hpka PKA handle
  905. * @param out Output information
  906. * @retval HAL status
  907. */
  908. void HAL_PKA_ECCMul_GetResult(PKA_HandleTypeDef *hpka, PKA_ECCMulOutTypeDef *out)
  909. {
  910. uint32_t size;
  911. /* Retrieve the size of the array from the PKA RAM */
  912. size = (hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] + 7UL) / 8UL;
  913. /* If a destination buffer is provided */
  914. if (out != NULL)
  915. {
  916. /* Move the result to appropriate location (indicated in out parameter) */
  917. PKA_Memcpy_u32_to_u8(out->ptX, &hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_OUT_RESULT_X], size);
  918. PKA_Memcpy_u32_to_u8(out->ptY, &hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_OUT_RESULT_Y], size);
  919. }
  920. }
  921. /**
  922. * @brief Arithmetic addition in blocking mode.
  923. * @param hpka PKA handle
  924. * @param in Input information
  925. * @param Timeout Timeout duration
  926. * @retval HAL status
  927. */
  928. HAL_StatusTypeDef HAL_PKA_Add(PKA_HandleTypeDef *hpka, PKA_AddInTypeDef *in, uint32_t Timeout)
  929. {
  930. /* Set input parameter in PKA RAM */
  931. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  932. /* Start the operation */
  933. return PKA_Process(hpka, PKA_MODE_ARITHMETIC_ADD, Timeout);
  934. }
  935. /**
  936. * @brief Arithmetic addition in non-blocking mode with Interrupt.
  937. * @param hpka PKA handle
  938. * @param in Input information
  939. * @retval HAL status
  940. */
  941. HAL_StatusTypeDef HAL_PKA_Add_IT(PKA_HandleTypeDef *hpka, PKA_AddInTypeDef *in)
  942. {
  943. /* Set input parameter in PKA RAM */
  944. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  945. /* Start the operation */
  946. return PKA_Process_IT(hpka, PKA_MODE_ARITHMETIC_ADD);
  947. }
  948. /**
  949. * @brief Arithmetic substraction in blocking mode.
  950. * @param hpka PKA handle
  951. * @param in Input information
  952. * @param Timeout Timeout duration
  953. * @retval HAL status
  954. */
  955. HAL_StatusTypeDef HAL_PKA_Sub(PKA_HandleTypeDef *hpka, PKA_SubInTypeDef *in, uint32_t Timeout)
  956. {
  957. /* Set input parameter in PKA RAM */
  958. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  959. /* Start the operation */
  960. return PKA_Process(hpka, PKA_MODE_ARITHMETIC_SUB, Timeout);
  961. }
  962. /**
  963. * @brief Arithmetic substraction in non-blocking mode with Interrupt.
  964. * @param hpka PKA handle
  965. * @param in Input information
  966. * @retval HAL status
  967. */
  968. HAL_StatusTypeDef HAL_PKA_Sub_IT(PKA_HandleTypeDef *hpka, PKA_SubInTypeDef *in)
  969. {
  970. /* Set input parameter in PKA RAM */
  971. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  972. /* Start the operation */
  973. return PKA_Process_IT(hpka, PKA_MODE_ARITHMETIC_SUB);
  974. }
  975. /**
  976. * @brief Arithmetic multiplication in blocking mode.
  977. * @param hpka PKA handle
  978. * @param in Input information
  979. * @param Timeout Timeout duration
  980. * @retval HAL status
  981. */
  982. HAL_StatusTypeDef HAL_PKA_Mul(PKA_HandleTypeDef *hpka, PKA_MulInTypeDef *in, uint32_t Timeout)
  983. {
  984. /* Set input parameter in PKA RAM */
  985. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  986. /* Start the operation */
  987. return PKA_Process(hpka, PKA_MODE_ARITHMETIC_MUL, Timeout);
  988. }
  989. /**
  990. * @brief Arithmetic multiplication in non-blocking mode with Interrupt.
  991. * @param hpka PKA handle
  992. * @param in Input information
  993. * @retval HAL status
  994. */
  995. HAL_StatusTypeDef HAL_PKA_Mul_IT(PKA_HandleTypeDef *hpka, PKA_MulInTypeDef *in)
  996. {
  997. /* Set input parameter in PKA RAM */
  998. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  999. /* Start the operation */
  1000. return PKA_Process_IT(hpka, PKA_MODE_ARITHMETIC_MUL);
  1001. }
  1002. /**
  1003. * @brief Comparison in blocking mode.
  1004. * @param hpka PKA handle
  1005. * @param in Input information
  1006. * @param Timeout Timeout duration
  1007. * @retval HAL status
  1008. */
  1009. HAL_StatusTypeDef HAL_PKA_Cmp(PKA_HandleTypeDef *hpka, PKA_CmpInTypeDef *in, uint32_t Timeout)
  1010. {
  1011. /* Set input parameter in PKA RAM */
  1012. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  1013. /* Start the operation */
  1014. return PKA_Process(hpka, PKA_MODE_COMPARISON, Timeout);
  1015. }
  1016. /**
  1017. * @brief Comparison in non-blocking mode with Interrupt.
  1018. * @param hpka PKA handle
  1019. * @param in Input information
  1020. * @retval HAL status
  1021. */
  1022. HAL_StatusTypeDef HAL_PKA_Cmp_IT(PKA_HandleTypeDef *hpka, PKA_CmpInTypeDef *in)
  1023. {
  1024. /* Set input parameter in PKA RAM */
  1025. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, NULL);
  1026. /* Start the operation */
  1027. return PKA_Process_IT(hpka, PKA_MODE_COMPARISON);
  1028. }
  1029. /**
  1030. * @brief Modular addition in blocking mode.
  1031. * @param hpka PKA handle
  1032. * @param in Input information
  1033. * @param Timeout Timeout duration
  1034. * @retval HAL status
  1035. */
  1036. HAL_StatusTypeDef HAL_PKA_ModAdd(PKA_HandleTypeDef *hpka, PKA_ModAddInTypeDef *in, uint32_t Timeout)
  1037. {
  1038. /* Set input parameter in PKA RAM */
  1039. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
  1040. /* Start the operation */
  1041. return PKA_Process(hpka, PKA_MODE_MODULAR_ADD, Timeout);
  1042. }
  1043. /**
  1044. * @brief Modular addition in non-blocking mode with Interrupt.
  1045. * @param hpka PKA handle
  1046. * @param in Input information
  1047. * @retval HAL status
  1048. */
  1049. HAL_StatusTypeDef HAL_PKA_ModAdd_IT(PKA_HandleTypeDef *hpka, PKA_ModAddInTypeDef *in)
  1050. {
  1051. /* Set input parameter in PKA RAM */
  1052. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
  1053. /* Start the operation */
  1054. return PKA_Process_IT(hpka, PKA_MODE_MODULAR_ADD);
  1055. }
  1056. /**
  1057. * @brief Modular inversion in blocking mode.
  1058. * @param hpka PKA handle
  1059. * @param in Input information
  1060. * @param Timeout Timeout duration
  1061. * @retval HAL status
  1062. */
  1063. HAL_StatusTypeDef HAL_PKA_ModInv(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in, uint32_t Timeout)
  1064. {
  1065. /* Set input parameter in PKA RAM */
  1066. PKA_ModInv_Set(hpka, in);
  1067. /* Start the operation */
  1068. return PKA_Process(hpka, PKA_MODE_MODULAR_INV, Timeout);
  1069. }
  1070. /**
  1071. * @brief Modular inversion in non-blocking mode with Interrupt.
  1072. * @param hpka PKA handle
  1073. * @param in Input information
  1074. * @retval HAL status
  1075. */
  1076. HAL_StatusTypeDef HAL_PKA_ModInv_IT(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in)
  1077. {
  1078. /* Set input parameter in PKA RAM */
  1079. PKA_ModInv_Set(hpka, in);
  1080. /* Start the operation */
  1081. return PKA_Process_IT(hpka, PKA_MODE_MODULAR_INV);
  1082. }
  1083. /**
  1084. * @brief Modular substraction in blocking mode.
  1085. * @param hpka PKA handle
  1086. * @param in Input information
  1087. * @param Timeout Timeout duration
  1088. * @retval HAL status
  1089. */
  1090. HAL_StatusTypeDef HAL_PKA_ModSub(PKA_HandleTypeDef *hpka, PKA_ModSubInTypeDef *in, uint32_t Timeout)
  1091. {
  1092. /* Set input parameter in PKA RAM */
  1093. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
  1094. /* Start the operation */
  1095. return PKA_Process(hpka, PKA_MODE_MODULAR_SUB, Timeout);
  1096. }
  1097. /**
  1098. * @brief Modular substraction in non-blocking mode with Interrupt.
  1099. * @param hpka PKA handle
  1100. * @param in Input information
  1101. * @retval HAL status
  1102. */
  1103. HAL_StatusTypeDef HAL_PKA_ModSub_IT(PKA_HandleTypeDef *hpka, PKA_ModSubInTypeDef *in)
  1104. {
  1105. /* Set input parameter in PKA RAM */
  1106. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
  1107. /* Start the operation */
  1108. return PKA_Process_IT(hpka, PKA_MODE_MODULAR_SUB);
  1109. }
  1110. /**
  1111. * @brief Modular reduction in blocking mode.
  1112. * @param hpka PKA handle
  1113. * @param in Input information
  1114. * @param Timeout Timeout duration
  1115. * @retval HAL status
  1116. */
  1117. HAL_StatusTypeDef HAL_PKA_ModRed(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in, uint32_t Timeout)
  1118. {
  1119. /* Set input parameter in PKA RAM */
  1120. PKA_ModRed_Set(hpka, in);
  1121. /* Start the operation */
  1122. return PKA_Process(hpka, PKA_MODE_MODULAR_RED, Timeout);
  1123. }
  1124. /**
  1125. * @brief Modular reduction in non-blocking mode with Interrupt.
  1126. * @param hpka PKA handle
  1127. * @param in Input information
  1128. * @retval HAL status
  1129. */
  1130. HAL_StatusTypeDef HAL_PKA_ModRed_IT(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in)
  1131. {
  1132. /* Set input parameter in PKA RAM */
  1133. PKA_ModRed_Set(hpka, in);
  1134. /* Start the operation */
  1135. return PKA_Process_IT(hpka, PKA_MODE_MODULAR_RED);
  1136. }
  1137. /**
  1138. * @brief Montgomery multiplication in blocking mode.
  1139. * @param hpka PKA handle
  1140. * @param in Input information
  1141. * @param Timeout Timeout duration
  1142. * @retval HAL status
  1143. */
  1144. HAL_StatusTypeDef HAL_PKA_MontgomeryMul(PKA_HandleTypeDef *hpka, PKA_MontgomeryMulInTypeDef *in, uint32_t Timeout)
  1145. {
  1146. /* Set input parameter in PKA RAM */
  1147. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
  1148. /* Start the operation */
  1149. return PKA_Process(hpka, PKA_MODE_MONTGOMERY_MUL, Timeout);
  1150. }
  1151. /**
  1152. * @brief Montgomery multiplication in non-blocking mode with Interrupt.
  1153. * @param hpka PKA handle
  1154. * @param in Input information
  1155. * @retval HAL status
  1156. */
  1157. HAL_StatusTypeDef HAL_PKA_MontgomeryMul_IT(PKA_HandleTypeDef *hpka, PKA_MontgomeryMulInTypeDef *in)
  1158. {
  1159. /* Set input parameter in PKA RAM */
  1160. PKA_ARI_Set(hpka, in->size, in->pOp1, in->pOp2, in->pOp3);
  1161. /* Start the operation */
  1162. return PKA_Process_IT(hpka, PKA_MODE_MONTGOMERY_MUL);
  1163. }
  1164. /**
  1165. * @brief Retrieve operation result.
  1166. * @param hpka PKA handle
  1167. * @param pRes Pointer to memory location to receive the result of the operation
  1168. */
  1169. void HAL_PKA_Arithmetic_GetResult(PKA_HandleTypeDef *hpka, uint32_t *pRes)
  1170. {
  1171. uint32_t mode = (hpka->Instance->CR & PKA_CR_MODE_Msk) >> PKA_CR_MODE_Pos;
  1172. uint32_t size = 0;
  1173. /* Move the result to appropriate location (indicated in pRes parameter) */
  1174. switch (mode)
  1175. {
  1176. case PKA_MODE_ARITHMETIC_SUB:
  1177. case PKA_MODE_MODULAR_ADD:
  1178. case PKA_MODE_MODULAR_RED:
  1179. case PKA_MODE_MODULAR_INV:
  1180. case PKA_MODE_MODULAR_SUB:
  1181. case PKA_MODE_MONTGOMERY_MUL:
  1182. size = hpka->Instance->RAM[1] / 32UL;
  1183. break;
  1184. case PKA_MODE_ARITHMETIC_ADD:
  1185. size = hpka->Instance->RAM[1] / 32UL;
  1186. /* Manage the overflow of the addition */
  1187. if (hpka->Instance->RAM[500U + size] != 0UL)
  1188. {
  1189. size += 1UL;
  1190. }
  1191. break;
  1192. case PKA_MODE_COMPARISON:
  1193. size = 1;
  1194. break;
  1195. case PKA_MODE_ARITHMETIC_MUL:
  1196. size = hpka->Instance->RAM[1] / 32UL * 2UL;
  1197. break;
  1198. default:
  1199. break;
  1200. }
  1201. if (pRes != NULL)
  1202. {
  1203. switch (mode)
  1204. {
  1205. case PKA_MODE_ARITHMETIC_SUB:
  1206. case PKA_MODE_MODULAR_ADD:
  1207. case PKA_MODE_MODULAR_RED:
  1208. case PKA_MODE_MODULAR_INV:
  1209. case PKA_MODE_MODULAR_SUB:
  1210. case PKA_MODE_MONTGOMERY_MUL:
  1211. case PKA_MODE_ARITHMETIC_ADD:
  1212. case PKA_MODE_COMPARISON:
  1213. case PKA_MODE_ARITHMETIC_MUL:
  1214. PKA_Memcpy_u32_to_u32(pRes, &hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_OUT_RESULT], size);
  1215. break;
  1216. default:
  1217. break;
  1218. }
  1219. }
  1220. }
  1221. /**
  1222. * @brief Montgomery parameter computation in blocking mode.
  1223. * @param hpka PKA handle
  1224. * @param in Input information
  1225. * @param Timeout Timeout duration
  1226. * @retval HAL status
  1227. */
  1228. HAL_StatusTypeDef HAL_PKA_MontgomeryParam(PKA_HandleTypeDef *hpka, PKA_MontgomeryParamInTypeDef *in, uint32_t Timeout)
  1229. {
  1230. /* Set input parameter in PKA RAM */
  1231. PKA_MontgomeryParam_Set(hpka, in->size, in->pOp1);
  1232. /* Start the operation */
  1233. return PKA_Process(hpka, PKA_MODE_MONTGOMERY_PARAM, Timeout);
  1234. }
  1235. /**
  1236. * @brief Montgomery parameter computation in non-blocking mode with Interrupt.
  1237. * @param hpka PKA handle
  1238. * @param in Input information
  1239. * @retval HAL status
  1240. */
  1241. HAL_StatusTypeDef HAL_PKA_MontgomeryParam_IT(PKA_HandleTypeDef *hpka, PKA_MontgomeryParamInTypeDef *in)
  1242. {
  1243. /* Set input parameter in PKA RAM */
  1244. PKA_MontgomeryParam_Set(hpka, in->size, in->pOp1);
  1245. /* Start the operation */
  1246. return PKA_Process_IT(hpka, PKA_MODE_MONTGOMERY_PARAM);
  1247. }
  1248. /**
  1249. * @brief Retrieve operation result.
  1250. * @param hpka PKA handle
  1251. * @param pRes pointer to buffer where the result will be copied
  1252. * @retval HAL status
  1253. */
  1254. void HAL_PKA_MontgomeryParam_GetResult(PKA_HandleTypeDef *hpka, uint32_t *pRes)
  1255. {
  1256. uint32_t size;
  1257. /* Retrieve the size of the buffer from the PKA RAM */
  1258. size = (hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MOD_NB_BITS] + 31UL) / 32UL;
  1259. /* Move the result to appropriate location (indicated in out parameter) */
  1260. PKA_Memcpy_u32_to_u32(pRes, &hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_OUT_PARAMETER], size);
  1261. }
  1262. /**
  1263. * @brief Abort any ongoing operation.
  1264. * @param hpka PKA handle
  1265. * @retval HAL status
  1266. */
  1267. HAL_StatusTypeDef HAL_PKA_Abort(PKA_HandleTypeDef *hpka)
  1268. {
  1269. HAL_StatusTypeDef err = HAL_OK;
  1270. /* Clear EN bit */
  1271. /* This abort any operation in progress (PKA RAM content is not guaranted in this case) */
  1272. CLEAR_BIT(hpka->Instance->CR, PKA_CR_EN);
  1273. SET_BIT(hpka->Instance->CR, PKA_CR_EN);
  1274. /* Reset any pending flag */
  1275. SET_BIT(hpka->Instance->CLRFR, PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
  1276. /* Reset the error code */
  1277. hpka->ErrorCode = HAL_PKA_ERROR_NONE;
  1278. /* Reset the state */
  1279. hpka->State = HAL_PKA_STATE_READY;
  1280. return err;
  1281. }
  1282. /**
  1283. * @brief Reset the PKA RAM.
  1284. * @param hpka PKA handle
  1285. * @retval None
  1286. */
  1287. void HAL_PKA_RAMReset(PKA_HandleTypeDef *hpka)
  1288. {
  1289. uint32_t index;
  1290. /* For each element in the PKA RAM */
  1291. for (index = 0; index < PKA_RAM_SIZE; index++)
  1292. {
  1293. /* Clear the content */
  1294. hpka->Instance->RAM[index] = 0UL;
  1295. }
  1296. }
  1297. /**
  1298. * @brief This function handles PKA event interrupt request.
  1299. * @param hpka PKA handle
  1300. * @retval None
  1301. */
  1302. void HAL_PKA_IRQHandler(PKA_HandleTypeDef *hpka)
  1303. {
  1304. uint32_t mode = PKA_GetMode(hpka);
  1305. FlagStatus addErrFlag = __HAL_PKA_GET_FLAG(hpka, PKA_FLAG_ADDRERR);
  1306. FlagStatus ramErrFlag = __HAL_PKA_GET_FLAG(hpka, PKA_FLAG_RAMERR);
  1307. FlagStatus procEndFlag = __HAL_PKA_GET_FLAG(hpka, PKA_FLAG_PROCEND);
  1308. /* Address error interrupt occurred */
  1309. if ((__HAL_PKA_GET_IT_SOURCE(hpka, PKA_IT_ADDRERR) == SET) && (addErrFlag == SET))
  1310. {
  1311. hpka->ErrorCode |= HAL_PKA_ERROR_ADDRERR;
  1312. /* Clear ADDRERR flag */
  1313. __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_ADDRERR);
  1314. }
  1315. /* RAM access error interrupt occurred */
  1316. if ((__HAL_PKA_GET_IT_SOURCE(hpka, PKA_IT_RAMERR) == SET) && (ramErrFlag == SET))
  1317. {
  1318. hpka->ErrorCode |= HAL_PKA_ERROR_RAMERR;
  1319. /* Clear RAMERR flag */
  1320. __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_RAMERR);
  1321. }
  1322. /* Check the operation success in case of ECDSA signature */
  1323. if (mode == PKA_MODE_ECDSA_SIGNATURE)
  1324. {
  1325. /* If error output result is different from 0, ecdsa sign operation need to be repeated */
  1326. if (hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_ERROR] != 0UL)
  1327. {
  1328. hpka->ErrorCode |= HAL_PKA_ERROR_OPERATION;
  1329. }
  1330. }
  1331. /* Trigger the error callback if an error is present */
  1332. if (hpka->ErrorCode != HAL_PKA_ERROR_NONE)
  1333. {
  1334. #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
  1335. hpka->ErrorCallback(hpka);
  1336. #else
  1337. HAL_PKA_ErrorCallback(hpka);
  1338. #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
  1339. }
  1340. /* End Of Operation interrupt occurred */
  1341. if ((__HAL_PKA_GET_IT_SOURCE(hpka, PKA_IT_PROCEND) == SET) && (procEndFlag == SET))
  1342. {
  1343. /* Clear PROCEND flag */
  1344. __HAL_PKA_CLEAR_FLAG(hpka, PKA_FLAG_PROCEND);
  1345. /* Set the state to ready */
  1346. hpka->State = HAL_PKA_STATE_READY;
  1347. #if (USE_HAL_PKA_REGISTER_CALLBACKS == 1)
  1348. hpka->OperationCpltCallback(hpka);
  1349. #else
  1350. HAL_PKA_OperationCpltCallback(hpka);
  1351. #endif /* USE_HAL_PKA_REGISTER_CALLBACKS */
  1352. }
  1353. }
  1354. /**
  1355. * @brief Process completed callback.
  1356. * @param hpka PKA handle
  1357. * @retval None
  1358. */
  1359. __weak void HAL_PKA_OperationCpltCallback(PKA_HandleTypeDef *hpka)
  1360. {
  1361. /* Prevent unused argument(s) compilation warning */
  1362. UNUSED(hpka);
  1363. /* NOTE : This function should not be modified, when the callback is needed,
  1364. the HAL_PKA_OperationCpltCallback could be implemented in the user file
  1365. */
  1366. }
  1367. /**
  1368. * @brief Error callback.
  1369. * @param hpka PKA handle
  1370. * @retval None
  1371. */
  1372. __weak void HAL_PKA_ErrorCallback(PKA_HandleTypeDef *hpka)
  1373. {
  1374. /* Prevent unused argument(s) compilation warning */
  1375. UNUSED(hpka);
  1376. /* NOTE : This function should not be modified, when the callback is needed,
  1377. the HAL_PKA_ErrorCallback could be implemented in the user file
  1378. */
  1379. }
  1380. /**
  1381. * @}
  1382. */
  1383. /** @defgroup PKA_Exported_Functions_Group3 Peripheral State and Error functions
  1384. * @brief Peripheral State and Error functions
  1385. *
  1386. @verbatim
  1387. ===============================================================================
  1388. ##### Peripheral State and Error functions #####
  1389. ===============================================================================
  1390. [..]
  1391. This subsection permit to get in run-time the status of the peripheral.
  1392. @endverbatim
  1393. * @{
  1394. */
  1395. /**
  1396. * @brief Return the PKA handle state.
  1397. * @param hpka PKA handle
  1398. * @retval HAL status
  1399. */
  1400. HAL_PKA_StateTypeDef HAL_PKA_GetState(PKA_HandleTypeDef *hpka)
  1401. {
  1402. /* Return PKA handle state */
  1403. return hpka->State;
  1404. }
  1405. /**
  1406. * @brief Return the PKA error code.
  1407. * @param hpka PKA handle
  1408. * @retval PKA error code
  1409. */
  1410. uint32_t HAL_PKA_GetError(PKA_HandleTypeDef *hpka)
  1411. {
  1412. /* Return PKA handle error code */
  1413. return hpka->ErrorCode;
  1414. }
  1415. /**
  1416. * @}
  1417. */
  1418. /**
  1419. * @}
  1420. */
  1421. /** @addtogroup PKA_Private_Functions
  1422. * @{
  1423. */
  1424. /**
  1425. * @brief Get PKA operating mode.
  1426. * @param hpka PKA handle
  1427. * @retval Return the current mode
  1428. */
  1429. uint32_t PKA_GetMode(PKA_HandleTypeDef *hpka)
  1430. {
  1431. /* return the shifted PKA_CR_MODE value */
  1432. return (uint32_t)(READ_BIT(hpka->Instance->CR, PKA_CR_MODE) >> PKA_CR_MODE_Pos);
  1433. }
  1434. /**
  1435. * @brief Wait for operation completion or timeout.
  1436. * @param hpka PKA handle
  1437. * @param Timeout Timeout duration in millisecond.
  1438. * @param Tickstart Tick start value
  1439. * @retval HAL status
  1440. */
  1441. HAL_StatusTypeDef PKA_PollEndOfOperation(PKA_HandleTypeDef *hpka, uint32_t Timeout, uint32_t Tickstart)
  1442. {
  1443. /* Wait for the end of operation or timeout */
  1444. while ((hpka->Instance->SR & PKA_SR_PROCENDF) == 0UL)
  1445. {
  1446. /* Check if timeout is disabled (set to infinite wait) */
  1447. if (Timeout != HAL_MAX_DELAY)
  1448. {
  1449. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0UL))
  1450. {
  1451. return HAL_TIMEOUT;
  1452. }
  1453. }
  1454. }
  1455. return HAL_OK;
  1456. }
  1457. /**
  1458. * @brief Return a hal error code based on PKA error flags.
  1459. * @param hpka PKA handle
  1460. * @param mode PKA operating mode
  1461. * @retval error code
  1462. */
  1463. uint32_t PKA_CheckError(PKA_HandleTypeDef *hpka, uint32_t mode)
  1464. {
  1465. uint32_t err = HAL_PKA_ERROR_NONE;
  1466. /* Check RAMERR error */
  1467. if (__HAL_PKA_GET_FLAG(hpka, PKA_FLAG_RAMERR) == SET)
  1468. {
  1469. err |= HAL_PKA_ERROR_RAMERR;
  1470. }
  1471. /* Check ADDRERR error */
  1472. if (__HAL_PKA_GET_FLAG(hpka, PKA_FLAG_ADDRERR) == SET)
  1473. {
  1474. err |= HAL_PKA_ERROR_ADDRERR;
  1475. }
  1476. /* Check the operation success in case of ECDSA signature */
  1477. if (mode == PKA_MODE_ECDSA_SIGNATURE)
  1478. {
  1479. #define EDCSA_SIGN_NOERROR 0UL
  1480. /* If error output result is different from no error, ecsa sign operation need to be repeated */
  1481. if (hpka->Instance->RAM[PKA_ECDSA_SIGN_OUT_ERROR] != EDCSA_SIGN_NOERROR)
  1482. {
  1483. err |= HAL_PKA_ERROR_OPERATION;
  1484. }
  1485. }
  1486. return err;
  1487. }
  1488. /**
  1489. * @brief Get number of bits inside an array of u8.
  1490. * @param byteNumber Number of u8 inside the array
  1491. */
  1492. uint32_t PKA_GetBitSize_u8(uint32_t byteNumber)
  1493. {
  1494. /* Convert from number of uint8_t in an array to the associated number of bits in this array */
  1495. return byteNumber * 8UL;
  1496. }
  1497. /**
  1498. * @brief Get optimal number of bits inside an array of u8.
  1499. * @param byteNumber Number of u8 inside the array
  1500. * @param msb Most significant uint8_t of the array
  1501. */
  1502. uint32_t PKA_GetOptBitSize_u8(uint32_t byteNumber, uint8_t msb)
  1503. {
  1504. uint32_t position;
  1505. position = 32UL - __CLZ(msb);
  1506. return (((byteNumber - 1UL) * 8UL) + position);
  1507. }
  1508. /**
  1509. * @brief Get number of bits inside an array of u32.
  1510. * @param wordNumber Number of u32 inside the array
  1511. */
  1512. uint32_t PKA_GetBitSize_u32(uint32_t wordNumber)
  1513. {
  1514. /* Convert from number of uint32_t in an array to the associated number of bits in this array */
  1515. return wordNumber * 32UL;
  1516. }
  1517. /**
  1518. * @brief Get number of uint8_t element in an array of bitSize bits.
  1519. * @param bitSize Number of bits in an array
  1520. */
  1521. uint32_t PKA_GetArraySize_u8(uint32_t bitSize)
  1522. {
  1523. /* Manage the non aligned on uint8_t bitsize: */
  1524. /* 512 bits requires 64 uint8_t */
  1525. /* 521 bits requires 66 uint8_t */
  1526. return ((bitSize + 7UL) / 8UL);
  1527. }
  1528. /**
  1529. * @brief Copy uint32_t array to uint8_t array to fit PKA number representation.
  1530. * @param dst Pointer to destination
  1531. * @param src Pointer to source
  1532. * @param n Number of uint8_t to copy
  1533. * @retval dst
  1534. */
  1535. void PKA_Memcpy_u32_to_u8(uint8_t dst[], __IO const uint32_t src[], size_t n)
  1536. {
  1537. if (dst != NULL)
  1538. {
  1539. if (src != NULL)
  1540. {
  1541. uint32_t index_uint32_t = 0UL; /* This index is used outside of the loop */
  1542. for (; index_uint32_t < (n / 4UL); index_uint32_t++)
  1543. {
  1544. /* Avoid casting from uint8_t* to uint32_t* by copying 4 uint8_t in a row */
  1545. /* Apply __REV equivalent */
  1546. uint32_t index_uint8_t = n - 4UL - (index_uint32_t * 4UL);
  1547. dst[index_uint8_t + 3UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
  1548. dst[index_uint8_t + 2UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
  1549. dst[index_uint8_t + 1UL] = (uint8_t)((src[index_uint32_t] & 0x00FF0000U) >> 16UL);
  1550. dst[index_uint8_t + 0UL] = (uint8_t)((src[index_uint32_t] & 0xFF000000U) >> 24UL);
  1551. }
  1552. /* Manage the buffers not aligned on uint32_t */
  1553. if ((n % 4UL) == 1UL)
  1554. {
  1555. dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
  1556. }
  1557. else if ((n % 4UL) == 2UL)
  1558. {
  1559. dst[1UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
  1560. dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
  1561. }
  1562. else if ((n % 4UL) == 3UL)
  1563. {
  1564. dst[2UL] = (uint8_t)((src[index_uint32_t] & 0x000000FFU));
  1565. dst[1UL] = (uint8_t)((src[index_uint32_t] & 0x0000FF00U) >> 8UL);
  1566. dst[0UL] = (uint8_t)((src[index_uint32_t] & 0x00FF0000U) >> 16UL);
  1567. }
  1568. else
  1569. {
  1570. /* The last element is already handle in the loop */
  1571. }
  1572. }
  1573. }
  1574. }
  1575. /**
  1576. * @brief Copy uint8_t array to uint32_t array to fit PKA number representation.
  1577. * @param dst Pointer to destination
  1578. * @param src Pointer to source
  1579. * @param n Number of uint8_t to copy (must be multiple of 4)
  1580. * @retval dst
  1581. */
  1582. void PKA_Memcpy_u8_to_u32(__IO uint32_t dst[], const uint8_t src[], size_t n)
  1583. {
  1584. if (dst != NULL)
  1585. {
  1586. if (src != NULL)
  1587. {
  1588. uint32_t index = 0UL; /* This index is used outside of the loop */
  1589. for (; index < (n / 4UL); index++)
  1590. {
  1591. /* Apply the equivalent of __REV from uint8_t to uint32_t */
  1592. dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
  1593. | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL) \
  1594. | ((uint32_t)src[(n - (index * 4UL) - 3UL)] << 16UL) \
  1595. | ((uint32_t)src[(n - (index * 4UL) - 4UL)] << 24UL);
  1596. }
  1597. /* Manage the buffers not aligned on uint32_t */
  1598. if ((n % 4UL) == 1UL)
  1599. {
  1600. dst[index] = (uint32_t)src[(n - (index * 4UL) - 1UL)];
  1601. }
  1602. else if ((n % 4UL) == 2UL)
  1603. {
  1604. dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
  1605. | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL);
  1606. }
  1607. else if ((n % 4UL) == 3UL)
  1608. {
  1609. dst[index] = ((uint32_t)src[(n - (index * 4UL) - 1UL)]) \
  1610. | ((uint32_t)src[(n - (index * 4UL) - 2UL)] << 8UL) \
  1611. | ((uint32_t)src[(n - (index * 4UL) - 3UL)] << 16UL);
  1612. }
  1613. else
  1614. {
  1615. /* The last element is already handle in the loop */
  1616. }
  1617. }
  1618. }
  1619. }
  1620. /**
  1621. * @brief Copy uint32_t array to uint32_t array.
  1622. * @param dst Pointer to destination
  1623. * @param src Pointer to source
  1624. * @param n Number of u32 to be handled
  1625. * @retval dst
  1626. */
  1627. void PKA_Memcpy_u32_to_u32(__IO uint32_t dst[], __IO const uint32_t src[], size_t n)
  1628. {
  1629. /* If a destination buffer is provided */
  1630. if (dst != NULL)
  1631. {
  1632. /* If a source buffer is provided */
  1633. if (src != NULL)
  1634. {
  1635. /* For each element in the array */
  1636. for (uint32_t index = 0UL; index < n; index++)
  1637. {
  1638. /* Copy the content */
  1639. dst[index] = src[index];
  1640. }
  1641. }
  1642. }
  1643. }
  1644. /**
  1645. * @brief Generic function to start a PKA operation in blocking mode.
  1646. * @param hpka PKA handle
  1647. * @param mode PKA operation
  1648. * @param Timeout Timeout duration
  1649. * @retval HAL status
  1650. */
  1651. HAL_StatusTypeDef PKA_Process(PKA_HandleTypeDef *hpka, uint32_t mode, uint32_t Timeout)
  1652. {
  1653. HAL_StatusTypeDef err = HAL_OK;
  1654. uint32_t tickstart;
  1655. if (hpka->State == HAL_PKA_STATE_READY)
  1656. {
  1657. /* Set the state to busy */
  1658. hpka->State = HAL_PKA_STATE_BUSY;
  1659. /* Clear any pending error */
  1660. hpka->ErrorCode = HAL_PKA_ERROR_NONE;
  1661. /* Init tickstart for timeout management*/
  1662. tickstart = HAL_GetTick();
  1663. /* Set the mode and deactivate the interrupts */
  1664. MODIFY_REG(hpka->Instance->CR, PKA_CR_MODE | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE, mode << PKA_CR_MODE_Pos);
  1665. /* Start the computation */
  1666. hpka->Instance->CR |= PKA_CR_START;
  1667. /* Wait for the end of operation or timeout */
  1668. if (PKA_PollEndOfOperation(hpka, Timeout, tickstart) != HAL_OK)
  1669. {
  1670. /* Abort any ongoing operation */
  1671. CLEAR_BIT(hpka->Instance->CR, PKA_CR_EN);
  1672. hpka->ErrorCode |= HAL_PKA_ERROR_TIMEOUT;
  1673. /* Make ready for the next operation */
  1674. SET_BIT(hpka->Instance->CR, PKA_CR_EN);
  1675. }
  1676. /* Check error */
  1677. hpka->ErrorCode |= PKA_CheckError(hpka, mode);
  1678. /* Clear all flags */
  1679. hpka->Instance->CLRFR |= (PKA_CLRFR_PROCENDFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_ADDRERRFC);
  1680. /* Set the state to ready */
  1681. hpka->State = HAL_PKA_STATE_READY;
  1682. /* Manage the result based on encountered errors */
  1683. if (hpka->ErrorCode != HAL_PKA_ERROR_NONE)
  1684. {
  1685. err = HAL_ERROR;
  1686. }
  1687. }
  1688. else
  1689. {
  1690. err = HAL_ERROR;
  1691. }
  1692. return err;
  1693. }
  1694. /**
  1695. * @brief Generic function to start a PKA operation in non-blocking mode with Interrupt.
  1696. * @param hpka PKA handle
  1697. * @param mode PKA operation
  1698. * @retval HAL status
  1699. */
  1700. HAL_StatusTypeDef PKA_Process_IT(PKA_HandleTypeDef *hpka, uint32_t mode)
  1701. {
  1702. HAL_StatusTypeDef err = HAL_OK;
  1703. if (hpka->State == HAL_PKA_STATE_READY)
  1704. {
  1705. /* Set the state to busy */
  1706. hpka->State = HAL_PKA_STATE_BUSY;
  1707. /* Clear any pending error */
  1708. hpka->ErrorCode = HAL_PKA_ERROR_NONE;
  1709. /* Set the mode and activate interrupts */
  1710. MODIFY_REG(hpka->Instance->CR, PKA_CR_MODE | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE, (mode << PKA_CR_MODE_Pos) | PKA_CR_PROCENDIE | PKA_CR_RAMERRIE | PKA_CR_ADDRERRIE);
  1711. /* Start the computation */
  1712. hpka->Instance->CR |= PKA_CR_START;
  1713. }
  1714. else
  1715. {
  1716. err = HAL_ERROR;
  1717. }
  1718. return err;
  1719. }
  1720. /**
  1721. * @brief Set input parameters.
  1722. * @param hpka PKA handle
  1723. * @param in Input information
  1724. */
  1725. void PKA_ModExp_Set(PKA_HandleTypeDef *hpka, PKA_ModExpInTypeDef *in)
  1726. {
  1727. /* Get the number of bit per operand */
  1728. hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = PKA_GetBitSize_u8(in->OpSize);
  1729. /* Get the number of bit of the exponent */
  1730. hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = PKA_GetBitSize_u8(in->expSize);
  1731. /* Move the input parameters pOp1 to PKA RAM */
  1732. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT_BASE], in->pOp1, in->OpSize);
  1733. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT_BASE + (in->OpSize / 4UL));
  1734. /* Move the exponent to PKA RAM */
  1735. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT], in->pExp, in->expSize);
  1736. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT + (in->expSize / 4UL));
  1737. /* Move the modulus to PKA RAM */
  1738. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MODULUS], in->pMod, in->OpSize);
  1739. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MODULUS + (in->OpSize / 4UL));
  1740. }
  1741. /**
  1742. * @brief Set input parameters.
  1743. * @param hpka PKA handle
  1744. * @param in Input information
  1745. */
  1746. void PKA_ModExpFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ModExpFastModeInTypeDef *in)
  1747. {
  1748. /* Get the number of bit per operand */
  1749. hpka->Instance->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = PKA_GetBitSize_u8(in->OpSize);
  1750. /* Get the number of bit of the exponent */
  1751. hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = PKA_GetBitSize_u8(in->expSize);
  1752. /* Move the input parameters pOp1 to PKA RAM */
  1753. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT_BASE], in->pOp1, in->OpSize);
  1754. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT_BASE + (in->OpSize / 4UL));
  1755. /* Move the exponent to PKA RAM */
  1756. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_EXPONENT], in->pExp, in->expSize);
  1757. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_EXPONENT + (in->expSize / 4UL));
  1758. /* Move the modulus to PKA RAM */
  1759. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MODULUS], in->pMod, in->OpSize);
  1760. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MODULUS + (in->OpSize / 4UL));
  1761. /* Move the Montgomery parameter to PKA RAM */
  1762. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_EXP_IN_MONTGOMERY_PARAM], in->pMontgomeryParam, in->expSize / 4UL);
  1763. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_EXP_IN_MONTGOMERY_PARAM + (in->expSize / 4UL));
  1764. }
  1765. /**
  1766. * @brief Set input parameters.
  1767. * @param hpka PKA handle
  1768. * @param in Input information
  1769. */
  1770. void PKA_ECDSASign_Set(PKA_HandleTypeDef *hpka, PKA_ECDSASignInTypeDef *in)
  1771. {
  1772. /* Get the prime order n length */
  1773. hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS] = PKA_GetOptBitSize_u8(in->primeOrderSize, *(in->primeOrder));
  1774. /* Get the modulus p length */
  1775. hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1776. /* Get the coefficient a sign */
  1777. hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN] = in->coefSign;
  1778. /* Move the input parameters coefficient |a| to PKA RAM */
  1779. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_A_COEFF], in->coef, in->modulusSize);
  1780. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1781. /* Move the input parameters modulus value p to PKA RAM */
  1782. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_MOD_GF], in->modulus, in->modulusSize);
  1783. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1784. /* Move the input parameters integer k to PKA RAM */
  1785. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_K], in->integer, in->primeOrderSize);
  1786. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_K + ((in->primeOrderSize + 3UL) / 4UL));
  1787. /* Move the input parameters base point G coordinate x to PKA RAM */
  1788. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_INITIAL_POINT_X], in->basePointX, in->modulusSize);
  1789. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1790. /* Move the input parameters base point G coordinate y to PKA RAM */
  1791. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y], in->basePointY, in->modulusSize);
  1792. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1793. /* Move the input parameters hash of message z to PKA RAM */
  1794. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_HASH_E], in->hash, in->primeOrderSize);
  1795. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_HASH_E + ((in->primeOrderSize + 3UL) / 4UL));
  1796. /* Move the input parameters private key d to PKA RAM */
  1797. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D], in->privateKey, in->primeOrderSize);
  1798. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D + ((in->primeOrderSize + 3UL) / 4UL));
  1799. /* Move the input parameters prime order n to PKA RAM */
  1800. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_SIGN_IN_ORDER_N], in->primeOrder, in->primeOrderSize);
  1801. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_SIGN_IN_ORDER_N + ((in->primeOrderSize + 3UL) / 4UL));
  1802. }
  1803. /**
  1804. * @brief Set input parameters.
  1805. * @param hpka PKA handle
  1806. * @param in Input information
  1807. */
  1808. void PKA_ECDSAVerif_Set(PKA_HandleTypeDef *hpka, PKA_ECDSAVerifInTypeDef *in)
  1809. {
  1810. /* Get the prime order n length */
  1811. hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS] = PKA_GetOptBitSize_u8(in->primeOrderSize, *(in->primeOrder));
  1812. /* Get the modulus p length */
  1813. hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1814. /* Get the coefficient a sign */
  1815. hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN] = in->coefSign;
  1816. /* Move the input parameters coefficient |a| to PKA RAM */
  1817. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_A_COEFF], in->coef, in->modulusSize);
  1818. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1819. /* Move the input parameters modulus value p to PKA RAM */
  1820. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_MOD_GF], in->modulus, in->modulusSize);
  1821. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1822. /* Move the input parameters base point G coordinate x to PKA RAM */
  1823. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_INITIAL_POINT_X], in->basePointX, in->modulusSize);
  1824. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1825. /* Move the input parameters base point G coordinate y to PKA RAM */
  1826. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y], in->basePointY, in->modulusSize);
  1827. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1828. /* Move the input parameters public-key curve point Q coordinate xQ to PKA RAM */
  1829. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X], in->pPubKeyCurvePtX, in->modulusSize);
  1830. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1831. /* Move the input parameters public-key curve point Q coordinate xQ to PKA RAM */
  1832. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y], in->pPubKeyCurvePtY, in->modulusSize);
  1833. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1834. /* Move the input parameters signature part r to PKA RAM */
  1835. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_SIGNATURE_R], in->RSign, in->primeOrderSize);
  1836. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_SIGNATURE_R + ((in->primeOrderSize + 3UL) / 4UL));
  1837. /* Move the input parameters signature part s to PKA RAM */
  1838. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_SIGNATURE_S], in->SSign, in->primeOrderSize);
  1839. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_SIGNATURE_S + ((in->primeOrderSize + 3UL) / 4UL));
  1840. /* Move the input parameters hash of message z to PKA RAM */
  1841. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_HASH_E], in->hash, in->primeOrderSize);
  1842. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_HASH_E + ((in->primeOrderSize + 3UL) / 4UL));
  1843. /* Move the input parameters curve prime order n to PKA RAM */
  1844. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECDSA_VERIF_IN_ORDER_N], in->primeOrder, in->primeOrderSize);
  1845. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECDSA_VERIF_IN_ORDER_N + ((in->primeOrderSize + 3UL) / 4UL));
  1846. }
  1847. /**
  1848. * @brief Set input parameters.
  1849. * @param hpka PKA handle
  1850. * @param in Input information
  1851. */
  1852. void PKA_RSACRTExp_Set(PKA_HandleTypeDef *hpka, PKA_RSACRTExpInTypeDef *in)
  1853. {
  1854. /* Get the operand length M */
  1855. hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] = PKA_GetBitSize_u8(in->size);
  1856. /* Move the input parameters operand dP to PKA RAM */
  1857. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_DP_CRT], in->pOpDp, in->size / 2UL);
  1858. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_DP_CRT + (in->size / 8UL));
  1859. /* Move the input parameters operand dQ to PKA RAM */
  1860. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_DQ_CRT], in->pOpDq, in->size / 2UL);
  1861. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_DQ_CRT + (in->size / 8UL));
  1862. /* Move the input parameters operand qinv to PKA RAM */
  1863. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_QINV_CRT], in->pOpQinv, in->size / 2UL);
  1864. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_QINV_CRT + (in->size / 8UL));
  1865. /* Move the input parameters prime p to PKA RAM */
  1866. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_PRIME_P], in->pPrimeP, in->size / 2UL);
  1867. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_PRIME_P + (in->size / 8UL));
  1868. /* Move the input parameters prime q to PKA RAM */
  1869. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_PRIME_Q], in->pPrimeQ, in->size / 2UL);
  1870. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_PRIME_Q + (in->size / 8UL));
  1871. /* Move the input parameters operand A to PKA RAM */
  1872. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_RSA_CRT_EXP_IN_EXPONENT_BASE], in->popA, in->size);
  1873. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_RSA_CRT_EXP_IN_EXPONENT_BASE + (in->size / 4UL));
  1874. }
  1875. /**
  1876. * @brief Set input parameters.
  1877. * @param hpka PKA handle
  1878. * @param in Input information
  1879. */
  1880. void PKA_PointCheck_Set(PKA_HandleTypeDef *hpka, PKA_PointCheckInTypeDef *in)
  1881. {
  1882. /* Get the modulus length */
  1883. hpka->Instance->RAM[PKA_POINT_CHECK_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1884. /* Get the coefficient a sign */
  1885. hpka->Instance->RAM[PKA_POINT_CHECK_IN_A_COEFF_SIGN] = in->coefSign;
  1886. /* Move the input parameters coefficient |a| to PKA RAM */
  1887. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_A_COEFF], in->coefA, in->modulusSize);
  1888. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1889. /* Move the input parameters coefficient b to PKA RAM */
  1890. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_B_COEFF], in->coefB, in->modulusSize);
  1891. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_B_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1892. /* Move the input parameters modulus value p to PKA RAM */
  1893. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_MOD_GF], in->modulus, in->modulusSize);
  1894. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1895. /* Move the input parameters Point P coordinate x to PKA RAM */
  1896. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
  1897. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1898. /* Move the input parameters Point P coordinate y to PKA RAM */
  1899. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
  1900. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1901. }
  1902. /**
  1903. * @brief Set input parameters.
  1904. * @param hpka PKA handle
  1905. * @param in Input information
  1906. */
  1907. void PKA_ECCMul_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulInTypeDef *in)
  1908. {
  1909. /* Get the scalar multiplier k length */
  1910. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = PKA_GetOptBitSize_u8(in->scalarMulSize, *(in->scalarMul));
  1911. /* Get the modulus length */
  1912. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1913. /* Get the coefficient a sign */
  1914. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = in->coefSign;
  1915. /* Move the input parameters coefficient |a| to PKA RAM */
  1916. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF], in->coefA, in->modulusSize);
  1917. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1918. /* Move the input parameters modulus value p to PKA RAM */
  1919. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MOD_GF], in->modulus, in->modulusSize);
  1920. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1921. /* Move the input parameters scalar multiplier k to PKA RAM */
  1922. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_K], in->scalarMul, in->scalarMulSize);
  1923. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_K + ((in->scalarMulSize + 3UL) / 4UL));
  1924. /* Move the input parameters Point P coordinate x to PKA RAM */
  1925. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
  1926. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1927. /* Move the input parameters Point P coordinate y to PKA RAM */
  1928. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
  1929. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1930. }
  1931. /**
  1932. * @brief Set input parameters.
  1933. * @param hpka PKA handle
  1934. * @param in Input information
  1935. */
  1936. void PKA_ECCMulFastMode_Set(PKA_HandleTypeDef *hpka, PKA_ECCMulFastModeInTypeDef *in)
  1937. {
  1938. /* Get the scalar multiplier k length */
  1939. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = PKA_GetOptBitSize_u8(in->scalarMulSize, *(in->scalarMul));
  1940. /* Get the modulus length */
  1941. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = PKA_GetOptBitSize_u8(in->modulusSize, *(in->modulus));
  1942. /* Get the coefficient a sign */
  1943. hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = in->coefSign;
  1944. /* Move the input parameters coefficient |a| to PKA RAM */
  1945. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF], in->coefA, in->modulusSize);
  1946. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_A_COEFF + ((in->modulusSize + 3UL) / 4UL));
  1947. /* Move the input parameters modulus value p to PKA RAM */
  1948. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MOD_GF], in->modulus, in->modulusSize);
  1949. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MOD_GF + ((in->modulusSize + 3UL) / 4UL));
  1950. /* Move the input parameters scalar multiplier k to PKA RAM */
  1951. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_K], in->scalarMul, in->scalarMulSize);
  1952. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_K + ((in->scalarMulSize + 3UL) / 4UL));
  1953. /* Move the input parameters Point P coordinate x to PKA RAM */
  1954. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_X], in->pointX, in->modulusSize);
  1955. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_X + ((in->modulusSize + 3UL) / 4UL));
  1956. /* Move the input parameters Point P coordinate y to PKA RAM */
  1957. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_POINT_CHECK_IN_INITIAL_POINT_Y], in->pointY, in->modulusSize);
  1958. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_POINT_CHECK_IN_INITIAL_POINT_Y + ((in->modulusSize + 3UL) / 4UL));
  1959. /* Move the Montgomery parameter to PKA RAM */
  1960. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ECC_SCALAR_MUL_IN_MONTGOMERY_PARAM], in->pMontgomeryParam, (in->modulusSize + 3UL) / 4UL);
  1961. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ECC_SCALAR_MUL_IN_MONTGOMERY_PARAM + ((in->modulusSize + 3UL) / 4UL));
  1962. }
  1963. /**
  1964. * @brief Set input parameters.
  1965. * @param hpka PKA handle
  1966. * @param in Input information
  1967. */
  1968. void PKA_ModInv_Set(PKA_HandleTypeDef *hpka, PKA_ModInvInTypeDef *in)
  1969. {
  1970. /* Get the number of bit per operand */
  1971. hpka->Instance->RAM[PKA_MODULAR_INV_NB_BITS] = PKA_GetBitSize_u32(in->size);
  1972. /* Move the input parameters operand A to PKA RAM */
  1973. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_INV_IN_OP1], in->pOp1, in->size);
  1974. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_INV_IN_OP1 + in->size);
  1975. /* Move the input parameters modulus value n to PKA RAM */
  1976. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_INV_IN_OP2_MOD], in->pMod, in->size * 4UL);
  1977. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_INV_IN_OP2_MOD + in->size);
  1978. }
  1979. /**
  1980. * @brief Set input parameters.
  1981. * @param hpka PKA handle
  1982. * @param in Input information
  1983. */
  1984. void PKA_ModRed_Set(PKA_HandleTypeDef *hpka, PKA_ModRedInTypeDef *in)
  1985. {
  1986. /* Get the number of bit per operand */
  1987. hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_OP_LENGTH] = PKA_GetBitSize_u32(in->OpSize);
  1988. /* Get the number of bit per modulus */
  1989. hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_MOD_LENGTH] = PKA_GetBitSize_u8(in->modSize);
  1990. /* Move the input parameters operand A to PKA RAM */
  1991. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_OPERAND], in->pOp1, in->OpSize);
  1992. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_REDUC_IN_OPERAND + in->OpSize);
  1993. /* Move the input parameters modulus value n to PKA RAM */
  1994. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MODULAR_REDUC_IN_MODULUS], in->pMod, in->modSize);
  1995. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MODULAR_REDUC_IN_MODULUS + (in->modSize / 4UL));
  1996. }
  1997. /**
  1998. * @brief Set input parameters.
  1999. * @param hpka PKA handle
  2000. * @param size Size of the operand
  2001. * @param pOp1 Generic pointer to input data
  2002. */
  2003. void PKA_MontgomeryParam_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint8_t *pOp1)
  2004. {
  2005. if (pOp1 != NULL)
  2006. {
  2007. /* Get the number of bit per operand */
  2008. hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MOD_NB_BITS] = PKA_GetOptBitSize_u8(size, *pOp1);
  2009. /* Move the input parameters pOp1 to PKA RAM */
  2010. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_MONTGOMERY_PARAM_IN_MODULUS], pOp1, size);
  2011. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_MONTGOMERY_PARAM_IN_MODULUS + ((size + 3UL) / 4UL));
  2012. }
  2013. }
  2014. /**
  2015. * @brief Generic function to set input parameters.
  2016. * @param hpka PKA handle
  2017. * @param size Size of the operand
  2018. * @param pOp1 Generic pointer to input data
  2019. * @param pOp2 Generic pointer to input data
  2020. * @param pOp3 Generic pointer to input data
  2021. */
  2022. void PKA_ARI_Set(PKA_HandleTypeDef *hpka, const uint32_t size, const uint32_t *pOp1, const uint32_t *pOp2, const uint8_t *pOp3)
  2023. {
  2024. /* Get the number of bit per operand */
  2025. hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_NB_BITS] = PKA_GetBitSize_u32(size);
  2026. if (pOp1 != NULL)
  2027. {
  2028. /* Move the input parameters pOp1 to PKA RAM */
  2029. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP1], pOp1, size);
  2030. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP1 + size);
  2031. }
  2032. if (pOp2 != NULL)
  2033. {
  2034. /* Move the input parameters pOp2 to PKA RAM */
  2035. PKA_Memcpy_u32_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP2], pOp2, size);
  2036. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP2 + size);
  2037. }
  2038. if (pOp3 != NULL)
  2039. {
  2040. /* Move the input parameters pOp3 to PKA RAM */
  2041. PKA_Memcpy_u8_to_u32(&hpka->Instance->RAM[PKA_ARITHMETIC_ALL_OPS_IN_OP3], pOp3, size * 4UL);
  2042. __PKA_RAM_PARAM_END(hpka->Instance->RAM, PKA_ARITHMETIC_ALL_OPS_IN_OP3 + size);
  2043. }
  2044. }
  2045. /**
  2046. * @}
  2047. */
  2048. /**
  2049. * @}
  2050. */
  2051. #endif /* defined(PKA) && defined(HAL_PKA_MODULE_ENABLED) */
  2052. /**
  2053. * @}
  2054. */
  2055. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/