Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged

SHA3 #84132

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
ef69307
Initial SHA3 bringup on Linux
vcsjones Mar 19, 2023
64f1c2e
Fix failures on unsupported platforms
vcsjones Mar 20, 2023
d077e4e
Some HKDF tests
vcsjones Mar 20, 2023
deccd5a
Get HKDF working with tests
vcsjones Mar 20, 2023
2fa0c13
Add derived hash tests
vcsjones Mar 20, 2023
9319d66
Remove outdated comment
vcsjones Mar 20, 2023
9d1f651
Fix hash validation for PBKDF2
vcsjones Mar 20, 2023
0e79766
Wire in CNG algorithm support testing
vcsjones Mar 20, 2023
5741ea2
Fix querying invalid handles
vcsjones Mar 20, 2023
faa42cf
Wire SHA3 in to SP800108
vcsjones Mar 21, 2023
e8ff0b7
SP800108: Throw PNSE for SHA3 on .NET 8 when not supported
vcsjones Mar 21, 2023
7b90cca
Fix CNG identifiers
vcsjones Mar 23, 2023
9948ff6
Fix PlatformDetection
vcsjones Mar 23, 2023
8081638
Use correct version
vcsjones Mar 23, 2023
693e19b
Psuedo handles for Windows
vcsjones Mar 23, 2023
1f399e7
Fix algorithm identifiers throughout
vcsjones Mar 23, 2023
e80d2fd
Block sizes for SP800-108
vcsjones Mar 23, 2023
34e3077
Add one shots for SP800-108 in CNG
vcsjones Mar 23, 2023
54d16d4
Use known consts for identifiers
vcsjones Mar 23, 2023
8b8a193
SHA3_256 Platform guards
vcsjones Mar 29, 2023
63d383b
Fix SHA3-384 and 512 UOSP
vcsjones Mar 29, 2023
95af4f5
Fix missing SHA3 test coverage
vcsjones Mar 30, 2023
14bc9a0
Only ask CNG if supported for SHA3
vcsjones Mar 30, 2023
b1a27c4
Fix SHA3 availability detection for RSAOpenSsl
vcsjones Mar 30, 2023
bb13b32
Revert "Fix SHA3 availability detection for RSAOpenSsl"
vcsjones Mar 31, 2023
733813c
Prevent SHA3 usage on RSAOpenSsl for macOS
vcsjones Mar 31, 2023
3d608eb
Fixup CAPI and add some tests for signatures
vcsjones Mar 31, 2023
f459f63
Fix verify for CryptoServiceProvider, too
vcsjones Apr 1, 2023
4ae22be
Make PSS as forgiving as PKCS1
vcsjones Apr 1, 2023
c178ca0
Add HMAC and hash tests for ECDH with SHA-3
vcsjones Apr 1, 2023
52f5434
Merge remote-tracking branch 'ms/main' into sha3
vcsjones Apr 13, 2023
81071fd
Code review feedback
vcsjones Apr 13, 2023
630b72d
More code review feedback
vcsjones Apr 13, 2023
a8d26e1
Merge remote-tracking branch 'ms/main' into sha3
vcsjones Apr 25, 2023
9fc9541
Merge remote-tracking branch 'ms/main' into sha3
vcsjones May 3, 2023
86d7ec6
Merge remote-tracking branch 'ms/main' into sha3
vcsjones May 9, 2023
7dfcd6f
Remove UOSP attribute from SHA3
vcsjones May 9, 2023
d7d9abe
Remove unused using for HMAC
vcsjones May 9, 2023
f904880
Merge remote-tracking branch 'ms/main' into sha3
vcsjones May 17, 2023
43385c2
Code review feedback on HKDF tests
vcsjones May 17, 2023
08af75f
Additional code review feedback
vcsjones May 17, 2023
8abcc7a
Add tests for IsSupported on algorithm implementations
vcsjones May 17, 2023
dc8ae5f
Simplify IsBCryptAlgorithmSupported
vcsjones May 17, 2023
b3efe82
Implement and test SHA3 with SignedCms
vcsjones May 17, 2023
5305f10
Add multi-'block' tests for SHA3
vcsjones May 17, 2023
a639910
Use a separate bool to track cached EVP handles
vcsjones May 17, 2023
af7a6b1
Add a SHA3 certificate test
vcsjones May 18, 2023
95a7ced
Change IsSupported to have backing fields to help enlighten JIT about…
vcsjones May 19, 2023
81049b2
Change HashOneShotHelpers to use a switch
vcsjones May 19, 2023
64329a9
Add trailing commas
vcsjones May 19, 2023
c55fea4
Fix CNG test failures
vcsjones May 19, 2023
c735e7a
Fix hash algorithm support detection on Android
vcsjones May 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix algorithm identifiers throughout
  • Loading branch information
vcsjones committed Apr 4, 2023
commit 1f399e7b81568181fa1e76d2be68bf9a9455c8d6
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ internal static IntPtr EvpSha512() =>
nameof(HashAlgorithmName.SHA384) => EvpSha384(),
nameof(HashAlgorithmName.SHA512) => EvpSha512(),
nameof(HashAlgorithmName.MD5) => EvpMd5(),

nameof(HashAlgorithmName.SHA3_256) or
nameof(HashAlgorithmName.SHA3_384) or
nameof(HashAlgorithmName.SHA3_512) => throw new PlatformNotSupportedException(),
"SHA3-256" or "SHA3-384" or "SHA3-512" => throw new PlatformNotSupportedException(),
_ => throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmId))
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ internal static IntPtr HashAlgorithmToEvp(string hashAlgorithmId)
case nameof(HashAlgorithmName.SHA256): return EvpSha256();
case nameof(HashAlgorithmName.SHA384): return EvpSha384();
case nameof(HashAlgorithmName.SHA512): return EvpSha512();
case nameof(HashAlgorithmName.SHA3_256):
case "SHA3-256":
IntPtr sha3_256 = EvpSha3_256();
return sha3_256 != 0 ? sha3_256 : throw new PlatformNotSupportedException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do any of the myriad PNSEs in this change need a custom string, or will it always be pretty obvious that the thing not supported is a SHA-3?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most straight forward uses of SHA3 like SHA3_256 are going to check is supported in Create or their constructor, etc. Many other places try to guard against unsupported SHA3 (like RSA.SignData) guard as well.

We can change it to have a custom string if you prefer, but the call site that throws should make it pretty obvious. The throw here is unlikely to even get hit since we do more up-front checking, but, I can imagine there might be some path to this that doesn't have a precondition check, so an assert felt wrong.

case nameof(HashAlgorithmName.SHA3_384):
case "SHA3-384":
IntPtr sha3_384 = EvpSha3_384();
return sha3_384 != 0 ? sha3_384 : throw new PlatformNotSupportedException();
case nameof(HashAlgorithmName.SHA3_512):
case "SHA3-512":
IntPtr sha3_512 = EvpSha3_512();
return sha3_512 != 0 ? sha3_512 : throw new PlatformNotSupportedException();
case nameof(HashAlgorithmName.MD5): return EvpMd5();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,22 @@ public static void EmptyTests(byte[] key, string label, string context, byte[] e
[InlineData(nameof(HashAlgorithmName.SHA512), 1024 / 8, new byte[] { 0xba, 0xf6, 0xed, 0xa7, 0x3a, 0xf7, 0x12, 0x27 })]
[InlineData(nameof(HashAlgorithmName.SHA512), 1024 / 8 + 1, new byte[] { 0x34, 0xdf, 0x2d, 0x21, 0xfd, 0xf1, 0x0e, 0x13 })]
#if NET8_0_OR_GREATER
[InlineData(nameof(HashAlgorithmName.SHA3_256), 1088 / 8 - 1, new byte[] { 0xa1, 0x96, 0xae, 0x83, 0x56, 0xf4, 0x2a, 0x4b })]
[InlineData(nameof(HashAlgorithmName.SHA3_256), 1088 / 8, new byte[] { 0xe7, 0xe9, 0xe0, 0x98, 0x09, 0x54, 0x54, 0x2d })]
[InlineData(nameof(HashAlgorithmName.SHA3_256), 1088 / 8 + 1, new byte[] { 0x7d, 0x7a, 0x71, 0xdf, 0x1f, 0x5d, 0x5b, 0x44 })]
[InlineData(nameof(HashAlgorithmName.SHA3_384), 832 / 8 - 1, new byte[] { 0xd6, 0x08, 0x69, 0xd0, 0x99, 0x98, 0x6d, 0xcc })]
[InlineData(nameof(HashAlgorithmName.SHA3_384), 832 / 8, new byte[] { 0x49, 0x83, 0x06, 0x4e, 0x08, 0xf8, 0x93, 0x62 })]
[InlineData(nameof(HashAlgorithmName.SHA3_384), 832 / 8 + 1, new byte[] { 0xcc, 0x03, 0x1f, 0x57, 0x5e, 0x0c, 0xe1, 0xe8 })]
[InlineData(nameof(HashAlgorithmName.SHA3_512), 576 / 8 - 1, new byte[] { 0x47, 0xd2, 0x7e, 0x61, 0x01, 0x61, 0x9a, 0xd0 })]
[InlineData(nameof(HashAlgorithmName.SHA3_512), 576 / 8, new byte[] { 0xda, 0x56, 0x5b, 0x08, 0x73, 0xbc, 0x4d, 0x33 })]
[InlineData(nameof(HashAlgorithmName.SHA3_512), 576 / 8 + 1, new byte[] { 0xd3, 0xa1, 0xfd, 0x76, 0xc4, 0xf9, 0x62, 0xc3 })]
[InlineData("SHA3-256", 1088 / 8 - 1, new byte[] { 0xa1, 0x96, 0xae, 0x83, 0x56, 0xf4, 0x2a, 0x4b })]
[InlineData("SHA3-256", 1088 / 8, new byte[] { 0xe7, 0xe9, 0xe0, 0x98, 0x09, 0x54, 0x54, 0x2d })]
[InlineData("SHA3-256", 1088 / 8 + 1, new byte[] { 0x7d, 0x7a, 0x71, 0xdf, 0x1f, 0x5d, 0x5b, 0x44 })]
[InlineData("SHA3-384", 832 / 8 - 1, new byte[] { 0xd6, 0x08, 0x69, 0xd0, 0x99, 0x98, 0x6d, 0xcc })]
[InlineData("SHA3-384", 832 / 8, new byte[] { 0x49, 0x83, 0x06, 0x4e, 0x08, 0xf8, 0x93, 0x62 })]
[InlineData("SHA3-384", 832 / 8 + 1, new byte[] { 0xcc, 0x03, 0x1f, 0x57, 0x5e, 0x0c, 0xe1, 0xe8 })]
[InlineData("SHA3-512", 576 / 8 - 1, new byte[] { 0x47, 0xd2, 0x7e, 0x61, 0x01, 0x61, 0x9a, 0xd0 })]
[InlineData("SHA3-512", 576 / 8, new byte[] { 0xda, 0x56, 0x5b, 0x08, 0x73, 0xbc, 0x4d, 0x33 })]
[InlineData("SHA3-512", 576 / 8 + 1, new byte[] { 0xd3, 0xa1, 0xfd, 0x76, 0xc4, 0xf9, 0x62, 0xc3 })]
#endif
public static void Kdk_HmacBlockBoundarySizes(string hashAlgorithmName, int kdkSize, byte[] expected)
{
#if NET8_0_OR_GREATER
if ((hashAlgorithmName == nameof(HashAlgorithmName.SHA3_256) && !SHA3_256.IsSupported) ||
(hashAlgorithmName == nameof(HashAlgorithmName.SHA3_384) && !SHA3_384.IsSupported) ||
(hashAlgorithmName == nameof(HashAlgorithmName.SHA3_512) && !SHA3_512.IsSupported))
if ((hashAlgorithmName == "SHA3-256" && !SHA3_256.IsSupported) ||
(hashAlgorithmName == "SHA3-384" && !SHA3_384.IsSupported) ||
(hashAlgorithmName == "SHA3-512" && !SHA3_512.IsSupported))
{
throw new SkipTestException($"Algorithm '{hashAlgorithmName}' is not supported on the current platform.");
}
Expand Down Expand Up @@ -334,7 +334,7 @@ public static IEnumerable<object[]> GetOutputLengthBoundaries()
// HMACSHA3_256 output size is 32 bytes
yield return new object[]
{
nameof(HashAlgorithmName.SHA3_256),
"SHA3-256",
new byte[63]
{
0x25, 0xb6, 0xc5, 0xba, 0x60, 0x47, 0x95, 0xb6, 0x5c, 0x92, 0xcb, 0x8f, 0xd2, 0x4d, 0x40, 0xfa,
Expand All @@ -346,7 +346,7 @@ public static IEnumerable<object[]> GetOutputLengthBoundaries()

yield return new object[]
{
nameof(HashAlgorithmName.SHA3_256),
"SHA3-256",
new byte[64]
{
0xc8, 0x8a, 0xfe, 0xd4, 0x62, 0xa6, 0x9e, 0xa6, 0x90, 0xa7, 0xb1, 0xe2, 0x01, 0xff, 0x67, 0x52,
Expand All @@ -358,7 +358,7 @@ public static IEnumerable<object[]> GetOutputLengthBoundaries()

yield return new object[]
{
nameof(HashAlgorithmName.SHA3_256),
"SHA3-256",
new byte[65]
{
0xcc, 0x3a, 0x45, 0x55, 0x26, 0x1e, 0x56, 0xf4, 0x3c, 0x18, 0x66, 0x1d, 0xa8, 0xe3, 0x91, 0xe7,
Expand All @@ -375,7 +375,7 @@ public static IEnumerable<object[]> GetOutputLengthBoundaries()
// HMACSHA3_384 output size is 48 bytes
yield return new object[]
{
nameof(HashAlgorithmName.SHA3_384),
"SHA3-384",
new byte[95]
{
0x80, 0x3b, 0x0a, 0x83, 0xe5, 0xae, 0xab, 0xff, 0x16, 0x7a, 0x04, 0x60, 0x97, 0x74, 0x39, 0xcf,
Expand All @@ -389,7 +389,7 @@ public static IEnumerable<object[]> GetOutputLengthBoundaries()

yield return new object[]
{
nameof(HashAlgorithmName.SHA3_384),
"SHA3-384",
new byte[96]
{
0xb8, 0xea, 0xbd, 0xcb, 0x67, 0xcb, 0xd7, 0xf5, 0x12, 0xdf, 0xc2, 0x35, 0x89, 0x66, 0x80, 0x6f,
Expand All @@ -403,7 +403,7 @@ public static IEnumerable<object[]> GetOutputLengthBoundaries()

yield return new object[]
{
nameof(HashAlgorithmName.SHA3_384),
"SHA3-384",
new byte[97]
{
0x62, 0x93, 0xa5, 0x45, 0x03, 0x7a, 0x2f, 0x50, 0xfe, 0x6b, 0xe6, 0x85, 0x56, 0x88, 0x8c, 0x78,
Expand All @@ -422,7 +422,7 @@ public static IEnumerable<object[]> GetOutputLengthBoundaries()
// HMACSHA3_512 output size is 64 bytes
yield return new object[]
{
nameof(HashAlgorithmName.SHA3_512),
"SHA3-512",
new byte[127]
{
0x41, 0xe4, 0xb5, 0xe6, 0x5b, 0x8c, 0xce, 0x14, 0xa8, 0x39, 0xd3, 0xdf, 0xb4, 0x27, 0x0f, 0xff,
Expand All @@ -438,7 +438,7 @@ public static IEnumerable<object[]> GetOutputLengthBoundaries()

yield return new object[]
{
nameof(HashAlgorithmName.SHA3_512),
"SHA3-512",
new byte[128]
{
0xbe, 0x9f, 0x29, 0xd7, 0x9d, 0x11, 0x7e, 0xd7, 0x33, 0xd4, 0xcf, 0x66, 0xed, 0x1a, 0x61, 0xfd,
Expand All @@ -454,7 +454,7 @@ public static IEnumerable<object[]> GetOutputLengthBoundaries()

yield return new object[]
{
nameof(HashAlgorithmName.SHA3_512),
"SHA3-512",
new byte[129]
{
0x5f, 0xf7, 0xd9, 0x1e, 0x98, 0xb3, 0xa4, 0xab, 0x4d, 0xc8, 0x50, 0x9a, 0xd7, 0x50, 0x37, 0xba,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

namespace System.Security.Cryptography
{
// Strings need to match CNG identifiers.
internal static class HashAlgorithmNames
{
internal const string SHA1 = nameof(SHA1);
internal const string SHA256 = nameof(SHA256);
internal const string SHA384 = nameof(SHA384);
internal const string SHA512 = nameof(SHA512);
#if NET8_0_OR_GREATER
internal const string SHA3_256 = nameof(SHA3_256);
internal const string SHA3_384 = nameof(SHA3_384);
internal const string SHA3_512 = nameof(SHA3_512);
internal const string SHA3_256 = "SHA3-256";
internal const string SHA3_384 = "SHA3-384";
internal const string SHA3_512 = "SHA3-512";
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@ public static void Pbkdf2_Rfc6070_HighIterations()
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.DoesNotSupportSha3))]
[InlineData(nameof(HashAlgorithmName.SHA3_256))]
[InlineData(nameof(HashAlgorithmName.SHA3_384))]
[InlineData(nameof(HashAlgorithmName.SHA3_512))]
[InlineData("SHA3-256")]
[InlineData("SHA3-384")]
[InlineData("SHA3-512")]
public static void UnsupportedPkbdf2Algorithms(string hashAlgorithm)
{
HashAlgorithmName hashAlgorithmName = new HashAlgorithmName(hashAlgorithm);
Expand Down Expand Up @@ -409,7 +409,7 @@ public static IEnumerable<object[]> Pbkdf2_OpenSsl_Vectors()
// hashAlgorithm, password, salt, iterations, expected
yield return new object[]
{
nameof(HashAlgorithmName.SHA3_256),
"SHA3-256",
"password",
"salt",
4096,
Expand All @@ -418,7 +418,7 @@ public static IEnumerable<object[]> Pbkdf2_OpenSsl_Vectors()

yield return new object[]
{
nameof(HashAlgorithmName.SHA3_384),
"SHA3-384",
"password",
"salt",
4096,
Expand All @@ -427,7 +427,7 @@ public static IEnumerable<object[]> Pbkdf2_OpenSsl_Vectors()

yield return new object[]
{
nameof(HashAlgorithmName.SHA3_512),
"SHA3-512",
"password",
"salt",
4096,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ private static IEnumerable<KnownValuesTestCase> GetKnownValuesTestCases()
yield return new KnownValuesTestCase
{
CaseName = "OpenSSL SHA3-256",
HashAlgorithmName = nameof(HashAlgorithmName.SHA3_256),
HashAlgorithmName = "SHA3-256",
Password = "password",
Salt = "salt"u8.ToArray(),
IterationCount = 4096,
Expand All @@ -625,7 +625,7 @@ private static IEnumerable<KnownValuesTestCase> GetKnownValuesTestCases()
yield return new KnownValuesTestCase
{
CaseName = "OpenSSL SHA3-384",
HashAlgorithmName = nameof(HashAlgorithmName.SHA3_384),
HashAlgorithmName = "SHA3-384",
Password = "password",
Salt = "salt"u8.ToArray(),
IterationCount = 4096,
Expand All @@ -636,7 +636,7 @@ private static IEnumerable<KnownValuesTestCase> GetKnownValuesTestCases()
yield return new KnownValuesTestCase
{
CaseName = "OpenSSL SHA3-512",
HashAlgorithmName = nameof(HashAlgorithmName.SHA3_512),
HashAlgorithmName = "SHA3-512",
Password = "password",
Salt = "salt"u8.ToArray(),
IterationCount = 4096,
Expand Down