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 <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <string.h>
// Mock SHA256 class for native testing. // 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 { class SHA256 {
uint32_t state_ = 2166136261u;
public: public:
void update(const void* data, size_t len) { void update(const void*, size_t) {}
update(static_cast<const uint8_t*>(data), len);
}
void update(const uint8_t* data, size_t len) { void update(const uint8_t*, size_t) {}
for (size_t i = 0; i < len; ++i) {
state_ ^= data[i];
state_ *= 16777619u;
state_ += 0x9E3779B9u;
}
}
void finalize(uint8_t* hash, size_t hashLen) { void finalize(uint8_t* hash, size_t hashLen) {
uint32_t value = state_;
for (size_t i = 0; i < hashLen; ++i) { for (size_t i = 0; i < hashLen; ++i) {
value ^= value >> 13; hash[i] = 0x11;
value *= 1274126177u;
hash[i] = static_cast<uint8_t>((value >> ((i & 3) * 8)) & 0xFF);
} }
} }
void resetHMAC(const uint8_t* key, size_t keyLen) { void resetHMAC(const uint8_t*, size_t) {}
state_ = 2166136261u;
update(key, keyLen);
state_ ^= 0x36363636u;
}
void finalizeHMAC(const uint8_t* key, size_t keyLen, uint8_t* hash, size_t hashLen) { void finalizeHMAC(const uint8_t*, size_t, uint8_t* hash, size_t hashLen) {
update(key, keyLen);
finalize(hash, hashLen); finalize(hash, hashLen);
} }
}; };

Loading…
Cancel
Save