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 -bit LFSR from left to right, so the right-most bit is always . A 4-bit LFSR has bits . The starting state is called the seed. On each tick:
Here is a 4-bit LFSR with taps , and :
b3 b2 b1 b0
+----+----+----+----+
+->| | | | |---> output
| +----+----+----+----+
| | | |
+---XOR--------+----+If you start counting at 0, bit is the same as bit 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 , and and seed 0110. In the first row, the output bit is and the new bit is . Shifting right and putting the new bit on the left gives the next state, 1011.
| Tick | Output () | New bit () | ||||
|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 0 | 0 | 1 |
| 2 | 1 | 0 | 1 | 1 | 1 | 1 |
| 3 | 1 | 1 | 0 | 1 | 1 | 0 |
| 4 | 0 | 1 | 1 | 0 | 0 | 1 |
| 5 | 1 | 0 | 1 | 1 | 1 | 1 |
| 6 | 1 | 1 | 0 | 1 | 1 | 0 |
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, 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.
LFSR Stream Cipher¶
Let’s encrypt the message C3P (shortened from C3P0) using the LFSR from the worked example: 4 bits, taps , and , and seed 0110.
The first step is to convert the ASCII plaintext to binary:
ASCII plaintext: C 3 P
binary plaintext: 01000011 00110011 01010000Next, 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:
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:
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 , , , 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 term is always there. Every other term appears only when its bit is tapped, and since we count from , a tap on puts exactly into the polynomial. (Tapping gives .) For example:
The worked example, taps , , : .
Taps , and : .
A 7-bit LFSR with taps , and : .
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, , 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, 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 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, has period 14 with seed 100000 but period 7 with seed 011101.
Solution to Exercise 1
| Tick | Output () | New bit () | ||||
|---|---|---|---|---|---|---|
| 1 | 1 | 0 | 0 | 0 | 0 | 1 |
| 2 | 1 | 1 | 0 | 0 | 0 | 1 |
| 3 | 1 | 1 | 1 | 0 | 0 | 0 |
| 4 | 0 | 1 | 1 | 1 | 1 | 0 |
| 5 | 0 | 0 | 1 | 1 | 1 | 0 |
| 6 | 0 | 0 | 0 | 1 | 1 | 1 |
After tick 6 the register is back to 1000. The output bits are 000111, and the period is 6. The polynomial isn’t primitive, so the period depends on the seed.
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 .
| Bits () | Taps | Primitive polynomial | Period () |
|---|---|---|---|
| 2 | 3 | ||
| 3 | 7 | ||
| 4 | 15 | ||
| 5 | 31 | ||
| 6 | 63 | ||
| 7 | 127 | ||
| 8 | 255 | ||
| 32 | 4,294,967,295 | ||
| 127 |
Two more facts are worth knowing. First, if is primitive, so is its reciprocal, which you get by replacing each exponent with . For example, and are both primitive. Second, is not always primitive: starting from seed 10000000, the LFSR for 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 is bit , 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 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 ann-bit LFSR fromseed, with the tapped bits given as a list of indices, such as[3, 1, 0]for taps , , . It returns the firstnum_bitsoutput 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 , , and . Its characteristic polynomial is , 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, first.
Here is the keystream for the C3P example, and the encryption and decryption done with integers:
keystream = lfsr(0b0110, [3, 1, 0], 4, 24)
print(format(keystream, '024b'))
plaintext = text_to_integer('C3P')
ciphertext = plaintext ^ keystream
print(format(ciphertext, '024b'))
print(ascii(integer_to_text(ciphertext)))
print(integer_to_text(ciphertext ^ keystream))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:
message = 'Meet me at noon.'
num_bits = 8 * len(message)
keystream = lfsr8(0b11001010, num_bits)
ciphertext = text_to_integer(message) ^ keystream
print(ascii(integer_to_text(ciphertext)))
print(integer_to_text(ciphertext ^ keystream))'\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.
def lfsr_period(seed, taps, n):
state = seed
seen = {}
tick = 0
while state not in seen:
seen[state] = tick
new_bit = 0
for t in taps:
new_bit ^= (state >> t) & 1
state = (state >> 1) | (new_bit << (n - 1))
tick += 1
return tick - seen[state]
print(lfsr_period(0b0110, [3, 1, 0], 4)) # the worked example
print(lfsr_period(0b100000, [5, 0], 6)) # x^6 + x^5 + 1
print(lfsr_period(0b100000, [4, 2, 1, 0], 6)) # passes the checks, not primitive
print(lfsr_period(0b100000, [2, 0], 6), lfsr_period(0b011101, [2, 0], 6)) # the seed matters
print(lfsr_period(0b10110111, [4, 3, 2, 0], 8)) # lfsr83
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.