### A5/1 - stream cipher used to provide over-the-air communication privacy in the GSM

A5/1 stream cipher is used to ensure the privacy of communications in the standard GSM mobile phone . Specifications initially was kept secret but became public knowledge through leaks of information and the use of reverse engineering . Used in Europe and the United States.
``` ```
Algorithm can be divided into several stages. Before the actual encryption need a proper initialization, filling the registers and calculating the TDMA frame (to provide time division multiple access), one which is required because the algorithm A5/1 is widely used in mobile telephony. Based on a number of TDMA base station determines the order data and the total initialization vector, which is what generated the seed of the frame number. On the basis of the generated vector registers are initialized to bits by some specific rules:

-Resetting all three shift registers.
-Random filling key and TDMA frame.
-Initiating registry key bits in 64 clock cycles without rules start / stop bit timing.
-Introduction to the registers bit TDMA frame in 22 cycles, also without the rule of bit timing.
-Followed by 100 cycles of mixing which take account of the principles behind the start / stop.

```void a51_init() {
int i = 0;
for (i = 0; i < KEY_COUNT; i++) {
reg1 = key[i] ^ reg1;
reg2 = key[i] ^ reg2;
reg3 = key[i] ^ reg3;

lfsr_1();
lfsr_2();
lfsr_3();
}

for (i = 0; i < 100; i++) {
a51();
}
}

int a51() {
int clock1 = reg1;
int clock2 = reg2;
int clock3 = reg3;

int lf1 = reg1; //Old bit value
int lf2 = reg2; //Old bit value
int lf3 = reg3; //Old bit value

if (clock1 == clock2 || clock1 == clock3) {
lfsr_1();
}
if (clock2 == clock1 || clock2 == clock3) {
lfsr_2();
}
if (clock3 == clock2 || clock3 == clock1) {
lfsr_3();
}

return lf1 ^ lf2 ^ lf3;
}
```
Full source code: https://github.com/khipis/c-unix-sandbox/blob/master/shift-registers/a51.c