Problems using the 5275 MDHA for MD5 / HMAC

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problems using the 5275 MDHA for MD5 / HMAC

1,590 Views
taigbr
Contributor I
Hi,
I am having severe trouble to get the MDHA engine running reliable.
MCU is a 5275.
My code consists of three parts: init, transform and final.
Normal use would be:
init
transform 64 bytes  n times
final

But:
- the result is not always the same though the input is the same
- sometimes MDHA stalls after the GO cmd with status = 0x00000498
Any attempt to change the status=0498 results in an error status.

I appreciate any help!
Kind regards,
Georg Brechlin

#define    MdsrData(k)    (k&0x001F0000)
#define    MdsrBusy(k)    (k&0x00000010)
#define    MdsrHash(k)    (k&0x00000040)
#define    MdsrPad(k)    ((k&0x0000E000)==0x00008000)
#define    MdsrInt(k)    (k&0x00000007)

#define    MdsrHashDone(k)    ((!MdsrBusy(k)) || (MdsrHash(k)) || (MdsrData(k)) || (!MdsrPad(k)) )


init:
    MCF_MDHA_MDCMR = 0x00000001;        // Software Reset
    while (!(MCF_MDHA_MDSR&0x00000008)){}    // still resetting?
    MCF_MDHA_MDCR = 0;    // no Ints
    MCF_MDHA_MDMR = 0x21;    // MD5 Init

transform puts 16 32bit words into the fifo and sets new datasize to 64 bytes
    for (j=16;j;j--)
    {
        d = mdha_byte_to_uint(&k,d);   // fill 32bit word with bytes
        MCF_MDHA_MDIN = k;    // put 32 bit word to fifo
    }

    MCF_MDHA_MDDSR = 64;            //  64 bytes filled in

    k = MCF_MDHA_MDSR;
    // wait for MDSR status:
    // (makes no big difference if waiting or not)
    // - bit hash = 0
    // - bit busy = 1
    // - bits IFL = 0 , Input Fifo Level
    while (MdsrHashDone(k))
    {
        k = MCF_MDHA_MDSR;
    }

final: tell MDHA to calculate the hash

    MCF_MDHA_MDCMR = 0x00000008;    // GO ...

    k = MCF_MDHA_MDSR;
    while ((MdsrBusy(k))||(!MdsrInt(k)))    // wait while BUSY=1 and DONE=0
    {
        k = MCF_MDHA_MDSR;
    }

    // get the result
    md5_transform_erg[0] = MCF_MDHA_MDA0;
    md5_transform_erg[1] = MCF_MDHA_MDB0;
    md5_transform_erg[2] = MCF_MDHA_MDC0;
    md5_transform_erg[3] = MCF_MDHA_MDD0;

    MCF_MDHA_MDCMR = 0x00000004;    // clr Int flag

Labels (1)
0 Kudos
Reply
2 Replies

571 Views
Martin_
NXP Employee
NXP Employee
Hello, I think the "transform" part may be causing the issue. It seems you execute transform part n-times, depending on the size of the input data.  For this, a different approach may be used:

Performing a Standard HASH Operation, multiple 64 byte input blocks:
1. reset the MDHA using SWR bit
2. MDCR register write
3. MDMR register write
4. write the complete data size to the MDDSR register (for example 256 for four input blocks)
5. poll MDSR[IFL] to check if there is a space available in the FIFO
6. write a longword to the FIFO
7. repeat steps 5 and 6 until the entire block is written
8. set the GO bit
9. wait for MDSR[INT]

This approach gives reliable outputs.

Source code example:

   MCF_MDHA_MDCMR = (0 | MCF_MDHA_MDCMR_CI
                  | MCF_MDHA_MDCMR_RI
                  | MCF_MDHA_MDCMR_SWR )  ;      // do a reset
   while (!MCF_MDHA_MDSR & MCF_MDHA_MDSR_RD){}   // reset done?

   MCF_MDHA_MDCR = 0;            // no ints
   MCF_MDHA_MDMR = (0 |
                                        MCF_MDHA_MDMR_INIT |
                                        MCF_MDHA_MDMR_ALG);      // select MD5 with init
   
   MCF_MDHA_MDDSR = 256;   //256 input bytes in total
   for(i=0;i<256/4;i++)    //move 64 longwords to the fifo
   {
      //wait until there is a space in the FIFO
      while((MCF_MDHA_MDSR & 0x1F0000 ) == 0x10)
      {} //do nothing
      
      MCF_MDHA_MDIN = input_data[i];      //move longword to the FIFO
   }
   
   MCF_MDHA_MDCMR = MCF_MDHA_MDCMR_GO;   // GO
      
   k = MCF_MDHA_MDSR;
   /* wait for digest to complete */
   while( !( MCF_MDHA_MDSR & MCF_MDHA_MDSR_INT ))
   {};         
   
   /* Check for errors! */
   if ( MCF_MDHA_MDSR & MCF_MDHA_MDSR_ERR)
   {
      printf("Error during message digest!!!\r\n" );
      k = MCF_MDHA_MDISR;   
      printf("MCF_MDHA_MDISR: 0x%08x\n\r", k);
   }

0 Kudos
Reply

571 Views
taigbr
Contributor I
It seems to be a matter of speed. If the cache is turned off
there still is a problem, but not so often. The MDHA engine
probably needs some cycles for execution, but nothing is
mentioned in the manual.
Kind regards,
Georg

0 Kudos
Reply