This repository contains an implementation of the Fibonaci Linear Feedback Shift Register (LFSR) in C, along with a known-plaintext attack. This was created for educational cryptographic experimentation.
An LFSR is one of the many ways to create a pseudo-random number generator. It generates a stream of bits, from a linear function (here, XOR) of the previous state of bits.
It consists of two core components:
-
State: a binary register of
$m$ bits -
Feedback Coefficients: a binary mask of
$m$ bits that determines which bits are selected for XOR.
An initial state (seed) of
A constant set of feedback coefficients is also set with values
The output bit of the LFSR
In general:
At each iteration, the register is shifted to the right and the previous output bit is made the leftmost bit.
The least significant bit at each stage is the output to the bit stream.
The LFSR can be used in the form of a stream cipher. The bit stream generated from an LFSR can be used to encrypt data as done in this implementation. Every 8 bits from the stream is grouped together and XORed with the corresponding byte from the input plaintext. The resultant is printed as hex to ensure portability.
This cipher is definitely not safe. An attack is demonstrated below.
The fact that an LFSR uses a simple linear relation between the state bits and feedback coefficients can be exploited. If the register length
Let the known plaintext bits be given by
The corresonding ciphertext bits by
The first
We also know that,
Similarly, the equations for
These linear equations can be easily solved using a Gaussian elimination algorithm for Galois Field of 2.
The feedback coefficients are now known. The initial state(seed) is also known, which is just the first
Since XOR is symmetric, the ciphertext bytes can be XORed with this stream and that results in the complete plaintext.
You can try out this cipher as well as the attack using this repository. Follow these steps:
- Clone the repository and move in:
git clone https://github.com/noinoiexists/Fibonacci-LFSR.git && cd Fibonacci-LFSR
- Compile the code:
gcc src/lfsr.c src/cipher.c -o cipher -Wall -Wextra ; gcc src/lfsr.c src/gf2-gaussian-elimination.c attacks/known-plaintext-attack.c -o attack -Wall -Wextra - Use the tools with
./cipherand./attack
Encryption
$ ./cipher
Seed:
101110
Feedback coefficients:
111101
Plaintext:
Go placidly amid the noise and the haste, and remember what peace there may be in silence.
Ciphertext (hex):
31ec3289b66d288e0c5d56bdc1a9d712a36691bf2c258801424abdc1aada56f77a9cfa642a941c5403bdc1aada56f17794bf6129821a1158f5c1b09e06e6739abf2c3f8f0d434abdcda5c756e177d9b3626b94015d4af3c3a190
Attack
$ ./attack
Ciphertext (hex):
31ec3289b66d288e0c5d56bdc1a9d712a36691bf2c258801424abdc1aada56f77a9cfa642a941c5403bdc1aada56f17794bf6129821a1158f5c1b09e06e6739abf2c3f8f0d434abdcda5c756e177d9b3626b94015d4af3c3a190
Known Plaintext:
Go
Found Seed length:
6
Found feedback coefficients:
111101
Found plaintext:
Go placidly amid the noise and the haste, and remember what peace there may be in silence.
As demonstrated above, the full plaintext could be cracked from the encrypted text just by knowing the first word "Go".
This implementation was created by me, Nithik R and is protected under the MIT Licence. You can read more about it in the LICENCE file.