#include "hcu_cases.h"

/* 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
};

/* Result by SHA-256 */
static const uint32_t mac_result[8] = {
    0x09CEE9FE, 0x64967BEB, 0xBB330BDD, 0x3834295F,
    0x9C3E6C56, 0x8D7CBEE4, 0x81760715, 0xC02B625A
};

static uint32_t result[8];

/* HCU encrypt configuration */
static sha_algorithm_config_t encryptCfg = {
    .dataOutputPtr = result,
    .dataInputPtr = plainText,
    .msgLen = 64,
    .totalLen = 64,
    .shaType = HCU_SHA_256,
    .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_sha_demo(void)
{
    uint32_t i = 0;
    status_t status = STATUS_SUCCESS;
    PRINTF("HCU SHA-256 Using Polling!\r\n");

    /* Init HCU */
    status |= HCU_DRV_Init(&hcuUserConfig, &hcuState);

    PRINTF("SHA-256 generation!\r\n");
    /* Start SHA-256 generation */
    status |= HCU_DRV_GenerateSHA(&encryptCfg);
    /* Wait encrypt done */
    while(hcuState.cmdInProgress);

    /* Check result */
    for(i = 0; i < 8; i++)
    {
        if(result[i] != mac_result[i])
        {
            PRINTF("SHA-256 generation Failed!\r\n");
            PRINTF("Expected is 0x%08X, actual is 0x%08X.\r\n", mac_result[i], result[i]);
            break;
        }
    }
    if(i == 8)
    {
        PRINTF("SHA-256 generation success!\r\n");
    }

    PRINTF("SHA-256 authorization!\r\n");
    /* Start SHA-256 generation */
    status |= HCU_DRV_AuthorizeSHA(&encryptCfg);
    /* Wait encrypt done */
    while(hcuState.cmdInProgress);
    if(status != STATUS_SUCCESS)
    {
        PRINTF("SHA-256 authorization Failed!\r\n");
    }
    else
    {
        PRINTF("SHA-256 authorization Pass!\r\n");  
    }

    HCU_DRV_DeInit(&hcuState);
}


