added optional use of openssl instead of public domain sha-1 implementation, ticket #13
This commit is contained in:
parent
2f5e1ff6b2
commit
8cb1f9f899
8
Jamfile
8
Jamfile
|
@ -30,6 +30,10 @@ feature.compose <dht-support>on : ;
|
||||||
feature.compose <dht-support>off : <define>TORRENT_DISABLE_DHT ;
|
feature.compose <dht-support>off : <define>TORRENT_DISABLE_DHT ;
|
||||||
feature.compose <dht-support>logging : <define>TORRENT_DHT_VERBOSE_LOGGING ;
|
feature.compose <dht-support>logging : <define>TORRENT_DHT_VERBOSE_LOGGING ;
|
||||||
|
|
||||||
|
feature openssl : off on : composite propagated symmetric link-incompatible ;
|
||||||
|
feature.compose <openssl>off : ;
|
||||||
|
feature.compose <openssl>on : <define>TORRENT_USE_OPENSSL ;
|
||||||
|
|
||||||
SOURCES =
|
SOURCES =
|
||||||
allocate_resources.cpp
|
allocate_resources.cpp
|
||||||
alert.cpp
|
alert.cpp
|
||||||
|
@ -56,7 +60,6 @@ SOURCES =
|
||||||
tracker_manager.cpp
|
tracker_manager.cpp
|
||||||
http_tracker_connection.cpp
|
http_tracker_connection.cpp
|
||||||
udp_tracker_connection.cpp
|
udp_tracker_connection.cpp
|
||||||
sha1.cpp
|
|
||||||
metadata_transfer.cpp
|
metadata_transfer.cpp
|
||||||
upnp.cpp
|
upnp.cpp
|
||||||
ut_pex.cpp
|
ut_pex.cpp
|
||||||
|
@ -117,6 +120,7 @@ if [ os.name ] = NT
|
||||||
DEFINES += WIN32 ;
|
DEFINES += WIN32 ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lib openssl_lib : : <name>crypto ;
|
||||||
|
|
||||||
project torrent
|
project torrent
|
||||||
|
|
||||||
|
@ -173,5 +177,7 @@ lib torrent
|
||||||
:
|
:
|
||||||
<dht-support>on:<source>src/$(KADEMLIA_SOURCES)
|
<dht-support>on:<source>src/$(KADEMLIA_SOURCES)
|
||||||
<dht-support>logging:<source>src/$(KADEMLIA_SOURCES)
|
<dht-support>logging:<source>src/$(KADEMLIA_SOURCES)
|
||||||
|
<openssl>off:<source>src/sha1.cpp
|
||||||
|
<openssl>on:<library>openssl_lib
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
|
@ -40,17 +40,25 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include "libtorrent/config.hpp"
|
#include "libtorrent/config.hpp"
|
||||||
#include "zlib.h"
|
#include "zlib.h"
|
||||||
|
|
||||||
|
#ifdef TORRENT_USE_OPENSSL
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#include <openssl/sha.h>
|
||||||
|
}
|
||||||
|
#else
|
||||||
// from sha1.cpp
|
// from sha1.cpp
|
||||||
struct TORRENT_EXPORT SHA1_CTX
|
struct TORRENT_EXPORT SHA_CTX
|
||||||
{
|
{
|
||||||
boost::uint32_t state[5];
|
boost::uint32_t state[5];
|
||||||
boost::uint32_t count[2];
|
boost::uint32_t count[2];
|
||||||
boost::uint8_t buffer[64];
|
boost::uint8_t buffer[64];
|
||||||
};
|
};
|
||||||
|
|
||||||
TORRENT_EXPORT void SHA1Init(SHA1_CTX* context);
|
TORRENT_EXPORT void SHA1_Init(SHA_CTX* context);
|
||||||
TORRENT_EXPORT void SHA1Update(SHA1_CTX* context, boost::uint8_t const* data, boost::uint32_t len);
|
TORRENT_EXPORT void SHA1_Update(SHA_CTX* context, boost::uint8_t const* data, boost::uint32_t len);
|
||||||
TORRENT_EXPORT void SHA1Final(SHA1_CTX* context, boost::uint8_t* digest);
|
TORRENT_EXPORT void SHA1_Final(boost::uint8_t* digest, SHA_CTX* context);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace libtorrent
|
namespace libtorrent
|
||||||
{
|
{
|
||||||
|
@ -79,33 +87,33 @@ namespace libtorrent
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
hasher() { SHA1Init(&m_context); }
|
hasher() { SHA1_Init(&m_context); }
|
||||||
hasher(const char* data, int len)
|
hasher(const char* data, int len)
|
||||||
{
|
{
|
||||||
SHA1Init(&m_context);
|
SHA1_Init(&m_context);
|
||||||
assert(data != 0);
|
assert(data != 0);
|
||||||
assert(len > 0);
|
assert(len > 0);
|
||||||
SHA1Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
|
SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
|
||||||
}
|
}
|
||||||
void update(const char* data, int len)
|
void update(const char* data, int len)
|
||||||
{
|
{
|
||||||
assert(data != 0);
|
assert(data != 0);
|
||||||
assert(len > 0);
|
assert(len > 0);
|
||||||
SHA1Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
|
SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
|
||||||
}
|
}
|
||||||
|
|
||||||
sha1_hash final()
|
sha1_hash final()
|
||||||
{
|
{
|
||||||
sha1_hash digest;
|
sha1_hash digest;
|
||||||
SHA1Final(&m_context, digest.begin());
|
SHA1_Final(digest.begin(), &m_context);
|
||||||
return digest;
|
return digest;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset() { SHA1Init(&m_context); }
|
void reset() { SHA1_Init(&m_context); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
SHA1_CTX m_context;
|
SHA_CTX m_context;
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
25
src/sha1.cpp
25
src/sha1.cpp
|
@ -25,16 +25,16 @@ using boost::uint8_t;
|
||||||
|
|
||||||
#include "libtorrent/config.hpp"
|
#include "libtorrent/config.hpp"
|
||||||
|
|
||||||
struct TORRENT_EXPORT SHA1_CTX
|
struct TORRENT_EXPORT SHA_CTX
|
||||||
{
|
{
|
||||||
uint32_t state[5];
|
uint32_t state[5];
|
||||||
uint32_t count[2];
|
uint32_t count[2];
|
||||||
uint8_t buffer[64];
|
uint8_t buffer[64];
|
||||||
};
|
};
|
||||||
|
|
||||||
TORRENT_EXPORT void SHA1Init(SHA1_CTX* context);
|
TORRENT_EXPORT void SHA1_Init(SHA_CTX* context);
|
||||||
TORRENT_EXPORT void SHA1Update(SHA1_CTX* context, uint8_t const* data, uint32_t len);
|
TORRENT_EXPORT void SHA1_Update(SHA_CTX* context, uint8_t const* data, uint32_t len);
|
||||||
TORRENT_EXPORT void SHA1Final(SHA1_CTX* context, uint8_t* digest);
|
TORRENT_EXPORT void SHA1_Final(uint8_t* digest, SHA_CTX* context);
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
@ -125,7 +125,7 @@ namespace
|
||||||
a = b = c = d = e = 0;
|
a = b = c = d = e = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHAPrintContext(SHA1_CTX *context, char *msg)
|
void SHAPrintContext(SHA_CTX *context, char *msg)
|
||||||
{
|
{
|
||||||
using namespace std;
|
using namespace std;
|
||||||
printf("%s (%d,%d) %x %x %x %x %x\n"
|
printf("%s (%d,%d) %x %x %x %x %x\n"
|
||||||
|
@ -136,7 +136,7 @@ namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class BlkFun>
|
template <class BlkFun>
|
||||||
void internal_update(SHA1_CTX* context, uint8_t const* data, uint32_t len)
|
void internal_update(SHA_CTX* context, uint8_t const* data, uint32_t len)
|
||||||
{
|
{
|
||||||
using namespace std;
|
using namespace std;
|
||||||
uint32_t i, j; // JHB
|
uint32_t i, j; // JHB
|
||||||
|
@ -176,7 +176,7 @@ namespace
|
||||||
|
|
||||||
// SHA1Init - Initialize new context
|
// SHA1Init - Initialize new context
|
||||||
|
|
||||||
void SHA1Init(SHA1_CTX* context)
|
void SHA1_Init(SHA_CTX* context)
|
||||||
{
|
{
|
||||||
// SHA1 initialization constants
|
// SHA1 initialization constants
|
||||||
context->state[0] = 0x67452301;
|
context->state[0] = 0x67452301;
|
||||||
|
@ -190,7 +190,7 @@ void SHA1Init(SHA1_CTX* context)
|
||||||
|
|
||||||
// Run your data through this.
|
// Run your data through this.
|
||||||
|
|
||||||
void SHA1Update(SHA1_CTX* context, uint8_t const* data, uint32_t len)
|
void SHA1_Update(SHA_CTX* context, uint8_t const* data, uint32_t len)
|
||||||
{
|
{
|
||||||
#if defined __BIG_ENDIAN__
|
#if defined __BIG_ENDIAN__
|
||||||
internal_update<big_endian_blk0>(context, data, len);
|
internal_update<big_endian_blk0>(context, data, len);
|
||||||
|
@ -209,7 +209,7 @@ void SHA1Update(SHA1_CTX* context, uint8_t const* data, uint32_t len)
|
||||||
|
|
||||||
// Add padding and return the message digest.
|
// Add padding and return the message digest.
|
||||||
|
|
||||||
void SHA1Final(SHA1_CTX* context, uint8_t* digest)
|
void SHA1_Final(uint8_t* digest, SHA_CTX* context)
|
||||||
{
|
{
|
||||||
uint8_t finalcount[8];
|
uint8_t finalcount[8];
|
||||||
|
|
||||||
|
@ -221,10 +221,10 @@ void SHA1Final(SHA1_CTX* context, uint8_t* digest)
|
||||||
>> ((3-(i & 3)) * 8) ) & 255);
|
>> ((3-(i & 3)) * 8) ) & 255);
|
||||||
}
|
}
|
||||||
|
|
||||||
SHA1Update(context, (uint8_t const*)"\200", 1);
|
SHA1_Update(context, (uint8_t const*)"\200", 1);
|
||||||
while ((context->count[0] & 504) != 448)
|
while ((context->count[0] & 504) != 448)
|
||||||
SHA1Update(context, (uint8_t const*)"\0", 1);
|
SHA1_Update(context, (uint8_t const*)"\0", 1);
|
||||||
SHA1Update(context, finalcount, 8); // Should cause a SHA1Transform()
|
SHA1_Update(context, finalcount, 8); // Should cause a SHA1Transform()
|
||||||
|
|
||||||
for (uint32_t i = 0; i < 20; ++i)
|
for (uint32_t i = 0; i < 20; ++i)
|
||||||
{
|
{
|
||||||
|
@ -301,6 +301,7 @@ By Arvid Norberg <arvidn@sourceforge.net>
|
||||||
4- using anonymous namespace to avoid external
|
4- using anonymous namespace to avoid external
|
||||||
linkage on internal functions
|
linkage on internal functions
|
||||||
5- using standard C++ includes
|
5- using standard C++ includes
|
||||||
|
6- made API compatible with openssl
|
||||||
|
|
||||||
still 100% PD
|
still 100% PD
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue