Crypto++  5.6.5
Free C++ class library of cryptographic schemes
ida.h
Go to the documentation of this file.
1 // ida.h - written and placed in the public domain by Wei Dai
2 
3 //! \file ida.h
4 //! \brief Classes for Rabin's Information Dispersal and Shamir's Secret Sharing algorithms
5 
6 #ifndef CRYPTOPP_IDA_H
7 #define CRYPTOPP_IDA_H
8 
9 #include "cryptlib.h"
10 #include "mqueue.h"
11 #include "filters.h"
12 #include "channels.h"
13 #include "secblock.h"
14 #include "stdcpp.h"
15 #include "misc.h"
16 
17 NAMESPACE_BEGIN(CryptoPP)
18 
19 //! \class RawIDA
20 //! \brief Secret sharing and information dispersal base class
21 //! \since Crypto++ 1.0
23 {
24 public:
25  RawIDA(BufferedTransformation *attachment=NULL)
26  : m_threshold (0), m_channelsReady(0), m_channelsFinished(0)
27  {Detach(attachment);}
28 
29  unsigned int GetThreshold() const {return m_threshold;}
30  void AddOutputChannel(word32 channelId);
31  void ChannelData(word32 channelId, const byte *inString, size_t length, bool messageEnd);
32  lword InputBuffered(word32 channelId) const;
33 
34  void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
35  size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
36  {
37  if (!blocking)
38  throw BlockingInputOnly("RawIDA");
39  ChannelData(StringToWord<word32>(channel), begin, length, messageEnd != 0);
40  return 0;
41  }
42 
43 protected:
44  virtual void FlushOutputQueues();
45  virtual void OutputMessageEnds();
46 
47  unsigned int InsertInputChannel(word32 channelId);
48  unsigned int LookupInputChannel(word32 channelId) const;
49  void ComputeV(unsigned int);
50  void PrepareInterpolation();
51  void ProcessInputQueues();
52 
53  typedef std::map<word32, unsigned int> InputChannelMap;
54  InputChannelMap m_inputChannelMap;
55  InputChannelMap::iterator m_lastMapPosition;
56  std::vector<MessageQueue> m_inputQueues;
57  std::vector<word32> m_inputChannelIds, m_outputChannelIds, m_outputToInput;
58  std::vector<std::string> m_outputChannelIdStrings;
59  std::vector<ByteQueue> m_outputQueues;
60  int m_threshold;
61  unsigned int m_channelsReady, m_channelsFinished;
62  std::vector<SecBlock<word32> > m_v;
63  SecBlock<word32> m_u, m_w, m_y;
64 };
65 
66 //! \class SecretSharing
67 //! \brief Shamir's Secret Sharing Algorithm
68 //! \details SecretSharing is a variant of Shamir's secret sharing algorithm
69 //! \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
70 //! \since Crypto++ 1.0
71 class SecretSharing : public CustomFlushPropagation<Filter>
72 {
73 public:
74  //! \brief Construct a SecretSharing
75  SecretSharing(RandomNumberGenerator &rng, int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
76  : m_rng(rng), m_ida(new OutputProxy(*this, true))
77  {
78  Detach(attachment);
79  IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
80  }
81 
83  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
84  bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
85 
86 protected:
87  RandomNumberGenerator &m_rng;
88  RawIDA m_ida;
89  bool m_pad;
90 };
91 
92 //! \class SecretRecovery
93 //! \brief Shamir's Secret Sharing Algorithm
94 //! \details SecretSharing is a variant of Shamir's secret sharing algorithm
95 //! \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
96 //! \since Crypto++ 1.0
97 class SecretRecovery : public RawIDA
98 {
99 public:
100  //! \brief Construct a SecretRecovery
101  SecretRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
102  : RawIDA(attachment)
103  {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
104 
106 
107 protected:
108  void FlushOutputQueues();
109  void OutputMessageEnds();
110 
111  bool m_pad;
112 };
113 
114 /// a variant of Rabin's Information Dispersal Algorithm
115 
116 //! \class InformationDispersal
117 //! \brief Rabin's Information Dispersal Algorithm
118 //! \details InformationDispersal is a variant of Rabin's information dispersal algorithm
119 //! \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
120 //! \since Crypto++ 1.0
122 {
123 public:
124  //! \brief Construct a InformationDispersal
125  InformationDispersal(int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
126  : m_ida(new OutputProxy(*this, true)), m_pad(false), m_nextChannel(0)
127  {
128  Detach(attachment);
129  IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
130  }
131 
133  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
134  bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
135 
136 protected:
137  RawIDA m_ida;
138  bool m_pad;
139  unsigned int m_nextChannel;
140 };
141 
142 //! \class InformationRecovery
143 //! \brief Rabin's Information Dispersal Algorithm
144 //! \details InformationDispersal is a variant of Rabin's information dispersal algorithm
145 //! \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
146 //! \since Crypto++ 1.0
148 {
149 public:
150  //! \brief Construct a InformationRecovery
151  InformationRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
152  : RawIDA(attachment), m_pad(false)
153  {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
154 
156 
157 protected:
158  void FlushOutputQueues();
159  void OutputMessageEnds();
160 
161  bool m_pad;
162  ByteQueue m_queue;
163 };
164 
165 class PaddingRemover : public Unflushable<Filter>
166 {
167 public:
168  PaddingRemover(BufferedTransformation *attachment=NULL)
169  : m_possiblePadding(false), m_zeroCount(0) {Detach(attachment);}
170 
171  void IsolatedInitialize(const NameValuePairs &parameters)
172  {CRYPTOPP_UNUSED(parameters); m_possiblePadding = false;}
173  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
174 
175  // GetPossiblePadding() == false at the end of a message indicates incorrect padding
176  bool GetPossiblePadding() const {return m_possiblePadding;}
177 
178 private:
179  bool m_possiblePadding;
180  lword m_zeroCount;
181 };
182 
183 NAMESPACE_END
184 
185 #endif
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: ida.h:171
Utility functions for the Crypto++ library.
Secret sharing and information dispersal base class.
Definition: ida.h:22
Filter class that is a proxy for a sink.
Definition: filters.h:903
Abstract base classes that provide a uniform interface to this library.
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
Interface for random number generators.
Definition: cryptlib.h:1188
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: ida.h:84
Interface for buffered transformations.
Definition: cryptlib.h:1352
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
Interface for custom flush signals propagation.
Definition: simple.h:159
Classes for multiple named channels.
Classes and functions for secure memory allocations.
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
void Detach(BufferedTransformation *newAttachment=NULL)
Replace an attached transformation.
Definition: filters.cpp:50
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:500
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
SecretSharing(RandomNumberGenerator &rng, int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
Construct a SecretSharing.
Definition: ida.h:75
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
InformationDispersal(int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
Construct a InformationDispersal.
Definition: ida.h:125
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Definition: cryptlib.cpp:76
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Data structure used to store byte strings.
Definition: queue.h:20
Implementation of BufferedTransformation's attachment interface.
Exception thrown by objects that have not implemented nonblocking input processing.
Definition: cryptlib.h:1470
Base class for unflushable filters.
Definition: simple.h:94
Rabin's Information Dispersal Algorithm.
Definition: ida.h:147
Provides auto signaling support.
Definition: simple.h:280
Implementation of BufferedTransformation's attachment interface.
Definition: filters.h:36
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
SecretRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
Construct a SecretRecovery.
Definition: ida.h:101
Crypto++ library namespace.
Shamir's Secret Sharing Algorithm.
Definition: ida.h:97
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Multiple channels support for custom signal processing.
Definition: simple.h:215
a variant of Rabin's Information Dispersal Algorithm
Definition: ida.h:121
Shamir's Secret Sharing Algorithm.
Definition: ida.h:71
InformationRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
Construct a InformationRecovery.
Definition: ida.h:151
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: ida.h:134
Interface for retrieving values given their names.
Definition: cryptlib.h:279