2011-02-26 08:56:20 +01:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2007-2016, Un Shyam, Arvid Norberg, Steven Siloti
|
2011-02-26 08:56:20 +01:00
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
|
2011-02-26 08:56:20 +01:00
|
|
|
|
2016-06-20 17:32:06 +02:00
|
|
|
#include <cstdint>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <random>
|
|
|
|
|
2015-12-14 01:44:42 +01:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
|
|
|
|
2016-05-30 20:33:24 +02:00
|
|
|
#include <boost/multiprecision/integer.hpp>
|
|
|
|
#include <boost/multiprecision/cpp_int.hpp>
|
2011-02-26 08:56:20 +01:00
|
|
|
|
2016-05-30 20:33:24 +02:00
|
|
|
// for backwards compatibility with boost < 1.60 which was before export_bits
|
|
|
|
// and import_bits were introduced
|
|
|
|
#if BOOST_VERSION < 106000
|
|
|
|
#include "libtorrent/aux_/cppint_import_export.hpp"
|
|
|
|
#endif
|
2011-02-26 08:56:20 +01:00
|
|
|
|
2015-12-14 01:44:42 +01:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2015-09-06 22:47:10 +02:00
|
|
|
|
2014-12-29 17:50:22 +01:00
|
|
|
#include "libtorrent/random.hpp"
|
2016-06-20 05:05:59 +02:00
|
|
|
#include "libtorrent/alloca.hpp"
|
2011-02-26 08:56:20 +01:00
|
|
|
#include "libtorrent/pe_crypto.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/assert.hpp"
|
2016-07-22 18:31:42 +02:00
|
|
|
#include "libtorrent/span.hpp"
|
2011-02-26 08:56:20 +01:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2016-05-30 20:33:24 +02:00
|
|
|
namespace mp = boost::multiprecision;
|
2011-02-26 08:56:20 +01:00
|
|
|
|
2016-05-30 20:33:24 +02:00
|
|
|
namespace {
|
|
|
|
// TODO: it would be nice to get the literal working
|
|
|
|
key_t const dh_prime
|
|
|
|
("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A36210000000000090563");
|
|
|
|
}
|
2011-02-26 08:56:20 +01:00
|
|
|
|
|
|
|
// Set the prime P and the generator, generate local public key
|
|
|
|
dh_key_exchange::dh_key_exchange()
|
|
|
|
{
|
2016-05-30 20:33:24 +02:00
|
|
|
std::array<std::uint8_t, 96> random_key;
|
|
|
|
for (auto& i : random_key) i = random();
|
2011-02-26 08:56:20 +01:00
|
|
|
|
2016-05-30 20:33:24 +02:00
|
|
|
// create local key (random)
|
|
|
|
mp::import_bits(m_dh_local_secret, random_key.begin(), random_key.end());
|
2011-02-26 08:56:20 +01:00
|
|
|
|
|
|
|
// key = (2 ^ secret) % prime
|
2016-05-30 20:33:24 +02:00
|
|
|
m_dh_local_key = mp::powm(key_t(2), m_dh_local_secret, dh_prime);
|
2014-12-29 17:50:22 +01:00
|
|
|
}
|
2011-02-26 08:56:20 +01:00
|
|
|
|
|
|
|
// compute shared secret given remote public key
|
2016-06-18 20:01:38 +02:00
|
|
|
void dh_key_exchange::compute_secret(std::uint8_t const* remote_pubkey)
|
2011-02-26 08:56:20 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(remote_pubkey);
|
2016-05-30 20:33:24 +02:00
|
|
|
key_t key;
|
|
|
|
mp::import_bits(key, remote_pubkey, remote_pubkey + 96);
|
|
|
|
compute_secret(key);
|
|
|
|
}
|
2011-02-26 08:56:20 +01:00
|
|
|
|
2016-05-30 20:33:24 +02:00
|
|
|
void dh_key_exchange::compute_secret(key_t const& remote_pubkey)
|
|
|
|
{
|
|
|
|
// shared_secret = (remote_pubkey ^ local_secret) % prime
|
|
|
|
m_dh_shared_secret = mp::powm(remote_pubkey, m_dh_local_secret, dh_prime);
|
2016-05-27 18:39:03 +02:00
|
|
|
|
2016-07-22 16:29:39 +02:00
|
|
|
std::array<char, 96> buffer;
|
|
|
|
mp::export_bits(m_dh_shared_secret, reinterpret_cast<std::uint8_t*>(buffer.data()), 8);
|
2011-02-26 08:56:20 +01:00
|
|
|
|
2016-07-24 00:57:04 +02:00
|
|
|
static char const req3[4] = {'r', 'e', 'q', '3'};
|
2011-02-26 08:56:20 +01:00
|
|
|
// calculate the xor mask for the obfuscated hash
|
2016-07-24 00:57:04 +02:00
|
|
|
m_xor_mask = hasher(req3).update(buffer).final();
|
2011-02-26 08:56:20 +01:00
|
|
|
}
|
|
|
|
|
2016-07-24 00:54:39 +02:00
|
|
|
std::tuple<int, span<aux::const_buffer>>
|
2016-06-20 05:05:59 +02:00
|
|
|
encryption_handler::encrypt(
|
2016-07-24 00:54:39 +02:00
|
|
|
span<aux::mutable_buffer> iovec)
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!m_send_barriers.empty());
|
|
|
|
TORRENT_ASSERT(m_send_barriers.front().enc_handler);
|
|
|
|
|
|
|
|
int to_process = m_send_barriers.front().next;
|
|
|
|
|
2016-07-24 00:54:39 +02:00
|
|
|
aux::mutable_buffer* bufs;
|
2016-07-22 16:29:39 +02:00
|
|
|
size_t num_bufs;
|
2016-06-20 05:05:59 +02:00
|
|
|
bool need_destruct = false;
|
2014-11-23 07:14:47 +01:00
|
|
|
if (to_process != INT_MAX)
|
|
|
|
{
|
2016-07-24 00:54:39 +02:00
|
|
|
bufs = TORRENT_ALLOCA(aux::mutable_buffer, iovec.size());
|
2016-06-20 05:05:59 +02:00
|
|
|
need_destruct = true;
|
|
|
|
num_bufs = 0;
|
2016-07-04 08:48:13 +02:00
|
|
|
for (int i = 0; to_process > 0 && i < iovec.size(); ++i)
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
2016-06-20 05:05:59 +02:00
|
|
|
++num_bufs;
|
2016-07-24 00:54:39 +02:00
|
|
|
int const size = int(iovec[i].size());
|
2016-06-20 05:05:59 +02:00
|
|
|
if (to_process < size)
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
2016-07-24 00:54:39 +02:00
|
|
|
new (&bufs[i]) aux::mutable_buffer(
|
|
|
|
iovec[i].data(), to_process);
|
2016-06-20 05:05:59 +02:00
|
|
|
to_process = 0;
|
2014-11-23 07:14:47 +01:00
|
|
|
}
|
2016-06-20 05:05:59 +02:00
|
|
|
else
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
2016-07-24 00:54:39 +02:00
|
|
|
new (&bufs[i]) aux::mutable_buffer(iovec[i]);
|
2016-06-20 05:05:59 +02:00
|
|
|
to_process -= size;
|
2014-11-23 07:14:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 05:05:59 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bufs = iovec.data();
|
|
|
|
num_bufs = iovec.size();
|
|
|
|
}
|
2014-11-23 07:14:47 +01:00
|
|
|
|
|
|
|
int next_barrier = 0;
|
2016-07-24 00:54:39 +02:00
|
|
|
span<aux::const_buffer> out_iovec;
|
2016-07-04 08:48:13 +02:00
|
|
|
if (num_bufs != 0)
|
2016-04-17 22:56:07 +02:00
|
|
|
{
|
2016-06-20 05:05:59 +02:00
|
|
|
std::tie(next_barrier, out_iovec)
|
2016-07-24 00:57:04 +02:00
|
|
|
= m_send_barriers.front().enc_handler->encrypt({bufs, size_t(num_bufs)});
|
2016-04-17 22:56:07 +02:00
|
|
|
}
|
2016-07-04 08:48:13 +02:00
|
|
|
|
|
|
|
if (m_send_barriers.front().next != INT_MAX)
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
2016-07-04 08:48:13 +02:00
|
|
|
// to_process holds the difference between the size of the buffers
|
|
|
|
// and the bytes left to the next barrier
|
|
|
|
// if it's zero then pop the barrier
|
|
|
|
// otherwise update the number of bytes remaining to the next barrier
|
|
|
|
if (to_process == 0)
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
|
|
|
if (m_send_barriers.size() == 1)
|
2016-06-20 05:05:59 +02:00
|
|
|
{
|
2014-11-23 07:14:47 +01:00
|
|
|
// transitioning back to plaintext
|
|
|
|
next_barrier = INT_MAX;
|
2016-06-20 05:05:59 +02:00
|
|
|
}
|
2014-11-23 07:14:47 +01:00
|
|
|
m_send_barriers.pop_front();
|
|
|
|
}
|
2016-07-04 08:48:13 +02:00
|
|
|
else
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
2016-07-04 08:48:13 +02:00
|
|
|
m_send_barriers.front().next = to_process;
|
2014-11-23 07:14:47 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-04 08:48:13 +02:00
|
|
|
|
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
if (next_barrier != INT_MAX && next_barrier != 0)
|
|
|
|
{
|
|
|
|
int payload = 0;
|
|
|
|
for (int i = 0; i < num_bufs; ++i)
|
2016-07-24 00:54:39 +02:00
|
|
|
payload += int(bufs[i].size());
|
2016-07-04 08:48:13 +02:00
|
|
|
|
|
|
|
int overhead = 0;
|
|
|
|
for (auto buf : out_iovec)
|
2016-07-24 00:54:39 +02:00
|
|
|
overhead += int(buf.size());
|
2016-07-04 08:48:13 +02:00
|
|
|
TORRENT_ASSERT(overhead + payload == next_barrier);
|
|
|
|
}
|
|
|
|
#endif
|
2016-06-20 05:05:59 +02:00
|
|
|
if (need_destruct)
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
2016-06-20 05:05:59 +02:00
|
|
|
for (int i = 0; i < num_bufs; ++i)
|
2016-07-24 00:54:39 +02:00
|
|
|
bufs[i].~span<char>();
|
2014-11-23 07:14:47 +01:00
|
|
|
}
|
2016-06-20 05:05:59 +02:00
|
|
|
return std::make_tuple(next_barrier, out_iovec);
|
2014-11-23 07:14:47 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 05:05:59 +02:00
|
|
|
int encryption_handler::decrypt(crypto_receive_buffer& recv_buffer
|
|
|
|
, std::size_t& bytes_transferred)
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!is_recv_plaintext());
|
|
|
|
int consume = 0;
|
|
|
|
if (recv_buffer.crypto_packet_finished())
|
|
|
|
{
|
2016-07-24 00:54:39 +02:00
|
|
|
aux::mutable_buffer wr_buf = recv_buffer.mutable_buffer(bytes_transferred);
|
2014-11-23 07:14:47 +01:00
|
|
|
int packet_size = 0;
|
2016-04-25 23:22:09 +02:00
|
|
|
int produce = int(bytes_transferred);
|
2014-11-23 07:14:47 +01:00
|
|
|
m_dec_handler->decrypt(wr_buf, consume, produce, packet_size);
|
|
|
|
TORRENT_ASSERT(packet_size || produce);
|
|
|
|
TORRENT_ASSERT(packet_size >= 0);
|
|
|
|
bytes_transferred = produce;
|
|
|
|
if (packet_size)
|
|
|
|
recv_buffer.crypto_cut(consume, packet_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bytes_transferred = 0;
|
|
|
|
return consume;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool encryption_handler::switch_send_crypto(boost::shared_ptr<crypto_plugin> crypto
|
|
|
|
, int pending_encryption)
|
|
|
|
{
|
|
|
|
bool place_barrier = false;
|
|
|
|
if (!m_send_barriers.empty())
|
|
|
|
{
|
|
|
|
std::list<barrier>::iterator end = m_send_barriers.end(); --end;
|
|
|
|
for (std::list<barrier>::iterator b = m_send_barriers.begin();
|
|
|
|
b != end; ++b)
|
|
|
|
pending_encryption -= b->next;
|
|
|
|
TORRENT_ASSERT(pending_encryption >= 0);
|
|
|
|
m_send_barriers.back().next = pending_encryption;
|
|
|
|
}
|
|
|
|
else if (crypto)
|
|
|
|
place_barrier = true;
|
|
|
|
|
|
|
|
if (crypto)
|
|
|
|
m_send_barriers.push_back(barrier(crypto, INT_MAX));
|
|
|
|
|
|
|
|
return place_barrier;
|
|
|
|
}
|
|
|
|
|
|
|
|
void encryption_handler::switch_recv_crypto(boost::shared_ptr<crypto_plugin> crypto
|
|
|
|
, crypto_receive_buffer& recv_buffer)
|
|
|
|
{
|
|
|
|
m_dec_handler = crypto;
|
|
|
|
int packet_size = 0;
|
|
|
|
if (crypto)
|
|
|
|
{
|
|
|
|
int consume = 0;
|
|
|
|
int produce = 0;
|
2016-07-24 00:54:39 +02:00
|
|
|
std::vector<aux::mutable_buffer> wr_buf;
|
2014-11-23 07:14:47 +01:00
|
|
|
crypto->decrypt(wr_buf, consume, produce, packet_size);
|
|
|
|
TORRENT_ASSERT(wr_buf.empty());
|
|
|
|
TORRENT_ASSERT(consume == 0);
|
|
|
|
TORRENT_ASSERT(produce == 0);
|
|
|
|
}
|
|
|
|
recv_buffer.crypto_reset(packet_size);
|
|
|
|
}
|
|
|
|
|
2015-05-05 04:32:14 +02:00
|
|
|
rc4_handler::rc4_handler()
|
|
|
|
: m_encrypt(false)
|
|
|
|
, m_decrypt(false)
|
|
|
|
{
|
|
|
|
m_rc4_incoming.x = 0;
|
|
|
|
m_rc4_incoming.y = 0;
|
|
|
|
m_rc4_outgoing.x = 0;
|
|
|
|
m_rc4_outgoing.y = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
void rc4_handler::set_incoming_key(unsigned char const* key, int len)
|
|
|
|
{
|
|
|
|
m_decrypt = true;
|
|
|
|
rc4_init(key, len, &m_rc4_incoming);
|
|
|
|
// Discard first 1024 bytes
|
|
|
|
int consume = 0;
|
|
|
|
int produce = 0;
|
|
|
|
int packet_size = 0;
|
2016-06-20 05:05:59 +02:00
|
|
|
char buf[1024];
|
2016-07-24 00:54:39 +02:00
|
|
|
aux::mutable_buffer vec(buf, sizeof(buf));
|
2014-11-23 07:14:47 +01:00
|
|
|
decrypt(vec, consume, produce, packet_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rc4_handler::set_outgoing_key(unsigned char const* key, int len)
|
|
|
|
{
|
|
|
|
m_encrypt = true;
|
|
|
|
rc4_init(key, len, &m_rc4_outgoing);
|
|
|
|
// Discard first 1024 bytes
|
|
|
|
char buf[1024];
|
2016-07-24 00:54:39 +02:00
|
|
|
aux::mutable_buffer vec(buf, sizeof(buf));
|
2014-11-23 07:14:47 +01:00
|
|
|
encrypt(vec);
|
|
|
|
}
|
|
|
|
|
2016-07-24 00:54:39 +02:00
|
|
|
std::tuple<int, span<aux::const_buffer>>
|
|
|
|
rc4_handler::encrypt(span<aux::mutable_buffer> bufs)
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
2016-07-24 00:54:39 +02:00
|
|
|
span<aux::const_buffer> empty;
|
2016-06-20 05:05:59 +02:00
|
|
|
if (!m_encrypt) return std::make_tuple(0, empty);
|
|
|
|
if (bufs.size() == 0) return std::make_tuple(0, empty);
|
2014-11-23 07:14:47 +01:00
|
|
|
|
|
|
|
int bytes_processed = 0;
|
2016-06-20 05:05:59 +02:00
|
|
|
for (auto& buf : bufs)
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
2016-07-24 00:54:39 +02:00
|
|
|
unsigned char* const pos = reinterpret_cast<unsigned char*>(buf.data());
|
|
|
|
int const len = int(buf.size());
|
2014-11-23 07:14:47 +01:00
|
|
|
|
|
|
|
TORRENT_ASSERT(len >= 0);
|
|
|
|
TORRENT_ASSERT(pos);
|
|
|
|
|
|
|
|
bytes_processed += len;
|
2015-08-06 08:30:06 +02:00
|
|
|
rc4_encrypt(pos, len, &m_rc4_outgoing);
|
2014-11-23 07:14:47 +01:00
|
|
|
}
|
2016-06-20 05:05:59 +02:00
|
|
|
return std::make_tuple(bytes_processed, empty);
|
2014-11-23 07:14:47 +01:00
|
|
|
}
|
|
|
|
|
2016-07-24 00:54:39 +02:00
|
|
|
void rc4_handler::decrypt(span<aux::mutable_buffer> bufs
|
2014-11-23 07:14:47 +01:00
|
|
|
, int& consume
|
|
|
|
, int& produce
|
|
|
|
, int& packet_size)
|
|
|
|
{
|
2015-05-18 03:30:32 +02:00
|
|
|
// these are out-parameters that are not set
|
2015-04-26 02:51:44 +02:00
|
|
|
TORRENT_UNUSED(consume);
|
2015-05-18 03:30:32 +02:00
|
|
|
TORRENT_UNUSED(packet_size);
|
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
if (!m_decrypt) return;
|
|
|
|
|
|
|
|
int bytes_processed = 0;
|
2016-06-20 05:05:59 +02:00
|
|
|
for (auto& buf : bufs)
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
2016-07-24 00:54:39 +02:00
|
|
|
unsigned char* const pos = reinterpret_cast<unsigned char*>(buf.data());
|
|
|
|
int const len = int(buf.size());
|
2014-11-23 07:14:47 +01:00
|
|
|
|
|
|
|
TORRENT_ASSERT(len >= 0);
|
|
|
|
TORRENT_ASSERT(pos);
|
|
|
|
|
|
|
|
bytes_processed += len;
|
2015-08-06 08:30:06 +02:00
|
|
|
rc4_encrypt(pos, len, &m_rc4_incoming);
|
2014-11-23 07:14:47 +01:00
|
|
|
}
|
|
|
|
produce = bytes_processed;
|
|
|
|
}
|
|
|
|
|
2011-02-26 08:56:20 +01:00
|
|
|
// All this code is based on libTomCrypt (http://www.libtomcrypt.com/)
|
|
|
|
// this library is public domain and has been specially
|
|
|
|
// tailored for libtorrent by Arvid Norberg
|
|
|
|
|
|
|
|
void rc4_init(const unsigned char* in, unsigned long len, rc4 *state)
|
|
|
|
{
|
2016-05-27 18:39:03 +02:00
|
|
|
size_t const key_size = sizeof(state->buf);
|
|
|
|
unsigned char key[key_size], tmp, *s;
|
2011-02-26 08:56:20 +01:00
|
|
|
int keylen, x, y, j;
|
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
TORRENT_ASSERT(state != nullptr);
|
2016-05-27 18:39:03 +02:00
|
|
|
TORRENT_ASSERT(len <= key_size);
|
|
|
|
if (len > key_size) len = key_size;
|
2011-02-26 08:56:20 +01:00
|
|
|
|
|
|
|
state->x = 0;
|
|
|
|
while (len--) {
|
|
|
|
state->buf[state->x++] = *in++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* extract the key */
|
2016-05-30 20:33:24 +02:00
|
|
|
s = state->buf.data();
|
2016-05-27 18:39:03 +02:00
|
|
|
std::memcpy(key, s, key_size);
|
2011-02-26 08:56:20 +01:00
|
|
|
keylen = state->x;
|
|
|
|
|
|
|
|
/* make RC4 perm and shuffle */
|
2016-05-27 18:39:03 +02:00
|
|
|
for (x = 0; x < key_size; ++x) {
|
2011-02-26 08:56:20 +01:00
|
|
|
s[x] = x;
|
|
|
|
}
|
|
|
|
|
2016-05-27 18:39:03 +02:00
|
|
|
for (j = x = y = 0; x < key_size; x++) {
|
2011-02-26 08:56:20 +01:00
|
|
|
y = (y + state->buf[x] + key[j++]) & 255;
|
|
|
|
if (j == keylen) {
|
2016-05-27 18:39:03 +02:00
|
|
|
j = 0;
|
2011-02-26 08:56:20 +01:00
|
|
|
}
|
|
|
|
tmp = s[x]; s[x] = s[y]; s[y] = tmp;
|
|
|
|
}
|
|
|
|
state->x = 0;
|
|
|
|
state->y = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long rc4_encrypt(unsigned char *out, unsigned long outlen, rc4 *state)
|
|
|
|
{
|
|
|
|
unsigned char x, y, *s, tmp;
|
|
|
|
unsigned long n;
|
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
TORRENT_ASSERT(out != nullptr);
|
|
|
|
TORRENT_ASSERT(state != nullptr);
|
2011-02-26 08:56:20 +01:00
|
|
|
|
|
|
|
n = outlen;
|
|
|
|
x = state->x;
|
|
|
|
y = state->y;
|
2016-05-30 20:33:24 +02:00
|
|
|
s = state->buf.data();
|
2011-02-26 08:56:20 +01:00
|
|
|
while (outlen--) {
|
|
|
|
x = (x + 1) & 255;
|
|
|
|
y = (y + s[x]) & 255;
|
|
|
|
tmp = s[x]; s[x] = s[y]; s[y] = tmp;
|
|
|
|
tmp = (s[x] + s[y]) & 255;
|
|
|
|
*out++ ^= s[tmp];
|
|
|
|
}
|
|
|
|
state->x = x;
|
|
|
|
state->y = y;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-05-30 20:33:24 +02:00
|
|
|
} // namespace libtorrent
|
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
#endif // #if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
|