Botan 3.13.0
Crypto and TLS for C&
frodo_mode.cpp
Go to the documentation of this file.
1/*
2 * FrodoKEM modes and constants
3 *
4 * The Fellowship of the FrodoKEM:
5 * (C) 2023 Jack Lloyd
6 * 2023 René Meusel, Amos Treiber - Rohde & Schwarz Cybersecurity
7 *
8 * Botan is released under the Simplified BSD License (see license.txt)
9 */
10
11#include <botan/frodo_mode.h>
12
13#include <botan/assert.h>
14#include <botan/internal/fmt.h>
15
16namespace Botan {
17
18namespace {
19
20FrodoKEMMode::Mode FrodoKEM_mode_from_string(std::string_view str) {
21 if(str == "FrodoKEM-640-SHAKE") {
23 }
24 if(str == "FrodoKEM-976-SHAKE") {
26 }
27 if(str == "FrodoKEM-1344-SHAKE") {
29 }
30 if(str == "eFrodoKEM-640-SHAKE") {
32 }
33 if(str == "eFrodoKEM-976-SHAKE") {
35 }
36 if(str == "eFrodoKEM-1344-SHAKE") {
38 }
39
40 if(str == "FrodoKEM-640-AES") {
42 }
43 if(str == "FrodoKEM-976-AES") {
45 }
46 if(str == "FrodoKEM-1344-AES") {
48 }
49 if(str == "eFrodoKEM-640-AES") {
51 }
52 if(str == "eFrodoKEM-976-AES") {
54 }
55 if(str == "eFrodoKEM-1344-AES") {
57 }
58
59 throw Invalid_Argument(fmt("'{}' is not a valid FrodoKEM mode name", str));
60}
61
62FrodoKEMMode::Mode FrodoKEM_mode_from_oid(const OID& oid) {
63 if(const auto name = oid.registered_name()) {
64 return FrodoKEM_mode_from_string(*name);
65 }
66
67 throw Invalid_Argument(fmt("OID '{}' is not registered as a FrodoKEM mode", oid));
68}
69
70} // anonymous namespace
71
73
74FrodoKEMMode::FrodoKEMMode(const OID& oid) : m_mode(FrodoKEM_mode_from_oid(oid)) {}
75
76FrodoKEMMode::FrodoKEMMode(std::string_view str) : m_mode(FrodoKEM_mode_from_string(str)) {}
77
81
83 if(is_aes()) {
84#if defined(BOTAN_HAS_FRODOKEM_AES)
85 return true;
86#else
87 return false;
88#endif
89 }
90
91 if(is_shake()) {
92#if defined(BOTAN_HAS_FRODOKEM_SHAKE)
93 return true;
94#else
95 return false;
96#endif
97 }
98
99 return false;
100}
101
102std::string FrodoKEMMode::to_string() const {
103 switch(m_mode) {
105 return "FrodoKEM-640-SHAKE";
107 return "FrodoKEM-976-SHAKE";
109 return "FrodoKEM-1344-SHAKE";
111 return "eFrodoKEM-640-SHAKE";
113 return "eFrodoKEM-976-SHAKE";
115 return "eFrodoKEM-1344-SHAKE";
116
117 case FrodoKEM640_AES:
118 return "FrodoKEM-640-AES";
119 case FrodoKEM976_AES:
120 return "FrodoKEM-976-AES";
121 case FrodoKEM1344_AES:
122 return "FrodoKEM-1344-AES";
123 case eFrodoKEM640_AES:
124 return "eFrodoKEM-640-AES";
125 case eFrodoKEM976_AES:
126 return "eFrodoKEM-976-AES";
128 return "eFrodoKEM-1344-AES";
129 }
130
132}
133
134} // namespace Botan
#define BOTAN_ASSERT_UNREACHABLE()
Definition assert.h:166
bool is_aes() const
Definition frodo_mode.h:63
bool is_available() const
std::string to_string() const
Mode mode() const
Definition frodo_mode.h:46
bool is_shake() const
Definition frodo_mode.h:58
FrodoKEMMode(Mode mode)
OID object_identifier() const
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:80
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53