Crypto++  8.8
Free C++ class library of cryptographic schemes
ida.h
Go to the documentation of this file.
1 // ida.h - originally 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 "gf2_32.h"
15 #include "stdcpp.h"
16 #include "misc.h"
17 
18 NAMESPACE_BEGIN(CryptoPP)
19 
20 /// \brief Secret sharing and information dispersal base class
21 /// \since Crypto++ 1.0
23 {
24 public:
25  RawIDA(BufferedTransformation *attachment=NULLPTR)
26  : m_channelsReady(0), m_channelsFinished(0), m_threshold (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  std::vector<SecBlock<word32> > m_v;
61  SecBlock<word32> m_u, m_w, m_y;
62  const GF2_32 m_gf32;
63  unsigned int m_channelsReady, m_channelsFinished;
64  int m_threshold;
65 };
66 
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=NULLPTR, 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 /// \brief Shamir's Secret Sharing Algorithm
93 /// \details SecretSharing is a variant of Shamir's secret sharing algorithm
94 /// \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
95 /// \since Crypto++ 1.0
96 class SecretRecovery : public RawIDA
97 {
98 public:
99  /// \brief Construct a SecretRecovery
100  SecretRecovery(int threshold, BufferedTransformation *attachment=NULLPTR, bool removePadding=true)
101  : RawIDA(attachment)
102  {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
103 
104  void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
105 
106 protected:
107  void FlushOutputQueues();
108  void OutputMessageEnds();
109 
110  bool m_pad;
111 };
112 
113 /// a variant of Rabin's Information Dispersal Algorithm
114 
115 /// \brief Rabin's Information Dispersal Algorithm
116 /// \details InformationDispersal is a variant of Rabin's information dispersal algorithm
117 /// \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
118 /// \since Crypto++ 1.0
120 {
121 public:
122  /// \brief Construct a InformationDispersal
123  InformationDispersal(int threshold, int nShares, BufferedTransformation *attachment=NULLPTR, bool addPadding=true)
124  : m_ida(new OutputProxy(*this, true)), m_pad(false), m_nextChannel(0)
125  {
126  Detach(attachment);
127  IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
128  }
129 
131  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
132  bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
133 
134 protected:
135  RawIDA m_ida;
136  bool m_pad;
137  unsigned int m_nextChannel;
138 };
139 
140 /// \brief Rabin's Information Dispersal Algorithm
141 /// \details InformationDispersal is a variant of Rabin's information dispersal algorithm
142 /// \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
143 /// \since Crypto++ 1.0
145 {
146 public:
147  /// \brief Construct a InformationRecovery
148  InformationRecovery(int threshold, BufferedTransformation *attachment=NULLPTR, bool removePadding=true)
149  : RawIDA(attachment), m_pad(false)
150  {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
151 
152  void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
153 
154 protected:
155  void FlushOutputQueues();
156  void OutputMessageEnds();
157 
158  bool m_pad;
159  ByteQueue m_queue;
160 };
161 
162 class PaddingRemover : public Unflushable<Filter>
163 {
164 public:
165  PaddingRemover(BufferedTransformation *attachment=NULLPTR)
166  : m_possiblePadding(false), m_zeroCount(0) {Detach(attachment);}
167 
168  void IsolatedInitialize(const NameValuePairs &parameters)
169  {CRYPTOPP_UNUSED(parameters); m_possiblePadding = false;}
170  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
171 
172  // GetPossiblePadding() == false at the end of a message indicates incorrect padding
173  bool GetPossiblePadding() const {return m_possiblePadding;}
174 
175 private:
176  bool m_possiblePadding;
177  lword m_zeroCount;
178 };
179 
180 NAMESPACE_END
181 
182 #endif
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:508
Classes for multiple named channels.
Provides auto signaling support.
Definition: simple.h:423
Interface for buffered transformations.
Definition: cryptlib.h:1657
Data structure used to store byte strings.
Definition: queue.h:23
Interface for custom flush signals propagation.
Definition: simple.h:262
Implementation of BufferedTransformation's attachment interface.
Definition: filters.h:36
void Detach(BufferedTransformation *newAttachment=NULL)
Replace an attached transformation.
GF(2^32) with polynomial basis.
Definition: gf2_32.h:17
a variant of Rabin's Information Dispersal Algorithm
Definition: ida.h:120
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: ida.cpp:320
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
Definition: ida.cpp:313
InformationDispersal(int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
Construct a InformationDispersal.
Definition: ida.h:123
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: ida.h:132
Rabin's Information Dispersal Algorithm.
Definition: ida.h:145
InformationRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
Construct a InformationRecovery.
Definition: ida.h:148
Multiple channels support for custom signal processing.
Definition: simple.h:316
Interface for retrieving values given their names.
Definition: cryptlib.h:327
Filter class that is a proxy for a sink.
Definition: filters.h:991
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: ida.h:168
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: ida.cpp:378
Interface for random number generators.
Definition: cryptlib.h:1440
Secret sharing and information dispersal base class.
Definition: ida.h:23
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
Shamir's Secret Sharing Algorithm.
Definition: ida.h:97
SecretRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
Construct a SecretRecovery.
Definition: ida.h:100
Shamir's Secret Sharing Algorithm.
Definition: ida.h:72
SecretSharing(RandomNumberGenerator &rng, int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
Construct a SecretSharing.
Definition: ida.h:75
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: ida.cpp:248
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: ida.h:84
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
Definition: ida.cpp:242
Base class for unflushable filters.
Definition: simple.h:134
bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: simple.h:155
unsigned int word32
32-bit unsigned datatype
Definition: config_int.h:72
word64 lword
Large word type.
Definition: config_int.h:168
Abstract base classes that provide a uniform interface to this library.
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Definition: cryptlib.h:534
Implementation of BufferedTransformation's attachment interface.
Classes and functions for schemes over GF(2^32)
Utility functions for the Crypto++ library.
Classes for an unlimited queue to store messages.
Crypto++ library namespace.
Classes and functions for secure memory allocations.
Common C++ header files.