Botan 3.6.1
Crypto and TLS for C&
pgp_s2k.cpp
Go to the documentation of this file.
1/*
2* OpenPGP S2K
3* (C) 1999-2007,2017 Jack Lloyd
4* (C) 2018 Ribose Inc
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/pgp_s2k.h>
10
11#include <botan/exceptn.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/timer.h>
14#include <algorithm>
15
16namespace Botan {
17
18namespace {
19
20void pgp_s2k(HashFunction& hash,
21 uint8_t output_buf[],
22 size_t output_len,
23 const char* password,
24 const size_t password_size,
25 const uint8_t salt[],
26 size_t salt_len,
27 size_t iterations) {
28 if(iterations > 1 && salt_len == 0) {
29 throw Invalid_Argument("OpenPGP S2K requires a salt in iterated mode");
30 }
31
32 secure_vector<uint8_t> input_buf(salt_len + password_size);
33 if(salt_len > 0) {
34 copy_mem(&input_buf[0], salt, salt_len);
35 }
36 if(password_size > 0) {
37 copy_mem(&input_buf[salt_len], cast_char_ptr_to_uint8(password), password_size);
38 }
39
40 secure_vector<uint8_t> hash_buf(hash.output_length());
41
42 size_t pass = 0;
43 size_t generated = 0;
44
45 while(generated != output_len) {
46 const size_t output_this_pass = std::min(hash_buf.size(), output_len - generated);
47
48 // Preload some number of zero bytes (empty first iteration)
49 std::vector<uint8_t> zero_padding(pass);
50 hash.update(zero_padding);
51
52 // The input is always fully processed even if iterations is very small
53 if(input_buf.empty() == false) {
54 size_t left = std::max(iterations, input_buf.size());
55 while(left > 0) {
56 const size_t input_to_take = std::min(left, input_buf.size());
57 hash.update(input_buf.data(), input_to_take);
58 left -= input_to_take;
59 }
60 }
61
62 hash.final(hash_buf.data());
63 copy_mem(output_buf + generated, hash_buf.data(), output_this_pass);
64 generated += output_this_pass;
65 ++pass;
66 }
67}
68
69} // namespace
70
71size_t OpenPGP_S2K::pbkdf(uint8_t output_buf[],
72 size_t output_len,
73 std::string_view password,
74 const uint8_t salt[],
75 size_t salt_len,
76 size_t iterations,
77 std::chrono::milliseconds msec) const {
78 std::unique_ptr<PasswordHash> pwdhash;
79
80 if(iterations == 0) {
81 RFC4880_S2K_Family s2k_params(m_hash->new_object());
82 iterations = s2k_params.tune(output_len, msec, 0, std::chrono::milliseconds(10))->iterations();
83 }
84
85 pgp_s2k(*m_hash, output_buf, output_len, password.data(), password.size(), salt, salt_len, iterations);
86
87 return iterations;
88}
89
90std::string RFC4880_S2K_Family::name() const {
91 return fmt("OpenPGP-S2K({})", m_hash->name());
92}
93
94std::unique_ptr<PasswordHash> RFC4880_S2K_Family::tune(size_t output_len,
95 std::chrono::milliseconds msec,
96 size_t /*max_memory_usage_mb*/,
97 std::chrono::milliseconds tune_time) const {
98 const size_t buf_size = 1024;
99 std::vector<uint8_t> buffer(buf_size);
100
101 Timer timer("RFC4880_S2K", buf_size);
102 timer.run_until_elapsed(tune_time, [&]() { m_hash->update(buffer); });
103
104 const double hash_bytes_per_second = timer.bytes_per_second();
105 const uint64_t desired_nsec = msec.count() * 1000000;
106
107 const size_t hash_size = m_hash->output_length();
108 const size_t blocks_required = (output_len <= hash_size ? 1 : (output_len + hash_size - 1) / hash_size);
109
110 const double bytes_to_be_hashed = (hash_bytes_per_second * (desired_nsec / 1000000000.0)) / blocks_required;
111 const size_t iterations = RFC4880_round_iterations(static_cast<size_t>(bytes_to_be_hashed));
112
113 return std::make_unique<RFC4880_S2K>(m_hash->new_object(), iterations);
114}
115
116std::unique_ptr<PasswordHash> RFC4880_S2K_Family::from_params(size_t iter, size_t /*i2*/, size_t /*i3*/) const {
117 return std::make_unique<RFC4880_S2K>(m_hash->new_object(), iter);
118}
119
120std::unique_ptr<PasswordHash> RFC4880_S2K_Family::default_params() const {
121 return std::make_unique<RFC4880_S2K>(m_hash->new_object(), 50331648);
122}
123
124std::unique_ptr<PasswordHash> RFC4880_S2K_Family::from_iterations(size_t iter) const {
125 return std::make_unique<RFC4880_S2K>(m_hash->new_object(), iter);
126}
127
128RFC4880_S2K::RFC4880_S2K(std::unique_ptr<HashFunction> hash, size_t iterations) :
129 m_hash(std::move(hash)), m_iterations(iterations) {}
130
131std::string RFC4880_S2K::to_string() const {
132 return fmt("OpenPGP-S2K({},{})", m_hash->name(), m_iterations);
133}
134
135void RFC4880_S2K::derive_key(uint8_t out[],
136 size_t out_len,
137 const char* password,
138 const size_t password_len,
139 const uint8_t salt[],
140 size_t salt_len) const {
141 pgp_s2k(*m_hash, out, out_len, password, password_len, salt, salt_len, m_iterations);
142}
143
144} // namespace Botan
size_t pbkdf(uint8_t output_buf[], size_t output_len, std::string_view passphrase, const uint8_t salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const override
Definition pgp_s2k.cpp:71
std::unique_ptr< PasswordHash > default_params() const override
Definition pgp_s2k.cpp:120
std::unique_ptr< PasswordHash > tune(size_t output_len, std::chrono::milliseconds msec, size_t max_mem, std::chrono::milliseconds tune_msec) const override
Definition pgp_s2k.cpp:94
std::string name() const override
Definition pgp_s2k.cpp:90
std::unique_ptr< PasswordHash > from_params(size_t iter, size_t, size_t) const override
Definition pgp_s2k.cpp:116
std::unique_ptr< PasswordHash > from_iterations(size_t iter) const override
Definition pgp_s2k.cpp:124
std::string to_string() const override
Definition pgp_s2k.cpp:131
void derive_key(uint8_t out[], size_t out_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len) const override
Definition pgp_s2k.cpp:135
RFC4880_S2K(std::unique_ptr< HashFunction > hash, size_t iterations)
Definition pgp_s2k.cpp:128
void run_until_elapsed(std::chrono::milliseconds msec, F f)
Definition timer.h:60
double bytes_per_second() const
Definition timer.h:89
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146
size_t RFC4880_round_iterations(size_t iterations)
Definition rfc4880.h:32
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:273