New test.
[mono.git] / mcs / class / corlib / System.Security.Cryptography / TripleDESCryptoServiceProvider.cs
1 //
2 // TripleDESCryptoServiceProvider.cs: Default TripleDES implementation
3 //
4 // Author:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 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 #if !MOONLIGHT
31
32 using System.Runtime.InteropServices;
33 using Mono.Security.Cryptography;
34
35 namespace System.Security.Cryptography {
36
37         // References:
38         // a.   FIPS PUB 46-3: TripleDES
39         //      http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
40         // b.   ANSI X9.52
41         //      not free :-(
42         //      http://webstore.ansi.org/ansidocstore/product.asp?sku=ANSI+X9%2E52%2D1998
43         
44         [ComVisible (true)]
45         public sealed class TripleDESCryptoServiceProvider : TripleDES {
46         
47                 public TripleDESCryptoServiceProvider ()
48                 {
49                 }
50         
51                 public override void GenerateIV () 
52                 {
53                         IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
54                 }
55                 
56                 public override void GenerateKey () 
57                 {
58                         KeyValue = TripleDESTransform.GetStrongKey ();
59                 }
60                 
61                 public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV) 
62                 {
63                         return new TripleDESTransform (this, false, rgbKey, rgbIV);
64                 }
65                 
66                 public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV) 
67                 {
68                         return new TripleDESTransform (this, true, rgbKey, rgbIV);
69                 }
70         }
71         
72         // TripleDES is just DES-EDE
73         internal class TripleDESTransform : SymmetricTransform {
74         
75                 // for encryption
76                 private DESTransform E1;
77                 private DESTransform D2;
78                 private DESTransform E3;
79         
80                 // for decryption
81                 private DESTransform D1;
82                 private DESTransform E2;
83                 private DESTransform D3;
84                 
85                 public TripleDESTransform (TripleDES algo, bool encryption, byte[] key, byte[] iv) : base (algo, encryption, iv) 
86                 {
87                         if (key == null) {
88                                 key = GetStrongKey ();
89                         }
90                         // note: checking weak keys also checks valid key length
91                         if (TripleDES.IsWeakKey (key)) {
92                                 string msg = Locale.GetText ("This is a known weak key.");
93                                 throw new CryptographicException (msg);
94                         }
95
96                         byte[] key1 = new byte [8];
97                         byte[] key2 = new byte [8];
98                         byte[] key3 = new byte [8];
99                         DES des = DES.Create ();
100                         Buffer.BlockCopy (key, 0, key1, 0, 8);
101                         Buffer.BlockCopy (key, 8, key2, 0, 8);
102                         if (key.Length == 16)
103                                 Buffer.BlockCopy (key, 0, key3, 0, 8);
104                         else
105                                 Buffer.BlockCopy (key, 16, key3, 0, 8);
106         
107                         // note: some modes (like CFB) requires encryption when decrypting
108                         if ((encryption) || (algo.Mode == CipherMode.CFB)) {
109                                 E1 = new DESTransform (des, true, key1, iv);
110                                 D2 = new DESTransform (des, false, key2, iv);
111                                 E3 = new DESTransform (des, true, key3, iv);
112                         }
113                         else {
114                                 D1 = new DESTransform (des, false, key3, iv);
115                                 E2 = new DESTransform (des, true, key2, iv);
116                                 D3 = new DESTransform (des, false, key1, iv);
117                         }
118                 }
119         
120                 // note: this method is garanteed to be called with a valid blocksize
121                 // for both input and output
122                 protected override void ECB (byte[] input, byte[] output) 
123                 {
124                         DESTransform.Permutation (input, output, DESTransform.ipTab, false);
125                         if (encrypt) {
126                                 E1.ProcessBlock (output, output);
127                                 D2.ProcessBlock (output, output);
128                                 E3.ProcessBlock (output, output);
129                         }
130                         else {
131                                 D1.ProcessBlock (output, output);
132                                 E2.ProcessBlock (output, output);
133                                 D3.ProcessBlock (output, output);
134                         }
135                         DESTransform.Permutation (output, output, DESTransform.fpTab, true);
136                 }
137
138                 static internal byte[] GetStrongKey ()
139                 {
140                         int size = DESTransform.BLOCK_BYTE_SIZE * 3;
141                         byte[] key = KeyBuilder.Key (size);
142                         while (TripleDES.IsWeakKey (key))
143                                 key = KeyBuilder.Key (size);
144                         return key;
145                 }
146         }
147 }
148
149 #endif
150