Botan  2.4.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/exceptn.h>
13 #include <botan/symkey.h>
14 #include <botan/types.h>
15 
16 namespace Botan {
17 
18 /**
19 * This class represents a symmetric algorithm object.
20 */
22  {
23  public:
24  virtual ~SymmetricAlgorithm() = default;
25 
26  /**
27  * Reset the state.
28  */
29  virtual void clear() = 0;
30 
31  /**
32  * @return object describing limits on key size
33  */
34  virtual Key_Length_Specification key_spec() const = 0;
35 
36  /**
37  * @return minimum allowed key length
38  */
39  size_t maximum_keylength() const
40  {
41  return key_spec().maximum_keylength();
42  }
43 
44  /**
45  * @return maximum allowed key length
46  */
47  size_t minimum_keylength() const
48  {
49  return key_spec().minimum_keylength();
50  }
51 
52  /**
53  * Check whether a given key length is valid for this algorithm.
54  * @param length the key length to be checked.
55  * @return true if the key length is valid.
56  */
57  bool valid_keylength(size_t length) const
58  {
59  return key_spec().valid_keylength(length);
60  }
61 
62  /**
63  * Set the symmetric key of this object.
64  * @param key the SymmetricKey to be set.
65  */
66  void set_key(const SymmetricKey& key)
67  {
68  set_key(key.begin(), key.length());
69  }
70 
71  template<typename Alloc>
72  void set_key(const std::vector<uint8_t, Alloc>& key)
73  {
74  set_key(key.data(), key.size());
75  }
76 
77  /**
78  * Set the symmetric key of this object.
79  * @param key the to be set as a byte array.
80  * @param length in bytes of key param
81  */
82  void set_key(const uint8_t key[], size_t length)
83  {
84  if(!valid_keylength(length))
85  throw Invalid_Key_Length(name(), length);
86  key_schedule(key, length);
87  }
88 
89  /**
90  * @return the algorithm name
91  */
92  virtual std::string name() const = 0;
93 
94  protected:
95  void verify_key_set(bool cond) const
96  {
97  if(cond == false)
98  throw Key_Not_Set(name());
99  }
100 
101  private:
102  /**
103  * Run the key schedule
104  * @param key the key
105  * @param length of key
106  */
107  virtual void key_schedule(const uint8_t key[], size_t length) = 0;
108  };
109 
110 }
111 
112 #endif
void verify_key_set(bool cond) const
Definition: sym_algo.h:95
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
void set_key(const uint8_t key[], size_t length)
Definition: sym_algo.h:82
bool valid_keylength(size_t length) const
Definition: sym_algo.h:57
size_t maximum_keylength() const
Definition: sym_algo.h:39
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:66
void set_key(const std::vector< uint8_t, Alloc > &key)
Definition: sym_algo.h:72
size_t minimum_keylength() const
Definition: sym_algo.h:47
Definition: alg_id.cpp:13
size_t length() const
Definition: symkey.h:25
const uint8_t * begin() const
Definition: symkey.h:36