diff --git a/cryptlib.cpp b/cryptlib.cpp index 8920d356..24eb0209 100644 --- a/cryptlib.cpp +++ b/cryptlib.cpp @@ -177,9 +177,6 @@ size_t BlockTransformation::AdvancedProcessBlocks(const byte *inBlocks, const by { // Coverity finding. However, xorBlocks is never NULL if BT_XorInput. CRYPTOPP_ASSERT(xorBlocks); -#if defined(__COVERITY__) - if (xorBlocks) -#endif xorbuf(outBlocks, xorBlocks, inBlocks, blockSize); ProcessBlock(outBlocks); } diff --git a/cryptlib.h b/cryptlib.h index 8f38e70b..f2c04267 100644 --- a/cryptlib.h +++ b/cryptlib.h @@ -1265,7 +1265,7 @@ public: { // TODO: What happens if there are more than 2^32 elements? for (; begin != end; ++begin) - std::iter_swap(begin, begin + GenerateWord32(0, end-begin-1)); + std::iter_swap(begin, begin + GenerateWord32(0, static_cast(end-begin-1))); } }; diff --git a/validat0.cpp b/validat0.cpp index 4d3c5c90..291d354b 100644 --- a/validat0.cpp +++ b/validat0.cpp @@ -11,8 +11,11 @@ #include "stdcpp.h" #include "default.h" #include "zinflate.h" +#include "channels.h" +#include "files.h" #include "gzip.h" #include "zlib.h" +#include "ida.h" #include "hex.h" #include "asn.h" @@ -40,7 +43,7 @@ bool TestCompressors() for (unsigned int i=0; i<128; ++i) { std::string src, dest, rec; - unsigned int len = GlobalRNG().GenerateWord32() & 0xffff; + unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff); src.resize(len); GlobalRNG().GenerateBlock(reinterpret_cast(&src[0]), src.size()); @@ -68,7 +71,7 @@ bool TestCompressors() for (unsigned int i=0; i<128; i++) { std::string src, dest; - unsigned int len = GlobalRNG().GenerateWord32() & 0xffff; + unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff); src.resize(len); GlobalRNG().GenerateBlock(reinterpret_cast(&src[0]), src.size()); @@ -101,7 +104,7 @@ bool TestCompressors() for (unsigned int i=0; i<128; ++i) { std::string src, dest, rec; - unsigned int len = GlobalRNG().GenerateWord32() & 0xffff; + unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff); src.resize(len); GlobalRNG().GenerateBlock(reinterpret_cast(&src[0]), src.size()); @@ -130,7 +133,7 @@ bool TestCompressors() { // See if we can induce a crash std::string src, dest; - unsigned int len = GlobalRNG().GenerateWord32() & 0xffff; + unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff); src.resize(len); GlobalRNG().GenerateBlock(reinterpret_cast(&src[0]), src.size()); @@ -144,7 +147,7 @@ bool TestCompressors() for (unsigned int i=0; i<128; i++) { std::string src, dest; - unsigned int len = GlobalRNG().GenerateWord32() & 0xffff; + unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff); src.resize(len); GlobalRNG().GenerateBlock(reinterpret_cast(&src[0]), src.size()); @@ -177,7 +180,7 @@ bool TestCompressors() for (unsigned int i=0; i<128; ++i) { std::string src, dest, rec; - unsigned int len = GlobalRNG().GenerateWord32() & 0xffff; + unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff); src.resize(len); GlobalRNG().GenerateBlock(reinterpret_cast(&src[0]), src.size()); @@ -205,7 +208,7 @@ bool TestCompressors() for (unsigned int i=0; i<128; i++) { std::string src, dest; - unsigned int len = GlobalRNG().GenerateWord32() & 0xffff; + unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff); src.resize(len); GlobalRNG().GenerateBlock(reinterpret_cast(&src[0]), src.size()); @@ -237,7 +240,7 @@ bool TestEncryptors() for (unsigned int i=0; i<128; ++i) { std::string pwd, src, dest, rec; - unsigned int len = (GlobalRNG().GenerateWord32() & 0xffff) + 8; + unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff) + 8; src.resize(len); GlobalRNG().GenerateBlock(reinterpret_cast(&src[0]), src.size()); @@ -270,7 +273,7 @@ bool TestEncryptors() for (unsigned int i=0; i<128; ++i) { std::string pwd, src, dest, rec; - unsigned int len = (GlobalRNG().GenerateWord32() & 0xffff) + 8; + unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff) + 8; src.resize(len); GlobalRNG().GenerateBlock(reinterpret_cast(&src[0]), src.size()); @@ -335,7 +338,7 @@ bool TestEncryptors() for (unsigned int i=0; i<128; ++i) { std::string pwd, src, dest, rec; - unsigned int len = (GlobalRNG().GenerateWord32() & 0xffff) + 8; + unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff) + 8; src.resize(len); GlobalRNG().GenerateBlock(reinterpret_cast(&src[0]), src.size()); @@ -368,7 +371,7 @@ bool TestEncryptors() for (unsigned int i=0; i<128; ++i) { std::string pwd, src, dest, rec; - unsigned int len = (GlobalRNG().GenerateWord32() & 0xffff) + 8; + unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff) + 8; src.resize(len); GlobalRNG().GenerateBlock(reinterpret_cast(&src[0]), src.size()); @@ -429,6 +432,156 @@ bool TestEncryptors() 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(&message[0]), message.size()); + + ChannelSwitch *channelSwitch = NULLPTR; + StringSource source(message, false, new InformationDispersal(threshold, shares, channelSwitch = new ChannelSwitch)); + + std::vector strShares(shares); + vector_member_ptrs strSinks(shares); + std::string channel; + + // ********** Create Shares + for (unsigned int i=0; i(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 strSources(threshold); + channel.resize(CHID_LENGTH); + for (unsigned int i=0; iPump(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; iPump(256); + } + + for (unsigned int i=0; iPumpAll(); + + 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(&message[0]), message.size()); + + ChannelSwitch *channelSwitch = NULLPTR; + StringSource source(message, false, new SecretSharing(GlobalRNG(), threshold, shares, channelSwitch = new ChannelSwitch)); + + std::vector strShares(shares); + vector_member_ptrs strSinks(shares); + std::string channel; + + // ********** Create Shares + for (unsigned int i=0; i(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 strSources(threshold); + channel.resize(CHID_LENGTH); + for (unsigned int i=0; iPump(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; iPump(256); + } + + for (unsigned int i=0; iPumpAll(); + + fail = (message != recovered); + } + catch (const Exception&) + { + fail = true; + } + + pass &= !fail; + } + + std::cout << (fail ? "FAILED:" : "passed:") << " 64 secret sharings\n"; + + return pass; +} + bool TestRounding() { std::cout << "\nTesting RoundUpToMultipleOf/RoundDownToMultipleOf...\n\n"; diff --git a/validat1.cpp b/validat1.cpp index dd430de8..b38dac66 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -97,6 +97,7 @@ bool ValidateAll(bool thorough) // Additional tests due to no coverage pass=ValidateBaseCode() && pass; pass=TestCompressors() && pass; + pass=TestSharing() && pass; pass=TestEncryptors() && pass; #endif diff --git a/validate.h b/validate.h index ed9d98f7..23d28414 100644 --- a/validate.h +++ b/validate.h @@ -129,6 +129,7 @@ bool TestASN1Parse(); bool TestCompressors(); bool TestEncryptors(); bool TestMersenne(); +bool TestSharing(); #endif #if 1