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