STM32L4xx_HAL_Driver  1.14.0
Initialization and deinitialization functions

Initialization and Configuration functions. More...

Functions

HAL_StatusTypeDef HAL_CRYP_Init (CRYP_HandleTypeDef *hcryp)
 Initialize the CRYP according to the specified parameters in the CRYP_InitTypeDef and initialize the associated handle. More...
 
HAL_StatusTypeDef HAL_CRYP_DeInit (CRYP_HandleTypeDef *hcryp)
 DeInitialize the CRYP peripheral. More...
 
void HAL_CRYP_MspInit (CRYP_HandleTypeDef *hcryp)
 Initialize the CRYP MSP. More...
 
void HAL_CRYP_MspDeInit (CRYP_HandleTypeDef *hcryp)
 DeInitialize CRYP MSP. More...
 

Detailed Description

Initialization and Configuration functions.

  ==============================================================================
              ##### Initialization and deinitialization functions #####
  ==============================================================================
    [..]  This section provides functions allowing to:
      (+) Initialize the CRYP according to the specified parameters
          in the CRYP_InitTypeDef and creates the associated handle
      (+) DeInitialize the CRYP peripheral
      (+) Initialize the CRYP MSP (MCU Specific Package)
      (+) De-Initialize the CRYP MSP

    [..]
    (@) Specific care must be taken to format the key and the Initialization Vector IV!

   [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where
        b127 is the MSB and b0 the LSB, the key must be stored in MCU memory
        (+) as a sequence of words where the MSB word comes first (occupies the
          lowest memory address)
        (+) where each word is byte-swapped:
         (++)   address n+0 : 0b b103 .. b96 b111 .. b104 b119 .. b112 b127 .. b120
         (++)   address n+4 : 0b b71 .. b64 b79 .. b72 b87 .. b80 b95 .. b88
         (++)   address n+8 : 0b b39 .. b32 b47 .. b40 b55 .. b48 b63 .. b56
         (++)   address n+C : 0b b7 .. b0 b15 .. b8 b23 .. b16 b31 .. b24
    [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}.
        The 4 32-bit words that make the key must be stored as follows in MCU memory:
         (+)    address n+0 : 0x B12 B13 B14 B15
         (+)    address n+4 : 0x B8 B9 B10 B11
         (+)    address n+8 : 0x B4 B5 B6 B7
         (+)    address n+C : 0x B0 B1 B2 B3
    [..]  which leads to the expected setting
      (+)       AES_KEYR3 = 0x B15 B14 B13 B12
      (+)       AES_KEYR2 = 0x B11 B10 B9 B8
      (+)       AES_KEYR1 = 0x B7 B6 B5 B4
      (+)       AES_KEYR0 = 0x B3 B2 B1 B0

   [..]  Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}.
         The 8 32-bit words that make the key must be stored as follows in MCU memory:
         (+)    address n+00 : 0x B28 B29 B30 B31
         (+)    address n+04 : 0x B24 B25 B26 B27
         (+)    address n+08 : 0x B20 B21 B22 B23
         (+)    address n+0C : 0x B16 B17 B18 B19
         (+)    address n+10 : 0x B12 B13 B14 B15
         (+)    address n+14 : 0x B8 B9 B10 B11
         (+)    address n+18 : 0x B4 B5 B6 B7
         (+)    address n+1C : 0x B0 B1 B2 B3
    [..]  which leads to the expected setting
      (+)       AES_KEYR7 = 0x B31 B30 B29 B28
      (+)       AES_KEYR6 = 0x B27 B26 B25 B24
      (+)       AES_KEYR5 = 0x B23 B22 B21 B20
      (+)       AES_KEYR4 = 0x B19 B18 B17 B16
      (+)       AES_KEYR3 = 0x B15 B14 B13 B12
      (+)       AES_KEYR2 = 0x B11 B10 B9 B8
      (+)       AES_KEYR1 = 0x B7 B6 B5 B4
      (+)       AES_KEYR0 = 0x B3 B2 B1 B0

   [..] Initialization Vector IV (4 32-bit words) format must follow the same as
        that of a 128-bit long key.

  [..]

Function Documentation

◆ HAL_CRYP_DeInit()

HAL_StatusTypeDef HAL_CRYP_DeInit ( CRYP_HandleTypeDef hcryp)

DeInitialize the CRYP peripheral.

Parameters
hcryppointer to a CRYP_HandleTypeDef structure that contains the configuration information for CRYP module
Return values
HALstatus

Definition at line 442 of file stm32l4xx_hal_cryp.c.

443 {
444  /* Check the CRYP handle allocation */
445  if(hcryp == NULL)
446  {
447  return HAL_ERROR;
448  }
449 
450  /* Change the CRYP state */
451  hcryp->State = HAL_CRYP_STATE_BUSY;
452 
453  /* Set the default CRYP phase */
454  hcryp->Phase = HAL_CRYP_PHASE_READY;
455 
456  /* Reset CrypInCount and CrypOutCount */
457  hcryp->CrypInCount = 0;
458  hcryp->CrypOutCount = 0;
459 
460  /* Disable the CRYP Peripheral Clock */
461  __HAL_CRYP_DISABLE(hcryp);
462 
463 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
464  if(hcryp->MspDeInitCallback == NULL)
465  {
467  }
468 
469  /* DeInit the low level hardware */
470  hcryp->MspDeInitCallback(hcryp);
471 #else
472  /* DeInit the low level hardware: CLOCK, NVIC.*/
473  HAL_CRYP_MspDeInit(hcryp);
474 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
475 
476  /* Change the CRYP state */
477  hcryp->State = HAL_CRYP_STATE_RESET;
478 
479  /* Release Lock */
480  __HAL_UNLOCK(hcryp);
481 
482  /* Return function status */
483  return HAL_OK;
484 }
__IO HAL_CRYP_STATETypeDef State
__HAL_UNLOCK(hrtc)
return HAL_OK
void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
DeInitialize CRYP MSP.
HAL_PhaseTypeDef Phase
void(* MspDeInitCallback)(struct __CRYP_HandleTypeDef *hcryp)

◆ HAL_CRYP_Init()

HAL_StatusTypeDef HAL_CRYP_Init ( CRYP_HandleTypeDef hcryp)

Initialize the CRYP according to the specified parameters in the CRYP_InitTypeDef and initialize the associated handle.

Note
Specific care must be taken to format the key and the Initialization Vector IV stored in the MCU memory before calling HAL_CRYP_Init(). Refer to explanations hereabove.
Return values
HALstatus

Definition at line 240 of file stm32l4xx_hal_cryp.c.

241 {
242  /* Check the CRYP handle allocation */
243  if(hcryp == NULL)
244  {
245  return HAL_ERROR;
246  }
247 
248  /* Check the instance */
249  assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance));
250 
251  /* Check the parameters */
252  assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
253  assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
254  assert_param(IS_CRYP_ALGOMODE(hcryp->Init.OperatingMode));
255  /* ChainingMode parameter is irrelevant when mode is set to Key derivation */
256  if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
257  {
258  assert_param(IS_CRYP_CHAINMODE(hcryp->Init.ChainingMode));
259  }
260  assert_param(IS_CRYP_WRITE(hcryp->Init.KeyWriteFlag));
261 
262  /*========================================================*/
263  /* Check the proper operating/chaining modes combinations */
264  /*========================================================*/
265  /* Check the proper chaining when the operating mode is key derivation and decryption */
266 #if defined(AES_CR_NPBLB)
267  if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
268  ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \
269  || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \
270  || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)))
271 #else
272  if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
273  ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \
274  || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \
275  || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)))
276 #endif
277  {
278  return HAL_ERROR;
279  }
280  /* Check that key derivation is not set in CMAC mode or CCM mode when applicable */
281 #if defined(AES_CR_NPBLB)
282  if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
283  && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
284 #else
285  if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
286  && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
287 #endif
288  {
289  return HAL_ERROR;
290  }
291 
292 
293  /*================*/
294  /* Initialization */
295  /*================*/
296  /* Initialization start */
297 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
298  if (hcryp->State == HAL_CRYP_STATE_RESET)
299  {
300  /* Allocate lock resource and initialize it */
301  hcryp->Lock = HAL_UNLOCKED;
302 
303  /* Reset Callback pointers in HAL_CRYP_STATE_RESET only */
304  hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak (surcharged) input DMA transfer completion callback */
305  hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak (surcharged) output DMA transfer completion callback */
306  hcryp->CompCpltCallback = HAL_CRYPEx_ComputationCpltCallback; /* Legacy weak (surcharged) computation completion callback */
307  hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak (surcharged) error callback */
308  if(hcryp->MspInitCallback == NULL)
309  {
311  }
312 
313  /* Init the low level hardware */
314  hcryp->MspInitCallback(hcryp);
315  }
316 #else
317  if(hcryp->State == HAL_CRYP_STATE_RESET)
318  {
319  /* Allocate lock resource and initialize it */
320  hcryp->Lock = HAL_UNLOCKED;
321 
322  /* Init the low level hardware */
323  HAL_CRYP_MspInit(hcryp);
324  }
325 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
326 
327  /* Change the CRYP state */
328  hcryp->State = HAL_CRYP_STATE_BUSY;
329 
330  /* Disable the Peripheral */
331  __HAL_CRYP_DISABLE(hcryp);
332 
333  /*=============================================================*/
334  /* AES initialization common to all operating modes */
335  /*=============================================================*/
336  /* Set the Key size selection */
337  MODIFY_REG(hcryp->Instance->CR, AES_CR_KEYSIZE, hcryp->Init.KeySize);
338 
339  /* Set the default CRYP phase when this parameter is not used.
340  Phase is updated below in case of GCM/GMAC(/CMAC)(/CCM) setting. */
342 
343 
344 
345  /*=============================================================*/
346  /* Carry on the initialization based on the AES operating mode */
347  /*=============================================================*/
348  /* Key derivation */
349  if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
350  {
351  MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_ALGOMODE_KEYDERIVATION);
352 
353  /* Configure the Key registers */
354  if (CRYP_SetKey(hcryp) != HAL_OK)
355  {
356  return HAL_ERROR;
357  }
358  }
359  else
360  /* Encryption / Decryption (with or without key derivation) / authentication */
361  {
362 #if !defined(AES_CR_NPBLB)
363  /* Set data type, operating and chaining modes.
364  In case of GCM or GMAC, data type is forced to 0b00 */
365  if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
366  {
367  MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
368  }
369  else
370 #endif
371  {
372  MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.DataType|hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
373  }
374 
375 
376  /* Specify the encryption/decryption phase in case of Galois counter mode (GCM),
377  Galois message authentication code (GMAC), cipher message authentication code (CMAC) when applicable
378  or Counter with Cipher Mode (CCM) when applicable */
379 #if defined(AES_CR_NPBLB)
380  if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
381  || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
382 #else
383  if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
384  || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
385 #endif
386  {
387  MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, hcryp->Init.GCMCMACPhase);
388  hcryp->Phase = HAL_CRYP_PHASE_START;
389  }
390 
391 
392  /* Configure the Key registers if no need to bypass this step */
393  if (hcryp->Init.KeyWriteFlag == CRYP_KEY_WRITE_ENABLE)
394  {
395  if (CRYP_SetKey(hcryp) != HAL_OK)
396  {
397  return HAL_ERROR;
398  }
399  }
400 
401  /* If applicable, configure the Initialization Vector */
402  if (hcryp->Init.ChainingMode != CRYP_CHAINMODE_AES_ECB)
403  {
404  if (CRYP_SetInitVector(hcryp) != HAL_OK)
405  {
406  return HAL_ERROR;
407  }
408  }
409  }
410 
411 #if defined(AES_CR_NPBLB)
412  /* Clear NPBLB field */
413  CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
414 #endif
415 
416  /* Reset CrypInCount and CrypOutCount */
417  hcryp->CrypInCount = 0;
418  hcryp->CrypOutCount = 0;
419 
420  /* Reset ErrorCode field */
421  hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
422 
423  /* Reset Mode suspension request */
425 
426  /* Change the CRYP state */
427  hcryp->State = HAL_CRYP_STATE_READY;
428 
429  /* Enable the Peripheral */
430  __HAL_CRYP_ENABLE(hcryp);
431 
432  /* Return function status */
433  return HAL_OK;
434 }
void(* ErrorCallback)(struct __CRYP_HandleTypeDef *hcryp)
__IO HAL_CRYP_STATETypeDef State
void(* MspInitCallback)(struct __CRYP_HandleTypeDef *hcryp)
void(* OutCpltCallback)(struct __CRYP_HandleTypeDef *hcryp)
void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
Initialize the CRYP MSP.
void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
Output DMA transfer complete callback.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
HAL_SuspendTypeDef SuspendRequest
static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp)
Write the InitVector/InitCounter in IVRx registers.
return HAL_OK
static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp)
Write the Key in KeyRx registers.
void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
CRYP error callback.
void(* CompCpltCallback)(struct __CRYP_HandleTypeDef *hcryp)
void(* InCpltCallback)(struct __CRYP_HandleTypeDef *hcryp)
void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
Input DMA transfer complete callback.
HAL_PhaseTypeDef Phase
__weak void HAL_CRYPEx_ComputationCpltCallback(CRYP_HandleTypeDef *hcryp)
Computation completed callbacks.
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_CRYP_MspDeInit()

__weak void HAL_CRYP_MspDeInit ( CRYP_HandleTypeDef hcryp)

DeInitialize CRYP MSP.

Parameters
hcryppointer to a CRYP_HandleTypeDef structure that contains the configuration information for CRYP module
Return values
None

Definition at line 508 of file stm32l4xx_hal_cryp.c.

509 {
510  /* Prevent unused argument(s) compilation warning */
511  UNUSED(hcryp);
512 
513  /* NOTE : This function should not be modified; when the callback is needed,
514  the HAL_CRYP_MspDeInit can be implemented in the user file
515  */
516 }

◆ HAL_CRYP_MspInit()

__weak void HAL_CRYP_MspInit ( CRYP_HandleTypeDef hcryp)

Initialize the CRYP MSP.

Parameters
hcryppointer to a CRYP_HandleTypeDef structure that contains the configuration information for CRYP module
Return values
None

Definition at line 492 of file stm32l4xx_hal_cryp.c.

493 {
494  /* Prevent unused argument(s) compilation warning */
495  UNUSED(hcryp);
496 
497  /* NOTE : This function should not be modified; when the callback is needed,
498  the HAL_CRYP_MspInit can be implemented in the user file
499  */
500 }