**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Security.Cryptography / TripleDESCryptoServiceProvider.cs
blob5c14c437d838a84a7350a447b8da206d32483dcd
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 // (C) 2004 Novell (http://www.novell.com)
9 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
34 using System;
35 using Mono.Security.Cryptography;
37 namespace System.Security.Cryptography {
39 // References:
40 // a. FIPS PUB 46-3: TripleDES
41 // http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
42 // b. ANSI X9.52
43 // not free :-(
44 // http://webstore.ansi.org/ansidocstore/product.asp?sku=ANSI+X9%2E52%2D1998
46 public sealed class TripleDESCryptoServiceProvider : TripleDES {
48 public TripleDESCryptoServiceProvider ()
52 public override void GenerateIV ()
54 IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
57 public override void GenerateKey ()
59 KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
62 public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV)
64 Key = rgbKey;
65 IV = rgbIV;
66 return new TripleDESTransform (this, false, rgbKey, rgbIV);
69 public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV)
71 Key = rgbKey;
72 IV = rgbIV;
73 return new TripleDESTransform (this, true, rgbKey, rgbIV);
77 // TripleDES is just DES-EDE
78 internal class TripleDESTransform : SymmetricTransform {
80 // for encryption
81 private DESTransform E1;
82 private DESTransform D2;
83 private DESTransform E3;
85 // for decryption
86 private DESTransform D1;
87 private DESTransform E2;
88 private DESTransform D3;
90 public TripleDESTransform (TripleDES algo, bool encryption, byte[] key, byte[] iv) : base (algo, encryption, iv)
92 byte[] key1 = new byte [8];
93 byte[] key2 = new byte [8];
94 byte[] key3 = new byte [8];
95 DES des = DES.Create ();
96 Buffer.BlockCopy (key, 0, key1, 0, 8);
97 Buffer.BlockCopy (key, 8, key2, 0, 8);
98 if (key.Length == 16)
99 Buffer.BlockCopy (key, 0, key3, 0, 8);
100 else
101 Buffer.BlockCopy (key, 16, key3, 0, 8);
103 // note: some modes (like CFB) requires encryption when decrypting
104 if ((encryption) || (algo.Mode == CipherMode.CFB)) {
105 E1 = new DESTransform (des, true, key1, iv);
106 D2 = new DESTransform (des, false, key2, iv);
107 E3 = new DESTransform (des, true, key3, iv);
109 else {
110 D1 = new DESTransform (des, false, key3, iv);
111 E2 = new DESTransform (des, true, key2, iv);
112 D3 = new DESTransform (des, false, key1, iv);
116 // note: this method is garanteed to be called with a valid blocksize
117 // for both input and output
118 protected override void ECB (byte[] input, byte[] output)
120 byte[] temp = new byte [input.Length];
121 if (encrypt) {
122 E1.ProcessBlock (input, output);
123 D2.ProcessBlock (output, temp);
124 E3.ProcessBlock (temp, output);
126 else {
127 D1.ProcessBlock (input, output);
128 E2.ProcessBlock (output, temp);
129 D3.ProcessBlock (temp, output);
130 // don't keep decrypted content in memory
131 Array.Clear (temp, 0, temp.Length);