1

I found an implementation of SHA256 on the net. It consists of a single file sha256.c, I've tested the function successfully on Linux.

Here's the link for the file: http://bradconte.com/sha256_c

When I try to use it in contiki, the output is not right.

Here's a piece of the code :

unsigned char text1[]={"sallam"}, hash[32];
int idx;
SHA256_CTX ctx;
sha256_init(&ctx);
sha256_update(&ctx, text1, strlen(text1));
sha256_final(&ctx, hash);
print_hash(hash);

Here's the function I use to print the hash:

void print_hash(unsigned char hash[])
{
    int idx;
    for (idx=0; idx < 32; idx++)
       printf("%02x",hash[idx]);
    printf("\n");
}

I've already tried to change the format ouptut to %x but it didn't help. I think it has something to do with little-endian vs big-endian (endianness) issues.

kfx
  • 8,136
  • 3
  • 28
  • 52
yushaa yave
  • 145
  • 1
  • 9
  • So which Endianness does your platform (contiki?) use? – alk Apr 30 '15 at 17:04
  • And also which platform was the Linux running, you used for testing? – alk Apr 30 '15 at 17:06
  • I'm using instant contiki 2.7 on virtual machine. The ubuntu endianness is little endian, for the contiki i'don t know how to check it, pardon me. – yushaa yave Apr 30 '15 at 17:14
  • As it would be essential to know the endianess to solve your issue you'd better and go check it out: http://stackoverflow.com/q/2100331/694576 – alk Apr 30 '15 at 17:26
  • 1
    I don't think this has anything to do with endians. You should remove the `big-endian` tag, Contiki does not support have *any* big endian platforms (unlike Linux). – kfx May 01 '15 at 10:14

1 Answers1

4

The SHA256 library you're using does not handle non-32 bit platforms properly. It fails in Contiki since for MSP430 platforms the size of int is 16 bits, rather than 32 bits that the authors implicitly assumes.

The line 4 in file sha256.c:

#define uint unsigned int // 32-bit word

should be changed to:

#define uint uint32_t // 32-bit word

Make sure to #include <stdint.h> before this line to get the proper platform-independent typedef of uint32_t.

kfx
  • 8,136
  • 3
  • 28
  • 52