-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Trivial.cs
436 lines (384 loc) · 14 KB
/
Trivial.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Linq;
using Xunit;
namespace System.Security.Cryptography.Tests.Symmetric
{
public static class TrivialTests
{
[Theory]
[InlineData(-1)]
[InlineData(0)]
[InlineData(7)]
[InlineData(48)]
public static void TestBadFeedbackSize(int size)
{
using (var t = new Trivial { BlockSize = 40 })
{
Assert.Throws<CryptographicException>(() => t.FeedbackSize = size);
}
}
[Fact]
public static void TestFeedbackSizeDoesntErrorWhenBlockSizeChanged()
{
using (var t = new Trivial { BlockSize = 104 })
{
t.FeedbackSize = 96;
t.BlockSize = 40;
Assert.Equal(96, t.FeedbackSize);
}
}
[Fact]
public static void TestFeedbackSizeDoesntValidateFieldValue()
{
using (var t = new Trivial())
{
t.SetFeedbackSize(9);
Assert.Equal(9, t.FeedbackSize);
}
}
[Fact]
public static void TestClearIsDispose()
{
// The obvious test would have been to .Clear() and then check that
// next time we get a different autogenerated Key and IV -- but
// occasionally by chance they would be the same.
using (Trivial t = new Trivial())
{
var k = t.Key; // Initialize
Assert.False(t.IsDisposed);
t.Clear();
Assert.True(t.IsDisposed);
}
}
[Fact]
public static void TestAutomaticKey()
{
using (Trivial t = new Trivial())
{
byte[] generatedKey = t.Key;
Assert.Equal(generatedKey, Trivial.GeneratedKey);
Assert.NotSame(generatedKey, Trivial.GeneratedKey);
}
}
[Fact]
public static void TestKey()
{
using (Trivial s = new Trivial())
{
Assert.Equal(0, s.KeySize);
Assert.Throws<ArgumentNullException>(() => s.Key = null);
// Testing KeySize and Key setter.
int[] validKeySizes = { 40, 104, 152, 808, 816, 824, 832 };
for (int keySize = -10; keySize < 200 * 8; keySize++)
{
if (validKeySizes.Contains(keySize))
{
s.KeySize = keySize;
Assert.Equal(keySize, s.KeySize);
}
else
{
Assert.Throws<CryptographicException>(() => s.KeySize = keySize);
}
if (keySize >= 0)
{
int keySizeInBytes = keySize / 8;
byte[] key = GenerateRandom(keySizeInBytes);
if (validKeySizes.Contains(keySizeInBytes * 8))
{
s.SetKeySize(-1);
s.Key = key;
byte[] copyOfKey = s.Key;
Assert.Equal(key, copyOfKey);
Assert.Equal(key.Length * 8, s.KeySize);
Assert.NotSame(key, copyOfKey);
}
else
{
Assert.Throws<CryptographicException>(() => s.Key = key);
}
}
}
// Test overflow
try
{
byte[] hugeKey = new byte[536870917]; // value chosen so that when multiplied by 8 (bits) it overflows to the value 40
Assert.Throws<CryptographicException>(() => s.Key = hugeKey);
}
catch (OutOfMemoryException) { } // in case there isn't enough memory at test-time to allocate the large array
}
}
[Fact]
public static void KeySize_Key_LooseCoupling()
{
using (Trivial t = new Trivial())
{
t.GenerateKey();
// Set the KeySizeValue field, then confirm that get_Key doesn't check it at all.
const int UnusualKeySize = 51;
t.SetKeySize(UnusualKeySize);
Assert.Equal(UnusualKeySize, t.KeySize);
byte[] key = t.Key;
// It doesn't equal it in bytes
Assert.NotEqual(UnusualKeySize, key.Length);
// It doesn't equal it in bits
Assert.NotEqual(UnusualKeySize, key.Length * 8);
}
}
[Fact]
public static void KeySize_CurrentValue_NotGrandfathered()
{
using (Trivial t = new Trivial())
{
t.SetKeySize(525600);
Assert.Throws<CryptographicException>(() => t.KeySize = t.KeySize);
}
}
[Fact]
public static void TestAutomaticIv()
{
using (Trivial t = new Trivial())
{
t.BlockSize = 5 * 8;
byte[] generatedIv = t.IV;
Assert.Equal(generatedIv, Trivial.GeneratedIV);
Assert.NotSame(generatedIv, Trivial.GeneratedIV);
}
}
[Fact]
public static void TestIv()
{
using (Trivial s = new Trivial())
{
Assert.Throws<ArgumentNullException>(() => s.IV = null);
// Testing IV property setter
{
s.BlockSize = 5 * 8;
{
byte[] iv = GenerateRandom(5);
s.IV = iv;
byte[] copyOfIv = s.IV;
Assert.Equal(iv, copyOfIv);
Assert.False(object.ReferenceEquals(iv, copyOfIv));
}
{
byte[] iv = GenerateRandom(6);
Assert.Throws<CryptographicException>(() => s.IV = iv);
}
}
}
}
[Fact]
public static void TestBlockSize()
{
using (Trivial s = new Trivial())
{
Assert.Equal(0, s.BlockSize);
// Testing BlockSizeSetter.
int[] validBlockSizes = { 40, 104, 152, 808, 816, 824, 832 };
for (int blockSize = -10; blockSize < 200 * 8; blockSize++)
{
if (validBlockSizes.Contains(blockSize))
{
s.BlockSize = blockSize;
Assert.Equal(blockSize, s.BlockSize);
}
else
{
Assert.Throws<CryptographicException>(() => s.BlockSize = blockSize);
}
}
}
return;
}
[Fact]
public static void GetCipherMode_NoValidation()
{
using (Trivial t = new Trivial())
{
t.SetCipherMode(24601);
Assert.Equal(24601, (int)t.Mode);
}
}
[Fact]
public static void GetPaddingMode_NoValidation()
{
using (Trivial t = new Trivial())
{
t.SetPaddingMode(24601);
Assert.Equal(24601, (int)t.Padding);
}
}
[Fact]
public static void LegalBlockSizes_CopiesData()
{
using (Trivial t = new Trivial())
{
KeySizes[] a = t.LegalBlockSizes;
KeySizes[] b = t.LegalBlockSizes;
Assert.NotSame(a, b);
}
}
[Fact]
public static void LegalKeySizes_CopiesData()
{
using (Trivial t = new Trivial())
{
KeySizes[] a = t.LegalKeySizes;
KeySizes[] b = t.LegalKeySizes;
Assert.NotSame(a, b);
}
}
[Fact]
public static void SetKey_Uses_LegalKeySizesProperty()
{
using (SymmetricAlgorithm s = new DoesNotSetKeySizesFields())
{
Assert.Throws<CryptographicException>(() => s.Key = Array.Empty<byte>());
s.Key = new byte[16];
}
}
[Fact]
public static void SetKeySize_Uses_LegalKeySizesProperty()
{
using (SymmetricAlgorithm s = new DoesNotSetKeySizesFields())
{
Assert.Throws<CryptographicException>(() => s.KeySize = 0);
s.KeySize = 128;
}
}
[Fact]
public static void SetBlockSize_Uses_LegalBlockSizesProperty()
{
using (SymmetricAlgorithm s = new DoesNotSetKeySizesFields())
{
Assert.Throws<CryptographicException>(() => s.BlockSize = 0);
s.BlockSize = 8;
}
}
private static byte[] GenerateRandom(int size)
{
byte[] data = new byte[size];
Random r = new Random();
for (int i = 0; i < size; i++)
{
data[i] = unchecked((byte)(r.Next()));
}
return data;
}
private class Trivial : SymmetricAlgorithm
{
public Trivial()
{
// Desktop's SymmetricAlgorithm reads from the field overly aggressively,
// but in Core it always reads from the property. By setting the field
// we're still happy on Desktop tests, and we can validate the default
// behavior of the LegalBlockSizes property.
LegalBlockSizesValue = new KeySizes[]
{
new KeySizes(5*8, -99*8, 0*8),
new KeySizes(13*8, 22*8, 6*8),
new KeySizes(101*8, 104*8, 1*8),
};
// Desktop's SymmetricAlgorithm reads from the property correctly, but
// we'll set the field here, anyways, to validate the default behavior
// of the LegalKeySizes property.
LegalKeySizesValue = new KeySizes[]
{
new KeySizes(5*8, -99*8, 0*8),
new KeySizes(13*8, 22*8, 6*8),
new KeySizes(101*8, 104*8, 1*8),
};
}
public bool IsDisposed
{
get
{
return KeyValue == null && IVValue == null;
}
}
public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
{
throw new CreateDecryptorNotImplementedException();
}
public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
{
throw new CreateEncryptorNotImplementedException();
}
public override void GenerateIV()
{
IV = GeneratedIV;
}
public override void GenerateKey()
{
Key = GeneratedKey;
}
public void SetBlockSize(int blockSize)
{
BlockSizeValue = blockSize;
}
public void SetKeySize(int keySize)
{
KeySizeValue = keySize;
}
public void SetCipherMode(int anyValue)
{
ModeValue = (CipherMode)anyValue;
}
public void SetPaddingMode(int anyValue)
{
PaddingValue = (PaddingMode)anyValue;
}
public void SetFeedbackSize(int value)
{
FeedbackSizeValue = value;
}
public static readonly byte[] GeneratedKey = GenerateRandom(13);
public static readonly byte[] GeneratedIV = GenerateRandom(5);
}
private class DoesNotSetKeySizesFields : SymmetricAlgorithm
{
public DoesNotSetKeySizesFields()
{
// Ensure the default values for the fields.
Assert.Null(KeyValue);
Assert.Null(IVValue);
Assert.Null(LegalKeySizesValue);
Assert.Null(LegalBlockSizesValue);
Assert.Equal(0, KeySizeValue);
Assert.Equal(0, BlockSizeValue);
Assert.Equal(CipherMode.CBC, ModeValue);
Assert.Equal(PaddingMode.PKCS7, PaddingValue);
}
public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
{
throw new CreateDecryptorNotImplementedException();
}
public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
{
throw new CreateEncryptorNotImplementedException();
}
public override void GenerateIV()
{
throw new GenerateIvNotImplementedException();
}
public override void GenerateKey()
{
throw new GenerateKeyNotImplementedException();
}
public override KeySizes[] LegalBlockSizes
{
get { return new[] { new KeySizes(8, 64, 8) }; }
}
public override KeySizes[] LegalKeySizes
{
get { return new[] { new KeySizes(64, 128, 8) }; }
}
}
private class GenerateIvNotImplementedException : Exception { }
private class GenerateKeyNotImplementedException : Exception { }
private class CreateDecryptorNotImplementedException : Exception { }
private class CreateEncryptorNotImplementedException : Exception { }
}
}