diff options
author | lloyd <[email protected]> | 2008-11-08 19:42:16 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-11-08 19:42:16 +0000 |
commit | 8dba7b5264403e781bbb86ff61850e4377dca7b9 (patch) | |
tree | 5e784f8e4bbf0ed423e850600adaef3eda4cd07d /src/core/sym_algo.h | |
parent | 8f86d41613db588e4912fd23a61f7c6b3947e32a (diff) |
Split base.h into block_cipher.h and stream_cipher.h
It turned out many files were including base.h merely to get other
includes (like types.h, secmem.h, and exceptn.h). Those have been changed
to directly include the files containing the declarations that code needs.
Diffstat (limited to 'src/core/sym_algo.h')
-rw-r--r-- | src/core/sym_algo.h | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/core/sym_algo.h b/src/core/sym_algo.h new file mode 100644 index 000000000..02343ed56 --- /dev/null +++ b/src/core/sym_algo.h @@ -0,0 +1,93 @@ +/** +* Symmetric Algorithm Base Class +* (C) 1999-2007 Jack Lloyd +*/ + +#ifndef BOTAN_SYMMETRIC_ALGORITHM_H__ +#define BOTAN_SYMMETRIC_ALGORITHM_H__ + +#include <botan/types.h> +#include <botan/exceptn.h> +#include <botan/symkey.h> + +namespace Botan { + +/** +* This class represents a symmetric algorithm object. +*/ +class BOTAN_DLL SymmetricAlgorithm + { + public: + + /** + * The maximum allowed key length. + */ + const u32bit MAXIMUM_KEYLENGTH; + + /** + * The minimal allowed key length. + */ + const u32bit MINIMUM_KEYLENGTH; + + /** + * A valid keylength is a multiple of this value. + */ + const u32bit KEYLENGTH_MULTIPLE; + + /** + * The name of the algorithm. + * @return the name of the algorithm + */ + virtual std::string name() const = 0; + + /** + * Set the symmetric key of this object. + * @param key the SymmetricKey to be set. + */ + void set_key(const SymmetricKey& skey) throw(Invalid_Key_Length) + { set_key(skey.begin(), skey.length()); } + + /** + * Set the symmetric key of this object. + * @param key the to be set as a byte array. + * @param the length of the byte array. + */ + void set_key(const byte skey[], u32bit length) throw(Invalid_Key_Length) + { + if(!valid_keylength(length)) + throw Invalid_Key_Length(name(), length); + key(skey, length); + } + + /** + * Check whether a given key length is valid for this algorithm. + * @param length the key length to be checked. + * @return true if the key length is valid. + */ + bool valid_keylength(u32bit length) const + { + return ((length >= MINIMUM_KEYLENGTH) && + (length <= MAXIMUM_KEYLENGTH) && + (length % KEYLENGTH_MULTIPLE == 0)); + } + + /** + * Construct a SymmetricAlgorithm. + * @param key_min the minimum allowed key length + * @param key_max the maximum allowed key length + * @param key_mod any valid key length must be a multiple of this value + */ + SymmetricAlgorithm(u32bit key_min, u32bit key_max, u32bit key_mod) : + MAXIMUM_KEYLENGTH(key_max ? key_max : key_min), + MINIMUM_KEYLENGTH(key_min), + KEYLENGTH_MULTIPLE(key_mod) + {} + + virtual ~SymmetricAlgorithm() {} + private: + virtual void key(const byte[], u32bit) = 0; + }; + +} + +#endif |