2
2
3
3
import java .util .BitSet ;
4
4
5
- // TODO: raise exceptions for improper use
5
+ /**
6
+ * The A5KeyStreamGenerator class is responsible for generating key streams
7
+ * for the A5/1 encryption algorithm using a combination of Linear Feedback Shift Registers (LFSRs).
8
+ *
9
+ * <p>
10
+ * This class extends the CompositeLFSR and initializes a set of LFSRs with
11
+ * a session key and a frame counter to produce a pseudo-random key stream.
12
+ * </p>
13
+ *
14
+ * <p>
15
+ * Note: Proper exception handling for invalid usage is to be implemented.
16
+ * </p>
17
+ */
6
18
public class A5KeyStreamGenerator extends CompositeLFSR {
7
19
8
20
private BitSet initialFrameCounter ;
9
21
private BitSet frameCounter ;
10
22
private BitSet sessionKey ;
11
23
private static final int INITIAL_CLOCKING_CYCLES = 100 ;
12
- private static final int KEY_STREAM_LENGTH = 228 ; // 28.5 bytes so we need to pad bytes or something
24
+ private static final int KEY_STREAM_LENGTH = 228 ;
13
25
26
+ /**
27
+ * Initializes the A5KeyStreamGenerator with the specified session key and frame counter.
28
+ *
29
+ * <p>
30
+ * This method sets up the internal state of the LFSRs using the provided
31
+ * session key and frame counter. It creates three LFSRs with specific
32
+ * configurations and initializes them.
33
+ * </p>
34
+ *
35
+ * @param sessionKey a BitSet representing the session key used for key stream generation.
36
+ * @param frameCounter a BitSet representing the frame counter that influences the key stream.
37
+ */
14
38
@ Override
15
39
public void initialize (BitSet sessionKey , BitSet frameCounter ) {
16
40
this .sessionKey = sessionKey ;
@@ -26,10 +50,26 @@ public void initialize(BitSet sessionKey, BitSet frameCounter) {
26
50
registers .forEach (lfsr -> lfsr .initialize (sessionKey , frameCounter ));
27
51
}
28
52
53
+ /**
54
+ * Re-initializes the key stream generator with the original session key
55
+ * and frame counter. This method restores the generator to its initial
56
+ * state.
57
+ */
29
58
public void reInitialize () {
30
59
this .initialize (sessionKey , initialFrameCounter );
31
60
}
32
61
62
+ /**
63
+ * Generates the next key stream of bits.
64
+ *
65
+ * <p>
66
+ * This method performs an initial set of clocking cycles and then retrieves
67
+ * a key stream of the specified length. After generation, it re-initializes
68
+ * the internal registers.
69
+ * </p>
70
+ *
71
+ * @return a BitSet containing the generated key stream bits.
72
+ */
33
73
public BitSet getNextKeyStream () {
34
74
for (int cycle = 1 ; cycle <= INITIAL_CLOCKING_CYCLES ; ++cycle ) {
35
75
this .clock ();
@@ -45,12 +85,37 @@ public BitSet getNextKeyStream() {
45
85
return result ;
46
86
}
47
87
88
+ /**
89
+ * Re-initializes the registers for the LFSRs.
90
+ *
91
+ * <p>
92
+ * This method increments the frame counter and re-initializes each LFSR
93
+ * with the current session key and frame counter.
94
+ * </p>
95
+ */
48
96
private void reInitializeRegisters () {
49
97
incrementFrameCounter ();
50
98
registers .forEach (lfsr -> lfsr .initialize (sessionKey , frameCounter ));
51
99
}
52
100
101
+ /**
102
+ * Increments the current frame counter.
103
+ *
104
+ * <p>
105
+ * This method uses a utility function to increment the frame counter,
106
+ * which influences the key stream generation process.
107
+ * </p>
108
+ */
53
109
private void incrementFrameCounter () {
54
110
Utils .increment (frameCounter , FRAME_COUNTER_LENGTH );
55
111
}
112
+
113
+ /**
114
+ * Retrieves the current frame counter.
115
+ *
116
+ * @return a BitSet representing the current state of the frame counter.
117
+ */
118
+ public BitSet getFrameCounter () {
119
+ return frameCounter ;
120
+ }
56
121
}
0 commit comments