コード例 #1
0
ファイル: main.c プロジェクト: dazuo78/TBall
/**
  * @brief  Configures the USARTx and associated pins.
  * @param  None
  * @retval None
  */
void UART_Config(void)
{
  /* UARTx 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
  */
  huart.Init.BaudRate   = 115200;
  huart.Init.Mode       = UART_MODE_TX_RX;
  huart.Init.Parity     = UART_PARITY_NONE;
  huart.Init.StopBits   = UART_STOPBITS_1;
  huart.Init.WordLength = UART_WORDLENGTH_8B; 
  huart.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  BSP_COM_Init(COM1, &huart);
}
コード例 #2
0
ファイル: main.c プロジェクト: PaxInstruments/STM32CubeF4
/**
  * @brief  Initialize the IAP: Configure USART.
  * @param  None
  * @retval None
  */
static void IAP_Init(void)
{
  /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/
  /* USART 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
  */
  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_RX | UART_MODE_TX;

  BSP_COM_Init(COM1, &UartHandle);
}
コード例 #3
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 ");    
  } 
}
コード例 #4
0
ファイル: main.c プロジェクト: acrepina/STM32F7_serverWEB
/**
  * @brief  Initializes the STM32756G-EVAL's LCD and LEDs resources.
  * @param  None
  * @retval None
  */
static void BSP_Config(void)
{
  /* Initialize RNG peripheral */
  RngHandle.Instance = RNG;
  HAL_RNG_Init(&RngHandle);

  /* UART configuration */
  UartHandle.Instance          = USART1;
  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;
  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
    
  /* Initialize UART peripheral */
  HAL_UART_Init(&UartHandle);
  
  /* Configures COM1 port */
  BSP_COM_Init(COM1, &UartHandle);
  
  /* Configure LED1, LED2, LED3 and LED4 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
  
  /* Init IO Expander */
  BSP_IO_Init();
  
  /* Enable IO Expander interrupt for ETH MII pin */
  BSP_IO_ConfigPin(MII_INT_PIN, IO_MODE_IT_FALLING_EDGE);
  
#ifdef USE_LCD

  /* Initialize the LCD */
  BSP_LCD_Init();
  
  /* Initialize the LCD Layers */
  BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS);
  
  /* Set LCD Foreground Layer  */
  BSP_LCD_SelectLayer(1);
  
  BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
  
  /* Clear the LCD */
  BSP_LCD_Clear(LCD_COLOR_BLACK);
  
  /* Set the LCD Back Color */
  BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
  
  /* Set the LCD Text Color */
  BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
  
  /* Display message on the LCD*/
  BSP_LCD_DisplayStringAtLine(0, (uint8_t*)"        STM32F756xx          ");
  BSP_LCD_DisplayStringAtLine(1, (uint8_t*)"      STM32F-7 Series        ");
  BSP_LCD_DisplayStringAtLine(2, (uint8_t*)"      SSL Server demo        ");
  BSP_LCD_DisplayStringAtLine(3, (uint8_t*)"      using HW Crypto        ");

#endif
}
コード例 #5
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)
  {
  }
}
コード例 #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
ファイル: main.c プロジェクト: z80/stm32f429
/**
  * @brief  Initializes the STM32479I-EVAL's LCD and LEDs resources.
  * @param  None
  * @retval None
  */
static void BSP_Config(void)
{
#ifdef USE_LCD
  uint8_t lcd_status = LCD_OK;
  uint8_t  sdram_status = SDRAM_OK;
#endif /* USE_LCD */

  /* Initialize RNG peripheral */
  HAL_RNG_Init(&RngHandle);

  /* UART configuration */
  UartHandle.Instance          = USART1;
  UartHandle.Init.BaudRate     = 9600;
  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;
  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
    
  /* Initialize UART peripheral */
  HAL_UART_Init(&UartHandle);
  
  /* Configures COM1 port */
  BSP_COM_Init(COM1, &UartHandle);

  /* Configure LED1, LED2, LED3 and LED4 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);

  /* Set Systick Interrupt to the highest priority */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0xF, 0x0);

  /* Init IO Expander (MFX) */
  BSP_IO_Init();

  /* Enable IO Expander (MFX) interrupt for ETH MII pin */
  BSP_IO_ConfigPin(MII_INT_PIN, IO_MODE_IT_FALLING_EDGE);

#ifdef USE_LCD

  /* Initialize the SDRAM */
  sdram_status = BSP_SDRAM_Init();
  if(sdram_status != SDRAM_OK)
  {
	  Error_Handler();
  }

  /* Initialize LCD in landscape mode in DSI mode video burst */
  /* Initialize and start the LCD display in mode 'lcd_mode'
   *  Using LCD_FB_START_ADDRESS as frame buffer displayed contents.
   *  This buffer is modified by the BSP (draw fonts, objects depending on BSP calls).
   */

  /* Set Portrait orientation if needed, by default orientation is set to
     Landscape */
  
  /* Initialize DSI LCD */
  //  BSP_LCD_InitEx(LCD_ORIENTATION_PORTRAIT); /* uncomment if Portrait orientation is needed */
  BSP_LCD_Init(); /* Uncomment if default config (landscape orientation) is needed */
  while(lcd_status != LCD_OK);

  BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS);   

  /* Set LCD Foreground Layer as active one */
  BSP_LCD_SelectLayer(1);

  BSP_LCD_SetFont(&LCD_DEFAULT_FONT);

  /* Clear the LCD */
  BSP_LCD_Clear(LCD_COLOR_BLACK);
  
  /* Set the LCD Back Color */
  BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
  
  /* Set the LCD Text Color */
  BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
  
  /* Display message on the LCD*/
  BSP_LCD_DisplayStringAtLine(0, (uint8_t*)"                STM32F479xx          ");
  BSP_LCD_DisplayStringAtLine(1, (uint8_t*)"              STM32F-4 Series        ");
  BSP_LCD_DisplayStringAtLine(2, (uint8_t*)"              SSL Client demo        ");
  BSP_LCD_DisplayStringAtLine(3, (uint8_t*)"              using HW Crypto        ");
  BSP_LCD_DisplayStringAtLine(8, (uint8_t*)"  Ethernet Initialization ..."); 

#endif /* USE_LCD */
}
コード例 #8
0
ファイル: main.c プロジェクト: EarnestHein89/STM32Cube_FW_F4
/**
  * @brief  BSP configuration.
  * @param  None
  * @retval None
  */
static void BSP_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  
  /* Enable PB14 to IT mode: Ethernet Link interrupt */ 
  __GPIOB_CLK_ENABLE(); 
  GPIO_InitStructure.Pin = GPIO_PIN_14;
  GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStructure.Pull = GPIO_NOPULL ;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  /* Enable EXTI Line interrupt */
  HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0xF, 0);
  HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  
  /* Initialize RNG peripheral */
  HAL_RNG_Init(&RngHandle);
  
  /* UART configuration */
  UartHandle.Instance        = USART1;
  UartHandle.Init.BaudRate   = 9600;
  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;
  
  /* Initialize UART peripheral */
  HAL_UART_Init(&UartHandle);
  
  /* Configures COM1 port */
  BSP_COM_Init(COM1, &UartHandle);
  
  /* Initialize STM324x9I-EVAL's LEDs */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
  
#ifdef USE_LCD
  
  /* Initialize the LCD */
  BSP_LCD_Init();
  
  BSP_LCD_SetFont(&Font20);
  
  /* Clear the LCD */
  BSP_LCD_Clear(LCD_COLOR_BLACK);
  
  /* Set the LCD Back Color */
  BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
  
  /* Set the LCD Text Color */
  BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
  
  /* Display message on the LCD*/
  BSP_LCD_DisplayStringAtLine(0, (uint8_t*)"     STM32F417xx    ");
  BSP_LCD_DisplayStringAtLine(1, (uint8_t*)"   STM32F-4 Series  ");
  BSP_LCD_DisplayStringAtLine(2, (uint8_t*)"   SSL Server demo  ");
  BSP_LCD_DisplayStringAtLine(3, (uint8_t*)"   using HW Crypto  ");
  
#endif
}
コード例 #9
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 ");


  }
}
コード例 #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 プロジェクト: dazuo78/TBall
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
	float x,y,z;
    short sx,sy,sz;
    unsigned char regVal;
    unsigned char regVals[29]; //0x1D~0x39
	/* STM32F103xG HAL library initialization:
	   - Configure the Flash prefetch
	   - 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 72 MHz */
	SystemClock_Config();
    
    //uart
    BSP_COM_Init(COM1, &huart);

	/* Configure LED1 and LED3 */
	BSP_LED_Init(LED1);
	BSP_LED_Init(LED3);

	/* Configure Key push-button */
	BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);

	/*##-1- Configure the I2C peripheral ######################################*/
	ADXL345_Init(ADXL345_I2C_COMM);
  
   
    // full resolution
    ADXL345_SetRangeResolution(ADXL345_RANGE_PM_2G, 1); 
    
    // enter measurement mode
    ADXL345_SetPowerMode(1);
    
    // self test mode
//    regVal = ADXL345_GetRegisterValue(ADXL345_DATA_FORMAT);
//    regVal |= ADXL345_SELF_TEST;
//    ADXL345_SetRegisterValue(ADXL345_DATA_FORMAT, regVal);
    // dump ADXL345 registers
    HAL_Delay(1);
    //ADXL345_GetMultiRegisterValue(ADXL345_THRESH_TAP, sizeof(regVals), regVals);

	/* Infinite loop */  
    printf("\n\r UART Printf Example: retarget the C library printf function to the UART\n\r");
	while (1)
	{
        
        ADXL345_GetGxyz(&x, &y, &z);
        printf("\n\rx: %.1f, y: %.1f, z: %.1f\n\r", x, y, z);
       // ADXL345_GetXyz(&sx, &sy, &sz);
       // printf("\n\rx: %d, y: %d, z: %d\n\r", sx, sy, sz);
        HAL_Delay(1000);
	}
}
コード例 #12
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)
  {
    ;
  }
}
コード例 #13
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 ");   
  }
}