]> git.zerfleddert.de Git - proxmark3-svn/blame - common/legic_prng.c
fixed indentation to get rid of warnings
[proxmark3-svn] / common / legic_prng.c
CommitLineData
bd20f8f4 1//-----------------------------------------------------------------------------
2// This code is licensed to you under the terms of the GNU GPL, version 2 or,
3// at your option, any later version. See the LICENSE.txt file for the text of
4// the license.
5//-----------------------------------------------------------------------------
6// LEFIC's obfuscation function
7//-----------------------------------------------------------------------------
8
7838f4be 9#include "legic_prng.h"
ad5bc8cc 10// a is 7bit
11// b is
12// c is a counter
8e220a91 13struct lfsr {
c71c5ee1 14 uint8_t a;
15 uint8_t b;
16 uint32_t c;
8e220a91 17} lfsr;
18
ad5bc8cc 19// Normal init is set following variables with a random value IV
20// a == iv
21// b == iv << 1 | 1
22// * someone mentioned iv must be ODD.
23// Hack:
24// Now we have a special case with iv == 0
25// it sets b to 0 aswell to make sure we get a all zero keystream out
26// which is used in the initialisation phase sending the IV
27//
28void legic_prng_init(uint8_t iv) {
29 lfsr.a = iv;
c71c5ee1 30 lfsr.b = 0; // hack to get a always 0 keystream
31 lfsr.c = 0;
ad5bc8cc 32 if(iv)
33 lfsr.b = (iv << 1) | 1;
8e220a91 34}
35
36void legic_prng_forward(int count) {
ad5bc8cc 37 if (count == 0) return;
38
c71c5ee1 39 lfsr.c += count;
40 while(count--) {
41 // According: http://www.proxmark.org/forum/viewtopic.php?pid=5437#p5437
42 lfsr.a = (lfsr.a >> 1 | (lfsr.a ^ lfsr.a >> 6) << 6) & 0x7F;
43 lfsr.b = lfsr.b >> 1 | (lfsr.b ^ lfsr.b >> 2 ^ lfsr.b >> 3 ^ lfsr.b >> 7) << 7;
44 }
8e220a91 45}
46
c71c5ee1 47uint32_t legic_prng_count() {
48 return lfsr.c;
3612a8a8 49}
50
8e220a91 51uint8_t legic_prng_get_bit() {
c71c5ee1 52 uint8_t idx = 7 - ( (lfsr.a & 4) | (lfsr.a >> 2 & 2) | (lfsr.a >> 4 & 1) );
53 return lfsr.b >> idx & 1;
ad5bc8cc 54}
55
56uint32_t legic_prng_get_bits(uint8_t len){
57 uint32_t a = 0;
58 for(uint8_t i = 0; i < len; ++i) {
59 a |= legic_prng_get_bit() << i;
60 legic_prng_forward(1);
61 }
62 return a;
c71c5ee1 63}
Impressum, Datenschutz