forked from premiere/premiere-libtorrent
refactor in crypto_plugin to use span
This commit is contained in:
parent
9b0bc2ed5f
commit
7a2b21407c
|
@ -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<char const> key) = 0;
|
||||
virtual void set_outgoing_key(span<char const> 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
|
||||
|
|
|
@ -58,7 +58,7 @@ namespace libtorrent
|
|||
|
||||
using key_t = mp::number<mp::cpp_int_backend<768, 768, mp::unsigned_magnitude, mp::unchecked, void>>;
|
||||
|
||||
std::array<char, 96> export_key(key_t const& k);
|
||||
TORRENT_EXTRA_EXPORT std::array<char, 96> export_key(key_t const& k);
|
||||
|
||||
// RC4 state from libtomcrypt
|
||||
struct rc4 {
|
||||
|
@ -66,9 +66,6 @@ namespace libtorrent
|
|||
std::array<std::uint8_t, 256> 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<char const> key) override;
|
||||
void set_outgoing_key(span<char const> key) override;
|
||||
|
||||
std::tuple<int, span<span<char const>>>
|
||||
encrypt(span<span<char>> buf) override;
|
||||
|
|
|
@ -122,8 +122,8 @@ namespace libtorrent
|
|||
|
||||
boost::shared_ptr<rc4_handler> ret = boost::make_shared<rc4_handler>();
|
||||
|
||||
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<char, dh_key_len> 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);
|
||||
|
|
|
@ -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<char const> key)
|
||||
{
|
||||
m_decrypt = true;
|
||||
rc4_init(key, len, &m_rc4_incoming);
|
||||
rc4_init(reinterpret_cast<unsigned char const*>(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<char const> key)
|
||||
{
|
||||
m_encrypt = true;
|
||||
rc4_init(key, len, &m_rc4_outgoing);
|
||||
rc4_init(reinterpret_cast<unsigned char const*>(key.data())
|
||||
, key.size(), &m_rc4_outgoing);
|
||||
// Discard first 1024 bytes
|
||||
char buf[1024];
|
||||
span<char> vec(buf, sizeof(buf));
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue