Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
src: cleanup more of crypto/ncrypto
  • Loading branch information
jasnell committed Jan 27, 2025
commit a69e2512c3455a11a28ec758c22693b48fbbef77
43 changes: 41 additions & 2 deletions deps/ncrypto/ncrypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,15 @@ EVPKeyPointer::operator Rsa() const {
return Rsa(rsa);
}

EVPKeyPointer::operator Dsa() const {
int type = id();
if (type != EVP_PKEY_DSA) return {};

OSSL3_CONST DSA* dsa = EVP_PKEY_get0_DSA(get());
if (dsa == nullptr) return {};
return Dsa(dsa);
}

bool EVPKeyPointer::validateDsaParameters() const {
if (!pkey_) return false;
/* Validate DSA2 parameters from FIPS 186-4 */
Expand Down Expand Up @@ -2660,8 +2669,8 @@ bool SSLCtxPointer::setGroups(const char* groups) {

// ============================================================================

const Cipher Cipher::FromName(const char* name) {
return Cipher(EVP_get_cipherbyname(name));
const Cipher Cipher::FromName(std::string_view name) {
return Cipher(EVP_get_cipherbyname(name.data()));
}

const Cipher Cipher::FromNid(int nid) {
Expand Down Expand Up @@ -3902,4 +3911,34 @@ std::pair<std::string, std::string> X509Name::Iterator::operator*() const {
std::string(reinterpret_cast<const char*>(value_str), value_str_size)};
}

// ============================================================================

Dsa::Dsa() : dsa_(nullptr) {}

Dsa::Dsa(OSSL3_CONST DSA* dsa) : dsa_(dsa) {}

const BIGNUM* Dsa::getP() const {
if (dsa_ == nullptr) return nullptr;
const BIGNUM* p;
DSA_get0_pqg(dsa_, &p, nullptr, nullptr);
return p;
}

const BIGNUM* Dsa::getQ() const {
if (dsa_ == nullptr) return nullptr;
const BIGNUM* q;
DSA_get0_pqg(dsa_, nullptr, &q, nullptr);
return q;
}

size_t Dsa::getModulusLength() const {
if (dsa_ == nullptr) return 0;
return BignumPointer::GetBitCount(getP());
}

size_t Dsa::getDivisorLength() const {
if (dsa_ == nullptr) return 0;
return BignumPointer::GetBitCount(getQ());
}

} // namespace ncrypto
29 changes: 28 additions & 1 deletion deps/ncrypto/ncrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class ECDSASigPointer;
class ECGroupPointer;
class ECPointPointer;
class ECKeyPointer;
class Dsa;
class Rsa;
class Ec;

Expand Down Expand Up @@ -267,7 +268,7 @@ class Cipher final {

bool isSupportedAuthenticatedMode() const;

static const Cipher FromName(const char* name);
static const Cipher FromName(std::string_view name);
static const Cipher FromNid(int nid);
static const Cipher FromCtx(const CipherCtxPointer& ctx);

Expand All @@ -292,10 +293,35 @@ class Cipher final {
const CipherParams& params,
const Buffer<const void> in);

static constexpr bool IsValidGCMTagLength(unsigned int tag_len) {
return tag_len == 4 || tag_len == 8 || (tag_len >= 12 && tag_len <= 16);
}

private:
const EVP_CIPHER* cipher_ = nullptr;
};

// ============================================================================
// DSA

class Dsa final {
public:
Dsa();
Comment thread
jasnell marked this conversation as resolved.
Resolved
Dsa(OSSL3_CONST DSA* dsa);
NCRYPTO_DISALLOW_COPY_AND_MOVE(Dsa)

inline operator bool() const { return dsa_ != nullptr; }
inline operator OSSL3_CONST DSA*() const { return dsa_; }

const BIGNUM* getP() const;
const BIGNUM* getQ() const;
size_t getModulusLength() const;
size_t getDivisorLength() const;

private:
OSSL3_CONST DSA* dsa_;
};

// ============================================================================
// RSA

Expand Down Expand Up @@ -767,6 +793,7 @@ class EVPKeyPointer final {
std::optional<uint32_t> getBytesOfRS() const;
int getDefaultSignPadding() const;
operator Rsa() const;
operator Dsa() const;

bool isRsaVariant() const;
bool isOneShotVariant() const;
Expand Down
Loading