Botan  2.6.0
Crypto and TLS for C++11
key_spec.h
Go to the documentation of this file.
1 /*
2 * Symmetric Key Length Specification
3 * (C) 2010 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_KEY_LEN_SPECIFICATION_H_
9 #define BOTAN_KEY_LEN_SPECIFICATION_H_
10 
11 #include <botan/types.h>
12 
13 namespace Botan {
14 
15 /**
16 * Represents the length requirements on an algorithm key
17 */
19  {
20  public:
21  /**
22  * Constructor for fixed length keys
23  * @param keylen the supported key length
24  */
25  explicit Key_Length_Specification(size_t keylen) :
26  m_min_keylen(keylen),
27  m_max_keylen(keylen),
28  m_keylen_mod(1)
29  {
30  }
31 
32  /**
33  * Constructor for variable length keys
34  * @param min_k the smallest supported key length
35  * @param max_k the largest supported key length
36  * @param k_mod the number of bytes the key must be a multiple of
37  */
39  size_t max_k,
40  size_t k_mod = 1) :
41  m_min_keylen(min_k),
42  m_max_keylen(max_k ? max_k : min_k),
43  m_keylen_mod(k_mod)
44  {
45  }
46 
47  /**
48  * @param length is a key length in bytes
49  * @return true iff this length is a valid length for this algo
50  */
51  bool valid_keylength(size_t length) const
52  {
53  return ((length >= m_min_keylen) &&
54  (length <= m_max_keylen) &&
55  (length % m_keylen_mod == 0));
56  }
57 
58  /**
59  * @return minimum key length in bytes
60  */
61  size_t minimum_keylength() const
62  {
63  return m_min_keylen;
64  }
65 
66  /**
67  * @return maximum key length in bytes
68  */
69  size_t maximum_keylength() const
70  {
71  return m_max_keylen;
72  }
73 
74  /**
75  * @return key length multiple in bytes
76  */
77  size_t keylength_multiple() const
78  {
79  return m_keylen_mod;
80  }
81 
82  /*
83  * Multiplies all length requirements with the given factor
84  * @param n the multiplication factor
85  * @return a key length specification multiplied by the factor
86  */
88  {
89  return Key_Length_Specification(n * m_min_keylen,
90  n * m_max_keylen,
91  n * m_keylen_mod);
92  }
93 
94  private:
95  size_t m_min_keylen, m_max_keylen, m_keylen_mod;
96  };
97 
98 }
99 
100 #endif
Key_Length_Specification(size_t min_k, size_t max_k, size_t k_mod=1)
Definition: key_spec.h:38
size_t maximum_keylength() const
Definition: key_spec.h:69
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
size_t keylength_multiple() const
Definition: key_spec.h:77
bool valid_keylength(size_t length) const
Definition: key_spec.h:51
Definition: alg_id.cpp:13
Key_Length_Specification multiple(size_t n) const
Definition: key_spec.h:87
size_t minimum_keylength() const
Definition: key_spec.h:61
Key_Length_Specification(size_t keylen)
Definition: key_spec.h:25