1 //
2 // TripleDESTest.cs - NUnit Test Cases for TripleDES
3 //
4 // Author:
5 //	Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 
29 using NUnit.Framework;
30 using System;
31 using System.Security.Cryptography;
32 
33 namespace MonoTests.System.Security.Cryptography {
34 
35 	[TestFixture]
36 	public class TripleDESTest {
37 
38 		[Test]
DefaultProperties()39 		public void DefaultProperties ()
40 		{
41 			TripleDES algo = TripleDES.Create ();
42 			Assert.AreEqual (64, algo.BlockSize, "BlockSize");
43 			Assert.AreEqual (8, algo.FeedbackSize, "FeedbackSize");
44 			Assert.AreEqual (192, algo.KeySize, "KeySize");
45 			Assert.AreEqual (CipherMode.CBC, algo.Mode, "Mode");
46 			Assert.AreEqual (PaddingMode.PKCS7, algo.Padding, "Padding");
47 			Assert.AreEqual (1, algo.LegalBlockSizes.Length, "LegalBlockSizes");
48 			Assert.AreEqual (64, algo.LegalBlockSizes [0].MaxSize, "LegalBlockSizes.MaxSize");
49 			Assert.AreEqual (64, algo.LegalBlockSizes [0].MinSize, "LegalBlockSizes.MinSize");
50 			Assert.AreEqual (0, algo.LegalBlockSizes [0].SkipSize, "LegalBlockSizes.SkipSize");
51 			Assert.AreEqual (1, algo.LegalKeySizes.Length, "LegalKeySizes");
52 			Assert.AreEqual (192, algo.LegalKeySizes [0].MaxSize, "LegalKeySizes.MaxSize");
53 			Assert.AreEqual (128, algo.LegalKeySizes [0].MinSize, "LegalKeySizes.MinSize");
54 			Assert.AreEqual (64, algo.LegalKeySizes [0].SkipSize, "LegalKeySizes.SkipSize");
55 		}
56 
57 		[Test]
Key()58 		public void Key ()
59 		{
60 			TripleDES algo = TripleDES.Create ();
61 			algo.GenerateKey ();
62 			algo.GenerateIV ();
63 			Assert.AreEqual (192, algo.KeySize, "Key Size");
64 			Assert.AreEqual (24, algo.Key.Length, "Key Length");
65 			Assert.AreEqual (8, algo.IV.Length, "IV Length");
66 		}
67 
68 		[Test]
69 		[ExpectedException (typeof (ArgumentNullException))]
KeyNull()70 		public void KeyNull ()
71 		{
72 			TripleDES algo = TripleDES.Create ();
73 			algo.Key = null;
74 		}
75 
76 		[Test]
77 		[ExpectedException (typeof (CryptographicException))]
KeyWeak128bits()78 		public void KeyWeak128bits ()
79 		{
80 			byte[] wk128 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
81 			TripleDES algo = TripleDES.Create ();
82 			algo.Key = wk128;
83 		}
84 
85 		[Test]
86 		[ExpectedException (typeof (CryptographicException))]
KeyWeak192bits_AB()87 		public void KeyWeak192bits_AB ()
88 		{
89 			byte[] wk192 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD };
90 			TripleDES algo = TripleDES.Create ();
91 			algo.Key = wk192;
92 		}
93 
94 		[Test]
95 		[ExpectedException (typeof (CryptographicException))]
KeyWeak192bits_BC()96 		public void KeyWeak192bits_BC ()
97 		{
98 			byte[] wk192 = { 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
99 			TripleDES algo = TripleDES.Create ();
100 			algo.Key = wk192;
101 		}
102 
103 		[Test]
104 		[ExpectedException (typeof (CryptographicException))]
KeyWrongLength()105 		public void KeyWrongLength ()
106 		{
107 			byte[] wk64 = { 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD };
108 			TripleDES algo = TripleDES.Create ();
109 			algo.Key = wk64;
110 		}
111 
112 		[Test]
UseOneDesWeakKeyIn128bitsKey()113 		public void UseOneDesWeakKeyIn128bitsKey ()
114 		{
115 			// the first 8 bytes are a DES weak key
116 			byte[] wk128 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
117 			TripleDES algo = TripleDES.Create ();
118 			algo.Key = wk128;
119 		}
120 
121 		[Test]
UseOneDesWeakKeyIn192bitsKey()122 		public void UseOneDesWeakKeyIn192bitsKey ()
123 		{
124 			// the bytes 8-16 are a DES weak key
125 			byte[] wk192 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
126 			TripleDES algo = TripleDES.Create ();
127 			algo.Key = wk192;
128 		}
129 
130 		[Test]
131 		[ExpectedException (typeof (CryptographicException))]
IsSemiWeakKey_Null()132 		public void IsSemiWeakKey_Null ()
133 		{
134 			TripleDES.IsWeakKey (null);
135 		}
136 	}
137 }
138