Botan  1.11.15
src/lib/kdf/kdf2/kdf2.cpp
Go to the documentation of this file.
00001 /*
00002 * KDF2
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/internal/kdf_utils.h>
00009 #include <botan/kdf2.h>
00010 
00011 namespace Botan {
00012 
00013 BOTAN_REGISTER_KDF_1HASH(KDF2, "KDF2");
00014 
00015 size_t KDF2::kdf(byte key[], size_t key_len,
00016                  const byte secret[], size_t secret_len,
00017                  const byte salt[], size_t salt_len) const
00018    {
00019    u32bit counter = 1;
00020    secure_vector<byte> h;
00021 
00022    size_t offset = 0;
00023    while(offset != key_len && counter != 0)
00024       {
00025       m_hash->update(secret, secret_len);
00026       m_hash->update_be(counter++);
00027       m_hash->update(salt, salt_len);
00028       m_hash->final(h);
00029 
00030       const size_t added = std::min(h.size(), key_len - offset);
00031       copy_mem(&key[offset], &h[0], added);
00032       offset += added;
00033       }
00034 
00035    return offset;
00036    }
00037 
00038 }