<<

ì Security COMP 178 | Spring 2021 | University of the Pacific | Jeff Shafer

Cryptography: Stream Ciphers, Nonce and IVs 2

ì Stream Ciphers

Computer Network Security Spring 2021 3 Stream Ciphers

ì Have: A stream of bits ì Image, video, webpage, email, …

ì Want: A cipher that can take an unlimited (or at least very long) stream of plaintext bits and encrypt

ì Idea: Divide incoming stream into blocks and encrypt each separately via existing block cipher ì Called Block Cipher Mode of Operation ì First attempt: Electronic Code Book (ECB) mode ì Note: These are not AES-specific – Modes of Operation work for any block cipher

Computer Network Security Spring 2021 4 Electronic Code Book Mode (ECB)

Computer Network Security Spring 2021 5 Electronic Code Book (ECB) Mode

Computer Network Security Spring 2021 6 Electronic Code Book Mode (ECB)

ì Electronic Code Book Mode (ECB) – Don’t use!

ì Two big problems ì Same input block produces the same cipher block each time ì Replay attacks

Computer Network Security Spring 2021 7 Identical Blocks

Computer Network Security Spring 2021 8

#!/usr/bin/python3 # Jeff Shafer, University of the Pacific # Demo program illustrating information leakage # of block ciphers (e.g. AES) in ECB mode

# Requires Python3 and PyCrypto # https://www.pycrypto.org from Crypto.Cipher import AES from hashlib import md5

# Example image, 1418x779 pixels, 8 bit color depth # AES default block size of 128 bits will take this image # 15 pixels at a time file = open("pacific.bmp", "rb") plaintext_original = file.read() + b'000000' # Pad length to multiple of 16. BMP files don't care. ##print(len(plaintext_original))

# Generate a for AES /decryption # AES-128 key length is 16 bytes (128 bits) key = md5("bogus garbage".encode('ascii')).hexdigest()

# Encrypt with AES in ECB mode cipher = AES.new(key, AES.MODE_ECB) ciphertext = cipher.encrypt(plaintext_original)

# "Cheat" for demo purposes - In order to view the ciphertext as a bitmap image, # we copy the bitmap header bytes (specifying dimensions, color depth, etc...) # from the unencrypted image and append the ciphertext after that. # Use the bless hex editor, look at offset 0xA, and that byte will # tell you where the actual image data starts after the header. fake_ciphertext = plaintext_original[0:121] + ciphertext[122:] file2 = open("pacific_encrypted.bmp", "wb") file2.write(fake_ciphertext)

# Decrypt plaintext_final = cipher.decrypt(ciphertext) plaintext_final = plaintext_final[:-6] # Cut off padding applied earlier file3 = open("pacific_decrypted.bmp", "wb") file3.write(plaintext_final) Computer Network Security Spring 2021 9

Original Decrypted

Ciphertext

Computer Network Security Spring 2021 10

https://www.zerodayclothing.com/

Computer Network Security Spring 2021 11 Replay Attack

� What’s the password for � the exclusive party? � � ✓ � � (alice) miecznik (bob) � � � Miecznik = � Polish word � for swordfish � � � What’s the password for � the exclusive party? � � � ✓ � miecznik (bob) (eve) �

Computer Network Security Spring 2021 12

Sneakers (1992) “My voice is my passport, verify”

Computer Network Security Spring 2021 13 Block Cipher Modes of Operation

ì Electronic Code Book Mode (ECB) – Don’t use! ì Cipher Block Chaining (CBC) – Good but inefficient ì Propagating Cipher Block Chaining (PCBC) ì Ciphertext Stealing (CTS) ì Cipher Feedback (CFB) ì Output Feedback (OFB) ì Counter (CTR) - Good ì … and more options that add to the confidentiality already provided (will cover later) ì CCM, GCM, CWC, EAX, IAPM, OCB….

Computer Network Security Spring 2021 14

ì And this is one place where people make crypto mistakes ì You don’t just pick AES, you pick AES+EBC, AES+CBC, etc…. L

Computer Network Security Spring 2021 15 Cipher Block Chaining (CBC) Mode

ì Plaintext blocks are XORed with previous ciphertext block before being encrypted

ì First block is XORed with Initialization Vector (IV) – length of 1 block ì Must be cryptographically random! (predictability here was cause of BEAST SSL/TLS attack) ì Is not secret – typically prepended to ciphertext in plain text

Computer Network Security Spring 2021 16 Cipher Block Chaining (CBC) Mode

Computer Network Security Spring 2021 17 Counter (CTR) Mode

ì Developed by Whitfield Diffie and Martin Hellman, ì Operaon 1979 ì “Nonce” = IV (cryptographically random) ì “Counter” is any sequence guaranteed not to ì Encrypt a {nonce, counter} value, then XOR with repeat for long me (like a counter!) plaintext to yield ciphertext ì Combine Nonce with Counter via concatenaon (upper and lower 64 bits) ì The encrypted {nonce, counter} are like a pseudo- OTP!

Computer Network Security Spring 2021 18 Counter (CTR) Mode

Computer Network Security Spring 2021 19

Original Decrypted Ciphertext – AES ECB Mode Histogram

Ciphertext – AES CTR Mode Histogram

Computer Network Security Spring 2021 20 Block Cipher w/Padding

ì Q: What if the plaintext size isn’t a multiple of the block size?

ì A: Need padding at end of plaintext data

ì Q: How do I distinguish my padding from the original plaintext? (for arbitrary plaintext)

ì A: PKCS#5 / PKCS#7 padding standard

Computer Network Security Spring 2021 21 PKCS#7 Padding

ì Padding is in whole bytes. Value of each added byte is number of bytes that are added

Need to pad by 1 byte? 01 02 02 03 03 03 04 04 04 04 05 05 05 05 05 Need to pad by 6 bytes? 06 06 06 06 06 06 ... ì Note: Always pad ì Even if plaintext is multiple of block size, in which case an entire block is added ì AES block size: 128 bits (16 bytes), e.g 10 (hex)

Computer Network Security Spring 2021 22 (Native) Stream Ciphers

ì What about designing a cipher that doesn’t work with blocks at all? ì Native Stream Cipher

ì Categories ì Synchronous stream ciphers ì Self-Synchronizing stream ciphers - rare

Computer Network Security Spring 2021 23 Synchronous Stream Ciphers

ì Like a one-time pad, but with a pseudo-random pad generated by the cipher

(key) K C K C (pseudo- (pseudo- random random ki ki pad) pad) (plaintext) (encrypted) (plaintext) � Pi ⊕ Ci ⊕ Pi � (alice) (bob) � Computer Network Security (eve) Spring 2021 24 Native Stream Cipher Examples

ì Rivest Cipher 4 (RC4) – Don’t use! ì Designed by Ron Rivest (R in RSA) ì Simple and fast in software and hardware J ì Used in popular protocols like SSL, TLS, WEP, WPA J ì Insecure L ì Break WPA-TKIP w/RC4 in under an hour ì Break TLS-protected HTTP cookie in 75 hours ì Prohibited in TLS in 2016+ (dropped by Chrome, Firefox, IE/Edge) ì Shouldn’t be using WEP any more

Research paper Mathy Vanhoef and Frank Piessens. “All your biases belong to us: breaking RC4 in WPA- TKIP and TLS.” In Proceedings of the 24th USENIX Conference on Security Symposium (SEC'15), Jaeyeon Jung (Ed.). USENIX Association, Berkeley, CA, USA, 2015

Computer Network Security Spring 2021 25 Native Stream Cipher Examples

ì Salsa20 and ChaCha20 ì Developed by DJB (NaCL author) ì Secure (that we know…) ì Useful features? ì Jump to arbitrary location in bitstream and begin decryption (no need to decrypt from beginning) ì Sections of ciphertext can be decrypted in parallel ì Resistant to side channel attacks (all operations are constant time) ì Competitive performance to AES, even without being hardware accelerated

Computer Network Security Spring 2021 26

ì Nonce

Computer Network Security Spring 2021 27 Nonce

ì Nonce = “Number used ONCE” ì Random or pseudo-random

ì Common applications ì Initialization Vector ì (prevent replay attacks) ì Cryptographic hash / proof-of-work systems

Computer Network Security Spring 2021 28 Nonce / Initialization Vector

ì Example 1: Initialization Vector (IV) - Fixed input to crypto function that is required to be random or non-repeating (depends on mode of operation)

“nonce”

Computer Network Security Spring 2021 29 Nonce / Authentication Protocol

ì Example 2: Authentication Protocol ì Goal: prevent replay attacks ì Example: HTTP Digest Auth – Communicate login password to server without encryption plus prevent replay attacks

Computer Network Security Spring 2021 30 Nonce / Authentication Protocol

ì HTTP Digest Auth

HTTP Client � � HTTP Server 1. Client requests protected resource Request 2. Server creates random nonce and Nonce sends it to client with 3. Client enters user/pw challenge to authenticate 4. Client creates client (nonce only good once) nonce and hash and Login: Username, client-nonce, sends both to server Hash(nonce, cnonce, password) 5. Server validates hash by creating its own hash to see if it Token ✓ matches

Computer Network Security Spring 2021 31 Nonce / Proof-of-Work System

ì Example 3: Cryptographic hash / proof-of-work systems (e.g. Blockchain!) ì Blockchain is database shared by all network users that stores transaction history ì Transaction not recognized until it is added to blockchain

Computer Network Security Spring 2021 32 Nonce / Proof-of-Work System

ì Example 3: Cryptographic hash / proof-of-work systems (e.g. Blockchain!) ì Must vary nonce (trial and error) until a desirable cryptographic hash is produced

Find a nonce x such that SHA-256(SHA-256(r+x)) < T/d r = header + transactions (Simplified) H(B0) Nonce H(B1) Nonce H(B1) Nonce B0 Transactions Transactions Transactions

Computer Network Security Spring 2021