The Authenticated Encryption with Associated Data (AEAD) primitive is the most common primitive for data encryption and is suitable for most needs.
AEAD has the following properties:
- Secrecy: Nothing about the plaintext is known, except its length.
- Authenticity: It is impossible to change the encrypted plaintext underlying the ciphertext without being detected.
- Symmetric: Encrypting the plaintext and decrypting the ciphertext is done with the same key.
- Randomization: Encryption is randomized. Two messages with the same plaintext yield different ciphertexts. Attackers cannot know which ciphertext corresponds to a given plaintext. If you want to avoid this, use Deterministic AEAD instead.
Associated data
AEAD can be used to tie ciphertext to specific associated
data. Suppose you have a database with the fields user-id
and encrypted-medical-history
. In this scenario, user-id
can be used as
associated data when encrypting encrypted-medical-history
. This prevents an
attacker from moving medical history from one user to another.
Choose a key type
While we recommend AES128_GCM for most uses, there are various key types for different needs (for 256-bit security, replace AES128 with AES256 below). Generally:
- AES128_CTR_HMAC_SHA256 with a 16-byte Initialization Vector (IV) is the most conservative mode with good bounds.
- AES128_EAX is slightly less conservative and slightly faster than AES128_CTR_HMAC_SHA256.
- AES128_GCM is usually the fastest mode, with the strictest limits on the number of messages and message size. When these limits on plaintext and associated data lengths (below) are exceeded, AES128_GCM fails and leaks key material.
- AES128_GCM_SIV is nearly as fast as AES128_GCM, with very good bounds for a large amount of messages, but is slightly less established. To use this in Java, you have to install Conscrypt.
- XChaCha20Poly1305 has a much greater limit on the number of messages and message size than AES128_GCM, but when it does fail (very unlikely) it also leaks key material. It isn't hardware accelerated, so it can be slower than AES modes in situations where hardware acceleration is available.
Security guarantees
AEAD implementations offer:
- CCA2 security.
- At least 80-bit authentication strength.
- The ability to encrypt at least 232 messages with a total of 250 bytes. No attack with up to 232 chosen plaintexts or chosen ciphertexts has success probability larger than 2-32.
Example use cases
See I want to encrypt data and I want to bind ciphertext to its context.