Fabcoin Core  0.16.2
P2P Digital Currency
seckey.h
Go to the documentation of this file.
1 // seckey.h - written and placed in the public domain by Wei Dai
2 
5 
6 #ifndef CRYPTOPP_SECKEY_H
7 #define CRYPTOPP_SECKEY_H
8 
9 #include "config.h"
10 #include "cryptlib.h"
11 #include "misc.h"
12 #include "simple.h"
13 
14 #if CRYPTOPP_MSC_VERSION
15 # pragma warning(push)
16 # pragma warning(disable: 4189)
17 #endif
18 
19 // Issue 340
20 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
21 # pragma GCC diagnostic push
22 # pragma GCC diagnostic ignored "-Wconversion"
23 # pragma GCC diagnostic ignored "-Wsign-conversion"
24 #endif
25 
27 
32 {
33  return (dir == ENCRYPTION) ? DECRYPTION : ENCRYPTION;
34 }
35 
39 template <unsigned int N>
41 {
42 public:
44  CRYPTOPP_CONSTANT(BLOCKSIZE = N)
45 };
46 
47 // ************** rounds ***************
48 
52 template <unsigned int R>
54 {
55 public:
57  CRYPTOPP_CONSTANT(ROUNDS = R)
58 };
59 
65 template <unsigned int D, unsigned int N=1, unsigned int M=INT_MAX> // use INT_MAX here because enums are treated as signed ints
67 {
68 public:
70  CRYPTOPP_CONSTANT(DEFAULT_ROUNDS = D)
72  CRYPTOPP_CONSTANT(MIN_ROUNDS = N)
74  CRYPTOPP_CONSTANT(MAX_ROUNDS = M)
79  CRYPTOPP_STATIC_CONSTEXPR unsigned int StaticGetDefaultRounds(size_t keylength)
80  {
81  return CRYPTOPP_UNUSED(keylength), static_cast<unsigned int>(DEFAULT_ROUNDS);
82  }
83 
84 protected:
90  inline void ThrowIfInvalidRounds(int rounds, const Algorithm *alg)
91  {
92  if (M == INT_MAX) // Coverity and result_independent_of_operands
93  {
94  if (rounds < MIN_ROUNDS)
95  throw InvalidRounds(alg ? alg->AlgorithmName() : std::string("VariableRounds"), rounds);
96  }
97  else
98  {
99  if (rounds < MIN_ROUNDS || rounds > MAX_ROUNDS)
100  throw InvalidRounds(alg ? alg->AlgorithmName() : std::string("VariableRounds"), rounds);
101  }
102  }
103 
110  inline unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs &param, const Algorithm *alg)
111  {
112  int rounds = param.GetIntValueWithDefault("Rounds", DEFAULT_ROUNDS);
113  ThrowIfInvalidRounds(rounds, alg);
114  return (unsigned int)rounds;
115  }
116 };
117 
118 // ************** key length ***************
119 
126 template <unsigned int N, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE, unsigned int IV_L = 0>
128 {
129 public:
132  CRYPTOPP_CONSTANT(KEYLENGTH=N)
135  CRYPTOPP_CONSTANT(MIN_KEYLENGTH=N)
138  CRYPTOPP_CONSTANT(MAX_KEYLENGTH=N)
141  CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=N)
145  CRYPTOPP_CONSTANT(IV_REQUIREMENT = IV_REQ)
148  CRYPTOPP_CONSTANT(IV_LENGTH = IV_L)
153  CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
154  {
155  return CRYPTOPP_UNUSED(keylength), static_cast<size_t>(KEYLENGTH);
156  }
157 };
158 
168 template <unsigned int D, unsigned int N, unsigned int M, unsigned int Q = 1, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE, unsigned int IV_L = 0>
170 {
171  // Make these private to avoid Doxygen documenting them in all derived classes
173  CRYPTOPP_COMPILE_ASSERT(N % Q == 0);
174  CRYPTOPP_COMPILE_ASSERT(M % Q == 0);
176  CRYPTOPP_COMPILE_ASSERT(D >= N);
177  CRYPTOPP_COMPILE_ASSERT(M >= D);
178 
179 public:
182  CRYPTOPP_CONSTANT(MIN_KEYLENGTH=N)
185  CRYPTOPP_CONSTANT(MAX_KEYLENGTH=M)
188  CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=D)
191  CRYPTOPP_CONSTANT(KEYLENGTH_MULTIPLE=Q)
195  CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ)
198  CRYPTOPP_CONSTANT(IV_LENGTH=IV_L)
207  CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
208  {
209  return (keylength <= N) ? N :
210  (keylength >= M) ? M :
211  (keylength+Q-1) - (keylength+Q-1)%Q;
212  }
213 };
214 
221 template <class T, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE, unsigned int IV_L = 0>
223 {
224 public:
227  CRYPTOPP_CONSTANT(MIN_KEYLENGTH=T::MIN_KEYLENGTH)
230  CRYPTOPP_CONSTANT(MAX_KEYLENGTH=T::MAX_KEYLENGTH)
233  CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=T::DEFAULT_KEYLENGTH)
237  CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ)
240  CRYPTOPP_CONSTANT(IV_LENGTH=IV_L)
249  CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
250  {return T::StaticGetValidKeyLength(keylength);}
251 };
252 
253 // ************** implementation helper for SimpleKeyingInterface ***************
254 
262 template <class BASE, class INFO = BASE>
264 {
265 public:
268  size_t MinKeyLength() const
269  {return INFO::MIN_KEYLENGTH;}
270 
273  size_t MaxKeyLength() const
274  {return (size_t)INFO::MAX_KEYLENGTH;}
275 
278  size_t DefaultKeyLength() const
279  {return INFO::DEFAULT_KEYLENGTH;}
280 
289  size_t GetValidKeyLength(size_t keylength) const {return INFO::StaticGetValidKeyLength(keylength);}
290 
295  {return (SimpleKeyingInterface::IV_Requirement)INFO::IV_REQUIREMENT;}
296 
299  unsigned int IVSize() const
300  {return INFO::IV_LENGTH;}
301 };
302 
310 template <class INFO, class BASE = BlockCipher>
311 class CRYPTOPP_NO_VTABLE BlockCipherImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<BASE, INFO> > >
312 {
313 public:
316  unsigned int BlockSize() const {return this->BLOCKSIZE;}
317 };
318 
323 template <CipherDir DIR, class BASE>
324 class BlockCipherFinal : public ClonableImpl<BlockCipherFinal<DIR, BASE>, BASE>
325 {
326 public:
330 
335  BlockCipherFinal(const byte *key)
336  {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
337 
343  BlockCipherFinal(const byte *key, size_t length)
344  {this->SetKey(key, length);}
345 
352  BlockCipherFinal(const byte *key, size_t length, unsigned int rounds)
353  {this->SetKeyWithRounds(key, length, rounds);}
354 
358  bool IsForwardTransformation() const {return DIR == ENCRYPTION;}
359 };
360 
369 template <class BASE, class INFO = BASE>
370 class MessageAuthenticationCodeImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>
371 {
372 };
373 
378 template <class BASE>
379 class MessageAuthenticationCodeFinal : public ClonableImpl<MessageAuthenticationCodeFinal<BASE>, MessageAuthenticationCodeImpl<BASE> >
380 {
381 public:
390  {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
396  MessageAuthenticationCodeFinal(const byte *key, size_t length)
397  {this->SetKey(key, length);}
398 };
399 
400 // ************** documentation ***************
401 
409 {
414 };
415 
425 {
430 };
431 
438 {
443 };
444 
446 
447 #if CRYPTOPP_MSC_VERSION
448 # pragma warning(pop)
449 #endif
450 
451 // Issue 340
452 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
453 # pragma GCC diagnostic pop
454 #endif
455 
456 #endif
the cipher is performing decryption
Definition: cryptlib.h:108
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:127
uint8_t byte
Definition: Common.h:57
Classes providing basic library services.
Utility functions for the Crypto++ library.
#define CRYPTOPP_STATIC_CONSTEXPR
Definition: config.h:892
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher...
Definition: seckey.h:408
MessageAuthenticationCodeFinal(const byte *key, size_t length)
Construct a BlockCipherFinal.
Definition: seckey.h:396
Interface for one direction (encryption or decryption) of a stream cipher or block cipher mode with a...
Definition: cryptlib.h:1121
Base class for identifying alogorithm.
Definition: simple.h:26
Provides a base implementation of SimpleKeyingInterface.
Definition: seckey.h:263
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
CipherDir
Specifies a direction for a cipher to operate.
Definition: cryptlib.h:104
#define Q(i)
Definition: cast.cpp:199
Abstract base classes that provide a uniform interface to this library.
unsigned int BlockSize() const
Provides the block size of the algorithm.
Definition: seckey.h:316
#define R(a, b)
Provides Encryption and Decryption typedefs used by derived classes to implement an authenticated enc...
Definition: seckey.h:437
Library configuration file.
void ThrowIfInvalidRounds(int rounds, const Algorithm *alg)
Validates the number of rounds for an algorithm.
Definition: seckey.h:90
Provides class member functions to key a message authentication code.
Definition: seckey.h:379
BlockCipherFinal(const byte *key)
Construct a BlockCipherFinal.
Definition: seckey.h:335
BlockCipher Decryption
implements the BlockCipher interface
Definition: seckey.h:413
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1095
the cipher is performing encryption
Definition: cryptlib.h:106
Inherited by algorithms with fixed block size.
Definition: seckey.h:40
BlockCipher Encryption
implements the BlockCipher interface
Definition: seckey.h:411
Inherited by algorithms with variable number of rounds.
Definition: seckey.h:66
#define CRYPTOPP_COMPILE_ASSERT(assertion)
Definition: misc.h:139
BlockCipherFinal()
Construct a default BlockCipherFinal.
Definition: seckey.h:329
Exception thrown when an invalid number of rounds is encountered.
Definition: simple.h:55
unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs &param, const Algorithm *alg)
Validates the number of rounds for an algorithm.
Definition: seckey.h:110
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Definition: cryptlib.h:382
size_t DefaultKeyLength() const
The default key length used by the algorithm.
Definition: seckey.h:278
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
Definition: cryptlib.h:1103
Provides class member functions to key a block cipher.
Definition: seckey.h:324
virtual std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: cryptlib.h:518
Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication code...
Definition: seckey.h:370
#define CRYPTOPP_API
Definition: config.h:705
MessageAuthenticationCodeFinal(const byte *key)
Construct a BlockCipherFinal.
Definition: seckey.h:389
bool IsForwardTransformation() const
Provides the direction of the cipher.
Definition: seckey.h:358
size_t GetValidKeyLength(size_t keylength) const
Provides a valid key length for the algorithm.
Definition: seckey.h:289
Provides key lengths based on another class&#39;s key length.
Definition: seckey.h:222
#define CRYPTOPP_CONSTANT(x)
Definition: config.h:540
Inherited by algorithms with fixed number of rounds.
Definition: seckey.h:53
CipherDir ReverseCipherDir(CipherDir dir)
Inverts the cipher&#39;s direction.
Definition: seckey.h:31
SymmetricCipher Decryption
implements the SymmetricCipher interface
Definition: seckey.h:429
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:169
#define CRYPTOPP_NO_VTABLE
Definition: config.h:369
Interface for all crypto algorithms.
Definition: cryptlib.h:497
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher...
Definition: seckey.h:424
AuthenticatedSymmetricCipher Encryption
implements the AuthenticatedSymmetricCipher interface
Definition: seckey.h:440
IV_Requirement
Secure IVs requirements as enumerated values.
Definition: cryptlib.h:598
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
#define NAMESPACE_END
Definition: config.h:201
BlockCipherFinal(const byte *key, size_t length, unsigned int rounds)
Construct a BlockCipherFinal.
Definition: seckey.h:352
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:311
AuthenticatedSymmetricCipher Decryption
implements the AuthenticatedSymmetricCipher interface
Definition: seckey.h:442
SimpleKeyingInterface::IV_Requirement IVRequirement() const
The default IV requirements for the algorithm.
Definition: seckey.h:294
BlockCipherFinal(const byte *key, size_t length)
Construct a BlockCipherFinal.
Definition: seckey.h:343
unsigned int IVSize() const
The default initialization vector length for the algorithm.
Definition: seckey.h:299
size_t MaxKeyLength() const
The maximum key length used by the algorithm.
Definition: seckey.h:273
SymmetricCipher Encryption
implements the SymmetricCipher interface
Definition: seckey.h:427
size_t MinKeyLength() const
The minimum key length used by the algorithm.
Definition: seckey.h:268
MessageAuthenticationCodeFinal()
Construct a default MessageAuthenticationCodeFinal.
Definition: seckey.h:384
Interface for retrieving values given their names.
Definition: cryptlib.h:279
Base class for identifying alogorithm.
Definition: simple.h:38