SEN2008 · Week 12 · Computer and Network Security: Security Techniques (Encryption, digital signatures, message authentication, and hash functions, Use of cryptography for network security, Protection and defense mechanisms and tools

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.

public-key cryptographykey pairRSAEuler's totientmodular inverseextended Euclidean algorithmsquare-and-multiplyDiffie-Hellmandiscrete logarithmhash functionSHA-256collision resistancebirthday attackMACHMACdigital signaturenon-repudiationcertificatecertificate authorityPKIchain of trustTLS handshake

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:

  1. For confidentiality the receiver’s public key, for a signature the sender’s private key.
  2. When doing modular exponentiation, take the mod after every multiplication. The numbers stay small.
  3. In RSA, ee must be coprime to φ(n)\varphi(n). Otherwise dd does not exist.
  4. A hash is keyless, a MAC uses a shared key, a signature uses a private key. Only the signature gives non-repudiation.
  5. 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:

  1. Choose two primes: pp, qq.
  2. n=pqn = pq and φ(n)=(p−1)(q−1)\varphi(n) = (p-1)(q-1).
  3. Choose an ee with gcd⁡(e,φ(n))=1\gcd(e, \varphi(n)) = 1.
  4. d=e−1 mod φ(n)d = e^{-1} \bmod \varphi(n), that is ed≡1(modφ(n))ed \equiv 1 \pmod{\varphi(n)}.
  5. Public key (e,n)(e, n), private key (d,n)(d, n).

Encryption is c=me mod nc = m^e \bmod n, decryption is m=cd mod nm = c^d \bmod n (m<nm < n).

Worked example. p=13p = 13, q=17q = 17.

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 m=42m = 42: c=425 mod 221c = 42^5 \bmod 221, 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 977 mod 2219^{77} \bmod 221. Instead of doing 77 multiplications, use square-and-multiply: 77=64+8+4+177 = 64 + 8 + 4 + 1.

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 n=221n = 221, they find φ(n)\varphi(n) and dd. In real RSA nn is 2048 bits or more; factoring it is practically impossible with today’s computers.

PITFALL · computing dd mod nn. The inverse is taken mod φ(n)\varphi(n): ed≡1(modφ(n))ed \equiv 1 \pmod{\varphi(n)}. A dd computed with  mod  n\bmod\ n comes out wrong. Encryption and decryption, on the other hand, are done with  mod  n\bmod\ n.

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 pp and a generator gg. Alice chooses a secret aa, Bob chooses a secret bb.

A=ga mod p,B=gb mod p,K=Ba mod p=Ab mod p=gab mod pA = g^a \bmod p, \quad B = g^b \bmod p, \quad K = B^a \bmod p = A^b \bmod p = g^{ab} \bmod p

Worked example. p=29p = 29, g=2g = 2, a=6a = 6, b=11b = 11.

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 K=9K = 9. An eavesdropper sees only 29, 2, 6 and 18; finding aa from AA 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:

PropertyMeaning
Preimage resistanceGiven hh, you cannot find an xx with H(x)=hH(x) = h
Second preimage resistanceGiven xx, you cannot find another x′x' with H(x′)=H(x)H(x') = H(x)
Collision resistanceYou cannot find any pair with H(x)=H(x′)H(x) = H(x')
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 nn-bit hash a collision is found in about 2n/22^{n/2} attempts. For SHA-256 that is 21282^{128}. 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: t=MACK(m)t = \text{MAC}_K(m). Whoever doesn’t know the key cannot produce a valid tag. The most common construction is HMAC:

HMACK(m)=H((K⊕opad) ∥ H((K⊕ipad) ∥ m))\text{HMAC}_K(m) = H\big((K \oplus opad) \,\|\, H((K \oplus ipad) \,\|\, m)\big)

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: s=hd mod ns = h^d \bmod n, verification se mod n=?hs^e \bmod n \stackrel{?}{=} h.

A toy example (n=221n = 221, d=77d = 77, e=5e = 5), hash h=30h = 30:

s = 30^77 mod 221 = 166
verify: 166^2 = 152, 166^3 = 38, 166^4 = 120, 166^5 = 30 (mod 221)  → 30 = h, valid
HashMACDigital signature
Keynoneshared secretprivate / public
Integrityagainst accidentsyesyes
Source authenticationnoyesyes
Non-repudiationnonoyes

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 ideaCorrect
A signature is made with the receiver’s public keyIt is made with the sender’s private key
A secret message is encrypted with the sender’s private keyWith the receiver’s public key
d=e−1 mod nd = e^{-1} \bmod nd=e−1 mod φ(n)d = e^{-1} \bmod \varphi(n)
Any ee is validgcd⁡(e,φ(n))=1\gcd(e, \varphi(n)) = 1 is required
First compute mem^e fully, then take the modTake the mod after every multiplication
DH is resistant to a man-in-the-middleUnauthenticated DH is open to MITM
A hash protects the messageA keyless hash is recomputed; a MAC or signature is needed
A MAC provides non-repudiationIt doesn’t, because of the shared key
A collision in a 128-bit hash needs 21282^{128} attemptsBirthday: about 2642^{64}
A certificate is verified with the server’s own keyWith the public key of the issuing CA

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1medium

RSA key generation with p = 7, q = 19 and public exponent e = 5. Find n, φ(n) and the private exponent d.

Show answer
n = 133, φ(n) = 108, d = 65

n=pq=7⋅19=133n = p q = 7 \cdot 19 = 133.

φ(n)=(p−1)(q−1)=6⋅18=108\varphi(n) = (p-1)(q-1) = 6 \cdot 18 = 108.

dd is the number with 5d≡1(mod108)5d \equiv 1 \pmod{108}. Extended Euclid: 108=21⋅5+3108 = 21 \cdot 5 + 3, 5=1⋅3+25 = 1 \cdot 3 + 2, 3=1⋅2+13 = 1 \cdot 2 + 1. Substituting back gives 1=2⋅108−43⋅51 = 2 \cdot 108 - 43 \cdot 5, so d≡−43≡65d \equiv -43 \equiv 65.

Check: 5⋅65=325=3⋅108+15 \cdot 65 = 325 = 3 \cdot 108 + 1.

Question 2medium

Using the RSA public key (e, n) = (5, 133), encrypt the message m = 10.

Show answer
117

c=me mod n=105 mod 133c = m^e \bmod n = 10^5 \bmod 133. Take the mod at every step so the numbers don't grow:

101=1010^1 = 10, 102=10010^2 = 100, 103=1000 mod 133=6910^3 = 1000 \bmod 133 = 69, 104=690 mod 133=2510^4 = 690 \bmod 133 = 25, 105=250 mod 133=11710^5 = 250 \bmod 133 = 117.

c=117c = 117. Decryption 11765 mod 133=10117^{65} \bmod 133 = 10 (verified with node).

Question 3hard

RSA with p = 11, q = 23 and e = 3. Compute the private exponent d.

Show answer
147

φ(n)=10⋅22=220\varphi(n) = 10 \cdot 22 = 220. gcd⁡(3,220)=1\gcd(3, 220) = 1, so ee is valid.

3d≡1(mod220)3d \equiv 1 \pmod{220}. Since 220=73⋅3+1220 = 73 \cdot 3 + 1, 1=220−73⋅31 = 220 - 73 \cdot 3, so d≡−73≡220−73=147d \equiv -73 \equiv 220 - 73 = 147.

Check: 3⋅147=441=2⋅220+13 \cdot 147 = 441 = 2 \cdot 220 + 1.

Question 4medium

For RSA with p = 13 and q = 17, why can e = 3 NOT be used as the public exponent?

  1. 3 is larger than p − 1
  2. 3 divides φ(n) = 192, so e has no inverse mod φ(n)
  3. 3 is not prime
  4. e must always equal 65537
Show answer
B) 3 divides φ(n) = 192, so e has no inverse mod φ(n)

φ(n)=12⋅16=192=3⋅64\varphi(n) = 12 \cdot 16 = 192 = 3 \cdot 64. Since gcd⁡(3,192)=3≠1\gcd(3, 192) = 3 \ne 1, the equation 3d≡1(mod192)3d \equiv 1 \pmod{192} has no solution; dd cannot be found. Rule: ee must be coprime to φ(n)\varphi(n). That is why e=5e = 5 is chosen in this example (d=77d = 77).

Question 5medium

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
A = 26, B = 20, K = 25

A=ga mod p=35 mod 31=243 mod 31=26A = g^a \bmod p = 3^5 \bmod 31 = 243 \bmod 31 = 26.

B=38 mod 31B = 3^8 \bmod 31: 35=263^5 = 26, 38=26⋅27=702 mod 31=203^8 = 26 \cdot 27 = 702 \bmod 31 = 20.

Alice: K=Ba=205 mod 31K = B^a = 20^5 \bmod 31. 202=400 mod 31=2820^2 = 400 \bmod 31 = 28, 204=282=784 mod 31=920^4 = 28^2 = 784 \bmod 31 = 9, 205=9⋅20=180 mod 31=2520^5 = 9 \cdot 20 = 180 \bmod 31 = 25.

Bob computes Ab=268 mod 31A^b = 26^8 \bmod 31 and gets the same 25. Only 31, 3, 26 and 20 are visible on the network; 25 is never sent.

Question 6medium

Plain (unauthenticated) Diffie-Hellman is vulnerable mainly to which attack?

  1. Brute-forcing the prime p
  2. A man-in-the-middle who runs a separate exchange with each side
  3. Frequency analysis
  4. Replay of the shared key
Show answer
B) A man-in-the-middle who runs a separate exchange with each side

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.

Question 7medium

A hash function produces 64-bit outputs. Roughly how many random inputs must be hashed before a collision becomes likely (birthday bound)?

Show answer
about 2^32 (about 4.3 × 10^9)

Birthday bound: for an nn-bit output a collision is expected after about 2n/22^{n/2} attempts. 264/2=232≈4.29×1092^{64/2} = 2^{32} \approx 4.29 \times 10^9.

This is easy on today's hardware; a 64-bit hash is insecure. For SHA-256 the bound is 21282^{128}. Finding a preimage requires 2n2^n; a collision is always cheaper.

Question 8easy

Alice signs a message with her digital signature. Which key does Bob use to verify the signature?

  1. Bob's private key
  2. Bob's public key
  3. Alice's private key
  4. Alice's public key
Show answer
D) Alice's public key

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.

Question 9hard

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
s = 122; 122^5 mod 133 = 12

Signature: s=hd mod n=1265 mod 133=122s = h^d \bmod n = 12^{65} \bmod 133 = 122 (by square-and-multiply, verified with node).

Verification: compute se mod ns^e \bmod n and compare with the hash.

1222 mod 133=121122^2 \bmod 133 = 121, 1223=121⋅122 mod 133=132122^3 = 121 \cdot 122 \bmod 133 = 132, 1224=132⋅122 mod 133=11122^4 = 132 \cdot 122 \bmod 133 = 11, 1225=11⋅122=1342 mod 133=12122^5 = 11 \cdot 122 = 1342 \bmod 133 = 12.

12=h12 = h: the signature is valid. A message with one bit changed has a different hash and verification fails.

Question 10medium

Which mechanism provides non-repudiation, so that the sender cannot later deny having sent the message?

  1. HMAC with a shared secret key
  2. SHA-256 hash of the message
  3. Digital signature with the sender's private key
  4. AES encryption in CTR mode
Show answer
C) Digital signature with the sender's private key

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.

Question 11medium

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?

  1. The server's own public key in S
  2. The public key of intermediate CA I
  3. The public key of root CA R
  4. The browser's private key
Show answer
B) The public key of intermediate CA I

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.

Question 12medium

In a TLS 1.3 handshake, what is the main purpose of the server's CertificateVerify message?

  1. To send the session key encrypted with RSA
  2. To prove the server holds the private key matching its certificate by signing the handshake transcript
  3. To compress the application data
  4. To choose the TCP port
Show answer
B) To prove the server holds the private key matching its certificate by signing the handshake transcript

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.

Review card

Public key: everyone knows the public key, the private key stays with the owner Confidentiality: encrypt with the RECEIVER's public key. Signature: sign with the SENDER's private key, verify with their public key RSA: n = pq, φ = (p−1)(q−1), gcd(e, φ) = 1, d = e^(−1) mod φ c = m^e mod n m = c^d mod n (take the mod at every step, square-and-multiply) Example: p=13 q=17 → n=221 φ=192 e=5 d=77; m=42 → c=9 DH: A = g^a mod p, B = g^b mod p, K = B^a = A^b = g^(ab) mod p DH does not authenticate → MITM. Fix: signed/certified DH Hash: fixed size, one-way, collision resistant. SHA-256 = 256 bits = 64 hex Birthday: collision ≈ 2^(n/2); preimage ≈ 2^n. MD5, SHA-1 are broken MAC = tag with a shared key (integrity + authentication), NO non-repudiation HMAC = H((K ⊕ opad) || H((K ⊕ ipad) || m)) Signature: s = h^d mod n, verify s^e mod n = h → integrity + authentication + non-repudiation Certificate = (identity + public key) signed by a CA. Chain: leaf → intermediate → root TLS 1.3: ClientHello(key share) → ServerHello(key share), Certificate, CertificateVerify, Finished → AES-GCM data