stream.bend checks
raw source on the hub · import bend-osdev-crypto@0.1.0.0/stream.bend as Stream
The stream-cipher interface, and stream ciphers built from a keystream generator.
The interface (template arguments, in this order): ~S: Data the state: key, IV and position ~O: Data what one step answers ~start: B.Octets -> B.Octets -> Result<&2, &2, CrError, S> the state for a key and an IV (a cipher without an IV takes the empty one) ~update: S -> B.Octets -> O the next piece of data, encrypted or decrypted (the same thing) ~out_state: O -> S the state after the piece ~out_data: O -> B.Octets the piece encrypted Laws, for every state and all data (README.md, "Laws"): involution out_data(update(s, out_data(update(s, m)))) == m streaming_data out_data(update(s, a ++ b)) == out_data(update(s, a)) ++ out_data(update(out_state(update(s, a)), b)) streaming_state out_state(update(s, a ++ b)) == out_state(update(out_state(update(s, a)), b))
Keystream generation is the extension point: CrXor turns any function that gives the next keystream byte and the next state (~next: S -> CrKsStep<S>) into a stream cipher with exactly this interface, and LAWS.bend proves its three laws for every such function, once. CTR mode (modes.bend) is one; RC4 (inst_rc4.bend) satisfies the interface directly.
3 imports
import Base import bend-osdev-bytes@0.1.0.0/bytes.bend as B import ./crypto.bend as Cr
Types
type CrKsStep source · line 43 · raw
@-S:Data -> Data
one keystream byte and the state after it
CrKsStep@-S:Data -> @k:U32 -> @st:S -> CrKsStep<S>
type CrXorOut source · line 48 · raw
@-S:Data -> Data
what CrXor.update answers: the state after the data, and the data encrypted (open type)
CrXorOut@-S:Data -> @state:S -> @data:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> CrXorOut<S>
type CrKsRun source · line 79 · raw
@-S:Data -> Data
The loop that runs: tail-recursive, the output kept reversed, each keystream byte computed once
CrKsRun@-S:Data -> @st:S -> @rev:List<&2, U32> -> CrKsRun<S>
Definitions
def cr.rev_onto source · line 82 · raw
@xs:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>
Templates
template CrStream.crypt source · line 33 · raw
@-S:Data -> @-O:Data -> @-start:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, S>) -> @-update:(@_:S -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> O) -> @-out_data:(@_:O -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @key:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @iv:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
A stream cipher in one call: the data under a key and an IV
template CrStream.keystream source · line 37 · raw
@-S:Data -> @-O:Data -> @-update:(@_:S -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> O) -> @-out_data:(@_:O -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @s:S -> @n:Nat -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
The next n keystream bytes (the data n zero bytes)
template CrKsStep.k source · line 51 · raw
@-S:Data -> @r:CrKsStep<S> -> U32
template CrKsStep.st source · line 56 · raw
@-S:Data -> @r:CrKsStep<S> -> S
template cr.ks.xs source · line 63 · raw
@-S:Data -> @-next:(@_:S -> CrKsStep<S>) -> @ys:List<&2, U32> -> @+s:S -> List<&2, U32>
The structural forms the proofs reason about: the data, byte by byte, and the state after it
template cr.ks.after source · line 70 · raw
@-S:Data -> @-next:(@_:S -> CrKsStep<S>) -> @ys:List<&2, U32> -> @+s:S -> S
template cr.ks.go source · line 89 · raw
@-S:Data -> @-next:(@_:S -> CrKsStep<S>) -> @ys:List<&2, U32> -> @+s:S -> @acc:List<&2, U32> -> CrKsRun<S>
template cr.ks.out source · line 97 · raw
@-S:Data -> @r:CrKsRun<S> -> CrXorOut<S>
template CrXor.update source · line 104 · raw
@-S:Data -> @-next:(@_:S -> CrKsStep<S>) -> @s:S -> @m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> CrXorOut<S>
The stream cipher of a keystream generator: m XORed with the next len(m) keystream bytes, and the state after them
template CrXor.out_state source · line 107 · raw
@-S:Data -> @o:CrXorOut<S> -> S
template CrXor.out_data source · line 112 · raw
@-S:Data -> @o:CrXorOut<S> -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets