intrbiz / arduino-crypto

A minimal crypto library for ESP8266 Arduino
Other
140 stars 53 forks source link

HMAC-SHA-256 not correctly calculated #4

Closed pasko-zh closed 7 years ago

pasko-zh commented 7 years ago

Hello!

I am calculating a HMAC-SHA-256 with your library (latest version cloned) as follows (snippet):

#include <Crypto.h>

byte calculated_HMAC[32];

// "Jefe"
byte testvector_2_key[4] = {74, 101, 102, 101}; 

SHA256HMAC hmac_for_check(testvector_2_key, 4);
hmac_for_check.doUpdate("what do ya want for nothing?");
hmac_for_check.doFinal(calculated_HMAC);
int i;
for (i = 0; i < 32; i++) {
  Serial.print("HMAC is:");
  Serial.println(calculated_HMAC[i], HEX);
}

Which then gives:

HMAC in hex is:89
HMAC in hex is:A1
HMAC in hex is:B9
HMAC in hex is:C1
HMAC in hex is:26
HMAC in hex is:E4
HMAC in hex is:CB
HMAC in hex is:FE
HMAC in hex is:0
HMAC in hex is:11
HMAC in hex is:B6
HMAC in hex is:9C
HMAC in hex is:87
HMAC in hex is:E2
HMAC in hex is:39
HMAC in hex is:50
HMAC in hex is:30
HMAC in hex is:96
HMAC in hex is:77
HMAC in hex is:9C
HMAC in hex is:47
HMAC in hex is:FB
HMAC in hex is:88
HMAC in hex is:9
HMAC in hex is:77
HMAC in hex is:3C
HMAC in hex is:A9
HMAC in hex is:1A
HMAC in hex is:FE
HMAC in hex is:63
HMAC in hex is:28
HMAC in hex is:80

According to rfc4231 section 4.3. Test Case 2 it should give instead:

HMAC-SHA-256 = 5bdcc146bf60754e6a042426089575c7
                  5a003f089d2739839dec58b964ec3843 

When I use my python code or on online Tool, e.g., HMAC generator it gives the HMAC-SHA-256 as in the rfc.

This observation makes me conclude that your library does not calculate HMAC-SHA-256 correctly.

Could you please have a look at it?

Thanks a lot, paško