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