Botan 3.0.0-alpha0
Crypto and TLS for C&
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/symkey.h>
12#include <botan/types.h>
13
14namespace Botan {
15
16/**
17* Represents the length requirements on an algorithm key
18*/
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 {
31 }
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 */
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 {
46 }
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 {
54 return ((length >= m_min_keylen) &&
55 (length <= m_max_keylen) &&
56 (length % m_keylen_mod == 0));
57 }
58
59 /**
60 * @return minimum key length in bytes
61 */
62 size_t minimum_keylength() const
63 {
64 return m_min_keylen;
65 }
66
67 /**
68 * @return maximum key length in bytes
69 */
70 size_t maximum_keylength() const
71 {
72 return m_max_keylen;
73 }
74
75 /**
76 * @return key length multiple in bytes
77 */
78 size_t keylength_multiple() const
79 {
80 return m_keylen_mod;
81 }
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 */
89 {
90 return Key_Length_Specification(n * m_min_keylen,
91 n * m_max_keylen,
92 n * m_keylen_mod);
93 }
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*/
103 {
104 public:
105 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 */
116
117 /**
118 * @return maximum allowed key length
119 */
120 size_t maximum_keylength() const
121 {
122 return key_spec().maximum_keylength();
123 }
124
125 /**
126 * @return minimum allowed key length
127 */
128 size_t minimum_keylength() const
129 {
130 return key_spec().minimum_keylength();
131 }
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 {
140 return key_spec().valid_keylength(length);
141 }
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 {
149 set_key(key.begin(), key.length());
150 }
151
152 template<typename Alloc>
153 void set_key(const std::vector<uint8_t, Alloc>& key)
154 {
155 set_key(key.data(), key.size());
156 }
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 {
173 if(cond == false)
174 throw_key_not_set_error();
175 }
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
Key_Length_Specification(size_t keylen)
Definition: sym_algo.h:26
Key_Length_Specification(size_t min_k, size_t max_k, size_t k_mod=1)
Definition: sym_algo.h:39
size_t maximum_keylength() const
Definition: sym_algo.h:70
bool valid_keylength(size_t length) const
Definition: sym_algo.h:52
size_t keylength_multiple() const
Definition: sym_algo.h:78
Key_Length_Specification multiple(size_t n) const
Definition: sym_algo.h:88
size_t minimum_keylength() const
Definition: sym_algo.h:62
const uint8_t * begin() const
Definition: symkey.h:38
size_t length() const
Definition: symkey.h:27
bool valid_keylength(size_t length) const
Definition: sym_algo.h:138
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:147
void verify_key_set(bool cond) const
Definition: sym_algo.h:171
virtual std::string name() const =0
size_t maximum_keylength() const
Definition: sym_algo.h:120
size_t minimum_keylength() const
Definition: sym_algo.h:128
void set_key(const std::vector< uint8_t, Alloc > &key)
Definition: sym_algo.h:153
virtual void clear()=0
virtual ~SymmetricAlgorithm()=default
virtual Key_Length_Specification key_spec() const =0
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: alg_id.cpp:13