SHA256 of big file in external FLASH

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

SHA256 of big file in external FLASH

1,569 Views
nxp-obrist
Contributor II

Hi,

I'm using an LPC55S16 with an external FLASH. I'll save a binary file in this flash which is bigger than the internal RAM of the LPC55S16.

 

I would like to calculate the SHA256 of this file using the MCUXpresso driver fsl_hashcrypt.

Now the provided example for the calculation of the SHA256 uses a hard coded string message.

 

Is there a example for calculating a SHA256 of a big file which can not be handled at once because of not enough RAM on the LPC55? Or could anyone tell me how to do it with hte fsl_hashcrypt driver?

 

Best regards,

Corsin

Labels (1)
Tags (2)
0 Kudos
Reply
5 Replies

1,559 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

As you know that there is a _hashcrypt example in SDK package for the LPC55S16, in the example, the hash value is from a array in RAM.

But I think the array can be located in flash directly, so it is unnecessary to copy from flash to RAM. You can compute hash value from a flash address with a size.

You can use the code like:

status_t status;
size_t outLength;
unsigned int length;
unsigned char output[32];

unsigned char *message;

length=0x1234;
outLength = sizeof(output);
memset(&output, 0, outLength);

message=(unsigned char *)0x12345678;

/************************ SHA-256 **************************/
status = HASHCRYPT_SHA(HASHCRYPT, kHASHCRYPT_Sha256, message, length, output, &outLength);

 

@brief Create HASH on given data
*
* Perform the full SHA in one function call. The function is blocking.
*
* @Param base HASHCRYPT peripheral base address
* @Param algo Underlaying algorithm to use for hash computation.
* @Param input Input data
* @Param inputSize Size of input data in bytes
* @Param[out] output Output hash data
* @Param[out] outputSize Output parameter storing the size of the output hash in bytes
* @return Status of the one call hash operation.
*/
status_t HASHCRYPT_SHA(HASHCRYPT_Type *base,
hashcrypt_algo_t algo,
const uint8_t *input,
size_t inputSize,
uint8_t *output,
size_t *outputSize);

Hope it can help you

BR

XiangJun Rong

 

0 Kudos
Reply

1,551 Views
nxp-obrist
Contributor II

@xiangjun_rong Thanks for the fast reply.

The problem is, I'm using a external SPI Flash and a littleFS filesystem. 

As I understand, somehow the hashdriver has to known the SPI dirver interface to read block by block from it.

Is there a example for this usecase?

Best regards 

Corsin

0 Kudos
Reply

1,546 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

I suppose you can call the low level function of hash, in other words, you do not call the HASHCRYPT_SHA(), but call the HASHCRYPT_SHA_Init(), then multiple HASHCRYPT_SHA_Update(), then HASHCRYPT_SHA_Finish().

Hope it can help

BR

XiangJun Rong

status_t HASHCRYPT_SHA(HASHCRYPT_Type *base,
hashcrypt_algo_t algo,
const uint8_t *input,
size_t inputSize,
uint8_t *output,
size_t *outputSize)
{
hashcrypt_hash_ctx_t hashCtx;
status_t status;

status = HASHCRYPT_SHA_Init(base, &hashCtx, algo);
if (status != kStatus_Success)
{
return status;
}

status = HASHCRYPT_SHA_Update(base, &hashCtx, input, inputSize);
if (status != kStatus_Success)
{
return status;
}

status = HASHCRYPT_SHA_Finish(base, &hashCtx, output, outputSize);

return status;
}

0 Kudos
Reply

1,529 Views
nxp-obrist
Contributor II

@xiangjun_rong thanks for your reply.

I thought that I have to call these low level function.. I was debuging the whole day about the functionality of the HASHCRYPT_SHA_Update function and the hashcrypt_sha_process_message_data fuction.

The problem is, that I can't see where he writes the block to the HASH processor. It seems like he passes once the address of the message to the HASH processor and the HASH processor will process all block indipendandly until the last one which is not 64byte aligned. This one will then be passed to the HASH processor at the end.

I can't find a way to pass like 1024byte blocks from the external FLASH to the crypto engine. 

Can you help me with this? 

best regards 

Corsin Obrist

0 Kudos
Reply

1,526 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Corsin,

You can read a predefined size of data from external spifi flash to on-chip RAM, call the HASHCRYPT_SHA_Update() function for the predefined size of data in on-chip RAM to do hash, and repeat the procedure several times.

for reading the spifi flash, I think it is okay to read directly.

BR

XiangJun Rong

0 Kudos
Reply