Crypto++
ida.h
1 #ifndef CRYPTOPP_IDA_H
2 #define CRYPTOPP_IDA_H
3 
4 #include "mqueue.h"
5 #include "filters.h"
6 #include "channels.h"
7 #include <map>
8 #include <vector>
9 
10 NAMESPACE_BEGIN(CryptoPP)
11 
12 /// base class for secret sharing and information dispersal
14 {
15 public:
16  RawIDA(BufferedTransformation *attachment=NULL)
17  {Detach(attachment);}
18 
19  unsigned int GetThreshold() const {return m_threshold;}
20  void AddOutputChannel(word32 channelId);
21  void ChannelData(word32 channelId, const byte *inString, size_t length, bool messageEnd);
22  lword InputBuffered(word32 channelId) const;
23 
24  void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
25  size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
26  {
27  if (!blocking)
28  throw BlockingInputOnly("RawIDA");
29  ChannelData(StringToWord<word32>(channel), begin, length, messageEnd != 0);
30  return 0;
31  }
32 
33 protected:
34  virtual void FlushOutputQueues();
35  virtual void OutputMessageEnds();
36 
37  unsigned int InsertInputChannel(word32 channelId);
38  unsigned int LookupInputChannel(word32 channelId) const;
39  void ComputeV(unsigned int);
40  void PrepareInterpolation();
41  void ProcessInputQueues();
42 
43  typedef std::map<word32, unsigned int> InputChannelMap;
44  InputChannelMap m_inputChannelMap;
45  InputChannelMap::iterator m_lastMapPosition;
46  std::vector<MessageQueue> m_inputQueues;
47  std::vector<word32> m_inputChannelIds, m_outputChannelIds, m_outputToInput;
48  std::vector<std::string> m_outputChannelIdStrings;
49  std::vector<ByteQueue> m_outputQueues;
50  int m_threshold;
51  unsigned int m_channelsReady, m_channelsFinished;
52  std::vector<SecBlock<word32> > m_v;
53  SecBlock<word32> m_u, m_w, m_y;
54 };
55 
56 /// a variant of Shamir's Secret Sharing Algorithm
57 class SecretSharing : public CustomFlushPropagation<Filter>
58 {
59 public:
60  SecretSharing(RandomNumberGenerator &rng, int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
61  : m_rng(rng), m_ida(new OutputProxy(*this, true))
62  {
63  Detach(attachment);
64  IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
65  }
66 
67  void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
68  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
69  bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
70 
71 protected:
72  RandomNumberGenerator &m_rng;
73  RawIDA m_ida;
74  bool m_pad;
75 };
76 
77 /// a variant of Shamir's Secret Sharing Algorithm
78 class SecretRecovery : public RawIDA
79 {
80 public:
81  SecretRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
82  : RawIDA(attachment)
83  {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
84 
85  void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
86 
87 protected:
88  void FlushOutputQueues();
89  void OutputMessageEnds();
90 
91  bool m_pad;
92 };
93 
94 /// a variant of Rabin's Information Dispersal Algorithm
96 {
97 public:
98  InformationDispersal(int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
99  : m_ida(new OutputProxy(*this, true))
100  {
101  Detach(attachment);
102  IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
103  }
104 
105  void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
106  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
107  bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
108 
109 protected:
110  RawIDA m_ida;
111  bool m_pad;
112  unsigned int m_nextChannel;
113 };
114 
115 /// a variant of Rabin's Information Dispersal Algorithm
117 {
118 public:
119  InformationRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
120  : RawIDA(attachment)
121  {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
122 
123  void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
124 
125 protected:
126  void FlushOutputQueues();
127  void OutputMessageEnds();
128 
129  bool m_pad;
130  ByteQueue m_queue;
131 };
132 
133 class PaddingRemover : public Unflushable<Filter>
134 {
135 public:
136  PaddingRemover(BufferedTransformation *attachment=NULL)
137  : m_possiblePadding(false) {Detach(attachment);}
138 
139  void IsolatedInitialize(const NameValuePairs &parameters) {m_possiblePadding = false;}
140  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
141 
142  // GetPossiblePadding() == false at the end of a message indicates incorrect padding
143  bool GetPossiblePadding() const {return m_possiblePadding;}
144 
145 private:
146  bool m_possiblePadding;
147  lword m_zeroCount;
148 };
149 
150 NAMESPACE_END
151 
152 #endif
base class for secret sharing and information dispersal
Definition: ida.h:13
interface for random number generators
Definition: cryptlib.h:668
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
flush buffered input and/or output
Definition: ida.h:69
interface for buffered transformations
Definition: cryptlib.h:770
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
input multiple bytes for blocking or non-blocking processing
void Detach(BufferedTransformation *newAttachment=NULL)
delete the current attachment chain and replace it with newAttachment
const NameValuePairs & g_nullNameValuePairs
empty set of name-value pairs
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
input multiple bytes for blocking or non-blocking processing
Byte Queue.
Definition: queue.h:16
thrown by objects that have not implemented nonblocking input processing
Definition: cryptlib.h:820
a variant of Rabin's Information Dispersal Algorithm
Definition: ida.h:116
provides an implementation of BufferedTransformation's attachment interface
Definition: filters.h:17
a variant of Shamir's Secret Sharing Algorithm
Definition: ida.h:78
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
input multiple bytes for blocking or non-blocking processing
a variant of Rabin's Information Dispersal Algorithm
Definition: ida.h:95
a variant of Shamir's Secret Sharing Algorithm
Definition: ida.h:57
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
flush buffered input and/or output
Definition: ida.h:107
interface for retrieving values given their names
Definition: cryptlib.h:224