ida.h
Go to the documentation of this file.
// ida.h - originally written and placed in the public domain by Wei Dai
/// \file ida.h
/// \brief Classes for Rabin's Information Dispersal and Shamir's Secret Sharing algorithms
#ifndef CRYPTOPP_IDA_H
#define CRYPTOPP_IDA_H
#include "cryptlib.h"
#include "mqueue.h"
#include "filters.h"
#include "channels.h"
#include "secblock.h"
#include "gf2_32.h"
#include "stdcpp.h"
#include "misc.h"
NAMESPACE_BEGIN(CryptoPP)
/// \brief Secret sharing and information dispersal base class
/// \since Crypto++ 1.0
class RawIDA : public AutoSignaling<Unflushable<Multichannel<Filter> > >
{
public:
RawIDA(BufferedTransformation *attachment=NULLPTR)
: m_channelsReady(0), m_channelsFinished(0), m_threshold (0)
{Detach(attachment);}
unsigned int GetThreshold() const {return m_threshold;}
void AddOutputChannel(word32 channelId);
void ChannelData(word32 channelId, const byte *inString, size_t length, bool messageEnd);
lword InputBuffered(word32 channelId) const;
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
{
if (!blocking)
throw BlockingInputOnly("RawIDA");
ChannelData(StringToWord<word32>(channel), begin, length, messageEnd != 0);
return 0;
}
protected:
virtual void FlushOutputQueues();
virtual void OutputMessageEnds();
unsigned int InsertInputChannel(word32 channelId);
unsigned int LookupInputChannel(word32 channelId) const;
void ComputeV(unsigned int);
void PrepareInterpolation();
void ProcessInputQueues();
typedef std::map<word32, unsigned int> InputChannelMap;
InputChannelMap m_inputChannelMap;
InputChannelMap::iterator m_lastMapPosition;
std::vector<MessageQueue> m_inputQueues;
std::vector<word32> m_inputChannelIds, m_outputChannelIds, m_outputToInput;
std::vector<std::string> m_outputChannelIdStrings;
std::vector<ByteQueue> m_outputQueues;
std::vector<SecBlock<word32> > m_v;
SecBlock<word32> m_u, m_w, m_y;
const GF2_32 m_gf32;
unsigned int m_channelsReady, m_channelsFinished;
int m_threshold;
};
/// \brief Shamir's Secret Sharing Algorithm
/// \details SecretSharing is a variant of Shamir's secret sharing algorithm
/// \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
/// \since Crypto++ 1.0
class SecretSharing : public CustomFlushPropagation<Filter>
{
public:
/// \brief Construct a SecretSharing
SecretSharing(RandomNumberGenerator &rng, int threshold, int nShares, BufferedTransformation *attachment=NULLPTR, bool addPadding=true)
: m_rng(rng), m_ida(new OutputProxy(*this, true))
{
Detach(attachment);
IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
}
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
protected:
RandomNumberGenerator &m_rng;
RawIDA m_ida;
bool m_pad;
};
/// \brief Shamir's Secret Sharing Algorithm
/// \details SecretSharing is a variant of Shamir's secret sharing algorithm
/// \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
/// \since Crypto++ 1.0
class SecretRecovery : public RawIDA
{
public:
/// \brief Construct a SecretRecovery
SecretRecovery(int threshold, BufferedTransformation *attachment=NULLPTR, bool removePadding=true)
: RawIDA(attachment)
{IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
protected:
void FlushOutputQueues();
void OutputMessageEnds();
bool m_pad;
};
/// a variant of Rabin's Information Dispersal Algorithm
/// \brief Rabin's Information Dispersal Algorithm
/// \details InformationDispersal is a variant of Rabin's information dispersal algorithm
/// \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
/// \since Crypto++ 1.0
class InformationDispersal : public CustomFlushPropagation<Filter>
{
public:
/// \brief Construct a InformationDispersal
InformationDispersal(int threshold, int nShares, BufferedTransformation *attachment=NULLPTR, bool addPadding=true)
: m_ida(new OutputProxy(*this, true)), m_pad(false), m_nextChannel(0)
{
Detach(attachment);
IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
}
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
protected:
RawIDA m_ida;
bool m_pad;
unsigned int m_nextChannel;
};
/// \brief Rabin's Information Dispersal Algorithm
/// \details InformationDispersal is a variant of Rabin's information dispersal algorithm
/// \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
/// \since Crypto++ 1.0
class InformationRecovery : public RawIDA
{
public:
/// \brief Construct a InformationRecovery
InformationRecovery(int threshold, BufferedTransformation *attachment=NULLPTR, bool removePadding=true)
: RawIDA(attachment), m_pad(false)
{IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
protected:
void FlushOutputQueues();
void OutputMessageEnds();
bool m_pad;
ByteQueue m_queue;
};
class PaddingRemover : public Unflushable<Filter>
{
public:
PaddingRemover(BufferedTransformation *attachment=NULLPTR)
: m_possiblePadding(false), m_zeroCount(0) {Detach(attachment);}
void IsolatedInitialize(const NameValuePairs &parameters)
{CRYPTOPP_UNUSED(parameters); m_possiblePadding = false;}
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
// GetPossiblePadding() == false at the end of a message indicates incorrect padding
bool GetPossiblePadding() const {return m_possiblePadding;}
private:
bool m_possiblePadding;
lword m_zeroCount;
};
NAMESPACE_END
#endif
MakeParameters
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:508
channels.h
Classes for multiple named channels.
AutoSignaling
Provides auto signaling support.
Definition: simple.h:423
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1652
ByteQueue
Data structure used to store byte strings.
Definition: queue.h:23
CustomFlushPropagation
Interface for custom flush signals propagation.
Definition: simple.h:262
Filter
Implementation of BufferedTransformation's attachment interface.
Definition: filters.h:36
Filter::Detach
void Detach(BufferedTransformation *newAttachment=NULL)
Replace an attached transformation.
GF2_32
GF(2^32) with polynomial basis.
Definition: gf2_32.h:17
InformationDispersal
a variant of Rabin's Information Dispersal Algorithm
Definition: ida.h:120
InformationDispersal::Put2
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: ida.cpp:320
InformationDispersal::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
Definition: ida.cpp:313
InformationDispersal::InformationDispersal
InformationDispersal(int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
Construct a InformationDispersal.
Definition: ida.h:123
InformationDispersal::Flush
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: ida.h:132
InformationRecovery
Rabin's Information Dispersal Algorithm.
Definition: ida.h:145
InformationRecovery::InformationRecovery
InformationRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
Construct a InformationRecovery.
Definition: ida.h:148
Multichannel
Multiple channels support for custom signal processing.
Definition: simple.h:316
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:322
OutputProxy
Filter class that is a proxy for a sink.
Definition: filters.h:991
PaddingRemover
Definition: ida.h:163
PaddingRemover::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: ida.h:168
PaddingRemover::Put2
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: ida.cpp:378
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1435
RawIDA
Secret sharing and information dispersal base class.
Definition: ida.h:23
RawIDA::ChannelPut2
size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing on a channel.
Definition: ida.h:35
SecBlock< word32 >
SecretRecovery
Shamir's Secret Sharing Algorithm.
Definition: ida.h:97
SecretRecovery::SecretRecovery
SecretRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
Construct a SecretRecovery.
Definition: ida.h:100
SecretSharing
Shamir's Secret Sharing Algorithm.
Definition: ida.h:72
SecretSharing::SecretSharing
SecretSharing(RandomNumberGenerator &rng, int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
Construct a SecretSharing.
Definition: ida.h:75
SecretSharing::Put2
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: ida.cpp:248
SecretSharing::Flush
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: ida.h:84
SecretSharing::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
Definition: ida.cpp:242
Unflushable
Base class for unflushable filters.
Definition: simple.h:134
Unflushable::Flush
bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: simple.h:155
word32
unsigned int word32
32-bit unsigned datatype
Definition: config_int.h:62
lword
word64 lword
Large word type.
Definition: config_int.h:158
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
g_nullNameValuePairs
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Definition: cryptlib.h:529
filters.h
Implementation of BufferedTransformation's attachment interface.
gf2_32.h
Classes and functions for schemes over GF(2^32)
misc.h
Utility functions for the Crypto++ library.
mqueue.h
Classes for an unlimited queue to store messages.
CryptoPP
Crypto++ library namespace.
secblock.h
Classes and functions for secure memory allocations.
stdcpp.h
Common C++ header files.