@@ -1669,17 +1669,17 @@ const EVP_CIPHER* getCipherByName(const std::string_view name) {
16691669 return EVP_get_cipherbyname (name.data ());
16701670}
16711671
1672- bool checkHkdfLength (const EVP_MD* md, size_t length) {
1672+ bool checkHkdfLength (const Digest& md, size_t length) {
16731673 // HKDF-Expand computes up to 255 HMAC blocks, each having as many bits as
16741674 // the output of the hash function. 255 is a hard limit because HKDF appends
16751675 // an 8-bit counter to each HMAC'd message, starting at 1.
16761676 static constexpr size_t kMaxDigestMultiplier = 255 ;
1677- size_t max_length = EVP_MD_size (md ) * kMaxDigestMultiplier ;
1677+ size_t max_length = md. size ( ) * kMaxDigestMultiplier ;
16781678 if (length > max_length) return false ;
16791679 return true ;
16801680}
16811681
1682- DataPointer hkdf (const EVP_MD* md,
1682+ DataPointer hkdf (const Digest& md,
16831683 const Buffer<const unsigned char >& key,
16841684 const Buffer<const unsigned char >& info,
16851685 const Buffer<const unsigned char >& salt,
@@ -1703,7 +1703,7 @@ DataPointer hkdf(const EVP_MD* md,
17031703 if (salt.len > 0 ) {
17041704 actual_salt = {reinterpret_cast <const char *>(salt.data ), salt.len };
17051705 } else {
1706- actual_salt = {default_salt, static_cast <unsigned >(EVP_MD_size (md ))};
1706+ actual_salt = {default_salt, static_cast <unsigned >(md. size ( ))};
17071707 }
17081708
17091709 // We do not use EVP_PKEY_HKDF_MODE_EXTRACT_AND_EXPAND because and instead
@@ -2786,6 +2786,11 @@ bool Cipher::isStreamMode() const {
27862786 return getMode () == EVP_CIPH_STREAM_CIPHER;
27872787}
27882788
2789+ bool Cipher::isChaCha20Poly1305 () const {
2790+ if (!cipher_) return false ;
2791+ return getNid () == NID_chacha20_poly1305;
2792+ }
2793+
27892794int Cipher::getMode () const {
27902795 if (!cipher_) return 0 ;
27912796 return EVP_CIPHER_mode (cipher_);
@@ -2862,6 +2867,14 @@ bool Cipher::isSupportedAuthenticatedMode() const {
28622867 }
28632868}
28642869
2870+ int Cipher::bytesToKey (const Digest& digest,
2871+ const Buffer<const unsigned char >& input,
2872+ unsigned char * key,
2873+ unsigned char * iv) const {
2874+ return EVP_BytesToKey (
2875+ *this , Digest::MD5, nullptr , input.data , input.len , 1 , key, iv);
2876+ }
2877+
28652878// ============================================================================
28662879
28672880CipherCtxPointer CipherCtxPointer::New () {
@@ -2938,6 +2951,26 @@ int CipherCtxPointer::getMode() const {
29382951 return EVP_CIPHER_CTX_mode (ctx_.get ());
29392952}
29402953
2954+ bool CipherCtxPointer::isGcmMode () const {
2955+ if (!ctx_) return false ;
2956+ return getMode () == EVP_CIPH_GCM_MODE;
2957+ }
2958+
2959+ bool CipherCtxPointer::isCcmMode () const {
2960+ if (!ctx_) return false ;
2961+ return getMode () == EVP_CIPH_CCM_MODE;
2962+ }
2963+
2964+ bool CipherCtxPointer::isWrapMode () const {
2965+ if (!ctx_) return false ;
2966+ return getMode () == EVP_CIPH_WRAP_MODE;
2967+ }
2968+
2969+ bool CipherCtxPointer::isChaCha20Poly1305 () const {
2970+ if (!ctx_) return false ;
2971+ return getNid () == NID_chacha20_poly1305;
2972+ }
2973+
29412974int CipherCtxPointer::getNid () const {
29422975 if (!ctx_) return 0 ;
29432976 return EVP_CIPHER_CTX_nid (ctx_.get ());
@@ -3720,9 +3753,7 @@ DataPointer Cipher::recover(const EVPKeyPointer& key,
37203753namespace {
37213754struct CipherCallbackContext {
37223755 Cipher::CipherNameCallback cb;
3723- void operator ()(std::string_view name) {
3724- cb (name);
3725- }
3756+ void operator ()(std::string_view name) { cb (name); }
37263757};
37273758
37283759#if OPENSSL_VERSION_MAJOR >= 3
@@ -3759,9 +3790,9 @@ void array_push_back(const TypeName* evp_ref,
37593790#else
37603791template <class TypeName >
37613792void array_push_back (const TypeName* evp_ref,
3762- const char * from,
3763- const char * to,
3764- void * arg) {
3793+ const char * from,
3794+ const char * to,
3795+ void * arg) {
37653796 if (!from) return ;
37663797 auto & cb = *(static_cast <CipherCallbackContext*>(arg));
37673798 cb (from);
@@ -3776,15 +3807,15 @@ void Cipher::ForEach(Cipher::CipherNameCallback callback) {
37763807
37773808 EVP_CIPHER_do_all_sorted (
37783809#if OPENSSL_VERSION_MAJOR >= 3
3779- array_push_back<EVP_CIPHER,
3780- EVP_CIPHER_fetch,
3781- EVP_CIPHER_free,
3782- EVP_get_cipherbyname,
3783- EVP_CIPHER_get0_name>,
3810+ array_push_back<EVP_CIPHER,
3811+ EVP_CIPHER_fetch,
3812+ EVP_CIPHER_free,
3813+ EVP_get_cipherbyname,
3814+ EVP_CIPHER_get0_name>,
37843815#else
3785- array_push_back<EVP_CIPHER>,
3816+ array_push_back<EVP_CIPHER>,
37863817#endif
3787- &context);
3818+ &context);
37883819}
37893820
37903821// ============================================================================
@@ -4143,4 +4174,21 @@ size_t Dsa::getDivisorLength() const {
41434174 return BignumPointer::GetBitCount (getQ ());
41444175}
41454176
4177+ // ============================================================================
4178+
4179+ size_t Digest::size () const {
4180+ if (md_ == nullptr ) return 0 ;
4181+ return EVP_MD_size (md_);
4182+ }
4183+
4184+ const Digest Digest::MD5 = Digest (EVP_md5 ());
4185+ const Digest Digest::SHA1 = Digest (EVP_sha1 ());
4186+ const Digest Digest::SHA256 = Digest (EVP_sha256 ());
4187+ const Digest Digest::SHA384 = Digest (EVP_sha384 ());
4188+ const Digest Digest::SHA512 = Digest (EVP_sha512 ());
4189+
4190+ const Digest Digest::FromName (std::string_view name) {
4191+ return ncrypto::getDigestByName (name);
4192+ }
4193+
41464194} // namespace ncrypto
0 commit comments