void AESThread(void *arg) { unsigned char *sendbuf = "I am linhui, who are you?"; unsigned char recevbuf[64] = {'\0'}; unsigned char CipherText[64] = {0x0}; unsigned int data_len = strlen(sendbuf); unsigned int templen = data_len; unsigned char *mykey = "nufront123456"; log_notice("Create AES test demo process task thread.\n"); OSTimeDly(100); while (1) { OSTimeDly(300); memcpy(key, '\0', sizeof(key)); memcpy(key, mykey, strlen(mykey)); printf("key:%s\n", key); AES_Init(key); /* AES Encrypt plain data */ templen = NL6621_AES_Encrypt(sendbuf, CipherText, data_len); printf("plain data(len:%d):%s.\n", data_len, sendbuf); print_hex_data(CipherText, templen); /* AES Decrypt cipher data */ NL6621_AES_Decrypt(recevbuf, CipherText, templen); print_hex_data(recevbuf, templen); log_notice("Decrypt Data(len:%d):%s\n", templen, recevbuf); } }
static void check(u1 *plain, u1 *cipher, u1 *key, int Nk, char *title) { void *ctx; u1 data[Nb * 4]; MEMCPY(data, plain, Nb * 4); ctx = AES_Init(key, Nk); if(ctx == NULL) { printf("no memory\n"); return; } AES_Cipher(ctx, data); ps(data); if(MEMCMP(data, cipher, Nb * 4)) printf("%s-cipher is wrong\n", title); MEMCPY(data, cipher, Nb * 4); AES_InvCipher(ctx, data); ps(data); if(MEMCMP(data, plain, Nb * 4)) printf("%s-invcipher is wrong\n", title); AES_Finish(ctx); }
/** * @brief Encrypt using AES in ECB Mode * @param Key: Key used for AES algorithm. * @param Input: pointer to the Input buffer. * @param Ilength: length of the Input buffer, must be a multiple of 16 bytes. * @param Output: pointer to the returned buffer. * @retval An ErrorStatus enumeration value: * - SUCCESS: Operation done * - ERROR: Operation failed */ ErrorStatus AES_ECB_Encrypt(uint8_t * Key, uint8_t * Input, uint32_t Ilength, uint8_t * Output) { AES_InitTypeDef AES_InitStructure; AES_KeyInitTypeDef AES_KeyInitStructure; ErrorStatus status = SUCCESS; uint32_t keyaddr = (uint32_t) Key; uint32_t inputaddr = (uint32_t) Input; uint32_t outputaddr = (uint32_t) Output; __IO uint32_t counter = 0; uint32_t ccstatus = 0; uint32_t i = 0; /* AES Key initialisation */ AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t *) (keyaddr)); keyaddr += 4; AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t *) (keyaddr)); keyaddr += 4; AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t *) (keyaddr)); keyaddr += 4; AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t *) (keyaddr)); AES_KeyInit(&AES_KeyInitStructure); /* AES configuration */ AES_InitStructure.AES_Operation = AES_Operation_Encryp; AES_InitStructure.AES_Chaining = AES_Chaining_ECB; AES_InitStructure.AES_DataType = AES_DataType_8b; AES_Init(&AES_InitStructure); /* Enable AES */ AES_Cmd(ENABLE); for (i = 0; ((i < Ilength) && (status != ERROR)); i += 16) { AES_WriteSubData(*(uint32_t *) (inputaddr)); inputaddr += 4; AES_WriteSubData(*(uint32_t *) (inputaddr)); inputaddr += 4; AES_WriteSubData(*(uint32_t *) (inputaddr)); inputaddr += 4; AES_WriteSubData(*(uint32_t *) (inputaddr)); inputaddr += 4; /* Wait for CCF flag to be set */ counter = 0; do { ccstatus = AES_GetFlagStatus(AES_FLAG_CCF); counter++; } while ((counter != AES_CC_TIMEOUT) && (ccstatus == RESET)); if (ccstatus == RESET) { status = ERROR; } else { /* Clear CCF flag */ AES_ClearFlag(AES_FLAG_CCF); /* Read cipher text */ *(uint32_t *) (outputaddr) = AES_ReadSubData(); outputaddr += 4; *(uint32_t *) (outputaddr) = AES_ReadSubData(); outputaddr += 4; *(uint32_t *) (outputaddr) = AES_ReadSubData(); outputaddr += 4; *(uint32_t *) (outputaddr) = AES_ReadSubData(); outputaddr += 4; } } /* Disable AES before starting new processing */ AES_Cmd(DISABLE); return status; }
/** * @brief AES-ECB Demo * @param * @return * @note */ void AesEcbDemo(void) { printf("\r\n-------AesEcb128 Test-------\r\n"); #ifdef ALG_DEMO_PRINT_EN print_buf(AES_PLAIN,AES_BYTELEN,"AES-128-ECB Plaintext"); print_buf(AES_Key,16,"AES-128-ECB Key"); #endif AES_Init(AES_Key, 16); AES_Crypto(AES_PLAIN, AES_BYTELEN, 0, 0, AES_IV, AES_OUT); #ifdef ALG_DEMO_PRINT_EN print_buf(AES_OUT,AES_BYTELEN,"AES-128-ECB Cipher"); #endif AES_Close(); if(memcmp(AES_OUT, AES_128_ECB_CIPHER, AES_BYTELEN)) { printf("\r\n-------AesEcb128 Encrypto Test Failed-------\r\n"); print_buf(AES_PLAIN, AES_BYTELEN, "AesEcb128 plain\r\n"); print_buf(AES_Key, 16, "AesEcb128 key\r\n"); print_buf(AES_OUT, AES_BYTELEN, "----AesEcb128 cipher----\r\n"); print_buf(AES_128_ECB_CIPHER, AES_BYTELEN, "----AesEcb128 correct cipher----\r\n"); } else { printf("\r\n-------AesEcb128 Encrypto Test Success-------\r\n"); } AES_Init(AES_Key, 16); AES_Crypto(AES_OUT, AES_BYTELEN, 1, 0, AES_IV, AES_OUT); #ifdef ALG_DEMO_PRINT_EN print_buf(AES_OUT,AES_BYTELEN,"AES-128-CBC Cipher Decrypt(plaintext)"); #endif AES_Close(); if(memcmp(AES_OUT, AES_PLAIN, AES_BYTELEN)) { printf("\r\n-------AesEcb128 Decrypto Test Failed-------\r\n"); } else { printf("\r\n-------AesEcb128 Decrypto Test Success-------\r\n"); } printf("\r\n-------AesEcb192 Test-------\r\n"); AES_Init(AES_Key, 24); AES_Crypto(AES_PLAIN, AES_BYTELEN, 0, 0, AES_IV, AES_OUT); AES_Close(); if(memcmp(AES_OUT, AES_192_ECB_CIPHER, AES_BYTELEN)) { printf("\r\n-------AesEcb192 Encrypto Test Failed-------\r\n"); print_buf(AES_PLAIN, AES_BYTELEN, "AesEcb192 plain\r\n"); print_buf(AES_Key, AES_BYTELEN, "AesEcb192 key\r\n"); print_buf(AES_OUT, AES_BYTELEN, "----AesEcb192 cipher----\r\n"); print_buf(AES_192_ECB_CIPHER, AES_BYTELEN, "----AesEcb192 correct cipher----\r\n"); } else { printf("\r\n-------AesEcb192 Encrypto Test Success-------\r\n"); } AES_Init(AES_Key, 24); AES_Crypto(AES_OUT, AES_BYTELEN, 1, 0, AES_IV, AES_OUT); AES_Close(); if(memcmp(AES_OUT, AES_PLAIN, AES_BYTELEN)) { printf("\r\n-------AesEcb192 Decrypto Test Failed-------\r\n"); } else { printf("\r\n-------AesEcb192 Decrypto Test Success-------\r\n"); } printf("\r\n-------AesEcb256 Test-------\r\n"); AES_Init(AES_Key, 32); AES_Crypto(AES_PLAIN, AES_BYTELEN, 0, 0, AES_IV, AES_OUT); AES_Close(); if(memcmp(AES_OUT, AES_256_ECB_CIPHER, AES_BYTELEN)) { printf("\r\n-------AesEcb256 Encrypto Test Failed-------\r\n"); print_buf(AES_PLAIN, AES_BYTELEN, "AesEcb256 plain\r\n"); print_buf(AES_Key, AES_BYTELEN, "AesEcb256 key\r\n"); print_buf(AES_OUT, AES_BYTELEN, "----AesEcb256 cipher----\r\n"); print_buf(AES_128_ECB_CIPHER, AES_BYTELEN, "----AesEcb256 correct cipher----\r\n"); } else { printf("\r\n-------AesEcb256 Encrypto Test Success-------\r\n"); } AES_Init(AES_Key, 32); AES_Crypto(AES_OUT, AES_BYTELEN, 1, 0, AES_IV, AES_OUT); AES_Close(); if(memcmp(AES_OUT, AES_PLAIN, AES_BYTELEN)) { printf("\r\n-------AesEcb256 Decrypto Test Failed-------\r\n"); } else { printf("\r\n-------AesEcb256 Decrypto Test Success-------\r\n"); } }