Skip to content

About

A from-scratch 4-bit LFSR stream cipher and file encryptor in C, with no crypto libraries.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lfsr-cipher

A stream cipher and file encryptor written in C from scratch — no crypto libraries. The keystream is generated by a 4-bit Linear-Feedback Shift Register (LFSR) with feedback taps on the 3rd and 4th flip-flops.

Educational project. This explores bitwise operations, low-level memory management, and cipher architecture in C. A 4-bit LFSR keystream repeats every 15 bits and is trivially breakable — never use it to protect real data.

Build

make        # builds build/lfsr-cipher
make test   # encrypt + decrypt round-trip smoke test

Usage

lfsr-cipher encrypt <key 1-15> input.txt cipher.bin
lfsr-cipher decrypt <key 1-15> cipher.bin output.txt

Encryption prints the ciphertext payload in hex with leading zeros stripped.

The mathematics behind the LFSR

A Linear-Feedback Shift Register turns a tiny amount of state into a long, deterministic, pseudorandom bit sequence. This implementation is a 4-bit Fibonacci LFSR with feedback taps on the 3rd and 4th flip-flops, which realizes the primitive polynomial x⁴ + x³ + 1 over GF(2).

Register structure

The register is four 1-bit flip-flops, packed into the low nibble of a uint8_t. Label them Q4 Q3 Q2 Q1, from most- to least-significant bit. On each clock tick the bits shift one position left: the bit in Q4 falls off the end and becomes the output (keystream) bit, every other bit moves up one slot, and a freshly computed feedback bit is shifted in at Q1.

        MSB                              LSB
      +------+   +------+   +------+   +------+
 out <-|  Q4  |<-|  Q3  |<-|  Q2  |<-|  Q1  |<-- feedback in
      +------+   +------+   +------+   +------+
          |          |                    ^
          |          |                    |
          +--> XOR <-+                    |
                 |                         |
                 +-------------------------+
               feedback = Q4 XOR Q3

The two "top" bits — Q4 and Q3, the 3rd and 4th flip-flops — are tapped, XORed together, and the result is fed back into the bottom (Q1). In code this is a left shift, a mask to keep 4 bits, and an OR to insert the feedback bit:

uint8_t bit4 = (state >> 3) & 1;        /* Q4 */
uint8_t bit3 = (state >> 2) & 1;        /* Q3 */
uint8_t feedback = bit3 ^ bit4;
state = ((state << 1) | feedback) & 0x0F;

Eight clock ticks are then packed MSB-first into one keystream byte (lfsr_next_byte).

The feedback function

The feedback is a single XOR: feedback = Q3 ⊕ Q4. XOR is precisely addition modulo 2 — 0⊕0=0, 0⊕1=1, 1⊕1=0 — so each new bit is a linear combination of the current bits over the two-element field GF(2). Because the feedback rule (and therefore the whole state-transition function) is linear, the entire machine is a linear map. That linearity is the "L" in LFSR, and it's what lets us predict the period with polynomial algebra instead of brute force.

Characteristic polynomial

Taps on the 3rd and 4th flip-flops correspond to the polynomial

x⁴ + x³ + 1

over GF(2). Clocking the register is equivalent to multiplying the current state by x modulo this polynomial. Crucially, x⁴ + x³ + 1 is primitive over GF(2) (one of only two primitive degree-4 polynomials, the other being x⁴ + x + 1). Primitivity is what buys the guarantee: when the feedback polynomial is primitive, the register is mathematically guaranteed to cycle through every non-zero state before repeating — the maximum possible period. A non-primitive tap choice such as x⁴ + x² + 1 would collapse into a short cycle and produce a much weaker keystream.

Period

Four bits give 2⁴ = 16 possible states. The all-zero state is a dead end (see below), so the longest achievable cycle is 2⁴ − 1 = 15. Because our polynomial is primitive, we hit that maximum — every non-zero state appears exactly once per period. Starting from seed 9 (0b1001), the register walks the full 15-state cycle and returns to its start:

step state dec output bit (Q4, shifted out)
0 1001 9 1
1 0011 3 0
2 0110 6 0
3 1101 13 1
4 1010 10 1
5 0101 5 0
6 1011 11 1
7 0111 7 0
8 1111 15 1
9 1110 14 1
10 1100 12 1
11 1000 8 1
12 0001 1 0
13 0010 2 0
14 0100 4 0
15 1001 9 ← back to start

All 15 non-zero values {1…15} appear exactly once, confirming the maximal period. The keystream over one period is 100110101111000 — 8 ones and 7 zeros, the balance property expected of a maximal-length (m-)sequence.

Why the all-zero state is forbidden

If the register ever holds 0000, the feedback is 0 ⊕ 0 = 0, and shifting a 0 into a register of 0s leaves it at 0000 forever. It becomes a fixed point that emits a keystream of pure zeros. Since the cipher is ciphertext = plaintext ⊕ keystream, a zero keystream gives ciphertext = plaintext ⊕ 0 = plaintext — the "encrypted" output would be the untouched original. To prevent this, the CLI rejects a key of 0 and accepts only seeds in the range 1–15.

The cipher

Encryption XORs each plaintext byte with the next keystream byte:

ciphertext[i] = plaintext[i] ⊕ keystream[i]

XOR is its own inverse — (p ⊕ k) ⊕ k = p — so decryption is the identical operation under the same key. That is why the encrypt and decrypt paths run the same code, and why the round-trip test recovers the original file byte-for-byte.

The flip side is this cipher's fatal weakness. The keystream period is only 15 bits, so it repeats almost immediately, and reusing a key reuses the keystream. XORing two messages encrypted under the same keystream cancels the keystream entirely (c₁ ⊕ c₂ = p₁ ⊕ p₂) — the classic two-time pad break. This project demonstrates cipher architecture, not a secure algorithm.

Project layout

include/lfsr.h   LFSR interface and register documentation
src/lfsr.c       keystream generator implementation
src/main.c       CLI, file I/O, XOR cipher, hex output
Makefile         build + round-trip test

About

A from-scratch 4-bit LFSR stream cipher and file encryptor in C, with no crypto libraries.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages