Botan  1.10.9
pgp_s2k.cpp
Go to the documentation of this file.
1 /*
2 * OpenPGP S2K
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/pgp_s2k.h>
9 
10 namespace Botan {
11 
12 /*
13 * Derive a key using the OpenPGP S2K algorithm
14 */
16  const std::string& passphrase,
17  const byte salt_buf[], size_t salt_size,
18  size_t iterations) const
19  {
20  SecureVector<byte> key(key_len), hash_buf;
21 
22  size_t pass = 0, generated = 0,
23  total_size = passphrase.size() + salt_size;
24  size_t to_hash = std::max(iterations, total_size);
25 
26  hash->clear();
27  while(key_len > generated)
28  {
29  for(size_t j = 0; j != pass; ++j)
30  hash->update(0);
31 
32  size_t left = to_hash;
33  while(left >= total_size)
34  {
35  hash->update(salt_buf, salt_size);
36  hash->update(passphrase);
37  left -= total_size;
38  }
39  if(left <= salt_size)
40  hash->update(salt_buf, left);
41  else
42  {
43  hash->update(salt_buf, salt_size);
44  left -= salt_size;
45  hash->update(reinterpret_cast<const byte*>(passphrase.data()), left);
46  }
47 
48  hash_buf = hash->final();
49  key.copy(generated, &hash_buf[0], hash->output_length());
50  generated += hash->output_length();
51  ++pass;
52  }
53 
54  return key;
55  }
56 
57 }
virtual void clear()=0
unsigned char byte
Definition: types.h:22
void update(const byte in[], size_t length)
Definition: buf_comp.h:33
size_t size() const
Definition: secmem.h:29
void final(byte out[])
Definition: buf_comp.h:80
OctetString derive_key(size_t output_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations) const
Definition: pgp_s2k.cpp:15
virtual size_t output_length() const =0