block.bend checks
raw source on the hub · import bend-osdev-crypto@0.1.0.0/block.bend as Block
The block-cipher interface.
A block cipher (template arguments, in this order): ~K: Data its key schedule ~Blk: Data a block ~bsize: Nat the block size in bytes ~key: B.Octets -> Result<&2, &2, CrError, K> the key schedule of a raw key, refusing a key of a length the cipher does not take (CrBadKeyLength{got}) ~enc: K -> Blk -> Blk encrypt one block ~dec: K -> Blk -> Blk decrypt one block and its block format, the bytes of a block: ~blk_of: B.Octets -> Maybe<&2, Blk> bsize bytes as a block (None for any other length) ~blk_to: Blk -> B.Octets the bsize bytes of a block ~bxor: Blk -> Blk -> Blk two blocks XORed A generic algorithm takes only the arguments it uses (CBC encryption: ~K, ~Blk, ~enc, ~bxor). A value of K is only ever made by ~key, so every K is a valid key, and every value of Blk is a block.
Laws, for every key schedule and block (README.md, "Laws"): round_trip dec(k, enc(k, b)) == b round_trip_dual enc(k, dec(k, b)) == b (the cipher is a permutation; where the cipher proves it) format blk_of(blk_to(b)) == Some{b} xor bxor(bxor(a, b), b) == a key_length a key of a length the cipher does not take is refused
The formats of the existing ciphers: inst_cm.bend (cipher-modes' 128-bit CmBlock) and inst_lc.bend (legacy-ciphers' 64-bit LcBlock64). Modes over any block cipher are in modes.bend.
3 imports
import Base import bend-osdev-bytes@0.1.0.0/bytes.bend as B import ./crypto.bend as Cr
Templates
template cr.blk.enc source · line 38 · raw
@-K:Data -> @-Blk:Data -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @m:Maybe<&2, Blk> -> @+k:K -> @+n:Nat -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
template CrBlockCipher.encrypt_octets source · line 46 · raw
@-K:Data -> @-Blk:Data -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+k:K -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
One block of bytes encrypted (CrBadBlockSize for another length)
template CrBlockCipher.decrypt_octets source · line 50 · raw
@-K:Data -> @-Blk:Data -> @-dec:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+k:K -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
One block of bytes decrypted
template CrBlockCipher.encrypt_with source · line 54 · raw
@-K:Data -> @-Blk:Data -> @-key:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, K>) -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @key_o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
The key schedule of a raw key, then one block of bytes encrypted