9#include <botan/internal/pbes2.h>
11#include <botan/asn1_obj.h>
12#include <botan/ber_dec.h>
13#include <botan/cipher_mode.h>
14#include <botan/der_enc.h>
15#include <botan/pwdhash.h>
17#include <botan/internal/fmt.h>
18#include <botan/internal/parsing.h>
24bool known_pbes_cipher_mode(std::string_view mode) {
25 return (mode ==
"CBC" || mode ==
"GCM" || mode ==
"SIV");
29 const AlgorithmIdentifier& kdf_algo,
30 size_t default_key_size) {
33 size_t iterations = 0, key_length = 0;
35 AlgorithmIdentifier prf_algo;
36 BER_Decoder(kdf_algo.parameters())
41 .decode_optional(prf_algo,
48 throw Decoding_Error(
"PBE-PKCS5 v2.0: Encoded salt is too small");
52 key_length = default_key_size;
55 const std::string prf = prf_algo.oid().human_name_or_empty();
56 if(prf.empty() || !prf.starts_with(
"HMAC")) {
57 throw Decoding_Error(
fmt(
"Unknown PBES2 PRF {}", prf_algo.oid()));
61 auto pbkdf = pbkdf_fam->from_params(iterations);
64 pbkdf->hash(derived_key, passphrase, salt);
68 size_t N = 0, r = 0, p = 0;
69 size_t key_length = 0;
71 AlgorithmIdentifier prf_algo;
72 BER_Decoder(kdf_algo.parameters())
82 key_length = default_key_size;
88 auto pwdhash = pwdhash_fam->from_params(N, r, p);
89 pwdhash->hash(derived_key, passphrase, salt);
93 throw Decoding_Error(
fmt(
"PBE-PKCS5 v2.0: Unknown KDF algorithm {}", kdf_algo.oid()));
98 std::string_view digest,
99 RandomNumberGenerator& rng,
100 size_t* msec_in_iterations_out,
101 size_t iterations_if_msec_null,
103 bool include_key_length_in_struct,
104 AlgorithmIdentifier& kdf_algo) {
105 const size_t salt_len = 16;
108 if(digest ==
"Scrypt") {
111 std::unique_ptr<PasswordHash> pwhash;
113 if(msec_in_iterations_out) {
114 const std::chrono::milliseconds msec(*msec_in_iterations_out);
115 pwhash = pwhash_fam->tune(key_length, msec);
117 pwhash = pwhash_fam->from_iterations(iterations_if_msec_null);
121 pwhash->hash(key, passphrase, salt);
123 const size_t N = pwhash->memory_param();
124 const size_t r = pwhash->iterations();
125 const size_t p = pwhash->parallelism();
127 if(msec_in_iterations_out) {
128 *msec_in_iterations_out = 0;
131 std::vector<uint8_t> scrypt_params;
132 DER_Encoder(scrypt_params)
138 .encode_if(include_key_length_in_struct, key_length)
144 const std::string prf =
fmt(
"HMAC({})", digest);
145 const std::string pbkdf_name =
fmt(
"PBKDF2({})", prf);
149 throw Invalid_Argument(
fmt(
"Unknown password hash digest {}", digest));
152 std::unique_ptr<PasswordHash> pwhash;
154 if(msec_in_iterations_out) {
155 const std::chrono::milliseconds msec(*msec_in_iterations_out);
156 pwhash = pwhash_fam->tune(key_length, msec);
158 pwhash = pwhash_fam->from_iterations(iterations_if_msec_null);
162 pwhash->hash(key, passphrase, salt);
164 std::vector<uint8_t> pbkdf2_params;
166 const size_t iterations = pwhash->iterations();
168 if(msec_in_iterations_out) {
169 *msec_in_iterations_out = iterations;
172 DER_Encoder(pbkdf2_params)
176 .encode_if(include_key_length_in_struct, key_length)
180 kdf_algo = AlgorithmIdentifier(
"PKCS5.PBKDF2", pbkdf2_params);
188std::pair<AlgorithmIdentifier, std::vector<uint8_t>> pbes2_encrypt_shared(std::span<const uint8_t> key_bits,
189 std::string_view passphrase,
190 size_t* msec_in_iterations_out,
191 size_t iterations_if_msec_null,
192 std::string_view cipher,
193 std::string_view prf,
194 RandomNumberGenerator& rng) {
197 const auto cipher_spec =
split_on(cipher,
'/');
199 if(cipher_spec.size() != 2 || !known_pbes_cipher_mode(cipher_spec[1]) || !enc) {
200 throw Encoding_Error(
fmt(
"PBE-PKCS5 v2.0: Invalid or unavailable cipher '{}'", cipher));
203 const size_t key_length = enc->key_spec().maximum_keylength();
207 AlgorithmIdentifier kdf_algo;
209 const bool include_key_length_in_struct = enc->key_spec().minimum_keylength() != enc->key_spec().maximum_keylength();
211 const auto derived_key = derive_key(passphrase,
214 msec_in_iterations_out,
215 iterations_if_msec_null,
217 include_key_length_in_struct,
220 enc->set_key(derived_key);
225 std::vector<uint8_t> encoded_iv;
228 std::vector<uint8_t> pbes2_params;
229 DER_Encoder(pbes2_params)
232 .encode(AlgorithmIdentifier(cipher, encoded_iv))
237 return std::make_pair(
id,
unlock(ctext));
242std::pair<AlgorithmIdentifier, std::vector<uint8_t>>
pbes2_encrypt(std::span<const uint8_t> key_bits,
243 std::string_view passphrase,
244 std::chrono::milliseconds msec,
245 std::string_view cipher,
246 std::string_view digest,
248 size_t msec_in_iterations_out =
static_cast<size_t>(msec.count());
249 return pbes2_encrypt_shared(key_bits, passphrase, &msec_in_iterations_out, 0, cipher, digest, rng);
253std::pair<AlgorithmIdentifier, std::vector<uint8_t>>
pbes2_encrypt_msec(std::span<const uint8_t> key_bits,
254 std::string_view passphrase,
255 std::chrono::milliseconds msec,
256 size_t* out_iterations_if_nonnull,
257 std::string_view cipher,
258 std::string_view digest,
260 size_t msec_in_iterations_out =
static_cast<size_t>(msec.count());
262 auto ret = pbes2_encrypt_shared(key_bits, passphrase, &msec_in_iterations_out, 0, cipher, digest, rng);
264 if(out_iterations_if_nonnull) {
265 *out_iterations_if_nonnull = msec_in_iterations_out;
271std::pair<AlgorithmIdentifier, std::vector<uint8_t>>
pbes2_encrypt_iter(std::span<const uint8_t> key_bits,
272 std::string_view passphrase,
274 std::string_view cipher,
275 std::string_view digest,
277 return pbes2_encrypt_shared(key_bits, passphrase,
nullptr, pbkdf_iter, cipher, digest, rng);
281 std::string_view passphrase,
282 const std::vector<uint8_t>& params) {
288 const auto cipher_spec =
split_on(cipher,
'/');
289 if(cipher_spec.size() != 2 || !known_pbes_cipher_mode(cipher_spec[1])) {
301 dec->set_key(derive_key(passphrase, kdf_algo, dec->key_spec().maximum_keylength()));
const std::vector< uint8_t > & parameters() const
BER_Decoder & decode(bool &out)
BER_Decoder & verify_end()
BER_Decoder start_sequence()
static std::unique_ptr< Cipher_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
std::string human_name_or_empty() const
static OID from_string(std::string_view str)
static std::unique_ptr< PasswordHashFamily > create_or_throw(std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< PasswordHashFamily > create(std::string_view algo_spec, std::string_view provider="")
std::string fmt(std::string_view format, const T &... args)
std::vector< std::string > split_on(std::string_view str, char delim)
std::pair< AlgorithmIdentifier, std::vector< uint8_t > > pbes2_encrypt_iter(std::span< const uint8_t > key_bits, std::string_view passphrase, size_t pbkdf_iter, std::string_view cipher, std::string_view digest, RandomNumberGenerator &rng)
secure_vector< uint8_t > pbes2_decrypt(std::span< const uint8_t > key_bits, std::string_view passphrase, const std::vector< uint8_t > ¶ms)
std::vector< T > unlock(const secure_vector< T > &in)
std::pair< AlgorithmIdentifier, std::vector< uint8_t > > pbes2_encrypt_msec(std::span< const uint8_t > key_bits, std::string_view passphrase, std::chrono::milliseconds msec, size_t *out_iterations_if_nonnull, std::string_view cipher, std::string_view digest, RandomNumberGenerator &rng)
std::vector< T, secure_allocator< T > > secure_vector
std::pair< AlgorithmIdentifier, std::vector< uint8_t > > pbes2_encrypt(std::span< const uint8_t > key_bits, std::string_view passphrase, std::chrono::milliseconds msec, std::string_view cipher, std::string_view digest, RandomNumberGenerator &rng)