Get a Pentest and security assessment of your IT network.

Cyber Security

HMAC: Confirming Message Receipt

TL;DR

No, HMAC alone cannot confirm a message’s existence or receipt. It verifies integrity and authenticity – that the message hasn’t been tampered with and comes from who it claims to be. You need an additional mechanism like sequence numbers, timestamps, or acknowledgements.

Understanding HMAC

HMAC (Hash-based Message Authentication Code) uses a secret key along with a hash function to create a message digest. Both the sender and receiver share this key. The receiver recalculates the HMAC using the received message and the shared key, comparing it to the HMAC included in the message.

# Example Python (using hashlib library)
import hashlib
import hmac

key = b'YourSecretKey'
message = b'This is the message'

hmac_obj = hmac.new(key, message, hashlib.sha256)
digest = hmac_obj.hexdigest()
print(digest) # This is the HMAC value to send with your message

Why HMAC Doesn’t Prove Existence

HMAC only confirms that a message matching a specific hash, calculated using the shared secret key, was sent. It doesn’t guarantee:

  • The message was actually received by the intended recipient.
  • The message wasn’t dropped or ignored during transit.
  • The recipient hasn’t already received the same message before (replay attack).

How to Confirm Message Existence/Receipt

Here are a few ways to add confirmation on top of HMAC:

1. Sequence Numbers

  1. The sender includes an incrementing sequence number with each message.
  2. The receiver tracks the highest received sequence number.
  3. If a message has a sequence number lower than or equal to the last seen, it’s a duplicate and is ignored.
# Example (Conceptual)
sender_sequence = 1
message = f'{sender_sequence}:{data}' # Include sequence in message

receiver_last_seen = 0
received_sequence = int(received_message.split(':')[0])
if received_sequence > receiver_last_seen:
  # Process the message
  receiver_last_seen = received_sequence
else:
  # Ignore duplicate message

2. Timestamps

  1. The sender includes a timestamp with each message.
  2. The receiver checks if the timestamp is within an acceptable window (to account for network delays).
  3. This helps prevent replay attacks, but relies on synchronized clocks.

3. Acknowledgements

  1. After receiving a message and verifying its HMAC, the receiver sends an acknowledgement back to the sender.
  2. The sender waits for this acknowledgement before considering the message successfully delivered.

4. Nonces (Number used Once)

  1. The sender generates a random nonce for each message.
  2. The receiver stores these nonces to prevent replay attacks.
  3. If it receives the same nonce twice, it rejects the message.

Important Considerations

  • Clock Synchronization: Timestamps require reasonably accurate clocks on both sides.
  • Acknowledgement Reliability: Acknowledgements themselves can be lost or delayed; consider retransmissions and timeouts.
  • Sequence Number Storage: The receiver needs to store sequence numbers, which requires memory and potentially a database.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation