2 // MACAlgorithm.cs: Handles MAC with any symmetric algorithm
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 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 System
.Security
.Cryptography
;
33 namespace Mono
.Security
.Cryptography
{
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
;
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
)
62 // note: the encryptor transform may be reusable - see Final
64 enc
= algo
.CreateEncryptor ();
66 Array
.Clear (block
, 0, blockSize
);
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
);
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);
88 // 4. if data is still present fill the "block" with the remainder
91 Array
.Copy (rgb
, n
, block
, 0, blockCount
);
95 public byte[] Final ()
99 result
= enc
.TransformFinalBlock (block
, 0, blockCount
);
102 // add an empty (zeros) block for MAC padding
103 byte[] emptyBlock
= new byte [blockSize
];
104 result
= enc
.TransformFinalBlock (emptyBlock
, 0, blockSize
);
106 result
= (byte[]) block
.Clone ();
109 if (!enc
.CanReuseTransform
) {