MAC & HMAC

About

When it comes to data security, ensuring data integrity and authenticity of information is the most important part. In terms of safeguarding sensitive data, MAC (Message Authentication Code) and HMAC (Hash-based Message Authentication Code) act as a crucial cryptographic constructs. They play a vital role in verifying that messages remain unaltered and originate from trusted sources.

circle-check

MACs and HMACs are cryptographic techniques used to verify the authenticity and integrity of messages. They work by creating a "tag" that is associated with a message. The tag is generated using a secret key and it can be used to verify that the message has not been tampered with. The MAC is appended to the message and transmitted along with it. The recipient of the message can then use the same key and algorithm to compute a new MAC for the received message. By comparing the computed MAC with the received MAC, the recipient can verify whether the message has been tampered with or modified during transmission. If the MACs match, the recipient can have confidence that the message is authentic and has not been altered.

They are used in various security protocols and applications.

Here is the High Level Diagram showing the steps involved in message authentication using MACs

High level diagram showing the flow of MAC between sender and receiver
circle-exclamation

HMAC is a specific type of MAC that uses a cryptographic hash function, such as SHA-256 or MD5, along with a secret key. The HMAC improves security of the MAC by providing resistance against certain cryptographic attacks. HMAC is computed by applying the hash function to the input message and key in a specific way that incorporates both. The resulting output is the HMAC. Like MAC, the HMAC is sent along with the message, allowing the recipient to verify the integrity and authenticity of the message.

The main difference between MAC and HMAC is that MAC can use various cryptographic algorithms, such as symmetric ciphers or cryptographic hash functions, while HMAC specifically uses a hash function in combination with a key. HMAC is considered to be a more secure and recommended approach for message authentication due to its resistance against certain attacks.

circle-check

Use cases of MAC and HMAC are

  • Message Authentication: MAC and HMAC are used to authenticate the sender of a message. By sharing a secret key between the sender and receiver, the receiver can verify that the message was sent by the claimed sender.

  • Data Integrity: MAC and HMAC are used to verify that data has not been tampered with during transmission. By comparing the computed authentication code with the received code, the receiver can ensure the integrity of the data.

  • Password Protection: MAC and HMAC are frequently used in password hashing and storage. When a user sets or changes a password, the MAC or HMAC of the password is computed and stored. During login attempts, the computed MAC or HMAC of the entered password is compared with the stored value for verification.

  • Secure Communication: MAC and HMAC are used in various secure communication protocols, such as SSL/TLS, SSH etc. They provide data integrity and authentication, ensuring that the transmitted data remains confidential and has not been tampered.

  • Digital Signatures: HMAC can be used in digital signature as a part of the signature generation process. By applying an HMAC to the message using the signer's private key, a digital signature is created. The recipient can then verify the signature using the signer's public key and the original message. For Eg. JSON Web Token (JWT) uses digital signatures to ensure the integrity and authenticity of the token.

  • Network Security: They play a crucial role in network security protocols such a secure email (S/MIME), secure file transfer (SFTP), and secure network protocols (e.g., HTTPS).

Common MAC algorithms

  • HMAC (Hash-based Message Authentication Code) - HMAC is a widely used MAC algorithm that is based on cryptographic hash functions.

    • HMAC-MD5

    • HMAC-SHA1

    • HMAC-SHA256

    • HMAC-SHA512

  • CBC-MAC (Cipher Block Chaining Message Authentication Code) - CBC-MAC is a MAC algorithm that is based on the cipher block chaining (CBC) mode of operation. It works by encrypting the message with a block cipher in CBC mode. The encryption process generates a sequence of blocks, and the last block is the MAC.

    • CBC-MAC using DES

    • CBC-MAC using AES

  • CMAC (Cipher-based Message Authentication Code) - CMAC is a MAC algorithm that is based on block ciphers. CMAC is similar to HMAC, but it uses a block cipher instead of a hash function

    • CMAC using AES

  • OMAC (One-Key CBC MAC) - OMAC is a MAC algorithm that is designed to be used with one-time keys.

    • OMAC1 using AES

    • OMAC2 using AES

Best practices for Key Management

  • Use Strong Secret Keys -> Use a long and randomly generated secret key for the MAC computation. The strength of the key greatly impacts the security of the MAC.

  • Protect the Secret Key -> Ensure that the secret key is properly protected and kept confidential. Avoid hardcoding or transmitting the key insecurely. We should not store the keys in plain text files or on unencrypted disks. It should be stored in secure location, such as a password manager.

  • Rotate the keys frequently -> We should rotate the keys regularly. This means that we should generate new keys and update the systems to use the new keys. Rotating the keys helps to protect the data from attackers which might have compromised the old keys.

  • Back up the keys -> We should store copies of keys in a secure location. If primary copy of the keys is lost or compromised, backup can be used to restore the keys.

  • Use of a hardware security module (HSM) -> A hardware security module (HSM) is a physical device that can be used to store the keys.

  • Use of a key management system -> A key management system (KMS) is a software application that helps manage keys. A KMS can help generate, store, rotate, and back up of the keys.

Example

Hands-on with HMAC-SHA512 MAC algorithm that is based on the SHA-512 hash function.

Using Java inbuilt library javax.crypto.Mac for MAC (HMAC-SHA512)

Log output of the above code

Using Bouncy Castle library in Spring for HMAC (HMAC-SHA512)

Log output of the above sample bouncy castle code
circle-check

Last updated