11 #include "polynomi.cpp" 13 ANONYMOUS_NAMESPACE_BEGIN
14 const CryptoPP::GF2_32 field;
19 #if (defined(_MSC_VER) && (_MSC_VER < 1400)) && !defined(__MWERKS__) 21 typedef std::reverse_bidirectional_iterator<const byte *, const byte> RevIt;
22 #elif defined(_RWSTD_NO_CLASS_PARTIAL_SPEC) 23 typedef std::reverse_iterator<const byte *, std::random_access_iterator_tag, const byte> RevIt;
25 typedef std::reverse_iterator<const byte *> RevIt;
30 if (!parameters.
GetIntValue(
"RecoveryThreshold", m_threshold))
35 throw InvalidArgument(
"RawIDA: RecoveryThreshold must be greater than 0");
37 m_lastMapPosition = m_inputChannelMap.end();
39 m_channelsFinished = 0;
42 m_inputQueues.reserve(m_threshold);
44 m_outputChannelIds.clear();
45 m_outputChannelIdStrings.clear();
46 m_outputQueues.clear();
48 word32 outputChannelID;
49 if (parameters.
GetValue(
"OutputChannelID", outputChannelID))
50 AddOutputChannel(outputChannelID);
55 if (nShares <= 0) {nShares = m_threshold;}
56 for (
unsigned int i=0; i< (
unsigned int)(nShares); i++)
61 unsigned int RawIDA::InsertInputChannel(word32 channelId)
63 if (m_lastMapPosition != m_inputChannelMap.end())
65 if (m_lastMapPosition->first == channelId)
68 if (m_lastMapPosition != m_inputChannelMap.end() && m_lastMapPosition->first == channelId)
71 m_lastMapPosition = m_inputChannelMap.find(channelId);
74 if (m_lastMapPosition == m_inputChannelMap.end())
76 if (m_inputChannelIds.size() == size_t(m_threshold))
79 m_lastMapPosition = m_inputChannelMap.insert(InputChannelMap::value_type(channelId, (
unsigned int)m_inputChannelIds.size())).first;
81 m_inputChannelIds.push_back(channelId);
83 if (m_inputChannelIds.size() == size_t(m_threshold))
84 PrepareInterpolation();
86 return m_lastMapPosition->second;
89 unsigned int RawIDA::LookupInputChannel(word32 channelId)
const 91 std::map<word32, unsigned int>::const_iterator it = m_inputChannelMap.find(channelId);
92 if (it == m_inputChannelMap.end())
98 void RawIDA::ChannelData(word32 channelId,
const byte *inString,
size_t length,
bool messageEnd)
100 int i = InsertInputChannel(channelId);
103 lword size = m_inputQueues[i].MaxRetrievable();
104 m_inputQueues[i].Put(inString, length);
105 if (size < 4 && size + length >= 4)
108 if (m_channelsReady ==
size_t(m_threshold))
109 ProcessInputQueues();
114 m_inputQueues[i].MessageEnd();
117 m_channelsFinished++;
118 if (m_channelsFinished ==
size_t(m_threshold))
121 for (i=0; i<m_threshold; i++)
123 ProcessInputQueues();
130 lword RawIDA::InputBuffered(word32 channelId)
const 132 int i = LookupInputChannel(channelId);
133 return i < m_threshold ? m_inputQueues[i].MaxRetrievable() : 0;
136 void RawIDA::ComputeV(
unsigned int i)
141 m_outputToInput.resize(i+1);
144 m_outputToInput[i] = LookupInputChannel(m_outputChannelIds[i]);
145 if (m_outputToInput[i] ==
size_t(m_threshold) && i *
size_t(m_threshold) <= 1000*1000)
147 m_v[i].resize(m_threshold);
148 PrepareBulkPolynomialInterpolationAt(field, m_v[i].begin(), m_outputChannelIds[i], &(m_inputChannelIds[0]), m_w.
begin(), m_threshold);
152 void RawIDA::AddOutputChannel(word32 channelId)
154 m_outputChannelIds.push_back(channelId);
155 m_outputChannelIdStrings.push_back(
WordToString(channelId));
157 if (m_inputChannelIds.size() == size_t(m_threshold))
158 ComputeV((
unsigned int)m_outputChannelIds.size() - 1);
161 void RawIDA::PrepareInterpolation()
164 PrepareBulkPolynomialInterpolation(field, m_w.
begin(), &(m_inputChannelIds[0]), (
unsigned int)(m_threshold));
165 for (
unsigned int i=0; i<m_outputChannelIds.size(); i++)
169 void RawIDA::ProcessInputQueues()
171 bool finished = (m_channelsFinished == size_t(m_threshold));
174 while (finished ? m_channelsReady > 0 : m_channelsReady ==
size_t(m_threshold))
177 for (i=0; i<size_t(m_threshold); i++)
188 for (i=0; (
unsigned int)i<m_outputChannelIds.size(); i++)
190 if (m_outputToInput[i] !=
size_t(m_threshold))
191 m_outputQueues[i].
PutWord32(m_y[m_outputToInput[i]]);
192 else if (m_v[i].size() == size_t(m_threshold))
193 m_outputQueues[i].
PutWord32(BulkPolynomialInterpolateAt(field, m_y.
begin(), m_v[i].begin(), m_threshold));
197 PrepareBulkPolynomialInterpolationAt(field, m_u.
begin(), m_outputChannelIds[i], &(m_inputChannelIds[0]), m_w.
begin(), m_threshold);
198 m_outputQueues[i].PutWord32(BulkPolynomialInterpolateAt(field, m_y.
begin(), m_u.
begin(), m_threshold));
203 if (m_outputChannelIds.size() > 0 && m_outputQueues[0].AnyRetrievable())
211 m_channelsFinished = 0;
214 std::vector<MessageQueue> inputQueues;
215 std::vector<word32> inputChannelIds;
217 inputQueues.swap(m_inputQueues);
218 inputChannelIds.swap(m_inputChannelIds);
219 m_inputChannelMap.clear();
220 m_lastMapPosition = m_inputChannelMap.end();
222 for (i=0; i<size_t(m_threshold); i++)
224 inputQueues[i].GetNextMessage();
230 void RawIDA::FlushOutputQueues()
232 for (
unsigned int i=0; i<m_outputChannelIds.size(); i++)
236 void RawIDA::OutputMessageEnds()
238 if (GetAutoSignalPropagation() != 0)
240 for (
unsigned int i=0; i<m_outputChannelIds.size(); i++)
250 m_ida.IsolatedInitialize(parameters);
253 size_t SecretSharing::Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking)
256 throw BlockingInputOnly(
"SecretSharing");
259 unsigned int threshold = m_ida.GetThreshold();
262 size_t len =
STDMIN(length, buf.size());
263 m_ida.ChannelData(0xffffffff, begin, len,
false);
264 for (
unsigned int i=0; i<threshold-1; i++)
266 m_rng.GenerateBlock(buf, len);
267 m_ida.ChannelData(i, buf, len,
false);
275 m_ida.SetAutoSignalPropagation(messageEnd-1);
279 while (m_ida.InputBuffered(0xffffffff) > 0)
282 m_ida.ChannelData(0xffffffff, NULLPTR, 0,
true);
283 for (
unsigned int i=0; i<m_ida.GetThreshold()-1; i++)
284 m_ida.ChannelData(i, NULLPTR, 0,
true);
296 void SecretRecovery::FlushOutputQueues()
304 void SecretRecovery::OutputMessageEnds()
309 m_outputQueues[0].TransferAllTo(paddingRemover);
312 if (GetAutoSignalPropagation() != 0)
322 m_ida.IsolatedInitialize(parameters);
328 throw BlockingInputOnly(
"InformationDispersal");
332 m_ida.ChannelData(m_nextChannel, begin, 1,
false);
335 if (m_nextChannel == m_ida.GetThreshold())
341 m_ida.SetAutoSignalPropagation(messageEnd-1);
344 for (word32 i=0; i<m_ida.GetThreshold(); i++)
345 m_ida.ChannelData(i, NULLPTR, 0,
true);
357 void InformationRecovery::FlushOutputQueues()
361 for (
unsigned int i=0; i<m_outputChannelIds.size(); i++)
371 void InformationRecovery::OutputMessageEnds()
376 m_queue.TransferAllTo(paddingRemover);
379 if (GetAutoSignalPropagation() != 0)
386 throw BlockingInputOnly(
"PaddingRemover");
388 const byte *
const end = begin + length;
390 if (m_possiblePadding)
392 size_t len =
FindIfNot(begin, end, byte(0)) - begin;
399 while (m_zeroCount--)
402 m_possiblePadding =
false;
405 const byte *x =
FindIfNot(RevIt(end), RevIt(begin), byte(0)).base();
406 if (x != begin && *(x-1) == 1)
409 m_possiblePadding =
true;
410 m_zeroCount = end - x;
417 m_possiblePadding =
false;
418 Output(0, begin, length, messageEnd, blocking);
int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
InputIt FindIfNot(InputIt first, InputIt last, const T &value)
Finds first element not in a range.
An invalid argument was detected.
bool AnyRetrievable() const
Determines whether bytes are ready for retrieval.
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
void resize(size_type newSize)
Change size and preserve contents.
Library configuration file.
std::string WordToString(T value, ByteOrder order=BIG_ENDIAN_ORDER)
Convert a word to a string.
void New(size_type newSize)
Change size without preserving contents.
Combines two sets of NameValuePairs.
Classes for performing mathematics over different fields.
void IsolatedInitialize(const NameValuePairs ¶meters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
Classes and functions for schemes over GF(2^32)
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
unsigned int NumberOfMessages() const
Provides the number of meesages processed by this object.
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Classes for polynomial basis and operations.
Redirect input to another BufferedTransformation without owning it.
Data structure used to store byte strings.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
void IsolatedInitialize(const NameValuePairs ¶meters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
void IsolatedInitialize(const NameValuePairs ¶meters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
Crypto++ library namespace.
bool GetValue(const char *name, T &value) const
Get a named value.
bool GetIntValue(const char *name, int &value) const
Get a named value with type int.
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Classes for Rabin's Information Dispersal and Shamir's Secret Sharing algorithms. ...
Interface for retrieving values given their names.