Botan  1.11.15
src/lib/base/sym_algo.h
Go to the documentation of this file.
00001 /*
00002 * Symmetric Algorithm Base Class
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_SYMMETRIC_ALGORITHM_H__
00009 #define BOTAN_SYMMETRIC_ALGORITHM_H__
00010 
00011 #include <botan/key_spec.h>
00012 #include <botan/exceptn.h>
00013 #include <botan/symkey.h>
00014 #include <botan/types.h>
00015 
00016 namespace Botan {
00017 
00018 /**
00019 * This class represents a symmetric algorithm object.
00020 */
00021 class BOTAN_DLL SymmetricAlgorithm
00022    {
00023    public:
00024       virtual ~SymmetricAlgorithm() {}
00025 
00026       virtual void clear() = 0;
00027 
00028       /**
00029       * @return object describing limits on key size
00030       */
00031       virtual Key_Length_Specification key_spec() const = 0;
00032 
00033       /**
00034       * @return minimum allowed key length
00035       */
00036       size_t maximum_keylength() const
00037          {
00038          return key_spec().maximum_keylength();
00039          }
00040 
00041       /**
00042       * @return maxmium allowed key length
00043       */
00044       size_t minimum_keylength() const
00045          {
00046          return key_spec().minimum_keylength();
00047          }
00048 
00049       /**
00050       * Check whether a given key length is valid for this algorithm.
00051       * @param length the key length to be checked.
00052       * @return true if the key length is valid.
00053       */
00054       bool valid_keylength(size_t length) const
00055          {
00056          return key_spec().valid_keylength(length);
00057          }
00058 
00059       /**
00060       * Set the symmetric key of this object.
00061       * @param key the SymmetricKey to be set.
00062       */
00063       void set_key(const SymmetricKey& key)
00064          {
00065          set_key(key.begin(), key.length());
00066          }
00067 
00068       template<typename Alloc>
00069       void set_key(const std::vector<byte, Alloc>& key)
00070          {
00071          set_key(&key[0], key.size());
00072          }
00073 
00074       /**
00075       * Set the symmetric key of this object.
00076       * @param key the to be set as a byte array.
00077       * @param length in bytes of key param
00078       */
00079       void set_key(const byte key[], size_t length)
00080          {
00081          if(!valid_keylength(length))
00082             throw Invalid_Key_Length(name(), length);
00083          key_schedule(key, length);
00084          }
00085 
00086       virtual std::string name() const = 0;
00087 
00088    private:
00089       /**
00090       * Run the key schedule
00091       * @param key the key
00092       * @param length of key
00093       */
00094       virtual void key_schedule(const byte key[], size_t length) = 0;
00095    };
00096 
00097 }
00098 
00099 #endif