This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C129ENCPDT: SHAMD5 - SHA256 on non consecutive memory blocks

Part Number: TM4C129ENCPDT


Hello

I wish to run a SHA256 hash over a range of memory on external SPI flash which means I need to read a number of blocks of the memory from SPI flash, run the SHA256 on each block to get the final hash.

I can find plenty of examples of how to use the SHAMD5 library to run over a single contiguous block of memory and this works well.

However I cannot find any examples as to how I keep the hash going over a number of blocks to get the final result.

Is anyone able to show me an example of how to do this.

Thanks

  • Here is an example that does 1024 bytes using SHA256 as one block, and then again as 16 blocks of 64 bytes. In this example the data must be a multiple of 64 bytes.

    /cfs-file/__key/communityserver-discussions-components-files/908/SHA256.zip

  • Hello Bob

    Thanks for the response.

    While I was waiting for a response I had another go at doing something myself and cam up with the following:

    First Initialise the hardware:

    			SHAMD5Reset(SHAMD5_BASE);
    			SHAMD5ConfigSet(SHAMD5_BASE, SHAMD5_ALGO_SHA256);
    			SHAMD5HashLengthSet(SHAMD5_BASE, size);
    

    Then I write 1 or more blocks of 64 (Can be called multiple times till end of full 64 byte blocks reached.

    //============================================================
    // Write blocks of 64 bytes to SHAMD5
    //============================================================
    void SHAMD5DataWriteBlocks(uint32_t ui32Base, uint32_t * pui32Src, int BlockCount)
    {
    	while (BlockCount > 0)
    	{
    		SHAMD5DataWrite(ui32Base, pui32Src);
    		BlockCount--;
    		pui32Src += 16;
    	}
    }	// SHAMD5DataWriteBlocks
    

    Finally if any more data left that is less than the 64 bytes I call

    //============================================================
    // Write the last bytes and close the hash
    //============================================================
    void SHAMD5DataWriteLast(uint32_t ui32Base, uint32_t * pui32Src, int length)
    {
    	uint32_t ui32Counter;
    
    	// Wait for the module to be ready to accept data.
    	while ((HWREG(ui32Base + SHAMD5_O_IRQSTATUS) & SHAMD5_INT_INPUT_READY) == 0)
    	{
    	}
    
    	// Write the available data.
    	for (ui32Counter = 0; ui32Counter < length; ui32Counter += 4)
    	{
    		HWREG(ui32Base + SHAMD5_O_DATA_0_IN + ui32Counter) = *pui32Src++;
    	}
    } // SHAMD5DataWriteLast
    

    Then I wait for it to finish and read the result into a suitable buffer

    			// Wait for the output to be ready.
    			while ((HWREG(SHAMD5_BASE + SHAMD5_O_IRQSTATUS) & SHAMD5_INT_OUTPUT_READY) == 0)
    			{
    			}
    
    			// Read the result.
    			SHAMD5ResultRead(SHAMD5_BASE, (uint32_t *)hash256);
    

    I have tested this with both SHA1 and SHA256 and works fine for me

  • Yes, that looks like a good solution.