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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

94.03
/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_56a.h>
46
#endif
47

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

52
namespace Botan {
53

54
namespace {
55

56
template <typename KDF_Type>
57
std::unique_ptr<KDF> kdf_create_mac_or_hash(std::string_view nm) {
6,513✔
58
   if(auto mac = MessageAuthenticationCode::create(fmt("HMAC({})", nm)))
18,738✔
59
      return std::make_unique<KDF_Type>(std::move(mac));
5,712✔
60

61
   if(auto mac = MessageAuthenticationCode::create(nm))
1,602✔
62
      return std::make_unique<KDF_Type>(std::move(mac));
801✔
63

64
   return nullptr;
6,513✔
65
}
66

67
}
68

69
std::unique_ptr<KDF> KDF::create(std::string_view algo_spec, std::string_view provider) {
7,666✔
70
   const SCAN_Name req(algo_spec);
7,666✔
71

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

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

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

92
#if defined(BOTAN_HAS_KDF2)
93
   if(req.algo_name() == "KDF2" && req.arg_count() == 1) {
22,887✔
94
      if(provider.empty() || provider == "base") {
549✔
95
         if(auto hash = HashFunction::create(req.arg(0)))
1,098✔
96
            return std::make_unique<KDF2>(std::move(hash));
549✔
97
      }
98
   }
99
#endif
100

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

110
#if defined(BOTAN_HAS_KDF1)
111
   if(req.algo_name() == "KDF1" && req.arg_count() == 1) {
21,024✔
112
      if(provider.empty() || provider == "base") {
18✔
113
         if(auto hash = HashFunction::create(req.arg(0)))
36✔
114
            return std::make_unique<KDF1>(std::move(hash));
18✔
115
      }
116
   }
117
#endif
118

119
#if defined(BOTAN_HAS_TLS_V12_PRF)
120
   if(req.algo_name() == "TLS-12-PRF" && req.arg_count() == 1) {
20,970✔
121
      if(provider.empty() || provider == "base") {
5,716✔
122
         return kdf_create_mac_or_hash<TLS_12_PRF>(req.arg(0));
11,432✔
123
      }
124
   }
125
#endif
126

127
#if defined(BOTAN_HAS_X942_PRF)
128
   if(req.algo_name() == "X9.42-PRF" && req.arg_count() == 1) {
3,822✔
129
      if(provider.empty() || provider == "base") {
2✔
130
         return std::make_unique<X942_PRF>(req.arg(0));
4✔
131
      }
132
   }
133
#endif
134

135
#if defined(BOTAN_HAS_SP800_108)
136
   if(req.algo_name() == "SP800-108-Counter" && req.arg_count() == 1) {
3,816✔
137
      if(provider.empty() || provider == "base") {
240✔
138
         return kdf_create_mac_or_hash<SP800_108_Counter>(req.arg(0));
480✔
139
      }
140
   }
141

142
   if(req.algo_name() == "SP800-108-Feedback" && req.arg_count() == 1) {
3,096✔
143
      if(provider.empty() || provider == "base") {
240✔
144
         return kdf_create_mac_or_hash<SP800_108_Feedback>(req.arg(0));
480✔
145
      }
146
   }
147

148
   if(req.algo_name() == "SP800-108-Pipeline" && req.arg_count() == 1) {
2,376✔
149
      if(provider.empty() || provider == "base") {
240✔
150
         return kdf_create_mac_or_hash<SP800_108_Pipeline>(req.arg(0));
480✔
151
      }
152
   }
153
#endif
154

155
#if defined(BOTAN_HAS_SP800_56A)
156
   if(req.algo_name() == "SP800-56A" && req.arg_count() == 1) {
1,656✔
157
      if(auto hash = HashFunction::create(req.arg(0)))
960✔
158
         return std::make_unique<SP800_56A_Hash>(std::move(hash));
235✔
159
      if(auto mac = MessageAuthenticationCode::create(req.arg(0)))
490✔
160
         return std::make_unique<SP800_56A_HMAC>(std::move(mac));
245✔
161
   }
162
#endif
163

164
#if defined(BOTAN_HAS_SP800_56C)
165
   if(req.algo_name() == "SP800-56C" && req.arg_count() == 1) {
216✔
166
      std::unique_ptr<KDF> exp(kdf_create_mac_or_hash<SP800_108_Feedback>(req.arg(0)));
40✔
167
      if(exp) {
40✔
168
         if(auto mac = MessageAuthenticationCode::create(req.arg(0)))
80✔
169
            return std::make_unique<SP800_56C>(std::move(mac), std::move(exp));
40✔
170

171
         if(auto mac = MessageAuthenticationCode::create(fmt("HMAC({})", req.arg(0))))
×
172
            return std::make_unique<SP800_56C>(std::move(mac), std::move(exp));
×
173
      }
174
   }
40✔
175
#endif
176

177
   BOTAN_UNUSED(req);
32✔
178
   BOTAN_UNUSED(provider);
32✔
179

180
   return nullptr;
32✔
181
}
7,666✔
182

183
//static
184
std::unique_ptr<KDF> KDF::create_or_throw(std::string_view algo, std::string_view provider) {
6,187✔
185
   if(auto kdf = KDF::create(algo, provider)) {
6,187✔
186
      return kdf;
6,187✔
187
   }
6,187✔
188
   throw Lookup_Error("KDF", algo, provider);
×
189
}
190

191
std::vector<std::string> KDF::providers(std::string_view algo_spec) { return probe_providers_of<KDF>(algo_spec); }
×
192

193
}
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