3
Copyright (c) 2007, Un Shyam
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
10
* Redistributions of source code must retain the above copyright
11
notice, this list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in
14
the documentation and/or other materials provided with the distribution.
15
* Neither the name of the author nor the names of its
16
contributors may be used to endorse or promote products derived
17
from this software without specific prior written permission.
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
33
#ifndef TORRENT_DISABLE_ENCRYPTION
35
#ifndef TORRENT_PE_CRYPTO_HPP_INCLUDED
36
#define TORRENT_PE_CRYPTO_HPP_INCLUDED
38
#include <openssl/dh.h>
39
#include <openssl/engine.h>
40
#include <openssl/rc4.h>
42
#include "libtorrent/peer_id.hpp" // For sha1_hash
43
#include "libtorrent/assert.hpp"
53
// Get local public key, always 96 bytes
54
char const* get_local_key (void) const;
56
// read remote_pubkey, generate and store shared secret in
58
void compute_secret (const char* remote_pubkey);
60
const char* get_secret (void) const;
63
int get_local_key_size () const
66
return BN_num_bytes (m_DH->pub_key);
70
static const unsigned char m_dh_prime[96];
71
static const unsigned char m_dh_generator[1];
73
char m_dh_local_key[96];
77
class RC4_handler // Non copyable
80
// Input longkeys must be 20 bytes
81
RC4_handler (const sha1_hash& rc4_local_longkey,
82
const sha1_hash& rc4_remote_longkey)
85
RC4_set_key (&m_local_key, 20,
86
reinterpret_cast<unsigned char const*>(rc4_local_longkey.begin()));
87
RC4_set_key (&m_remote_key, 20,
88
reinterpret_cast<unsigned char const*>(rc4_remote_longkey.begin()));
90
// Discard first 1024 bytes
98
void encrypt (char* pos, int len)
100
TORRENT_ASSERT(len >= 0);
103
RC4 (&m_local_key, len, reinterpret_cast<unsigned char const*>(pos),
104
reinterpret_cast<unsigned char*>(pos));
107
void decrypt (char* pos, int len)
109
TORRENT_ASSERT(len >= 0);
112
RC4 (&m_remote_key, len, reinterpret_cast<unsigned char const*>(pos),
113
reinterpret_cast<unsigned char*>(pos));
117
RC4_KEY m_local_key; // Key to encrypt outgoing data
118
RC4_KEY m_remote_key; // Key to decrypt incoming data
121
} // namespace libtorrent
123
#endif // TORRENT_PE_CRYPTO_HPP_INCLUDED
124
#endif // TORRENT_DISABLE_ENCRYPTION