Skip to content

Commit a69e251

Browse files
committed
src: cleanup more of crypto/ncrypto
1 parent 5843486 commit a69e251

15 files changed

+366
-351
lines changed

deps/ncrypto/ncrypto.cc

+41-2
Original file line numberDiff line numberDiff line change
@@ -2402,6 +2402,15 @@ EVPKeyPointer::operator Rsa() const {
24022402
return Rsa(rsa);
24032403
}
24042404

2405+
EVPKeyPointer::operator Dsa() const {
2406+
int type = id();
2407+
if (type != EVP_PKEY_DSA) return {};
2408+
2409+
OSSL3_CONST DSA* dsa = EVP_PKEY_get0_DSA(get());
2410+
if (dsa == nullptr) return {};
2411+
return Dsa(dsa);
2412+
}
2413+
24052414
bool EVPKeyPointer::validateDsaParameters() const {
24062415
if (!pkey_) return false;
24072416
/* Validate DSA2 parameters from FIPS 186-4 */
@@ -2660,8 +2669,8 @@ bool SSLCtxPointer::setGroups(const char* groups) {
26602669

26612670
// ============================================================================
26622671

2663-
const Cipher Cipher::FromName(const char* name) {
2664-
return Cipher(EVP_get_cipherbyname(name));
2672+
const Cipher Cipher::FromName(std::string_view name) {
2673+
return Cipher(EVP_get_cipherbyname(name.data()));
26652674
}
26662675

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

3914+
// ============================================================================
3915+
3916+
Dsa::Dsa() : dsa_(nullptr) {}
3917+
3918+
Dsa::Dsa(OSSL3_CONST DSA* dsa) : dsa_(dsa) {}
3919+
3920+
const BIGNUM* Dsa::getP() const {
3921+
if (dsa_ == nullptr) return nullptr;
3922+
const BIGNUM* p;
3923+
DSA_get0_pqg(dsa_, &p, nullptr, nullptr);
3924+
return p;
3925+
}
3926+
3927+
const BIGNUM* Dsa::getQ() const {
3928+
if (dsa_ == nullptr) return nullptr;
3929+
const BIGNUM* q;
3930+
DSA_get0_pqg(dsa_, nullptr, &q, nullptr);
3931+
return q;
3932+
}
3933+
3934+
size_t Dsa::getModulusLength() const {
3935+
if (dsa_ == nullptr) return 0;
3936+
return BignumPointer::GetBitCount(getP());
3937+
}
3938+
3939+
size_t Dsa::getDivisorLength() const {
3940+
if (dsa_ == nullptr) return 0;
3941+
return BignumPointer::GetBitCount(getQ());
3942+
}
3943+
39053944
} // namespace ncrypto

deps/ncrypto/ncrypto.h

+28-1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ class ECDSASigPointer;
221221
class ECGroupPointer;
222222
class ECPointPointer;
223223
class ECKeyPointer;
224+
class Dsa;
224225
class Rsa;
225226
class Ec;
226227

@@ -267,7 +268,7 @@ class Cipher final {
267268

268269
bool isSupportedAuthenticatedMode() const;
269270

270-
static const Cipher FromName(const char* name);
271+
static const Cipher FromName(std::string_view name);
271272
static const Cipher FromNid(int nid);
272273
static const Cipher FromCtx(const CipherCtxPointer& ctx);
273274

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

296+
static constexpr bool IsValidGCMTagLength(unsigned int tag_len) {
297+
return tag_len == 4 || tag_len == 8 || (tag_len >= 12 && tag_len <= 16);
298+
}
299+
295300
private:
296301
const EVP_CIPHER* cipher_ = nullptr;
297302
};
298303

304+
// ============================================================================
305+
// DSA
306+
307+
class Dsa final {
308+
public:
309+
Dsa();
310+
Dsa(OSSL3_CONST DSA* dsa);
311+
NCRYPTO_DISALLOW_COPY_AND_MOVE(Dsa)
312+
313+
inline operator bool() const { return dsa_ != nullptr; }
314+
inline operator OSSL3_CONST DSA*() const { return dsa_; }
315+
316+
const BIGNUM* getP() const;
317+
const BIGNUM* getQ() const;
318+
size_t getModulusLength() const;
319+
size_t getDivisorLength() const;
320+
321+
private:
322+
OSSL3_CONST DSA* dsa_;
323+
};
324+
299325
// ============================================================================
300326
// RSA
301327

@@ -767,6 +793,7 @@ class EVPKeyPointer final {
767793
std::optional<uint32_t> getBytesOfRS() const;
768794
int getDefaultSignPadding() const;
769795
operator Rsa() const;
796+
operator Dsa() const;
770797

771798
bool isRsaVariant() const;
772799
bool isOneShotVariant() const;

0 commit comments

Comments
 (0)