1
/*************************************************
2
* KDF1/KDF2 Source File *
3
* (C) 1999-2007 The Botan Project *
4
*************************************************/
7
#include <botan/lookup.h>
8
#include <botan/bit_ops.h>
14
/*************************************************
16
*************************************************/
17
SecureVector<byte> KDF::derive_key(u32bit key_len,
18
const MemoryRegion<byte>& secret,
19
const std::string& salt) const
21
return derive_key(key_len, secret, secret.size(),
22
(const byte*)salt.c_str(), salt.length());
25
/*************************************************
27
*************************************************/
28
SecureVector<byte> KDF::derive_key(u32bit key_len,
29
const MemoryRegion<byte>& secret,
30
const byte salt[], u32bit salt_len) const
32
return derive_key(key_len, secret.begin(), secret.size(),
36
/*************************************************
38
*************************************************/
39
SecureVector<byte> KDF::derive_key(u32bit key_len,
40
const MemoryRegion<byte>& secret,
41
const MemoryRegion<byte>& salt) const
43
return derive_key(key_len, secret.begin(), secret.size(),
44
salt.begin(), salt.size());
47
/*************************************************
49
*************************************************/
50
SecureVector<byte> KDF::derive_key(u32bit key_len,
51
const byte secret[], u32bit secret_len,
52
const std::string& salt) const
54
return derive_key(key_len, secret, secret_len,
55
(const byte*)salt.c_str(), salt.length());
58
/*************************************************
60
*************************************************/
61
SecureVector<byte> KDF::derive_key(u32bit key_len,
62
const byte secret[], u32bit secret_len,
63
const byte salt[], u32bit salt_len) const
65
return derive(key_len, secret, secret_len, salt, salt_len);
68
/*************************************************
69
* KDF1 Key Derivation Mechanism *
70
*************************************************/
71
SecureVector<byte> KDF1::derive(u32bit,
72
const byte secret[], u32bit secret_len,
73
const byte P[], u32bit P_len) const
75
std::auto_ptr<HashFunction> hash(get_hash(hash_name));
77
hash->update(secret, secret_len);
78
hash->update(P, P_len);
82
/*************************************************
84
*************************************************/
85
KDF1::KDF1(const std::string& h_name) : hash_name(h_name)
87
if(!have_hash(hash_name))
88
throw Algorithm_Not_Found(hash_name);
91
/*************************************************
92
* KDF2 Key Derivation Mechanism *
93
*************************************************/
94
SecureVector<byte> KDF2::derive(u32bit out_len,
95
const byte secret[], u32bit secret_len,
96
const byte P[], u32bit P_len) const
98
SecureVector<byte> output;
101
std::auto_ptr<HashFunction> hash(get_hash(hash_name));
104
hash->update(secret, secret_len);
105
for(u32bit j = 0; j != 4; ++j)
106
hash->update(get_byte(j, counter));
107
hash->update(P, P_len);
108
SecureVector<byte> hash_result = hash->final();
110
u32bit added = std::min(hash_result.size(), out_len);
111
output.append(hash_result, added);
120
/*************************************************
122
*************************************************/
123
KDF2::KDF2(const std::string& h_name) : hash_name(h_name)
125
if(!have_hash(hash_name))
126
throw Algorithm_Not_Found(hash_name);