1 /* USER CODE BEGIN Header */
2 /**
3   ******************************************************************************
4   * @file    pka_manager.c
5   * @author  GPM WBL Application Team
6   * @brief   This file provides utility functions for concurrent access to PKA
7   *          peripheral.
8   ******************************************************************************
9   * @attention
10   *
11   * Copyright (c) 2024 STMicroelectronics.
12   * All rights reserved.
13   *
14   * This software is licensed under terms that can be found in the LICENSE file
15   * in the root directory of this software component.
16   * If no LICENSE file comes with this software, it is provided AS-IS.
17   *
18   ******************************************************************************
19   */
20 /* USER CODE END Header */
21 
22 /* Includes ------------------------------------------------------------------*/
23 #include "pka_manager.h"
24 #include "stm32wb0x.h"
25 
26 /** @defgroup PKA_Manager  PKA Manager
27 * @{
28 */
29 
30 /** @defgroup PKA_Manager_Private_Variables Private Variables
31 * @{
32 */
33 
34 /**
35 * @}
36 */
37 
38 /** @defgroup PKA_Manager_TypesDefinitions Private Type Definitions
39 * @{
40 */
41 
42 /* Internal state of the PKA */
43 typedef enum
44 {
45   PKAMGR_STATE_READY     =  0,
46   PKAMGR_STATE_IDLE,
47   PKAMGR_STATE_RESET,
48   PKAMGR_STATE_BUSY
49 } PKAMGR_State;
50 
51 /**
52 * @}
53 */
54 
55 /** @defgroup PKA_Manager_Private_Defines Private Defines
56 * @{
57 */
58 #define ATOMIC_SECTION_BEGIN() uint32_t uwPRIMASK_Bit = __get_PRIMASK(); \
59 __disable_irq(); \
60   /* Must be called in the same or in a lower scope of ATOMIC_SECTION_BEGIN */
61 #define ATOMIC_SECTION_END() __set_PRIMASK(uwPRIMASK_Bit)
62 /**
63 * @}
64 */
65 
66 /** @defgroup PKA_Manager_Private_Variables Private Variables
67 * @{
68 */
69 static volatile uint32_t internalState = PKAMGR_STATE_RESET;
70 /**
71 * @}
72 */
73 
74 /** @defgroup PKA_Manager_Private_FunctionPrototypes Private Function Prototypes
75 * @{
76 */
77 PKAMGR_ResultStatus HW_PKA_PrivateInit(void);
78 
79 PKAMGR_ResultStatus HW_PKA_PrivateDeinit(void);
80 
81 PKAMGR_ResultStatus PKAMGR_Status(void);
82 /**
83 * @}
84 */
85 
86 /** @defgroup PKA_Manager_Public_Functions Public Functions
87 * @{
88 */
PKAMGR_Init(void)89 PKAMGR_ResultStatus PKAMGR_Init(void)
90 {
91     internalState = PKAMGR_STATE_IDLE;
92 
93   return PKAMGR_SUCCESS;
94 }
95 
PKAMGR_Deinit(void)96 PKAMGR_ResultStatus PKAMGR_Deinit(void)
97 {
98     internalState = PKAMGR_STATE_RESET;
99 
100   return PKAMGR_SUCCESS;
101 }
102 
PKAMGR_SleepCheck(void)103 PKAMGR_ResultStatus PKAMGR_SleepCheck(void)
104 {
105   PKAMGR_ResultStatus return_value = PKAMGR_ERR_BUSY;
106 
107   if(internalState == PKAMGR_STATE_IDLE)
108     return_value = PKAMGR_SUCCESS;
109 
110   return return_value;
111 }
112 
PKAMGR_Lock()113 PKAMGR_ResultStatus PKAMGR_Lock()
114 {
115   PKAMGR_ResultStatus return_value = PKAMGR_SUCCESS;
116 
117   /* Only one consumer (Application layer or Stack) can use the PKA at the time */
118   ATOMIC_SECTION_BEGIN();
119   if(internalState != PKAMGR_STATE_IDLE)
120   {
121     return_value = PKAMGR_ERR_BUSY;
122   }
123   else
124   {
125     /* Lock mechanism to access concurrently at the PKA resource */
126     internalState = PKAMGR_STATE_BUSY;
127   }
128   ATOMIC_SECTION_END();
129 
130   return return_value;
131 }
132 
PKAMGR_Unlock()133 PKAMGR_ResultStatus PKAMGR_Unlock()
134 {
135   PKAMGR_ResultStatus return_value = PKAMGR_SUCCESS;
136 
137   /* Only one consumer (Application layer or Stack) can use the PKA at the time */
138   ATOMIC_SECTION_BEGIN();
139   if(internalState != PKAMGR_STATE_BUSY)
140   {
141     return_value = PKAMGR_ERR_BUSY;
142   }
143   else
144   {
145     /* Unlock mechanism to access concurrently at the PKA resource */
146     internalState = PKAMGR_STATE_IDLE;
147   }
148   ATOMIC_SECTION_END();
149 
150   return return_value;
151 }
152 
PKAMGR_StartP256PublicKeyGeneration(const uint32_t * privateKey,PKAMGR_funcCB funcCB)153 PKAMGR_ResultStatus PKAMGR_StartP256PublicKeyGeneration(const uint32_t *privateKey, PKAMGR_funcCB funcCB)
154 {
155   return HW_PKA_StartP256DHkeyGeneration(privateKey, (uint32_t *)&PKAStartPoint[0], funcCB);
156 }
157 
PKAMGR_IRQCallback(void)158 __weak void PKAMGR_IRQCallback(void)
159 {
160 }
161 
162 /**
163 * @}
164 */
165 
166 /**
167 * @}
168 */
169