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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
|
|
|
|
#ifndef TORRENT_PE_CRYPTO_HPP_INCLUDED
|
|
|
|
#define TORRENT_PE_CRYPTO_HPP_INCLUDED
|
|
|
|
|
2009-11-08 04:09:19 +01:00
|
|
|
#ifdef TORRENT_USE_GCRYPT
|
|
|
|
#include <gcrypt.h>
|
2009-11-09 08:26:40 +01:00
|
|
|
#elif defined TORRENT_USE_OPENSSL
|
2007-06-06 02:41:20 +02:00
|
|
|
#include <openssl/rc4.h>
|
2009-11-09 08:26:40 +01:00
|
|
|
#else
|
|
|
|
// RC4 state from libtomcrypt
|
|
|
|
struct rc4 {
|
|
|
|
int x, y;
|
|
|
|
unsigned char buf[256];
|
|
|
|
};
|
|
|
|
|
|
|
|
void rc4_init(const unsigned char* in, unsigned long len, rc4 *state);
|
|
|
|
unsigned long rc4_encrypt(unsigned char *out, unsigned long outlen, rc4 *state);
|
2009-11-08 04:09:19 +01:00
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2007-09-01 06:08:39 +02:00
|
|
|
#include "libtorrent/peer_id.hpp" // For sha1_hash
|
|
|
|
#include "libtorrent/assert.hpp"
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2008-06-28 12:10:05 +02:00
|
|
|
class dh_key_exchange
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
|
|
|
public:
|
2008-06-28 12:10:05 +02:00
|
|
|
dh_key_exchange();
|
2009-11-08 04:09:19 +01:00
|
|
|
bool good() const { return true; }
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// Get local public key, always 96 bytes
|
2008-06-28 12:10:05 +02:00
|
|
|
char const* get_local_key() const;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// read remote_pubkey, generate and store shared secret in
|
2009-11-08 04:09:19 +01:00
|
|
|
// m_dh_shared_secret.
|
2008-06-28 12:10:05 +02:00
|
|
|
int compute_secret(const char* remote_pubkey);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2009-11-08 04:09:19 +01:00
|
|
|
char const* get_secret() const { return m_dh_shared_secret; }
|
2008-08-21 01:05:12 +02:00
|
|
|
|
|
|
|
sha1_hash const& get_hash_xor_mask() const { return m_xor_mask; }
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2009-11-08 04:09:19 +01:00
|
|
|
int get_local_key_size() const
|
|
|
|
{ return sizeof(m_dh_local_key); }
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
char m_dh_local_key[96];
|
2009-11-08 04:09:19 +01:00
|
|
|
char m_dh_local_secret[96];
|
|
|
|
char m_dh_shared_secret[96];
|
2008-08-21 01:05:12 +02:00
|
|
|
sha1_hash m_xor_mask;
|
2007-06-06 02:41:20 +02:00
|
|
|
};
|
2009-11-09 08:26:40 +01:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
class RC4_handler // Non copyable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Input longkeys must be 20 bytes
|
2009-11-08 04:09:19 +01:00
|
|
|
RC4_handler(sha1_hash const& rc4_local_longkey,
|
|
|
|
sha1_hash const& rc4_remote_longkey)
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
2009-11-08 04:09:19 +01:00
|
|
|
#ifdef TORRENT_USE_GCRYPT
|
|
|
|
gcry_cipher_open(&m_rc4_incoming, GCRY_CIPHER_ARCFOUR, GCRY_CIPHER_MODE_STREAM, 0);
|
|
|
|
gcry_cipher_open(&m_rc4_outgoing, GCRY_CIPHER_ARCFOUR, GCRY_CIPHER_MODE_STREAM, 0);
|
|
|
|
gcry_cipher_setkey(m_rc4_incoming, &rc4_remote_longkey[0], 20);
|
|
|
|
gcry_cipher_setkey(m_rc4_outgoing, &rc4_local_longkey[0], 20);
|
|
|
|
#elif defined TORRENT_USE_OPENSSL
|
|
|
|
RC4_set_key(&m_local_key, 20, &rc4_local_longkey[0]);
|
|
|
|
RC4_set_key(&m_remote_key, 20, &rc4_remote_longkey[0]);
|
2009-11-09 08:26:40 +01:00
|
|
|
#else
|
|
|
|
rc4_init(&rc4_remote_longkey[0], 20, &m_rc4_incoming);
|
|
|
|
rc4_init(&rc4_local_longkey[0], 20, &m_rc4_outgoing);
|
2009-11-08 04:09:19 +01:00
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// Discard first 1024 bytes
|
|
|
|
char buf[1024];
|
2008-06-28 12:10:05 +02:00
|
|
|
encrypt(buf, 1024);
|
|
|
|
decrypt(buf, 1024);
|
2007-06-06 02:41:20 +02:00
|
|
|
};
|
|
|
|
|
2009-11-08 04:09:19 +01:00
|
|
|
~RC4_handler()
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_USE_GCRYPT
|
|
|
|
gcry_cipher_close(m_rc4_incoming);
|
|
|
|
gcry_cipher_close(m_rc4_outgoing);
|
|
|
|
#endif
|
|
|
|
};
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2008-06-28 12:10:05 +02:00
|
|
|
void encrypt(char* pos, int len)
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
2007-12-27 04:43:02 +01:00
|
|
|
TORRENT_ASSERT(len >= 0);
|
|
|
|
TORRENT_ASSERT(pos);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2009-11-08 04:09:19 +01:00
|
|
|
#ifdef TORRENT_USE_GCRYPT
|
|
|
|
gcry_cipher_encrypt(m_rc4_outgoing, pos, len, 0, 0);
|
|
|
|
#elif defined TORRENT_USE_OPENSSL
|
|
|
|
RC4(&m_local_key, len, (const unsigned char*)pos, (unsigned char*)pos);
|
2009-11-09 08:26:40 +01:00
|
|
|
#else
|
|
|
|
rc4_encrypt((unsigned char*)pos, len, &m_rc4_outgoing);
|
2009-11-08 04:09:19 +01:00
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
|
2008-06-28 12:10:05 +02:00
|
|
|
void decrypt(char* pos, int len)
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
2007-12-27 04:43:02 +01:00
|
|
|
TORRENT_ASSERT(len >= 0);
|
|
|
|
TORRENT_ASSERT(pos);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2009-11-08 04:09:19 +01:00
|
|
|
#ifdef TORRENT_USE_GCRYPT
|
|
|
|
gcry_cipher_decrypt(m_rc4_incoming, pos, len, 0, 0);
|
|
|
|
#elif defined TORRENT_USE_OPENSSL
|
|
|
|
RC4(&m_remote_key, len, (const unsigned char*)pos, (unsigned char*)pos);
|
2009-11-09 08:26:40 +01:00
|
|
|
#else
|
|
|
|
rc4_encrypt((unsigned char*)pos, len, &m_rc4_incoming);
|
2009-11-08 04:09:19 +01:00
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2009-11-08 04:09:19 +01:00
|
|
|
#ifdef TORRENT_USE_GCRYPT
|
|
|
|
gcry_cipher_hd_t m_rc4_incoming;
|
|
|
|
gcry_cipher_hd_t m_rc4_outgoing;
|
|
|
|
#elif defined TORRENT_USE_OPENSSL
|
2007-06-06 02:41:20 +02:00
|
|
|
RC4_KEY m_local_key; // Key to encrypt outgoing data
|
|
|
|
RC4_KEY m_remote_key; // Key to decrypt incoming data
|
2009-11-09 08:26:40 +01:00
|
|
|
#else
|
|
|
|
rc4 m_rc4_incoming;
|
|
|
|
rc4 m_rc4_outgoing;
|
2009-11-08 04:09:19 +01:00
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
};
|
2008-12-27 17:44:20 +01:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
} // namespace libtorrent
|
|
|
|
|
|
|
|
#endif // TORRENT_PE_CRYPTO_HPP_INCLUDED
|
|
|
|
#endif // TORRENT_DISABLE_ENCRYPTION
|
2008-06-28 12:10:05 +02:00
|
|
|
|