📄 validat1_8cpp-source.html
字号:
00474 <a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a> filter(e, <span class="keyword">new</span> <a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(d));00475 byte plaintext[20480];00476 00477 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=1; i<<span class="keyword">sizeof</span>(plaintext); i*=2)00478 {00479 <a class="code" href="class_sec_block.html">SecByteBlock</a> iv(e.IVSize());00480 e.GetNextIV(iv);00481 00482 <span class="keywordflow">if</span> (iv == lastIV)00483 <span class="keywordflow">return</span> <span class="keyword">false</span>;00484 <span class="keywordflow">else</span>00485 lastIV = iv;00486 00487 e.Resynchronize(iv);00488 d.Resynchronize(iv);00489 00490 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length = STDMAX(GlobalRNG().GenerateWord32(0, i), (word32)e.MinLastBlockSize());00491 GlobalRNG().<a class="code" href="class_random_pool.html#_random_poola7">GenerateBlock</a>(plaintext, length);00492 00493 <span class="keywordflow">if</span> (!TestFilter(filter, plaintext, length, plaintext, length))00494 <span class="keywordflow">return</span> <span class="keyword">false</span>;00495 }00496 00497 <span class="keywordflow">return</span> <span class="keyword">true</span>;00498 }00499 00500 <span class="keywordtype">bool</span> ValidateCipherModes()00501 {00502 cout << <span class="stringliteral">"\nTesting DES modes...\n\n"</span>;00503 <span class="keyword">const</span> byte key[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};00504 <span class="keyword">const</span> byte iv[] = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};00505 <span class="keyword">const</span> byte plain[] = { <span class="comment">// "Now is the time for all " without tailing 0</span>00506 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,00507 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,00508 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};00509 <a class="code" href="class_block_cipher_template.html">DESEncryption</a> desE(key);00510 <a class="code" href="class_block_cipher_template.html">DESDecryption</a> desD(key);00511 <span class="keywordtype">bool</span> pass=<span class="keyword">true</span>, fail;00512 00513 {00514 <span class="comment">// from FIPS 81</span>00515 <span class="keyword">const</span> byte encrypted[] = {00516 0x3f, 0xa4, 0x0e, 0x8a, 0x98, 0x4d, 0x48, 0x15,00517 0x6a, 0x27, 0x17, 0x87, 0xab, 0x88, 0x83, 0xf9,00518 0x89, 0x3d, 0x51, 0xec, 0x4b, 0x56, 0x3b, 0x53};00519 00520 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">ECB_Mode_ExternalCipher::Encryption</a> modeE(desE);00521 fail = !TestFilter(<a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(modeE, NULL, StreamTransformationFilter::NO_PADDING).Ref(),00522 plain, <span class="keyword">sizeof</span>(plain), encrypted, <span class="keyword">sizeof</span>(encrypted));00523 pass = pass && !fail;00524 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"ECB encryption"</span> << endl;00525 00526 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">ECB_Mode_ExternalCipher::Decryption</a> modeD(desD);00527 fail = !TestFilter(<a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(modeD, NULL, StreamTransformationFilter::NO_PADDING).Ref(),00528 encrypted, <span class="keyword">sizeof</span>(encrypted), plain, <span class="keyword">sizeof</span>(plain));00529 pass = pass && !fail;00530 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"ECB decryption"</span> << endl;00531 }00532 {00533 <span class="comment">// from FIPS 81</span>00534 <span class="keyword">const</span> byte encrypted[] = {00535 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C, 00536 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F, 00537 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6};00538 00539 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">CBC_Mode_ExternalCipher::Encryption</a> modeE(desE, iv);00540 fail = !TestFilter(<a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(modeE, NULL, StreamTransformationFilter::NO_PADDING).Ref(),00541 plain, <span class="keyword">sizeof</span>(plain), encrypted, <span class="keyword">sizeof</span>(encrypted));00542 pass = pass && !fail;00543 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"CBC encryption with no padding"</span> << endl;00544 00545 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">CBC_Mode_ExternalCipher::Decryption</a> modeD(desD, iv);00546 fail = !TestFilter(<a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(modeD, NULL, StreamTransformationFilter::NO_PADDING).Ref(),00547 encrypted, <span class="keyword">sizeof</span>(encrypted), plain, <span class="keyword">sizeof</span>(plain));00548 pass = pass && !fail;00549 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"CBC decryption with no padding"</span> << endl;00550 00551 fail = !TestModeIV(modeE, modeD);00552 pass = pass && !fail;00553 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"CBC mode IV generation"</span> << endl;00554 }00555 {00556 <span class="comment">// generated with Crypto++, matches FIPS 81</span>00557 <span class="comment">// but has extra 8 bytes as result of padding</span>00558 <span class="keyword">const</span> byte encrypted[] = {00559 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C, 00560 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F, 00561 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6, 00562 0x62, 0xC1, 0x6A, 0x27, 0xE4, 0xFC, 0xF2, 0x77};00563 00564 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">CBC_Mode_ExternalCipher::Encryption</a> modeE(desE, iv);00565 fail = !TestFilter(<a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(modeE).Ref(),00566 plain, <span class="keyword">sizeof</span>(plain), encrypted, <span class="keyword">sizeof</span>(encrypted));00567 pass = pass && !fail;00568 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"CBC encryption with PKCS #7 padding"</span> << endl;00569 00570 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">CBC_Mode_ExternalCipher::Decryption</a> modeD(desD, iv);00571 fail = !TestFilter(<a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(modeD).Ref(),00572 encrypted, <span class="keyword">sizeof</span>(encrypted), plain, <span class="keyword">sizeof</span>(plain));00573 pass = pass && !fail;00574 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"CBC decryption with PKCS #7 padding"</span> << endl;00575 }00576 {00577 <span class="comment">// generated with Crypto++, matches FIPS 81</span>00578 <span class="comment">// but has extra 8 bytes as result of padding</span>00579 <span class="keyword">const</span> byte encrypted[] = {00580 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C, 00581 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F, 00582 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6, 00583 0x57, 0x25, 0x0C, 0x94, 0x83, 0xD5, 0x01, 0x79};00584 00585 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">CBC_Mode_ExternalCipher::Encryption</a> modeE(desE, iv);00586 fail = !TestFilter(<a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(modeE, NULL, StreamTransformationFilter::ONE_AND_ZEROS_PADDING).Ref(),00587 plain, <span class="keyword">sizeof</span>(plain), encrypted, <span class="keyword">sizeof</span>(encrypted));00588 pass = pass && !fail;00589 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"CBC encryption with one-and-zeros padding"</span> << endl;00590 00591 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">CBC_Mode_ExternalCipher::Decryption</a> modeD(desD, iv);00592 fail = !TestFilter(<a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(modeD, NULL, StreamTransformationFilter::ONE_AND_ZEROS_PADDING).Ref(),00593 encrypted, <span class="keyword">sizeof</span>(encrypted), plain, <span class="keyword">sizeof</span>(plain));00594 pass = pass && !fail;00595 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"CBC decryption with one-and-zeros padding"</span> << endl;00596 }00597 {00598 <span class="keyword">const</span> byte plain[] = {<span class="charliteral">'a'</span>, 0, 0, 0, 0, 0, 0, 0};00599 <span class="comment">// generated with Crypto++</span>00600 <span class="keyword">const</span> byte encrypted[] = {00601 0x9B, 0x47, 0x57, 0x59, 0xD6, 0x9C, 0xF6, 0xD0};00602 00603 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">CBC_Mode_ExternalCipher::Encryption</a> modeE(desE, iv);00604 fail = !TestFilter(<a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(modeE, NULL, StreamTransformationFilter::ZEROS_PADDING).Ref(),00605 plain, 1, encrypted, <span class="keyword">sizeof</span>(encrypted));00606 pass = pass && !fail;00607 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"CBC encryption with zeros padding"</span> << endl;00608 00609 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">CBC_Mode_ExternalCipher::Decryption</a> modeD(desD, iv);00610 fail = !TestFilter(<a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(modeD, NULL, StreamTransformationFilter::ZEROS_PADDING).Ref(),00611 encrypted, <span class="keyword">sizeof</span>(encrypted), plain, <span class="keyword">sizeof</span>(plain));00612 pass = pass && !fail;00613 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"CBC decryption with zeros padding"</span> << endl;00614 }00615 {00616 <span class="comment">// generated with Crypto++, matches FIPS 81</span>00617 <span class="comment">// but with last two blocks swapped as result of CTS</span>00618 <span class="keyword">const</span> byte encrypted[] = {00619 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C, 00620 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6, 00621 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F};00622 00623 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">CBC_CTS_Mode_ExternalCipher::Encryption</a> modeE(desE, iv);00624 fail = !TestFilter(<a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(modeE).Ref(),00625 plain, <span class="keyword">sizeof</span>(plain), encrypted, <span class="keyword">sizeof</span>(encrypted));00626 pass = pass && !fail;00627 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"CBC encryption with ciphertext stealing (CTS)"</span> << endl;00628 00629 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">CBC_CTS_Mode_ExternalCipher::Decryption</a> modeD(desD, iv);00630 fail = !TestFilter(<a class="code" href="class_stream_transformation_filter.html">StreamTransformationFilter</a>(modeD).Ref(),00631 encrypted, <span class="keyword">sizeof</span>(encrypted), plain, <span class="keyword">sizeof</span>(plain));00632 pass = pass && !fail;00633 cout << (fail ? <span class="stringliteral">"FAILED "</span> : <span class="stringliteral">"passed "</span>) << <span class="stringliteral">"CBC decryption with ciphertext stealing (CTS)"</span> << endl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -