/src/botan/build/include/botan/kdf.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Key Derivation Function interfaces |
3 | | * (C) 1999-2007 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #ifndef BOTAN_KDF_BASE_H_ |
9 | | #define BOTAN_KDF_BASE_H_ |
10 | | |
11 | | #include <botan/secmem.h> |
12 | | #include <botan/types.h> |
13 | | #include <string> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | | /** |
18 | | * Key Derivation Function |
19 | | */ |
20 | | class BOTAN_PUBLIC_API(2,0) KDF |
21 | | { |
22 | | public: |
23 | 12.0k | virtual ~KDF() = default; |
24 | | |
25 | | /** |
26 | | * Create an instance based on a name |
27 | | * If provider is empty then best available is chosen. |
28 | | * @param algo_spec algorithm name |
29 | | * @param provider provider implementation to choose |
30 | | * @return a null pointer if the algo/provider combination cannot be found |
31 | | */ |
32 | | static std::unique_ptr<KDF> |
33 | | create(const std::string& algo_spec, |
34 | | const std::string& provider = ""); |
35 | | |
36 | | /** |
37 | | * Create an instance based on a name, or throw if the |
38 | | * algo/provider combination cannot be found. If provider is |
39 | | * empty then best available is chosen. |
40 | | */ |
41 | | static std::unique_ptr<KDF> |
42 | | create_or_throw(const std::string& algo_spec, |
43 | | const std::string& provider = ""); |
44 | | |
45 | | /** |
46 | | * @return list of available providers for this algorithm, empty if not available |
47 | | */ |
48 | | static std::vector<std::string> providers(const std::string& algo_spec); |
49 | | |
50 | | /** |
51 | | * @return KDF name |
52 | | */ |
53 | | virtual std::string name() const = 0; |
54 | | |
55 | | /** |
56 | | * Derive a key |
57 | | * @param key buffer holding the derived key, must be of length key_len |
58 | | * @param key_len the desired output length in bytes |
59 | | * @param secret the secret input |
60 | | * @param secret_len size of secret in bytes |
61 | | * @param salt a diversifier |
62 | | * @param salt_len size of salt in bytes |
63 | | * @param label purpose for the derived keying material |
64 | | * @param label_len size of label in bytes |
65 | | */ |
66 | | virtual void kdf(uint8_t key[], size_t key_len, |
67 | | const uint8_t secret[], size_t secret_len, |
68 | | const uint8_t salt[], size_t salt_len, |
69 | | const uint8_t label[], size_t label_len) const = 0; |
70 | | |
71 | | /** |
72 | | * Derive a key |
73 | | * @param key_len the desired output length in bytes |
74 | | * @param secret the secret input |
75 | | * @param secret_len size of secret in bytes |
76 | | * @param salt a diversifier |
77 | | * @param salt_len size of salt in bytes |
78 | | * @param label purpose for the derived keying material |
79 | | * @param label_len size of label in bytes |
80 | | * @return the derived key |
81 | | */ |
82 | | secure_vector<uint8_t> derive_key(size_t key_len, |
83 | | const uint8_t secret[], |
84 | | size_t secret_len, |
85 | | const uint8_t salt[], |
86 | | size_t salt_len, |
87 | | const uint8_t label[] = nullptr, |
88 | | size_t label_len = 0) const |
89 | 22.2k | { |
90 | 22.2k | secure_vector<uint8_t> key(key_len); |
91 | 22.2k | kdf(key.data(), key.size(), secret, secret_len, salt, salt_len, label, label_len); |
92 | 22.2k | return key; |
93 | 22.2k | } |
94 | | |
95 | | /** |
96 | | * Derive a key |
97 | | * @param key_len the desired output length in bytes |
98 | | * @param secret the secret input |
99 | | * @param salt a diversifier |
100 | | * @param label purpose for the derived keying material |
101 | | * @return the derived key |
102 | | */ |
103 | | secure_vector<uint8_t> derive_key(size_t key_len, |
104 | | const secure_vector<uint8_t>& secret, |
105 | | const std::string& salt = "", |
106 | | const std::string& label = "") const |
107 | 0 | { |
108 | 0 | return derive_key(key_len, secret.data(), secret.size(), |
109 | 0 | cast_char_ptr_to_uint8(salt.data()), |
110 | 0 | salt.length(), |
111 | 0 | cast_char_ptr_to_uint8(label.data()), |
112 | 0 | label.length()); |
113 | 0 |
|
114 | 0 | } |
115 | | |
116 | | /** |
117 | | * Derive a key |
118 | | * @param key_len the desired output length in bytes |
119 | | * @param secret the secret input |
120 | | * @param salt a diversifier |
121 | | * @param label purpose for the derived keying material |
122 | | * @return the derived key |
123 | | */ |
124 | | template<typename Alloc, typename Alloc2, typename Alloc3> |
125 | | secure_vector<uint8_t> derive_key(size_t key_len, |
126 | | const std::vector<uint8_t, Alloc>& secret, |
127 | | const std::vector<uint8_t, Alloc2>& salt, |
128 | | const std::vector<uint8_t, Alloc3>& label) const |
129 | 12.0k | { |
130 | 12.0k | return derive_key(key_len, |
131 | 12.0k | secret.data(), secret.size(), |
132 | 12.0k | salt.data(), salt.size(), |
133 | 12.0k | label.data(), label.size()); |
134 | 12.0k | } |
135 | | |
136 | | /** |
137 | | * Derive a key |
138 | | * @param key_len the desired output length in bytes |
139 | | * @param secret the secret input |
140 | | * @param salt a diversifier |
141 | | * @param salt_len size of salt in bytes |
142 | | * @param label purpose for the derived keying material |
143 | | * @return the derived key |
144 | | */ |
145 | | secure_vector<uint8_t> derive_key(size_t key_len, |
146 | | const secure_vector<uint8_t>& secret, |
147 | | const uint8_t salt[], |
148 | | size_t salt_len, |
149 | | const std::string& label = "") const |
150 | 0 | { |
151 | 0 | return derive_key(key_len, |
152 | 0 | secret.data(), secret.size(), |
153 | 0 | salt, salt_len, |
154 | 0 | cast_char_ptr_to_uint8(label.data()), |
155 | 0 | label.size()); |
156 | 0 | } |
157 | | |
158 | | /** |
159 | | * Derive a key |
160 | | * @param key_len the desired output length in bytes |
161 | | * @param secret the secret input |
162 | | * @param secret_len size of secret in bytes |
163 | | * @param salt a diversifier |
164 | | * @param label purpose for the derived keying material |
165 | | * @return the derived key |
166 | | */ |
167 | | secure_vector<uint8_t> derive_key(size_t key_len, |
168 | | const uint8_t secret[], |
169 | | size_t secret_len, |
170 | | const std::string& salt = "", |
171 | | const std::string& label = "") const |
172 | 0 | { |
173 | 0 | return derive_key(key_len, secret, secret_len, |
174 | 0 | cast_char_ptr_to_uint8(salt.data()), |
175 | 0 | salt.length(), |
176 | 0 | cast_char_ptr_to_uint8(label.data()), |
177 | 0 | label.length()); |
178 | 0 | } |
179 | | |
180 | | /** |
181 | | * @return new object representing the same algorithm as *this |
182 | | */ |
183 | | virtual KDF* clone() const = 0; |
184 | | }; |
185 | | |
186 | | /** |
187 | | * Factory method for KDF (key derivation function) |
188 | | * @param algo_spec the name of the KDF to create |
189 | | * @return pointer to newly allocated object of that type |
190 | | */ |
191 | | BOTAN_PUBLIC_API(2,0) KDF* get_kdf(const std::string& algo_spec); |
192 | | |
193 | | } |
194 | | |
195 | | #endif |