Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIC32MZ hardware support throws ASYNC_OP_E #1

Open
youpko opened this issue Mar 4, 2020 · 1 comment
Open

PIC32MZ hardware support throws ASYNC_OP_E #1

youpko opened this issue Mar 4, 2020 · 1 comment

Comments

@youpko
Copy link

youpko commented Mar 4, 2020

For my project I wanted SHA256 support and the random number generator. In the harmony configurator I enabled hardware support for both my requirements.

I looked in to the documentation and found the following:

  1. RNG
#define RANDOM_BYTE_SZ 32

int           ret;
CRYPT_RNG_CTX mcRng;
byte          out[RANDOM_BYTE_SZ];

ret = CRYPT_RNG_Initialize(&mcRng);
ret = CRYPT_RNG_BlockGenerate(&mcRng, out, RANDOM_BYTE_SZ);

The initialize function fails with error code -209 (DRBG_CONT_FIPS_E).
But the initialize function tries to preform a sha256 operations with fails which I will mention below.

  1. SHA256
CRYPT_SHA_CTX sha;
uint8_t shaSum[SHA_DIGEST_SIZE];

CRYPT_SHA_Initialize(&sha);
CRYPT_SHA_DataAdd(&sha, buffer, sizeof(buffer));
CRYPT_SHA_Finalize(&sha, shaSum);

The initialize function fails with error code -245 (ASYNC_OP_E).

After going line by line with the debugger I stumbled on the while-wait loop that times out in pic32mz-crypt.c Pic32Crypto()

        /* Software Reset the Crypto Engine */
        CECON = 1 << 6;
        while (CECON);

        /* Clear the interrupt flags */
        CEINTSRC = 0xF;

        /* Run the engine */
        CEBDPADDR = (unsigned int)KVA_TO_PA(&bd);
        CEINTEN = 0x07; /* enable DMA Packet Completion Interrupt */

        /* input swap, enable BD fetch and start DMA */
    #if PIC32_NO_OUT_SWAP
        CECON = 0x25;
    #else
        CECON = 0xa5; /* bit 7 = enable out swap */
    #endif

        /* wait for operation to complete */
        while (CEINTSRCbits.PKTIF == 0 && --timeout > 0) {};

        /* Clear the interrupt flags */
        CEINTSRC = 0xF;

        /* check for errors */
        if (CESTATbits.ERROP || timeout <= 0) {
        #if 0
            printf("PIC32 Crypto: ERROP %x, ERRPHASE %x, TIMEOUT %s\n",
                CESTATbits.ERROP, CESTATbits.ERRPHASE, timeout <= 0 ? "yes" : "no");
        #endif
            ret = ASYNC_OP_E;
        }

the CEINTSRCbits.PKTIF seems to be never set to 1.
I looked up what this function does and compared it with the PIC32MZ datasheet and this function seems to enable al the right registers.

Maybe I forgot to setup/initialize something in the project but I can't find anything in the Harmony documentation files.

@youpko
Copy link
Author

youpko commented Mar 5, 2020

I discovered what went wrong.

In the harmony configurator I forgot to enable the RNG peripheral, after enabling the peripheral the crypto library generated the random numbers I wanted.
But this let me to the peripheral disable bits in plib_clk.c CLK_Initialize() where I found that harmony never enabled the crypto engine peripheral after clearing bit PMD7<22> everything started working.

Harmony generated:

PMD7 = 0xffefffef;

Clearing the PMD7<22> bit:

PMD7 = 0xffafffef;

But there is no way to enable the crypto engine in the harmony configurator as there is no crypto peripheral node to add, like how you add the RNG peripheral node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant