📄 dlltest_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++: dlltest.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>dlltest.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#ifndef CRYPTOPP_DLL_ONLY</span><a name="l00002"></a>00002 <span class="preprocessor"></span><span class="preprocessor">#define CRYPTOPP_DEFAULT_NO_DLL</span><a name="l00003"></a>00003 <span class="preprocessor"></span><span class="preprocessor">#endif</span><a name="l00004"></a>00004 <span class="preprocessor"></span><a name="l00005"></a>00005 <span class="preprocessor">#include "dll.h"</span><a name="l00006"></a>00006 <span class="preprocessor">#include <iostream></span><a name="l00007"></a>00007 <a name="l00008"></a>00008 USING_NAMESPACE(CryptoPP)<a name="l00009"></a>00009 USING_NAMESPACE(std)<a name="l00010"></a>00010 <a name="l00011"></a>00011 <span class="keywordtype">void</span> FIPS140_SampleApplication()<a name="l00012"></a>00012 {<a name="l00013"></a>00013 <span class="keywordflow">if</span> (!FIPS_140_2_ComplianceEnabled())<a name="l00014"></a>00014 {<a name="l00015"></a>00015 cerr << <span class="stringliteral">"FIPS 140-2 compliance was turned off at compile time.\n"</span>;<a name="l00016"></a>00016 abort();<a name="l00017"></a>00017 }<a name="l00018"></a>00018 <a name="l00019"></a>00019 <span class="comment">// check self test status</span><a name="l00020"></a>00020 <span class="keywordflow">if</span> (GetPowerUpSelfTestStatus() != <a class="code" href="fips140_8h.html#8ccac50ac3bfc5abaa6cb457f0ded35d6c4f7efa0a4251f4c3a2836abba17877">POWER_UP_SELF_TEST_PASSED</a>)<a name="l00021"></a>00021 {<a name="l00022"></a>00022 cerr << <span class="stringliteral">"Automatic power-up self test failed.\n"</span>;<a name="l00023"></a>00023 abort();<a name="l00024"></a>00024 }<a name="l00025"></a>00025 cout << <span class="stringliteral">"0. Automatic power-up self test passed.\n"</span>;<a name="l00026"></a>00026 <a name="l00027"></a>00027 <span class="comment">// simulate a power-up self test error</span><a name="l00028"></a>00028 SimulatePowerUpSelfTestFailure();<a name="l00029"></a>00029 <span class="keywordflow">try</span><a name="l00030"></a>00030 {<a name="l00031"></a>00031 <span class="comment">// trying to use a crypto algorithm after power-up self test error will result in an exception</span><a name="l00032"></a>00032 <a class="code" href="class_block_cipher_final.html">AES::Encryption</a> aes;<a name="l00033"></a>00033 <a name="l00034"></a>00034 <span class="comment">// should not be here</span><a name="l00035"></a>00035 cerr << <span class="stringliteral">"Use of AES failed to cause an exception after power-up self test error.\n"</span>;<a name="l00036"></a>00036 abort();<a name="l00037"></a>00037 }<a name="l00038"></a>00038 <span class="keywordflow">catch</span> (<a class="code" href="class_self_test_failure.html" title="exception thrown when a crypto algorithm is used after a self test fails">SelfTestFailure</a> &e)<a name="l00039"></a>00039 {<a name="l00040"></a>00040 cout << <span class="stringliteral">"1. Caught expected exception when simulating self test failure. Exception message follows: "</span>;<a name="l00041"></a>00041 cout << e.<a class="code" href="class_exception.html#dd710fe2bae3ac497ed60393eea35c91">what</a>() << endl;<a name="l00042"></a>00042 }<a name="l00043"></a>00043 <a name="l00044"></a>00044 <span class="comment">// clear the self test error state and redo power-up self test</span><a name="l00045"></a>00045 <a class="code" href="fips140_8h.html#0391333183b583a192abc0184483ae38" title="perform the power-up self test using the filename of this DLL and the embedded module...">DoDllPowerUpSelfTest</a>();<a name="l00046"></a>00046 <span class="keywordflow">if</span> (GetPowerUpSelfTestStatus() != <a class="code" href="fips140_8h.html#8ccac50ac3bfc5abaa6cb457f0ded35d6c4f7efa0a4251f4c3a2836abba17877">POWER_UP_SELF_TEST_PASSED</a>)<a name="l00047"></a>00047 {<a name="l00048"></a>00048 cerr << <span class="stringliteral">"Re-do power-up self test failed.\n"</span>;<a name="l00049"></a>00049 abort();<a name="l00050"></a>00050 }<a name="l00051"></a>00051 cout << <span class="stringliteral">"2. Re-do power-up self test passed.\n"</span>;<a name="l00052"></a>00052 <a name="l00053"></a>00053 <span class="comment">// encrypt and decrypt</span><a name="l00054"></a>00054 <span class="keyword">const</span> byte key[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};<a name="l00055"></a>00055 <span class="keyword">const</span> byte iv[] = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};<a name="l00056"></a>00056 <span class="keyword">const</span> byte plaintext[] = { <span class="comment">// "Now is the time for all " without tailing 0</span><a name="l00057"></a>00057 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,<a name="l00058"></a>00058 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,<a name="l00059"></a>00059 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};<a name="l00060"></a>00060 byte ciphertext[24];<a name="l00061"></a>00061 byte decrypted[24];<a name="l00062"></a>00062 <a name="l00063"></a>00063 <a class="code" href="struct_c_f_b___f_i_p_s___mode.html" title="CFB mode FIPS variant, requiring full block plaintext according to FIPS 800-38A.">CFB_FIPS_Mode<DES_EDE3>::Encryption</a> encryption_DES_EDE3_CFB;<a name="l00064"></a>00064 encryption_DES_EDE3_CFB.SetKeyWithIV(key, <span class="keyword">sizeof</span>(key), iv);<a name="l00065"></a>00065 encryption_DES_EDE3_CFB.ProcessString(ciphertext, plaintext, 24);<a name="l00066"></a>00066 <a name="l00067"></a>00067 <a class="code" href="struct_c_f_b___f_i_p_s___mode.html" title="CFB mode FIPS variant, requiring full block plaintext according to FIPS 800-38A.">CFB_FIPS_Mode<DES_EDE3>::Decryption</a> decryption_DES_EDE3_CFB;<a name="l00068"></a>00068 decryption_DES_EDE3_CFB.SetKeyWithIV(key, <span class="keyword">sizeof</span>(key), iv);<a name="l00069"></a>00069 decryption_DES_EDE3_CFB.ProcessString(decrypted, ciphertext, 24);<a name="l00070"></a>00070 <a name="l00071"></a>00071 <span class="keywordflow">if</span> (memcmp(plaintext, decrypted, 24) != 0)<a name="l00072"></a>00072 {<a name="l00073"></a>00073 cerr << <span class="stringliteral">"DES-EDE3-CFB Encryption/decryption failed.\n"</span>;<a name="l00074"></a>00074 abort();<a name="l00075"></a>00075 }<a name="l00076"></a>00076 cout << <span class="stringliteral">"3. DES-EDE3-CFB Encryption/decryption succeeded.\n"</span>;<a name="l00077"></a>00077 <a name="l00078"></a>00078 <span class="comment">// hash</span><a name="l00079"></a>00079 <span class="keyword">const</span> byte message[] = {<span class="charliteral">'a'</span>, <span class="charliteral">'b'</span>, <span class="charliteral">'c'</span>};<a name="l00080"></a>00080 <span class="keyword">const</span> byte expectedDigest[] = {0xA9,0x99,0x3E,0x36,0x47,0x06,0x81,0x6A,0xBA,0x3E,0x25,0x71,0x78,0x50,0xC2,0x6C,0x9C,0xD0,0xD8,0x9D};<a name="l00081"></a>00081 byte digest[20];<a name="l00082"></a>00082 <a name="l00083"></a>00083 <a class="code" href="class_s_h_a1.html" title="SHA-1">SHA1</a> sha;<a name="l00084"></a>00084 sha.<a class="code" href="class_iterated_hash_base.html#3202914349a18f79facb605d75f0d10c">Update</a>(message, 3);<a name="l00085"></a>00085 sha.<a class="code" href="class_hash_transformation.html#a0b8c7a110d8968268fd02ec32b9a8e8" title="compute hash for current message, then restart for a new message">Final</a>(digest);<a name="l00086"></a>00086 <a name="l00087"></a>00087 <span class="keywordflow">if</span> (memcmp(digest, expectedDigest, 20) != 0)<a name="l00088"></a>00088 {<a name="l00089"></a>00089 cerr << <span class="stringliteral">"SHA-1 hash failed.\n"</span>;<a name="l00090"></a>00090 abort();<a name="l00091"></a>00091 }<a name="l00092"></a>00092 cout << <span class="stringliteral">"4. SHA-1 hash succeeded.\n"</span>;<a name="l00093"></a>00093 <a name="l00094"></a>00094 <span class="comment">// create auto-seeded X9.17 RNG object, if available</span><a name="l00095"></a>00095 <span class="preprocessor">#ifdef OS_RNG_AVAILABLE</span><a name="l00096"></a>00096 <span class="preprocessor"></span> <a class="code" href="class_auto_seeded_x917_r_n_g.html" title="RNG from ANSI X9.17 Appendix C, seeded using an OS provided RNG.">AutoSeededX917RNG<AES></a> rng;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -