Botan  1.11.15
src/lib/kdf/kdf.h
Go to the documentation of this file.
00001 /*
00002 * Key Derivation Function interfaces
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_KDF_BASE_H__
00009 #define BOTAN_KDF_BASE_H__
00010 
00011 #include <botan/scan_name.h>
00012 #include <botan/secmem.h>
00013 #include <botan/types.h>
00014 #include <string>
00015 
00016 namespace Botan {
00017 
00018 /**
00019 * Key Derivation Function
00020 */
00021 class BOTAN_DLL KDF
00022    {
00023    public:
00024       virtual ~KDF() {}
00025 
00026       virtual std::string name() const = 0;
00027 
00028       virtual size_t kdf(byte key[], size_t key_len,
00029                          const byte secret[], size_t secret_len,
00030                          const byte salt[], size_t salt_len) const = 0;
00031 
00032 
00033       /**
00034       * Derive a key
00035       * @param key_len the desired output length in bytes
00036       * @param secret the secret input
00037       * @param secret_len size of secret in bytes
00038       * @param salt a diversifier
00039       * @param salt_len size of salt in bytes
00040       */
00041       secure_vector<byte> derive_key(size_t key_len,
00042                                     const byte secret[],
00043                                     size_t secret_len,
00044                                     const byte salt[],
00045                                     size_t salt_len) const
00046          {
00047          secure_vector<byte> key(key_len);
00048          key.resize(kdf(&key[0], key.size(), secret, secret_len, salt, salt_len));
00049          return key;
00050          }
00051 
00052       /**
00053       * Derive a key
00054       * @param key_len the desired output length in bytes
00055       * @param secret the secret input
00056       * @param salt a diversifier
00057       */
00058       secure_vector<byte> derive_key(size_t key_len,
00059                                     const secure_vector<byte>& secret,
00060                                     const std::string& salt = "") const
00061          {
00062          return derive_key(key_len, &secret[0], secret.size(),
00063                            reinterpret_cast<const byte*>(salt.data()),
00064                            salt.length());
00065          }
00066 
00067       /**
00068       * Derive a key
00069       * @param key_len the desired output length in bytes
00070       * @param secret the secret input
00071       * @param salt a diversifier
00072       */
00073       template<typename Alloc, typename Alloc2>
00074       secure_vector<byte> derive_key(size_t key_len,
00075                                      const std::vector<byte, Alloc>& secret,
00076                                      const std::vector<byte, Alloc2>& salt) const
00077          {
00078          return derive_key(key_len,
00079                            &secret[0], secret.size(),
00080                            &salt[0], salt.size());
00081          }
00082 
00083       /**
00084       * Derive a key
00085       * @param key_len the desired output length in bytes
00086       * @param secret the secret input
00087       * @param salt a diversifier
00088       * @param salt_len size of salt in bytes
00089       */
00090       secure_vector<byte> derive_key(size_t key_len,
00091                                     const secure_vector<byte>& secret,
00092                                     const byte salt[],
00093                                     size_t salt_len) const
00094          {
00095          return derive_key(key_len,
00096                            &secret[0], secret.size(),
00097                            salt, salt_len);
00098          }
00099 
00100       /**
00101       * Derive a key
00102       * @param key_len the desired output length in bytes
00103       * @param secret the secret input
00104       * @param secret_len size of secret in bytes
00105       * @param salt a diversifier
00106       */
00107       secure_vector<byte> derive_key(size_t key_len,
00108                                     const byte secret[],
00109                                     size_t secret_len,
00110                                     const std::string& salt = "") const
00111          {
00112          return derive_key(key_len, secret, secret_len,
00113                            reinterpret_cast<const byte*>(salt.data()),
00114                            salt.length());
00115          }
00116 
00117       virtual KDF* clone() const = 0;
00118 
00119       typedef SCAN_Name Spec;
00120 
00121    };
00122 
00123 /**
00124 * Factory method for KDF (key derivation function)
00125 * @param algo_spec the name of the KDF to create
00126 * @return pointer to newly allocated object of that type
00127 */
00128 BOTAN_DLL KDF*  get_kdf(const std::string& algo_spec);
00129 
00130 }
00131 
00132 #endif