-
Notifications
You must be signed in to change notification settings - Fork 5k
SHA3 #84132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SHA3 #84132
Changes from all commits
ef69307
64f1c2e
d077e4e
deccd5a
2fa0c13
9319d66
9d1f651
0e79766
5741ea2
faa42cf
e8ff0b7
7b90cca
9948ff6
8081638
693e19b
1f399e7
e80d2fd
34e3077
54d16d4
8b8a193
63d383b
95af4f5
14bc9a0
b1a27c4
bb13b32
733813c
3d608eb
f459f63
4ae22be
c178ca0
52f5434
81071fd
630b72d
a8d26e1
9fc9541
86d7ec6
7dfcd6f
d7d9abe
f904880
43385c2
08af75f
8abcc7a
dc8ae5f
b3efe82
5305f10
a639910
af7a6b1
95a7ced
81049b2
64329a9
c55fea4
c735e7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,45 +14,128 @@ internal static partial class Crypto | |
private static volatile IntPtr s_evpSha256; | ||
private static volatile IntPtr s_evpSha384; | ||
private static volatile IntPtr s_evpSha512; | ||
private static volatile IntPtr s_evpSha3_256; | ||
private static volatile IntPtr s_evpSha3_384; | ||
private static volatile IntPtr s_evpSha3_512; | ||
private static volatile bool s_evpSha3_256Cached; | ||
private static volatile bool s_evpSha3_384Cached; | ||
private static volatile bool s_evpSha3_512Cached; | ||
|
||
[LibraryImport(Libraries.CryptoNative)] | ||
private static partial IntPtr CryptoNative_EvpMd5(); | ||
|
||
internal static IntPtr EvpMd5() => | ||
private static IntPtr EvpMd5() => | ||
s_evpMd5 != IntPtr.Zero ? s_evpMd5 : (s_evpMd5 = CryptoNative_EvpMd5()); | ||
|
||
[LibraryImport(Libraries.CryptoNative)] | ||
internal static partial IntPtr CryptoNative_EvpSha1(); | ||
private static partial IntPtr CryptoNative_EvpSha1(); | ||
|
||
internal static IntPtr EvpSha1() => | ||
private static IntPtr EvpSha1() => | ||
s_evpSha1 != IntPtr.Zero ? s_evpSha1 : (s_evpSha1 = CryptoNative_EvpSha1()); | ||
|
||
[LibraryImport(Libraries.CryptoNative)] | ||
internal static partial IntPtr CryptoNative_EvpSha256(); | ||
private static partial IntPtr CryptoNative_EvpSha256(); | ||
|
||
internal static IntPtr EvpSha256() => | ||
private static IntPtr EvpSha256() => | ||
s_evpSha256 != IntPtr.Zero ? s_evpSha256 : (s_evpSha256 = CryptoNative_EvpSha256()); | ||
|
||
[LibraryImport(Libraries.CryptoNative)] | ||
internal static partial IntPtr CryptoNative_EvpSha384(); | ||
private static partial IntPtr CryptoNative_EvpSha384(); | ||
|
||
internal static IntPtr EvpSha384() => | ||
private static IntPtr EvpSha384() => | ||
s_evpSha384 != IntPtr.Zero ? s_evpSha384 : (s_evpSha384 = CryptoNative_EvpSha384()); | ||
|
||
[LibraryImport(Libraries.CryptoNative)] | ||
internal static partial IntPtr CryptoNative_EvpSha512(); | ||
private static partial IntPtr CryptoNative_EvpSha512(); | ||
|
||
internal static IntPtr EvpSha512() => | ||
private static IntPtr EvpSha512() => | ||
s_evpSha512 != IntPtr.Zero ? s_evpSha512 : (s_evpSha512 = CryptoNative_EvpSha512()); | ||
|
||
internal static IntPtr HashAlgorithmToEvp(string hashAlgorithmId) => hashAlgorithmId switch | ||
[LibraryImport(Libraries.CryptoNative)] | ||
private static partial IntPtr CryptoNative_EvpSha3_256(); | ||
|
||
private static IntPtr EvpSha3_256() | ||
{ | ||
if (!s_evpSha3_256Cached) | ||
{ | ||
s_evpSha3_256 = CryptoNative_EvpSha3_256(); | ||
s_evpSha3_256Cached = true; | ||
} | ||
|
||
return s_evpSha3_256; | ||
} | ||
|
||
[LibraryImport(Libraries.CryptoNative)] | ||
private static partial IntPtr CryptoNative_EvpSha3_384(); | ||
|
||
private static IntPtr EvpSha3_384() | ||
{ | ||
if (!s_evpSha3_384Cached) | ||
{ | ||
s_evpSha3_384 = CryptoNative_EvpSha3_384(); | ||
s_evpSha3_384Cached = true; | ||
} | ||
|
||
return s_evpSha3_384; | ||
} | ||
|
||
[LibraryImport(Libraries.CryptoNative)] | ||
private static partial IntPtr CryptoNative_EvpSha3_512(); | ||
|
||
private static IntPtr EvpSha3_512() | ||
{ | ||
if (!s_evpSha3_512Cached) | ||
{ | ||
s_evpSha3_512 = CryptoNative_EvpSha3_512(); | ||
s_evpSha3_512Cached = true; | ||
} | ||
|
||
return s_evpSha3_512; | ||
} | ||
|
||
|
||
internal static IntPtr HashAlgorithmToEvp(string hashAlgorithmId) | ||
{ | ||
switch (hashAlgorithmId) | ||
{ | ||
case HashAlgorithmNames.SHA1: return EvpSha1(); | ||
case HashAlgorithmNames.SHA256: return EvpSha256(); | ||
case HashAlgorithmNames.SHA384: return EvpSha384(); | ||
case HashAlgorithmNames.SHA512: return EvpSha512(); | ||
case HashAlgorithmNames.SHA3_256: | ||
IntPtr sha3_256 = EvpSha3_256(); | ||
return sha3_256 != 0 ? sha3_256 : throw new PlatformNotSupportedException(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most straight forward uses of SHA3 like 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 HashAlgorithmNames.SHA3_384: | ||
IntPtr sha3_384 = EvpSha3_384(); | ||
return sha3_384 != 0 ? sha3_384 : throw new PlatformNotSupportedException(); | ||
case HashAlgorithmNames.SHA3_512: | ||
IntPtr sha3_512 = EvpSha3_512(); | ||
return sha3_512 != 0 ? sha3_512 : throw new PlatformNotSupportedException(); | ||
case nameof(HashAlgorithmName.MD5): return EvpMd5(); | ||
default: | ||
throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmId)); | ||
}; | ||
} | ||
|
||
internal static bool HashAlgorithmSupported(string hashAlgorithmId) | ||
{ | ||
nameof(HashAlgorithmName.SHA1) => EvpSha1(), | ||
nameof(HashAlgorithmName.SHA256) => EvpSha256(), | ||
nameof(HashAlgorithmName.SHA384) => EvpSha384(), | ||
nameof(HashAlgorithmName.SHA512) => EvpSha512(), | ||
nameof(HashAlgorithmName.MD5) => EvpMd5(), | ||
_ => throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmId)) | ||
}; | ||
switch (hashAlgorithmId) | ||
{ | ||
case HashAlgorithmNames.SHA1: | ||
case HashAlgorithmNames.SHA256: | ||
case HashAlgorithmNames.SHA384: | ||
case HashAlgorithmNames.SHA512: | ||
case HashAlgorithmNames.MD5: | ||
return true; | ||
case HashAlgorithmNames.SHA3_256: | ||
return EvpSha3_256() != 0; | ||
case HashAlgorithmNames.SHA3_384: | ||
return EvpSha3_384() != 0; | ||
case HashAlgorithmNames.SHA3_512: | ||
return EvpSha3_512() != 0; | ||
default: | ||
throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmId)); | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.