Botan  1.11.31
kdf.h
Go to the documentation of this file.
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/scan_name.h>
12 #include <botan/secmem.h>
13 #include <botan/types.h>
14 #include <string>
15 
16 namespace Botan {
17 
18 /**
19 * Key Derivation Function
20 */
21 class BOTAN_DLL KDF
22  {
23  public:
24  virtual ~KDF();
25 
26  /**
27  * Create an instance based on a name
28  * Will return a null pointer if the algo/provider combination cannot
29  * be found. If provider is empty then best available is chosen.
30  */
31  static std::unique_ptr<KDF> create(const std::string& algo_spec,
32  const std::string& provider = "");
33 
34  /**
35  * Returns the list of available providers for this algorithm, empty if not available
36  */
37  static std::vector<std::string> providers(const std::string& algo_spec);
38 
39  virtual std::string name() const = 0;
40 
41  virtual size_t kdf(byte key[], size_t key_len,
42  const byte secret[], size_t secret_len,
43  const byte salt[], size_t salt_len,
44  const byte label[], size_t label_len) const = 0;
45 
46 
47  /**
48  * Derive a key
49  * @param key_len the desired output length in bytes
50  * @param secret the secret input
51  * @param secret_len size of secret in bytes
52  * @param salt a diversifier
53  * @param salt_len size of salt in bytes
54  * @param label purpose for the derived keying material
55  * @param label_len size of label in bytes
56  */
58  const byte secret[],
59  size_t secret_len,
60  const byte salt[],
61  size_t salt_len,
62  const byte label[] = nullptr,
63  size_t label_len = 0) const
64  {
65  secure_vector<byte> key(key_len);
66  key.resize(kdf(key.data(), key.size(), secret, secret_len, salt, salt_len, label, label_len));
67  return key;
68  }
69 
70  /**
71  * Derive a key
72  * @param key_len the desired output length in bytes
73  * @param secret the secret input
74  * @param salt a diversifier
75  * @param label purpose for the derived keying material
76  */
78  const secure_vector<byte>& secret,
79  const std::string& salt = "",
80  const std::string& label = "") const
81  {
82  return derive_key(key_len, secret.data(), secret.size(),
83  reinterpret_cast<const byte*>(salt.data()),
84  salt.length(),
85  reinterpret_cast<const byte*>(label.data()),
86  label.length());
87 
88  }
89 
90  /**
91  * Derive a key
92  * @param key_len the desired output length in bytes
93  * @param secret the secret input
94  * @param salt a diversifier
95  * @param label purpose for the derived keying material
96  */
97  template<typename Alloc, typename Alloc2, typename Alloc3>
99  const std::vector<byte, Alloc>& secret,
100  const std::vector<byte, Alloc2>& salt,
101  const std::vector<byte, Alloc3>& label) const
102  {
103  return derive_key(key_len,
104  secret.data(), secret.size(),
105  salt.data(), salt.size(),
106  label.data(), label.size());
107  }
108 
109  /**
110  * Derive a key
111  * @param key_len the desired output length in bytes
112  * @param secret the secret input
113  * @param salt a diversifier
114  * @param salt_len size of salt in bytes
115  * @param label purpose for the derived keying material
116  */
118  const secure_vector<byte>& secret,
119  const byte salt[],
120  size_t salt_len,
121  const std::string& label = "") const
122  {
123  return derive_key(key_len,
124  secret.data(), secret.size(),
125  salt, salt_len,
126  reinterpret_cast<const byte*>(label.data()),
127  label.size());
128  }
129 
130  /**
131  * Derive a key
132  * @param key_len the desired output length in bytes
133  * @param secret the secret input
134  * @param secret_len size of secret in bytes
135  * @param salt a diversifier
136  * @param label purpose for the derived keying material
137  */
139  const byte secret[],
140  size_t secret_len,
141  const std::string& salt = "",
142  const std::string& label = "") const
143  {
144  return derive_key(key_len, secret, secret_len,
145  reinterpret_cast<const byte*>(salt.data()),
146  salt.length(),
147  reinterpret_cast<const byte*>(label.data()),
148  label.length());
149  }
150 
151  virtual KDF* clone() const = 0;
152 
153  typedef SCAN_Name Spec;
154 
155  };
156 
157 /**
158 * Factory method for KDF (key derivation function)
159 * @param algo_spec the name of the KDF to create
160 * @return pointer to newly allocated object of that type
161 */
162 BOTAN_DLL KDF* get_kdf(const std::string& algo_spec);
163 
164 }
165 
166 #endif
secure_vector< byte > derive_key(size_t key_len, const std::vector< byte, Alloc > &secret, const std::vector< byte, Alloc2 > &salt, const std::vector< byte, Alloc3 > &label) const
Definition: kdf.h:98
secure_vector< byte > derive_key(size_t key_len, const secure_vector< byte > &secret, const byte salt[], size_t salt_len, const std::string &label="") const
Definition: kdf.h:117
SCAN_Name Spec
Definition: kdf.h:153
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:101
Definition: alg_id.cpp:13
secure_vector< byte > derive_key(size_t key_len, const byte secret[], size_t secret_len, const std::string &salt="", const std::string &label="") const
Definition: kdf.h:138
Definition: kdf.h:21
KDF * get_kdf(const std::string &algo_spec)
Definition: kdf.cpp:71
secure_vector< byte > derive_key(size_t key_len, const byte secret[], size_t secret_len, const byte salt[], size_t salt_len, const byte label[]=nullptr, size_t label_len=0) const
Definition: kdf.h:57
secure_vector< byte > derive_key(size_t key_len, const secure_vector< byte > &secret, const std::string &salt="", const std::string &label="") const
Definition: kdf.h:77
std::uint8_t byte
Definition: types.h:31