📄 validat2_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++: validat2.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>validat2.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// validat2.cpp - written and placed in the public domain by Wei Dai</span>00002 00003 <span class="preprocessor">#include "pch.h"</span>00004 00005 <span class="preprocessor">#include "blumshub.h"</span>00006 <span class="preprocessor">#include "<a class="code" href="rsa_8h.html">rsa.h</a>"</span>00007 <span class="preprocessor">#include "md2.h"</span>00008 <span class="preprocessor">#include "elgamal.h"</span>00009 <span class="preprocessor">#include "nr.h"</span>00010 <span class="preprocessor">#include "<a class="code" href="dsa_8h.html">dsa.h</a>"</span>00011 <span class="preprocessor">#include "<a class="code" href="dh_8h.html">dh.h</a>"</span>00012 <span class="preprocessor">#include "<a class="code" href="mqv_8h.html">mqv.h</a>"</span>00013 <span class="preprocessor">#include "<a class="code" href="luc_8h.html">luc.h</a>"</span>00014 <span class="preprocessor">#include "<a class="code" href="xtrcrypt_8h.html">xtrcrypt.h</a>"</span>00015 <span class="preprocessor">#include "<a class="code" href="rabin_8h.html">rabin.h</a>"</span>00016 <span class="preprocessor">#include "<a class="code" href="rw_8h.html">rw.h</a>"</span>00017 <span class="preprocessor">#include "<a class="code" href="eccrypto_8h.html">eccrypto.h</a>"</span>00018 <span class="preprocessor">#include "ecp.h"</span>00019 <span class="preprocessor">#include "ec2n.h"</span>00020 <span class="preprocessor">#include "asn.h"</span>00021 <span class="preprocessor">#include "rng.h"</span>00022 <span class="preprocessor">#include "files.h"</span>00023 <span class="preprocessor">#include "hex.h"</span>00024 <span class="preprocessor">#include "oids.h"</span>00025 <span class="preprocessor">#include "<a class="code" href="esign_8h.html">esign.h</a>"</span>00026 <span class="preprocessor">#include "osrng.h"</span>00027 00028 <span class="preprocessor">#include <iostream></span>00029 <span class="preprocessor">#include <iomanip></span>00030 00031 <span class="preprocessor">#include "validate.h"</span>00032 00033 USING_NAMESPACE(CryptoPP)00034 USING_NAMESPACE(std)00035 00036 <span class="keyword">class </span>FixedRNG : <span class="keyword">public</span> <a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a>00037 {00038 <span class="keyword">public</span>:00039 FixedRNG(<a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &source) : m_source(source) {}00040 00041 byte GenerateByte()00042 {00043 byte b;00044 m_source.Get(b);00045 <span class="keywordflow">return</span> b;00046 }00047 00048 <span class="keyword">private</span>:00049 <a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &m_source;00050 };00051 00052 <span class="keywordtype">bool</span> ValidateBBS()00053 {00054 cout << <span class="stringliteral">"\nBlumBlumShub validation suite running...\n\n"</span>;00055 00056 <a class="code" href="class_integer.html">Integer</a> p(<span class="stringliteral">"212004934506826557583707108431463840565872545889679278744389317666981496005411448865750399674653351"</span>);00057 <a class="code" href="class_integer.html">Integer</a> q(<span class="stringliteral">"100677295735404212434355574418077394581488455772477016953458064183204108039226017738610663984508231"</span>);00058 <a class="code" href="class_integer.html">Integer</a> seed(<span class="stringliteral">"63239752671357255800299643604761065219897634268887145610573595874544114193025997412441121667211431"</span>);00059 <a class="code" href="class_blum_blum_shub.html">BlumBlumShub</a> bbs(p, q, seed);00060 <span class="keywordtype">bool</span> pass = <span class="keyword">true</span>, fail;00061 <span class="keywordtype">int</span> j;00062 00063 <span class="keyword">const</span> byte output1[] = {00064 0x49,0xEA,0x2C,0xFD,0xB0,0x10,0x64,0xA0,0xBB,0xB9,00065 0x2A,0xF1,0x01,0xDA,0xC1,0x8A,0x94,0xF7,0xB7,0xCE};00066 <span class="keyword">const</span> byte output2[] = {00067 0x74,0x45,0x48,0xAE,0xAC,0xB7,0x0E,0xDF,0xAF,0xD7,00068 0xD5,0x0E,0x8E,0x29,0x83,0x75,0x6B,0x27,0x46,0xA1};00069 00070 byte buf[20];00071 00072 bbs.<a class="code" href="class_random_number_generator.html#_x917_r_n_ga4">GenerateBlock</a>(buf, 20);00073 fail = memcmp(output1, buf, 20) != 0;00074 pass = pass && !fail;00075 00076 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>);00077 <span class="keywordflow">for</span> (j=0;j<20;j++)00078 cout << setw(2) << setfill(<span class="charliteral">'0'</span>) << hex << (<span class="keywordtype">int</span>)buf[j];00079 cout << endl;00080 00081 bbs.<a class="code" href="class_blum_blum_shub.html#_blum_blum_shuba2">Seek</a>(10);00082 bbs.<a class="code" href="class_random_number_generator.html#_x917_r_n_ga4">GenerateBlock</a>(buf, 10);00083 fail = memcmp(output1+10, buf, 10) != 0;00084 pass = pass && !fail;00085 00086 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>);00087 <span class="keywordflow">for</span> (j=0;j<10;j++)00088 cout << setw(2) << setfill(<span class="charliteral">'0'</span>) << hex << (<span class="keywordtype">int</span>)buf[j];00089 cout << endl;00090 00091 bbs.<a class="code" href="class_blum_blum_shub.html#_blum_blum_shuba2">Seek</a>(1234567);00092 bbs.<a class="code" href="class_random_number_generator.html#_x917_r_n_ga4">GenerateBlock</a>(buf, 20);00093 fail = memcmp(output2, buf, 20) != 0;00094 pass = pass && !fail;00095 00096 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>);00097 <span class="keywordflow">for</span> (j=0;j<20;j++)00098 cout << setw(2) << setfill(<span class="charliteral">'0'</span>) << hex << (<span class="keywordtype">int</span>)buf[j];00099 cout << endl;00100 00101 <span class="keywordflow">return</span> pass;00102 }00103 00104 <span class="keywordtype">bool</span> SignatureValidate(<a class="code" href="class_p_k___signer.html">PK_Signer</a> &priv, <a class="code" href="class_p_k___verifier.html">PK_Verifier</a> &pub, <span class="keywordtype">bool</span> thorough = <span class="keyword">false</span>)00105 {00106 <span class="keywordtype">bool</span> pass = <span class="keyword">true</span>, fail;00107 00108 fail = !pub.<a class="code" href="class_public_key_algorithm.html#_verifier_with_recovery_templatea20">GetMaterial</a>().<a class="code" href="class_crypto_material.html#_x509_public_keya12">Validate</a>(GlobalRNG(), thorough ? 3 : 2) || !priv.<a class="code" href="class_private_key_algorithm.html#_t_f___private_object_impl_3_01_t_f___digest_signer_base_00_01_s_c_h_e_m_e___o_p_t_i_o_n_s_01_4a14">GetMaterial</a>().<a class="code" href="class_crypto_material.html#_x509_public_keya12">Validate</a>(GlobalRNG(), thorough ? 3 : 2);00109 pass = pass && !fail;00110 00111 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>);00112 cout << <span class="stringliteral">"signature key validation\n"</span>;00113 00114 <span class="keyword">const</span> byte *message = (byte *)<span class="stringliteral">"test message"</span>;00115 <span class="keyword">const</span> <span class="keywordtype">int</span> messageLen = 12;00116 byte buffer[512];00117 00118 memset(buffer, 0, <span class="keyword">sizeof</span>(buffer));00119 priv.<a class="code" href="class_p_k___signer.html#_signer_with_recovery_templatea11">SignMessage</a>(GlobalRNG(), message, messageLen, buffer);00120 fail = !pub.<a class="code" href="class_p_k___verifier.html#_verifier_with_recovery_templatea18">VerifyMessage</a>(message, messageLen, buffer);00121 pass = pass && !fail;00122 00123 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>);00124 cout << <span class="stringliteral">"signature and verification\n"</span>;00125 00126 ++buffer[0];00127 fail = pub.<a class="code" href="class_p_k___verifier.html#_verifier_with_recovery_templatea18">VerifyMessage</a>(message, messageLen, buffer);00128 pass = pass && !fail;00129 00130 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>);00131 cout << <span class="stringliteral">"checking invalid signature"</span> << endl;00132 00133 <span class="keywordflow">return</span> pass;00134 }00135 00136 <span class="keywordtype">bool</span> CryptoSystemValidate(<a class="code" href="class_p_k___decryptor.html">PK_Decryptor</a> &priv, <a class="code" href="class_p_k___encryptor.html">PK_Encryptor</a> &pub, <span class="keywordtype">bool</span> thorough = <span class="keyword">false</span>)00137 {00138 <span class="keywordtype">bool</span> pass = <span class="keyword">true</span>, fail;00139 00140 fail = !pub.<a class="code" href="class_public_key_algorithm.html#_verifier_with_recovery_templatea20">GetMaterial</a>().<a class="code" href="class_crypto_material.html#_x509_public_keya12">Validate</a>(GlobalRNG(), thorough ? 3 : 2) || !priv.<a class="code" href="class_private_key_algorithm.html#_t_f___private_object_impl_3_01_t_f___digest_signer_base_00_01_s_c_h_e_m_e___o_p_t_i_o_n_s_01_4a14">GetMaterial</a>().<a class="code" href="class_crypto_material.html#_x509_public_keya12">Validate</a>(GlobalRNG(), thorough ? 3 : 2);00141 pass = pass && !fail;00142 00143 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>);00144 cout << <span class="stringliteral">"cryptosystem key validation\n"</span>;00145 00146 <span class="keyword">const</span> byte *message = (byte *)<span class="stringliteral">"test message"</span>;00147 <span class="keyword">const</span> <span class="keywordtype">int</span> messageLen = 12;00148 <a class="code" href="class_sec_block.html">SecByteBlock</a> ciphertext(priv.<a class="code" href="class_p_k___crypto_system.html#_t_f___public_object_impl_3_01_t_f___encryptor_base_00_01_s_c_h_e_m_e___o_p_t_i_o_n_s_01_4a15">CiphertextLength</a>(messageLen));00149 <a class="code" href="class_sec_block.html">SecByteBlock</a> plaintext(priv.<a class="code" href="class_p_k___crypto_system.html#_t_f___public_object_impl_3_01_t_f___encryptor_base_00_01_s_c_h_e_m_e___o_p_t_i_o_n_s_01_4a13">MaxPlaintextLength</a>(ciphertext.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>()));00150 00151 pub.<a class="code" href="class_p_k___encryptor.html#_t_f___crypto_system_base_3_01_p_k___fixed_length_encryptor_00_01_t_f___base_3_01_randomized_trapdoor_function_01_4_01_4a2">Encrypt</a>(GlobalRNG(), message, messageLen, ciphertext);00152 fail = priv.<a class="code" href="class_p_k___decryptor.html#_p_k___decryptora0">Decrypt</a>(ciphertext, priv.<a class="code" href="class_p_k___crypto_system.html#_t_f___public_object_impl_3_01_t_f___encryptor_base_00_01_s_c_h_e_m_e___o_p_t_i_o_n_s_01_4a15">CiphertextLength</a>(messageLen), plaintext) != <a class="code" href="struct_decoding_result.html">DecodingResult</a>(messageLen);00153 fail = fail || memcmp(message, plaintext, messageLen);00154 pass = pass && !fail;00155 00156 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>);00157 cout << <span class="stringliteral">"encryption and decryption\n"</span>;00158 00159 <span class="keywordflow">return</span> pass;00160 }00161 00162 <span class="keywordtype">bool</span> SimpleKeyAgreementValidate(<a class="code" href="class_simple_key_agreement_domain.html">SimpleKeyAgreementDomain</a> &d)00163 {00164 <span class="keywordflow">if</span> (d.GetCryptoParameters().Validate(GlobalRNG(), 3))00165 cout << <span class="stringliteral">"passed simple key agreement domain parameters validation"</span> << endl;00166 <span class="keywordflow">else</span>00167 {00168 cout << <span class="stringliteral">"FAILED simple key agreement domain parameters invalid"</span> << endl;00169 <span class="keywordflow">return</span> <span class="keyword">false</span>;00170 }00171 00172 <a class="code" href="class_sec_block.html">SecByteBlock</a> priv1(d.PrivateKeyLength()), priv2(d.PrivateKeyLength());00173 <a class="code" href="class_sec_block.html">SecByteBlock</a> pub1(d.PublicKeyLength()), pub2(d.PublicKeyLength());00174 <a class="code" href="class_sec_block.html">SecByteBlock</a> val1(d.AgreedValueLength()), val2(d.AgreedValueLength());00175 00176 d.GenerateKeyPair(GlobalRNG(), priv1, pub1);00177 d.GenerateKeyPair(GlobalRNG(), priv2, pub2);00178 00179 memset(val1.<a class="code" href="class_sec_block.html#_sec_block_with_hinta7">begin</a>(), 0x10, val1.<a class="code" href="class_sec_block.html#_sec_block_with_hinta13">size</a>());00180 memset(val2.begin(), 0x11, val2.size());00181 00182 <span class="keywordflow">if</span> (!(d.Agree(val1, priv1, pub2) && d.Agree(val2, priv2, pub1)))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -