• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 13274522654

11 Feb 2025 11:26PM UTC coverage: 91.645% (-0.007%) from 91.652%
13274522654

push

github

web-flow
Merge pull request #4647 from randombit/jack/internal-assert-and-mem-ops

Avoid using mem_ops.h or assert.h in public headers

94854 of 103501 relevant lines covered (91.65%)

11334975.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.24
/src/lib/kdf/kdf.cpp
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/fmt.h>
15
#include <botan/internal/scan_name.h>
16

17
#if defined(BOTAN_HAS_HKDF)
18
   #include <botan/internal/hkdf.h>
19
#endif
20

21
#if defined(BOTAN_HAS_KDF1)
22
   #include <botan/internal/kdf1.h>
23
#endif
24

25
#if defined(BOTAN_HAS_KDF2)
26
   #include <botan/internal/kdf2.h>
27
#endif
28

29
#if defined(BOTAN_HAS_KDF1_18033)
30
   #include <botan/internal/kdf1_iso18033.h>
31
#endif
32

33
#if defined(BOTAN_HAS_TLS_V12_PRF)
34
   #include <botan/internal/prf_tls.h>
35
#endif
36

37
#if defined(BOTAN_HAS_X942_PRF)
38
   #include <botan/internal/prf_x942.h>
39
#endif
40

41
#if defined(BOTAN_HAS_SP800_108)
42
   #include <botan/internal/sp800_108.h>
43
#endif
44

45
#if defined(BOTAN_HAS_SP800_56A)
46
   #include <botan/internal/sp800_56c_one_step.h>
47
#endif
48

49
#if defined(BOTAN_HAS_SP800_56C)
50
   #include <botan/internal/sp800_56c_two_step.h>
51
#endif
52

53
namespace Botan {
54

55
namespace {
56

57
template <typename KDF_Type, typename... ParamTs>
58
std::unique_ptr<KDF> kdf_create_mac_or_hash(std::string_view nm, ParamTs&&... params) {
6,836✔
59
   if(auto mac = MessageAuthenticationCode::create(fmt("HMAC({})", nm))) {
19,530✔
60
      return std::make_unique<KDF_Type>(std::move(mac), std::forward<ParamTs>(params)...);
5,858✔
61
   }
62

63
   if(auto mac = MessageAuthenticationCode::create(nm)) {
1,956✔
64
      return std::make_unique<KDF_Type>(std::move(mac), std::forward<ParamTs>(params)...);
978✔
65
   }
66

67
   return nullptr;
×
68
}
69

70
}  // namespace
71

72
std::unique_ptr<KDF> KDF::create(std::string_view algo_spec, std::string_view provider) {
8,010✔
73
   const SCAN_Name req(algo_spec);
8,010✔
74

75
#if defined(BOTAN_HAS_HKDF)
76
   if(req.algo_name() == "HKDF" && req.arg_count() == 1) {
8,010✔
77
      if(provider.empty() || provider == "base") {
13✔
78
         return kdf_create_mac_or_hash<HKDF>(req.arg(0));
26✔
79
      }
80
   }
81

82
   if(req.algo_name() == "HKDF-Extract" && req.arg_count() == 1) {
7,997✔
83
      if(provider.empty() || provider == "base") {
12✔
84
         return kdf_create_mac_or_hash<HKDF_Extract>(req.arg(0));
24✔
85
      }
86
   }
87

88
   if(req.algo_name() == "HKDF-Expand" && req.arg_count() == 1) {
7,985✔
89
      if(provider.empty() || provider == "base") {
12✔
90
         return kdf_create_mac_or_hash<HKDF_Expand>(req.arg(0));
24✔
91
      }
92
   }
93
#endif
94

95
#if defined(BOTAN_HAS_KDF2)
96
   if(req.algo_name() == "KDF2" && req.arg_count() == 1) {
7,973✔
97
      if(provider.empty() || provider == "base") {
529✔
98
         if(auto hash = HashFunction::create(req.arg(0))) {
1,058✔
99
            return std::make_unique<KDF2>(std::move(hash));
529✔
100
         }
529✔
101
      }
102
   }
103
#endif
104

105
#if defined(BOTAN_HAS_KDF1_18033)
106
   if(req.algo_name() == "KDF1-18033" && req.arg_count() == 1) {
7,444✔
107
      if(provider.empty() || provider == "base") {
72✔
108
         if(auto hash = HashFunction::create(req.arg(0))) {
144✔
109
            return std::make_unique<KDF1_18033>(std::move(hash));
72✔
110
         }
72✔
111
      }
112
   }
113
#endif
114

115
#if defined(BOTAN_HAS_KDF1)
116
   if(req.algo_name() == "KDF1" && req.arg_count() == 1) {
7,372✔
117
      if(provider.empty() || provider == "base") {
18✔
118
         if(auto hash = HashFunction::create(req.arg(0))) {
36✔
119
            return std::make_unique<KDF1>(std::move(hash));
18✔
120
         }
18✔
121
      }
122
   }
123
#endif
124

125
#if defined(BOTAN_HAS_TLS_V12_PRF)
126
   if(req.algo_name() == "TLS-12-PRF" && req.arg_count() == 1) {
7,354✔
127
      if(provider.empty() || provider == "base") {
5,862✔
128
         return kdf_create_mac_or_hash<TLS_12_PRF>(req.arg(0));
11,724✔
129
      }
130
   }
131
#endif
132

133
#if defined(BOTAN_HAS_X942_PRF)
134
   if(req.algo_name() == "X9.42-PRF" && req.arg_count() == 1) {
1,492✔
135
      if(provider.empty() || provider == "base") {
2✔
136
         return std::make_unique<X942_PRF>(req.arg(0));
2✔
137
      }
138
   }
139
#endif
140

141
#if defined(BOTAN_HAS_SP800_108)
142
   if(req.algo_name() == "SP800-108-Counter" && req.arg_count_between(1, 3)) {
1,490✔
143
      if(provider.empty() || provider == "base") {
299✔
144
         return kdf_create_mac_or_hash<SP800_108_Counter>(
299✔
145
            req.arg(0), req.arg_as_integer(1, 32), req.arg_as_integer(2, 32));
598✔
146
      }
147
   }
148

149
   if(req.algo_name() == "SP800-108-Feedback" && req.arg_count_between(1, 3)) {
1,191✔
150
      if(provider.empty() || provider == "base") {
299✔
151
         return kdf_create_mac_or_hash<SP800_108_Feedback>(
299✔
152
            req.arg(0), req.arg_as_integer(1, 32), req.arg_as_integer(2, 32));
598✔
153
      }
154
   }
155

156
   if(req.algo_name() == "SP800-108-Pipeline" && req.arg_count_between(1, 3)) {
892✔
157
      if(provider.empty() || provider == "base") {
299✔
158
         return kdf_create_mac_or_hash<SP800_108_Pipeline>(
299✔
159
            req.arg(0), req.arg_as_integer(1, 32), req.arg_as_integer(2, 32));
598✔
160
      }
161
   }
162
#endif
163

164
#if defined(BOTAN_HAS_SP800_56A)
165
   if(req.algo_name() == "SP800-56A" && req.arg_count() == 1) {
593✔
166
      if(auto hash = HashFunction::create(req.arg(0))) {
1,042✔
167
         return std::make_unique<SP800_56C_One_Step_Hash>(std::move(hash));
244✔
168
      }
244✔
169
      if(req.arg(0) == "KMAC-128") {
277✔
170
         return std::make_unique<SP800_56C_One_Step_KMAC128>();
9✔
171
      }
172
      if(req.arg(0) == "KMAC-256") {
268✔
173
         return std::make_unique<SP800_56C_One_Step_KMAC256>();
×
174
      }
175
      if(auto mac = MessageAuthenticationCode::create(req.arg(0))) {
536✔
176
         return std::make_unique<SP800_56C_One_Step_HMAC>(std::move(mac));
268✔
177
      }
268✔
178
   }
179
#endif
180

181
#if defined(BOTAN_HAS_SP800_56C)
182
   if(req.algo_name() == "SP800-56C" && req.arg_count() == 1) {
72✔
183
      std::unique_ptr<KDF> exp(kdf_create_mac_or_hash<SP800_108_Feedback>(req.arg(0), 32, 32));
40✔
184
      if(exp) {
40✔
185
         if(auto mac = MessageAuthenticationCode::create(req.arg(0))) {
80✔
186
            return std::make_unique<SP800_56C_Two_Step>(std::move(mac), std::move(exp));
40✔
187
         }
40✔
188

189
         if(auto mac = MessageAuthenticationCode::create(fmt("HMAC({})", req.arg(0)))) {
×
190
            return std::make_unique<SP800_56C_Two_Step>(std::move(mac), std::move(exp));
×
191
         }
×
192
      }
193
   }
40✔
194
#endif
195

196
   BOTAN_UNUSED(req);
32✔
197
   BOTAN_UNUSED(provider);
32✔
198

199
   return nullptr;
32✔
200
}
8,010✔
201

202
//static
203
std::unique_ptr<KDF> KDF::create_or_throw(std::string_view algo, std::string_view provider) {
6,313✔
204
   if(auto kdf = KDF::create(algo, provider)) {
6,313✔
205
      return kdf;
6,313✔
206
   }
6,313✔
207
   throw Lookup_Error("KDF", algo, provider);
×
208
}
209

210
std::vector<std::string> KDF::providers(std::string_view algo_spec) {
×
211
   return probe_providers_of<KDF>(algo_spec);
×
212
}
213

214
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc