📄 dlltest_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++: dlltest.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>dlltest.cpp</h1><div class="fragment"><pre>00001 <span class="preprocessor">#include "dll.h"</span>00002 <span class="preprocessor">#include <iostream></span>00003 00004 USING_NAMESPACE(CryptoPP)00005 USING_NAMESPACE(std)00006 00007 <span class="keywordtype">void</span> FIPS140_SampleApplication()00008 {00009 <span class="keywordflow">if</span> (!FIPS_140_2_ComplianceEnabled())00010 {00011 cerr << <span class="stringliteral">"FIPS-140-2 compliance was turned off at compile time.\n"</span>;00012 abort();00013 }00014 00015 <span class="comment">// check self test status</span>00016 <span class="keywordflow">if</span> (GetPowerUpSelfTestStatus() != POWER_UP_SELF_TEST_PASSED)00017 {00018 cerr << <span class="stringliteral">"Automatic power-up self test failed.\n"</span>;00019 abort();00020 }00021 cout << <span class="stringliteral">"0. Automatic power-up self test passed.\n"</span>;00022 00023 <span class="comment">// simulate a power-up self test error</span>00024 SimulatePowerUpSelfTestFailure();00025 <span class="keywordflow">try</span>00026 {00027 <span class="comment">// trying to use a crypto algorithm after power-up self test error will result in an exception</span>00028 <a class="code" href="class_rijndael.html#_rijndaelw0">AES::Encryption</a> aes;00029 00030 <span class="comment">// should not be here</span>00031 cerr << <span class="stringliteral">"Use of AES failed to cause an exception after power-up self test error.\n"</span>;00032 abort();00033 }00034 <span class="keywordflow">catch</span> (<a class="code" href="class_self_test_failure.html">SelfTestFailure</a> &e)00035 {00036 cout << <span class="stringliteral">"1. Caught expected exception when simulating self test failure. Exception message follows: "</span>;00037 cout << e.what() << endl;00038 }00039 00040 <span class="comment">// clear the self test error state and redo power-up self test</span>00041 DoDllPowerUpSelfTest();00042 <span class="keywordflow">if</span> (GetPowerUpSelfTestStatus() != POWER_UP_SELF_TEST_PASSED)00043 {00044 cerr << <span class="stringliteral">"Re-do power-up self test failed.\n"</span>;00045 abort();00046 }00047 cout << <span class="stringliteral">"2. Re-do power-up self test passed.\n"</span>;00048 00049 <span class="comment">// encrypt and decrypt</span>00050 <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};00051 <span class="keyword">const</span> byte iv[] = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};00052 <span class="keyword">const</span> byte plaintext[] = { <span class="comment">// "Now is the time for all " without tailing 0</span>00053 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,00054 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,00055 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};00056 byte ciphertext[24];00057 byte decrypted[24];00058 00059 <a class="code" href="struct_c_f_b___mode.html">CFB_Mode<DES_EDE3></a>::Encryption encryption_DES_EDE3_CBC;00060 encryption_DES_EDE3_CBC.SetKeyWithIV(key, <span class="keyword">sizeof</span>(key), iv);00061 encryption_DES_EDE3_CBC.ProcessString(ciphertext, plaintext, 24);00062 00063 <a class="code" href="struct_c_f_b___mode.html">CFB_Mode<DES_EDE3></a>::Decryption decryption_DES_EDE3_CBC;00064 decryption_DES_EDE3_CBC.SetKeyWithIV(key, <span class="keyword">sizeof</span>(key), iv);00065 decryption_DES_EDE3_CBC.ProcessString(decrypted, ciphertext, 24);00066 00067 <span class="keywordflow">if</span> (memcmp(plaintext, decrypted, 24) != 0)00068 {00069 cerr << <span class="stringliteral">"DES-EDE3-CBC Encryption/decryption failed.\n"</span>;00070 abort();00071 }00072 cout << <span class="stringliteral">"3. DES-EDE3-CBC Encryption/decryption succeeded.\n"</span>;00073 00074 <span class="comment">// hash</span>00075 <span class="keyword">const</span> byte message[] = {<span class="charliteral">'a'</span>, <span class="charliteral">'b'</span>, <span class="charliteral">'c'</span>};00076 <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};00077 byte digest[20];00078 00079 <a class="code" href="class_s_h_a.html">SHA1</a> sha;00080 sha.Update(message, 3);00081 sha.<a class="code" href="class_hash_transformation.html#_x_m_a_c_c___basea13">Final</a>(digest);00082 00083 <span class="keywordflow">if</span> (memcmp(digest, expectedDigest, 20) != 0)00084 {00085 cerr << <span class="stringliteral">"SHA-1 hash failed.\n"</span>;00086 abort();00087 }00088 cout << <span class="stringliteral">"4. SHA-1 hash succeeded.\n"</span>;00089 00090 <span class="comment">// create auto-seeded X9.17 RNG object, if available</span>00091 <span class="preprocessor">#ifdef OS_RNG_AVAILABLE</span>00092 <span class="preprocessor"></span> <a class="code" href="class_auto_seeded_x917_r_n_g.html">AutoSeededX917RNG<DES_EDE3></a> rng;00093 <span class="preprocessor">#else</span>00094 <span class="preprocessor"></span> <span class="comment">// this is used to allow this function to compile on platforms that don't have auto-seeded RNGs</span>00095 <a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &rng(NullRNG());00096 <span class="preprocessor">#endif</span>00097 <span class="preprocessor"></span>00098 <span class="comment">// generate DSA key</span>00099 DSA::PrivateKey dsaPrivateKey;00100 dsaPrivateKey.GenerateRandomWithKeySize(rng, 1024);00101 DSA::PublicKey dsaPublicKey;00102 dsaPublicKey.AssignFrom(dsaPrivateKey);00103 <span class="keywordflow">if</span> (!dsaPrivateKey.Validate(rng, 3) || !dsaPublicKey.Validate(rng, 3))00104 {00105 cerr << <span class="stringliteral">"DSA key generation failed.\n"</span>;00106 abort();00107 }00108 cout << <span class="stringliteral">"5. DSA key generation succeeded.\n"</span>;00109 00110 <span class="comment">// encode DSA key</span>00111 std::string encodedDsaPublicKey, encodedDsaPrivateKey;00112 dsaPublicKey.DEREncode(<a class="code" href="class_string_sink_template.html">StringSink</a>(encodedDsaPublicKey).Ref());00113 dsaPrivateKey.DEREncode(<a class="code" href="class_string_sink_template.html">StringSink</a>(encodedDsaPrivateKey).Ref());00114 00115 <span class="comment">// decode DSA key</span>00116 DSA::PrivateKey decodedDsaPrivateKey;00117 decodedDsaPrivateKey.BERDecode(<a class="code" href="class_string_store.html">StringStore</a>(encodedDsaPrivateKey).Ref());00118 DSA::PublicKey decodedDsaPublicKey;00119 decodedDsaPublicKey.BERDecode(<a class="code" href="class_string_store.html">StringStore</a>(encodedDsaPublicKey).Ref());00120 00121 <span class="keywordflow">if</span> (!decodedDsaPrivateKey.Validate(rng, 3) || !decodedDsaPublicKey.Validate(rng, 3))00122 {00123 cerr << <span class="stringliteral">"DSA key encode/decode failed.\n"</span>;00124 abort();00125 }00126 cout << <span class="stringliteral">"6. DSA key encode/decode succeeded.\n"</span>;00127 00128 <span class="comment">// sign and verify</span>00129 byte signature[40];00130 <a class="code" href="class_d_l___s_s_a.html#_d_l___s_s_aw0">DSA::Signer</a> signer(dsaPrivateKey);00131 assert(signer.SignatureLength() == 40);00132 signer.SignMessage(rng, message, 3, signature);00133 00134 <a class="code" href="class_d_l___s_s_a.html#_d_l___s_s_aw1">DSA::Verifier</a> verifier(dsaPublicKey);00135 <span class="keywordflow">if</span> (!verifier.VerifyMessage(message, 3, signature))00136 {00137 cerr << <span class="stringliteral">"DSA signature and verification failed.\n"</span>;00138 abort();00139 }00140 cout << <span class="stringliteral">"7. DSA signature and verification succeeded.\n"</span>;00141 00142 00143 <span class="comment">// try to verify an invalid signature</span>00144 signature[0] ^= 1;00145 <span class="keywordflow">if</span> (verifier.VerifyMessage(message, 3, signature))00146 {00147 cerr << <span class="stringliteral">"DSA signature verification failed to detect bad signature.\n"</span>;00148 abort();00149 }00150 cout << <span class="stringliteral">"8. DSA signature verification successfully detected bad signature.\n"</span>;00151 00152 <span class="comment">// try to use an invalid key length</span>00153 <span class="keywordflow">try</span>00154 {00155 encryption_DES_EDE3_CBC.SetKey(key, 5);00156 00157 <span class="comment">// should not be here</span>00158 cerr << <span class="stringliteral">"DES-EDE3 implementation did not detect use of invalid key length.\n"</span>;00159 abort();00160 }00161 <span class="keywordflow">catch</span> (<a class="code" href="class_invalid_argument.html">InvalidArgument</a> &e)00162 {00163 cout << <span class="stringliteral">"9. Caught expected exception when using invalid key length. Exception message follows: "</span>;00164 cout << e.what() << endl;00165 }00166 00167 cout << <span class="stringliteral">"\nFIPS 140-2 Sample Application completed normally.\n"</span>;00168 }00169 00170 <span class="preprocessor">#ifdef CRYPTOPP_DLL_ONLY</span>00171 <span class="preprocessor"></span>00172 <span class="keywordtype">int</span> __cdecl main()00173 {00174 FIPS140_SampleApplication();00175 <span class="keywordflow">return</span> 0;00176 }00177 00178 <span class="preprocessor">#endif</span></pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:12 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 + -