bring Mono Security to monotouch
[mcs.git] / class / corlib / System.Security.Cryptography / TripleDES.cs
blob59285a0e04ceffbabe8827fc619b7766bfe87be1
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.
30 #if !NET_2_1 || MONOTOUCH
32 using System.Globalization;
33 using System.Runtime.InteropServices;
34 using System.Security.Cryptography;
36 namespace System.Security.Cryptography {
38 // References:
39 // a. FIPS PUB 46-3: TripleDES
40 // http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
41 // b. ANSI X9.52
42 // not free :-(
43 // http://webstore.ansi.org/ansidocstore/product.asp?sku=ANSI+X9%2E52%2D1998
45 #if NET_2_0
46 [ComVisible (true)]
47 #endif
48 public abstract class TripleDES : SymmetricAlgorithm {
50 #if NET_2_0
51 protected TripleDES ()
52 #else
53 public TripleDES ()
54 #endif
56 // from SymmetricAlgorithm
57 KeySizeValue = 192;
58 BlockSizeValue = 64;
59 FeedbackSizeValue = 8;
61 LegalKeySizesValue = new KeySizes [1];
62 LegalKeySizesValue [0] = new KeySizes (128, 192, 64);
64 LegalBlockSizesValue = new KeySizes [1];
65 LegalBlockSizesValue [0] = new KeySizes (64, 64, 0);
68 public override byte[] Key {
69 get {
70 if (KeyValue == null) {
71 // generate keys as long as we get weak keys
72 GenerateKey ();
73 while (IsWeakKey (KeyValue))
74 GenerateKey ();
76 return (byte[]) KeyValue.Clone ();
78 set {
79 if (value == null)
80 throw new ArgumentNullException ("Key");
81 // this will check for both key size and weak keys
82 if (IsWeakKey (value))
83 throw new CryptographicException (Locale.GetText ("Weak Key"));
84 KeyValue = (byte[]) value.Clone ();
88 // Triple DES is DES in EDE = Encrypt - Decrypt - Encrypt
89 // with 2 keys (a,b)
90 // EDE = Encrypt (a) - Decrypt (b) - Encrypt (a)
91 // if a == b then TripleDES == DES(a) (hence weak key)
92 // with 3 keys (a,b,c)
93 // EDE = Encrypt (a) - Decrypt (b) - Encrypt (c)
94 // if ( a == b ) then TripleDES == DES(c) (hence weak key)
95 // if ( b == c ) then TripleDES == DES(a) (hence weak key)
96 public static bool IsWeakKey (byte[] rgbKey)
98 #if NET_2_0
99 if (rgbKey == null)
100 throw new CryptographicException (Locale.GetText ("Null Key"));
101 #endif
102 // 128 bits (16 bytes) is 3 DES with 2 keys
103 if (rgbKey.Length == 16) {
104 // weak if first half == second half
105 for (int i = 0; i < 8; i++)
106 if (rgbKey [i] != rgbKey [i+8])
107 return false;
109 // 192 bits (24 bytes) is 3 DES with 3 keys
110 else if (rgbKey.Length == 24) {
111 bool bFirstCase = true;
112 // weak if first third == second third
113 for (int i = 0; i < 8; i++) {
114 if (rgbKey [i] != rgbKey [i+8]) {
115 bFirstCase = false;
116 break;
119 // weak if second third == third third
120 if (!bFirstCase) {
121 for (int i = 8; i < 16; i++)
122 if (rgbKey [i] != rgbKey [i+8])
123 return false;
126 else
127 throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
129 return true;
132 public static new TripleDES Create ()
134 return Create ("System.Security.Cryptography.TripleDES");
137 public static new TripleDES Create (string str)
139 return (TripleDES) CryptoConfig.CreateFromName (str);
145 #endif