Skip to content

Commit 79747f4

Browse files
committed
Move magic enum to local and avoid allocation
WE2-1204 Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent 8804358 commit 79747f4

8 files changed

Lines changed: 58 additions & 60 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ add_library(${PROJECT_NAME}
3939
src/electronic-ids/ms-cryptoapi/MsCryptoApiElectronicID.cpp
4040
src/electronic-ids/ms-cryptoapi/MsCryptoApiElectronicID.hpp
4141
>
42+
src/magic_enum/magic_enum.hpp
4243
)
4344

4445
target_include_directories(${PROJECT_NAME}

include/electronic-id/electronic-id.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <functional>
2828
#include <optional>
29+
#include <set>
2930

3031
namespace electronic_id
3132
{
@@ -99,15 +100,15 @@ class ElectronicID
99100
* By default, this function does nothing. It serves as an extension point for
100101
* Pkcs11ElectronicID which needs to release the PKCS#11 module before the application exits to
101102
* prevent potential crashes. */
102-
virtual void release() const {}
103+
virtual void release() const { }
103104

104105
virtual std::string name() const = 0;
105106
virtual Type type() const = 0;
106107

107108
virtual pcsc_cpp::SmartCard const& smartcard() const { return card; }
108109

109110
protected:
110-
ElectronicID(pcsc_cpp::SmartCard&& _card) noexcept : card(std::move(_card)) {}
111+
ElectronicID(pcsc_cpp::SmartCard&& _card) noexcept : card(std::move(_card)) { }
111112

112113
pcsc_cpp::SmartCard card;
113114
};

include/electronic-id/enums.hpp

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
#include "pcsc-cpp/pcsc-cpp.hpp"
2626

27-
#include <set>
2827
#include <string>
2928

3029
namespace electronic_id
@@ -35,15 +34,13 @@ class CertificateType
3534
public:
3635
enum CertificateTypeEnum : int8_t { AUTHENTICATION, SIGNING, NONE = -1 };
3736

38-
CertificateType() = default;
39-
constexpr CertificateType(const CertificateTypeEnum _value) : value(_value) {}
37+
constexpr CertificateType() noexcept = default;
38+
constexpr CertificateType(const CertificateTypeEnum _value) noexcept : value(_value) { }
4039

41-
bool isAuthentication() const { return value == AUTHENTICATION; }
40+
constexpr bool isAuthentication() const noexcept { return value == AUTHENTICATION; }
41+
constexpr bool isSigning() const noexcept { return value == SIGNING; }
4242

43-
bool isSigning() const { return value == SIGNING; }
44-
45-
constexpr bool operator==(const CertificateType other) const { return value == other.value; }
46-
operator std::string() const;
43+
operator std::string_view() const noexcept;
4744

4845
private:
4946
CertificateTypeEnum value = NONE;
@@ -66,27 +63,27 @@ class HashAlgorithm
6663
NONE = -1
6764
};
6865

69-
HashAlgorithm() = default;
70-
constexpr HashAlgorithm(const HashAlgorithmEnum _value) : value(_value) {}
66+
constexpr HashAlgorithm() = default;
67+
constexpr HashAlgorithm(const HashAlgorithmEnum _value) noexcept : value(_value) { }
7168
// String conversion constructor.
72-
HashAlgorithm(const std::string&);
69+
explicit HashAlgorithm(const std::string&);
7370

7471
constexpr bool operator==(HashAlgorithmEnum other) const { return value == other; }
75-
constexpr operator HashAlgorithmEnum() const { return value; }
72+
constexpr operator HashAlgorithmEnum() const noexcept { return value; }
7673

77-
operator std::string() const;
74+
operator std::string_view() const noexcept;
7875

79-
constexpr size_t hashByteLength() const
76+
constexpr size_t hashByteLength() const noexcept
8077
{
81-
return size_t(value <= SHA512 ? value / 8 : (value / 10) / 8);
78+
return size_t((value <= SHA512 ? value : (value / 10)) / 8);
8279
}
8380

84-
constexpr bool isSHA2() const
81+
constexpr bool isSHA2() const noexcept
8582
{
8683
return value >= HashAlgorithm::SHA224 && value <= HashAlgorithm::SHA512;
8784
}
8885

89-
constexpr bool isSHA3() const
86+
constexpr bool isSHA3() const noexcept
9087
{
9188
return value >= HashAlgorithm::SHA3_224 && value <= HashAlgorithm::SHA3_512;
9289
}
@@ -136,26 +133,25 @@ class SignatureAlgorithm
136133
NONE = -1
137134
};
138135

139-
constexpr SignatureAlgorithm(const SignatureAlgorithmEnum _value) : value(_value) {}
140-
constexpr SignatureAlgorithm(const SignatureAlgorithmEnum key, const HashAlgorithm hash) :
136+
constexpr SignatureAlgorithm(const SignatureAlgorithmEnum _value) noexcept : value(_value) { }
137+
constexpr SignatureAlgorithm(const SignatureAlgorithmEnum key,
138+
const HashAlgorithm hash) noexcept :
141139
value(SignatureAlgorithmEnum(key | int16_t(hash)))
142140
{
143141
}
144142

145-
constexpr bool operator==(HashAlgorithm other) const
143+
constexpr bool operator==(HashAlgorithm other) const noexcept
146144
{
147145
return other.operator==(operator HashAlgorithm());
148146
}
149-
constexpr bool operator==(SignatureAlgorithmEnum other) const { return value == other; }
150-
151-
constexpr operator HashAlgorithm() const
147+
constexpr operator HashAlgorithm() const noexcept
152148
{
153149
return HashAlgorithm::HashAlgorithmEnum(value & ~(ES | PS | RS));
154150
}
155151

156-
constexpr operator SignatureAlgorithmEnum() const { return value; }
152+
constexpr operator SignatureAlgorithmEnum() const noexcept { return value; }
157153

158-
operator std::string() const;
154+
operator std::string_view() const noexcept;
159155

160156
private:
161157
SignatureAlgorithmEnum value = NONE;
@@ -178,14 +174,14 @@ class JsonWebSignatureAlgorithm
178174
NONE = -1
179175
};
180176

181-
constexpr JsonWebSignatureAlgorithm(const JsonWebSignatureAlgorithmEnum _value) : value(_value)
177+
constexpr JsonWebSignatureAlgorithm(const JsonWebSignatureAlgorithmEnum _value) noexcept :
178+
value(_value)
182179
{
183180
}
184181

185-
constexpr bool operator==(JsonWebSignatureAlgorithmEnum other) const { return value == other; }
186-
constexpr operator JsonWebSignatureAlgorithmEnum() const { return value; }
182+
constexpr operator JsonWebSignatureAlgorithmEnum() const noexcept { return value; }
187183

188-
operator std::string() const;
184+
operator std::string_view() const noexcept;
189185

190186
constexpr HashAlgorithm hashAlgorithm() const
191187
{
@@ -208,12 +204,12 @@ class JsonWebSignatureAlgorithm
208204
}
209205
}
210206

211-
constexpr bool isRSAWithPKCS1Padding()
207+
constexpr bool isRSAWithPKCS1Padding() const noexcept
212208
{
213209
return value == RS256 || value == RS384 || value == RS512;
214210
}
215211

216-
constexpr size_t hashByteLength() const { return hashAlgorithm().hashByteLength(); }
212+
constexpr size_t hashByteLength() const noexcept { return hashAlgorithm().hashByteLength(); }
217213

218214
private:
219215
JsonWebSignatureAlgorithmEnum value = NONE;

lib/libpcsc-cpp/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ add_library(${PROJECT_NAME}
99
include/${PROJECT_NAME}/${PROJECT_NAME}.hpp
1010
include/${PROJECT_NAME}/${PROJECT_NAME}-utils.hpp
1111
include/${PROJECT_NAME}/comp_winscard.hpp
12-
include/magic_enum/magic_enum.hpp
1312
src/Context.hpp
1413
src/SCardCall.hpp
1514
src/SmartCard.cpp

lib/libpcsc-cpp/include/pcsc-cpp/pcsc-cpp-utils.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,5 @@ constexpr const char* removeAbsolutePathPrefix(std::string_view filePath)
6969

7070
#define REQUIRE_NON_NULL(val) \
7171
if (!(val)) { \
72-
throw std::logic_error("Null " + std::string(#val) + " in " \
73-
+ pcsc_cpp::removeAbsolutePathPrefix(__FILE__) + ':' \
74-
+ std::to_string(__LINE__) + ':' + __func__); \
72+
THROW(std::logic_error, "Null " #val); \
7573
}

src/electronic-id.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,11 @@ const std::vector<MaskedATREntry> MASKED_ATRS = {
211211
constructor<ElectronicID::Type::LuxEID>},
212212
};
213213

214-
const auto SUPPORTED_ALGORITHMS = std::map<std::string, HashAlgorithm> {
215-
{"SHA-224"s, HashAlgorithm::SHA224}, {"SHA-256"s, HashAlgorithm::SHA256},
216-
{"SHA-384"s, HashAlgorithm::SHA384}, {"SHA-512"s, HashAlgorithm::SHA512},
217-
{"SHA3-224"s, HashAlgorithm::SHA3_224}, {"SHA3-256"s, HashAlgorithm::SHA3_256},
218-
{"SHA3-384"s, HashAlgorithm::SHA3_384}, {"SHA3-512"s, HashAlgorithm::SHA3_512},
214+
const auto SUPPORTED_ALGORITHMS = std::map<std::string_view, HashAlgorithm> {
215+
{"SHA-224", HashAlgorithm::SHA224}, {"SHA-256", HashAlgorithm::SHA256},
216+
{"SHA-384", HashAlgorithm::SHA384}, {"SHA-512", HashAlgorithm::SHA512},
217+
{"SHA3-224", HashAlgorithm::SHA3_224}, {"SHA3-256", HashAlgorithm::SHA3_256},
218+
{"SHA3-384", HashAlgorithm::SHA3_384}, {"SHA3-512", HashAlgorithm::SHA3_512},
219219
};
220220

221221
} // namespace
@@ -288,68 +288,71 @@ HashAlgorithm::HashAlgorithm(const std::string& algoName)
288288
value = SUPPORTED_ALGORITHMS.at(algoName);
289289
}
290290

291-
HashAlgorithm::operator std::string() const
291+
HashAlgorithm::operator std::string_view() const noexcept
292292
{
293293
const auto algoNameValuePair =
294294
std::find_if(SUPPORTED_ALGORITHMS.cbegin(), SUPPORTED_ALGORITHMS.cend(),
295295
[this](const auto& pair) { return pair.second == value; });
296-
return algoNameValuePair != SUPPORTED_ALGORITHMS.cend() ? algoNameValuePair->first : "UNKNOWN";
296+
if (algoNameValuePair != SUPPORTED_ALGORITHMS.cend())
297+
return algoNameValuePair->first;
298+
return "UNKNOWN";
297299
}
298300

299301
std::string HashAlgorithm::allSupportedAlgorithmNames()
300302
{
301303
static const auto SUPPORTED_ALGORITHM_NAMES = std::accumulate(
302304
std::next(SUPPORTED_ALGORITHMS.begin()), SUPPORTED_ALGORITHMS.end(),
303305
std::string(SUPPORTED_ALGORITHMS.begin()->first),
304-
[](auto result, const auto& value) { return result + ", "s + std::string(value.first); });
306+
[](auto result, const auto& value) { return (result + ", ").append(value.first); });
305307
return SUPPORTED_ALGORITHM_NAMES;
306308
}
307309

308310
pcsc_cpp::byte_vector HashAlgorithm::rsaOID(const HashAlgorithmEnum hash)
309311
{
310312
switch (hash) {
311-
case HashAlgorithm::SHA224:
313+
using enum HashAlgorithm::HashAlgorithmEnum;
314+
case SHA224:
312315
return {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
313316
0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c};
314-
case HashAlgorithm::SHA256:
317+
case SHA256:
315318
return {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
316319
0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
317-
case HashAlgorithm::SHA384:
320+
case SHA384:
318321
return {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
319322
0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30};
320-
case HashAlgorithm::SHA512:
323+
case SHA512:
321324
return {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
322325
0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
323-
case HashAlgorithm::SHA3_224:
326+
case SHA3_224:
324327
return {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
325328
0x65, 0x03, 0x04, 0x02, 0x07, 0x05, 0x00, 0x04, 0x1c};
326-
case HashAlgorithm::SHA3_256:
329+
case SHA3_256:
327330
return {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
328331
0x65, 0x03, 0x04, 0x02, 0x08, 0x05, 0x00, 0x04, 0x20};
329-
case HashAlgorithm::SHA3_384:
332+
case SHA3_384:
330333
return {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
331334
0x65, 0x03, 0x04, 0x02, 0x09, 0x05, 0x00, 0x04, 0x30};
332-
case HashAlgorithm::SHA3_512:
335+
case SHA3_512:
333336
return {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
334337
0x65, 0x03, 0x04, 0x02, 0x0A, 0x05, 0x00, 0x04, 0x40};
335338
default:
336339
THROW(ArgumentFatalError, "No OID for algorithm " + std::string(HashAlgorithm(hash)));
337340
}
338341
}
339342

340-
CertificateType::operator std::string() const
343+
CertificateType::operator std::string() const noexcept
341344
{
342-
return std::string(magic_enum::enum_name(value));
345+
return magic_enum::enum_name(value);
343346
}
344347

345-
JsonWebSignatureAlgorithm::operator std::string() const
348+
JsonWebSignatureAlgorithm::operator std::string_view() const noexcept
346349
{
347-
return std::string(magic_enum::enum_name(value));
350+
return magic_enum::enum_name(value);
348351
}
349352

350-
SignatureAlgorithm::operator std::string() const
353+
SignatureAlgorithm::operator std::string_view() const noexcept
351354
{
352-
return std::string(magic_enum::enum_name(value));
355+
return magic_enum::enum_name(value);
353356
}
354357

355358
} // namespace electronic_id

src/electronic-ids/ms-cryptoapi/MsCryptoApiElectronicID.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ ElectronicID::Signature
5959
MsCryptoApiElectronicID::signWithSigningKey(byte_vector&& /* pin */, const byte_vector& hash,
6060
const HashAlgorithm hashAlgo) const
6161
{
62-
if (certType != CertificateType::SIGNING) {
62+
if (!certType.isSigning()) {
6363
THROW(WrongCertificateTypeError,
6464
"This electronic ID does not support signing with the digital signature key. "
6565
"It contains a "
File renamed without changes.

0 commit comments

Comments
 (0)