MCF5235 MDHA SHA1 problem

Showing results for 
Search instead for 
Did you mean: 

MCF5235 MDHA SHA1 problem

Contributor I

I am using the MDHA of  MCF5235 to calculate the  SHA1.

The test code is :

void sha1_test()


   uint8 buf[]="abcdefghijklmnopqrstuvwxyz";
   uint32 results[5]={0};
   int i;

   MCF_MDHA_MDCMR = MCF_MDHA_MDCMR_SWR;// software reset
   while(0 != (MCF_MDHA_MDCMR & MCF_MDHA_MDCMR_SWR)) // self clearing
   MCF_MDHA_MDCR = 0u;//disable all interrupt
                                       | MCF_MDHA_MDMR_PDATA
                                       | MCF_MDHA_MDMR_INIT;//enable initialization
      MCF_MDHA_MDIN = *(uint32 *)buf[i];
   MCF_MDHA_MDIN = *(uint32 *)buf[i];


   delay(10);//delay 10 us

   results[0] = MCF_MDHA_MDA0;
   results[1] = MCF_MDHA_MDB0;
   results[2] = MCF_MDHA_MDC0;
   results[3] = MCF_MDHA_MDD0;
   results[4] = MCF_MDHA_MDE0;


The test result is : 0xC700DFB3, 0x14957954,0x9087EE75, 0xED04935D,0x30B541E9

It is different from the SHA1 tools from the web, for example 

The test result is 0x32d10c7b,0x8cf96570,0xca04ce37,0xf2a19d84,0x240d3a89.


I had known both the results are correct. But why they are so different? 

Original Attachment has been moved to:

0 Kudos
1 Reply

Specialist I

You should have searched this forum first for "MDHA". You're not the first one to have this problem.

MCF5271 SKHA and MDHA endian 

And the trick? My code looks like this:

    MCF_MDHA_MDDSR = 64;
    for (i = 0; i != 16; ++i) {
        LOAD32H(a, buf + (4 * i));
        /* load data */
        MCF_MDHA_MDIN = bswap32(a);

Note the byte swap! Yes, hidden in the manual is the following single mention of this:

28.2.8 MDHA Message Digest Registers 0 (MDx0)
Each word (4 bytes) in the MDx0 is assumed to be in little endian byte order for all reads/writes.

This hardware module was always little-endian. So you have to byte-swap all data and keys going into this thing, and then swap it all back on the way out. Fortunately the CPU has a byte-swap machine code instruction, which you have to then go and write code or inline-assembly to use. If you don't you may take as long to swap the bytes as the hardware takes to do the operation! In some CPUs in this line (the MCF53xx but not MCF52xx) have a bit in the register that says "swap them for me!". That also means you can use DMA to funnel data through this module.

But not with your one.


0 Kudos