I am trying to implement SHA256 on a TI cc26xx sensor tag. I am using the TI_SHA_256 API provided here: http://www.ti.com/tool/crypto. My main test code is the following:
...
/* Crypto APIs Header */
#include "lib/TI_SHA_256.h"
#include <stdio.h>
uint32_t M[32] = { 0xe8 , 0x9d , 0xa1 , 0xd1 , 0xc7 , 0x4d , 0xee , 0x16 , 0x75 , 0x30 , 0x07 , 0x9a , 0x19 , 0xd1 , 0x5d , 0x76,
0x12 , 0x97 , 0xe4 , 0xb6 , 0xc8 , 0x03 , 0x38 , 0x1a , 0x41 , 0x6d , 0xac , 0x92 , 0xbf , 0x63 , 0x51 , 0x7a };
uint32_t Ha[8];
uint32_t *H_Array;
volatile uint64_t L = 0x800;
void uint32_print(char name[], uint32_t *data, int c)
{
uint32_t i;
if (c == 1){
printf(name);
for(i = 0; i < 32; i++) {
printf("%x", data[i]);
}
printf("\r\n");
}
else {
printf(name);
for(i = 0; i < 8; i++) {
printf("%x ", data[i]);
}
printf("\r\n");
}
}
/*---------------------------------------------------------------------------*/
PROCESS(test_process, "Test process");
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(test_process, ev, data)
{
PROCESS_BEGIN();
printf("\r\n ---------------- TEST OF SHA API ------------------------ \r\n");
uint32_print("Original text:", M, 1);
L= 0x200;
SHA_256(M, L, Ha, 1);
uint32_print("text:", M, 1);
uint32_print("Hashed text: ", Ha, 2);
printf("\r\n ---------------- \r\n");
PROCESS_END();
}
AUTOSTART_PROCESSES(&test_process);
According to various online SHA256 calculators such (http://www.fileformat.info/tool/hash.htm), the checksum is supposed to be:
73411b58707db59d6bc3cd854850eca62058d0d9f74a1ea8260d5ccdd9ac5f87
Where as my code prints the following:
---------------- TEST OF SHA API ------------------------
text:e89da1d1c74dee16753079a19d15d761297e4b6c83381a416dac92bf63517a
text:e89da1d1c74dee16753079a19d15d768000000000000000000000200
Hashed text: 91da5dd5 6cbfcca9 85fcf373 90ae73e0 9e27a4d9 c42b100c 6e746091 eda68da7
----------------
My question is the following: why is my code returning the wrong hash-sum?
Your code computes the SHA-256 hash of the first half (0x200=512
bits or 64 bytes) of the array M
, initialized to 0xe8 , 0x9d , ... , 0x76
.
However, your array M
has elements of type uint32_t
, which mean that in memory it looks like this (assuming big-endian architecture):
000000e80000009d000000a1000000d1000000c70000004d000000ee000000160000007500000030000000070000009a00000019000000d10000005d00000076
As one can check in your online calculator, the hash of this is 91da5dd56cbfcca985fcf37390ae73e09e27a4d9c42b100c6e746091eda68da7
. This is the correct value computed by the SHA_256()
function in the code.