Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-crypto

Source of release 0.6.0

0.6.0 / src / hash_kat_tests.lucb

## Hash, HMAC and HKDF known answers: every BearSSL test_crypto.c vector for
## these primitives (with its one-shot, byte-by-byte and split-and-copy update
## patterns) and the complete Wycheproof hmac_sha256/384/512 and
## hkdf_sha256/384 files, and OpenSSL's evpmd_sha, HMAC and HKDF stanzas. Vectors for algorithms this package does not
## implement (MD5, HMAC/HKDF over SHA-1) are counted and reported, not run.
import sha1
import sha2
import hmac
import hkdf
import vector_file

## Pass, out-of-scope and rejection counts for one run.
struct Tally:
    var passed: usize
    var out_of_scope: usize
    var rejected: usize

## The SHA-2 algorithm named by a vector field, or "" for any other.
func sha2_name(name: const u8[]) -> str:
    if name == b"sha224": return "sha224"
    if name == b"sha256": return "sha256"
    if name == b"sha384": return "sha384"
    if name == b"sha512": return "sha512"
    return ""

## A SHA-1 or SHA-2 digest of `data` fed in pieces of `step` bytes (0: whole).
func digest(name: const u8[], data: const u8[], step: usize, out: u8[]) -> usize!:
    if name == b"sha1":
        var state = sha1.make()
        try feed_sha1(&state, data, step)
        return try state.finish(out)
    var state = try sha2.make_hash(sha2_name(name))
    defer state.close()
    var at: usize = 0
    while at < data.length:
        let take = data.length - at if step == 0 or data.length - at < step else step
        try state.update(data[at..<at + take])
        at += take
    return try state.digest(out)

func feed_sha1(state: sha1.Hash*, data: const u8[], step: usize) -> !:
    var at: usize = 0
    while at < data.length:
        let take = data.length - at if step == 0 or data.length - at < step else step
        try state.update(data[at..<at + take])
        at += take

## Split at every position; continue the original and a copy (BearSSL KAT 3/4).
func split_and_copy(name: const u8[], data: const u8[], expected: const u8[]) -> !:
    var out: u8[64]
    for u in 0..<data.length:
        if name == b"sha1":
            var first = sha1.make()
            try first.update(data[..<u])
            var second = first
            try first.update(data[u..])
            try second.update(data[u..])
            assert(out[..<try first.finish(out)] == expected, "SHA-1 split mismatch")
            assert(out[..<try second.finish(out)] == expected, "SHA-1 copy mismatch")
        else:
            var first = try sha2.make_hash(sha2_name(name))
            try first.update(data[..<u])
            var second = first
            try first.update(data[u..])
            try second.update(data[u..])
            assert(out[..<try first.digest(out)] == expected, "SHA-2 split mismatch")
            assert(out[..<try second.digest(out)] == expected, "SHA-2 copy mismatch")
            first.close()
            second.close()

func bearssl_hashes(directory: str, tally: Tally*) -> !:
    var lines = try vector_file.Lines.open(directory, "bearssl/hash.txt")
    defer lines.close()
    var data: u8[1024]
    var expected: u8[64]
    var out: u8[64]
    while lines.next():
        let name = try lines.field(0)
        if name == b"md5":
            tally.out_of_scope += 1
            continue
        let size = try lines.decode(2, expected)
        if try lines.is(1, "million"):
            var block: u8[1000]
            for i in 0..<block.length: block[i] = 'a'
            if name == b"sha1":
                var state = sha1.make()
                for _ in 0..<(usize)1000: try state.update(block)
                assert(out[..<try state.finish(out)] == expected[..<size], "SHA-1 million-a")
            else:
                var million = try sha2.make_hash(sha2_name(name))
                defer million.close()
                for _ in 0..<(usize)1000: try million.update(block)
                assert(out[..<try million.digest(out)] == expected[..<size], "SHA-2 million-a")
            tally.passed += 1
            continue
        let n = try lines.decode(1, data)
        assert(out[..<try digest(name, data[..<n], 0, out)] == expected[..<size], "hash one-shot")
        assert(out[..<try digest(name, data[..<n], 1, out)] == expected[..<size], "hash byte-by-byte")
        try split_and_copy(name, data[..<n], expected[..<size])
        tally.passed += 1

func mac(algorithm: str, key: const u8[], data: const u8[], step: usize, out: u8[]) -> usize!:
    var context = hmac.Context()
    defer context.close()
    try context.start(key, algorithm)
    var at: usize = 0
    while at < data.length:
        let take = data.length - at if step == 0 or data.length - at < step else step
        try context.update(data[at..<at + take])
        at += take
    return try context.finish(out)

func bearssl_hmac(directory: str, tally: Tally*) -> !:
    var lines = try vector_file.Lines.open(directory, "bearssl/hmac.txt")
    defer lines.close()
    var key: u8[256]
    var data: u8[256]
    var expected: u8[64]
    var out: u8[64]
    while lines.next():
        let algorithm = sha2_name(try lines.field(0))
        if algorithm.length == 0:
            tally.out_of_scope += 1
            continue
        let k = try lines.decode(1, key)
        let n = try lines.decode(2, data)
        let size = try lines.decode(3, expected)
        _ = try mac(algorithm, key[..<k], data[..<n], 0, out)
        assert(out[..<size] == expected[..<size], "HMAC one-shot")
        _ = try mac(algorithm, key[..<k], data[..<n], 1, out)
        assert(out[..<size] == expected[..<size], "HMAC byte-by-byte")
        tally.passed += 1

func bearssl_hkdf(directory: str, tally: Tally*) -> !:
    var lines = try vector_file.Lines.open(directory, "bearssl/hkdf.txt")
    defer lines.close()
    var ikm: u8[128]
    var salt: u8[128]
    var info: u8[128]
    var expected: u8[128]
    var out: u8[128]
    while lines.next():
        let algorithm = sha2_name(try lines.field(0))
        if algorithm.length == 0:
            tally.out_of_scope += 1
            continue
        let i = try lines.decode(1, ikm)
        var s: usize = 0
        if not (try lines.is(2, "nosalt")):
            s = try lines.decode(2, salt)
        let f = try lines.decode(3, info)
        let size = try lines.decode(4, expected)
        try hkdf.derive(ikm[..<i], salt[..<s], info[..<f], out[..<size], algorithm)
        assert(out[..<size] == expected[..<size], "HKDF mismatch")
        tally.passed += 1

func wycheproof_hmac(directory: str, name: str, algorithm: str, tally: Tally*) -> !:
    var lines = try vector_file.Lines.open(directory, name)
    defer lines.close()
    var key: u8[128]
    var data: u8[256]
    var tag: u8[64]
    var out: u8[64]
    var tag_size: usize = 0
    while lines.next():
        if try lines.is(0, "group"):
            let bits = try lines.field(1)
            tag_size = 0
            for i in 0..<bits.length: tag_size = tag_size * 10 + (usize)(bits[i] - '0')
            tag_size = tag_size // 8
            continue
        let k = try lines.decode(1, key)
        let n = try lines.decode(2, data)
        let t = try lines.decode(3, tag)
        _ = try mac(algorithm, key[..<k], data[..<n], 0, out)
        let accepted = t == tag_size and out[..<tag_size] == tag[..<t]
        if try lines.is(0, "valid"):
            assert(accepted, "valid Wycheproof HMAC rejected")
            tally.passed += 1
        else:
            assert(not accepted, "invalid Wycheproof HMAC accepted")
            tally.rejected += 1

func wycheproof_hkdf(directory: str, name: str, algorithm: str, tally: Tally*) -> !:
    var lines = try vector_file.Lines.open(directory, name)
    defer lines.close()
    var ikm: u8[256]
    var salt: u8[256]
    var info: u8[256]
    var size_bytes: u8[2]
    var expected: u8[16384]
    var out: u8[16384]
    while lines.next():
        let i = try lines.decode(1, ikm)
        let s = try lines.decode(2, salt)
        let f = try lines.decode(3, info)
        _ = try lines.decode(4, size_bytes)
        let size = ((usize)size_bytes[0] << 8) | (usize)size_bytes[1]
        let e = try lines.decode(5, expected)
        var accepted = true
        hkdf.derive(ikm[..<i], salt[..<s], info[..<f], out[..<size], algorithm) catch failure:
            assert(failure.code == sha2.invalid)
            accepted = false
            recover ()
        if try lines.is(0, "valid"):
            assert(accepted and e == size and out[..<size] == expected[..<e], "valid Wycheproof HKDF failed")
            tally.passed += 1
        else:
            assert(not accepted, "invalid Wycheproof HKDF size accepted")
            tally.rejected += 1

func decimal(field: const u8[]) -> usize:
    var value: usize = 0
    for i in 0..<field.length: value = value * 10 + (usize)(field[i] - '0')
    return value

## OpenSSL evpmd_sha.txt: each input chunk repeated Ncopy times and fed
## Count times, in order.
func openssl_digest(directory: str, tally: Tally*) -> !:
    var lines = try vector_file.Lines.open(directory, "openssl/digest.txt")
    defer lines.close()
    var expected: u8[64]
    var out: u8[64]
    while lines.next():
        let name = try lines.field(0)
        let size = try lines.decode(1, expected)
        var one = sha1.make()
        var two = try sha2.make_hash(sha2_name(name) if name != b"sha1" else "sha256")
        defer two.close()
        var index: usize = 2
        while index < lines.count():
            var chunk: u8[256]
            let text = try lines.field(index)
            var parts: usize[3]
            var part: usize = 0
            for i in 0..<text.length:
                if text[i] == ':':
                    parts[part] = i
                    part += 1
            let n = try vector_file.decode_hex(text[..<parts[0]], chunk)
            let copies = decimal(text[parts[0] + 1..<parts[1]])
            let count = decimal(text[parts[1] + 1..])
            for _ in 0..<count:
                for _ in 0..<copies:
                    if name == b"sha1":
                        try one.update(chunk[..<n])
                    else:
                        try two.update(chunk[..<n])
            index += 1
        if name == b"sha1":
            assert(out[..<try one.finish(out)] == expected[..<size], "OpenSSL SHA-1 mismatch")
        else:
            assert(out[..<try two.digest(out)] == expected[..<size], "OpenSSL SHA-2 mismatch")
        tally.passed += 1

## OpenSSL evpmac_common.txt HMAC stanzas over SHA-1/SHA-2 (SHA-1 HMAC is
## not implemented and is counted out of scope).
func openssl_hmac(directory: str, tally: Tally*) -> !:
    var lines = try vector_file.Lines.open(directory, "openssl/hmac.txt")
    defer lines.close()
    var key: u8[256]
    var data: u8[512]
    var expected: u8[64]
    var out: u8[64]
    while lines.next():
        let algorithm = sha2_name(try lines.field(0))
        if algorithm.length == 0:
            tally.out_of_scope += 1
            continue
        assert(try lines.is(4, "ok"))
        let k = try lines.decode(1, key)
        let n = try lines.decode(2, data)
        let size = try lines.decode(3, expected)
        _ = try mac(algorithm, key[..<k], data[..<n], 0, out)
        assert(out[..<size] == expected[..<size], "OpenSSL HMAC mismatch")
        tally.passed += 1

## OpenSSL evpkdf_hkdf.txt and evppkey_kdf_hkdf.txt: extract-and-expand,
## extract only and expand only, including the expected failures (no digest,
## no key, an extract output that is not the hash size).
func openssl_hkdf(directory: str, tally: Tally*) -> !:
    var lines = try vector_file.Lines.open(directory, "openssl/hkdf.txt")
    defer lines.close()
    var ikm: u8[256]
    var salt: u8[256]
    var info: u8[256]
    var expected: u8[512]
    var out: u8[512]
    while lines.next():
        let digest_name = try lines.field(1)
        let ok = try lines.is(6, "ok")
        let algorithm = sha2_name(digest_name)
        if algorithm.length == 0 and not (try lines.is(1, "absent")):
            tally.out_of_scope += 1
            continue
        let size = try lines.decode(5, expected)
        var accepted = algorithm.length != 0 and not (try lines.is(2, "absent"))
        if accepted:
            let i = try lines.decode(2, ikm)
            let s = 0 if try lines.is(3, "absent") else try lines.decode(3, salt)
            let f = 0 if try lines.is(4, "absent") else try lines.decode(4, info)
            if try lines.is(0, "extract"):
                if size != try hmac_size(algorithm):
                    accepted = false
                else:
                    _ = try hkdf.extract(ikm[..<i], salt[..<s], out, algorithm)
            elif try lines.is(0, "expand"):
                try hkdf.expand(ikm[..<i], info[..<f], out[..<size], algorithm)
            else:
                try hkdf.derive(ikm[..<i], salt[..<s], info[..<f], out[..<size], algorithm)
        if ok:
            assert(accepted and out[..<size] == expected[..<size], "OpenSSL HKDF mismatch")
            tally.passed += 1
        else:
            assert(not accepted, "OpenSSL HKDF error case accepted")
            tally.rejected += 1

func hmac_size(algorithm: str) -> usize!:
    var state = try sha2.make_hash(algorithm)
    defer state.close()
    return try state.digest_size()

## Optional argument: the vector directory (default tests/vectors).
pub func main(arguments: str[]) -> i32!:
    let directory = arguments[1] if arguments.length > 1 else "tests/vectors"
    var bearssl = Tally()
    try bearssl_hashes(directory, &bearssl)
    try bearssl_hmac(directory, &bearssl)
    try bearssl_hkdf(directory, &bearssl)
    var wycheproof = Tally()
    try wycheproof_hmac(directory, "wycheproof/hmac_sha256.txt", "sha256", &wycheproof)
    try wycheproof_hmac(directory, "wycheproof/hmac_sha384.txt", "sha384", &wycheproof)
    try wycheproof_hmac(directory, "wycheproof/hmac_sha512.txt", "sha512", &wycheproof)
    try wycheproof_hkdf(directory, "wycheproof/hkdf_sha256.txt", "sha256", &wycheproof)
    try wycheproof_hkdf(directory, "wycheproof/hkdf_sha384.txt", "sha384", &wycheproof)
    var openssl = Tally()
    try openssl_digest(directory, &openssl)
    try openssl_hmac(directory, &openssl)
    try openssl_hkdf(directory, &openssl)
    assert(bearssl.passed == 42 and bearssl.out_of_scope == 26, "unexpected BearSSL hash/HMAC/HKDF counts")
    assert(wycheproof.passed == 361 and wycheproof.rejected == 330, "unexpected Wycheproof HMAC/HKDF counts")
    assert(openssl.passed == 46 and openssl.rejected == 2 and openssl.out_of_scope == 40, "unexpected OpenSSL hash/HMAC/HKDF counts")
    print(f"PASS hash/HMAC/HKDF: BearSSL {bearssl.passed} (+{bearssl.out_of_scope} MD5/SHA-1-keyed out of scope), Wycheproof {wycheproof.passed} valid, {wycheproof.rejected} invalid, OpenSSL {openssl.passed} ok, {openssl.rejected} errors (+{openssl.out_of_scope} SHA-1/other-hash HKDF out of scope)")
    return 0