Blog | G5 Cyber Security

Verify Ciphertext Authenticity

TL;DR

This guide shows you how to check if a ciphertext message hasn’t been tampered with using a Message Authentication Code (MAC). We’ll cover generating the MAC, verifying it upon receipt, and what to do if verification fails.

Generating & Verifying a MAC

  1. Choose a MAC Algorithm: Common choices include HMAC-SHA256 or HMAC-SHA512. The stronger the algorithm, generally the more secure (but also potentially slower).
  2. Shared Secret Key: You and the sender must agree on a secret key beforehand. This is crucial! Keep it safe – don’t share it in emails or public places.
  3. Generate the MAC (Sender Side): Before sending the ciphertext, the sender calculates its MAC using the shared secret key and the chosen algorithm.
    # Example using Python with HMAC-SHA256
    import hmac
    hash_object = hmac.new(b'your_secret_key', msg=ciphertext.encode(), digestmod='sha256')
    mac = hash_object.hexdigest()
    print(mac)

    The sender sends both the ciphertext and the MAC to the receiver.

  4. Verify the MAC (Receiver Side): The receiver recalculates the MAC using the same secret key and algorithm, but with the received ciphertext.
  5. # Example using Python with HMAC-SHA256
    import hmac
    hash_object = hmac.new(b'your_secret_key', msg=received_ciphertext.encode(), digestmod='sha256')
    calculated_mac = hash_object.hexdigest()
    print(calculated_mac)
  6. Compare MACs: The receiver compares the calculated MAC with the received MAC.
    • If they match: The ciphertext is likely authentic – it hasn’t been altered in transit.
    • If they don’t match: The ciphertext has been tampered with, or the wrong secret key was used. Do not trust the message!

What to do if Verification Fails

  1. Check Key Agreement: Double-check that you and the sender are using the exact same shared secret key. A single character difference will cause a mismatch.
  2. Resend Message: Ask the sender to resend the message, ensuring they haven’t accidentally modified it before sending.
  3. Investigate Potential Attacks: If repeated failures occur, consider whether your communication channel might be compromised (e.g., man-in-the-middle attack).

Important Considerations

Exit mobile version