Botan  1.11.15
src/lib/base/key_spec.h
Go to the documentation of this file.
00001 /*
00002 * Symmetric Key Length Specification
00003 * (C) 2010 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_KEY_LEN_SPECIFICATION_H__
00009 #define BOTAN_KEY_LEN_SPECIFICATION_H__
00010 
00011 #include <botan/types.h>
00012 
00013 namespace Botan {
00014 
00015 /**
00016 * Represents the length requirements on an algorithm key
00017 */
00018 class BOTAN_DLL Key_Length_Specification
00019    {
00020    public:
00021       /**
00022       * Constructor for fixed length keys
00023       * @param keylen the supported key length
00024       */
00025       Key_Length_Specification(size_t keylen) :
00026          min_keylen(keylen),
00027          max_keylen(keylen),
00028          keylen_mod(1)
00029          {
00030          }
00031 
00032       /**
00033       * Constructor for variable length keys
00034       * @param min_k the smallest supported key length
00035       * @param max_k the largest supported key length
00036       * @param k_mod the number of bytes the key must be a multiple of
00037       */
00038       Key_Length_Specification(size_t min_k,
00039                                size_t max_k,
00040                                size_t k_mod = 1) :
00041          min_keylen(min_k),
00042          max_keylen(max_k ? max_k : min_k),
00043          keylen_mod(k_mod)
00044          {
00045          }
00046 
00047       /**
00048       * @param length is a key length in bytes
00049       * @return true iff this length is a valid length for this algo
00050       */
00051       bool valid_keylength(size_t length) const
00052          {
00053          return ((length >= min_keylen) &&
00054                  (length <= max_keylen) &&
00055                  (length % keylen_mod == 0));
00056          }
00057 
00058       /**
00059       * @return minimum key length in bytes
00060       */
00061       size_t minimum_keylength() const
00062          {
00063          return min_keylen;
00064          }
00065 
00066       /**
00067       * @return maximum key length in bytes
00068       */
00069       size_t maximum_keylength() const
00070          {
00071          return max_keylen;
00072          }
00073 
00074       /**
00075       * @return key length multiple in bytes
00076       */
00077       size_t keylength_multiple() const
00078          {
00079          return keylen_mod;
00080          }
00081 
00082       Key_Length_Specification multiple(size_t n) const
00083          {
00084          return Key_Length_Specification(n * min_keylen,
00085                                          n * max_keylen,
00086                                          n * keylen_mod);
00087          }
00088 
00089    private:
00090       size_t min_keylen, max_keylen, keylen_mod;
00091    };
00092 
00093 }
00094 
00095 #endif