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