~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/pe_crypto.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright (c) 2007, Un Shyam
 
4
All rights reserved.
 
5
 
 
6
Redistribution and use in source and binary forms, with or without
 
7
modification, are permitted provided that the following conditions
 
8
are met:
 
9
 
 
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.
 
18
 
 
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.
 
30
 
 
31
*/
 
32
 
 
33
#ifndef TORRENT_DISABLE_ENCRYPTION
 
34
 
 
35
#ifndef TORRENT_PE_CRYPTO_HPP_INCLUDED
 
36
#define TORRENT_PE_CRYPTO_HPP_INCLUDED
 
37
 
 
38
#include <openssl/dh.h>
 
39
#include <openssl/engine.h>
 
40
#include <openssl/rc4.h>
 
41
 
 
42
#include "libtorrent/peer_id.hpp" // For sha1_hash
 
43
#include "libtorrent/assert.hpp"
 
44
 
 
45
namespace libtorrent
 
46
{
 
47
        class DH_key_exchange
 
48
        {
 
49
        public:
 
50
                DH_key_exchange ();
 
51
                ~DH_key_exchange ();
 
52
 
 
53
                // Get local public key, always 96 bytes
 
54
                char const* get_local_key (void) const;
 
55
 
 
56
                // read remote_pubkey, generate and store shared secret in
 
57
                // m_dh_secret
 
58
                void compute_secret (const char* remote_pubkey);
 
59
 
 
60
                const char* get_secret (void) const;
 
61
                
 
62
        private:
 
63
                int get_local_key_size () const
 
64
                {
 
65
                        TORRENT_ASSERT(m_DH);
 
66
                        return BN_num_bytes (m_DH->pub_key);
 
67
                }
 
68
 
 
69
                DH* m_DH;
 
70
                static const unsigned char m_dh_prime[96];
 
71
                static const unsigned char m_dh_generator[1];
 
72
 
 
73
                char m_dh_local_key[96];
 
74
                char m_dh_secret[96];
 
75
        };
 
76
        
 
77
        class RC4_handler // Non copyable
 
78
        {
 
79
        public:
 
80
                // Input longkeys must be 20 bytes
 
81
                RC4_handler (const sha1_hash& rc4_local_longkey,
 
82
                                         const sha1_hash& rc4_remote_longkey)
 
83
                        
 
84
                {
 
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()));
 
89
 
 
90
                        // Discard first 1024 bytes
 
91
                        char buf[1024];
 
92
                        encrypt (buf, 1024);
 
93
                        decrypt (buf, 1024);
 
94
                };
 
95
                
 
96
                ~RC4_handler () {};
 
97
 
 
98
                void encrypt (char* pos, int len)
 
99
                {
 
100
                        TORRENT_ASSERT(len >= 0);
 
101
                        TORRENT_ASSERT(pos);
 
102
 
 
103
                        RC4 (&m_local_key, len, reinterpret_cast<unsigned char const*>(pos),
 
104
                                 reinterpret_cast<unsigned char*>(pos));
 
105
                }
 
106
 
 
107
                void decrypt (char* pos, int len)
 
108
                {
 
109
                        TORRENT_ASSERT(len >= 0);
 
110
                        TORRENT_ASSERT(pos);
 
111
 
 
112
                        RC4 (&m_remote_key, len, reinterpret_cast<unsigned char const*>(pos),
 
113
                                 reinterpret_cast<unsigned char*>(pos));
 
114
                }
 
115
 
 
116
        private:
 
117
                RC4_KEY m_local_key; // Key to encrypt outgoing data
 
118
                RC4_KEY m_remote_key; // Key to decrypt incoming data
 
119
        };
 
120
        
 
121
} // namespace libtorrent
 
122
 
 
123
#endif // TORRENT_PE_CRYPTO_HPP_INCLUDED
 
124
#endif // TORRENT_DISABLE_ENCRYPTION