📄 validat2_8cpp-source.html
字号:
00183 {00184 cout << <span class="stringliteral">"FAILED simple key agreement failed"</span> << endl;00185 <span class="keywordflow">return</span> <span class="keyword">false</span>;00186 }00187 00188 <span class="keywordflow">if</span> (memcmp(val1.<a class="code" href="class_sec_block.html#_sec_block_with_hinta7">begin</a>(), val2.begin(), d.AgreedValueLength()))00189 {00190 cout << <span class="stringliteral">"FAILED simple agreed values not equal"</span> << endl;00191 <span class="keywordflow">return</span> <span class="keyword">false</span>;00192 }00193 00194 cout << <span class="stringliteral">"passed simple key agreement"</span> << endl;00195 <span class="keywordflow">return</span> <span class="keyword">true</span>;00196 }00197 00198 <span class="keywordtype">bool</span> AuthenticatedKeyAgreementValidate(<a class="code" href="class_authenticated_key_agreement_domain.html">AuthenticatedKeyAgreementDomain</a> &d)00199 {00200 <span class="keywordflow">if</span> (d.GetCryptoParameters().Validate(GlobalRNG(), 3))00201 cout << <span class="stringliteral">"passed authenticated key agreement domain parameters validation"</span> << endl;00202 <span class="keywordflow">else</span>00203 {00204 cout << <span class="stringliteral">"FAILED authenticated key agreement domain parameters invalid"</span> << endl;00205 <span class="keywordflow">return</span> <span class="keyword">false</span>;00206 }00207 00208 <a class="code" href="class_sec_block.html">SecByteBlock</a> spriv1(d.StaticPrivateKeyLength()), spriv2(d.StaticPrivateKeyLength());00209 <a class="code" href="class_sec_block.html">SecByteBlock</a> epriv1(d.EphemeralPrivateKeyLength()), epriv2(d.EphemeralPrivateKeyLength());00210 <a class="code" href="class_sec_block.html">SecByteBlock</a> spub1(d.StaticPublicKeyLength()), spub2(d.StaticPublicKeyLength());00211 <a class="code" href="class_sec_block.html">SecByteBlock</a> epub1(d.EphemeralPublicKeyLength()), epub2(d.EphemeralPublicKeyLength());00212 <a class="code" href="class_sec_block.html">SecByteBlock</a> val1(d.AgreedValueLength()), val2(d.AgreedValueLength());00213 00214 d.GenerateStaticKeyPair(GlobalRNG(), spriv1, spub1);00215 d.GenerateStaticKeyPair(GlobalRNG(), spriv2, spub2);00216 d.GenerateEphemeralKeyPair(GlobalRNG(), epriv1, epub1);00217 d.GenerateEphemeralKeyPair(GlobalRNG(), epriv2, epub2);00218 00219 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>());00220 memset(val2.begin(), 0x11, val2.size());00221 00222 <span class="keywordflow">if</span> (!(d.Agree(val1, spriv1, epriv1, spub2, epub2) && d.Agree(val2, spriv2, epriv2, spub1, epub1)))00223 {00224 cout << <span class="stringliteral">"FAILED authenticated key agreement failed"</span> << endl;00225 <span class="keywordflow">return</span> <span class="keyword">false</span>;00226 }00227 00228 <span class="keywordflow">if</span> (memcmp(val1.<a class="code" href="class_sec_block.html#_sec_block_with_hinta7">begin</a>(), val2.begin(), d.AgreedValueLength()))00229 {00230 cout << <span class="stringliteral">"FAILED authenticated agreed values not equal"</span> << endl;00231 <span class="keywordflow">return</span> <span class="keyword">false</span>;00232 }00233 00234 cout << <span class="stringliteral">"passed authenticated key agreement"</span> << endl;00235 <span class="keywordflow">return</span> <span class="keyword">true</span>;00236 }00237 00238 <span class="keywordtype">bool</span> ValidateRSA()00239 {00240 cout << <span class="stringliteral">"\nRSA validation suite running...\n\n"</span>;00241 00242 byte out[100], outPlain[100];00243 <span class="keywordtype">bool</span> pass = <span class="keyword">true</span>, fail;00244 00245 {00246 <span class="keywordtype">char</span> *plain = <span class="stringliteral">"Everyone gets Friday off."</span>;00247 byte *signature = (byte *)00248 <span class="stringliteral">"\x05\xfa\x6a\x81\x2f\xc7\xdf\x8b\xf4\xf2\x54\x25\x09\xe0\x3e\x84"</span>00249 <span class="stringliteral">"\x6e\x11\xb9\xc6\x20\xbe\x20\x09\xef\xb4\x40\xef\xbc\xc6\x69\x21"</span>00250 <span class="stringliteral">"\x69\x94\xac\x04\xf3\x41\xb5\x7d\x05\x20\x2d\x42\x8f\xb2\xa2\x7b"</span>00251 <span class="stringliteral">"\x5c\x77\xdf\xd9\xb1\x5b\xfc\x3d\x55\x93\x53\x50\x34\x10\xc1\xe1"</span>;00252 00253 <a class="code" href="class_file_source.html">FileSource</a> keys(<span class="stringliteral">"rsa512a.dat"</span>, <span class="keyword">true</span>, <span class="keyword">new</span> <a class="code" href="class_hex_decoder.html">HexDecoder</a>);00254 <a class="code" href="struct_r_s_a_s_s_a.html">RSASSA_PKCS1v15_MD2_Signer</a> rsaPriv(keys);00255 <a class="code" href="struct_r_s_a_s_s_a.html">RSASSA_PKCS1v15_MD2_Verifier</a> rsaPub(rsaPriv);00256 00257 rsaPriv.SignMessage(GlobalRNG(), (byte *)plain, strlen(plain), out);00258 fail = memcmp(signature, out, 64) != 0;00259 pass = pass && !fail;00260 00261 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>);00262 cout << <span class="stringliteral">"signature check against test vector\n"</span>;00263 00264 fail = !rsaPub.VerifyMessage((byte *)plain, strlen(plain), out);00265 pass = pass && !fail;00266 00267 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>);00268 cout << <span class="stringliteral">"verification check against test vector\n"</span>;00269 00270 out[10]++;00271 fail = rsaPub.VerifyMessage((byte *)plain, strlen(plain), out);00272 pass = pass && !fail;00273 00274 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>);00275 cout << <span class="stringliteral">"invalid signature verification\n"</span>;00276 }00277 {00278 <a class="code" href="class_file_source.html">FileSource</a> keys(<span class="stringliteral">"rsa1024.dat"</span>, <span class="keyword">true</span>, <span class="keyword">new</span> <a class="code" href="class_hex_decoder.html">HexDecoder</a>);00279 <a class="code" href="struct_r_s_a_e_s.html">RSAES_PKCS1v15_Decryptor</a> rsaPriv(keys);00280 <a class="code" href="struct_r_s_a_e_s.html">RSAES_PKCS1v15_Encryptor</a> rsaPub(rsaPriv);00281 00282 pass = CryptoSystemValidate(rsaPriv, rsaPub) && pass;00283 }00284 {00285 byte *plain = (byte *)00286 <span class="stringliteral">"\x54\x85\x9b\x34\x2c\x49\xea\x2a"</span>;00287 byte *encrypted = (byte *)00288 <span class="stringliteral">"\x14\xbd\xdd\x28\xc9\x83\x35\x19\x23\x80\xe8\xe5\x49\xb1\x58\x2a"</span>00289 <span class="stringliteral">"\x8b\x40\xb4\x48\x6d\x03\xa6\xa5\x31\x1f\x1f\xd5\xf0\xa1\x80\xe4"</span>00290 <span class="stringliteral">"\x17\x53\x03\x29\xa9\x34\x90\x74\xb1\x52\x13\x54\x29\x08\x24\x52"</span>00291 <span class="stringliteral">"\x62\x51"</span>;00292 byte *oaepSeed = (byte *)00293 <span class="stringliteral">"\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2"</span>00294 <span class="stringliteral">"\xf0\x6c\xb5\x8f"</span>;00295 <a class="code" href="class_byte_queue.html">ByteQueue</a> bq;00296 bq.<a class="code" href="class_buffered_transformation.html#_zlib_decompressorz1_0">Put</a>(oaepSeed, 20);00297 FixedRNG rng(bq);00298 00299 <a class="code" href="class_file_source.html">FileSource</a> privFile(<span class="stringliteral">"rsa400pv.dat"</span>, <span class="keyword">true</span>, <span class="keyword">new</span> <a class="code" href="class_hex_decoder.html">HexDecoder</a>);00300 <a class="code" href="class_file_source.html">FileSource</a> pubFile(<span class="stringliteral">"rsa400pb.dat"</span>, <span class="keyword">true</span>, <span class="keyword">new</span> <a class="code" href="class_hex_decoder.html">HexDecoder</a>);00301 <a class="code" href="struct_r_s_a_e_s.html">RSAES_OAEP_SHA_Decryptor</a> rsaPriv;00302 rsaPriv.AccessKey().BERDecodeKey(privFile);00303 <a class="code" href="struct_r_s_a_e_s.html">RSAES_OAEP_SHA_Encryptor</a> rsaPub(pubFile);00304 00305 memset(out, 0, 50);00306 memset(outPlain, 0, 8);00307 rsaPub.Encrypt(rng, plain, 8, out);00308 <a class="code" href="struct_decoding_result.html">DecodingResult</a> result = rsaPriv.FixedLengthDecrypt(encrypted, outPlain);00309 fail = !result.<a class="code" href="struct_decoding_result.html#_decoding_resulto0">isValidCoding</a> || (result.<a class="code" href="struct_decoding_result.html#_decoding_resulto1">messageLength</a>!=8) || memcmp(out, encrypted, 50) || memcmp(plain, outPlain, 8);00310 pass = pass && !fail;00311 00312 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>);00313 cout << <span class="stringliteral">"PKCS 2.0 encryption and decryption\n"</span>;00314 }00315 00316 <span class="keywordflow">return</span> pass;00317 }00318 00319 <span class="keywordtype">bool</span> ValidateDH()00320 {00321 cout << <span class="stringliteral">"\nDH validation suite running...\n\n"</span>;00322 00323 <a class="code" href="class_file_source.html">FileSource</a> f(<span class="stringliteral">"dh1024.dat"</span>, <span class="keyword">true</span>, <span class="keyword">new</span> <a class="code" href="class_hex_decoder.html">HexDecoder</a>());00324 <a class="code" href="class_d_h___domain.html">DH</a> dh(f);00325 <span class="keywordflow">return</span> SimpleKeyAgreementValidate(dh);00326 }00327 00328 <span class="keywordtype">bool</span> ValidateMQV()00329 {00330 cout << <span class="stringliteral">"\nMQV validation suite running...\n\n"</span>;00331 00332 <a class="code" href="class_file_source.html">FileSource</a> f(<span class="stringliteral">"mqv1024.dat"</span>, <span class="keyword">true</span>, <span class="keyword">new</span> <a class="code" href="class_hex_decoder.html">HexDecoder</a>());00333 <a class="code" href="class_m_q_v___domain.html">MQV</a> mqv(f);00334 <span class="keywordflow">return</span> AuthenticatedKeyAgreementValidate(mqv);00335 }00336 00337 <span class="keywordtype">bool</span> ValidateLUC_DH()00338 {00339 cout << <span class="stringliteral">"\nLUC-DH validation suite running...\n\n"</span>;00340 00341 <a class="code" href="class_file_source.html">FileSource</a> f(<span class="stringliteral">"lucd512.dat"</span>, <span class="keyword">true</span>, <span class="keyword">new</span> <a class="code" href="class_hex_decoder.html">HexDecoder</a>());00342 <a class="code" href="class_d_h___domain.html">LUC_DH</a> dh(f);00343 <span class="keywordflow">return</span> SimpleKeyAgreementValidate(dh);00344 }00345 00346 <span class="keywordtype">bool</span> ValidateXTR_DH()00347 {00348 cout << <span class="stringliteral">"\nXTR-DH validation suite running...\n\n"</span>;00349 00350 <a class="code" href="class_file_source.html">FileSource</a> f(<span class="stringliteral">"xtrdh171.dat"</span>, <span class="keyword">true</span>, <span class="keyword">new</span> <a class="code" href="class_hex_decoder.html">HexDecoder</a>());00351 <a class="code" href="class_x_t_r___d_h.html">XTR_DH</a> dh(f);00352 <span class="keywordflow">return</span> SimpleKeyAgreementValidate(dh);00353 }00354 00355 <span class="keywordtype">bool</span> ValidateElGamal()00356 {00357 cout << <span class="stringliteral">"\nElGamal validation suite running...\n\n"</span>;00358 <span class="keywordtype">bool</span> pass = <span class="keyword">true</span>;00359 {00360 <a class="code" href="class_file_source.html">FileSource</a> fc(<span class="stringliteral">"elgc1024.dat"</span>, <span class="keyword">true</span>, <span class="keyword">new</span> <a class="code" href="class_hex_decoder.html">HexDecoder</a>);00361 <a class="code" href="class_p_k___final_template.html">ElGamalDecryptor</a> privC(fc);00362 <a class="code" href="class_p_k___final_template.html">ElGamalEncryptor</a> pubC(privC);00363 privC.AccessKey().Precompute();00364 <a class="code" href="class_byte_queue.html">ByteQueue</a> queue;00365 privC.AccessKey().SavePrecomputation(queue);00366 privC.AccessKey().LoadPrecomputation(queue);00367 00368 pass = CryptoSystemValidate(privC, pubC) && pass;00369 }00370 <span class="keywordflow">return</span> pass;00371 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -