Salta al contenuto
Note per Studenti Public-Key Cryptography, RSA and TLS Report

Public-Key Cryptography, RSA and TLS

In questa pagina 8

See also: TCP - Connection, Reliability and Flow ControlTCP is the connection-oriented, reliable transport protocol that turns IP's unreliable service into an ordered byte stream without errors or duplicates; the connection opens with a three-way handshake (SYN, SYN+ACK, ACK) and closes with FIN segments, bytes are numbered (sequence number = first byte of the segment, cumulative ACK = next byte expected), the sender may transmit min(rwnd, cwnd) unacknowledged bytes (rwnd, the receiver window in a 16-bit field, is flow control), errors are handled with checksum, ACKs, retransmission timeout and fast retransmit after three duplicate ACKs, and to fill the pipe the window must reach the bandwidth-delay product, with maximum throughput MSS times Wmax over RTT.TCP - Connection, Reliability and Flow Control →, UDP - User Datagram ProtocolUDP is the connectionless, unreliable transport protocol that adds to IP only process-to-process communication through ports and an optional checksum; its header is 8 bytes (source port, destination port, length, checksum), the checksum covers a pseudo-header (IP addresses, protocol 17, length), the UDP header and the data as a one's complement sum (0 means not computed and a computed 0 is sent as 0xFFFF), and UDP has no connection setup, sequence numbers, flow, error or congestion control, which makes it suitable for short messages (DNS, DHCP, RIP, SNMP) and real-time applications.UDP - User Datagram Protocol →.

Asymmetric cryptography

Definition. Asymmetric (public-key) cryptography uses different keys to encrypt and decrypt. The encryption key is public; the decryption key is private, kept secret by each node. The two keys are mathematically linked.

With vBv_B the public and sBs_B the private key of B: C=EvB(P),P=DsB(C).C=E_{v_B}(P),\qquad P=D_{s_B}(C). Required properties: encryption and decryption are easy; generating a key pair is easy; deriving the private key from the public one is infeasible; recovering the plaintext from ciphertext and public key is infeasible. Confidentiality holds because only B knows sBs_B.

Example. n=100n=100 people need 100100 key pairs, against (1002)=4950\binom{100}{2}=4950 secret keys in symmetric cryptography (Hash Functions and Symmetric CryptographyEncryption turns plaintext into ciphertext with a key; hash functions give a fixed-length one-way digest used for integrity and passwords; symmetric ciphers (Caesar, Vigenere, DES, AES with block modes) share one secret key, exchanged for example with Diffie-Hellman; MAC and HMAC authenticate messages with a shared key.Hash Functions and Symmetric Cryptography →). But public keys must be distributed and their link to the owner certified.

Digital certificates

Definition. A certificate is issued by a third party, the Certificate Authority (CA), which validates the identity of the requester. It contains the owner's name and public key, validity, the issuing CA, and is signed with the CA's private key, so anyone with the CA's public key can verify it.

Example. Visiting https://www.google.com, the browser checks the certificate sent by the site: site name, public key, issuer, validity period, CA signature. The browser verifies the signature with the CA's public key, going up the chain of trust to a root CA already present in the operating system or browser, and also checks the dates and that the site name matches.

Digital signature

Encrypting with one's own private key lets anyone with the public key verify the sender. A signature guarantees authenticity, integrity and non-repudiation.

Definition. Signing: compute the hash of the message (e.g. SHA-256), encrypt the hash with the private key (this is the signature), send message and signature. Verification: the receiver computes the hash of the message, decrypts the signature with the sender's public key, and the message is authentic if the two hashes are equal.

Roles are opposite to confidential encryption: signature uses the sender's private key, confidentiality the recipient's public key. The hash is signed because it is short and fast.

Modular arithmetic

n=q d+rn=q\,d+r, n mod d=rn\bmod d=r. Examples: 27 mod 6=327\bmod6=3, 6 mod 4=26\bmod4=2, 21 mod 6=321\bmod6=3, 81 mod 4=181\bmod4=1. One can reduce before operating: (a b) mod d=((a mod d)(b mod d)) mod d(a\,b)\bmod d=\big((a\bmod d)(b\bmod d)\big)\bmod d (same for sums), so numbers never grow. Example with N=3233N=3233: 652=4225≡99265^2=4225\equiv992; 654≡9922=984064≡123265^4\equiv992^2=984064\equiv1232.

Square-and-multiply. Write the exponent in binary, compute successive squares g,g2,g4,…g,g^2,g^4,\dots (reduced modulo dd) and multiply only those matching the 11 digits. g17=g16⋅gg^{17}=g^{16}\cdot g needs 5 multiplications instead of 16; a kk-bit exponent needs at most 2k2k.

Definition (modular inverse). b−1b^{-1} is the inverse of bb modulo dd if b b−1≡1(modd)b\,b^{-1}\equiv1\pmod d. It exists if gcd⁡(b,d)=1\gcd(b,d)=1. Modular division: (a/b) mod d=(a b−1) mod d(a/b)\bmod d=(a\,b^{-1})\bmod d.

Extended Euclid finds the inverse. Inverse of 1717 modulo 31203120: 3120=183⋅17+93120=183\cdot17+9; 17=1⋅9+817=1\cdot9+8; 9=1⋅8+19=1\cdot8+1 (gcd =1=1). Going back: 1=9−8=2⋅9−17=2⋅3120−367⋅171=9-8=2\cdot9-17=2\cdot3120-367\cdot17, so 17−1≡−367≡275317^{-1}\equiv-367\equiv2753. Other examples: 4−1 mod 7=24^{-1}\bmod7=2; 5−1 mod 11=95^{-1}\bmod11=9; (8/4) mod 5=2(8/4)\bmod5=2.

Definition (discrete logarithm). If ba≡y(modd)b^a\equiv y\pmod d, then log⁡by=a\log_b y=a. No polynomial-time algorithm is known. Example: 52≡4(mod7)5^2\equiv4\pmod7, so log⁡54=2\log_54=2.

RSA

RSA (Rivest, Shamir, Adleman, 1978) relies on the difficulty of factoring large numbers.

Formula (RSA key generation).

  1. Choose two large primes pp and qq.
  2. N=p qN=p\,q.
  3. φ(N)=(p−1)(q−1)\varphi(N)=(p-1)(q-1).
  4. Choose e<φ(N)e<\varphi(N) with gcd⁡(e,φ(N))=1\gcd(e,\varphi(N))=1.
  5. d=e−1 mod φ(N)d=e^{-1}\bmod\varphi(N), so d e≡1(modφ(N))d\,e\equiv1\pmod{\varphi(N)}.

Public key PK=(N,e)PK=(N,e); secret key SK=(N,d)SK=(N,d); the secrets (p,q,d,φ(N))(p,q,d,\varphi(N)) are never shared.

Formula (encryption and decryption). For m<Nm<N: c=me mod Nc=m^e\bmod N, m=cd mod Nm=c^d\bmod N.

Why φ(N)=(p−1)(q−1)\varphi(N)=(p-1)(q-1). Integers in 1..N−11..N-1 not coprime with NN are multiples of pp (q−1q-1 of them) or of qq (p−1p-1 of them), none of both. The coprime ones are (N−1)−(q−1)−(p−1)=(p−1)(q−1)(N-1)-(q-1)-(p-1)=(p-1)(q-1).

Why it works (Euler's theorem). For mm coprime with NN, mφ(N)≡1(modN)m^{\varphi(N)}\equiv1\pmod N (multiplying the integers coprime with NN by mm merely permutes them, so the product is unchanged and can be divided out). Since ed=1+kφ(N)ed=1+k\varphi(N): cd≡(me)d=m1+kφ(N)=m⋅(mφ(N))k≡m(modN).c^d\equiv(m^e)^d=m^{1+k\varphi(N)}=m\cdot\left(m^{\varphi(N)}\right)^k\equiv m\pmod N. The result also holds when mm shares a factor with NN (use Fermat's theorem modulo pp and qq separately).

Complete example. p=61p=61, q=53q=53: N=3233N=3233, φ(N)=60⋅52=3120\varphi(N)=60\cdot52=3120, e=17e=17, d=2753d=2753 (17⋅2753=46801=15⋅3120+117\cdot2753=46801=15\cdot3120+1). Message m=65m=65: 652≡99265^2\equiv992, 654≡123265^4\equiv1232, 658≡154765^8\equiv1547, 6516≡78965^{16}\equiv789, so c=6517≡789⋅65≡2790c=65^{17}\equiv789\cdot65\equiv\mathbf{2790}. Decryption: 27902753 mod 3233=652790^{2753}\bmod3233=65.

Security. An attacker who factors NN gets the secret key. pp and qq are random, prime, of similar size (1500 to 3000 bits), and NN exceeds 2048 bits (about 617 decimal digits). Implementation details matter even if the algorithm looks simple.

Random padding is mandatory. With PKCS #1 v1.5 the padded message is 00  02  [random string]  00  [m]\texttt{00}\;\texttt{02}\;[\text{random string}]\;\texttt{00}\;[m]; it defends against adaptive chosen-ciphertext attacks and gives semantic security.

Authentication with RSA

The server sends P=[m∣s]P=[m\mid s] with s=md mod Ns=m^d\bmod N (using the secret key); the client computes se mod Ns^e\bmod N with the public key and accepts if it equals mm.

Example. m=100m=100: s=1002753 mod 3233=1391s=100^{2753}\bmod3233=1391; the client computes 139117 mod 3233=1001391^{17}\bmod3233=100: valid. If mm arrives as 101101 the check fails. Signing a hash h=42h=42 gives s=3065s=3065 and 306517 mod 3233=423065^{17}\bmod3233=42.

Attacks on plain RSA

Malleability. Mallory intercepts c=me mod Nc=m^e\bmod N, picks aa, sends c′=c ae mod N=Enc(m a)c'=c\,a^e\bmod N=\text{Enc}(m\,a); if Bob returns the decrypted m am\,a, Mallory computes (m a) a−1=m(m\,a)\,a^{-1}=m. Example (N=3233N=3233, m=65m=65, c=2790c=2790, a=2a=2): c′=3017c'=3017, decrypted 130130, and 130⋅2−1=65130\cdot2^{-1}=65.

Determinism. Without padding, equal messages always give equal ciphertexts. Countermeasure: mp=m+pm_p=m+p with random padding; Bob removes it and returns m′≠m am'\ne m\,a, so the attack fails.

Signature attack. The attacker asks the server to sign m′=m ae mod Nm'=m\,a^e\bmod N and computes s(m′) a−1=s(m)s(m')\,a^{-1}=s(m). Example (m=7m=7, a=2a=2): m′=2565m'=2565, s(m′)=2101s(m')=2101, 2101⋅2−1=2667=s(7)2101\cdot2^{-1}=2667=s(7). Hence the hash with padding (PSS) is signed, not the bare message.

Where to put security

  • Application layer: end-to-end protection; overhead per data item, not per packet.
  • Transport or network layer: the same mechanism is shared by many applications.

TLS and SSL

Definition. SSL and TLS (Secure Socket Layer, Transport Layer Security) are cryptographic protocols that give reliable secure communication. They work on TCP and use certificates. SSL 3.0 has known vulnerabilities (POODLE) and is discouraged; TLS is recommended.

Main functions: authenticate the endpoints and define the keys; exchange confidential data with symmetric encryption; authenticate messages with a secure hash.

How it works.

  1. The client asks for a protected resource.
  2. The server answers with its certificate (public key and signature).
  3. The client checks that it is valid and trusted (issued by a CA, not expired, valid signature).
  4. The client generates a symmetric session key and encrypts it with the server's public key.
  5. The server decrypts it with its private key.
  6. Both use the session key on an encrypted channel.

TLS 1.0 messages: (1) ClientHello (supported ciphers); (2) ServerHello (chosen cipher); (3) server certificate and signature; (4) client verifies the certificate; (5) client key exchange, encrypted with the server's public key; (6) server obtains the session key with its private key; (7) Client finished; (8) Server finished; then data are encrypted.

Client                                                          Server
  |--(1) ClientHello: supported ciphers ---------------------------->|
  |<-(2) ServerHello: chosen cipher --------------------------------|
  |<-(3) server certificate + signature ----------------------------|
  | (4) verifies the certificate                                     |
  |--(5) client key exchange (encrypted with public key) ----------->|
  |                          (6) derives session key with private key
  |--(7) Client finished ------------------------------------------>|
  |<-(8) Server finished -------------------------------------------|
  |<=============== data encrypted with the session key ============>|

Services: authentication (certificates from a trusted CA), encryption (asymmetric to set up, symmetric for data), integrity (a MAC on every message).

From TLS 1.0 to TLS 1.3

Using the same key pair both to authenticate and to generate the shared key is no longer done: an attacker recording the traffic could later break or obtain the server's private key and read all sessions, even past ones. Now Diffie-Hellman generates the shared key (the private key only signs) and the handshake is shortened.

TLS 1.3
Client                                                            Server
  |--(1) ClientHello [Random, g^c mod p] ------------------------->|
  | (2) K = (g^s mod p)^c           |<-(3) ServerHello [Random, g^s mod p]
  |                                   (4) K = (g^c mod p)^s
  |<-(5) Certificate + Sign(Ks, handshake (1)+(3)), Finished, data
  |--(6) Finished ---------------------------------------------------->|
  |--(7) application data ===========================================>|

KK exists after the first two messages; the certificate (5) is encrypted with KK; the signature covers messages (1)+(3); the random nonces guarantee uniqueness; KsK_s is the server's private key. With client authentication the server asks for a certificate in (5) and the client answers in (6) with its certificate and Sign(Kc,handshake)\text{Sign}(K_c,\text{handshake}).

Example (p=23p=23, g=5g=5). Client c=6c=6 sends 56 mod 23=85^6\bmod23=8; server s=15s=15 sends 515 mod 23=195^{15}\bmod23=19. Client: 196 mod 23=219^6\bmod23=2; server: 815 mod 23=28^{15}\bmod23=2. In real TLS pp has at least 2048 bits (or elliptic curves). Since KK is ephemeral, a future compromise of the private key does not decrypt recorded traffic (forward secrecy).

Example (RTT count). TLS 1.0: 2 RTT before protected data; TLS 1.3: 1 RTT. First HTTPS request with RTT=100\text{RTT}=100 ms: TCP 1 + TLS 1.3 1 + request 1 =3=3 RTT =0.3=0.3 s; with TLS 1.0, 1+2+1=41+2+1=4 RTT =0.4=0.4 s.

Two parts of the protocol. The handshake protocol negotiates parameters, authenticates the parties and establishes the key material; the ServerHello includes the protocol level, chosen cryptographic parameters, a random number with date and time, session identifier, compression method and the certificate. The record protocol protects the traffic in independent records with the traffic keys.

DTLS (Datagram TLS) works over UDP: higher overhead, point-to-point secure association, not compatible with IP multicast. Modes: PreSharedKey, RawPublicKey, Certificate (X.509). Network-layer security (IPsec) is in VPN and IPsecA VPN is a private network built over a public one: a VPN server admitted by the firewall authenticates outside users, and protected IP packets are encapsulated as the payload of other IP packets (IP tunnelling) using IPsec (AH, ESP, tunnel or transport mode, one-way Security Associations) or an SSL/TLS tunnel through TUN/TAP virtual interfaces.VPN and IPsec →.

Common mistakes

Versione ripasso

  • Asymmetric. C=EvB(P)C=E_{v_B}(P), P=DsB(C)P=D_{s_B}(C); the private key cannot be derived from the public one. n=100n=100 people: 100 pairs against 49504950 symmetric keys.
  • Certificate. Issued by a CA after checking identity; contains name, public key, validity, issuer; signed with the CA private key. The browser verifies signature, dates, name and the chain of trust to a root CA.
  • Signature. Hash H(m)H(m) encrypted with the sender's private key; the receiver recomputes H(m)H(m), decrypts with the public key and compares. Gives authenticity, integrity, non-repudiation.
  • Modular arithmetic. 27 mod 6=327\bmod6=3, 81 mod 4=181\bmod4=1. Inverse exists if gcd⁡(b,d)=1\gcd(b,d)=1: 4−1 mod 7=24^{-1}\bmod7=2, 5−1 mod 11=95^{-1}\bmod11=9. Extended Euclid: 17−1 mod 3120=275317^{-1}\bmod3120=2753. Square-and-multiply: at most 2k2k multiplications for a kk-bit exponent. Discrete log: 52≡4(mod7)5^2\equiv4\pmod7.
  • RSA keys. p,qp,q primes, N=pqN=pq, φ(N)=(p−1)(q−1)\varphi(N)=(p-1)(q-1), gcd⁡(e,φ)=1\gcd(e,\varphi)=1, d=e−1 mod φd=e^{-1}\bmod\varphi. PK=(N,e)PK=(N,e), SK=(N,d)SK=(N,d).
  • RSA operation. c=me mod Nc=m^e\bmod N, m=cd mod Nm=c^d\bmod N, m<Nm<N. Euler: mφ≡1m^{\varphi}\equiv1, so med=m1+kφ≡mm^{ed}=m^{1+k\varphi}\equiv m.
  • Example. p=61p=61, q=53q=53, N=3233N=3233, φ=3120\varphi=3120, e=17e=17, d=2753d=2753; m=65→c=2790→65m=65\to c=2790\to65.
  • Security. Factoring NN breaks RSA; N≥2048N\ge2048 bits; p,qp,q random and of similar size. Padding mandatory (PKCS#1 v1.5: 00 02 [random] 00 [m]).
  • RSA signature. s=md mod Ns=m^d\bmod N, check se mod N=ms^e\bmod N=m. m=100m=100: s=1391s=1391. Sign the hash with padding, not the bare message.
  • Attacks on plain RSA. Malleability: c′=c aec'=c\,a^e, recover m=(m a) a−1m=(m\,a)\,a^{-1} (c′=3017c'=3017, decrypted 130130). Deterministic encryption. Signature forgery: s(m′) a−1=s(m)s(m')\,a^{-1}=s(m) (m=7m=7, s=2667s=2667).
  • Where. Application layer: end-to-end; transport or network layer: shared by many applications.
  • TLS (over TCP; SSL 3.0 discouraged, POODLE): endpoint authentication with certificate, symmetric session key for data, MAC for integrity.
  • TLS 1.0 steps. ClientHello, ServerHello, certificate+signature, certificate check, client key exchange (encrypted with server public key), server decrypts, Client finished, Server finished.
  • TLS 1.3. Diffie-Hellman gives K=(gs)c=(gc)sK=(g^s)^c=(g^c)^s after two messages; the private key only signs; certificate encrypted with KK. Example p=23p=23, g=5g=5, c=6c=6, s=15s=15: K=2K=2. Forward secrecy: KK is ephemeral.
  • RTT. TLS 1.0 2 RTT, TLS 1.3 1 RTT; HTTPS first request at 100 ms: TCP 1 + TLS 1 + request 1 =0.3=0.3 s (0.4 s with TLS 1.0).
  • Handshake and record protocols; DTLS for UDP (PreSharedKey, RawPublicKey, Certificate; no multicast). IPsec: VPN and IPsecA VPN is a private network built over a public one: a VPN server admitted by the firewall authenticates outside users, and protected IP packets are encapsulated as the payload of other IP packets (IP tunnelling) using IPsec (AH, ESP, tunnel or transport mode, one-way Security Associations) or an SSL/TLS tunnel through TUN/TAP virtual interfaces.VPN and IPsec →.
  • Mistakes: wrong keys; RSA without padding or on whole files; ee not coprime with φ\varphi; m≥Nm\ge N; forgetting certificates; TLS 1.3 session key encrypted with public key; TLS vs DTLS.

Teoria collegata