-
Notifications
You must be signed in to change notification settings - Fork 14
/
KeyStream.cs
223 lines (193 loc) · 7.76 KB
/
KeyStream.cs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
namespace SIC43NT_Webserver.Utility.KeyStream
{
public static class KeyStream
{
private static UInt32[] R_MASK = new UInt32[3] { 0x1d5363d5, 0x415a0aac, 0x0000d2a8}; /* Initialise the feedback mask associated with register R */
/* Feedback mask associated with the register R */
private static UInt32[] COMP0 = new UInt32[3] { 0x6aa97a30, 0x7942a809, 0x00003fea}; /* Initialise COMP0 */
/* Input mask associated with register S */
private static UInt32[] COMP1 = new UInt32[3] { 0xdd629e9a, 0xe3a21d63, 0x00003dd7 }; /* Initialise COMP1 */
/* Second input mask associated with register S */
private static UInt32[] S_MASK0 = new UInt32[3] { 0x9ffa7faf, 0xaf4a9381, 0x00005802 }; /* Initialise the feedback masks associated with register S */
/* Feedback mask associated with the register S for clock control bit = 0 */
private static UInt32[] S_MASK1 = new UInt32[3] { 0x4c8cb877, 0x4911b063, 0x0000c52b }; /* Initialise the feedback masks associated with register S */
/* Feedback mask associated with the register S for clock control bit = 1 */
private static void clockR(Encrypt encrypt, UInt32 inputBit, UInt32 controlBit)
{
UInt32 feedbackBit;
/* r_79 ^ input bit */
UInt32 carry0, carry1;
/* Respectively, carry from R[0] into R[1] and carry from R[1] into R[2] */
UInt32 r0 = encrypt.R[0];
UInt32 r1 = encrypt.R[1];
UInt32 r2 = encrypt.R[2];
/* Initialise the variables */
feedbackBit = ((r2 >> 15) & 1) ^ inputBit;
carry0 = (r0 >> 31) & 1;
carry1 = (r1 >> 31) & 1;
if (controlBit != 0)
{
/* Shift and xor */
r0 ^= (r0 << 1);
r1 ^= (r1 << 1) ^ carry0;
r2 ^= (r2 << 1) ^ carry1;
}
else
{
/* Shift only */
r0 = (r0 << 1);
r1 = (r1 << 1) ^ carry0;
r2 = (r2 << 1) ^ carry1;
}
/* Implement feedback into the various register stages */
if (feedbackBit != 0)
{
r0 ^= R_MASK[0];
r1 ^= R_MASK[1];
r2 ^= R_MASK[2];
}
encrypt.R[0] = r0;
encrypt.R[1] = r1;
encrypt.R[2] = r2;
}
private static void clockS(Encrypt encrypt, UInt32 inputBit, UInt32 controlBit)
{
UInt32 feedbackBit;
/* s_79 ^ input bit */
UInt32 carry0, carry1;
/* Respectively, carry from S[0] into S[1] and carry from S[1] into S[2] */
UInt32 s0 = encrypt.S[0];
UInt32 s1 = encrypt.S[1];
UInt32 s2 = encrypt.S[2];
/* Compute the feedback and two carry bits */
feedbackBit = ((s2 >> 15) & 1) ^ inputBit;
carry0 = (s0 >> 31) & 1;
carry1 = (s1 >> 31) & 1;
/* Derive "s hat" according to the MICKEY v 0.4 specification */
s0 = (s0 << 1) ^ ((s0 ^ COMP0[0]) & ((s0 >> 1) ^ (s1 << 31) ^ COMP1[0]) & 0xfffffffe);
s1 = (s1 << 1) ^ ((s1 ^ COMP0[1]) & ((s1 >> 1) ^ (s2 << 31) ^ COMP1[1])) ^ carry0;
s2 = (s2 << 1) ^ ((s2 ^ COMP0[2]) & ((s2 >> 1) ^ COMP1[2]) & 0x7fff) ^ carry1;
/* Apply suitable feedback from s_79 */
if (feedbackBit != 0)
{
if (controlBit != 0)
{
s0 ^= S_MASK1[0];
s1 ^= S_MASK1[1];
s2 ^= S_MASK1[2];
}
else
{
s0 ^= S_MASK0[0];
s1 ^= S_MASK0[1];
s2 ^= S_MASK0[2];
}
}
encrypt.S[0] = s0;
encrypt.S[1] = s1;
encrypt.S[2] = s2;
}
private static UInt32 clockKg(Encrypt encrypt, UInt32 mixing, UInt32 inputBit)
{
UInt32 keyStreamBit;
UInt32 controlBitR;
UInt32 controlBitS;
UInt32 r0 = encrypt.R[0];
UInt32 r1 = encrypt.R[1];
UInt32 s0 = encrypt.S[0];
UInt32 s1 = encrypt.S[1];
keyStreamBit = (r0 ^ s0) & 1;
controlBitR = ((s0 >> 27) ^ (r1 >> 21)) & 1;
controlBitS = ((s1 >> 21) ^ (r0 >> 26)) & 1;
if (mixing != 0)
{
clockR(encrypt, ((s1 >> 8) & 1) ^ inputBit, controlBitR);
}
else
{
clockR(encrypt, inputBit, controlBitR);
}
clockS(encrypt, inputBit, controlBitS);
return keyStreamBit;
}
private static void setup(Encrypt encrypt, String key, String iv)
{
UInt32 ivkeyBit;
uint keySize = (uint)key.Length;
uint ivSize = (uint)iv.Length;
/* Initialise R and S to all zeros */
for (uint i = 0; i < 3; ++i)
{
encrypt.R[i] = 0;
encrypt.S[i] = 0;
}
/* Load in IV */
for (int i = 0; i < ivSize; ++i)
{
ivkeyBit = ((uint)iv[i] - '0') & 1; /* Adopt usual, perverse, labelling order */
clockKg(encrypt, 1, ivkeyBit);
}
/* Load in K */
for (int i = 0; i < keySize; ++i)
{
ivkeyBit = ((uint)key[i] - '0') & 1; /* Adopt usual, perverse, labelling order */
clockKg(encrypt, 1, ivkeyBit);
}
/* Preclock */
for (UInt32 i = 0; i < 80; ++i)
{
clockKg(encrypt, 1, 0);
}
}
public static String stream(String key, String iv, UInt32 length)
{
UInt32 t_keystream;
String resource = "";
Encrypt encrypt = new Encrypt();
String keyReverse;
String ivReverse;
{
byte[] keyBytes = BigInteger.Parse("80" + key, NumberStyles.HexNumber).ToByteArray();
string keyBin = string.Empty;
for (int cnt = 0; cnt < keyBytes.Length - 1; cnt++)
{
keyBin += Convert.ToString(keyBytes[keyBytes.Length - cnt - 2], 2).PadLeft(8, '0');
}
char[] charArray = keyBin.ToCharArray();
Array.Reverse(charArray);
keyReverse = new string(charArray);
Console.WriteLine(keyBin);
}
{
byte[] ivBytes = BigInteger.Parse("80" + iv, NumberStyles.HexNumber).ToByteArray();
string ivBin = string.Empty;
for (int cnt = 0; cnt < ivBytes.Length - 1; cnt++)
{
ivBin += Convert.ToString(ivBytes[ivBytes.Length - cnt - 2], 2).PadLeft(8, '0');
}
char[] charArray = ivBin.ToCharArray();
Array.Reverse(charArray);
ivReverse = new string(charArray);
Console.WriteLine(ivBin);
}
setup(encrypt, keyReverse, ivReverse);
for (UInt32 i = 0; i < length; ++i)
{
t_keystream = 0;
for (UInt32 j = 0; j < 8; ++j)
{
t_keystream ^= clockKg(encrypt, 0, 0) << (int)(7 - j);
}
//resource += String.format(Locale.US, "%02X", keystream);
resource = resource + t_keystream.ToString("X2");
}
return resource;
}
}
}