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