Skip to content

Commit c6bf4d5

Browse files
authored
Add stub implementation of SlhDsaCng and algorithm identifiers (#117889)
1 parent 8277598 commit c6bf4d5

File tree

7 files changed

+138
-1
lines changed

7 files changed

+138
-1
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 the Stateless Hash-Based Digital Signature
11+
/// Algorithm (SLH-DSA).
12+
/// </summary>
13+
/// <remarks>
14+
/// <para>
15+
/// This algorithm is specified by FIPS-205.
16+
/// </para>
17+
/// <para>
18+
/// Developers are encouraged to program against the <see cref="SlhDsa" /> base class,
19+
/// rather than any specific derived class.
20+
/// The derived classes are intended for interop with the underlying system
21+
/// cryptographic libraries.
22+
/// </para>
23+
/// </remarks>
24+
[Experimental(Experimentals.PostQuantumCryptographyDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
25+
public sealed partial class SlhDsaCng : SlhDsa
26+
{
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="SlhDsaCng"/> class by using the specified <see cref="CngKey"/>.
29+
/// </summary>
30+
/// <param name="key">
31+
/// The key that will be used as input to the cryptographic operations performed by the current object.
32+
/// </param>
33+
/// <exception cref="ArgumentNullException">
34+
/// <paramref name="key"/> is <see langword="null"/>.
35+
/// </exception>
36+
/// <exception cref="ArgumentException">
37+
/// <paramref name="key"/> does not specify a Stateless Hash-Based Digital Signature Algorithm (SLH-DSA) group.
38+
/// </exception>
39+
/// <exception cref="PlatformNotSupportedException">
40+
/// Cryptography Next Generation (CNG) classes are not supported on this system.
41+
/// </exception>
42+
[SupportedOSPlatform("windows")]
43+
public SlhDsaCng(CngKey key) : base(SlhDsaAlgorithm.SlhDsaShake256f) // We need to pass something to the base so we can throw PNSE.
44+
{
45+
ArgumentNullException.ThrowIfNull(key);
46+
throw new PlatformNotSupportedException();
47+
}
48+
49+
/// <summary>
50+
/// Gets a new <see cref="CngKey" /> representing the key used by the current instance.
51+
/// </summary>
52+
/// <exception cref="ObjectDisposedException">
53+
/// This instance has been disposed.
54+
/// </exception>
55+
/// <remarks>
56+
/// This <see cref="CngKey"/> object is not the same as the one passed to <see cref="SlhDsaCng(CngKey)"/>,
57+
/// if that constructor was used. However, it will point to the same CNG key.
58+
/// </remarks>
59+
public CngKey GetKey()
60+
{
61+
throw new PlatformNotSupportedException();
62+
}
63+
64+
/// <inheritdoc />
65+
protected override void SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, Span<byte> destination) =>
66+
throw new PlatformNotSupportedException();
67+
68+
/// <inheritdoc />
69+
protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, ReadOnlySpan<byte> signature) =>
70+
throw new PlatformNotSupportedException();
71+
72+
/// <inheritdoc />
73+
protected override void SignPreHashCore(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> context, string hashAlgorithmOid, Span<byte> destination) =>
74+
throw new PlatformNotSupportedException();
75+
76+
/// <inheritdoc />
77+
protected override bool VerifyPreHashCore(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> context, string hashAlgorithmOid, ReadOnlySpan<byte> signature) =>
78+
throw new PlatformNotSupportedException();
79+
80+
/// <inheritdoc />
81+
protected override void ExportSlhDsaPublicKeyCore(Span<byte> destination) =>
82+
throw new PlatformNotSupportedException();
83+
84+
/// <inheritdoc />
85+
protected override void ExportSlhDsaSecretKeyCore(Span<byte> destination) =>
86+
throw new PlatformNotSupportedException();
87+
88+
/// <inheritdoc />
89+
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
90+
throw new PlatformNotSupportedException();
91+
}
92+
}

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
@@ -22,6 +22,7 @@
2222
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.MLKemCng))]
2323
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SlhDsa))]
2424
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SlhDsaAlgorithm))]
25+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SlhDsaCng))]
2526
#endif
2627
#if NET || NETSTANDARD2_1_OR_GREATER
2728
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.PbeEncryptionAlgorithm))]

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@
392392
<Compile Include="$(CommonPath)Interop\Windows\BCrypt\Interop.BCryptGenerateKeyPair.cs"
393393
Link="Common\Interop\Windows\BCrypt\Interop.BCryptGenerateKeyPair.cs" />
394394
<Compile Include="$(CommonPath)Interop\Windows\BCrypt\Interop.BCryptGenRandom.cs"
395-
Link="Common\Interop\Windows\BCrypt\Interop.BCryptGenRandom.cs" />
395+
Link="Common\Interop\Windows\BCrypt\Interop.BCryptGenRandom.cs" />
396396
<Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Interop.BCryptImportKeyPair.cs"
397397
Link="Common\Interop\Windows\BCrypt\Interop.BCryptImportKeyPair.cs" />
398398
<Compile Include="$(CommonPath)Interop\Windows\BCrypt\Interop.BCryptPropertyStrings.cs"
@@ -478,6 +478,8 @@
478478
Link="Common\System\Security\Cryptography\MLKemCng.cs" />
479479
<Compile Include="$(CommonPath)System\Security\Cryptography\MLKemCng.Windows.cs"
480480
Link="Common\System\Security\Cryptography\MLKemCng.Windows.cs" />
481+
<Compile Include="$(CommonPath)System\Security\Cryptography\SlhDsaCng.cs"
482+
Link="Common\System\Security\Cryptography\SlhDsaCng.cs" />
481483
<Compile Include="$(CommonPath)Interop\Windows\NCrypt\Interop.AsymmetricPaddingMode.cs"
482484
Link="Common\Interop\Windows\NCrypt\Interop.AsymmetricPaddingMode.cs" />
483485
<Compile Include="$(CommonPath)Interop\Windows\NCrypt\Interop.ErrorCode.cs"

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ public CngAlgorithm(string algorithm) { }
342342
public static System.Security.Cryptography.CngAlgorithm Sha256 { get { throw null; } }
343343
public static System.Security.Cryptography.CngAlgorithm Sha384 { get { throw null; } }
344344
public static System.Security.Cryptography.CngAlgorithm Sha512 { get { throw null; } }
345+
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
346+
public static System.Security.Cryptography.CngAlgorithm SlhDsa { get { throw null; } }
345347
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? obj) { throw null; }
346348
public bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] System.Security.Cryptography.CngAlgorithm? other) { throw null; }
347349
public override int GetHashCode() { throw null; }
@@ -362,6 +364,8 @@ public CngAlgorithmGroup(string algorithmGroup) { }
362364
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
363365
public static System.Security.Cryptography.CngAlgorithmGroup MLKem { get { throw null; } }
364366
public static System.Security.Cryptography.CngAlgorithmGroup Rsa { get { throw null; } }
367+
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
368+
public static System.Security.Cryptography.CngAlgorithmGroup SlhDsa { get { throw null; } }
365369
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? obj) { throw null; }
366370
public bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] System.Security.Cryptography.CngAlgorithmGroup? other) { throw null; }
367371
public override int GetHashCode() { throw null; }
@@ -3110,6 +3114,20 @@ internal SlhDsaAlgorithm() { }
31103114
public override string ToString() { throw null; }
31113115
}
31123116
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
3117+
public sealed partial class SlhDsaCng : System.Security.Cryptography.SlhDsa
3118+
{
3119+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
3120+
public SlhDsaCng(System.Security.Cryptography.CngKey key) : base (default(System.Security.Cryptography.SlhDsaAlgorithm)) { }
3121+
protected override void ExportSlhDsaPublicKeyCore(System.Span<byte> destination) { }
3122+
protected override void ExportSlhDsaSecretKeyCore(System.Span<byte> destination) { }
3123+
public System.Security.Cryptography.CngKey GetKey() { throw null; }
3124+
protected override void SignDataCore(System.ReadOnlySpan<byte> data, System.ReadOnlySpan<byte> context, System.Span<byte> destination) { }
3125+
protected override void SignPreHashCore(System.ReadOnlySpan<byte> hash, System.ReadOnlySpan<byte> context, string hashAlgorithmOid, System.Span<byte> destination) { }
3126+
protected override bool TryExportPkcs8PrivateKeyCore(System.Span<byte> destination, out int bytesWritten) { throw null; }
3127+
protected override bool VerifyDataCore(System.ReadOnlySpan<byte> data, System.ReadOnlySpan<byte> context, System.ReadOnlySpan<byte> signature) { throw null; }
3128+
protected override bool VerifyPreHashCore(System.ReadOnlySpan<byte> hash, System.ReadOnlySpan<byte> context, string hashAlgorithmOid, System.ReadOnlySpan<byte> signature) { throw null; }
3129+
}
3130+
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
31133131
public sealed partial class SlhDsaOpenSsl : System.Security.Cryptography.SlhDsa
31143132
{
31153133
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]

src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@
456456
Link="Common\System\Security\Cryptography\SlhDsa.cs" />
457457
<Compile Include="$(CommonPath)System\Security\Cryptography\SlhDsaAlgorithm.cs"
458458
Link="Common\System\Security\Cryptography\SlhDsaAlgorithm.cs" />
459+
<Compile Include="$(CommonPath)System\Security\Cryptography\SlhDsaCng.cs"
460+
Link="Common\System\Security\Cryptography\SlhDsaCng.cs" />
459461
<Compile Include="$(CommonPath)System\Security\Cryptography\SlhDsaImplementation.cs"
460462
Link="Common\System\Security\Cryptography\SlhDsaImplementation.cs" />
461463
<Compile Include="$(CommonPath)System\Security\Cryptography\SP800108HmacCounterKdf.cs"

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CngAlgorithm.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@ public static CngAlgorithm Sha512
219219
[Experimental(Experimentals.PostQuantumCryptographyDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
220220
public static CngAlgorithm MLKem => field ??= new CngAlgorithm("ML-KEM"); // BCRYPT_MLKEM_ALGORITHM
221221

222+
/// <summary>
223+
/// Gets a new <see cref="CngAlgorithm"/> object that specifies the Stateless Hash-Based Digital Signature
224+
/// Algorithm (SLH-DSA).
225+
/// </summary>
226+
/// <value>
227+
/// A new <see cref="CngAlgorithm"/> object that specifies the Stateless Hash-Based Digital Signature
228+
/// Algorithm (SLH-DSA).
229+
/// </value>
230+
[Experimental(Experimentals.PostQuantumCryptographyDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
231+
public static CngAlgorithm SlhDsa => field ??= new CngAlgorithm("SLH-DSA"); // BCRYPT_SLHDSA_ALGORITHM
232+
222233
private static CngAlgorithm? s_ecdh;
223234
private static CngAlgorithm? s_ecdhp256;
224235
private static CngAlgorithm? s_ecdhp384;

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CngAlgorithmGroup.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ public static CngAlgorithmGroup Rsa
148148
public static CngAlgorithmGroup MLKem =>
149149
field ??= new CngAlgorithmGroup("MLKEM"); // NCRYPT_MLKEM_ALGORITHM_GROUP
150150

151+
/// <summary>
152+
/// Gets a <see cref="CngAlgorithmGroup" /> object that specifies the Stateless Hash-Based Digital Signature
153+
/// Algorithm (SLH-DSA) family of algorithms.
154+
/// </summary>
155+
/// <value>
156+
/// An object that specifies the SLH-DSA family of algorithms.
157+
/// </value>
158+
[Experimental(Experimentals.PostQuantumCryptographyDiagId, UrlFormat = Experimentals.SharedUrlFormat)]
159+
public static CngAlgorithmGroup SlhDsa =>
160+
field ??= new CngAlgorithmGroup("SLHDSA"); // NCRYPT_SLHDSA_ALGORITHM_GROUP
161+
151162
private static CngAlgorithmGroup? s_dh;
152163
private static CngAlgorithmGroup? s_dsa;
153164
private static CngAlgorithmGroup? s_ecdh;

0 commit comments

Comments
 (0)