Skip to content

Commit f21b945

Browse files
vcsjonesCopilot
andauthored
Support EVP_MAC for HMAC
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent b4a1c4e commit f21b945

File tree

5 files changed

+333
-50
lines changed

5 files changed

+333
-50
lines changed

src/libraries/System.Security.Cryptography/tests/IncrementalHashTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,24 @@ public static void VerifyEmptyHMAC_Span(HMAC referenceAlgorithm, HashAlgorithmNa
517517
}
518518
}
519519

520+
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotAndroid))]
521+
[MemberData(nameof(GetHMACs))]
522+
public static void VerifyEmptyHMACKey_Cloned(HMAC referenceAlgorithm, HashAlgorithmName hashAlgorithm)
523+
{
524+
using (referenceAlgorithm)
525+
using (IncrementalHash incrementalHash = IncrementalHash.CreateHMAC(hashAlgorithm, Array.Empty<byte>()))
526+
using (IncrementalHash cloned = incrementalHash.Clone())
527+
{
528+
referenceAlgorithm.Key = Array.Empty<byte>();
529+
cloned.AppendData([1, 2, 3]);
530+
byte[] referenceHash = referenceAlgorithm.ComputeHash([1, 2, 3]);
531+
byte[] clonedResult = new byte[referenceHash.Length];
532+
Assert.True(cloned.TryGetHashAndReset(clonedResult, out int bytesWritten));
533+
Assert.Equal(referenceHash.Length, bytesWritten);
534+
Assert.Equal(referenceHash, clonedResult);
535+
}
536+
}
537+
520538
[Theory]
521539
[MemberData(nameof(GetHashAlgorithms))]
522540
public static void VerifyTrivialHash_Span(HashAlgorithm referenceAlgorithm, HashAlgorithmName hashAlgorithm)

src/native/libs/System.Security.Cryptography.Native/opensslshim.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "pal_crypto_config.h"
4040
#include "pal_compiler.h"
4141
#define OPENSSL_VERSION_4_0_RTM 0x40000000L
42+
#define OPENSSL_VERSION_3_0_2_RTM 0x3000002FL
4243
#define OPENSSL_VERSION_3_0_RTM 0x30000000L
4344
#define OPENSSL_VERSION_1_1_1_RTM 0x10101000L
4445
#define OPENSSL_VERSION_1_1_0_RTM 0x10100000L
@@ -362,10 +363,12 @@ extern bool g_libSslUses32BitTime;
362363
REQUIRED_FUNCTION(BN_value_one) \
363364
REQUIRED_FUNCTION(BN_CTX_new) \
364365
REQUIRED_FUNCTION(BN_CTX_free) \
366+
REQUIRED_FUNCTION(CRYPTO_clear_free) \
365367
REQUIRED_FUNCTION(CRYPTO_free) \
366368
REQUIRED_FUNCTION(CRYPTO_get_ex_new_index) \
367369
REQUIRED_FUNCTION(CRYPTO_malloc) \
368370
REQUIRED_FUNCTION(CRYPTO_set_mem_functions) \
371+
REQUIRED_FUNCTION(CRYPTO_zalloc) \
369372
REQUIRED_FUNCTION(d2i_OCSP_RESPONSE) \
370373
REQUIRED_FUNCTION(d2i_PKCS12_fp) \
371374
REQUIRED_FUNCTION(d2i_PKCS7) \
@@ -519,7 +522,9 @@ extern bool g_libSslUses32BitTime;
519522
REQUIRED_FUNCTION(EVP_MD_CTX_new) \
520523
REQUIRED_FUNCTION(EVP_MD_CTX_set_flags) \
521524
LIGHTUP_FUNCTION(EVP_MD_fetch) \
525+
LIGHTUP_FUNCTION(EVP_MD_get0_name) \
522526
RENAMED_FUNCTION(EVP_MD_get_size, EVP_MD_size) \
527+
LIGHTUP_FUNCTION(EVP_MD_is_a) \
523528
REQUIRED_FUNCTION(EVP_PKCS82PKEY) \
524529
REQUIRED_FUNCTION(EVP_PKEY2PKCS8) \
525530
REQUIRED_FUNCTION(EVP_PKEY_CTX_ctrl) \
@@ -913,10 +918,12 @@ extern TYPEOF(OPENSSL_gmtime)* OPENSSL_gmtime_ptr;
913918
#define BN_value_one BN_value_one_ptr
914919
#define BN_CTX_free BN_CTX_free_ptr
915920
#define BN_CTX_new BN_CTX_new_ptr
921+
#define CRYPTO_clear_free CRYPTO_clear_free_ptr
916922
#define CRYPTO_free CRYPTO_free_ptr
917923
#define CRYPTO_get_ex_new_index CRYPTO_get_ex_new_index_ptr
918924
#define CRYPTO_malloc CRYPTO_malloc_ptr
919925
#define CRYPTO_set_mem_functions CRYPTO_set_mem_functions_ptr
926+
#define CRYPTO_zalloc CRYPTO_zalloc_ptr
920927
#define d2i_OCSP_RESPONSE d2i_OCSP_RESPONSE_ptr
921928
#define d2i_PKCS12_fp d2i_PKCS12_fp_ptr
922929
#define d2i_PKCS7 d2i_PKCS7_ptr
@@ -1070,7 +1077,9 @@ extern TYPEOF(OPENSSL_gmtime)* OPENSSL_gmtime_ptr;
10701077
#define EVP_MD_CTX_new EVP_MD_CTX_new_ptr
10711078
#define EVP_MD_CTX_set_flags EVP_MD_CTX_set_flags_ptr
10721079
#define EVP_MD_fetch EVP_MD_fetch_ptr
1080+
#define EVP_MD_get0_name EVP_MD_get0_name_ptr
10731081
#define EVP_MD_get_size EVP_MD_get_size_ptr
1082+
#define EVP_MD_is_a EVP_MD_is_a_ptr
10741083
#define EVP_PKCS82PKEY EVP_PKCS82PKEY_ptr
10751084
#define EVP_PKEY2PKCS8 EVP_PKEY2PKCS8_ptr
10761085
#define EVP_PKEY_CTX_ctrl EVP_PKEY_CTX_ctrl_ptr

src/native/libs/System.Security.Cryptography.Native/osslcompat_30.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#define OSSL_MAC_PARAM_KEY "key"
2323
#define OSSL_MAC_PARAM_CUSTOM "custom"
24+
#define OSSL_MAC_PARAM_DIGEST "digest"
2425
#define OSSL_MAC_PARAM_XOF "xof"
2526
#define OSSL_MAC_PARAM_SIZE "size"
2627

@@ -91,6 +92,8 @@ void EVP_MAC_free(EVP_MAC *mac);
9192

9293
EVP_MD* EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, const char *properties);
9394
int EVP_MD_get_size(const EVP_MD* md);
95+
const char *EVP_MD_get0_name(const EVP_MD *md);
96+
int EVP_MD_is_a(const EVP_MD *md, const char *name);
9497
EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx, const char *name, const char *propquery);
9598
EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey, const char *propquery);
9699
int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params);

0 commit comments

Comments
 (0)