Security Scol plugin
seckey.h
Go to the documentation of this file.
1// seckey.h - originally 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#include "stdcpp.h"
14
15#if CRYPTOPP_MSC_VERSION
16# pragma warning(push)
17# pragma warning(disable: 4189 4296)
18#endif
19
20// Issue 340
21#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
22# pragma GCC diagnostic push
23# pragma GCC diagnostic ignored "-Wconversion"
24# pragma GCC diagnostic ignored "-Wsign-conversion"
25#endif
26
27NAMESPACE_BEGIN(CryptoPP)
28
29
33{
34 return (dir == ENCRYPTION) ? DECRYPTION : ENCRYPTION;
35}
36
39template <unsigned int N>
41{
42public:
44 CRYPTOPP_CONSTANT(BLOCKSIZE = N);
45};
46
47// ************** rounds ***************
48
51template <unsigned int R>
53{
54public:
56 CRYPTOPP_CONSTANT(ROUNDS = R);
57};
58
63template <unsigned int D, unsigned int N=1, unsigned int M=INT_MAX> // use INT_MAX here because enums are treated as signed ints
65{
66public:
68 CRYPTOPP_CONSTANT(DEFAULT_ROUNDS = D);
70 CRYPTOPP_CONSTANT(MIN_ROUNDS = N);
72 CRYPTOPP_CONSTANT(MAX_ROUNDS = M);
77 CRYPTOPP_STATIC_CONSTEXPR unsigned int StaticGetDefaultRounds(size_t keylength)
78 {
79 return CRYPTOPP_UNUSED(keylength), static_cast<unsigned int>(DEFAULT_ROUNDS);
80 }
81
82protected:
88 inline void ThrowIfInvalidRounds(int rounds, const Algorithm *alg)
89 {
90 if (M == INT_MAX) // Coverity and result_independent_of_operands
91 {
92 if (rounds < MIN_ROUNDS)
93 throw InvalidRounds(alg ? alg->AlgorithmName() : std::string("VariableRounds"), rounds);
94 }
95 else
96 {
97 if (rounds < MIN_ROUNDS || rounds > MAX_ROUNDS)
98 throw InvalidRounds(alg ? alg->AlgorithmName() : std::string("VariableRounds"), rounds);
99 }
100 }
101
108 inline unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs &param, const Algorithm *alg)
109 {
110 int rounds = param.GetIntValueWithDefault("Rounds", DEFAULT_ROUNDS);
111 ThrowIfInvalidRounds(rounds, alg);
112 return static_cast<unsigned int>(rounds);
113 }
114};
115
116// ************** key length ***************
117
123template <unsigned int N, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE, unsigned int IV_L = 0>
125{
126public:
129 CRYPTOPP_CONSTANT(KEYLENGTH=N);
132 CRYPTOPP_CONSTANT(MIN_KEYLENGTH=N);
135 CRYPTOPP_CONSTANT(MAX_KEYLENGTH=N);
138 CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=N);
142 CRYPTOPP_CONSTANT(IV_REQUIREMENT = IV_REQ);
145 CRYPTOPP_CONSTANT(IV_LENGTH = IV_L);
150 CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
151 {
152 return CRYPTOPP_UNUSED(keylength), static_cast<size_t>(KEYLENGTH);
153 }
154};
155
164template <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>
166{
167 // Make these private to avoid Doxygen documenting them in all derived classes
168 CRYPTOPP_COMPILE_ASSERT(Q > 0);
169 CRYPTOPP_COMPILE_ASSERT(N % Q == 0);
170 CRYPTOPP_COMPILE_ASSERT(M % Q == 0);
171 CRYPTOPP_COMPILE_ASSERT(N < M);
172 CRYPTOPP_COMPILE_ASSERT(D >= N);
173 CRYPTOPP_COMPILE_ASSERT(M >= D);
174
175public:
178 CRYPTOPP_CONSTANT(MIN_KEYLENGTH=N);
181 CRYPTOPP_CONSTANT(MAX_KEYLENGTH=M);
184 CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=D);
187 CRYPTOPP_CONSTANT(KEYLENGTH_MULTIPLE=Q);
191 CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ);
194 CRYPTOPP_CONSTANT(IV_LENGTH=IV_L);
203 CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
204 {
205 return (keylength <= N) ? N :
206 (keylength >= M) ? M :
207 (keylength+Q-1) - (keylength+Q-1)%Q;
208 }
209};
210
216template <class T, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE, unsigned int IV_L = 0>
218{
219public:
222 CRYPTOPP_CONSTANT(MIN_KEYLENGTH=T::MIN_KEYLENGTH);
225 CRYPTOPP_CONSTANT(MAX_KEYLENGTH=T::MAX_KEYLENGTH);
228 CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=T::DEFAULT_KEYLENGTH);
232 CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ);
235 CRYPTOPP_CONSTANT(IV_LENGTH=IV_L);
244 CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
245 {return T::StaticGetValidKeyLength(keylength);}
246};
247
248// ************** implementation helper for SimpleKeyingInterface ***************
249
256template <class BASE, class INFO = BASE>
257class CRYPTOPP_NO_VTABLE SimpleKeyingInterfaceImpl : public BASE
258{
259public:
262 size_t MinKeyLength() const
263 {return INFO::MIN_KEYLENGTH;}
264
267 size_t MaxKeyLength() const
268 {return static_cast<size_t>(INFO::MAX_KEYLENGTH);}
269
272 size_t DefaultKeyLength() const
273 {return INFO::DEFAULT_KEYLENGTH;}
274
283 size_t GetValidKeyLength(size_t keylength) const {return INFO::StaticGetValidKeyLength(keylength);}
284
289 {return static_cast<SimpleKeyingInterface::IV_Requirement>(INFO::IV_REQUIREMENT);}
290
294 unsigned int IVSize() const
295 {return INFO::IV_LENGTH;}
296};
297
304template <class INFO, class BASE = BlockCipher>
305class CRYPTOPP_NO_VTABLE BlockCipherImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<BASE, INFO> > >
306{
307public:
310 unsigned int BlockSize() const {return this->BLOCKSIZE;}
311};
312
316template <CipherDir DIR, class BASE>
317class BlockCipherFinal : public ClonableImpl<BlockCipherFinal<DIR, BASE>, BASE>
318{
319public:
323
328 BlockCipherFinal(const byte *key)
329 {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
330
336 BlockCipherFinal(const byte *key, size_t length)
337 {this->SetKey(key, length);}
338
345 BlockCipherFinal(const byte *key, size_t length, unsigned int rounds)
346 {this->SetKeyWithRounds(key, length, rounds);}
347
351 bool IsForwardTransformation() const {return DIR == ENCRYPTION;}
352};
353
361template <class BASE, class INFO = BASE>
362class MessageAuthenticationCodeImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>
363{
364};
365
369template <class BASE>
370class MessageAuthenticationCodeFinal : public ClonableImpl<MessageAuthenticationCodeFinal<BASE>, MessageAuthenticationCodeImpl<BASE> >
371{
372public:
381 {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
387 MessageAuthenticationCodeFinal(const byte *key, size_t length)
388 {this->SetKey(key, length);}
389};
390
391// ************** documentation ***************
392
405
420
432
433NAMESPACE_END
434
435#if CRYPTOPP_MSC_VERSION
436# pragma warning(pop)
437#endif
438
439// Issue 340
440#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
441# pragma GCC diagnostic pop
442#endif
443
444#endif
Interface for all crypto algorithms.
Definition cryptlib.h:599
virtual std::string AlgorithmName() const
Provides the name of this algorithm.
Definition cryptlib.h:619
Base class information.
Definition simple.h:40
Interface for authenticated encryption modes of operation.
Definition cryptlib.h:1321
Provides class member functions to key a block cipher.
Definition seckey.h:318
bool IsForwardTransformation() const
Provides the direction of the cipher.
Definition seckey.h:351
BlockCipherFinal()
Construct a default BlockCipherFinal.
Definition seckey.h:322
BlockCipherFinal(const byte *key)
Construct a BlockCipherFinal.
Definition seckey.h:328
BlockCipherFinal(const byte *key, size_t length, unsigned int rounds)
Construct a BlockCipherFinal.
Definition seckey.h:345
BlockCipherFinal(const byte *key, size_t length)
Construct a BlockCipherFinal.
Definition seckey.h:336
Interface for one direction (encryption or decryption) of a block cipher.
Definition cryptlib.h:1283
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition seckey.h:306
unsigned int BlockSize() const
Definition seckey.h:310
Base class for identifying algorithm.
Definition simple.h:26
Inherited by algorithms with fixed block size.
Definition seckey.h:41
CRYPTOPP_CONSTANT(BLOCKSIZE=N)
The block size of the algorithm provided as a constant.
Inherited by keyed algorithms with fixed key length.
Definition seckey.h:125
CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ)
The default IV requirements for the algorithm provided as a constant.
CRYPTOPP_CONSTANT(IV_LENGTH=IV_L)
The default IV length used by the algorithm provided as a constant.
CRYPTOPP_CONSTANT(MAX_KEYLENGTH=N)
The maximum key length used by the algorithm provided as a constant.
CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
The default key length for the algorithm provided by a static function.
Definition seckey.h:150
CRYPTOPP_CONSTANT(KEYLENGTH=N)
The default key length used by the algorithm provided as a constant.
CRYPTOPP_CONSTANT(MIN_KEYLENGTH=N)
The minimum key length used by the algorithm provided as a constant.
CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=N)
The default key length used by the algorithm provided as a constant.
Inherited by algorithms with fixed number of rounds.
Definition seckey.h:53
CRYPTOPP_CONSTANT(ROUNDS=R)
The number of rounds for the algorithm provided as a constant.
Exception thrown when an invalid number of rounds is encountered.
Definition simple.h:66
Provides class member functions to key a message authentication code.
Definition seckey.h:371
MessageAuthenticationCodeFinal(const byte *key, size_t length)
Construct a BlockCipherFinal.
Definition seckey.h:387
MessageAuthenticationCodeFinal(const byte *key)
Construct a BlockCipherFinal.
Definition seckey.h:380
MessageAuthenticationCodeFinal()
Construct a default MessageAuthenticationCodeFinal.
Definition seckey.h:375
Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication code...
Definition seckey.h:363
Interface for retrieving values given their names.
Definition cryptlib.h:322
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Definition cryptlib.h:424
Provides key lengths based on another class's key length.
Definition seckey.h:218
CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ)
The default IV requirements for the algorithm provided as a constant.
CRYPTOPP_CONSTANT(MIN_KEYLENGTH=T::MIN_KEYLENGTH)
The minimum key length used by the algorithm provided as a constant.
CRYPTOPP_CONSTANT(MAX_KEYLENGTH=T::MAX_KEYLENGTH)
The maximum key length used by the algorithm provided as a constant.
CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
Provides a valid key length for the algorithm provided by a static function.
Definition seckey.h:244
CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=T::DEFAULT_KEYLENGTH)
The default key length used by the algorithm provided as a constant.
CRYPTOPP_CONSTANT(IV_LENGTH=IV_L)
The default initialization vector length for the algorithm provided as a constant.
IV_Requirement
Secure IVs requirements as enumerated values.
Definition cryptlib.h:719
Provides a base implementation of SimpleKeyingInterface.
Definition seckey.h:258
size_t DefaultKeyLength() const
The default key length used by the algorithm.
Definition seckey.h:272
size_t GetValidKeyLength(size_t keylength) const
Provides a valid key length for the algorithm.
Definition seckey.h:283
size_t MaxKeyLength() const
The maximum key length used by the algorithm.
Definition seckey.h:267
unsigned int IVSize() const
The initialization vector length for the algorithm.
Definition seckey.h:294
size_t MinKeyLength() const
The minimum key length used by the algorithm.
Definition seckey.h:262
SimpleKeyingInterface::IV_Requirement IVRequirement() const
The default IV requirements for the algorithm.
Definition seckey.h:288
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode.
Definition cryptlib.h:1291
Inherited by keyed algorithms with variable key length.
Definition seckey.h:166
CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ)
The default IV requirements for the algorithm provided as a constant.
CRYPTOPP_CONSTANT(KEYLENGTH_MULTIPLE=Q)
The key length multiple used by the algorithm provided as a constant.
CRYPTOPP_CONSTANT(MIN_KEYLENGTH=N)
The minimum key length used by the algorithm provided as a constant.
CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=D)
The default key length used by the algorithm provided as a constant.
CRYPTOPP_CONSTANT(MAX_KEYLENGTH=M)
The maximum key length used by the algorithm provided as a constant.
CRYPTOPP_STATIC_CONSTEXPR size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
Provides a valid key length for the algorithm provided by a static function.
Definition seckey.h:203
CRYPTOPP_CONSTANT(IV_LENGTH=IV_L)
The default initialization vector length for the algorithm provided as a constant.
Inherited by algorithms with variable number of rounds.
Definition seckey.h:65
CRYPTOPP_CONSTANT(DEFAULT_ROUNDS=D)
The default number of rounds for the algorithm provided as a constant.
unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs &param, const Algorithm *alg)
Validates the number of rounds for an algorithm.
Definition seckey.h:108
CRYPTOPP_CONSTANT(MAX_ROUNDS=M)
The maximum number of rounds for the algorithm provided as a constant.
void ThrowIfInvalidRounds(int rounds, const Algorithm *alg)
Validates the number of rounds for an algorithm.
Definition seckey.h:88
CRYPTOPP_STATIC_CONSTEXPR unsigned int StaticGetDefaultRounds(size_t keylength)
The default number of rounds for the algorithm based on key length provided by a static function.
Definition seckey.h:77
CRYPTOPP_CONSTANT(MIN_ROUNDS=N)
The minimum number of rounds for the algorithm provided as a constant.
Library configuration file.
Abstract base classes that provide a uniform interface to this library.
CipherDir
Specifies a direction for a cipher to operate.
Definition cryptlib.h:123
@ ENCRYPTION
the cipher is performing encryption
Definition cryptlib.h:125
@ DECRYPTION
the cipher is performing decryption
Definition cryptlib.h:127
Utility functions for the Crypto++ library.
CipherDir ReverseCipherDir(CipherDir dir)
Inverts the cipher's direction.
Definition seckey.h:32
Classes providing basic library services.
Common C++ header files.
Provides Encryption and Decryption typedefs used by derived classes to implement an authenticated enc...
Definition seckey.h:426
AuthenticatedSymmetricCipher Decryption
implements the AuthenticatedSymmetricCipher interface
Definition seckey.h:430
AuthenticatedSymmetricCipher Encryption
implements the AuthenticatedSymmetricCipher interface
Definition seckey.h:428
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher.
Definition seckey.h:399
BlockCipher Decryption
implements the BlockCipher interface
Definition seckey.h:403
BlockCipher Encryption
implements the BlockCipher interface
Definition seckey.h:401
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher.
Definition seckey.h:414
SymmetricCipher Decryption
implements the SymmetricCipher interface
Definition seckey.h:418
SymmetricCipher Encryption
implements the SymmetricCipher interface
Definition seckey.h:416