source: trunk/SSCext/SSC2fish.cs @ 226

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

2FISH - first fixes

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to URL Author Date Rev Id
File size: 9.4 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:: 226                                                                            $://
9// $Author:: junker                                                                           $://
10// $Date:: 2009-02-17 18:36:05 +0000 (Tue, 17 Feb 2009)                                       $://
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      Array.Clear(iv, 0, iv.Length);
79
80      if (null != rgbIV)
81      {
82          for (int i = 0; i < iv.Length && i < rgbIV.Length; i++)
83              iv[i] = rgbIV[i];
84      }
85
86        int kl = rgbKey.Length * 8;
87        if (ValidKeySize(kl)) keySize = kl;
88
89      return new TwofishEncryption(keySize, ref key, ref iv, cipherMode, 
90        TwofishManaged.EncryptionDirection.Decrypting);
91    }
92
93    /// <summary>
94    /// Creates a symmetric encryptor object with the specified
95    /// <see cref="P:System.Security.Cryptography.SymmetricAlgorithm.Key"/> property and
96    /// initialization vector (<see cref="P:System.Security.Cryptography.SymmetricAlgorithm.IV"/>).
97    /// </summary>
98    /// <param name="rgbKey">The secret key to use for the symmetric algorithm.</param>
99    /// <param name="rgbIV">The initialization vector to use for the symmetric algorithm.</param>
100    /// <returns>A symmetric encryptor object.</returns>
101    public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
102    {
103      key = rgbKey; // this appears to make a new copy
104
105      Array.Clear(iv, 0, iv.Length);
106
107      if (null != rgbIV)
108      {
109          for (int i = 0; i < iv.Length && i < rgbIV.Length; i++)
110              iv[i] = rgbIV[i];
111      }
112
113      int kl = rgbKey.Length * 8;
114      if (ValidKeySize(kl)) keySize = kl;
115
116      return new TwofishEncryption(keySize, ref key, ref iv, cipherMode, 
117        TwofishManaged.EncryptionDirection.Encrypting);
118    }
119
120    /// <summary>
121    /// Generates a random initialization vector
122    /// (<see cref="P:System.Security.Cryptography.SymmetricAlgorithm.IV"/>) to use for the algorithm.
123    /// </summary>
124    public override void GenerateIV()
125    {
126      if ((iv == null) || (iv.Length == 0))
127        iv  = new byte[16]; // zeroed by default
128      else
129        Array.Clear(iv, 0, iv.Length);
130    }
131
132    /// <summary>
133    /// Generates a random key
134    /// (<see cref="P:System.Security.Cryptography.SymmetricAlgorithm.Key"/>) to use for the algorithm.
135    /// </summary>
136    public override void GenerateKey()
137    {
138      if ((key == null) || (key.Length == 0))
139        key = new byte[keySize   / 8]; // zeroed by default
140      else
141        Array.Clear(key, 0, Key.Length);
142    }
143
144
145    /// <summary>
146    /// Gets or sets the cipherMode for operation of the symmetric algorithm.
147    /// </summary>
148    /// <value></value>
149    /// <returns>
150    /// The cipherMode for operation of the symmetric algorithm.
151    /// The default is <see cref="F:System.Security.Cryptography.CipherMode.CBC"/>.
152    /// </returns>
153    /// <exception cref="T:System.Security.Cryptography.CryptographicException">
154    /// The cipher cipherMode is not one of the <see cref="T:System.Security.Cryptography.CipherMode"/> values.
155    /// </exception>
156    public override CipherMode Mode
157    {
158      set
159      {
160        switch (value)
161        {
162          case CipherMode.CBC:
163          case CipherMode.ECB:
164            break;
165
166          default:
167            throw new CryptographicException("CipherMode is not supported.");
168        }
169        ModeValue = value;
170      }
171    }
172
173
174    /// <summary>
175    /// Gets or sets the block size, in bits, of the cryptographic operation.
176    /// </summary>
177    /// <value></value>
178    /// <returns>
179    /// The block size, in bits.
180    /// </returns>
181    /// <exception cref="T:System.Security.Cryptography.CryptographicException">
182    /// The block size is invalid.
183    /// </exception>
184    public override int BlockSize
185    {
186      get
187      {
188        return blockSize;
189      }
190      set
191      {
192        blockSize = value;
193        base.BlockSize = blockSize;
194      }
195    }
196
197
198    /// <summary>
199    /// Gets or sets the keysize.
200    /// </summary>
201    /// <value>The keysize.</value>
202    public int Keysize
203    {
204      get
205      {
206        return keySize;
207      }
208      set
209      {
210        if (ValidKeySize(value))
211          keySize = value;
212      }
213    }
214
215
216    /// <summary>
217    /// Valids the size of the key.
218    /// </summary>
219    /// <param name="keysize">The keysize.</param>
220    /// <returns></returns>
221    public new bool ValidKeySize(int keysize)
222    {
223      switch (keysize)
224      {
225        case 128:
226        case 192:
227        case 256:
228          return true;
229        default:
230          return false;
231      }
232    }
233    /// <summary>
234    /// Gets or sets the secret key for the symmetric algorithm.
235    /// </summary>
236    /// <value></value>
237    /// <returns>
238    /// The secret key to use for the symmetric algorithm.
239    /// </returns>
240    /// <exception cref="T:System.ArgumentNullException">
241    /// An attempt was made to set the key to null.
242    /// </exception>
243    /// <exception cref="T:System.Security.Cryptography.CryptographicException">
244    /// The key size is invalid.
245    /// </exception>
246    public override byte[] Key
247    {
248      set
249      {
250        key = value;
251      }
252      get
253      {
254        return key;
255      }
256    }
257
258   
259    /// <summary>
260    /// Gets or sets the initialization vector
261    /// (<see cref="P:System.Security.Cryptography.SymmetricAlgorithm.IV"/>) for the symmetric algorithm.
262    /// </summary>
263    /// <value></value>
264    /// <returns>
265    /// The initialization vector.
266    /// </returns>
267    /// <exception cref="T:System.ArgumentNullException">
268    /// An attempt was made to set the initialization vector to null.
269    /// </exception>
270    /// <exception cref="T:System.Security.Cryptography.CryptographicException">
271    /// An attempt was made to set the initialization vector to an invalid size.
272    /// </exception>
273    public override byte[] IV
274    {
275      set
276      {
277          Array.Clear(iv, 0, iv.Length);
278          for (int i = 0; i < iv.Length && i < value.Length; i++ )
279              iv[i] = value[i];
280      }
281      get
282      {
283        return iv;
284      }
285    }
286
287
288    /// <summary>
289    /// Gets or sets the padding cipherMode.
290    /// </summary>
291    /// <value>The padding cipherMode.</value>
292    public PaddingMode PaddingMode
293    {
294      get
295      {
296        return paddingMode;
297      }
298      set
299      {
300        paddingMode = value;
301      }
302    }
303
304
305    /// <summary>
306    /// Gets the key sizes, in bits, that are supported by the symmetric algorithm.
307    /// </summary>
308    /// <value></value>
309    /// <returns>
310    /// An array that contains the key sizes supported by the algorithm.
311    /// </returns>
312    public override KeySizes[] LegalKeySizes
313    {
314      get
315      {
316        KeySizes[] ks = new KeySizes[1];
317        ks[0] = new KeySizes(128, 256, 64);
318        return ks;
319      }
320    }
321  }
322}
Note: See TracBrowser for help on using the repository browser.