make built-in sha functions not conflict with libcrypto

This commit is contained in:
Arvid Norberg 2012-12-10 08:05:39 +00:00
parent 9acaf06c75
commit fc0b5da9ad
3 changed files with 41 additions and 32 deletions

View File

@ -1,3 +1,4 @@
* make built-in sha functions not conflict with libcrypto
* improve web seed hash failure case
* improve DHT lookup times
* uTP path MTU discovery improvements

View File

@ -48,16 +48,20 @@ extern "C"
}
#else
// from sha1.cpp
struct TORRENT_EXTRA_EXPORT SHA_CTX
namespace libtorrent
{
struct TORRENT_EXTRA_EXPORT sha_ctx
{
boost::uint32_t state[5];
boost::uint32_t count[2];
boost::uint8_t buffer[64];
};
};
TORRENT_EXTRA_EXPORT void SHA1_Init(SHA_CTX* context);
TORRENT_EXTRA_EXPORT void SHA1_Update(SHA_CTX* context, boost::uint8_t const* data, boost::uint32_t len);
TORRENT_EXTRA_EXPORT void SHA1_Final(boost::uint8_t* digest, SHA_CTX* context);
TORRENT_EXTRA_EXPORT void SHA1_init(sha_ctx* context);
TORRENT_EXTRA_EXPORT void SHA1_update(sha_ctx* context, boost::uint8_t const* data, boost::uint32_t len);
TORRENT_EXTRA_EXPORT void SHA1_final(boost::uint8_t* digest, sha_ctx* context);
} // namespace libtorrent
#endif
@ -72,7 +76,7 @@ namespace libtorrent
#ifdef TORRENT_USE_GCRYPT
gcry_md_open(&m_context, GCRY_MD_SHA1, 0);
#else
SHA1_Init(&m_context);
SHA1_init(&m_context);
#endif
}
hasher(const char* data, int len)
@ -83,8 +87,8 @@ namespace libtorrent
gcry_md_open(&m_context, GCRY_MD_SHA1, 0);
gcry_md_write(m_context, data, len);
#else
SHA1_Init(&m_context);
SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
SHA1_init(&m_context);
SHA1_update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
#endif
}
@ -110,7 +114,7 @@ namespace libtorrent
#ifdef TORRENT_USE_GCRYPT
gcry_md_write(m_context, data, len);
#else
SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
SHA1_update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
#endif
}
@ -121,7 +125,7 @@ namespace libtorrent
gcry_md_final(m_context);
digest.assign((const char*)gcry_md_read(m_context, 0));
#else
SHA1_Final(digest.begin(), &m_context);
SHA1_final(digest.begin(), &m_context);
#endif
return digest;
}
@ -131,7 +135,7 @@ namespace libtorrent
#ifdef TORRENT_USE_GCRYPT
gcry_md_reset(m_context);
#else
SHA1_Init(&m_context);
SHA1_init(&m_context);
#endif
}
@ -147,7 +151,7 @@ namespace libtorrent
#ifdef TORRENT_USE_GCRYPT
gcry_md_hd_t m_context;
#else
SHA_CTX m_context;
sha_ctx m_context;
#endif
};
}

View File

@ -27,16 +27,20 @@ typedef boost::uint8_t u8;
#include "libtorrent/config.hpp"
struct TORRENT_EXPORT SHA_CTX
namespace libtorrent
{
struct TORRENT_EXPORT sha_ctx
{
u32 state[5];
u32 count[2];
u8 buffer[64];
};
TORRENT_EXPORT void SHA1_Init(SHA_CTX* context);
TORRENT_EXPORT void SHA1_Update(SHA_CTX* context, u8 const* data, u32 len);
TORRENT_EXPORT void SHA1_Final(u8* digest, SHA_CTX* context);
// we don't want these to clash with openssl's libcrypto
TORRENT_EXPORT void SHA1_init(sha_ctx* context);
TORRENT_EXPORT void SHA1_update(sha_ctx* context, u8 const* data, u32 len);
TORRENT_EXPORT void SHA1_final(u8* digest, sha_ctx* context);
namespace
{
@ -80,7 +84,7 @@ namespace
// Hash a single 512-bit block. This is the core of the algorithm.
template <class BlkFun>
void SHA1Transform(u32 state[5], u8 const buffer[64])
void SHA1transform(u32 state[5], u8 const buffer[64])
{
using namespace std;
u32 a, b, c, d, e;
@ -123,12 +127,10 @@ namespace
state[2] += c;
state[3] += d;
state[4] += e;
// Wipe variables
a = b = c = d = e = 0;
}
#ifdef VERBOSE
void SHAPrintContext(SHA_CTX *context, char *msg)
void SHAPrintContext(sha_ctx *context, char *msg)
{
using namespace std;
printf("%s (%d,%d) %x %x %x %x %x\n"
@ -143,7 +145,7 @@ namespace
#endif
template <class BlkFun>
void internal_update(SHA_CTX* context, u8 const* data, u32 len)
void internal_update(sha_ctx* context, u8 const* data, u32 len)
{
using namespace std;
u32 i, j; // JHB
@ -157,10 +159,10 @@ namespace
if ((j + len) > 63)
{
memcpy(&context->buffer[j], data, (i = 64-j));
SHA1Transform<BlkFun>(context->state, context->buffer);
SHA1transform<BlkFun>(context->state, context->buffer);
for ( ; i + 63 < len; i += 64)
{
SHA1Transform<BlkFun>(context->state, &data[i]);
SHA1transform<BlkFun>(context->state, &data[i]);
}
j = 0;
}
@ -185,7 +187,7 @@ namespace
// SHA1Init - Initialize new context
void SHA1_Init(SHA_CTX* context)
void SHA1_init(sha_ctx* context)
{
// SHA1 initialization constants
context->state[0] = 0x67452301;
@ -199,7 +201,7 @@ void SHA1_Init(SHA_CTX* context)
// Run your data through this.
void SHA1_Update(SHA_CTX* context, u8 const* data, u32 len)
void SHA1_update(sha_ctx* context, u8 const* data, u32 len)
{
// GCC standard defines for endianness
// test with: cpp -dM /dev/null
@ -220,7 +222,7 @@ void SHA1_Update(SHA_CTX* context, u8 const* data, u32 len)
// Add padding and return the message digest.
void SHA1_Final(u8* digest, SHA_CTX* context)
void SHA1_final(u8* digest, sha_ctx* context)
{
u8 finalcount[8];
@ -232,10 +234,10 @@ void SHA1_Final(u8* digest, SHA_CTX* context)
>> ((3-(i & 3)) * 8) ) & 255);
}
SHA1_Update(context, (u8 const*)"\200", 1);
SHA1_update(context, (u8 const*)"\200", 1);
while ((context->count[0] & 504) != 448)
SHA1_Update(context, (u8 const*)"\0", 1);
SHA1_Update(context, finalcount, 8); // Should cause a SHA1Transform()
SHA1_update(context, (u8 const*)"\0", 1);
SHA1_update(context, finalcount, 8); // Should cause a SHA1transform()
for (u32 i = 0; i < 20; ++i)
{
@ -244,6 +246,8 @@ void SHA1_Final(u8* digest, SHA_CTX* context)
}
}
} // libtorrent namespace
/************************************************************
-----------------