Salta al contenuto
Note per Studenti Hash Functions and Symmetric Cryptography Report

Hash Functions and Symmetric Cryptography

In questa pagina 5

Cryptography

Definition. Cryptography is the encryption and decryption of messages in secret code. It can provide confidentiality, integrity and authentication. A message (plaintext) is converted into ciphertext with a key, so that without the right key the inverse conversion is impossible.

C=Eke(P),P=Dkd(C).C=E_{k_e}(P),\qquad P=D_{k_d}(C).

Attacks on cryptosystems. Ciphertext only (the attacker analyses intercepted data); known plaintext (has plaintext and matching ciphertext); chosen plaintext (can make the source encrypt a message of his choice); brute force (tries many or all keys, helped by known weaknesses such as language regularities).

Property (exhaustive search). Trying all keys of kk bits takes on average 2k−12^{k-1} attempts: each extra bit doubles the work.

Example. At 101210^{12} keys per second, DES (2562^{56} keys) needs on average 255/1012=3.6⋅1042^{55}/10^{12}=3.6\cdot10^4 s, about 10 hours; AES-128 needs 2127/1012=1.7⋅10262^{127}/10^{12}=1.7\cdot10^{26} s, about 5.4⋅10185.4\cdot10^{18} years.

Two families. Asymmetric (public-key): different keys to encrypt and decrypt (Public-Key Cryptography, RSA and TLSPublic-key cryptography uses a public key to encrypt and a private key to decrypt; certificates issued by a Certificate Authority bind identities to public keys; digital signatures give authenticity, integrity and non-repudiation; RSA relies on the difficulty of factoring; TLS combines certificates, a symmetric session key (derived with Diffie-Hellman since TLS 1.3) and MACs.Public-Key Cryptography, RSA and TLS →). Symmetric (private-key): the same secret key, which needs a secure distribution mechanism.

Hash functions

Definition. A function mapping data of arbitrary size to data of fixed size; the values are called hash values, digests or hashes.

Example. f(x)=x mod 1000f(x)=x\bmod1000 is a hash function (10 bits): 1234→2341234\to234 and 2234→2342234\to234. It is not one-way: given h=234h=234 one finds all x=234+1000kx=234+1000k.

Definition (one-way hash function).

  • one-way: given hh it is hard to find mm with hash(m)=h\text{hash}(m)=h (the map is many-to-one, but finding any valid mm must be hard);
  • collision resistance: it is hard to find m1≠m2m_1\ne m_2 with hash(m1)=hash(m2)\text{hash}(m_1)=\text{hash}(m_2).

Application: fair odd/even game. I choose xx, you choose yy. 1) I tell you hash(x)\text{hash}(x); 2) you tell me yy; 3) I reveal xx and you check it against the hash; 4) if the sum is even I win. The hash commits me to xx. Risk: brute force, so xx must be large (e.g. 256 bits): if xx is in 1..1001..100 the receiver finds it immediately.

Popular hash functions.

  • MD series (Rivest): MD2, MD4 broken and obsolete; MD5 (128 bits) has had collision resistance broken since 2004, still used where only one-way is needed.
  • SHA series (NIST): SHA-0 and SHA-1 broken; SHA-2 (e.g. SHA-256: 256 bits = 32 bytes) never broken and the most used; SHA-3 is the newer standard with a different design.

Merkle-Damgård construction (MD5, SHA-1, SHA-2): the input is split into fixed-size blocks (the last is completed with padding); each block and the previous output enter a compression function (the first iteration uses an initial vector, IV); the last output is the hash.

Example (SHA-256). Blocks of 512 bits; padding adds a bit 1, zeros and the message length on 64 bits. A message of LL bits needs ⌈(L+1+64)/512⌉\lceil(L+1+64)/512\rceil blocks: abc (2424 bits) fits in ⌈89/512⌉=1\lceil89/512\rceil=1 block; a 1000-byte file (80008000 bits) needs ⌈8065/512⌉=16\lceil8065/512\rceil=16 blocks; the hash is always 256 bits.

Integrity. Changing a single bit changes the hash completely. Storing the hash (32 bytes for SHA-256) is like storing a secure duplicate; trusted sources publish the hash next to the file so that a download can be verified.

Example. SHA-256 of ciao starts with b133a0c0e9bee3be…, of ciap with 433a76b7fe6e8c73…: 135 bits out of 256 differ, about half (avalanche effect: the number of different bits is binomial with mean 128128 and standard deviation 88). Finding a preimage costs about 22562^{256} attempts; finding a collision about 21282^{128} (birthday paradox, below).

Passwords. Passwords cannot be stored in clear: the system stores their hash (Linux: /etc/shadow) and verifies a login by comparing the hash of the supplied password with the stored one.

Birthday paradox and collisions

With M=2bM=2^b possible hash values, the probability that nn values are all distinct is (1−1M)⋯(1−n−1M)\left(1-\frac1M\right)\cdots\left(1-\frac{n-1}M\right), so P(collision)≈1−e−n(n−1)/(2M)≈1−e−n2/(2M).P(\text{collision})\approx1-e^{-n(n-1)/(2M)}\approx1-e^{-n^2/(2M)}. Setting P=12P=\frac12 gives n≈2ln⁡2⋅M≈1.18Mn\approx\sqrt{2\ln2\cdot M}\approx1.18\sqrt M: the number of values needed is of the order of M\sqrt M.

case MM nn for P=50%P=50\%
birthdays 365365 2323 people
32-bit hash 2322^{32} about 77 00077\,000 messages
MD5 (128 bit) 21282^{128} about 2642^{64}
SHA-256 22562^{256} about 21282^{128}

With 2642^{64} attempts at 101210^{12} hashes per second, about 213213 days are needed: this is why 128 bits no longer suffice against collisions. A bb-bit hash offers security 2b/22^{b/2} against collisions and 2b2^b against preimages.

Symmetric cryptography

Definition. One shared secret key KK for both operations: C=EK(P)C=E_K(P), P=DK(C)P=D_K(C). It gives confidentiality, integrity and authentication between the nodes sharing the key.

It follows the open design principle: the algorithm is public, all secrecy lies in the key.

Historical ciphers

Caesar cipher. Shift by nn letters (A = 0, ..., Z = 25): En(x)=(x+n) mod 26,Dn(x)=(x−n) mod 26.E_n(x)=(x+n)\bmod26,\qquad D_n(x)=(x-n)\bmod26. Example. n=3n=3: ATTACCO →\to DWWDFFR. Only 25 keys: all can be tried. A monoalphabetic substitution has 26!≈4.03⋅1026≈28826!\approx4.03\cdot10^{26}\approx2^{88} keys but is broken by frequency analysis.

Vigenère. Several ciphers used in turn according to a key. Example. Key LEMON repeated: ATTACKATDAWN →\to LXFOPVEFRNHR (A+L=LA+L=L, T+E=XT+E=X, T+M=FT+M=F, ...); the same letter A becomes L, O, E, so simple frequency analysis fails.

Enigma. Developed from 1918 combining the ideas of Alberti and Vigenère; broken first by the Polish with the Bomba and then improved at Bletchley Park with Turing's contribution.

DES and AES

cipher block key status
DES 64 bit 56 bit (64 in input, 8 for error checking) broken by brute force (22 hours in 1999)
Triple DES 64 bit three DES costly, replaced by AES
AES 128 bit 128, 192, 256 bit current standard

Block cipher modes

Naively, equal plaintext blocks give equal ciphertext blocks. Modes of operation make at least one input different. Notation: PiP_i, CiC_i, EkE_k, C0=IVC_0=\text{IV} (initialisation vector).

  • ECB: Ci=Ek(Pi)C_i=E_k(P_i). Equal blocks give equal ciphertexts, the structure of the plaintext remains visible. Insecure.
  • CBC: Ci=Ek(Pi⊕Ci−1)C_i=E_k(P_i\oplus C_{i-1}). Equal blocks give different ciphertexts; the IV must be unpredictable and unique; sequential.
  • CFB: Ci=Pi⊕Ek(Ci−1)C_i=P_i\oplus E_k(C_{i-1}). Stream cipher, works on data of any length; an error propagates over several blocks.
  • OFB: Oi=Ek(Oi−1)O_i=E_k(O_{i-1}), O0=IVO_0=\text{IV}, Ci=Pi⊕OiC_i=P_i\oplus O_i. An error affects only the current block; the keystream can be precomputed.
  • CTR: Ci=Pi⊕Ek(IV+i−1)C_i=P_i\oplus E_k(\text{IV}+i-1). Parallelisable; an error affects only its block.

Toy example. E(x)=(5x+17) mod 256E(x)=(5x+17)\bmod256, plaintext blocks 10,10,10,2010,10,10,20, IV=7\text{IV}=7 (⊕\oplus is bit-wise XOR).

mode ciphertext blocks first block
ECB 67,67,67,11767,67,67,117 E(10)=67E(10)=67
CBC 82,201,224,21382,201,224,213 E(10⊕7)=E(13)=82E(10\oplus7)=E(13)=82
CFB 62,77,152,2962,77,152,29 10⊕E(7)=10⊕52=6210\oplus E(7)=10\oplus52=62
OFB 62,31,112,10362,31,112,103 10⊕E(7)=6210\oplus E(7)=62, then O2=E(52)=21O_2=E(52)=21
CTR 62,51,52,8762,51,52,87 10⊕E(7)=6210\oplus E(7)=62, then 10⊕E(8)=5110\oplus E(8)=51

In ECB the three equal blocks are visible; in the other modes all ciphertext blocks differ.

Key exchange and Diffie-Hellman

With nn people talking in pairs, symmetric cryptography needs n(n−1)2\frac{n(n-1)}2 keys (49504950 for n=100n=100), while asymmetric needs nn key pairs. The key can be sent with public-key cryptography, or built together:

Formula (Diffie-Hellman). Alice and Bob agree on a large prime pp (e.g. 2048 bits) and a generator gg (small). A picks x<px<p and sends L=gx mod pL=g^x\bmod p; B picks y<py<p and sends M=gy mod pM=g^y\bmod p. A computes K=Mx mod pK=M^x\bmod p, B computes K′=Ly mod pK'=L^y\bmod p, and K=K′=gxy mod pK=K'=g^{xy}\bmod p.

Example (p=23p=23, g=5g=5). x=6x=6: L=56 mod 23=8L=5^6\bmod23=8. y=15y=15: M=515 mod 23=19M=5^{15}\bmod23=19. Alice: K=196 mod 23=2K=19^6\bmod23=2. Bob: K′=815 mod 23=2K'=8^{15}\bmod23=2. Powers by repeated squaring, reducing at each step: 52≡25^2\equiv2, 54≡45^4\equiv4, 58≡165^8\equiv16, so 56≡4⋅2=85^6\equiv4\cdot2=8 and 515≡16⋅4⋅2⋅5=640≡195^{15}\equiv16\cdot4\cdot2\cdot5=640\equiv19.

A listener knows p,g,L,Mp,g,L,M but would need xx from 5x≡8(mod23)5^x\equiv8\pmod{23}: the discrete logarithm, with no known polynomial algorithm. Diffie-Hellman alone does not authenticate the parties: a signature or certificate is needed (Public-Key Cryptography, RSA and TLSPublic-key cryptography uses a public key to encrypt and a private key to decrypt; certificates issued by a Certificate Authority bind identities to public keys; digital signatures give authenticity, integrity and non-repudiation; RSA relies on the difficulty of factoring; TLS combines certificates, a symmetric session key (derived with Diffie-Hellman since TLS 1.3) and MACs.Public-Key Cryptography, RSA and TLS →).

aspect symmetric asymmetric
keys one shared key public and private pair
speed faster slower
key management shared key must be distributed securely public key distributed freely
cost low, for large amounts of data high, for handshakes

Authentication: MAC and HMAC

Definition (MAC). A Message Authentication Code uses a shared secret key: the sender computes a short fixed-length tag with a known function and the key, and appends it. The receiver recomputes the tag and compares: if equal, the message was not altered and the other party holds the key.

Formula (HMAC). With BB the hash block size (usually 64 bytes) and KK padded with zeros to BB: HMACK(m)=H((K⊕opad) ∥ H((K⊕ipad) ∥ m)),\text{HMAC}_K(m)=H\big((K\oplus\text{opad})\,\|\,H\big((K\oplus\text{ipad})\,\|\,m\big)\big), with ipad=0x36\text{ipad}=\texttt{0x36} and opad=0x5c\text{opad}=\texttt{0x5c} repeated BB times. Two hashes are used because with Merkle-Damgård hashes, knowing H(K ∥ m)H(K\,\|\,m) allows computing the hash of K ∥ m ∥ padding ∥ m′K\,\|\,m\,\|\,\text{padding}\,\|\,m' without KK (length extension); the outer hash prevents it.

Example. With K=K= chiave-segreta, HMAC-SHA256 of bonifico:100:IT60 is bc838925c97eca28… and of bonifico:900:IT60 is dadd0ebe877f0fc5…: without KK one cannot recompute the tag after changing 100 into 900; with plain SHA-256 one could.

aspect MAC digital signature
based on symmetric key (shared secret) asymmetric keys
key shared by sender and receiver private of sender, public to all
main purpose integrity + authentication integrity + authentication + non-repudiation

A MAC gives no non-repudiation: the receiver could have generated the same tag.

Common mistakes

  • Thinking a hash is encryption: it has no key and is not invertible (nor is base64).
  • Using ECB, or reusing an IV in CBC, OFB or CTR.
  • Thinking encryption also gives integrity: a MAC or signature is needed.
  • Saying MD5 and SHA-1 are secure.
  • Confusing the 56-bit DES key with the 64-bit input.
  • Believing Diffie-Hellman encrypts messages: it only establishes a shared key and does not authenticate.

Versione ripasso

  • Cryptography. C=Eke(P)C=E_{k_e}(P), P=Dkd(C)P=D_{k_d}(C); gives confidentiality, integrity, authentication. Attacks: ciphertext only, known plaintext, chosen plaintext, brute force.
  • Exhaustive search. 2k−12^{k-1} attempts on average; each bit doubles the work. At 101210^{12} keys/s: DES about 10 hours, AES-128 about 5.4⋅10185.4\cdot10^{18} years.
  • Hash. Arbitrary input, fixed-length digest. One-way: given hh, hard to find mm. Collision resistant: hard to find m1≠m2m_1\ne m_2 with equal hash. x mod 1000x\bmod1000 is a hash but not one-way.
  • Odd/even game. Send hash(x)(x) first, receive yy, reveal xx; xx must be large against brute force.
  • Families. MD5 (128 bit) collisions broken in 2004; MD2, MD4 obsolete; SHA-0 and SHA-1 broken; SHA-2 (SHA-256 = 32 bytes) most used; SHA-3 standard.
  • Merkle-Damgård. Fixed-size blocks with padding, compression function, IV, last output is the hash. SHA-256: 512-bit blocks, padding adds 1, zeros, 64-bit length; abc = 1 block, 1000 bytes = ⌈8065/512⌉=16\lceil8065/512\rceil=16 blocks.
  • Integrity and passwords. One bit changed changes the whole hash (ciao vs ciap: 135 of 256 bits differ). Preimage ≈2256\approx2^{256}, collision ≈2128\approx2^{128}. Passwords stored as hashes (/etc/shadow).
  • Birthday. P≈1−e−n2/(2M)P\approx1-e^{-n^2/(2M)}, n≈1.18Mn\approx1.18\sqrt M: 23 people for 365 days; 2642^{64} for MD5; 21282^{128} for SHA-256. bb-bit hash: 2b/22^{b/2} against collisions, 2b2^b against preimages.
  • Symmetric. One shared key, open design. Caesar En(x)=(x+n) mod 26E_n(x)=(x+n)\bmod26: ATTACCO →\to DWWDFFR, 25 keys. Monoalphabetic: 26!≈28826!\approx2^{88} keys, broken by frequency analysis. Vigenère with LEMON: ATTACKATDAWN →\to LXFOPVEFRNHR. Enigma: broken with the Bomba and at Bletchley Park.
  • DES 64-bit block, 56-bit key, broken in 22 h (1999); Triple DES replaced by AES: 128-bit block, keys 128/192/256.
  • Modes. ECB Ci=Ek(Pi)C_i=E_k(P_i) insecure; CBC Ci=Ek(Pi⊕Ci−1)C_i=E_k(P_i\oplus C_{i-1}); CFB Ci=Pi⊕Ek(Ci−1)C_i=P_i\oplus E_k(C_{i-1}); OFB Oi=Ek(Oi−1)O_i=E_k(O_{i-1}), Ci=Pi⊕OiC_i=P_i\oplus O_i; CTR Ci=Pi⊕Ek(IV+i−1)C_i=P_i\oplus E_k(\text{IV}+i-1), parallelisable. Toy cipher E(x)=(5x+17) mod 256E(x)=(5x+17)\bmod256, blocks 10,10,10,2010,10,10,20, IV =7=7: ECB 67,67,67,11767,67,67,117; CBC 82,201,224,21382,201,224,213; CFB 62,77,152,2962,77,152,29; OFB 62,31,112,10362,31,112,103; CTR 62,51,52,8762,51,52,87.
  • Keys. n(n−1)2\frac{n(n-1)}2 symmetric keys (49504950 for n=100n=100) against nn pairs.
  • Diffie-Hellman. L=gx mod pL=g^x\bmod p, M=gy mod pM=g^y\bmod p, K=gxy mod pK=g^{xy}\bmod p. p=23p=23, g=5g=5, x=6x=6, y=15y=15: L=8L=8, M=19M=19, K=2K=2. Security: discrete logarithm. No authentication.
  • MAC. Tag from a shared key; receiver recomputes and compares. HMAC =H((K⊕opad)∥H((K⊕ipad)∥m))=H((K\oplus\text{opad})\|H((K\oplus\text{ipad})\|m)), ipad=0x36\text{ipad}=\texttt{0x36}, opad=0x5c\text{opad}=\texttt{0x5c}; the two hashes prevent length extension. MAC: no non-repudiation; signature: yes.
  • Mistakes: hash is not encryption; ECB; IV reuse; encryption does not give integrity; MD5/SHA-1 secure; DES 56 vs 64 bits; Diffie-Hellman encrypts.

Teoria collegata