Skip to content

Commit f600db4

Browse files
Add stubs for CompositeMLDsa APIs
1 parent bca7bfa commit f600db4

File tree

14 files changed

+379
-1
lines changed

14 files changed

+379
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
namespace System.Security.Cryptography
5+
{
6+
public sealed partial class CompositeMLDsaCng : CompositeMLDsa
7+
{
8+
public partial CngKey GetKey() =>
9+
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(CompositeMLDsa)));
10+
11+
/// <inheritdoc/>
12+
protected override int SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, Span<byte> destination) =>
13+
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(CompositeMLDsa)));
14+
15+
/// <inheritdoc/>
16+
protected override bool TryExportCompositeMLDsaPrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
17+
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(CompositeMLDsa)));
18+
19+
/// <inheritdoc/>
20+
protected override bool TryExportCompositeMLDsaPublicKeyCore(Span<byte> destination, out int bytesWritten) =>
21+
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(CompositeMLDsa)));
22+
23+
/// <inheritdoc/>
24+
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
25+
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(CompositeMLDsa)));
26+
27+
/// <inheritdoc/>
28+
protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, ReadOnlySpan<byte> signature) =>
29+
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(CompositeMLDsa)));
30+
}
31+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.Diagnostics.CodeAnalysis;
5+
using System.Runtime.Versioning;
6+
7+
namespace System.Security.Cryptography
8+
{
9+
/// <summary>
10+
/// Provides a Cryptography Next Generation (CNG) implementation of Composite ML-DSA.
11+
/// </summary>
12+
/// <remarks>
13+
/// <para>
14+
/// Developers are encouraged to program against the <see cref="CompositeMLDsa" /> base class,
15+
/// rather than any specific derived class.
16+
/// The derived classes are intended for interop with the underlying system
17+
/// cryptographic libraries.
18+
/// </para>
19+
/// </remarks>
20+
[Experimental(Experimentals.PostQuantumCryptographyDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
21+
public sealed partial class CompositeMLDsaCng : CompositeMLDsa
22+
{
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="CompositeMLDsaCng"/> class by using the specified <see cref="CngKey"/>.
25+
/// </summary>
26+
/// <param name="key">
27+
/// The key that will be used as input to the cryptographic operations performed by the current object.
28+
/// </param>
29+
/// <exception cref="ArgumentNullException">
30+
/// <paramref name="key"/> is <see langword="null"/>.
31+
/// </exception>
32+
/// <exception cref="ArgumentException">
33+
/// <paramref name="key"/> does not specify a Composite ML-DSA group.
34+
/// </exception>
35+
/// <exception cref="PlatformNotSupportedException">
36+
/// Cryptography Next Generation (CNG) classes are not supported on this system.
37+
/// </exception>
38+
[SupportedOSPlatform("windows")]
39+
public CompositeMLDsaCng(CngKey key)
40+
: base(AlgorithmFromHandle(key))
41+
{
42+
throw new PlatformNotSupportedException();
43+
}
44+
45+
private static CompositeMLDsaAlgorithm AlgorithmFromHandle(CngKey key) =>
46+
throw new PlatformNotSupportedException();
47+
48+
/// <summary>
49+
/// Gets a new <see cref="CngKey" /> representing the key used by the current instance.
50+
/// </summary>
51+
/// <exception cref="ObjectDisposedException">
52+
/// This instance has been disposed.
53+
/// </exception>
54+
/// <remarks>
55+
/// This <see cref="CngKey"/> object is not the same as the one passed to <see cref="CompositeMLDsaCng(CngKey)"/>,
56+
/// if that constructor was used. However, it will point to the same CNG key.
57+
/// </remarks>
58+
public partial CngKey GetKey();
59+
}
60+
}

src/libraries/Microsoft.Bcl.Cryptography/src/Microsoft.Bcl.Cryptography.Forwards.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#if NET10_0_OR_GREATER
1515
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.CompositeMLDsa))]
1616
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.CompositeMLDsaAlgorithm))]
17+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.CompositeMLDsaCng))]
1718
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.MLDsa))]
1819
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.MLDsaAlgorithm))]
1920
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.MLDsaCng))]

src/libraries/Microsoft.Bcl.Cryptography/src/Microsoft.Bcl.Cryptography.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,10 @@
495495
Link="Common\System\Security\Cryptography\CngHelpers.SignVerify.cs" />
496496
<Compile Include="$(CommonPath)System\Security\Cryptography\CngPkcs8.Shared.cs"
497497
Link="Common\System\Security\Cryptography\CngPkcs8.Shared.cs" />
498+
<Compile Include="$(CommonPath)System\Security\Cryptography\CompositeMLDsaCng.cs"
499+
Link="Common\System\Security\Cryptography\CompositeMLDsaCng.cs" />
500+
<Compile Include="$(CommonPath)System\Security\Cryptography\CompositeMLDsaCng.Windows.cs"
501+
Link="Common\System\Security\Cryptography\CompositeMLDsaCng.Windows.cs" />
498502
<Compile Include="$(CommonPath)System\Security\Cryptography\KeyPropertyName.cs"
499503
Link="Common\System\Security\Cryptography\KeyPropertyName.cs" />
500504
<Compile Include="$(CommonPath)System\Security\Cryptography\MLDsaCng.cs"

src/libraries/Microsoft.Bcl.Cryptography/src/System/Security/Cryptography/X509Certificates/X509CertificateKeyAccessors.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,86 @@ public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certific
395395
#endif
396396
}
397397

398+
/// <summary>
399+
/// Gets the <see cref="CompositeMLDsa"/> public key from this certificate.
400+
/// </summary>
401+
/// <param name="certificate">
402+
/// The X.509 certificate that contains the public key.
403+
/// </param>
404+
/// <returns>
405+
/// The public key, or <see langword="null"/> if this certificate does not have a Composite ML-DSA public key.
406+
/// </returns>
407+
/// <exception cref="ArgumentNullException">
408+
/// <paramref name="certificate"/> is <see langword="null"/>.
409+
/// </exception>
410+
/// <exception cref="PlatformNotSupportedException">
411+
/// The certificate has a Composite ML-DSA public key, but the platform does not support Composite ML-DSA.
412+
/// </exception>
413+
/// <exception cref="CryptographicException">
414+
/// The public key was invalid, or otherwise could not be imported.
415+
/// </exception>
416+
[Experimental(Experimentals.PostQuantumCryptographyDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
417+
public static CompositeMLDsa? GetCompositeMLDsaPublicKey(this X509Certificate2 certificate)
418+
{
419+
throw new PlatformNotSupportedException();
420+
}
421+
422+
/// <summary>
423+
/// Gets the <see cref="CompositeMLDsa"/> private key from this certificate.
424+
/// </summary>
425+
/// <param name="certificate">
426+
/// The X.509 certificate that contains the private key.
427+
/// </param>
428+
/// <returns>
429+
/// The private key, or <see langword="null"/> if this certificate does not have a Composite ML-DSA private key.
430+
/// </returns>
431+
/// <exception cref="ArgumentNullException">
432+
/// <paramref name="certificate"/> is <see langword="null"/>.
433+
/// </exception>
434+
/// <exception cref="PlatformNotSupportedException">
435+
/// Retrieving a Composite ML-DSA private key from a certificate is not supported on this platform.
436+
/// </exception>
437+
/// <exception cref="CryptographicException">
438+
/// An error occurred accessing the private key.
439+
/// </exception>
440+
[Experimental(Experimentals.PostQuantumCryptographyDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
441+
public static CompositeMLDsa? GetCompositeMLDsaPrivateKey(this X509Certificate2 certificate)
442+
{
443+
throw new PlatformNotSupportedException();
444+
}
445+
446+
/// <summary>
447+
/// Combines a private key with a certificate containing the associated public key into a
448+
/// new instance that can access the private key.
449+
/// </summary>
450+
/// <param name="certificate">
451+
/// The X.509 certificate that contains the public key.
452+
/// </param>
453+
/// <param name="privateKey">
454+
/// The Composite ML-DSA private key that corresponds to the Composite ML-DSA public key in this certificate.
455+
/// </param>
456+
/// <returns>
457+
/// A new certificate with the <see cref="X509Certificate2.HasPrivateKey" /> property set to <see langword="true"/>.
458+
/// The current certificate isn't modified.
459+
/// </returns>
460+
/// <exception cref="ArgumentNullException">
461+
/// <paramref name="certificate"/> or <paramref name="privateKey"/> is <see langword="null"/>.
462+
/// </exception>
463+
/// <exception cref="ArgumentException">
464+
/// The specified private key doesn't match the public key for this certificate.
465+
/// </exception>
466+
/// <exception cref="InvalidOperationException">
467+
/// The certificate already has an associated private key.
468+
/// </exception>
469+
/// <exception cref="PlatformNotSupportedException">
470+
/// Combining a certificate and a Composite ML-DSA private key is not supported on this platform.
471+
/// </exception>
472+
[Experimental(Experimentals.PostQuantumCryptographyDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
473+
public static X509Certificate2 CopyWithPrivateKey(this X509Certificate2 certificate, CompositeMLDsa privateKey)
474+
{
475+
throw new PlatformNotSupportedException();
476+
}
477+
398478
#if !NET10_0_OR_GREATER
399479
private static ArraySegment<byte> GetCertificateSubjectPublicKeyInfo(X509Certificate2 certificate)
400480
{

src/libraries/System.Security.Cryptography.Pkcs/ref/System.Security.Cryptography.Pkcs.netcoreapp.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public sealed partial class CmsSigner
1616
{
1717
public CmsSigner(System.Security.Cryptography.Pkcs.SubjectIdentifierType signerIdentifierType, System.Security.Cryptography.X509Certificates.X509Certificate2? certificate, System.Security.Cryptography.AsymmetricAlgorithm? privateKey) { }
1818
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
19+
public CmsSigner(System.Security.Cryptography.Pkcs.SubjectIdentifierType signerIdentifierType, System.Security.Cryptography.X509Certificates.X509Certificate2? certificate, System.Security.Cryptography.CompositeMLDsa? privateKey) { }
20+
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
1921
public CmsSigner(System.Security.Cryptography.Pkcs.SubjectIdentifierType signerIdentifierType, System.Security.Cryptography.X509Certificates.X509Certificate2? certificate, System.Security.Cryptography.MLDsa? privateKey) { }
2022
public CmsSigner(System.Security.Cryptography.Pkcs.SubjectIdentifierType signerIdentifierType, System.Security.Cryptography.X509Certificates.X509Certificate2? certificate, System.Security.Cryptography.RSA? privateKey, System.Security.Cryptography.RSASignaturePadding? signaturePadding) { }
2123
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]

src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/CmsSigner.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ public CmsSigner(SubjectIdentifierType signerIdentifierType, X509Certificate2? c
133133
{
134134
}
135135

136+
#if NET || NETSTANDARD2_1
137+
[Experimental(Experimentals.PostQuantumCryptographyDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
138+
public
139+
#else
140+
private
141+
#endif
142+
CmsSigner(SubjectIdentifierType signerIdentifierType, X509Certificate2? certificate, CompositeMLDsa? privateKey)
143+
: this(signerIdentifierType, certificate, privateKey, signaturePadding: null)
144+
{
145+
throw new PlatformNotSupportedException();
146+
}
147+
136148
/// <summary>
137149
/// Initializes a new instance of the CmsSigner class with a specified signer
138150
/// certificate, subject identifier type, private key object, and RSA signature padding.

0 commit comments

Comments
 (0)