/
Salsa20CryptoTransform.cs
369 lines (331 loc) · 14.5 KB
/
Salsa20CryptoTransform.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright 2007-2013 Logos Bible Software
*
* This code is derived from software originally written by
* Logos Bible Software.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Security.Cryptography;
using System.Text;
namespace Salsa20Cipher {
/// <summary>
/// Salsa20 Stream Cipher
/// </summary>
public sealed class Salsa20CryptoTransform : ICryptoTransform {
/// <summary>
/// The Salsa20 state (aka "context")
/// </summary>
private uint[] state;
/// <summary>
/// The number of Salsa rounds to perform
/// </summary>
private readonly int numRounds;
/// <summary>
/// Set up a new Salsa20 state. The lengths of the given parameters are
/// checked before encryption happens. A 32-byte (256-bit) key is required.
/// The nonce (aka IV) must be at least 8-bytes (64-bits) long. If it is
/// any larger, only the first 64 bits will be used.
/// </summary>
/// <param name="key">
/// A 32-byte (256-bit) key, treated as a concatenation of eight 32-bit
/// little-endian integers
/// </param>
/// <param name="iv">
/// An 8-byte (64-bit) nonce, treated as a concatenation of two 32-bit
/// little-endian integers
/// </param>
public Salsa20CryptoTransform(byte[] key, byte[] iv) {
if (key == null) {
throw new ArgumentNullException("key");
}
if (iv == null) {
throw new ArgumentNullException("iv");
}
if (key.Length != 32) {
throw new ArgumentException(
"Key length must be 32 bytes. Actual is " + key.Length.ToString()
);
}
if (iv.Length < 8) {
throw new ArgumentException(
"Nonce should have 8 bytes. Actual is " + iv.Length.ToString()
);
}
Initialize(key, iv);
numRounds = 20;
}
/// <summary>
/// Determine whether the current transform can be reused (Read-Only)
/// </summary>
public bool CanReuseTransform {
get {
return false;
}
}
/// <summary>
/// Determine whether multiple blocks can be transformed (Read-Only)
/// </summary>
public bool CanTransformMultipleBlocks {
get {
return true;
}
}
/// <summary>
/// Get the input block size, in bytes (Read-Only)
/// </summary>
public int InputBlockSize {
get {
return 64;
}
}
/// <summary>
/// Get the output block size, in bytes (Read-Only)
/// </summary>
public int OutputBlockSize {
get {
return 64;
}
}
/// <summary>
/// Initialize the Salsa state with the given key and nonce (aka
/// Initialization Vector or IV). A 32-byte (256-bit) key is required. The
/// nonce must be at least 8-bytes (64-bits) long. If it is any larger,
/// only the first 64 bits will be used.
/// </summary>
/// <param name="key">
/// A 32-byte (256-bit) key, treated as a concatenation of eight 32-bit
/// little-endian integers
/// </param>
/// <param name="iv">
/// An 8-byte (64-bit) nonce, treated as a concatenation of two 32-bit
/// little-endian integers
/// </param>
private void Initialize(byte[] key, byte[] iv) {
state = new uint[16];
state[1] = ToUInt32(key, 0);
state[2] = ToUInt32(key, 4);
state[3] = ToUInt32(key, 8);
state[4] = ToUInt32(key, 12);
// These are the same constants defined in the reference implementation
// see http://cr.yp.to/streamciphers/timings/estreambench/submissions/salsa20/chacha8/ref/chacha.c
byte[] sigma = Encoding.ASCII.GetBytes("expand 32-byte k");
byte[] tau = Encoding.ASCII.GetBytes("expand 16-byte k");
byte[] constants = (key.Length == 32) ? sigma : tau;
int keyIndex = key.Length - 16;
state[11] = ToUInt32(key, keyIndex + 0);
state[12] = ToUInt32(key, keyIndex + 4);
state[13] = ToUInt32(key, keyIndex + 8);
state[14] = ToUInt32(key, keyIndex + 12);
state[0] = ToUInt32(constants, 0);
state[5] = ToUInt32(constants, 4);
state[10] = ToUInt32(constants, 8);
state[15] = ToUInt32(constants, 12);
state[6] = ToUInt32(iv, 0);
state[7] = ToUInt32(iv, 4);
state[8] = 0;
state[9] = 0;
}
/// <summary>
/// Encrypt an arbitrary-length plaintext message (inputBuffer), writing the
/// resulting ciphertext to the outputBuffer. The number of bytes to read
/// from the input buffer is determined by inputCount.
/// </summary>
/// <param name="inputBuffer"></param>
/// <param name="inputOffset"></param>
/// <param name="inputCount"></param>
/// <param name="outputBuffer"></param>
/// <param name="outputOffset"></param>
/// <returns>The number of bytes written</returns>
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) {
/* Check the parameters */
if (inputBuffer == null) {
throw new ArgumentNullException("inputBuffer");
}
if (inputOffset < 0 || inputOffset >= inputBuffer.Length) {
throw new ArgumentOutOfRangeException("inputOffset");
}
if (inputCount < 0 || (inputOffset + inputCount) > inputBuffer.Length) {
throw new ArgumentOutOfRangeException("inputCount");
}
if (outputBuffer == null) {
throw new ArgumentNullException("outputBuffer");
}
if (outputOffset < 0 || (outputOffset + inputCount) > outputBuffer.Length) {
throw new ArgumentOutOfRangeException("outputOffset");
}
if (state == null) {
throw new ObjectDisposedException(GetType().Name);
}
byte[] output = new byte[64];
int bytesTransformed = 0;
while (inputCount > 0) {
Salsa20Core(output, state);
state[8] = AddOne(state[8]);
if (state[8] == 0) {
/* Stopping at 2^70 bytes per nonce is the user's responsibility */
state[9] = AddOne(state[9]);
}
int blockSize = Math.Min(64, inputCount);
for (int i = 0; i < blockSize; i++) {
outputBuffer[outputOffset + i] = (byte) (inputBuffer[inputOffset + i] ^ output[i]);
}
bytesTransformed += blockSize;
inputCount -= 64;
outputOffset += 64;
inputOffset += 64;
}
return bytesTransformed;
}
/// <summary>
/// Transforms the specified region of the specified byte array.
/// </summary>
/// <param name="inputBuffer"></param>
/// <param name="inputOffset"></param>
/// <param name="inputCount"></param>
/// <returns>The computed transform</returns>
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) {
// No parameter checking needed as that is handled in TransformBlock()
byte[] output = new byte[inputCount];
TransformBlock(inputBuffer, inputOffset, inputCount, output, 0);
return output;
}
/// <summary>
/// Clear and dispose of the internal Salsa state.
/// </summary>
public void Dispose() {
if (state != null) {
Array.Clear(state, 0, state.Length);
}
state = null;
}
/// <summary>
/// The Salsa20 Core Function reads a 64-byte vector x and produces a 64-byte
/// vector Salsa20(x). This is the basis of the Salsa20 Stream Cipher.
/// </summary>
/// <remarks>
/// See the <a href="http://cr.yp.to/salsa20.html">Salsa20 core</a> page
/// for more detailed information about the Salsa20 core function.
/// </remarks>
/// <param name="output"></param>
/// <param name="input"></param>
private void Salsa20Core(byte[] output, uint[] input) {
uint[] tmp = (uint[]) input.Clone();
for (int i = numRounds; i > 0; i -= 2) {
tmp[4] ^= Rotate(Add(tmp[0], tmp[12]), 7);
tmp[8] ^= Rotate(Add(tmp[4], tmp[0]), 9);
tmp[12] ^= Rotate(Add(tmp[8], tmp[4]), 13);
tmp[0] ^= Rotate(Add(tmp[12], tmp[8]), 18);
tmp[9] ^= Rotate(Add(tmp[5], tmp[1]), 7);
tmp[13] ^= Rotate(Add(tmp[9], tmp[5]), 9);
tmp[1] ^= Rotate(Add(tmp[13], tmp[9]), 13);
tmp[5] ^= Rotate(Add(tmp[1], tmp[13]), 18);
tmp[14] ^= Rotate(Add(tmp[10], tmp[6]), 7);
tmp[2] ^= Rotate(Add(tmp[14], tmp[10]), 9);
tmp[6] ^= Rotate(Add(tmp[2], tmp[14]), 13);
tmp[10] ^= Rotate(Add(tmp[6], tmp[2]), 18);
tmp[3] ^= Rotate(Add(tmp[15], tmp[11]), 7);
tmp[7] ^= Rotate(Add(tmp[3], tmp[15]), 9);
tmp[11] ^= Rotate(Add(tmp[7], tmp[3]), 13);
tmp[15] ^= Rotate(Add(tmp[11], tmp[7]), 18);
tmp[1] ^= Rotate(Add(tmp[0], tmp[3]), 7);
tmp[2] ^= Rotate(Add(tmp[1], tmp[0]), 9);
tmp[3] ^= Rotate(Add(tmp[2], tmp[1]), 13);
tmp[0] ^= Rotate(Add(tmp[3], tmp[2]), 18);
tmp[6] ^= Rotate(Add(tmp[5], tmp[4]), 7);
tmp[7] ^= Rotate(Add(tmp[6], tmp[5]), 9);
tmp[4] ^= Rotate(Add(tmp[7], tmp[6]), 13);
tmp[5] ^= Rotate(Add(tmp[4], tmp[7]), 18);
tmp[11] ^= Rotate(Add(tmp[10], tmp[9]), 7);
tmp[8] ^= Rotate(Add(tmp[11], tmp[10]), 9);
tmp[9] ^= Rotate(Add(tmp[8], tmp[11]), 13);
tmp[10] ^= Rotate(Add(tmp[9], tmp[8]), 18);
tmp[12] ^= Rotate(Add(tmp[15], tmp[14]), 7);
tmp[13] ^= Rotate(Add(tmp[12], tmp[15]), 9);
tmp[14] ^= Rotate(Add(tmp[13], tmp[12]), 13);
tmp[15] ^= Rotate(Add(tmp[14], tmp[13]), 18);
}
for (int i = 0; i < 16; i++) {
ToBytes(Add(tmp[i], input[i]), output, 4 * i);
}
}
/// <summary>
/// n-bit left rotation operation (towards the high bits) for 32-bit
/// integers.
/// </summary>
/// <param name="v"></param>
/// <param name="c"></param>
/// <returns>The result of (v LEFTSHIFT c)</returns>
private static uint Rotate(uint v, int c) {
return (v << c) | (v >> (32 - c));
}
/// <summary>
/// Unchecked integer addition. The Salsa spec defines certain operations
/// to use 32-bit unsigned integer addition modulo 2^32.
/// </summary>
/// <param name="v"></param>
/// <param name="w"></param>
/// <returns>The result of (v + w) modulo 2^32</returns>
private static uint Add(uint v, uint w) {
return unchecked(v + w);
}
/// <summary>
/// Add 1 to the input parameter using unchecked integer addition. The
/// Salsa spec defines certain operations to use 32-bit unsigned integer
/// addition modulo 2^32.
/// </summary>
/// <param name="v"></param>
/// <returns>The result of (v + 1) modulo 2^32</returns>
private static uint AddOne(uint v) {
return unchecked(v + 1);
}
/// <summary>
/// Convert four bytes of the input buffer into an unsigned
/// 32-bit integer, beginning at the inputOffset.
/// </summary>
/// <param name="p"></param>
/// <param name="inputOffset"></param>
/// <returns>An unsigned 32-bit integer</returns>
private static uint ToUInt32(byte[] input, int inputOffset) {
unchecked {
return (uint) (((input[inputOffset] |
(input[inputOffset + 1] << 8)) |
(input[inputOffset + 2] << 16)) |
(input[inputOffset + 3] << 24));
}
}
/// <summary>
/// Serialize the input integer into the output buffer. The input integer
/// will be split into 4 bytes and put into four sequential places in the
/// output buffer, starting at the outputOffset.
/// </summary>
/// <param name="output"></param>
/// <param name="input"></param>
/// <param name="outputOffset"></param>
private static void ToBytes(uint input, byte[] output, int outputOffset) {
unchecked {
output[outputOffset] = (byte) input;
output[outputOffset + 1] = (byte) (input >> 8);
output[outputOffset + 2] = (byte) (input >> 16);
output[outputOffset + 3] = (byte) (input >> 24);
}
}
}
}