Message authentication code (MAC)¶
Name¶
Message authentication code (MAC)
Summary¶
This pattern encapsulates the common considerations one should take into account when using Message Authentication Codes (MAC) to ensure the integrity of messages and the identity of the provider.
Parent¶
Problem¶
See Cryptographic action.
Solution¶
A Message Authentication Code (MAC) is a tag that is computed from a message, i.e. data elements and or action requests, using a special hash function whose output also depends on a secret cryptographic key.1 In other words, generating a MAC for a message requires the message itself as well as the possession of a secret key. Consequently, only parties that possess or have access to an agreed upon secret key can generate (and verify) valid MACs. Appending a MAC to a message can be used to provide two properties1:
- Data integrity: assurance that the received message is identical to the one used to calculate the MAC; and
- Data origin authentication: assurance of the identity of the party that originated the message (i.e. assurance that the originating party possesses the correct secret key).
Note that these properties depend on the secrecy of the cryptographic key used to generate MACs. Anyone in possession of this key is able to generate valid MACs for any message. For example, an attacker that has obtained the secret can simply compute a new MAC after altering a message, and the receiver will not be able to detect this alteration.
A MAC only serves as evidence that the party that generated the MAC actually possesses the correct secret key. A MAC does not provide evidence that a specific party generated it since all involved parties involved are required to possess the same key to generate and/or verify MACs. If such evidence is a requirement one should consider using digital signatures instead of a MAC as they can also provide non-repudiation.
Note
If one wants to protect the integrity of encrypted messages in transit it is advised to use authenticated encryption instead of explicitly computing and appending MACs.1 Doing encryption and MAC computation separately would require two distinct secret keys, complicating key management and distribution. Authenticated encryption allows to safely use a single symmetric key for both encryption and data origin authentication.
Roles¶
- EntityA (: Entity) an entity that wants to create an MAC for message(s)
- EntityA <- Cryptographic action.entity
- MAC generator (: Cryptographic primitive) a cryptographic primitive that at least supports generating a MAC for a given message and cryptographic key
- MAC generator <- Cryptographic action.Cryptographer
- EntityB (: Entity) an entity that wants to verify whether a message was modified since the corresponding MAC was created
- EntityA <- Cryptographic action.entity
- MAC verifier (: Cryptographic primitive) a cryptographic primitive that at least supports verifying whether a given message and MAC match
- MAC verifier <- Cryptographic action.Cryptographer
Info
The roles of MAC generator and MAC verifier can be performed by a single (instance of an) entity.
If a single system requires both MAC generation and verification 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¶
- generate (: Action) a new MAC based on the given message and cryptographic key
- generate <- Cryptographic action.crypto_action
- verify (: Action) whether a given MAC was generated from the provided message and cryptographic key.
This essentially comes down to calculating the MAC for the provided message and key, and verifying whether the result is identical to the received MAC.
- verify <- Cryptographic action.crypto_action
Data elements¶
- m (: Data) the plaintext message, i.e. data element(s) and/or action request(s), for which a new MAC should be generated or a given MAC should be verified
- m <- Cryptographic action.input
- mac (: Data) a MAC generated from some message
- Generation: mac <- Cryptographic action.output
- Verification: mac <- Cryptographic action.input
- keyInfo (: Data) information on the secret key
to be used in MAC generation and verification.
The exact information depends on the chosen key management method (cf. Cryptographic key management).
- Tags: Untampered[, Secret]
- keyInfo <- Cryptographic action.KeyInfo
- config (: Data) the configuration for the
MAC generatorand/orMAC verifiercontaining, for example, the desired cipher (optional)- Tags: Untampered
- ok (: Data) indication that a message and MAC match
- Verification: ok <- Cryptographic action.output
- error (: Data) indication that a message and MAC did not match.
This may be because the message was modified after the MAC was generated or that a different secret key was used to generate the MAC.
- Verification: error <- Cryptographic action.output
Behaviour¶
In order to generate a MAC for a message m:
EntityArequests theMAC generatorto generate a new MAC and provides the plaintextmalong with the necessary information on the secret key to be used.- The
MAC generatorgenerates the requested MAC value and replies it toEntityB.

In order to verify a given MAC for against a data element m:
EntityBrequests theMAC verifierto verify a whether an elementmand MAC match and providesm, the MAC, and secret key used to generate the MAC.- The
MAC verifierverifies whether the data element and MAC match, i.e. it checks whether the MAC generated frommusing the given key is identical to the MAC provided as input. If so, theMAC verifierconfirms this toEntityB, otherwise an error message is replied.

Role considerations¶
Reuse existing libraries (MAC generator, MAC verifier)¶
Specialisation of Cryptographic action.Reuse existing libraries
Use keys for a single purpose (Entity)¶
Specialisation of Cryptographic action.Use keys for a single purpose
Concretely, a cryptographic key that is used to generate and verify MACs should not be used for other purposes such as encrypting messages.
Design for change (EntityA, EntityB, MAC generator, MAC verifier)¶
Specialisation of Cryptographic action.Design for change
Cipher selection¶
One should always dedicated ciphers to generate and verify MACs, ad hoc ciphers such as using an unkeyed hash function, e.g. SHA, to calculate the hash value of the concatenation of a message and the secret have been shown to be insecure.1 4
Any MAC generation and verification cipher based on MD5, e.g. HMAC-MD5, should be avoided except when needed to verify MACs previously generated with such a cipher.5 6 Instead other ciphers such HMAC-SHA*, AES-CMAC, or Blake2b are recommended.
Key length¶
The length of the secret is advised to be at least 128 bits.7 8
MAC output size¶
The recommended length of a MAC depends on the time frame it is intended to be used. Common advice is to use MACs of 128 bits for time frames of up to 10 years and longer MACs of 256bits for longer time frames3 6 In case of MACs that are used for a very short time, e.g. as part of short-lived authentication token, shorter output sizes of at least 64 bits can be acceptable.7 2
Data considerations¶
keyInfo¶
Specialisation of Cryptographic action.keyInfo
A cryptographic key that is used to generate and verify MACs should at all times be kept confidential and its integrity should be protected. This concerns when the key is stored persistently as well as when it is transmitted between entities.
config¶
Specialisation of Cryptographic action.config
Implementations¶
None
References¶
-
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 ↩↩↩↩
-
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 ↩
-
‘Algorithms, Key Size and Protocols Report (2018)’, Feb. 2018. [Online]. Available: https://www.ecrypt.eu.org/csa/documents/D5.4-FinalAlgKeySizeProt.pdf ↩
-
B. Preneel and P. C. van Oorschot, ‘On the security of iterated message authentication codes’, IEEE Transactions on Information Theory, vol. 45, no. 1, pp. 188–199, Jan. 1999, doi: 10.1109/18.746787. ↩
-
S. Turner and L. Chen, ‘Updated Security Considerations for the MD5 Message-Digest and the HMAC-MD5 Algorithms’, IETF, RFC 6151, Mar. 2011. [Online]. Available: https://www.rfc-editor.org/info/rfc6151 ↩
-
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 ↩↩
-
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.pdf?__blob=publicationFile ↩↩
-
Tink - Message Authentication Code (MAC) https://developers.google.com/tink/mac ↩