void mbedtls_aes_encrypt( mbedtls_aes_context *ctx, const unsigned char input[16], unsigned char output[16] ) { if (HAL_CRYP_AESECB_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) !=0) { // error found to be returned } }
/** * @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 "); } }
/** * @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 "); } }
/** * @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) { ; } }