Public key, hash, MAC, signature, PKI and TLS
RSA and Diffie-Hellman by hand with small numbers, SHA-256 and the birthday bound, MAC/HMAC, digital signatures, certificate chains and the TLS handshake. The RSA and DH calculation questions of the exam are on this page.
The previous page left the big problem of symmetric encryption: how do we share the key? This page gives the answer and puts together all the parts that make up a secure Internet connection. The exam almost certainly has an RSA or Diffie-Hellman calculation; also “which key is used?” and “which mechanism provides what?” questions. Golden rules:
- For confidentiality the receiver’s public key, for a signature the sender’s private key.
- When doing modular exponentiation, take the mod after every multiplication. The numbers stay small.
- In RSA, must be coprime to . Otherwise does not exist.
- A hash is keyless, a MAC uses a shared key, a signature uses a private key. Only the signature gives non-repudiation.
- You verify a certificate with the public key of whoever signed it.
Public-key cryptography
In a public-key (asymmetric) system everyone has a key pair: the public key is distributed to everyone, and the private key stays with the owner. What is done with one can be undone only by the other. Security rests on mathematical problems that are easy in one direction but very hard in the reverse: factoring a large number (RSA) and the discrete logarithm (Diffie-Hellman).
Asymmetric operations are hundreds of times slower than symmetric ones. That is why hybrid use is the practice: the public key is used to establish a session key or to sign, and the data is encrypted with AES.
RSA
Key generation:
- Choose two primes: , .
- and .
- Choose an with .
- , that is .
- Public key , private key .
Encryption is , decryption is ().
Worked example. , .
n = 13 × 17 = 221
φ(n) = 12 × 16 = 192
e = 3 won't do (3 divides 192); choose e = 5 (gcd(5,192) = 1)
d : 192 = 38·5 + 2, 5 = 2·2 + 1
1 = 5 − 2·2 = 5 − 2·(192 − 38·5) = 77·5 − 2·192
d = 77 check: 5 × 77 = 385 = 2·192 + 1
Encrypt : , step by step:
42^1 = 42
42^2 = 1764 mod 221 = 217
42^3 = 217·42 = 9114 mod 221 = 53
42^4 = 53·42 = 2226 mod 221 = 16
42^5 = 16·42 = 672 mod 221 = 9 → c = 9
Decrypt . Instead of doing 77 multiplications, use square-and-multiply: .
9^1 = 9 9^2 = 81 9^4 = 81^2 mod 221 = 152
9^8 = 152^2 mod 221 = 120 9^16 = 35 9^32 = 120 9^64 = 35
9^77 = 9^1 · 9^4 · 9^8 · 9^64
= 9 · 152 = 1368 mod 221 = 42
42 · 120 = 5040 mod 221 = 178
178 · 35 = 6230 mod 221 = 42 → m = 42
The message came back. If an attacker can factor , they find and . In real RSA is 2048 bits or more; factoring it is practically impossible with today’s computers.
PITFALL · computing mod . The inverse is taken mod : . A computed with comes out wrong. Encryption and decryption, on the other hand, are done with .
Diffie-Hellman key exchange
Two parties produce a shared secret key over an open channel, without sharing any secret beforehand. Public to everyone: a prime and a generator . Alice chooses a secret , Bob chooses a secret .
Worked example. , , , .
A = 2^6 mod 29 = 64 mod 29 = 6
B = 2^11 mod 29 = 2048 mod 29 = 18
Alice: K = 18^6 mod 29. 18^2 = 324 mod 29 = 5, 18^4 = 25, 18^6 = 25·5 = 125 mod 29 = 9
Bob: K = 6^11 mod 29. 6^2 = 7, 6^4 = 20, 6^8 = 23, 6^11 = 23·7·6 = 966 mod 29 = 9
Both found . An eavesdropper sees only 29, 2, 6 and 18; finding from is the discrete logarithm problem.
PITFALL · DH does not authenticate. A man-in-the-middle runs a separate DH with each side. That is why in real protocols the DH values are signed (in TLS, the server certificate and CertificateVerify).
Hash functions
A cryptographic hash function turns input of any size into a fixed-size output. SHA-256 produces 256 bits, that is 64 hexadecimal characters. Three security properties:
| Property | Meaning |
|---|---|
| Preimage resistance | Given , you cannot find an with |
| Second preimage resistance | Given , you cannot find another with |
| Collision resistance | You cannot find any pair with |
SHA-256("abc") = ba7816bf8f01cfea414140de5dae2223 b00361a396177a9cb410ff61f20015ad
SHA-256("SEN2008") = 7f0f2c908360e1f1...
SHA-256("SEN2009") = 8ef7e4e5e6c5c0bd... (127 of 256 bits differ)
When one character changes, about half of the bits change (avalanche). Birthday attack: for an -bit hash a collision is found in about attempts. For SHA-256 that is . Practical collisions have been found in MD5 and SHA-1, and they are not used in new systems.
MAC and HMAC
A plain hash does not protect integrity: an attacker modifies the message and recomputes the hash. A message authentication code (MAC) uses a shared secret key: . Whoever doesn’t know the key cannot produce a valid tag. The most common construction is HMAC:
HMAC-SHA256("bau-key", "pay 100") = 0bf39f2a443f4059...
HMAC-SHA256("bau-key", "pay 900") = 5ab3cabd5e34bc43...
A MAC provides integrity and source authentication, but not non-repudiation: both sides have the key.
Digital signature
The sender signs the hash of the message with their private key; anyone verifies with the public key. With RSA: , verification .
A toy example (, , ), hash :
s = 30^77 mod 221 = 166
verify: 166^2 = 152, 166^3 = 38, 166^4 = 120, 166^5 = 30 (mod 221) → 30 = h, valid
| Hash | MAC | Digital signature | |
|---|---|---|---|
| Key | none | shared secret | private / public |
| Integrity | against accidents | yes | yes |
| Source authentication | no | yes | yes |
| Non-repudiation | no | no | yes |
Certificates and PKI
How will you know that a public key really belongs to the claimed person? A certificate (X.509 certificate) binds an identity (a domain name) to a public key and is signed by a certificate authority (CA). It contains: the subject, the public key, the issuer, validity dates, a serial number and the CA’s signature.
Chain of trust: root CA (in the browser’s trust store) → intermediate CA → the server (leaf) certificate. Each certificate is verified with the public key of its issuer; the chain must reach a trusted root. This whole structure is called the public key infrastructure (PKI). A certificate whose private key was stolen is revoked (CRL, OCSP).
The TLS handshake
TLS provides confidentiality, integrity and server authentication over TCP (HTTPS = HTTP + TLS). The TLS 1.3 handshake finishes in one round trip:
Client → ClientHello: supported versions, cipher suites, random value, (EC)DHE key share
Server → ServerHello: chosen suite, its own key share
(everything after this is encrypted with the handshake keys)
Certificate the server certificate chain
CertificateVerify signature of the handshake hash with the server's private key
Finished MAC over the entire handshake
Client → Finished
Both directions: application data with AES-GCM (or ChaCha20-Poly1305)
The role of the parts: (EC)DHE produces the shared key, the certificate and CertificateVerify prove the server’s identity and prevent MITM, the Finished messages verify that the handshake was not modified, and AES-GCM encrypts the data and protects its integrity. Since a new DH key is used in every session, old recordings cannot be decrypted even if the server’s private key is stolen later (forward secrecy). TLS 1.2 had the option of RSA key transport and lacked this property; 1.3 removed it.
Common mistakes
| Wrong idea | Correct |
|---|---|
| A signature is made with the receiver’s public key | It is made with the sender’s private key |
| A secret message is encrypted with the sender’s private key | With the receiver’s public key |
| Any is valid | is required |
| First compute fully, then take the mod | Take the mod after every multiplication |
| DH is resistant to a man-in-the-middle | Unauthenticated DH is open to MITM |
| A hash protects the message | A keyless hash is recomputed; a MAC or signature is needed |
| A MAC provides non-repudiation | It doesn’t, because of the shared key |
| A collision in a 128-bit hash needs attempts | Birthday: about |
| A certificate is verified with the server’s own key | With the public key of the issuing CA |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
RSA key generation with p = 7, q = 19 and public exponent e = 5. Find n, φ(n) and the private exponent d.
Show answer
.
.
is the number with . Extended Euclid: , , . Substituting back gives , so .
Check: .
Using the RSA public key (e, n) = (5, 133), encrypt the message m = 10.
Show answer
. Take the mod at every step so the numbers don't grow:
, , , , .
. Decryption (verified with node).
RSA with p = 11, q = 23 and e = 3. Compute the private exponent d.
Show answer
. , so is valid.
. Since , , so .
Check: .
For RSA with p = 13 and q = 17, why can e = 3 NOT be used as the public exponent?
- 3 is larger than p − 1
- 3 divides φ(n) = 192, so e has no inverse mod φ(n)
- 3 is not prime
- e must always equal 65537
Show answer
. Since , the equation has no solution; cannot be found. Rule: must be coprime to . That is why is chosen in this example ().
Diffie-Hellman with public p = 31 and g = 3. Alice picks a = 5, Bob picks b = 8. Compute A, B and the shared key K.
Show answer
.
: , .
Alice: . , , .
Bob computes and gets the same 25. Only 31, 3, 26 and 20 are visible on the network; 25 is never sent.
Plain (unauthenticated) Diffie-Hellman is vulnerable mainly to which attack?
- Brute-forcing the prime p
- A man-in-the-middle who runs a separate exchange with each side
- Frequency analysis
- Replay of the shared key
Show answer
DH does not verify who the other party is. Mallory sets up one key with Alice and another with Bob, and decrypts and re-encrypts the traffic in between. Both sides see a channel they believe is secure. The fix: sign the DH values or authenticate them with a certificate. In TLS, the server signs the handshake with the key in its certificate.
A hash function produces 64-bit outputs. Roughly how many random inputs must be hashed before a collision becomes likely (birthday bound)?
Show answer
Birthday bound: for an -bit output a collision is expected after about attempts. .
This is easy on today's hardware; a 64-bit hash is insecure. For SHA-256 the bound is . Finding a preimage requires ; a collision is always cheaper.
Alice signs a message with her digital signature. Which key does Bob use to verify the signature?
- Bob's private key
- Bob's public key
- Alice's private key
- Alice's public key
Show answer
A signature is made with the sender's private key and verified with their publicly known public key. For encryption it is the reverse: to send a secret message to Bob, you encrypt with Bob's public key, and Bob decrypts with his own private key. In short: confidentiality uses the receiver's key, a signature uses the sender's key.
Toy RSA signature with n = 133, private d = 65, public e = 5. The message hash is h = 12. Compute the signature s and show that it verifies.
Show answer
Signature: (by square-and-multiply, verified with node).
Verification: compute and compare with the hash.
, , , .
: the signature is valid. A message with one bit changed has a different hash and verification fails.
Which mechanism provides non-repudiation, so that the sender cannot later deny having sent the message?
- HMAC with a shared secret key
- SHA-256 hash of the message
- Digital signature with the sender's private key
- AES encryption in CTR mode
Show answer
Both sides have the HMAC key; the receiver can also produce the same tag, so it can't be proven to a third party that "he sent this". A plain hash is keyless, and anyone recomputes it. AES provides confidentiality, it doesn't prove identity. Since the private key is only with the sender, the signature provides non-repudiation.
A browser trusts root CA R. The server sends its certificate S, signed by intermediate CA I, and I's certificate, signed by R. To check the signature on S, which public key does the browser use?
- The server's own public key in S
- The public key of intermediate CA I
- The public key of root CA R
- The browser's private key
Show answer
The signature of a certificate is made with the private key of its issuer, so it is verified with the issuer's public key. I signed S: I's public key is used. Then I's certificate is verified with R's public key. Since R is in the browser's trust store, the chain ends at a trusted root. The domain name, validity dates and revocation status are also checked.
In a TLS 1.3 handshake, what is the main purpose of the server's CertificateVerify message?
- To send the session key encrypted with RSA
- To prove the server holds the private key matching its certificate by signing the handshake transcript
- To compress the application data
- To choose the TCP port
Show answer
A certificate is public, and an attacker can send the same certificate. In CertificateVerify, the server signs a hash of the handshake messages so far with its private key. This both proves that the server really holds the key and authenticates the (EC)DHE values, preventing MITM. In TLS 1.3 the session key is not sent with RSA, it is derived with (EC)DHE.
Verification: RSA keys (modular inverse via the extended Euclidean algorithm), encryption/decryption, the square-and-multiply trace, toy signatures and Diffie-Hellman values were computed with BigInt modular exponentiation; the order of g mod p with a loop; SHA-256 and HMAC-SHA256 values with node:crypto (verify/SEN2008/12.mjs). PKI and TLS questions are conceptual.