# MACs and Digital Signatures We now turn our attention to protecting the integrity of messages with cryptography. **Threat:** A Dolev–Yao attacker. **Harm:** The information contained in messages could be modified, thus violating integrity. **Harm:** The purported sender of a message could be changed, thus violating integrity. **Vulnerability:** Messages sent on the communication channel between the sender and receiver can be modified by untrusted principals. **Countermeasure:** Like encryption, there are symmetric and asymmetric algorithms for protecting integrity. The symmetric version is called a *message authentication code* (MAC). The asymmetric version is called a *digital signature*. ### Encryption and integrity Encryption is designed to protect confidentiality. There are some block modes designed in last decade to protect both confidentiality and integrity. But encryption does not, in general, protect integrity. The usual mistake is to reason as follows: "The message is encrypted. If attacker changes the ciphertext, it will decrypt to nonsense. That nonsense can be detected." But that reasoning is not valid. For example: - Attackers can be smart about changes. A ciphertext from another execution of the same protocol might decrypt just fine. - The plaintext block could itself be a random number, and recipient would have no way of determining whether it's the right one if the attacker substitutes a different random number. - In CTR mode (or any *stream cipher*), it's easy to flip individual bits. E.g., change "admin=0" to "admin=1" just by knowing position of that bit in stream. - In CBC mode, it's easy to truncate blocks from the beginning of a message. For more examples, see section 9.6.5.i, "Encryption alone does not guarantee data integrity", in [HAC][hac]. [hac]: http://cacr.uwaterloo.ca/hac/ ### Cryptographic hash functions Both MACs and digital signatures use another primitive that we'll cover first: a *cryptographic hash function*, also called a *message digest*, which takes an arbitrary size input m and produces a fixed length output H(m). The output length is typically 128–1024 bits. The goal of a cryptographic hash is to produce a compact representation of an original object. That representation should behave much like a fingerprint: - It's hard to find 2 people with same fingerprint. That's true whether you get to pick pairs of people, or whether you are given one person then must find another. That means fingerprints are **collision resistant**. - Given a person, it's easy to get their fingerprint. But given a fingerprint, it's hard to find the person it came from. (Which is why law enforcement invests money in building databases to do just that.) That means fingerprints are **one way**. Likewise, cryptographic hash functions must be collision resistant and one way. Cryptographic hash functions are not the same as the ordinary hash functions that are used to implement hash tables, even though both compress their inputs. Collision resistance and one way-ness are not required of ordinary hash functions. The strength of a hash function is, in the absence of any clever attacks, half the function's output length. E.g., if the output length is 256 bits, then the strength is at most 128 bits. Why? There's a generic attack that works on all hash functions that halves the security level. It's called the *birthday attack*. MD5 and SHA-1 used to be the most commonly used hash functions. But: - The collision resistance of MD5 (invented by Ron Rivest in 1991) was broken in 2004–8. It's now possible to find collisions in mere seconds. Moreover, the collisions can even be engineered to be (maliciously) useful, for example, [generating rogue CA certificates](http://www.win.tue.nl/hashclash/rogue-ca/). - The collision resistance of SHA-1 (released by the NSA in 1995) has been broken in ongoing work since 2005. Attacks are known that reduce its strength to only 65 bits or fewer. SHA-2, released by the NSA in 2001. is actually a whole family of algorithms, SHA-{224,256,384,512}. The name indicates the output size in bits. Each should have security level equal to its output size halved. But these are based on similar ideas to SHA-1, so there's concern that they might one day turn out to be vulnerable to similar attacks. SHA-3 was released in 2015. NIST held a public competition for the new algorithm. There were five finalists, all based on different ideas than SHA-1 and SHA-2, and all developed openly and peer reviewed. The output size can be 224, 256, 384, or 512 bits; or a variable-length output can be produced using a variant called SHAKE. ### Message authentication codes A *message authentication code* (MAC) is an algorithm for detecting modification of messages based on a shared key. ``` 0. k = Gen(len) // A and B somehow share key k 1. A: t = MAC(m; k) // t is called the "tag" 2. A -> B: m, t 3. B: verify t = MAC(m; k) ``` The length of input m to MAC may be arbitrary. The output length of MAC is fixed and depends upon the particular MAC algorithm. When is a MAC secure? It should behave like a *random function*, for each key. Especially, it shouldn't be possible to predict new (m,t) pairs if you don't know k. There are many examples of MACs. HMAC (a hash-based MAC) is one of the most common. It is parameterized on a cryptographic hash function, which can be instantiated, for example, by any of the SHA family. Another example is CBC-MAC, which is parameterized on a block cipher and uses that block cipher in CBC mode. ### Digital signatures A *digital signature scheme* is a set of algorithms for detecting modification of messages based on an asymmetric key pair. The public key for principal A, written K_A, is used to verify A's signatures. The private key for principal A, written k_A, is used by A to create signatures. ``` 0. (K_A, k_A) = Gen(len) 1. A: s = Sign(m; k_A) 2. A -> B: m, s 3. B: accept if Ver(m; s; K_A) ``` The digital signature scheme is the triple (Gen, Sign, Ver) of algorithms. Note that Ver takes three inputs: the message to verify, the purported signature on that message, and the verification key of the signer. As with MACs, we want to be able to sign arbitrary length messages. But these Sign and Ver are public-key algorithms, which operate on big integers. So, as with public-key encryption, they are constrained to a limited input size. In practice, messages are therefore hashed before being signed: ``` 0. (K_A, k_A) = Gen(len) 1. A: s = Sign(H(m); k_A) 2. A -> B: m, s 3. B: accept if Ver(H(m); s; K_A) ``` Hashing is such a pervasive practice with signatures that, henceforth, we'll just assume the message is hashed without bothering to write that down as part of the protocol. When is a digital signature scheme secure? It should work like hand-written signatures. In fact, it should be even better: an adversary shouldn't be able to forge signatures on new messages, even if given samples of other signed messages. Well-known examples of digital signature schemes include the following: - **Digital Signature Algorithm (DSA).** Released by NIST in 1991 as part of the Digital Signature Standard (DSS). There is no proof of security for DSA, but it's been used for decades now without any serious attacks being discovered. Originally, the standard required SHA-1 as the hash, but that's since been updated. Also, the keys were originally short, but have since been lengthened. - **RSA.** The RSA encryption scheme can be adapted for use as a digital signature scheme. The Gen algorithm stays the same. Sign is roughly equivalent to Dec, and Ver to Enc. However, they aren't exactly the same. Sometimes people will say that you should "do an RSA decryption to sign". That's partially correct, but also partially wrong. Here is a set of notes explaining [why RSA decryption is not the same as RSA signing][rsa-dec-sign]. Also, **never use the same key pair for both signing and encryption**. There's a provably secure variant of RSA signatures called RSA-PSS (*probabilistic signature scheme*). [rsa-dec-sign]: http://www.cs.cornell.edu/courses/cs5430/2015sp/notes/rsa_sign_vs_dec.php ### Authenticated encryption Suppose you want to protect both confidentiality and integrity. The cryptographic primitive you want is called *authenticated encryption*. There are three generic ways of constructing authenticated encryption out of a standard block cipher and MAC. All three are used in real-world protocols. - **Encrypt and MAC.** Encrypt the message. Separately MAC the message under a different shared key. Send both the ciphertext and the tag. This algorithm is probably the worst of the three, because the tag could reveal information about the plaintext. Secure Shell (ssh) uses this algorithm in a safe way. - **Encrypt then MAC.** Encrypt the message. MAC the resulting ciphertext. Send both the ciphertext and the tag. This is provably the most secure of the three algorithms; IPsec uses it. - **MAC then Encrypt.** MAC the plaintext message. Encrypt the message and the tag together. Send the resulting ciphertext. As long as the MAC algorithm is strong enough (and HMAC is), this algorithm is just as secure as the previous one. SSL uses this algorithm. There are also block cipher modes that are specifically designed to achieve both confidentiality and integrity. *Galois/Counter Mode* (GCM) is a popular choice, because it has high performance and is not encumbered by patents.