@@ -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
336405class 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-
576585class 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};
0 commit comments