Botan  1.11.26
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) const = 0;
44 
45 
46  /**
47  * Derive a key
48  * @param key_len the desired output length in bytes
49  * @param secret the secret input
50  * @param secret_len size of secret in bytes
51  * @param salt a diversifier
52  * @param salt_len size of salt in bytes
53  */
55  const byte secret[],
56  size_t secret_len,
57  const byte salt[],
58  size_t salt_len) const
59  {
60  secure_vector<byte> key(key_len);
61  key.resize(kdf(key.data(), key.size(), secret, secret_len, salt, salt_len));
62  return key;
63  }
64 
65  /**
66  * Derive a key
67  * @param key_len the desired output length in bytes
68  * @param secret the secret input
69  * @param salt a diversifier
70  */
72  const secure_vector<byte>& secret,
73  const std::string& salt = "") const
74  {
75  return derive_key(key_len, secret.data(), secret.size(),
76  reinterpret_cast<const byte*>(salt.data()),
77  salt.length());
78  }
79 
80  /**
81  * Derive a key
82  * @param key_len the desired output length in bytes
83  * @param secret the secret input
84  * @param salt a diversifier
85  */
86  template<typename Alloc, typename Alloc2>
88  const std::vector<byte, Alloc>& secret,
89  const std::vector<byte, Alloc2>& salt) const
90  {
91  return derive_key(key_len,
92  secret.data(), secret.size(),
93  salt.data(), salt.size());
94  }
95 
96  /**
97  * Derive a key
98  * @param key_len the desired output length in bytes
99  * @param secret the secret input
100  * @param salt a diversifier
101  * @param salt_len size of salt in bytes
102  */
104  const secure_vector<byte>& secret,
105  const byte salt[],
106  size_t salt_len) const
107  {
108  return derive_key(key_len,
109  secret.data(), secret.size(),
110  salt, salt_len);
111  }
112 
113  /**
114  * Derive a key
115  * @param key_len the desired output length in bytes
116  * @param secret the secret input
117  * @param secret_len size of secret in bytes
118  * @param salt a diversifier
119  */
121  const byte secret[],
122  size_t secret_len,
123  const std::string& salt = "") const
124  {
125  return derive_key(key_len, secret, secret_len,
126  reinterpret_cast<const byte*>(salt.data()),
127  salt.length());
128  }
129 
130  virtual KDF* clone() const = 0;
131 
132  typedef SCAN_Name Spec;
133 
134  };
135 
136 /**
137 * Factory method for KDF (key derivation function)
138 * @param algo_spec the name of the KDF to create
139 * @return pointer to newly allocated object of that type
140 */
141 BOTAN_DLL KDF* get_kdf(const std::string& algo_spec);
142 
143 }
144 
145 #endif
secure_vector< byte > derive_key(size_t key_len, const secure_vector< byte > &secret, const byte salt[], size_t salt_len) const
Definition: kdf.h:103
secure_vector< byte > derive_key(size_t key_len, const byte secret[], size_t secret_len, const byte salt[], size_t salt_len) const
Definition: kdf.h:54
SCAN_Name Spec
Definition: kdf.h:132
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:96
Definition: alg_id.cpp:13
Definition: kdf.h:21
secure_vector< byte > derive_key(size_t key_len, const std::vector< byte, Alloc > &secret, const std::vector< byte, Alloc2 > &salt) const
Definition: kdf.h:87
secure_vector< byte > derive_key(size_t key_len, const secure_vector< byte > &secret, const std::string &salt="") const
Definition: kdf.h:71
KDF * get_kdf(const std::string &algo_spec)
Definition: kdf.cpp:59
secure_vector< byte > derive_key(size_t key_len, const byte secret[], size_t secret_len, const std::string &salt="") const
Definition: kdf.h:120
std::uint8_t byte
Definition: types.h:31