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.7,1.8 TestAES.cs,1.2,1.3 TestDES.cs,1.3,1.4 TestRC2.cs,1.2,1.3 TestTripleDES.cs,1.3,1.4
Date: Thu, 28 Nov 2002 19:33:40 -0500

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

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


Test cases and bug fixes for the cipher modes.


Index: CryptoTestCase.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Security/Cryptography/CryptoTestCase.cs,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** CryptoTestCase.cs   27 Nov 2002 22:27:06 -0000      1.7
--- CryptoTestCase.cs   29 Nov 2002 00:33:38 -0000      1.8
***************
*** 346,350 ****
--- 346,356 ----
                        {
                                ICryptoTransform encryptor;
+                               CipherMode mode = alg.Mode;
+                               PaddingMode padding = alg.Padding;
+                               alg.Mode = CipherMode.ECB;
+                               alg.Padding = PaddingMode.None;
                                encryptor = alg.CreateEncryptor(key, null);
+                               alg.Mode = mode;
+                               alg.Padding = padding;
                                encryptor.TransformBlock(buf, index, 
alg.BlockSize / 8,
                                                                                
 buf, index);
***************
*** 366,369 ****
--- 372,385 ----
                        }
  
+       // XOR two blocks.
+       private void XorBlock(byte[] buf1, int index1, byte[] buf2,
+                                             int index2, int length)
+                       {
+                               while(length-- > 0)
+                               {
+                                       buf1[index1++] ^= buf2[index2++];
+                               }
+                       }
+ 
        // Copy one block to another.
        private void CopyBlock(byte[] src, int srcIndex,
***************
*** 508,513 ****
                                                 byte[] key, byte[] _iv)
                        {
!                               // TODO
!                               return input;
                        }
  
--- 524,547 ----
                                                 byte[] key, byte[] _iv)
                        {
!                               byte[] iv = new byte [_iv.Length];
!                               Array.Copy(_iv, 0, iv, 0, _iv.Length);
!                               byte[] output = new byte [input.Length];
!                               Array.Copy(input, 0, output, 0, input.Length);
!                               int size = alg.BlockSize / 8;
!                               int index = 0;
!                               while(index < input.Length)
!                               {
!                                       ECBBlock(iv, 0, alg, key);
!                                       if((input.Length - index) >= size)
!                                       {
!                                               XorBlock(output, index, iv, 0, 
alg);
!                                       }
!                                       else
!                                       {
!                                               XorBlock(output, index, iv, 0, 
input.Length - index);
!                                       }
!                                       index += size;
!                               }
!                               return output;
                        }
  
***************
*** 516,521 ****
                                                 byte[] key, byte[] _iv)
                        {
!                               // TODO
!                               return input;
                        }
  
--- 550,574 ----
                                                 byte[] key, byte[] _iv)
                        {
!                               byte[] iv = new byte [_iv.Length];
!                               Array.Copy(_iv, 0, iv, 0, _iv.Length);
!                               byte[] output = new byte [input.Length];
!                               Array.Copy(input, 0, output, 0, input.Length);
!                               int size = alg.BlockSize / 8;
!                               int index = 0;
!                               while(index < input.Length)
!                               {
!                                       ECBBlock(iv, 0, alg, key);
!                                       if((input.Length - index) >= size)
!                                       {
!                                               XorBlock(output, index, iv, 0, 
alg);
!                                               CopyBlock(output, index, iv, 0, 
alg);
!                                       }
!                                       else
!                                       {
!                                               XorBlock(output, index, iv, 0, 
input.Length - index);
!                                       }
!                                       index += size;
!                               }
!                               return output;
                        }
  
***************
*** 524,529 ****
                                                 byte[] key, byte[] _iv)
                        {
!                               // TODO
!                               return input;
                        }
  
--- 577,624 ----
                                                 byte[] key, byte[] _iv)
                        {
!                               if(input.Length < (alg.BlockSize / 8))
!                               {
!                                       // Streams shorter than one block are 
CFB-encrypted.
!                                       return DoCFB(input, alg, key, _iv);
!                               }
!                               byte[] iv = new byte [_iv.Length];
!                               Array.Copy(_iv, 0, iv, 0, _iv.Length);
!                               byte[] output = new byte [input.Length];
!                               int size = alg.BlockSize / 8;
!                               Array.Copy(input, 0, output, 0, input.Length);
!                               int index = 0;
!                               int limit = input.Length;
!                               limit -= limit % size;
!                               limit -= size;
! 
!                               // Encrypt the bulk of the input with CBC.
!                               while(index < limit)
!                               {
!                                       XorBlock(output, index, iv, 0, alg);
!                                       ECBBlock(output, index, alg, key);
!                                       CopyBlock(output, index, iv, 0, alg);
!                                       index += size;
!                               }
! 
!                               // Encrypt the last two blocks using ciphertext 
stealing.
!                               byte[] last = new byte [size * 2];
!                               Array.Copy(output, index, last, 0, input.Length 
- limit);
!                               XorBlock(last, 0, iv, 0, alg);
!                               ECBBlock(last, 0, alg, key);
!                               XorBlock(last, size, last, 0, alg);
!                               ECBBlock(last, size, alg, key);
!                               Array.Copy(last, size, output, index, size);
!                               Array.Copy(last, 0, output, index + size,
!                                                  input.Length % size);
!                               return output;
!                       }
! 
!       // Get a string that describes a particular cipher mode test,
!       // for use in error messages.
!       private static String GetError(String msg, SymmetricAlgorithm alg,
!                                                                  String input)
!                       {
!                               return msg + String.Format
!                                       (" ({0}, {1}, \"{2}\")", alg.Mode, 
alg.Padding, input);
                        }
  
***************
*** 549,552 ****
--- 644,668 ----
                                ICryptoTransform encryptor;
                                encryptor = alg.CreateEncryptor(key, iv);
+                               Assert(GetError("encryptor cannot transform 
multiple blocks",
+                                                               alg, input),
+                                          
encryptor.CanTransformMultipleBlocks);
+                               if(mode == CipherMode.ECB || mode == 
CipherMode.CBC)
+                               {
+                                       AssertEquals(GetError("encryptor has 
wrong input size",
+                                                                               
  alg, input),
+                                                                size, 
encryptor.InputBlockSize);
+                                       AssertEquals(GetError("encryptor has 
wrong output size",
+                                                                               
  alg, input),
+                                                                size, 
encryptor.OutputBlockSize);
+                               }
+                               else
+                               {
+                                       AssertEquals(GetError("encryptor has 
wrong input size",
+                                                                               
  alg, input),
+                                                                1, 
encryptor.InputBlockSize);
+                                       AssertEquals(GetError("encryptor has 
wrong output size",
+                                                                               
  alg, input),
+                                                                1, 
encryptor.OutputBlockSize);
+                               }
                                byte[] rawOutput = new byte [rawInput.Length + 
256];
                                int len = encryptor.TransformBlock
***************
*** 562,565 ****
--- 678,702 ----
                                ICryptoTransform decryptor;
                                decryptor = alg.CreateDecryptor(key, iv);
+                               Assert(GetError("decryptor cannot transform 
multiple blocks",
+                                                               alg, input),
+                                          
decryptor.CanTransformMultipleBlocks);
+                               if(mode == CipherMode.ECB || mode == 
CipherMode.CBC)
+                               {
+                                       AssertEquals(GetError("decryptor has 
wrong input size",
+                                                                               
  alg, input),
+                                                                size, 
decryptor.InputBlockSize);
+                                       AssertEquals(GetError("decryptor has 
wrong output size",
+                                                                               
  alg, input),
+                                                                size, 
decryptor.OutputBlockSize);
+                               }
+                               else
+                               {
+                                       AssertEquals(GetError("decryptor has 
wrong input size",
+                                                                               
  alg, input),
+                                                                1, 
decryptor.InputBlockSize);
+                                       AssertEquals(GetError("decryptor has 
wrong output size",
+                                                                               
  alg, input),
+                                                                1, 
decryptor.OutputBlockSize);
+                               }
                                byte[] rawReverse = new byte [rawInput.Length + 
256];
                                int rlen = decryptor.TransformBlock
***************
*** 574,582 ****
                                if(padding != PaddingMode.None)
                                {
!                                       AssertEquals("reversed plaintext has 
incorrect length",
!                                                                
rawInput.Length, rlen);
                                        if(!IdenticalBlock(rawInput, 0, 
rawReverse, 0, rlen))
                                        {
!                                               Fail("reversed plaintext is not 
the same as original");
                                        }
                                }
--- 711,722 ----
                                if(padding != PaddingMode.None)
                                {
!                                       AssertEquals(GetError
!                                                       ("reversed plaintext 
has incorrect length",
!                                                        alg, input), 
rawInput.Length, rlen);
                                        if(!IdenticalBlock(rawInput, 0, 
rawReverse, 0, rlen))
                                        {
!                                               Fail(GetError
!                                                       ("reversed plaintext is 
not the same as original",
!                                                        alg, input));
                                        }
                                }
***************
*** 585,594 ****
                                        if(rawInput.Length > rlen)
                                        {
!                                               Fail("reversed plaintext has 
incorrect length");
                                        }
                                        if(!IdenticalBlock(rawInput, 0, 
rawReverse, 0,
                                                                           
rawInput.Length))
                                        {
!                                               Fail("reversed plaintext is not 
the same as original");
                                        }
                                }
--- 725,738 ----
                                        if(rawInput.Length > rlen)
                                        {
!                                               Fail(GetError
!                                                       ("reversed plaintext 
has incorrect length",
!                                                        alg, input));
                                        }
                                        if(!IdenticalBlock(rawInput, 0, 
rawReverse, 0,
                                                                           
rawInput.Length))
                                        {
!                                               Fail(GetError
!                                                       ("reversed plaintext is 
not the same as original",
!                                                        alg, input));
                                        }
                                }
***************
*** 632,644 ****
  
                                // Compare the actual output with the expected 
output.
!                               AssertEquals("ciphertext has incorrect length",
                                                         paddedOutput.Length, 
len);
                                if(!IdenticalBlock(paddedOutput, 0, rawOutput, 
0, len))
                                {
!                                       Fail("ciphertext was not the expected 
value");
                                }
                        }
  
!       // Run a mode test using a number of different inputs.
        protected void RunModeTest(SymmetricAlgorithm alg, CipherMode mode,
                                                           PaddingMode padding)
--- 776,790 ----
  
                                // Compare the actual output with the expected 
output.
!                               AssertEquals(GetError("ciphertext has incorrect 
length",
!                                                                         alg, 
input),
                                                         paddedOutput.Length, 
len);
                                if(!IdenticalBlock(paddedOutput, 0, rawOutput, 
0, len))
                                {
!                                       Fail(GetError("ciphertext was not the 
expected value",
!                                                                 alg, input));
                                }
                        }
  
!       // Run a mode test using a number of different inputs and padding modes.
        protected void RunModeTest(SymmetricAlgorithm alg, CipherMode mode,
                                                           PaddingMode padding)
***************
*** 646,650 ****
                                RunModeTest(alg, mode, padding, "");
                                RunModeTest(alg, mode, padding, "abc");
-                       #if false
                                RunModeTest(alg, mode, padding, "abcdefgh");
                                RunModeTest(alg, mode, padding, "abcdefghijk");
--- 792,795 ----
***************
*** 652,656 ****
                                RunModeTest(alg, mode, padding,
                                                        "The time has come the 
walrus said.");
!                       #endif
                        }
  
--- 797,806 ----
                                RunModeTest(alg, mode, padding,
                                                        "The time has come the 
walrus said.");
!                       }
!       protected void RunModeTest(SymmetricAlgorithm alg, CipherMode mode)
!                       {
!                               RunModeTest(alg, mode, PaddingMode.None);
!                               RunModeTest(alg, mode, PaddingMode.PKCS7);
!                               RunModeTest(alg, mode, PaddingMode.Zeros);
                        }
  

Index: TestAES.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Security/Cryptography/TestAES.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** TestAES.cs  25 Nov 2002 23:14:17 -0000      1.2
--- TestAES.cs  29 Nov 2002 00:33:38 -0000      1.3
***************
*** 118,120 ****
--- 118,142 ----
                        }
  
+       // Run mode tests.
+       public void TestAESECB()
+                       {
+                               RunModeTest(Rijndael.Create(), CipherMode.ECB);
+                       }
+       public void TestAESCBC()
+                       {
+                               RunModeTest(Rijndael.Create(), CipherMode.CBC);
+                       }
+       public void TestAESOFB()
+                       {
+                               RunModeTest(Rijndael.Create(), CipherMode.OFB);
+                       }
+       public void TestAESCFB()
+                       {
+                               RunModeTest(Rijndael.Create(), CipherMode.CFB);
+                       }
+       public void TestAESCTS()
+                       {
+                               RunModeTest(Rijndael.Create(), CipherMode.CTS);
+                       }
+ 
  }; // TestAES

Index: TestDES.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Security/Cryptography/TestDES.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** TestDES.cs  27 Nov 2002 22:27:06 -0000      1.3
--- TestDES.cs  29 Nov 2002 00:33:38 -0000      1.4
***************
*** 326,339 ****
                        }
  
- #if false
        // Run mode tests.
        public void TestDESECB()
                        {
!                               DES alg = DES.Create();
!                               RunModeTest(alg, CipherMode.ECB, 
PaddingMode.None);
!                               RunModeTest(alg, CipherMode.ECB, 
PaddingMode.PKCS7);
!                               RunModeTest(alg, CipherMode.ECB, 
PaddingMode.Zeros);
                        }
- #endif
  
  }; // TestDES
--- 326,350 ----
                        }
  
        // Run mode tests.
        public void TestDESECB()
                        {
!                               RunModeTest(DES.Create(), CipherMode.ECB);
!                       }
!       public void TestDESCBC()
!                       {
!                               RunModeTest(DES.Create(), CipherMode.CBC);
!                       }
!       public void TestDESOFB()
!                       {
!                               RunModeTest(DES.Create(), CipherMode.OFB);
!                       }
!       public void TestDESCFB()
!                       {
!                               RunModeTest(DES.Create(), CipherMode.CFB);
!                       }
!       public void TestDESCTS()
!                       {
!                               RunModeTest(DES.Create(), CipherMode.CTS);
                        }
  
  }; // TestDES

Index: TestRC2.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Security/Cryptography/TestRC2.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** TestRC2.cs  25 Nov 2002 23:14:17 -0000      1.2
--- TestRC2.cs  29 Nov 2002 00:33:38 -0000      1.3
***************
*** 77,79 ****
--- 77,101 ----
                        }
  
+       // Run mode tests.
+       public void TestRC2ECB()
+                       {
+                               RunModeTest(RC2.Create(), CipherMode.ECB);
+                       }
+       public void TestRC2CBC()
+                       {
+                               RunModeTest(RC2.Create(), CipherMode.CBC);
+                       }
+       public void TestRC2OFB()
+                       {
+                               RunModeTest(RC2.Create(), CipherMode.OFB);
+                       }
+       public void TestRC2CFB()
+                       {
+                               RunModeTest(RC2.Create(), CipherMode.CFB);
+                       }
+       public void TestRC2CTS()
+                       {
+                               RunModeTest(RC2.Create(), CipherMode.CTS);
+                       }
+ 
  }; // TestRC2

Index: TestTripleDES.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Security/Cryptography/TestTripleDES.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** TestTripleDES.cs    27 Nov 2002 02:56:30 -0000      1.3
--- TestTripleDES.cs    29 Nov 2002 00:33:38 -0000      1.4
***************
*** 296,298 ****
--- 296,320 ----
                        }
  
+       // Run mode tests.
+       public void TestTripleDESECB()
+                       {
+                               RunModeTest(TripleDES.Create(), CipherMode.ECB);
+                       }
+       public void TestTripleDESCBC()
+                       {
+                               RunModeTest(TripleDES.Create(), CipherMode.CBC);
+                       }
+       public void TestTripleDESOFB()
+                       {
+                               RunModeTest(TripleDES.Create(), CipherMode.OFB);
+                       }
+       public void TestTripleDESCFB()
+                       {
+                               RunModeTest(TripleDES.Create(), CipherMode.CFB);
+                       }
+       public void TestTripleDESCTS()
+                       {
+                               RunModeTest(TripleDES.Create(), CipherMode.CTS);
+                       }
+ 
  }; // TestTripleDES





reply via email to

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