コード例 #1
0
ファイル: main.c プロジェクト: scottmnowakowski/sensor_suite
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)
       before to branch to application main.
     */
          
  /* USART Configuration */     
  USART_Config();

  /* Display Plain Data */
  Display_PlainData();

/*=====================================================
    Encryption                                          
======================================================*/
  /* Encryption */
  AES128_Encrypt_DMA();
  
  /* Display encrypted Data */
  Display_EncryptedData();

/*=====================================================
    Decryption                                          
======================================================*/
  /* Decryption */
  AES128_Decrypt_DMA();

  /* Display decrypted data */
  Display_DecryptedData();

  while(1);  
}
コード例 #2
0
ファイル: main.c プロジェクト: szymon2103/Stm32
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)
       before to branch to application main.
     */

  /* USARTx configured as follows:
        - BaudRate = 115200 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART_Config();

  /* Enable CRYP clock */
  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE);

  while(1)
  {
    /* Display Plain Text */
    Display_PlainData(PLAINTEXT_SIZE);

    /********************************************************************/
    /* !!!! This example runs only on STM32F437x/STM32F439x Devices !!! */
    /********************************************************************/

    /* Encrypt the plaintext message */
    if(CRYP_AES_GCM(MODE_ENCRYPT, InitVector, AES128key, KEY_SIZE, PlainText, PLAINTEXT_SIZE, HeaderMessage, HEADER_SIZE, OutputText, TAG) == SUCCESS)
    {
      /* Display encrypted Data */
      Display_EncryptedData(AES_MODE_GCM, KEY_SIZE, PLAINTEXT_SIZE);
      /* Display computed TAG, TAG size is 16 */
      Display_TAG(TAG);
    }

    /* Decrypt the cyphertext message */
    if(CRYP_AES_GCM(MODE_DECRYPT, InitVector, AES128key, KEY_SIZE, ExpectedCypherText, PLAINTEXT_SIZE, HeaderMessage, HEADER_SIZE, OutputText, TAG) == SUCCESS)
    {
      /* Display encrypted Data */
      Display_DecryptedData(AES_MODE_GCM, KEY_SIZE, PLAINTEXT_SIZE);
      /* Display computed TAG, TAG size is 16 */
      Display_TAG(TAG);
    }

    PressToContinue();
    printf("\n\r Example restarted...\n ");
  }
}
コード例 #3
0
ファイル: main.c プロジェクト: JanusRC/T2-Terminus
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f2xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f2xx.c file
     */     
       
  /* USARTx configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  /* USART Configuration */
  USART_Config();

  /* Display Plain Data*/
  Display_PlainData();

/*=====================================================
    Encryption
======================================================*/

  /* Encrypt */
  TDES_Encrypt_DMA();

  /* Display encrypted Data*/
  Display_EncryptedData();

/*=====================================================
    Decryption
======================================================*/

  /* Decrypt */
  TDES_Decrypt_DMA();

  /* Display decrypted data*/
  Display_DecryptedData();

  while(1);
}
コード例 #4
0
ファイル: main.c プロジェクト: Bosvark/STM32Cube_FW_F4_V1.1.0
/**
  * @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 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 common CRYP parameters */
  CrypHandle.Init.DataType = CRYP_DATATYPE_8B;

  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler(); 
  }
  
  /* Infinite loop */ 
  while(1)
  {
    /* Display Plain Data */
    Display_PlainData(AES_TEXT_SIZE);
    
    /* Display Cypher Data */
    Display_CypherData(AES_TEXT_SIZE); 
    
    PressToContinue();
    
/******************************************************************************/
/*                             AES mode ECB                                   */
/******************************************************************************/

/*=====================================================
    Encryption ECB mode                                        
======================================================*/

/*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize = CRYP_KEYSIZE_128B;
    CrypHandle.Init.pKey    = aAES128key;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESECB_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(ECB, 128, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }     
/*****************  AES 192   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize = CRYP_KEYSIZE_192B;
    CrypHandle.Init.pKey    = aAES192key;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESECB_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(ECB, 192, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }    
/*****************  AES 256   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize = CRYP_KEYSIZE_256B;
    CrypHandle.Init.pKey    = aAES256key;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }
    
    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESECB_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(ECB, 256, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }    
    
    PressToContinue();
    
/*=====================================================
    Decryption ECB mode                                        
======================================================*/
    
/*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize = CRYP_KEYSIZE_128B;
    CrypHandle.Init.pKey    = aAES128key;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESECB_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(ECB,128,AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }   
/*****************  AES 192   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize = CRYP_KEYSIZE_192B;
    CrypHandle.Init.pKey    = aAES192key;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESECB_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(ECB,192,AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }  
/*****************  AES 256   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize = CRYP_KEYSIZE_256B;
    CrypHandle.Init.pKey    = aAES256key;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESECB_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(ECB,256,AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }
  
    PressToContinue();
    
/******************************************************************************/
/*                             AES mode CBC                                   */
/******************************************************************************/
    
/*=====================================================
    Encryption CBC mode                                        
======================================================*/
    
/*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize   = CRYP_KEYSIZE_128B;
    CrypHandle.Init.pKey      = aAES128key;
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESCBC_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(CBC, 128, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }   
/*****************  AES 192   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize   = CRYP_KEYSIZE_192B;
    CrypHandle.Init.pKey      = aAES192key;
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESCBC_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(CBC, 192, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }   
/*****************  AES 256   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize   = CRYP_KEYSIZE_256B;
    CrypHandle.Init.pKey      = aAES256key;
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESCBC_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(CBC, 256, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }
    
    PressToContinue();
/*=====================================================
    Decryption CBC mode                                        
======================================================*/
    
/*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize   = CRYP_KEYSIZE_128B;
    CrypHandle.Init.pKey      = aAES128key;
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESCBC_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(CBC,128,AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }   
/*****************  AES 192   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize   = CRYP_KEYSIZE_192B;
    CrypHandle.Init.pKey      = aAES192key;
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }
    
    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESCBC_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(CBC,192,AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    } 
/*****************  AES 256   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize   = CRYP_KEYSIZE_256B;
    CrypHandle.Init.pKey      = aAES256key;
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESCBC_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(CBC,256,AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }

    PressToContinue();
/******************************************************************************/
/*                             AES mode CTR                                   */
/******************************************************************************/

/*=====================================================
    Encryption CTR mode                                        
======================================================*/
    
/*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize   = CRYP_KEYSIZE_128B;
    CrypHandle.Init.pKey      = aAES128key;
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESCTR_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(CTR, 128, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }
/*****************  AES 192   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize   = CRYP_KEYSIZE_192B;
    CrypHandle.Init.pKey      = aAES192key;
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESCTR_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(CTR, 192, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }   
/*****************  AES 256   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize   = CRYP_KEYSIZE_256B;
    CrypHandle.Init.pKey      = aAES256key;
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESCTR_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(CTR, 256, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }
    
    PressToContinue();
/*=====================================================
    Decryption CTR mode                                        
======================================================*/
    
/*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize   = CRYP_KEYSIZE_128B;
    CrypHandle.Init.pKey      = aAES128key;
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESCTR_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(CTR,128,AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }
/*****************  AES 192   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize   = CRYP_KEYSIZE_192B;
    CrypHandle.Init.pKey      = aAES192key;
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESCTR_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(CTR,192,AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }
/*****************  AES 256   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Init.KeySize   = CRYP_KEYSIZE_256B;
    CrypHandle.Init.pKey      = aAES256key;
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESCTR_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(CTR, 256, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler(); 
    }
    
    PressToContinue();
    printf("\n\r Example restarted...\n ");    
  } 
}
コード例 #5
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)
       before to branch to application main.
     */     
       
  /* USARTx configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART_Config();
  
  /* Enable CRYP clock */
  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE);
  
  while(1)
  { 
    /* Display Plain Data*/
    Display_PlainData(AES_TEXT_SIZE);

/******************************************************************************/
/*                             AES mode ECB                                   */
/******************************************************************************/

/*=====================================================
  Encryption ECB mode                                        
======================================================*/
    PressToContinue();
/****************************************/
/*                           AES 128   **/
/****************************************/
    /* Encrypt the plaintext message*/
    if(CRYP_AES_ECB(MODE_ENCRYPT, AES128key, 128, Plaintext, AES_TEXT_SIZE, Encryptedtext) == SUCCESS)
    {
      /* Display encrypted Data*/
      Display_EncryptedData(ECB,128,AES_TEXT_SIZE);
    }
/****************************************/
/*                           AES 192   **/
/****************************************/
    /* Encrypt the plaintext message*/
    if(CRYP_AES_ECB(MODE_ENCRYPT,AES192key,192,Plaintext,AES_TEXT_SIZE,Encryptedtext) == SUCCESS)
    {
      /* Display encrypted Data*/
      Display_EncryptedData(ECB,192,AES_TEXT_SIZE);
    }
/****************************************/
/*                           AES 256   **/
/****************************************/
    /* Encrypt the plaintext message*/
    if(CRYP_AES_ECB(MODE_ENCRYPT,AES256key,256,Plaintext,AES_TEXT_SIZE,Encryptedtext) == SUCCESS)
    {
      /* Display encrypted Data*/
      Display_EncryptedData(ECB, 256,AES_TEXT_SIZE);
    }
/*=====================================================
    Decryption in ECB mode                                       
======================================================*/
   PressToContinue();
/****************************************/
/*                           AES 128   **/
/****************************************/
    /* Decrypt the plaintext message  */
    if(CRYP_AES_ECB(MODE_DECRYPT,AES128key,128,Ciphertext,AES_TEXT_SIZE,Decryptedtext) == SUCCESS) 
    {
      /* Display decrypted data*/
      Display_DecryptedData(ECB,128,AES_TEXT_SIZE);
    }
/****************************************/
/*                           AES 192   **/
/****************************************/
    /* Decrypt the plaintext message  */
    if(CRYP_AES_ECB(MODE_DECRYPT,AES192key, 192,Ciphertext, AES_TEXT_SIZE,Decryptedtext) == SUCCESS) 
    {
      /* Display decrypted data*/
      Display_DecryptedData(ECB, 192,AES_TEXT_SIZE);
    }
/****************************************/
/*                           AES 256   **/
/****************************************/
    /* Decrypt the plaintext message  */
    if(CRYP_AES_ECB(MODE_DECRYPT,AES256key, 256,Ciphertext, AES_TEXT_SIZE,Decryptedtext) == SUCCESS)
    {
      /* Display decrypted data*/
      Display_DecryptedData(ECB,256,AES_TEXT_SIZE);
    }

/******************************************************************************/
/*                             AES mode CBC                                   */
/******************************************************************************/
    PressToContinue();
/*=====================================================
  Encryption CBC mode                                        
======================================================*/

/****************************************/
/*                           AES 128   **/
/****************************************/
    /* Encrypt the plaintext message*/
    if(CRYP_AES_CBC(MODE_ENCRYPT,IV_1,AES128key,128,Plaintext,AES_TEXT_SIZE,Encryptedtext) == SUCCESS)
    {
      /* Display encrypted Data*/
      Display_EncryptedData(CBC,128,AES_TEXT_SIZE);
    }
/****************************************/
/*                           AES 192   **/
/****************************************/
    /* Encrypt the plaintext message*/
    if(CRYP_AES_CBC(MODE_ENCRYPT,IV_1,AES192key,192,Plaintext,AES_TEXT_SIZE,Encryptedtext) == SUCCESS)
    {
      /* Display encrypted Data*/
      Display_EncryptedData(CBC,192,AES_TEXT_SIZE);
    }
/****************************************/
/*                           AES 256   **/
/****************************************/
    /* Encrypt the plaintext message*/
    if(CRYP_AES_CBC(MODE_ENCRYPT,IV_1,AES256key,256,Plaintext,AES_TEXT_SIZE,Encryptedtext) == SUCCESS)
    {
      /* Display encrypted Data*/
      Display_EncryptedData(CBC, 256,AES_TEXT_SIZE);
    }
/*=====================================================
    Decryption in CBC mode                                       
======================================================*/
    PressToContinue();
    /* Deinitializes the CRYP peripheral */
    CRYP_DeInit();
/****************************************/
/*                           AES 128   **/
/****************************************/
    /* Decrypt the plaintext message  */
    if(CRYP_AES_CBC(MODE_DECRYPT,IV_1,AES128key,128,Ciphertext,AES_TEXT_SIZE,Decryptedtext) == SUCCESS)
    {
      /* Display decrypted data*/
      Display_DecryptedData(CBC,128,AES_TEXT_SIZE);
    }
/****************************************/
/*                           AES 192   **/
/****************************************/
    /* Deinitializes the CRYP peripheral */
    CRYP_DeInit();

    /* Decrypt the plaintext message  */
    if(CRYP_AES_CBC(MODE_DECRYPT,IV_1,AES192key, 192,Ciphertext, AES_TEXT_SIZE,Decryptedtext) == SUCCESS)
    {
      /* Display decrypted data*/
      Display_DecryptedData(CBC, 192,AES_TEXT_SIZE);
    }
/****************************************/
/*                           AES 256   **/
/****************************************/
    /* Deinitializes the CRYP peripheral */
    CRYP_DeInit();

    /* Decrypt the plaintext message  */
    if(CRYP_AES_CBC(MODE_DECRYPT,IV_1,AES256key, 256,Ciphertext, AES_TEXT_SIZE,Decryptedtext) == SUCCESS)
    {
      /* Display decrypted data*/
      Display_DecryptedData(CBC,256,AES_TEXT_SIZE);
    }

/******************************************************************************/
/*                             AES mode CTR                                   */
/******************************************************************************/

/*=====================================================
  Encryption CTR mode                                        
======================================================*/
   PressToContinue();
/****************************************/
/*                           AES 128   **/
/****************************************/
    /* Encrypt the plaintext message*/
    if(CRYP_AES_CTR(MODE_ENCRYPT,IV_1,AES128key,128,Plaintext,AES_TEXT_SIZE,Encryptedtext) == SUCCESS)
    {
      /* Display encrypted Data*/
      Display_EncryptedData(CTR,128, AES_TEXT_SIZE);
    }
/****************************************/
/*                           AES 192   **/
/****************************************/
    /* Encrypt the plaintext message*/
    if(CRYP_AES_CTR(MODE_ENCRYPT,IV_1,AES192key,192,Plaintext,AES_TEXT_SIZE,Encryptedtext) == SUCCESS)
    {
      /* Display encrypted Data*/
      Display_EncryptedData(CTR,192, AES_TEXT_SIZE);
    }
/****************************************/
/*                           AES 256   **/
/****************************************/
    /* Encrypt the plaintext message*/
    if(CRYP_AES_CTR(MODE_ENCRYPT,IV_1,AES256key,256,Plaintext,AES_TEXT_SIZE,Encryptedtext) == SUCCESS)
    {
      /* Display encrypted Data*/
      Display_EncryptedData(CTR, 256, AES_TEXT_SIZE);
    }
/*=====================================================
    Decryption in CTR mode                                       
======================================================*/
   PressToContinue();
/****************************************/
/*                           AES 128   **/
/****************************************/
    /* Decrypt the plaintext message  */
    if(CRYP_AES_CTR(MODE_DECRYPT,IV_1,AES128key,128,Ciphertext,AES_TEXT_SIZE,Decryptedtext) == SUCCESS)
    {
      /* Display decrypted data*/
      Display_DecryptedData(CTR, 128, AES_TEXT_SIZE);
    }
/****************************************/
/*                           AES 192   **/
/****************************************/
    /* Decrypt the plaintext message  */
    if(CRYP_AES_CTR(MODE_DECRYPT,IV_1,AES192key,192,Ciphertext,AES_TEXT_SIZE,Decryptedtext) == SUCCESS)
    {
      /* Display decrypted data*/
      Display_DecryptedData(CTR, 192, AES_TEXT_SIZE);
    }
/****************************************/
/*                           AES 256   **/
/****************************************/
    /* Decrypt the plaintext message  */
    if(CRYP_AES_CTR(MODE_DECRYPT,IV_1,AES256key, 256,Ciphertext, AES_TEXT_SIZE,Decryptedtext) == SUCCESS)
    {
      /* Display decrypted data*/
      Display_DecryptedData(CTR, 256, AES_TEXT_SIZE);
    }
/******************************************************************************/
    PressToContinue();
    printf("\n\r Example restarted...\n ");
  }
}
コード例 #6
0
/**
  * @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 ");   
  }
}
コード例 #7
0
/**
  * @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 168 MHz */
  SystemClock_Config();
  
  /* 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 common CRYP parameters */
  CrypHandle.Instance = CRYP;
  
  CrypHandle.Init.DataType = CRYP_DATATYPE_8B;
  CrypHandle.Init.KeySize  = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey     = aDESKey;
 
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  /* Display Plain Data */
  Display_PlainData(PLAINTEXT_SIZE);
  
  /*##-2- Start Encryption ###################################################*/
  
  /****************************************************************************/
  /*                             DES mode ECB                                 */
  /****************************************************************************/
  
  /*=====================================================
  Encryption ECB mode                                        
  ======================================================*/
  /* Encrypt the plaintext message */
  if(HAL_CRYP_DESECB_Encrypt(&CrypHandle, aPlaintext, 16, aEncryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted data */
    Display_EncryptedData(DES, ECB, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*=====================================================
  Decryption ECB mode                                        
  ======================================================*/
  /* ReInitialize the DES Key */
  CrypHandle.Init.pKey = aDESKey;
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  /* Decrypt the Encrypted message */
  if(HAL_CRYP_DESECB_Decrypt(&CrypHandle, aEncryptedText, 16, aDecryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted data */
    Display_DecryptedData(DES, ECB, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  
  /****************************************************************************/
  /*                             DES mode CBC                                 */
  /****************************************************************************/
  /* Insert the Initialization Vector & reInitialize the DES Key */
  CrypHandle.Init.pKey      = aDESKey;
  CrypHandle.Init.pInitVect = aInitVector;
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }  
  /*=====================================================
  Encryption CBC mode                                        
  ======================================================*/
  /* Encrypt the plaintext message */
  if(HAL_CRYP_DESCBC_Encrypt(&CrypHandle, aPlaintext, 16, aEncryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted data */
    Display_EncryptedData(DES,CBC,PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*=====================================================
  Decryption CBC mode                                        
  ======================================================*/
  /* ReInitialize the DES Key */
  CrypHandle.Init.pKey = aDESKey;
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  /* Decrypt the Encrypted message */
  if(HAL_CRYP_DESCBC_Decrypt(&CrypHandle, aEncryptedText, 16, aDecryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted data */
    Display_DecryptedData(DES, CBC, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  
  /****************************************************************************/
  /*                             TDES mode ECB                                */
  /****************************************************************************/
  /* ReInitialize the DES Key */
  CrypHandle.Init.pKey = aTDESKey; 
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  /*=====================================================
  Encryption ECB mode                                        
  ======================================================*/
  /* Encrypt the plaintext message */
  if(HAL_CRYP_TDESECB_Encrypt(&CrypHandle, aPlaintext, 16, aEncryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted data */
    Display_EncryptedData(TDES, ECB, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*=====================================================
  Decryption ECB mode                                        
  ======================================================*/
  /* ReInitialize the DES Key */
  CrypHandle.Init.pKey = aTDESKey;
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  /* Decrypt the Encrypted message */
  if(HAL_CRYP_TDESECB_Decrypt(&CrypHandle, aEncryptedText, 16, aDecryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted data*/
    Display_DecryptedData(TDES, ECB, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  
  /****************************************************************************/
  /*                             TDES mode CBC                                */
  /****************************************************************************/
  /* Insert the Initialization Vector & reInitialize the DES Key */
  CrypHandle.Init.pKey      = aTDESKey;
  CrypHandle.Init.pInitVect = aInitVector;
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }  
  /*=====================================================
  Encryption CBC mode                                        
  ======================================================*/
  
  /* Encrypt the plaintext message */
  if(HAL_CRYP_TDESCBC_Encrypt(&CrypHandle, aPlaintext, 16, aEncryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted data */
    Display_EncryptedData(TDES, CBC, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*=====================================================
  Decryption CBC mode                                        
  ======================================================*/
  /* Insert the Initialization Vector & reInitialize the TDES Key */
  CrypHandle.Init.pKey      = aTDESKey;
  CrypHandle.Init.pInitVect = aInitVector;
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }  
  /* Decrypt the Encrypted message */
  if(HAL_CRYP_TDESCBC_Decrypt(&CrypHandle, aEncryptedText, 16, aDecryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted data */
    Display_DecryptedData(TDES, CBC, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  
  /* Infinite loop */   
  while(1)
  {
  }
}
コード例 #8
0
ファイル: main.c プロジェクト: pengphei/STM32Cube_L0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32L0xx HAL library initialization:
       - Configure the Flash prefetch, Flash preread and Buffer 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.
       - Low Level Initialization
     */
  HAL_Init();

  /* Configure the system clock to 2 MHz */
  SystemClock_Config();

#if defined(TERMINAL_IO_OUT)
  /* Configure Key Button */
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
#else 
  /* 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);
#endif

  /* Configures LED */
  BSP_LED_Init(LED2);

  /*##- Configure the CRYP peripheral ######################################*/
  /* Set the common CRYP parameters */
  CrypHandle.Instance = AES;
  CrypHandle.Init.DataType = CRYP_DATATYPE_8B;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Infinite loop */
  while (1)
  {

  /* Display Plain Data*/
  Display_PlainData(AES_TEXT_SIZE);
    
  /* Display Cypher Data*/
  Display_CypherData(AES_TEXT_SIZE);

#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif
 
  BSP_LED_Off(LED2);


    /******************************************************************************/
    /*                             AES mode ECB                                   */
    /******************************************************************************/

    if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
    {
      Error_Handler();
    }

    /*=====================================================
        Encryption ECB mode
    ======================================================*/

    /*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Instance = AES;
    CrypHandle.Init.pKey          = aAES128key;

    if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESECB_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(ECB, 128, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler();
    }
    
    /* Compare the encrypted text with the expected one *************************/ 
    data_cmp(aEncryptedtext, aEncryptedtextECB, AES_TEXT_SIZE); 
	


#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif

    /*=====================================================
        Decryption ECB mode
    ======================================================*/
    if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
    {
      Error_Handler();
    }
    /*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Instance = AES;
    CrypHandle.Init.pKey    = aAES128key;

    if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESECB_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(ECB, 128, AES_TEXT_SIZE);
    }
    else
    {
      /* Processing Error */
      Error_Handler();
    }
    /* Check the encrypted text with the expected one *************************/ 
    data_cmp(aDecryptedtext, aDecryptedtextECB, AES_TEXT_SIZE); 



#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif

    /******************************************************************************/
    /*                             AES mode CBC                                   */
    /******************************************************************************/

    /*=====================================================
        Encryption CBC mode
    ======================================================*/
    if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
    {
      Error_Handler();
    }
    /*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Instance = AES;
    CrypHandle.Init.pKey      = aAES128key;
    CrypHandle.Init.pInitVect = aInitVector;

    if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESCBC_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(CBC, 128, AES_TEXT_SIZE);
    }
    else  
    {
      /* Processing Error */
      Error_Handler();
    }
    /* Check the encrypted text with the expected one *************************/ 
    data_cmp(aEncryptedtext, aCyphertext, AES_TEXT_SIZE); 



#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif

    /*=====================================================
        Decryption CBC mode
    ======================================================*/
    if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
    {
      Error_Handler();
    }
    /*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Instance = AES;
    CrypHandle.Init.pKey      = aAES128key;
    CrypHandle.Init.pInitVect = aInitVector;

    if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESCBC_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(CBC, 128, AES_TEXT_SIZE);
    }
    else   
    {
      /* Processing Error */
      Error_Handler();
    }
    /* Check the encrypted text with the expected one *************************/ 
    data_cmp(aDecryptedtext, aDecryptedtextCBC, AES_TEXT_SIZE); 



#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif

    /******************************************************************************/
    /*                             AES mode CTR                                   */
    /******************************************************************************/

    /*=====================================================
        Encryption CTR mode
    ======================================================*/
    if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
    {
      Error_Handler();
    }
    /*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Instance = AES;
    CrypHandle.Init.pKey      = aAES128key;
    CrypHandle.Init.pInitVect = aInitVector;

    if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }

    /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
    if (HAL_CRYP_AESCTR_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(CTR, 128, AES_TEXT_SIZE);
    }
    else     
    {
      /* Processing Error */
      Error_Handler();
    }
    
    /* Check the encrypted text with the expected one *************************/ 
    data_cmp(aEncryptedtext, aEncryptedtextCTR, AES_TEXT_SIZE); 



#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif
    /*=====================================================
        Decryption CTR mode
    ======================================================*/
    if (HAL_CRYP_DeInit(&CrypHandle) != HAL_OK)
    {
      Error_Handler();
    }
    /*****************  AES 128   ****************/
    /* Initialize the CRYP peripheral */
    CrypHandle.Instance = AES;
    CrypHandle.Init.pKey      = aAES128key;
    CrypHandle.Init.pInitVect = aInitVector;

    if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler();
    }

    /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
    if (HAL_CRYP_AESCTR_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display decrypted Data */
      Display_DecryptedData(CTR, 128, AES_TEXT_SIZE);
    }
    else  
    {
      /* Processing Error */
      Error_Handler();

	  }

    /* Check the encrypted text with the expected one *************************/ 
    data_cmp(aDecryptedtext, aDecryptedtextCTR, AES_TEXT_SIZE); 
	



#if defined(TERMINAL_IO_OUT)
  printf("\n\r Press User button to continue...\n\r ");
  /* Wait until Key button is pressed to enter the next mode */
  while(BSP_PB_GetState(BUTTON_KEY) != RESET){}
  /* Loop while Key button is maintained pressed */
  while(BSP_PB_GetState(BUTTON_KEY) == RESET){}
#else 
    PressToContinue();
#endif

  BSP_LED_On(LED2);


    printf("\n\r Example restarted...\n ");


  }
}
コード例 #9
0
ファイル: main.c プロジェクト: XDeca/LED_POV
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)
       before to branch to application main.
     */     
       
  /* USARTx configured as follows:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART_Config();

  /* Display Plain Data */
  Display_PlainData(PLAIN_TEXT_SIZE);

  /* Enable CRYP clock */
  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE);

/******************************************************************************/
/*                             DES mode ECB                                   */
/******************************************************************************/

/*=====================================================
  Encryption ECB mode                                        
======================================================*/

  /* Encrypt the plaintext message */
  if(CRYP_DES_ECB(MODE_ENCRYPT,DESkey,Plaintext,PLAIN_TEXT_SIZE,Encryptedtext) == SUCCESS)
  {
    /* Display encrypted Data*/
    Display_EncryptedData(DES,ECB,PLAIN_TEXT_SIZE);
  }

/*=====================================================
    Decryption in ECB mode                                       
======================================================*/

  /* Decrypt the plaintext message  */
  if(CRYP_DES_ECB(MODE_DECRYPT,DESkey,Encryptedtext,PLAIN_TEXT_SIZE,Decryptedtext) == SUCCESS)
  {
    /* Display decrypted data*/
    Display_DecryptedData(DES,ECB,PLAIN_TEXT_SIZE);
  }

/******************************************************************************/
/*                             DES mode CBC                                   */
/******************************************************************************/

/*=====================================================
  Encryption CBC mode                                        
======================================================*/

  /* Encrypt the plaintext message*/
  if(CRYP_DES_CBC(MODE_ENCRYPT,DESkey,IV_1,Plaintext,PLAIN_TEXT_SIZE,Encryptedtext) == SUCCESS)
  {
    /* Display encrypted Data*/
    Display_EncryptedData(DES,CBC,PLAIN_TEXT_SIZE);
  }
/*=====================================================
    Decryption in CBC mode                                       
======================================================*/

  /* Decrypt the plaintext message  */
  if(CRYP_DES_CBC(MODE_DECRYPT,DESkey,IV_1,Encryptedtext,PLAIN_TEXT_SIZE,Decryptedtext) == SUCCESS)
  {
    /* Display decrypted data*/
    Display_DecryptedData(DES,CBC,PLAIN_TEXT_SIZE);
  }

/******************************************************************************/
/*                             TDES mode ECB                                  */
/******************************************************************************/

/*=====================================================
  Encryption ECB mode                                        
======================================================*/

  /* Encrypt the plaintext message*/
  if(CRYP_TDES_ECB(MODE_ENCRYPT,TDESkey,Plaintext,PLAIN_TEXT_SIZE,Encryptedtext) == SUCCESS)
  {
    /* Display encrypted Data*/
    Display_EncryptedData(TDES,ECB,PLAIN_TEXT_SIZE);
  }
/*=====================================================
    Decryption in ECB mode                                       
======================================================*/

  /* Decrypt the plaintext message  */
  if(CRYP_TDES_ECB(MODE_DECRYPT,TDESkey,Encryptedtext,PLAIN_TEXT_SIZE,Decryptedtext) == SUCCESS)
  {
    /* Display decrypted data*/
    Display_DecryptedData(TDES,ECB,PLAIN_TEXT_SIZE);
  }

/******************************************************************************/
/*                             TDES mode CBC                                  */
/******************************************************************************/

/*=====================================================
  Encryption CBC mode                                        
======================================================*/

  /* Encrypt the plaintext message*/
  if(CRYP_TDES_CBC(MODE_ENCRYPT,TDESkey,IV_1,Plaintext,PLAIN_TEXT_SIZE,Encryptedtext) == SUCCESS)
  {
    /* Display encrypted Data*/
    Display_EncryptedData(TDES,CBC,PLAIN_TEXT_SIZE);
  }
/*=====================================================
    Decryption in CBC mode                                       
======================================================*/

  /* Decrypt the plaintext message  */
  if(CRYP_TDES_CBC(MODE_DECRYPT,TDESkey,IV_1,Encryptedtext,PLAIN_TEXT_SIZE,Decryptedtext) == SUCCESS)
  {
    /* Display decrypted data*/
    Display_DecryptedData(TDES,CBC,PLAIN_TEXT_SIZE);  
  }
/******************************************************************************/

  while(1);  
}
コード例 #10
0
ファイル: main.c プロジェクト: Lembed/STM32CubeF4-mirrors
/**
  * @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 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 ######################################*/
  CrypHandle.Instance = CRYP;
  /* Set the common CRYP parameters */
  CrypHandle.Init.DataType = CRYP_DATATYPE_8B;
  CrypHandle.Init.KeySize  = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey     = aDESKey;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Display Plain Data */
  Display_PlainData(PLAINTEXT_SIZE);

  /*##-2- Start Encryption ###################################################*/

  /****************************************************************************/
  /*                             DES mode ECB                                 */
  /****************************************************************************/

  /*=====================================================
  Encryption ECB mode
  ======================================================*/
  /* Encrypt the plaintext message */
  if (HAL_CRYP_DESECB_Encrypt(&CrypHandle, aPlaintext, 16, aEncryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted data */
    Display_EncryptedData(DES, ECB, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*=====================================================
  Decryption ECB mode
  ======================================================*/
  /* ReInitialize the DES Key */
  CrypHandle.Init.pKey = aDESKey;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Decrypt the Encrypted message */
  if (HAL_CRYP_DESECB_Decrypt(&CrypHandle, aEncryptedText, 16, aDecryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted data */
    Display_DecryptedData(DES, ECB, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  /****************************************************************************/
  /*                             DES mode CBC                                 */
  /****************************************************************************/
  /* Insert the Initialization Vector & reInitialize the DES Key */
  CrypHandle.Init.pKey      = aDESKey;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  /*=====================================================
  Encryption CBC mode
  ======================================================*/
  /* Encrypt the plaintext message */
  if (HAL_CRYP_DESCBC_Encrypt(&CrypHandle, aPlaintext, 16, aEncryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted data */
    Display_EncryptedData(DES, CBC, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*=====================================================
  Decryption CBC mode
  ======================================================*/
  /* ReInitialize the DES Key */
  CrypHandle.Init.pKey = aDESKey;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  /* Decrypt the Encrypted message */
  if (HAL_CRYP_DESCBC_Decrypt(&CrypHandle, aEncryptedText, 16, aDecryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted data */
    Display_DecryptedData(DES, CBC, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  /****************************************************************************/
  /*                             TDES mode ECB                                */
  /****************************************************************************/
  /* ReInitialize the DES Key */
  CrypHandle.Init.pKey = aTDESKey;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  /*=====================================================
  Encryption ECB mode
  ======================================================*/
  /* Encrypt the plaintext message */
  if (HAL_CRYP_TDESECB_Encrypt(&CrypHandle, aPlaintext, 16, aEncryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted data */
    Display_EncryptedData(TDES, ECB, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*=====================================================
  Decryption ECB mode
  ======================================================*/
  /* ReInitialize the DES Key */
  CrypHandle.Init.pKey = aTDESKey;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  /* Decrypt the Encrypted message */
  if (HAL_CRYP_TDESECB_Decrypt(&CrypHandle, aEncryptedText, 16, aDecryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted data*/
    Display_DecryptedData(TDES, ECB, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  /****************************************************************************/
  /*                             TDES mode CBC                                */
  /****************************************************************************/
  /* Insert the Initialization Vector & reInitialize the DES Key */
  CrypHandle.Init.pKey      = aTDESKey;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  /*=====================================================
  Encryption CBC mode
  ======================================================*/

  /* Encrypt the plaintext message */
  if (HAL_CRYP_TDESCBC_Encrypt(&CrypHandle, aPlaintext, 16, aEncryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted data */
    Display_EncryptedData(TDES, CBC, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*=====================================================
  Decryption CBC mode
  ======================================================*/
  /* Insert the Initialization Vector & reInitialize the TDES Key */
  CrypHandle.Init.pKey      = aTDESKey;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  /* Decrypt the Encrypted message */
  if (HAL_CRYP_TDESCBC_Decrypt(&CrypHandle, aEncryptedText, 16, aDecryptedText, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted data */
    Display_DecryptedData(TDES, CBC, PLAINTEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  printf("\n\r *** TEST ENDED *** \n\r");

  /* Infinite loop */
  while (1)
  {
  }
}
コード例 #11
0
ファイル: main.c プロジェクト: vlsi1217/STM32F7Cube
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* Enable the CPU Cache */
  CPU_CACHE_Enable();

  /* STM32F7xx HAL library initialization:
       - Configure the Flash ART accelerator on ITCM interface
       - 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
     */
  HAL_Init();

  /* Configure the system clock to 216 MHz */
  SystemClock_Config();

  /* 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 ######################################*/
  CrypHandle.Instance = CRYP;
  /* Set the common CRYP parameters */
  CrypHandle.Init.DataType = CRYP_DATATYPE_8B;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Display Plain Data*/
  Display_PlainData(AES_TEXT_SIZE);

  PressToContinue();

  /******************************************************************************/
  /*                             AES mode ECB                                   */
  /******************************************************************************/

  /*=====================================================
        Encryption ECB mode
    ======================================================*/

  printf("*** Encryption AES ECB mode *** \r ");

  /*****************  AES 128   ****************/

  printf("-- AES 128 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey    = aAES128key;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
  if (HAL_CRYP_AESECB_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted Data */
    Display_EncryptedData(ECB, 128, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*****************  AES 192   ****************/

  printf("-- AES 192 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize = CRYP_KEYSIZE_192B;
  CrypHandle.Init.pKey    = aAES192key;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
  if (HAL_CRYP_AESECB_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted Data */
    Display_EncryptedData(ECB, 192, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*****************  AES 256   ****************/

  printf("-- AES 256 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize = CRYP_KEYSIZE_256B;
  CrypHandle.Init.pKey    = aAES256key;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
  if (HAL_CRYP_AESECB_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted Data */
    Display_EncryptedData(ECB, 256, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  PressToContinue();

  /*=====================================================
        Decryption ECB mode
    ======================================================*/

  printf("*** Decryption AES ECB mode *** \r ");

  /*****************  AES 128   ****************/

  printf("-- AES 128 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey    = aAES128key;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
  if (HAL_CRYP_AESECB_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted Data */
    Display_DecryptedData(ECB, 128, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  /*****************  AES 192   ****************/

  printf("-- AES 192 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize = CRYP_KEYSIZE_192B;
  CrypHandle.Init.pKey    = aAES192key;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
  if (HAL_CRYP_AESECB_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted Data */
    Display_DecryptedData(ECB, 192, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*****************  AES 256   ****************/

  printf("-- AES 256 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize = CRYP_KEYSIZE_256B;
  CrypHandle.Init.pKey    = aAES256key;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
  if (HAL_CRYP_AESECB_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted Data */
    Display_DecryptedData(ECB, 256, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  PressToContinue();

  /******************************************************************************/
  /*                             AES mode CBC                                   */
  /******************************************************************************/

  /*=====================================================
        Encryption CBC mode
    ======================================================*/

  printf("*** Encryption AES CBC mode *** \r ");

  /*****************  AES 128   ****************/

  printf("-- AES 128 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize   = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey      = aAES128key;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
  if (HAL_CRYP_AESCBC_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted Data */
    Display_EncryptedData(CBC, 128, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*****************  AES 192   ****************/

  printf("-- AES 192 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize   = CRYP_KEYSIZE_192B;
  CrypHandle.Init.pKey      = aAES192key;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
  if (HAL_CRYP_AESCBC_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted Data */
    Display_EncryptedData(CBC, 192, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*****************  AES 256   ****************/

  printf("-- AES 256 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize   = CRYP_KEYSIZE_256B;
  CrypHandle.Init.pKey      = aAES256key;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
  if (HAL_CRYP_AESCBC_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted Data */
    Display_EncryptedData(CBC, 256, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  PressToContinue();

  /*=====================================================
        Decryption CBC mode
    ======================================================*/

  printf("*** Decryption AES CBC mode *** \r ");

  /*****************  AES 128   ****************/

  printf("-- AES 128 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize   = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey      = aAES128key;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
  if (HAL_CRYP_AESCBC_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted Data */
    Display_DecryptedData(CBC, 128, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  /*****************  AES 192   ****************/

  printf("-- AES 192 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize   = CRYP_KEYSIZE_192B;
  CrypHandle.Init.pKey      = aAES192key;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
  if (HAL_CRYP_AESCBC_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted Data */
    Display_DecryptedData(CBC, 192, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  /*****************  AES 256   ****************/

  printf("-- AES 256 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize   = CRYP_KEYSIZE_256B;
  CrypHandle.Init.pKey      = aAES256key;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
  if (HAL_CRYP_AESCBC_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted Data */
    Display_DecryptedData(CBC, 256, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  PressToContinue();

  /******************************************************************************/
  /*                             AES mode CTR                                   */
  /******************************************************************************/

  /*=====================================================
        Encryption CTR mode
    ======================================================*/

  printf("*** Encryption AES CTR mode *** \r ");

  /*****************  AES 128   ****************/

  printf("-- AES 128 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize   = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey      = aAES128key;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
  if (HAL_CRYP_AESCTR_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted Data */
    Display_EncryptedData(CTR, 128, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }
  /*****************  AES 192   ****************/

  printf("-- AES 192 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize   = CRYP_KEYSIZE_192B;
  CrypHandle.Init.pKey      = aAES192key;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
  if (HAL_CRYP_AESCTR_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted Data */
    Display_EncryptedData(CTR, 192, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  /*****************  AES 256   ****************/

  printf("-- AES 256 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize   = CRYP_KEYSIZE_256B;
  CrypHandle.Init.pKey      = aAES256key;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start encrypting aPlaintext, the cypher data is available in aEncryptedtext */
  if (HAL_CRYP_AESCTR_Encrypt(&CrypHandle, aPlaintext, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display encrypted Data */
    Display_EncryptedData(CTR, 256, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  PressToContinue();

  /*=====================================================
        Decryption CTR mode
    ======================================================*/

  printf("*** Decryption AES CTR mode *** \r ");

  /*****************  AES 128   ****************/

  printf("-- AES 128 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize   = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey      = aAES128key;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
  if (HAL_CRYP_AESCTR_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted Data */
    Display_DecryptedData(CTR, 128, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  /*****************  AES 192   ****************/

  printf("-- AES 192 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize   = CRYP_KEYSIZE_192B;
  CrypHandle.Init.pKey      = aAES192key;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
  if (HAL_CRYP_AESCTR_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted Data */
    Display_DecryptedData(CTR, 192, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  /*****************  AES 256   ****************/

  printf("-- AES 256 --\r ");

  /* Initialize the CRYP peripheral */
  CrypHandle.Init.KeySize   = CRYP_KEYSIZE_256B;
  CrypHandle.Init.pKey      = aAES256key;
  CrypHandle.Init.pInitVect = aInitVector;

  if (HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Start decrypting aCyphertext, the decrypted data is available in aDecryptedtext */
  if (HAL_CRYP_AESCTR_Decrypt(&CrypHandle, aCyphertext, AES_TEXT_SIZE, aDecryptedtext, TIMEOUT_VALUE) == HAL_OK)
  {
    /* Display decrypted Data */
    Display_DecryptedData(CTR, 256, AES_TEXT_SIZE);
  }
  else
  {
    /* Processing Error */
    Error_Handler();
  }

  PressToContinue();

  printf("\n\r *** TEST ENDED. *** \n ");

  while(1)
  {
    ;
  }
}
コード例 #12
0
ファイル: main.c プロジェクト: pierreroth64/STM32Cube_FW_F4
/**
  * @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 ");   
  }
}