~ubuntu-branches/debian/experimental/libtorrent/experimental

« back to all changes in this revision

Viewing changes to src/protocol/handshake_encryption.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jose Luis Rivas
  • Date: 2007-03-31 10:31:05 UTC
  • mto: (4.1.4 gutsy) (6.2.1 squeeze) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20070331103105-jzpp1rml6ud0ff75
Tags: upstream-0.11.4
ImportĀ upstreamĀ versionĀ 0.11.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// libTorrent - BitTorrent library
 
2
// Copyright (C) 2005-2006, Jari Sundell
 
3
//
 
4
// This program is free software; you can redistribute it and/or modify
 
5
// it under the terms of the GNU General Public License as published by
 
6
// the Free Software Foundation; either version 2 of the License, or
 
7
// (at your option) any later version.
 
8
// 
 
9
// This program is distributed in the hope that it will be useful,
 
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
// GNU General Public License for more details.
 
13
// 
 
14
// You should have received a copy of the GNU General Public License
 
15
// along with this program; if not, write to the Free Software
 
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
//
 
18
// In addition, as a special exception, the copyright holders give
 
19
// permission to link the code of portions of this program with the
 
20
// OpenSSL library under certain conditions as described in each
 
21
// individual source file, and distribute linked combinations
 
22
// including the two.
 
23
//
 
24
// You must obey the GNU General Public License in all respects for
 
25
// all of the code used other than OpenSSL.  If you modify file(s)
 
26
// with this exception, you may extend this exception to your version
 
27
// of the file(s), but you are not obligated to do so.  If you do not
 
28
// wish to do so, delete this exception statement from your version.
 
29
// If you delete this exception statement from all source files in the
 
30
// program, then also delete it here.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
#include "config.h"
 
38
 
 
39
#include <algorithm>
 
40
#include <functional>
 
41
 
 
42
#include "torrent/connection_manager.h"
 
43
#include "torrent/exceptions.h"
 
44
#include "utils/diffie_hellman.h"
 
45
#include "utils/sha1.h"
 
46
 
 
47
#include "handshake_encryption.h"
 
48
 
 
49
namespace torrent {
 
50
 
 
51
const unsigned char HandshakeEncryption::dh_prime[] = {
 
52
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
 
53
  0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
 
54
  0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6,
 
55
  0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
 
56
  0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D,
 
57
  0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45, 
 
58
  0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9,
 
59
  0xA6, 0x3A, 0x36, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x05, 0x63, 
 
60
};
 
61
 
 
62
const unsigned char HandshakeEncryption::dh_generator[] = { 2 };
 
63
const unsigned char HandshakeEncryption::vc_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
 
64
 
 
65
bool
 
66
HandshakeEncryption::should_retry() const {
 
67
  return (m_options & ConnectionManager::encryption_enable_retry) != 0 && m_retry != HandshakeEncryption::RETRY_NONE;
 
68
}
 
69
 
 
70
void
 
71
HandshakeEncryption::initialize() {
 
72
  m_key = new DiffieHellman(dh_prime, dh_prime_length, dh_generator, dh_generator_length);
 
73
}
 
74
 
 
75
void
 
76
HandshakeEncryption::cleanup() {
 
77
  delete m_key;
 
78
  m_key = NULL;
 
79
}
 
80
 
 
81
bool
 
82
HandshakeEncryption::compare_vc(const void* buf) {
 
83
  return std::memcmp(buf, vc_data, vc_length) == 0;
 
84
}
 
85
 
 
86
void
 
87
HandshakeEncryption::initialize_decrypt(const char* origHash, bool incoming) {
 
88
  char hash[20];
 
89
  unsigned char discard[1024];
 
90
 
 
91
  sha1_salt(incoming ? "keyA" : "keyB", 4, m_key->c_str(), 96, origHash, 20, hash);
 
92
 
 
93
  m_info.set_decrypt(RC4((const unsigned char*)hash, 20));
 
94
  m_info.decrypt(discard, 1024);
 
95
}
 
96
 
 
97
void
 
98
HandshakeEncryption::initialize_encrypt(const char* origHash, bool incoming) {
 
99
  char hash[20];
 
100
  unsigned char discard[1024];
 
101
 
 
102
  sha1_salt(incoming ? "keyB" : "keyA", 4, m_key->c_str(), 96, origHash, 20, hash);
 
103
 
 
104
  m_info.set_encrypt(RC4((const unsigned char*)hash, 20));
 
105
  m_info.encrypt(discard, 1024);
 
106
}
 
107
 
 
108
// Obfuscated hash is HASH('req2', download_hash), extract that from
 
109
// HASH('req2', download_hash) ^ HASH('req3', S).
 
110
void
 
111
HandshakeEncryption::deobfuscate_hash(char* src) const {
 
112
  char tmp[20];
 
113
  sha1_salt("req3", 4, m_key->c_str(), m_key->size(), tmp);
 
114
 
 
115
  for (int i = 0; i < 20; i++)
 
116
    src[i] ^= tmp[i];
 
117
}
 
118
 
 
119
void
 
120
HandshakeEncryption::hash_req1_to_sync() {
 
121
  sha1_salt("req1", 4,
 
122
            m_key->c_str(), m_key->size(),
 
123
            modify_sync(20));
 
124
}
 
125
 
 
126
void
 
127
HandshakeEncryption::encrypt_vc_to_sync(const char* origHash) {
 
128
  m_syncLength = vc_length;
 
129
  std::memcpy(m_sync, vc_data, vc_length);
 
130
 
 
131
  char hash[20];
 
132
  char discard[1024];
 
133
 
 
134
  sha1_salt("keyB", 4, m_key->c_str(), 96, origHash, 20, hash);
 
135
 
 
136
  RC4 peerEncrypt((const unsigned char*)hash, 20);
 
137
 
 
138
  peerEncrypt.crypt(discard, 1024);
 
139
  peerEncrypt.crypt(m_sync, HandshakeEncryption::vc_length);
 
140
}
 
141
 
 
142
}