ida.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // ida.h - originally written and placed in the public domain by Wei Dai
  2. /// \file ida.h
  3. /// \brief Classes for Rabin's Information Dispersal and Shamir's Secret Sharing algorithms
  4. #ifndef CRYPTOPP_IDA_H
  5. #define CRYPTOPP_IDA_H
  6. #include "cryptlib.h"
  7. #include "mqueue.h"
  8. #include "filters.h"
  9. #include "channels.h"
  10. #include "secblock.h"
  11. #include "gf2_32.h"
  12. #include "stdcpp.h"
  13. #include "misc.h"
  14. NAMESPACE_BEGIN(CryptoPP)
  15. /// \brief Secret sharing and information dispersal base class
  16. /// \since Crypto++ 1.0
  17. class RawIDA : public AutoSignaling<Unflushable<Multichannel<Filter> > >
  18. {
  19. public:
  20. RawIDA(BufferedTransformation *attachment=NULLPTR)
  21. : m_channelsReady(0), m_channelsFinished(0), m_threshold (0)
  22. {Detach(attachment);}
  23. unsigned int GetThreshold() const {return m_threshold;}
  24. void AddOutputChannel(word32 channelId);
  25. void ChannelData(word32 channelId, const byte *inString, size_t length, bool messageEnd);
  26. lword InputBuffered(word32 channelId) const;
  27. void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
  28. size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
  29. {
  30. if (!blocking)
  31. throw BlockingInputOnly("RawIDA");
  32. ChannelData(StringToWord<word32>(channel), begin, length, messageEnd != 0);
  33. return 0;
  34. }
  35. protected:
  36. virtual void FlushOutputQueues();
  37. virtual void OutputMessageEnds();
  38. unsigned int InsertInputChannel(word32 channelId);
  39. unsigned int LookupInputChannel(word32 channelId) const;
  40. void ComputeV(unsigned int);
  41. void PrepareInterpolation();
  42. void ProcessInputQueues();
  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. std::vector<SecBlock<word32> > m_v;
  51. SecBlock<word32> m_u, m_w, m_y;
  52. const GF2_32 m_gf32;
  53. unsigned int m_channelsReady, m_channelsFinished;
  54. int m_threshold;
  55. };
  56. /// \brief Shamir's Secret Sharing Algorithm
  57. /// \details SecretSharing is a variant of Shamir's secret sharing algorithm
  58. /// \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
  59. /// \since Crypto++ 1.0
  60. class SecretSharing : public CustomFlushPropagation<Filter>
  61. {
  62. public:
  63. /// \brief Construct a SecretSharing
  64. SecretSharing(RandomNumberGenerator &rng, int threshold, int nShares, BufferedTransformation *attachment=NULLPTR, bool addPadding=true)
  65. : m_rng(rng), m_ida(new OutputProxy(*this, true))
  66. {
  67. Detach(attachment);
  68. IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
  69. }
  70. void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
  71. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
  72. bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
  73. protected:
  74. RandomNumberGenerator &m_rng;
  75. RawIDA m_ida;
  76. bool m_pad;
  77. };
  78. /// \brief Shamir's Secret Sharing Algorithm
  79. /// \details SecretSharing is a variant of Shamir's secret sharing algorithm
  80. /// \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
  81. /// \since Crypto++ 1.0
  82. class SecretRecovery : public RawIDA
  83. {
  84. public:
  85. /// \brief Construct a SecretRecovery
  86. SecretRecovery(int threshold, BufferedTransformation *attachment=NULLPTR, bool removePadding=true)
  87. : RawIDA(attachment)
  88. {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
  89. void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
  90. protected:
  91. void FlushOutputQueues();
  92. void OutputMessageEnds();
  93. bool m_pad;
  94. };
  95. /// a variant of Rabin's Information Dispersal Algorithm
  96. /// \brief Rabin's Information Dispersal Algorithm
  97. /// \details InformationDispersal is a variant of Rabin's information dispersal algorithm
  98. /// \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
  99. /// \since Crypto++ 1.0
  100. class InformationDispersal : public CustomFlushPropagation<Filter>
  101. {
  102. public:
  103. /// \brief Construct a InformationDispersal
  104. InformationDispersal(int threshold, int nShares, BufferedTransformation *attachment=NULLPTR, bool addPadding=true)
  105. : m_ida(new OutputProxy(*this, true)), m_pad(false), m_nextChannel(0)
  106. {
  107. Detach(attachment);
  108. IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
  109. }
  110. void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
  111. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
  112. bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
  113. protected:
  114. RawIDA m_ida;
  115. bool m_pad;
  116. unsigned int m_nextChannel;
  117. };
  118. /// \brief Rabin's Information Dispersal Algorithm
  119. /// \details InformationDispersal is a variant of Rabin's information dispersal algorithm
  120. /// \sa SecretRecovery, SecretRecovery, InformationDispersal, InformationRecovery
  121. /// \since Crypto++ 1.0
  122. class InformationRecovery : public RawIDA
  123. {
  124. public:
  125. /// \brief Construct a InformationRecovery
  126. InformationRecovery(int threshold, BufferedTransformation *attachment=NULLPTR, bool removePadding=true)
  127. : RawIDA(attachment), m_pad(false)
  128. {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
  129. void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
  130. protected:
  131. void FlushOutputQueues();
  132. void OutputMessageEnds();
  133. bool m_pad;
  134. ByteQueue m_queue;
  135. };
  136. class PaddingRemover : public Unflushable<Filter>
  137. {
  138. public:
  139. PaddingRemover(BufferedTransformation *attachment=NULLPTR)
  140. : m_possiblePadding(false), m_zeroCount(0) {Detach(attachment);}
  141. void IsolatedInitialize(const NameValuePairs &parameters)
  142. {CRYPTOPP_UNUSED(parameters); m_possiblePadding = false;}
  143. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
  144. // GetPossiblePadding() == false at the end of a message indicates incorrect padding
  145. bool GetPossiblePadding() const {return m_possiblePadding;}
  146. private:
  147. bool m_possiblePadding;
  148. lword m_zeroCount;
  149. };
  150. NAMESPACE_END
  151. #endif