diff options
author | lloyd <[email protected]> | 2010-10-28 21:15:21 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-10-28 21:15:21 +0000 |
commit | 22f02b418f7f53431da168abe9fb74f15bf3cb0e (patch) | |
tree | cdc81938c979403d20a438d134bbd6d64479f17d /src/sym_algo | |
parent | a7a047e6823dcbf23e172dd5c0f9a7b4fd748f10 (diff) |
Eliminate the constant size_t values in SymmetricAlgorithm that give
the parameters of the key length. Instead define a new function which
returns a simple object which contains this information.
This definitely breaks backwards compatability, though only with code
that directly manipulates low level objects like BlockCipher*s
directly, which is probably relatively rare.
Also remove some deprecated accessor functions from lookup.h. It turns
out block_size_of and output_size_of are being used in the TLS code; I
need to remove them from there before I can delete these entirely.
Really that didn't make much sense, because they assumed all
implementations of a particular algorithm will have the same
specifications, which is definitely not necessarily true, especially
WRT key length. It is much safer (and probably simpler) to first
retrieve an instance of the actual object you are going to use and
then ask it directly.
Diffstat (limited to 'src/sym_algo')
-rw-r--r-- | src/sym_algo/key_spec.h | 62 | ||||
-rw-r--r-- | src/sym_algo/sym_algo.h | 61 |
2 files changed, 91 insertions, 32 deletions
diff --git a/src/sym_algo/key_spec.h b/src/sym_algo/key_spec.h new file mode 100644 index 000000000..7788bb988 --- /dev/null +++ b/src/sym_algo/key_spec.h @@ -0,0 +1,62 @@ +/* +* Symmetric Key Length Specification +* (C) 2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_KEY_LEN_SPECIFICATION_H__ +#define BOTAN_KEY_LEN_SPECIFICATION_H__ + +#include <botan/types.h> + +namespace Botan { + +class BOTAN_DLL Key_Length_Specification + { + public: + Key_Length_Specification(size_t keylen) : + min_keylen(keylen), + max_keylen(keylen), + keylen_mod(1) + { + } + + Key_Length_Specification(size_t min_k, + size_t max_k, + size_t k_mod = 1) : + min_keylen(min_k), + max_keylen(max_k ? max_k : min_k), + keylen_mod(k_mod) + { + } + + bool valid_keylength(size_t length) const + { + return ((length >= min_keylen) && + (length <= max_keylen) && + (length % keylen_mod == 0)); + } + + size_t minimum_keylength() const + { + return min_keylen; + } + + size_t maximum_keylength() const + { + return max_keylen; + } + + size_t keylength_multiple() const + { + return keylen_mod; + } + + private: + size_t min_keylen, max_keylen, keylen_mod; + }; + +} + +#endif diff --git a/src/sym_algo/sym_algo.h b/src/sym_algo/sym_algo.h index 0a1423f13..aea0d06ba 100644 --- a/src/sym_algo/sym_algo.h +++ b/src/sym_algo/sym_algo.h @@ -9,6 +9,7 @@ #define BOTAN_SYMMETRIC_ALGORITHM_H__ #include <botan/types.h> +#include <botan/key_spec.h> #include <botan/exceptn.h> #include <botan/symkey.h> @@ -20,21 +21,43 @@ namespace Botan { class BOTAN_DLL SymmetricAlgorithm { public: + virtual ~SymmetricAlgorithm() {} + + /** + * Zeroize internal state + */ + virtual void clear() = 0; /** - * The maximum allowed key length. + * @return object describing limits on key size */ - const size_t MAXIMUM_KEYLENGTH; + virtual Key_Length_Specification key_spec() const = 0; /** - * The minimal allowed key length. + * @return minimum allowed key length */ - const size_t MINIMUM_KEYLENGTH; + size_t maximum_keylength() const + { + return key_spec().maximum_keylength(); + } /** - * A valid keylength is a multiple of this value. + * @return maxmium allowed key length */ - const size_t KEYLENGTH_MULTIPLE; + size_t minimum_keylength() const + { + return key_spec().minimum_keylength(); + } + + /** + * 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(size_t length) const + { + return key_spec().valid_keylength(length); + } /** * The name of the algorithm. @@ -60,32 +83,6 @@ class BOTAN_DLL SymmetricAlgorithm throw Invalid_Key_Length(name(), length); key_schedule(key, 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(size_t 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(size_t key_min, size_t key_max, size_t key_mod) : - MAXIMUM_KEYLENGTH(key_max ? key_max : key_min), - MINIMUM_KEYLENGTH(key_min), - KEYLENGTH_MULTIPLE(key_mod) - {} - - virtual ~SymmetricAlgorithm() {} private: /** * Run the key schedule |