spaniakos / Cryptosuite

Cryptographic suite for Arduino & RPi (SHA, HMAC-SHA)
http://spaniakos.github.io/Cryptosuite/
GNU Affero General Public License v3.0
23 stars 11 forks source link

Unexpected results with simple hmac #8

Closed sterling closed 8 years ago

sterling commented 8 years ago

I'm not quite sure if this library is at fault yet, but I wanted to see if you have any insights into the issue. I am using PlatformIO (http://platformio.org/) to edit, compile, and upload my sketches to my Arduino Uno. The following is my failing sketch:

#include "sha256.h"
#include "printf.h"

void printHash(uint8_t* buffer);

void setup() {
  printf_begin();
  Serial.begin(9600);

  uint8_t *hash;
  Sha256.initHmac((uint8_t*)"This key", 8); // key, and length of key in bytes
  Sha256.print("This is a message to hash");
  hash = Sha256.resultHmac();
  printHash(hash);
// Expected: 9b6ddb679a640a975e250771f72d1bdd8fd763fe9df35b560ccaaed7ffe422ce
// Actual: 31738d267649d91117013e9aae5f18f0f00c7b076e85dffaee9cf7ba19ccead3
}

void loop() {}

void printHash(uint8_t* buffer) {
  for (int i = 0; i < 32; i++) {
    printf("%02x", buffer[i]);
  }
  printf("\n");
}

The peculiar thing is that when I use the official Arduino IDE, it actually works as expected. When building with PlatformIO, results are incorrect (so maybe PlatformIO is at fault?).

spaniakos commented 8 years ago

Can you give me some more information for platformIO? So i can helo with the issue. Maybe some platformIO libraries works differentw or some more libraries are required On Mar 1, 2016 08:52, "sterling" notifications@github.com wrote:

I'm not quite sure if this library is at fault yet, but I wanted to see if you have any insights into the issue. I am using PlatformIO ( http://platformio.org/) to edit, compile, and upload my sketches to my Arduino Uno. The following is my failing sketch:

include "sha256.h"

include "printf.h"

void printHash(uint8_t* buffer);

void setup() { printf_begin(); Serial.begin(9600);

uint8_t _hash; Sha256.initHmac((uint8t)"This key", 8); // key, and length of key in bytes Sha256.print("This is a message to hash"); hash = Sha256.resultHmac(); printHash(hash); // Expected: 9b6ddb679a640a975e250771f72d1bdd8fd763fe9df35b560ccaaed7ffe422ce // Actual: 31738d267649d91117013e9aae5f18f0f00c7b076e85dffaee9cf7ba19ccead3 }

void loop() {}

void printHash(uint8_t* buffer) { for (int i = 0; i < 32; i++) { printf("%02x", buffer[i]); } printf("\n"); }

The peculiar thing is that when I use the official Arduino IDE, it actually works as expected. When building with PlatformIO, results are incorrect (so maybe PlatformIO is at fault?).

— Reply to this email directly or view it on GitHub https://github.com/spaniakos/Cryptosuite/issues/8.

sterling commented 8 years ago

Thank you for helping out! I am not using any other libraries with PlatformIO. I have simply installed the PlatformIO IDE, which is integrated with the Atom editor (downloadable here: http://platformio.org/#!/platformio-ide). Also, I'm on OSX 10.11.2 if that makes any difference. Then I setup src/main.cpp with the sketch outlined above and copied your library into lib/sha. After building and uploading to my Uno, I get the unexpected results outlined above.

spaniakos commented 8 years ago

the library works as inteded with the default IDE. saddly i cannot test withy platformio-ide as my current situaition it keeping me away from my equipment for some more months... i can try my best to assist you using no equipment.

sterling commented 8 years ago

What would you suggest I do to debug? Any ideas what might be causing a bad hash?

spaniakos commented 8 years ago

Maybe it has something to do with how the progmem is used, and/or it might need additional libraries. If you can, sent some some output samples of the platformio On Mar 5, 2016 03:05, "sterling" notifications@github.com wrote:

What would you suggest I do to debug? Any ideas what might be causing a bad hash?

— Reply to this email directly or view it on GitHub https://github.com/spaniakos/Cryptosuite/issues/8#issuecomment-192541456 .

sterling commented 8 years ago

I've located the source of the problem. The behavior of Print differs between PlatformIO and the default Arduino IDE. I've changed the example above from:

Sha256.print("This is a message to hash");

to

const char* message = "This is a message to hash";
while(*message) Sha256.print(message++);

This fixes my problem and I get the expected hash. I've also submitted an issue with PlatformIO since the two IDE are behaving differently: https://github.com/platformio/platformio/issues/565

spaniakos commented 8 years ago

I see Therefore i need to make some updates to the print functions in the files. I will do that when i have the chance.

Thanks for the support. On Mar 6, 2016 20:30, "sterling" notifications@github.com wrote:

I've located the source of the problem. The behavior of Print differs between PlatformIO and the default Arduino IDE. I've changed the example above from:

Sha256.print("This is a message to hash");

to

const char* message = "This is a message to hash";while(*message) Sha256.print(message++);

This fixes my problem and I get the expected hash. I've also submitted an issue with PlatformIO since the two IDE are behaving differently: platformio/platformio#565 https://github.com/platformio/platformio/issues/565

— Reply to this email directly or view it on GitHub https://github.com/spaniakos/Cryptosuite/issues/8#issuecomment-192955663 .

sterling commented 8 years ago

Created a PR to address this issue: https://github.com/spaniakos/Cryptosuite/pull/9