<!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++: emsa2.cpp 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">emsa2.cpp</div> </div> </div><!--header--> <div class="contents"> <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment">// emsa2.cpp - 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> </div><div class="line"><a name="l00003"></a><span class="lineno"> 3</span> <span class="preprocessor">#include "<a class="code" href="pch_8h.html">pch.h</a>"</span></div><div class="line"><a name="l00004"></a><span class="lineno"> 4</span> <span class="preprocessor">#include "<a class="code" href="emsa2_8h.html">emsa2.h</a>"</span></div><div class="line"><a name="l00005"></a><span class="lineno"> 5</span> </div><div class="line"><a name="l00006"></a><span class="lineno"> 6</span> <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span></div><div class="line"><a name="l00007"></a><span class="lineno"> 7</span> </div><div class="line"><a name="l00008"></a><span class="lineno"> 8</span> NAMESPACE_BEGIN(<a class="code" href="namespace_crypto_p_p.html">CryptoPP</a>)</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="comment">// Inclusion based on DLL due to Clang, http://github.com/weidai11/cryptopp/issues/300</span></div><div class="line"><a name="l00011"></a><span class="lineno"> 11</span> <span class="preprocessor">#ifndef CRYPTOPP_IS_DLL</span></div><div class="line"><a name="l00012"></a><span class="lineno"> 12</span> <span class="keyword">template</span><> <span class="keyword">const</span> <span class="keywordtype">byte</span> <a class="code" href="class_e_m_s_a2_hash_id.html">EMSA2HashId<SHA1>::id</a> = 0x33;</div><div class="line"><a name="l00013"></a><span class="lineno"> 13</span> <span class="keyword">template</span><> <span class="keyword">const</span> <span class="keywordtype">byte</span> <a class="code" href="class_e_m_s_a2_hash_id.html">EMSA2HashId<SHA224>::id</a> = 0x38;</div><div class="line"><a name="l00014"></a><span class="lineno"> 14</span> <span class="keyword">template</span><> <span class="keyword">const</span> <span class="keywordtype">byte</span> <a class="code" href="class_e_m_s_a2_hash_id.html">EMSA2HashId<SHA256>::id</a> = 0x34;</div><div class="line"><a name="l00015"></a><span class="lineno"> 15</span> <span class="keyword">template</span><> <span class="keyword">const</span> <span class="keywordtype">byte</span> <a class="code" href="class_e_m_s_a2_hash_id.html">EMSA2HashId<SHA384>::id</a> = 0x36;</div><div class="line"><a name="l00016"></a><span class="lineno"> 16</span> <span class="keyword">template</span><> <span class="keyword">const</span> <span class="keywordtype">byte</span> <a class="code" href="class_e_m_s_a2_hash_id.html">EMSA2HashId<SHA512>::id</a> = 0x35;</div><div class="line"><a name="l00017"></a><span class="lineno"> 17</span> <span class="preprocessor">#endif</span></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="keywordtype">void</span> EMSA2Pad::ComputeMessageRepresentative(<a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a>& <span class="comment">/*rng*/</span>,</div><div class="line"><a name="l00020"></a><span class="lineno"> 20</span>  <span class="keyword">const</span> <span class="keywordtype">byte</span>* recoverableMessage, <span class="keywordtype">size_t</span> recoverableMessageLength,</div><div class="line"><a name="l00021"></a><span class="lineno"> 21</span>  <a class="code" href="class_hash_transformation.html">HashTransformation</a> &hash, HashIdentifier hashIdentifier, <span class="keywordtype">bool</span> messageEmpty,</div><div class="line"><a name="l00022"></a><span class="lineno"> 22</span>  <span class="keywordtype">byte</span> *representative, <span class="keywordtype">size_t</span> representativeBitLength)<span class="keyword"> const</span></div><div class="line"><a name="l00023"></a><span class="lineno"> 23</span> <span class="keyword"></span>{</div><div class="line"><a name="l00024"></a><span class="lineno"> 24</span>  CRYPTOPP_UNUSED(recoverableMessage), CRYPTOPP_UNUSED(recoverableMessageLength), CRYPTOPP_UNUSED(representativeBitLength);</div><div class="line"><a name="l00025"></a><span class="lineno"> 25</span>  <a class="code" href="trap_8h.html#adf3b392588bc94cbeae9f415a78c7b95">CRYPTOPP_ASSERT</a>(representativeBitLength >= MinRepresentativeBitLength(hashIdentifier.second, hash.<a class="code" href="class_hash_transformation.html#a9f42cc280bac76b884ad12615c0dabb5">DigestSize</a>()));</div><div class="line"><a name="l00026"></a><span class="lineno"> 26</span> </div><div class="line"><a name="l00027"></a><span class="lineno"> 27</span>  <span class="keywordflow">if</span> (representativeBitLength % 8 != 7)</div><div class="line"><a name="l00028"></a><span class="lineno"> 28</span>  <span class="keywordflow">throw</span> <a class="code" href="class_p_k___signature_scheme_1_1_invalid_key_length.html">PK_SignatureScheme::InvalidKeyLength</a>(<span class="stringliteral">"EMSA2: EMSA2 requires a key length that is a multiple of 8"</span>);</div><div class="line"><a name="l00029"></a><span class="lineno"> 29</span> </div><div class="line"><a name="l00030"></a><span class="lineno"> 30</span>  <span class="keywordtype">size_t</span> digestSize = hash.<a class="code" href="class_hash_transformation.html#a9f42cc280bac76b884ad12615c0dabb5">DigestSize</a>();</div><div class="line"><a name="l00031"></a><span class="lineno"> 31</span>  <span class="keywordtype">size_t</span> representativeByteLength = <a class="code" href="misc_8h.html#a7c5d30b17c9d4869fdc3458dd05c500d">BitsToBytes</a>(representativeBitLength);</div><div class="line"><a name="l00032"></a><span class="lineno"> 32</span> </div><div class="line"><a name="l00033"></a><span class="lineno"> 33</span>  representative[0] = messageEmpty ? 0x4b : 0x6b;</div><div class="line"><a name="l00034"></a><span class="lineno"> 34</span>  memset(representative+1, 0xbb, representativeByteLength-digestSize-4); <span class="comment">// pad with 0xbb</span></div><div class="line"><a name="l00035"></a><span class="lineno"> 35</span>  <span class="keywordtype">byte</span> *afterP2 = representative+representativeByteLength-digestSize-3;</div><div class="line"><a name="l00036"></a><span class="lineno"> 36</span>  afterP2[0] = 0xba;</div><div class="line"><a name="l00037"></a><span class="lineno"> 37</span>  hash.<a class="code" href="class_hash_transformation.html#aa0b8c7a110d8968268fd02ec32b9a8e8">Final</a>(afterP2+1);</div><div class="line"><a name="l00038"></a><span class="lineno"> 38</span>  representative[representativeByteLength-2] = *hashIdentifier.first;</div><div class="line"><a name="l00039"></a><span class="lineno"> 39</span>  representative[representativeByteLength-1] = 0xcc;</div><div class="line"><a name="l00040"></a><span class="lineno"> 40</span> }</div><div class="line"><a name="l00041"></a><span class="lineno"> 41</span> </div><div class="line"><a name="l00042"></a><span class="lineno"> 42</span> NAMESPACE_END</div><div class="line"><a name="l00043"></a><span class="lineno"> 43</span> </div><div class="line"><a name="l00044"></a><span class="lineno"> 44</span> <span class="preprocessor">#endif</span></div><div class="ttc" id="emsa2_8h_html"><div class="ttname"><a href="emsa2_8h.html">emsa2.h</a></div><div class="ttdoc">Classes and functions for various padding schemes used in public key algorithms.</div></div> <div class="ttc" id="misc_8h_html_a7c5d30b17c9d4869fdc3458dd05c500d"><div class="ttname"><a href="misc_8h.html#a7c5d30b17c9d4869fdc3458dd05c500d">BitsToBytes</a></div><div class="ttdeci">size_t BitsToBytes(size_t bitCount)</div><div class="ttdoc">Returns the number of 8-bit bytes or octets required for the specified number of bits.</div><div class="ttdef"><b>Definition:</b> <a href="misc_8h_source.html#l00790">misc.h:790</a></div></div> <div class="ttc" id="class_random_number_generator_html"><div class="ttname"><a href="class_random_number_generator.html">RandomNumberGenerator</a></div><div class="ttdoc">Interface for random number generators.</div><div class="ttdef"><b>Definition:</b> <a href="cryptlib_8h_source.html#l01330">cryptlib.h:1330</a></div></div> <div class="ttc" id="class_e_m_s_a2_hash_id_html"><div class="ttname"><a href="class_e_m_s_a2_hash_id.html">EMSA2HashId</a></div><div class="ttdoc">EMSA2 hash identifier.</div><div class="ttdef"><b>Definition:</b> <a href="emsa2_8h_source.html#l00023">emsa2.h:23</a></div></div> <div class="ttc" id="pch_8h_html"><div class="ttname"><a href="pch_8h.html">pch.h</a></div><div class="ttdoc">Precompiled header file.</div></div> <div class="ttc" id="class_p_k___signature_scheme_1_1_invalid_key_length_html"><div class="ttname"><a href="class_p_k___signature_scheme_1_1_invalid_key_length.html">PK_SignatureScheme::InvalidKeyLength</a></div><div class="ttdoc">Exception throw when the private or public key has a length that can't be used.</div><div class="ttdef"><b>Definition:</b> <a href="cryptlib_8h_source.html#l02619">cryptlib.h:2619</a></div></div> <div class="ttc" id="trap_8h_html_adf3b392588bc94cbeae9f415a78c7b95"><div class="ttname"><a href="trap_8h.html#adf3b392588bc94cbeae9f415a78c7b95">CRYPTOPP_ASSERT</a></div><div class="ttdeci">#define CRYPTOPP_ASSERT(exp)</div><div class="ttdoc">Debugging and diagnostic assertion.</div><div class="ttdef"><b>Definition:</b> <a href="trap_8h_source.html#l00060">trap.h:60</a></div></div> <div class="ttc" id="class_hash_transformation_html_a9f42cc280bac76b884ad12615c0dabb5"><div class="ttname"><a href="class_hash_transformation.html#a9f42cc280bac76b884ad12615c0dabb5">HashTransformation::DigestSize</a></div><div class="ttdeci">virtual unsigned int DigestSize() const =0</div><div class="ttdoc">Provides the digest size of the hash.</div></div> <div class="ttc" id="class_hash_transformation_html"><div class="ttname"><a href="class_hash_transformation.html">HashTransformation</a></div><div class="ttdoc">Interface for hash functions and data processing part of MACs.</div><div class="ttdef"><b>Definition:</b> <a href="cryptlib_8h_source.html#l01065">cryptlib.h:1065</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_hash_transformation_html_aa0b8c7a110d8968268fd02ec32b9a8e8"><div class="ttname"><a href="class_hash_transformation.html#aa0b8c7a110d8968268fd02ec32b9a8e8">HashTransformation::Final</a></div><div class="ttdeci">virtual void Final(byte *digest)</div><div class="ttdoc">Computes the hash of the current message.</div><div class="ttdef"><b>Definition:</b> <a href="cryptlib_8h_source.html#l01095">cryptlib.h:1095</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:30 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>