forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterop.EVP.MacAlgs.cs
53 lines (44 loc) · 1.89 KB
/
Interop.EVP.MacAlgs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Microsoft.Win32.SafeHandles;
internal static partial class Interop
{
internal static partial class Crypto
{
internal static partial class EvpMacAlgs
{
internal static SafeEvpMacHandle? Kmac128 { get; }
internal static SafeEvpMacHandle? Kmac256 { get; }
static EvpMacAlgs()
{
CryptoInitializer.Initialize();
// Do not use property initializers for these because we need to ensure CryptoInitializer.Initialize
// is called first. Property initializers happen before cctors, so instead set the property after the
// initializer is run.
Kmac128 = EvpMacFetch(HashAlgorithmNames.KMAC128);
Kmac256 = EvpMacFetch(HashAlgorithmNames.KMAC256);
}
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpMacFetch", StringMarshalling = StringMarshalling.Utf8)]
private static partial SafeEvpMacHandle CryptoNative_EvpMacFetch(string algorithm, out int haveFeature);
private static SafeEvpMacHandle? EvpMacFetch(string algorithm)
{
SafeEvpMacHandle mac = CryptoNative_EvpMacFetch(algorithm, out int haveFeature);
if (haveFeature == 0)
{
Debug.Assert(mac.IsInvalid);
mac.Dispose();
return null;
}
if (mac.IsInvalid)
{
mac.Dispose();
throw CreateOpenSslCryptographicException();
}
return mac;
}
}
}
}