~zooko/cryptopp/trunk

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