🔐 CBC-MAC Hash Function

Practical 8: Hash Function using CBC-MAC (Based on Block Cipher)

Understanding Message Authentication in Symmetric Cryptography

🎯 Objective & Purpose

🎯

Objective

Implement CBC-MAC using AES encryption and understand how it ensures message integrity in symmetric cryptography.

🔍

Purpose

Understand CBC-MAC working, observe MAC changes with message alterations, and demonstrate AES block cipher usage.

📚 Theory: What is CBC-MAC?

CBC-MAC (Cipher Block Chaining - Message Authentication Code) is a method of constructing a message authentication code using a block cipher like AES in CBC mode.

Key Characteristics:

  • ✅ Ensures message integrity and authenticity
  • 🔑 Requires a secret key (unlike cryptographic hashes)
  • 🔗 Uses the last ciphertext block in CBC mode as the MAC
  • 🛡️ Common in secure communication protocols

Why Use CBC-MAC?

💻 Implementation Code

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Random import get_random_bytes

def cbc_mac(message, key):
cipher = AES.new(key, AES.MODE_CBC, iv=bytes(16))
padded = pad(message.encode(), 16)
ciphertext = cipher.encrypt(padded)
return ciphertext[-16:].hex() # Last block is MAC

key = get_random_bytes(16)
msg1 = "GECBHARUCH"
msg2 = "GECBHARUCH?"

print("CBC-MAC of 'GECBHARUCH':", cbc_mac(msg1, key))
print("CBC-MAC of 'GECBHARUCH?':", cbc_mac(msg2, key))

📤 Output:

CBC-MAC of 'GECBHARUCH': d886959fd150509ac60bf9c4b4262dc8
CBC-MAC of 'GECBHARUCH?': 2c4420278f0a88dbe792fbde4d242b59

📊 Important Features

Feature Explanation
Key is required Unlike hash functions, CBC-MAC uses a symmetric key
Sensitive to input Small changes in input → completely different MAC
Fixed IV (zero) CBC-MAC standard requires IV = 0
Not collision-resistant CBC-MAC is for integrity, not hashing large data
Used in Secure protocols like IPsec, TLS (modified forms)

🔒 Security Reminder

⚠️ CBC-MAC should only be used when:

  • The message length is fixed, OR
  • It is combined with length-prefixing to prevent length-extension attacks

🛡️ More Secure Alternatives:

  • HMAC: Based on SHA-256
  • CMAC: Block-cipher-based MAC designed to be secure even for variable-length messages

🌍 Real-life Applications

💬

Secure Messaging

Ensures messages are not tampered with during transmission

💳

Payment Systems

Verifies the authenticity of transferred financial data

🔄

Firmware Updates

Guarantees that update files are untampered

🌐

IoT & Embedded Systems

Used where symmetric cryptography is preferred due to resource constraints

🔑 Key Observation

The MAC for msg1 = "GECBHARUCH" and msg2 = "GECBHARUCH?" is entirely different, even though the messages are nearly identical.

This occurs due to:

  • Block-based operation: CBC-MAC operates in fixed-size blocks (16 bytes for AES)
  • Avalanche effect: Small changes in plaintext result in large changes in ciphertext/MAC
  • Last block sensitivity: Only the last ciphertext block is used as MAC

📈 Learning Outcomes

After this practical, students will be able to:

🎓

Block Cipher Understanding

Understand how a block cipher can be used to generate a MAC

🔗

CBC Mode & Padding

Learn how padding and CBC mode work together

🛠️

AES Implementation

Use AES in CBC mode to generate a MAC for messages

📊

Sensitivity Analysis

Observe how small message changes affect the MAC output

📝 Summary

In this practical, we implemented CBC-MAC using AES to authenticate messages. The output MAC depends heavily on the exact message content and length. Even a minor change in the message completely changes the MAC, making it suitable for message integrity verification in secure systems.

CBC-MAC is a powerful tool in symmetric cryptography, particularly in resource-constrained environments like IoT and embedded systems.