monotone

monotone Mtn Source Tree

Root/cryptopp/simple.h

1// simple.h - written and placed in the public domain by Wei Dai
2/*! \file
3 Simple non-interface classes derived from classes in cryptlib.h.
4*/
5
6#ifndef CRYPTOPP_SIMPLE_H
7#define CRYPTOPP_SIMPLE_H
8
9#include "cryptlib.h"
10#include "misc.h"
11
12NAMESPACE_BEGIN(CryptoPP)
13
14template <class BASE, class ALGORITHM_INFO = BASE>
15class AlgorithmImpl : public BASE
16{
17public:
18std::string AlgorithmName() const {return ALGORITHM_INFO::StaticAlgorithmName();}
19};
20
21//! .
22class InvalidKeyLength : public InvalidArgument
23{
24public:
25explicit InvalidKeyLength(const std::string &algorithm, unsigned int length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid key length") {}
26};
27
28//! .
29class InvalidRounds : public InvalidArgument
30{
31public:
32explicit InvalidRounds(const std::string &algorithm, unsigned int rounds) : InvalidArgument(algorithm + ": " + IntToString(rounds) + " is not a valid number of rounds") {}
33};
34
35class HashTransformationWithDefaultTruncation : public HashTransformation
36{
37public:
38virtual void Final(byte *digest) =0;
39void TruncatedFinal(byte *digest, unsigned int digestSize);
40};
41
42//! .
43// TODO: look into this virtual inheritance
44class ASN1CryptoMaterial : virtual public ASN1Object, virtual public CryptoMaterial
45{
46public:
47void Save(BufferedTransformation &bt) const
48{BEREncode(bt);}
49void Load(BufferedTransformation &bt)
50{BERDecode(bt);}
51};
52
53// *****************************
54
55template <class T>
56class Bufferless : public T
57{
58public:
59Bufferless() {}
60Bufferless(BufferedTransformation *q) : T(q) {}
61bool IsolatedFlush(bool hardFlush, bool blocking) {return false;}
62};
63
64template <class T>
65class Unflushable : public T
66{
67public:
68Unflushable() {}
69Unflushable(BufferedTransformation *q) : T(q) {}
70bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
71{return ChannelFlush(NULL_CHANNEL, completeFlush, propagation);}
72bool IsolatedFlush(bool hardFlush, bool blocking)
73{assert(false); return false;}
74bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
75{
76if (hardFlush && !InputBufferIsEmpty())
77throw CannotFlush("Unflushable<T>: this object has buffered input that cannot be flushed");
78else
79{
80BufferedTransformation *attached = AttachedTransformation();
81return attached && propagation ? attached->ChannelFlush(channel, hardFlush, propagation-1, blocking) : false;
82}
83}
84
85protected:
86virtual bool InputBufferIsEmpty() const {return false;}
87};
88
89template <class T>
90class InputRejecting : public T
91{
92public:
93InputRejecting() {}
94InputRejecting(BufferedTransformation *q) : T(q) {}
95
96protected:
97struct InputRejected : public NotImplemented
98{InputRejected() : NotImplemented("BufferedTransformation: this object doesn't allow input") {}};
99
100// shouldn't be calling these functions on this class
101unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
102{throw InputRejected();}
103bool IsolatedFlush(bool, bool) {return false;}
104bool IsolatedMessageSeriesEnd(bool) {throw InputRejected();}
105
106unsigned int ChannelPut2(const std::string &channel, const byte *begin, unsigned int length, int messageEnd, bool blocking)
107{throw InputRejected();}
108bool ChannelMessageSeriesEnd(const std::string &, int, bool) {throw InputRejected();}
109};
110
111template <class T>
112class CustomSignalPropagation : public T
113{
114public:
115CustomSignalPropagation() {}
116CustomSignalPropagation(BufferedTransformation *q) : T(q) {}
117
118virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1) =0;
119virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) =0;
120
121private:
122void IsolatedInitialize(const NameValuePairs &parameters) {assert(false);}
123bool IsolatedFlush(bool hardFlush, bool blocking) {assert(false); return false;}
124};
125
126template <class T>
127class Multichannel : public CustomSignalPropagation<T>
128{
129public:
130Multichannel() {}
131Multichannel(BufferedTransformation *q) : CustomSignalPropagation<T>(q) {}
132
133void Initialize(const NameValuePairs &parameters, int propagation)
134{ChannelInitialize(NULL_CHANNEL, parameters, propagation);}
135bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
136{return ChannelFlush(NULL_CHANNEL, hardFlush, propagation, blocking);}
137bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
138{return ChannelMessageSeriesEnd(NULL_CHANNEL, propagation, blocking);}
139byte * CreatePutSpace(unsigned int &size)
140{return ChannelCreatePutSpace(NULL_CHANNEL, size);}
141unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
142{return ChannelPut2(NULL_CHANNEL, begin, length, messageEnd, blocking);}
143unsigned int PutModifiable2(byte *inString, unsigned int length, int messageEnd, bool blocking)
144{return ChannelPutModifiable2(NULL_CHANNEL, inString, length, messageEnd, blocking);}
145
146//void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1)
147//{PropagateMessageSeriesEnd(propagation, channel);}
148byte * ChannelCreatePutSpace(const std::string &channel, unsigned int &size)
149{size = 0; return NULL;}
150bool ChannelPutModifiable(const std::string &channel, byte *inString, unsigned int length)
151{ChannelPut(channel, inString, length); return false;}
152
153virtual unsigned int ChannelPut2(const std::string &channel, const byte *begin, unsigned int length, int messageEnd, bool blocking) =0;
154unsigned int ChannelPutModifiable2(const std::string &channel, byte *begin, unsigned int length, int messageEnd, bool blocking)
155{return ChannelPut2(channel, begin, length, messageEnd, blocking);}
156
157virtual void ChannelInitialize(const std::string &channel, const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1) =0;
158virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true) =0;
159};
160
161template <class T>
162class AutoSignaling : public T
163{
164public:
165AutoSignaling(int propagation=-1) : m_autoSignalPropagation(propagation) {}
166AutoSignaling(BufferedTransformation *q, int propagation=-1) : T(q), m_autoSignalPropagation(propagation) {}
167
168void SetAutoSignalPropagation(int propagation)
169{m_autoSignalPropagation = propagation;}
170int GetAutoSignalPropagation() const
171{return m_autoSignalPropagation;}
172
173private:
174int m_autoSignalPropagation;
175};
176
177//! A BufferedTransformation that only contains pre-existing data as "output"
178class Store : public AutoSignaling<InputRejecting<BufferedTransformation> >
179{
180public:
181Store() : m_messageEnd(false) {}
182
183void IsolatedInitialize(const NameValuePairs &parameters)
184{
185m_messageEnd = false;
186StoreInitialize(parameters);
187}
188
189unsigned int NumberOfMessages() const {return m_messageEnd ? 0 : 1;}
190bool GetNextMessage();
191unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=NULL_CHANNEL) const;
192
193protected:
194virtual void StoreInitialize(const NameValuePairs &parameters) =0;
195
196bool m_messageEnd;
197};
198
199//! A BufferedTransformation that doesn't produce any retrievable output
200class Sink : public BufferedTransformation
201{
202protected:
203// make these functions protected to help prevent unintentional calls to them
204BufferedTransformation::Get;
205BufferedTransformation::Peek;
206BufferedTransformation::TransferTo;
207BufferedTransformation::CopyTo;
208BufferedTransformation::CopyRangeTo;
209BufferedTransformation::TransferMessagesTo;
210BufferedTransformation::CopyMessagesTo;
211BufferedTransformation::TransferAllTo;
212BufferedTransformation::CopyAllTo;
213unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true)
214{transferBytes = 0; return 0;}
215unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const
216{return 0;}
217};
218
219class BitBucket : public Bufferless<Sink>
220{
221public:
222std::string AlgorithmName() const {return "BitBucket";}
223void IsolatedInitialize(const NameValuePairs &parameters) {}
224unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
225{return 0;}
226};
227
228NAMESPACE_END
229
230#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status