- /* Plaintext */
- uint32_t aPlaintextECB[AES_TEXT_SIZE] =
- {0x6BC1BEE2 ,0x2E409F96 ,0xE93D7E11 ,0x7393172A ,
- 0xAE2D8A57 ,0x1E03AC9C ,0x9EB76FAC ,0x45AF8E51 ,
- 0x30C81C46 ,0xA35CE411 ,0xE5FBC119 ,0x1A0A52EF ,
- 0xF69F2445 ,0xDF4F9B17 ,0xAD2B417B ,0xE66C3710};
- uint32_t aPlaintextCBC[AES_TEXT_SIZE] =
- {0xE2BEC16B ,0x969F402E ,0x117E3DE9 ,0x2A179373 ,
- 0x578A2DAE ,0x9CAC031E ,0xAC6FB79E ,0x518EAF45 ,
- 0x461CC830 ,0x11E45CA3 ,0x19C1FBE5 ,0xEF520A1A ,
- 0x45249FF6 ,0x179B4FDF ,0x7B412BAD ,0x10376CE6};
- uint32_t aPlaintextCTR[AES_TEXT_SIZE] =
- {0x477D83D6 ,0x69F90274 ,0x887EBC97 ,0x54E8C9CE ,
- 0xEA51B475 ,0x3935C078 ,0x35F6ED79 ,0x8A71F5A2 ,
- 0x6238130C ,0x88273AC5 ,0x9883DFA7 ,0xF74A5058,
- 0xA224F96F ,0xE8D9F2FB ,0xDE82D4B5 ,0x08EC3667};
串口初始化:
- /* Configure COM port */
- COM_Init.BaudRate = 115200;
- COM_Init.WordLength = COM_WORDLENGTH_8B;
- COM_Init.StopBits = COM_STOPBITS_1;
- COM_Init.Parity = COM_PARITY_NONE;
- COM_Init.HwFlowCtl = COM_HWCONTROL_NONE;
- if (BSP_COM_Init(COM1, &COM_Init) != BSP_ERROR_NONE)
- {
- Error_Handler();
- }
初始化AES模块:
- static void MX_AES_Init(void)
- {
- /* USER CODE BEGIN AES_Init 0 */
- /* USER CODE END AES_Init 0 */
- /* USER CODE BEGIN AES_Init 1 */
- /* USER CODE END AES_Init 1 */
- hcryp.Instance = AES;
- hcryp.Init.DataType = CRYP_DATATYPE_32B;
- hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
- hcryp.Init.pKey = (uint32_t *)pKeyAES;
- hcryp.Init.Algorithm = CRYP_AES_ECB;
- hcryp.Init.DataWidthUnit = CRYP_DATAWIDTHUNIT_WORD;
- hcryp.Init.KeyIVConfigSkip = CRYP_KEYIVCONFIG_ALWAYS;
- if (HAL_CRYP_Init(&hcryp) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN AES_Init 2 */
- /* USER CODE END AES_Init 2 */
- }
调用HAL API进行数据加密:
- /***************** AES 128 ****************/
- /* Start encrypting aPlaintextECB, the cypher data is available in aEncryptedtext */
- if (HAL_CRYP_Encrypt(&hcryp, aPlaintextECB, AES_TEXT_SIZE, aEncryptedtext, TIMEOUT_VALUE) == HAL_OK)
- {
- /* Display encrypted Data */
- Display_EncryptedData(ECB, 128, AES_TEXT_SIZE);
- }
- else
- {
- /* Processing Error */
- Error_Handler();
- }
与预期结果相比较:
- static void data_cmp(uint32_t *EncryptedText, uint32_t *RefText, uint8_t Size)
- {
- /* 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(&hcryp) != HAL_CRYP_STATE_READY)
- {
- }
-
- /*##-3- Check the encrypted text with the expected one #####################*/
- if(memcmp(EncryptedText, RefText, Size) != 0)
- {
- Error_Handler();
- }
- else
- {
- /* Right encryption */
- }
- }
实物串口结果输出: