Practical 9: HMAC using SHA-256

Hash-based Message Authentication Code Implementation

Objective

Implementation

To implement HMAC using SHA-256 hash function

Observation

Observe how small changes in message affect the resulting MAC

Security

Ensuring data integrity and authenticity

Purpose of the Practical

  • Understanding how cryptographic hash functions like SHA-256 can be used to generate HMAC
  • Illustrating the importance of message integrity and authentication in communication
  • Demonstrating the effect of minor message changes on the resulting MAC

Theory: What is HMAC?

HMAC (Hash-based Message Authentication Code) is a message authentication mechanism based on cryptographic hash functions like SHA-256.

HMAC Ensures:

🔒 Integrity

Message hasn't been tampered with

🔑 Authenticity

Message came from someone who knows the secret key

How HMAC Works

HMAC combines a secret key, a message, and a cryptographic hash function (e.g., SHA-256):

HMAC(key, message) = H((key ⊕ opad) || H((key ⊕ ipad) || message))

Where:
H = hash function (e.g., SHA-256)
ipad and opad = fixed padding constants
⊕ = XOR operation
|| = Concatenation

This construction ensures resistance to length-extension and collision attacks, unlike simple H(key || message).

Code Implementation

from Crypto.Hash import HMAC, SHA256

# Secret key
key = b'secretkey123'

# Messages
msg1 = b'GECBHARUCH'
msg2 = b'GECBharuch'  # Slightly different

# Generate HMAC using SHA-256
hmac1 = HMAC.new(key, msg1, SHA256).hexdigest()
hmac2 = HMAC.new(key, msg2, SHA256).hexdigest()

print("HMAC of msg1:", hmac1)
print("HMAC of msg2:", hmac2)

Output:

HMAC of msg1: 1ea6242d27ef42d9b686ef9ad0f71ba49c5ed48b70d344a5e4da3625dcb2d641
HMAC of msg2: d6025e3bd35aac9aac30e7643d778ddc75414efce655df9e96d67675bb91c708

Security Notes

Feature Description
Secure Resistant to length-extension, collision attacks, and key-recovery
Preferred Over simple hashing (hash(key + msg)), which is insecure
Used in TLS, SSH, APIs (e.g., AWS signing), JWT authentication
Key Management Key must be secret; exposure compromises security

HMAC vs Other MACs

Feature HMAC (SHA-256) CBC-MAC CMAC
Based On Hash function Block cipher (AES) Block cipher (AES)
Secure for Variable-length messages Fixed-length or prefixed messages Variable-length messages
Collision Safe Yes No (if misused) Yes
Common Uses APIs, TLS, JWT, emails Legacy systems IPsec, Bluetooth

Real-life Applications

🏦 Banking

Securing transaction messages

🌐 APIs & Webhooks

Verifying requests using shared secret keys

🔐 JWT Tokens

Ensuring tokens haven't been tampered with

💾 Software Updates

Ensuring downloaded files are genuine

📱 IoT Communication

Preventing spoofing or tampering

Key Observations

  • The two messages msg1 = b'GECBHARUCH' and msg2 = b'GECBharuch' produce completely different HMACs, even though they differ by just some character.
  • This highlights the avalanche effect — a small change in input drastically changes the output in cryptographic systems.
  • The HMAC depends on both the message and the secret key. Even with the same message, a different key would result in a different MAC.

Summary

Concept Explanation
HMAC A MAC based on a hash function and a secret key
SHA-256 A secure hash algorithm used within the HMAC
Key Role Key is required to compute and verify the MAC
Use Case To verify that a message is authentic and unaltered

Learning Outcomes

After this practical, students will be able to:

🎯 Understand HMAC Purpose

Grasp the role of HMAC in securing messages

🔍 Observe Sensitivity

See how HMAC responds to message and key changes

⚖️ Distinguish MACs

Differentiate HMAC from other MACs like CBC-MAC