<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://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"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <meta name="generator" content="Doxygen 1.8.15"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> <title>Crypto++: cmac.h Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="dynsections.js"></script> <link href="doxygen.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td id="projectalign" style="padding-left: 0.5em;"> <div id="projectname">Crypto++  <span id="projectnumber">7.0</span> </div> <div id="projectbrief">Free C++ class library of cryptographic schemes</div> </td> </tr> </tbody> </table> </div> <!-- end header part --> <!-- Generated by Doxygen 1.8.15 --> <script type="text/javascript" src="menudata.js"></script> <script type="text/javascript" src="menu.js"></script> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ $(function() { initMenu('',false,false,'search.php','Search'); }); /* @license-end */</script> <div id="main-nav"></div> </div><!-- top --> <div class="header"> <div class="headertitle"> <div class="title">cmac.h</div> </div> </div><!--header--> <div class="contents"> <a href="cmac_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment">// cmac.h - originally written and placed in the public domain by Wei Dai</span></div><div class="line"><a name="l00002"></a><span class="lineno"> 2</span> <span class="comment"></span></div><div class="line"><a name="l00003"></a><span class="lineno"> 3</span> <span class="comment">/// \file cmac.h</span></div><div class="line"><a name="l00004"></a><span class="lineno"> 4</span> <span class="comment">/// \brief Classes for CMAC message authentication code</span></div><div class="line"><a name="l00005"></a><span class="lineno"> 5</span> <span class="comment">/// \since Crypto++ 5.6.0</span></div><div class="line"><a name="l00006"></a><span class="lineno"> 6</span> <span class="comment"></span></div><div class="line"><a name="l00007"></a><span class="lineno"> 7</span> <span class="preprocessor">#ifndef CRYPTOPP_CMAC_H</span></div><div class="line"><a name="l00008"></a><span class="lineno"> 8</span> <span class="preprocessor">#define CRYPTOPP_CMAC_H</span></div><div class="line"><a name="l00009"></a><span class="lineno"> 9</span> </div><div class="line"><a name="l00010"></a><span class="lineno"> 10</span> <span class="preprocessor">#include "<a class="code" href="seckey_8h.html">seckey.h</a>"</span></div><div class="line"><a name="l00011"></a><span class="lineno"> 11</span> <span class="preprocessor">#include "<a class="code" href="secblock_8h.html">secblock.h</a>"</span></div><div class="line"><a name="l00012"></a><span class="lineno"> 12</span> </div><div class="line"><a name="l00013"></a><span class="lineno"> 13</span> NAMESPACE_BEGIN(<a class="code" href="namespace_crypto_p_p.html">CryptoPP</a>)</div><div class="line"><a name="l00014"></a><span class="lineno"> 14</span> </div><div class="line"><a name="l00015"></a><span class="lineno"> 15</span> <span class="comment">/// \brief CMAC base implementation</span></div><div class="line"><a name="l00016"></a><span class="lineno"> 16</span> <span class="comment"></span><span class="comment">/// \since Crypto++ 5.6.0</span></div><div class="line"><a name="l00017"></a><span class="lineno"><a class="line" href="class_c_m_a_c___base.html"> 17</a></span> <span class="comment"></span>class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE <a class="code" href="class_c_m_a_c___base.html">CMAC_Base</a> : public <a class="code" href="class_message_authentication_code.html">MessageAuthenticationCode</a></div><div class="line"><a name="l00018"></a><span class="lineno"> 18</span> {</div><div class="line"><a name="l00019"></a><span class="lineno"> 19</span> <span class="keyword">public</span>:</div><div class="line"><a name="l00020"></a><span class="lineno"> 20</span>  <a class="code" href="class_c_m_a_c___base.html">CMAC_Base</a>() : m_counter(0) {}</div><div class="line"><a name="l00021"></a><span class="lineno"> 21</span> </div><div class="line"><a name="l00022"></a><span class="lineno"> 22</span>  <span class="keywordtype">void</span> UncheckedSetKey(<span class="keyword">const</span> <span class="keywordtype">byte</span> *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length, <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html">NameValuePairs</a> &params);</div><div class="line"><a name="l00023"></a><span class="lineno"> 23</span>  <span class="keywordtype">void</span> Update(<span class="keyword">const</span> <span class="keywordtype">byte</span> *input, <span class="keywordtype">size_t</span> length);</div><div class="line"><a name="l00024"></a><span class="lineno"> 24</span>  <span class="keywordtype">void</span> TruncatedFinal(<span class="keywordtype">byte</span> *mac, <span class="keywordtype">size_t</span> size);</div><div class="line"><a name="l00025"></a><span class="lineno"><a class="line" href="class_c_m_a_c___base.html#af8008ae4d6541f2a8aca5078d869f8bb"> 25</a></span>  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_c_m_a_c___base.html#af8008ae4d6541f2a8aca5078d869f8bb">DigestSize</a>()<span class="keyword"> const </span>{<span class="keywordflow">return</span> GetCipher().BlockSize();}</div><div class="line"><a name="l00026"></a><span class="lineno"><a class="line" href="class_c_m_a_c___base.html#a9af8a01e7e07ef698b0f2d45381e85f9"> 26</a></span>  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_c_m_a_c___base.html#a9af8a01e7e07ef698b0f2d45381e85f9">OptimalBlockSize</a>()<span class="keyword"> const </span>{<span class="keywordflow">return</span> GetCipher().BlockSize();}</div><div class="line"><a name="l00027"></a><span class="lineno"><a class="line" href="class_c_m_a_c___base.html#a37fab7ee3b6d92260d0edef94d034947"> 27</a></span>  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_c_m_a_c___base.html#a37fab7ee3b6d92260d0edef94d034947">OptimalDataAlignment</a>()<span class="keyword"> const </span>{<span class="keywordflow">return</span> GetCipher().OptimalDataAlignment();}</div><div class="line"><a name="l00028"></a><span class="lineno"> 28</span> </div><div class="line"><a name="l00029"></a><span class="lineno"> 29</span> <span class="keyword">protected</span>:</div><div class="line"><a name="l00030"></a><span class="lineno"> 30</span>  <span class="keyword">friend</span> <span class="keyword">class </span><a class="code" href="class_e_a_x___base.html">EAX_Base</a>;</div><div class="line"><a name="l00031"></a><span class="lineno"> 31</span> </div><div class="line"><a name="l00032"></a><span class="lineno"> 32</span>  <span class="keyword">const</span> <a class="code" href="class_block_cipher.html">BlockCipher</a> & GetCipher()<span class="keyword"> const </span>{<span class="keywordflow">return</span> const_cast<CMAC_Base*>(<span class="keyword">this</span>)->AccessCipher();}</div><div class="line"><a name="l00033"></a><span class="lineno"> 33</span>  <span class="keyword">virtual</span> <a class="code" href="class_block_cipher.html">BlockCipher</a> & AccessCipher() =0;</div><div class="line"><a name="l00034"></a><span class="lineno"> 34</span> </div><div class="line"><a name="l00035"></a><span class="lineno"> 35</span>  <span class="keywordtype">void</span> ProcessBuf();</div><div class="line"><a name="l00036"></a><span class="lineno"> 36</span>  <a class="code" href="class_sec_byte_block.html">SecByteBlock</a> m_reg;</div><div class="line"><a name="l00037"></a><span class="lineno"> 37</span>  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> m_counter;</div><div class="line"><a name="l00038"></a><span class="lineno"> 38</span> };</div><div class="line"><a name="l00039"></a><span class="lineno"> 39</span> <span class="comment"></span></div><div class="line"><a name="l00040"></a><span class="lineno"> 40</span> <span class="comment">/// \brief CMAC message authentication code</span></div><div class="line"><a name="l00041"></a><span class="lineno"> 41</span> <span class="comment">/// \tparam T block cipher</span></div><div class="line"><a name="l00042"></a><span class="lineno"> 42</span> <span class="comment">/// \details Template parameter T should be a class derived from BlockCipherDocumentation, for example AES, with a block size of 8, 16, or 32.</span></div><div class="line"><a name="l00043"></a><span class="lineno"> 43</span> <span class="comment">/// \sa <a href="http://www.cryptolounge.org/wiki/CMAC">CMAC</a></span></div><div class="line"><a name="l00044"></a><span class="lineno"> 44</span> <span class="comment">/// \since Crypto++ 5.6.0</span></div><div class="line"><a name="l00045"></a><span class="lineno"> 45</span> <span class="comment"></span><span class="keyword">template</span> <<span class="keyword">class</span> T></div><div class="line"><a name="l00046"></a><span class="lineno"><a class="line" href="class_c_m_a_c.html"> 46</a></span> <span class="keyword">class </span><a class="code" href="class_c_m_a_c.html">CMAC</a> : <span class="keyword">public</span> <a class="code" href="class_message_authentication_code_impl.html">MessageAuthenticationCodeImpl</a><CMAC_Base, CMAC<T> >, <span class="keyword">public</span> <a class="code" href="class_same_key_length_as.html">SameKeyLengthAs</a><T></div><div class="line"><a name="l00047"></a><span class="lineno"> 47</span> {</div><div class="line"><a name="l00048"></a><span class="lineno"> 48</span> <span class="keyword">public</span>:<span class="comment"></span></div><div class="line"><a name="l00049"></a><span class="lineno"> 49</span> <span class="comment"> /// \brief Construct a CMAC</span></div><div class="line"><a name="l00050"></a><span class="lineno"><a class="line" href="class_c_m_a_c.html#a4c3f1634a2f67075b7e539af4fff0eb4"> 50</a></span> <span class="comment"></span> <a class="code" href="class_c_m_a_c.html#a4c3f1634a2f67075b7e539af4fff0eb4">CMAC</a>() {}<span class="comment"></span></div><div class="line"><a name="l00051"></a><span class="lineno"> 51</span> <span class="comment"> /// \brief Construct a CMAC</span></div><div class="line"><a name="l00052"></a><span class="lineno"> 52</span> <span class="comment"> /// \param key the MAC key</span></div><div class="line"><a name="l00053"></a><span class="lineno"> 53</span> <span class="comment"> /// \param length the key size, in bytes</span></div><div class="line"><a name="l00054"></a><span class="lineno"><a class="line" href="class_c_m_a_c.html#ab095b5454838c35e1ccfe779c1c7cea5"> 54</a></span> <span class="comment"></span> <a class="code" href="class_c_m_a_c.html#ab095b5454838c35e1ccfe779c1c7cea5">CMAC</a>(<span class="keyword">const</span> <span class="keywordtype">byte</span> *key, <span class="keywordtype">size_t</span> length=<a class="code" href="class_same_key_length_as.html">SameKeyLengthAs<T>::DEFAULT_KEYLENGTH</a>)</div><div class="line"><a name="l00055"></a><span class="lineno"> 55</span>  {this-><a class="code" href="class_simple_keying_interface.html#adf3c29b3ef3af74788a58c7c49887fd7">SetKey</a>(key, length);}</div><div class="line"><a name="l00056"></a><span class="lineno"> 56</span> </div><div class="line"><a name="l00057"></a><span class="lineno"> 57</span>  <span class="keyword">static</span> std::string StaticAlgorithmName() {<span class="keywordflow">return</span> std::string(<span class="stringliteral">"CMAC("</span>) + T::StaticAlgorithmName() + <span class="stringliteral">")"</span>;}</div><div class="line"><a name="l00058"></a><span class="lineno"> 58</span> </div><div class="line"><a name="l00059"></a><span class="lineno"> 59</span> <span class="keyword">private</span>:</div><div class="line"><a name="l00060"></a><span class="lineno"> 60</span>  <a class="code" href="class_block_cipher.html">BlockCipher</a> & AccessCipher() {<span class="keywordflow">return</span> m_cipher;}</div><div class="line"><a name="l00061"></a><span class="lineno"> 61</span>  <span class="keyword">typename</span> T::Encryption m_cipher;</div><div class="line"><a name="l00062"></a><span class="lineno"> 62</span> };</div><div class="line"><a name="l00063"></a><span class="lineno"> 63</span> </div><div class="line"><a name="l00064"></a><span class="lineno"> 64</span> NAMESPACE_END</div><div class="line"><a name="l00065"></a><span class="lineno"> 65</span> </div><div class="line"><a name="l00066"></a><span class="lineno"> 66</span> <span class="preprocessor">#endif</span></div><div class="ttc" id="class_message_authentication_code_html"><div class="ttname"><a href="class_message_authentication_code.html">MessageAuthenticationCode</a></div><div class="ttdoc">Interface for message authentication codes.</div><div class="ttdef"><b>Definition:</b> <a href="cryptlib_8h_source.html#l01245">cryptlib.h:1245</a></div></div> <div class="ttc" id="class_simple_keying_interface_html_adf3c29b3ef3af74788a58c7c49887fd7"><div class="ttname"><a href="class_simple_keying_interface.html#adf3c29b3ef3af74788a58c7c49887fd7">SimpleKeyingInterface::SetKey</a></div><div class="ttdeci">virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)</div><div class="ttdoc">Sets or reset the key of this object.</div><div class="ttdef"><b>Definition:</b> <a href="cryptlib_8cpp_source.html#l00064">cryptlib.cpp:64</a></div></div> <div class="ttc" id="class_c_m_a_c___base_html_af8008ae4d6541f2a8aca5078d869f8bb"><div class="ttname"><a href="class_c_m_a_c___base.html#af8008ae4d6541f2a8aca5078d869f8bb">CMAC_Base::DigestSize</a></div><div class="ttdeci">unsigned int DigestSize() const</div><div class="ttdoc">Provides the digest size of the hash.</div><div class="ttdef"><b>Definition:</b> <a href="cmac_8h_source.html#l00025">cmac.h:25</a></div></div> <div class="ttc" id="class_e_a_x___base_html"><div class="ttname"><a href="class_e_a_x___base.html">EAX_Base</a></div><div class="ttdoc">EAX block cipher base implementation.</div><div class="ttdef"><b>Definition:</b> <a href="eax_8h_source.html#l00018">eax.h:18</a></div></div> <div class="ttc" id="class_sec_byte_block_html"><div class="ttname"><a href="class_sec_byte_block.html">SecByteBlock</a></div><div class="ttdoc">SecBlock<byte> typedef.</div><div class="ttdef"><b>Definition:</b> <a href="secblock_8h_source.html#l00822">secblock.h:822</a></div></div> <div class="ttc" id="class_block_cipher_html"><div class="ttname"><a href="class_block_cipher.html">BlockCipher</a></div><div class="ttdoc">Interface for one direction (encryption or decryption) of a block cipher.</div><div class="ttdef"><b>Definition:</b> <a href="cryptlib_8h_source.html#l01229">cryptlib.h:1229</a></div></div> <div class="ttc" id="class_c_m_a_c_html"><div class="ttname"><a href="class_c_m_a_c.html">CMAC</a></div><div class="ttdoc">CMAC message authentication code.</div><div class="ttdef"><b>Definition:</b> <a href="cmac_8h_source.html#l00046">cmac.h:46</a></div></div> <div class="ttc" id="secblock_8h_html"><div class="ttname"><a href="secblock_8h.html">secblock.h</a></div><div class="ttdoc">Classes and functions for secure memory allocations.</div></div> <div class="ttc" id="class_c_m_a_c___base_html_a37fab7ee3b6d92260d0edef94d034947"><div class="ttname"><a href="class_c_m_a_c___base.html#a37fab7ee3b6d92260d0edef94d034947">CMAC_Base::OptimalDataAlignment</a></div><div class="ttdeci">unsigned int OptimalDataAlignment() const</div><div class="ttdoc">Provides input and output data alignment for optimal performance.</div><div class="ttdef"><b>Definition:</b> <a href="cmac_8h_source.html#l00027">cmac.h:27</a></div></div> <div class="ttc" id="class_c_m_a_c_html_a4c3f1634a2f67075b7e539af4fff0eb4"><div class="ttname"><a href="class_c_m_a_c.html#a4c3f1634a2f67075b7e539af4fff0eb4">CMAC::CMAC</a></div><div class="ttdeci">CMAC()</div><div class="ttdoc">Construct a CMAC.</div><div class="ttdef"><b>Definition:</b> <a href="cmac_8h_source.html#l00050">cmac.h:50</a></div></div> <div class="ttc" id="seckey_8h_html"><div class="ttname"><a href="seckey_8h.html">seckey.h</a></div><div class="ttdoc">Classes and functions for implementing secret key algorithms.</div></div> <div class="ttc" id="class_c_m_a_c___base_html_a9af8a01e7e07ef698b0f2d45381e85f9"><div class="ttname"><a href="class_c_m_a_c___base.html#a9af8a01e7e07ef698b0f2d45381e85f9">CMAC_Base::OptimalBlockSize</a></div><div class="ttdeci">unsigned int OptimalBlockSize() const</div><div class="ttdoc">Provides the input block size most efficient for this hash.</div><div class="ttdef"><b>Definition:</b> <a href="cmac_8h_source.html#l00026">cmac.h:26</a></div></div> <div class="ttc" id="class_message_authentication_code_impl_html"><div class="ttname"><a href="class_message_authentication_code_impl.html">MessageAuthenticationCodeImpl</a></div><div class="ttdoc">Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication code...</div><div class="ttdef"><b>Definition:</b> <a href="seckey_8h_source.html#l00384">seckey.h:384</a></div></div> <div class="ttc" id="class_same_key_length_as_html"><div class="ttname"><a href="class_same_key_length_as.html">SameKeyLengthAs</a></div><div class="ttdoc">Provides key lengths based on another class's key length.</div><div class="ttdef"><b>Definition:</b> <a href="seckey_8h_source.html#l00240">seckey.h:240</a></div></div> <div class="ttc" id="class_c_m_a_c_html_ab095b5454838c35e1ccfe779c1c7cea5"><div class="ttname"><a href="class_c_m_a_c.html#ab095b5454838c35e1ccfe779c1c7cea5">CMAC::CMAC</a></div><div class="ttdeci">CMAC(const byte *key, size_t length=SameKeyLengthAs< T >::DEFAULT_KEYLENGTH)</div><div class="ttdoc">Construct a CMAC.</div><div class="ttdef"><b>Definition:</b> <a href="cmac_8h_source.html#l00054">cmac.h:54</a></div></div> <div class="ttc" id="class_c_m_a_c___base_html"><div class="ttname"><a href="class_c_m_a_c___base.html">CMAC_Base</a></div><div class="ttdoc">CMAC base implementation.</div><div class="ttdef"><b>Definition:</b> <a href="cmac_8h_source.html#l00017">cmac.h:17</a></div></div> <div class="ttc" id="namespace_crypto_p_p_html"><div class="ttname"><a href="namespace_crypto_p_p.html">CryptoPP</a></div><div class="ttdoc">Crypto++ library namespace.</div></div> <div class="ttc" id="class_name_value_pairs_html"><div class="ttname"><a href="class_name_value_pairs.html">NameValuePairs</a></div><div class="ttdoc">Interface for retrieving values given their names.</div><div class="ttdef"><b>Definition:</b> <a href="cryptlib_8h_source.html#l00290">cryptlib.h:290</a></div></div> </div><!-- fragment --></div><!-- contents --> <!-- start footer part --> <hr class="footer"/><address class="footer"><small> Generated on Wed Nov 27 2019 15:03:27 for Crypto++ by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/> </a> 1.8.15 </small></address> </body> </html>