CH2 FBC Openssl Lab
CH2 FBC Openssl Lab
MRN Tagore
Dept of CSE(CIC)
Secret-Key Encryption
Introduction
• Encryption is the process of encoding a message in such a way that
only authorized parties can read the content of the original message
• History of encryption dates back to 1900 BC
• Two types of encryption
• secret-key encryption : same key for encryption and decryption
• pubic-key encryption : different keys for encryption and decryption
• Result
Authenticated Encryption
• To protect the integrity, the sender needs to generate a Message
Authentication Code (MAC) from the ciphertext using a secret shared
by the sender and the receiver.
• The MAC and the ciphertext will be sent to the receiver, who will
compute a MAC from the received ciphertext.
• If the MAC is the same as the one received, the ciphertext is not
modified.
• Two operations are needed to achieve integrity of ciphertext: one for
encrypting data and other for generating MAC.
• Authenticated encryption combines these two separate operations
into one encryption mode. E.g GCM, CCM, OCB
The GCM Mode
Programming using the GCM Mode
The unique part of the above code is the tag
generation and verification.
In Line 3 , we use the digest() to get the
authentication tag, which is generated from
the ciphertext.
Programming using the GCM Mode
• Public-key infrastructure
• SSL/TLS protocol
Diffie-Hellman Key Exchange
• Allows communicating parties with no prior knowledge to exchange
shared secret keys over an insecure channel
• Alice and Bob want to communicate
• Alice and Bob agree on:
• Number p: big prime number (such as a 2048-bit number)
• Generator g: small prime number (such as 2 and 3)
• Alice picks a random positive integer x < p
• Bob picks a random positive integer y < p
Diffie-Hellman Key Exchange (Contd.)
Turn DH Key Exchange into a Public-Key
Encryption Algorithm
• DH key exchange protocol allows exchange of a secret
• Protocol can be tweaked to turn into a public-key encryption
scheme
• Need:
• Public key: known to the public and used for encryption
• Private key: known only to the owner, and used for decryption
• Algorithm for encryption and decryption
Turn DH Key Exchange into a Public-Key
Encryption Algorithm (Contd.)
RSA Algorithm
We will cover:
• Modulo Operation
• Euler’s Theorem
• Extended Euclidean Algorithm
• RSA Algorithm
• Algorithm example on small and large number
Modulo Operation
• The RSA algorithm is based on modulo operations
• a mod n is the remainder after division of a by the modulus n
• Second number is called modulus
• For example, (10 mod 3) equals to 1 and (15 mod 5) equals to 0
• Modulo operations are distributive:
Euler’s Theorem
• Euler’s totient function φ(n) counts the positive integers up to a
given integer n that are relatively prime to n
• φ(n) = n − 1, if n is a prime number.
• Euler’s totient function property:
• if m and n are relatively prime, φ(mn) = φ(m) ∗ φ(n)
• Euler’s theorem states:
• a φ(n) = 1 (mod n)
Euler’s Theorem (Contd.)
Example: to calculate 4 100003 mod 33
• φ(33) = φ(3) ∗ φ(11) = (3 − 1) ∗ (11 − 1) = 20
• 100003 = 5000φ(33) + 3
Extended Euclidean Algorithm
• Euclid’s algorithm: efficient method for computing GCD
• Extended Euclidean algorithm:
• computes GCD of integers a and b
• finds integers x and y, such that: ax + by = gcd(a, b)
• RSA uses extended Euclidean algorithm:
• e and n are components of public key
• Find solution to equation:
e ∗ x + φ(n) ∗ y = gcd(e, φ(n)) = 1
• x is private key (also referred as d)
• Equation results: e ∗ d mod φ(n) = 1
RSA Algorithm
We will cover:
• Key generation
• Encryption
• Decryption
RSA: Key Generation
• Need to generate: modulus n, public key exponent e, private key
exponent d
• Approach
• Choose p,q (large random prime numbers)
• n = pq (should be large)
• Choose e, 1 < e < φ(n) and e is relatively prime to φ(n)
• Find d, ed mod φ(n) = 1
• Result
• (e,n) is public key
• d is private key
RSA: Encryption and Decryption
• Encryption
• treat the plaintext as a number
• assuming M < n
• C = Me mod n
• Decryption
• M = Cd mod n
RSA Exercise: Small Numbers
• Choose two prime numbers p = 13 and q = 17
• Find e:
• n = pq = 221
• φ(n) = (p − 1)(q − 1) = 192
• choose e = 7 (7 is relatively prime to φ(n))
• Find d:
• ed = 1 mod φ(n)
• Solving the above equation is equivalent to: 7d + 192y = 1
• Using extended Euclidean algorithm, we get d = 55 and y = −2
RSA Exercise: Small Numbers (Contd.)
Encrypt M = 36
• Encryption
• Decryption
Paddings for RSA
• Secret-key encryption uses encryption modes to encrypt plaintext
longer than block size.
• RSA used in hybrid approach (Content key length << RSA key length)
• To encrypt:
• short plaintext: treat it a number, raise it to the power of e (modulo n)
• large plaintext: use hybrid approach (treat the content key as a number and
raise it to the power of e (modulo n)
• Treating plaintext as a number and directly applying RSA is called
plain RSA or textbook RSA
Attacks Against Textbook RSA
• RSA is deterministic encryption algorithm
• same plaintext encrypted using same public key gives same ciphertext
• secret-key encryption uses randomized IV to have different ciphertext for
same plaintext
• For small e and m
• if me < modulus n
• e-th root of ciphertext gives plaintext
• If same plaintext is encrypted e times or more using the same e but
different n, then it is easy to decrypt the original plaintext message
via the Chinese remainder theorem
Paddings: PKCS#1 v1.5 and OAEP
• Simple fix to defend against previous attacks is to add randomness
to the plaintext before encryption
• Approach is called padding
• Types of padding:
• PKCS#1 (up to version 1.5): weakness discovered since 1998
• Optimal Asymmetric Encryption Padding (OAEP): prevents attacks on PKCS
• Authentication
• HTTPS and TLS/SSL
• Chip Technology Used in Credit Cards
Applications: Authentication
• Typical way to conduct authentication is to use passwords
• Disadvantage:
• A sends password to B: B can get hacked and A may use same password for
multiple accounts
• cannot be used for many parties to authenticate a single party
• Usage examples:
• Detect change in system files
• Detect if file downloaded from website is corrupted
Committing a Secret Without Telling It
• One-way property
• Disclosing the hash does not disclose the original message
• Useful to commit secret without disclosing the secret itself
• Usage Example - Stock Market
• Need to make prediction about the stock market about a certain day
• Publish the hash of the secret on your website
• On the particular day, release the secret
• Your audience can verify it against the hash
Password Verification
• To login into account, user needs to tell a secret (password)
• Cannot store the secrets in their plaintext
• Need for:
• Password storage where nobody can know what the password is
• If provided with a password, it verified against the stored password
• Solution: one-way hash function
• Example: Linux stores passwords in the /etc/shadow file
Case Study: Linux Shadow File
• Password field has 3 parts: algorithm used, salt, password hash
• Salt and password hash are encoded into printable characters
• Multiple rounds of hash function (slow down brute-force attack)
Purpose of Salt
• Using salt, same input can result in different hashes
• Password hash = one-way hash rounds (password || random string)
• Random string is the salt
Attacks Prevented by Salt
• Dictionary Attack
• Put candidate words in a dictionary
• Try each against the targeted password hash to find a match
• Rainbow Table Attack
• Precomputed table for reversing cryptographic hash functions
• Why Salt Prevents them ?
• If target password is same as precomputed data, the hash will be the same
• If this property does not hold, all the precomputed data are useless
• Salt destroys that property
Trusted Timestamping
• Need: How to prove that a document existed prior to certain date ?
• Timestamping Approaches:
• Approach # 1: Publish one-way hash (instead of document) in a newspaper
or a magazine
• Approach # 2: Time Stamping Authority (TSA) can sign the document hash
using private key
• Approach # 3:
• Use Blockchain i.e. a growing list of record (blocks)
• Publish document hash in a block
• Blockchain depends on one-way hash
Message Authentication Code (MAC)
• Network communication can encounter MITM attacks
• MITM can intercept and modify data
• Receiver needs to verify integrity of data
• Attach tag to data
• Using one-way hash as tag won’t work because MITM can recompute hash
• Use a shared secret (key) between sender and receiver in the hash
• MITM cannot compute hash without secret key
Length Extension Attack on MAC
• Key and message need to be mixed properly before computing hash
• Simple concatenation (K || M) does not work
Keyed-Hash MAC (HMAC)
• Uses hash function H (compression function block size B) and a
secret key K
• ipad = 0x36 (B times), opad = 0x5c (B times)
• Can be used with any one-way hash function
Blockchain and Bitcoins
• Continuously growing list of records, called blocks
• Managed by ledgers in a peer-to-peer network
• Accepted ledger block is difficult to modify because it requires
alteration of all subsequent blocks
• Popular application is Bitcoin
• We will cover:
• Hash Chain and Blockchain
• Make Chaining Difficult
• Adding Incentives and Bitcoin
Hash Chain
• Successive application of a one-way hash function to a piece of data
• If a block gets modified, it will fall off from the chain, and will not be
considered as part of the chain
• If original data is changed, then the entire chain needs to be
regenerated
Blockchain
• Similar to hash chain, but has additional information in each block
• Bitcoin example: information about bitcoin transactions in blocks
• Create Chain: Hash value of a block is inside the next block
• If one block is modified:
• All the chains after this block are broken
• Require need to re-chain all the subsequent blocks
Blockchain: Make Chaining Difficult
• Nonce is added to each block
• Block hash must satisfy requirement (e.g. 20 leading zeros)
• Since computation power will increase over time, number of leading
zeros is intentionally increased over time
Blockchain Incentives and Bitcoin
• Provide bitcoins to anyone who can find nonce to chain blocks
• Companies / individuals who search for the nonce are “miners”
Hash Collision Attacks
• Popular one-way hash functions have trouble maintaining the
collision-resistance property
• We will cover:
• Security Impact of Collision Attacks
• Generating Two Different Files with the Same MD5 Hash
• Generating Two Programs with the Same MD5 Hash
Security Impact of Collision Attacks
• Forging public-key certificates
• Assume two certificate requests for www.example.com and
www.attacker.com have same hash due to collision
• CA signing of either request would be equivalent
• Attacker can get certificate signed for www.example.com without owning it
• Integrity of Programs
• Ask CA to sign a legitimate program’s hash
• Attacker creates a malicious program with same hash
• The certificate for legitimate program is also valid for malicious version
• These two examples are theoretical with questionable feasibility
Generating Two Different Files with the Same
MD5 Hash
• md5collgen tool generates two files with same prefix
Length Extension
• Generate two files with same prefix and same suffix
• Focus on MD5, SHA-1, SHA-2 using Merkle-Damgard construction
• If Hash (M) = Hash (N), then for any input T, Hash (M || T) = Hash (N || T)
Length Extension (Contd.)
Example using out1.bin and out2.bin generated by md5collgen
Generating Two Programs with the Same
MD5 Hash
Create two versions of program below with different value for xyz
Two Programs with Same Hash (Contd.)
• Program will be compiled into binary (fill xyz with fixed value)
• Portion of binary containing xyz will be divided into three parts
Two Programs with Same Hash (Contd.)
• Use md5collgen on prefix:
• generate two files with same hash
• last 128 bit of each generated file is P and Q
• MD5 (prefix || P) = MD5 (prefix || Q)
• MD5 (prefix || P || suffix) = MD5 (prefix || Q || suffix)
Two Programs with Same Hash (Contd.)
Summary
• One-way hash function is an essential building block in cryptography
• Important Properties: One-way and Collision resistant
• Applications
• Password authentication
• Trusted Timestamping
• Blockchain and Bitcoin
• MAC used to preserve integrity of communication
• One-way hash are subject to length extension and collision attacks