2 // TripleDESCryptoServiceProvider.cs: Default TripleDES implementation
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using Mono.Security.Cryptography;
33 namespace System.Security.Cryptography {
36 // a. FIPS PUB 46-3: TripleDES
37 // http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
40 // http://webstore.ansi.org/ansidocstore/product.asp?sku=ANSI+X9%2E52%2D1998
42 public sealed class TripleDESCryptoServiceProvider : TripleDES {
44 public TripleDESCryptoServiceProvider ()
48 public override void GenerateIV ()
50 IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
53 public override void GenerateKey ()
55 KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
58 public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV)
62 return new TripleDESTransform (this, false, rgbKey, rgbIV);
65 public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV)
69 return new TripleDESTransform (this, true, rgbKey, rgbIV);
73 // TripleDES is just DES-EDE
74 internal class TripleDESTransform : SymmetricTransform {
77 private DESTransform E1;
78 private DESTransform D2;
79 private DESTransform E3;
82 private DESTransform D1;
83 private DESTransform E2;
84 private DESTransform D3;
86 public TripleDESTransform (TripleDES algo, bool encryption, byte[] key, byte[] iv) : base (algo, encryption, iv)
88 byte[] key1 = new byte [8];
89 byte[] key2 = new byte [8];
90 byte[] key3 = new byte [8];
91 DES des = DES.Create ();
92 Buffer.BlockCopy (key, 0, key1, 0, 8);
93 Buffer.BlockCopy (key, 8, key2, 0, 8);
95 Buffer.BlockCopy (key, 0, key3, 0, 8);
97 Buffer.BlockCopy (key, 16, key3, 0, 8);
99 // note: some modes (like CFB) requires encryption when decrypting
100 if ((encryption) || (algo.Mode == CipherMode.CFB)) {
101 E1 = new DESTransform (des, true, key1, iv);
102 D2 = new DESTransform (des, false, key2, iv);
103 E3 = new DESTransform (des, true, key3, iv);
106 D1 = new DESTransform (des, false, key3, iv);
107 E2 = new DESTransform (des, true, key2, iv);
108 D3 = new DESTransform (des, false, key1, iv);
112 // note: this method is garanteed to be called with a valid blocksize
113 // for both input and output
114 protected override void ECB (byte[] input, byte[] output)
116 DESTransform.Permutation (input, output, DESTransform.ipTab, false);
118 E1.ProcessBlock (output, output);
119 D2.ProcessBlock (output, output);
120 E3.ProcessBlock (output, output);
123 D1.ProcessBlock (output, output);
124 E2.ProcessBlock (output, output);
125 D3.ProcessBlock (output, output);
127 DESTransform.Permutation (output, output, DESTransform.fpTab, true);