dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/tests/runtime/System/Security/Cryptog


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/tests/runtime/System/Security/Cryptography CryptoTestCase.cs,1.1,1.2 TestAES.cs,1.1,1.2 TestDES.cs,1.1,1.2 TestRC2.cs,1.1,1.2 TestTripleDES.cs,1.1,1.2
Date: Mon, 25 Nov 2002 18:14:19 -0500

Update of 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Security/Cryptography
In directory 
subversions:/tmp/cvs-serv32211/tests/runtime/System/Security/Cryptography

Modified Files:
        CryptoTestCase.cs TestAES.cs TestDES.cs TestRC2.cs 
        TestTripleDES.cs 
Log Message:


Test the properties of symmetric cryptographic objects and
for weak DES keys.


Index: CryptoTestCase.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Security/Cryptography/CryptoTestCase.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** CryptoTestCase.cs   22 Nov 2002 00:17:48 -0000      1.1
--- CryptoTestCase.cs   25 Nov 2002 23:14:17 -0000      1.2
***************
*** 204,206 ****
--- 204,252 ----
                        }
  
+       // Check that a size value is in a size list.
+       private void CheckSize(String msg, KeySizes[] sizes, int value)
+                       {
+                               foreach(KeySizes size in sizes)
+                               {
+                                       if(value >= size.MinSize && value <= 
size.MaxSize &&
+                                          ((value - size.MinSize) % 
size.SkipSize) == 0)
+                                       {
+                                               return;
+                                       }
+                               }
+                               Fail(msg);
+                       }
+ 
+       // Test the properties on a symmetric algorithm instance.
+       protected void SymmetricPropertyTest(SymmetricAlgorithm alg,
+                                                                               
 int expectedKeySize,
+                                                                               
 int expectedBlockSize)
+                       {
+                               // Check the initial property values.
+                               AssertEquals("initial key size is incorrect",
+                                                        alg.KeySize, 
expectedKeySize);
+                               AssertEquals("initial block size is incorrect",
+                                                        alg.BlockSize, 
expectedBlockSize);
+                               AssertEquals("initial feedback block size is 
incorrect",
+                                                        alg.FeedbackSize, 
expectedBlockSize);
+                               AssertEquals("initial cipher mode is incorrect",
+                                                        alg.Mode, 
CipherMode.CBC);
+                               AssertEquals("initial padding mode is 
incorrect",
+                                                        alg.Padding, 
PaddingMode.PKCS7);
+                               AssertNotNull("legal key size array is null",
+                                                         alg.LegalKeySizes);
+                               AssertNotNull("legal block size array is null",
+                                                         alg.LegalBlockSizes);
+ 
+                               // Check that the size values are initially 
valid.
+                               CheckSize("initial key size is not legal",
+                                                 alg.LegalKeySizes, 
alg.KeySize);
+                               CheckSize("initial block size is not legal",
+                                                 alg.LegalBlockSizes, 
alg.BlockSize);
+ 
+                               // Try setting the key size to all legal values.
+ 
+                               // TODO: check automatic key and IV generation
+                       }
+ 
  }; // CryptoTestCase

Index: TestAES.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Security/Cryptography/TestAES.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** TestAES.cs  22 Nov 2002 00:17:48 -0000      1.1
--- TestAES.cs  25 Nov 2002 23:14:17 -0000      1.2
***************
*** 112,114 ****
--- 112,120 ----
                        }
  
+       // Test the properties of the default algorithm instance.
+       public void TestAESProperties()
+                       {
+                               SymmetricPropertyTest(Rijndael.Create(), 128, 
128);
+                       }
+ 
  }; // TestAES

Index: TestDES.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Security/Cryptography/TestDES.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** TestDES.cs  22 Nov 2002 00:17:48 -0000      1.1
--- TestDES.cs  25 Nov 2002 23:14:17 -0000      1.2
***************
*** 225,228 ****
--- 225,248 ----
        public void TestDESWeak()
                        {
+                               // Test exception behaviour of "DES.IsWeakKey".
+                               try
+                               {
+                                       DES.IsWeakKey(null);
+                                       Fail("null");
+                               }
+                               catch(CryptographicException)
+                               {
+                                       // success
+                               }
+                               try
+                               {
+                                       DES.IsWeakKey(new byte [0]);
+                                       Fail("wrong size");
+                               }
+                               catch(CryptographicException)
+                               {
+                                       // success
+                               }
+ 
                                // These keys are weak.
                                CheckWeak("weak1", new byte[]
***************
*** 245,248 ****
--- 265,288 ----
        public void TestDESSemiWeak()
                        {
+                               // Test exception behaviour of 
"DES.IsSemiWeakKey".
+                               try
+                               {
+                                       DES.IsSemiWeakKey(null);
+                                       Fail("null");
+                               }
+                               catch(CryptographicException)
+                               {
+                                       // success
+                               }
+                               try
+                               {
+                                       DES.IsSemiWeakKey(new byte [0]);
+                                       Fail("wrong size");
+                               }
+                               catch(CryptographicException)
+                               {
+                                       // success
+                               }
+ 
                                // These keys are semi-weak.
                                CheckSemiWeak("semi1", new byte[]
***************
*** 278,281 ****
--- 318,327 ----
                                CheckNonSemiWeak("semi14", new byte[]
                                        {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 
0xcd, 0xef});
+                       }
+ 
+       // Test the properties of the default algorithm instance.
+       public void TestDESProperties()
+                       {
+                               SymmetricPropertyTest(DES.Create(), 64, 64);
                        }
  

Index: TestRC2.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Security/Cryptography/TestRC2.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** TestRC2.cs  22 Nov 2002 00:17:48 -0000      1.1
--- TestRC2.cs  25 Nov 2002 23:14:17 -0000      1.2
***************
*** 71,73 ****
--- 71,79 ----
                        }
  
+       // Test the properties of the default algorithm instance.
+       public void TestRC2Properties()
+                       {
+                               SymmetricPropertyTest(RC2.Create(), 128, 64);
+                       }
+ 
  }; // TestRC2

Index: TestTripleDES.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Security/Cryptography/TestTripleDES.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** TestTripleDES.cs    22 Nov 2002 00:17:48 -0000      1.1
--- TestTripleDES.cs    25 Nov 2002 23:14:17 -0000      1.2
***************
*** 171,173 ****
--- 171,298 ----
                        }
  
+       // Test the properties of the default algorithm instance.
+       public void TestTripleDESProperties()
+                       {
+                               SymmetricPropertyTest(TripleDES.Create(), 192, 
64);
+                       }
+ 
+       // Check that a key is weak.
+       private void CheckWeak(String name, byte[] key)
+                       {
+                               if(!TripleDES.IsWeakKey(key))
+                               {
+                                       Fail(name + ": key wasn't recognized as 
weak");
+                               }
+                               TripleDES alg = TripleDES.Create();
+                               try
+                               {
+                                       alg.Key = key;
+                                       Fail(name + ": key was supposed to be 
detected as weak");
+                               }
+                               catch(CryptographicException)
+                               {
+                                       // Success
+                               }
+                       }
+ 
+       // Check that a key is non-weak.
+       private void CheckNonWeak(String name, byte[] key)
+                       {
+                               CheckNonWeak(name, key, false);
+                       }
+       private void CheckNonWeak(String name, byte[] key, bool suppressCreate)
+                       {
+                               if(TripleDES.IsWeakKey(key))
+                               {
+                                       Fail(name + "key was recognized as 
weak");
+                               }
+                               if(!suppressCreate)
+                               {
+                                       TripleDES alg = TripleDES.Create();
+                                       try
+                                       {
+                                               alg.Key = key;
+                                       }
+                                       catch(CryptographicException)
+                                       {
+                                               Fail(name +
+                                                        ": key was not 
supposed to be detected as weak");
+                                       }
+                               }
+                       }
+ 
+       // Test for weak keys.
+       public void TestTripleDESWeak()
+                       {
+                               // Test exception behaviour of 
"TripleDES.IsWeakKey".
+                               try
+                               {
+                                       TripleDES.IsWeakKey(null);
+                                       Fail("null");
+                               }
+                               catch(CryptographicException)
+                               {
+                                       // success
+                               }
+                               try
+                               {
+                                       TripleDES.IsWeakKey(new byte [0]);
+                                       Fail("wrong size");
+                               }
+                               catch(CryptographicException)
+                               {
+                                       // success
+                               }
+ 
+                               // These keys are weak.
+                               CheckWeak("weak1", new byte[]
+                                       {0x01, 0x01, 0x01, 0x01,  0x01, 0x01, 
0x01, 0x01,
+                                        0x01, 0x01, 0x01, 0x01,  0x01, 0x01, 
0x01, 0x01});
+                               CheckWeak("weak2", new byte[]
+                                       {0x1F, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E,
+                                        0x1F, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E,
+                                        0x1F, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E});
+                               CheckWeak("weak3", new byte[]
+                                       {0x1F, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E,
+                                        0x1F, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E,
+                                        0x01, 0x01, 0x01, 0x01,  0x01, 0x01, 
0x01, 0x01});
+                               CheckWeak("weak4", new byte[]
+                                       {0x01, 0x01, 0x01, 0x01,  0x01, 0x01, 
0x01, 0x01,
+                                        0x1F, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E,
+                                        0x1F, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E});
+ 
+                               // Test weak keys whose components differ only 
in parity,
+                               // because parity bits aren't used in the key 
schedules.
+                               CheckWeak("weak5", new byte[]
+                                       {0x01, 0x00, 0x01, 0x00,  0x01, 0x00, 
0x01, 0x00,
+                                        0x00, 0x01, 0x00, 0x01,  0x00, 0x01, 
0x00, 0x01});
+                               CheckWeak("weak6", new byte[]
+                                       {0x1E, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E,
+                                        0x1F, 0x1E, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E,
+                                        0x1F, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E});
+ 
+                               // Test normal keys.
+                               CheckNonWeak("weak7", new byte[]
+                                       {0x01, 0x23, 0x45, 0x67,  0x89, 0xab, 
0xcd, 0xef,
+                                        0xef, 0xcd, 0xab, 0x89,  0x67, 0x45, 
0x23, 0x01});
+                               CheckNonWeak("weak8", new byte[]
+                                       {0x01, 0x23, 0x45, 0x67,  0x89, 0xab, 
0xcd, 0xef,
+                                        0xef, 0xcd, 0xab, 0x89,  0x67, 0x45, 
0x23, 0x01,
+                                        0x11, 0x22, 0x33, 0x44,  0x55, 0x66, 
0x77, 0x88});
+                               CheckNonWeak("weak9", new byte[]
+                                       {0x1F, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E,
+                                        0xef, 0xcd, 0xab, 0x89,  0x67, 0x45, 
0x23, 0x01,
+                                        0x1F, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E});
+ 
+                               // Test normal keys made up of weak DES 
components.
+                               // The Triple-DES form is not weak.
+                               CheckNonWeak("weak10", new byte[]
+                                       {0x01, 0x01, 0x01, 0x01,  0x01, 0x01, 
0x01, 0x01,
+                                        0x1F, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E});
+                               CheckNonWeak("weak11", new byte[]
+                                       {0x01, 0x01, 0x01, 0x01,  0x01, 0x01, 
0x01, 0x01,
+                                        0x1F, 0x1F, 0x1F, 0x1F,  0x0E, 0x0E, 
0x0E, 0x0E,
+                                        0xE0, 0xE0, 0xE0, 0xE0,  0xF1, 0xF1, 
0xF1, 0xF1});
+                       }
+ 
  }; // TestTripleDES





reply via email to

[Prev in Thread] Current Thread [Next in Thread]