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"
52 bool good() const { return m_dh
; }
54 // Get local public key, always 96 bytes
55 char const* get_local_key() const;
57 // read remote_pubkey, generate and store shared secret in
59 int compute_secret(const char* remote_pubkey
);
61 char const* get_secret() const { return m_dh_secret
; }
63 sha1_hash
const& get_hash_xor_mask() const { return m_xor_mask
; }
66 int get_local_key_size() const
69 return BN_num_bytes(m_dh
->pub_key
);
74 char m_dh_local_key
[96];
79 class RC4_handler
// Non copyable
82 // Input longkeys must be 20 bytes
83 RC4_handler(const sha1_hash
& rc4_local_longkey
,
84 const sha1_hash
& rc4_remote_longkey
)
87 RC4_set_key(&m_local_key
, 20,
88 reinterpret_cast<unsigned char const*>(rc4_local_longkey
.begin()));
89 RC4_set_key(&m_remote_key
, 20,
90 reinterpret_cast<unsigned char const*>(rc4_remote_longkey
.begin()));
92 // Discard first 1024 bytes
100 void encrypt(char* pos
, int len
)
102 TORRENT_ASSERT(len
>= 0);
105 RC4 (&m_local_key
, len
, reinterpret_cast<unsigned char const*>(pos
),
106 reinterpret_cast<unsigned char*>(pos
));
109 void decrypt(char* pos
, int len
)
111 TORRENT_ASSERT(len
>= 0);
114 RC4 (&m_remote_key
, len
, reinterpret_cast<unsigned char const*>(pos
),
115 reinterpret_cast<unsigned char*>(pos
));
119 RC4_KEY m_local_key
; // Key to encrypt outgoing data
120 RC4_KEY m_remote_key
; // Key to decrypt incoming data
123 } // namespace libtorrent
125 #endif // TORRENT_PE_CRYPTO_HPP_INCLUDED
126 #endif // TORRENT_DISABLE_ENCRYPTION