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

Mismatch between expected and actual results #7

Closed axelbusch closed 6 years ago

axelbusch commented 8 years ago

I'm not sure where the mismatch is coming. Can you help me?

I set sha256K[] PROGMEM and sha256InitState[] PROGMEM to const

Test: RFC4231 4.2 Expect:b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7 Result:017c04f38fdc5e8b6a754fd3875680925d11dd325a04c55c1ac1468bed4fa1af Hash took : 82160 micros

Test: RFC4231 4.3 Expect:5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843 Result:2ad5636de0f08f7b0ecdec6db61026636820bb79bba11a13f30ea122f14ae4e2 Hash took : 82160 micros

Test: RFC4231 4.4 Expect:773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe Result:773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe Hash took : 82160 micros

Test: RFC4231 4.5 Expect:82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b Result:82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b Hash took : 82160 micros

Test: RFC4231 4.6 Expect:a3b6167473100ee06e0c796c2955552b------------------------------- Result:b5d822588417b0fac1b7d99ba44772842f95818d418aca4a7ba739be7e445ac5 Hash took : 82160 micros

Test: RFC4231 4.7 Expect:60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54 Result:504635cd34096118a1548accaec3c388d8b0c22faf9d362e8ed19fe03d436864 Hash took : 91104 micros

Test: RFC4231 4.8 Expect:9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2 Result:504635cd34096118a1548accaec3c388d8b0c22faf9d362e8ed19fe03d436864 Hash took : 91140 micros

axelbusch commented 8 years ago

ok found out that using for (int i = 0; i < 8; ++i) Sha256.write(cyphertext[i]); instead of Sha256.print("Hi There"); maybe any problems in Sha256.print?

spaniakos commented 8 years ago

can you post your sketch with the missmatch? I will be able to check after 3 of january as i dont have access toy equipment until then. On Dec 29, 2015 6:42 PM, "axelbusch" notifications@github.com wrote:

ok found out that using for (int i = 0; i < 8; ++i) Sha256.write(cyphertext[i]); instead of Sha256.print("Hi There"); maybe any problems in Sha256.print?

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

axelbusch commented 8 years ago
void setup() {
  printf_begin();
  uint8_t* hash;
  uint32_t a;
  unsigned long ms;
  Serial.begin(9600);

  // HMAC tests
  Serial.println("Test: RFC4231 4.2");
  Serial.println("Expect:b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7");
  Serial.print("Result:");
  ms = micros();
  Sha256.initHmac(hmacKey1,20);
  const char* cyphertext = "Hi There";
  for (int i = 0; i < 8; ++i)
    Sha256.write(cyphertext[i]);
  printHash(Sha256.resultHmac());
  Serial.print(" Hash took : ");
  Serial.print((micros() - ms));
  Serial.println(" micros");
  Serial.println();

  // HMAC tests
  Serial.println("Test: RFC4231 4.2");
  Serial.println("Expect:b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7");
  Serial.print("Result:");
  ms = micros();
  Sha256.initHmac(hmacKey1,20);
  Sha256.print("Hi There");
  printHash(Sha256.resultHmac());
  Serial.print(" Hash took : ");
  Serial.print((micros() - ms));
  Serial.println(" micros");
  Serial.println();

}

shows

Test: RFC4231 4.2
Expect:b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7
Result:b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7
 Hash took : 82160 micros

Test: RFC4231 4.2
Expect:b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7
Result:017c04f38fdc5e8b6a754fd3875680925d11dd325a04c55c1ac1468bed4fa1af
 Hash took : 82160 micros
spaniakos commented 7 years ago

this class inherits from the print.h of arduino. the print.h has:

virtual size_t write(uint8_t) = 0; size_t write(const char str) { if (str == NULL) return 0; return write((const uint8_t )str, strlen(str)); }

and the calss adds a handle for : size_t Sha1Class::write(uint8_t data) { ++byteCount; addUncounted(data); return 1; }

therefore it routes the print to the internal write function. if the print.h of you board doesnt have that virtual function in the print.h. then the write is not handled properly.

please check and get back to me.

thanks spaniakos