0

I am using TinyOS-2.1.2 and to achieve security techniques I am using TinyECC-2.0. I want to use the SHA1 available in tinyecc. But, when I take the hash of a value say,

uint8_t data=123;

I use the three functions of sha given in SHA1.nc namely, SHA1.reset, SHA1.update and SHA1.digest to obtain the result. But each time I run the code ie. do "make micaz sim" I get different hash results for the same data.

How to get a unique hash value for each data taken?

The code is:

#include "sha1.h"

module DisseminationC {
  uses {
            interface SHA1;
}
implementation{

void hash(){

uint8_t x=123;

call SHA1.context(context);
call SHA1.update(context, x, sizeof(x));
call SHA1.digest(context, Message_Digest[SHA1HashSize]);

dbg("All", "%s Hash is : %d \n", sim_time_string(), Message_Digest);
}

I made modifications in the code as shown below. Now, I am getting a hash output. But the problem is that for every different number given as input I am getting the same answer. How do I solve this issue?

Please help me..

#include "sha1.h"

module SecurityC{

    uses interface Boot;
    uses interface SHA1;
}

implementation{

    uint8_t Message_Digest[SHA1HashSize];
    SHA1Context context;
    uint8_t num=123;
    uint32_t length=3;
    uint8_t i;

    event void Boot.booted()
    {
        dbg("Boot", "Application booted.\n");

        call SHA1.reset(&context);
        while(length>0)
        {
            length=length/10;
            call SHA1.update(&context, &num, length);
        }
            call SHA1.digest(&context, Message_Digest);
            for(i = 0; i < SHA1HashSize; i++) {
                dbg("Boot", "%s KEY IS: %x \n", sim_time_string(), Message_Digest[i]);
        }
    }
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880

1 Answers1

0

First of all, your code is bad. It lacks two braces and the function SHA1.context doesn't exist in this library (it should be SHA1.reset, I think). Moreover, Message_Digest and context aren't declared. Please provide the full code you actually use.

However, I see you have at least two serious bugs.

Firstly, you pass the value of x to SHA1.update, but you should pass a pointer to the message. Therefore, the function processes a message that lies at the address 123 in the memory (you should get a compiler warning about this). If you want to calculate a hash from the value of x, try this:

call SHA1.update(context, &x, sizeof(x));

Secondly, Message_Digest seems to be a uint8_t array of size SHA1HashSize. In the last statement you print a pointer to this array instead of its content (and again, the compiler should warn you), so you get an adress of this array in the memory. You may want to process the array in a loop:

uint8_t i;
for(i = 0; i < SHA1HashSize; ++i) {
    // process Message_Digest[i], for instance print it
}
maral
  • 1,431
  • 1
  • 10
  • 15