Skip to content

Commit 261c0d0

Browse files
committed
Take PIN ownership to minimze memory copy-s
WE2-1007 Signed-off-by: Raul Metsma <[email protected]>
1 parent 7991e0e commit 261c0d0

File tree

14 files changed

+78
-81
lines changed

14 files changed

+78
-81
lines changed

include/electronic-id/electronic-id.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ElectronicID
6565

6666
virtual PinRetriesRemainingAndMax authPinRetriesLeft() const = 0;
6767

68-
virtual pcsc_cpp::byte_vector signWithAuthKey(const byte_vector& pin,
68+
virtual pcsc_cpp::byte_vector signWithAuthKey(byte_vector&& pin,
6969
const byte_vector& hash) const = 0;
7070

7171
// Functions related to signing.
@@ -77,7 +77,7 @@ class ElectronicID
7777

7878
virtual PinRetriesRemainingAndMax signingPinRetriesLeft() const = 0;
7979

80-
virtual Signature signWithSigningKey(const byte_vector& pin, const byte_vector& hash,
80+
virtual Signature signWithSigningKey(byte_vector&& pin, const byte_vector& hash,
8181
const HashAlgorithm hashAlgo) const = 0;
8282

8383
// General functions.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ JsonWebSignatureAlgorithm MsCryptoApiElectronicID::authSignatureAlgorithm() cons
3434
return getAuthAlgorithmFromCert(certData);
3535
}
3636

37-
byte_vector MsCryptoApiElectronicID::signWithAuthKey(const byte_vector& /* pin */,
37+
byte_vector MsCryptoApiElectronicID::signWithAuthKey(byte_vector&& /* pin */,
3838
const byte_vector& hash) const
3939
{
4040
if (certType != CertificateType::AUTHENTICATION) {
@@ -56,7 +56,7 @@ const std::set<SignatureAlgorithm>& MsCryptoApiElectronicID::supportedSigningAlg
5656
}
5757

5858
ElectronicID::Signature
59-
MsCryptoApiElectronicID::signWithSigningKey(const byte_vector& /* pin */, const byte_vector& hash,
59+
MsCryptoApiElectronicID::signWithSigningKey(byte_vector&& /* pin */, const byte_vector& hash,
6060
const HashAlgorithm hashAlgo) const
6161
{
6262
if (certType != CertificateType::SIGNING) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class MsCryptoApiElectronicID : public ElectronicID
8484
return {uint8_t(PIN_RETRY_COUNT_PLACEHOLDER), PIN_RETRY_COUNT_PLACEHOLDER};
8585
}
8686

87-
byte_vector signWithAuthKey(const byte_vector& pin, const byte_vector& hash) const override;
87+
byte_vector signWithAuthKey(byte_vector&& pin, const byte_vector& hash) const override;
8888

8989
const std::set<SignatureAlgorithm>& supportedSigningAlgorithms() const override;
9090

@@ -98,7 +98,7 @@ class MsCryptoApiElectronicID : public ElectronicID
9898
return {uint8_t(PIN_RETRY_COUNT_PLACEHOLDER), PIN_RETRY_COUNT_PLACEHOLDER};
9999
}
100100

101-
Signature signWithSigningKey(const byte_vector& pin, const byte_vector& hash,
101+
Signature signWithSigningKey(byte_vector&& pin, const byte_vector& hash,
102102
const HashAlgorithm hashAlgo) const override;
103103

104104
std::string name() const override

src/electronic-ids/pcsc/EIDIDEMIA.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ byte_vector EIDIDEMIA::getCertificateImpl(const CertificateType type) const
4646
: selectCertificate().SIGN_CERT);
4747
}
4848

49-
byte_vector EIDIDEMIA::signWithAuthKeyImpl(const byte_vector& pin, const byte_vector& hash) const
49+
byte_vector EIDIDEMIA::signWithAuthKeyImpl(byte_vector&& pin, const byte_vector& hash) const
5050
{
5151
// Select authentication application and authentication security environment.
5252
transmitApduWithExpectedResponse(*card, selectApplicationID().MAIN_AID);
5353
transmitApduWithExpectedResponse(*card, selectApplicationID().AUTH_AID);
5454
selectAuthSecurityEnv();
5555

56-
verifyPin(*card, AUTH_PIN_REFERENCE, pin, authPinMinMaxLength().first, pinBlockLength(),
57-
PIN_PADDING_CHAR);
56+
verifyPin(*card, AUTH_PIN_REFERENCE, std::move(pin), authPinMinMaxLength().first,
57+
pinBlockLength(), PIN_PADDING_CHAR);
5858

5959
return internalAuthenticate(*card,
6060
authSignatureAlgorithm().isRSAWithPKCS1Padding()
@@ -69,7 +69,7 @@ ElectronicID::PinRetriesRemainingAndMax EIDIDEMIA::authPinRetriesLeftImpl() cons
6969
return pinRetriesLeft(AUTH_PIN_REFERENCE);
7070
}
7171

72-
ElectronicID::Signature EIDIDEMIA::signWithSigningKeyImpl(const byte_vector& pin,
72+
ElectronicID::Signature EIDIDEMIA::signWithSigningKeyImpl(byte_vector&& pin,
7373
const byte_vector& hash,
7474
const HashAlgorithm hashAlgo) const
7575
{
@@ -88,8 +88,8 @@ ElectronicID::Signature EIDIDEMIA::signWithSigningKeyImpl(const byte_vector& pin
8888
}
8989
}
9090

91-
verifyPin(*card, signingPinReference(), pin, signingPinMinMaxLength().first, pinBlockLength(),
92-
PIN_PADDING_CHAR);
91+
verifyPin(*card, signingPinReference(), std::move(pin), signingPinMinMaxLength().first,
92+
pinBlockLength(), PIN_PADDING_CHAR);
9393

9494
return {useInternalAuthenticateAndRSAWithPKCS1PaddingDuringSigning()
9595
? internalAuthenticate(*card, addRSAOID(hashAlgo, hash), name())

src/electronic-ids/pcsc/EIDIDEMIA.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ class EIDIDEMIA : public PcscElectronicID
4949
byte_vector getCertificateImpl(const CertificateType type) const override;
5050

5151
PinRetriesRemainingAndMax authPinRetriesLeftImpl() const override;
52-
byte_vector signWithAuthKeyImpl(const byte_vector& pin, const byte_vector& hash) const override;
52+
byte_vector signWithAuthKeyImpl(byte_vector&& pin, const byte_vector& hash) const override;
5353

5454
PinRetriesRemainingAndMax signingPinRetriesLeftImpl() const override;
55-
Signature signWithSigningKeyImpl(const byte_vector& pin, const byte_vector& hash,
55+
Signature signWithSigningKeyImpl(byte_vector&& pin, const byte_vector& hash,
5656
const HashAlgorithm hashAlgo) const override;
5757

5858
virtual const SelectApplicationIDCmds& selectApplicationID() const;

src/electronic-ids/pcsc/FinEID.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ byte_vector FinEIDv3::getCertificateImpl(const CertificateType type) const
7575
*card, type.isAuthentication() ? SELECT_AUTH_CERT_FILE : SELECT_SIGN_CERT_FILE_V3);
7676
}
7777

78-
byte_vector FinEIDv3::signWithAuthKeyImpl(const byte_vector& pin, const byte_vector& hash) const
78+
byte_vector FinEIDv3::signWithAuthKeyImpl(byte_vector&& pin, const byte_vector& hash) const
7979
{
80-
return sign(authSignatureAlgorithm().hashAlgorithm(), hash, pin, AUTH_PIN_REFERENCE,
80+
return sign(authSignatureAlgorithm().hashAlgorithm(), hash, std::move(pin), AUTH_PIN_REFERENCE,
8181
authPinMinMaxLength(), AUTH_KEY_REFERENCE, RSA_PSS_ALGO, 0x00);
8282
}
8383

@@ -91,11 +91,10 @@ const std::set<SignatureAlgorithm>& FinEIDv3::supportedSigningAlgorithms() const
9191
return ELLIPTIC_CURVE_SIGNATURE_ALGOS();
9292
}
9393

94-
ElectronicID::Signature FinEIDv3::signWithSigningKeyImpl(const byte_vector& pin,
95-
const byte_vector& hash,
94+
ElectronicID::Signature FinEIDv3::signWithSigningKeyImpl(byte_vector&& pin, const byte_vector& hash,
9695
const HashAlgorithm hashAlgo) const
9796
{
98-
return {sign(hashAlgo, hash, pin, SIGNING_PIN_REFERENCE, signingPinMinMaxLength(),
97+
return {sign(hashAlgo, hash, std::move(pin), SIGNING_PIN_REFERENCE, signingPinMinMaxLength(),
9998
SIGNING_KEY_REFERENCE_V3, ECDSA_ALGO, 0x40),
10099
{SignatureAlgorithm::ES, hashAlgo}};
101100
}
@@ -105,10 +104,9 @@ ElectronicID::PinRetriesRemainingAndMax FinEIDv3::signingPinRetriesLeftImpl() co
105104
return pinRetriesLeft(SIGNING_PIN_REFERENCE);
106105
}
107106

108-
byte_vector FinEIDv3::sign(const HashAlgorithm hashAlgo, const byte_vector& hash,
109-
const byte_vector& pin, byte_type pinReference,
110-
PinMinMaxLength pinMinMaxLength, byte_type keyReference,
111-
byte_type signatureAlgo, byte_type LE) const
107+
byte_vector FinEIDv3::sign(const HashAlgorithm hashAlgo, const byte_vector& hash, byte_vector&& pin,
108+
byte_type pinReference, PinMinMaxLength pinMinMaxLength,
109+
byte_type keyReference, byte_type signatureAlgo, byte_type LE) const
112110
{
113111
if (signatureAlgo != ECDSA_ALGO && hashAlgo.isSHA3()) {
114112
THROW(ArgumentFatalError, "No OID for algorithm " + std::string(hashAlgo));
@@ -137,7 +135,7 @@ byte_vector FinEIDv3::sign(const HashAlgorithm hashAlgo, const byte_vector& hash
137135

138136
transmitApduWithExpectedResponse(*card, SELECT_MASTER_FILE);
139137

140-
verifyPin(*card, pinReference, pin, pinMinMaxLength.first, pinMinMaxLength.second,
138+
verifyPin(*card, pinReference, std::move(pin), pinMinMaxLength.first, pinMinMaxLength.second,
141139
PIN_PADDING_CHAR);
142140
// Select security environment for COMPUTE SIGNATURE.
143141
selectSecurityEnv(*card, 0xB6, signatureAlgo, keyReference, name());
@@ -198,17 +196,16 @@ byte_vector FinEIDv4::getCertificateImpl(const CertificateType type) const
198196
*card, type.isAuthentication() ? SELECT_AUTH_CERT_FILE : SELECT_SIGN_CERT_FILE_V4);
199197
}
200198

201-
byte_vector FinEIDv4::signWithAuthKeyImpl(const byte_vector& pin, const byte_vector& hash) const
199+
byte_vector FinEIDv4::signWithAuthKeyImpl(byte_vector&& pin, const byte_vector& hash) const
202200
{
203-
return sign(authSignatureAlgorithm().hashAlgorithm(), hash, pin, AUTH_PIN_REFERENCE,
201+
return sign(authSignatureAlgorithm().hashAlgorithm(), hash, std::move(pin), AUTH_PIN_REFERENCE,
204202
authPinMinMaxLength(), AUTH_KEY_REFERENCE, ECDSA_ALGO, 0x60);
205203
}
206204

207-
ElectronicID::Signature FinEIDv4::signWithSigningKeyImpl(const byte_vector& pin,
208-
const byte_vector& hash,
205+
ElectronicID::Signature FinEIDv4::signWithSigningKeyImpl(byte_vector&& pin, const byte_vector& hash,
209206
const HashAlgorithm hashAlgo) const
210207
{
211-
return {sign(hashAlgo, hash, pin, SIGNING_PIN_REFERENCE, signingPinMinMaxLength(),
208+
return {sign(hashAlgo, hash, std::move(pin), SIGNING_PIN_REFERENCE, signingPinMinMaxLength(),
212209
SIGNING_KEY_REFERENCE_V4, ECDSA_ALGO, 0x60),
213210
{SignatureAlgorithm::ES, hashAlgo}};
214211
}

src/electronic-ids/pcsc/FinEID.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ class FinEIDv3 : public PcscElectronicID
4949
std::string name() const override { return "FinEID v3"; }
5050
Type type() const override { return FinEID; }
5151

52-
byte_vector signWithAuthKeyImpl(const byte_vector& pin, const byte_vector& hash) const override;
52+
byte_vector signWithAuthKeyImpl(byte_vector&& pin, const byte_vector& hash) const override;
5353

54-
Signature signWithSigningKeyImpl(const byte_vector& pin, const byte_vector& hash,
54+
Signature signWithSigningKeyImpl(byte_vector&& pin, const byte_vector& hash,
5555
const HashAlgorithm hashAlgo) const override;
5656

57-
byte_vector sign(const HashAlgorithm hashAlgo, const byte_vector& hash, const byte_vector& pin,
57+
byte_vector sign(const HashAlgorithm hashAlgo, const byte_vector& hash, byte_vector&& pin,
5858
byte_type pinReference, PinMinMaxLength pinMinMaxLength,
5959
byte_type keyReference, byte_type signatureAlgo, byte_type LE) const;
6060

@@ -76,9 +76,9 @@ class FinEIDv4 : public FinEIDv3
7676

7777
std::string name() const override { return "FinEID v4"; }
7878

79-
byte_vector signWithAuthKeyImpl(const byte_vector& pin, const byte_vector& hash) const override;
79+
byte_vector signWithAuthKeyImpl(byte_vector&& pin, const byte_vector& hash) const override;
8080

81-
Signature signWithSigningKeyImpl(const byte_vector& pin, const byte_vector& hash,
81+
Signature signWithSigningKeyImpl(byte_vector&& pin, const byte_vector& hash,
8282
const HashAlgorithm hashAlgo) const override;
8383
};
8484

src/electronic-ids/pcsc/PcscElectronicID.hpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,27 @@ class PcscElectronicID : public ElectronicID
3535
PcscElectronicID(pcsc_cpp::SmartCard::ptr _card) : ElectronicID(std::move(_card)) {}
3636

3737
protected:
38-
pcsc_cpp::byte_vector getCertificate(const CertificateType type) const override
38+
byte_vector getCertificate(const CertificateType type) const override
3939
{
4040
auto transactionGuard = card->beginTransaction();
4141
return getCertificateImpl(type);
4242
}
4343

44-
pcsc_cpp::byte_vector signWithAuthKey(const pcsc_cpp::byte_vector& pin,
45-
const pcsc_cpp::byte_vector& hash) const override
44+
byte_vector signWithAuthKey(byte_vector&& pin, const byte_vector& hash) const override
4645
{
4746
validateAuthHashLength(authSignatureAlgorithm(), name(), hash);
4847

4948
auto transactionGuard = card->beginTransaction();
50-
return signWithAuthKeyImpl(pin, hash);
49+
return signWithAuthKeyImpl(std::move(pin), hash);
5150
}
5251

53-
Signature signWithSigningKey(const pcsc_cpp::byte_vector& pin,
54-
const pcsc_cpp::byte_vector& hash,
52+
Signature signWithSigningKey(byte_vector&& pin, const byte_vector& hash,
5553
const HashAlgorithm hashAlgo) const override
5654
{
5755
validateSigningHash(*this, hashAlgo, hash);
5856

5957
auto transactionGuard = card->beginTransaction();
60-
return signWithSigningKeyImpl(pin, hash, hashAlgo);
58+
return signWithSigningKeyImpl(std::move(pin), hash, hashAlgo);
6159
}
6260

6361
PinRetriesRemainingAndMax signingPinRetriesLeft() const override
@@ -77,15 +75,13 @@ class PcscElectronicID : public ElectronicID
7775
// they have to be implemented when adding a new electronic ID.
7876
// This design follows the non-virtual interface pattern.
7977

80-
virtual pcsc_cpp::byte_vector getCertificateImpl(const CertificateType type) const = 0;
78+
virtual byte_vector getCertificateImpl(const CertificateType type) const = 0;
8179

82-
virtual pcsc_cpp::byte_vector signWithAuthKeyImpl(const pcsc_cpp::byte_vector& pin,
83-
const pcsc_cpp::byte_vector& hash) const = 0;
80+
virtual byte_vector signWithAuthKeyImpl(byte_vector&& pin, const byte_vector& hash) const = 0;
8481

8582
virtual PinRetriesRemainingAndMax authPinRetriesLeftImpl() const = 0;
8683

87-
virtual Signature signWithSigningKeyImpl(const pcsc_cpp::byte_vector& pin,
88-
const pcsc_cpp::byte_vector& hash,
84+
virtual Signature signWithSigningKeyImpl(byte_vector&& pin, const byte_vector& hash,
8985
const HashAlgorithm hashAlgo) const = 0;
9086

9187
virtual PinRetriesRemainingAndMax signingPinRetriesLeftImpl() const = 0;

src/electronic-ids/pcsc/pcsc-common.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,15 @@ inline pcsc_cpp::byte_vector getCertificate(pcsc_cpp::SmartCard& card,
4343
return readBinary(card, length, MAX_LE_VALUE);
4444
}
4545

46-
inline pcsc_cpp::byte_vector addPaddingToPin(const pcsc_cpp::byte_vector& pin, size_t paddingLength,
46+
inline pcsc_cpp::byte_vector addPaddingToPin(pcsc_cpp::byte_vector&& pin, size_t paddingLength,
4747
pcsc_cpp::byte_type paddingChar)
4848
{
49-
auto paddedPin = pin;
50-
paddedPin.resize(std::max(pin.size(), paddingLength), paddingChar);
51-
return paddedPin;
49+
pin.resize(std::max(pin.size(), paddingLength), paddingChar);
50+
return pin;
5251
}
5352

5453
inline void verifyPin(pcsc_cpp::SmartCard& card, pcsc_cpp::byte_type p2,
55-
const pcsc_cpp::byte_vector& pin, uint8_t pinMinLength, size_t paddingLength,
54+
pcsc_cpp::byte_vector&& pin, uint8_t pinMinLength, size_t paddingLength,
5655
pcsc_cpp::byte_type paddingChar)
5756
{
5857
const pcsc_cpp::CommandApdu VERIFY_PIN {0x00, 0x20, 0x00, p2};
@@ -64,8 +63,8 @@ inline void verifyPin(pcsc_cpp::SmartCard& card, pcsc_cpp::byte_type p2,
6463
response = card.transmitCTL(verifyPin, 0, pinMinLength);
6564

6665
} else {
67-
const pcsc_cpp::CommandApdu verifyPin {VERIFY_PIN,
68-
addPaddingToPin(pin, paddingLength, paddingChar)};
66+
const pcsc_cpp::CommandApdu verifyPin {
67+
VERIFY_PIN, addPaddingToPin(std::move(pin), paddingLength, paddingChar)};
6968

7069
response = card.transmit(verifyPin);
7170
}

src/electronic-ids/pkcs11/Pkcs11ElectronicID.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ ElectronicID::PinRetriesRemainingAndMax Pkcs11ElectronicID::authPinRetriesLeft()
214214
return {authToken.retry, module.retryMax};
215215
}
216216

217-
pcsc_cpp::byte_vector Pkcs11ElectronicID::signWithAuthKey(const byte_vector& pin,
217+
pcsc_cpp::byte_vector Pkcs11ElectronicID::signWithAuthKey(byte_vector&& pin,
218218
const byte_vector& hash) const
219219
{
220220
REQUIRE_NON_NULL(manager)
@@ -254,7 +254,7 @@ ElectronicID::PinRetriesRemainingAndMax Pkcs11ElectronicID::signingPinRetriesLef
254254
return {signingToken.retry, module.retryMax};
255255
}
256256

257-
ElectronicID::Signature Pkcs11ElectronicID::signWithSigningKey(const byte_vector& pin,
257+
ElectronicID::Signature Pkcs11ElectronicID::signWithSigningKey(byte_vector&& pin,
258258
const byte_vector& hash,
259259
const HashAlgorithm hashAlgo) const
260260
{

0 commit comments

Comments
 (0)