#include "hcu_cases.h"

/* AES-128 key */
static const uint32_t key[4] = {
    0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c
};

/* PlainText to encrypt */
static const uint32_t plainText[16] = { 
    0x6bc1bee2, 0x2e409f96, 0xe93d7e11, 0x7393172a,
    0xae2d8a57, 0x1e03ac9c, 0x9eb76fac, 0x45af8e51,
    0x30c81c46, 0xa35ce411, 0xe5fbc119, 0x1a0a52ef,
    0xf69f2445, 0xdf4f9b17, 0xad2b417b, 0xe66c3710
};

/* CipherText encrypted by AES-ECB */
static const uint32_t cipherText[16] = { 
    0xC48D5784, 0xF6BB1688, 0xA1F6AC18, 0xEEDAB413,
    0x39CBB8F3, 0x7FAB8C25, 0x3B1C1E50, 0x8C53E9D0,
    0xDF273015, 0x3603F913, 0xD4298D27, 0xBB91F6FA,
    0xF95B31D0, 0x9053DFF1, 0x214A42EF, 0xF22A080D
};

static uint32_t result[16];

/* HCU encrypt configuration */
static aes_algorithm_config_t encryptCfg = {
    .dataOutputPtr = result,
    .dataInputPtr = plainText,
    .msgLen = 64,
    .swKeyPtr = key,
    .hwKeySelected = false,
    .keySize = KEY_SIZE_128_BITS,
    .msgType = MSG_ALL,
};

static hcu_user_config_t hcuUserConfig = {
    .swap = MODE_SWAPPING_NO,
    .carryType = HCU_USING_POLLING,
    .ingressDMAChannel = 0U,
    .egressDMAChannel = 1U,
};

static hcu_state_t hcuState;

void hcu_ecb_demo(void)
{
    uint32_t i = 0;
    status_t status = STATUS_SUCCESS;
    PRINTF("HCU AES-ECB Using Polling!\r\n");

    /* Init HCU */
    status |= HCU_DRV_Init(&hcuUserConfig, &hcuState);
    /* Start AES-ECB encrypt */
    status |= HCU_DRV_EncryptECB(&encryptCfg);
    /* Wait encrypt done */
    while(hcuState.cmdInProgress);

    if(status != STATUS_SUCCESS)
    {
        PRINTF("AES-ECB encrypt Failed!\r\n");
        HCU_DRV_DeInit(&hcuState);
        return;
    }

    /* Check result */
    for(i = 0; i < 16; i++)
    {
        if(result[i] != cipherText[i])
        {
            PRINTF("AES-ECB encrypt Failed!\r\n");
            PRINTF("Expected is 0x%08X, actual is 0x%08X.\r\n", cipherText[i], result[i]);
            break;
        }
    }
    if(i == 16)
    {
        PRINTF("AES-ECB encrypt success!\r\n");
    }
    
    HCU_DRV_DeInit(&hcuState);
}
