I tried to use the SecretSharing Class in Crypto++, but I couldn't make it work.
Here is my code:
using namespace CryptoPP;
void secretSharing(){
AutoSeededRandomPool rng;
SecretSharing shamir(rng, 4, 6);
byte test[] = {'a', 'b', 'c', 'd'};
shamir.Put(test, 4);
//shamir.MessageEnd();
//cout << shamir.TotalBytesRetrievable() <<endl;
}
After compile and run, I will get:
./main
terminate called after throwing an instance of 'CryptoPP::BufferedTransformation::NoChannelSupport'
what(): unknown: this object doesn't support multiple channels
[1] 3597 abort (core dumped) ./main
The declaration of SecretSharing::SecretSharing()
is:
SecretSharing (RandomNumberGenerator &rng, int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
Should I give it a BufferedTransformation*
, but exactly which class should I use?
Are there any Secret Sharing example code in Crypto++?
Building on Fraser's answer, here's the code.
void SecretShareFile(int threshold, int nShares, const char *filename, const char *seed)
{
RandomPool rng;
rng.IncorporateEntropy((byte *)seed, strlen(seed));
ChannelSwitch *channelSwitch;
FileSource source(filename, false, new SecretSharing(rng,
threshold, nShares, channelSwitch = new ChannelSwitch));
vector_member_ptrs<FileSink> fileSinks(nShares);
string channel;
for (int i=0; i<nShares; i++)
{
char extension[5] = ".000";
extension[1]='0'+byte(i/100);
extension[2]='0'+byte((i/10)%10);
extension[3]='0'+byte(i%10);
fileSinks[i].reset(new FileSink((string(filename)+extension).c_str()));
channel = WordToString<word32>(i);
fileSinks[i]->Put((byte *)channel.data(), 4);
channelSwitch->AddRoute(channel, *fileSinks[i], DEFAULT_CHANNEL);
}
source.PumpAll();
}
In the code above, there's a named ChannelSwitch
variable created with new
. The FileSource
source
owns it and will delete it. But he needed a named variable (and not an anonymous or temporary) because he later calls channelSwitch->AddRoute
Wei could have done it with a Redirector
to allow a stack allocation (stay out of the memory manager) and to ensure the FileSource
filter does not delete it:
ChannelSwitch channelSwitch;
FileSource source(filename, false, new SecretSharing(rng,
threshold, nShares, new Redirector(channelSwitch));
...
channelSwitch.AddRoute(channel, *fileSinks[i], DEFAULT_CHANNEL);
And the recovery code:
void SecretRecoverFile(int threshold, const char *outFilename, char *const *inFilenames)
{
SecretRecovery recovery(threshold, new FileSink(outFilename));
vector_member_ptrs<FileSource> fileSources(threshold);
SecByteBlock channel(4);
int i;
for (i=0; i<threshold; i++)
{
fileSources[i].reset(new FileSource(inFilenames[i], false));
fileSources[i]->Pump(4);
fileSources[i]->Get(channel, 4);
fileSources[i]->Attach(new ChannelSwitch(recovery, string((char *)channel.begin(), 4)));
}
while (fileSources[0]->Pump(256))
for (i=1; i<threshold; i++)
fileSources[i]->Pump(256);
for (i=0; i<threshold; i++)
fileSources[i]->PumpAll();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With