**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Mono.Security.Cryptography / MACAlgorithm.cs
blob53fab92d4c5fce7a9806be6e7950926f6bf13e3b
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.
30 using System;
31 using System.Security.Cryptography;
33 namespace Mono.Security.Cryptography {
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
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 {
44 private SymmetricAlgorithm algo;
45 private ICryptoTransform enc;
46 private byte[] block;
47 private int blockSize;
48 private int blockCount;
50 public MACAlgorithm (SymmetricAlgorithm algorithm)
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];
59 public void Initialize (byte[] key)
61 algo.Key = key;
62 // note: the encryptor transform may be reusable - see Final
63 if (enc == null) {
64 enc = algo.CreateEncryptor ();
66 Array.Clear (block, 0, blockSize);
67 blockCount = 0;
70 public void Core (byte[] rgb, int ib, int cb)
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;
77 // 2. if block is full then transform it
78 if (blockCount == blockSize) {
79 enc.TransformBlock (block, 0, blockSize, block, 0);
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;
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);
95 public byte[] Final ()
97 byte[] result;
98 if (blockCount > 0)
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
109 if (!enc.CanReuseTransform) {
110 enc.Dispose ();
111 enc = null;
113 return result;