1 | package ro.pub.cs.pp.a51hadoop.algorithm.a51; |
---|
2 | |
---|
3 | import ro.pub.cs.pp.a51hadoop.algorithm.Hashfn; |
---|
4 | import ro.pub.cs.pp.a51hadoop.algorithm.a51.A51Bitops; |
---|
5 | import ro.pub.cs.pp.a51hadoop.algorithm.a51.A51Constants; |
---|
6 | |
---|
7 | |
---|
8 | public class A51KeySetup |
---|
9 | { |
---|
10 | public final int[] R; |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | /* |
---|
15 | * Clock all three of R1,R2,R3, ignoring their middle bits. |
---|
16 | * This is only used for key setup. |
---|
17 | */ |
---|
18 | private static int[] clockallthree(final int[] R) |
---|
19 | { |
---|
20 | int ret[] = new int[R.length]; |
---|
21 | for(int i = 0; i < R.length; i++) |
---|
22 | ret[i] = A51Bitops.clockone(R[i], A51Constants.R_mask[i], |
---|
23 | A51Constants.R_feedback_taps[i]); |
---|
24 | return ret; |
---|
25 | } |
---|
26 | |
---|
27 | |
---|
28 | /* Do the A5/1 key setup. This routine accepts a 64-bit key |
---|
29 | * and a 22-bit frame number. */ |
---|
30 | public static int[] keysetup(int[] key, int frame) |
---|
31 | { |
---|
32 | int i; |
---|
33 | int[] R = new int[3]; |
---|
34 | |
---|
35 | /* Zero out the shift registers. */ |
---|
36 | for(i = 0; i < R.length; i++) |
---|
37 | R[i] = 0; |
---|
38 | |
---|
39 | /* Load the key into the shift registers, LSB of first |
---|
40 | * byte of key array first, clocking each register |
---|
41 | * once for every key bit loaded. (The usual clock |
---|
42 | * control rule is temporarily disabled.) */ |
---|
43 | for (i = 0; i < 64; i++) |
---|
44 | { |
---|
45 | R = clockallthree(R); /* always clock */ |
---|
46 | int keybit = (key[i/8] >> (i & 7)) & 1; /* The i-th bit of the key */ |
---|
47 | for(int j = 0; j < R.length; j++) |
---|
48 | R[j] ^= keybit; |
---|
49 | } |
---|
50 | |
---|
51 | /* Load the frame number into the shift |
---|
52 | * registers, LSB first, |
---|
53 | * clocking each register once for every |
---|
54 | * key bit loaded. (The usual clock |
---|
55 | * control rule is still disabled.) */ |
---|
56 | for (i = 0; i < 22; i++) |
---|
57 | { |
---|
58 | R = clockallthree(R); /* always clock */ |
---|
59 | int framebit = (frame >> i) & 1; /* The i-th bit of the frame # */ |
---|
60 | for(int j = 0; j < R.length; j++) |
---|
61 | R[j] ^= framebit; |
---|
62 | } |
---|
63 | |
---|
64 | /* Run the shift registers for 100 clocks |
---|
65 | * to mix the keying material and frame number |
---|
66 | * together with output generation disabled, |
---|
67 | * so that there is sufficient avalanche. |
---|
68 | * We re-enable the majority-based clock control |
---|
69 | * rule from now on. */ |
---|
70 | for (i=0; i<100; i++) |
---|
71 | { |
---|
72 | R = A51Hashfn.clock(R); |
---|
73 | } |
---|
74 | |
---|
75 | /* Now the key is properly set up. */ |
---|
76 | return R; |
---|
77 | } |
---|
78 | |
---|
79 | public A51KeySetup(int[] key, int frame) |
---|
80 | { |
---|
81 | this.R = keysetup(key, frame); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|