source: trunk/SSCext/SSC2fish.cs @ 160

Last change on this file since 160 was 160, checked in by Gerhard Junker, 13 years ago

Twofish Plugin Beta

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to URL Author Date Rev Id
File size: 9.0 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////////////////////
2// CrypTool V2
3// © 2008 - Gerhard Junker
4// Apache License see http://www.apache.org/licenses/
5//
6// $HeadURL: trunk/SSCext/SSC2fish.cs $
7//////////////////////////////////////////////////////////////////////////////////////////////////
8// $Revision:: 160                                                                            $://
9// $Author:: junker                                                                           $://
10// $Date:: 2008-12-18 08:09:12 +0000 (Thu, 18 Dec 2008)                                       $://
11//////////////////////////////////////////////////////////////////////////////////////////////////
12
13using System;
14using System.Text;
15
16using System.Security.Cryptography;
17using System.Runtime.InteropServices;
18
19#if DEBUG
20using System.Diagnostics;
21#endif
22
23namespace System.Security.Cryptography
24{
25  /// <summary>
26  ///
27  /// </summary>
28  [ComVisibleAttribute(true)]
29  public partial class TwofishManaged : SymmetricAlgorithm
30  {
31    private const int   BLOCK_SIZE   = 128;
32    private const int   ROUNDS       =  16;
33    private const int   MAX_KEY_BITS = 256;
34    private const int   MIN_KEY_BITS = 128;
35
36    private int blockSize = BLOCK_SIZE;
37    private int keySize = MIN_KEY_BITS;
38
39    private byte[] key = null;
40    private byte[] iv = null;
41
42    private CipherMode  cipherMode = CipherMode.CBC;
43    private PaddingMode paddingMode = PaddingMode.Zeros;
44
45
46    /// <summary>
47    /// Initializes a new instance of the <see cref="TwofishManaged"/> class.
48    /// </summary>
49    /// <exception cref="T:System.Security.Cryptography.CryptographicException">
50    /// The implementation of the class derived from the symmetric algorithm is not valid.
51    /// </exception>
52    public TwofishManaged() 
53    {
54      Key = new byte[KeySize   / 8]; // zeroed by default
55      IV  = new byte[BlockSize / 8]; // zeroed by default
56    }
57
58    public static new TwofishManaged Create()
59    {
60      TwofishManaged fm = new TwofishManaged();
61      fm.GenerateKey();
62      fm.GenerateIV();
63      return fm;
64    }
65
66    /// <summary>
67    /// Creates a symmetric decryptor object with the specified
68    /// <see cref="P:System.Security.Cryptography.SymmetricAlgorithm.Key"/> property and
69    /// initialization vector (<see cref="P:System.Security.Cryptography.SymmetricAlgorithm.IV"/>).
70    /// </summary>
71    /// <param name="rgbKey">The secret key to use for the symmetric algorithm.</param>
72    /// <param name="rgbIV">The initialization vector to use for the symmetric algorithm.</param>
73    /// <returns>A symmetric decryptor object.</returns>
74    public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
75    {
76      key = rgbKey;
77
78      if (cipherMode == CipherMode.CBC)
79        iv = rgbIV;
80
81      int kl = rgbKey.Length * 8;
82      if (ValidKeySize(kl)) keySize = kl;
83
84      return new TwofishEncryption(keySize, ref key, ref iv, cipherMode, 
85        TwofishManaged.EncryptionDirection.Decrypting);
86    }
87
88    /// <summary>
89    /// Creates a symmetric encryptor object with the specified
90    /// <see cref="P:System.Security.Cryptography.SymmetricAlgorithm.Key"/> property and
91    /// initialization vector (<see cref="P:System.Security.Cryptography.SymmetricAlgorithm.IV"/>).
92    /// </summary>
93    /// <param name="rgbKey">The secret key to use for the symmetric algorithm.</param>
94    /// <param name="rgbIV">The initialization vector to use for the symmetric algorithm.</param>
95    /// <returns>A symmetric encryptor object.</returns>
96    public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
97    {
98      key = rgbKey; // this appears to make a new copy
99
100      if (cipherMode == CipherMode.CBC)
101        iv = rgbIV;
102
103      int kl = rgbKey.Length * 8;
104      if (ValidKeySize(kl)) keySize = kl;
105
106      return new TwofishEncryption(keySize, ref key, ref iv, cipherMode, 
107        TwofishManaged.EncryptionDirection.Encrypting);
108    }
109
110    /// <summary>
111    /// Generates a random initialization vector
112    /// (<see cref="P:System.Security.Cryptography.SymmetricAlgorithm.IV"/>) to use for the algorithm.
113    /// </summary>
114    public override void GenerateIV()
115    {
116      if ((iv == null) || (iv.Length == 0))
117        iv  = new byte[blockSize / 8]; // zeroed by default
118      else
119        Array.Clear(iv, 0, IV.Length);
120    }
121
122    /// <summary>
123    /// Generates a random key
124    /// (<see cref="P:System.Security.Cryptography.SymmetricAlgorithm.Key"/>) to use for the algorithm.
125    /// </summary>
126    public override void GenerateKey()
127    {
128      if ((key == null) || (key.Length == 0))
129        key = new byte[keySize   / 8]; // zeroed by default
130      else
131        Array.Clear(key, 0, Key.Length);
132    }
133
134
135    /// <summary>
136    /// Gets or sets the cipherMode for operation of the symmetric algorithm.
137    /// </summary>
138    /// <value></value>
139    /// <returns>
140    /// The cipherMode for operation of the symmetric algorithm.
141    /// The default is <see cref="F:System.Security.Cryptography.CipherMode.CBC"/>.
142    /// </returns>
143    /// <exception cref="T:System.Security.Cryptography.CryptographicException">
144    /// The cipher cipherMode is not one of the <see cref="T:System.Security.Cryptography.CipherMode"/> values.
145    /// </exception>
146    public override CipherMode Mode
147    {
148      set
149      {
150        switch (value)
151        {
152          case CipherMode.CBC:
153          case CipherMode.ECB:
154            break;
155
156          default:
157            throw new CryptographicException("CipherMode is not supported.");
158        }
159        ModeValue = value;
160      }
161    }
162
163
164    /// <summary>
165    /// Gets or sets the block size, in bits, of the cryptographic operation.
166    /// </summary>
167    /// <value></value>
168    /// <returns>
169    /// The block size, in bits.
170    /// </returns>
171    /// <exception cref="T:System.Security.Cryptography.CryptographicException">
172    /// The block size is invalid.
173    /// </exception>
174    public override int BlockSize
175    {
176      get
177      {
178        return blockSize;
179      }
180      set
181      {
182        blockSize = value;
183        base.BlockSize = blockSize;
184      }
185    }
186
187
188    /// <summary>
189    /// Gets or sets the keysize.
190    /// </summary>
191    /// <value>The keysize.</value>
192    public int Keysize
193    {
194      get
195      {
196        return keySize;
197      }
198      set
199      {
200        if (ValidKeySize(value))
201          keySize = value;
202      }
203    }
204
205
206    /// <summary>
207    /// Valids the size of the key.
208    /// </summary>
209    /// <param name="keysize">The keysize.</param>
210    /// <returns></returns>
211    public new bool ValidKeySize(int keysize)
212    {
213      switch (keysize)
214      {
215        case 128:
216        case 192:
217        case 256:
218          return true;
219        default:
220          return false;
221      }
222    }
223    /// <summary>
224    /// Gets or sets the secret key for the symmetric algorithm.
225    /// </summary>
226    /// <value></value>
227    /// <returns>
228    /// The secret key to use for the symmetric algorithm.
229    /// </returns>
230    /// <exception cref="T:System.ArgumentNullException">
231    /// An attempt was made to set the key to null.
232    /// </exception>
233    /// <exception cref="T:System.Security.Cryptography.CryptographicException">
234    /// The key size is invalid.
235    /// </exception>
236    public override byte[] Key
237    {
238      set
239      {
240        key = value;
241      }
242      get
243      {
244        return key;
245      }
246    }
247
248   
249    /// <summary>
250    /// Gets or sets the initialization vector
251    /// (<see cref="P:System.Security.Cryptography.SymmetricAlgorithm.IV"/>) for the symmetric algorithm.
252    /// </summary>
253    /// <value></value>
254    /// <returns>
255    /// The initialization vector.
256    /// </returns>
257    /// <exception cref="T:System.ArgumentNullException">
258    /// An attempt was made to set the initialization vector to null.
259    /// </exception>
260    /// <exception cref="T:System.Security.Cryptography.CryptographicException">
261    /// An attempt was made to set the initialization vector to an invalid size.
262    /// </exception>
263    public override byte[] IV
264    {
265      set
266      {
267        iv = value;
268      }
269      get
270      {
271        return iv;
272      }
273    }
274
275
276    /// <summary>
277    /// Gets or sets the padding cipherMode.
278    /// </summary>
279    /// <value>The padding cipherMode.</value>
280    public PaddingMode PaddingMode
281    {
282      get
283      {
284        return paddingMode;
285      }
286      set
287      {
288        paddingMode = value;
289      }
290    }
291
292
293    /// <summary>
294    /// Gets the key sizes, in bits, that are supported by the symmetric algorithm.
295    /// </summary>
296    /// <value></value>
297    /// <returns>
298    /// An array that contains the key sizes supported by the algorithm.
299    /// </returns>
300    public override KeySizes[] LegalKeySizes
301    {
302      get
303      {
304        KeySizes[] ks = new KeySizes[1];
305        ks[0] = new KeySizes(128, 256, 64);
306        return ks;
307      }
308    }
309  }
310}
Note: See TracBrowser for help on using the repository browser.