1 /**
2   ******************************************************************************
3   * @file    stm32l5xx_hal_otfdec.c
4   * @author  MCD Application Team
5   * @brief   OTFDEC HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the On-The-Fly Decryption/Encryption (OTFDEC)
8   *          peripheral:
9   *           + Initialization and de-initialization functions
10   *           + Region setting/enable functions
11   *           + Peripheral State functions
12   *
13   @verbatim
14   ==============================================================================
15                      ##### How to use this driver #####
16   ==============================================================================
17  [..]
18     The OTFDEC HAL driver can be used as follows:
19 
20     (#) Declare an OTFDEC_HandleTypeDef handle structure (eg. OTFDEC_HandleTypeDef hotfdec).
21 
22     (#) Initialize the OTFDEC low level resources by implementing the HAL_OTFDEC_MspInit() API:
23         (++) Enable the OTFDEC interface clock.
24         (++) NVIC configuration if interrupts are used
25             (+++) Configure the OTFDEC interrupt priority.
26             (+++) Enable the NVIC OTFDEC IRQ handle.
27 
28     (#) Initialize the OTFDEC peripheral by calling the HAL_OTFDEC_Init() API.
29 
30     (#) In the case of encryption, enable ciphering mode for the peripheral
31 
32     (#) For each region,
33 
34         (++) Configure the region deciphering mode by calling the HAL_OTFDEC_RegionSetMode() API.
35 
36         (++) Write the region Key by calling the HAL_OTFDEC_RegionSetKey() API. If desired,
37         read the key CRC by calling HAL_OTFDEC_RegionGetKeyCRC() API and compare the
38         result with the theoretically expected CRC.
39 
40         (++) Initialize the OTFDEC region config structure with the Nonce, protected
41         region start and end addresses and firmware version, and wrap-up the region
42         configuration by calling HAL_OTFDEC_RegionConfig() API.
43 
44     (#) At this point, the OTFDEC region configuration is done and the deciphering
45         or enciphering enabled. The region can be deciphered on the fly after
46         having made sure the OctoSPI is configured in memory-mapped mode or data can
47         be enciphered by calling HAL_OTFDEC_Cipher() API.
48 
49     [..]
50     (@) Warning: the OTFDEC en/deciphering is based on a different endianness compared
51         to the AES-CTR as implemented in the AES peripheral. E.g., if the OTFEC
52         resorts to the Key (B0, B1, B2, B3) where Bi are 32-bit longwords and B0
53         is the Least Significant Word, the AES has to be configured with the Key
54         (B3, B2, B1, B0) to report the same result (with the same swapping applied
55         to the Initialization Vector).
56 
57     [..]
58 
59     *** Callback registration ***
60     =============================================
61     [..]
62 
63      The compilation flag USE_HAL_OTFDEC_REGISTER_CALLBACKS, when set to 1,
64      allows the user to configure dynamically the driver callbacks.
65      Use Functions @ref HAL_OTFDEC_RegisterCallback()
66      to register an interrupt callback.
67     [..]
68 
69      Function @ref HAL_OTFDEC_RegisterCallback() allows to register following callbacks:
70        (+) ErrorCallback                  : OTFDEC error callback
71        (+) MspInitCallback                : OTFDEC Msp Init callback
72        (+) MspDeInitCallback              : OTFDEC Msp DeInit callback
73      This function takes as parameters the HAL peripheral handle, the Callback ID
74      and a pointer to the user callback function.
75     [..]
76 
77      Use function @ref HAL_OTFDEC_UnRegisterCallback to reset a callback to the default
78      weak function.
79     [..]
80 
81      @ref HAL_OTFDEC_UnRegisterCallback takes as parameters the HAL peripheral handle,
82      and the Callback ID.
83      This function allows to reset following callbacks:
84        (+) ErrorCallback                  : OTFDEC error callback
85        (+) MspInitCallback                : OTFDEC Msp Init callback
86        (+) MspDeInitCallback              : OTFDEC Msp DeInit callback
87      [..]
88 
89      By default, after the @ref HAL_OTFDEC_Init() and when the state is @ref HAL_OTFDEC_STATE_RESET
90      all callbacks are set to the corresponding weak functions:
91      example @ref HAL_OTFDEC_ErrorCallback().
92      Exception done for MspInit and MspDeInit functions that are
93      reset to the legacy weak functions in the @ref HAL_OTFDEC_Init()/ @ref HAL_OTFDEC_DeInit() only when
94      these callbacks are null (not registered beforehand).
95     [..]
96 
97      If MspInit or MspDeInit are not null, the @ref HAL_OTFDEC_Init()/ @ref HAL_OTFDEC_DeInit()
98      keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
99      [..]
100 
101      Callbacks can be registered/unregistered in @ref HAL_OTFDEC_STATE_READY state only.
102      Exception done MspInit/MspDeInit functions that can be registered/unregistered
103      in @ref HAL_OTFDEC_STATE_READY or @ref HAL_OTFDEC_STATE_RESET state,
104      thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
105     [..]
106 
107      Then, the user first registers the MspInit/MspDeInit user callbacks
108      using @ref HAL_OTFDEC_RegisterCallback() before calling @ref HAL_OTFDEC_DeInit()
109      or @ref HAL_OTFDEC_Init() function.
110      [..]
111 
112      When the compilation flag USE_HAL_OTFDEC_REGISTER_CALLBACKS is set to 0 or
113      not defined, the callback registration feature is not available and all callbacks
114      are set to the corresponding weak functions.
115 
116   @endverbatim
117   ******************************************************************************
118   * @attention
119   *
120   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
121   * All rights reserved.</center></h2>
122   *
123   * This software component is licensed by ST under BSD 3-Clause license,
124   * the "License"; You may not use this file except in compliance with the
125   * License. You may obtain a copy of the License at:
126   *                        opensource.org/licenses/BSD-3-Clause
127   *
128   ******************************************************************************
129   */
130 
131 /* Includes ------------------------------------------------------------------*/
132 #include "stm32l5xx_hal.h"
133 
134 /** @addtogroup STM32L5xx_HAL_Driver
135   * @{
136   */
137 
138 /** @defgroup OTFDEC OTFDEC
139   * @brief OTFDEC HAL module driver.
140   * @{
141   */
142 
143 
144 #ifdef HAL_OTFDEC_MODULE_ENABLED
145 
146 #if defined(OTFDEC1)
147 
148 /* Private typedef -----------------------------------------------------------*/
149 /* Private define ------------------------------------------------------------*/
150 /* Private macro -------------------------------------------------------------*/
151 /* Private variables ---------------------------------------------------------*/
152 /* Private function prototypes -----------------------------------------------*/
153 /* Private functions ---------------------------------------------------------*/
154 
155 
156 
157  /* Exported functions --------------------------------------------------------*/
158 /** @addtogroup OTFDEC_Exported_Functions
159   * @{
160   */
161 
162 /** @defgroup OTFDEC_Exported_Functions_Group1 Initialization and de-initialization functions
163  *  @brief    Initialization and Configuration functions.
164  *
165 @verbatim
166   ==============================================================================
167               ##### Initialization and de-initialization functions #####
168   ==============================================================================
169 
170 @endverbatim
171   * @{
172   */
173 
174 /**
175   * @brief  Initialize the OTFDEC peripheral and create the associated handle.
176   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
177   *         the configuration information for OTFDEC module
178   * @retval HAL status
179   */
HAL_OTFDEC_Init(OTFDEC_HandleTypeDef * hotfdec)180 HAL_StatusTypeDef HAL_OTFDEC_Init(OTFDEC_HandleTypeDef *hotfdec)
181 {
182   /* Check the OTFDEC handle allocation */
183   if(hotfdec == NULL)
184   {
185     return HAL_ERROR;
186   }
187 
188   /* Check the parameters */
189   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
190 
191   if(hotfdec->State == HAL_OTFDEC_STATE_RESET)
192   {
193     /* Allocate lock resource and initialize it */
194     __HAL_UNLOCK(hotfdec);
195 
196 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
197     /* Init the OTFDEC Callback settings */
198     hotfdec->ErrorCallback = HAL_OTFDEC_ErrorCallback; /* Legacy weak callback */
199 
200     if (hotfdec->MspInitCallback == NULL)
201     {
202       hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
203     }
204 
205     /* Init the low level hardware */
206     hotfdec->MspInitCallback(hotfdec);
207 #else
208     /* Init the low level hardware */
209     HAL_OTFDEC_MspInit(hotfdec);
210 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
211   }
212 
213   /* Change the OTFDEC state */
214   hotfdec->State = HAL_OTFDEC_STATE_READY;
215 
216   /* Return function status */
217   return HAL_OK;
218 }
219 
220 /**
221   * @brief  DeInitialize the OTFDEC peripheral.
222   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
223   *         the configuration information for OTFDEC module
224   * @retval HAL status
225   */
HAL_OTFDEC_DeInit(OTFDEC_HandleTypeDef * hotfdec)226 HAL_StatusTypeDef HAL_OTFDEC_DeInit(OTFDEC_HandleTypeDef *hotfdec)
227 {
228   /* Check the OTFDEC handle allocation */
229   if(hotfdec == NULL)
230   {
231     return HAL_ERROR;
232   }
233 
234   /* Check the parameters */
235   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
236 
237   /* Change the OTFDEC state */
238   hotfdec->State = HAL_OTFDEC_STATE_BUSY;
239 
240 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
241   if (hotfdec->MspDeInitCallback == NULL)
242   {
243     hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
244   }
245 
246   /* DeInit the low level hardware: CLOCK, NVIC */
247   hotfdec->MspDeInitCallback(hotfdec);
248 #else
249   /* DeInit the low level hardware: CLOCK, NVIC */
250   HAL_OTFDEC_MspDeInit(hotfdec);
251 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
252 
253   /* Change the OTFDEC state */
254   hotfdec->State = HAL_OTFDEC_STATE_RESET;
255 
256   /* Reset OTFDEC error status */
257   hotfdec->ErrorCode = HAL_OTFDEC_ERROR_NONE;
258 
259   /* Release Lock */
260   __HAL_UNLOCK(hotfdec);
261 
262   /* Return function status */
263   return HAL_OK;
264 }
265 
266 /**
267   * @brief  Initialize the OTFDEC MSP.
268   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
269   *         the configuration information for OTFDEC module
270   * @retval None
271   */
HAL_OTFDEC_MspInit(OTFDEC_HandleTypeDef * hotfdec)272 __weak void HAL_OTFDEC_MspInit(OTFDEC_HandleTypeDef *hotfdec)
273 {
274   /* Prevent unused argument(s) compilation warning */
275   UNUSED(hotfdec);
276 
277   /* NOTE : This function should not be modified; when the callback is needed,
278             the HAL_OTFDEC_MspInit can be implemented in the user file.
279    */
280 }
281 
282 /**
283   * @brief  DeInitialize OTFDEC MSP.
284   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
285   *         the configuration information for OTFDEC module
286   * @retval None
287   */
HAL_OTFDEC_MspDeInit(OTFDEC_HandleTypeDef * hotfdec)288 __weak void HAL_OTFDEC_MspDeInit(OTFDEC_HandleTypeDef *hotfdec)
289 {
290   /* Prevent unused argument(s) compilation warning */
291   UNUSED(hotfdec);
292 
293   /* NOTE : This function should not be modified; when the callback is needed,
294             the HAL_OTFDEC_MspDeInit can be implemented in the user file.
295    */
296 }
297 
298 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
299 /**
300   * @brief  Register a User OTFDEC Callback
301   *         To be used instead of the weak predefined callback
302   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
303   *         the configuration information for OTFDEC module
304   * @param  CallbackID ID of the callback to be registered
305   *         This parameter can be one of the following values:
306   *          @arg @ref HAL_OTFDEC_ERROR_CB_ID           OTFDEC error callback ID
307   *          @arg @ref HAL_OTFDEC_MSPINIT_CB_ID         MspInit callback ID
308   *          @arg @ref HAL_OTFDEC_MSPDEINIT_CB_ID       MspDeInit callback ID
309   * @param  pCallback pointer to the Callback function
310   * @retval HAL status
311   */
HAL_OTFDEC_RegisterCallback(OTFDEC_HandleTypeDef * hotfdec,HAL_OTFDEC_CallbackIDTypeDef CallbackID,pOTFDEC_CallbackTypeDef pCallback)312 HAL_StatusTypeDef HAL_OTFDEC_RegisterCallback(OTFDEC_HandleTypeDef *hotfdec, HAL_OTFDEC_CallbackIDTypeDef CallbackID, pOTFDEC_CallbackTypeDef pCallback)
313 {
314   HAL_StatusTypeDef status = HAL_OK;
315 
316   if (pCallback == NULL)
317   {
318     /* Update the error code */
319     hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
320 
321     return HAL_ERROR;
322   }
323 
324   if (hotfdec->State == HAL_OTFDEC_STATE_READY)
325   {
326     switch (CallbackID)
327     {
328       case HAL_OTFDEC_ERROR_CB_ID :
329         hotfdec->ErrorCallback = pCallback;
330         break;
331 
332       case HAL_OTFDEC_MSPINIT_CB_ID :
333         hotfdec->MspInitCallback = pCallback;
334         break;
335 
336       case HAL_OTFDEC_MSPDEINIT_CB_ID :
337         hotfdec->MspDeInitCallback = pCallback;
338         break;
339 
340       default :
341         /* Update the error code */
342         hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
343 
344         /* Return error status */
345         status = HAL_ERROR;
346         break;
347     }
348   }
349   else if (HAL_OTFDEC_STATE_RESET == hotfdec->State)
350   {
351     switch (CallbackID)
352     {
353       case HAL_OTFDEC_MSPINIT_CB_ID :
354         hotfdec->MspInitCallback = pCallback;
355         break;
356 
357       case HAL_OTFDEC_MSPDEINIT_CB_ID :
358         hotfdec->MspDeInitCallback = pCallback;
359         break;
360 
361       default :
362         /* Update the error code */
363         hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
364 
365         /* Return error status */
366         status = HAL_ERROR;
367         break;
368     }
369   }
370   else
371   {
372     /* Update the error code */
373     hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
374 
375     /* Return error status */
376     status =  HAL_ERROR;
377   }
378 
379   return status;
380 }
381 
382 /**
383   * @brief  Unregister a OTFDEC Callback
384   *         OTFDEC callback is redirected to the weak predefined callback
385   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
386   *         the configuration information for OTFDEC module
387   * @param  CallbackID ID of the callback to be registered
388   *         This parameter can be one of the following values:
389   *          @arg @ref HAL_OTFDEC_ERROR_CB_ID           OTFDEC error callback ID
390   *          @arg @ref HAL_OTFDEC_MSPINIT_CB_ID         MspInit callback ID
391   *          @arg @ref HAL_OTFDEC_MSPDEINIT_CB_ID       MspDeInit callback ID
392   * @retval HAL status
393   */
HAL_OTFDEC_UnRegisterCallback(OTFDEC_HandleTypeDef * hotfdec,HAL_OTFDEC_CallbackIDTypeDef CallbackID)394 HAL_StatusTypeDef HAL_OTFDEC_UnRegisterCallback(OTFDEC_HandleTypeDef *hotfdec, HAL_OTFDEC_CallbackIDTypeDef CallbackID)
395 {
396   HAL_StatusTypeDef status = HAL_OK;
397 
398   if (hotfdec->State == HAL_OTFDEC_STATE_READY)
399   {
400     switch (CallbackID)
401     {
402       case HAL_OTFDEC_ERROR_CB_ID :
403         hotfdec->ErrorCallback = HAL_OTFDEC_ErrorCallback;
404         break;
405 
406       case HAL_OTFDEC_MSPINIT_CB_ID :
407         hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
408         break;
409 
410       case HAL_OTFDEC_MSPDEINIT_CB_ID :
411         hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
412         break;
413 
414       default :
415         /* Update the error code */
416         hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
417 
418         /* Return error status */
419         status =  HAL_ERROR;
420         break;
421     }
422   }
423   else if (HAL_OTFDEC_STATE_RESET == hotfdec->State)
424   {
425     switch (CallbackID)
426     {
427       case HAL_OTFDEC_MSPINIT_CB_ID :
428         hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
429         break;
430 
431       case HAL_OTFDEC_MSPDEINIT_CB_ID :
432         hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
433         break;
434 
435       default :
436         /* Update the error code */
437         hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
438 
439         /* Return error status */
440         status =  HAL_ERROR;
441         break;
442     }
443   }
444   else
445   {
446     /* Update the error code */
447     hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
448 
449     /* Return error status */
450     status =  HAL_ERROR;
451   }
452 
453   return status;
454 }
455 
456 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
457 
458 /**
459   * @}
460   */
461 
462 /** @defgroup OTFDEC_Exported_Functions_Group2  OTFDEC IRQ handler management
463  *  @brief   OTFDEC IRQ handler.
464  *
465 @verbatim
466   ==============================================================================
467                 ##### OTFDEC IRQ handler management #####
468   ==============================================================================
469 [..]  This section provides OTFDEC IRQ handler function.
470 
471 @endverbatim
472   * @{
473   */
474 
475 /**
476   * @brief  Handle OTFDEC interrupt request.
477   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
478   *         the configuration information for OTFDEC module
479   * @retval None
480   */
HAL_OTFDEC_IRQHandler(OTFDEC_HandleTypeDef * hotfdec)481 void HAL_OTFDEC_IRQHandler(OTFDEC_HandleTypeDef *hotfdec)
482 {
483   uint32_t isr_reg;
484 
485   isr_reg = READ_REG(hotfdec->Instance->ISR);
486   if ((isr_reg & OTFDEC_ISR_SEIF) == OTFDEC_ISR_SEIF)
487   {
488     SET_BIT( hotfdec->Instance->ICR, OTFDEC_ICR_SEIF );
489     hotfdec->ErrorCode |= HAL_OTFDEC_SECURITY_ERROR;
490   }
491   if ((isr_reg & OTFDEC_ISR_XONEIF) == OTFDEC_ISR_XONEIF)
492   {
493     SET_BIT( hotfdec->Instance->ICR, OTFDEC_ICR_XONEIF );
494     hotfdec->ErrorCode |= HAL_OTFDEC_EXECUTE_ERROR;
495   }
496   if ((isr_reg & OTFDEC_ISR_KEIF) == OTFDEC_ISR_KEIF)
497   {
498     SET_BIT( hotfdec->Instance->ICR, OTFDEC_ICR_KEIF );
499     hotfdec->ErrorCode |= HAL_OTFDEC_KEY_ERROR;
500   }
501 
502 #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
503   hotfdec->ErrorCallback(hotfdec);
504 #else
505   HAL_OTFDEC_ErrorCallback(hotfdec);
506 #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
507 }
508 
509 /**
510   * @brief OTFDEC error callback.
511   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
512   *         the configuration information for OTFDEC module
513   * @retval None
514   */
HAL_OTFDEC_ErrorCallback(OTFDEC_HandleTypeDef * hotfdec)515 __weak void HAL_OTFDEC_ErrorCallback(OTFDEC_HandleTypeDef *hotfdec)
516 {
517   /* Prevent unused argument(s) compilation warning */
518   UNUSED(hotfdec);
519 
520   /* NOTE : This function should not be modified; when the callback is needed,
521             the HAL_OTFDEC_ErrorCallback can be implemented in the user file.
522    */
523 }
524 
525 /**
526   * @}
527   */
528 
529 
530 
531 
532 /** @defgroup OTFDEC_Exported_Functions_Group3 Peripheral Control functions
533  *  @brief   Peripheral control functions.
534  *
535 @verbatim
536   ==============================================================================
537                       ##### Peripheral Control functions #####
538   ==============================================================================
539     [..]
540     This subsection permits to configure the OTFDEC peripheral
541 
542 @endverbatim
543   * @{
544   */
545 
546 /**
547   * @brief  Lock region keys.
548   * @note   Writes to this region KEYRx registers are ignored until next OTFDEC reset.
549   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
550   *         the configuration information for OTFDEC module
551   * @param  RegionIndex index of region the keys of which are locked
552   * @retval HAL state
553   */
HAL_OTFDEC_RegionKeyLock(OTFDEC_HandleTypeDef * hotfdec,uint32_t RegionIndex)554 HAL_StatusTypeDef HAL_OTFDEC_RegionKeyLock(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
555 {
556   OTFDEC_Region_TypeDef * region;
557   uint32_t address;
558 
559   /* Check the parameters */
560   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
561   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
562 
563   /* Take Lock */
564   __HAL_LOCK(hotfdec);
565 
566   address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
567   region = (OTFDEC_Region_TypeDef *)address;
568 
569   SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_KEYLOCK );
570 
571   /* Release Lock */
572   __HAL_UNLOCK(hotfdec);
573 
574   /* Status is okay */
575   return HAL_OK;
576 }
577 
578 /**
579   * @brief  Set region keys.
580   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
581   *         the configuration information for OTFDEC module
582   * @param  RegionIndex index of region the keys of which are set
583   * @param  pKey pointer at set of keys
584   * @note   The API reads the key CRC computed by the peripheral and compares it with thzt
585   *         theoretically expected. An error is reported if they are different.
586   * @retval HAL state
587   */
HAL_OTFDEC_RegionSetKey(OTFDEC_HandleTypeDef * hotfdec,uint32_t RegionIndex,uint32_t * pKey)588 HAL_StatusTypeDef HAL_OTFDEC_RegionSetKey(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, uint32_t *pKey)
589 {
590   OTFDEC_Region_TypeDef * region;
591   uint32_t address;
592 
593   /* Check the parameters */
594   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
595   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
596 
597   if (pKey == NULL)
598   {
599     return HAL_ERROR;
600   }
601   else
602   {
603     /* Take Lock */
604     __HAL_LOCK(hotfdec);
605 
606     address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
607     region = (OTFDEC_Region_TypeDef *)address;
608 
609     /* Set Key */
610     WRITE_REG( region->REG_KEYR0, pKey[0]);
611 
612     __DSB();
613     __ISB();
614 
615     WRITE_REG( region->REG_KEYR1, pKey[1]);
616 
617     __DSB();
618     __ISB();
619 
620     WRITE_REG( region->REG_KEYR2, pKey[2]);
621 
622     __DSB();
623     __ISB();
624 
625     WRITE_REG( region->REG_KEYR3, pKey[3]);
626 
627     /* Compute theoretically expected CRC and compare it with that reported by the peripheral */
628     if (HAL_OTFDEC_KeyCRCComputation(pKey) != HAL_OTFDEC_RegionGetKeyCRC(hotfdec, RegionIndex))
629     {
630       /* Release Lock */
631       __HAL_UNLOCK(hotfdec);
632 
633       /* Status is okay */
634       return HAL_ERROR;
635     }
636 
637     /* Release Lock */
638     __HAL_UNLOCK(hotfdec);
639 
640     /* Status is okay */
641     return HAL_OK;
642   }
643 }
644 
645 /**
646   * @brief  Set region mode.
647   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
648   *         the configuration information for OTFDEC module
649   * @param  RegionIndex index of region the mode of which is set
650   * @param  mode This parameter can be only:
651   *           @arg @ref OTFDEC_REG_MODE_INSTRUCTION_OR_DATA_ACCESSES          All read accesses are decrypted (instruction or data)
652   *           @arg @ref OTFDEC_REG_MODE_INSTRUCTION_ACCESSES_ONLY_WITH_CIPHER Only instruction accesses are decrypted with proprietary cipher activated
653   * @retval HAL state
654   */
HAL_OTFDEC_RegionSetMode(OTFDEC_HandleTypeDef * hotfdec,uint32_t RegionIndex,uint32_t mode)655 HAL_StatusTypeDef HAL_OTFDEC_RegionSetMode(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, uint32_t mode)
656 {
657   OTFDEC_Region_TypeDef * region;
658   uint32_t address;
659 
660   /* Check the parameters */
661   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
662   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
663   assert_param(IS_OTFDEC_REGION_OPERATING_MODE(mode));
664 
665   /* Take Lock */
666   __HAL_LOCK(hotfdec);
667 
668   address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
669   region = (OTFDEC_Region_TypeDef *)address;
670 
671   /* Set mode */
672   MODIFY_REG(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_MODE, mode);
673 
674   /* Release Lock */
675   __HAL_UNLOCK(hotfdec);
676 
677   /* Status is okay */
678   return HAL_OK;
679 }
680 
681 /**
682   * @brief  Set region configuration.
683   * @note  Region enciphering/deciphering is enabled at the end of this function
684   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
685   *         the configuration information for OTFDEC module
686   * @param  RegionIndex index of region that is configured
687   * @param  Config pointer on structure containing the region configuration parameters
688   * @param  lock configuration lock enable or disable parameter
689   *         This parameter can be one of the following values:
690   *          @arg @ref OTFDEC_REG_CONFIGR_LOCK_DISABLE      OTFDEC region configuration is not locked
691   *          @arg @ref OTFDEC_REG_CONFIGR_LOCK_ENABLE       OTFDEC region configuration is locked
692   * @retval HAL state
693   */
HAL_OTFDEC_RegionConfig(OTFDEC_HandleTypeDef * hotfdec,uint32_t RegionIndex,OTFDEC_RegionConfigTypeDef * Config,uint32_t lock)694 HAL_StatusTypeDef HAL_OTFDEC_RegionConfig(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, OTFDEC_RegionConfigTypeDef *Config, uint32_t lock)
695 {
696   OTFDEC_Region_TypeDef * region;
697   uint32_t address;
698 
699   /* Check the parameters */
700   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
701   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
702   assert_param(IS_OTFDEC_REGION_CONFIG_LOCK(lock));
703 
704   if (Config == NULL)
705   {
706     return HAL_ERROR;
707   }
708   else
709   {
710 
711     /* Take Lock */
712     __HAL_LOCK(hotfdec);
713 
714     address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
715     region = (OTFDEC_Region_TypeDef *)address;
716 
717     /* Set Nonce */
718     WRITE_REG( region->REG_NONCER0, Config->Nonce[0]);
719 
720     WRITE_REG( region->REG_NONCER1, Config->Nonce[1]);
721 
722     /* Write region protected area start and end addresses */
723     WRITE_REG( region->REG_START_ADDR, Config->StartAddress);
724 
725     WRITE_REG( region->REG_END_ADDR, Config->EndAddress);
726 
727     /* Write Version */
728     MODIFY_REG( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_VERSION, (uint32_t)(Config->Version) << OTFDEC_REG_CONFIGR_VERSION_Pos );
729 
730     /* Enable region deciphering or enciphering (depending of OTFDEC_CR ENC bit setting) */
731     SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
732 
733     /* Lock the region configuration according to lock parameter value */
734     if (lock == OTFDEC_REG_CONFIGR_LOCK_ENABLE)
735     {
736       SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_LOCK_ENABLE);
737     }
738 
739     /* Release Lock */
740     __HAL_UNLOCK(hotfdec);
741 
742     /* Status is okay */
743     return HAL_OK;
744   }
745 }
746 
747 /**
748   * @brief  Configure OTFDEC attributes.
749   * @note   This function sets or resets regions privileged access protection.
750   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
751   *         the configuration information for OTFDEC module
752   * @param  Attributes This parameter can be only:
753   *           @arg @ref OTFDEC_ATTRIBUTE_PRIV   Set privileged access protection
754   *           @arg @ref OTFDEC_ATTRIBUTE_NPRIV  Reset privileged access protection
755   * @retval HAL state
756   */
HAL_OTFDEC_ConfigAttributes(OTFDEC_HandleTypeDef * hotfdec,uint32_t Attributes)757 HAL_StatusTypeDef HAL_OTFDEC_ConfigAttributes(OTFDEC_HandleTypeDef *hotfdec, uint32_t Attributes)
758 {
759   /* Check the parameters */
760   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
761   assert_param(IS_OTFDEC_ATTRIBUTE(Attributes));
762 
763   /* Take Lock */
764   __HAL_LOCK(hotfdec);
765 
766   MODIFY_REG( hotfdec->Instance->PRIVCFGR, OTFDEC_PRIVCFGR_PRIV, Attributes);
767 
768   /* Release Lock */
769   __HAL_UNLOCK(hotfdec);
770 
771   /* Status is okay */
772   return HAL_OK;
773 }
774 
775 /**
776   * @brief  Compute Key CRC
777   * @param  pKey pointer at set of keys
778   * @retval CRC value
779   */
HAL_OTFDEC_KeyCRCComputation(uint32_t * pKey)780 uint32_t HAL_OTFDEC_KeyCRCComputation(uint32_t *pKey)
781 {
782   uint8_t crc7_poly = 0x7;
783   uint32_t key_strobe[4] = {0xAA55AA55U, 0x3U, 0x18U, 0xC0U};
784   uint8_t  i;
785   uint8_t crc = 0;
786   uint32_t  j, keyval, k;
787   uint32_t * temp = pKey;
788 
789   for (j = 0U; j < 4U; j++)
790   {
791     keyval = *temp;
792     temp++;
793     if (j == 0U)
794     {
795       keyval ^= key_strobe[0];
796     }
797     else
798     {
799       keyval ^= (key_strobe[j] << 24) | ((uint32_t)crc << 16) | (key_strobe[j] << 8) | crc;
800     }
801 
802     crc = 0;
803     for (i = 0; i < (uint8_t)32; i++)
804     {
805       k = ((((uint32_t)crc >> 7) ^ ((keyval >> ((uint8_t)31-i))&((uint8_t)0xF)))) & 1U;
806       crc <<= 1;
807       if (k != 0U)
808       {
809         crc ^= crc7_poly;
810        }
811     }
812 
813     crc^=(uint8_t)0x55;
814   }
815 
816   return (uint32_t) crc;
817 }
818 
819 /**
820   * @brief  Enable peripheral enciphering.
821   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
822   *         the configuration information for OTFDEC module
823   * @note By default, deciphering mode is enabled at reset
824   * @retval HAL state
825   */
HAL_OTFDEC_EnableEnciphering(OTFDEC_HandleTypeDef * hotfdec)826 HAL_StatusTypeDef HAL_OTFDEC_EnableEnciphering(OTFDEC_HandleTypeDef *hotfdec)
827 {
828   /* Take Lock */
829   __HAL_LOCK(hotfdec);
830 
831   SET_BIT( hotfdec->Instance->CR, OTFDEC_CR_ENC );
832 
833   /* Release Lock */
834   __HAL_UNLOCK(hotfdec);
835 
836   /* Status is okay */
837   return HAL_OK;
838 }
839 
840 /**
841   * @brief  Disable peripheral enciphering.
842   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
843   *         the configuration information for OTFDEC module
844   * @retval HAL state
845   */
HAL_OTFDEC_DisableEnciphering(OTFDEC_HandleTypeDef * hotfdec)846 HAL_StatusTypeDef HAL_OTFDEC_DisableEnciphering(OTFDEC_HandleTypeDef *hotfdec)
847 {
848   /* Take Lock */
849   __HAL_LOCK(hotfdec);
850 
851   CLEAR_BIT( hotfdec->Instance->CR, OTFDEC_CR_ENC );
852 
853   /* Release Lock */
854   __HAL_UNLOCK(hotfdec);
855 
856   /* Status is okay */
857   return HAL_OK;
858 }
859 
860 
861 /**
862   * @brief  Cipher data.
863   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
864   *         the configuration information for OTFDEC module
865   * @param  RegionIndex index of region the configuration of which is used to encipher
866   * @param  input plain data
867   * @param  output ciphered data
868   * @param  size plain data size in words
869   * @param  start_address starting address in the external memory area where the enciphered data will be eventually stored
870   * @note   Region configuration parameters and OTFDEC_CR ENC bit must be set.
871   * @note   output pointer points at a temporary area in RAM to store the ciphered data. It is up to the user code
872   *         to copy the ciphered data in external RAM once the enciphering process is over.
873   * @retval HAL state
874   */
HAL_OTFDEC_Cipher(OTFDEC_HandleTypeDef * hotfdec,uint32_t RegionIndex,uint32_t * input,uint32_t * output,uint32_t size,uint32_t start_address)875 HAL_StatusTypeDef HAL_OTFDEC_Cipher(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, uint32_t * input, uint32_t * output, uint32_t size, uint32_t start_address)
876 {
877   uint32_t j;
878   volatile uint32_t * extMem_ptr = (uint32_t *)start_address;
879   uint32_t * in_ptr = input;
880   uint32_t * out_ptr = output;
881 
882   /* Check the parameters */
883   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
884   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
885 
886   if ((input == NULL) || (output == NULL) || (size == 0U))
887   {
888     return HAL_ERROR;
889   }
890   else
891   {
892     /* Take Lock */
893     __HAL_LOCK(hotfdec);
894 
895      for (j = 0; j < size; j++)
896      {
897          *extMem_ptr = *in_ptr;
898          in_ptr++;
899          *out_ptr = *extMem_ptr;
900          out_ptr++;
901          extMem_ptr++;
902      }
903 
904   /* Release Lock */
905   __HAL_UNLOCK(hotfdec);
906 
907   /* Status is okay */
908   return HAL_OK;
909   }
910 }
911 
912 /**
913   * @brief  Enable region processing (enciphering or deciphering).
914   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
915   *         the configuration information for OTFDEC module
916   * @param  RegionIndex index of region the enciphering or deciphering is enabled
917   * @note   An error is reported when the configuration is locked.
918   * @retval HAL state
919   */
HAL_OTFDEC_RegionEnable(OTFDEC_HandleTypeDef * hotfdec,uint32_t RegionIndex)920 HAL_StatusTypeDef HAL_OTFDEC_RegionEnable(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
921 {
922   OTFDEC_Region_TypeDef * region;
923   uint32_t address;
924 
925   /* Check the parameters */
926   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
927   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
928 
929   /* Take Lock */
930   __HAL_LOCK(hotfdec);
931 
932   address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
933   region = (OTFDEC_Region_TypeDef *)address;
934 
935   if (READ_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_LOCK_ENABLE) == OTFDEC_REG_CONFIGR_LOCK_ENABLE)
936   {
937     /* Configuration is locked, REG_EN bit can't be modified */
938     __HAL_UNLOCK(hotfdec);
939 
940      return HAL_ERROR;
941   }
942 
943   /* Enable region processing */
944   SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
945 
946   /* Release Lock */
947   __HAL_UNLOCK(hotfdec);
948 
949   /* Status is okay */
950   return HAL_OK;
951 }
952 
953 /**
954   * @brief  Disable region processing (enciphering or deciphering).
955   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
956   *         the configuration information for OTFDEC module
957   * @param  RegionIndex index of region the enciphering or deciphering is disabled
958   * @note   An error is reported when the configuration is locked.
959   * @retval HAL state
960   */
HAL_OTFDEC_RegionDisable(OTFDEC_HandleTypeDef * hotfdec,uint32_t RegionIndex)961 HAL_StatusTypeDef HAL_OTFDEC_RegionDisable(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
962 {
963   OTFDEC_Region_TypeDef * region;
964   uint32_t address;
965 
966   /* Check the parameters */
967   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
968   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
969 
970   /* Take Lock */
971   __HAL_LOCK(hotfdec);
972 
973   address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
974   region = (OTFDEC_Region_TypeDef *)address;
975 
976   if (READ_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_LOCK_ENABLE) == OTFDEC_REG_CONFIGR_LOCK_ENABLE)
977   {
978     /* Configuration is locked, REG_EN bit can't be modified */
979     __HAL_UNLOCK(hotfdec);
980 
981      return HAL_ERROR;
982   }
983 
984   /* Disable region processing */
985   CLEAR_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
986 
987   /* Release Lock */
988   __HAL_UNLOCK(hotfdec);
989 
990   /* Status is okay */
991   return HAL_OK;
992 }
993 
994 /**
995   * @}
996   */
997 
998 /** @defgroup OTFDEC_Exported_Functions_Group4 Peripheral State and Status functions
999  *  @brief   Peripheral State functions.
1000  *
1001 @verbatim
1002   ==============================================================================
1003                       ##### Peripheral State functions #####
1004   ==============================================================================
1005     [..]
1006     This subsection permits to get in run-time the status of the peripheral.
1007 
1008 @endverbatim
1009   * @{
1010   */
1011 
1012 /**
1013   * @brief  Return the OTFDEC state.
1014   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
1015   *         the configuration information for OTFDEC module
1016   * @retval HAL state
1017   */
HAL_OTFDEC_GetState(OTFDEC_HandleTypeDef * hotfdec)1018 HAL_OTFDEC_StateTypeDef HAL_OTFDEC_GetState(OTFDEC_HandleTypeDef *hotfdec)
1019 {
1020   return hotfdec->State;
1021 }
1022 
1023 /**
1024   * @brief  Get OTFDEC configuration attributes.
1025   * @note   This function returns whether or not the regions access protection is in privileged mode.
1026   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
1027   *         the configuration information for OTFDEC module
1028   * @param  Attributes pointer to attributes variable. This parameter can be only:
1029   *           @arg @ref OTFDEC_ATTRIBUTE_PRIV   Set privileged access protection
1030   *           @arg @ref OTFDEC_ATTRIBUTE_NPRIV  Reset privileged access protection
1031   * @retval HAL state
1032   */
HAL_OTFDEC_GetConfigAttributes(OTFDEC_HandleTypeDef * hotfdec,uint32_t * Attributes)1033 HAL_StatusTypeDef HAL_OTFDEC_GetConfigAttributes(OTFDEC_HandleTypeDef *hotfdec, uint32_t * Attributes)
1034 {
1035   /* Check the parameters */
1036   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
1037 
1038   /* Take Lock */
1039   __HAL_LOCK(hotfdec);
1040 
1041   *Attributes = READ_BIT( hotfdec->Instance->PRIVCFGR, OTFDEC_PRIVCFGR_PRIV);
1042 
1043   /* Release Lock */
1044   __HAL_UNLOCK(hotfdec);
1045 
1046   /* Status is okay */
1047   return HAL_OK;
1048 }
1049 
1050 /**
1051   * @brief  Return region keys CRC.
1052   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
1053   *         the configuration information for OTFDEC module
1054   * @param  RegionIndex index of region the keys CRC of which is read
1055   * @retval Key CRC
1056   */
HAL_OTFDEC_RegionGetKeyCRC(OTFDEC_HandleTypeDef * hotfdec,uint32_t RegionIndex)1057 uint32_t HAL_OTFDEC_RegionGetKeyCRC(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
1058 {
1059   OTFDEC_Region_TypeDef * region;
1060   uint32_t address;
1061   uint32_t keycrc;
1062 
1063   /* Check the parameters */
1064   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
1065   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
1066 
1067   address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
1068   region = (OTFDEC_Region_TypeDef *)address;
1069 
1070   keycrc = (READ_REG( region->REG_CONFIGR )) & OTFDEC_REG_CONFIGR_KEYCRC;
1071 
1072   keycrc >>= OTFDEC_REG_CONFIGR_KEYCRC_Pos;
1073 
1074   return keycrc;
1075 }
1076 
1077 /**
1078   * @brief  Return region configuration parameters.
1079   * @param  hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
1080   *         the configuration information for OTFDEC module
1081   * @param  RegionIndex index of region the configuration of which is read
1082   * @param  Config pointer on structure that will be filled up with the region configuration parameters
1083   * @retval HAL state
1084   */
HAL_OTFDEC_RegionGetConfig(OTFDEC_HandleTypeDef * hotfdec,uint32_t RegionIndex,OTFDEC_RegionConfigTypeDef * Config)1085 HAL_StatusTypeDef HAL_OTFDEC_RegionGetConfig(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, OTFDEC_RegionConfigTypeDef *Config)
1086 {
1087   OTFDEC_Region_TypeDef * region;
1088   uint32_t address;
1089 
1090   /* Check the parameters */
1091   assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
1092   assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
1093 
1094   if (Config == NULL)
1095   {
1096     return HAL_ERROR;
1097   }
1098   else
1099   {
1100     /* Take Lock */
1101     __HAL_LOCK(hotfdec);
1102 
1103     address =   (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
1104     region = (OTFDEC_Region_TypeDef *)address;
1105 
1106     /* Read Nonce */
1107     Config->Nonce[0] = READ_REG(region->REG_NONCER0);
1108     Config->Nonce[1] = READ_REG(region->REG_NONCER1);
1109 
1110     /* Read Addresses */
1111     Config->StartAddress = READ_REG(region->REG_START_ADDR);
1112     Config->EndAddress = READ_REG(region->REG_END_ADDR);
1113 
1114     /* Read Version */
1115     Config->Version = (uint16_t)(READ_REG(region->REG_CONFIGR) & OTFDEC_REG_CONFIGR_VERSION) >> OTFDEC_REG_CONFIGR_VERSION_Pos;
1116 
1117     /* Release Lock */
1118     __HAL_UNLOCK(hotfdec);
1119 
1120     /* Status is okay */
1121     return HAL_OK;
1122   }
1123 }
1124 
1125 
1126 /**
1127   * @}
1128   */
1129 
1130 /**
1131   * @}
1132   */
1133 
1134 #endif /* OTFDEC1 */
1135 
1136 #endif /* HAL_OTFDEC_MODULE_ENABLED */
1137 
1138 
1139 /**
1140   * @}
1141   */
1142 
1143 /**
1144   * @}
1145   */
1146