hashes.bend checks
raw source on the hub · import bend-osdev-hashes@0.1.0.0/hashes.bend as Hashes
bend-osdev-hashes: the Merkle-Damgard engine every hash of this package streams through, and the word types and byte orders they share.
A hash of this shape keeps a chaining value (any type C) and compresses the message a block at a time (~compress: C -> the block's bytes -> C). The engine holds the bytes of the unfinished block, how many there are, and how many blocks it has compressed; its update takes bytes one at a time, and compresses when a block is full. Because update is a fold over the bytes, feeding a ++ b is feeding a and then b, from every state (LAWS.bend: md_streaming), whatever the compression function; each hash's split law (streaming in any pieces equals the one-shot digest) follows.
Each algorithm's file supplies its chaining value, compression function, and final step (the padding, and the digest from the chaining value): md4.bend (RFC 1320), md5.bend (RFC 1321), sha1.bend (FIPS 180-4), ripemd160.bend (Dobbertin, Bosselaers, Preneel), sm3.bend (GB/T 32905), streebog.bend (RFC 6986), michael.bend (IEEE 802.11i's MIC). The block size is a parameter (~last: the block size less one, a power of two less one for HsMd.md_pad), so 64-byte blocks (all the hashes) and the 4-byte words of Michael run through the same engine.
3 imports
import Base import bend-osdev-bytes@0.1.0.0/bytes.bend as B import bend-osdev-word64@0.1.0.0/word64.bend as W
Types
type HsError source · line 26 · raw
Data
The package's errors: a key of a length the algorithm does not take
HsBadKeyLength@got:Nat -> HsError
type HsMdState source · line 32 · raw
@-C:Data -> Data
The engine's state (opaque): the chaining value, the bytes of the unfinished block (last first), how many there are, and the number of blocks compressed
HsMdState@-C:Data -> @cv:C -> @buf:List<&2, U32> -> @cnt:U32 -> @nb:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> HsMdState<C>
type HsW2 source · line 36 · raw
Data
Chaining values of two, four, five and eight 32-bit words (open types)
HsW2@a:U32 -> @b:U32 -> HsW2
type HsW4 source · line 39 · raw
Data
HsW4@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> HsW4
type HsW5 source · line 42 · raw
Data
HsW5@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> @e:U32 -> HsW5
type HsW8 source · line 45 · raw
Data
HsW8@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> @e:U32 -> @f:U32 -> @g:U32 -> @h:U32 -> HsW8
type HsW16 source · line 49 · raw
Data
The sixteen words of a 64-byte block (internal to the package)
HsW16@x0:U32 -> @x1:U32 -> @x2:U32 -> @x3:U32 -> @x4:U32 -> @x5:U32 -> @x6:U32 -> @x7:U32 -> @x8:U32 -> @x9:U32 -> @x10:U32 -> @x11:U32 -> @x12:U32 -> @x13:U32 -> @x14:U32 -> @x15:U32 -> HsW16
Definitions
def hs.rotl source · line 55 · raw
@+x:U32 -> @+n:Nat -> U32
x rotated left by n (1 to 31)
def hs.le source · line 59 · raw
@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> U32
the word with bytes a (least significant), b, c, d
def hs.be source · line 63 · raw
@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> U32
the word with bytes a (most significant), b, c, d
def hs.block.le source · line 69 · raw
@blk:List<&2, U32> -> HsW16
A block's sixteen words, little-endian (MD4, MD5, RIPEMD-160) or big-endian (SHA-1, SM3); a block of another length gives zeros (the engine passes only whole blocks)
def hs.block.be source · line 76 · raw
@blk:List<&2, U32> -> HsW16
def HsW2.le source · line 86 · raw
@x:HsW2 -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
def HsW4.le source · line 91 · raw
@x:HsW4 -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
def HsW5.le source · line 96 · raw
@x:HsW5 -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
def HsW5.be source · line 101 · raw
@x:HsW5 -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
def HsW8.be source · line 106 · raw
@x:HsW8 -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
def hs.len.le source · line 174 · raw
@v:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> List<&2, U32>
The length field: eight bytes, little-endian (MD4, MD5, RIPEMD-160) or big-endian (SHA-1, SM3)
def hs.len.be source · line 177 · raw
@v:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> List<&2, U32>
Templates
template HsMd.start source · line 114 · raw
@-C:Data -> @cv:C -> HsMdState<C>
the state before any byte, from the initial chaining value
template hs.md.push source · line 117 · raw
@-C:Data -> @-compress:(@_:C -> @_:List<&2, U32> -> C) -> @full:Bool -> @cv:C -> @buf:List<&2, U32> -> @cnt:U32 -> @nb:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> HsMdState<C>
template hs.md.step source · line 125 · raw
@-C:Data -> @-compress:(@_:C -> @_:List<&2, U32> -> C) -> @-last:U32 -> @s:HsMdState<C> -> @b:U32 -> HsMdState<C>
one byte in: a block of last + 1 bytes is compressed when it is full
template hs.md.feed source · line 130 · raw
@-C:Data -> @-compress:(@_:C -> @_:List<&2, U32> -> C) -> @-last:U32 -> @xs:List<&2, U32> -> @s:HsMdState<C> -> HsMdState<C>
template HsMd.update source · line 138 · raw
@-C:Data -> @-compress:(@_:C -> @_:List<&2, U32> -> C) -> @-last:U32 -> @s:HsMdState<C> -> @o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> HsMdState<C>
the state after more bytes
template HsMd.update_list source · line 143 · raw
@-C:Data -> @-compress:(@_:C -> @_:List<&2, U32> -> C) -> @-last:U32 -> @s:HsMdState<C> -> @xs:List<&2, U32> -> HsMdState<C>
the same, for bytes as a list (each element is taken as it is: callers pass bytes)
template HsMd.cv source · line 148 · raw
@-C:Data -> @s:HsMdState<C> -> C
The state's parts: the chaining value, the bytes of the unfinished block (first first), how many there are, and the number of blocks compressed
template HsMd.pending source · line 153 · raw
@-C:Data -> @s:HsMdState<C> -> List<&2, U32>
template HsMd.count source · line 158 · raw
@-C:Data -> @s:HsMdState<C> -> U32
template HsMd.blocks source · line 163 · raw
@-C:Data -> @s:HsMdState<C> -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64
template HsMd.bits source · line 169 · raw
@-C:Data -> @-last:U32 -> @+s:HsMdState<C> -> 0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64
The length so far in bits, modulo 2^64 (blocks of last + 1 bytes)
template HsMd.md_pad source · line 183 · raw
@-last:U32 -> @+cnt:U32 -> @+field:List<&2, U32> -> List<&2, U32>
The Merkle-Damgard padding after cnt bytes of an unfinished block: the byte 0x80, zeros, then the length field, so that the whole is a number of blocks (the block size last + 1 a power of two)
template HsMd.final_md source · line 190 · raw
@-C:Data -> @-compress:(@_:C -> @_:List<&2, U32> -> C) -> @-last:U32 -> @-field:(@_:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> List<&2, U32>) -> @-out:(@_:C -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+s:HsMdState<C> -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
The final step of a Merkle-Damgard hash: the padding with the length in bits (~field writes it: eight bytes, little- or big-endian), through the engine, then the digest of the chaining value (~out)