Skip to content

Commit 1872cc6

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

15 files changed

+368
-351
lines changed

deps/ncrypto/ncrypto.cc

+45-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) {
@@ -2748,6 +2757,10 @@ bool Cipher::isSupportedAuthenticatedMode() const {
27482757
}
27492758
}
27502759

2760+
bool Cipher::IsValidGCMTagLength(unsigned int tag_len) {
2761+
return tag_len == 4 || tag_len == 8 || (tag_len >= 12 && tag_len <= 16);
2762+
}
2763+
27512764
// ============================================================================
27522765

27532766
CipherCtxPointer CipherCtxPointer::New() {
@@ -3902,4 +3915,34 @@ std::pair<std::string, std::string> X509Name::Iterator::operator*() const {
39023915
std::string(reinterpret_cast<const char*>(value_str), value_str_size)};
39033916
}
39043917

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

deps/ncrypto/ncrypto.h

+26-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,33 @@ class Cipher final {
292293
const CipherParams& params,
293294
const Buffer<const void> in);
294295

296+
static bool IsValidGCMTagLength(unsigned int tag_len);
297+
295298
private:
296299
const EVP_CIPHER* cipher_ = nullptr;
297300
};
298301

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

@@ -767,6 +791,7 @@ class EVPKeyPointer final {
767791
std::optional<uint32_t> getBytesOfRS() const;
768792
int getDefaultSignPadding() const;
769793
operator Rsa() const;
794+
operator Dsa() const;
770795

771796
bool isRsaVariant() const;
772797
bool isOneShotVariant() const;

0 commit comments

Comments
 (0)