/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 180 MHz */
  SystemClock_Config();
  
  /* Configure LED3 */
  BSP_LED_Init(LED3);  
  
  /* Configure the COM port */
  UartHandle.Init.BaudRate = 115200;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits = UART_STOPBITS_1;
  UartHandle.Init.Parity = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode = UART_MODE_TX_RX;
  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;  
  BSP_COM_Init(COM1, &UartHandle);
  
  /*##-1- Configure the CRYP peripheral ######################################*/
  /* Set the CRYP parameters */
  CrypHandle.Instance = CRYP;
  
  CrypHandle.Init.DataType   = CRYP_DATATYPE_8B;
  CrypHandle.Init.KeySize    = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey       = aAES128Key;
  CrypHandle.Init.pInitVect  = aInitVector;
  CrypHandle.Init.Header     = aHeaderMessage;
  CrypHandle.Init.HeaderSize = sizeof(aHeaderMessage);
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler(); 
  }
  
  /* Infinite loop */
  while(1)
  {
    /* Display Plaintext */
    Display_Plaintext(PLAINTEXT_SIZE);
    
    /*##-2- Encryption Phase #################################################*/
    /* Set the Initialization vector */
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }
  
    /* Encrypt the plaintext message */
    if(HAL_CRYPEx_AESGCM_Encrypt(&CrypHandle, aPlaintext, 16, aEncryptedText, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(AES_MODE_GCM, KEY_SIZE, PLAINTEXT_SIZE);
    }
    else 
    {
      /* Processing Error */
      Error_Handler();
    }
    
    /* Compute the authentication aTAG */
    if(HAL_CRYPEx_AESGCM_Finish(&CrypHandle, 16, aTAG, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display the computed aTAG, aTAG size is 16 bytes */
      Display_TAG(aTAG);
    }
    else 
    {
      /* Processing Error */
      Error_Handler();
    }

    /*##-3- Decryption Phase #################################################*/    
    /* Set the Initialization vector */
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }
  
    /* Encrypt the plaintext message */
    if(HAL_CRYPEx_AESGCM_Decrypt(&CrypHandle, aCyphertext, 16, aDecryptedText, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_DecryptedData(AES_MODE_GCM, KEY_SIZE, PLAINTEXT_SIZE);
    }
    else 
    {
      /* Processing Error */
      Error_Handler();
    }
       
    /* Compute the authentication aTAG */
    if(HAL_CRYPEx_AESGCM_Finish(&CrypHandle, 16, aTAG, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display the computed aTAG, aTAG size is 16 bytes */
      Display_TAG(aTAG);
    }
    else 
    {
      /* Processing Error */
      Error_Handler();
    }    
    
    PressToContinue();
    printf("\n\r Example restarted...\n ");   
  }
}
Example #2
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization: global MSP (MCU Support Package) initialization
     */
  HAL_Init();

  /* Configure the system clock to 180 MHz */
  SystemClock_Config();
  
  /* Configure LED3 */
  BSP_LED_Init(LED3);  

  /* Configure the COM port */
  UartHandle.Init.BaudRate = 115200;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits = UART_STOPBITS_1;
  UartHandle.Init.Parity = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode = UART_MODE_TX_RX;
  BSP_COM_Init(COM1, &UartHandle);

  /*##-1- Configure the CRYP peripheral ######################################*/
  /* Set the CRYP parameters */
  CrypHandle.Instance = CRYP;
  
  CrypHandle.Init.DataType   = CRYP_DATATYPE_8B;
  CrypHandle.Init.KeySize    = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey       = aAES128Key;
  CrypHandle.Init.pInitVect  = aInitVector;
  CrypHandle.Init.Header     = aHeaderMessage;
  CrypHandle.Init.HeaderSize = sizeof(aHeaderMessage);

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  /* Infinite loop */
  while(1)
  {
    /* Display Plaintext */
    Display_Plaintext(PLAINTEXT_SIZE);
    
    /*##-2- Encryption Phase #################################################*/
    /* Set the Initialization vector */
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }
  
    /* Encrypt the plaintext message */
    if(HAL_CRYPEx_AESGCM_Encrypt(&CrypHandle, aPlaintext, 16, aEncryptedText, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(AES_MODE_GCM, KEY_SIZE, PLAINTEXT_SIZE);
    }
    else 
    {
      /* Processing Error */
      Error_Handler();
    }
    
    /* Compute the authentication aTAG */
    if(HAL_CRYPEx_AESGCM_Finish(&CrypHandle, 16, aTAG, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display the computed aTAG, aTAG size is 16 bytes */
      Display_TAG(aTAG);
    }
    else 
    {
      /* Processing Error */
      Error_Handler();
    }

    /*##-3- Decryption Phase #################################################*/    
    /* Set the Initialization vector */
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }
  
    /* Encrypt the plaintext message */
    if(HAL_CRYPEx_AESGCM_Decrypt(&CrypHandle, aCyphertext, 16, aDecryptedText, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_DecryptedData(AES_MODE_GCM, KEY_SIZE, PLAINTEXT_SIZE);
    }
    else 
    {
      /* Processing Error */
      Error_Handler();
    }
       
    /* Compute the authentication aTAG */
    if(HAL_CRYPEx_AESGCM_Finish(&CrypHandle, 16, aTAG, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display the computed aTAG, aTAG size is 16 bytes */
      Display_TAG(aTAG);
    }
    else 
    {
      /* Processing Error */
      Error_Handler();
    }    
    
    PressToContinue();
    printf("\n\r Example restarted...\n ");   
  }
}