2008-02-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Random bytes generation using libgcrypt and OpenSSL.
	* src/DHTUtil.{h, cc}
	* test/DHTUtilTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2008-02-20 16:57:47 +00:00
parent 1ea9033363
commit dd8f18956a
6 changed files with 69 additions and 9 deletions

View File

@ -1,3 +1,9 @@
2008-02-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Random bytes generation using libgcrypt and OpenSSL.
* src/DHTUtil.{h, cc}
* test/DHTUtilTest.cc
2008-02-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Fixed segmentation fault when a DHT message which doesn't have 'y' key

View File

@ -37,6 +37,12 @@
#include <cassert>
#include <fstream>
#include <iomanip>
#ifdef HAVE_LIBGCRYPT
# include <gcrypt.h>
#elif HAVE_LIBSSL
# include <openssl/rand.h>
# include "SimpleRandomizer.h"
#endif // HAVE_LIBSSL
namespace aria2 {
@ -47,10 +53,20 @@ void DHTUtil::generateRandomKey(unsigned char* key)
MessageDigestHelper::digest(key, 20, "sha1", bytes, sizeof(bytes));
}
void DHTUtil::generateRandomData(char* data, size_t length)
void DHTUtil::generateRandomData(unsigned char* data, size_t length)
{
#ifdef HAVE_LIBGCRYPT
gcry_randomize(data, length, GCRY_STRONG_RANDOM);
#elif HAVE_LIBSSL
if(RAND_bytes(data, length) != 1) {
for(size_t i = 0; i < length; ++i) {
data[i] = SimpleRandomizer::getInstance()->getRandomNumber(UINT8_MAX+1);
}
}
#else
std::ifstream i("/dev/urandom");
i.read(data, length);
#endif // HAVE_LIBSSL
}
void DHTUtil::flipBit(unsigned char* data, size_t length, size_t bitIndex)

View File

@ -45,11 +45,11 @@ public:
static void generateRandomKey(unsigned char* key);
static void generateRandomData(char* data, size_t length);
static void generateRandomData(unsigned char* data, size_t length);
static void generateRandomData(unsigned char* data, size_t length)
static void generateRandomData(char* data, size_t length)
{
return generateRandomData(reinterpret_cast<char*>(data), length);
return generateRandomData(reinterpret_cast<unsigned char*>(data), length);
}
static void flipBit(unsigned char* data, size_t length, size_t bitIndex);

33
test/DHTUtilTest.cc Normal file
View File

@ -0,0 +1,33 @@
#include "DHTUtil.h"
#include "Exception.h"
#include "Util.h"
#include <cppunit/extensions/HelperMacros.h>
namespace aria2 {
class DHTUtilTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DHTUtilTest);
CPPUNIT_TEST(testGenerateRandomData);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
void testGenerateRandomData();
};
CPPUNIT_TEST_SUITE_REGISTRATION(DHTUtilTest);
void DHTUtilTest::testGenerateRandomData()
{
unsigned char data1[20];
DHTUtil::generateRandomData(data1, sizeof(data1));
unsigned char data2[20];
DHTUtil::generateRandomData(data2, sizeof(data2));
CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
}
} // namespace aria2

View File

@ -131,7 +131,8 @@ aria2c_SOURCES += BtAllowedFastMessageTest.cc\
DHTRoutingTableDeserializerTest.cc\
DHKeyExchangeTest.cc\
ARC4Test.cc\
MSEHandshakeTest.cc
MSEHandshakeTest.cc\
DHTUtilTest.cc
endif # ENABLE_BITTORRENT
if ENABLE_METALINK

View File

@ -116,7 +116,8 @@ check_PROGRAMS = $(am__EXEEXT_1)
@ENABLE_BITTORRENT_TRUE@ DHTRoutingTableDeserializerTest.cc\
@ENABLE_BITTORRENT_TRUE@ DHKeyExchangeTest.cc\
@ENABLE_BITTORRENT_TRUE@ ARC4Test.cc\
@ENABLE_BITTORRENT_TRUE@ MSEHandshakeTest.cc
@ENABLE_BITTORRENT_TRUE@ MSEHandshakeTest.cc\
@ENABLE_BITTORRENT_TRUE@ DHTUtilTest.cc
@ENABLE_METALINK_TRUE@am__append_3 = MetalinkerTest.cc\
@ENABLE_METALINK_TRUE@ MetalinkEntryTest.cc\
@ -211,8 +212,9 @@ am__aria2c_SOURCES_DIST = AllTest.cc SocketCoreTest.cc \
DHTTokenTrackerTest.cc XORCloserTest.cc DHTIDCloserTest.cc \
DHTRoutingTableSerializerTest.cc \
DHTRoutingTableDeserializerTest.cc DHKeyExchangeTest.cc \
ARC4Test.cc MSEHandshakeTest.cc MetalinkerTest.cc \
MetalinkEntryTest.cc Metalink2RequestGroupTest.cc \
ARC4Test.cc MSEHandshakeTest.cc DHTUtilTest.cc \
MetalinkerTest.cc MetalinkEntryTest.cc \
Metalink2RequestGroupTest.cc \
MetalinkPostDownloadHandlerTest.cc MetalinkHelperTest.cc \
MetalinkParserControllerTest.cc MetalinkProcessorTest.cc
@ENABLE_MESSAGE_DIGEST_TRUE@am__objects_1 = \
@ -291,7 +293,8 @@ am__aria2c_SOURCES_DIST = AllTest.cc SocketCoreTest.cc \
@ENABLE_BITTORRENT_TRUE@ DHTRoutingTableDeserializerTest.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ DHKeyExchangeTest.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ ARC4Test.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ MSEHandshakeTest.$(OBJEXT)
@ENABLE_BITTORRENT_TRUE@ MSEHandshakeTest.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ DHTUtilTest.$(OBJEXT)
@ENABLE_METALINK_TRUE@am__objects_3 = MetalinkerTest.$(OBJEXT) \
@ENABLE_METALINK_TRUE@ MetalinkEntryTest.$(OBJEXT) \
@ENABLE_METALINK_TRUE@ Metalink2RequestGroupTest.$(OBJEXT) \
@ -684,6 +687,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTRoutingTableTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTTokenTrackerTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTUnknownMessageTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTUtilTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DataTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultAuthResolverTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtAnnounceTest.Po@am__quote@