📄 block.cpp
字号:
/* This file is in the public domain *//* If defined, make sure decryption actually works (ie is the inverse of the encryption function). But it makes the benchmarks really slow, because of all the overhead for checking the decryption. So it's normally disabled (don't worry, it's turned on once in a while to make sure nothing is broken).*///#define CHECK_DECRYPTION// I added this warning after once spending 4 hours searching around trying to// find out why the block ciphers were going so slow. It sucked.#ifdef CHECK_DECRYPTION#warning "WARNING: Checking decryption. This will slow down the benchmarks!"#endif#include <iostream>#include <string>#include <cstdlib>#include <botan/filter.h>using namespace Botan;#ifdef CHECK_DECRYPTION#include <botan/lookup.h>class ECB_Encryption_ErrorCheck : public Filter { public: void write(const byte[], u32bit); void end_msg(); ECB_Encryption_ErrorCheck(const std::string& cipher_name, const SymmetricKey& key) : BLOCKSIZE(block_size_of(cipher_name)) { cipher = get_block_cipher(cipher_name); input_hash = get_hash("SHA-1"); decrypt_hash = get_hash("SHA-1"); buffer.create(BLOCKSIZE); cipher->set_key(key); position = 0; } ~ECB_Encryption_ErrorCheck() { delete cipher; delete input_hash; delete decrypt_hash; } private: const u32bit BLOCKSIZE; BlockCipher* cipher; SecureVector<byte> buffer; u32bit position; HashFunction* input_hash, *decrypt_hash; };void ECB_Encryption_ErrorCheck::write(const byte input[], u32bit length) { input_hash->update(input, length); buffer.copy(position, input, length); if(position + length >= BLOCKSIZE) { cipher->encrypt(buffer); send(buffer, BLOCKSIZE); cipher->decrypt(buffer); decrypt_hash->update(buffer, BLOCKSIZE); input += (BLOCKSIZE - position); length -= (BLOCKSIZE - position); while(length >= BLOCKSIZE) { cipher->encrypt(input, buffer); send(buffer, BLOCKSIZE); cipher->decrypt(buffer); decrypt_hash->update(buffer, BLOCKSIZE); input += BLOCKSIZE; length -= BLOCKSIZE; } buffer.copy(input, length); position = 0; } position += length; }void ECB_Encryption_ErrorCheck::end_msg() { SecureVector<byte> hash1 = input_hash->final(); SecureVector<byte> hash2 = decrypt_hash->final(); if(hash1 != hash2) { std::cout << "In " << cipher->name() << " decryption check failed." << std::endl; } if(position) throw Exception("ECB: input was not in full blocks"); }#else#include <botan/ecb.h>#endifFilter* lookup_block(const std::string& algname, const std::string& key) {#ifdef CHECK_DECRYPTION #define ECB_Filter ECB_Encryption_ErrorCheck#else #define ECB_Filter ECB_Encryption#endif Filter* cipher = 0; try { cipher = new ECB_Filter(algname, key); } catch(Algorithm_Not_Found) {} return cipher; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -