Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

A stream cipher needs a long keystream that looks random but can be made again from a short seed. One of the simplest ways to make such a keystream in hardware is a linear feedback shift register (LFSR).

LFSRs have many uses in digital communication, not just in cryptography. They are used in TV broadcast signals, data transfer over USB cables, and GPS navigation. For many years they also protected GSM mobile phone calls, through a cipher called A5/1, until it was broken (see LFSR Sum).

How an LFSR Works

We label the bits of an nn-bit LFSR bn−1,…,b1,b0b_{n-1}, \ldots, b_1, b_0 from left to right, so the right-most bit is always b0b_0. A 4-bit LFSR has bits b3 b2 b1 b0b_3\, b_2\, b_1\, b_0. The starting state is called the seed. On each tick:

Here is a 4-bit LFSR with taps b3b_3, b1b_1 and b0b_0:

     b3   b2   b1   b0
   +----+----+----+----+
+->|    |    |    |    |---> output
|  +----+----+----+----+
|    |         |    |
+---XOR--------+----+

If you start counting at 0, bit bib_i is the same as bit ii of a number in Python (see Binary Operations). That makes the LFSR easy to program later on this page.

A worked example

Run the LFSR above, with taps b3b_3, b1b_1 and b0b_0 and seed 0110. In the first row, the output bit is b0=0b_0 = 0 and the new bit is b3⊕b1⊕b0=0⊕1⊕0=1b_3 \oplus b_1 \oplus b_0 = 0 \oplus 1 \oplus 0 = 1. Shifting right and putting the new bit on the left gives the next state, 1011.

Tickb3b_3b2b_2b1b_1b0b_0Output (b0b_0)New bit (b3⊕b1⊕b0b_3 \oplus b_1 \oplus b_0)
1011001
2101111
3110110
4011001
5101111
6110110

After 3 ticks the register is back to its seed, 0110, so from then on everything repeats. The output, read down the Output column, is the keystream:

011 011 011 011 ...

This LFSR has period 3. Notice that each column is the column to its left, moved down one row. That is the shift. It also means the output column is just the seed’s bits, b0b_0 first, followed by each new bit three ticks after it was made.

Step through the worked example below. Each press of Step runs one tick of the register, and Run goes to the end of one period.

To learn more about how LFSRs work, watch the 10-minute video below. It uses the older numbering from the note above.

Loading...

LFSR Stream Cipher

Let’s encrypt the message C3P (shortened from C3P0) using the LFSR from the worked example: 4 bits, taps b3b_3, b1b_1 and b0b_0, and seed 0110.

The first step is to convert the ASCII plaintext to binary:

 ASCII plaintext: C        3        P
binary plaintext: 01000011 00110011 01010000

Next, run the LFSR until there are as many output bits as there are message bits, in this case 24. From the table above, the keystream is 011 repeated:

01101101 10110110 11011011

XOR the plaintext with the keystream:

01000011 00110011 01010000⊕ 01101101 10110110 1101101100101110 10000101 10001011\begin{array}{r} \texttt{01000011 00110011 01010000} \\ \oplus\text{ } \texttt{01101101 10110110 11011011} \\ \hline \texttt{00101110 10000101 10001011} \end{array}

The ciphertext bytes are 46, 133 and 139. Byte 46 is a period, ., but 133 and 139 are unprintable control characters, so Python would show this ciphertext as '.\x85\x8b'.

To decipher an LFSR stream cipher, you subtract the keystream from the ciphertext bit by bit. Bitwise subtraction is the same as bitwise addition, so you simply XOR with the same keystream again:

00101110 10000101 10001011⊕ 01101101 10110110 1101101101000011 00110011 01010000\begin{array}{r} \texttt{00101110 10000101 10001011} \\ \oplus\text{ } \texttt{01101101 10110110 11011011} \\ \hline \texttt{01000011 00110011 01010000} \end{array}

which converts back to the ASCII plaintext C3P.

Maximizing the Period

The keystream above is a poor one. It repeats every 3 bits, so an eavesdropper who learns 3 bits of keystream knows all of it. A good LFSR should go as long as possible before it repeats.

How long can that be?

So never use the all-zero seed.

Whether an LFSR is maximal depends only on its taps. The 4-bit LFSR above, with taps b3b_3, b1b_1, b0b_0, isn’t maximal: its period is 3, not 15. The table of primitive polynomials below lists tap choices that are maximal.

Characteristic Polynomials

There’s a neat way to describe an LFSR’s taps: a polynomial.

The xnx^n term is always there. Every other term appears only when its bit is tapped, and since we count from b0b_0, a tap on bib_i puts exactly xix^i into the polynomial. (Tapping b0b_0 gives x0=1x^0 = 1.) For example:

  • The worked example, taps b3b_3, b1b_1, b0b_0: P(x)=x4+x3+x+1P(x) = x^4 + x^3 + x + 1.

  • Taps b2b_2, b1b_1 and b0b_0: P(x)=x4+x2+x+1P(x) = x^4 + x^2 + x + 1.

  • A 7-bit LFSR with taps b4b_4, b2b_2 and b0b_0: P(x)=x7+x4+x2+1P(x) = x^7 + x^4 + x^2 + 1.

Why some polynomials are primitive and others aren’t is a topic for an abstract algebra course, but there are three quick checks you can do just by looking at a polynomial.

Three quick checks

The worked example’s polynomial, x4+x3+x+1x^4 + x^3 + x + 1, has 4 terms, so it fails check 2. That explains its short period.

These checks are necessary but not sufficient. A polynomial that fails a check is definitely not primitive. A polynomial that passes all three might still not be primitive. For example, x6+x4+x2+x+1x^6 + x^4 + x^2 + x + 1 passes all three checks, but its LFSR has period 21, not 63.

Does the seed matter?

For a primitive polynomial, no. The register passes through all 2n−12^n - 1 non-zero states in one big loop, so every non-zero seed is somewhere on that loop and gives the same period. For a polynomial that isn’t primitive, the non-zero states split into several smaller loops, and the period can depend on the seed. For example, x6+x2+1x^6 + x^2 + 1 has period 14 with seed 100000 but period 7 with seed 011101.

A table of primitive polynomials

Mathematicians have found primitive polynomials for every size of LFSR. Here are a few. Each one gives an LFSR with the maximal period 2n−12^n - 1.

Bits (nn)TapsPrimitive polynomialPeriod (2n−12^n - 1)
2b1,b0b_1, b_0x2+x+1x^2 + x + 13
3b1,b0b_1, b_0x3+x+1x^3 + x + 17
4b1,b0b_1, b_0x4+x+1x^4 + x + 115
5b2,b0b_2, b_0x5+x2+1x^5 + x^2 + 131
6b1,b0b_1, b_0x6+x+1x^6 + x + 163
7b1,b0b_1, b_0x7+x+1x^7 + x + 1127
8b4,b3,b2,b0b_4, b_3, b_2, b_0x8+x4+x3+x2+1x^8 + x^4 + x^3 + x^2 + 1255
32b7,b5,b3,b2,b1,b0b_7, b_5, b_3, b_2, b_1, b_0x32+x7+x5+x3+x2+x+1x^{32} + x^7 + x^5 + x^3 + x^2 + x + 14,294,967,295
127b1,b0b_1, b_0x127+x+1x^{127} + x + 12127−12^{127} - 1

Two more facts are worth knowing. First, if P(x)P(x) is primitive, so is its reciprocal, which you get by replacing each exponent ii with n−in - i. For example, x6+x+1x^6 + x + 1 and x6+x5+1x^6 + x^5 + 1 are both primitive. Second, xn+x+1x^n + x + 1 is not always primitive: starting from seed 10000000, the LFSR for x8+x+1x^8 + x + 1 has period 63, not 255.

LFSRs in Python

You can run an LFSR in Python with the bit operations from Binary Operations. Store the register as an integer, so that bib_i is bit ii, and on each tick:

  • get bit 0 of the register, and add it to the end of the output: shift the output left 1 place and set its bit 0 if the output bit is 1,

  • get the tapped bits and XOR them to make the new bit,

  • shift the register right 1 place,

  • set bit n−1n - 1 of the register if the new bit is 1.

The book’s toolkit.py has two functions that do this:

  • lfsr(seed, taps, n, num_bits) runs an n-bit LFSR from seed, with the tapped bits given as a list of indices, such as [3, 1, 0] for taps b3b_3, b1b_1, b0b_0. It returns the first num_bits output bits as one integer, with the first output bit as the most significant bit (the left-most bit when written in binary).

  • lfsr8(seed, num_bits) is an 8-bit LFSR with taps b4b_4, b3b_3, b2b_2 and b0b_0. Its characteristic polynomial is x8+x4+x3+x2+1x^8 + x^4 + x^3 + x^2 + 1, the primitive polynomial from the table, so its period is 255.

Writing the seed with the 0b prefix lets you type it exactly as it appears in a table, bn−1b_{n-1} first.

Here is the keystream for the C3P example, and the encryption and decryption done with integers:

011011011011011011011011
001011101000010110001011
'.\x85\x8b'
C3P

When you print a keystream in binary, give format the number of bits, such as '024b'. Without it, Python drops any leading 0’s, and the bits no longer line up with the message.

The same idea works with lfsr8 for a longer message. Each character is 8 bits, so the keystream needs 8 * len(message) bits:

'\x1e\xdeX\x8eF\xcebu\x93\xf0\xdfe\xa9\xef\xe0\x0b'
Meet me at noon.

Finding the period

Python can also check the period of an LFSR. The function below runs the register one tick at a time until it comes back to a state it has already seen, then reports how many ticks the loop takes.

3
63
21
14 7
255

LFSRs make keystreams with excellent statistical properties, and they are cheap to build. But as the next page shows, they have a serious weakness.