Botan 3.5.0
Crypto and TLS for C&
tls_algos.cpp
Go to the documentation of this file.
1/*
2* (C) 2017 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/tls_algos.h>
8
9#include <botan/ec_group.h>
10#include <botan/exceptn.h>
11#include <botan/internal/fmt.h>
12
13namespace Botan::TLS {
14
15std::string kdf_algo_to_string(KDF_Algo algo) {
16 switch(algo) {
17 case KDF_Algo::SHA_1:
18 return "SHA-1";
20 return "SHA-256";
22 return "SHA-384";
23 }
24
25 throw Invalid_State("kdf_algo_to_string unknown enum value");
26}
27
28std::string kex_method_to_string(Kex_Algo method) {
29 switch(method) {
31 return "RSA";
32 case Kex_Algo::DH:
33 return "DH";
34 case Kex_Algo::ECDH:
35 return "ECDH";
36 case Kex_Algo::PSK:
37 return "PSK";
39 return "ECDHE_PSK";
41 return "DHE_PSK";
42 case Kex_Algo::KEM:
43 return "KEM";
45 return "KEM_PSK";
47 return "HYBRID";
49 return "HYBRID_PSK";
51 return "UNDEFINED";
52 }
53
54 throw Invalid_State("kex_method_to_string unknown enum value");
55}
56
57Kex_Algo kex_method_from_string(std::string_view str) {
58 if(str == "RSA") {
60 }
61
62 if(str == "DH") {
63 return Kex_Algo::DH;
64 }
65
66 if(str == "ECDH") {
67 return Kex_Algo::ECDH;
68 }
69
70 if(str == "PSK") {
71 return Kex_Algo::PSK;
72 }
73
74 if(str == "ECDHE_PSK") {
76 }
77
78 if(str == "DHE_PSK") {
79 return Kex_Algo::DHE_PSK;
80 }
81
82 if(str == "KEM") {
83 return Kex_Algo::KEM;
84 }
85
86 if(str == "KEM_PSK") {
87 return Kex_Algo::KEM_PSK;
88 }
89
90 if(str == "HYBRID") {
91 return Kex_Algo::HYBRID;
92 }
93
94 if(str == "HYBRID_PSK") {
96 }
97
98 if(str == "UNDEFINED") {
100 }
101
102 throw Invalid_Argument(fmt("Unknown kex method '{}'", str));
103}
104
106 switch(method) {
107 case Auth_Method::RSA:
108 return "RSA";
110 return "ECDSA";
112 return "IMPLICIT";
114 return "UNDEFINED";
115 }
116
117 throw Invalid_State("auth_method_to_string unknown enum value");
118}
119
121 if(str == "RSA") {
122 return Auth_Method::RSA;
123 }
124 if(str == "ECDSA") {
125 return Auth_Method::ECDSA;
126 }
127 if(str == "IMPLICIT") {
129 }
130 if(str == "UNDEFINED") {
132 }
133
134 throw Invalid_Argument(fmt("Unknown TLS signature method '{}'", str));
135}
136
137std::optional<Group_Params> Group_Params::from_string(std::string_view group_name) {
138 if(group_name == "secp256r1") {
139 return Group_Params::SECP256R1;
140 }
141 if(group_name == "secp384r1") {
142 return Group_Params::SECP384R1;
143 }
144 if(group_name == "secp521r1") {
145 return Group_Params::SECP521R1;
146 }
147 if(group_name == "brainpool256r1") {
148 return Group_Params::BRAINPOOL256R1;
149 }
150 if(group_name == "brainpool384r1") {
151 return Group_Params::BRAINPOOL384R1;
152 }
153 if(group_name == "brainpool512r1") {
154 return Group_Params::BRAINPOOL512R1;
155 }
156 if(group_name == "x25519") {
157 return Group_Params::X25519;
158 }
159 if(group_name == "x448") {
160 return Group_Params::X448;
161 }
162
163 if(group_name == "ffdhe/ietf/2048") {
164 return Group_Params::FFDHE_2048;
165 }
166 if(group_name == "ffdhe/ietf/3072") {
167 return Group_Params::FFDHE_3072;
168 }
169 if(group_name == "ffdhe/ietf/4096") {
170 return Group_Params::FFDHE_4096;
171 }
172 if(group_name == "ffdhe/ietf/6144") {
173 return Group_Params::FFDHE_6144;
174 }
175 if(group_name == "ffdhe/ietf/8192") {
176 return Group_Params::FFDHE_8192;
177 }
178
179 if(group_name == "Kyber-512-r3") {
180 return Group_Params::KYBER_512_R3_OQS;
181 }
182 if(group_name == "Kyber-768-r3") {
183 return Group_Params::KYBER_768_R3_OQS;
184 }
185 if(group_name == "Kyber-1024-r3") {
186 return Group_Params::KYBER_1024_R3_OQS;
187 }
188
189 if(group_name == "eFrodoKEM-640-SHAKE") {
190 return Group_Params::eFRODOKEM_640_SHAKE_OQS;
191 }
192 if(group_name == "eFrodoKEM-976-SHAKE") {
193 return Group_Params::eFRODOKEM_976_SHAKE_OQS;
194 }
195 if(group_name == "eFrodoKEM-1344-SHAKE") {
196 return Group_Params::eFRODOKEM_1344_SHAKE_OQS;
197 }
198 if(group_name == "eFrodoKEM-640-AES") {
199 return Group_Params::eFRODOKEM_640_AES_OQS;
200 }
201 if(group_name == "eFrodoKEM-976-AES") {
202 return Group_Params::eFRODOKEM_976_AES_OQS;
203 }
204 if(group_name == "eFrodoKEM-1344-AES") {
205 return Group_Params::eFRODOKEM_1344_AES_OQS;
206 }
207
208 if(group_name == "x25519/Kyber-512-r3/cloudflare") {
209 return Group_Params::HYBRID_X25519_KYBER_512_R3_CLOUDFLARE;
210 }
211 if(group_name == "x25519/Kyber-512-r3") {
212 return Group_Params::HYBRID_X25519_KYBER_512_R3_OQS;
213 }
214 if(group_name == "x25519/Kyber-768-r3") {
215 return Group_Params::HYBRID_X25519_KYBER_768_R3_OQS;
216 }
217 if(group_name == "x448/Kyber-768-r3") {
218 return Group_Params::HYBRID_X448_KYBER_768_R3_OQS;
219 }
220 if(group_name == "x25519/eFrodoKEM-640-SHAKE") {
221 return Group_Params::HYBRID_X25519_eFRODOKEM_640_SHAKE_OQS;
222 }
223 if(group_name == "x25519/eFrodoKEM-640-AES") {
224 return Group_Params::HYBRID_X25519_eFRODOKEM_640_AES_OQS;
225 }
226 if(group_name == "x448/eFrodoKEM-976-SHAKE") {
227 return Group_Params::HYBRID_X448_eFRODOKEM_976_SHAKE_OQS;
228 }
229 if(group_name == "x448/eFrodoKEM-976-AES") {
230 return Group_Params::HYBRID_X448_eFRODOKEM_976_AES_OQS;
231 }
232
233 if(group_name == "secp256r1/Kyber-512-r3") {
234 return Group_Params::HYBRID_SECP256R1_KYBER_512_R3_OQS;
235 }
236 if(group_name == "secp256r1/Kyber-768-r3") {
237 return Group_Params::HYBRID_SECP256R1_KYBER_768_R3_OQS;
238 }
239 if(group_name == "secp256r1/eFrodoKEM-640-SHAKE") {
240 return Group_Params::HYBRID_SECP256R1_eFRODOKEM_640_SHAKE_OQS;
241 }
242 if(group_name == "secp256r1/eFrodoKEM-640-AES") {
243 return Group_Params::HYBRID_SECP256R1_eFRODOKEM_640_AES_OQS;
244 }
245
246 if(group_name == "secp384r1/Kyber-768-r3") {
247 return Group_Params::HYBRID_SECP384R1_KYBER_768_R3_OQS;
248 }
249 if(group_name == "secp384r1/eFrodoKEM-976-SHAKE") {
250 return Group_Params::HYBRID_SECP384R1_eFRODOKEM_976_SHAKE_OQS;
251 }
252 if(group_name == "secp384r1/eFrodoKEM-976-AES") {
253 return Group_Params::HYBRID_SECP384R1_eFRODOKEM_976_AES_OQS;
254 }
255
256 if(group_name == "secp521r1/Kyber-1024-r3") {
257 return Group_Params::HYBRID_SECP521R1_KYBER_1024_R3_OQS;
258 }
259 if(group_name == "secp521r1/eFrodoKEM-1344-SHAKE") {
260 return Group_Params::HYBRID_SECP521R1_eFRODOKEM_1344_SHAKE_OQS;
261 }
262 if(group_name == "secp521r1/eFrodoKEM-1344-AES") {
263 return Group_Params::HYBRID_SECP521R1_eFRODOKEM_1344_AES_OQS;
264 }
265
266 return std::nullopt;
267}
268
269std::optional<std::string> Group_Params::to_string() const {
270 switch(m_code) {
271 case Group_Params::SECP256R1:
272 return "secp256r1";
273 case Group_Params::SECP384R1:
274 return "secp384r1";
275 case Group_Params::SECP521R1:
276 return "secp521r1";
277 case Group_Params::BRAINPOOL256R1:
278 return "brainpool256r1";
279 case Group_Params::BRAINPOOL384R1:
280 return "brainpool384r1";
281 case Group_Params::BRAINPOOL512R1:
282 return "brainpool512r1";
283 case Group_Params::X25519:
284 return "x25519";
285 case Group_Params::X448:
286 return "x448";
287
288 case Group_Params::FFDHE_2048:
289 return "ffdhe/ietf/2048";
290 case Group_Params::FFDHE_3072:
291 return "ffdhe/ietf/3072";
292 case Group_Params::FFDHE_4096:
293 return "ffdhe/ietf/4096";
294 case Group_Params::FFDHE_6144:
295 return "ffdhe/ietf/6144";
296 case Group_Params::FFDHE_8192:
297 return "ffdhe/ietf/8192";
298
299 case Group_Params::KYBER_512_R3_OQS:
300 return "Kyber-512-r3";
301 case Group_Params::KYBER_768_R3_OQS:
302 return "Kyber-768-r3";
303 case Group_Params::KYBER_1024_R3_OQS:
304 return "Kyber-1024-r3";
305
306 case Group_Params::eFRODOKEM_640_SHAKE_OQS:
307 return "eFrodoKEM-640-SHAKE";
308 case Group_Params::eFRODOKEM_976_SHAKE_OQS:
309 return "eFrodoKEM-976-SHAKE";
310 case Group_Params::eFRODOKEM_1344_SHAKE_OQS:
311 return "eFrodoKEM-1344-SHAKE";
312 case Group_Params::eFRODOKEM_640_AES_OQS:
313 return "eFrodoKEM-640-AES";
314 case Group_Params::eFRODOKEM_976_AES_OQS:
315 return "eFrodoKEM-976-AES";
316 case Group_Params::eFRODOKEM_1344_AES_OQS:
317 return "eFrodoKEM-1344-AES";
318
319 case Group_Params::HYBRID_X25519_eFRODOKEM_640_SHAKE_OQS:
320 return "x25519/eFrodoKEM-640-SHAKE";
321 case Group_Params::HYBRID_X25519_eFRODOKEM_640_AES_OQS:
322 return "x25519/eFrodoKEM-640-AES";
323 case Group_Params::HYBRID_X448_eFRODOKEM_976_SHAKE_OQS:
324 return "x448/eFrodoKEM-976-SHAKE";
325 case Group_Params::HYBRID_X448_eFRODOKEM_976_AES_OQS:
326 return "x448/eFrodoKEM-976-AES";
327 case Group_Params::HYBRID_SECP256R1_eFRODOKEM_640_SHAKE_OQS:
328 return "secp256r1/eFrodoKEM-640-SHAKE";
329 case Group_Params::HYBRID_SECP256R1_eFRODOKEM_640_AES_OQS:
330 return "secp256r1/eFrodoKEM-640-AES";
331 case Group_Params::HYBRID_SECP384R1_eFRODOKEM_976_SHAKE_OQS:
332 return "secp384r1/eFrodoKEM-976-SHAKE";
333 case Group_Params::HYBRID_SECP384R1_eFRODOKEM_976_AES_OQS:
334 return "secp384r1/eFrodoKEM-976-AES";
335 case Group_Params::HYBRID_SECP521R1_eFRODOKEM_1344_SHAKE_OQS:
336 return "secp521r1/eFrodoKEM-1344-SHAKE";
337 case Group_Params::HYBRID_SECP521R1_eFRODOKEM_1344_AES_OQS:
338 return "secp521r1/eFrodoKEM-1344-AES";
339
340 case Group_Params::HYBRID_X25519_KYBER_512_R3_CLOUDFLARE:
341 return "x25519/Kyber-512-r3/cloudflare";
342
343 case Group_Params::HYBRID_X25519_KYBER_512_R3_OQS:
344 return "x25519/Kyber-512-r3";
345 case Group_Params::HYBRID_X25519_KYBER_768_R3_OQS:
346 return "x25519/Kyber-768-r3";
347 case Group_Params::HYBRID_X448_KYBER_768_R3_OQS:
348 return "x448/Kyber-768-r3";
349
350 case Group_Params::HYBRID_SECP256R1_KYBER_512_R3_OQS:
351 return "secp256r1/Kyber-512-r3";
352 case Group_Params::HYBRID_SECP256R1_KYBER_768_R3_OQS:
353 return "secp256r1/Kyber-768-r3";
354 case Group_Params::HYBRID_SECP384R1_KYBER_768_R3_OQS:
355 return "secp384r1/Kyber-768-r3";
356 case Group_Params::HYBRID_SECP521R1_KYBER_1024_R3_OQS:
357 return "secp521r1/Kyber-1024-r3";
358
359 default:
360 return std::nullopt;
361 }
362}
363
364} // namespace Botan::TLS
std::optional< std::string > to_string() const
static std::optional< Group_Params > from_string(std::string_view group_name)
Kex_Algo kex_method_from_string(std::string_view str)
Definition tls_algos.cpp:57
Auth_Method auth_method_from_string(std::string_view str)
std::string kdf_algo_to_string(KDF_Algo algo)
Definition tls_algos.cpp:15
std::string kex_method_to_string(Kex_Algo method)
Definition tls_algos.cpp:28
std::string auth_method_to_string(Auth_Method method)
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53