Botan: src/pbkdf/pgps2k/pgp_s2k.cpp Source File

Botan 1.10.16

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 */
15 OctetString OpenPGP_S2K::derive_key(size_t key_len,
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 }
Botan::Algorithm::clear
virtual void clear()=0
Botan::OctetString
Definition: symkey.h:19
Botan::OpenPGP_S2K::derive_key
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
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::Buffered_Computation::update
void update(const byte in[], size_t length)
Definition: buf_comp.h:33
Botan
Definition: algo_base.h:14
Botan::Buffered_Computation::final
void final(byte out[])
Definition: buf_comp.h:80
Botan::CT::max
T max(T a, T b)
Definition: ct_utils.h:120
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::SecureVector< byte >
Botan::Buffered_Computation::output_length
virtual size_t output_length() const =0