1 /**
2   ******************************************************************************
3   * @file    stm32n6xx_hal_mce.h
4   * @author  MCD Application Team
5   * @brief   Header file of MCE HAL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2024 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 
19 /* Define to prevent recursive inclusion -------------------------------------*/
20 #ifndef STM32N6xx_HAL_MCE_H
21 #define STM32N6xx_HAL_MCE_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32n6xx_hal_def.h"
29 
30 /** @addtogroup STM32N6xx_HAL_Driver
31   * @{
32   */
33 
34 #if defined(MCE1)
35 /** @addtogroup MCE
36   * @{
37   */
38 typedef struct
39 {
40   uint32_t         ContextID;              /*!< MCE region context ID (can be null) */
41   /*!< This parameter is a value of @ref MCE_Context_Index. */
42 
43   uint32_t         StartAddress;           /*!< MCE region start address */
44 
45   uint32_t         EndAddress;             /*!< MCE region end address */
46 
47   uint32_t         Mode;                   /*!< Indicates the chaining mode used for encryption. */
48   /*!< This parameter is a value of @defgroup MCE_Ciphering_Algorithm */
49   uint32_t         AccessMode;             /*!< MCE region writes enabled or not */
50   /*!< This parameter is a value of @ref MCE_Region_Privilege. */
51 
52   uint32_t         PrivilegedAccess;       /*!< MCE region privileged access or not */
53   /*!< This parameter is a value of @ref MCE_Region_Privilege. */
54 
55 } MCE_RegionConfigTypeDef;
56 
57 typedef struct
58 {
59   uint32_t         Nonce[2];              /*!< MCE context nonce */
60 
61   uint32_t         Version;               /*!< 16-bit long MCE context version */
62 
63   uint32_t         *pKey;                  /*!< Pointer at the key used for encryption/decryption */
64 
65   uint32_t         KeySize;               /*!< This parameter can be MCE_AES_128 or MCE_AES_256 */
66 
67   uint32_t         Cipher_Mode;           /*!< Authorized cipher mode  */
68 
69 } MCE_AESConfigTypeDef;
70 
71 typedef struct
72 {
73   uint32_t          KeyType;             /*!< This parameter is a value of @ref MCE_KeyType. */
74 
75   uint32_t          *pKey;                   /*!< Pointer at the key used for encryption/decryption .*/
76 
77 } MCE_NoekeonConfigTypeDef;
78 
79 
80 /**
81   * @brief MCE states structure definition
82   */
83 typedef enum
84 {
85   HAL_MCE_STATE_RESET             = 0x00U,  /*!< MCE not yet initialized or disabled */
86   HAL_MCE_STATE_READY             = 0x01U,  /*!< MCE initialized and ready for use   */
87   HAL_MCE_STATE_BUSY              = 0x02U,  /*!< MCE internal processing is ongoing  */
88 } HAL_MCE_StateTypeDef;
89 
90 #if (USE_HAL_MCE_REGISTER_CALLBACKS == 1)
91 typedef struct __MCE_HandleTypeDef
92 #else
93 typedef struct
94 #endif /* USE_HAL_MCE_REGISTER_CALLBACKS */
95 {
96   MCE_TypeDef             *Instance;        /*!< MCE registers base address */
97 
98   HAL_MCE_StateTypeDef    State;            /*!< MCE state */
99 
100   HAL_LockTypeDef         Lock;             /*!< MCE Locking object */
101 
102   __IO uint32_t           ErrorCode;        /*!< MCE error code */
103 
104 #if (USE_HAL_MCE_REGISTER_CALLBACKS == 1)
105   void (* ErrorCallback)(struct __MCE_HandleTypeDef *hmce);     /*!< MCE error callback */
106 
107   void (* MspInitCallback)(struct __MCE_HandleTypeDef *hmce);   /*!< MCE Msp Init callback */
108 
109   void (* MspDeInitCallback)(struct __MCE_HandleTypeDef *hmce); /*!< MCE Msp DeInit callback */
110 #endif /* USE_HAL_MCE_REGISTER_CALLBACKS */
111 
112 } MCE_HandleTypeDef;
113 
114 #if (USE_HAL_MCE_REGISTER_CALLBACKS == 1)
115 /**
116   * @brief  HAL MCE Callback ID enumeration definition
117   */
118 typedef enum
119 {
120   HAL_MCE_ERROR_CB_ID       = 0x00U,  /*!< MCE error callback ID      */
121   HAL_MCE_MSPINIT_CB_ID     = 0x01U,  /*!< MCE Msp DeInit callback ID */
122   HAL_MCE_MSPDEINIT_CB_ID   = 0x02U   /*!< MCE Msp DeInit callback ID */
123 } HAL_MCE_CallbackIDTypeDef;
124 
125 /**
126   * @brief  HAL MCE Callback pointer definition
127   */
128 typedef  void (*pMCE_CallbackTypeDef)(MCE_HandleTypeDef *hmce); /*!< pointer to a MCE callback function */
129 
130 #endif /* USE_HAL_MCE_REGISTER_CALLBACKS */
131 
132 /* Exported constants --------------------------------------------------------*/
133 /** @defgroup MCE_Exported_Constants MCE Exported Constants
134   * @{
135   */
136 
137 /** @defgroup MCE_Error_Definition   MCE Error Definition
138   * @{
139   */
140 #define  HAL_MCE_ERROR_NONE                       ((uint32_t)0x00000000U)    /*!< No error                                                         */
141 #define  HAL_MCE_CONFIGURATION_ACCESS_ERROR       ((uint32_t)0x00000001U)    /*!< Configuration access error                                       */
142 #define  HAL_MCE_ILLEGAL_ACCESS_READ_PRIV_ERROR   ((uint32_t)0x00000002U)    /*!< Illegal privileged data read or instruction fetch access error   */
143 #define  HAL_MCE_ILLEGAL_ACCESS_READ_NPRIV_ERROR  ((uint32_t)0x00000004U)    /*!< Illegal unprivileged data read or instruction fetch access error */
144 #define  HAL_MCE_ILLEGAL_ACCESS_WRITE_PRIV_ERROR  ((uint32_t)0x00000008U)    /*!< Illegal privileged data write access error                       */
145 #define  HAL_MCE_ILLEGAL_ACCESS_WRITE_NPRIV_ERROR ((uint32_t)0x00000010U)    /*!< Illegal un privileged data write access error                    */
146 #define  HAL_MCE_MASTER_KEY_ERROR                 ((uint32_t)0x00000020U)    /*!< Master key error                                                 */
147 #define  HAL_MCE_FASTMASTER_KEY_ERROR             ((uint32_t)0x00000040U)    /*!< Fast master key error                                            */
148 #define  HAL_MCE_CONTEXT_KEY_ERROR                ((uint32_t)0x00000080U)    /*!< Context key error                                                */
149 #if (USE_HAL_MCE_REGISTER_CALLBACKS == 1)
150 #define HAL_MCE_ERROR_INVALID_CALLBACK            ((uint32_t)0x00000100U)    /*!< Invalid Callback error                                           */
151 #endif /* USE_HAL_MCE_REGISTER_CALLBACKS */
152 /**
153   * @}
154   */
155 
156 /** @defgroup MCE_Interrupts    MCE Interrupts
157   * @{
158   */
159 #define MCE_IT_ILLEGAL_ACCESS_ERROR        MCE_IAIER_IAEIE                   /*!< Illegal access error interrupt       */
160 /**
161   * @}
162   */
163 
164 /** @defgroup MCE_Illegal_Access_Flags    MCE Illegal Access Flags
165   * @{
166   */
167 #define MCE_ILLEGAL_ACCESS_READ_NPRIV      MCE_IASR_IAEF                      /*!< Illegal unprivileged data read/instruction fetch access flag   */
168 /**
169   * @}
170   */
171 
172 
173 /** @defgroup MCE_Regions_Index   MCE Regions Index
174   * @{
175   */
176 #define MCE_REGION1                       0U    /*!< MCE region 1 */
177 #define MCE_REGION2                       1U    /*!< MCE region 2 */
178 #define MCE_REGION3                       2U    /*!< MCE region 3 */
179 #define MCE_REGION4                       3U    /*!< MCE region 4 */
180 /**
181   * @}
182   */
183 
184 /** @defgroup MCE_Context_Index   MCE Context Index
185   * @{
186   */
187 #define MCE_NO_CONTEXT                    0U                /*!< MCE no context */
188 #define MCE_CONTEXT1                      MCE_REGCR_CTXID_0 /*!< MCE context 1  */
189 #define MCE_CONTEXT2                      MCE_REGCR_CTXID_1 /*!< MCE context 2  */
190 /**
191   * @}
192   */
193 
194 /** @defgroup MCE_Ciphering_Algorithm   MCE Ciphering Algorithm
195   * @{
196   */
197 #define MCE_NO_CIPHER                     0U               /*!< MCE no cipher         */
198 #define MCE_STREAM_CIPHER                 MCE_REGCR_ENC_0  /*!< MCE stream cipher     */
199 #define MCE_BLOCK_CIPHER                  MCE_REGCR_ENC_1  /*!< MCE block cipher      */
200 #define MCE_FASTBLOCK_CIPHER              MCE_REGCR_ENC    /*!< MCE fast block cipher */
201 /**
202   * @}
203   */
204 
205 
206 /** @defgroup MCE_Region_Privilege   MCE Region Privilege
207   * @{
208   */
209 #define MCE_REGION_READONLY               0U               /*!< Writes to region are ignored, reads are allowed */
210 #define MCE_REGION_READWRITE              MCE_ATTR_WREN    /*!< Region can be read and written                  */
211 /**
212   * @}
213   */
214 
215 /** @defgroup MCE_Configuration_Attributes   MCE Configuration Attributes
216   * @{
217   */
218 #define MCE_ATTRIBUTE_NPRIV               0U                /*!< Non-privileged access protection */
219 #define MCE_ATTRIBUTE_PRIV                MCE_PRIVCFGR_PRIV /*!< Privileged access protection     */
220 /**
221   * @}
222   */
223 
224 /** @defgroup MCE_Lock MCE Lock values
225   * @{
226   */
227 #define MCE_LOCK_OFF                      0U            /*!< No global lock set */
228 #define MCE_LOCK_ON                       MCE_CR_GLOCK  /*!< Global lock set    */
229 /**
230   * @}
231   */
232 
233 /** @defgroup MCE_Lock MCE Lock values
234   * @{
235   */
236 #define MCE_MASTERKEYS_LOCK_OFF           0U             /*!< No master keys lock set */
237 #define MCE_MASTERKEYS_LOCK_ON            MCE_CR_MKLOCK  /*!< Master keys lock set    */
238 /**
239   * @}
240   */
241 
242 /** @defgroup MCE_KeyType key type used for encryption, Master key or Fast Master key
243   * @{
244   */
245 #define MCE_USE_MASTERKEYS              0U             /*!< Master keys used for encryption         */
246 #define MCE_USE_FASTMASTERKEYS          1U             /*!< Fast Master keys used for encryption    */
247 /**
248   * @}
249   */
250 
251 
252 
253 /** @defgroup MCE_CipherSelection MCE Cipher Selection
254   * @{
255   */
256 #define MCE_AES_128                       MCE_CR_CIPHERSEL_0  /*!< AES-128 cipher selected for all encrypted regions */
257 #define MCE_AES_256                       MCE_CR_CIPHERSEL    /*!< AES-256 cipher selected for all encrypted regions */
258 #define MCE_NOEKEON                       MCE_CR_CIPHERSEL_1  /*!< Noekeon cipher selected for all encrypted regions */
259 /**
260   * @}
261   */
262 
263 /** @defgroup MCE_ContextMode MCE Context Mode
264   * @{
265   */
266 #define MCE_CONTEXT_STREAM_CIPHER      MCE_CCCFGR_MODE_0  /*!< Stream cipher is allowed with this cipher context     */
267 #define MCE_CONTEXT_BLOCK_CIPHER       MCE_CCCFGR_MODE_1  /*!< Block cipher is allowed with this cipher context      */
268 #define MCE_CONTEXT_FASTBLOCK_CIPHER   MCE_CCCFGR_MODE    /*!< Fast block cipher is allowed with this cipher context */
269 /**
270   * @}
271   */
272 
273 
274 /**
275   * @}
276   */
277 
278 /* Exported macro ------------------------------------------------------------*/
279 /** @defgroup MCE_Exported_Macros MCE Exported Macros
280   * @{
281   */
282 
283 /** @brief  Reset MCE handle state.
284   * @param  __HANDLE__ pointer to an MCE_HandleTypeDef structure that contains
285   *         the configuration information for MCE module
286   * @retval None
287   */
288 #if (USE_HAL_MCE_REGISTER_CALLBACKS == 1)
289 #define __HAL_MCE_RESET_HANDLE_STATE(__HANDLE__)                            \
290   do{                                                                       \
291     (__HANDLE__)->State = HAL_MCE_STATE_RESET;                              \
292     (__HANDLE__)->MspInitCallback = NULL;                                   \
293     (__HANDLE__)->MspDeInitCallback = NULL;                                 \
294   } while(0)
295 #else
296 #define __HAL_MCE_RESET_HANDLE_STATE(__HANDLE__)                            \
297   ((__HANDLE__)->State = HAL_MCE_STATE_RESET)
298 #endif /* USE_HAL_MCE_REGISTER_CALLBACKS */
299 
300 /**
301   * @brief  Enable MCE peripheral interrupts combination
302   * @param  __HANDLE__ pointer to an MCE_HandleTypeDef structure that contains
303   *         the configuration information for MCE module
304   * @param  __INTERRUPT__ mask on enabled interrupts
305   *          This parameter can be one of the following values:
306   *            @arg @ref MCE_IT_ILLEGAL_ACCESS_ERROR        MCE illegal access error interrupt
307   * @retval None
308   */
309 #define __HAL_MCE_ENABLE_IT(__HANDLE__, __INTERRUPT__)  SET_BIT(((__HANDLE__)->Instance->IAIER), (__INTERRUPT__))
310 
311 /**
312   * @brief  Disable MCE peripheral interrupts combination
313   * @param  __HANDLE__ pointer to an MCE_HandleTypeDef structure that contains
314   *         the configuration information for MCE module
315   * @param  __INTERRUPT__ mask on disabled interrupts
316   *          This parameter can be one of the following values:
317   *            @arg @ref MCE_IT_ILLEGAL_ACCESS_ERROR        MCE illegal access error interrupt
318   * @retval None
319   */
320 #define __HAL_MCE_DISABLE_IT(__HANDLE__, __INTERRUPT__)  CLEAR_BIT(((__HANDLE__)->Instance->IAIER), (__INTERRUPT__))
321 
322 
323 /**
324   * @brief  Get MCE peripheral access error flag
325   * @param  __HANDLE__ pointer to an MCE_HandleTypeDef structure that contains
326   *         the configuration information for MCE module
327   * @param  __FLAG__ access error flag to check
328   *          This parameter can be one of the following values:
329   *            @arg @ref MCE_ILLEGAL_ACCESS_READ_NPRIV                 MCE illegal access error flag
330   * @retval 0 (not set) or 1 (set)
331   */
332 #define __HAL_MCE_GET_FLAG(__HANDLE__, __FLAG__)  READ_BIT((__HANDLE__)->Instance->IASR, MCE_IASR_IAEF)
333 
334 
335 
336 /**
337   * @brief  Clear MCE peripheral illegal/configuration access flag
338   * @param  __HANDLE__ pointer to an MCE_HandleTypeDef structure that contains
339   *         the configuration information for MCE module
340   * @param  __FLAG__ illegal access flag to check
341   *          This parameter can be one of the following values:
342   *            @arg @ref MCE_ILLEGAL_ACCESS_READ_NPRIV                 MCE illegal access error flag
343   * @retval 0 (not set) or 1 (set)
344   */
345 #define __HAL_MCE_CLEAR_FLAG(__HANDLE__, __FLAG__)  WRITE_REG(((__HANDLE__)->Instance->IACR), MCE_IACR_IAEF)
346 
347 
348 /**
349   * @}
350   */
351 
352 
353 /* Exported functions ---------------------------------------------------------*/
354 
355 
356 HAL_StatusTypeDef HAL_MCE_Init(MCE_HandleTypeDef *hmce);
357 HAL_StatusTypeDef HAL_MCE_DeInit(MCE_HandleTypeDef *hmce);
358 void HAL_MCE_MspInit(MCE_HandleTypeDef *hmce);
359 void HAL_MCE_MspDeInit(MCE_HandleTypeDef *hmce);
360 
361 
362 HAL_StatusTypeDef HAL_MCE_ConfigNoekeon(MCE_HandleTypeDef *hmce, const MCE_NoekeonConfigTypeDef *pConfig);
363 HAL_StatusTypeDef HAL_MCE_ConfigAESContext(MCE_HandleTypeDef *hmce, const MCE_AESConfigTypeDef  *AESConfig,
364                                            uint32_t ContextIndex);
365 HAL_StatusTypeDef HAL_MCE_ConfigRegion(MCE_HandleTypeDef *hmce, uint32_t RegionIndex,
366                                        const MCE_RegionConfigTypeDef *pConfig);
367 HAL_StatusTypeDef HAL_MCE_SetRegionAESContext(MCE_HandleTypeDef *hmce, uint32_t ContextIndex, uint32_t RegionIndex);
368 HAL_StatusTypeDef HAL_MCE_EnableAESContext(MCE_HandleTypeDef *hmce, uint32_t ContextIndex);
369 HAL_StatusTypeDef HAL_MCE_DisableAESContext(MCE_HandleTypeDef *hmce, uint32_t ContextIndex);
370 HAL_StatusTypeDef HAL_MCE_EnableRegion(MCE_HandleTypeDef *hmce, uint32_t RegionIndex);
371 HAL_StatusTypeDef HAL_MCE_DisableRegion(MCE_HandleTypeDef *hmce, uint32_t RegionIndex);
372 HAL_StatusTypeDef HAL_MCE_LockGlobalConfig(MCE_HandleTypeDef *hmce);
373 HAL_StatusTypeDef HAL_MCE_LockAESContextConfig(MCE_HandleTypeDef *hmce, uint32_t ContextIndex);
374 HAL_StatusTypeDef HAL_MCE_LockAESContextKey(MCE_HandleTypeDef *hmce, uint32_t ContextIndex);
375 HAL_StatusTypeDef HAL_MCE_LockNoekeonMasterKeys(MCE_HandleTypeDef *hmce);
376 HAL_StatusTypeDef HAL_MCE_LockNoekeonFastKeys(MCE_HandleTypeDef *hmce);
377 HAL_StatusTypeDef HAL_MCE_GetAESContextCRCKey(const MCE_HandleTypeDef *hmce, uint32_t *pCRCKey, uint32_t ContextIndex);
378 
379 
380 void HAL_MCE_IRQHandler(MCE_HandleTypeDef *hmce);
381 void HAL_MCE_ErrorCallback(MCE_HandleTypeDef *hmce);
382 
383 HAL_MCE_StateTypeDef HAL_MCE_GetState(MCE_HandleTypeDef const *hmce);
384 uint32_t HAL_MCE_GetError(MCE_HandleTypeDef const *hmce);
385 uint32_t HAL_MCE_KeyCRCComputation(const uint32_t *pKey);
386 
387 #define IS_MCE_INTERRUPT(__INTERRUPT__) ((__INTERRUPT__) == MCE_IT_ILLEGAL_ACCESS_ERROR)
388 
389 /**
390   * @brief Verify the MCE region index.
391   * @param __INDEX__ MCE region index
392   * @retval SET (__INDEX__ is valid) or RESET (__INDEX__ is invalid)
393   */
394 #define IS_MCE_REGIONINDEX(__INDEX__) (((__INDEX__) == MCE_REGION1)     || \
395                                        ((__INDEX__) == MCE_REGION2)     || \
396                                        ((__INDEX__) == MCE_REGION3)     || \
397                                        ((__INDEX__) == MCE_REGION4))
398 
399 /**
400   * @brief Verify the MCE configuration attributes.
401   * @param __ATTRIBUTE__ MCE region index
402   * @retval SET (__ATTRIBUTE__ is valid) or RESET (__ATTRIBUTE__ is invalid)
403   */
404 #define IS_MCE_ATTRIBUTE(__ATTRIBUTE__) (((__ATTRIBUTE__) == MCE_ATTRIBUTE_PRIV)   || \
405                                          ((__ATTRIBUTE__) == MCE_ATTRIBUTE_NPRIV))
406 
407 /**
408   * @brief Verify the MCE region privilege attribute.
409   * @param __PRIVILEGED__ MCE region privilege attribute
410   * @retval SET (__PRIVILEGED__ is valid) or RESET (__PRIVILEGED__ is invalid)
411   */
412 #define IS_MCE_REGIONPRIVILEGED(__PRIVILEGED__) (((__PRIVILEGED__) == MCE_REGION_NPRIV)   || \
413                                                  ((__PRIVILEGED__) == MCE_REGION_PRIV))
414 
415 /**
416   * @brief Verify the MCE region write enable attribute.
417   * @param __WRITE__ MCE region write enable attribute
418   * @retval SET (__WRITE__ is valid) or RESET (__WRITE__ is invalid)
419   */
420 #define IS_MCE_WRITE(__WRITE__) (((__WRITE__) == MCE_REGION_READONLY)   || \
421                                  ((__WRITE__) == MCE_REGION_READWRITE))
422 
423 /**
424   * @brief Verify the MCE region context.
425   * @param __INSTANCE__ MCE instance
426   * @param __CONTEXT__ MCE region context
427   * @retval SET (__CONTEXT__ is valid) or RESET (__CONTEXT__ is invalid)
428   */
429 
430 #define IS_MCE_CONTEXT(__INSTANCE__, __CONTEXT__) (((__INSTANCE__) == (MCE1)) ?           \
431                                                    (((__CONTEXT__) == MCE_NO_CONTEXT)   || \
432                                                     ((__CONTEXT__) == MCE_CONTEXT1)     || \
433                                                     ((__CONTEXT__) == MCE_CONTEXT2))     : \
434                                                    (((__CONTEXT__) == MCE_CONTEXT1)     || \
435                                                     ((__CONTEXT__) == MCE_CONTEXT2)     || \
436                                                     ((__CONTEXT__) == MCE_NO_CONTEXT)))
437 /**
438   * @brief Verify the MCE region algorithm.
439   * @param __INSTANCE__ MCE instance
440   * @param __ALGO__ MCE region context
441   * @retval SET (__ALGO__ is valid) or RESET (__ALGO__ is invalid)
442   */
443 #define IS_MCE_ALGORITHM(__INSTANCE__, __ALGO__) (((__INSTANCE__) == (MCE1)) ?             \
444                                                   (((__ALGO__) == MCE_NO_CIPHER)         || \
445                                                    ((__ALGO__) == MCE_STREAM_CIPHER)     || \
446                                                    ((__ALGO__) == MCE_BLOCK_CIPHER)      || \
447                                                    ((__ALGO__) == MCE_FASTBLOCK_CIPHER))  : \
448                                                   (((__ALGO__) == MCE_NO_CIPHER)         || \
449                                                    ((__ALGO__) == MCE_BLOCK_CIPHER)      || \
450                                                    ((__ALGO__) == MCE_STREAM_CIPHER)     || \
451                                                    ((__ALGO__) == MCE_FASTBLOCK_CIPHER)))
452 
453 #endif /* MCE1 */
454 
455 /**
456   * @}
457   */
458 
459 /**
460   * @}
461   */
462 #ifdef __cplusplus
463 }
464 #endif
465 
466 #endif /* STM32N6xx_HAL_MCE_H */
467 
468