~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/kdf.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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/bit_ops.h>
9
 
#include <algorithm>
10
 
#include <memory>
11
 
 
12
 
namespace Botan {
13
 
 
14
 
/*************************************************
15
 
* Derive a key                                   *
16
 
*************************************************/
17
 
SecureVector<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
 
                     (const byte*)salt.c_str(), salt.length());
23
 
   }
24
 
 
25
 
/*************************************************
26
 
* Derive a key                                   *
27
 
*************************************************/
28
 
SecureVector<byte> KDF::derive_key(u32bit key_len,
29
 
                                   const MemoryRegion<byte>& secret,
30
 
                                   const byte salt[], u32bit salt_len) const
31
 
   {
32
 
   return derive_key(key_len, secret.begin(), secret.size(),
33
 
                     salt, salt_len);
34
 
   }
35
 
 
36
 
/*************************************************
37
 
* Derive a key                                   *
38
 
*************************************************/
39
 
SecureVector<byte> KDF::derive_key(u32bit key_len,
40
 
                                   const MemoryRegion<byte>& secret,
41
 
                                   const MemoryRegion<byte>& salt) const
42
 
   {
43
 
   return derive_key(key_len, secret.begin(), secret.size(),
44
 
                     salt.begin(), salt.size());
45
 
   }
46
 
 
47
 
/*************************************************
48
 
* Derive a key                                   *
49
 
*************************************************/
50
 
SecureVector<byte> KDF::derive_key(u32bit key_len,
51
 
                                   const byte secret[], u32bit secret_len,
52
 
                                   const std::string& salt) const
53
 
   {
54
 
   return derive_key(key_len, secret, secret_len,
55
 
                     (const byte*)salt.c_str(), salt.length());
56
 
   }
57
 
 
58
 
/*************************************************
59
 
* Derive a key                                   *
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
64
 
   {
65
 
   return derive(key_len, secret, secret_len, salt, salt_len);
66
 
   }
67
 
 
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
74
 
   {
75
 
   std::auto_ptr<HashFunction> hash(get_hash(hash_name));
76
 
 
77
 
   hash->update(secret, secret_len);
78
 
   hash->update(P, P_len);
79
 
   return hash->final();
80
 
   }
81
 
 
82
 
/*************************************************
83
 
* KDF1 Constructor                               *
84
 
*************************************************/
85
 
KDF1::KDF1(const std::string& h_name) : hash_name(h_name)
86
 
   {
87
 
   if(!have_hash(hash_name))
88
 
      throw Algorithm_Not_Found(hash_name);
89
 
   }
90
 
 
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
97
 
   {
98
 
   SecureVector<byte> output;
99
 
   u32bit counter = 1;
100
 
 
101
 
   std::auto_ptr<HashFunction> hash(get_hash(hash_name));
102
 
   while(out_len)
103
 
      {
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();
109
 
 
110
 
      u32bit added = std::min(hash_result.size(), out_len);
111
 
      output.append(hash_result, added);
112
 
      out_len -= added;
113
 
 
114
 
      ++counter;
115
 
      }
116
 
 
117
 
   return output;
118
 
   }
119
 
 
120
 
/*************************************************
121
 
* KDF2 Constructor                               *
122
 
*************************************************/
123
 
KDF2::KDF2(const std::string& h_name) : hash_name(h_name)
124
 
   {
125
 
   if(!have_hash(hash_name))
126
 
      throw Algorithm_Not_Found(hash_name);
127
 
   }
128
 
 
129
 
}