Crypto++  5.6.5 Free C++ class library of cryptographic schemes
seckey.h
Go to the documentation of this file.
1 // seckey.h - originally written and placed in the public domain by Wei Dai
2
3 /// \file seckey.h
4 /// \brief Classes and functions for implementing secret key algorithms.
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
26 NAMESPACE_BEGIN(CryptoPP)
27
28 /// \brief Inverts the cipher's direction
29 /// \param dir the cipher's direction
30 /// \returns DECRYPTION if \ref CipherDir "dir" is ENCRYPTION, DECRYPTION otherwise
32 {
33  return (dir == ENCRYPTION) ? DECRYPTION : ENCRYPTION;
34 }
35
36 /// \class FixedBlockSize
37 /// \brief Inherited by algorithms with fixed block size
38 /// \tparam N the blocksize of the algorithm
39 template <unsigned int N>
41 {
42 public:
43  /// \brief The block size of the algorithm provided as a constant.
44  CRYPTOPP_CONSTANT(BLOCKSIZE = N)
45  /// \brief The default blocksize for the algorithm provided as a constant.
46  CRYPTOPP_CONSTANT(DEFAULT_BLOCKSIZE = N)
47  /// \brief The minimum blocksize for the algorithm provided as a constant.
48  CRYPTOPP_CONSTANT(MIN_BLOCKSIZE = N)
49  /// \brief The maximum blocksize for the algorithm provided as a constant.
50  CRYPTOPP_CONSTANT(MAX_BLOCKSIZE = N)
51  /// \brief The default block size for the algorithm provided by a static function.
52  /// \param blocksize the block size, in bytes
53  /// \details The default implementation returns BLOCKSIZE. blocksize is unused
54  /// in the default implementation.
55  CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidBlockSize(size_t blocksize)
56  {
57  return CRYPTOPP_UNUSED(blocksize), static_cast<size_t>(BLOCKSIZE);
58  }
59  /// \brief The default block size under a key provided by a static function.
60  /// \param keylength the size of the key, in bytes
61  /// \param blocksize the block size, in bytes
62  /// \details The default implementation returns BLOCKSIZE. blocksize is unused
63  /// in the default implementation.
64  CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidBlockSize(size_t keylength, size_t blocksize)
65  {
66  return CRYPTOPP_UNUSED(keylength), CRYPTOPP_UNUSED(blocksize), static_cast<size_t>(BLOCKSIZE);
67  }
68 };
69
70 // ************** rounds ***************
71
72 /// \class FixedRounds
73 /// \brief Inherited by algorithms with fixed number of rounds
74 /// \tparam R the number of rounds used by the algorithm
75 template <unsigned int R>
77 {
78 public:
79  /// \brief The number of rounds for the algorithm provided as a constant.
80  CRYPTOPP_CONSTANT(ROUNDS = R)
81 };
82
83 /// \class VariableRounds
84 /// \brief Inherited by algorithms with variable number of rounds
85 /// \tparam D Default number of rounds
86 /// \tparam N Minimum number of rounds
87 /// \tparam M Maximum number of rounds
88 template <unsigned int D, unsigned int N=1, unsigned int M=INT_MAX> // use INT_MAX here because enums are treated as signed ints
90 {
91 public:
92  /// \brief The default number of rounds for the algorithm provided as a constant.
93  CRYPTOPP_CONSTANT(DEFAULT_ROUNDS = D)
94  /// \brief The minimum number of rounds for the algorithm provided as a constant.
95  CRYPTOPP_CONSTANT(MIN_ROUNDS = N)
96  /// \brief The maximum number of rounds for the algorithm provided as a constant.
97  CRYPTOPP_CONSTANT(MAX_ROUNDS = M)
98  /// \brief The default number of rounds for the algorithm based on key length
99  /// provided by a static function.
100  /// \param keylength the size of the key, in bytes
101  /// \details keylength is unused in the default implementation.
102  CRYPTOPP_STATIC_CONSTEXPR unsigned int StaticGetDefaultRounds(size_t keylength)
103  {
104  return CRYPTOPP_UNUSED(keylength), static_cast<unsigned int>(DEFAULT_ROUNDS);
105  }
106
107 protected:
108  /// \brief Validates the number of rounds for an algorithm.
109  /// \param rounds the candidate number of rounds
110  /// \param alg an Algorithm object used if the number of rounds are invalid
111  /// \throws InvalidRounds if the number of rounds are invalid
112  /// \details ThrowIfInvalidRounds() validates the number of rounds and throws if invalid.
113  inline void ThrowIfInvalidRounds(int rounds, const Algorithm *alg)
114  {
115  if (M == INT_MAX) // Coverity and result_independent_of_operands
116  {
117  if (rounds < MIN_ROUNDS)
118  throw InvalidRounds(alg ? alg->AlgorithmName() : std::string("VariableRounds"), rounds);
119  }
120  else
121  {
122  if (rounds < MIN_ROUNDS || rounds > MAX_ROUNDS)
123  throw InvalidRounds(alg ? alg->AlgorithmName() : std::string("VariableRounds"), rounds);
124  }
125  }
126
127  /// \brief Validates the number of rounds for an algorithm
128  /// \param param the candidate number of rounds
129  /// \param alg an Algorithm object used if the number of rounds are invalid
130  /// \returns the number of rounds for the algorithm
131  /// \throws InvalidRounds if the number of rounds are invalid
132  /// \details GetRoundsAndThrowIfInvalid() validates the number of rounds and throws if invalid.
133  inline unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs &param, const Algorithm *alg)
134  {
135  int rounds = param.GetIntValueWithDefault("Rounds", DEFAULT_ROUNDS);
136  ThrowIfInvalidRounds(rounds, alg);
137  return static_cast<unsigned int>(rounds);
138  }
139 };
140
141 /// \class VariableBlockSize
142 /// \brief Inherited by algorithms with variable blocksize
143 /// \tparam D Default blocksize
144 /// \tparam N Minimum blocksize
145 /// \tparam M Maximum blocksize
146 template <unsigned int D, unsigned int N=1, unsigned int M=INT_MAX> // use INT_MAX here because enums are treated as signed ints
148 {
149 public:
150  /// \brief The default blocksize for the algorithm provided as a constant.
151  CRYPTOPP_CONSTANT(DEFAULT_BLOCKSIZE = D)
152  /// \brief The minimum blocksize for the algorithm provided as a constant.
153  CRYPTOPP_CONSTANT(MIN_BLOCKSIZE = N)
154  /// \brief The maximum blocksize for the algorithm provided as a constant.
155  CRYPTOPP_CONSTANT(MAX_BLOCKSIZE = M)
156  /// \brief The default block size for the algorithm provided by a static function.
157  /// \param blocksize the block size, in bytes
158  /// \details The default implementation returns BLOCKSIZE. blocksize is unused
159  /// in the default implementation.
160  CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidBlockSize(size_t blocksize)
161  {
162  return CRYPTOPP_UNUSED(blocksize), static_cast<size_t>(DEFAULT_BLOCKSIZE);
163  }
164  /// \brief The default block size under a key provided by a static function.
165  /// \param keylength the size of the key, in bytes
166  /// \param blocksize the block size, in bytes
167  /// \details The default implementation returns BLOCKSIZE. blocksize is unused
168  /// in the default implementation.
169  CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidBlockSize(size_t keylength, size_t blocksize)
170  {
171  return CRYPTOPP_UNUSED(keylength), CRYPTOPP_UNUSED(blocksize), static_cast<size_t>(DEFAULT_BLOCKSIZE);
172  }
173
174 protected:
175  /// \brief Validates the blocksize for an algorithm.
176  /// \param blocksize the candidate blocksize
177  /// \param alg an Algorithm object used if the blocksize is invalid
178  /// \throws InvalidBlockSize if the blocksize is invalid
179  /// \details ThrowIfInvalidBlockSize() validates the blocksize and throws if invalid.
180  inline void ThrowIfInvalidBlockSize(int blocksize, const Algorithm *alg)
181  {
182  if (M == INT_MAX) // Coverity and result_independent_of_operands
183  {
184  if (blocksize < MIN_BLOCKSIZE)
185  throw InvalidBlockSize(alg ? alg->AlgorithmName() : std::string("VariableBlockSize"), blocksize);
186  }
187  else
188  {
189  if (blocksize < MIN_BLOCKSIZE || blocksize > MAX_BLOCKSIZE)
190  throw InvalidBlockSize(alg ? alg->AlgorithmName() : std::string("VariableBlockSize"), blocksize);
191  }
192  }
193
194  /// \brief Validates the blocksize for an algorithm
195  /// \param param the candidate blocksize
196  /// \param alg an Algorithm object used if the blocksize is invalid
197  /// \returns the blocksize for the algorithm
198  /// \throws InvalidBlockSize if the blocksize is invalid
199  /// \details GetBlockSizeAndThrowIfInvalid() validates the blocksize and throws if invalid.
200  inline unsigned int GetBlockSizeAndThrowIfInvalid(const NameValuePairs &param, const Algorithm *alg)
201  {
202  int keylength = param.GetIntValueWithDefault("KeySize", 0);
203  int blocksize = param.GetIntValueWithDefault("BlockSize", DEFAULT_BLOCKSIZE);
204  if (keylength > 0)
205  ThrowIfInvalidBlockSize(keylength, blocksize, alg);
206  else
207  ThrowIfInvalidBlockSize(blocksize, alg);
208  return static_cast<unsigned int>(blocksize);
209  }
210
211  /// Provides the block size of the cipher
212  /// \return the block size, in bytes
213  /// \details The sematics of BlockSize() is return DEFAULT_BLOCKSIZE if the default blocksize
214  /// is in effect. If the blocksize has changed, then the value returned is the BlockSize()
215  /// parameter used during SetKey().
216  /// \details DEFAULT_BLOCKSIZE should be paired with DEFAULT_KEYLENGTH, and it is the same as
217  /// BLOCKSIZE in a FixedBlockSize cipher.
218  virtual unsigned int BlockSize() const =0;
219
220  /// Provides the minimum block size of the cipher
221  /// \return the minimum block size, in bytes
222  /// \details MinBlockSize() returns the smallest blocksize a cipher can use. The size can
223  /// be affected by the key length. For example, Threefish has key sizes of 256, 512 and 1024 bits,
224  /// and the blocksize follows the key length. If a 512-bit key is used, then the block size is 512
225  /// bits. Once keyed, the minimum block size of 256 is not accurate, nor is a block size of 1024 bit.
226  virtual unsigned int MinBlockSize() const
227  { return MIN_BLOCKSIZE; }
228
229  /// Provides the maximum block size of the cipher
230  /// \return the maximum block size, in bytes
231  /// \details MaxBlockSize() returns the largest blocksize a cipher can use. The size can
232  /// be affected by the key length. For example, Threefish has key sizes of 256, 512 and 1024 bits,
233  /// and the blocksize follows the key length. If a 512-bit key is used, then the block size is 512
234  /// bits. Once keyed, the minimum block size of 256 is not accurate, nor is a block size of 1024 bit.
235  virtual unsigned int MaxBlockSize() const
236  { return MAX_BLOCKSIZE; }
237
238  /// Provides the initialization vector length of the cipher
239  /// \return the initialization vector length, in bytes
240  /// \details The sematics of IVSize() is return IV_LENGTH if the default blocksize is
241  /// in effect. If the blocksize has changed, then the default implentation returns the value of
242  /// the BlockSize() parameter used during SetKey().
243  /// \details Derived classes may override the behavior such that a different value is returned.
244  /// This may happen with a cipher that requires an IV that is twice the block size.
245  virtual unsigned int IVSize() const =0;
246
247  /// \brief Provides the minimum size of an IV
248  /// \return minimal length of IVs accepted by this cipher, in bytes
249  virtual unsigned int MinIVLength() const
250  { return MIN_BLOCKSIZE; }
251
252  /// \brief Provides the maximum size of an IV
253  /// \return maximal length of IVs accepted by this cipher, in bytes
254  virtual unsigned int MaxIVLength() const
255  { return MAX_BLOCKSIZE; }
256 };
257
258 // ************** key length ***************
259
260 /// \class FixedKeyLength
261 /// \brief Inherited by keyed algorithms with fixed key length
262 /// \tparam N Default key length, in bytes
263 /// \tparam IV_REQ the \ref SimpleKeyingInterface::IV_Requirement "IV requirements"
264 /// \tparam IV_L default IV length, in bytes
265 /// \sa SimpleKeyingInterface
266 template <unsigned int N, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE, unsigned int IV_L = 0>
268 {
269 public:
270  /// \brief The default key length used by the algorithm provided as a constant
271  /// \details KEYLENGTH is provided in bytes, not bits
272  CRYPTOPP_CONSTANT(KEYLENGTH=N)
273  /// \brief The minimum key length used by the algorithm provided as a constant
274  /// \details MIN_KEYLENGTH is provided in bytes, not bits
275  CRYPTOPP_CONSTANT(MIN_KEYLENGTH=N)
276  /// \brief The maximum key length used by the algorithm provided as a constant
277  /// \details MAX_KEYLENGTH is provided in bytes, not bits
278  CRYPTOPP_CONSTANT(MAX_KEYLENGTH=N)
279  /// \brief The default key length used by the algorithm provided as a constant
280  /// \details DEFAULT_KEYLENGTH is provided in bytes, not bits
281  CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=N)
282  /// \brief The default IV requirements for the algorithm provided as a constant
283  /// \details The default value is NOT_RESYNCHRONIZABLE. See IV_Requirement
284  /// in cryptlib.h for allowed values.
285  CRYPTOPP_CONSTANT(IV_REQUIREMENT = IV_REQ)
286  /// \brief The default IV length used by the algorithm provided as a constant
287  /// \details IV_LENGTH is provided in bytes, not bits. The default implementation uses 0.
288  CRYPTOPP_CONSTANT(IV_LENGTH = IV_L)
289  /// \brief The default key length for the algorithm provided by a static function.
290  /// \param keylength the size of the key, in bytes
291  /// \details The default implementation returns KEYLENGTH. keylength is unused
292  /// in the default implementation.
293  CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
294  {
295  return CRYPTOPP_UNUSED(keylength), static_cast<size_t>(KEYLENGTH);
296  }
297 };
298
299 /// \class VariableKeyLength
300 /// \brief Inherited by keyed algorithms with variable key length
301 /// \tparam D Default key length, in bytes
302 /// \tparam N Minimum key length, in bytes
303 /// \tparam M Maximum key length, in bytes
304 /// \tparam Q Default key length multiple, in bytes. The default multiple is 1.
305 /// \tparam IV_REQ the \ref SimpleKeyingInterface::IV_Requirement "IV requirements"
306 /// \tparam IV_L default IV length, in bytes. The default length is 0.
307 /// \sa SimpleKeyingInterface
308 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>
310 {
311  // Make these private to avoid Doxygen documenting them in all derived classes
313  CRYPTOPP_COMPILE_ASSERT(N % Q == 0);
314  CRYPTOPP_COMPILE_ASSERT(M % Q == 0);
316  CRYPTOPP_COMPILE_ASSERT(D >= N);
317  CRYPTOPP_COMPILE_ASSERT(M >= D);
318
319 public:
320  /// \brief The minimum key length used by the algorithm provided as a constant
321  /// \details MIN_KEYLENGTH is provided in bytes, not bits
322  CRYPTOPP_CONSTANT(MIN_KEYLENGTH=N)
323  /// \brief The maximum key length used by the algorithm provided as a constant
324  /// \details MAX_KEYLENGTH is provided in bytes, not bits
325  CRYPTOPP_CONSTANT(MAX_KEYLENGTH=M)
326  /// \brief The default key length used by the algorithm provided as a constant
327  /// \details DEFAULT_KEYLENGTH is provided in bytes, not bits
328  CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=D)
329  /// \brief The key length multiple used by the algorithm provided as a constant
330  /// \details MAX_KEYLENGTH is provided in bytes, not bits
331  CRYPTOPP_CONSTANT(KEYLENGTH_MULTIPLE=Q)
332  /// \brief The default IV requirements for the algorithm provided as a constant
333  /// \details The default value is NOT_RESYNCHRONIZABLE. See IV_Requirement
334  /// in cryptlib.h for allowed values.
335  CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ)
336  /// \brief The default initialization vector length for the algorithm provided as a constant
337  /// \details IV_LENGTH is provided in bytes, not bits. The default implementation uses 0.
338  CRYPTOPP_CONSTANT(IV_LENGTH=IV_L)
339  /// \brief Provides a valid key length for the algorithm provided by a static function.
340  /// \param keylength the size of the key, in bytes
341  /// \details If keylength is less than MIN_KEYLENGTH, then the function returns
342  /// MIN_KEYLENGTH. If keylength is greater than MAX_KEYLENGTH, then the function
343  /// returns MAX_KEYLENGTH. If keylength is a multiple of KEYLENGTH_MULTIPLE,
344  /// then keylength is returned. Otherwise, the function returns keylength rounded
345  /// \a down to the next smaller multiple of KEYLENGTH_MULTIPLE.
346  /// \details keylength is provided in bytes, not bits.
347  CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
348  {
349  return (keylength <= N) ? N :
350  (keylength >= M) ? M :
351  (keylength+Q-1) - (keylength+Q-1)%Q;
352  }
353 };
354
355 /// \class SameKeyLengthAs
356 /// \brief Provides key lengths based on another class's key length
357 /// \tparam T another FixedKeyLength or VariableKeyLength class
358 /// \tparam IV_REQ the \ref SimpleKeyingInterface::IV_Requirement "IV requirements"
359 /// \tparam IV_L default IV length, in bytes
360 /// \sa SimpleKeyingInterface
361 template <class T, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE, unsigned int IV_L = 0>
363 {
364 public:
365  /// \brief The minimum key length used by the algorithm provided as a constant
366  /// \details MIN_KEYLENGTH is provided in bytes, not bits
367  CRYPTOPP_CONSTANT(MIN_KEYLENGTH=T::MIN_KEYLENGTH)
368  /// \brief The maximum key length used by the algorithm provided as a constant
369  /// \details MIN_KEYLENGTH is provided in bytes, not bits
370  CRYPTOPP_CONSTANT(MAX_KEYLENGTH=T::MAX_KEYLENGTH)
371  /// \brief The default key length used by the algorithm provided as a constant
372  /// \details MIN_KEYLENGTH is provided in bytes, not bits
373  CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=T::DEFAULT_KEYLENGTH)
374  /// \brief The default IV requirements for the algorithm provided as a constant
375  /// \details The default value is NOT_RESYNCHRONIZABLE. See IV_Requirement
376  /// in cryptlib.h for allowed values.
377  CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ)
378  /// \brief The default initialization vector length for the algorithm provided as a constant
379  /// \details IV_LENGTH is provided in bytes, not bits. The default implementation uses 0.
380  CRYPTOPP_CONSTANT(IV_LENGTH=IV_L)
381  /// \brief Provides a valid key length for the algorithm provided by a static function.
382  /// \param keylength the size of the key, in bytes
383  /// \details If keylength is less than MIN_KEYLENGTH, then the function returns
384  /// MIN_KEYLENGTH. If keylength is greater than MAX_KEYLENGTH, then the function
385  /// returns MAX_KEYLENGTH. If keylength is a multiple of KEYLENGTH_MULTIPLE,
386  /// then keylength is returned. Otherwise, the function returns keylength rounded
387  /// \a down to the next smaller multiple of KEYLENGTH_MULTIPLE.
388  /// \details keylength is provided in bytes, not bits.
389  CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
390  {return T::StaticGetValidKeyLength(keylength);}
391 };
392
393 // ************** implementation helper for SimpleKeyingInterface ***************
394
395 /// \class SimpleKeyingInterfaceImpl
396 /// \brief Provides a base implementation of SimpleKeyingInterface
397 /// \tparam BASE a SimpleKeyingInterface derived class
398 /// \tparam INFO a SimpleKeyingInterface derived class
399 /// \details SimpleKeyingInterfaceImpl() provides a default implementation for ciphers providing a keying interface.
400 /// Functions are virtual and not eligible for C++11 <tt>constexpr</tt>-ness.
401 /// \sa Algorithm(), SimpleKeyingInterface()
402 template <class BASE, class INFO = BASE>
403 class CRYPTOPP_NO_VTABLE SimpleKeyingInterfaceImpl : public BASE
404 {
405 public:
406  /// \brief The minimum key length used by the algorithm
407  /// \returns minimum key length used by the algorithm, in bytes
408  size_t MinKeyLength() const
409  {return INFO::MIN_KEYLENGTH;}
410
411  /// \brief The maximum key length used by the algorithm
412  /// \returns maximum key length used by the algorithm, in bytes
413  size_t MaxKeyLength() const
414  {return (size_t)INFO::MAX_KEYLENGTH;}
415
416  /// \brief The default key length used by the algorithm
417  /// \returns default key length used by the algorithm, in bytes
418  size_t DefaultKeyLength() const
419  {return INFO::DEFAULT_KEYLENGTH;}
420
421  /// \brief Provides a valid key length for the algorithm
422  /// \param keylength the size of the key, in bytes
423  /// \returns the valid key length, in bytes
424  /// \details keylength is provided in bytes, not bits. If keylength is less than MIN_KEYLENGTH,
425  /// then the function returns MIN_KEYLENGTH. If keylength is greater than MAX_KEYLENGTH,
426  /// then the function returns MAX_KEYLENGTH. if If keylength is a multiple of KEYLENGTH_MULTIPLE,
427  /// then keylength is returned. Otherwise, the function returns a \a lower multiple of
428  /// KEYLENGTH_MULTIPLE.
429  size_t GetValidKeyLength(size_t keylength) const {return INFO::StaticGetValidKeyLength(keylength);}
430
431  /// \brief The default IV requirements for the algorithm
432  /// \details The default value is NOT_RESYNCHRONIZABLE. See IV_Requirement
433  /// in cryptlib.h for allowed values.
435  {return (SimpleKeyingInterface::IV_Requirement)INFO::IV_REQUIREMENT;}
436
437  /// \brief The default initialization vector length for the algorithm
438  /// \details IVSize is provided in bytes, not bits. The default implementation uses IV_LENGTH, which is 0.
439  unsigned int IVSize() const
440  {return INFO::IV_LENGTH;}
441 };
442
443 /// \class BlockCipherImpl
444 /// \brief Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers
445 /// \tparam INFO a SimpleKeyingInterface derived class
446 /// \tparam BASE a SimpleKeyingInterface derived class
447 /// \details BlockCipherImpl() provides a default implementation for block ciphers using AlgorithmImpl()
448 /// and SimpleKeyingInterfaceImpl(). Functions are virtual and not eligible for C++11 <tt>constexpr</tt>-ness.
449 /// \sa Algorithm(), SimpleKeyingInterface(), AlgorithmImpl(), SimpleKeyingInterfaceImpl()
450 template <class INFO, class BASE = BlockCipher>
451 class CRYPTOPP_NO_VTABLE BlockCipherImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<BASE, INFO> > >
452 {
453 public:
454  /// Provides the block size of the algorithm
455  /// \returns the block size of the algorithm, in bytes
456  unsigned int BlockSize() const {return this->BLOCKSIZE;}
457 };
458
459 /// \class VariableBlockCipherImpl
460 /// \brief Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers with varibale block sizes
461 /// \tparam INFO a SimpleKeyingInterface derived class
462 /// \tparam BASE a SimpleKeyingInterface derived class
463 /// \details VariableBlockCipherImpl() provides a default implementation for block ciphers with varibale block sizes using AlgorithmImpl()
464 /// and SimpleKeyingInterfaceImpl().
465 /// \sa Algorithm(), SimpleKeyingInterface(), AlgorithmImpl(), SimpleKeyingInterfaceImpl()
466 template <class INFO, class BASE = BlockCipher>
467 class CRYPTOPP_NO_VTABLE VariableBlockCipherImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<BASE, INFO> > >
468 {
469 public:
470  VariableBlockCipherImpl() : m_blocksize(0), m_ivlength(0) {}
471  VariableBlockCipherImpl(unsigned int blockSize) : m_blocksize(blockSize), m_ivlength(blockSize) {}
472  VariableBlockCipherImpl(unsigned int blockSize, unsigned int ivLength) : m_blocksize(blockSize), m_ivlength(ivLength) {}
473
474  /// Provides the block size of the algorithm
475  /// \returns the block size, in bytes
476  unsigned int BlockSize() const {
477  return m_blocksize ? m_blocksize :
478  static_cast<unsigned int>(this->DEFAULT_BLOCKSIZE);
479  }
480
481  /// Provides the initialization vector length of the algorithm
482  /// \returns the initialization vector length, in bytes
483  unsigned int IVSize() const {
484  if (!this->IsResynchronizable())
485  throw NotImplemented(this->GetAlgorithm().AlgorithmName() + ": this object doesn't support resynchronization");
486  return m_ivlength ? m_ivlength :
487  static_cast<unsigned int>(this->IV_LENGTH);
488  }
489
490 protected:
491  unsigned int m_blocksize, m_ivlength;
492 };
493
494 /// \class BlockCipherFinal
495 /// \brief Provides class member functions to key a block cipher
496 /// \tparam DIR a CipherDir
497 /// \tparam BASE a BlockCipherImpl derived class
498 template <CipherDir DIR, class BASE>
499 class BlockCipherFinal : public ClonableImpl<BlockCipherFinal<DIR, BASE>, BASE>
500 {
501 public:
502  /// \brief Construct a default BlockCipherFinal
503  /// \details The cipher is not keyed.
505
506  /// \brief Construct a BlockCipherFinal
507  /// \param key a byte array used to key the cipher
508  /// \details key must be at least DEFAULT_KEYLENGTH in length. Internally, the function calls
509  /// SimpleKeyingInterface::SetKey.
510  BlockCipherFinal(const byte *key)
511  {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
512
513  /// \brief Construct a BlockCipherFinal
514  /// \param key a byte array used to key the cipher
515  /// \param length the length of the byte array
516  /// \details key must be at least DEFAULT_KEYLENGTH in length. Internally, the function calls
517  /// SimpleKeyingInterface::SetKey.
518  BlockCipherFinal(const byte *key, size_t length)
519  {this->SetKey(key, length);}
520
521  /// \brief Construct a BlockCipherFinal
522  /// \param key a byte array used to key the cipher
523  /// \param length the length of the byte array
524  /// \param rounds the number of rounds
525  /// \details key must be at least DEFAULT_KEYLENGTH in length. Internally, the function calls
526  /// SimpleKeyingInterface::SetKeyWithRounds.
527  BlockCipherFinal(const byte *key, size_t length, unsigned int rounds)
528  {this->SetKeyWithRounds(key, length, rounds);}
529
530  /// \brief Provides the direction of the cipher
531  /// \returns true if DIR is ENCRYPTION, false otherwise
532  /// \sa GetCipherDirection(), IsPermutation()
533  bool IsForwardTransformation() const {return DIR == ENCRYPTION;}
534 };
535
536 /// \class MessageAuthenticationCodeImpl
537 /// \brief Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication codes
538 /// \tparam INFO a SimpleKeyingInterface derived class
539 /// \tparam BASE a SimpleKeyingInterface derived class
540 /// \details MessageAuthenticationCodeImpl() provides a default implementation for message authentication codes
541 /// using AlgorithmImpl() and SimpleKeyingInterfaceImpl(). Functions are virtual and not subject to C++11
542 /// <tt>constexpr</tt>.
543 /// \sa Algorithm(), SimpleKeyingInterface(), AlgorithmImpl(), SimpleKeyingInterfaceImpl()
544 template <class BASE, class INFO = BASE>
545 class MessageAuthenticationCodeImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>
546 {
547 };
548
549 /// \class MessageAuthenticationCodeFinal
550 /// \brief Provides class member functions to key a message authentication code
551 /// \tparam BASE a BlockCipherImpl derived class
552 /// \details A default implementation for MessageAuthenticationCode
553 template <class BASE>
554 class MessageAuthenticationCodeFinal : public ClonableImpl<MessageAuthenticationCodeFinal<BASE>, MessageAuthenticationCodeImpl<BASE> >
555 {
556 public:
557  /// \brief Construct a default MessageAuthenticationCodeFinal
558  /// \details The message authentication code is not keyed.
560  /// \brief Construct a BlockCipherFinal
561  /// \param key a byte array used to key the algorithm
562  /// \details key must be at least DEFAULT_KEYLENGTH in length. Internally, the function calls
563  /// SimpleKeyingInterface::SetKey.
565  {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
566  /// \brief Construct a BlockCipherFinal
567  /// \param key a byte array used to key the algorithm
568  /// \param length the length of the byte array
569  /// \details key must be at least DEFAULT_KEYLENGTH in length. Internally, the function calls
570  /// SimpleKeyingInterface::SetKey.
571  MessageAuthenticationCodeFinal(const byte *key, size_t length)
572  {this->SetKey(key, length);}
573 };
574
575 // ************** documentation ***************
576
577 /// \class BlockCipherDocumentation
578 /// \brief Provides Encryption and Decryption typedefs used by derived classes to
579 /// implement a block cipher
580 /// \details These objects usually should not be used directly. See CipherModeDocumentation
581 /// instead. Each class derived from this one defines two types, Encryption and Decryption,
582 /// both of which implement the BlockCipher interface.
584 {
585  /// implements the BlockCipher interface
587  /// implements the BlockCipher interface
589 };
590
591 /// \class SymmetricCipherDocumentation
592 /// \brief Provides Encryption and Decryption typedefs used by derived classes to
593 /// implement a symmetric cipher
594 /// \details Each class derived from this one defines two types, Encryption and Decryption,
595 /// both of which implement the SymmetricCipher interface. Two types of classes derive
596 /// from this class: stream ciphers and block cipher modes. Stream ciphers can be used
597 /// alone, cipher mode classes need to be used with a block cipher. See CipherModeDocumentation
598 /// for more for information about using cipher modes and block ciphers.
600 {
601  /// implements the SymmetricCipher interface
603  /// implements the SymmetricCipher interface
605 };
606
607 /// \class AuthenticatedSymmetricCipherDocumentation
608 /// \brief Provides Encryption and Decryption typedefs used by derived classes to
609 /// implement an authenticated encryption cipher
610 /// \details Each class derived from this one defines two types, Encryption and Decryption,
611 /// both of which implement the AuthenticatedSymmetricCipher interface.
613 {
614  /// implements the AuthenticatedSymmetricCipher interface
616  /// implements the AuthenticatedSymmetricCipher interface
618 };
619
620 NAMESPACE_END
621
622 #if CRYPTOPP_MSC_VERSION
623 # pragma warning(pop)
624 #endif
625
626 // Issue 340
627 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
628 # pragma GCC diagnostic pop
629 #endif
630
631 #endif
int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Definition: cryptlib.h:397
the cipher is performing decryption
Definition: cryptlib.h:125
size_t MinKeyLength() const
The minimum key length used by the algorithm.
Definition: seckey.h:408
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:267
size_t DefaultKeyLength() const
The default key length used by the algorithm.
Definition: seckey.h:418
Classes providing basic library services.
Utility functions for the Crypto++ library.
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher...
Definition: seckey.h:583
MessageAuthenticationCodeFinal(const byte *key, size_t length)
Construct a BlockCipherFinal.
Definition: seckey.h:571
Interface for authenticated encryption modes of operation.
Definition: cryptlib.h:1277
Base class for identifying alogorithm.
Definition: simple.h:26
SimpleKeyingInterface::IV_Requirement IVRequirement() const
The default IV requirements for the algorithm.
Definition: seckey.h:434
Provides a base implementation of SimpleKeyingInterface.
Definition: seckey.h:403
CipherDir
Specifies a direction for a cipher to operate.
Definition: cryptlib.h:121
Abstract base classes that provide a uniform interface to this library.
Provides Encryption and Decryption typedefs used by derived classes to implement an authenticated enc...
Definition: seckey.h:612
Library configuration file.
Provides class member functions to key a message authentication code.
Definition: seckey.h:554
BlockCipherFinal(const byte *key)
Construct a BlockCipherFinal.
Definition: seckey.h:510
BlockCipher Decryption
implements the BlockCipher interface
Definition: seckey.h:588
size_t MaxKeyLength() const
The maximum key length used by the algorithm.
Definition: seckey.h:413
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1238
Exception thrown when an invalid block size is encountered.
Definition: simple.h:71
Inherited by algorithms with variable blocksize.
Definition: seckey.h:147
the cipher is performing encryption
Definition: cryptlib.h:123
Inherited by algorithms with fixed block size.
Definition: seckey.h:40
unsigned int IVSize() const
Provides the initialization vector length of the algorithm.
Definition: seckey.h:483
BlockCipher Encryption
implements the BlockCipher interface
Definition: seckey.h:586
Inherited by algorithms with variable number of rounds.
Definition: seckey.h:89
bool IsForwardTransformation() const
Provides the direction of the cipher.
Definition: seckey.h:533
A method was called which was not implemented.
Definition: cryptlib.h:222
BlockCipherFinal()
Construct a default BlockCipherFinal.
Definition: seckey.h:504
Exception thrown when an invalid number of rounds is encountered.
Definition: simple.h:63
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:144
static const int MIN_BLOCKSIZE
The minimum blocksize for the algorithm provided as a constant.
Definition: seckey.h:48
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
Definition: cryptlib.h:1246
Provides class member functions to key a block cipher.
Definition: seckey.h:499
unsigned int BlockSize() const
Provides the block size of the algorithm.
Definition: seckey.h:456
Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication code...
Definition: seckey.h:545
MessageAuthenticationCodeFinal(const byte *key)
Construct a BlockCipherFinal.
Definition: seckey.h:564
static size_t StaticGetValidBlockSize(size_t blocksize)
The default block size for the algorithm provided by a static function.
Definition: seckey.h:55
Provides key lengths based on another class&#39;s key length.
Definition: seckey.h:362
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers with varibale...
Definition: seckey.h:467
virtual std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: cryptlib.h:603
Inherited by algorithms with fixed number of rounds.
Definition: seckey.h:76
CipherDir ReverseCipherDir(CipherDir dir)
Inverts the cipher&#39;s direction.
Definition: seckey.h:31
size_t GetValidKeyLength(size_t keylength) const
Provides a valid key length for the algorithm.
Definition: seckey.h:429
SymmetricCipher Decryption
implements the SymmetricCipher interface
Definition: seckey.h:604
const char * BlockSize()
int, in bytes
Definition: argnames.h:27
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:309
Interface for all crypto algorithms.
Definition: cryptlib.h:582
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher...
Definition: seckey.h:599
unsigned int IVSize() const
The default initialization vector length for the algorithm.
Definition: seckey.h:439
AuthenticatedSymmetricCipher Encryption
implements the AuthenticatedSymmetricCipher interface
Definition: seckey.h:615
IV_Requirement
Secure IVs requirements as enumerated values.
Definition: cryptlib.h:683
static const int DEFAULT_BLOCKSIZE
The default blocksize for the algorithm provided as a constant.
Definition: seckey.h:46
BlockCipherFinal(const byte *key, size_t length, unsigned int rounds)
Construct a BlockCipherFinal.
Definition: seckey.h:527
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:451
Crypto++ library namespace.
static const int BLOCKSIZE
The block size of the algorithm provided as a constant.
Definition: seckey.h:44
AuthenticatedSymmetricCipher Decryption
implements the AuthenticatedSymmetricCipher interface
Definition: seckey.h:617
BlockCipherFinal(const byte *key, size_t length)
Construct a BlockCipherFinal.
Definition: seckey.h:518
static const int MAX_BLOCKSIZE
The maximum blocksize for the algorithm provided as a constant.
Definition: seckey.h:50
SymmetricCipher Encryption
implements the SymmetricCipher interface
Definition: seckey.h:602
MessageAuthenticationCodeFinal()
Construct a default MessageAuthenticationCodeFinal.
Definition: seckey.h:559
Interface for retrieving values given their names.
Definition: cryptlib.h:294
unsigned int BlockSize() const
Provides the block size of the algorithm.
Definition: seckey.h:476
Base class information.
Definition: simple.h:38