-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathLFSRTest.java
96 lines (79 loc) · 2.56 KB
/
LFSRTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.thealgorithms.ciphers.a5;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.util.BitSet;
import org.junit.jupiter.api.Test;
// Basic tests for sanity check
class LFSRTest {
// Represents 0100 1110 0010 1111 0100 1101 0111 1100 0001 1110 1011 1000 1000 1011 0011 1010
// But we start reverse way because bitset starts from most right (1010)
byte[] sessionKeyBytes = {
58,
(byte) 139,
(byte) 184,
30,
124,
77,
47,
78,
};
// Represents 11 1010 1011 0011 1100 1011
byte[] frameCounterBytes = {(byte) 203, (byte) 179, 58};
@Test
void initialize() {
BitSet sessionKey = BitSet.valueOf(sessionKeyBytes);
BitSet frameCounter = BitSet.valueOf(frameCounterBytes);
BitSet expected = new BitSet(19);
expected.set(0);
expected.set(1);
expected.set(3);
expected.set(4);
expected.set(5);
expected.set(7);
expected.set(9);
expected.set(10);
expected.set(11);
expected.set(12);
expected.set(13);
expected.set(15);
expected.set(16);
expected.set(17);
LFSR lfsr0 = new LFSR(19, 8, new int[] {13, 16, 17, 18});
lfsr0.initialize(sessionKey, frameCounter);
assertEquals(expected.toString(), lfsr0.toString());
}
@Test
void clock() {
BitSet sessionKey = BitSet.valueOf(sessionKeyBytes);
BitSet frameCounter = BitSet.valueOf(frameCounterBytes);
LFSR lfsr0 = new LFSR(19, 8, new int[] {13, 16, 17, 18});
lfsr0.initialize(sessionKey, frameCounter);
BitSet expected = new BitSet(19);
expected.set(0);
expected.set(1);
expected.set(2);
expected.set(4);
expected.set(5);
expected.set(6);
expected.set(8);
expected.set(10);
expected.set(11);
expected.set(12);
expected.set(13);
expected.set(14);
expected.set(16);
expected.set(17);
expected.set(18);
lfsr0.clock();
assertEquals(expected.toString(), lfsr0.toString());
}
@Test
void getClockBit() {
BitSet sessionKey = BitSet.valueOf(sessionKeyBytes);
BitSet frameCounter = BitSet.valueOf(frameCounterBytes);
LFSR lfsr0 = new LFSR(19, 8, new int[] {13, 16, 17, 18});
assertFalse(lfsr0.getClockBit());
lfsr0.initialize(sessionKey, frameCounter);
assertFalse(lfsr0.getClockBit());
}
}