📄 fipstest_8cpp-source.html
字号:
00220 <span class="keywordflow">try</span>00221 {00222 <span class="keywordflow">if</span> (FIPS_140_2_ComplianceEnabled() || moduleFilename != NULL)00223 {00224 <span class="comment">// integrity test</span>00225 std::auto_ptr<MessageAuthenticationCode> mac(NewIntegrityCheckingMAC());00226 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> macSize = mac->DigestSize();00227 g_actualMac.<a class="code" href="class_sec_block.html#_sec_block_with_hinta23">resize</a>(macSize);00228 <a class="code" href="class_hash_filter.html">HashFilter</a> verifier(*mac, <span class="keyword">new</span> <a class="code" href="class_array_sink.html">ArraySink</a>(g_actualMac, g_actualMac.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>()));00229 <a class="code" href="class_file_store.html">FileStore</a> file(moduleFilename);00230 00231 <span class="preprocessor">#ifdef CRYPTOPP_WIN32_AVAILABLE</span>00232 <span class="preprocessor"></span> <span class="comment">// try to hash from memory first</span>00233 HMODULE h = GetModuleHandle(moduleFilename);00234 IMAGE_DOS_HEADER *ph = (IMAGE_DOS_HEADER *)h;00235 IMAGE_NT_HEADERS *phnt = (IMAGE_NT_HEADERS *)((byte *)h + ph->e_lfanew);00236 IMAGE_SECTION_HEADER *phs = IMAGE_FIRST_SECTION(phnt);00237 DWORD nSections = phnt->FileHeader.NumberOfSections;00238 DWORD currentFilePos = 0;00239 00240 <span class="keywordflow">while</span> (nSections--)00241 {00242 <span class="keywordflow">switch</span> (phs->Characteristics)00243 {00244 <span class="keywordflow">default</span>:00245 <span class="keywordflow">break</span>;00246 <span class="keywordflow">case</span> IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ:00247 <span class="keywordflow">case</span> IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ:00248 DWORD sectionSize = STDMIN(phs->SizeOfRawData, phs->Misc.VirtualSize);00249 <span class="keyword">const</span> byte *memStart = (<span class="keyword">const</span> byte *)h + phs->VirtualAddress;00250 DWORD fileStart = phs->PointerToRawData;00251 <span class="keywordflow">if</span> (phs->VirtualAddress == phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress)00252 {00253 <span class="comment">// read IAT, which is changed during DLL loading, from disk</span>00254 DWORD iatSize = phnt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size;00255 fileStart += iatSize;00256 memStart += iatSize;00257 sectionSize -= iatSize;00258 }00259 file.<a class="code" href="class_buffered_transformation.html#_zlib_decompressorz7_10">TransferTo</a>(verifier, fileStart - currentFilePos);00260 <span class="keywordflow">if</span> (memStart <= expectedModuleMac && expectedModuleMac < memStart + sectionSize)00261 {00262 <span class="comment">// skip over the MAC</span>00263 verifier.<a class="code" href="class_buffered_transformation.html#_zlib_decompressorz1_0">Put</a>(memStart, expectedModuleMac - memStart);00264 verifier.<a class="code" href="class_buffered_transformation.html#_zlib_decompressorz1_0">Put</a>(expectedModuleMac + macSize, sectionSize - macSize - (expectedModuleMac - memStart));00265 g_macFileLocation = fileStart + (expectedModuleMac - memStart);00266 }00267 <span class="keywordflow">else</span>00268 verifier.<a class="code" href="class_buffered_transformation.html#_zlib_decompressorz1_0">Put</a>(memStart, sectionSize);00269 ::VirtualUnlock((LPVOID)memStart, sectionSize); <span class="comment">// release the memory from working set</span>00270 file.<a class="code" href="class_file_store.html#_file_storea8">Skip</a>(sectionSize);00271 currentFilePos = fileStart + sectionSize;00272 }00273 phs++;00274 }00275 <span class="preprocessor">#endif</span>00276 <span class="preprocessor"></span> file.<a class="code" href="class_buffered_transformation.html#_zlib_decompressorz9_8">TransferAllTo</a>(verifier);00277 00278 <span class="preprocessor">#ifdef CRYPTOPP_WIN32_AVAILABLE</span>00279 <span class="preprocessor"></span> <span class="comment">// if that fails (could be caused by debug breakpoints or DLL base relocation modifying image in memory),</span>00280 <span class="comment">// hash from disk instead</span>00281 <span class="keywordflow">if</span> (memcmp(expectedModuleMac, g_actualMac, macSize) != 0)00282 {00283 OutputDebugString(<span class="stringliteral">"In memory integrity check failed. This may be caused by debug breakpoints or DLL relocation.\n"</span>);00284 file.<a class="code" href="class_buffered_transformation.html#_windows_pipe_sinkz5_2">Initialize</a>(MakeParameters(<span class="stringliteral">"InputFileName"</span>, moduleFilename));00285 verifier.<a class="code" href="class_filter.html#_zlib_decompressora10">Detach</a>(<span class="keyword">new</span> <a class="code" href="class_array_sink.html">ArraySink</a>(g_actualMac, g_actualMac.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>()));00286 <span class="keywordflow">if</span> (g_macFileLocation)00287 {00288 file.<a class="code" href="class_buffered_transformation.html#_zlib_decompressorz7_10">TransferTo</a>(verifier, g_macFileLocation);00289 file.<a class="code" href="class_file_store.html#_file_storea8">Skip</a>(macSize);00290 }00291 file.<a class="code" href="class_buffered_transformation.html#_zlib_decompressorz9_8">TransferAllTo</a>(verifier);00292 }00293 <span class="preprocessor">#endif</span>00294 <span class="preprocessor"></span>00295 <span class="keywordflow">if</span> (memcmp(expectedModuleMac, g_actualMac, macSize) != 0)00296 {00297 <span class="preprocessor">#ifdef CRYPTOPP_WIN32_AVAILABLE</span>00298 <span class="preprocessor"></span> std::string hexMac;00299 <a class="code" href="class_hex_encoder.html">HexEncoder</a>(<span class="keyword">new</span> <a class="code" href="class_string_sink_template.html">StringSink</a>(hexMac)).PutMessageEnd(g_actualMac, g_actualMac.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>());00300 OutputDebugString((<span class="stringliteral">"Crypto++ integrity check failed. Actual MAC is: "</span> + hexMac + <span class="stringliteral">"\n"</span>).c_str());00301 <span class="preprocessor">#endif</span>00302 <span class="preprocessor"></span> <span class="keywordflow">throw</span> 0; <span class="comment">// throw here so we break in the debugger, this will be caught right away</span>00303 }00304 }00305 00306 <span class="comment">// algorithm tests</span>00307 00308 X917RNG_KnownAnswerTest<DES_EDE3>(00309 <span class="stringliteral">"48851090B4992453E83CDA86416534E53EA2FCE1A0B3A40C"</span>, <span class="comment">// key</span>00310 <span class="stringliteral">"7D00BD0A79F6B0F5"</span>, <span class="comment">// seed</span>00311 <span class="stringliteral">"22B590B08B53363AEB89AD65F81A5B6FB83F326CE06BF35751E6C41B43B729C4"</span>, <span class="comment">// output</span>00312 1489728269); <span class="comment">// time vector</span>00313 00314 <span class="comment">/* SymmetricEncryptionKnownAnswerTest<DES>(</span>00315 <span class="comment"> "0123456789abcdef", // key</span>00316 <span class="comment"> "1234567890abcdef", // IV</span>00317 <span class="comment"> "4e6f77206973207468652074696d6520666f7220616c6c20", // plaintext</span>00318 <span class="comment"> "3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53", // ecb</span>00319 <span class="comment"> "E5C7CDDE872BF27C43E934008C389C0F683788499A7C05F6", // cbc</span>00320 <span class="comment"> "F3096249C7F46E51A69E839B1A92F78403467133898EA622", // cfb</span>00321 <span class="comment"> "f3096249c7f46e5135f24a242eeb3d3f3d6d5be3255af8c3", // ofb</span>00322 <span class="comment"> "F3096249C7F46E51163A8CA0FFC94C27FA2F80F480B86F75");// ctr</span>00323 <span class="comment">*/</span>00324 SymmetricEncryptionKnownAnswerTest<DES_EDE3>(00325 <span class="stringliteral">"385D7189A5C3D485E1370AA5D408082B5CCCCB5E19F2D90E"</span>,00326 <span class="stringliteral">"C141B5FCCD28DC8A"</span>,00327 <span class="stringliteral">"6E1BD7C6120947A464A6AAB293A0F89A563D8D40D3461B68"</span>,00328 <span class="stringliteral">"64EAAD4ACBB9CEAD6C7615E7C7E4792FE587D91F20C7D2F4"</span>,00329 <span class="stringliteral">"6235A461AFD312973E3B4F7AA7D23E34E03371F8E8C376C9"</span>,00330 <span class="stringliteral">"E26BA806A59B0330DE40CA38E77A3E494BE2B212F6DD624B"</span>,00331 <span class="stringliteral">"E26BA806A59B03307DE2BCC25A08BA40A8BA335F5D604C62"</span>,00332 <span class="stringliteral">"E26BA806A59B03303C62C2EFF32D3ACDD5D5F35EBCC53371"</span>);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -