Botan 3.13.0
Crypto and TLS for C&
pkcs12_kdf.h
Go to the documentation of this file.
1/*
2* PKCS#12 KDF (RFC 7292 Appendix B)
3* (C) 2026 Damiano Mazzella
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_PKCS12_KDF_H_
9#define BOTAN_PKCS12_KDF_H_
10
11#include <botan/hash.h>
12#include <botan/pwdhash.h>
13#include <botan/types.h>
14
15#include <memory>
16#include <span>
17#include <string>
18#include <string_view>
19
20namespace Botan {
21
22/**
23* PKCS#12 KDF (RFC 7292 Appendix B)
24*
25* The id parameter (purpose byte) is fixed at family construction time:
26* 1 = encryption key, 2 = IV, 3 = MAC key
27*
28* The family spec used with PasswordHashFamily::create() is
29* "PKCS12-KDF(hash,id)", e.g. "PKCS12-KDF(SHA-1,1)" with two arguments.
30* PKCS12_KDF::to_string() additionally embeds the iteration count for
31* round-tripping a fully-parameterized instance, producing
32* "PKCS12-KDF(hash,id,iterations)"; that 3-argument form is not accepted
33* by PasswordHashFamily::create() and should not be used as an algo spec.
34*/
35class PKCS12_KDF final : public PasswordHash {
36 public:
37 PKCS12_KDF(std::unique_ptr<HashFunction> hash, uint8_t id, size_t iterations);
38
39 std::string to_string() const override;
40
41 size_t iterations() const override { return m_iterations; }
42
43 void derive_key(uint8_t out[],
44 size_t out_len,
45 const char* password,
46 size_t password_len,
47 const uint8_t salt[],
48 size_t salt_len) const override;
49
50 private:
51 std::unique_ptr<HashFunction> m_hash;
52 uint8_t m_id;
53 size_t m_iterations;
54};
55
56/**
57* Low-level PKCS#12 KDF (RFC 7292 Appendix B).
58*
59* Runs the KDF on a caller-supplied @p pwd_bytes buffer without applying
60* @c pkcs12_encode_password. Intended for the very rare case (e.g. OpenSSL
61* empty-password interop) where the RFC 7292 UCS-2 BE encoding + null
62* terminator is not the desired input. Normal callers should use the
63* PKCS12_KDF class.
64*/
65BOTAN_TEST_API void pkcs12_kdf(std::span<uint8_t> out,
66 std::span<const uint8_t> pwd_bytes,
67 std::span<const uint8_t> salt,
68 size_t iterations,
69 uint8_t id,
70 HashFunction& hash);
71
73 public:
74 PKCS12_KDF_Family(std::unique_ptr<HashFunction> hash, size_t id);
75
76 std::string name() const override;
77
78 std::unique_ptr<PasswordHash> tune_params(size_t output_length,
79 uint64_t desired_runtime_msec,
80 std::optional<size_t> max_memory_usage_mb = {},
81 uint64_t tuning_msec = 10) const override;
82
83 std::unique_ptr<PasswordHash> default_params() const override;
84
85 std::unique_ptr<PasswordHash> from_iterations(size_t iterations) const override;
86
87 std::unique_ptr<PasswordHash> from_params(size_t i1, size_t i2 = 0, size_t i3 = 0) const override;
88
89 private:
90 std::unique_ptr<HashFunction> m_hash;
91 uint8_t m_id;
92};
93
94} // namespace Botan
95
96#endif
#define BOTAN_TEST_API
Definition api.h:41
std::string name() const override
std::unique_ptr< PasswordHash > from_params(size_t i1, size_t i2=0, size_t i3=0) const override
std::unique_ptr< PasswordHash > tune_params(size_t output_length, uint64_t desired_runtime_msec, std::optional< size_t > max_memory_usage_mb={}, uint64_t tuning_msec=10) const override
std::unique_ptr< PasswordHash > default_params() const override
std::unique_ptr< PasswordHash > from_iterations(size_t iterations) const override
PKCS12_KDF_Family(std::unique_ptr< HashFunction > hash, size_t id)
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
std::string to_string() const override
PKCS12_KDF(std::unique_ptr< HashFunction > hash, uint8_t id, size_t iterations)
size_t iterations() const override
Definition pkcs12_kdf.h:41
void hash(std::span< uint8_t > out, std::string_view password, std::span< const uint8_t > salt) const
Definition pwdhash.h:95
void pkcs12_kdf(std::span< uint8_t > out, std::span< const uint8_t > pwd_bytes, std::span< const uint8_t > salt, size_t iterations, uint8_t id, HashFunction &hash)