Stream Cipher Cryptography Guide
News Home

📚 Stream Cipher Theory

What are Stream Ciphers?

Stream ciphers are symmetric encryption algorithms that encrypt plaintext one bit or byte at a time using a pseudorandom keystream. Unlike block ciphers that operate on fixed-size blocks, stream ciphers generate a continuous stream of key material that's XORed with the plaintext.

🔑 Key Components

  • Keystream Generator: Produces pseudorandom bits
  • Key & IV: Secret key and initialization vector
  • XOR Operation: Combines plaintext with keystream
  • Synchronous/Asynchronous: Clock-controlled or self-synchronizing

📐 Stream Cipher Mathematical Foundation

Encryption: Ci = Pi ⊕ Ki
Decryption: Pi = Ci ⊕ Ki
Where K is the keystream generated from key and IV
Keystream Generation
1. Key Schedule: Expand key + IV into initial state
2. State Update: Apply mixing function f: Si+1 = f(Si)
3. Output Function: Ki = g(Si)
4. Periodicity: State must have long cycle length
Security Requirements
  • Long Period: 2n where n is state size
  • Linear Complexity: ≈ n/2 (ideal for LFSR-based)
  • Statistical Properties: Passes all randomness tests
  • Key Sensitivity: 1-bit change → 50% output difference

📱 A5/1 Stream Cipher (GSM)

Overview

A5/1 is a stream cipher used to encrypt GSM cellular communications. It was developed in 1987 and uses three linear feedback shift registers (LFSRs) of different lengths combined with irregular clocking for enhanced security.

📋 A5/1 Specifications

  • Key Length: 64 bits (54 effective)
  • Frame Size: 114 bits
  • LFSRs: 19-bit, 22-bit, 23-bit
  • Clocking: Majority rule based

📐 A5/1 Mathematical Structure

LFSR Specifications
R1 (19-bit LFSR)
Length: 19 bits
Feedback: x19 + x18 + x17 + x14 + 1
Taps: positions 18, 17, 16, 13
Output: bit 18
R2 (22-bit LFSR)
Length: 22 bits
Feedback: x22 + x21 + 1
Taps: positions 21, 20
Output: bit 21
R3 (23-bit LFSR)
Length: 23 bits
Feedback: x23 + x22 + x21 + x8 + 1
Taps: positions 22, 21, 20, 7
Output: bit 22
Clocking Mechanism
Majority = floor((R18 + R210 + R310) / 2)
if R18 = Majority: Clock R1
if R210 = Majority: Clock R2
if R310 = Majority: Clock R3
Keystream bit = R118 ⊕ R221 ⊕ R322
Mathematical Properties
  • State Space: 219+22+23 = 264 ≈ 1.8 × 1019 states
  • Period: LCM of individual LFSR periods
  • Linear Complexity: Complex due to irregular clocking
  • Security: Vulnerable to time-memory tradeoffs

🔷 E0 Stream Cipher (Bluetooth)

Overview

E0 is the encryption algorithm used in Bluetooth systems for securing wireless communications between devices. It uses a combination of linear feedback shift registers and finite state machines to generate the keystream.

📋 E0 Specifications

  • Key Length: Up to 128 bits
  • Architecture: Four LFSRs + FSM
  • Frame: Variable length
  • Standard: Bluetooth BR/EDR

🎬 E0 Key Exchange Simulation

Device A

Master Key: 0x1234ABCD

Address: 00:11:22:33:44:55

↔️

Device B

Master Key: 0x5678EFGH

Address: 66:77:88:99:AA:BB

0
1
0
1

🌪️ ChaCha20 (TLS 1.3)

Overview

ChaCha20 is a modern stream cipher designed by Daniel J. Bernstein. It's used in TLS 1.3 as an alternative to AES, particularly on systems where AES hardware acceleration isn't available. ChaCha20 is based on the ChaCha permutation and provides excellent performance and security.

📋 ChaCha20 Specifications

  • Key Length: 256 bits
  • Nonce: 96 bits
  • Block Size: 512 bits
  • Rounds: 20 (ChaCha20)
  • Security: Post-quantum resistant design

📐 ChaCha20 Mathematical Operations

State Matrix (4×4 array of 32-bit words)
σ0 σ1 σ2 σ3
K0 K1 K2 K3
K4 K5 K6 K7
C0 C1 N0 N1
Where σ = "expand 32-byte k", C = 32-bit counter, N = 96-bit nonce
Quarter Round Function
QR(a, b, c, d):
a += b; d ⊕= a; d ≪= 16;
c += d; b ⊕= c; b ≪= 12;
a += b; d ⊕= a; d ≪= 8;
c += d; b ⊕= c; b ≪= 7;
Column Round: QR(0,4,8,12), QR(1,5,9,13), QR(2,6,10,14), QR(3,7,11,15)
Diagonal Round: QR(0,5,10,15), QR(1,6,11,12), QR(2,7,8,13), QR(3,4,9,14)
ChaCha20 Algorithm
1. Initialize 16-word state matrix
2. Copy state to working buffer
3. for round = 1 to 10:
Apply column round to working buffer
Apply diagonal round to working buffer
4. Add working buffer back to state
5. Serialize state to keystream

🔄 RC4 (WEP/TLS/SSL)

Overview

RC4 (Rivest Cipher 4) is one of the most widely used stream ciphers, particularly known for its implementation in WEP for wireless security and early versions of TLS/SSL. Despite its speed and simplicity, RC4 has several cryptographic weaknesses that have led to its deprecation.

⚠️ RC4 Vulnerabilities

  • Bias Attacks: Key scheduling weaknesses
  • Fluhrer-Mantin-Shamir: Distinguishes RC4 from random
  • Key Recovery: Related key attacks possible
  • Status: Deprecated for new systems

📐 RC4 Mathematical Algorithm

Key Scheduling Algorithm (KSA)
1. S[0..255] = [0, 1, 2, ..., 255]
2. j = 0
3. for i = 0 to 255:
j = (j + S[i] + key[i mod keylen]) mod 256
swap(S[i], S[j])
Pseudo-Random Generation Algorithm (PRGA)
1. i = 0, j = 0
2. while generating output:
i = (i + 1) mod 256
j = (j + S[i]) mod 256
swap(S[i], S[j])
output = S[(S[i] + S[j]) mod 256]
Mathematical Properties
Key Length: Variable (typically 40-256 bits)
State Size: 256 bytes (2048 bits)
Period: ≈ 22048 (theoretically)
Output: S[(S[i] + S[j]) mod 256]
Vulnerability: Key scheduling biases in first 256 bytes
RC4 Encryption/Decryption
Ci = Pi ⊕ Ki
Pi = Ci ⊕ Ki
Where K is the keystream from PRGA