inst_crc.bend

bend-osdev-crypto@0.1.0.0 · 43 lines · 8 definitions

1# CRCs as hashes: bend-osdev-crc's CRC-32 family (any Crc32Spec: widths 8 to
2# 32) and CRC-64 family (any Crc64Spec), through its streaming interface
3# (start, update, finish; the bitwise reference engine). A CRC is not a
4# cryptographic hash: these instances are for checksums, and for exercising
5# the generic algorithms (HMAC, HKDF) over a second and third hash.
6#
7# The state is the register; the digest is the final value, big-endian (4
8# bytes for CRC-32, 8 for CRC-64; a CRC narrower than 32 bits has leading
9# zero bytes). The block size is a parameter of HMAC only (a CRC has none);
10# 64 is the named default. The instance for the CRC-32 of spec:
11#   ~U32, ~64n, ~4n, ~CrCrc32.init(~spec), ~CrCrc32.update(~spec),
12#   ~CrCrc32.final(~spec), ~CrCrc32.digest(~spec)
13# with spec C.crc32.spec(), C.crc32c.spec(), ...; for CRC-64:
14#   ~W.W64, ~64n, ~8n, ~CrCrc64.init(~spec), ~CrCrc64.update(~spec),
15#   ~CrCrc64.final(~spec), ~CrCrc64.digest(~spec)
16import Base
17import bend-osdev-bytes@0.1.0.0/bytes.bend as B
18import bend-osdev-word64@0.1.0.0/word64.bend as W
19import bend-osdev-crc@0.1.0.0/crc.bend as C
20
21def CrCrc32.init(~spec: C.Crc32Spec) -> U32:
22  C.Crc32Spec.start(spec)
23
24def CrCrc32.update(~spec: C.Crc32Spec, c: U32, o: B.Octets) -> U32:
25  C.Crc32Spec.update(spec, c, o)
26
27def CrCrc32.final(~spec: C.Crc32Spec, c: U32) -> B.Octets:
28  B.Octets.be32(C.Crc32Spec.finish(spec, c))
29
30def CrCrc32.digest(~spec: C.Crc32Spec, o: B.Octets) -> B.Octets:
31  CrCrc32.final(~spec, CrCrc32.update(~spec, CrCrc32.init(~spec), o))
32
33def CrCrc64.init(~spec: C.Crc64Spec) -> W.W64:
34  C.Crc64Spec.start(spec)
35
36def CrCrc64.update(~spec: C.Crc64Spec, c: W.W64, o: B.Octets) -> W.W64:
37  C.Crc64Spec.update(spec, c, o)
38
39def CrCrc64.final(~spec: C.Crc64Spec, c: W.W64) -> B.Octets:
40  B.Octets.be64(C.Crc64Spec.finish(spec, c))
41
42def CrCrc64.digest(~spec: C.Crc64Spec, o: B.Octets) -> B.Octets:
43  CrCrc64.final(~spec, CrCrc64.update(~spec, CrCrc64.init(~spec), o))