3 Copyright (c) 2007, Un Shyam & Arvid Norberg
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
37 #include <openssl/dh.h>
38 #include <openssl/engine.h>
40 #include "libtorrent/pe_crypto.hpp"
41 #include "libtorrent/assert.hpp"
47 const unsigned char dh_prime
[96] = {
48 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
49 0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
50 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6,
51 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
52 0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D,
53 0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
54 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9,
55 0xA6, 0x3A, 0x36, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x05, 0x63
58 const unsigned char dh_generator
[1] = { 2 };
61 // Set the prime P and the generator, generate local public key
62 dh_key_exchange::dh_key_exchange()
65 if (m_dh
== 0) return;
67 m_dh
->p
= BN_bin2bn(dh_prime
, sizeof(dh_prime
), 0);
68 m_dh
->g
= BN_bin2bn(dh_generator
, sizeof(dh_generator
), 0);
69 if (m_dh
->p
== 0 || m_dh
->g
== 0)
78 TORRENT_ASSERT(sizeof(dh_prime
) == DH_size(m_dh
));
80 if (DH_generate_key(m_dh
) == 0 || m_dh
->pub_key
== 0)
87 // DH can generate key sizes that are smaller than the size of
88 // P with exponentially decreasing probability, in which case
89 // the msb's of m_dh_local_key need to be zeroed
91 int key_size
= get_local_key_size();
92 int len_dh
= sizeof(dh_prime
); // must equal DH_size(m_DH)
93 if (key_size
!= len_dh
)
95 TORRENT_ASSERT(key_size
> 0 && key_size
< len_dh
);
97 int pad_zero_size
= len_dh
- key_size
;
98 std::fill(m_dh_local_key
, m_dh_local_key
+ pad_zero_size
, 0);
99 if (BN_bn2bin(m_dh
->pub_key
, (unsigned char*)m_dh_local_key
+ pad_zero_size
) == 0)
108 if (BN_bn2bin(m_dh
->pub_key
, (unsigned char*)m_dh_local_key
) == 0)
117 dh_key_exchange::~dh_key_exchange()
119 if (m_dh
) DH_free(m_dh
);
122 char const* dh_key_exchange::get_local_key() const
124 return m_dh_local_key
;
128 // compute shared secret given remote public key
129 int dh_key_exchange::compute_secret(char const* remote_pubkey
)
131 TORRENT_ASSERT(remote_pubkey
);
132 BIGNUM
* bn_remote_pubkey
= BN_bin2bn ((unsigned char*)remote_pubkey
, 96, NULL
);
133 if (bn_remote_pubkey
== 0) return -1;
136 int secret_size
= DH_compute_key((unsigned char*)dh_secret
137 , bn_remote_pubkey
, m_dh
);
138 if (secret_size
< 0 || secret_size
> 96) return -1;
140 if (secret_size
!= 96)
142 TORRENT_ASSERT(secret_size
< 96 && secret_size
> 0);
143 std::fill(m_dh_secret
, m_dh_secret
+ 96 - secret_size
, 0);
145 std::copy(dh_secret
, dh_secret
+ secret_size
, m_dh_secret
+ 96 - secret_size
);
146 BN_free(bn_remote_pubkey
);
150 char const* dh_key_exchange::get_secret() const
155 } // namespace libtorrent
157 #endif // #ifndef TORRENT_DISABLE_ENCRYPTION