diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index 8f9ccd077..26df5da11 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -482,8 +482,8 @@ namespace libtorrent // hidden virtual ~crypto_plugin() {} - virtual void set_incoming_key(unsigned char const* key, int len) = 0; - virtual void set_outgoing_key(unsigned char const* key, int len) = 0; + virtual void set_incoming_key(span key) = 0; + virtual void set_outgoing_key(span key) = 0; // encrypted the provided buffers and returns the number of bytes which // are now ready to be sent to the lower layer. This must be at least diff --git a/include/libtorrent/pe_crypto.hpp b/include/libtorrent/pe_crypto.hpp index d01d6ecda..b20d3306c 100644 --- a/include/libtorrent/pe_crypto.hpp +++ b/include/libtorrent/pe_crypto.hpp @@ -58,7 +58,7 @@ namespace libtorrent using key_t = mp::number>; - std::array export_key(key_t const& k); + TORRENT_EXTRA_EXPORT std::array export_key(key_t const& k); // RC4 state from libtomcrypt struct rc4 { @@ -66,9 +66,6 @@ namespace libtorrent std::array buf; }; - void TORRENT_EXTRA_EXPORT rc4_init(const unsigned char* in, unsigned long len, rc4 *state); - unsigned long TORRENT_EXTRA_EXPORT rc4_encrypt(unsigned char *out, unsigned long outlen, rc4 *state); - // TODO: 3 dh_key_exchange should probably move into its own file class TORRENT_EXTRA_EXPORT dh_key_exchange { @@ -142,9 +139,8 @@ namespace libtorrent rc4_handler(); // Input keys must be 20 bytes - // TODO: 4 use uint768_t here instead of pointer + length - void set_incoming_key(unsigned char const* key, int len) override; - void set_outgoing_key(unsigned char const* key, int len) override; + void set_incoming_key(span key) override; + void set_outgoing_key(span key) override; std::tuple>> encrypt(span> buf) override; diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index a87b0a989..d4dfe1dab 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -122,8 +122,8 @@ namespace libtorrent boost::shared_ptr ret = boost::make_shared(); - ret->set_incoming_key(&remote_key[0], 20); - ret->set_outgoing_key(&local_key[0], 20); + ret->set_incoming_key(remote_key); + ret->set_outgoing_key(local_key); return ret; } @@ -542,7 +542,7 @@ namespace libtorrent int const buf_size = dh_key_len + pad_size; std::array const local_key = export_key(m_dh_key_exchange->get_local_key()); - memcpy(ptr, local_key.data(), dh_key_len); + std::memcpy(ptr, local_key.data(), dh_key_len); ptr += dh_key_len; std::generate(ptr, ptr + pad_size, random_byte); diff --git a/src/pe_crypto.cpp b/src/pe_crypto.cpp index 8acfe08ae..8bb16e87b 100644 --- a/src/pe_crypto.cpp +++ b/src/pe_crypto.cpp @@ -77,12 +77,15 @@ namespace libtorrent if (end < begin + 96) { int const len = end - begin; - memmove(begin + 96 - len, begin, len); - memset(begin, 0, 96 - len); + std::memmove(begin + 96 - len, begin, len); + std::memset(begin, 0, 96 - len); } return ret; } + void rc4_init(const unsigned char* in, unsigned long len, rc4 *state); + unsigned long rc4_encrypt(unsigned char *out, unsigned long outlen, rc4 *state); + // Set the prime P and the generator, generate local public key dh_key_exchange::dh_key_exchange() { @@ -280,10 +283,11 @@ namespace libtorrent m_rc4_outgoing.y = 0; } - void rc4_handler::set_incoming_key(unsigned char const* key, int len) + void rc4_handler::set_incoming_key(span key) { m_decrypt = true; - rc4_init(key, len, &m_rc4_incoming); + rc4_init(reinterpret_cast(key.data()) + , key.size(), &m_rc4_incoming); // Discard first 1024 bytes int consume = 0; int produce = 0; @@ -293,10 +297,11 @@ namespace libtorrent decrypt(vec, consume, produce, packet_size); } - void rc4_handler::set_outgoing_key(unsigned char const* key, int len) + void rc4_handler::set_outgoing_key(span key) { m_encrypt = true; - rc4_init(key, len, &m_rc4_outgoing); + rc4_init(reinterpret_cast(key.data()) + , key.size(), &m_rc4_outgoing); // Discard first 1024 bytes char buf[1024]; span vec(buf, sizeof(buf)); diff --git a/src/ut_pex.cpp b/src/ut_pex.cpp index fde34b0ef..83aed37a5 100644 --- a/src/ut_pex.cpp +++ b/src/ut_pex.cpp @@ -31,26 +31,18 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/config.hpp" -#include "libtorrent/peer_connection.hpp" #include "libtorrent/bt_peer_connection.hpp" #include "libtorrent/peer_connection_handle.hpp" #include "libtorrent/bencode.hpp" #include "libtorrent/torrent.hpp" -#include "libtorrent/torrent_handle.hpp" #include "libtorrent/extensions.hpp" #include "libtorrent/broadcast_socket.hpp" #include "libtorrent/socket_io.hpp" #include "libtorrent/peer_info.hpp" -#include "libtorrent/random.hpp" #include "libtorrent/socket_type.hpp" // for is_utp #include "libtorrent/performance_counters.hpp" // for counters - #include "libtorrent/extensions/ut_pex.hpp" -#ifndef TORRENT_DISABLE_LOGGING -#include "libtorrent/lazy_entry.hpp" -#endif - #ifndef TORRENT_DISABLE_EXTENSIONS namespace libtorrent { namespace diff --git a/test/test_pe_crypto.cpp b/test/test_pe_crypto.cpp index 509bcc85e..10abebe70 100644 --- a/test/test_pe_crypto.cpp +++ b/test/test_pe_crypto.cpp @@ -145,11 +145,11 @@ TORRENT_TEST(rc4) std::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); + rc41.set_incoming_key(test2_key); + rc41.set_outgoing_key(test1_key); rc4_handler rc42; - rc42.set_incoming_key(&test1_key[0], 20); - rc42.set_outgoing_key(&test2_key[0], 20); + rc42.set_incoming_key(test1_key); + rc42.set_outgoing_key(test2_key); test_enc_handler(rc41, rc42); }