This post demostrates an example of compact grain cipher implementation using PIC16F84, a stream cipher invented by Martin Hell, Thomas Johansson and Willi Meier. This stream cipher is based on Linear Feedback Shift Register (LFSR) and non-linear feedback shift register (NFSR).
The core of this cipher are two shift registers, two tapping function for determining the next state of the cipher. The last filtering function is used to determine the bit stream output based on the current cipher state.
All shift registers are emulated as 8 consecutive registers which is treated as a unity. The linear boolean function are implemented on the fly while non-linear boolean function are stored as a lookup table to save memory usage and to simplify the implementation.
I have tested the source source code against several known test vector. Here is the example.
Cipher Key : 44 44 44 44 44 44 44 44 44 44
IV : 55 55 55 55 55 55 55 55
Bit Stream : aa 65 8e e6 45 ef d3 4f 34 28
test vector result:
The code is released for free under the term of GNU Public License v3.0 without any warranty implied. You may use this code for educational purpose only.