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