/src/botan/build/include/botan/sym_algo.h
Line | Count | Source (jump to first uncovered line) |
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/symkey.h> |
12 | | #include <botan/types.h> |
13 | | |
14 | | namespace Botan { |
15 | | |
16 | | /** |
17 | | * Represents the length requirements on an algorithm key |
18 | | */ |
19 | | class BOTAN_PUBLIC_API(2,0) Key_Length_Specification final |
20 | | { |
21 | | public: |
22 | | /** |
23 | | * Constructor for fixed length keys |
24 | | * @param keylen the supported key length |
25 | | */ |
26 | | explicit Key_Length_Specification(size_t keylen) : |
27 | | m_min_keylen(keylen), |
28 | | m_max_keylen(keylen), |
29 | | m_keylen_mod(1) |
30 | 2.60k | { |
31 | 2.60k | } |
32 | | |
33 | | /** |
34 | | * Constructor for variable length keys |
35 | | * @param min_k the smallest supported key length |
36 | | * @param max_k the largest supported key length |
37 | | * @param k_mod the number of bytes the key must be a multiple of |
38 | | */ |
39 | | Key_Length_Specification(size_t min_k, |
40 | | size_t max_k, |
41 | | size_t k_mod = 1) : |
42 | | m_min_keylen(min_k), |
43 | | m_max_keylen(max_k ? max_k : min_k), |
44 | | m_keylen_mod(k_mod) |
45 | 48.9k | { |
46 | 48.9k | } |
47 | | |
48 | | /** |
49 | | * @param length is a key length in bytes |
50 | | * @return true iff this length is a valid length for this algo |
51 | | */ |
52 | | bool valid_keylength(size_t length) const |
53 | 51.5k | { |
54 | 51.5k | return ((length >= m_min_keylen) && |
55 | 51.5k | (length <= m_max_keylen) && |
56 | 51.5k | (length % m_keylen_mod == 0)); |
57 | 51.5k | } |
58 | | |
59 | | /** |
60 | | * @return minimum key length in bytes |
61 | | */ |
62 | | size_t minimum_keylength() const |
63 | 0 | { |
64 | 0 | return m_min_keylen; |
65 | 0 | } |
66 | | |
67 | | /** |
68 | | * @return maximum key length in bytes |
69 | | */ |
70 | | size_t maximum_keylength() const |
71 | 0 | { |
72 | 0 | return m_max_keylen; |
73 | 0 | } |
74 | | |
75 | | /** |
76 | | * @return key length multiple in bytes |
77 | | */ |
78 | | size_t keylength_multiple() const |
79 | 0 | { |
80 | 0 | return m_keylen_mod; |
81 | 0 | } |
82 | | |
83 | | /* |
84 | | * Multiplies all length requirements with the given factor |
85 | | * @param n the multiplication factor |
86 | | * @return a key length specification multiplied by the factor |
87 | | */ |
88 | | Key_Length_Specification multiple(size_t n) const |
89 | 0 | { |
90 | 0 | return Key_Length_Specification(n * m_min_keylen, |
91 | 0 | n * m_max_keylen, |
92 | 0 | n * m_keylen_mod); |
93 | 0 | } |
94 | | |
95 | | private: |
96 | | size_t m_min_keylen, m_max_keylen, m_keylen_mod; |
97 | | }; |
98 | | |
99 | | /** |
100 | | * This class represents a symmetric algorithm object. |
101 | | */ |
102 | | class BOTAN_PUBLIC_API(2,0) SymmetricAlgorithm |
103 | | { |
104 | | public: |
105 | 36.3k | virtual ~SymmetricAlgorithm() = default; |
106 | | |
107 | | /** |
108 | | * Reset the state. |
109 | | */ |
110 | | virtual void clear() = 0; |
111 | | |
112 | | /** |
113 | | * @return object describing limits on key size |
114 | | */ |
115 | | virtual Key_Length_Specification key_spec() const = 0; |
116 | | |
117 | | /** |
118 | | * @return maximum allowed key length |
119 | | */ |
120 | | size_t maximum_keylength() const |
121 | 0 | { |
122 | 0 | return key_spec().maximum_keylength(); |
123 | 0 | } |
124 | | |
125 | | /** |
126 | | * @return minimum allowed key length |
127 | | */ |
128 | | size_t minimum_keylength() const |
129 | 0 | { |
130 | 0 | return key_spec().minimum_keylength(); |
131 | 0 | } |
132 | | |
133 | | /** |
134 | | * Check whether a given key length is valid for this algorithm. |
135 | | * @param length the key length to be checked. |
136 | | * @return true if the key length is valid. |
137 | | */ |
138 | | bool valid_keylength(size_t length) const |
139 | 51.5k | { |
140 | 51.5k | return key_spec().valid_keylength(length); |
141 | 51.5k | } |
142 | | |
143 | | /** |
144 | | * Set the symmetric key of this object. |
145 | | * @param key the SymmetricKey to be set. |
146 | | */ |
147 | | void set_key(const SymmetricKey& key) |
148 | 6.99k | { |
149 | 6.99k | set_key(key.begin(), key.length()); |
150 | 6.99k | } |
151 | | |
152 | | template<typename Alloc> |
153 | | void set_key(const std::vector<uint8_t, Alloc>& key) |
154 | 4.26k | { |
155 | 4.26k | set_key(key.data(), key.size()); |
156 | 4.26k | } void Botan::SymmetricAlgorithm::set_key<std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&) Line | Count | Source | 154 | 7 | { | 155 | 7 | set_key(key.data(), key.size()); | 156 | 7 | } |
void Botan::SymmetricAlgorithm::set_key<Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&) Line | Count | Source | 154 | 4.25k | { | 155 | 4.25k | set_key(key.data(), key.size()); | 156 | 4.25k | } |
|
157 | | |
158 | | /** |
159 | | * Set the symmetric key of this object. |
160 | | * @param key the to be set as a byte array. |
161 | | * @param length in bytes of key param |
162 | | */ |
163 | | void set_key(const uint8_t key[], size_t length); |
164 | | |
165 | | /** |
166 | | * @return the algorithm name |
167 | | */ |
168 | | virtual std::string name() const = 0; |
169 | | |
170 | | protected: |
171 | | void verify_key_set(bool cond) const |
172 | 1.04M | { |
173 | 1.04M | if(cond == false) |
174 | 0 | throw_key_not_set_error(); |
175 | 1.04M | } |
176 | | |
177 | | private: |
178 | | void throw_key_not_set_error() const; |
179 | | |
180 | | /** |
181 | | * Run the key schedule |
182 | | * @param key the key |
183 | | * @param length of key |
184 | | */ |
185 | | virtual void key_schedule(const uint8_t key[], size_t length) = 0; |
186 | | }; |
187 | | |
188 | | } |
189 | | |
190 | | #endif |