New test.
[mono.git] / mcs / class / corlib / System.Security.Cryptography / TripleDES.cs
1 //
2 // TripleDES.cs: Handles TripleDES (abstract class)
3 //
4 // Author:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Globalization;
31 using System.Runtime.InteropServices;
32 using System.Security.Cryptography;
33
34 namespace System.Security.Cryptography {
35
36 // References:
37 // a.   FIPS PUB 46-3: TripleDES
38 //      http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
39 // b.   ANSI X9.52
40 //      not free :-(
41 //      http://webstore.ansi.org/ansidocstore/product.asp?sku=ANSI+X9%2E52%2D1998
42
43 #if NET_2_0
44 [ComVisible (true)]
45 #endif
46 public abstract class TripleDES : SymmetricAlgorithm {
47
48 #if NET_2_0
49         protected TripleDES ()
50 #else
51         public TripleDES ()
52 #endif
53         {
54                 // from SymmetricAlgorithm
55                 KeySizeValue = 192;
56                 BlockSizeValue = 64;
57                 FeedbackSizeValue = 8;
58
59                 LegalKeySizesValue = new KeySizes [1];
60                 LegalKeySizesValue [0] = new KeySizes (128, 192, 64);
61
62                 LegalBlockSizesValue = new KeySizes [1];
63                 LegalBlockSizesValue [0] = new KeySizes (64, 64, 0);
64         }
65
66         public override byte[] Key {
67                 get {
68                         if (KeyValue == null) {
69                                 // generate keys as long as we get weak keys
70                                 GenerateKey ();
71                                 while (IsWeakKey (KeyValue))
72                                         GenerateKey ();
73                         }
74                         return (byte[]) KeyValue.Clone ();
75                 }
76                 set { 
77                         if (value == null)
78                                 throw new ArgumentNullException ("Key");
79                         // this will check for both key size and weak keys
80                         if (IsWeakKey (value))
81                                 throw new CryptographicException (Locale.GetText ("Weak Key"));
82                         KeyValue = (byte[]) value.Clone (); 
83                 }
84         }
85
86         // Triple DES is DES in EDE = Encrypt - Decrypt - Encrypt
87         // with 2 keys (a,b)
88         //      EDE = Encrypt (a) - Decrypt (b) - Encrypt (a)
89         //      if a == b then TripleDES == DES(a) (hence weak key)
90         // with 3 keys (a,b,c)
91         //      EDE = Encrypt (a) - Decrypt (b) - Encrypt (c)
92         //      if ( a == b ) then TripleDES == DES(c) (hence weak key)
93         //      if ( b == c ) then TripleDES == DES(a) (hence weak key)
94         public static bool IsWeakKey (byte[] rgbKey)
95         {
96 #if NET_2_0
97                 if (rgbKey == null)
98                         throw new CryptographicException (Locale.GetText ("Null Key"));
99 #endif
100                 // 128 bits (16 bytes) is 3 DES with 2 keys
101                 if (rgbKey.Length == 16) {
102                         // weak if first half == second half
103                         for (int i = 0; i < 8; i++)
104                                 if (rgbKey [i] != rgbKey [i+8])
105                                         return false;
106                 }
107                 // 192 bits (24 bytes) is 3 DES with 3 keys
108                 else if (rgbKey.Length == 24) {
109                         bool bFirstCase = true; 
110                         // weak if first third == second third
111                         for (int i = 0; i < 8; i++) {
112                                 if (rgbKey [i] != rgbKey [i+8]) {
113                                         bFirstCase = false;
114                                         break;
115                                 }
116                         }
117                         // weak if second third == third third 
118                         if (!bFirstCase) {
119                                 for (int i = 8; i < 16; i++)
120                                         if (rgbKey [i] != rgbKey [i+8])
121                                                 return false;
122                         }
123                 }
124                 else
125                         throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
126
127                 return true;
128         }
129
130         public static new TripleDES Create ()
131         {
132                 return Create ("System.Security.Cryptography.TripleDES");
133         }
134
135         public static new TripleDES Create (string str)
136         {
137                 return (TripleDES) CryptoConfig.CreateFromName (str);
138         }
139 }
140
141 }