10#include "polynomi.cpp"
14#if (defined(CRYPTOPP_MSC_VERSION) && (CRYPTOPP_MSC_VERSION < 1400)) && !defined(__MWERKS__)
16 typedef std::reverse_bidirectional_iterator<const byte *, const byte> RevIt;
17#elif defined(_RWSTD_NO_CLASS_PARTIAL_SPEC)
18 typedef std::reverse_iterator<const byte *, std::random_access_iterator_tag, const byte> RevIt;
20 typedef std::reverse_iterator<const byte *> RevIt;
25 if (!parameters.
GetIntValue(
"RecoveryThreshold", m_threshold))
30 throw InvalidArgument(
"RawIDA: RecoveryThreshold must be greater than 0");
32 m_lastMapPosition = m_inputChannelMap.end();
34 m_channelsFinished = 0;
37 m_inputQueues.reserve(m_threshold);
39 m_outputChannelIds.clear();
40 m_outputChannelIdStrings.clear();
41 m_outputQueues.clear();
44 if (parameters.
GetValue(
"OutputChannelID", outputChannelID))
45 AddOutputChannel(outputChannelID);
50 if (nShares <= 0) {nShares = m_threshold;}
51 for (
unsigned int i=0; i< (
unsigned int)(nShares); i++)
56unsigned int RawIDA::InsertInputChannel(
word32 channelId)
58 if (m_lastMapPosition != m_inputChannelMap.end())
60 if (m_lastMapPosition->first == channelId)
63 if (m_lastMapPosition != m_inputChannelMap.end() && m_lastMapPosition->first == channelId)
66 m_lastMapPosition = m_inputChannelMap.find(channelId);
69 if (m_lastMapPosition == m_inputChannelMap.end())
71 if (m_inputChannelIds.size() ==
size_t(m_threshold))
74 m_lastMapPosition = m_inputChannelMap.insert(InputChannelMap::value_type(channelId, (
unsigned int)m_inputChannelIds.size())).first;
76 m_inputChannelIds.push_back(channelId);
78 if (m_inputChannelIds.size() ==
size_t(m_threshold))
79 PrepareInterpolation();
81 return m_lastMapPosition->second;
84unsigned int RawIDA::LookupInputChannel(
word32 channelId)
const
86 std::map<word32, unsigned int>::const_iterator it = m_inputChannelMap.find(channelId);
87 if (it == m_inputChannelMap.end())
93void RawIDA::ChannelData(
word32 channelId,
const byte *inString,
size_t length,
bool messageEnd)
95 int i = InsertInputChannel(channelId);
98 lword size = m_inputQueues[i].MaxRetrievable();
99 m_inputQueues[i].Put(inString, length);
100 if (size < 4 && size + length >= 4)
103 if (m_channelsReady ==
size_t(m_threshold))
104 ProcessInputQueues();
109 m_inputQueues[i].MessageEnd();
110 if (m_inputQueues[i].NumberOfMessages() == 1)
112 m_channelsFinished++;
113 if (m_channelsFinished ==
size_t(m_threshold))
116 for (i=0; i<m_threshold; i++)
117 m_channelsReady += m_inputQueues[i].AnyRetrievable();
118 ProcessInputQueues();
127 int i = LookupInputChannel(channelId);
128 return i < m_threshold ? m_inputQueues[i].MaxRetrievable() : 0;
131void RawIDA::ComputeV(
unsigned int i)
136 m_outputToInput.resize(i+1);
139 m_outputToInput[i] = LookupInputChannel(m_outputChannelIds[i]);
140 if (m_outputToInput[i] ==
size_t(m_threshold) && i *
size_t(m_threshold) <= 1000*1000)
142 m_v[i].resize(m_threshold);
143 PrepareBulkPolynomialInterpolationAt(m_gf32, m_v[i].begin(), m_outputChannelIds[i], &(m_inputChannelIds[0]), m_w.
begin(), m_threshold);
147void RawIDA::AddOutputChannel(
word32 channelId)
149 m_outputChannelIds.push_back(channelId);
150 m_outputChannelIdStrings.push_back(
WordToString(channelId));
152 if (m_inputChannelIds.size() ==
size_t(m_threshold))
153 ComputeV((
unsigned int)m_outputChannelIds.size() - 1);
156void RawIDA::PrepareInterpolation()
159 PrepareBulkPolynomialInterpolation(m_gf32, m_w.
begin(), &(m_inputChannelIds[0]), (
unsigned int)(m_threshold));
160 for (
unsigned int i=0; i<m_outputChannelIds.size(); i++)
164void RawIDA::ProcessInputQueues()
166 bool finished = (m_channelsFinished == size_t(m_threshold));
169 while (finished ? m_channelsReady > 0 : m_channelsReady == size_t(m_threshold))
172 for (i=0; i<size_t(m_threshold); i++)
183 for (i=0; (
unsigned int)i<m_outputChannelIds.size(); i++)
185 if (m_outputToInput[i] !=
size_t(m_threshold))
186 m_outputQueues[i].PutWord32(m_y[m_outputToInput[i]]);
187 else if (m_v[i].size() == size_t(m_threshold))
188 m_outputQueues[i].PutWord32(BulkPolynomialInterpolateAt(m_gf32, m_y.
begin(), m_v[i].begin(), m_threshold));
192 PrepareBulkPolynomialInterpolationAt(m_gf32, m_u.
begin(), m_outputChannelIds[i], &(m_inputChannelIds[0]), m_w.
begin(), m_threshold);
193 m_outputQueues[i].PutWord32(BulkPolynomialInterpolateAt(m_gf32, m_y.
begin(), m_u.
begin(), m_threshold));
198 if (m_outputChannelIds.size() > 0 && m_outputQueues[0].AnyRetrievable())
206 m_channelsFinished = 0;
209 std::vector<MessageQueue> inputQueues;
210 std::vector<word32> inputChannelIds;
212 inputQueues.swap(m_inputQueues);
213 inputChannelIds.swap(m_inputChannelIds);
214 m_inputChannelMap.clear();
215 m_lastMapPosition = m_inputChannelMap.end();
217 for (i=0; i<size_t(m_threshold); i++)
219 inputQueues[i].GetNextMessage();
220 inputQueues[i].TransferAllTo(*AttachedTransformation(),
WordToString(inputChannelIds[i]));
225void RawIDA::FlushOutputQueues()
227 for (
unsigned int i=0; i<m_outputChannelIds.size(); i++)
228 m_outputQueues[i].TransferAllTo(*AttachedTransformation(), m_outputChannelIdStrings[i]);
231void RawIDA::OutputMessageEnds()
235 for (
unsigned int i=0; i<m_outputChannelIds.size(); i++)
245 m_ida.IsolatedInitialize(parameters);
254 unsigned int threshold = m_ida.GetThreshold();
258 m_ida.ChannelData(0xffffffff, begin, len,
false);
259 for (
unsigned int i=0; i<threshold-1; i++)
262 m_ida.ChannelData(i, buf, len,
false);
274 while (m_ida.InputBuffered(0xffffffff) > 0)
277 m_ida.ChannelData(0xffffffff, NULLPTR, 0,
true);
278 for (
unsigned int i=0; i<m_ida.GetThreshold()-1; i++)
279 m_ida.ChannelData(i, NULLPTR, 0,
true);
285void SecretRecovery::IsolatedInitialize(
const NameValuePairs ¶meters)
291void SecretRecovery::FlushOutputQueues()
294 m_outputQueues[0].TransferTo(*AttachedTransformation(), m_outputQueues[0].MaxRetrievable()-4);
296 m_outputQueues[0].TransferTo(*AttachedTransformation());
299void SecretRecovery::OutputMessageEnds()
304 m_outputQueues[0].TransferAllTo(paddingRemover);
317 m_ida.IsolatedInitialize(parameters);
327 m_ida.ChannelData(m_nextChannel, begin, 1,
false);
330 if (m_nextChannel == m_ida.GetThreshold())
339 for (
word32 i=0; i<m_ida.GetThreshold(); i++)
340 m_ida.ChannelData(i, NULLPTR, 0,
true);
346void InformationRecovery::IsolatedInitialize(
const NameValuePairs ¶meters)
349 RawIDA::IsolatedInitialize(parameters);
352void InformationRecovery::FlushOutputQueues()
354 while (m_outputQueues[0].AnyRetrievable())
356 for (
unsigned int i=0; i<m_outputChannelIds.size(); i++)
357 m_outputQueues[i].TransferTo(m_queue, 1);
363 m_queue.
TransferTo(*AttachedTransformation());
366void InformationRecovery::OutputMessageEnds()
383 const byte *
const end = begin + length;
385 if (m_possiblePadding)
387 size_t len =
FindIfNot(begin, end,
byte(0)) - begin;
394 while (m_zeroCount--)
397 m_possiblePadding =
false;
400 const byte *x =
FindIfNot(RevIt(end), RevIt(begin),
byte(0)).base();
401 if (x != begin && *(x-1) == 1)
404 m_possiblePadding =
true;
405 m_zeroCount = end - x;
412 m_possiblePadding =
false;
413 Output(0, begin, length, messageEnd, blocking);
Classes for performing mathematics over different fields.
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
int GetAutoSignalPropagation() const
Retrieve automatic signal propagation value.
void SetAutoSignalPropagation(int propagation)
Set propagation of automatically generated and transferred signals.
Data structure used to store byte strings.
lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
Combines two sets of NameValuePairs.
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
An invalid argument was detected.
Data structure used to store messages.
bool AnyRetrievable() const
Determines whether bytes are ready for retrieval.
unsigned int NumberOfMessages() const
Provides the number of meesages processed by this object.
lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
Interface for retrieving values given their names.
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
bool GetValue(const char *name, T &value) const
Get a named value.
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
CRYPTOPP_DLL 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.
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Redirect input to another BufferedTransformation without owning it.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
void New(size_type newSize)
Change size without preserving contents.
size_type size() const
Provides the count of elements in the SecBlock.
void resize(size_type newSize)
Change size and preserve contents.
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
void IsolatedInitialize(const NameValuePairs ¶meters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
Library configuration file.
unsigned int word32
32-bit unsigned datatype
word64 lword
Large word type.
Classes for Rabin's Information Dispersal and Shamir's Secret Sharing algorithms.
std::string WordToString(T value, ByteOrder order=BIG_ENDIAN_ORDER)
Convert a word to a string.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
InputIt FindIfNot(InputIt first, InputIt last, const T &value)
Finds first element not in a range.
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be negative and incorrectly promoted.
Crypto++ library namespace.
Classes for polynomial basis and operations.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.