* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / corlib / Mono.Security.Cryptography / MACAlgorithm.cs
1 //
2 // MACAlgorithm.cs: Handles MAC with any symmetric algorithm
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 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 using System;
31 using System.Security.Cryptography;
32
33 namespace Mono.Security.Cryptography {
34
35         // References:
36         // a.   FIPS PUB 81: DES MODES OF OPERATION 
37         //      MAC: Appendix F (MACDES not MACTripleDES but close enough ;-)
38         //      http://www.itl.nist.gov/fipspubs/fip81.htm
39         
40         // Generic MAC mechanims - most of the work is done in here
41         // It should work with any symmetric algorithm function e.g. DES for MACDES (fips81)
42         internal class MACAlgorithm {
43
44                 private SymmetricAlgorithm algo;
45                 private ICryptoTransform enc;
46                 private byte[] block;
47                 private int blockSize;
48                 private int blockCount;
49
50                 public MACAlgorithm (SymmetricAlgorithm algorithm) 
51                 {
52                         algo = (SymmetricAlgorithm) algorithm;
53                         algo.Mode = CipherMode.CBC;
54                         blockSize = (algo.BlockSize >> 3); // in bytes
55                         algo.IV = new byte [blockSize];
56                         block = new byte [blockSize];
57                 }
58         
59                 public void Initialize (byte[] key) 
60                 {
61                         algo.Key = key;
62                         // note: the encryptor transform may be reusable - see Final
63                         if (enc == null) {
64                                 enc = algo.CreateEncryptor ();
65                         }
66                         Array.Clear (block, 0, blockSize);
67                         blockCount = 0;
68                 }
69                 
70                 public void Core (byte[] rgb, int ib, int cb) 
71                 {
72                         // 1. fill the rest of the "block"
73                         int n = System.Math.Min (blockSize - blockCount, cb);
74                         Array.Copy (rgb, ib, block, blockCount, n); 
75                         blockCount += n;
76         
77                         // 2. if block is full then transform it
78                         if (blockCount == blockSize) {
79                                 enc.TransformBlock (block, 0, blockSize, block, 0);
80         
81                                 // 3. transform any other full block in specified buffer
82                                 int b = (int) ((cb - n) / blockSize);
83                                 for (int i=0; i < b; i++) {
84                                         enc.TransformBlock (rgb, n, blockSize, block, 0);
85                                         n += blockSize;
86                                 }
87         
88                                 // 4. if data is still present fill the "block" with the remainder
89                                 blockCount = cb - n;
90                                 if (blockCount > 0)
91                                         Array.Copy (rgb, n, block, 0, blockCount);
92                         }
93                 }
94         
95                 public byte[] Final () 
96                 {
97                         byte[] result;
98                         if ((blockCount > 0) || ((algo.Padding != PaddingMode.Zeros) && (algo.Padding != PaddingMode.None))) {
99                                 result = enc.TransformFinalBlock (block, 0, blockCount);
100                         } else {
101 #if NET_1_0
102                                 // add an empty (zeros) block for MAC padding
103                                 byte[] emptyBlock = new byte [blockSize];
104                                 result = enc.TransformFinalBlock (emptyBlock, 0, blockSize);
105 #else
106                                 result = (byte[]) block.Clone ();
107 #endif
108                         }
109
110                         if (!enc.CanReuseTransform) {
111                                 enc.Dispose ();
112                                 enc = null;
113                         }
114                         return result;
115                 }
116         }
117 }