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