monotone

monotone Mtn Source Tree

Root/cryptopp/seckey.h

1// seckey.h - written and placed in the public domain by Wei Dai
2
3// This file contains helper classes/functions for implementing secret key algorithms.
4
5#ifndef CRYPTOPP_SECKEY_H
6#define CRYPTOPP_SECKEY_H
7
8#include "cryptlib.h"
9#include "misc.h"
10#include "simple.h"
11
12NAMESPACE_BEGIN(CryptoPP)
13
14inline CipherDir ReverseCipherDir(CipherDir dir)
15{
16return (dir == ENCRYPTION) ? DECRYPTION : ENCRYPTION;
17}
18
19//! .
20template <unsigned int N>
21class FixedBlockSize
22{
23public:
24enum {BLOCKSIZE = N};
25};
26
27// ************** rounds ***************
28
29//! .
30template <unsigned int R>
31class FixedRounds
32{
33public:
34enum {ROUNDS = R};
35
36protected:
37template <class T>
38static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, unsigned int length, const NameValuePairs &param)
39{
40obj->ThrowIfInvalidKeyLength(length);
41int rounds = param.GetIntValueWithDefault("Rounds", ROUNDS);
42if (rounds != ROUNDS)
43throw InvalidRounds(obj->StaticAlgorithmName(), rounds);
44obj->UncheckedSetKey(dir, key, length);
45}
46};
47
48//! .
49template <unsigned int D, unsigned int N=1, unsigned int M=INT_MAX>// use INT_MAX here because enums are treated as signed ints
50class VariableRounds
51{
52public:
53enum {DEFAULT_ROUNDS = D, MIN_ROUNDS = N, MAX_ROUNDS = M};
54static unsigned int StaticGetDefaultRounds(unsigned int keylength) {return DEFAULT_ROUNDS;}
55
56protected:
57static inline void AssertValidRounds(unsigned int rounds)
58{
59assert(rounds >= MIN_ROUNDS && rounds <= MAX_ROUNDS);
60}
61
62template <class T>
63static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, unsigned int length, const NameValuePairs &param)
64{
65obj->ThrowIfInvalidKeyLength(length);
66int rounds = param.GetIntValueWithDefault("Rounds", obj->StaticGetDefaultRounds(length));
67if (rounds < (unsigned int)MIN_ROUNDS || rounds > (unsigned int)MAX_ROUNDS)
68throw InvalidRounds(obj->AlgorithmName(), rounds);
69obj->UncheckedSetKey(dir, key, length, rounds);
70}
71};
72
73// ************** key length ***************
74
75//! .
76template <unsigned int N, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE>
77class FixedKeyLength
78{
79public:
80enum {KEYLENGTH=N, MIN_KEYLENGTH=N, MAX_KEYLENGTH=N, DEFAULT_KEYLENGTH=N};
81enum {IV_REQUIREMENT = IV_REQ};
82static unsigned int StaticGetValidKeyLength(unsigned int) {return KEYLENGTH;}
83};
84
85/// support query of variable key length, template parameters are default, min, max, multiple (default multiple 1)
86template <unsigned int D, unsigned int N, unsigned int M, unsigned int Q = 1, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE>
87class VariableKeyLength
88{
89// make these private to avoid Doxygen documenting them in all derived classes
90CRYPTOPP_COMPILE_ASSERT(Q > 0);
91CRYPTOPP_COMPILE_ASSERT(N % Q == 0);
92CRYPTOPP_COMPILE_ASSERT(M % Q == 0);
93CRYPTOPP_COMPILE_ASSERT(N < M);
94CRYPTOPP_COMPILE_ASSERT(D >= N && M >= D);
95
96public:
97enum {MIN_KEYLENGTH=N, MAX_KEYLENGTH=M, DEFAULT_KEYLENGTH=D, KEYLENGTH_MULTIPLE=Q};
98enum {IV_REQUIREMENT = IV_REQ};
99static unsigned int StaticGetValidKeyLength(unsigned int n)
100{
101if (n < (unsigned int)MIN_KEYLENGTH)
102return MIN_KEYLENGTH;
103else if (n > (unsigned int)MAX_KEYLENGTH)
104return (unsigned int)MAX_KEYLENGTH;
105else
106{
107n += KEYLENGTH_MULTIPLE-1;
108return n - n%KEYLENGTH_MULTIPLE;
109}
110}
111};
112
113/// support query of key length that's the same as another class
114template <class T>
115class SameKeyLengthAs
116{
117public:
118enum {MIN_KEYLENGTH=T::MIN_KEYLENGTH, MAX_KEYLENGTH=T::MAX_KEYLENGTH, DEFAULT_KEYLENGTH=T::DEFAULT_KEYLENGTH};
119enum {IV_REQUIREMENT = T::IV_REQUIREMENT};
120static unsigned int StaticGetValidKeyLength(unsigned int keylength)
121{return T::StaticGetValidKeyLength(keylength);}
122};
123
124// ************** implementation helper for SimpledKeyed ***************
125
126template <class T>
127static inline void CheckedSetKey(T *obj, Empty empty, const byte *key, unsigned int length, const NameValuePairs &param)
128{
129obj->ThrowIfInvalidKeyLength(length);
130obj->UncheckedSetKey(key, length);
131}
132
133template <class T>
134static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, unsigned int length, const NameValuePairs &param)
135{
136obj->ThrowIfInvalidKeyLength(length);
137obj->UncheckedSetKey(dir, key, length);
138}
139
140//! .
141template <class BASE, class INFO = BASE>
142class SimpleKeyingInterfaceImpl : public BASE
143{
144public:
145unsigned int MinKeyLength() const {return INFO::MIN_KEYLENGTH;}
146unsigned int MaxKeyLength() const {return (unsigned int)INFO::MAX_KEYLENGTH;}
147unsigned int DefaultKeyLength() const {return INFO::DEFAULT_KEYLENGTH;}
148unsigned int GetValidKeyLength(unsigned int n) const {return INFO::StaticGetValidKeyLength(n);}
149typename BASE::IV_Requirement IVRequirement() const {return (typename BASE::IV_Requirement)INFO::IV_REQUIREMENT;}
150
151protected:
152void AssertValidKeyLength(unsigned int length) {assert(GetValidKeyLength(length) == length);}
153};
154
155template <class INFO, class INTERFACE = BlockCipher>
156class BlockCipherBaseTemplate : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<INFO, INTERFACE> > >
157{
158public:
159unsigned int BlockSize() const {return BLOCKSIZE;}
160};
161
162//! .
163template <CipherDir DIR, class BASE>
164class BlockCipherTemplate : public BASE
165{
166public:
167 BlockCipherTemplate() {}
168BlockCipherTemplate(const byte *key)
169{SetKey(key, DEFAULT_KEYLENGTH);}
170BlockCipherTemplate(const byte *key, unsigned int length)
171{SetKey(key, length);}
172BlockCipherTemplate(const byte *key, unsigned int length, unsigned int rounds)
173{SetKeyWithRounds(key, length, rounds);}
174
175bool IsForwardTransformation() const {return DIR == ENCRYPTION;}
176
177void SetKey(const byte *key, unsigned int length, const NameValuePairs &param = g_nullNameValuePairs)
178{
179CheckedSetKey(this, DIR, key, length, param);
180}
181
182Clonable * Clone() const {return new BlockCipherTemplate<DIR, BASE>(*this);}
183};
184
185//! .
186template <class BASE>
187class MessageAuthenticationCodeTemplate : public
188#ifdef CRYPTOPP_DOXYGEN_PROCESSING
189MessageAuthenticationCode
190#else
191SimpleKeyingInterfaceImpl<BASE>
192#endif
193{
194public:
195 MessageAuthenticationCodeTemplate() {}
196MessageAuthenticationCodeTemplate(const byte *key)
197{SetKey(key, DEFAULT_KEYLENGTH);}
198MessageAuthenticationCodeTemplate(const byte *key, unsigned int length)
199{SetKey(key, length);}
200
201std::string AlgorithmName() const {return StaticAlgorithmName();}
202
203void SetKey(const byte *key, unsigned int length, const NameValuePairs &param = g_nullNameValuePairs)
204{
205CheckedSetKey(this, Empty(), key, length, param);
206}
207
208Clonable * Clone() const {return new MessageAuthenticationCodeTemplate<BASE>(*this);}
209};
210
211// ************** documentation ***************
212
213//! These objects usually should not be used directly. See CipherModeDocumentation instead.
214/*! Each class derived from this one defines two types, Encryption and Decryption,
215both of which implement the BlockCipher interface. */
216struct BlockCipherDocumentation
217{
218//! implements the BlockCipher interface
219typedef BlockCipher Encryption;
220//! implements the BlockCipher interface
221typedef BlockCipher Decryption;
222};
223
224/*! \brief Each class derived from this one defines two types, Encryption and Decryption,
225both of which implement the SymmetricCipher interface. See CipherModeDocumentation
226for information about using block ciphers. */
227struct SymmetricCipherDocumentation
228{
229//! implements the SymmetricCipher interface
230typedef SymmetricCipher Encryption;
231//! implements the SymmetricCipher interface
232typedef SymmetricCipher Decryption;
233};
234
235NAMESPACE_END
236
237#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status