Skip to content

Commit 9062c36

Browse files
authored
Cleanups and additional methods from workerd use of ncrypto (#4)
1 parent 315ec75 commit 9062c36

File tree

2 files changed

+109
-75
lines changed

2 files changed

+109
-75
lines changed

include/ncrypto.h

Lines changed: 85 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,77 @@ class Dsa final {
330330
OSSL3_CONST DSA* dsa_;
331331
};
332332

333-
// ============================================================================
334-
// RSA
333+
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+
};
335404

336405
class Rsa final {
337406
public:
@@ -390,12 +459,24 @@ class Ec final {
390459

391460
const EC_GROUP* getGroup() const;
392461
int getCurve() const;
462+
uint32_t getDegree() const;
463+
std::string getCurveName() const;
464+
const EC_POINT* getPublicKey() const;
465+
const BIGNUM* getPrivateKey() const;
393466

394467
inline operator bool() const { return ec_ != nullptr; }
395468
inline operator OSSL3_CONST EC_KEY*() const { return ec_; }
396469

470+
inline const BignumPointer& getX() const { return x_; }
471+
inline const BignumPointer& getY() const { return y_; }
472+
inline const BignumPointer& getD() const { return d_; }
473+
397474
private:
398475
OSSL3_CONST EC_KEY* ec_ = nullptr;
476+
// Affine coordinates for the EC_KEY.
477+
BignumPointer x_;
478+
BignumPointer y_;
479+
BignumPointer d_;
399480
};
400481

401482
// A managed pointer to a buffer of data. When destroyed the underlying
@@ -501,78 +582,6 @@ class BIOPointer final {
501582
mutable DeleteFnPtr<BIO, BIO_free_all> bio_;
502583
};
503584

504-
class BignumPointer final {
505-
public:
506-
BignumPointer() = default;
507-
explicit BignumPointer(BIGNUM* bignum);
508-
explicit BignumPointer(const unsigned char* data, size_t len);
509-
BignumPointer(BignumPointer&& other) noexcept;
510-
BignumPointer& operator=(BignumPointer&& other) noexcept;
511-
NCRYPTO_DISALLOW_COPY(BignumPointer)
512-
~BignumPointer();
513-
514-
int operator<=>(const BignumPointer& other) const noexcept;
515-
int operator<=>(const BIGNUM* other) const noexcept;
516-
inline operator bool() const { return bn_ != nullptr; }
517-
inline BIGNUM* get() const noexcept { return bn_.get(); }
518-
void reset(BIGNUM* bn = nullptr);
519-
void reset(const unsigned char* data, size_t len);
520-
BIGNUM* release();
521-
522-
bool isZero() const;
523-
bool isOne() const;
524-
525-
bool setWord(unsigned long w); // NOLINT(runtime/int)
526-
unsigned long getWord() const; // NOLINT(runtime/int)
527-
528-
size_t byteLength() const;
529-
size_t bitLength() const;
530-
531-
DataPointer toHex() const;
532-
DataPointer encode() const;
533-
DataPointer encodePadded(size_t size) const;
534-
size_t encodeInto(unsigned char* out) const;
535-
size_t encodePaddedInto(unsigned char* out, size_t size) const;
536-
537-
using PrimeCheckCallback = std::function<bool(int, int)>;
538-
int isPrime(int checks,
539-
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
540-
struct PrimeConfig {
541-
int bits;
542-
bool safe = false;
543-
const BignumPointer& add;
544-
const BignumPointer& rem;
545-
};
546-
547-
static BignumPointer NewPrime(
548-
const PrimeConfig& params,
549-
PrimeCheckCallback cb = defaultPrimeCheckCallback);
550-
551-
bool generate(const PrimeConfig& params,
552-
PrimeCheckCallback cb = defaultPrimeCheckCallback) const;
553-
554-
static BignumPointer New();
555-
static BignumPointer NewSecure();
556-
static BignumPointer NewSub(const BignumPointer& a, const BignumPointer& b);
557-
static BignumPointer NewLShift(size_t length);
558-
559-
static DataPointer Encode(const BIGNUM* bn);
560-
static DataPointer EncodePadded(const BIGNUM* bn, size_t size);
561-
static size_t EncodePaddedInto(const BIGNUM* bn, unsigned char* out,
562-
size_t size);
563-
static int GetBitCount(const BIGNUM* bn);
564-
static int GetByteCount(const BIGNUM* bn);
565-
static unsigned long GetWord(const BIGNUM* bn); // NOLINT(runtime/int)
566-
static const BIGNUM* One();
567-
568-
BignumPointer clone();
569-
570-
private:
571-
DeleteFnPtr<BIGNUM, BN_clear_free> bn_;
572-
573-
static bool defaultPrimeCheckCallback(int, int) { return 1; }
574-
};
575-
576585
class CipherCtxPointer final {
577586
public:
578587
static CipherCtxPointer New();
@@ -800,6 +809,8 @@ class EVPKeyPointer final {
800809
bool isSigVariant() const;
801810
bool validateDsaParameters() const;
802811

812+
EVPKeyPointer clone() const;
813+
803814
private:
804815
DeleteFnPtr<EVP_PKEY, EVP_PKEY_free> pkey_;
805816
};

src/ncrypto.cpp

Lines changed: 24 additions & 1 deletion
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();
@@ -3546,12 +3552,29 @@ DataPointer Cipher::recover(const EVPKeyPointer& key,
35463552

35473553
Ec::Ec() : ec_(nullptr) {}
35483554

3549-
Ec::Ec(OSSL3_CONST EC_KEY* key) : ec_(key) {}
3555+
Ec::Ec(OSSL3_CONST EC_KEY* key)
3556+
: ec_(key), x_(BignumPointer::New()), y_(BignumPointer::New()) {
3557+
if (ec_ != nullptr) {
3558+
MarkPopErrorOnReturn mark_pop_error_on_return;
3559+
EC_POINT_get_affine_coordinates(getGroup(), getPublicKey(), x_.get(),
3560+
y_.get(), nullptr);
3561+
}
3562+
}
35503563

35513564
const EC_GROUP* Ec::getGroup() const { return ECKeyPointer::GetGroup(ec_); }
35523565

35533566
int Ec::getCurve() const { return EC_GROUP_get_curve_name(getGroup()); }
35543567

3568+
uint32_t Ec::getDegree() const { return EC_GROUP_get_degree(getGroup()); }
3569+
3570+
std::string Ec::getCurveName() const {
3571+
return std::string(OBJ_nid2sn(getCurve()));
3572+
}
3573+
3574+
const EC_POINT* Ec::getPublicKey() const { return EC_KEY_get0_public_key(ec_); }
3575+
3576+
const BIGNUM* Ec::getPrivateKey() const { return EC_KEY_get0_private_key(ec_); }
3577+
35553578
// ============================================================================
35563579

35573580
EVPMDCtxPointer::EVPMDCtxPointer() : ctx_(nullptr) {}

0 commit comments

Comments
 (0)