SHA-3 API Example Usage - 2023.1 English

Standalone Library Documentation: BSP and Libraries Document Collection (UG643)

Document ID
UG643
Release Date
2023-05-16
Version
2023.1 English

The xilsecure_sha_example.c file is a simple example application that demonstrates the usage of SHA-3 accelerator to calculate a 384-bit hash on the Hello World string. A typical use case for the SHA3 accelerator is for calcuation of the boot image hash as part of the autentication operation. This is illustrated in the xilsecure_rsa_example.c.

The contents of the xilsecure_sha_example.c file are shown below:

static u32 SecureSha3Example()
{
        XSecure_Sha3 Secure_Sha3;
        XCsuDma CsuDma;
        XCsuDma_Config *Config;
        u8 Out[SHA3_HASH_LEN_IN_BYTES];
        u32 Status = XST_FAILURE;
        u32 Size = 0U;

        Size = Xil_Strnlen(Data, SHA3_INPUT_DATA_LEN);
        if (Size != SHA3_INPUT_DATA_LEN) {
                xil_printf("Provided data length is Invalid\n\r");
                Status = XST_FAILURE;
                goto END;
        }

        Config = XCsuDma_LookupConfig(0);
        if (NULL == Config) {
                xil_printf("config failed\n\r");
                Status = XST_FAILURE;
                goto END;
        }

        Status = XCsuDma_CfgInitialize(&CsuDma, Config, Config->BaseAddress);
        if (Status != XST_SUCCESS) {
                Status = XST_FAILURE;
                goto END;
        }

        /*
         * Initialize the SHA-3 driver so that it's ready to use
         */
        XSecure_Sha3Initialize(&Secure_Sha3, &CsuDma);

        XSecure_Sha3Digest(&Secure_Sha3, (u8*)Data, Size, Out);

        xil_printf(" Calculated Hash \r\n ");
        SecureSha3PrintHash(Out);

        Status = SecureSha3CompareHash(Out, ExpHash);
END:
        return Status;
}

/****************************************************************************/
static u32 SecureSha3CompareHash(u8 *Hash, u8 *ExpectedHash)
{
        u32 Index;
        u32 Status = XST_FAILURE;

        for (Index = 0U; Index < SHA3_HASH_LEN_IN_BYTES; Index++) {
                if (Hash[Index] != ExpectedHash[Index]) {
                        xil_printf("Expected Hash \r\n");
                        SecureSha3PrintHash(ExpectedHash);
                        xil_printf("SHA Example Failed at Hash Comparison \r\n");
                        break;
                }
        }
        if (Index == SHA3_HASH_LEN_IN_BYTES) {
                Status = XST_SUCCESS;
        }

        return Status;
}

/****************************************************************************/
static void SecureSha3PrintHash(u8 *Hash)
{
        u32 Index;

        for (Index = 0U; Index < SHA3_HASH_LEN_IN_BYTES; Index++) {
                xil_printf(" %0x ", Hash[Index]);
        }
        xil_printf(" \r\n ");
 }
Note: The xilsecure_sha_example.c and xilsecure_rsa_example.c example files are available in the <library-install-path>\examples folder. Where <library-install-path> is the XilSecure library installation path.