Encryption Security Pattern
Encrypt a message (data elements and/or action requests) to ensure its confidentiality with respect to entities that do not possess the correct decryption key.
Core Components
| Role |
Type |
Responsibility |
| EntityA |
Entity |
Wants to encrypt one or more data elements and/or action requests |
| EntityB |
Entity |
Wants to decrypt received ciphertext (may be same as EntityA) |
| Encrypter |
Cryptographic Primitive |
Library providing encryption action |
| Decrypter |
Cryptographic Primitive |
Library providing decryption action |
Note: Encrypter and Decrypter roles can be performed by a single library instance. Similarly, EntityA and EntityB can be the same entity.
Data Elements
- plaintext: Original data to be encrypted
- ciphertext: Encrypted data {plaintext}_k
- keyInfo: Information on cryptographic key (identifier or key material)
- config: Cipher configuration (algorithm, mode, parameters) - optional
Actions
- encrypt: Request to encrypt plaintext using identified key and configuration
- decrypt: Request to decrypt ciphertext using identified key and configuration
Pattern Flow
Encryption
EntityA → [encrypt(plaintext, keyInfo, config)] → Encrypter
Encrypter → [ciphertext] → EntityA
Decryption
EntityB → [decrypt(ciphertext, keyInfo, config)] → Decrypter
Decrypter → [plaintext] → EntityB
Symmetric vs. Asymmetric Encryption
Symmetric Ciphers
- Same secret key for encryption and decryption
- Fast performance
- Use for bulk data encryption
- Key distribution challenge
Asymmetric Ciphers
- Public key encrypts, private key decrypts
- Slower performance (significantly slower decryption)
- Use only for small amounts of data
- Better for key negotiation, digital signatures
Recommendation: Use symmetric ciphers for encrypting data. Use asymmetric ciphers only where appropriate (key exchange, small data).
Algorithm Recommendations
Symmetric Ciphers
| Algorithm |
Key Length |
Status |
| AES |
128 bits |
Minimum recommended |
| AES |
256 bits |
Recommended for long-term (30-50 years) |
| DES |
56 bits |
Deprecated - never use |
| 3DES/TDEA |
168 bits |
Deprecated - decrypt legacy only |
| Salsa/ChaCha |
Variable |
Use with caution (less studied than AES) |
Preferred: AES with minimum 128-bit key length. Use AES-256 for long-term protection.
Cipher Modes (for AES)
| Mode |
Status |
Notes |
| GCM |
Recommended |
Authenticated encryption |
| CCM |
Recommended |
Authenticated encryption |
| CBC |
Acceptable |
Requires separate MAC |
| CTR |
Acceptable |
Stream mode, needs MAC |
| ECB |
Never use |
Reveals patterns in plaintext |
Critical: Always use authenticated encryption modes (GCM, CCM) when possible. They provide both confidentiality AND integrity.
Asymmetric Ciphers
| Algorithm |
Key Length |
Status |
| RSA |
3072 bits |
Recommended (≈ AES-128 security) |
| RSA |
2048 bits |
Minimum acceptable |
| RSA-PKCS#1 v1.5 |
Any |
Avoid (padding oracle attacks) |
Note: RSA-3072 provides security strength comparable to AES-128.
Security Considerations
Reuse Existing Libraries
Specialization of Cryptographic action.Reuse existing libraries:
- Use well-known cryptographic libraries
- Verify library supports recommended ciphers
- Avoid libraries supporting only deprecated ciphers
- Consult library documentation
Use Keys for Single Purpose
Specialization of Cryptographic action.Use keys for single purpose:
- Never use encryption keys for other purposes (e.g., signing)
- Use different keys for different data types
- Symmetric key: one kind of plaintext only
- Asymmetric: public key for one kind of plaintext, private key for corresponding ciphertexts
Design for Change
Specialization of Cryptographic action.Design for change:
- Algorithms may become deprecated
- Key lengths may need to increase
- Design for easy cipher/library transitions
Authenticated Encryption
General consensus: Use authenticated encryption modes for symmetric ciphers.
Benefits:
- Provides integrity guarantees in addition to confidentiality
- Detects if ciphertext was modified after encryption (e.g., by attacker during transmission)
- Most libraries provide authenticated encryption modes for AES
Random Value Generation (Nonces/IVs)
If Entity must provide random values (nonces, initialization vectors):
- Always use cryptographically-secure generator
- OWASP provides overview of secure generators by language
- Never reuse nonce/IV with same key
Plaintext Leakage
Since plaintext needed encryption, it is likely sensitive:
- Analyze entire plaintext flow for potential leaks
- Check for caching before encryption
- Ensure plaintext doesn't leak through logs, errors, or side channels
Implementation Checklist
Related Patterns
- Cryptographic action (parent pattern)
- Cryptographic key management (key handling)
- Selective encrypted transmission (encryption in transit)
- Encrypted tunnel (channel-level encryption)
- Selective encrypted storage (encryption at rest)
- Transparent encrypted storage (storage-level encryption)
References
- Source: https://securitypatterns.distrinet-research.be/patterns/99_01_002__encryption/
- Bundesamt für Sicherheit in der Informationstechnik, 'Cryptographic Mechanisms: Recommendations and Key Lengths', BSI TR-02102-1, Mar. 2020
- N. P. Smart et al., 'Algorithms, Key size and parameters report', ENISA, Nov. 2014
- E. Barker, 'Recommendation for Key Management: Part 1 – General', NIST SP 800-57 Part 1, May 2020
- E. Barker, 'Guideline for Using Cryptographic Standards in the Federal Government: Cryptographic Mechanisms', NIST SP 800-175B, Mar. 2020
- E. Barker and A. Roginsky, 'Transitioning the Use of Cryptographic Algorithms and Key Lengths', NIST SP 800-131A rev 2, Mar. 2019
- W. Breyha et al., 'Applied Crypto Hardening', bettercrypto.org, Dec. 2018
- P. C. van Oorschot, Computer Security and the Internet - Tools and Jewels, 2020
- awesome-cryptography: https://github.com/sobolevn/awesome-cryptography
1---2name: encryption-pattern3description: Security pattern for implementing encryption and decryption operations. Use when encrypting data for confidentiality, selecting encryption algorithms (AES, RSA), configuring cipher modes (GCM, CBC), choosing key lengths, or implementing symmetric/asymmetric encryption. Specialization of Cryptographic action pattern addressing confidentiality requirements.4---56# Encryption Security Pattern78Encrypt a message (data elements and/or action requests) to ensure its confidentiality with respect to entities that do not possess the correct decryption key.910## Core Components1112| Role | Type | Responsibility |13|------|------|----------------|14| **EntityA** | Entity | Wants to encrypt one or more data elements and/or action requests |15| **EntityB** | Entity | Wants to decrypt received ciphertext (may be same as EntityA) |16| **Encrypter** | Cryptographic Primitive | Library providing encryption action |17| **Decrypter** | Cryptographic Primitive | Library providing decryption action |1819**Note**: Encrypter and Decrypter roles can be performed by a single library instance. Similarly, EntityA and EntityB can be the same entity.2021### Data Elements2223- **plaintext**: Original data to be encrypted24- **ciphertext**: Encrypted data {plaintext}_k25- **keyInfo**: Information on cryptographic key (identifier or key material)26- **config**: Cipher configuration (algorithm, mode, parameters) - optional2728### Actions2930- **encrypt**: Request to encrypt plaintext using identified key and configuration31- **decrypt**: Request to decrypt ciphertext using identified key and configuration3233## Pattern Flow3435### Encryption36```37EntityA → [encrypt(plaintext, keyInfo, config)] → Encrypter38Encrypter → [ciphertext] → EntityA39```4041### Decryption42```43EntityB → [decrypt(ciphertext, keyInfo, config)] → Decrypter44Decrypter → [plaintext] → EntityB45```4647## Symmetric vs. Asymmetric Encryption4849### Symmetric Ciphers50- Same secret key for encryption and decryption51- Fast performance52- Use for bulk data encryption53- Key distribution challenge5455### Asymmetric Ciphers56- Public key encrypts, private key decrypts57- Slower performance (significantly slower decryption)58- Use only for small amounts of data59- Better for key negotiation, digital signatures6061**Recommendation**: Use symmetric ciphers for encrypting data. Use asymmetric ciphers only where appropriate (key exchange, small data).6263## Algorithm Recommendations6465### Symmetric Ciphers6667| Algorithm | Key Length | Status |68|-----------|------------|--------|69| **AES** | 128 bits | Minimum recommended |70| **AES** | 256 bits | Recommended for long-term (30-50 years) |71| DES | 56 bits | **Deprecated - never use** |72| 3DES/TDEA | 168 bits | **Deprecated - decrypt legacy only** |73| Salsa/ChaCha | Variable | Use with caution (less studied than AES) |7475**Preferred**: AES with minimum 128-bit key length. Use AES-256 for long-term protection.7677### Cipher Modes (for AES)7879| Mode | Status | Notes |80|------|--------|-------|81| **GCM** | Recommended | Authenticated encryption |82| **CCM** | Recommended | Authenticated encryption |83| CBC | Acceptable | Requires separate MAC |84| CTR | Acceptable | Stream mode, needs MAC |85| **ECB** | **Never use** | Reveals patterns in plaintext |8687**Critical**: Always use authenticated encryption modes (GCM, CCM) when possible. They provide both confidentiality AND integrity.8889### Asymmetric Ciphers9091| Algorithm | Key Length | Status |92|-----------|------------|--------|93| **RSA** | 3072 bits | Recommended (≈ AES-128 security) |94| RSA | 2048 bits | Minimum acceptable |95| RSA-PKCS#1 v1.5 | Any | **Avoid** (padding oracle attacks) |9697**Note**: RSA-3072 provides security strength comparable to AES-128.9899## Security Considerations100101### Reuse Existing Libraries102103Specialization of Cryptographic action.Reuse existing libraries:104- Use well-known cryptographic libraries105- Verify library supports recommended ciphers106- Avoid libraries supporting only deprecated ciphers107- Consult library documentation108109### Use Keys for Single Purpose110111Specialization of Cryptographic action.Use keys for single purpose:112- **Never use encryption keys for other purposes** (e.g., signing)113- Use different keys for different data types114- Symmetric key: one kind of plaintext only115- Asymmetric: public key for one kind of plaintext, private key for corresponding ciphertexts116117### Design for Change118119Specialization of Cryptographic action.Design for change:120- Algorithms may become deprecated121- Key lengths may need to increase122- Design for easy cipher/library transitions123124### Authenticated Encryption125126**General consensus**: Use authenticated encryption modes for symmetric ciphers.127128Benefits:129- Provides integrity guarantees in addition to confidentiality130- Detects if ciphertext was modified after encryption (e.g., by attacker during transmission)131- Most libraries provide authenticated encryption modes for AES132133### Random Value Generation (Nonces/IVs)134135If Entity must provide random values (nonces, initialization vectors):136- **Always use cryptographically-secure generator**137- OWASP provides overview of secure generators by language138- Never reuse nonce/IV with same key139140### Plaintext Leakage141142Since plaintext needed encryption, it is likely sensitive:143- Analyze entire plaintext flow for potential leaks144- Check for caching before encryption145- Ensure plaintext doesn't leak through logs, errors, or side channels146147## Implementation Checklist148149- [ ] Using AES with minimum 128-bit keys150- [ ] Using authenticated encryption mode (GCM/CCM)151- [ ] **No ECB mode**152- [ ] **No deprecated ciphers** (DES, 3DES for new data)153- [ ] RSA minimum 2048 bits (prefer 3072)154- [ ] **No RSA-PKCS#1 v1.5**155- [ ] Keys used for single purpose only156- [ ] Nonces/IVs from cryptographically-secure generator157- [ ] Plaintext flow analyzed for leaks158- [ ] Library supports recommended ciphers159- [ ] Designed for algorithm/key transitions160161## Related Patterns162163- **Cryptographic action** (parent pattern)164- **Cryptographic key management** (key handling)165- **Selective encrypted transmission** (encryption in transit)166- **Encrypted tunnel** (channel-level encryption)167- **Selective encrypted storage** (encryption at rest)168- **Transparent encrypted storage** (storage-level encryption)169170## References171172- Source: https://securitypatterns.distrinet-research.be/patterns/99_01_002__encryption/173- Bundesamt für Sicherheit in der Informationstechnik, 'Cryptographic Mechanisms: Recommendations and Key Lengths', BSI TR-02102-1, Mar. 2020174- N. P. Smart et al., 'Algorithms, Key size and parameters report', ENISA, Nov. 2014175- E. Barker, 'Recommendation for Key Management: Part 1 – General', NIST SP 800-57 Part 1, May 2020176- E. Barker, 'Guideline for Using Cryptographic Standards in the Federal Government: Cryptographic Mechanisms', NIST SP 800-175B, Mar. 2020177- E. Barker and A. Roginsky, 'Transitioning the Use of Cryptographic Algorithms and Key Lengths', NIST SP 800-131A rev 2, Mar. 2019178- W. Breyha et al., 'Applied Crypto Hardening', bettercrypto.org, Dec. 2018179- P. C. van Oorschot, Computer Security and the Internet - Tools and Jewels, 2020180- awesome-cryptography: https://github.com/sobolevn/awesome-cryptography