1 /* USER CODE BEGIN Header */
2 /**
3   ******************************************************************************
4   * @file    hw_aes.c
5   * @author  GPM WBL Application Team
6   * @brief   This file provides functions implementation for AES Manager
7   ******************************************************************************
8   * @attention
9   *
10   * Copyright (c) 2024 STMicroelectronics.
11   * All rights reserved.
12   *
13   * This software is licensed under terms that can be found in the LICENSE file
14   * in the root directory of this software component.
15   * If no LICENSE file comes with this software, it is provided AS-IS.
16   *
17   ******************************************************************************
18   */
19 /* USER CODE END Header */
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32wb0x.h"
23 #include "stm32wb0x_ll_bus.h"
24 #include "hw_aes.h"
25 
26 /** @defgroup AES_Manager  AES Manager
27 * @{
28 */
29 
30 /** @defgroup AES_Manager_TypesDefinitions Private Type Definitions
31 * @{
32 */
33 /**
34 * @}
35 */
36 
37 /** @defgroup AES_Manager_Private_Defines Private Defines
38 * @{
39 */
40 #define ATOMIC_SECTION_BEGIN() uint32_t uwPRIMASK_Bit = __get_PRIMASK(); \
41 __disable_irq(); \
42   /* Must be called in the same or in a lower scope of ATOMIC_SECTION_BEGIN */
43 #define ATOMIC_SECTION_END() __set_PRIMASK(uwPRIMASK_Bit)
44 
45 /**
46 * @}
47 */
48 
49 __STATIC_INLINE uint8_t HW_AES_Start(void);
50 __STATIC_INLINE void LL_AES_StartManualEncription(BLUE_TypeDef *BLUEx);
51 __STATIC_INLINE uint32_t LL_AES_IsBusy(BLUE_TypeDef *BLUEx);
52 
53 /** @defgroup AES_Manager_Private_Variables Private Variables
54 * @{
55 */
56 /**
57 * @}
58 */
59 
60 /** @defgroup AES_Manager_External_Variables External Variables
61 * @{
62 */
63 /**
64 * @}
65 */
66 
67 /** @defgroup AES_Manager_Public_Functions Public Functions
68 * @{
69 */
70 
HW_AES_Init(void)71 HW_AES_ResultStatus HW_AES_Init(void)
72 {
73   if(!LL_APB2_GRP1_IsEnabledClock(LL_APB2_GRP1_PERIPH_MRBLE))
74   {
75     /* Peripheral reset */
76     LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_MRBLE);
77     LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_MRBLE);
78 
79     /* Enable MR_BLE's clock */
80     LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_MRBLE);
81   }
82   return HW_AES_SUCCESS;
83 }
84 
HW_AES_Deinit(void)85 HW_AES_ResultStatus HW_AES_Deinit(void)
86 {
87   return HW_AES_SUCCESS;
88 }
89 
HW_AES_Encrypt(const uint32_t * plainTextData,const uint32_t * key,uint32_t * encryptedData)90 HW_AES_ResultStatus HW_AES_Encrypt(const uint32_t *plainTextData, const uint32_t *key, uint32_t *encryptedData)
91 {
92   /* Counter to signal interruption by a higher priority routine. */
93   static volatile uint8_t start_cnt;
94   uint8_t priv_start_cnt;
95 
96   start_cnt++;
97 
98   do
99   {
100     priv_start_cnt = start_cnt;
101     /* Starting from this point, any call to HW_AES_Encrypt will change start_cnt. */
102 
103     /* Write the Key in the BLE register */
104     BLUE->MANAESKEY0REG = key[0];
105     BLUE->MANAESKEY1REG = key[1];
106     BLUE->MANAESKEY2REG = key[2];
107     BLUE->MANAESKEY3REG = key[3];
108 
109     /* Write the plain text data in the BLE register */
110     BLUE->MANAESCLEARTEXT0REG = plainTextData[0];
111     BLUE->MANAESCLEARTEXT1REG = plainTextData[1];
112     BLUE->MANAESCLEARTEXT2REG = plainTextData[2];
113     BLUE->MANAESCLEARTEXT3REG = plainTextData[3];
114 
115     HW_AES_Start();
116 
117     /* Read the plain text data in the BLE register */
118     encryptedData[0] = BLUE->MANAESCIPHERTEXT0REG;
119     encryptedData[1] = BLUE->MANAESCIPHERTEXT1REG;
120     encryptedData[2] = BLUE->MANAESCIPHERTEXT2REG;
121     encryptedData[3] = BLUE->MANAESCIPHERTEXT3REG;
122 
123   } while (priv_start_cnt != start_cnt);
124 
125   return HW_AES_SUCCESS;
126 }
127 
128 /**
129  * @brief Function to start the AES 128 encryption in blocking mode.
130  * @param  None
131  *
132  * @retval i
133  */
HW_AES_Start(void)134 __STATIC_INLINE uint8_t HW_AES_Start(void)
135 {
136     volatile uint8_t i = 100U;
137 
138     /* Start AES encryption */
139     LL_AES_StartManualEncription(BLUE);
140     do
141     {
142         i--;
143         if( !LL_AES_IsBusy(BLUE) )
144         {
145             break;
146         }
147     } while (i != 0U);
148 
149     return i;
150 }
151 
152 /**
153 * @}
154 */
155 
156 /** @defgroup AES_Manager_Private_Functions Private Functions
157 * @{
158 */
159 
160 /**
161   * @brief  AES manual encryption Start function.
162   * @rmtoll MANAESCMDREG          START         LL_AES_StartManualEncription
163   * @param  BLUEx BLUE Instance
164   * @retval None
165   */
LL_AES_StartManualEncription(BLUE_TypeDef * BLUEx)166 __STATIC_INLINE void LL_AES_StartManualEncription(BLUE_TypeDef *BLUEx)
167 {
168   SET_BIT(BLUEx->MANAESCMDREG, BLUE_MANAESCMDREG_START);
169 }
170 
171 /**
172   * @brief  Indicate if AES is busy
173   * @rmtoll MANAESSTATREG          BUSY         LL_AES_IsBusy
174   * @param  BLUEx BLUE Instance
175   * @retval State of bit (1 or 0).
176   */
LL_AES_IsBusy(BLUE_TypeDef * BLUEx)177 __STATIC_INLINE uint32_t LL_AES_IsBusy(BLUE_TypeDef *BLUEx)
178 {
179   return ((READ_BIT(BLUEx->MANAESSTATREG, BLUE_MANAESSTATREG_BUSY) == (BLUE_MANAESSTATREG_BUSY)) ? 1U : 0U);
180 }
181 
182 /**
183 * @}
184 */
185 
186 /**
187 * @}
188 */
189