optimized encryption
[libtorrent.git] / src / pe_crypto.cpp
blobfe6ab6c81fd0d1ac2a89b49e00767fea2d6fb917
1 /*
3 Copyright (c) 2007, Un Shyam & Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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 #include <algorithm>
37 #include <openssl/dh.h>
38 #include <openssl/engine.h>
40 #include "libtorrent/pe_crypto.hpp"
41 #include "libtorrent/hasher.hpp"
42 #include "libtorrent/assert.hpp"
44 namespace libtorrent
46 namespace
48 const unsigned char dh_prime[96] = {
49 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
50 0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
51 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6,
52 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
53 0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D,
54 0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
55 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9,
56 0xA6, 0x3A, 0x36, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x05, 0x63
59 const unsigned char dh_generator[1] = { 2 };
62 // Set the prime P and the generator, generate local public key
63 dh_key_exchange::dh_key_exchange()
65 m_dh = DH_new();
66 if (m_dh == 0) return;
68 m_dh->p = BN_bin2bn(dh_prime, sizeof(dh_prime), 0);
69 m_dh->g = BN_bin2bn(dh_generator, sizeof(dh_generator), 0);
70 if (m_dh->p == 0 || m_dh->g == 0)
72 DH_free(m_dh);
73 m_dh = 0;
74 return;
77 m_dh->length = 160l;
79 TORRENT_ASSERT(sizeof(dh_prime) == DH_size(m_dh));
81 if (DH_generate_key(m_dh) == 0 || m_dh->pub_key == 0)
83 DH_free(m_dh);
84 m_dh = 0;
85 return;
88 // DH can generate key sizes that are smaller than the size of
89 // P with exponentially decreasing probability, in which case
90 // the msb's of m_dh_local_key need to be zeroed
91 // appropriately.
92 int key_size = get_local_key_size();
93 int len_dh = sizeof(dh_prime); // must equal DH_size(m_DH)
94 if (key_size != len_dh)
96 TORRENT_ASSERT(key_size > 0 && key_size < len_dh);
98 int pad_zero_size = len_dh - key_size;
99 std::fill(m_dh_local_key, m_dh_local_key + pad_zero_size, 0);
100 if (BN_bn2bin(m_dh->pub_key, (unsigned char*)m_dh_local_key + pad_zero_size) == 0)
102 DH_free(m_dh);
103 m_dh = 0;
104 return;
107 else
109 if (BN_bn2bin(m_dh->pub_key, (unsigned char*)m_dh_local_key) == 0)
111 DH_free(m_dh);
112 m_dh = 0;
113 return;
118 dh_key_exchange::~dh_key_exchange()
120 if (m_dh) DH_free(m_dh);
123 char const* dh_key_exchange::get_local_key() const
125 return m_dh_local_key;
129 // compute shared secret given remote public key
130 int dh_key_exchange::compute_secret(char const* remote_pubkey)
132 TORRENT_ASSERT(remote_pubkey);
133 BIGNUM* bn_remote_pubkey = BN_bin2bn ((unsigned char*)remote_pubkey, 96, NULL);
134 if (bn_remote_pubkey == 0) return -1;
135 char dh_secret[96];
137 int secret_size = DH_compute_key((unsigned char*)dh_secret
138 , bn_remote_pubkey, m_dh);
139 if (secret_size < 0 || secret_size > 96) return -1;
141 if (secret_size != 96)
143 TORRENT_ASSERT(secret_size < 96 && secret_size > 0);
144 std::fill(m_dh_secret, m_dh_secret + 96 - secret_size, 0);
146 std::copy(dh_secret, dh_secret + secret_size, m_dh_secret + 96 - secret_size);
147 BN_free(bn_remote_pubkey);
149 // calculate the xor mask for the obfuscated hash
150 hasher h;
151 h.update("req3", 4);
152 h.update(m_dh_secret, 96);
153 m_xor_mask = h.final();
155 return 0;
158 } // namespace libtorrent
160 #endif // #ifndef TORRENT_DISABLE_ENCRYPTION