Skip to content

Commit

Permalink
optimized encryption
Browse files Browse the repository at this point in the history
git-svn-id: http://libtorrent.svn.sourceforge.net/svnroot/libtorrent/trunk@2620 a83610d8-ad2a-0410-a6ab-fc0612d85776
  • Loading branch information
arvidn committed Aug 20, 2008
1 parent f58fa2c commit b91d0d1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 36 deletions.
5 changes: 4 additions & 1 deletion include/libtorrent/pe_crypto.hpp
Expand Up @@ -58,7 +58,9 @@ namespace libtorrent
// m_dh_secret.
int compute_secret(const char* remote_pubkey);

const char* get_secret() const;
char const* get_secret() const { return m_dh_secret; }

sha1_hash const& get_hash_xor_mask() const { return m_xor_mask; }

private:
int get_local_key_size() const
Expand All @@ -71,6 +73,7 @@ namespace libtorrent

char m_dh_local_key[96];
char m_dh_secret[96];
sha1_hash m_xor_mask;
};

class RC4_handler // Non copyable
Expand Down
13 changes: 13 additions & 0 deletions include/libtorrent/torrent.hpp
Expand Up @@ -132,6 +132,14 @@ namespace libtorrent

void parse_resume_data(std::vector<char>* resume_data);

#ifndef TORRENT_DISABLE_ENCRYPTION
sha1_hash const& obfuscated_hash() const
{ return m_obfuscated_hash; }
#endif

sha1_hash const& info_hash() const
{ return m_torrent_file->info_hash(); }

// starts the announce timer
void start();

Expand Down Expand Up @@ -821,6 +829,11 @@ namespace libtorrent
// longer be used and will be reset
boost::scoped_ptr<std::string> m_name;

#ifndef TORRENT_DISABLE_ENCRYPTION
// this is SHA1("req2" + info-hash), used for
// encrypted hand shakes
sha1_hash m_obfuscated_hash;
#endif
session_settings const& m_settings;

storage_constructor_type m_storage_constructor;
Expand Down
42 changes: 12 additions & 30 deletions src/bt_peer_connection.cpp
Expand Up @@ -1841,7 +1841,8 @@ namespace libtorrent
return;
}

cut_receive_buffer(bytes_processed, (std::min)(packet_size(), (512+20) - m_sync_bytes_read));
cut_receive_buffer(bytes_processed, (std::min)(packet_size()
, (512+20) - m_sync_bytes_read));

TORRENT_ASSERT(!packet_finished());
return;
Expand Down Expand Up @@ -1872,51 +1873,32 @@ namespace libtorrent

recv_buffer = receive_buffer();

// only calls info_hash() on the torrent_handle's, which
// never throws.
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);

std::vector<torrent_handle> active_torrents = m_ses.get_torrents();
std::vector<torrent_handle>::const_iterator i;
hasher h;
sha1_hash skey_hash, obfs_hash;
aux::session_impl::torrent_map::const_iterator i;

for (i = active_torrents.begin(); i != active_torrents.end(); ++i)
for (i = m_ses.m_torrents.begin(); i != m_ses.m_torrents.end(); ++i)
{
torrent_handle const& t_h = *i; // TODO possible errors
sha1_hash const& info_hash = t_h.info_hash();
// TODO Does info_hash need to be checked for validity?

h.reset();
h.update("req2", 4);
h.update((char*)info_hash.begin(), 20);

skey_hash = h.final();

h.reset();
h.update("req3", 4);
h.update(m_dh_key_exchange->get_secret(), dh_key_len);

obfs_hash = h.final();
torrent const& ti = *i->second;
sha1_hash const& skey_hash = ti.obfuscated_hash();
sha1_hash obfs_hash = m_dh_key_exchange->get_hash_xor_mask();
obfs_hash ^= skey_hash;

if (std::equal (recv_buffer.begin, recv_buffer.begin + 20,
(char*)obfs_hash.begin()))
if (std::equal(recv_buffer.begin, recv_buffer.begin + 20,
(char*)&obfs_hash[0]))
{
if (!t)
{
attach_to_torrent(info_hash);
attach_to_torrent(ti.info_hash());
if (is_disconnecting()) return;

t = associated_torrent().lock();
TORRENT_ASSERT(t);
}

init_pe_RC4_handler(m_dh_key_exchange->get_secret(), info_hash);
init_pe_RC4_handler(m_dh_key_exchange->get_secret(), ti.info_hash());
#ifdef TORRENT_VERBOSE_LOGGING
(*m_logger) << " stream key found, torrent located.\n";
#endif
continue; // TODO Check flow control with multiple torrents
break;
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/pe_crypto.cpp
Expand Up @@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <openssl/engine.h>

#include "libtorrent/pe_crypto.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/assert.hpp"

namespace libtorrent
Expand Down Expand Up @@ -144,12 +145,14 @@ namespace libtorrent
}
std::copy(dh_secret, dh_secret + secret_size, m_dh_secret + 96 - secret_size);
BN_free(bn_remote_pubkey);
return 0;
}

char const* dh_key_exchange::get_secret() const
{
return m_dh_secret;
// calculate the xor mask for the obfuscated hash
hasher h;
h.update("req3", 4);
h.update(m_dh_secret, 96);
m_xor_mask = h.final();

return 0;
}

} // namespace libtorrent
Expand Down
15 changes: 15 additions & 0 deletions src/torrent.cpp
Expand Up @@ -206,6 +206,13 @@ namespace libtorrent
, m_complete_sent(false)
{
parse_resume_data(resume_data);

#ifndef TORRENT_DISABLE_ENCRYPTION
hasher h;
h.update("req2", 4);
h.update((char*)&tf->info_hash()[0], 20);
m_obfuscated_hash = h.final();
#endif
}

torrent::torrent(
Expand Down Expand Up @@ -280,6 +287,14 @@ namespace libtorrent
, m_complete_sent(false)
{
parse_resume_data(resume_data);

#ifndef TORRENT_DISABLE_ENCRYPTION
hasher h;
h.update("req2", 4);
h.update((char*)&info_hash[0], 20);
m_obfuscated_hash = h.final();
#endif

#ifndef NDEBUG
m_files_checked = false;
#endif
Expand Down

0 comments on commit b91d0d1

Please sign in to comment.