<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>Crypto++: hmac.h Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.1 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <h1>hmac.h</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// hmac.h - written and placed in the public domain by Wei Dai</span> <a name="l00002"></a>00002 <a name="l00003"></a>00003 <span class="preprocessor">#ifndef CRYPTOPP_HMAC_H</span> <a name="l00004"></a>00004 <span class="preprocessor"></span><span class="preprocessor">#define CRYPTOPP_HMAC_H</span> <a name="l00005"></a>00005 <span class="preprocessor"></span> <a name="l00006"></a>00006 <span class="preprocessor">#include "seckey.h"</span> <a name="l00007"></a>00007 <span class="preprocessor">#include "secblock.h"</span> <a name="l00008"></a>00008 <a name="l00009"></a>00009 NAMESPACE_BEGIN(CryptoPP) <a name="l00010"></a>00010 <a name="l00011"></a>00011 <span class="comment">//! _</span> <a name="l00012"></a><a class="code" href="class_h_m_a_c___base.html">00012</a> <span class="comment"></span>class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE <a class="code" href="class_h_m_a_c___base.html" title="_">HMAC_Base</a> : public <a class="code" href="class_variable_key_length.html" title="support query of variable key length, template parameters are default, min, max,...">VariableKeyLength</a><16, 0, INT_MAX>, public <a class="code" href="class_message_authentication_code.html" title="interface for message authentication codes">MessageAuthenticationCode</a> <a name="l00013"></a>00013 { <a name="l00014"></a>00014 <span class="keyword">public</span>: <a name="l00015"></a>00015 <a class="code" href="class_h_m_a_c___base.html" title="_">HMAC_Base</a>() : m_innerHashKeyed(<span class="keyword">false</span>) {} <a name="l00016"></a>00016 <span class="keywordtype">void</span> UncheckedSetKey(<span class="keyword">const</span> byte *userKey, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> keylength, <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html" title="interface for retrieving values given their names">NameValuePairs</a> &params); <a name="l00017"></a>00017 <a name="l00018"></a>00018 <span class="keywordtype">void</span> Restart(); <a name="l00019"></a>00019 <span class="keywordtype">void</span> Update(<span class="keyword">const</span> byte *input, <span class="keywordtype">size_t</span> length); <a name="l00020"></a>00020 <span class="keywordtype">void</span> TruncatedFinal(byte *mac, <span class="keywordtype">size_t</span> size); <a name="l00021"></a><a class="code" href="class_h_m_a_c___base.html#aff95ee2467f75accc29644b77f7ce781">00021</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_h_m_a_c___base.html#aff95ee2467f75accc29644b77f7ce781" title="input to Update() should have length a multiple of this for optimal speed">OptimalBlockSize</a>()<span class="keyword"> const </span>{<span class="keywordflow">return</span> <span class="keyword">const_cast<</span><a class="code" href="class_h_m_a_c___base.html" title="_">HMAC_Base</a>*<span class="keyword">></span>(<span class="keyword">this</span>)->AccessHash().OptimalBlockSize();} <a name="l00022"></a><a class="code" href="class_h_m_a_c___base.html#a4b3ee793dee4bf86a16fe18326db20e6">00022</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_h_m_a_c___base.html#a4b3ee793dee4bf86a16fe18326db20e6" title="size of the hash/digest/MAC returned by Final()">DigestSize</a>()<span class="keyword"> const </span>{<span class="keywordflow">return</span> <span class="keyword">const_cast<</span><a class="code" href="class_h_m_a_c___base.html" title="_">HMAC_Base</a>*<span class="keyword">></span>(<span class="keyword">this</span>)->AccessHash().DigestSize();} <a name="l00023"></a>00023 <a name="l00024"></a>00024 <span class="keyword">protected</span>: <a name="l00025"></a>00025 <span class="keyword">virtual</span> <a class="code" href="class_hash_transformation.html" title="interface for hash functions and data processing part of MACs">HashTransformation</a> & AccessHash() =0; <a name="l00026"></a>00026 byte * AccessIpad() {<span class="keywordflow">return</span> m_buf;} <a name="l00027"></a>00027 byte * AccessOpad() {<span class="keywordflow">return</span> m_buf + AccessHash().BlockSize();} <a name="l00028"></a>00028 byte * AccessInnerHash() {<span class="keywordflow">return</span> m_buf + 2*AccessHash().BlockSize();} <a name="l00029"></a>00029 <a name="l00030"></a>00030 <span class="keyword">private</span>: <a name="l00031"></a>00031 <span class="keywordtype">void</span> KeyInnerHash(); <a name="l00032"></a>00032 <a name="l00033"></a>00033 <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecByteBlock</a> m_buf; <a name="l00034"></a>00034 <span class="keywordtype">bool</span> m_innerHashKeyed; <a name="l00035"></a>00035 }; <a name="l00036"></a>00036 <span class="comment"></span> <a name="l00037"></a>00037 <span class="comment">//! <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a></span> <a name="l00038"></a>00038 <span class="comment"></span><span class="comment">/*! HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text)) */</span> <a name="l00039"></a>00039 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00040"></a><a class="code" href="class_h_m_a_c.html">00040</a> <span class="keyword">class </span><a class="code" href="class_h_m_a_c.html" title="HMAC">HMAC</a> : <span class="keyword">public</span> <a class="code" href="class_message_authentication_code_impl.html" title="_">MessageAuthenticationCodeImpl</a><HMAC_Base, HMAC<T> > <a name="l00041"></a>00041 { <a name="l00042"></a>00042 <span class="keyword">public</span>: <a name="l00043"></a>00043 CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE) <a name="l00044"></a>00044 CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE) <a name="l00045"></a>00045 <a name="l00046"></a>00046 <a class="code" href="class_h_m_a_c.html" title="HMAC">HMAC</a>() {} <a name="l00047"></a>00047 <a class="code" href="class_h_m_a_c.html" title="HMAC">HMAC</a>(<span class="keyword">const</span> byte *key, <span class="keywordtype">size_t</span> length=HMAC_Base::DEFAULT_KEYLENGTH) <a name="l00048"></a>00048 {this-><a class="code" href="class_simple_keying_interface.html#adf3c29b3ef3af74788a58c7c49887fd7" title="set or reset the key of this object">SetKey</a>(key, length);} <a name="l00049"></a>00049 <a name="l00050"></a>00050 <span class="keyword">static</span> std::string StaticAlgorithmName() {<span class="keywordflow">return</span> std::string(<span class="stringliteral">"HMAC("</span>) + T::StaticAlgorithmName() + <span class="stringliteral">")"</span>;} <a name="l00051"></a><a class="code" href="class_h_m_a_c.html#a95712a54fd2506f31cc5cb26e86dd658">00051</a> std::string <a class="code" href="class_h_m_a_c.html#a95712a54fd2506f31cc5cb26e86dd658" title="returns name of this algorithm, not universally implemented yet">AlgorithmName</a>()<span class="keyword"> const </span>{<span class="keywordflow">return</span> std::string(<span class="stringliteral">"HMAC("</span>) + m_hash.AlgorithmName() + <span class="stringliteral">")"</span>;} <a name="l00052"></a>00052 <a name="l00053"></a>00053 <span class="keyword">private</span>: <a name="l00054"></a>00054 <a class="code" href="class_hash_transformation.html" title="interface for hash functions and data processing part of MACs">HashTransformation</a> & AccessHash() {<span class="keywordflow">return</span> m_hash;} <a name="l00055"></a>00055 <a name="l00056"></a>00056 T m_hash; <a name="l00057"></a>00057 }; <a name="l00058"></a>00058 <a name="l00059"></a>00059 NAMESPACE_END <a name="l00060"></a>00060 <a name="l00061"></a>00061 <span class="preprocessor">#endif</span> </pre></div></div> <hr size="1"/><address style="text-align: right;"><small>Generated on 9 Dec 2009 for Crypto++ by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address> </body> </html>