Security Scol plugin
simple.h
Go to the documentation of this file.
1// simple.h - originally written and placed in the public domain by Wei Dai
2
5
6#ifndef CRYPTOPP_SIMPLE_H
7#define CRYPTOPP_SIMPLE_H
8
9#include "config.h"
10
11#if CRYPTOPP_MSC_VERSION
12# pragma warning(push)
13# pragma warning(disable: 4127 4189)
14#endif
15
16#include "cryptlib.h"
17#include "misc.h"
18
19NAMESPACE_BEGIN(CryptoPP)
20
21
24template <class DERIVED, class BASE>
25class CRYPTOPP_NO_VTABLE ClonableImpl : public BASE
26{
27public:
31 Clonable * Clone() const {return new DERIVED(*static_cast<const DERIVED *>(this));}
32};
33
38template <class BASE, class ALGORITHM_INFO=BASE>
39class CRYPTOPP_NO_VTABLE AlgorithmImpl : public BASE
40{
41public:
46 static std::string CRYPTOPP_API StaticAlgorithmName() {return ALGORITHM_INFO::StaticAlgorithmName();}
51 std::string AlgorithmName() const {return ALGORITHM_INFO::StaticAlgorithmName();}
52};
53
55class CRYPTOPP_DLL InvalidKeyLength : public InvalidArgument
56{
57public:
61 explicit InvalidKeyLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid key length") {}
62};
63
65class CRYPTOPP_DLL InvalidRounds : public InvalidArgument
66{
67public:
71 explicit InvalidRounds(const std::string &algorithm, unsigned int rounds) : InvalidArgument(algorithm + ": " + IntToString(rounds) + " is not a valid number of rounds") {}
72};
73
75class CRYPTOPP_DLL InvalidBlockSize : public InvalidArgument
76{
77public:
81 explicit InvalidBlockSize(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid block size") {}
82};
83
85class CRYPTOPP_DLL InvalidDerivedKeyLength : public InvalidArgument
86{
87public:
91 explicit InvalidDerivedKeyLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid derived key length") {}
92};
93
96{
97public:
101 explicit InvalidPersonalizationLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid salt length") {}
102};
103
105class CRYPTOPP_DLL InvalidSaltLength : public InvalidArgument
106{
107public:
111 explicit InvalidSaltLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid salt length") {}
112};
113
114// *****************************
115
118template <class T>
119class CRYPTOPP_NO_VTABLE Bufferless : public T
120{
121public:
126 bool IsolatedFlush(bool hardFlush, bool blocking)
127 {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); return false;}
128};
129
132template <class T>
133class CRYPTOPP_NO_VTABLE Unflushable : public T
134{
135public:
155 bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
156 {return ChannelFlush(DEFAULT_CHANNEL, completeFlush, propagation, blocking);}
157
162 bool IsolatedFlush(bool hardFlush, bool blocking)
163 {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); CRYPTOPP_ASSERT(false); return false;}
164
175 bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
176 {
177 if (hardFlush && !InputBufferIsEmpty())
178 throw CannotFlush("Unflushable<T>: this object has buffered input that cannot be flushed");
179 else
180 {
181 BufferedTransformation *attached = this->AttachedTransformation();
182 return attached && propagation ? attached->ChannelFlush(channel, hardFlush, propagation-1, blocking) : false;
183 }
184 }
185
186protected:
187 virtual bool InputBufferIsEmpty() const {return false;}
188};
189
193template <class T>
194class CRYPTOPP_NO_VTABLE InputRejecting : public T
195{
196public:
198 {InputRejected() : NotImplemented("BufferedTransformation: this object doesn't allow input") {}};
199
201
202
211 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
212 {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
214
216
217
222 bool IsolatedFlush(bool hardFlush, bool blocking)
223 {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); return false;}
224
229 bool IsolatedMessageSeriesEnd(bool blocking)
230 {CRYPTOPP_UNUSED(blocking); throw InputRejected();}
231
239 size_t ChannelPut2(const std::string &channel, const byte *inString, size_t length, int messageEnd, bool blocking)
240 {CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
241 CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
242
253 bool ChannelMessageSeriesEnd(const std::string& channel, int messageEnd, bool blocking)
254 {CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
256};
257
260template <class T>
261class CRYPTOPP_NO_VTABLE CustomFlushPropagation : public T
262{
263public:
265
266
281 virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) =0;
282
284
285private:
286 bool IsolatedFlush(bool hardFlush, bool blocking)
287 {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); CRYPTOPP_ASSERT(false); return false;}
288};
289
292template <class T>
293class CRYPTOPP_NO_VTABLE CustomSignalPropagation : public CustomFlushPropagation<T>
294{
295public:
304 virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1) =0;
305
306private:
307 void IsolatedInitialize(const NameValuePairs &parameters)
308 {CRYPTOPP_UNUSED(parameters); CRYPTOPP_ASSERT(false);}
309};
310
314template <class T>
315class CRYPTOPP_NO_VTABLE Multichannel : public CustomFlushPropagation<T>
316{
317public:
318 bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
319 {return this->ChannelFlush(DEFAULT_CHANNEL, hardFlush, propagation, blocking);}
320
329 bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
330 {return this->ChannelMessageSeriesEnd(DEFAULT_CHANNEL, propagation, blocking);}
331
341 byte * CreatePutSpace(size_t &size)
342 {return this->ChannelCreatePutSpace(DEFAULT_CHANNEL, size);}
343
351 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
352 {return this->ChannelPut2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
353
361 size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
362 {return this->ChannelPutModifiable2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
363
364 // void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1)
365 // {PropagateMessageSeriesEnd(propagation, channel);}
366
378 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
379 {CRYPTOPP_UNUSED(channel); size = 0; return NULLPTR;}
380
386 bool ChannelPutModifiable(const std::string &channel, byte *inString, size_t length)
387 {this->ChannelPut(channel, inString, length); return false;}
388
396 virtual size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) =0;
397
405 size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
406 {return ChannelPut2(channel, begin, length, messageEnd, blocking);}
407
416 virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true) =0;
417};
418
421template <class T>
422class CRYPTOPP_NO_VTABLE AutoSignaling : public T
423{
424public:
427 AutoSignaling(int propagation=-1) : m_autoSignalPropagation(propagation) {}
428
433 void SetAutoSignalPropagation(int propagation)
434 {m_autoSignalPropagation = propagation;}
435
440 {return m_autoSignalPropagation;}
441
442private:
443 int m_autoSignalPropagation;
444};
445
447class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Store : public AutoSignaling<InputRejecting<BufferedTransformation> >
448{
449public:
451 Store() : m_messageEnd(false) {}
452
453 void IsolatedInitialize(const NameValuePairs &parameters)
454 {
455 m_messageEnd = false;
456 StoreInitialize(parameters);
457 }
458
459 unsigned int NumberOfMessages() const {return m_messageEnd ? 0 : 1;}
460 bool GetNextMessage();
461 unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
462
463protected:
464 virtual void StoreInitialize(const NameValuePairs &parameters) =0;
465
466 bool m_messageEnd;
467};
468
478class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Sink : public BufferedTransformation
479{
480public:
481 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
482 {CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(transferBytes); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); transferBytes = 0; return 0;}
483 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
484 {CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(begin); CRYPTOPP_UNUSED(end); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); return 0;}
485};
486
490class CRYPTOPP_DLL BitBucket : public Bufferless<Sink>
491{
492public:
493 std::string AlgorithmName() const {return "BitBucket";}
495 {CRYPTOPP_UNUSED(params);}
496 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
497 {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); return 0;}
498};
499
500NAMESPACE_END
501
502#if CRYPTOPP_MSC_VERSION
503# pragma warning(pop)
504#endif
505
506#endif
Base class information.
Definition simple.h:40
static std::string CRYPTOPP_API StaticAlgorithmName()
The algorithm name.
Definition simple.h:46
std::string AlgorithmName() const
The algorithm name.
Definition simple.h:51
Provides auto signaling support.
Definition simple.h:423
int GetAutoSignalPropagation() const
Retrieve automatic signal propagation value.
Definition simple.h:439
AutoSignaling(int propagation=-1)
Construct an AutoSignaling.
Definition simple.h:427
void SetAutoSignalPropagation(int propagation)
Set propagation of automatically generated and transferred signals.
Definition simple.h:433
Acts as an input discarding Filter or Sink.
Definition simple.h:491
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition simple.h:496
void IsolatedInitialize(const NameValuePairs &params)
Initialize or reinitialize this object, without signal propagation.
Definition simple.h:494
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition simple.h:493
Interface for buffered transformations.
Definition cryptlib.h:1652
virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output on a channel.
Definition cryptlib.cpp:485
Base class for bufferless filters.
Definition simple.h:120
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
Definition simple.h:126
Flush(true) was called but it can't completely flush its buffers.
Definition cryptlib.h:243
Namespace containing NaCl library functions.
Definition cryptlib.h:585
Base class for identifying algorithm.
Definition simple.h:26
Clonable * Clone() const
Create a copy of this object.
Definition simple.h:31
Interface for custom flush signals propagation.
Definition simple.h:262
virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)=0
Flush buffered input and/or output, with signal propagation.
Interface for custom flush signals.
Definition simple.h:294
virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1)=0
Initialize or reinitialize this object, with signal propagation.
Base class for input rejecting filters.
Definition simple.h:195
bool ChannelMessageSeriesEnd(const std::string &channel, int messageEnd, bool blocking)
Marks the end of a series of messages on a channel.
Definition simple.h:253
size_t ChannelPut2(const std::string &channel, const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing on a channel.
Definition simple.h:239
bool IsolatedMessageSeriesEnd(bool blocking)
Marks the end of a series of messages, without signal propagation.
Definition simple.h:229
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input a byte array for processing.
Definition simple.h:211
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
Definition simple.h:222
An invalid argument was detected.
Definition cryptlib.h:203
Exception thrown when an invalid block size is encountered.
Definition simple.h:76
InvalidBlockSize(const std::string &algorithm, size_t length)
Construct an InvalidBlockSize.
Definition simple.h:81
Exception thrown when an invalid derived key length is encountered.
Definition simple.h:86
InvalidDerivedKeyLength(const std::string &algorithm, size_t length)
Construct an InvalidDerivedKeyLength.
Definition simple.h:91
Exception thrown when an invalid key length is encountered.
Definition simple.h:56
InvalidKeyLength(const std::string &algorithm, size_t length)
Construct an InvalidKeyLength.
Definition simple.h:61
Exception thrown when an invalid personalization string length is encountered.
Definition simple.h:96
InvalidPersonalizationLength(const std::string &algorithm, size_t length)
Construct an InvalidPersonalizationLength.
Definition simple.h:101
Exception thrown when an invalid number of rounds is encountered.
Definition simple.h:66
InvalidRounds(const std::string &algorithm, unsigned int rounds)
Construct an InvalidRounds.
Definition simple.h:71
Exception thrown when an invalid salt length is encountered.
Definition simple.h:106
InvalidSaltLength(const std::string &algorithm, size_t length)
Construct an InvalidSaltLength.
Definition simple.h:111
Multiple channels support for custom signal processing.
Definition simple.h:316
byte * CreatePutSpace(size_t &size)
Request space which can be written into by the caller.
Definition simple.h:341
virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)=0
Flush buffered input and/or output on a channel.
bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
Marks the end of a series of messages, with signal propagation.
Definition simple.h:329
size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes that may be modified by callee on a channel.
Definition simple.h:405
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition simple.h:351
virtual size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)=0
Input multiple bytes for processing on a channel.
byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
Request space which can be written into by the caller.
Definition simple.h:378
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition simple.h:318
bool ChannelPutModifiable(const std::string &channel, byte *inString, size_t length)
Input multiple bytes that may be modified by callee on a channel.
Definition simple.h:386
size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes that may be modified by callee.
Definition simple.h:361
Interface for retrieving values given their names.
Definition cryptlib.h:322
A method was called which was not implemented.
Definition cryptlib.h:233
Implementation of BufferedTransformation's attachment interface.
Definition simple.h:479
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes from this object to another BufferedTransformation.
Definition simple.h:481
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
Copy bytes from this object to another BufferedTransformation.
Definition simple.h:483
Acts as a Source for pre-existing, static data.
Definition simple.h:448
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition simple.h:453
Store()
Construct a Store.
Definition simple.h:451
unsigned int NumberOfMessages() const
Provides the number of meesages processed by this object.
Definition simple.h:459
Base class for unflushable filters.
Definition simple.h:134
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
Definition simple.h:162
bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output on a channel.
Definition simple.h:175
bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition simple.h:155
Library configuration file.
const lword LWORD_MAX
Large word type max value.
Definition config_int.h:164
word64 lword
Large word type.
Definition config_int.h:158
Abstract base classes that provide a uniform interface to this library.
Utility functions for the Crypto++ library.
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition misc.h:724