Changeset 157


Ignore:
Timestamp:
Dec 17, 2008, 8:07:48 AM (13 years ago)
Author:
Gerhard Junker
Message:

SSC-Twofish tests OK

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/DevTestMethods/TwofishTest.cs

    r156 r157  
    1313using System;
    1414using System.Text;
    15 
     15using System.Diagnostics;
    1616using System.Security.Cryptography;
    1717using System.Collections.Generic;
     
    6363    }
    6464
     65    /// <summary>
     66    /// Converts the byte to hex.
     67    /// </summary>
     68    /// <param name="bytes">The bytes.</param>
     69    /// <returns></returns>
     70    private string ConvertByteToHex(byte[] bytes)
     71    {
     72      string tmp = "";
     73      foreach (byte b in bytes)
     74      {
     75        if (b < 0x10)
     76          tmp += "0";
     77        tmp += b.ToString("X");
     78      }
     79      return tmp;
     80    }
     81
    6582    [TestMethod]
    6683    public void TwofishTestMethod()
    6784    {
    6885      // test vectors from
    69       // http://www.schneier.com/code/ecb_ival.txt
     86      // http://www.schneier.com/plain/ecb_ival.txt
    7087      //
    7188      string[] source =
    7289      {
     90        "00000000000000000000000000000000",
    7391        "00000000000000000000000000000000",
    7492        "00000000000000000000000000000000"
     
    86104        "00000000000000000000000000000000",
    87105        "0123456789ABCDEFFEDCBA98765432100011223344556677",
    88         "0123456789ABCDEFFEDCBA987654321000112233445566778899AABBCCDDEEFF"
     106        "0123456789ABCDEFFEDCBA987654321000112233445566778899AABBCCDDEEFF",
    89107      };
    90108
     
    102120        testContextInstance.WriteLine(" key  = " + key[i]);
    103121
    104         ICryptoTransform tr =  tf.CreateEncryptor(ConvertHexToByte(key[i]), iv);
     122        byte[] myKey = ConvertHexToByte(key[i]);
     123        ICryptoTransform encrypt =  tf.CreateEncryptor(myKey, iv);
    105124
    106         byte[] code = ConvertHexToByte(source[i]);
     125        byte[] plain = ConvertHexToByte(source[i]);
    107126
    108         //int olength = code.Length;
     127        //int olength = plain.Length;
    109128        //olength += (16 - (olength % 16));
    110129       
    111         //byte[] ct = new byte[olength];
     130        //byte[] code = new byte[olength];
    112131
    113132        //int pos = 0;
    114         //while (code.Length > 16)
     133        //while (plain.Length > 16)
    115134        //{
    116         //  tr.TransformBlock(code, pos, 16, ref ct, pos);
     135        //  encrypt.TransformBlock(plain, pos, 16, ref code, pos);
    117136        //}
    118137       
    119         byte[] ct = tr.TransformFinalBlock(code, 0, code.Length);
     138        byte[] code = encrypt.TransformFinalBlock(plain, 0, plain.Length);
    120139
    121         string tmp = "";
    122         foreach (byte b in ct)
    123         {
    124           if (b < 0x10)
    125             tmp += "0";
    126           tmp += b.ToString("X");
    127         }
    128 
     140        string tmp = ConvertByteToHex(code);
    129141        testContextInstance.WriteLine(" expected   = " + result[i]);
    130142        testContextInstance.WriteLine(" calculated = " + tmp);
    131143
    132144        Assert.AreEqual(tmp, result[i]);
     145
     146        ICryptoTransform decrypt =  tf.CreateDecryptor(myKey, iv);
     147
     148        byte[] plain2 = decrypt.TransformFinalBlock(code, 0, code.Length);
     149        string source2 = ConvertByteToHex(plain2);
     150        testContextInstance.WriteLine(" expected   = " + source[i]);
     151        testContextInstance.WriteLine(" calculated = " + source2);
     152
     153        Assert.AreEqual(source[i], source2);
    133154      }
    134155    }
  • trunk/DevTestMethods/pkcsTest.cs

    r138 r157  
    4444    }
    4545
     46    /// <summary>
     47    /// Converts the hex to byte.
     48    /// </summary>
     49    /// <param name="str">The STR.</param>
     50    /// <returns></returns>
     51    private byte[] ConvertHexToByte(string str)
     52    {
     53      int len = str.Length / 2;
     54      byte[] hex = new byte[len];
     55      for (int j = 0; j < len; j++)
     56      {
     57        hex[j] = byte.Parse(str.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
     58        str = str.Substring(2);
     59      }
     60
     61      return hex;
     62    }
     63
     64    /// <summary>
     65    /// Converts the byte to hex.
     66    /// </summary>
     67    /// <param name="bytes">The bytes.</param>
     68    /// <returns></returns>
     69    private string ConvertByteToHex(byte[] bytes)
     70    {
     71      string tmp = "";
     72      foreach (byte b in bytes)
     73      {
     74        if (b < 0x10)
     75          tmp += "0";
     76        tmp += b.ToString("X");
     77      }
     78      return tmp;
     79    }
     80
     81
    4682    private void pkcs5part(byte[] key, byte[] salt, int hmac, int count, byte[] result)
    4783    {
     
    5692      p.SaltData = salt;
    5793
    58       string tmp = "key is  ";
    59       foreach (byte b in key)
    60         tmp += b.ToString("x2") + " ";
    61       testContextInstance.WriteLine(tmp);
    62 
    63       tmp = "salt is ";
    64       foreach (byte b in salt)
    65         tmp += b.ToString("x2") + " ";
    66       testContextInstance.WriteLine(tmp);
     94      testContextInstance.WriteLine("key is  " + ConvertByteToHex(key));
     95      testContextInstance.WriteLine("salt is " + ConvertByteToHex(salt));
    6796
    6897      p.Hash();
     
    73102      Assert.AreEqual(h.Length, result.Length, "Different hash sizes found");
    74103
    75       tmp = "expected hash is  ";
    76       foreach (byte b in result)
    77         tmp += b.ToString("x2") + " ";
    78       testContextInstance.WriteLine(tmp);
    79 
    80       tmp = "calculated hash is ";
    81       foreach (byte b in h)
    82         tmp += b.ToString("x2") + " ";
    83       testContextInstance.WriteLine(tmp);
     104      testContextInstance.WriteLine("expected hash is  " + ConvertByteToHex(result));
     105      testContextInstance.WriteLine("calculated hash is " + ConvertByteToHex(h));
    84106
    85107      // the next compares references etc ... but not the array content :-(
  • trunk/SSCext/SSC2fish.cs

    r156 r157  
    7979        iv = rgbIV;
    8080
     81      int kl = rgbKey.Length * 8;
     82      if (ValidKeySize(kl)) keySize = kl;
     83
    8184      return new TwofishEncryption(keySize, ref key, ref iv, cipherMode,
    8285        TwofishManaged.EncryptionDirection.Decrypting);
     
    97100      if (cipherMode == CipherMode.CBC)
    98101        iv = rgbIV;
     102
     103      int kl = rgbKey.Length * 8;
     104      if (ValidKeySize(kl)) keySize = kl;
    99105
    100106      return new TwofishEncryption(keySize, ref key, ref iv, cipherMode,
  • trunk/SSCext/TwofishBase.cs

    r156 r157  
    207207      {
    208208        uint t0,t1,tmp;
    209 
     209#if DEBUG2
     210
     211        for (int i = 0; i < subKeys.Length; i++)
     212          Debug.WriteLine("SubKey " + subKeys[i].ToString("x"));
     213#endif
    210214        for (int i=0; i < BLOCK_SIZE / 32; i++) /* copy in the block, add whitening */
    211215        {
  • trunk/SSCext/TwofishEncryption.cs

    r156 r157  
    5858        encryptionDirection = direction;
    5959        reKey(keyLen, ref Key);
     60
     61#if DEBUG2
     62        for (int i =  0; i < Key.Length; i++)
     63          Debug.WriteLine(i.ToString("00") + " " + Key[i].ToString("x"));
     64#endif
    6065      }
    6166
Note: See TracChangeset for help on using the changeset viewer.