monotone

monotone Mtn Source Tree

Root/botan/kdf.cpp

1/*************************************************
2* KDF1/KDF2 Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/kdf.h>
7#include <botan/lookup.h>
8#include <botan/loadstor.h>
9#include <algorithm>
10#include <memory>
11
12namespace Botan {
13
14/*************************************************
15* Derive a key *
16*************************************************/
17SecureVector<byte> KDF::derive_key(u32bit key_len,
18 const MemoryRegion<byte>& secret,
19 const std::string& salt) const
20 {
21 return derive_key(key_len, secret, secret.size(),
22 reinterpret_cast<const byte*>(salt.data()),
23 salt.length());
24 }
25
26/*************************************************
27* Derive a key *
28*************************************************/
29SecureVector<byte> KDF::derive_key(u32bit key_len,
30 const MemoryRegion<byte>& secret,
31 const byte salt[], u32bit salt_len) const
32 {
33 return derive_key(key_len, secret.begin(), secret.size(),
34 salt, salt_len);
35 }
36
37/*************************************************
38* Derive a key *
39*************************************************/
40SecureVector<byte> KDF::derive_key(u32bit key_len,
41 const MemoryRegion<byte>& secret,
42 const MemoryRegion<byte>& salt) const
43 {
44 return derive_key(key_len, secret.begin(), secret.size(),
45 salt.begin(), salt.size());
46 }
47
48/*************************************************
49* Derive a key *
50*************************************************/
51SecureVector<byte> KDF::derive_key(u32bit key_len,
52 const byte secret[], u32bit secret_len,
53 const std::string& salt) const
54 {
55 return derive_key(key_len, secret, secret_len,
56 reinterpret_cast<const byte*>(salt.data()),
57 salt.length());
58 }
59
60/*************************************************
61* Derive a key *
62*************************************************/
63SecureVector<byte> KDF::derive_key(u32bit key_len,
64 const byte secret[], u32bit secret_len,
65 const byte salt[], u32bit salt_len) const
66 {
67 return derive(key_len, secret, secret_len, salt, salt_len);
68 }
69
70/*************************************************
71* KDF1 Key Derivation Mechanism *
72*************************************************/
73SecureVector<byte> KDF1::derive(u32bit,
74 const byte secret[], u32bit secret_len,
75 const byte P[], u32bit P_len) const
76 {
77 std::auto_ptr<HashFunction> hash(get_hash(hash_name));
78
79 hash->update(secret, secret_len);
80 hash->update(P, P_len);
81 return hash->final();
82 }
83
84/*************************************************
85* KDF1 Constructor *
86*************************************************/
87KDF1::KDF1(const std::string& h_name) : hash_name(h_name)
88 {
89 if(!have_hash(hash_name))
90 throw Algorithm_Not_Found(hash_name);
91 }
92
93/*************************************************
94* KDF2 Key Derivation Mechanism *
95*************************************************/
96SecureVector<byte> KDF2::derive(u32bit out_len,
97 const byte secret[], u32bit secret_len,
98 const byte P[], u32bit P_len) const
99 {
100 SecureVector<byte> output;
101 u32bit counter = 1;
102
103 std::auto_ptr<HashFunction> hash(get_hash(hash_name));
104 while(out_len && counter)
105 {
106 hash->update(secret, secret_len);
107 for(u32bit j = 0; j != 4; ++j)
108 hash->update(get_byte(j, counter));
109 hash->update(P, P_len);
110 SecureVector<byte> hash_result = hash->final();
111
112 u32bit added = std::min(hash_result.size(), out_len);
113 output.append(hash_result, added);
114 out_len -= added;
115
116 ++counter;
117 }
118
119 return output;
120 }
121
122/*************************************************
123* KDF2 Constructor *
124*************************************************/
125KDF2::KDF2(const std::string& h_name) : hash_name(h_name)
126 {
127 if(!have_hash(hash_name))
128 throw Algorithm_Not_Found(hash_name);
129 }
130
131}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status