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.
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 bits takes on average attempts: each extra bit doubles the work.
Example. At keys per second, DES ( keys) needs on average s, about 10 hours; AES-128 needs s, about 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. is a hash function (10 bits): and . It is not one-way: given one finds all .
Definition (one-way hash function).
- one-way: given it is hard to find with (the map is many-to-one, but finding any valid must be hard);
- collision resistance: it is hard to find with .
Application: fair odd/even game. I choose , you choose . 1) I tell you ; 2) you tell me ; 3) I reveal and you check it against the hash; 4) if the sum is even I win. The hash commits me to . Risk: brute force, so must be large (e.g. 256 bits): if is in 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 bits needs blocks: abc ( bits) fits in block; a 1000-byte file ( bits) needs 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 and standard deviation ). Finding a preimage costs about attempts; finding a collision about (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 possible hash values, the probability that values are all distinct is , so Setting gives : the number of values needed is of the order of .
| case | for | |
|---|---|---|
| birthdays | people | |
| 32-bit hash | about messages | |
| MD5 (128 bit) | about | |
| SHA-256 | about |
With attempts at hashes per second, about days are needed: this is why 128 bits no longer suffice against collisions. A -bit hash offers security against collisions and against preimages.
Symmetric cryptography
Definition. One shared secret key for both operations: , . 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 letters (A = 0, ..., Z = 25):
Example. : ATTACCO DWWDFFR. Only 25 keys: all can be tried. A monoalphabetic substitution has keys but is broken by frequency analysis.
Vigenère. Several ciphers used in turn according to a key. Example. Key LEMON repeated: ATTACKATDAWN LXFOPVEFRNHR (, , , ...); 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: , , , (initialisation vector).
- ECB: . Equal blocks give equal ciphertexts, the structure of the plaintext remains visible. Insecure.
- CBC: . Equal blocks give different ciphertexts; the IV must be unpredictable and unique; sequential.
- CFB: . Stream cipher, works on data of any length; an error propagates over several blocks.
- OFB: , , . An error affects only the current block; the keystream can be precomputed.
- CTR: . Parallelisable; an error affects only its block.
Toy example. , plaintext blocks , ( is bit-wise XOR).
| mode | ciphertext blocks | first block |
|---|---|---|
| ECB | ||
| CBC | ||
| CFB | ||
| OFB | , then | |
| CTR | , then |
In ECB the three equal blocks are visible; in the other modes all ciphertext blocks differ.
Key exchange and Diffie-Hellman
With people talking in pairs, symmetric cryptography needs keys ( for ), while asymmetric needs 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 (e.g. 2048 bits) and a generator (small). A picks and sends ; B picks and sends . A computes , B computes , and .
Example (, ). : . : . Alice: . Bob: . Powers by repeated squaring, reducing at each step: , , , so and .
A listener knows but would need from : 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 the hash block size (usually 64 bytes) and padded with zeros to : with and repeated times. Two hashes are used because with Merkle-Damgård hashes, knowing allows computing the hash of without (length extension); the outer hash prevents it.
Example. With chiave-segreta, HMAC-SHA256 of bonifico:100:IT60 is bc838925c97eca28… and of bonifico:900:IT60 is dadd0ebe877f0fc5…: without 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. , ; gives confidentiality, integrity, authentication. Attacks: ciphertext only, known plaintext, chosen plaintext, brute force.
- Exhaustive search. attempts on average; each bit doubles the work. At keys/s: DES about 10 hours, AES-128 about years.
- Hash. Arbitrary input, fixed-length digest. One-way: given , hard to find . Collision resistant: hard to find with equal hash. is a hash but not one-way.
- Odd/even game. Send hash first, receive , reveal ; 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 = blocks. - Integrity and passwords. One bit changed changes the whole hash (
ciaovsciap: 135 of 256 bits differ). Preimage , collision . Passwords stored as hashes (/etc/shadow). - Birthday. , : 23 people for 365 days; for MD5; for SHA-256. -bit hash: against collisions, against preimages.
- Symmetric. One shared key, open design. Caesar :
ATTACCODWWDFFR, 25 keys. Monoalphabetic: keys, broken by frequency analysis. Vigenère withLEMON:ATTACKATDAWNLXFOPVEFRNHR. 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 insecure; CBC ; CFB ; OFB , ; CTR , parallelisable. Toy cipher , blocks , IV : ECB ; CBC ; CFB ; OFB ; CTR .
- Keys. symmetric keys ( for ) against pairs.
- Diffie-Hellman. , , . , , , : , , . Security: discrete logarithm. No authentication.
- MAC. Tag from a shared key; receiver recomputes and compares. HMAC , , ; 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.