Skip to content

Commit 95904c9

Browse files
committed
Add EVPKeyPointer clone method
1 parent 1d4b139 commit 95904c9

File tree

2 files changed

+83
-81
lines changed

2 files changed

+83
-81
lines changed

include/ncrypto.h

Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -331,76 +331,76 @@ class Dsa final {
331331
};
332332

333333
class BignumPointer final {
334-
public:
335-
BignumPointer() = default;
336-
explicit BignumPointer(BIGNUM* bignum);
337-
explicit BignumPointer(const unsigned char* data, size_t len);
338-
BignumPointer(BignumPointer&& other) noexcept;
339-
BignumPointer& operator=(BignumPointer&& other) noexcept;
340-
NCRYPTO_DISALLOW_COPY(BignumPointer)
341-
~BignumPointer();
342-
343-
int operator<=>(const BignumPointer& other) const noexcept;
344-
int operator<=>(const BIGNUM* other) const noexcept;
345-
inline operator bool() const { return bn_ != nullptr; }
346-
inline BIGNUM* get() const noexcept { return bn_.get(); }
347-
void reset(BIGNUM* bn = nullptr);
348-
void reset(const unsigned char* data, size_t len);
349-
BIGNUM* release();
350-
351-
bool isZero() const;
352-
bool isOne() const;
353-
354-
bool setWord(unsigned long w); // NOLINT(runtime/int)
355-
unsigned long getWord() const; // NOLINT(runtime/int)
356-
357-
size_t byteLength() const;
358-
size_t bitLength() const;
359-
360-
DataPointer toHex() const;
361-
DataPointer encode() const;
362-
DataPointer encodePadded(size_t size) const;
363-
size_t encodeInto(unsigned char* out) const;
364-
size_t encodePaddedInto(unsigned char* out, size_t size) const;
365-
366-
using PrimeCheckCallback = std::function<bool(int, int)>;
367-
int isPrime(int checks,
368-
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
369-
struct PrimeConfig {
370-
int bits;
371-
bool safe = false;
372-
const BignumPointer& add;
373-
const BignumPointer& rem;
374-
};
375-
376-
static BignumPointer NewPrime(
377-
const PrimeConfig& params,
378-
PrimeCheckCallback cb = defaultPrimeCheckCallback);
379-
380-
bool generate(const PrimeConfig& params,
381-
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
382-
383-
static BignumPointer New();
384-
static BignumPointer NewSecure();
385-
static BignumPointer NewSub(const BignumPointer& a, const BignumPointer& b);
386-
static BignumPointer NewLShift(size_t length);
387-
388-
static DataPointer Encode(const BIGNUM* bn);
389-
static DataPointer EncodePadded(const BIGNUM* bn, size_t size);
390-
static size_t EncodePaddedInto(const BIGNUM* bn, unsigned char* out,
391-
size_t size);
392-
static int GetBitCount(const BIGNUM* bn);
393-
static int GetByteCount(const BIGNUM* bn);
394-
static unsigned long GetWord(const BIGNUM* bn); // NOLINT(runtime/int)
395-
static const BIGNUM* One();
396-
397-
BignumPointer clone();
398-
399-
private:
400-
DeleteFnPtr<BIGNUM, BN_clear_free> bn_;
401-
402-
static bool defaultPrimeCheckCallback(int, int) { return 1; }
403-
};
334+
public:
335+
BignumPointer() = default;
336+
explicit BignumPointer(BIGNUM* bignum);
337+
explicit BignumPointer(const unsigned char* data, size_t len);
338+
BignumPointer(BignumPointer&& other) noexcept;
339+
BignumPointer& operator=(BignumPointer&& other) noexcept;
340+
NCRYPTO_DISALLOW_COPY(BignumPointer)
341+
~BignumPointer();
342+
343+
int operator<=>(const BignumPointer& other) const noexcept;
344+
int operator<=>(const BIGNUM* other) const noexcept;
345+
inline operator bool() const { return bn_ != nullptr; }
346+
inline BIGNUM* get() const noexcept { return bn_.get(); }
347+
void reset(BIGNUM* bn = nullptr);
348+
void reset(const unsigned char* data, size_t len);
349+
BIGNUM* release();
350+
351+
bool isZero() const;
352+
bool isOne() const;
353+
354+
bool setWord(unsigned long w); // NOLINT(runtime/int)
355+
unsigned long getWord() const; // NOLINT(runtime/int)
356+
357+
size_t byteLength() const;
358+
size_t bitLength() const;
359+
360+
DataPointer toHex() const;
361+
DataPointer encode() const;
362+
DataPointer encodePadded(size_t size) const;
363+
size_t encodeInto(unsigned char* out) const;
364+
size_t encodePaddedInto(unsigned char* out, size_t size) const;
365+
366+
using PrimeCheckCallback = std::function<bool(int, int)>;
367+
int isPrime(int checks,
368+
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
369+
struct PrimeConfig {
370+
int bits;
371+
bool safe = false;
372+
const BignumPointer& add;
373+
const BignumPointer& rem;
374+
};
375+
376+
static BignumPointer NewPrime(
377+
const PrimeConfig& params,
378+
PrimeCheckCallback cb = defaultPrimeCheckCallback);
379+
380+
bool generate(const PrimeConfig& params,
381+
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
382+
383+
static BignumPointer New();
384+
static BignumPointer NewSecure();
385+
static BignumPointer NewSub(const BignumPointer& a, const BignumPointer& b);
386+
static BignumPointer NewLShift(size_t length);
387+
388+
static DataPointer Encode(const BIGNUM* bn);
389+
static DataPointer EncodePadded(const BIGNUM* bn, size_t size);
390+
static size_t EncodePaddedInto(const BIGNUM* bn, unsigned char* out,
391+
size_t size);
392+
static int GetBitCount(const BIGNUM* bn);
393+
static int GetByteCount(const BIGNUM* bn);
394+
static unsigned long GetWord(const BIGNUM* bn); // NOLINT(runtime/int)
395+
static const BIGNUM* One();
396+
397+
BignumPointer clone();
398+
399+
private:
400+
DeleteFnPtr<BIGNUM, BN_clear_free> bn_;
401+
402+
static bool defaultPrimeCheckCallback(int, int) { return 1; }
403+
};
404404

405405
class Rsa final {
406406
public:
@@ -809,6 +809,8 @@ class EVPKeyPointer final {
809809
bool isSigVariant() const;
810810
bool validateDsaParameters() const;
811811

812+
EVPKeyPointer clone() const;
813+
812814
private:
813815
DeleteFnPtr<EVP_PKEY, EVP_PKEY_free> pkey_;
814816
};

src/ncrypto.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,12 @@ EVPKeyPointer::EVPKeyPointer(EVP_PKEY* pkey) : pkey_(pkey) {}
18491849
EVPKeyPointer::EVPKeyPointer(EVPKeyPointer&& other) noexcept
18501850
: pkey_(other.release()) {}
18511851

1852+
EVPKeyPointer EVPKeyPointer::clone() const {
1853+
if (!pkey_) return {};
1854+
if (!EVP_PKEY_up_ref(pkey_.get())) return {};
1855+
return EVPKeyPointer(pkey_.get());
1856+
}
1857+
18521858
EVPKeyPointer& EVPKeyPointer::operator=(EVPKeyPointer&& other) noexcept {
18531859
if (this == &other) return *this;
18541860
this->~EVPKeyPointer();
@@ -3547,13 +3553,11 @@ DataPointer Cipher::recover(const EVPKeyPointer& key,
35473553
Ec::Ec() : ec_(nullptr) {}
35483554

35493555
Ec::Ec(OSSL3_CONST EC_KEY* key)
3550-
: ec_(key),
3551-
x_(BignumPointer::New()),
3552-
y_(BignumPointer::New()) {
3556+
: ec_(key), x_(BignumPointer::New()), y_(BignumPointer::New()) {
35533557
if (ec_ != nullptr) {
35543558
MarkPopErrorOnReturn mark_pop_error_on_return;
3555-
EC_POINT_get_affine_coordinates(getGroup(),
3556-
getPublicKey(), x_.get(), y_.get(), nullptr);
3559+
EC_POINT_get_affine_coordinates(getGroup(), getPublicKey(), x_.get(),
3560+
y_.get(), nullptr);
35573561
}
35583562
}
35593563

@@ -3567,13 +3571,9 @@ std::string Ec::getCurveName() const {
35673571
return std::string(OBJ_nid2sn(getCurve()));
35683572
}
35693573

3570-
const EC_POINT* Ec::getPublicKey() const {
3571-
return EC_KEY_get0_public_key(ec_);
3572-
}
3574+
const EC_POINT* Ec::getPublicKey() const { return EC_KEY_get0_public_key(ec_); }
35733575

3574-
const BIGNUM* Ec::getPrivateKey() const {
3575-
return EC_KEY_get0_private_key(ec_);
3576-
}
3576+
const BIGNUM* Ec::getPrivateKey() const { return EC_KEY_get0_private_key(ec_); }
35773577

35783578
// ============================================================================
35793579

0 commit comments

Comments
 (0)