📄 seal_8cpp-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>Crypto++: seal.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.2 --><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="classes.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>seal.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// seal.cpp - written and placed in the public domain by Wei Dai</span><a name="l00002"></a>00002 <span class="comment">// updated to SEAL 3.0 by Leonard Janke</span><a name="l00003"></a>00003 <a name="l00004"></a>00004 <span class="preprocessor">#include "pch.h"</span><a name="l00005"></a>00005 <a name="l00006"></a>00006 <span class="comment">// prevent Sun's CC compiler from including this file automatically</span><a name="l00007"></a>00007 <span class="preprocessor">#if !(defined(__SUNPRO_CC) && defined(CRYPTOPP_ITERHASH_H))</span><a name="l00008"></a>00008 <span class="preprocessor"></span><a name="l00009"></a>00009 <span class="preprocessor">#include "seal.h"</span><a name="l00010"></a>00010 <span class="preprocessor">#include "sha.h"</span><a name="l00011"></a>00011 <span class="preprocessor">#include "misc.h"</span><a name="l00012"></a>00012 <a name="l00013"></a>00013 NAMESPACE_BEGIN(CryptoPP)<a name="l00014"></a>00014 <a name="l00015"></a>00015 void SEAL_TestInstantiations()<a name="l00016"></a>00016 {<a name="l00017"></a>00017 <a class="code" href="struct_s_e_a_l.html" title="SEAL">SEAL<>::Encryption</a> x;<a name="l00018"></a>00018 }<a name="l00019"></a>00019 <a name="l00020"></a><a class="code" href="struct_s_e_a_l___gamma.html">00020</a> <span class="keyword">struct </span><a class="code" href="struct_s_e_a_l___gamma.html">SEAL_Gamma</a><a name="l00021"></a>00021 {<a name="l00022"></a><a class="code" href="struct_s_e_a_l___gamma.html#d594d9e976c4e8a5aec701a3b74b1d08">00022</a> <a class="code" href="struct_s_e_a_l___gamma.html#d594d9e976c4e8a5aec701a3b74b1d08">SEAL_Gamma</a>(<span class="keyword">const</span> byte *key)<a name="l00023"></a>00023 : <a class="code" href="struct_s_e_a_l___gamma.html#a1fb7299cea2ba9381ae5faae2c327ca">H</a>(5), <a class="code" href="struct_s_e_a_l___gamma.html#36e66acd2bcf2278edd7f713d1de4fd8">Z</a>(5), <a class="code" href="struct_s_e_a_l___gamma.html#06b05b66c10ca829b2676f0f4e7df33b">D</a>(16), <a class="code" href="struct_s_e_a_l___gamma.html#9fd193dfe51c32cd77dea11508f6fb11">lastIndex</a>(0xffffffff)<a name="l00024"></a>00024 {<a name="l00025"></a>00025 GetUserKey(<a class="code" href="cryptlib_8h.html#aeb92d42f5a6e27b8ba19f18d69d142bf0c3bb6c718cc7ff0bffef4e8f178c57">BIG_ENDIAN_ORDER</a>, <a class="code" href="struct_s_e_a_l___gamma.html#a1fb7299cea2ba9381ae5faae2c327ca">H</a>.<a class="code" href="class_sec_block.html#11a05906688172579cd3520816799446">begin</a>(), 5, key, 20);<a name="l00026"></a>00026 memset(<a class="code" href="struct_s_e_a_l___gamma.html#06b05b66c10ca829b2676f0f4e7df33b">D</a>, 0, 64);<a name="l00027"></a>00027 }<a name="l00028"></a>00028 <a name="l00029"></a>00029 word32 <a class="code" href="struct_s_e_a_l___gamma.html#2da8cbfbedff0adea3b9fb47631bc10a">Apply</a>(word32 i);<a name="l00030"></a>00030 <a name="l00031"></a><a class="code" href="struct_s_e_a_l___gamma.html#36e66acd2bcf2278edd7f713d1de4fd8">00031</a> <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">SecBlock<word32></a> <a class="code" href="struct_s_e_a_l___gamma.html#a1fb7299cea2ba9381ae5faae2c327ca">H</a>, <a class="code" href="struct_s_e_a_l___gamma.html#36e66acd2bcf2278edd7f713d1de4fd8">Z</a>, <a class="code" href="struct_s_e_a_l___gamma.html#06b05b66c10ca829b2676f0f4e7df33b">D</a>;<a name="l00032"></a><a class="code" href="struct_s_e_a_l___gamma.html#9fd193dfe51c32cd77dea11508f6fb11">00032</a> word32 <a class="code" href="struct_s_e_a_l___gamma.html#9fd193dfe51c32cd77dea11508f6fb11">lastIndex</a>;<a name="l00033"></a>00033 };<a name="l00034"></a>00034 <a name="l00035"></a><a class="code" href="struct_s_e_a_l___gamma.html#2da8cbfbedff0adea3b9fb47631bc10a">00035</a> word32 <a class="code" href="struct_s_e_a_l___gamma.html#2da8cbfbedff0adea3b9fb47631bc10a">SEAL_Gamma::Apply</a>(word32 i)<a name="l00036"></a>00036 {<a name="l00037"></a>00037 word32 shaIndex = i/5;<a name="l00038"></a>00038 <span class="keywordflow">if</span> (shaIndex != <a class="code" href="struct_s_e_a_l___gamma.html#9fd193dfe51c32cd77dea11508f6fb11">lastIndex</a>)<a name="l00039"></a>00039 {<a name="l00040"></a>00040 memcpy(<a class="code" href="struct_s_e_a_l___gamma.html#36e66acd2bcf2278edd7f713d1de4fd8">Z</a>, <a class="code" href="struct_s_e_a_l___gamma.html#a1fb7299cea2ba9381ae5faae2c327ca">H</a>, 20);<a name="l00041"></a>00041 <a class="code" href="struct_s_e_a_l___gamma.html#06b05b66c10ca829b2676f0f4e7df33b">D</a>[0] = shaIndex;<a name="l00042"></a>00042 <a class="code" href="class_s_h_a1.html#b8d330e8b8fdc39a5429ef3f056645da">SHA::Transform</a>(Z, <a class="code" href="struct_s_e_a_l___gamma.html#06b05b66c10ca829b2676f0f4e7df33b">D</a>);<a name="l00043"></a>00043 <a class="code" href="struct_s_e_a_l___gamma.html#9fd193dfe51c32cd77dea11508f6fb11">lastIndex</a> = shaIndex;<a name="l00044"></a>00044 }<a name="l00045"></a>00045 <span class="keywordflow">return</span> <a class="code" href="struct_s_e_a_l___gamma.html#36e66acd2bcf2278edd7f713d1de4fd8">Z</a>[i%5];<a name="l00046"></a>00046 }<a name="l00047"></a>00047 <a name="l00048"></a>00048 <span class="keyword">template</span> <<span class="keyword">class</span> B><a name="l00049"></a><a class="code" href="class_s_e_a_l___policy.html#fe09c6430e5ffa4edf50132a33953b3c">00049</a> <span class="keywordtype">void</span> <a class="code" href="class_s_e_a_l___policy.html#fe09c6430e5ffa4edf50132a33953b3c">SEAL_Policy<B>::CipherSetKey</a>(<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, <span class="keyword">const</span> byte *key, <span class="keywordtype">size_t</span> length)<a name="l00050"></a>00050 {<a name="l00051"></a>00051 m_insideCounter = m_outsideCounter = m_startCount = 0;<a name="l00052"></a>00052 <a name="l00053"></a>00053 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> L = params.<a class="code" href="class_name_value_pairs.html#7ddb654b7afcd1a04422a7b4b01366d9" title="get a named value with type int, with default">GetIntValueWithDefault</a>(<span class="stringliteral">"NumberOfOutputBitsPerPositionIndex"</span>, 32*1024);<a name="l00054"></a>00054 m_iterationsPerCount = L / 8192;<a name="l00055"></a>00055 <a name="l00056"></a>00056 <a class="code" href="struct_s_e_a_l___gamma.html">SEAL_Gamma</a> gamma(key);<a name="l00057"></a>00057 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;<a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="keywordflow">for</span> (i=0; i<512; i++)<a name="l00060"></a>00060 m_T[i] = gamma.<a class="code" href="struct_s_e_a_l___gamma.html#2da8cbfbedff0adea3b9fb47631bc10a">Apply</a>(i);<a name="l00061"></a>00061 <a name="l00062"></a>00062 <span class="keywordflow">for</span> (i=0; i<256; i++)<a name="l00063"></a>00063 m_S[i] = gamma.<a class="code" href="struct_s_e_a_l___gamma.html#2da8cbfbedff0adea3b9fb47631bc10a">Apply</a>(0x1000+i);<a name="l00064"></a>00064 <a name="l00065"></a>00065 m_R.<a class="code" href="class_sec_block.html#90d46e577c951d81a2d25a4742a3e979" title="change size, without preserving contents">New</a>(4*(L/8192));<a name="l00066"></a>00066 <a name="l00067"></a>00067 <span class="keywordflow">for</span> (i=0; i<m_R.<a class="code" href="class_sec_block.html#f5999bffe3193e62719cc0792b0282a7">size</a>(); i++)<a name="l00068"></a>00068 m_R[i] = gamma.<a class="code" href="struct_s_e_a_l___gamma.html#2da8cbfbedff0adea3b9fb47631bc10a">Apply</a>(0x2000+i);<a name="l00069"></a>00069 }<a name="l00070"></a>00070 <a name="l00071"></a>00071 <span class="keyword">template</span> <<span class="keyword">class</span> B><a name="l00072"></a><a class="code" href="class_s_e_a_l___policy.html#82d21a726d891590295b0ce1ded99335">00072</a> <span class="keywordtype">void</span> <a class="code" href="class_s_e_a_l___policy.html#82d21a726d891590295b0ce1ded99335">SEAL_Policy<B>::CipherResynchronize</a>(byte *keystreamBuffer, <span class="keyword">const</span> byte *IV)<a name="l00073"></a>00073 {<a name="l00074"></a>00074 m_outsideCounter = IV ? GetWord<word32>(<span class="keyword">false</span>, <a class="code" href="cryptlib_8h.html#aeb92d42f5a6e27b8ba19f18d69d142bf0c3bb6c718cc7ff0bffef4e8f178c57">BIG_ENDIAN_ORDER</a>, IV) : 0;<a name="l00075"></a>00075 m_startCount = m_outsideCounter;<a name="l00076"></a>00076 m_insideCounter = 0;<a name="l00077"></a>00077 }<a name="l00078"></a>00078 <a name="l00079"></a>00079 <span class="keyword">template</span> <<span class="keyword">class</span> B><a name="l00080"></a><a class="code" href="class_s_e_a_l___policy.html#78a9caa7b0b7feae0c9272994e2f4fdc">00080</a> <span class="keywordtype">void</span> <a class="code" href="class_s_e_a_l___policy.html#78a9caa7b0b7feae0c9272994e2f4fdc">SEAL_Policy<B>::SeekToIteration</a>(lword iterationCount)<a name="l00081"></a>00081 {<a name="l00082"></a>00082 m_outsideCounter = m_startCount + (<span class="keywordtype">unsigned</span> int)(iterationCount / m_iterationsPerCount);<a name="l00083"></a>00083 m_insideCounter = (<span class="keywordtype">unsigned</span> int)(iterationCount % m_iterationsPerCount);<a name="l00084"></a>00084 }<a name="l00085"></a>00085 <a name="l00086"></a>00086 <span class="keyword">template</span> <<span class="keyword">class</span> B><a name="l00087"></a><a class="code" href="class_s_e_a_l___policy.html#68ce4818503af3197075a66114a02690">00087</a> <span class="keywordtype">void</span> <a class="code" href="class_s_e_a_l___policy.html#68ce4818503af3197075a66114a02690">SEAL_Policy<B>::OperateKeystream</a>(<a class="code" href="strciphr_8h.html#b4a226527d2bd01ff19bfa14d0974227">KeystreamOperation</a> operation, byte *output, <span class="keyword">const</span> byte *input, <span class="keywordtype">size_t</span> iterationCount)<a name="l00088"></a>00088 {<a name="l00089"></a>00089 word32 a, b, c, d, n1, n2, n3, n4;<a name="l00090"></a>00090 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> p, q;<a name="l00091"></a>00091 <a name="l00092"></a>00092 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> iteration = 0; iteration < iterationCount; ++iteration)<a name="l00093"></a>00093 {<a name="l00094"></a>00094 #define Ttab(x) *(word32 *)((byte *)m_T.<a class="code" href="class_sec_block.html#11a05906688172579cd3520816799446">begin</a>()+x)<a name="l00095"></a>00095 <a name="l00096"></a>00096 a = m_outsideCounter ^ m_R[4*m_insideCounter];<a name="l00097"></a>00097 b = rotrFixed(m_outsideCounter, 8U) ^ m_R[4*m_insideCounter+1];<a name="l00098"></a>00098 c = rotrFixed(m_outsideCounter, 16U) ^ m_R[4*m_insideCounter+2];<a name="l00099"></a>00099 d = rotrFixed(m_outsideCounter, 24U) ^ m_R[4*m_insideCounter+3];<a name="l00100"></a>00100 <a name="l00101"></a>00101 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> j=0; j<2; j++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -