2 // HMAC.cs: Generic HMAC inplementation
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using Mono
.Security
.Cryptography
;
39 namespace System
.Security
.Cryptography
{
41 // Mostly copied from (internal) Mono.Security.Cryptography.HMACAlgorithm
44 // a. FIPS PUB 198: The Keyed-Hash Message Authentication Code (HMAC), 2002 March.
45 // http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf
46 // b. Internet RFC 2104, HMAC, Keyed-Hashing for Message Authentication
47 // (include C source for HMAC-MD5)
48 // http://www.ietf.org/rfc/rfc2104.txt
49 // c. IETF RFC2202: Test Cases for HMAC-MD5 and HMAC-SHA-1
50 // (include C source for HMAC-MD5 and HAMAC-SHA1)
51 // http://www.ietf.org/rfc/rfc2202.txt
52 // d. ANSI X9.71, Keyed Hash Message Authentication Code.
54 // http://webstore.ansi.org/ansidocstore/product.asp?sku=ANSI+X9%2E71%2D2000
56 public abstract class HMAC
: KeyedHashAlgorithm
{
58 private bool _disposed
;
59 private string _hashName
;
60 private HashAlgorithm _algo
;
61 private BlockProcessor _block
;
62 protected Int32 BlockSizeValue
;
73 public string HashName
{
74 get { return _hashName; }
77 _algo
= HashAlgorithm
.Create (_hashName
);
78 _block
= new BlockProcessor (_algo
, 8);
82 public override byte[] Key
{
83 get { return (byte[]) base.Key.Clone (); }
85 if ((value != null) && (value.Length
> 64))
86 base.Key
= _algo
.ComputeHash (value);
88 base.Key
= (byte[]) value.Clone();
94 private byte[] KeySetup (byte[] key
, byte padding
)
96 byte[] buf
= new byte [64];
98 for (int i
= 0; i
< key
.Length
; ++i
)
99 buf
[i
] = (byte) ((byte) key
[i
] ^ padding
);
101 for (int i
= key
.Length
; i
< 64; ++i
)
107 protected override void Dispose (bool disposing
)
110 base.Dispose (disposing
);
114 protected override void HashCore (byte[] rgb
, int ib
, int cb
)
117 throw new ObjectDisposedException ("HMACSHA1");
123 _block
.Core (rgb
, ib
, cb
);
126 protected override byte[] HashFinal ()
129 throw new ObjectDisposedException ("HMAC");
133 byte[] intermediate
= _algo
.Hash
;
135 byte[] buf
= KeySetup (Key
, 0x5C);
137 _algo
.TransformBlock (buf
, 0, buf
.Length
, buf
, 0);
138 _algo
.TransformFinalBlock (intermediate
, 0, intermediate
.Length
);
139 byte[] hash
= _algo
.Hash
;
141 // zeroize sensitive data
142 Array
.Clear (buf
, 0, buf
.Length
);
143 Array
.Clear (intermediate
, 0, intermediate
.Length
);
147 public override void Initialize ()
150 throw new ObjectDisposedException ("HMAC");
153 _block
.Initialize ();
154 byte[] buf
= KeySetup (Key
, 0x36);
158 Array
.Clear (buf
, 0, buf
.Length
);
163 public static new HMAC
Create ()
165 return Create ("System.Security.Cryptography.HMAC");
168 public static new HMAC
Create (string algName
)
170 return (HMAC
) CryptoConfig
.CreateFromName (algName
);