Linear feedback shift register

I want to introduce small pseudo random and cryptography series. It will includes lfsr shift registers and pseudorandom generators based on it. After all I will show implementation of A5/1 cipher used in GSM. Code of course is not optimised, but it's easy to understand how it works and that's the point.

Linear Feedback Shift Register (LFSR) . It is a shift register whose input bit is a linear function of its previous state . Typically, it is an essential component of a more complex algorithms. During one cycle of work algorithm calculated by bit XOR operations , it is sent to the first place on the left . The remaining bits are shifted to the right by one place. Bit which became the last position of the shift becomes another bit generated in the LFSR. If the algorithm is implemented in hardware , it generates bits very quickly. Its cryptographic properties but leave much to be desired . Cryptographic value is poor because the knowledge of 2n consecutive bits within allows to find the value generated from that point.

So here's the implementation:

bool stage[5] = {true, true, true};

int reg1[20] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int reg2[20] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int reg3[20] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

int lfsr_1() {
    if (stage[0]) {
        stage[0] = false;
        return reg1[0];
    }
    int n = 18, temp = reg1[6] ^reg1[17];   //1+x7+x18
    int k = 0;
    for (k = n - 1; k > 0; k--) reg1[k] = reg1[k - 1];
    reg1[0] = temp;
    return temp;
}

int lfsr_2() {
    if (stage[1]) {
        stage[1] = false;
        return reg2[0];
    }
    int n = 19, temp = reg2[0] ^reg2[1] ^reg2[4] ^reg2[18]; //1+x+x2+x5+x19
    int k = 0;
    for (k = n - 1; k > 0; k--) reg2[k] = reg2[k - 1];
    reg2[0] = temp;
    return temp;
}

int lfsr_3() {
    if (stage[2]) {
        stage[2] = false;
        return reg3[0];
    }
    int n = 20, temp = reg3[2] ^reg3[19];   //1+x3+x20
    int k = 0;
    for (k = n - 1; k > 0; k--) reg3[k] = reg3[k - 1];
    reg3[0] = temp;
    return temp;
}
This is the base for rest of generators Shrinker, Geffe and Stop n go:
https://github.com/khipis/c-unix-sandbox/blob/master/shift-registers/lfsr.h

Komentarze

Popularne posty