Encryption¶
Name¶
Encryption
Summary¶
Encrypt a message, i.e. one or more data elements and/or action requests, to ensure its confidentiality with respect to entities that do not possess the correct decryption key.
Parent¶
Problem¶
Solution¶
In this pattern the considered cryptographic actions are encryption and decryption of some data elements and/or action requests.
Roles¶
- EntityA (: Entity) wants to encrypt one or more data elements and/or action requests
- EntityA <- Cryptographic action.Entity
- Encrypter (: Cryptographic
primitive) a cryptographic library providing at least the necessary encryption action
- Encrypter <- Cryptographic action.Cryptographer
- EntityB (: Entity) wants to decrypt one or more data elements and/or action requests
- EntityB <- Cryptographic action.Entity
- Decrypter (: Cryptographic
primitive) a cryptographic library providing at least the necessary decryption action
- Decrypter <- Cryptographic action.Cryptographer
Info
The roles of Encrypter and Decrypter can be performed by a single (instance of an) entity.
If a single system requires both encryption and decryption a single entity, i.e. cryptographic library, should be used for this.
Similarly, the EntityA and EntityB roles can be performed by a single entity (instance) as well.
Actions¶
- encrypt (: Action) request to encrypt a given plaintext using the identified and, if provided, cipher configuration
- encrypt <- Cryptographic action.crypto_action
- decrypt (: Action) request to decrypt a given ciphertext using the identified and, if provided, cipher configuration
- decrypt <- Cryptographic action.crypto_action
Data elements¶
- plaintext (: Data) the input that should be encrypted
- Encrypt flow: plaintext <- Cryptographic action.input
- Decrypt flow: plaintext <- Cryptographic action.output
- ciphertext (: Data) the result of encrypting a plaintext
- Encrypt flow: ciphertext <- Cryptographic action.output
- Decrypt flow: ciphertext <- Cryptographic action.input
- keyInfo (: Data) information on the cryptographic key to be used during encryption and decryption.
- Tags: Untampered[, Secret]
- config (: Data) the configuration for the
Encrypterand/orDecryptercontaining, for example, the desired cipher (mode) (optional)- Tags: Untampered
Behaviour¶
Encrypting a plaintext (E.n flows):
- The
EntityArequests theEncryption providerto encrypt a given plaintext using the identified key. If necessary, theEntityAalso provides a configuration. - The
Encryption providerperforms the encryption and replies the resulting ciphertext to theEntityA.

Decrypting a ciphertext (D.n flows):
- The
EntityBrequests theEncryption providerto decrypt a given ciphertext using the identified key. If necessary, theEntityBalso provides a configuration. - The
Encryption providerperforms the decryption and replies the resulting plaintext to theEntityB.

Role considerations¶
Reuse existing libraries (Encrypter, Decrypter)¶
Specialisation of Cryptographic action.Reuse existing libraries
One important criterium to consider when considering possible libraries is that they support the recommended ciphers, where libraries supporting only deprecated ciphers should be avoided. See below for concrete recommendations for symmetric and asymmetric ciphers).
Use keys for a single purpose (Entity)¶
Specialisation of Cryptographic action.Use keys for a single purpose
Cryptographic keys used for encrypting plaintexts and/or decrypting ciphertexts should not be used for any other purposes.4 This also concerns the kind of plaintexts that are encrypted with a single key. For instance, different encryption keys should be to encrypt regular data or encrypting other cryptographic keys.
Concretely, a symmetric key should only be used to encrypt one kind of plaintexts and decrypt the corresponding ciphertexts. In case of asymmetric ciphers, the public key should be used only for encryption of one kind of plaintexts and the corresponding private key for decrypting the resulting ciphertexts. One should not reuse the same key pair to, for example, digitally sign messages.
Design for change (EntityA, EntityB, Encrypter, Decrypter)¶
Specialisation of Cryprographic action.Design for change
Symmetric versus asymmetric ciphers¶
In a symmetric cipher the same, secret key, is used to encryption as well as decryption. In contrast, asymmetric ciphers use a key pair containing a public key for encryption and a private key for decrypting messages encrypted with the corresponding public key. More background information can be found in literature, for example 7 and 10.
In practice asymmetric ciphers are only used to encrypt small amounts of data.10 11 This is due to the significantly slower (decryption) performance of asymmetric ciphers compared to symmetric ciphers. As illustrated by the benchmarks provided by the Crypto++ library. In other words, it is advised to encrypt actions and/or data using symmetric ciphers. Asymmetric ciphers should only be used where appropriate, e.g. when negotiating a symmetric key or for digital signatures.
Symmetric ciphers¶
Cipher selection¶
For symmetric encryption the general recommendation is to stick to AES with a minimal key length of 128 bits.1 2 3 9 Deprecated ciphers, such as DES and TripleDES (also known as TDEA), should not be used to encrypt any data and should only be used when necessary to decrypt legacy data.1 2 3 5
With respect to dedicated stream ciphers, e.g. Salsa and ChaCha, some advice to avoid using them as these ciphers are less extensively studied compared to block ciphers.1 2 Instead the usage of a block cipher such as AES in counter mode (CTR) is advised.
Key length¶
The advised key length varies depending on the underlying cipher and selected source, one can use online tools to compare the the different advises.
In summary, for symmetric block ciphers, such as AES, the minimal advised key length (and block size) is 128 bits.1 2 3 6 9 In case the encrypted data needs to be protected for a long time, e.g. 30 to 50 years, AES with a key length of 256 bits is advised.9
Modes¶
Depending on the used cryptographic library and cipher it might be possible (or necessary) to configure the mode of the cipher. When using AES the Electronic Codebook (ECB) mode should be avoided at all times, instead other modes such as CCM, GCM, or CBC should be used.1 2
Asymmetric ciphers¶
Cipher selection¶
For asymmetric ciphers, also called public-key ciphers, the general recommendation is to use RSA.1 2 Note that it is advised not to use RSA-PKCS#1 v1.5 and older variants because of the existence of several possible attacks.2
Key length¶
The advised key length varies depending on the underlying cipher and selected source, one can use online tools to compare the the different advises.
In order to achieve a similar security strength using an asymmetric cipher, a minimal public key length, or modulus length, of 3072 bits is advised.2 4 7 Such a key length provides a comparable security strength to AES with a 128 bit key (cf. Table 2 in 4 or Table 2.3 in 7). Some other sources advise a lower minimal key length for asymmetric ciphers, such as 2000 bits1 or 2048 bits5, typically as a trade off between security and performance.
Authenticated encryption¶
The general consensus is to use an authenticated encryption mode when using symmetric ciphers.2 7 8 Authenticated encryption also provides integrity guarantees for ciphertexts, in addition to the confidentiality provided by traditional encryption. In other words, authenticated encryption allows to detect whether a ciphertext was modified after encryption, e.g. by an attacker during transmission. Most cryptographic libraries provide authentication encryption modes for symmetric ciphers such as AES, it is advised to consult the documentation of the library for more information.
Cryptographically-secure randomness¶
If the Entity has to provide random values as part of the configuration, e.g. nonces or initialisation vectors (IV), these should be always generated using a cryptographically-secure generator.
OWASP provides an overview of such generators for a range or programming languages.
Furthermore, one should consult the documentation of the library for further requirements, e.g. minimal length, for such inputs.
Data considerations¶
keyInf¶
See Cryptographic key management.keyInfo
config¶
Specialisation of Cryptographic action.config
plaintext¶
As there was a need to encrypt a plaintext, e.g. before transmission or storage, it is likely sensitive data that should not leak to unauthorised parties. In this case it is important to carefully analyse the entire flow of the plain text data element for potential leaks. For example, a plaintext that is caches somewhere before encryption may leak to an attacker.
Implementations¶
None
References¶
A more extensive overview of available cryptography libraries for a range of different programming languages (along with other documentation) can be found on awesome-cryptography.
-
Bundesamt für Sicherheit in der Informationstechnik, ‘Cryptographic Mechanisms:Recommendations and Key Lengths’, BSI TR-02102-1, Mar. 2020. [Online]. Available: https://www.bsi.bund.de/SharedDocs/Downloads/EN/BSI/Publications/TechGuidelines/TG02102/BSI-TR-02102-1.html ↩↩↩↩↩↩↩
-
N. P. Smart et al., ‘Algorithms, Key size and parameters report’, ENISA, Nov. 2014. [Online]. Available: https://www.enisa.europa.eu/publications/algorithms-key-size-and-parameters-report-2014 ↩↩↩↩↩↩↩↩↩
-
E. Barker, ‘Guideline for Using Cryptographic Standards in the Federal Government: Cryptographic Mechanisms’, NIST, 800–175B, Mar. 2020. [Online]. Available: https://doi.org/10.6028/NIST.SP.800-175Br1 ↩↩↩
-
E. Barker, ‘Recommendation for Key Management: Part 1 – General’, NIST, 800–57 Part 1, May 2020. [Online]. Available: https://doi.org/10.6028/NIST.SP.800-57pt1r5 ↩↩↩
-
E. Barker and A. Roginsky, ‘Transitioning the Use of Cryptographic Algorithms and Key Lengths’, NIST, 800–131A rev 2, Mar. 2019. [Online]. Available: https://doi.org/10.6028/NIST.SP.800-131Ar2 ↩↩
-
W. Breyha et al., ‘Applied Crypto Hardening’, Applied Crypto Hardening: bettercrypto.org, Dec. 21, 2018. https://bettercrypto.org/ ↩
-
P. C. van Oorschot, Computer Security and the Internet - Tools and Jewels. 2020. [Online]. Available: https://doi.org/10.1007/978-3-030-33649-3 ↩↩↩↩
-
OWASP, ‘OWASP Top 10:2021 - A02: Cryptographic failures'. [Online]. Available: https://owasp.org/Top10/A02_2021-Cryptographic_Failures/ ↩
-
N. P. Smart et al., ‘Algorithms, Key Size and Protocols Report (2018)’, Feb. 2018. [Online]. Available: https://www.ecrypt.eu.org/csa/documents/D5.4-FinalAlgKeySizeProt.pdf ↩↩↩
-
A. J. Menezes, P. C. Van Oorschot, and S. A. Vanstone, Handbook of applied cryptography. CRC press, 1996. [Online]. Available: https://cacr.uwaterloo.ca/hac/ ↩↩
-
L. Van Houtven, ‘Crypto 101’, 2017. [Online]. Available: https://www.crypto101.io/ ↩