Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.8 KiB

  1. #ifndef CRYPTOPP_IDA_H
  2. #define CRYPTOPP_IDA_H
  3. #include "mqueue.h"
  4. #include "filters.h"
  5. #include "channels.h"
  6. #include <map>
  7. #include <vector>
  8. NAMESPACE_BEGIN(CryptoPP)
  9. /// base class for secret sharing and information dispersal
  10. class RawIDA : public AutoSignaling<Unflushable<Multichannel<Filter> > >
  11. {
  12. public:
  13. RawIDA(BufferedTransformation *attachment=NULL)
  14. {Detach(attachment);}
  15. unsigned int GetThreshold() const {return m_threshold;}
  16. void AddOutputChannel(word32 channelId);
  17. void ChannelData(word32 channelId, const byte *inString, size_t length, bool messageEnd);
  18. lword InputBuffered(word32 channelId) const;
  19. void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
  20. size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
  21. {
  22. if (!blocking)
  23. throw BlockingInputOnly("RawIDA");
  24. ChannelData(StringToWord<word32>(channel), begin, length, messageEnd != 0);
  25. return 0;
  26. }
  27. protected:
  28. virtual void FlushOutputQueues();
  29. virtual void OutputMessageEnds();
  30. unsigned int InsertInputChannel(word32 channelId);
  31. unsigned int LookupInputChannel(word32 channelId) const;
  32. void ComputeV(unsigned int);
  33. void PrepareInterpolation();
  34. void ProcessInputQueues();
  35. typedef std::map<word32, unsigned int> InputChannelMap;
  36. InputChannelMap m_inputChannelMap;
  37. InputChannelMap::iterator m_lastMapPosition;
  38. std::vector<MessageQueue> m_inputQueues;
  39. std::vector<word32> m_inputChannelIds, m_outputChannelIds, m_outputToInput;
  40. std::vector<std::string> m_outputChannelIdStrings;
  41. std::vector<ByteQueue> m_outputQueues;
  42. int m_threshold;
  43. unsigned int m_channelsReady, m_channelsFinished;
  44. std::vector<SecBlock<word32> > m_v;
  45. SecBlock<word32> m_u, m_w, m_y;
  46. };
  47. /// a variant of Shamir's Secret Sharing Algorithm
  48. class SecretSharing : public CustomFlushPropagation<Filter>
  49. {
  50. public:
  51. SecretSharing(RandomNumberGenerator &rng, int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
  52. : m_rng(rng), m_ida(new OutputProxy(*this, true))
  53. {
  54. Detach(attachment);
  55. IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
  56. }
  57. void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
  58. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
  59. bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
  60. protected:
  61. RandomNumberGenerator &m_rng;
  62. RawIDA m_ida;
  63. bool m_pad;
  64. };
  65. /// a variant of Shamir's Secret Sharing Algorithm
  66. class SecretRecovery : public RawIDA
  67. {
  68. public:
  69. SecretRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
  70. : RawIDA(attachment)
  71. {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
  72. void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
  73. protected:
  74. void FlushOutputQueues();
  75. void OutputMessageEnds();
  76. bool m_pad;
  77. };
  78. /// a variant of Rabin's Information Dispersal Algorithm
  79. class InformationDispersal : public CustomFlushPropagation<Filter>
  80. {
  81. public:
  82. InformationDispersal(int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
  83. : m_ida(new OutputProxy(*this, true))
  84. {
  85. Detach(attachment);
  86. IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
  87. }
  88. void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
  89. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
  90. bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
  91. protected:
  92. RawIDA m_ida;
  93. bool m_pad;
  94. unsigned int m_nextChannel;
  95. };
  96. /// a variant of Rabin's Information Dispersal Algorithm
  97. class InformationRecovery : public RawIDA
  98. {
  99. public:
  100. InformationRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
  101. : RawIDA(attachment)
  102. {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
  103. void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
  104. protected:
  105. void FlushOutputQueues();
  106. void OutputMessageEnds();
  107. bool m_pad;
  108. ByteQueue m_queue;
  109. };
  110. class PaddingRemover : public Unflushable<Filter>
  111. {
  112. public:
  113. PaddingRemover(BufferedTransformation *attachment=NULL)
  114. : m_possiblePadding(false) {Detach(attachment);}
  115. void IsolatedInitialize(const NameValuePairs &parameters) {m_possiblePadding = false;}
  116. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
  117. // GetPossiblePadding() == false at the end of a message indicates incorrect padding
  118. bool GetPossiblePadding() const {return m_possiblePadding;}
  119. private:
  120. bool m_possiblePadding;
  121. lword m_zeroCount;
  122. };
  123. NAMESPACE_END
  124. #endif