@@ -1669,17 +1669,17 @@ const EVP_CIPHER* getCipherByName(const std::string_view name) {
1669
1669
return EVP_get_cipherbyname (name.data ());
1670
1670
}
1671
1671
1672
- bool checkHkdfLength (const EVP_MD* md, size_t length) {
1672
+ bool checkHkdfLength (const Digest& md, size_t length) {
1673
1673
// HKDF-Expand computes up to 255 HMAC blocks, each having as many bits as
1674
1674
// the output of the hash function. 255 is a hard limit because HKDF appends
1675
1675
// an 8-bit counter to each HMAC'd message, starting at 1.
1676
1676
static constexpr size_t kMaxDigestMultiplier = 255 ;
1677
- size_t max_length = EVP_MD_size (md ) * kMaxDigestMultiplier ;
1677
+ size_t max_length = md. size ( ) * kMaxDigestMultiplier ;
1678
1678
if (length > max_length) return false ;
1679
1679
return true ;
1680
1680
}
1681
1681
1682
- DataPointer hkdf (const EVP_MD* md,
1682
+ DataPointer hkdf (const Digest& md,
1683
1683
const Buffer<const unsigned char >& key,
1684
1684
const Buffer<const unsigned char >& info,
1685
1685
const Buffer<const unsigned char >& salt,
@@ -1703,7 +1703,7 @@ DataPointer hkdf(const EVP_MD* md,
1703
1703
if (salt.len > 0 ) {
1704
1704
actual_salt = {reinterpret_cast <const char *>(salt.data ), salt.len };
1705
1705
} else {
1706
- actual_salt = {default_salt, static_cast <unsigned >(EVP_MD_size (md ))};
1706
+ actual_salt = {default_salt, static_cast <unsigned >(md. size ( ))};
1707
1707
}
1708
1708
1709
1709
// We do not use EVP_PKEY_HKDF_MODE_EXTRACT_AND_EXPAND because and instead
@@ -2786,6 +2786,11 @@ bool Cipher::isStreamMode() const {
2786
2786
return getMode () == EVP_CIPH_STREAM_CIPHER;
2787
2787
}
2788
2788
2789
+ bool Cipher::isChaCha20Poly1305 () const {
2790
+ if (!cipher_) return false ;
2791
+ return getNid () == NID_chacha20_poly1305;
2792
+ }
2793
+
2789
2794
int Cipher::getMode () const {
2790
2795
if (!cipher_) return 0 ;
2791
2796
return EVP_CIPHER_mode (cipher_);
@@ -2862,6 +2867,14 @@ bool Cipher::isSupportedAuthenticatedMode() const {
2862
2867
}
2863
2868
}
2864
2869
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
+
2865
2878
// ============================================================================
2866
2879
2867
2880
CipherCtxPointer CipherCtxPointer::New () {
@@ -2938,6 +2951,26 @@ int CipherCtxPointer::getMode() const {
2938
2951
return EVP_CIPHER_CTX_mode (ctx_.get ());
2939
2952
}
2940
2953
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
+
2941
2974
int CipherCtxPointer::getNid () const {
2942
2975
if (!ctx_) return 0 ;
2943
2976
return EVP_CIPHER_CTX_nid (ctx_.get ());
@@ -3720,9 +3753,7 @@ DataPointer Cipher::recover(const EVPKeyPointer& key,
3720
3753
namespace {
3721
3754
struct CipherCallbackContext {
3722
3755
Cipher::CipherNameCallback cb;
3723
- void operator ()(std::string_view name) {
3724
- cb (name);
3725
- }
3756
+ void operator ()(std::string_view name) { cb (name); }
3726
3757
};
3727
3758
3728
3759
#if OPENSSL_VERSION_MAJOR >= 3
@@ -3759,9 +3790,9 @@ void array_push_back(const TypeName* evp_ref,
3759
3790
#else
3760
3791
template <class TypeName >
3761
3792
void 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) {
3765
3796
if (!from) return ;
3766
3797
auto & cb = *(static_cast <CipherCallbackContext*>(arg));
3767
3798
cb (from);
@@ -3776,15 +3807,15 @@ void Cipher::ForEach(Cipher::CipherNameCallback callback) {
3776
3807
3777
3808
EVP_CIPHER_do_all_sorted (
3778
3809
#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>,
3784
3815
#else
3785
- array_push_back<EVP_CIPHER>,
3816
+ array_push_back<EVP_CIPHER>,
3786
3817
#endif
3787
- &context);
3818
+ &context);
3788
3819
}
3789
3820
3790
3821
// ============================================================================
@@ -4143,4 +4174,21 @@ size_t Dsa::getDivisorLength() const {
4143
4174
return BignumPointer::GetBitCount (getQ ());
4144
4175
}
4145
4176
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
+
4146
4194
} // namespace ncrypto
0 commit comments