Skip to content

Commit 42ba15a

Browse files
committed
Address feedback
1 parent 3a6a0c9 commit 42ba15a

File tree

17 files changed

+347
-396
lines changed

17 files changed

+347
-396
lines changed

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Crypto.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Globalization;
77
using System.Runtime.InteropServices;
8+
using System.Security.Cryptography;
89
using System.Security.Cryptography.X509Certificates;
910
using Microsoft.Win32.SafeHandles;
1011

@@ -102,6 +103,29 @@ private static partial int CryptoNative_X509StoreSetVerifyTime(
102103
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_CheckX509Hostname", StringMarshalling = StringMarshalling.Utf8)]
103104
internal static partial int CheckX509Hostname(SafeX509Handle x509, string hostname, int cchHostname);
104105

106+
[LibraryImport(Libraries.CryptoNative, StringMarshalling = StringMarshalling.Utf8)]
107+
private static partial int CryptoNative_IsSignatureAlgorithmAvailable(string algorithm);
108+
109+
internal static string? IsSignatureAlgorithmAvailable(string algorithm)
110+
{
111+
const int Available = 1;
112+
const int NotAvailable = 0;
113+
114+
int ret = CryptoNative_IsSignatureAlgorithmAvailable(algorithm);
115+
return ret switch
116+
{
117+
Available => algorithm,
118+
NotAvailable => null,
119+
int other => throw Fail(other),
120+
};
121+
122+
static CryptographicException Fail(int result)
123+
{
124+
Debug.Fail($"Unexpected result {result} from {nameof(CryptoNative_IsSignatureAlgorithmAvailable)}");
125+
return new CryptographicException();
126+
}
127+
}
128+
105129
internal static byte[] GetAsn1StringBytes(IntPtr asn1)
106130
{
107131
return GetDynamicBuffer(GetAsn1StringBytes, asn1);

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EvpPKey.MLDsa.cs

Lines changed: 98 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -12,168 +12,134 @@ internal static partial class Interop
1212
{
1313
internal static partial class Crypto
1414
{
15-
[Experimental(Experimentals.PostQuantumCryptographyDiagId)]
16-
internal static partial class EvpPKeyMLDsa
15+
[LibraryImport(Libraries.CryptoNative, StringMarshalling = StringMarshalling.Utf8)]
16+
private static partial SafeEvpPKeyHandle CryptoNative_MLDsaGenerateKey(string keyType, ReadOnlySpan<byte> seed, int seedLength);
17+
18+
internal static SafeEvpPKeyHandle MLDsaGenerateKey(string algorithmName, ReadOnlySpan<byte> seed)
1719
{
18-
internal static string? MLDsa44 { get; }
19-
internal static string? MLDsa65 { get; }
20-
internal static string? MLDsa87 { get; }
20+
SafeEvpPKeyHandle handle = CryptoNative_MLDsaGenerateKey(algorithmName, seed, seed.Length);
21+
Debug.Assert(handle != null, "handle != null");
2122

22-
static EvpPKeyMLDsa()
23+
if (handle.IsInvalid)
2324
{
24-
CryptoInitializer.Initialize();
25-
26-
// Do not use property initializers for these because we need to ensure CryptoInitializer.Initialize
27-
// is called first. Property initializers happen before cctors, so instead set the property after the
28-
// initializer is run.
29-
MLDsa44 = IsSignatureAlgorithmAvailable(MLDsaAlgorithm.MLDsa44.Name);
30-
MLDsa65 = IsSignatureAlgorithmAvailable(MLDsaAlgorithm.MLDsa65.Name);
31-
MLDsa87 = IsSignatureAlgorithmAvailable(MLDsaAlgorithm.MLDsa87.Name);
25+
Exception ex = Interop.Crypto.CreateOpenSslCryptographicException();
26+
handle.Dispose();
27+
throw ex;
3228
}
3329

34-
[LibraryImport(Libraries.CryptoNative, StringMarshalling = StringMarshalling.Utf8)]
35-
private static partial int CryptoNative_IsSignatureAlgorithmAvailable(string algorithm);
30+
return handle;
31+
}
3632

37-
private static string? IsSignatureAlgorithmAvailable(string algorithm)
38-
{
39-
const int Available = 1;
40-
const int NotAvailable = 0;
41-
42-
int ret = CryptoNative_IsSignatureAlgorithmAvailable(algorithm);
43-
return ret switch
44-
{
45-
Available => algorithm,
46-
NotAvailable => null,
47-
int other => throw Fail(other),
48-
};
49-
50-
static CryptographicException Fail(int result)
51-
{
52-
Debug.Fail($"Unexpected result {result} from {nameof(CryptoNative_IsSignatureAlgorithmAvailable)}");
53-
return new CryptographicException();
54-
}
55-
}
33+
[LibraryImport(Libraries.CryptoNative, StringMarshalling = StringMarshalling.Utf8)]
34+
private static partial SafeEvpPKeyHandle CryptoNative_MLDsaImportSecretKey(string keyType, ReadOnlySpan<byte> sk, int skLength);
5635

57-
[LibraryImport(Libraries.CryptoNative, StringMarshalling = StringMarshalling.Utf8)]
58-
private static partial SafeEvpPKeyHandle? CryptoNative_MLDsaGenerateKey(string keyType, ReadOnlySpan<byte> seed, int seedLength);
36+
internal static SafeEvpPKeyHandle MLDsaImportSecretKey(string algorithmName, ReadOnlySpan<byte> sk)
37+
{
38+
SafeEvpPKeyHandle? handle = CryptoNative_MLDsaImportSecretKey(algorithmName, sk, sk.Length);
39+
Debug.Assert(handle != null, "handle != null");
5940

60-
public static SafeEvpPKeyHandle MLDsaGenerateKey(string algorithmName, ReadOnlySpan<byte> seed)
41+
if (handle.IsInvalid)
6142
{
62-
SafeEvpPKeyHandle? handle = CryptoNative_MLDsaGenerateKey(algorithmName, seed, seed.Length);
63-
64-
if (handle == null || handle.IsInvalid)
65-
{
66-
throw Interop.Crypto.CreateOpenSslCryptographicException();
67-
}
68-
69-
return handle;
43+
Exception ex = Interop.Crypto.CreateOpenSslCryptographicException();
44+
handle.Dispose();
45+
throw ex;
7046
}
7147

72-
[LibraryImport(Libraries.CryptoNative, StringMarshalling = StringMarshalling.Utf8)]
73-
private static partial SafeEvpPKeyHandle? CryptoNative_MLDsaImportSecretKey(string keyType, ReadOnlySpan<byte> sk, int skLength);
48+
return handle;
49+
}
7450

75-
public static SafeEvpPKeyHandle MLDsaImportSecretKey(string algorithmName, ReadOnlySpan<byte> sk)
76-
{
77-
SafeEvpPKeyHandle? handle = CryptoNative_MLDsaImportSecretKey(algorithmName, sk, sk.Length);
51+
[LibraryImport(Libraries.CryptoNative, StringMarshalling = StringMarshalling.Utf8)]
52+
private static partial SafeEvpPKeyHandle CryptoNative_MLDsaImportPublicKey(string keyType, ReadOnlySpan<byte> pk, int pkLength);
7853

79-
if (handle == null || handle.IsInvalid)
80-
{
81-
throw Interop.Crypto.CreateOpenSslCryptographicException();
82-
}
54+
internal static SafeEvpPKeyHandle MLDsaImportPublicKey(string algorithmName, ReadOnlySpan<byte> pk)
55+
{
56+
SafeEvpPKeyHandle handle = CryptoNative_MLDsaImportPublicKey(algorithmName, pk, pk.Length);
57+
Debug.Assert(handle != null, "handle != null");
8358

84-
return handle;
59+
if (handle.IsInvalid)
60+
{
61+
Exception ex = Interop.Crypto.CreateOpenSslCryptographicException();
62+
handle.Dispose();
63+
throw ex;
8564
}
8665

87-
[LibraryImport(Libraries.CryptoNative, StringMarshalling = StringMarshalling.Utf8)]
88-
private static partial SafeEvpPKeyHandle? CryptoNative_MLDsaImportPublicKey(string keyType, ReadOnlySpan<byte> pk, int pkLength);
66+
return handle;
67+
}
68+
69+
[LibraryImport(Libraries.CryptoNative)]
70+
private static partial int CryptoNative_MLDsaSignPure(
71+
SafeEvpPKeyHandle pkey, IntPtr extraHandle,
72+
ReadOnlySpan<byte> msg, int msgLength,
73+
ReadOnlySpan<byte> context, int contextLength,
74+
Span<byte> destination, int destinationLength);
75+
76+
internal static void MLDsaSignPure(
77+
SafeEvpPKeyHandle pkey,
78+
ReadOnlySpan<byte> msg,
79+
ReadOnlySpan<byte> context,
80+
Span<byte> destination)
81+
{
82+
int ret = CryptoNative_MLDsaSignPure(
83+
pkey, pkey.ExtraHandle,
84+
msg, msg.Length,
85+
context, context.Length,
86+
destination, destination.Length);
8987

90-
public static SafeEvpPKeyHandle MLDsaImportPublicKey(string algorithmName, ReadOnlySpan<byte> pk)
88+
if (ret != 1)
9189
{
92-
SafeEvpPKeyHandle? handle = CryptoNative_MLDsaImportPublicKey(algorithmName, pk, pk.Length);
90+
throw Interop.Crypto.CreateOpenSslCryptographicException();
91+
}
92+
}
9393

94-
if (handle == null || handle.IsInvalid)
95-
{
96-
throw Interop.Crypto.CreateOpenSslCryptographicException();
97-
}
94+
[LibraryImport(Libraries.CryptoNative)]
95+
private static partial int CryptoNative_MLDsaVerifyPure(
96+
SafeEvpPKeyHandle pkey, IntPtr extraHandle,
97+
ReadOnlySpan<byte> msg, int msgLength,
98+
ReadOnlySpan<byte> context, int contextLength,
99+
ReadOnlySpan<byte> signature, int signatureLength);
100+
101+
internal static bool MLDsaVerifyPure(
102+
SafeEvpPKeyHandle pkey,
103+
ReadOnlySpan<byte> msg,
104+
ReadOnlySpan<byte> context,
105+
ReadOnlySpan<byte> signature)
106+
{
107+
int ret = CryptoNative_MLDsaVerifyPure(
108+
pkey, pkey.ExtraHandle,
109+
msg, msg.Length,
110+
context, context.Length,
111+
signature, signature.Length);
98112

99-
return handle;
113+
if (ret == 1)
114+
{
115+
return true;
100116
}
101-
102-
[LibraryImport(Libraries.CryptoNative)]
103-
private static partial int CryptoNative_MLDsaSignPure(
104-
SafeEvpPKeyHandle pkey, IntPtr extraHandle,
105-
ReadOnlySpan<byte> msg, int msgLength,
106-
ReadOnlySpan<byte> context, int contextLength,
107-
Span<byte> destination, int destinationLength);
108-
109-
public static void MLDsaSignPure(
110-
SafeEvpPKeyHandle pkey,
111-
ReadOnlySpan<byte> msg,
112-
ReadOnlySpan<byte> context,
113-
Span<byte> destination)
117+
else if (ret == 0)
114118
{
115-
int ret = CryptoNative_MLDsaSignPure(
116-
pkey, pkey.ExtraHandle,
117-
msg, msg.Length,
118-
context, context.Length,
119-
destination, destination.Length);
120-
121-
if (ret != 1)
122-
{
123-
throw Interop.Crypto.CreateOpenSslCryptographicException();
124-
}
119+
return false;
125120
}
126-
127-
[LibraryImport(Libraries.CryptoNative)]
128-
private static partial int CryptoNative_MLDsaVerifyPure(
129-
SafeEvpPKeyHandle pkey, IntPtr extraHandle,
130-
ReadOnlySpan<byte> msg, int msgLength,
131-
ReadOnlySpan<byte> context, int contextLength,
132-
ReadOnlySpan<byte> signature, int signatureLength);
133-
134-
public static bool MLDsaVerifyPure(
135-
SafeEvpPKeyHandle pkey,
136-
ReadOnlySpan<byte> msg,
137-
ReadOnlySpan<byte> context,
138-
ReadOnlySpan<byte> signature)
121+
else
139122
{
140-
int ret = CryptoNative_MLDsaVerifyPure(
141-
pkey, pkey.ExtraHandle,
142-
msg, msg.Length,
143-
context, context.Length,
144-
signature, signature.Length);
145-
146-
if (ret == 1)
147-
{
148-
return true;
149-
}
150-
else if (ret == 0)
151-
{
152-
return false;
153-
}
154-
else
155-
{
156-
throw Interop.Crypto.CreateOpenSslCryptographicException();
157-
}
123+
throw Interop.Crypto.CreateOpenSslCryptographicException();
158124
}
125+
}
159126

160-
[LibraryImport(Libraries.CryptoNative)]
161-
private static partial int CryptoNative_MLDsaExportSecretKey(SafeEvpPKeyHandle pkey, Span<byte> destination, int destinationLength);
127+
[LibraryImport(Libraries.CryptoNative)]
128+
private static partial int CryptoNative_MLDsaExportSecretKey(SafeEvpPKeyHandle pkey, Span<byte> destination, int destinationLength);
162129

163-
[LibraryImport(Libraries.CryptoNative)]
164-
private static partial int CryptoNative_MLDsaExportSeed(SafeEvpPKeyHandle pkey, Span<byte> destination, int destinationLength);
130+
[LibraryImport(Libraries.CryptoNative)]
131+
private static partial int CryptoNative_MLDsaExportSeed(SafeEvpPKeyHandle pkey, Span<byte> destination, int destinationLength);
165132

166-
[LibraryImport(Libraries.CryptoNative)]
167-
private static partial int CryptoNative_MLDsaExportPublicKey(SafeEvpPKeyHandle pkey, Span<byte> destination, int destinationLength);
133+
[LibraryImport(Libraries.CryptoNative)]
134+
private static partial int CryptoNative_MLDsaExportPublicKey(SafeEvpPKeyHandle pkey, Span<byte> destination, int destinationLength);
168135

169-
public static void MLDsaExportSecretKey(SafeEvpPKeyHandle key, Span<byte> destination) =>
170-
Interop.Crypto.ExportKeyContents(key, destination, CryptoNative_MLDsaExportSecretKey);
136+
internal static void MLDsaExportSecretKey(SafeEvpPKeyHandle key, Span<byte> destination) =>
137+
Interop.Crypto.ExportKeyContents(key, destination, CryptoNative_MLDsaExportSecretKey);
171138

172-
public static void MLDsaExportSeed(SafeEvpPKeyHandle key, Span<byte> destination) =>
173-
Interop.Crypto.ExportKeyContents(key, destination, CryptoNative_MLDsaExportSeed);
139+
internal static void MLDsaExportSeed(SafeEvpPKeyHandle key, Span<byte> destination) =>
140+
Interop.Crypto.ExportKeyContents(key, destination, CryptoNative_MLDsaExportSeed);
174141

175-
public static void MLDsaExportPublicKey(SafeEvpPKeyHandle key, Span<byte> destination) =>
176-
Interop.Crypto.ExportKeyContents(key, destination, CryptoNative_MLDsaExportPublicKey);
177-
}
142+
internal static void MLDsaExportPublicKey(SafeEvpPKeyHandle key, Span<byte> destination) =>
143+
Interop.Crypto.ExportKeyContents(key, destination, CryptoNative_MLDsaExportPublicKey);
178144
}
179145
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
7+
using System.Runtime.InteropServices;
8+
using System.Security.Cryptography;
9+
using Microsoft.Win32.SafeHandles;
10+
11+
internal static partial class Interop
12+
{
13+
internal static partial class Crypto
14+
{
15+
internal static partial class EvpPKeyMLDsaAlgs
16+
{
17+
internal static string? MLDsa44 { get; }
18+
internal static string? MLDsa65 { get; }
19+
internal static string? MLDsa87 { get; }
20+
21+
static EvpPKeyMLDsaAlgs()
22+
{
23+
CryptoInitializer.Initialize();
24+
25+
// Do not use property initializers for these because we need to ensure CryptoInitializer.Initialize
26+
// is called first. Property initializers happen before cctors, so instead set the property after the
27+
// initializer is run.
28+
MLDsa44 = IsSignatureAlgorithmAvailable(MLDsaAlgorithm.MLDsa44.Name);
29+
MLDsa65 = IsSignatureAlgorithmAvailable(MLDsaAlgorithm.MLDsa65.Name);
30+
MLDsa87 = IsSignatureAlgorithmAvailable(MLDsaAlgorithm.MLDsa87.Name);
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)