85 min total

Information Security

MACs and HMAC

MACs and HMAC

Encryption protects against passive attack,

Message or Data Authentication protects against active attack

For a message to be authentic, the message should not have been altered and the source must be authentic

For the rest of this note, both authentication of transmitted messages and of stored data is referred to as message authentication

Authentication using Symmetric encryption

Symmetric encryption has a shared key between sender and receiver

It is not suitable for data authentication due to potential threats such as block reordering, which can alter the meaning of the overall data sequence, making block reordering a threat.

E.g. in the ECB mode of encryption, if an attacker reorders the blocks of ciphertext, then each block will still decrypt successfully. However, the reordering may alter the meaning of the overall data sequence.

Symmetric encryption alone does not provide complete data authentication.

Message Authentication without Message Encryption

Generates and appends an authentication tag to each message for transmission. The message is not encrypted, allowing readability at any point.

But if someone changes the message, the authentication tag won't match the altered content, so the responsible system can detect the tampering.

Scenarios where message authentication without confidentiality is preferable:

  • Broadcast messages: Applications often broadcast the same message to multiple destinations, e.g. network notifications, alarm signals. However, having receiver responsible for authentication is cheaper and more reliable, requiring plaintext broadcast with an authentication tag.
  • Selective Authentication: In situations where one side is heavily loaded, cannot afford to decrypt all incoming messages, authentication is selective, with messages chosen randomly for checking, avoiding the need for decryption of all incoming messages.
  • Program Integrity: Authentication without encryption is a cost-effective program execution method that attaches a message authentication tag. When the program is executed, the system can quickly check the tag to ensure that the program has not been tampered with. Since program is in plaintext, it can be run directly without decryption, which saves processor resources.

Message Authentication Code (MAC)

A technique where the secret key is used to generate a small block of data called a Message Authentication Code (MAC). The MAC is appended to the message to ensure Authentication.

Process

  • A and B share a common secret key
  • Sender(A) computes MAC using a function that takes key and message as inputs
  • The message + MAC are transmitted to the recipient(B)
  • Recipient(B) computes MAC using received message and shared key, compares it to received MAC
  • If B MAC = A MAC then message is authenticated

+ve) Ensures message hasn’t been altered (integrity)

+ve) Confirms the message origin (authenticity)

+ve) Prevents alteration of sequence numbers

To generate MAC, earlier DES was used. AES is preferred nowadays. Historically, 16- or 32-bit MACs were common, but they are no longer sufficient for security.

Message authentication code generation and verification with a shared key
Transmission of a message with its message authentication code

One-Way Hash Functions

Accepts variable size message as input and produces a fixed-size message digest H(M) as output, unlike a MAC, it doesn’t require a secret key.

The message digest is padded (e.g. 1024 bits) out to some fixed length

Three ways in which message can be authenticated using a hash function:

  • Symmetric encryption:
    Uses Hash function to generate message digest which is encrypted using symmetric key. Sender sends original message AND encrypted digest to receiver. Receiver decrypts digest using symmetric key and computes hash of received unencrypted message using the same one-way hash function

Receiver compares decrypted digest with newly computed digest, if they both match it means message was not altered and came from sender who possess the same symmetric key

It leaves message vulnerable to passive attacks, it is what it is :)

Message authentication using a hash and symmetric encryption
  • Public-Key encryption:
    The message digest is encrypted using a sender's private key, allowing the receiver to verify the signature using the sender’s public key. This not only authenticates the message but also provides a digital signature.
  • sender hashes the message to create a message digest.
  • message digest is encrypted with the sender’s private key.
  • The message and the encrypted digest (digital signature) are sent to the receiver.
  • The receiver uses the sender’s public key to decrypt the digest and recomputes the hash from the message. If the two match, the message is authentic and unaltered.
Textbook illustration of signing a message digest and verifying it with a public key
  • Keyed Hash MAC:
    The message and a shared secret key are combined and passed through a hash function. The result, known as a MAC, is then sent along with the message to the receiver. The receiver, who also knows the secret key, computes the hash in the same way and compares it with the received MAC.
  • The sender concatenates the message with the shared secret key (or appends and prefixes the key to the message).
  • The sender then hashes the combination using a secure hash function (e.g., SHA-256) to produce the message digest (MAC).
  • The sender sends both the message and the MAC to the receiver.
  • The receiver, knowing the shared secret key, performs the same operation (concatenates the key with the message and hashes it) and compares the result with the received MAC. If they match, the message is authentic.
Keyed message digest formed from a message and a shared secret

Hash Function requirements

  • Variable-size input: The hash function should accept input data (message, file, or block) of any size.
  • Fixed-size output: The output (message digest or hash value) should always be of a fixed size, regardless of the size of the input.
  • Efficient to compute: The hash function should be computationally efficient, making it practical to implement in both hardware and software.
  • Preimage resistance (One-way property): It should be computationally hard to find an input x such that the hash of x matches a given hash value h. In other words, given known h from H(x), it should be difficult to find x.
  • Second preimage resistance: H(x) is one-way.
    For a given input x, it should be computationally infeasible to find a different input y(where y≠xy) such that H(y)=H(x). This is also known as weak collision resistance.
  • Collision resistance: H(x) is collision-free
    It should be computationally infeasible to find any two distinct inputs x and y such that H(x)=H(y). This is known as strong collision resistance.
Preimage, second-preimage, and collision resistance

Diffie Hellman Key Exchange Algorithm

Method used to securely share cryptographic keys over a public channel.

It allows two parties to generate a shared secret key, which can be used for symmetric encryption, without directly transmitting the key itself.

Two numbers are agreed upon publicly:

n (Large prime number)

g (Generator)

Each party selects a private key:

Party A chooses a private key a

Party B chooses a private key b

Each party computes their public key:

  • Party A computes A= mod n
  • Party B computes B= mod n

The public keys A and B are exchanged over the public channel.

Each party computes the shared secret using the other party's public key:

  • Party A computes S = mod n
  • Party B computes S = mod n

Both computations result in the same shared secret key, which can now be used for encryption.

Even if an attacker knows n, g, A, and B, they cannot easily compute S without knowing the private keys a and b.

Man in the Middle Attack

Type of cyberattack where an attacker secretly intercepts and relays communication between two parties who believe they are directly communicating with each other. The attacker can eavesdrop, modify, or inject messages without the knowledge of either party.

More content …