feature to encrypt peer connections with a secret AES-256 key stored in .torrent file

This commit is contained in:
Arvid Norberg
2011-09-05 05:50:41 +00:00
parent 43c9e640ab
commit 92bcb9ebfd
17 changed files with 397 additions and 69 deletions

View File

@@ -74,7 +74,7 @@ void display_pe_settings(libtorrent::pe_settings s)
void test_transfer(libtorrent::pe_settings::enc_policy policy,
libtorrent::pe_settings::enc_level level = libtorrent::pe_settings::both,
bool pref_rc4 = false)
bool pref_rc4 = false, bool encrypted_torrent = false)
{
using namespace libtorrent;
using std::cerr;
@@ -106,7 +106,8 @@ void test_transfer(libtorrent::pe_settings::enc_policy policy,
torrent_handle tor2;
using boost::tuples::ignore;
boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0, true, false, true, "_pe");
boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0, true, false, true
, "_pe", 16 * 1024, 0, false, 0, true, encrypted_torrent);
std::cerr << "waiting for transfer to complete\n";
@@ -131,6 +132,32 @@ void test_transfer(libtorrent::pe_settings::enc_policy policy,
remove_all("./tmp3_pe", ec);
}
void test_enc_handler(libtorrent::encryption_handler* a, libtorrent::encryption_handler* b)
{
int repcount = 128;
for (int rep = 0; rep < repcount; ++rep)
{
std::size_t buf_len = rand() % (512 * 1024);
char* buf = new char[buf_len];
char* cmp_buf = new char[buf_len];
std::generate(buf, buf + buf_len, &std::rand);
std::memcpy(cmp_buf, buf, buf_len);
a->encrypt(buf, buf_len);
TEST_CHECK(!std::equal(buf, buf + buf_len, cmp_buf));
b->decrypt(buf, buf_len);
TEST_CHECK(std::equal(buf, buf + buf_len, cmp_buf));
b->encrypt(buf, buf_len);
TEST_CHECK(!std::equal(buf, buf + buf_len, cmp_buf));
a->decrypt(buf, buf_len);
TEST_CHECK(std::equal(buf, buf + buf_len, cmp_buf));
delete[] buf;
delete[] cmp_buf;
}
}
int test_main()
{
@@ -156,35 +183,31 @@ int test_main()
sha1_hash test1_key = hasher("test1_key",8).final();
sha1_hash test2_key = hasher("test2_key",8).final();
fprintf(stderr, "testing RC4 handler\n");
rc4_handler rc41;
rc41.set_incoming_key(&test2_key[0], 20);
rc41.set_outgoing_key(&test1_key[0], 20);
rc4_handler rc42;
rc42.set_incoming_key(&test1_key[0], 20);
rc42.set_outgoing_key(&test2_key[0], 20);
for (int rep = 0; rep < repcount; ++rep)
{
std::size_t buf_len = rand() % (512 * 1024);
char* buf = new char[buf_len];
char* zero_buf = new char[buf_len];
std::fill(buf, buf + buf_len, 0);
std::fill(zero_buf, zero_buf + buf_len, 0);
rc41.encrypt(buf, buf_len);
rc42.decrypt(buf, buf_len);
TEST_CHECK(std::equal(buf, buf + buf_len, zero_buf));
rc42.encrypt(buf, buf_len);
rc41.decrypt(buf, buf_len);
TEST_CHECK(std::equal(buf, buf + buf_len, zero_buf));
delete[] buf;
delete[] zero_buf;
}
test_enc_handler(&rc41, &rc42);
#ifdef TORRENT_USE_OPENSSL
fprintf(stderr, "testing AES-256 handler\n");
char key1[32];
std::generate(key1, key1 + 32, &std::rand);
aes256_handler aes1;
aes1.set_incoming_key((const unsigned char*)key1, 32);
aes256_handler aes2;
aes2.set_incoming_key((const unsigned char*)key1, 32);
test_enc_handler(&aes1, &aes2);
#endif
test_transfer(pe_settings::enabled, pe_settings::both, false, true);
test_transfer(pe_settings::enabled, pe_settings::both, true, true);
return 0;
test_transfer(pe_settings::disabled);
test_transfer(pe_settings::forced, pe_settings::plaintext);