forked from premiere/premiere-libtorrent
refactor in crypto_plugin::decrypt to use tuple
This commit is contained in:
parent
253ae93df1
commit
f4127ae12b
|
@ -498,6 +498,9 @@ namespace libtorrent
|
||||||
encrypt(span<span<char>> /*send_vec*/) = 0;
|
encrypt(span<span<char>> /*send_vec*/) = 0;
|
||||||
|
|
||||||
// decrypt the provided buffers.
|
// decrypt the provided buffers.
|
||||||
|
// returns is a tuple representing the values
|
||||||
|
// (consume, produce, packet_size)
|
||||||
|
//
|
||||||
// consume is set to the number of bytes which should be trimmed from the
|
// consume is set to the number of bytes which should be trimmed from the
|
||||||
// head of the buffers, default is 0
|
// head of the buffers, default is 0
|
||||||
//
|
//
|
||||||
|
@ -506,8 +509,7 @@ namespace libtorrent
|
||||||
//
|
//
|
||||||
// packet_size is set to the minimum number of bytes which must be read to
|
// packet_size is set to the minimum number of bytes which must be read to
|
||||||
// advance the next step of decryption. default is 0
|
// advance the next step of decryption. default is 0
|
||||||
virtual void decrypt(span<span<char>> /*receive_vec*/
|
virtual std::tuple<int, int, int> decrypt(span<span<char>> /*receive_vec*/) = 0;
|
||||||
, int& /* consume */, int& /*produce*/, int& /*packet_size*/) = 0;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -145,10 +145,7 @@ namespace libtorrent
|
||||||
std::tuple<int, span<span<char const>>>
|
std::tuple<int, span<span<char const>>>
|
||||||
encrypt(span<span<char>> buf) override;
|
encrypt(span<span<char>> buf) override;
|
||||||
|
|
||||||
void decrypt(span<span<char>> buf
|
std::tuple<int, int, int> decrypt(span<span<char>> buf) override;
|
||||||
, int& consume
|
|
||||||
, int& produce
|
|
||||||
, int& packet_size) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
rc4 m_rc4_incoming;
|
rc4 m_rc4_incoming;
|
||||||
|
|
|
@ -727,10 +727,7 @@ namespace libtorrent
|
||||||
|
|
||||||
void bt_peer_connection::rc4_decrypt(span<char> buf)
|
void bt_peer_connection::rc4_decrypt(span<char> buf)
|
||||||
{
|
{
|
||||||
int consume = 0;
|
m_rc4->decrypt(buf);
|
||||||
int produce = int(buf.size());
|
|
||||||
int packet_size = 0;
|
|
||||||
m_rc4->decrypt(buf, consume, produce, packet_size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
|
@ -219,9 +219,9 @@ namespace libtorrent
|
||||||
if (recv_buffer.crypto_packet_finished())
|
if (recv_buffer.crypto_packet_finished())
|
||||||
{
|
{
|
||||||
span<char> wr_buf = recv_buffer.mutable_buffer(bytes_transferred);
|
span<char> wr_buf = recv_buffer.mutable_buffer(bytes_transferred);
|
||||||
|
int produce = 0;
|
||||||
int packet_size = 0;
|
int packet_size = 0;
|
||||||
int produce = int(bytes_transferred);
|
std::tie(consume, produce, packet_size) = m_dec_handler->decrypt(wr_buf);
|
||||||
m_dec_handler->decrypt(wr_buf, consume, produce, packet_size);
|
|
||||||
TORRENT_ASSERT(packet_size || produce);
|
TORRENT_ASSERT(packet_size || produce);
|
||||||
TORRENT_ASSERT(packet_size >= 0);
|
TORRENT_ASSERT(packet_size >= 0);
|
||||||
bytes_transferred = produce;
|
bytes_transferred = produce;
|
||||||
|
@ -265,7 +265,7 @@ namespace libtorrent
|
||||||
int consume = 0;
|
int consume = 0;
|
||||||
int produce = 0;
|
int produce = 0;
|
||||||
std::vector<span<char>> wr_buf;
|
std::vector<span<char>> wr_buf;
|
||||||
crypto->decrypt(wr_buf, consume, produce, packet_size);
|
std::tie(consume, produce, packet_size) = crypto->decrypt(wr_buf);
|
||||||
TORRENT_ASSERT(wr_buf.empty());
|
TORRENT_ASSERT(wr_buf.empty());
|
||||||
TORRENT_ASSERT(consume == 0);
|
TORRENT_ASSERT(consume == 0);
|
||||||
TORRENT_ASSERT(produce == 0);
|
TORRENT_ASSERT(produce == 0);
|
||||||
|
@ -289,12 +289,9 @@ namespace libtorrent
|
||||||
rc4_init(reinterpret_cast<unsigned char const*>(key.data())
|
rc4_init(reinterpret_cast<unsigned char const*>(key.data())
|
||||||
, key.size(), &m_rc4_incoming);
|
, key.size(), &m_rc4_incoming);
|
||||||
// Discard first 1024 bytes
|
// Discard first 1024 bytes
|
||||||
int consume = 0;
|
|
||||||
int produce = 0;
|
|
||||||
int packet_size = 0;
|
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
span<char> vec(buf, sizeof(buf));
|
span<char> vec(buf, sizeof(buf));
|
||||||
decrypt(vec, consume, produce, packet_size);
|
decrypt(vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
void rc4_handler::set_outgoing_key(span<char const> key)
|
void rc4_handler::set_outgoing_key(span<char const> key)
|
||||||
|
@ -330,16 +327,9 @@ namespace libtorrent
|
||||||
return std::make_tuple(bytes_processed, empty);
|
return std::make_tuple(bytes_processed, empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
void rc4_handler::decrypt(span<span<char>> bufs
|
std::tuple<int, int, int> rc4_handler::decrypt(span<span<char>> bufs)
|
||||||
, int& consume
|
|
||||||
, int& produce
|
|
||||||
, int& packet_size)
|
|
||||||
{
|
{
|
||||||
// these are out-parameters that are not set
|
if (!m_decrypt) std::make_tuple(0, 0, 0);
|
||||||
TORRENT_UNUSED(consume);
|
|
||||||
TORRENT_UNUSED(packet_size);
|
|
||||||
|
|
||||||
if (!m_decrypt) return;
|
|
||||||
|
|
||||||
int bytes_processed = 0;
|
int bytes_processed = 0;
|
||||||
for (auto& buf : bufs)
|
for (auto& buf : bufs)
|
||||||
|
@ -353,7 +343,7 @@ namespace libtorrent
|
||||||
bytes_processed += len;
|
bytes_processed += len;
|
||||||
rc4_encrypt(pos, len, &m_rc4_incoming);
|
rc4_encrypt(pos, len, &m_rc4_incoming);
|
||||||
}
|
}
|
||||||
produce = bytes_processed;
|
return std::make_tuple(0, bytes_processed, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// All this code is based on libTomCrypt (http://www.libtomcrypt.com/)
|
// All this code is based on libTomCrypt (http://www.libtomcrypt.com/)
|
||||||
|
|
|
@ -73,10 +73,10 @@ void test_enc_handler(libtorrent::crypto_plugin& a, libtorrent::crypto_plugin& b
|
||||||
|
|
||||||
{
|
{
|
||||||
int consume = 0;
|
int consume = 0;
|
||||||
int produce = buf_len;
|
int produce = 0;
|
||||||
int packet_size = 0;
|
int packet_size = 0;
|
||||||
lt::span<char> iovec(&buf[0], buf_len);
|
lt::span<char> iovec(&buf[0], buf_len);
|
||||||
b.decrypt(iovec, consume, produce, packet_size);
|
std::tie(consume, produce, packet_size) = b.decrypt(iovec);
|
||||||
TEST_CHECK(buf == cmp_buf);
|
TEST_CHECK(buf == cmp_buf);
|
||||||
TEST_EQUAL(consume, 0);
|
TEST_EQUAL(consume, 0);
|
||||||
TEST_EQUAL(produce, buf_len);
|
TEST_EQUAL(produce, buf_len);
|
||||||
|
@ -93,10 +93,10 @@ void test_enc_handler(libtorrent::crypto_plugin& a, libtorrent::crypto_plugin& b
|
||||||
TEST_EQUAL(next_barrier, buf_len);
|
TEST_EQUAL(next_barrier, buf_len);
|
||||||
|
|
||||||
int consume = 0;
|
int consume = 0;
|
||||||
int produce = buf_len;
|
int produce = 0;
|
||||||
int packet_size = 0;
|
int packet_size = 0;
|
||||||
lt::span<char> iovec2(&buf[0], buf_len);
|
lt::span<char> iovec2(&buf[0], buf_len);
|
||||||
a.decrypt(iovec2, consume, produce, packet_size);
|
std::tie(consume, produce, packet_size) = a.decrypt(iovec2);
|
||||||
TEST_CHECK(buf == cmp_buf);
|
TEST_CHECK(buf == cmp_buf);
|
||||||
TEST_EQUAL(consume, 0);
|
TEST_EQUAL(consume, 0);
|
||||||
TEST_EQUAL(produce, buf_len);
|
TEST_EQUAL(produce, buf_len);
|
||||||
|
|
Loading…
Reference in New Issue