Botan 1.10.17
Botan::OpenPGP_S2K Class Reference

#include <pgp_s2k.h>

Inheritance diagram for Botan::OpenPGP_S2K:
Botan::PBKDF Botan::Algorithm

Public Member Functions

void clear ()
PBKDFclone () const
OctetString derive_key (size_t output_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations) const
std::string name () const
 OpenPGP_S2K (HashFunction *hash_in)
 ~OpenPGP_S2K ()

Detailed Description

OpenPGP's S2K

Definition at line 19 of file pgp_s2k.h.

Constructor & Destructor Documentation

◆ OpenPGP_S2K()

Botan::OpenPGP_S2K::OpenPGP_S2K ( HashFunction * hash_in)
inline
Parameters
hash_inthe hash function to use

Definition at line 25 of file pgp_s2k.h.

25: hash(hash_in) {}

Referenced by clone(), and Botan::Core_Engine::find_pbkdf().

◆ ~OpenPGP_S2K()

Botan::OpenPGP_S2K::~OpenPGP_S2K ( )
inline

Definition at line 27 of file pgp_s2k.h.

27{ delete hash; }

Member Function Documentation

◆ clear()

void Botan::PBKDF::clear ( )
inlinevirtualinherited

Zeroize internal state

Implements Botan::Algorithm.

Definition at line 30 of file pbkdf.h.

30{}

◆ clone()

PBKDF * Botan::OpenPGP_S2K::clone ( ) const
inlinevirtual
Returns
new instance of this same algorithm

Implements Botan::PBKDF.

Definition at line 34 of file pgp_s2k.h.

35 {
36 return new OpenPGP_S2K(hash->clone());
37 }
OpenPGP_S2K(HashFunction *hash_in)
Definition pgp_s2k.h:25

References OpenPGP_S2K().

◆ derive_key()

OctetString Botan::OpenPGP_S2K::derive_key ( size_t output_len,
const std::string & passphrase,
const byte salt[],
size_t salt_len,
size_t iterations ) const
virtual

Derive a key from a passphrase

Parameters
output_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
iterationsthe number of iterations to use (use 10K or more)

Implements Botan::PBKDF.

Definition at line 15 of file pgp_s2k.cpp.

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 }

References Botan::MemoryRegion< T >::copy(), and Botan::MemoryRegion< T >::size().

◆ name()

std::string Botan::OpenPGP_S2K::name ( ) const
inlinevirtual
Returns
name of this algorithm

Implements Botan::Algorithm.

Definition at line 29 of file pgp_s2k.h.

30 {
31 return "OpenPGP-S2K(" + hash->name() + ")";
32 }

The documentation for this class was generated from the following files: