<!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.cpp 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.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// hmac.cpp - written and placed in the public domain by Wei Dai</span> <a name="l00002"></a>00002 <a name="l00003"></a>00003 <span class="preprocessor">#include "pch.h"</span> <a name="l00004"></a>00004 <a name="l00005"></a>00005 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span> <a name="l00006"></a>00006 <span class="preprocessor"></span> <a name="l00007"></a>00007 <span class="preprocessor">#include "hmac.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 void <a class="code" href="class_h_m_a_c___base.html" title="_">HMAC_Base</a>::UncheckedSetKey(const byte *userKey, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> keylength, const <a class="code" href="class_name_value_pairs.html" title="interface for retrieving values given their names">NameValuePairs</a> &) <a name="l00012"></a>00012 { <a name="l00013"></a>00013 AssertValidKeyLength(keylength); <a name="l00014"></a>00014 <a name="l00015"></a>00015 Restart(); <a name="l00016"></a>00016 <a name="l00017"></a>00017 <a class="code" href="class_hash_transformation.html" title="interface for hash functions and data processing part of MACs">HashTransformation</a> &hash = AccessHash(); <a name="l00018"></a>00018 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> blockSize = hash.<a class="code" href="class_hash_transformation.html#ab5507c62376f6bea41d53ae9544d0ff0" title="block size of underlying compression function, or 0 if not block based">BlockSize</a>(); <a name="l00019"></a>00019 <a name="l00020"></a>00020 <span class="keywordflow">if</span> (!blockSize) <a name="l00021"></a>00021 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_argument.html" title="exception thrown when an invalid argument is detected">InvalidArgument</a>(<span class="stringliteral">"HMAC: can only be used with a block-based hash function"</span>); <a name="l00022"></a>00022 <a name="l00023"></a>00023 m_buf.resize(2*AccessHash().BlockSize() + AccessHash().DigestSize()); <a name="l00024"></a>00024 <a name="l00025"></a>00025 <span class="keywordflow">if</span> (keylength <= blockSize) <a name="l00026"></a>00026 memcpy(AccessIpad(), userKey, keylength); <a name="l00027"></a>00027 <span class="keywordflow">else</span> <a name="l00028"></a>00028 { <a name="l00029"></a>00029 AccessHash().CalculateDigest(AccessIpad(), userKey, keylength); <a name="l00030"></a>00030 keylength = hash.<a class="code" href="class_hash_transformation.html#a9f42cc280bac76b884ad12615c0dabb5" title="size of the hash/digest/MAC returned by Final()">DigestSize</a>(); <a name="l00031"></a>00031 } <a name="l00032"></a>00032 <a name="l00033"></a>00033 assert(keylength <= blockSize); <a name="l00034"></a>00034 memset(AccessIpad()+keylength, 0, blockSize-keylength); <a name="l00035"></a>00035 <a name="l00036"></a>00036 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i<blockSize; i++) <a name="l00037"></a>00037 { <a name="l00038"></a>00038 AccessOpad()[i] = AccessIpad()[i] ^ 0x5c; <a name="l00039"></a>00039 AccessIpad()[i] ^= 0x36; <a name="l00040"></a>00040 } <a name="l00041"></a>00041 } <a name="l00042"></a>00042 <a name="l00043"></a>00043 <span class="keywordtype">void</span> HMAC_Base::KeyInnerHash() <a name="l00044"></a>00044 { <a name="l00045"></a>00045 assert(!m_innerHashKeyed); <a name="l00046"></a>00046 <a class="code" href="class_hash_transformation.html" title="interface for hash functions and data processing part of MACs">HashTransformation</a> &hash = AccessHash(); <a name="l00047"></a>00047 hash.<a class="code" href="class_hash_transformation.html#aafabefaf445b65d3ba0d8e1c0294afe5" title="process more input">Update</a>(AccessIpad(), hash.<a class="code" href="class_hash_transformation.html#ab5507c62376f6bea41d53ae9544d0ff0" title="block size of underlying compression function, or 0 if not block based">BlockSize</a>()); <a name="l00048"></a>00048 m_innerHashKeyed = <span class="keyword">true</span>; <a name="l00049"></a>00049 } <a name="l00050"></a>00050 <a name="l00051"></a><a class="code" href="class_h_m_a_c___base.html#a7bdeb5438c920045d122017a6b4ab426">00051</a> <span class="keywordtype">void</span> <a class="code" href="class_h_m_a_c___base.html#a7bdeb5438c920045d122017a6b4ab426" title="discard the current state, and restart with a new message">HMAC_Base::Restart</a>() <a name="l00052"></a>00052 { <a name="l00053"></a>00053 <span class="keywordflow">if</span> (m_innerHashKeyed) <a name="l00054"></a>00054 { <a name="l00055"></a>00055 AccessHash().<a class="code" href="class_hash_transformation.html#a92752eebac10497211d7a065faba8725" title="discard the current state, and restart with a new message">Restart</a>(); <a name="l00056"></a>00056 m_innerHashKeyed = <span class="keyword">false</span>; <a name="l00057"></a>00057 } <a name="l00058"></a>00058 } <a name="l00059"></a>00059 <a name="l00060"></a><a class="code" href="class_h_m_a_c___base.html#aefb8a1fb43ef8c9390ee89bbb41c83af">00060</a> <span class="keywordtype">void</span> <a class="code" href="class_h_m_a_c___base.html#aefb8a1fb43ef8c9390ee89bbb41c83af" title="process more input">HMAC_Base::Update</a>(<span class="keyword">const</span> byte *input, <span class="keywordtype">size_t</span> length) <a name="l00061"></a>00061 { <a name="l00062"></a>00062 <span class="keywordflow">if</span> (!m_innerHashKeyed) <a name="l00063"></a>00063 KeyInnerHash(); <a name="l00064"></a>00064 AccessHash().<a class="code" href="class_hash_transformation.html#aafabefaf445b65d3ba0d8e1c0294afe5" title="process more input">Update</a>(input, length); <a name="l00065"></a>00065 } <a name="l00066"></a>00066 <a name="l00067"></a><a class="code" href="class_h_m_a_c___base.html#a5454c1446981ddee322b0521c6ebd375">00067</a> <span class="keywordtype">void</span> <a class="code" href="class_h_m_a_c___base.html#a5454c1446981ddee322b0521c6ebd375" title="truncated version of Final()">HMAC_Base::TruncatedFinal</a>(byte *mac, <span class="keywordtype">size_t</span> size) <a name="l00068"></a>00068 { <a name="l00069"></a>00069 ThrowIfInvalidTruncatedSize(size); <a name="l00070"></a>00070 <a name="l00071"></a>00071 <a class="code" href="class_hash_transformation.html" title="interface for hash functions and data processing part of MACs">HashTransformation</a> &hash = AccessHash(); <a name="l00072"></a>00072 <a name="l00073"></a>00073 <span class="keywordflow">if</span> (!m_innerHashKeyed) <a name="l00074"></a>00074 KeyInnerHash(); <a name="l00075"></a>00075 hash.<a class="code" href="class_hash_transformation.html#aa0b8c7a110d8968268fd02ec32b9a8e8" title="compute hash for current message, then restart for a new message">Final</a>(AccessInnerHash()); <a name="l00076"></a>00076 <a name="l00077"></a>00077 hash.<a class="code" href="class_hash_transformation.html#aafabefaf445b65d3ba0d8e1c0294afe5" title="process more input">Update</a>(AccessOpad(), hash.<a class="code" href="class_hash_transformation.html#ab5507c62376f6bea41d53ae9544d0ff0" title="block size of underlying compression function, or 0 if not block based">BlockSize</a>()); <a name="l00078"></a>00078 hash.<a class="code" href="class_hash_transformation.html#aafabefaf445b65d3ba0d8e1c0294afe5" title="process more input">Update</a>(AccessInnerHash(), hash.<a class="code" href="class_hash_transformation.html#a9f42cc280bac76b884ad12615c0dabb5" title="size of the hash/digest/MAC returned by Final()">DigestSize</a>()); <a name="l00079"></a>00079 hash.<a class="code" href="class_hash_transformation.html#a590ecda344aa0fb045abf42ec08823b2" title="truncated version of Final()">TruncatedFinal</a>(mac, size); <a name="l00080"></a>00080 <a name="l00081"></a>00081 m_innerHashKeyed = <span class="keyword">false</span>; <a name="l00082"></a>00082 } <a name="l00083"></a>00083 <a name="l00084"></a>00084 NAMESPACE_END <a name="l00085"></a>00085 <a name="l00086"></a>00086 <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>