Botan 3.14.1
Crypto and TLS for C&
kdf.cpp
Go to the documentation of this file.
1/*
2* KDF Retrieval
3* (C) 1999-2007 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/kdf.h>
9
10#include <botan/assert.h>
11#include <botan/exceptn.h>
12#include <botan/hash.h>
13#include <botan/mac.h>
14#include <botan/internal/algorithm_spec.h>
15#include <botan/internal/fmt.h>
16#include <botan/internal/mem_utils.h>
17#include <botan/internal/probe_providers.h>
18
19#if defined(BOTAN_HAS_HKDF)
20 #include <botan/internal/hkdf.h>
21#endif
22
23#if defined(BOTAN_HAS_KDF1)
24 #include <botan/internal/kdf1.h>
25#endif
26
27#if defined(BOTAN_HAS_KDF2)
28 #include <botan/internal/kdf2.h>
29#endif
30
31#if defined(BOTAN_HAS_KDF1_18033)
32 #include <botan/internal/kdf1_iso18033.h>
33#endif
34
35#if defined(BOTAN_HAS_TLS_V12_PRF)
36 #include <botan/internal/prf_tls.h>
37#endif
38
39#if defined(BOTAN_HAS_X942_PRF)
40 #include <botan/internal/prf_x942.h>
41#endif
42
43#if defined(BOTAN_HAS_SP800_108)
44 #include <botan/internal/sp800_108.h>
45#endif
46
47#if defined(BOTAN_HAS_SP800_56A)
48 #include <botan/internal/sp800_56c_one_step.h>
49#endif
50
51#if defined(BOTAN_HAS_SP800_56C)
52 #include <botan/internal/sp800_56c_two_step.h>
53#endif
54
55namespace Botan {
56
57namespace {
58
59template <typename KDF_Type, typename... ParamTs>
60std::unique_ptr<KDF> kdf_create_mac_or_hash(std::string_view nm, ParamTs&&... params) {
61 if(auto mac = MessageAuthenticationCode::create(fmt("HMAC({})", nm))) {
62 return std::make_unique<KDF_Type>(std::move(mac), std::forward<ParamTs>(params)...);
63 }
64
65 if(auto mac = MessageAuthenticationCode::create(nm)) {
66 return std::make_unique<KDF_Type>(std::move(mac), std::forward<ParamTs>(params)...);
67 }
68
69 return nullptr;
70}
71
72} // namespace
73
74std::unique_ptr<KDF> KDF::create(std::string_view algo_spec, std::string_view provider) {
75 const AlgorithmSpec req(algo_spec);
76
77#if defined(BOTAN_HAS_HKDF)
78 if(auto m = req.match("HKDF({prf})")) {
79 if(provider.empty() || provider == "base") {
80 return kdf_create_mac_or_hash<HKDF>(m->str("prf"));
81 }
82 }
83
84 if(auto m = req.match("HKDF-Extract({prf})")) {
85 if(provider.empty() || provider == "base") {
86 return kdf_create_mac_or_hash<HKDF_Extract>(m->str("prf"));
87 }
88 }
89
90 if(auto m = req.match("HKDF-Expand({prf})")) {
91 if(provider.empty() || provider == "base") {
92 return kdf_create_mac_or_hash<HKDF_Expand>(m->str("prf"));
93 }
94 }
95#endif
96
97#if defined(BOTAN_HAS_KDF2)
98 if(auto m = req.match("KDF2({hash})")) {
99 if(provider.empty() || provider == "base") {
100 if(auto hash = HashFunction::create(m->str("hash"))) {
101 return std::make_unique<KDF2>(std::move(hash));
102 }
103 }
104 }
105#endif
106
107#if defined(BOTAN_HAS_KDF1_18033)
108 if(auto m = req.match("KDF1-18033({hash})")) {
109 if(provider.empty() || provider == "base") {
110 if(auto hash = HashFunction::create(m->str("hash"))) {
111 return std::make_unique<KDF1_18033>(std::move(hash));
112 }
113 }
114 }
115#endif
116
117#if defined(BOTAN_HAS_KDF1)
118 if(auto m = req.match("KDF1({hash})")) {
119 if(provider.empty() || provider == "base") {
120 if(auto hash = HashFunction::create(m->str("hash"))) {
121 return std::make_unique<KDF1>(std::move(hash));
122 }
123 }
124 }
125#endif
126
127#if defined(BOTAN_HAS_TLS_V12_PRF)
128 if(auto m = req.match("TLS-12-PRF({prf})")) {
129 if(provider.empty() || provider == "base") {
130 return kdf_create_mac_or_hash<TLS_12_PRF>(m->str("prf"));
131 }
132 }
133#endif
134
135#if defined(BOTAN_HAS_X942_PRF)
136 if(auto m = req.match("X9.42-PRF({kek:str})")) {
137 if(provider.empty() || provider == "base") {
138 return std::make_unique<X942_PRF>(m->str("kek"));
139 }
140 }
141#endif
142
143#if defined(BOTAN_HAS_SP800_108)
144 if(auto m = req.match("SP800-108-Counter({prf},{r:int=32},{L:int=32})")) {
145 if(provider.empty() || provider == "base") {
146 return kdf_create_mac_or_hash<SP800_108_Counter>(m->str("prf"), m->integer("r"), m->integer("L"));
147 }
148 }
149
150 if(auto m = req.match("SP800-108-Feedback({prf},{r:int=32},{L:int=32})")) {
151 if(provider.empty() || provider == "base") {
152 return kdf_create_mac_or_hash<SP800_108_Feedback>(m->str("prf"), m->integer("r"), m->integer("L"));
153 }
154 }
155
156 if(auto m = req.match("SP800-108-Pipeline({prf},{r:int=32},{L:int=32})")) {
157 if(provider.empty() || provider == "base") {
158 return kdf_create_mac_or_hash<SP800_108_Pipeline>(m->str("prf"), m->integer("r"), m->integer("L"));
159 }
160 }
161#endif
162
163#if defined(BOTAN_HAS_SP800_56A)
164 if(auto m = req.match("SP800-56A({fn})")) {
165 if(provider.empty() || provider == "base") {
166 const auto fn = m->str("fn");
167 if(auto hash = HashFunction::create(fn)) {
168 return std::make_unique<SP800_56C_One_Step_Hash>(std::move(hash));
169 }
170 if(fn == "KMAC-128") {
171 return std::make_unique<SP800_56C_One_Step_KMAC128>();
172 }
173 if(fn == "KMAC-256") {
174 return std::make_unique<SP800_56C_One_Step_KMAC256>();
175 }
176 if(auto mac = MessageAuthenticationCode::create(fn)) {
177 return std::make_unique<SP800_56C_One_Step_HMAC>(std::move(mac));
178 }
179 }
180 }
181#endif
182
183#if defined(BOTAN_HAS_SP800_56C)
184 if(auto m = req.match("SP800-56C({prf})")) {
185 if(provider.empty() || provider == "base") {
186 const auto prf = m->str("prf");
187 std::unique_ptr<KDF> exp(kdf_create_mac_or_hash<SP800_108_Feedback>(prf, 32, 32));
188 if(exp) {
189 if(auto mac = MessageAuthenticationCode::create(fmt("HMAC({})", prf))) {
190 return std::make_unique<SP800_56C_Two_Step>(std::move(mac), std::move(exp));
191 }
192
193 if(auto mac = MessageAuthenticationCode::create(prf)) {
194 return std::make_unique<SP800_56C_Two_Step>(std::move(mac), std::move(exp));
195 }
196 }
197 }
198 }
199#endif
200
201 BOTAN_UNUSED(req);
202 BOTAN_UNUSED(provider);
203
204 return nullptr;
205}
206
207//static
208std::unique_ptr<KDF> KDF::create_or_throw(std::string_view algo, std::string_view provider) {
209 if(auto kdf = KDF::create(algo, provider)) {
210 return kdf;
211 }
212 throw Lookup_Error("KDF", algo, provider);
213}
214
215std::vector<std::string> KDF::providers(std::string_view algo_spec) {
216 return probe_providers_of<KDF>(algo_spec);
217}
218
219//static
220std::span<const uint8_t> KDF::_as_span(std::string_view s) {
221 return as_span_of_bytes(s);
222}
223
224} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:112
static std::unique_ptr< KDF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition kdf.cpp:208
static std::vector< std::string > providers(std::string_view algo_spec)
Definition kdf.cpp:215
static std::unique_ptr< KDF > create(std::string_view algo_spec, std::string_view provider="")
Definition kdf.cpp:74
void kdf(uint8_t key[], size_t key_len, const uint8_t secret[], size_t secret_len, const uint8_t salt[], size_t salt_len, const uint8_t label[], size_t label_len) const
Definition kdf.h:69
static std::unique_ptr< MessageAuthenticationCode > create(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:51
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:59
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})