2002-12-27 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / corlib / System.Security.Cryptography / TripleDESCryptoServiceProvider.cs
1 //
2 // TripleDESCryptoServiceProvider.cs: Default TripleDES implementation
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11
12 namespace System.Security.Cryptography {
13
14 // References:
15 // a.   FIPS PUB 46-3: TripleDES
16 //      http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
17 // b.   ANSI X9.52
18 //      not free :-(
19 //      http://webstore.ansi.org/ansidocstore/product.asp?sku=ANSI+X9%2E52%2D1998
20
21 public sealed class TripleDESCryptoServiceProvider : TripleDES {
22
23         public TripleDESCryptoServiceProvider () {}
24
25         public override void GenerateIV () 
26         {
27                 IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
28         }
29         
30         public override void GenerateKey () 
31         {
32                 KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
33         }
34         
35         public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV) 
36         {
37                 Key = rgbKey;
38                 IV = rgbIV;
39                 return new TripleDESTransform (this, false, rgbKey, rgbIV);
40         }
41         
42         public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV) 
43         {
44                 Key = rgbKey;
45                 IV = rgbIV;
46                 return new TripleDESTransform (this, true, rgbKey, rgbIV);
47         }
48 }
49
50 // TripleDES is just DES-EDE
51 internal class TripleDESTransform : SymmetricTransform {
52
53         // for encryption
54         private DESTransform E1;
55         private DESTransform D2;
56         private DESTransform E3;
57
58         // for decryption
59         private DESTransform D1;
60         private DESTransform E2;
61         private DESTransform D3;
62         
63         public TripleDESTransform (TripleDES algo, bool encryption, byte[] key, byte[] iv) : base (algo, encryption, iv) 
64         {
65                 byte[] key1 = new byte [8];
66                 byte[] key2 = new byte [8];
67                 byte[] key3 = new byte [8];
68                 DES des = DES.Create ();
69                 Array.Copy (key, 0, key1, 0, 8);
70                 Array.Copy (key, 8, key2, 0, 8);
71                 if (key.Length == 16)
72                         Array.Copy (key, 0, key3, 0, 8);
73                 else
74                         Array.Copy (key, 16, key3, 0, 8);
75
76                 // note: some modes (like CFB) requires encryption when decrypting
77                 if ((encryption) || (algo.Mode == CipherMode.CFB)) {
78                         E1 = new DESTransform (des, true, key1, iv);
79                         D2 = new DESTransform (des, false, key2, iv);
80                         E3 = new DESTransform (des, true, key3, iv);
81                 }
82                 else {
83                         D1 = new DESTransform (des, false, key3, iv);
84                         E2 = new DESTransform (des, true, key2, iv);
85                         D3 = new DESTransform (des, false, key1, iv);
86                 }
87         }
88
89         // note: this method is garanteed to be called with a valid blocksize
90         // for both input and output
91         protected override void ECB (byte[] input, byte[] output) 
92         {
93                 byte[] temp = new byte [input.Length];
94                 if (encrypt) {
95                         E1.ProcessBlock (input, output);
96                         D2.ProcessBlock (output, temp);
97                         E3.ProcessBlock (temp, output);
98                 }
99                 else {
100                         D1.ProcessBlock (input, output);
101                         E2.ProcessBlock (output, temp);
102                         D3.ProcessBlock (temp, output);
103                 }
104         }
105 }
106
107 }