2004-03-28 Sebastien Pouliot <sebastien@ximian.com>
authorSebastien Pouliot <sebastien@ximian.com>
Wed, 28 Apr 2004 12:54:53 +0000 (12:54 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Wed, 28 Apr 2004 12:54:53 +0000 (12:54 -0000)
* DESCryptoServiceProviderTest.cs: New. Completes coverage for
DESCryptoServiceProvider class. Calls the FIPS 81 tests for this
implementation.
* DESTest.cs: Added test vectors from FIPS 81.

svn path=/trunk/mcs/; revision=26142

mcs/class/corlib/Test/System.Security.Cryptography/ChangeLog
mcs/class/corlib/Test/System.Security.Cryptography/DESCryptoServiceProviderTest.cs [new file with mode: 0755]
mcs/class/corlib/Test/System.Security.Cryptography/DESTest.cs

index eb4f314a7597acfe493d1f33415bc02efd1717e3..13615dc0b766500d99ece984521d9a66c7546515 100644 (file)
@@ -1,3 +1,10 @@
+2004-03-28  Sebastien Pouliot  <sebastien@ximian.com>
+
+       * DESCryptoServiceProviderTest.cs: New. Completes coverage for 
+       DESCryptoServiceProvider class. Calls the FIPS 81 tests for this
+       implementation.
+       * DESTest.cs: Added test vectors from FIPS 81.
+
 2004-03-26  Sebastien Pouliot  <sebastien@ximian.com>
 
        * DESTest.cs: New. Unit tests for DES including checks for weak and
diff --git a/mcs/class/corlib/Test/System.Security.Cryptography/DESCryptoServiceProviderTest.cs b/mcs/class/corlib/Test/System.Security.Cryptography/DESCryptoServiceProviderTest.cs
new file mode 100755 (executable)
index 0000000..fa9f6df
--- /dev/null
@@ -0,0 +1,45 @@
+//
+// TestSuite.System.Security.Cryptography.DESCryptoServiceProviderTest.cs
+//
+// Author:
+//      Sebastien Pouliot  <sebastien@ximian.com>
+//
+// (C) 2004 Novell (http://www.novell.com)
+//
+
+using System;
+using System.Security.Cryptography;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Security.Cryptography {
+
+       [TestFixture]
+       public class DESCryptoServiceProviderTest : DESFIPS81Test {
+
+               [SetUp]
+               void Create () 
+               {
+                       des = new DESCryptoServiceProvider ();
+               }
+
+               [Test]
+               public void KeyChecks () 
+               {
+                       byte[] key = des.Key;
+                       AssertEquals ("Key", 8, key.Length);
+                       Assert ("IsWeakKey", !DES.IsWeakKey (key));
+                       Assert ("IsSemiWeakKey", !DES.IsSemiWeakKey (key));
+               }
+
+               [Test]
+               public void IV () 
+               {
+                       byte[] iv = des.IV;
+                       AssertEquals ("IV", 8, iv.Length);
+               }
+
+               // other tests (test vectors) are inherited from DESFIPS81Test
+               // (in DESTest.cs) but executed here
+       }
+}
index 01710558cd35fb55e741c88eb274363e9abf8704..8a3cf1a37be8193ee3ccfe297834add0d0a6f8fb 100755 (executable)
@@ -1,5 +1,5 @@
 //
-// TestSuite.System.Security.Cryptography.AsymmetricAlgorithmTest.cs
+// TestSuite.System.Security.Cryptography.DESTest.cs
 //
 // Author:
 //      Sebastien Pouliot  <sebastien@ximian.com>
@@ -9,6 +9,7 @@
 
 using System;
 using System.Security.Cryptography;
+using System.Text;
 
 using NUnit.Framework;
 
@@ -181,4 +182,113 @@ namespace MonoTests.System.Security.Cryptography {
                        des.Key = swk1;
                }
        }
+
+       // Test vectors from FIPS 81 - DES Modes of Operations
+       // http://csrc.nist.gov/publications/fips/fips81/fips81.htm
+       //
+       // Note: they are to be called from specifics implementations -
+       //   not for the abstract DES. Thats why they are in a separate class
+       //   which doesn't have a [TestFixture] attribute
+       public class DESFIPS81Test : Assertion {
+               protected DES des;
+
+               // Table B1 - ECB Mode
+               [Test]
+               public void FIPS81_ECBMode () 
+               {
+                       byte[] plaintext = Encoding.ASCII.GetBytes ("Now is the time for all ");
+                       byte[] result = new byte [24];
+
+                       des.Key = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
+                       des.Mode = CipherMode.ECB;
+                       des.Padding = PaddingMode.None;
+                       ICryptoTransform encrypt = des.CreateEncryptor ();
+
+                       encrypt.TransformBlock (plaintext, 0, 8, result, 0);
+                       AssertEquals ("Encrypt Block 1", "3F-A4-0E-8A-98-4D-48-15", BitConverter.ToString (result, 0, 8));
+
+                       encrypt.TransformBlock (plaintext, 8, 8, result, 8);
+                       AssertEquals ("Encrypt Block 2", "6A-27-17-87-AB-88-83-F9", BitConverter.ToString (result, 8, 8));
+
+                       encrypt.TransformBlock (plaintext, 16, 8, result, 16);
+                       AssertEquals ("Encrypt Block 3", "89-3D-51-EC-4B-56-3B-53", BitConverter.ToString (result, 16, 8));
+
+                       ICryptoTransform decrypt = des.CreateDecryptor ();
+                       
+                       byte[] decrypted = new byte [24]; // MS cannot *always* reuse buffers
+                       decrypt.TransformBlock (result, 0, 8, decrypted, 0);
+                       AssertEquals ("Decrypt Block 1", "Now is t", Encoding.ASCII.GetString (decrypted, 0, 8));
+
+                       decrypt.TransformBlock (result, 8, 8, decrypted, 8);
+                       AssertEquals ("Decrypt Block 2", "he time ", Encoding.ASCII.GetString (decrypted, 8, 8));
+
+                       decrypt.TransformBlock (result, 16, 8, decrypted, 16);
+                       AssertEquals ("Decrypt Block 3", "for all ", Encoding.ASCII.GetString (decrypted, 16, 8));
+               }
+
+               // Table C1 - CBC Mode
+               [Test]
+               public void FIPS81_CBCMode () 
+               {
+                       byte[] plaintext = Encoding.ASCII.GetBytes ("Now is the time for all ");
+                       byte[] result = new byte [24];
+
+                       des.Key = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
+                       des.IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
+                       des.Mode = CipherMode.CBC;
+                       des.Padding = PaddingMode.None;
+                       ICryptoTransform encrypt = des.CreateEncryptor ();
+
+                       encrypt.TransformBlock (plaintext, 0, 8, result, 0);
+                       AssertEquals ("Encrypt Block 1", "E5-C7-CD-DE-87-2B-F2-7C", BitConverter.ToString (result, 0, 8));
+
+                       encrypt.TransformBlock (plaintext, 8, 8, result, 8);
+                       AssertEquals ("Encrypt Block 2", "43-E9-34-00-8C-38-9C-0F", BitConverter.ToString (result, 8, 8));
+
+                       byte[] final = encrypt.TransformFinalBlock (plaintext, 16, 8);
+                       Buffer.BlockCopy (final, 0, result, 16, 8);
+                       AssertEquals ("Encrypt Block 3", "68-37-88-49-9A-7C-05-F6", BitConverter.ToString (result, 16, 8));
+
+                       ICryptoTransform decrypt = des.CreateDecryptor ();
+                       
+                       decrypt.TransformBlock (result, 0, 8, result, 0);
+                       AssertEquals ("Decrypt Block 1", "Now is t", Encoding.ASCII.GetString (result, 0, 8));
+
+                       decrypt.TransformBlock (result, 8, 8, result, 8);
+                       AssertEquals ("Decrypt Block 2", "he time ", Encoding.ASCII.GetString (result, 8, 8));
+
+                       final = decrypt.TransformFinalBlock (result, 16, 8);
+                       AssertEquals ("Decrypt Block 3", "for all ", Encoding.ASCII.GetString (final));
+               }
+
+               // Table D2 - CFB Mode 8 bits
+               [Test]
+               public void FIPS81_CFB8Mode () 
+               {
+                       byte[] plaintext = Encoding.ASCII.GetBytes ("Now is theXXXXXX"); // padding
+                       byte[] result = new byte [16];
+
+                       des.Key = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
+                       des.IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
+                       des.Mode = CipherMode.CFB;
+                       des.Padding = PaddingMode.None;
+                       des.FeedbackSize = 8;
+                       ICryptoTransform encrypt = des.CreateEncryptor ();
+
+                       encrypt.TransformBlock (plaintext, 0, 8, result, 0);
+                       AssertEquals ("Encrypt Block 1", "F3-1F-DA-07-01-14-62-EE", BitConverter.ToString (result, 0, 8));
+
+                       byte[] final = encrypt.TransformFinalBlock (plaintext, 8, 8);
+                       Buffer.BlockCopy (final, 0, result, 8, 8);
+                       AssertEquals ("Encrypt Block 2", "18-7F", BitConverter.ToString (final).Substring (0, 5));
+
+                       ICryptoTransform decrypt = des.CreateDecryptor ();
+                       
+                       decrypt.TransformBlock (result, 0, 8, result, 0);
+                       AssertEquals ("Decrypt Block 1", "Now is t", Encoding.ASCII.GetString (result, 0, 8));
+
+                       final = decrypt.TransformFinalBlock (result, 8, 8);
+                       AssertEquals ("Decrypt Block 2", "he", Encoding.ASCII.GetString (final, 0, 2));
+               }
+       }
 }