Add tests for Information Dispersal and Secret Sharing
parent
35ec7b105e
commit
0cbaea0428
|
|
@ -177,9 +177,6 @@ size_t BlockTransformation::AdvancedProcessBlocks(const byte *inBlocks, const by
|
||||||
{
|
{
|
||||||
// Coverity finding. However, xorBlocks is never NULL if BT_XorInput.
|
// Coverity finding. However, xorBlocks is never NULL if BT_XorInput.
|
||||||
CRYPTOPP_ASSERT(xorBlocks);
|
CRYPTOPP_ASSERT(xorBlocks);
|
||||||
#if defined(__COVERITY__)
|
|
||||||
if (xorBlocks)
|
|
||||||
#endif
|
|
||||||
xorbuf(outBlocks, xorBlocks, inBlocks, blockSize);
|
xorbuf(outBlocks, xorBlocks, inBlocks, blockSize);
|
||||||
ProcessBlock(outBlocks);
|
ProcessBlock(outBlocks);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1265,7 +1265,7 @@ public:
|
||||||
{
|
{
|
||||||
// TODO: What happens if there are more than 2^32 elements?
|
// TODO: What happens if there are more than 2^32 elements?
|
||||||
for (; begin != end; ++begin)
|
for (; begin != end; ++begin)
|
||||||
std::iter_swap(begin, begin + GenerateWord32(0, end-begin-1));
|
std::iter_swap(begin, begin + GenerateWord32(0, static_cast<word32>(end-begin-1)));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
175
validat0.cpp
175
validat0.cpp
|
|
@ -11,8 +11,11 @@
|
||||||
#include "stdcpp.h"
|
#include "stdcpp.h"
|
||||||
#include "default.h"
|
#include "default.h"
|
||||||
#include "zinflate.h"
|
#include "zinflate.h"
|
||||||
|
#include "channels.h"
|
||||||
|
#include "files.h"
|
||||||
#include "gzip.h"
|
#include "gzip.h"
|
||||||
#include "zlib.h"
|
#include "zlib.h"
|
||||||
|
#include "ida.h"
|
||||||
#include "hex.h"
|
#include "hex.h"
|
||||||
#include "asn.h"
|
#include "asn.h"
|
||||||
|
|
||||||
|
|
@ -40,7 +43,7 @@ bool TestCompressors()
|
||||||
for (unsigned int i=0; i<128; ++i)
|
for (unsigned int i=0; i<128; ++i)
|
||||||
{
|
{
|
||||||
std::string src, dest, rec;
|
std::string src, dest, rec;
|
||||||
unsigned int len = GlobalRNG().GenerateWord32() & 0xffff;
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff);
|
||||||
|
|
||||||
src.resize(len);
|
src.resize(len);
|
||||||
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
||||||
|
|
@ -68,7 +71,7 @@ bool TestCompressors()
|
||||||
for (unsigned int i=0; i<128; i++)
|
for (unsigned int i=0; i<128; i++)
|
||||||
{
|
{
|
||||||
std::string src, dest;
|
std::string src, dest;
|
||||||
unsigned int len = GlobalRNG().GenerateWord32() & 0xffff;
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff);
|
||||||
|
|
||||||
src.resize(len);
|
src.resize(len);
|
||||||
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
||||||
|
|
@ -101,7 +104,7 @@ bool TestCompressors()
|
||||||
for (unsigned int i=0; i<128; ++i)
|
for (unsigned int i=0; i<128; ++i)
|
||||||
{
|
{
|
||||||
std::string src, dest, rec;
|
std::string src, dest, rec;
|
||||||
unsigned int len = GlobalRNG().GenerateWord32() & 0xffff;
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff);
|
||||||
|
|
||||||
src.resize(len);
|
src.resize(len);
|
||||||
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
||||||
|
|
@ -130,7 +133,7 @@ bool TestCompressors()
|
||||||
{
|
{
|
||||||
// See if we can induce a crash
|
// See if we can induce a crash
|
||||||
std::string src, dest;
|
std::string src, dest;
|
||||||
unsigned int len = GlobalRNG().GenerateWord32() & 0xffff;
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff);
|
||||||
|
|
||||||
src.resize(len);
|
src.resize(len);
|
||||||
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
||||||
|
|
@ -144,7 +147,7 @@ bool TestCompressors()
|
||||||
for (unsigned int i=0; i<128; i++)
|
for (unsigned int i=0; i<128; i++)
|
||||||
{
|
{
|
||||||
std::string src, dest;
|
std::string src, dest;
|
||||||
unsigned int len = GlobalRNG().GenerateWord32() & 0xffff;
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff);
|
||||||
|
|
||||||
src.resize(len);
|
src.resize(len);
|
||||||
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
||||||
|
|
@ -177,7 +180,7 @@ bool TestCompressors()
|
||||||
for (unsigned int i=0; i<128; ++i)
|
for (unsigned int i=0; i<128; ++i)
|
||||||
{
|
{
|
||||||
std::string src, dest, rec;
|
std::string src, dest, rec;
|
||||||
unsigned int len = GlobalRNG().GenerateWord32() & 0xffff;
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff);
|
||||||
|
|
||||||
src.resize(len);
|
src.resize(len);
|
||||||
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
||||||
|
|
@ -205,7 +208,7 @@ bool TestCompressors()
|
||||||
for (unsigned int i=0; i<128; i++)
|
for (unsigned int i=0; i<128; i++)
|
||||||
{
|
{
|
||||||
std::string src, dest;
|
std::string src, dest;
|
||||||
unsigned int len = GlobalRNG().GenerateWord32() & 0xffff;
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff);
|
||||||
|
|
||||||
src.resize(len);
|
src.resize(len);
|
||||||
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
||||||
|
|
@ -237,7 +240,7 @@ bool TestEncryptors()
|
||||||
for (unsigned int i=0; i<128; ++i)
|
for (unsigned int i=0; i<128; ++i)
|
||||||
{
|
{
|
||||||
std::string pwd, src, dest, rec;
|
std::string pwd, src, dest, rec;
|
||||||
unsigned int len = (GlobalRNG().GenerateWord32() & 0xffff) + 8;
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff) + 8;
|
||||||
|
|
||||||
src.resize(len);
|
src.resize(len);
|
||||||
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
||||||
|
|
@ -270,7 +273,7 @@ bool TestEncryptors()
|
||||||
for (unsigned int i=0; i<128; ++i)
|
for (unsigned int i=0; i<128; ++i)
|
||||||
{
|
{
|
||||||
std::string pwd, src, dest, rec;
|
std::string pwd, src, dest, rec;
|
||||||
unsigned int len = (GlobalRNG().GenerateWord32() & 0xffff) + 8;
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff) + 8;
|
||||||
|
|
||||||
src.resize(len);
|
src.resize(len);
|
||||||
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
||||||
|
|
@ -335,7 +338,7 @@ bool TestEncryptors()
|
||||||
for (unsigned int i=0; i<128; ++i)
|
for (unsigned int i=0; i<128; ++i)
|
||||||
{
|
{
|
||||||
std::string pwd, src, dest, rec;
|
std::string pwd, src, dest, rec;
|
||||||
unsigned int len = (GlobalRNG().GenerateWord32() & 0xffff) + 8;
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff) + 8;
|
||||||
|
|
||||||
src.resize(len);
|
src.resize(len);
|
||||||
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
||||||
|
|
@ -368,7 +371,7 @@ bool TestEncryptors()
|
||||||
for (unsigned int i=0; i<128; ++i)
|
for (unsigned int i=0; i<128; ++i)
|
||||||
{
|
{
|
||||||
std::string pwd, src, dest, rec;
|
std::string pwd, src, dest, rec;
|
||||||
unsigned int len = (GlobalRNG().GenerateWord32() & 0xffff) + 8;
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff) + 8;
|
||||||
|
|
||||||
src.resize(len);
|
src.resize(len);
|
||||||
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
|
||||||
|
|
@ -429,6 +432,156 @@ bool TestEncryptors()
|
||||||
return !fail1 && !fail2 && !fail3 && !fail4;
|
return !fail1 && !fail2 && !fail3 && !fail4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Information Dispesal and Secret Sharing
|
||||||
|
bool TestSharing()
|
||||||
|
{
|
||||||
|
std::cout << "\nInformation Dispersal and Secret Sharing...\n\n";
|
||||||
|
static const unsigned int INFORMATION_SHARES = 128;
|
||||||
|
static const unsigned int SECRET_SHARES = 64;
|
||||||
|
static const unsigned int CHID_LENGTH = 4;
|
||||||
|
bool pass=true, fail=false;
|
||||||
|
|
||||||
|
// ********** Infrmation Dispersal **********//
|
||||||
|
|
||||||
|
for (unsigned int shares=3; shares<INFORMATION_SHARES; ++shares)
|
||||||
|
{
|
||||||
|
std::string message;
|
||||||
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xff);
|
||||||
|
unsigned int threshold = GlobalRNG().GenerateWord32(2, shares-1);
|
||||||
|
|
||||||
|
message.resize(len);
|
||||||
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&message[0]), message.size());
|
||||||
|
|
||||||
|
ChannelSwitch *channelSwitch = NULLPTR;
|
||||||
|
StringSource source(message, false, new InformationDispersal(threshold, shares, channelSwitch = new ChannelSwitch));
|
||||||
|
|
||||||
|
std::vector<std::string> strShares(shares);
|
||||||
|
vector_member_ptrs<StringSink> strSinks(shares);
|
||||||
|
std::string channel;
|
||||||
|
|
||||||
|
// ********** Create Shares
|
||||||
|
for (unsigned int i=0; i<shares; i++)
|
||||||
|
{
|
||||||
|
strSinks[i].reset(new StringSink(strShares[i]));
|
||||||
|
channel = WordToString<word32>(i);
|
||||||
|
strSinks[i]->Put((const byte *)channel.data(), CHID_LENGTH);
|
||||||
|
channelSwitch->AddRoute(channel, *strSinks[i], DEFAULT_CHANNEL);
|
||||||
|
}
|
||||||
|
source.PumpAll();
|
||||||
|
|
||||||
|
// ********** Randomize shares
|
||||||
|
|
||||||
|
GlobalRNG().Shuffle(strShares.begin(), strShares.end());
|
||||||
|
|
||||||
|
// ********** Recover secret
|
||||||
|
try
|
||||||
|
{
|
||||||
|
std::string recovered;
|
||||||
|
InformationRecovery recovery(threshold, new StringSink(recovered));
|
||||||
|
|
||||||
|
vector_member_ptrs<StringSource> strSources(threshold);
|
||||||
|
channel.resize(CHID_LENGTH);
|
||||||
|
for (unsigned int i=0; i<threshold; i++)
|
||||||
|
{
|
||||||
|
strSources[i].reset(new StringSource(strShares[i], false));
|
||||||
|
strSources[i]->Pump(CHID_LENGTH);
|
||||||
|
strSources[i]->Get((byte*)&channel[0], CHID_LENGTH);
|
||||||
|
strSources[i]->Attach(new ChannelSwitch(recovery, channel));
|
||||||
|
}
|
||||||
|
|
||||||
|
while (strSources[0]->Pump(256))
|
||||||
|
{
|
||||||
|
for (unsigned int i=1; i<threshold; i++)
|
||||||
|
strSources[i]->Pump(256);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i=0; i<threshold; i++)
|
||||||
|
strSources[i]->PumpAll();
|
||||||
|
|
||||||
|
fail = (message != recovered);
|
||||||
|
}
|
||||||
|
catch (const Exception&)
|
||||||
|
{
|
||||||
|
fail = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pass &= !fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << (fail ? "FAILED:" : "passed:") << " 64 information dispersals\n";
|
||||||
|
|
||||||
|
// ********** Secret Sharing **********//
|
||||||
|
|
||||||
|
for (unsigned int shares=3; shares<SECRET_SHARES; ++shares)
|
||||||
|
{
|
||||||
|
std::string message;
|
||||||
|
unsigned int len = GlobalRNG().GenerateWord32(0, 0xff);
|
||||||
|
unsigned int threshold = GlobalRNG().GenerateWord32(2, shares-1);
|
||||||
|
|
||||||
|
message.resize(len);
|
||||||
|
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&message[0]), message.size());
|
||||||
|
|
||||||
|
ChannelSwitch *channelSwitch = NULLPTR;
|
||||||
|
StringSource source(message, false, new SecretSharing(GlobalRNG(), threshold, shares, channelSwitch = new ChannelSwitch));
|
||||||
|
|
||||||
|
std::vector<std::string> strShares(shares);
|
||||||
|
vector_member_ptrs<StringSink> strSinks(shares);
|
||||||
|
std::string channel;
|
||||||
|
|
||||||
|
// ********** Create Shares
|
||||||
|
for (unsigned int i=0; i<shares; i++)
|
||||||
|
{
|
||||||
|
strSinks[i].reset(new StringSink(strShares[i]));
|
||||||
|
channel = WordToString<word32>(i);
|
||||||
|
strSinks[i]->Put((const byte *)channel.data(), CHID_LENGTH);
|
||||||
|
channelSwitch->AddRoute(channel, *strSinks[i], DEFAULT_CHANNEL);
|
||||||
|
}
|
||||||
|
source.PumpAll();
|
||||||
|
|
||||||
|
// ********** Randomize shares
|
||||||
|
|
||||||
|
GlobalRNG().Shuffle(strShares.begin(), strShares.end());
|
||||||
|
|
||||||
|
// ********** Recover secret
|
||||||
|
try
|
||||||
|
{
|
||||||
|
std::string recovered;
|
||||||
|
SecretRecovery recovery(threshold, new StringSink(recovered));
|
||||||
|
|
||||||
|
vector_member_ptrs<StringSource> strSources(threshold);
|
||||||
|
channel.resize(CHID_LENGTH);
|
||||||
|
for (unsigned int i=0; i<threshold; i++)
|
||||||
|
{
|
||||||
|
strSources[i].reset(new StringSource(strShares[i], false));
|
||||||
|
strSources[i]->Pump(CHID_LENGTH);
|
||||||
|
strSources[i]->Get((byte*)&channel[0], CHID_LENGTH);
|
||||||
|
strSources[i]->Attach(new ChannelSwitch(recovery, channel));
|
||||||
|
}
|
||||||
|
|
||||||
|
while (strSources[0]->Pump(256))
|
||||||
|
{
|
||||||
|
for (unsigned int i=1; i<threshold; i++)
|
||||||
|
strSources[i]->Pump(256);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i=0; i<threshold; i++)
|
||||||
|
strSources[i]->PumpAll();
|
||||||
|
|
||||||
|
fail = (message != recovered);
|
||||||
|
}
|
||||||
|
catch (const Exception&)
|
||||||
|
{
|
||||||
|
fail = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pass &= !fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << (fail ? "FAILED:" : "passed:") << " 64 secret sharings\n";
|
||||||
|
|
||||||
|
return pass;
|
||||||
|
}
|
||||||
|
|
||||||
bool TestRounding()
|
bool TestRounding()
|
||||||
{
|
{
|
||||||
std::cout << "\nTesting RoundUpToMultipleOf/RoundDownToMultipleOf...\n\n";
|
std::cout << "\nTesting RoundUpToMultipleOf/RoundDownToMultipleOf...\n\n";
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,7 @@ bool ValidateAll(bool thorough)
|
||||||
// Additional tests due to no coverage
|
// Additional tests due to no coverage
|
||||||
pass=ValidateBaseCode() && pass;
|
pass=ValidateBaseCode() && pass;
|
||||||
pass=TestCompressors() && pass;
|
pass=TestCompressors() && pass;
|
||||||
|
pass=TestSharing() && pass;
|
||||||
pass=TestEncryptors() && pass;
|
pass=TestEncryptors() && pass;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -129,6 +129,7 @@ bool TestASN1Parse();
|
||||||
bool TestCompressors();
|
bool TestCompressors();
|
||||||
bool TestEncryptors();
|
bool TestEncryptors();
|
||||||
bool TestMersenne();
|
bool TestMersenne();
|
||||||
|
bool TestSharing();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if 1
|
#if 1
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue