~/bend-docscommunity

modes64.bend checks

raw source on the hub · import bend-osdev-legacy-ciphers@0.1.0.0/modes64.bend as Modes64

Modes of operation for 64-bit block ciphers (DES, 3DES): ECB and CBC (NIST SP 800-38A, sections 6.1 and 6.2), over any cipher on LcBlock64.

cipher-modes' modes are written for 128-bit blocks (CmBlock), so a 64-bit cipher cannot use them; these are the same modes on LcBlock64, with the cipher as template arguments in cipher-modes' style: ~K: Data the cipher's (expanded) key ~enc: K -> LcBlock64 -> LcBlock64 encrypt one block ~dec: K -> LcBlock64 -> LcBlock64 decrypt one block for instance ~D.LcDesKey, ~D.LcDes.encrypt, ~D.LcDes.decrypt. For padding, cipher-modes' Pkcs7.pad(8n, m) and Pkcs7.unpad(8n, m) take any block size.

The block layer works on lists of blocks: chaining one call's last ciphertext block into the next call's IV encrypts a long message in pieces. The byte layer takes Octets of whole blocks.

3 imports
import Base
import bend-osdev-bytes@0.1.0.0/bytes.bend as B
import ./legacy_ciphers.bend as L

Definitions

def m64.rev_onto source · line 50 · raw

@bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> @acc:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

def m64.split source · line 96 · raw

@xs:List<&2, U32> -> @acc:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> Maybe<&2, List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>>

the bytes as whole blocks (reversed onto acc), or None if bytes are left over

def m64.join source · line 106 · raw

@rs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> @acc:List<&2, U32> -> List<&2, U32>

the bytes of the blocks (reversed) in front of acc

def m64.octets source · line 113 · raw

@bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets

def m64.blocks source · line 116 · raw

@+n:Nat -> @m:Maybe<&2, List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>> -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>>

def Lc64.blocks_of source · line 124 · raw

@+o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>>

the message as whole blocks, or LcNotBlockMultiple

def Lc64.octets_of source · line 128 · raw

@bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets

the blocks' bytes, in order

def m64.iv source · line 131 · raw

@m:Maybe<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> @+n:Nat -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

Templates

template m64.ecb source · line 22 · raw

@-K:Data -> @-f:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

template m64.cbc.enc source · line 30 · raw

@-K:Data -> @-enc:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> @iv:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

C_1 = E(P_1 xor IV), C_j = E(P_j xor C_(j-1))

template m64.cbc.dec source · line 39 · raw

@-K:Data -> @-dec:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> @iv:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

P_1 = D(C_1) xor IV, P_j = D(C_j) xor C_(j-1)

template m64.ecb.go source · line 57 · raw

@-K:Data -> @-f:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> @acc:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

template m64.cbc.enc.go source · line 64 · raw

@-K:Data -> @-enc:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> @iv:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> @acc:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

template m64.cbc.dec.go source · line 72 · raw

@-K:Data -> @-dec:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> @iv:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> @acc:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

template Lc64.ecb_encrypt_blocks source · line 81 · raw

@-K:Data -> @-enc:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

template Lc64.ecb_decrypt_blocks source · line 84 · raw

@-K:Data -> @-dec:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

template Lc64.cbc_encrypt_blocks source · line 87 · raw

@-K:Data -> @-enc:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @iv:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> @bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

template Lc64.cbc_decrypt_blocks source · line 90 · raw

@-K:Data -> @-dec:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @iv:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> @bs:List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>

template Lc64.ecb_encrypt source · line 138 · raw

@-K:Data -> @-enc:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template Lc64.ecb_decrypt source · line 141 · raw

@-K:Data -> @-dec:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @c:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template m64.cbc.e2 source · line 144 · raw

@-K:Data -> @-enc:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @v:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> @r:Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>> -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template m64.cbc.e1 source · line 151 · raw

@-K:Data -> @-enc:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @r:Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> @+m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template m64.cbc.d2 source · line 158 · raw

@-K:Data -> @-dec:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @v:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> @r:Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, List<&2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64>> -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template m64.cbc.d1 source · line 165 · raw

@-K:Data -> @-dec:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @r:Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64> -> @+c:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

template Lc64.cbc_encrypt source · line 173 · raw

@-K:Data -> @-enc:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @+iv:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @+m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>

m (whole blocks) under the 8-byte iv

template Lc64.cbc_decrypt source · line 176 · raw

@-K:Data -> @-dec:(@_:K -> @_:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcBlock64) -> @+k:K -> @+iv:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @+c:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>