cts.bend checks
raw source on the hub · import bend-osdev-crypto@0.1.0.0/cts.bend as Cts
CBC with ciphertext stealing over any block cipher (the block-cipher interface, block.bend), in the variant CBC-CS3 of NIST SP 800-38A Addendum, "Three Variants of Ciphertext Stealing for CBC Mode" (2010), the one Kerberos 5 (RFC 3962) and the Linux kernel's "cts" use: a message of at least one block, of any length, gives a ciphertext of the same length.
For a message of exactly one block: C = E(P xor IV) (plain CBC). Longer, with the message cut into P_1 .. P_(n-1) whole blocks and a last piece P_n* of d bytes (1 to b, the block size b): C_1 .. C_(n-1) = CBC of P_1 .. P_(n-1) from the IV C_n = E((P_n* || 0^(b-d)) xor C_(n-1)) the ciphertext: C_1 .. C_(n-2), C_n, the first d bytes of C_(n-1) (CS3 swaps the last two blocks always, even when d = b). Decryption reads C_n, recovers C_(n-1) from its first d bytes and the last b - d bytes of D(C_n), and P_n* as the first d bytes of D(C_n) xor C_(n-1).
The cipher works on bytes here (a block the format refuses stays as it is). A message shorter than a block is refused (CrTooShort), and so is an IV that is not one block (CrBadIvLength).
4 imports
import Base import bend-osdev-bytes@0.1.0.0/bytes.bend as B import ./crypto.bend as Cr import ./octets.bend as O
Definitions
def cr.cts.rcat source · line 37 · raw
@xs:List<&2, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets> -> @acc:List<&2, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets> -> List<&2, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
the pieces in order, as byte strings, onto acc
def cr.cts.out source · line 44 · raw
@acc:List<&2, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets> -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
def cr.cts.check source · line 92 · raw
@+bsize:Nat -> @+iv:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @+n:Nat -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, Unit>
Templates
template cr.cts.b.go source · line 26 · raw
@-K:Data -> @-Blk:Data -> @-f:(@_:K -> @_:Blk -> Blk) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+k:K -> @x:Maybe<&2, Blk> -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
a block of bytes encrypted or decrypted (f is the cipher's enc or dec)
template cr.cts.b source · line 33 · raw
@-K:Data -> @-Blk:Data -> @-f:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+k:K -> @+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
template cr.cts.enc.go source · line 49 · raw
@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @ps:List<&2, List<&2, U32>> -> @+k:K -> @+v:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @acc:List<&2, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets> -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
Encryption over the pieces (at least two; the last 1 to b bytes) from the chaining value v; acc holds the ciphertext so far, last first
template cr.cts.dec.go source · line 70 · raw
@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-dec:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @ps:List<&2, List<&2, U32>> -> @+k:K -> @+v:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @acc:List<&2, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets> -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
Decryption over the pieces from the chaining value v
template CrCts.encrypt source · line 98 · raw
@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-enc:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+k:K -> @+iv:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @+m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
CBC-CS3 encryption of m (at least one block) under a one-block IV
template CrCts.decrypt source · line 102 · raw
@-K:Data -> @-Blk:Data -> @-bsize:Nat -> @-dec:(@_:K -> @_:Blk -> Blk) -> @-blk_of:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Maybe<&2, Blk>) -> @-blk_to:(@_:Blk -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+k:K -> @+iv:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @+c:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
CBC-CS3 decryption