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 the public and the private key of B: 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 .
Example. people need key pairs, against 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
, . Examples: , , , . One can reduce before operating: (same for sums), so numbers never grow. Example with : ; .
Square-and-multiply. Write the exponent in binary, compute successive squares (reduced modulo ) and multiply only those matching the digits. needs 5 multiplications instead of 16; a -bit exponent needs at most .
Definition (modular inverse). is the inverse of modulo if . It exists if . Modular division: .
Extended Euclid finds the inverse. Inverse of modulo : ; ; (gcd ). Going back: , so . Other examples: ; ; .
Definition (discrete logarithm). If , then . No polynomial-time algorithm is known. Example: , so .
RSA
RSA (Rivest, Shamir, Adleman, 1978) relies on the difficulty of factoring large numbers.
Formula (RSA key generation).
- Choose two large primes and .
- .
- .
- Choose with .
- , so .
Public key ; secret key ; the secrets are never shared.
Formula (encryption and decryption). For : , .
Why . Integers in not coprime with are multiples of ( of them) or of ( of them), none of both. The coprime ones are .
Why it works (Euler's theorem). For coprime with , (multiplying the integers coprime with by merely permutes them, so the product is unchanged and can be divided out). Since : The result also holds when shares a factor with (use Fermat's theorem modulo and separately).
Complete example. , : , , , (). Message : , , , , so . Decryption: .
Security. An attacker who factors gets the secret key. and are random, prime, of similar size (1500 to 3000 bits), and 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 ; it defends against adaptive chosen-ciphertext attacks and gives semantic security.
Authentication with RSA
The server sends with (using the secret key); the client computes with the public key and accepts if it equals .
Example. : ; the client computes : valid. If arrives as the check fails. Signing a hash gives and .
Attacks on plain RSA
Malleability. Mallory intercepts , picks , sends ; if Bob returns the decrypted , Mallory computes . Example (, , , ): , decrypted , and .
Determinism. Without padding, equal messages always give equal ciphertexts. Countermeasure: with random padding; Bob removes it and returns , so the attack fails.
Signature attack. The attacker asks the server to sign and computes . Example (, ): , , . 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.
- The client asks for a protected resource.
- The server answers with its certificate (public key and signature).
- The client checks that it is valid and trusted (issued by a CA, not expired, valid signature).
- The client generates a symmetric session key and encrypts it with the server's public key.
- The server decrypts it with its private key.
- 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 ===========================================>|exists after the first two messages; the certificate (5) is encrypted with ; the signature covers messages (1)+(3); the random nonces guarantee uniqueness; 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 .
Example (, ). Client sends ; server sends . Client: ; server: . In real TLS has at least 2048 bits (or elliptic curves). Since 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 ms: TCP 1 + TLS 1.3 1 + request 1 RTT s; with TLS 1.0, RTT 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
- Wrong key: confidentiality = recipient's public key; signature = sender's private key.
- Using RSA without padding, or thinking RSA encrypts a whole file: a session key is encrypted and data go in symmetric mode.
- Choosing not coprime with , or forgetting .
- Forgetting that certificates bind identity to public key: without them a man in the middle can offer his own key (Man-in-the-Middle Attack with ARP PoisoningLab 7 shows that Telnet sends user name and password in clear, so with a hub a tcpdump on the attacker is enough, while with a switch the attacker needs ARP poisoning (false ARP replies repeated every second) and IP forwarding to sit in the middle and read the session in Wireshark with Follow TCP stream.Man-in-the-Middle Attack with ARP Poisoning →).
- Thinking that in TLS 1.3 the session key is encrypted with the server's public key.
- Confusing TLS (TCP) with DTLS (UDP).
Versione ripasso
- Asymmetric. , ; the private key cannot be derived from the public one. people: 100 pairs against 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 encrypted with the sender's private key; the receiver recomputes , decrypts with the public key and compares. Gives authenticity, integrity, non-repudiation.
- Modular arithmetic. , . Inverse exists if : , . Extended Euclid: . Square-and-multiply: at most multiplications for a -bit exponent. Discrete log: .
- RSA keys. primes, , , , . , .
- RSA operation. , , . Euler: , so .
- Example. , , , , , ; .
- Security. Factoring breaks RSA; bits; random and of similar size. Padding mandatory (PKCS#1 v1.5:
00 02 [random] 00 [m]). - RSA signature. , check . : . Sign the hash with padding, not the bare message.
- Attacks on plain RSA. Malleability: , recover (, decrypted ). Deterministic encryption. Signature forgery: (, ).
- 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 after two messages; the private key only signs; certificate encrypted with . Example , , , : . Forward secrecy: 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 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; not coprime with ; ; forgetting certificates; TLS 1.3 session key encrypted with public key; TLS vs DTLS.