Example #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 LED1, LED2 and LED3 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);

  /*##-1- Configure the CRYP peripheral ######################################*/
  /* Set the CRYP parameters */
  CrypHandle.Init.DataType = CRYP_DATATYPE_8B;
  CrypHandle.Init.KeySize  = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey     = aTDESKey;

  if(HAL_CRYP_Init(&CrypHandle)!= HAL_OK)
  {
    /* Initialization process error */
    Error_Handler();  
  }
  
  /*##-2- Start the TDES encryption in ECB chaining mode #####################*/
  if (HAL_CRYP_TDESECB_Encrypt_DMA(&CrypHandle, aPlaintext, 48, aEncryptedtext) != HAL_OK)
  {
    /* Processing error */
    Error_Handler();  
  }
  while (HAL_CRYP_GetState(&CrypHandle) != HAL_CRYP_STATE_READY);
  
  /*##-3- Check the encrypted text with the expected one #####################*/
  if(memcmp(aEncryptedtext, aCyphertext, 48) != 0)
  {
    /* Wrong encryption: Turn LED2 on */
    BSP_LED_On(LED2);
  }
  else
  {
    /* Right encryption: Turn LED1 on */  
    BSP_LED_On(LED1);
  }
  
  /* Infinite loop */  
  while (1)
  {
  }
}
Example #2
0
/**
  * @brief  Decrypt pCypherData in AES CTR decryption mode, 
  *         the decyphered data are available in pPlainData.
  * @param  hcryp: pointer to a CRYP_HandleTypeDef structure that contains
  *         the configuration information for CRYP module
  * @param  pCypherData: Pointer to the cyphertext buffer
  * @param  Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
  * @param  pPlainData: Pointer to the plaintext buffer
  * @param  Timeout: Specify Timeout value
  * @note   This API is provided only to maintain compatibility with legacy software. Users should directly
  *         resort to generic HAL_CRYPEx_AES() API instead (usage recommended).     
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
{
  /* Re-initialize AES IP with proper parameters */
  if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
  {
    return HAL_ERROR;
  }
  hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
  hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
  hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
  if (HAL_CRYP_Init(hcryp) != HAL_OK)
  {
    return HAL_ERROR;
  }  
  
  return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
}
Example #3
0
static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsigned int keybits )
{
    switch( keybits ) {
        case 128:
            ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_128B;
            memcpy(ctx->aes_key, key, 16);
            break;
        case 192:
#if defined (TARGET_STM32L486xG)
            return(MBEDTLS_ERR_AES_INVALID_KEY_LENGTH);
#else
            ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_192B;
            memcpy(ctx->aes_key, key, 24);
            break;
#endif

        case 256:
            ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_256B;
            memcpy(ctx->aes_key, key, 32);
            break;
       default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
    }

    /* Deinitializes the CRYP peripheral */
    if (HAL_CRYP_DeInit(&ctx->hcryp_aes) == HAL_ERROR)
        return (HAL_ERROR);

    ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B;
    ctx->hcryp_aes.Instance = CRYP;
    /* Enable CRYP clock */
    __HAL_RCC_CRYP_CLK_ENABLE();

    ctx->hcryp_aes.Init.pKey = ctx->aes_key;
#if defined (TARGET_STM32L486xG)
    ctx->hcryp_aes.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
#endif
    if (HAL_CRYP_Init(&ctx->hcryp_aes) == HAL_ERROR)
        return (HAL_ERROR);

    /* allow multi-instance of CRYP use: save context for CRYP HW module CR */
    ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
    return(0);

}
Example #4
0
static int st_hal_cryp_cbc( mbedtls_aes_context *ctx, uint32_t opmode, size_t length, 
                            unsigned char iv[16], uint8_t *input, uint8_t *output) 
{
    int status = 0;
    ctx->hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init
    if ((ctx->hcryp_aes.Init.OperatingMode != opmode) || \
         (ctx->hcryp_aes.Init.ChainingMode != CRYP_CHAINMODE_AES_CBC) || \
         (ctx->hcryp_aes.Init.KeyWriteFlag != CRYP_KEY_WRITE_ENABLE)) {

        /* Re-initialize AES IP with proper parameters */
        if (HAL_CRYP_DeInit(&ctx->hcryp_aes) != HAL_OK)
            return HAL_ERROR;
        ctx->hcryp_aes.Init.OperatingMode = opmode;
        ctx->hcryp_aes.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
        ctx->hcryp_aes.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
        if (HAL_CRYP_Init(&ctx->hcryp_aes) != HAL_OK)
            return HAL_ERROR;
    }

    status =  HAL_CRYPEx_AES(&ctx->hcryp_aes, input, length, output, 10);

    return status;
}
Example #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 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 ");    
  } 
}
Example #6
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization: global MSP (MCU Support Package) initialization
     */
  HAL_Init();

  /* Configure the system clock to 180 MHz */
  SystemClock_Config();
 
  /* Initialize LED1, LED2, LED4 and LED3 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED4);
  BSP_LED_Init(LED3);
  
  /*##-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;
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler(); 
  }
  
  /*##-2- Start the AES encryption in ECB chaining mode with DMA #############*/
  if(HAL_CRYP_AESECB_Encrypt_DMA(&CrypHandle, aPlaintext, DATA_SIZE, aEncryptedText) != HAL_OK)
  {
    /* Processing Error */
    Error_Handler(); 
  }
  
  /*  Before starting a new process, you need to check the current state of the peripheral; 
      if it’s busy you need to wait for the end of current transfer before starting a new one.
      For simplicity reasons, this example is just waiting till the end of the 
      process, but application may perform other tasks while transfer operation
      is ongoing. */ 
  while (HAL_CRYP_GetState(&CrypHandle) != HAL_CRYP_STATE_READY)
  {
  }
  
  /*##-3- Check the encrypted text with the expected one #####################*/
  if(memcmp(aEncryptedText, aCyphertext, DATA_SIZE) != 0)
  {
    /* Wrong encryption: Turn LED2 on */
    BSP_LED_On(LED2);
  }
  else
  {
    /* Right encryption: Turn LED1 on */
    BSP_LED_On(LED1);
  }
  
  /* Deinitialize Crypto peripheral */
  HAL_CRYP_DeInit(&CrypHandle);
  
  /* Set the CRYP parameters */
  CrypHandle.Init.DataType = CRYP_DATATYPE_8B;
  CrypHandle.Init.KeySize  = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey     = aAES128Key;
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler(); 
  }
  
  /*##-4- Start the AES decryption in ECB chaining mode with DMA #############*/
  if(HAL_CRYP_AESECB_Decrypt_DMA(&CrypHandle, aEncryptedText, DATA_SIZE, aDecryptedText) != HAL_OK)
  {
    /* Processing Error */
    Error_Handler(); 
  }
  
  /*  Before starting a new process, you need to check the current state of the peripheral; 
      if it’s busy you need to wait for the end of current transfer before starting a new one.
      For simplicity reasons, this example is just waiting till the end of the 
      process, but application may perform other tasks while transfer operation
      is ongoing. */ 
  while (HAL_CRYP_GetState(&CrypHandle) != HAL_CRYP_STATE_READY)
  {
  }
  
  /*##-5- Check the decrypted text with the expected one #####################*/
  if(memcmp(aDecryptedText, aPlaintext, DATA_SIZE) != 0)
  {
    /* Wrong encryption: Turn LED2 on */
    BSP_LED_On(LED2);
  }
  else
  {
    /* Right encryption: Turn LED4 on */
    BSP_LED_On(LED4);
  }
   
  /* Infinite loop */
  while(1)
  {
  }
}
Example #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();

  /* Initialize LED1, LED2 and LED3 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  
  /*##-1- Configure the CRYP peripheral ######################################*/
  /* Set the CRYP parameters */
  CrypHandle.Init.DataType = CRYP_DATATYPE_8B;
  CrypHandle.Init.KeySize  = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey     = aAES128Key;
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler(); 
  }
  
  /*##-2- Start the AES encryption in ECB chaining mode with DMA #############*/
  if(HAL_CRYP_AESECB_Encrypt_DMA(&CrypHandle, aPlaintext, DATA_SIZE, aEncryptedText) != HAL_OK)
  {
    /* Processing Error */
    Error_Handler(); 
  }
  
  /*  Before starting a new process, you need to check the current state of the peripheral; 
      if it’s busy you need to wait for the end of current transfer before starting a new one.
      For simplicity reasons, this example is just waiting till the end of the 
      process, but application may perform other tasks while transfer operation
      is ongoing. */ 
  while (HAL_CRYP_GetState(&CrypHandle) != HAL_CRYP_STATE_READY)
  {
  }
  
  /*##-3- Check the encrypted text with the expected one #####################*/
  if(memcmp(aEncryptedText, aCyphertext, DATA_SIZE) != 0)
  {
    /* Wrong encryption: Turn LED2 on */
    BSP_LED_On(LED2);
  }
  else
  {
    /* Right encryption: Turn LED1 on */
    BSP_LED_On(LED1);
  }
  
  /* Deinitialize Crypto peripheral */
  HAL_CRYP_DeInit(&CrypHandle);
  
  /* Set the CRYP parameters */
  CrypHandle.Init.DataType = CRYP_DATATYPE_8B;
  CrypHandle.Init.KeySize  = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey     = aAES128Key;
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler(); 
  }
  
  /*##-4- Start the AES decryption in ECB chaining mode with DMA #############*/
  if(HAL_CRYP_AESECB_Decrypt_DMA(&CrypHandle, aEncryptedText, DATA_SIZE, aDecryptedText) != HAL_OK)
  {
    /* Processing Error */
    Error_Handler(); 
  }
  
  /*  Before starting a new process, you need to check the current state of the peripheral; 
      if it’s busy you need to wait for the end of current transfer before starting a new one.
      For simplicity reasons, this example is just waiting till the end of the 
      process, but application may perform other tasks while transfer operation
      is ongoing. */ 
  while (HAL_CRYP_GetState(&CrypHandle) != HAL_CRYP_STATE_READY)
  {
  }
  
  /*##-5- Check the decrypted text with the expected one #####################*/
  if(memcmp(aDecryptedText, aPlaintext, DATA_SIZE) != 0)
  {
    /* Wrong encryption: Turn LED2 on */
    BSP_LED_On(LED2);
  }
  else
  {
    /* Right encryption: Turn LED1 on */
    BSP_LED_On(LED1);
  }
  
  /* Infinite loop */
  while(1)
  {
  }
}
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 180 MHz */
  SystemClock_Config();
  
  /* Configure LED3 */
  BSP_LED_Init(LED3);  
  
  /* Configure the COM port */
  UartHandle.Init.BaudRate = 115200;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits = UART_STOPBITS_1;
  UartHandle.Init.Parity = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode = UART_MODE_TX_RX;
  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;  
  BSP_COM_Init(COM1, &UartHandle);
  
  /*##-1- Configure the CRYP peripheral ######################################*/
  /* Set the CRYP parameters */
  CrypHandle.Instance = CRYP;
  
  CrypHandle.Init.DataType   = CRYP_DATATYPE_8B;
  CrypHandle.Init.KeySize    = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey       = aAES128Key;
  CrypHandle.Init.pInitVect  = aInitVector;
  CrypHandle.Init.Header     = aHeaderMessage;
  CrypHandle.Init.HeaderSize = sizeof(aHeaderMessage);
  
  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler(); 
  }
  
  /* Infinite loop */
  while(1)
  {
    /* Display Plaintext */
    Display_Plaintext(PLAINTEXT_SIZE);
    
    /*##-2- Encryption Phase #################################################*/
    /* Set the Initialization vector */
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }
  
    /* Encrypt the plaintext message */
    if(HAL_CRYPEx_AESGCM_Encrypt(&CrypHandle, aPlaintext, 16, aEncryptedText, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_EncryptedData(AES_MODE_GCM, KEY_SIZE, PLAINTEXT_SIZE);
    }
    else 
    {
      /* Processing Error */
      Error_Handler();
    }
    
    /* Compute the authentication aTAG */
    if(HAL_CRYPEx_AESGCM_Finish(&CrypHandle, 16, aTAG, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display the computed aTAG, aTAG size is 16 bytes */
      Display_TAG(aTAG);
    }
    else 
    {
      /* Processing Error */
      Error_Handler();
    }

    /*##-3- Decryption Phase #################################################*/    
    /* Set the Initialization vector */
    CrypHandle.Init.pInitVect = aInitVector;
    
    if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
    {
      /* Initialization Error */
      Error_Handler(); 
    }
  
    /* Encrypt the plaintext message */
    if(HAL_CRYPEx_AESGCM_Decrypt(&CrypHandle, aCyphertext, 16, aDecryptedText, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display encrypted Data */
      Display_DecryptedData(AES_MODE_GCM, KEY_SIZE, PLAINTEXT_SIZE);
    }
    else 
    {
      /* Processing Error */
      Error_Handler();
    }
       
    /* Compute the authentication aTAG */
    if(HAL_CRYPEx_AESGCM_Finish(&CrypHandle, 16, aTAG, TIMEOUT_VALUE) == HAL_OK)
    {
      /* Display the computed aTAG, aTAG size is 16 bytes */
      Display_TAG(aTAG);
    }
    else 
    {
      /* Processing Error */
      Error_Handler();
    }    
    
    PressToContinue();
    printf("\n\r Example restarted...\n ");   
  }
}
Example #9
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();
  
  /* Initialize LED1, LED2 and LED3 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  
  /*##-1- Configure the CRYP peripheral ######################################*/
  /* Set the CRYP parameters */
  CrypHandle.Init.DataType   = CRYP_DATATYPE_8B;
  CrypHandle.Init.KeySize    = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey       = aAES128Key;
  CrypHandle.Init.pInitVect  = aNonce;
  CrypHandle.Init.IVSize     = uwNonceSize;
  CrypHandle.Init.Header     = aHeaderMessage;
  CrypHandle.Init.HeaderSize = 16;
  CrypHandle.Init.TagSize    = 6;
  CrypHandle.Init.pScratch   = aHBuffer;

  if(HAL_CRYP_Init(&CrypHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler(); 
  }
  
  /*##-2- Start the AES Encryption in CCM chaining mode ######################*/
  if(HAL_CRYPEx_AESCCM_Encrypt(&CrypHandle, aPlaintext, PLAINTEXT_SIZE, aOutputText, 0xFF) != HAL_OK)
  {
    /* Encryption Error */
    Error_Handler(); 
  }
  
  /*##-3- Compute the authentication TAG for AES CCM mode ####################*/ 
  if(HAL_CRYPEx_AESCCM_Finish(&CrypHandle, aMAC, 0xFF) != HAL_OK)
  {
    /* TAG Error */
    Error_Handler(); 
  }
  
  /*##-4- Compare the encrypted text and the aMAC with the expected ones #####*/
  if( (memcmp(aOutputText, aExpectedCyphertext, PLAINTEXT_SIZE) != 0) ||  (memcmp(aMAC, aExpectedMAC, MAC_SIZE) != 0))
  {
    /* Wrong encryption: Turn LED2 on */
    BSP_LED_On(LED2);
  }
  else
  {
    /* Right encryption: Turn LED1 on */
    BSP_LED_On(LED1);
  }
  
  /* Infinite loop */
  while(1)
  {
  }
}
/**
  * @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)
  {
  }
}
Example #11
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization: global MSP (MCU Support Package) initialization
     */
  HAL_Init();

  /* Configure the system clock to 180 MHz */
  SystemClock_Config();
 
  /* Initialize LED1, LED2 and LED3 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  
  /*##-1- Configure the CRYP peripheral ######################################*/
  CrypHandle.Instance = CRYP;
  
  /* Set the CRYP parameters */
  CrypHandle.Init.DataType = CRYP_DATATYPE_8B;
  CrypHandle.Init.KeySize  = CRYP_KEYSIZE_128B;
  CrypHandle.Init.pKey     = aTDESKey;

  if(HAL_CRYP_Init(&CrypHandle)!= HAL_OK)
  {
    /* Initialization process error */
    Error_Handler();  
  }
  
  /*##-2- Start the TDES encryption in ECB chaining mode #####################*/
  if (HAL_CRYP_TDESECB_Encrypt_DMA(&CrypHandle, aPlaintext, 48, aEncryptedtext) != HAL_OK)
  {
    /* Processing error */
    Error_Handler();  
  }
  while (HAL_CRYP_GetState(&CrypHandle) != HAL_CRYP_STATE_READY);
  
  /*##-3- Check the encrypted text with the expected one #####################*/
  if(memcmp(aEncryptedtext, aCyphertext, 48) != 0)
  {
    /* Wrong encryption: Turn LED2 on */
    BSP_LED_On(LED2);
  }
  else
  {
    /* Right encryption: Turn LED1 on */  
    BSP_LED_On(LED1);
  }

  /* Infinite loop */
  while(1)
  {
  }
}
Example #12
0
/**
  * @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 ");


  }
}
Example #13
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization: global MSP (MCU Support Package) initialization
     */
  HAL_Init();

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

  /* Configure 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)
  {
  }
}
Example #14
0
/**
  * @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)
  {
    ;
  }
}
Example #15
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Systick timer is configured by default as source of time base, but user
         can eventually implement his proper time base source (a general purpose
         timer for example or other time source), keeping in mind that Time base
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization: global MSP (MCU Support Package) initialization
     */
  HAL_Init();

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

  /* Initialize LED1, LED2 and LED3 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);

  /*##-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  = aNonce;
  CrypHandle.Init.IVSize     = uwNonceSize;
  CrypHandle.Init.Header     = aHeaderMessage;
  CrypHandle.Init.HeaderSize = 16;
  CrypHandle.Init.TagSize    = 6;
  CrypHandle.Init.pScratch   = aHBuffer;

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

  /*##-2- Start the AES Encryption in CCM chaining mode ######################*/
  if(HAL_CRYPEx_AESCCM_Encrypt(&CrypHandle, aPlaintext, PLAINTEXT_SIZE, aOutputText, 0xFF) != HAL_OK)
  {
    /* Encryption Error */
    Error_Handler();
  }

  /*##-3- Compute the authentication TAG for AES CCM mode ####################*/
  if(HAL_CRYPEx_AESCCM_Finish(&CrypHandle, aMAC, 0xFF) != HAL_OK)
  {
    /* TAG Error */
    Error_Handler();
  }

  /*##-4- Compare the encrypted text and the aMAC with the expected ones #####*/
  if( (memcmp(aOutputText, aExpectedCyphertext, PLAINTEXT_SIZE) != 0) ||  (memcmp(aMAC, aExpectedMAC, MAC_SIZE) != 0))
  {
    /* Wrong encryption: Turn LED2 on */
    BSP_LED_On(LED2);
  }
  else
  {
    /* Right encryption: Turn LED1 on */
    BSP_LED_On(LED1);
  }

  /* Infinite loop */
  while(1)
  {
  }
}
Example #16
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization: global MSP (MCU Support Package) initialization
     */
  HAL_Init();

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

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

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

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

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