Skip to content

Commit

Permalink
src: cleanup more of crypto/ncrypto
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Jan 27, 2025
1 parent 5843486 commit 1872cc6
Show file tree
Hide file tree
Showing 15 changed files with 368 additions and 351 deletions.
47 changes: 45 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 @@ -2748,6 +2757,10 @@ bool Cipher::isSupportedAuthenticatedMode() const {
}
}

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

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

CipherCtxPointer CipherCtxPointer::New() {
Expand Down Expand Up @@ -3902,4 +3915,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
27 changes: 26 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,33 @@ class Cipher final {
const CipherParams& params,
const Buffer<const void> in);

static bool IsValidGCMTagLength(unsigned int tag_len);

private:
const EVP_CIPHER* cipher_ = nullptr;
};

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

class Dsa final {
public:
Dsa();
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 +791,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

0 comments on commit 1872cc6

Please sign in to comment.