inst_ccm.bend

bend-osdev-crypto@0.1.0.0 · 21 lines · 2 definitions

1# CCM as an AEAD: cipher-modes' CCM (NIST SP 800-38C, RFC 3610) over any
2# 128-bit block cipher, with a tag of tlen bytes (4, 6, ..., 16) and a nonce
3# of 7 to 13 bytes. The instance over a block cipher (~K, ~key, ~enc):
4#   ~K, ~key, ~CrCcm.seal(~K, ~enc, ~tlen), ~CrCcm.open(~K, ~enc, ~tlen)
5# for example AES-CCM with a 16-byte tag:
6#   ~X.CmAesKey, ~CrAes.key, ~CrCcm.seal(~X.CmAesKey, ~X.CmAes.encrypt, ~16n),
7#   ~CrCcm.open(~X.CmAesKey, ~X.CmAes.encrypt, ~16n)
8import Base
9import bend-osdev-bytes@0.1.0.0/bytes.bend as B
10import bend-osdev-cipher-modes@0.1.0.0/cipher_modes.bend as M
11import bend-osdev-cipher-modes@0.1.0.0/ccm.bend as CC
12import bend-osdev-crypto@0.1.0.0/crypto.bend as Cr
13import bend-osdev-crypto@0.1.0.0/inst_cm.bend as CM
14
15# the ciphertext of p, then the tlen-byte tag
16def CrCcm.seal(~K: Data, ~enc: K -> M.CmBlock -> M.CmBlock, ~tlen: Nat, k: K, nonce: B.Octets, aad: B.Octets, p: B.Octets) -> Result<&2, &2, Cr.CrError, B.Octets>:
17  CM.CrCm.lift(~B.Octets, CC.Ccm.encrypt(~K, ~enc, k, tlen, nonce, aad, p))
18
19# the plaintext of c (the ciphertext, then the tag), or CrTagMismatch
20def CrCcm.open(~K: Data, ~enc: K -> M.CmBlock -> M.CmBlock, ~tlen: Nat, k: K, nonce: B.Octets, aad: B.Octets, c: B.Octets) -> Result<&2, &2, Cr.CrError, B.Octets>:
21  CM.CrCm.lift(~B.Octets, CC.Ccm.decrypt(~K, ~enc, k, tlen, nonce, aad, c))