pkcspad_8cpp-source.html

来自「Crypto++是一个非常强大的密码学库,主要是功能全」· HTML 代码 · 共 165 行

HTML
165
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Crypto++: pkcspad.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.2 --><div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Compound&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Compound&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div><h1>pkcspad.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// pkcspad.cpp - written and placed in the public domain by Wei Dai</span>00002 00003 <span class="preprocessor">#include "pch.h"</span>00004 00005 <span class="preprocessor">#include "pkcspad.h"</span>00006 <span class="preprocessor">#include &lt;assert.h&gt;</span>00007 00008 NAMESPACE_BEGIN(CryptoPP)00009 00010 <span class="keyword">template</span>&lt;&gt; <span class="keyword">const</span> byte PKCS_DigestDecoration&lt;MD2&gt;::decoration[] = {0x30,0x20,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x02,0x05,0x00,0x04,0x10};00011 <span class="keyword">template</span>&lt;&gt; <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> PKCS_DigestDecoration&lt;MD2&gt;::length = <span class="keyword">sizeof</span>(PKCS_DigestDecoration&lt;MD2&gt;::decoration);00012 00013 <span class="keyword">template</span>&lt;&gt; <span class="keyword">const</span> byte PKCS_DigestDecoration&lt;MD5&gt;::decoration[] = {0x30,0x20,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x04,0x10};00014 <span class="keyword">template</span>&lt;&gt; <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> PKCS_DigestDecoration&lt;MD5&gt;::length = <span class="keyword">sizeof</span>(PKCS_DigestDecoration&lt;MD5&gt;::decoration);00015 00016 <span class="keyword">template</span>&lt;&gt; <span class="keyword">const</span> byte PKCS_DigestDecoration&lt;RIPEMD160&gt;::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x24,0x03,0x02,0x01,0x05,0x00,0x04,0x14};00017 <span class="keyword">template</span>&lt;&gt; <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> PKCS_DigestDecoration&lt;RIPEMD160&gt;::length = <span class="keyword">sizeof</span>(PKCS_DigestDecoration&lt;RIPEMD160&gt;::decoration);00018 00019 <span class="keyword">template</span>&lt;&gt; <span class="keyword">const</span> byte PKCS_DigestDecoration&lt;SHA256&gt;::decoration[] = {0x30,0x31,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x01,0x05,0x00,0x04,0x20};00020 <span class="keyword">template</span>&lt;&gt; <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> PKCS_DigestDecoration&lt;SHA256&gt;::length = <span class="keyword">sizeof</span>(PKCS_DigestDecoration&lt;SHA256&gt;::decoration);00021 00022 <span class="keyword">template</span>&lt;&gt; <span class="keyword">const</span> byte PKCS_DigestDecoration&lt;SHA384&gt;::decoration[] = {0x30,0x41,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x02,0x05,0x00,0x04,0x30};00023 <span class="keyword">template</span>&lt;&gt; <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> PKCS_DigestDecoration&lt;SHA384&gt;::length = <span class="keyword">sizeof</span>(PKCS_DigestDecoration&lt;SHA384&gt;::decoration);00024 00025 <span class="keyword">template</span>&lt;&gt; <span class="keyword">const</span> byte PKCS_DigestDecoration&lt;SHA512&gt;::decoration[] = {0x30,0x51,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x03,0x05,0x00,0x04,0x40};00026 <span class="keyword">template</span>&lt;&gt; <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> PKCS_DigestDecoration&lt;SHA512&gt;::length = <span class="keyword">sizeof</span>(PKCS_DigestDecoration&lt;SHA512&gt;::decoration);00027 00028 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> PKCS_EncryptionPaddingScheme::MaxUnpaddedLength(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> paddedLength)<span class="keyword"> const</span>00029 <span class="keyword"></span>{00030         <span class="keywordflow">return</span> paddedLength/8 &gt; 10 ? paddedLength/8-10 : 0;00031 }00032 00033 <span class="keywordtype">void</span> PKCS_EncryptionPaddingScheme::Pad(<a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &amp;rng, <span class="keyword">const</span> byte *input, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> inputLen, byte *pkcsBlock, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pkcsBlockLen)<span class="keyword"> const</span>00034 <span class="keyword"></span>{00035         assert (inputLen &lt;= MaxUnpaddedLength(pkcsBlockLen));   <span class="comment">// this should be checked by caller</span>00036 00037         <span class="comment">// convert from bit length to byte length</span>00038         <span class="keywordflow">if</span> (pkcsBlockLen % 8 != 0)00039         {00040                 pkcsBlock[0] = 0;00041                 pkcsBlock++;00042         }00043         pkcsBlockLen /= 8;00044 00045         pkcsBlock[0] = 2;  <span class="comment">// block type 2</span>00046 00047         <span class="comment">// pad with non-zero random bytes</span>00048         <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 1; i &lt; pkcsBlockLen-inputLen-1; i++)00049                 pkcsBlock[i] = (byte)rng.<a class="code" href="class_random_number_generator.html#_x917_r_n_ga3">GenerateWord32</a>(1, 0xff);00050 00051         pkcsBlock[pkcsBlockLen-inputLen-1] = 0;     <span class="comment">// separator</span>00052         memcpy(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen);00053 }00054 00055 <a class="code" href="struct_decoding_result.html">DecodingResult</a> PKCS_EncryptionPaddingScheme::Unpad(<span class="keyword">const</span> byte *pkcsBlock, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pkcsBlockLen, byte *output)<span class="keyword"> const</span>00056 <span class="keyword"></span>{00057         <span class="keywordtype">bool</span> invalid = <span class="keyword">false</span>;00058         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> maxOutputLen = MaxUnpaddedLength(pkcsBlockLen);00059 00060         <span class="comment">// convert from bit length to byte length</span>00061         <span class="keywordflow">if</span> (pkcsBlockLen % 8 != 0)00062         {00063                 invalid = (pkcsBlock[0] != 0) || invalid;00064                 pkcsBlock++;00065         }00066         pkcsBlockLen /= 8;00067 00068         <span class="comment">// Require block type 2.</span>00069         invalid = (pkcsBlock[0] != 2) || invalid;00070 00071         <span class="comment">// skip past the padding until we find the seperator</span>00072         <span class="keywordtype">unsigned</span> i=1;00073         <span class="keywordflow">while</span> (i&lt;pkcsBlockLen &amp;&amp; pkcsBlock[i++]) { <span class="comment">// null body</span>00074                 }00075         assert(i==pkcsBlockLen || pkcsBlock[i-1]==0);00076 00077         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> outputLen = pkcsBlockLen - i;00078         invalid = (outputLen &gt; maxOutputLen) || invalid;00079 00080         <span class="keywordflow">if</span> (invalid)00081                 <span class="keywordflow">return</span> <a class="code" href="struct_decoding_result.html">DecodingResult</a>();00082 00083         memcpy (output, pkcsBlock+i, outputLen);00084         <span class="keywordflow">return</span> <a class="code" href="struct_decoding_result.html">DecodingResult</a>(outputLen);00085 }00086 00087 <span class="comment">// ********************************************************</span>00088 00089 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span>00090 <span class="preprocessor"></span>00091 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> PKCS_SignaturePaddingScheme::MaxUnpaddedLength(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> paddedLength)<span class="keyword"> const</span>00092 <span class="keyword"></span>{00093         <span class="keywordflow">return</span> paddedLength/8 &gt; 10 ? paddedLength/8-10 : 0;00094 }00095 00096 <span class="keywordtype">void</span> PKCS_SignaturePaddingScheme::Pad(<a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &amp;, <span class="keyword">const</span> byte *input, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> inputLen, byte *pkcsBlock, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pkcsBlockLen)<span class="keyword"> const</span>00097 <span class="keyword"></span>{00098         assert (inputLen &lt;= MaxUnpaddedLength(pkcsBlockLen));   <span class="comment">// this should be checked by caller</span>00099 00100         <span class="comment">// convert from bit length to byte length</span>00101         <span class="keywordflow">if</span> (pkcsBlockLen % 8 != 0)00102         {00103                 pkcsBlock[0] = 0;00104                 pkcsBlock++;00105         }00106         pkcsBlockLen /= 8;00107 00108         pkcsBlock[0] = 1;   <span class="comment">// block type 1</span>00109 00110         <span class="comment">// padd with 0xff</span>00111         memset(pkcsBlock+1, 0xff, pkcsBlockLen-inputLen-2);00112 00113         pkcsBlock[pkcsBlockLen-inputLen-1] = 0;               <span class="comment">// separator</span>00114         memcpy(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen);00115 }00116 00117 <a class="code" href="struct_decoding_result.html">DecodingResult</a> PKCS_SignaturePaddingScheme::Unpad(<span class="keyword">const</span> byte *pkcsBlock, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pkcsBlockLen, byte *output)<span class="keyword"> const</span>00118 <span class="keyword"></span>{00119         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> maxOutputLen = MaxUnpaddedLength(pkcsBlockLen);00120 00121         <span class="comment">// convert from bit length to byte length</span>00122         <span class="keywordflow">if</span> (pkcsBlockLen % 8 != 0)00123         {00124                 <span class="keywordflow">if</span> (pkcsBlock[0] != 0)00125                         <span class="keywordflow">return</span> <a class="code" href="struct_decoding_result.html">DecodingResult</a>();00126                 pkcsBlock++;00127         }00128         pkcsBlockLen /= 8;00129 00130         <span class="comment">// Require block type 1.</span>00131         <span class="keywordflow">if</span> (pkcsBlock[0] != 1)00132                 <span class="keywordflow">return</span> <a class="code" href="struct_decoding_result.html">DecodingResult</a>();00133 00134         <span class="comment">// skip past the padding until we find the seperator</span>00135         <span class="keywordtype">unsigned</span> i=1;00136         <span class="keywordflow">while</span> (i&lt;pkcsBlockLen &amp;&amp; pkcsBlock[i++])00137                 <span class="keywordflow">if</span> (pkcsBlock[i-1] != 0xff)     <span class="comment">// not valid padding</span>00138                         <span class="keywordflow">return</span> <a class="code" href="struct_decoding_result.html">DecodingResult</a>();00139         assert(i==pkcsBlockLen || pkcsBlock[i-1]==0);00140 00141         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> outputLen = pkcsBlockLen - i;00142         <span class="keywordflow">if</span> (outputLen &gt; maxOutputLen)00143                 <span class="keywordflow">return</span> <a class="code" href="struct_decoding_result.html">DecodingResult</a>();00144 00145         memcpy (output, pkcsBlock+i, outputLen);00146         <span class="keywordflow">return</span> <a class="code" href="struct_decoding_result.html">DecodingResult</a>(outputLen);00147 }00148 00149 <span class="preprocessor">#endif</span>00150 <span class="preprocessor"></span>00151 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:21 2003 for Crypto++ by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.2 </small></address></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?