Botan  2.7.0
Crypto and TLS for C++11
sym_algo.h
Go to the documentation of this file.
1 /*
2 * Symmetric Algorithm Base Class
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_SYMMETRIC_ALGORITHM_H_
9 #define BOTAN_SYMMETRIC_ALGORITHM_H_
10 
11 #include <botan/key_spec.h>
12 #include <botan/symkey.h>
13 #include <botan/types.h>
14 
15 namespace Botan {
16 
17 /**
18 * This class represents a symmetric algorithm object.
19 */
21  {
22  public:
23  virtual ~SymmetricAlgorithm() = default;
24 
25  /**
26  * Reset the state.
27  */
28  virtual void clear() = 0;
29 
30  /**
31  * @return object describing limits on key size
32  */
33  virtual Key_Length_Specification key_spec() const = 0;
34 
35  /**
36  * @return minimum allowed key length
37  */
38  size_t maximum_keylength() const
39  {
40  return key_spec().maximum_keylength();
41  }
42 
43  /**
44  * @return maximum allowed key length
45  */
46  size_t minimum_keylength() const
47  {
48  return key_spec().minimum_keylength();
49  }
50 
51  /**
52  * Check whether a given key length is valid for this algorithm.
53  * @param length the key length to be checked.
54  * @return true if the key length is valid.
55  */
56  bool valid_keylength(size_t length) const
57  {
58  return key_spec().valid_keylength(length);
59  }
60 
61  /**
62  * Set the symmetric key of this object.
63  * @param key the SymmetricKey to be set.
64  */
65  void set_key(const SymmetricKey& key)
66  {
67  set_key(key.begin(), key.length());
68  }
69 
70  template<typename Alloc>
71  void set_key(const std::vector<uint8_t, Alloc>& key)
72  {
73  set_key(key.data(), key.size());
74  }
75 
76  /**
77  * Set the symmetric key of this object.
78  * @param key the to be set as a byte array.
79  * @param length in bytes of key param
80  */
81  void set_key(const uint8_t key[], size_t length);
82 
83  /**
84  * @return the algorithm name
85  */
86  virtual std::string name() const = 0;
87 
88  protected:
89  void verify_key_set(bool cond) const
90  {
91  if(cond == false)
92  throw_key_not_set_error();
93  }
94 
95  private:
96  void throw_key_not_set_error() const;
97 
98  /**
99  * Run the key schedule
100  * @param key the key
101  * @param length of key
102  */
103  virtual void key_schedule(const uint8_t key[], size_t length) = 0;
104  };
105 
106 }
107 
108 #endif
void verify_key_set(bool cond) const
Definition: sym_algo.h:89
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
bool valid_keylength(size_t length) const
Definition: sym_algo.h:56
size_t maximum_keylength() const
Definition: sym_algo.h:38
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:65
void set_key(const std::vector< uint8_t, Alloc > &key)
Definition: sym_algo.h:71
size_t minimum_keylength() const
Definition: sym_algo.h:46
Definition: alg_id.cpp:13
size_t length() const
Definition: symkey.h:25
const uint8_t * begin() const
Definition: symkey.h:36