Fabcoin Core  0.16.2
P2P Digital Currency
strciphr.h
Go to the documentation of this file.
1 // strciphr.h - written and placed in the public domain by Wei Dai
2 
27 
28 #ifndef CRYPTOPP_STRCIPHR_H
29 #define CRYPTOPP_STRCIPHR_H
30 
31 #include "config.h"
32 
33 #if CRYPTOPP_MSC_VERSION
34 # pragma warning(push)
35 # pragma warning(disable: 4127 4189)
36 #endif
37 
38 #include "cryptlib.h"
39 #include "seckey.h"
40 #include "secblock.h"
41 #include "argnames.h"
42 
44 
45 template <class POLICY_INTERFACE, class BASE = Empty>
51 {
52 public:
53  typedef POLICY_INTERFACE PolicyInterface;
54  virtual ~AbstractPolicyHolder() {}
55 
56 protected:
57  virtual const POLICY_INTERFACE & GetPolicy() const =0;
58  virtual POLICY_INTERFACE & AccessPolicy() =0;
59 };
60 
65 template <class POLICY, class BASE, class POLICY_INTERFACE = typename BASE::PolicyInterface>
66 class ConcretePolicyHolder : public BASE, protected POLICY
67 {
68 public:
69  virtual ~ConcretePolicyHolder() {}
70 protected:
71  const POLICY_INTERFACE & GetPolicy() const {return *this;}
72  POLICY_INTERFACE & AccessPolicy() {return *this;}
73 };
74 
85 };
86 
103 
107 {
109 
114  virtual unsigned int GetAlignment() const {return 1;}
115 
119  virtual unsigned int GetBytesPerIteration() const =0;
120 
125  virtual unsigned int GetOptimalBlockSize() const {return GetBytesPerIteration();}
126 
129  virtual unsigned int GetIterationsToBuffer() const =0;
130 
135  virtual void WriteKeystream(byte *keystream, size_t iterationCount)
136  {OperateKeystream(KeystreamOperation(INPUT_NULL | (KeystreamOperationFlags)IsAlignedOn(keystream, GetAlignment())), keystream, NULL, iterationCount);}
137 
141  virtual bool CanOperateKeystream() const {return false;}
142 
151  virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
152  {CRYPTOPP_UNUSED(operation); CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(iterationCount); CRYPTOPP_ASSERT(false);}
153 
158  virtual void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length) =0;
159 
164  virtual void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
165  {CRYPTOPP_UNUSED(keystreamBuffer); CRYPTOPP_UNUSED(iv); CRYPTOPP_UNUSED(length); throw NotImplemented("SimpleKeyingInterface: this object doesn't support resynchronization");}
166 
170  virtual bool CipherIsRandomAccess() const =0;
171 
175  virtual void SeekToIteration(lword iterationCount)
176  {CRYPTOPP_UNUSED(iterationCount); CRYPTOPP_ASSERT(!CipherIsRandomAccess()); throw NotImplemented("StreamTransformation: this object doesn't support random access");}
177 };
178 
185 template <typename WT, unsigned int W, unsigned int X = 1, class BASE = AdditiveCipherAbstractPolicy>
187 {
188  typedef WT WordType;
189  CRYPTOPP_CONSTANT(BYTES_PER_ITERATION = sizeof(WordType) * W)
190 
191 #if !(CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X64)
192  unsigned int GetAlignment() const {return GetAlignmentOf<WordType>();}
197 #endif
198 
202  unsigned int GetBytesPerIteration() const {return BYTES_PER_ITERATION;}
203 
206  unsigned int GetIterationsToBuffer() const {return X;}
207 
211  bool CanOperateKeystream() const {return true;}
212 
221  virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount) =0;
222 };
223 
225 #define CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, b, i, a) \
226  PutWord(bool(x & OUTPUT_ALIGNED), b, output+i*sizeof(WordType), (x & INPUT_NULL) ? (a) : (a) ^ GetWord<WordType>(bool(x & INPUT_ALIGNED), b, input+i*sizeof(WordType)));
227 
229 #define CRYPTOPP_KEYSTREAM_OUTPUT_XMM(x, i, a) {\
230  __m128i t = (x & INPUT_NULL) ? a : _mm_xor_si128(a, (x & INPUT_ALIGNED) ? _mm_load_si128((__m128i *)input+i) : _mm_loadu_si128((__m128i *)input+i));\
231  if (x & OUTPUT_ALIGNED) _mm_store_si128((__m128i *)output+i, t);\
232  else _mm_storeu_si128((__m128i *)output+i, t);}
233 
235 #define CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(x, y) \
236  switch (operation) \
237  { \
238  case WRITE_KEYSTREAM: \
239  x(WRITE_KEYSTREAM) \
240  break; \
241  case XOR_KEYSTREAM: \
242  x(XOR_KEYSTREAM) \
243  input += y; \
244  break; \
245  case XOR_KEYSTREAM_INPUT_ALIGNED: \
246  x(XOR_KEYSTREAM_INPUT_ALIGNED) \
247  input += y; \
248  break; \
249  case XOR_KEYSTREAM_OUTPUT_ALIGNED: \
250  x(XOR_KEYSTREAM_OUTPUT_ALIGNED) \
251  input += y; \
252  break; \
253  case WRITE_KEYSTREAM_ALIGNED: \
254  x(WRITE_KEYSTREAM_ALIGNED) \
255  break; \
256  case XOR_KEYSTREAM_BOTH_ALIGNED: \
257  x(XOR_KEYSTREAM_BOTH_ALIGNED) \
258  input += y; \
259  break; \
260  } \
261  output += y;
262 
266 template <class BASE = AbstractPolicyHolder<AdditiveCipherAbstractPolicy, SymmetricCipher> >
268 {
269 public:
271 
277  void GenerateBlock(byte *output, size_t size);
278 
292  void ProcessData(byte *outString, const byte *inString, size_t length);
293 
297  void Resynchronize(const byte *iv, int length=-1);
298 
303  unsigned int OptimalBlockSize() const {return this->GetPolicy().GetOptimalBlockSize();}
304 
309  unsigned int GetOptimalNextBlockSize() const {return (unsigned int)this->m_leftOver;}
310 
314  unsigned int OptimalDataAlignment() const {return this->GetPolicy().GetAlignment();}
315 
318  bool IsSelfInverting() const {return true;}
319 
322  bool IsForwardTransformation() const {return true;}
323 
327  bool IsRandomAccess() const {return this->GetPolicy().CipherIsRandomAccess();}
328 
332  void Seek(lword position);
333 
334  typedef typename BASE::PolicyInterface PolicyInterface;
335 
336 protected:
337  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
338 
339  unsigned int GetBufferByteSize(const PolicyInterface &policy) const {return policy.GetBytesPerIteration() * policy.GetIterationsToBuffer();}
340 
341  inline byte * KeystreamBufferBegin() {return this->m_buffer.data();}
342  inline byte * KeystreamBufferEnd() {return (this->m_buffer.data() + this->m_buffer.size());}
343 
345  size_t m_leftOver;
346 };
347 
351 {
352 public:
354 
359  virtual unsigned int GetAlignment() const =0;
360 
364  virtual unsigned int GetBytesPerIteration() const =0;
365 
368  virtual byte * GetRegisterBegin() =0;
369 
371  virtual void TransformRegister() =0;
372 
375  virtual bool CanIterate() const {return false;}
376 
383  virtual void Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount)
384  {CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(dir); CRYPTOPP_UNUSED(iterationCount);
385  CRYPTOPP_ASSERT(false); /*throw 0;*/ throw Exception(Exception::OTHER_ERROR, "SimpleKeyingInterface: unexpected error");}
386 
391  virtual void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length) =0;
392 
396  virtual void CipherResynchronize(const byte *iv, size_t length)
397  {CRYPTOPP_UNUSED(iv); CRYPTOPP_UNUSED(length); throw NotImplemented("SimpleKeyingInterface: this object doesn't support resynchronization");}
398 };
399 
405 template <typename WT, unsigned int W, class BASE = CFB_CipherAbstractPolicy>
407 {
408  typedef WT WordType;
409 
414  unsigned int GetAlignment() const {return sizeof(WordType);}
415 
419  unsigned int GetBytesPerIteration() const {return sizeof(WordType) * W;}
420 
423  bool CanIterate() const {return true;}
424 
426  void TransformRegister() {this->Iterate(NULL, NULL, ENCRYPTION, 1);}
427 
433  template <class B>
435  {
436  RegisterOutput(byte *output, const byte *input, CipherDir dir)
437  : m_output(output), m_input(input), m_dir(dir) {}
438 
442  inline RegisterOutput& operator()(WordType &registerWord)
443  {
444  CRYPTOPP_ASSERT(IsAligned<WordType>(m_output));
445  CRYPTOPP_ASSERT(IsAligned<WordType>(m_input));
446 
447  if (!NativeByteOrderIs(B::ToEnum()))
448  registerWord = ByteReverse(registerWord);
449 
450  if (m_dir == ENCRYPTION)
451  {
452  if (m_input == NULL)
453  {
454  CRYPTOPP_ASSERT(m_output == NULL);
455  }
456  else
457  {
458  WordType ct = *(const WordType *)m_input ^ registerWord;
459  registerWord = ct;
460  *(WordType*)m_output = ct;
461  m_input += sizeof(WordType);
462  m_output += sizeof(WordType);
463  }
464  }
465  else
466  {
467  WordType ct = *(const WordType *)m_input;
468  *(WordType*)m_output = registerWord ^ ct;
469  registerWord = ct;
470  m_input += sizeof(WordType);
471  m_output += sizeof(WordType);
472  }
473 
474  // registerWord is left unreversed so it can be xor-ed with further input
475 
476  return *this;
477  }
478 
480  const byte *m_input;
482  };
483 };
484 
488 template <class BASE>
490 {
491 public:
505  void ProcessData(byte *outString, const byte *inString, size_t length);
506 
510  void Resynchronize(const byte *iv, int length=-1);
511 
516  unsigned int OptimalBlockSize() const {return this->GetPolicy().GetBytesPerIteration();}
517 
522  unsigned int GetOptimalNextBlockSize() const {return (unsigned int)m_leftOver;}
523 
527  unsigned int OptimalDataAlignment() const {return this->GetPolicy().GetAlignment();}
528 
532  bool IsRandomAccess() const {return false;}
533 
536  bool IsSelfInverting() const {return false;}
537 
538  typedef typename BASE::PolicyInterface PolicyInterface;
539 
540 protected:
541  virtual void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length) =0;
542 
543  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
544 
545  size_t m_leftOver;
546 };
547 
551 template <class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
553 {
554  bool IsForwardTransformation() const {return true;}
555  void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length);
556 };
557 
561 template <class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
563 {
564  bool IsForwardTransformation() const {return false;}
565  void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length);
566 };
567 
571 template <class BASE>
572 class CFB_RequireFullDataBlocks : public BASE
573 {
574 public:
575  unsigned int MandatoryBlockSize() const {return this->OptimalBlockSize();}
576 };
577 
583 template <class BASE, class INFO = BASE>
584 class SymmetricCipherFinal : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>
585 {
586 public:
588 
591 
596  {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
597 
601  SymmetricCipherFinal(const byte *key, size_t length)
602  {this->SetKey(key, length);}
603 
608  SymmetricCipherFinal(const byte *key, size_t length, const byte *iv)
609  {this->SetKeyWithIV(key, length, iv);}
610 
613  Clonable * Clone() const {return static_cast<SymmetricCipher *>(new SymmetricCipherFinal<BASE, INFO>(*this));}
614 };
615 
617 
618 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
619 #include "strciphr.cpp"
620 #endif
621 
624 CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, SymmetricCipher> >;
626 CRYPTOPP_DLL_TEMPLATE_CLASS CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >;
627 CRYPTOPP_DLL_TEMPLATE_CLASS CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >;
628 
630 
631 #if CRYPTOPP_MSC_VERSION
632 # pragma warning(pop)
633 #endif
634 
635 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:140
Standard names for retrieving values by name when working with NameValuePairs.
virtual unsigned int GetOptimalBlockSize() const
Provides number of ideal bytes to process.
Definition: strciphr.h:125
BASE::PolicyInterface PolicyInterface
Definition: strciphr.h:334
bool NativeByteOrderIs(ByteOrder order)
Determines whether order follows native byte ordering.
Definition: misc.h:993
Output buffer is aligned.
Definition: strciphr.h:80
uint8_t byte
Definition: Common.h:57
SymmetricCipherFinal(const byte *key)
Construct a stream cipher.
Definition: strciphr.h:595
Input buffer is aligned.
Definition: strciphr.h:82
virtual ~ConcretePolicyHolder()
Definition: strciphr.h:69
unsigned int GetBytesPerIteration() const
Provides number of bytes operated upon during an iteration.
Definition: strciphr.h:419
Base class for feedback based stream ciphers.
Definition: strciphr.h:406
Base class for additive stream ciphers.
Definition: strciphr.h:186
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
Operates the keystream.
Definition: strciphr.h:151
XOR the input buffer and keystream, write to the aligned output buffer.
Definition: strciphr.h:100
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
unsigned int GetBufferByteSize(const PolicyInterface &policy) const
Definition: strciphr.h:339
bool IsForwardTransformation() const
Definition: strciphr.h:564
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Definition: config.h:720
XOR the input buffer and keystream, write to the output buffer.
Definition: strciphr.h:96
CipherDir
Specifies a direction for a cipher to operate.
Definition: cryptlib.h:104
Abstract base classes that provide a uniform interface to this library.
Base class for feedback based stream ciphers with SymmetricCipher interface.
Definition: strciphr.h:489
Some other error occurred not belonging to other categories.
Definition: cryptlib.h:159
Library configuration file.
Interface for random number generators.
Definition: cryptlib.h:1188
Wirte the keystream to the output buffer, input is NULL.
Definition: strciphr.h:92
Stream cipher policy object.
Definition: strciphr.h:66
Interface for cloning objects.
Definition: cryptlib.h:482
unsigned int OptimalBlockSize() const
Provides number of ideal bytes to process.
Definition: strciphr.h:303
virtual void WriteKeystream(byte *keystream, size_t iterationCount)
Generate the keystream.
Definition: strciphr.h:135
Policy object for additive stream ciphers.
Definition: strciphr.h:106
the cipher is performing encryption
Definition: cryptlib.h:106
byte * KeystreamBufferBegin()
Definition: strciphr.h:341
Classes and functions for secure memory allocations.
virtual unsigned int GetAlignment() const
Provides data alignment requirements.
Definition: strciphr.h:114
void TransformRegister()
Perform one iteration in the forward direction.
Definition: strciphr.h:426
bool IsAlignedOn(const void *ptr, unsigned int alignment)
Determines whether ptr is aligned to a minimum value.
Definition: misc.h:954
Input buffer is NULL.
Definition: strciphr.h:84
POLICY_INTERFACE PolicyInterface
Definition: strciphr.h:53
Base class for feedback based stream ciphers in the reverse direction with SymmetricCipher interface...
Definition: strciphr.h:562
unsigned int OptimalDataAlignment() const
Provides number of ideal data alignment.
Definition: strciphr.h:527
POLICY_INTERFACE & AccessPolicy()
Definition: strciphr.h:72
BASE::PolicyInterface PolicyInterface
Definition: strciphr.h:538
A method was called which was not implemented.
Definition: cryptlib.h:205
RegisterOutput & operator()(WordType &registerWord)
XOR feedback register with data.
Definition: strciphr.h:442
virtual unsigned int GetBytesPerIteration() const =0
Provides number of bytes operated upon during an iteration.
Classes and functions for implementing secret key algorithms.
bool IsSelfInverting() const
Determines if the cipher is self inverting.
Definition: strciphr.h:318
virtual void SeekToIteration(lword iterationCount)
Seeks to a random position in the stream.
Definition: strciphr.h:175
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
Definition: cryptlib.h:1103
Base class for feedback based stream ciphers with a mandatory block size.
Definition: strciphr.h:572
Policy object for feeback based stream ciphers.
Definition: strciphr.h:350
byte * KeystreamBufferEnd()
Definition: strciphr.h:342
bool CanIterate() const
Flag indicating iteration support.
Definition: strciphr.h:423
bool IsForwardTransformation() const
Definition: strciphr.h:554
unsigned int MandatoryBlockSize() const
Definition: strciphr.h:575
unsigned int GetAlignment() const
Provides data alignment requirements.
Definition: strciphr.h:414
virtual ~AdditiveCipherTemplate()
Definition: strciphr.h:270
virtual ~AbstractPolicyHolder()
Definition: strciphr.h:54
Base class for feedback based stream ciphers in the forward direction with SymmetricCipher interface...
Definition: strciphr.h:552
#define CRYPTOPP_CONSTANT(x)
Definition: config.h:540
Clonable * Clone() const
Clone a SymmetricCipher.
Definition: strciphr.h:613
SymmetricCipherFinal()
Construct a stream cipher.
Definition: strciphr.h:590
virtual ~AdditiveCipherAbstractPolicy()
Definition: strciphr.h:108
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
virtual bool CanIterate() const
Flag indicating iteration support.
Definition: strciphr.h:375
unsigned int OptimalBlockSize() const
Provides number of ideal bytes to process.
Definition: strciphr.h:516
SecByteBlock m_buffer
Definition: strciphr.h:344
#define CRYPTOPP_NO_VTABLE
Definition: config.h:369
#define X(name)
Definition: net.cpp:642
virtual unsigned int GetIterationsToBuffer() const =0
Provides buffer size based on iterations.
bool IsRandomAccess() const
Flag indicating random access.
Definition: strciphr.h:327
XOR the aligned input buffer and keystream, write to the aligned output buffer.
Definition: strciphr.h:102
unsigned int OptimalDataAlignment() const
Provides number of ideal data alignment.
Definition: strciphr.h:314
uint8_t const size_t const size
Definition: sha3.h:20
SymmetricCipherFinal(const byte *key, size_t length, const byte *iv)
Construct a stream cipher.
Definition: strciphr.h:608
unsigned int GetOptimalNextBlockSize() const
Provides number of ideal bytes to process.
Definition: strciphr.h:309
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
unsigned int GetBytesPerIteration() const
Provides number of bytes operated upon during an iteration.
Definition: strciphr.h:202
bool IsForwardTransformation() const
Determines if the cipher is a forward transformation.
Definition: strciphr.h:322
virtual ~CFB_CipherAbstractPolicy()
Definition: strciphr.h:353
virtual void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
Resynchronize the cipher.
Definition: strciphr.h:164
virtual void Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount)
Iterate the cipher.
Definition: strciphr.h:383
An Empty class.
Definition: misc.h:184
virtual ~SymmetricCipherFinal()
Definition: strciphr.h:587
#define NAMESPACE_END
Definition: config.h:201
virtual bool CanOperateKeystream() const
Flag indicating.
Definition: strciphr.h:141
KeystreamOperation
Keystream operation flags.
Definition: strciphr.h:90
bool IsSelfInverting() const
Determines if the cipher is self inverting.
Definition: strciphr.h:536
SymmetricCipherFinal(const byte *key, size_t length)
Construct a stream cipher.
Definition: strciphr.h:601
unsigned int GetIterationsToBuffer() const
Provides buffer size based on iterations.
Definition: strciphr.h:206
virtual void CipherResynchronize(const byte *iv, size_t length)
Resynchronize the cipher.
Definition: strciphr.h:396
bool IsRandomAccess() const
Flag indicating random access.
Definition: strciphr.h:532
word64 lword
Definition: config.h:245
SymmetricCipher implementation.
Definition: strciphr.h:584
Wirte the keystream to the aligned output buffer, input is NULL.
Definition: strciphr.h:94
#define CRYPTOPP_DLL
Definition: config.h:704
bool CanOperateKeystream() const
Flag indicating.
Definition: strciphr.h:211
unsigned int GetOptimalNextBlockSize() const
Provides number of ideal bytes to process.
Definition: strciphr.h:522
byte ByteReverse(byte value)
Reverses bytes in a 8-bit value.
Definition: misc.h:1663
Base class for additive stream ciphers with SymmetricCipher interface.
Definition: strciphr.h:267
Access a stream cipher policy object.
Definition: strciphr.h:50
XOR the aligned input buffer and keystream, write to the output buffer.
Definition: strciphr.h:98
KeystreamOperationFlags
Keystream operation flags.
Definition: strciphr.h:78
const POLICY_INTERFACE & GetPolicy() const
Definition: strciphr.h:71
RegisterOutput(byte *output, const byte *input, CipherDir dir)
Definition: strciphr.h:436
Interface for retrieving values given their names.
Definition: cryptlib.h:279
Base class for identifying alogorithm.
Definition: simple.h:38