2007-06-06 02:41:20 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2007, Un Shyam
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/pe_crypto.hpp"
|
|
|
|
#include "libtorrent/session.hpp"
|
2014-07-19 04:17:19 +02:00
|
|
|
#include "libtorrent/random.hpp"
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
#include "setup_transfer.hpp"
|
|
|
|
#include "test.hpp"
|
|
|
|
|
2016-05-28 23:12:26 +02:00
|
|
|
extern "C" {
|
|
|
|
#include "libtorrent/tommath.h"
|
|
|
|
}
|
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
|
2007-06-13 03:48:24 +02:00
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
void test_enc_handler(libtorrent::crypto_plugin* a, libtorrent::crypto_plugin* b)
|
2011-09-05 07:50:41 +02:00
|
|
|
{
|
2013-10-16 10:22:14 +02:00
|
|
|
const int repcount = 128;
|
2011-09-05 07:50:41 +02:00
|
|
|
for (int rep = 0; rep < repcount; ++rep)
|
|
|
|
{
|
2016-05-08 15:58:13 +02:00
|
|
|
int const buf_len = rand() % (512 * 1024);
|
|
|
|
std::vector<char> buf(buf_len);
|
|
|
|
std::vector<char> cmp_buf(buf_len);
|
2015-05-18 01:32:13 +02:00
|
|
|
|
2016-05-08 15:58:13 +02:00
|
|
|
std::generate(buf.begin(), buf.end(), &std::rand);
|
|
|
|
std::copy(buf.begin(), buf.end(), cmp_buf.begin());
|
2015-05-18 01:32:13 +02:00
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
using namespace boost::asio;
|
|
|
|
std::vector<mutable_buffer> iovec;
|
2016-05-08 15:58:13 +02:00
|
|
|
iovec.push_back(mutable_buffer(&buf[0], buf_len));
|
2014-11-23 07:14:47 +01:00
|
|
|
a->encrypt(iovec);
|
2016-05-08 15:58:13 +02:00
|
|
|
TEST_CHECK(!std::equal(buf.begin(), buf.end(), cmp_buf.begin()));
|
2014-11-23 07:14:47 +01:00
|
|
|
TEST_CHECK(iovec.empty());
|
|
|
|
int consume = 0;
|
|
|
|
int produce = buf_len;
|
|
|
|
int packet_size = 0;
|
2016-05-08 15:58:13 +02:00
|
|
|
iovec.push_back(mutable_buffer(&buf[0], buf_len));
|
2014-11-23 07:14:47 +01:00
|
|
|
b->decrypt(iovec, consume, produce, packet_size);
|
2016-05-08 15:58:13 +02:00
|
|
|
TEST_CHECK(std::equal(buf.begin(), buf.end(), cmp_buf.begin()));
|
2014-11-23 07:14:47 +01:00
|
|
|
TEST_CHECK(iovec.empty());
|
|
|
|
TEST_EQUAL(consume, 0);
|
|
|
|
TEST_EQUAL(produce, buf_len);
|
|
|
|
TEST_EQUAL(packet_size, 0);
|
2015-05-18 01:32:13 +02:00
|
|
|
|
2016-05-08 15:58:13 +02:00
|
|
|
iovec.push_back(mutable_buffer(&buf[0], buf_len));
|
2014-11-23 07:14:47 +01:00
|
|
|
b->encrypt(iovec);
|
2016-05-08 15:58:13 +02:00
|
|
|
TEST_CHECK(!std::equal(buf.begin(), buf.end(), cmp_buf.begin()));
|
2014-11-23 07:14:47 +01:00
|
|
|
TEST_CHECK(iovec.empty());
|
|
|
|
consume = 0;
|
|
|
|
produce = buf_len;
|
|
|
|
packet_size = 0;
|
2016-05-08 15:58:13 +02:00
|
|
|
iovec.push_back(mutable_buffer(&buf[0], buf_len));
|
2014-11-23 07:14:47 +01:00
|
|
|
a->decrypt(iovec, consume, produce, packet_size);
|
2016-05-08 15:58:13 +02:00
|
|
|
TEST_CHECK(std::equal(buf.begin(), buf.end(), cmp_buf.begin()));
|
2014-11-23 07:14:47 +01:00
|
|
|
TEST_CHECK(iovec.empty());
|
|
|
|
TEST_EQUAL(consume, 0);
|
|
|
|
TEST_EQUAL(produce, buf_len);
|
|
|
|
TEST_EQUAL(packet_size, 0);
|
2011-09-05 07:50:41 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2015-06-18 03:55:27 +02:00
|
|
|
void print_key(char const* key)
|
|
|
|
{
|
|
|
|
for (int i = 0;i < 96; ++i)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("%02x ", unsigned(key[i]));
|
2015-06-18 03:55:27 +02:00
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("\n");
|
2015-06-18 03:55:27 +02:00
|
|
|
}
|
2013-09-01 08:52:50 +02:00
|
|
|
|
2015-06-18 03:55:27 +02:00
|
|
|
TORRENT_TEST(diffie_hellman)
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2013-09-01 08:52:50 +02:00
|
|
|
|
2013-10-16 10:22:14 +02:00
|
|
|
const int repcount = 128;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
for (int rep = 0; rep < repcount; ++rep)
|
|
|
|
{
|
2008-06-29 20:55:23 +02:00
|
|
|
dh_key_exchange DH1, DH2;
|
2015-05-18 01:32:13 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
DH1.compute_secret(DH2.get_local_key());
|
|
|
|
DH2.compute_secret(DH1.get_local_key());
|
2015-05-18 01:32:13 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
TEST_CHECK(std::equal(DH1.get_secret(), DH1.get_secret() + 96, DH2.get_secret()));
|
2015-06-18 03:55:27 +02:00
|
|
|
if (!std::equal(DH1.get_secret(), DH1.get_secret() + 96, DH2.get_secret()))
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("DH1 local: ");
|
2015-06-18 03:55:27 +02:00
|
|
|
print_key(DH1.get_local_key());
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("DH2 local: ");
|
2015-06-18 03:55:27 +02:00
|
|
|
print_key(DH2.get_local_key());
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("DH1 shared_secret: ");
|
2015-06-18 03:55:27 +02:00
|
|
|
print_key(DH1.get_secret());
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("DH2 shared_secret: ");
|
2015-06-18 03:55:27 +02:00
|
|
|
print_key(DH2.get_secret());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2015-06-18 03:55:27 +02:00
|
|
|
TORRENT_TEST(rc4)
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
sha1_hash test1_key = hasher("test1_key",8).final();
|
|
|
|
sha1_hash test2_key = hasher("test2_key",8).final();
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "testing RC4 handler\n");
|
2011-06-18 18:58:36 +02:00
|
|
|
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);
|
2011-09-05 07:50:41 +02:00
|
|
|
test_enc_handler(&rc41, &rc42);
|
2015-06-18 03:55:27 +02:00
|
|
|
}
|
2015-05-18 01:32:13 +02:00
|
|
|
|
2016-05-28 23:12:26 +02:00
|
|
|
TORRENT_TEST(tommath)
|
|
|
|
{
|
|
|
|
const unsigned char key[96] = {
|
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
|
|
|
|
0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
|
|
|
|
0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6,
|
|
|
|
0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
|
|
|
|
0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D,
|
|
|
|
0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
|
|
|
|
0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9,
|
|
|
|
0xA6, 0x3A, 0x36, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x05, 0x63
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_int bigint;
|
|
|
|
mp_init(&bigint);
|
|
|
|
TEST_CHECK(mp_read_unsigned_bin(&bigint, key, sizeof(key)) == 0);
|
|
|
|
|
|
|
|
TEST_EQUAL(mp_unsigned_bin_size(&bigint), sizeof(key));
|
|
|
|
|
|
|
|
mp_clear(&bigint);
|
|
|
|
}
|
|
|
|
|
2013-09-01 08:52:50 +02:00
|
|
|
#else
|
2015-06-18 03:55:27 +02:00
|
|
|
TORRENT_TEST(disabled)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "PE test not run because it's disabled\n");
|
2013-09-01 08:52:50 +02:00
|
|
|
}
|
2015-06-18 03:55:27 +02:00
|
|
|
#endif
|
2013-09-01 08:52:50 +02:00
|
|
|
|