Browse Source

Simplify to a more obviously fake SHA256 algorithm

pull/2459/head
Michael Lynch 1 month ago
parent
commit
837fcf9623
  1. 33
      test/mocks/SHA256.h

33
test/mocks/SHA256.h

@ -2,43 +2,26 @@
#include <stdint.h>
#include <stddef.h>
#include <string.h>
// Mock SHA256 class for native testing.
// Provides a deterministic stand-in so code can hash data without Arduino crypto deps.
// Provides a fixed stand-in so code can compile without Arduino crypto deps.
// update() and resetHMAC() ignore all input; finalize() and finalizeHMAC()
// fill the requested output buffer with 0x11 bytes.
class SHA256 {
uint32_t state_ = 2166136261u;
public:
void update(const void* data, size_t len) {
update(static_cast<const uint8_t*>(data), len);
}
void update(const void*, size_t) {}
void update(const uint8_t* data, size_t len) {
for (size_t i = 0; i < len; ++i) {
state_ ^= data[i];
state_ *= 16777619u;
state_ += 0x9E3779B9u;
}
}
void update(const uint8_t*, size_t) {}
void finalize(uint8_t* hash, size_t hashLen) {
uint32_t value = state_;
for (size_t i = 0; i < hashLen; ++i) {
value ^= value >> 13;
value *= 1274126177u;
hash[i] = static_cast<uint8_t>((value >> ((i & 3) * 8)) & 0xFF);
hash[i] = 0x11;
}
}
void resetHMAC(const uint8_t* key, size_t keyLen) {
state_ = 2166136261u;
update(key, keyLen);
state_ ^= 0x36363636u;
}
void resetHMAC(const uint8_t*, size_t) {}
void finalizeHMAC(const uint8_t* key, size_t keyLen, uint8_t* hash, size_t hashLen) {
update(key, keyLen);
void finalizeHMAC(const uint8_t*, size_t, uint8_t* hash, size_t hashLen) {
finalize(hash, hashLen);
}
};

Loading…
Cancel
Save