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