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

randombit / botan / 12772029907

14 Jan 2025 04:26PM UTC coverage: 91.24% (-0.01%) from 91.251%
12772029907

Pull #4551

github

web-flow
Merge 276278dd0 into 88c334057
Pull Request #4551: SP.800-108 may handle different counter and L encoding widths

93545 of 102526 relevant lines covered (91.24%)

11467026.07 hits per line

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

90.0
/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/exceptn.h>
11
#include <botan/hash.h>
12
#include <botan/mac.h>
13
#include <botan/internal/fmt.h>
14
#include <botan/internal/scan_name.h>
15

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

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

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

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

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

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

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

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

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

52
namespace Botan {
53

54
namespace {
55

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

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

66
   return nullptr;
×
67
}
68

69
}  // namespace
70

71
std::unique_ptr<KDF> KDF::create(std::string_view algo_spec, std::string_view provider) {
7,833✔
72
   const SCAN_Name req(algo_spec);
7,833✔
73

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

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

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

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

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

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

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

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

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

148
   if(req.algo_name() == "SP800-108-Feedback" && req.arg_count() == 1) {
1,073✔
149
      if(provider.empty() || provider == "base") {
240✔
150
         return kdf_create_mac_or_hash<SP800_108_Feedback>(req.arg(0));
480✔
151
      }
152
   }
153

154
   if(req.algo_name() == "SP800-108-Pipeline" && req.arg_count() == 1) {
833✔
155
      if(provider.empty() || provider == "base") {
240✔
156
         return kdf_create_mac_or_hash<SP800_108_Pipeline>(req.arg(0));
480✔
157
      }
158
   }
159
#endif
160

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

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

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

193
   BOTAN_UNUSED(req);
32✔
194
   BOTAN_UNUSED(provider);
32✔
195

196
   return nullptr;
32✔
197
}
7,833✔
198

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

207
std::vector<std::string> KDF::providers(std::string_view algo_spec) {
×
208
   return probe_providers_of<KDF>(algo_spec);
×
209
}
210

211
}  // 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