Authenticated encryption
Encrypt and authenticate data with associated data (AEAD).
Choose an algorithm, then encrypt with a key, a nonce and the plaintext.
encrypt returns the ciphertext followed by the tag, and decrypt raises
oxcrypto.InvalidTag when the ciphertext, tag, nonce or associated data do
not match. encrypt_detached returns (ciphertext, tag) and
decrypt_detached takes them separately.
import os
from oxcrypto import InvalidTag
from oxcrypto.aead.aes_gcm import AES_GCM
key = AES_GCM.AES256.generate()
nonce = os.urandom(12)
sealed = key.encrypt(nonce, b"message", associated_data=b"header")
try:
key.decrypt(nonce, sealed, associated_data=b"header")
except InvalidTag:
...The selector methods take the raw key bytes as the first argument
(AES_GCM.AES256.encrypt(key_bytes, nonce, data)). Key objects come from
generate() or from_bytes(), export with to_bytes(), compare with
subtle::ConstantTimeEq (written to run in constant time), hold their bytes in
zeroize::Zeroizing (written to wipe them when freed) and cannot be pickled or
copied. oxcrypto has not verified either property independently. Never reuse a
nonce with the same key unless the algorithm is nonce-misuse resistant.
Algorithms
- AES-GCM Encrypt with AES-128, AES-192 or AES-256 in Galois/Counter Mode.
- AES-GCM-SIV Nonce-misuse-resistant AES-GCM-SIV with AES-128 or AES-256.
- ChaCha20-Poly1305 Encrypt with ChaCha20-Poly1305, XChaCha20-Poly1305 or their reduced-round forms.
- Deoxys Encrypt with Deoxys-I or the nonce-misuse-resistant Deoxys-II.
- belt-dwp Encrypt with the Belarusian belt-dwp authenticated encryption mode.
- XAES-256-GCM Encrypt with AES-256-GCM using a 192-bit nonce that is can be chosen at random.
- Ascon-AEAD128 Encrypt with Ascon-AEAD128 from NIST SP 800-232.
- Xoodyak Encrypt with the Xoodyak lightweight AEAD.
- STREAM Encrypt a sequence of segments with any of the algorithms above.
Zeroization
The upstream crates for each algorithm on this page, with their zeroize
features on, are written to wipe their keys, expanded round keys, authenticator
keys (GHASH, POLYVAL and Poly1305) and permutation state when dropped. oxcrypto
read their source but has not verified this independently. oxcrypto keeps its
own copies of keys, plaintexts and the buffer of a failed decryption in
zeroize::Zeroizing, and a failed decryption returns no partial plaintext.
Some short-lived stack temporaries inside upstream crates, such as a padded
partial block, are not wiped.
Zeroization stops at the Python boundary. Key bytes passed to the selector
methods or returned by to_bytes(), plaintexts and decryption results are
immutable Python bytes objects that oxcrypto cannot erase, and Python, the
allocator or the operating system may keep further copies. Treat erasure as
best effort, not as protection against an attacker who can read process
memory.