📄 seal_8cpp-source.html
字号:
<!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++: seal.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 Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Compound List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Compound Members</a> | <a class="qindex" href="globals.html">File Members</a></div><h1>seal.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// seal.cpp - written and placed in the public domain by Wei Dai</span>00002 <span class="comment">// updated to SEAL 3.0 by Leonard Janke</span>00003 00004 <span class="preprocessor">#include "pch.h"</span>00005 <span class="preprocessor">#include "seal.h"</span>00006 <span class="preprocessor">#include "sha.h"</span>00007 <span class="preprocessor">#include "misc.h"</span>00008 00009 <span class="preprocessor">#include "strciphr.cpp"</span>00010 00011 NAMESPACE_BEGIN(CryptoPP)00012 00013 <span class="keywordtype">void</span> SEAL_TestInstantiations()00014 {00015 <a class="code" href="struct_s_e_a_l.html">SEAL<></a>::Encryption x;00016 }00017 00018 <span class="keyword">struct </span>SEAL_Gamma00019 {00020 SEAL_Gamma(<span class="keyword">const</span> byte *key)00021 : H(5), Z(5), D(16), lastIndex(0xffffffff)00022 {00023 GetUserKey(BIG_ENDIAN_ORDER, H.begin(), 5, key, 20);00024 memset(D, 0, 64);00025 }00026 00027 word32 Apply(word32 i);00028 00029 <a class="code" href="class_sec_block.html">SecBlock<word32></a> H, Z, D;00030 word32 lastIndex;00031 };00032 00033 word32 SEAL_Gamma::Apply(word32 i)00034 {00035 word32 shaIndex = i/5;00036 <span class="keywordflow">if</span> (shaIndex != lastIndex)00037 {00038 memcpy(Z, H, 20);00039 D[0] = shaIndex;00040 SHA::Transform(Z, D);00041 lastIndex = shaIndex;00042 }00043 <span class="keywordflow">return</span> Z[i%5];00044 }00045 00046 <span class="keyword">template</span> <<span class="keyword">class</span> B>00047 <span class="keywordtype">void</span> SEAL_Policy<B>::CipherSetKey(<span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html">NameValuePairs</a> &params, <span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00048 {00049 m_insideCounter = m_outsideCounter = m_startCount = 0;00050 00051 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> L = params.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha43">GetIntValueWithDefault</a>(<span class="stringliteral">"NumberOfOutputBitsPerPositionIndex"</span>, 32*1024);00052 m_iterationsPerCount = L / 8192;00053 00054 SEAL_Gamma gamma(key);00055 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;00056 00057 <span class="keywordflow">for</span> (i=0; i<512; i++)00058 m_T[i] = gamma.Apply(i);00059 00060 <span class="keywordflow">for</span> (i=0; i<256; i++)00061 m_S[i] = gamma.Apply(0x1000+i);00062 00063 m_R.New(4*(L/8192));00064 00065 <span class="keywordflow">for</span> (i=0; i<m_R.size(); i++)00066 m_R[i] = gamma.Apply(0x2000+i);00067 }00068 00069 <span class="keyword">template</span> <<span class="keyword">class</span> B>00070 <span class="keywordtype">void</span> SEAL_Policy<B>::CipherResynchronize(byte *keystreamBuffer, <span class="keyword">const</span> byte *IV)00071 {00072 m_outsideCounter = UnalignedGetWord<word32>(BIG_ENDIAN_ORDER, <a class="code" href="namespace_name.html#a4">IV</a>);00073 m_startCount = m_outsideCounter;00074 m_insideCounter = 0;00075 }00076 00077 <span class="keyword">template</span> <<span class="keyword">class</span> B>00078 <span class="keywordtype">void</span> SEAL_Policy<B>::SeekToIteration(dword iterationCount)00079 {00080 m_outsideCounter = m_startCount + iterationCount / m_iterationsPerCount;00081 m_insideCounter = iterationCount % m_iterationsPerCount;00082 }00083 00084 <span class="keyword">template</span> <<span class="keyword">class</span> B>00085 <span class="keywordtype">void</span> SEAL_Policy<B>::OperateKeystream(KeystreamOperation operation, byte *output, <span class="keyword">const</span> byte *input, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> iterationCount)00086 {00087 KeystreamOutput<B> keystreamOutput(operation, output, input);00088 word32 a, b, c, d, n1, n2, n3, n4;00089 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> p, q;00090 00091 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> iteration = 0; iteration < iterationCount; ++iteration)00092 {00093 <span class="preprocessor">#define Ttab(x) *(word32 *)((byte *)m_T.begin()+x)</span>00094 <span class="preprocessor"></span>00095 a = m_outsideCounter ^ m_R[4*m_insideCounter];00096 b = rotrFixed(m_outsideCounter, 8U) ^ m_R[4*m_insideCounter+1];00097 c = rotrFixed(m_outsideCounter, 16U) ^ m_R[4*m_insideCounter+2];00098 d = rotrFixed(m_outsideCounter, 24U) ^ m_R[4*m_insideCounter+3];00099 00100 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> j=0; j<2; j++)00101 {00102 p = a & 0x7fc;00103 b += Ttab(p);00104 a = rotrFixed(a, 9U);00105 00106 p = b & 0x7fc;00107 c += Ttab(p);00108 b = rotrFixed(b, 9U);00109 00110 p = c & 0x7fc;00111 d += Ttab(p);00112 c = rotrFixed(c, 9U);00113 00114 p = d & 0x7fc;00115 a += Ttab(p);00116 d = rotrFixed(d, 9U);00117 }00118 00119 n1 = d, n2 = b, n3 = a, n4 = c;00120 00121 p = a & 0x7fc;00122 b += Ttab(p);00123 a = rotrFixed(a, 9U);00124 00125 p = b & 0x7fc;00126 c += Ttab(p);00127 b = rotrFixed(b, 9U);00128 00129 p = c & 0x7fc;00130 d += Ttab(p);00131 c = rotrFixed(c, 9U);00132 00133 p = d & 0x7fc;00134 a += Ttab(p);00135 d = rotrFixed(d, 9U);00136 00137 <span class="comment">// generate 8192 bits</span>00138 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i<64; i++)00139 {00140 p = a & 0x7fc;00141 a = rotrFixed(a, 9U);00142 b += Ttab(p);00143 b ^= a;00144 00145 q = b & 0x7fc;00146 b = rotrFixed(b, 9U);00147 c ^= Ttab(q);00148 c += b;00149 00150 p = (p+c) & 0x7fc;00151 c = rotrFixed(c, 9U);00152 d += Ttab(p);00153 d ^= c;00154 00155 q = (q+d) & 0x7fc;00156 d = rotrFixed(d, 9U);00157 a ^= Ttab(q);00158 a += d;00159 00160 p = (p+a) & 0x7fc;00161 b ^= Ttab(p);00162 a = rotrFixed(a, 9U);00163 00164 q = (q+b) & 0x7fc;00165 c += Ttab(q);00166 b = rotrFixed(b, 9U);00167 00168 p = (p+c) & 0x7fc;00169 d ^= Ttab(p);00170 c = rotrFixed(c, 9U);00171 00172 q = (q+d) & 0x7fc;00173 d = rotrFixed(d, 9U);00174 a += Ttab(q);00175 00176 keystreamOutput (b + m_S[4*i+0])00177 (c ^ m_S[4*i+1])00178 (d + m_S[4*i+2])00179 (a ^ m_S[4*i+3]);00180 00181 <span class="keywordflow">if</span> (i & 1)00182 {00183 a += n3;00184 b += n4;00185 c ^= n3;00186 d ^= n4;00187 }00188 <span class="keywordflow">else</span>00189 {00190 a += n1;00191 b += n2; 00192 c ^= n1;00193 d ^= n2;00194 }00195 }00196 00197 <span class="keywordflow">if</span> (++m_insideCounter == m_iterationsPerCount)00198 {00199 ++m_outsideCounter;00200 m_insideCounter = 0;00201 }00202 }00203 00204 a = b = c = d = n1 = n2 = n3 = n4 = 0;00205 p = q = 0;00206 }00207 00208 <span class="keyword">template</span> <span class="keyword">class </span>SEAL_Policy<BigEndian>;00209 <span class="keyword">template</span> <span class="keyword">class </span>SEAL_Policy<LittleEndian>;00210 00211 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:24 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -