From 02b3873e6d1386293110ca7f6ffce8f7e96c7476 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 7 Nov 2011 22:24:47 +0900 Subject: [PATCH] Removed ARC4Decryptor because ARC4Encryptor can decrypt the message. --- src/ARC4Decryptor.h | 67 --------------------------- src/InitiatorMSEHandshakeCommand.cc | 3 +- src/LibgcryptARC4Decryptor.cc | 72 ----------------------------- src/LibgcryptARC4Decryptor.h | 59 ----------------------- src/LibnettleARC4Decryptor.cc | 57 ----------------------- src/LibnettleARC4Decryptor.h | 59 ----------------------- src/LibsslARC4Decryptor.cc | 72 ----------------------------- src/LibsslARC4Decryptor.h | 62 ------------------------- src/MSEHandshake.cc | 17 ++++--- src/MSEHandshake.h | 5 +- src/Makefile.am | 4 -- src/PeerConnection.cc | 5 +- src/PeerConnection.h | 5 +- src/ReceiverMSEHandshakeCommand.cc | 1 - test/ARC4Test.cc | 13 +++--- 15 files changed, 21 insertions(+), 480 deletions(-) delete mode 100644 src/ARC4Decryptor.h delete mode 100644 src/LibgcryptARC4Decryptor.cc delete mode 100644 src/LibgcryptARC4Decryptor.h delete mode 100644 src/LibnettleARC4Decryptor.cc delete mode 100644 src/LibnettleARC4Decryptor.h delete mode 100644 src/LibsslARC4Decryptor.cc delete mode 100644 src/LibsslARC4Decryptor.h diff --git a/src/ARC4Decryptor.h b/src/ARC4Decryptor.h deleted file mode 100644 index c2cb4c29..00000000 --- a/src/ARC4Decryptor.h +++ /dev/null @@ -1,67 +0,0 @@ -/* */ -#ifndef D_ARC4_DECRYPTOR_H -#define D_ARC4_DECRYPTOR_H - -#include "common.h" -#ifdef HAVE_LIBNETTLE -# include "LibnettleARC4Decryptor.h" -#elif HAVE_LIBGCRYPT -# include "LibgcryptARC4Decryptor.h" -#elif HAVE_OPENSSL -# include "LibsslARC4Decryptor.h" -#else - -// provide empty implementation to compile sources without both libgcrypt and -// openssl installed -namespace aria2 { - -class ARC4Decryptor { -public: - ARC4Decryptor() {} - - ~ARC4Decryptor() {} - - void init(const unsigned char* key, size_t keyLength) {} - - void decrypt(unsigned char* out, size_t outLength, - const unsigned char* in, size_t inLength) {} -}; - -} // namespace aria2 - -#endif - -#endif // D_ARC4_DECRYPTOR_H diff --git a/src/InitiatorMSEHandshakeCommand.cc b/src/InitiatorMSEHandshakeCommand.cc index e76a7336..cf82858e 100644 --- a/src/InitiatorMSEHandshakeCommand.cc +++ b/src/InitiatorMSEHandshakeCommand.cc @@ -50,7 +50,6 @@ #include "Option.h" #include "MSEHandshake.h" #include "ARC4Encryptor.h" -#include "ARC4Decryptor.h" #include "RequestGroup.h" #include "DownloadContext.h" #include "bittorrent_helper.h" @@ -158,7 +157,7 @@ bool InitiatorMSEHandshakeCommand::executeInternal() { mseHandshake_->getDecryptor()); size_t buflen = mseHandshake_->getBufferLength(); array_ptr buffer(new unsigned char[buflen]); - mseHandshake_->getDecryptor()->decrypt(buffer, buflen, + mseHandshake_->getDecryptor()->encrypt(buffer, buflen, mseHandshake_->getBuffer(), buflen); peerConnection->presetBuffer(buffer, buflen); diff --git a/src/LibgcryptARC4Decryptor.cc b/src/LibgcryptARC4Decryptor.cc deleted file mode 100644 index 2515bb10..00000000 --- a/src/LibgcryptARC4Decryptor.cc +++ /dev/null @@ -1,72 +0,0 @@ -/* */ -#include "LibgcryptARC4Decryptor.h" - -#include - -#include "DlAbortEx.h" -#include "fmt.h" - -namespace aria2 { - -namespace { -void handleError(gcry_error_t err) -{ - throw DL_ABORT_EX - (fmt("Exception in libgcrypt routine(ARC4Decryptor class): %s", - gcry_strerror(err))); -} -} // namespace - -ARC4Decryptor::ARC4Decryptor() {} - -ARC4Decryptor::~ARC4Decryptor() {} - -void ARC4Decryptor::init(const unsigned char* key, size_t keyLength) -{ - ctx_.init(key, keyLength); -} - -void ARC4Decryptor::decrypt -(unsigned char* out, size_t outLength, const unsigned char* in, size_t inLength) -{ - gcry_error_t r = gcry_cipher_decrypt(ctx_.getCipherContext(), - out, outLength, in, inLength); - if(r) { - handleError(r); - } -} - -} // namespace aria2 diff --git a/src/LibgcryptARC4Decryptor.h b/src/LibgcryptARC4Decryptor.h deleted file mode 100644 index 738528d7..00000000 --- a/src/LibgcryptARC4Decryptor.h +++ /dev/null @@ -1,59 +0,0 @@ -/* */ -#ifndef D_LIBGCRYPT_ARC4_DECRYPTOR_H -#define D_LIBGCRYPT_ARC4_DECRYPTOR_H - -#include "common.h" -#include "LibgcryptARC4Context.h" - -namespace aria2 { - -class ARC4Decryptor { -private: - LibgcryptARC4Context ctx_; -public: - ARC4Decryptor(); - - ~ARC4Decryptor(); - - void init(const unsigned char* key, size_t keyLength); - - void decrypt(unsigned char* out, size_t outLength, - const unsigned char* in, size_t inLength); -}; - -} // namespace aria2 - -#endif // D_LIBGCRYPT_ARC4_DECRYPTOR_H diff --git a/src/LibnettleARC4Decryptor.cc b/src/LibnettleARC4Decryptor.cc deleted file mode 100644 index e79a21bf..00000000 --- a/src/LibnettleARC4Decryptor.cc +++ /dev/null @@ -1,57 +0,0 @@ -/* */ -#include "LibnettleARC4Decryptor.h" - -#include - -namespace aria2 { - -ARC4Decryptor::ARC4Decryptor() {} - -ARC4Decryptor::~ARC4Decryptor() {} - -void ARC4Decryptor::init(const unsigned char* key, size_t keyLength) -{ - ctx_.init(key, keyLength); -} - -void ARC4Decryptor::decrypt(unsigned char* out, size_t outLength, - const unsigned char* in, size_t inLength) -{ - assert(outLength == inLength); - arcfour_crypt(ctx_.getCipherContext(), outLength, out, in); -} - -} // namespace aria2 diff --git a/src/LibnettleARC4Decryptor.h b/src/LibnettleARC4Decryptor.h deleted file mode 100644 index fab17cdb..00000000 --- a/src/LibnettleARC4Decryptor.h +++ /dev/null @@ -1,59 +0,0 @@ -/* */ -#ifndef D_LIBNETTLE_ARC4_DECRYPTOR_H -#define D_LIBNETTLE_ARC4_DECRYPTOR_H - -#include "common.h" -#include "LibnettleARC4Context.h" - -namespace aria2 { - -class ARC4Decryptor { -private: - LibnettleARC4Context ctx_; -public: - ARC4Decryptor(); - - ~ARC4Decryptor(); - - void init(const unsigned char* key, size_t keyLength); - - void decrypt(unsigned char* out, size_t outLength, - const unsigned char* in, size_t inLength); -}; - -} // namespace aria2 - -#endif // D_LIBNETTLE_ARC4_DECRYPTOR_H diff --git a/src/LibsslARC4Decryptor.cc b/src/LibsslARC4Decryptor.cc deleted file mode 100644 index 7af932d4..00000000 --- a/src/LibsslARC4Decryptor.cc +++ /dev/null @@ -1,72 +0,0 @@ -/* */ -#include "LibsslARC4Decryptor.h" - -#include - -#include "DlAbortEx.h" -#include "fmt.h" - -namespace aria2 { - -namespace { -void handleError() -{ - throw DL_ABORT_EX - (fmt("Exception in libssl routine(ARC4Decryptor class): %s", - ERR_error_string(ERR_get_error(), 0))); -} -} // namespace - -ARC4Decryptor::ARC4Decryptor() {} - -ARC4Decryptor::~ARC4Decryptor() {} - -void ARC4Decryptor::init(const unsigned char* key, size_t keyLength) -{ - ctx_.init(key, keyLength, 0); -} - -void ARC4Decryptor::decrypt(unsigned char* out, size_t outLength, - const unsigned char* in, size_t inLength) -{ - int soutLength = outLength; - if(!EVP_CipherUpdate(ctx_.getCipherContext(), out, &soutLength, - in, inLength)) { - handleError(); - } -} - -} // namespace aria2 diff --git a/src/LibsslARC4Decryptor.h b/src/LibsslARC4Decryptor.h deleted file mode 100644 index 53e591c7..00000000 --- a/src/LibsslARC4Decryptor.h +++ /dev/null @@ -1,62 +0,0 @@ -/* */ -#ifndef D_LIBSSL_ARC4_DECRYPTOR_H -#define D_LIBSSL_ARC4_DECRYPTOR_H - -#include "common.h" - -#include - -#include "LibsslARC4Context.h" - -namespace aria2 { - -class ARC4Decryptor { -private: - LibsslARC4Context ctx_; -public: - ARC4Decryptor(); - - ~ARC4Decryptor(); - - void init(const unsigned char* key, size_t keyLength); - - void decrypt(unsigned char* out, size_t outLength, - const unsigned char* in, size_t inLength); -}; - -} // namespace aria2 - -#endif // D_LIBSSL_ARC4_DECRYPTOR_H diff --git a/src/MSEHandshake.cc b/src/MSEHandshake.cc index 861ae1f9..74080feb 100644 --- a/src/MSEHandshake.cc +++ b/src/MSEHandshake.cc @@ -46,7 +46,6 @@ #include "a2netcompat.h" #include "DHKeyExchange.h" #include "ARC4Encryptor.h" -#include "ARC4Decryptor.h" #include "MessageDigest.h" #include "message_digest_helper.h" #include "SimpleRandomizer.h" @@ -204,14 +203,14 @@ void MSEHandshake::initCipher(const unsigned char* infoHash) sha1_->reset(); message_digest::digest(peerCipherKey, sizeof(peerCipherKey), sha1_, s, sizeof(s)); - decryptor_.reset(new ARC4Decryptor()); + decryptor_.reset(new ARC4Encryptor()); decryptor_->init(peerCipherKey, sizeof(peerCipherKey)); // discard first 1024 bytes ARC4 output. unsigned char from[1024]; unsigned char to[1024]; encryptor_->encrypt(to, 1024, from, 1024); - decryptor_->decrypt(to, 1024, from, 1024); + decryptor_->encrypt(to, 1024, from, 1024); if(initiator_) { ARC4Encryptor enc; @@ -264,7 +263,7 @@ void MSEHandshake::createReq23Hash(unsigned char* md, const unsigned char* infoH uint16_t MSEHandshake::decodeLength16(const unsigned char* buffer) { uint16_t be; - decryptor_->decrypt(reinterpret_cast(&be), + decryptor_->encrypt(reinterpret_cast(&be), sizeof(be), buffer, sizeof(be)); return ntohs(be); @@ -355,7 +354,7 @@ bool MSEHandshake::receiveInitiatorCryptoSelectAndPadDLength() unsigned char* rbufptr = rbuf_; { unsigned char cryptoSelect[CRYPTO_BITFIELD_LENGTH]; - decryptor_->decrypt(cryptoSelect, sizeof(cryptoSelect), + decryptor_->encrypt(cryptoSelect, sizeof(cryptoSelect), rbufptr, sizeof(cryptoSelect)); if(cryptoSelect[3]&CRYPTO_PLAIN_TEXT && option_->get(PREF_BT_MIN_CRYPTO_LEVEL) == V_PLAIN) { @@ -392,7 +391,7 @@ bool MSEHandshake::receivePad() return true; } unsigned char temp[MAX_PAD_LENGTH]; - decryptor_->decrypt(temp, padLength_, rbuf_, padLength_); + decryptor_->encrypt(temp, padLength_, rbuf_, padLength_); // shift rbuf_ shiftBuffer(padLength_); return true; @@ -459,7 +458,7 @@ bool MSEHandshake::receiveReceiverHashAndPadCLength rbufptr += VC_LENGTH; { unsigned char cryptoProvide[CRYPTO_BITFIELD_LENGTH]; - decryptor_->decrypt(cryptoProvide, sizeof(cryptoProvide), + decryptor_->encrypt(cryptoProvide, sizeof(cryptoProvide), rbufptr, sizeof(cryptoProvide)); // TODO choose the crypto type based on the preference. // For now, choose ARC4. @@ -514,7 +513,7 @@ bool MSEHandshake::receiveReceiverIA() } delete [] ia_; ia_ = new unsigned char[iaLength_]; - decryptor_->decrypt(ia_, iaLength_, rbuf_, iaLength_); + decryptor_->encrypt(ia_, iaLength_, rbuf_, iaLength_); A2_LOG_DEBUG(fmt("CUID#%lld - IA received.", cuid_)); // shift rbuf_ shiftBuffer(iaLength_); @@ -566,7 +565,7 @@ void MSEHandshake::verifyVC(const unsigned char* vcbuf) { A2_LOG_DEBUG(fmt("CUID#%lld - Verifying VC.", cuid_)); unsigned char vc[VC_LENGTH]; - decryptor_->decrypt(vc, sizeof(vc), vcbuf, sizeof(vc)); + decryptor_->encrypt(vc, sizeof(vc), vcbuf, sizeof(vc)); if(memcmp(VC, vc, sizeof(VC)) != 0) { throw DL_ABORT_EX (fmt("Invalid VC: %s", util::toHex(vc, VC_LENGTH).c_str())); diff --git a/src/MSEHandshake.h b/src/MSEHandshake.h index 65801940..94e57b80 100644 --- a/src/MSEHandshake.h +++ b/src/MSEHandshake.h @@ -50,7 +50,6 @@ class Option; class SocketCore; class DHKeyExchange; class ARC4Encryptor; -class ARC4Decryptor; class DownloadContext; class MessageDigest; @@ -90,7 +89,7 @@ private: CRYPTO_TYPE negotiatedCryptoType_; DHKeyExchange* dh_; SharedHandle encryptor_; - SharedHandle decryptor_; + SharedHandle decryptor_; unsigned char infoHash_[INFO_HASH_LENGTH]; unsigned char secret_[KEY_LENGTH]; bool initiator_; @@ -203,7 +202,7 @@ public: return encryptor_; } - const SharedHandle& getDecryptor() const + const SharedHandle& getDecryptor() const { return decryptor_; } diff --git a/src/Makefile.am b/src/Makefile.am index eeec254b..85dc6f05 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -265,7 +265,6 @@ endif # HAVE_LIBGNUTLS if HAVE_LIBGCRYPT SRCS += LibgcryptMessageDigestImpl.cc LibgcryptMessageDigestImpl.h\ LibgcryptARC4Context.cc LibgcryptARC4Context.h\ - LibgcryptARC4Decryptor.cc LibgcryptARC4Decryptor.h\ LibgcryptARC4Encryptor.cc LibgcryptARC4Encryptor.h\ LibgcryptDHKeyExchange.cc LibgcryptDHKeyExchange.h endif # HAVE_LIBGCRYPT @@ -273,7 +272,6 @@ endif # HAVE_LIBGCRYPT if HAVE_LIBNETTLE SRCS += LibnettleMessageDigestImpl.cc LibnettleMessageDigestImpl.h\ LibnettleARC4Context.cc LibnettleARC4Context.h\ - LibnettleARC4Decryptor.cc LibnettleARC4Decryptor.h\ LibnettleARC4Encryptor.cc LibnettleARC4Encryptor.h endif # HAVE_LIBNETTLE @@ -286,7 +284,6 @@ if HAVE_OPENSSL SRCS += LibsslTLSContext.cc LibsslTLSContext.h\ LibsslMessageDigestImpl.cc LibsslMessageDigestImpl.h\ LibsslARC4Context.cc LibsslARC4Context.h\ - LibsslARC4Decryptor.cc LibsslARC4Decryptor.h\ LibsslARC4Encryptor.cc LibsslARC4Encryptor.h\ LibsslDHKeyExchange.cc LibsslDHKeyExchange.h endif # HAVE_OPENSSL @@ -466,7 +463,6 @@ SRCS += PeerAbstractCommand.cc PeerAbstractCommand.h\ InitiatorMSEHandshakeCommand.cc InitiatorMSEHandshakeCommand.h\ ReceiverMSEHandshakeCommand.cc ReceiverMSEHandshakeCommand.h\ MSEHandshake.cc MSEHandshake.h\ - ARC4Decryptor.h\ ARC4Encryptor.h\ DHKeyExchange.h\ BtConstants.h\ diff --git a/src/PeerConnection.cc b/src/PeerConnection.cc index b894f8ae..6a3320fb 100644 --- a/src/PeerConnection.cc +++ b/src/PeerConnection.cc @@ -46,7 +46,6 @@ #include "Socket.h" #include "a2netcompat.h" #include "ARC4Encryptor.h" -#include "ARC4Decryptor.h" #include "fmt.h" #include "util.h" #include "Peer.h" @@ -216,7 +215,7 @@ void PeerConnection::readData unsigned char temp[MAX_PAYLOAD_LEN]; assert(MAX_PAYLOAD_LEN >= length); socket_->readData(temp, length); - decryptor_->decrypt(data, length, temp, length); + decryptor_->encrypt(data, length, temp, length); } else { socket_->readData(data, length); } @@ -224,7 +223,7 @@ void PeerConnection::readData void PeerConnection::enableEncryption (const SharedHandle& encryptor, - const SharedHandle& decryptor) + const SharedHandle& decryptor) { encryptor_ = encryptor; decryptor_ = decryptor; diff --git a/src/PeerConnection.h b/src/PeerConnection.h index 15676381..3da56eb4 100644 --- a/src/PeerConnection.h +++ b/src/PeerConnection.h @@ -48,7 +48,6 @@ namespace aria2 { class Peer; class SocketCore; class ARC4Encryptor; -class ARC4Decryptor; // The maximum length of payload. Messages beyond that length are // dropped. @@ -70,7 +69,7 @@ private: bool encryptionEnabled_; SharedHandle encryptor_; - SharedHandle decryptor_; + SharedHandle decryptor_; bool prevPeek_; @@ -104,7 +103,7 @@ public: (unsigned char* data, size_t& dataLength, bool peek = false); void enableEncryption(const SharedHandle& encryptor, - const SharedHandle& decryptor); + const SharedHandle& decryptor); void presetBuffer(const unsigned char* data, size_t length); diff --git a/src/ReceiverMSEHandshakeCommand.cc b/src/ReceiverMSEHandshakeCommand.cc index da445f6d..cfde2d42 100644 --- a/src/ReceiverMSEHandshakeCommand.cc +++ b/src/ReceiverMSEHandshakeCommand.cc @@ -46,7 +46,6 @@ #include "Option.h" #include "MSEHandshake.h" #include "ARC4Encryptor.h" -#include "ARC4Decryptor.h" #include "RequestGroupMan.h" #include "BtRegistry.h" #include "DownloadContext.h" diff --git a/test/ARC4Test.cc b/test/ARC4Test.cc index 0f6b672b..9275fa49 100644 --- a/test/ARC4Test.cc +++ b/test/ARC4Test.cc @@ -3,7 +3,6 @@ #include #include -#include "ARC4Decryptor.h" #include "Exception.h" #include "util.h" @@ -12,23 +11,23 @@ namespace aria2 { class ARC4Test:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(ARC4Test); - CPPUNIT_TEST(testEncryptDecrypt); + CPPUNIT_TEST(testEncrypt); CPPUNIT_TEST_SUITE_END(); public: void setUp() {} void tearDown() {} - void testEncryptDecrypt(); + void testEncrypt(); }; CPPUNIT_TEST_SUITE_REGISTRATION(ARC4Test); -void ARC4Test::testEncryptDecrypt() +void ARC4Test::testEncrypt() { ARC4Encryptor enc; - ARC4Decryptor dec; + ARC4Encryptor dec; const size_t LEN = 20; unsigned char key[LEN]; memset(key, 0, LEN); @@ -39,12 +38,12 @@ void ARC4Test::testEncryptDecrypt() unsigned char encrypted[LEN]; unsigned char decrypted[LEN]; enc.encrypt(encrypted, LEN, key, LEN); - dec.decrypt(decrypted, LEN, encrypted, LEN); + dec.encrypt(decrypted, LEN, encrypted, LEN); CPPUNIT_ASSERT(memcmp(key, decrypted, LEN) == 0); // once more enc.encrypt(encrypted, LEN, key, LEN); - dec.decrypt(decrypted, LEN, encrypted, LEN); + dec.encrypt(decrypted, LEN, encrypted, LEN); CPPUNIT_ASSERT(memcmp(key, decrypted, LEN) == 0); }