- /*******************************************************************************
- * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
- *
- * This software component is licensed by HDSC under BSD 3-Clause license
- * (the "License"); You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- */
- /******************************************************************************/
- /** \file main.c
- **
- ** \brief This sample demonstrates how to set GPIO as output function.
- **
- ** - 2021-04-16 CDT first version for Device Driver Library of GPIO.
- **
- ******************************************************************************/
- /*******************************************************************************
- * Include files
- ******************************************************************************/
- #include "hc32_ddl.h"
- #include "ev_hc32f460_lqfp100_v1.h"
- #include "TIM_Measure.h"
- /*******************************************************************************
- * Local type definitions ('typedef')
- ******************************************************************************/
- /*******************************************************************************
- * Local pre-processor symbols/macros ('#define')
- ******************************************************************************/
- /* AES key length in bytes is 16. */
- #define AES_KEYLEN ((uint8_t)16)
- /*******************************************************************************
- * Global variable definitions (declared in header file with 'extern')
- ******************************************************************************/
- /*******************************************************************************
- * Local function prototypes ('static')
- ******************************************************************************/
- /*******************************************************************************
- * Local variable definitions ('static')
- ******************************************************************************/
- float t = 0.0f;
- const static uint8_t m_au8AesKey[AES_KEYLEN] =
- {
- 0x12, 0x34, 0x56, 0x78, 0x9A, 0xCD, 0xEF,
- 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7
- };
- /* Word alignment. */
- __ALIGN_BEGIN static uint8_t m_au8Plaintext[64u] = {0u};
- __ALIGN_BEGIN static uint8_t m_au8Ciphertext[64u];
- /*******************************************************************************
- * Function implementation - global ('extern') and local ('static')
- ******************************************************************************/
- /**
- *******************************************************************************
- ** \brief Main function of GPIO output
- **
- ** \param None
- **
- ** \retval int32_t Return value, if needed
- **
- ******************************************************************************/
- int32_t main(void)
- {
- uint32_t i;
-
- BSP_CLK_Init();
- DDL_PrintfInit(BSP_PRINTF_DEVICE, BSP_PRINTF_BAUDRATE, BSP_PRINTF_PortInit);
-
- TIM_Measure_Init();
-
- /* Enable AES peripheral clock. */
- PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_AES, Enable);
- for (i = 0u; i < sizeof(m_au8Plaintext); i++)
- {
- m_au8Plaintext[i] = (uint8_t)(i + 1u);
- }
-
- while(1)
- {
- TIM_Measure_Start();
- /* AES encryption. */
- AES_Encrypt(m_au8Plaintext, sizeof(m_au8Plaintext), m_au8AesKey, m_au8Ciphertext);
- TIM_Measure_Stop();
- t = Get_Time();
- DDL_Printf("AES_Encrypt use time: %fus\n",t*1000.0f);
-
- DDL_Printf("AES encryption.\n");
- DDL_Printf("Plaintext:\n");
- for (i = 0u; i < sizeof(m_au8Plaintext); i++)
- {
- DDL_Printf("%.2x ", m_au8Plaintext[i]);
- }
- DDL_Printf("\n");
- DDL_Printf("Ciphertext:\n");
- for (i = 0u; i < sizeof(m_au8Ciphertext); i++)
- {
- DDL_Printf("%.2x ", m_au8Ciphertext[i]);
- }
- DDL_Printf("\n\n");
- /* AES decryption. */
- DDL_Printf("AES decryption.\n");
- TIM_Measure_Start();
- AES_Decrypt(m_au8Ciphertext, sizeof(m_au8Ciphertext), m_au8AesKey, m_au8Plaintext);
- TIM_Measure_Stop();
- t = Get_Time();
- DDL_Printf("AES_Decrypt use time: %fus\n",t*1000.0f);
- DDL_Printf("Ciphertext:\n");
- for (i = 0u; i < sizeof(m_au8Ciphertext); i++)
- {
- DDL_Printf("%.2x ", m_au8Ciphertext[i]);
- }
- DDL_Printf("\n");
- DDL_Printf("Plaintext:\n");
- for (i = 0u; i < sizeof(m_au8Plaintext); i++)
- {
- DDL_Printf("%.2x ", m_au8Plaintext[i]);
- }
- DDL_Printf("\n\n");
-
- while(1);
- };
- }
- /*******************************************************************************
- * EOF (not truncated)
- ******************************************************************************/
编译运行:
和网上的运行结果比较:
可以看出结果正确。至于运行时间和说明书中有差异,主要是填充数据比较耗时,实际计算不费时间。
HC32F460_AES.zip
(700.99 KB, 下载次数: 5)