From 93aa7e3aa291f3328cf34a5a2f86172f41d0affd Mon Sep 17 00:00:00 2001 From: cirakg Date: Sun, 18 Oct 2026 23:44:18 +0200 Subject: test: add cross-checked crypto/OTP vectors, unit tests and stricter screenshots - gen_vectors.py: RFC/FIPS/NIST vectors recomputed with Python's stdlib (openssl for AES) before use, plus seeded cases around SHA padding boundaries, 2038 and 2100; generates test/data/Vectors.mc. - 31 Monkey C tests (hashes, HMAC, PBKDF2, HKDF, AES, sealed records incl. every-byte tamper detection, Base32 errors, 100 OTP and 12 Steam vectors, clock, jobs) and a benchmark; pass on all four test devices. - Screenshot tolerance 0.01 %: one changed character no longer passes. - Golden images updated for version 0.2.0. Co-Authored-By: Claude Opus 5.5 --- test/crypto/HashHmacTest.mc | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 test/crypto/HashHmacTest.mc (limited to 'test/crypto/HashHmacTest.mc') diff --git a/test/crypto/HashHmacTest.mc b/test/crypto/HashHmacTest.mc new file mode 100644 index 0000000..15f166c --- /dev/null +++ b/test/crypto/HashHmacTest.mc @@ -0,0 +1,54 @@ +// Author: cirakg & claude ai +// Summary: SHA-1/256/512 and HMAC tests against FIPS 180-4, RFC 2202, RFC 4231 and seeded padding-boundary vectors. + +import Toybox.Lang; +import Toybox.Test; + +// Every hash vector (FIPS 180-4 examples + messages around the padding boundaries). +(:test) +function hashMatchesVectors(logger as Test.Logger) as Boolean { + var ok = true; + for (var i = 0; i < TestVectors.HASH.size(); i++) { + var r = TestSupport.row(TestVectors.HASH, i); + var actual = Hashes.digest(TestSupport.alg(r, 0), TestSupport.bytes(r, 1)); + ok = TestSupport.sameBytes(logger, "hash vector " + i, actual, TestSupport.bytes(r, 2)) && ok; + } + logger.debug(TestVectors.HASH.size() + " hash vectors"); + return ok; +} + +// Generic RFC 2104 HMAC for all three hashes, and the native HMAC-SHA256. +(:test) +function hmacMatchesRfcVectors(logger as Test.Logger) as Boolean { + var ok = true; + for (var i = 0; i < TestVectors.HMAC.size(); i++) { + var r = TestVectors.HMAC[i] as Array; + var alg = TestSupport.alg(r, 0); + var key = TestSupport.bytes(r, 1); + var data = TestSupport.bytes(r, 2); + var expected = TestSupport.bytes(r, 3); + ok = TestSupport.sameBytes(logger, "generic HMAC " + i, Hmac.generic(alg, key, data), expected) && ok; + ok = TestSupport.sameBytes(logger, "HMAC " + i, Hmac.compute(alg, key, data), expected) && ok; + } + return ok; +} + +// HMAC must not change the caller's key, even for keys longer than a block. +(:test) +function hmacLeavesKeyUntouched(logger as Test.Logger) as Boolean { + var key = new [131]b; + for (var i = 0; i < key.size(); i++) { + key[i] = 0xaa; + } + var before = Bytes.copy(key); + Hmac.generic(Hashes.SHA512, key, [1, 2, 3]b); + Hmac.generic(Hashes.SHA1, key.slice(0, 20), [1, 2, 3]b); + return Bytes.equal(key, before); +} + +(:test) +function hashSizesAndValidity(logger as Test.Logger) as Boolean { + return Hashes.digestSize(Hashes.SHA1) == 20 && Hashes.digestSize(Hashes.SHA256) == 32 + && Hashes.digestSize(Hashes.SHA512) == 64 && Hashes.blockSize(Hashes.SHA512) == 128 + && Hashes.blockSize(Hashes.SHA1) == 64 && Hashes.isValid(2) && !Hashes.isValid(3) && !Hashes.isValid(-1); +} -- cgit v1.2.3