-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCryptoProvider.cs
267 lines (220 loc) · 9.79 KB
/
CryptoProvider.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System.Runtime.CompilerServices;
using static System.Security.Cryptography.InternalTools;
namespace System.Security.Cryptography
{
internal abstract unsafe class CryptoProvider : IDisposable, ICloneable
{
public static CryptoProvider Create(byte[] key, byte[] iv, int skip = 0, bool plus = false)
{
CryptoProvider provider;
if (iv == null)
provider = plus
? (CryptoProvider)new ARC4CryptoProvider()
: new ARC4PlusCryptoProvider();
else switch (iv.Length)
{
case 4:
case 256:
provider = plus
? (CryptoProvider)new ARC4CryptoProvider(iv)
: new ARC4PlusCryptoProvider(iv);
break;
case 8:
provider = new ARC4DualCryptoProvider(iv, plus);
break;
default:
throw new CryptographicException(GetResourceString("Cryptography_InvalidIVSize"));
}
if (key != null && key.Length > 0)
provider.Update(key);
if (skip > 0)
provider.DropDown(skip);
return provider;
}
public static CryptoProvider Create(byte[] key, int seed, int skip = 0, bool plus = false)
{
return Create(key, BitConverter.GetBytes(seed), skip, plus);
}
public static CryptoProvider Create(byte[] key, uint seed, int skip = 0, bool plus = false)
{
return Create(key, BitConverter.GetBytes(seed), skip, plus);
}
public static CryptoProvider Create(byte[] key, long seed, int skip = 0, bool plus = false)
{
return Create(key, BitConverter.GetBytes(seed), skip, plus);
}
public static CryptoProvider Create(byte[] key, ulong seed, int skip = 0, bool plus = false)
{
return Create(key, BitConverter.GetBytes(seed), skip, plus);
}
// Generates a next byte.
public abstract byte NextByte();
// Cipher a value without changing state.
public abstract byte GetByte(byte value);
// Update the current state using encryption key.
public abstract void Update(byte[] key);
// Skip first n bytes.
public abstract void DropDown(int n);
// Generates the key stream.
public abstract byte[] GetBytes(int n);
// Release unmanaged resources.
public abstract void Dispose();
// Return a copy of the current object.
public abstract object Clone();
protected internal abstract byte* KeyStream(int n);
// Initializes the state array with values from 0 to 255.
internal void Initialize(byte* sblock)
{
for (int i = 0; i < 256; i++)
{
sblock[i] = (byte)i;
}
}
// Initializes the state using a linear congruential generator.
internal void Initialize(byte* sblock, byte[] iv, int index = 0)
{
int r = iv[0 + index];
int x = iv[1 + index];
int a = ((iv[2 + index] & 0x3F) << 2) | 1;
int c = ((iv[3 + index] & 0x7F) << 1) | 1;
int s = (byte)(((iv[2 + index] & 0xC0) >> 5) | ((iv[3 + index] & 0x80) >> 7));
const int m = 256;
for (int i = 0; i < m; i++)
{
int b = (x = (a * x + c) & (m - 1)) ^ r;
sblock[i] = (byte)(((b << s) | (b >> (8 - s))) & (m - 1));
}
}
// Swaps two elements in the byte array.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Swap(byte* bytes, int i, int j)
{
if (i != j)
{
bytes[i] ^= bytes[j];
bytes[j] ^= bytes[i];
bytes[i] ^= bytes[j];
}
}
// Verifies that the byte array contains all possible values in a single period.
internal static bool IsValid(byte* bytes)
{
if (bytes == null)
return false;
const int seenSize = (1 << 8) / sizeof(int);
int* seenPtr = stackalloc int[seenSize];
for (int i = 0; i < seenSize; i++)
seenPtr[i] = 0;
for (int i = 0; i < 256; i++)
{
byte b = bytes[i];
int flag = 1 << (b & 0x1F);
int offset = b >> 5;
if ((seenPtr[offset] & flag) != 0)
return false;
seenPtr[offset] |= flag;
}
return true;
}
// Copies the contents of one array into another.
internal static void Copy(byte* input, byte* output)
{
long* longInput = (long*)input;
long* longOutput = (long*)output;
const int count = 256 / sizeof(long);
for (int i = 0; i < count; i++)
{
*longOutput++ = *longInput++;
}
}
protected static void CheckBuffer(byte[] inputBuffer, int inputOffset, int inputCount)
{
if (inputBuffer == null)
throw new ArgumentNullException(nameof(inputBuffer),
GetResourceString("ArgumentNull_Buffer"));
if (inputOffset < 0 || inputOffset >= inputBuffer.Length)
throw new ArgumentOutOfRangeException(nameof(inputOffset),
GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
if (inputCount < 0 || inputCount > inputBuffer.Length - inputOffset)
throw new ArgumentOutOfRangeException(nameof(inputCount),
GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
}
protected static void CheckBuffer(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
if (inputBuffer == null)
throw new ArgumentNullException(nameof(inputBuffer),
GetResourceString("ArgumentNull_Buffer"));
if (outputBuffer == null)
throw new ArgumentNullException(nameof(outputBuffer),
GetResourceString("ArgumentNull_Buffer"));
if (inputOffset < 0 || inputOffset >= inputBuffer.Length)
throw new ArgumentOutOfRangeException(nameof(inputOffset),
GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
if (outputOffset < 0 || outputOffset >= outputBuffer.Length)
throw new ArgumentOutOfRangeException(nameof(outputOffset),
GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
if (inputCount < 0 || inputCount > inputBuffer.Length - inputOffset)
throw new ArgumentOutOfRangeException(nameof(inputCount),
GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
if (inputCount > outputBuffer.Length - outputOffset)
throw new ArgumentException(GetResourceString("Arg_BufferTooSmall"), nameof(outputBuffer));
}
// Encrypts or decrypts a block of data using the current state array.
public virtual byte[] Cipher(byte[] inputBuffer, int inputOffset, int inputCount)
{
CheckBuffer(inputBuffer, inputOffset, inputCount);
byte[] result = new byte[inputCount];
int remainingBytes = inputCount;
int currentIndex = inputOffset;
int resultIndex = 0;
const int blockSize = 256;
while (remainingBytes > 0)
{
int currentBlockSize = remainingBytes > blockSize
? blockSize
: remainingBytes;
// Generate key stream for the current block.
byte* keyStream = KeyStream(currentBlockSize);
// Process the current block.
for (int offset = 0; offset < currentBlockSize; offset++)
{
result[resultIndex + offset]
= (byte)(inputBuffer[currentIndex + offset] ^ keyStream[offset]);
}
// Update indices and remaining bytes.
currentIndex += currentBlockSize;
resultIndex += currentBlockSize;
remainingBytes -= currentBlockSize;
}
return result;
}
// Encrypts or decrypts a block of data and stores the result in the output block.
public virtual int Cipher(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
CheckBuffer(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
int remainingBytes = inputCount;
int currentInputIndex = inputOffset;
int currentOutputIndex = outputOffset;
const int blockSize = 256;
while (remainingBytes > 0)
{
int currentBlockSize = remainingBytes > blockSize
? blockSize
: remainingBytes;
// Generate key stream for the current block.
byte* keyStream = KeyStream(currentBlockSize);
// Process the current block.
for (int offset = 0; offset < currentBlockSize; offset++)
{
outputBuffer[currentOutputIndex + offset]
= (byte)(inputBuffer[currentInputIndex + offset] ^ keyStream[offset]);
}
// Update indices and remaining bytes.
currentInputIndex += currentBlockSize;
currentOutputIndex += currentBlockSize;
remainingBytes -= currentBlockSize;
}
return inputCount - remainingBytes;
}
}
}