monotone

monotone Mtn Source Tree

Root/botan/kdf.cpp

1/*************************************************
2* KDF1/KDF2 Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/kdf.h>
7#include <botan/lookup.h>
8#include <botan/bit_ops.h>
9#include <memory>
10
11namespace Botan {
12
13/*************************************************
14* KDF1 Key Derivation Mechanism *
15*************************************************/
16SecureVector<byte> KDF1::derive(u32bit,
17 const byte secret[], u32bit secret_len,
18 const byte P[], u32bit P_len) const
19 {
20 std::auto_ptr<HashFunction> hash(get_hash(hash_name));
21
22 hash->update(secret, secret_len);
23 hash->update(P, P_len);
24 return hash->final();
25 }
26
27/*************************************************
28* KDF1 Constructor *
29*************************************************/
30KDF1::KDF1(const std::string& h_name) : hash_name(h_name)
31 {
32 if(!have_hash(hash_name))
33 throw Algorithm_Not_Found(hash_name);
34 }
35
36/*************************************************
37* KDF2 Key Derivation Mechanism *
38*************************************************/
39SecureVector<byte> KDF2::derive(u32bit out_len,
40 const byte secret[], u32bit secret_len,
41 const byte P[], u32bit P_len) const
42 {
43 SecureVector<byte> output;
44 u32bit counter = 1;
45
46 std::auto_ptr<HashFunction> hash(get_hash(hash_name));
47 while(out_len)
48 {
49 hash->update(secret, secret_len);
50 for(u32bit j = 0; j != 4; j++)
51 hash->update(get_byte(j, counter));
52 hash->update(P, P_len);
53 SecureVector<byte> hash_result = hash->final();
54
55 u32bit added = std::min(hash_result.size(), out_len);
56 output.append(hash_result, added);
57 out_len -= added;
58
59 counter++;
60 }
61
62 return output;
63 }
64
65/*************************************************
66* KDF2 Constructor *
67*************************************************/
68KDF2::KDF2(const std::string& h_name) : hash_name(h_name)
69 {
70 if(!have_hash(hash_name))
71 throw Algorithm_Not_Found(hash_name);
72 }
73
74}

Archive Download this file

Branches

Tags

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