Crypto++
ida.h
00001 #ifndef CRYPTOPP_IDA_H
00002 #define CRYPTOPP_IDA_H
00003 
00004 #include "mqueue.h"
00005 #include "filters.h"
00006 #include "channels.h"
00007 #include <map>
00008 #include <vector>
00009 
00010 NAMESPACE_BEGIN(CryptoPP)
00011 
00012 /// base class for secret sharing and information dispersal
00013 class RawIDA : public AutoSignaling<Unflushable<Multichannel<Filter> > >
00014 {
00015 public:
00016     RawIDA(BufferedTransformation *attachment=NULL)
00017         {Detach(attachment);}
00018 
00019     unsigned int GetThreshold() const {return m_threshold;}
00020     void AddOutputChannel(word32 channelId);
00021     void ChannelData(word32 channelId, const byte *inString, size_t length, bool messageEnd);
00022     lword InputBuffered(word32 channelId) const;
00023 
00024     void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
00025     size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
00026     {
00027         if (!blocking)
00028             throw BlockingInputOnly("RawIDA");
00029         ChannelData(StringToWord<word32>(channel), begin, length, messageEnd != 0);
00030         return 0;
00031     }
00032 
00033 protected:
00034     virtual void FlushOutputQueues();
00035     virtual void OutputMessageEnds();
00036 
00037     unsigned int InsertInputChannel(word32 channelId);
00038     unsigned int LookupInputChannel(word32 channelId) const;
00039     void ComputeV(unsigned int);
00040     void PrepareInterpolation();
00041     void ProcessInputQueues();
00042 
00043     typedef std::map<word32, unsigned int> InputChannelMap;
00044     InputChannelMap m_inputChannelMap;
00045     InputChannelMap::iterator m_lastMapPosition;
00046     std::vector<MessageQueue> m_inputQueues;
00047     std::vector<word32> m_inputChannelIds, m_outputChannelIds, m_outputToInput;
00048     std::vector<std::string> m_outputChannelIdStrings;
00049     std::vector<ByteQueue> m_outputQueues;
00050     int m_threshold;
00051     unsigned int m_channelsReady, m_channelsFinished;
00052     std::vector<SecBlock<word32> > m_v;
00053     SecBlock<word32> m_u, m_w, m_y;
00054 };
00055 
00056 /// a variant of Shamir's Secret Sharing Algorithm
00057 class SecretSharing : public CustomFlushPropagation<Filter>
00058 {
00059 public:
00060     SecretSharing(RandomNumberGenerator &rng, int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
00061         : m_rng(rng), m_ida(new OutputProxy(*this, true))
00062     {
00063         Detach(attachment);
00064         IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
00065     }
00066 
00067     void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
00068     size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
00069     bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
00070 
00071 protected:
00072     RandomNumberGenerator &m_rng;
00073     RawIDA m_ida;
00074     bool m_pad;
00075 };
00076 
00077 /// a variant of Shamir's Secret Sharing Algorithm
00078 class SecretRecovery : public RawIDA
00079 {
00080 public:
00081     SecretRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
00082         : RawIDA(attachment)
00083         {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
00084 
00085     void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
00086 
00087 protected:
00088     void FlushOutputQueues();
00089     void OutputMessageEnds();
00090 
00091     bool m_pad;
00092 };
00093 
00094 /// a variant of Rabin's Information Dispersal Algorithm
00095 class InformationDispersal : public CustomFlushPropagation<Filter>
00096 {
00097 public:
00098     InformationDispersal(int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
00099         : m_ida(new OutputProxy(*this, true))
00100     {
00101         Detach(attachment);
00102         IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
00103     }
00104 
00105     void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
00106     size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
00107     bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
00108 
00109 protected:
00110     RawIDA m_ida;
00111     bool m_pad;
00112     unsigned int m_nextChannel;
00113 };
00114 
00115 /// a variant of Rabin's Information Dispersal Algorithm
00116 class InformationRecovery : public RawIDA
00117 {
00118 public:
00119     InformationRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
00120         : RawIDA(attachment)
00121         {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
00122 
00123     void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
00124 
00125 protected:
00126     void FlushOutputQueues();
00127     void OutputMessageEnds();
00128 
00129     bool m_pad;
00130     ByteQueue m_queue;
00131 };
00132 
00133 class PaddingRemover : public Unflushable<Filter>
00134 {
00135 public:
00136     PaddingRemover(BufferedTransformation *attachment=NULL)
00137         : m_possiblePadding(false) {Detach(attachment);}
00138 
00139     void IsolatedInitialize(const NameValuePairs &parameters) {m_possiblePadding = false;}
00140     size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
00141 
00142     // GetPossiblePadding() == false at the end of a message indicates incorrect padding
00143     bool GetPossiblePadding() const {return m_possiblePadding;}
00144 
00145 private:
00146     bool m_possiblePadding;
00147     lword m_zeroCount;
00148 };
00149 
00150 NAMESPACE_END
00151 
00152 #endif