context: /*****************************************************************************
  *
  * Copyright (c) [2009] Novell, Inc.
  * All Rights Reserved.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public License as
  * published by the Free Software Foundation.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, contact Novell, Inc.
  *
  * To contact Novell about this file by physical or electronic mail,
  * you may find current contact information at www.novell.com
  *
  *-----------------------------------------------------------------------------
   ***********************************************************************/
  
  using System;
  using System.Text;
  using System.IO;
  using NUnit.Framework;
  using Simias.Encryption;
  
  
  namespace Simias.Encryption
  {
      /// <summary>
      /// Blowfish Tests
      /// </summary>
      [TestFixture]
      public class BlowfishTest
      {
          /* plaintext bytes -- left halves */
          uint[] plaintext_l = 
  		{
  			0x00000000, 0xFFFFFFFF, 0x10000000, 0x11111111, 0x11111111,
  			0x01234567, 0x00000000, 0x01234567, 0x01A1D6D0, 0x5CD54CA8,
  			0x0248D438, 0x51454B58, 0x42FD4430, 0x059B5E08, 0x0756D8E0,
  			0x762514B8, 0x3BDD1190, 0x26955F68, 0x164D5E40, 0x6B056E18,
  			0x004BD6EF, 0x480D3900, 0x437540C8, 0x072D43A0, 0x02FE5577,
  			0x1D9D5C50, 0x30553228, 0x01234567, 0x01234567, 0x01234567,
  			0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFEDCBA98,
  			0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98,
  			0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98,
  			0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98,
  			0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98,
  			0xFEDCBA98, 0xFEDCBA98, 0xFEDCBA98 
  		};
  
          /* plaintext bytes -- right halves */
          uint[] plaintext_r = 
  		{
  			0x00000000, 0xFFFFFFFF, 0x00000001, 0x11111111, 0x11111111,
  			0x89ABCDEF, 0x00000000, 0x89ABCDEF, 0x39776742, 0x3DEF57DA,
  			0x06F67172, 0x2DDF440A, 0x59577FA2, 0x51CF143A, 0x774761D2,
  			0x29BF486A, 0x49372802, 0x35AF609A, 0x4F275232, 0x759F5CCA,
  			0x09176062, 0x6EE762F2, 0x698F3CFA, 0x77075292, 0x8117F12A,
  			0x18F728C2, 0x6D6F295A, 0x89ABCDEF, 0x89ABCDEF, 0x89ABCDEF,
  			0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF, 0x76543210,
  			0x76543210, 0x76543210, 0x76543210, 0x76543210, 0x76543210,
  			0x76543210, 0x76543210, 0x76543210, 0x76543210, 0x76543210,
  			0x76543210, 0x76543210, 0x76543210, 0x76543210, 0x76543210,
  			0x76543210, 0x76543210, 0x76543210, 0x76543210, 0x76543210,
  			0x76543210, 0x76543210, 0x76543210
  		};
  
          /* ciphertext bytes -- left halves */
          uint[] ciphertext_l = 
  		{
  			0x4EF99745, 0x51866FD5, 0x7D856F9A, 0x2466DD87, 0x61F9C380,
  			0x7D0CC630, 0x4EF99745, 0x0ACEAB0F, 0x59C68245, 0xB1B8CC0B,
  			0x1730E577, 0xA25E7856, 0x353882B1, 0x48F4D088, 0x432193B7,
  			0x13F04154, 0x2EEDDA93, 0xD887E039, 0x5F99D04F, 0x4A057A3B,
  			0x452031C1, 0x7555AE39, 0x53C55F9C, 0x7A8E7BFA, 0xCF9C5D7A,
  			0xD1ABB290, 0x55CB3774, 0xFA34EC48, 0xA7907951, 0xC39E072D,
  			0x014933E0, 0xF21E9A77, 0x24594688, 0x6B5C5A9C, 0xF9AD597C,
  			0xE91D21C1, 0xE9C2B70A, 0xBE1E6394, 0xB39E4448, 0x9457AA83,
  			0x8BB77032, 0xE87A244E, 0x15750E7A, 0x122BA70B, 0x3A833C9A,
  			0x9409DA87, 0x884F8062, 0x1F85031C, 0x79D9373A, 0x93142887,
  			0x03429E83, 0xA4299E27, 0xAFD5AED1, 0x10851C0E, 0xE6F51ED7,
  			0x64A6E14A, 0x80C7D7D4, 0x05044B62 
  		};
  
          /* ciphertext bytes -- right halves */
          uint[] ciphertext_r = 
  		{
  			0x6198DD78, 0xB85ECB8A, 0x613063F2, 0x8B963C9D, 0x2281B096,
  			0xAFDA1EC7, 0x6198DD78, 0xC6A0A28D, 0xEB05282B, 0x250F09A0,
  			0x8BEA1DA4, 0xCF2651EB, 0x09CE8F1A, 0x4C379918, 0x8951FC98,
  			0xD69D1AE5, 0xFFD39C79, 0x3C2DA6E3, 0x5B163969, 0x24D3977B,
  			0xE4FADA8E, 0xF59B87BD, 0xB49FC019, 0x937E89A3, 0x4986ADB5,
  			0x658BC778, 0xD13EF201, 0x47B268B2, 0x08EA3CAE, 0x9FAC631D,
  			0xCDAFF6E4, 0xB71C49BC, 0x5754369A, 0x5D9E0A5A, 0x49DB005E,
  			0xD961A6D6, 0x1BC65CF3, 0x08640F05, 0x1BDB1E6E, 0xB1928C0D,
  			0xF960629D, 0x2CC85E82, 0x4F4EC577, 0x3AB64AE0, 0xFFC537F6,
  			0xA90F6BF2, 0x5060B8B4, 0x19E11968, 0x714CA34F, 0xEE3BE15C,
  			0x8CE2D14B, 0x469FF67B, 0xC1BC96A8, 0x3858DA9F, 0x9B9DB21F,
  			0xFD36B46F, 0x5A5479AD, 0xFA52D080
  		};
  
          /* key bytes for variable key tests */
          byte[][] variable_key =
  		{
  			new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  			new byte[] {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  			new byte[] {0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  			new byte[] {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
  			new byte[] {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
  			new byte[] {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
  			new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  			new byte[] {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10},
  			new byte[] {0x7C, 0xA1, 0x10, 0x45, 0x4A, 0x1A, 0x6E, 0x57},
  			new byte[] {0x01, 0x31, 0xD9, 0x61, 0x9D, 0xC1, 0x37, 0x6E},
  			new byte[] {0x07, 0xA1, 0x13, 0x3E, 0x4A, 0x0B, 0x26, 0x86},
  			new byte[] {0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E},
  			new byte[] {0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6},
  			new byte[] {0x01, 0x13, 0xB9, 0x70, 0xFD, 0x34, 0xF2, 0xCE},
  			new byte[] {0x01, 0x70, 0xF1, 0x75, 0x46, 0x8F, 0xB5, 0xE6},
  			new byte[] {0x43, 0x29, 0x7F, 0xAD, 0x38, 0xE3, 0x73, 0xFE},
  			new byte[] {0x07, 0xA7, 0x13, 0x70, 0x45, 0xDA, 0x2A, 0x16},
  			new byte[] {0x04, 0x68, 0x91, 0x04, 0xC2, 0xFD, 0x3B, 0x2F},
  			new byte[] {0x37, 0xD0, 0x6B, 0xB5, 0x16, 0xCB, 0x75, 0x46},
  			new byte[] {0x1F, 0x08, 0x26, 0x0D, 0x1A, 0xC2, 0x46, 0x5E},
  			new byte[] {0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76},
  			new byte[] {0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xB0, 0x07},
  			new byte[] {0x49, 0x79, 0x3E, 0xBC, 0x79, 0xB3, 0x25, 0x8F},
  			new byte[] {0x4F, 0xB0, 0x5E, 0x15, 0x15, 0xAB, 0x73, 0xA7},
  			new byte[] {0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF},
  			new byte[] {0x01, 0x83, 0x10, 0xDC, 0x40, 0x9B, 0x26, 0xD6},
  			new byte[] {0x1C, 0x58, 0x7F, 0x1C, 0x13, 0x92, 0x4F, 0xEF},
  			new byte[] {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
  			new byte[] {0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E},
  			new byte[] {0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE},
  			new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  			new byte[] {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  			new byte[] {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
  			new byte[] {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10},
  		};
  
          /* key bytes for set key tests */
          byte[] set_key = 
  		{
  			0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87,
  			0x78, 0x69, 0x5A, 0x4B, 0x3C, 0x2D, 0x1E, 0x0F,
  			0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  		};
          /// <summary>
          /// Encrypt array.
          /// </summary>
          [Test]
          public void TestArray()
          {
              string key = "TestKey1";
              UTF8Encoding utf8 = new UTF8Encoding();
              Blowfish bf = new Blowfish(utf8.GetBytes(key));
              string data = "If you can read this it worked  ";
              byte[] en = utf8.GetBytes(data);
              bf.Encipher(en, en.Length);
              bf.Decipher(en, en.Length);
              Console.WriteLine(utf8.GetString(en, 0, en.Length));
          }
              [Test]
              public void CheckTestVectors()
              {
                  // Create the keys.
                  byte[][] keys = new byte[variable_key.Length + set_key.Length][];
                  int i = 0;
                  for (i = 0; i < variable_key.Length; ++i)
                  {
                      keys[i] = variable_key[i];
                  }
  
                  for (int j = 1; j <= set_key.Length; ++j, ++i)
                  {
                      keys[i] = new byte[j];
                      Array.Copy(set_key, 0, keys[i], 0, j);
                  }
  
                  for (i = 0; i < keys.Length; ++i)
                  {
                      byte[] key = keys[i];
                      Blowfish bf = new Blowfish(key);
                      uint xl, xr;
                      xl = plaintext_l[i];
                      xr = plaintext_r[i];
                      bf.callEncipher(ref xl, ref xr);
                      //bf.callEncipher(ref x1,ref xr);
                      // Encipher(ref xl, ref xr);
                      if (xl != ciphertext_l[i] || xr != ciphertext_r[i])
                          throw new ApplicationException("Encription Failed");
                      xl = ciphertext_l[i];
                      xr = ciphertext_r[i];
                     // bf.Decipher(ref xl, ref xr);
                      bf.callDecipher(ref xl, ref xr);
  
gt:                     if (xl != plaintext_l[i] || xr != plaintext_r[i])