Skip to content

Commit 7fae5e0

Browse files
Support SLH-DSA keys in certs (#115212)
Co-authored-by: Kevin Jones <[email protected]>
1 parent d7e5d95 commit 7fae5e0

File tree

44 files changed

+15293
-437
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+15293
-437
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ internal enum EvpAlgorithmFamilyId
338338
DSA = 2,
339339
ECC = 3,
340340
MLKem = 4,
341+
SlhDsa = 5,
341342
}
342343
}
343344
}

src/libraries/Common/src/System/Security/Cryptography/SlhDsaAlgorithm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private SlhDsaAlgorithm(string name, int n, int signatureSizeInBytes, string oid
180180
/// </value>
181181
public static SlhDsaAlgorithm SlhDsaShake256f { get; } = new SlhDsaAlgorithm("SLH-DSA-SHAKE-256f", 32, 49856, Oids.SlhDsaShake256f);
182182

183-
internal static SlhDsaAlgorithm? GetAlgorithmFromOid(string oid)
183+
internal static SlhDsaAlgorithm? GetAlgorithmFromOid(string? oid)
184184
{
185185
return oid switch
186186
{

src/libraries/Common/src/System/Security/Cryptography/SlhDsaImplementation.NotSupported.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal sealed partial class SlhDsaImplementation : SlhDsa
1313
private SlhDsaImplementation(SlhDsaAlgorithm algorithm) : base(algorithm) =>
1414
throw new PlatformNotSupportedException();
1515

16-
internal static partial SlhDsa GenerateKeyCore(SlhDsaAlgorithm algorithm) =>
16+
internal static partial SlhDsaImplementation GenerateKeyCore(SlhDsaAlgorithm algorithm) =>
1717
throw new PlatformNotSupportedException();
1818

1919
// The instance override methods are unreachable, as the constructor will always throw.
@@ -32,13 +32,13 @@ protected override void ExportSlhDsaSecretKeyCore(Span<byte> destination) =>
3232
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
3333
throw new PlatformNotSupportedException();
3434

35-
internal static partial SlhDsa ImportPublicKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
35+
internal static partial SlhDsaImplementation ImportPublicKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
3636
throw new PlatformNotSupportedException();
3737

38-
internal static partial SlhDsa ImportPkcs8PrivateKeyValue(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
38+
internal static partial SlhDsaImplementation ImportPkcs8PrivateKeyValue(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
3939
throw new PlatformNotSupportedException();
4040

41-
internal static partial SlhDsa ImportSecretKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
41+
internal static partial SlhDsaImplementation ImportSecretKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
4242
throw new PlatformNotSupportedException();
4343
}
4444
}

src/libraries/Common/src/System/Security/Cryptography/SlhDsaImplementation.Windows.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal sealed partial class SlhDsaImplementation : SlhDsa
1414
private SlhDsaImplementation(/* CngKey key, */ SlhDsaAlgorithm algorithm) : base(algorithm) =>
1515
throw new PlatformNotSupportedException();
1616

17-
internal static partial SlhDsa GenerateKeyCore(SlhDsaAlgorithm algorithm) =>
17+
internal static partial SlhDsaImplementation GenerateKeyCore(SlhDsaAlgorithm algorithm) =>
1818
throw new PlatformNotSupportedException();
1919

2020
protected override void SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, Span<byte> destination) =>
@@ -32,13 +32,13 @@ protected override void ExportSlhDsaSecretKeyCore(Span<byte> destination) =>
3232
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
3333
throw new PlatformNotSupportedException();
3434

35-
internal static partial SlhDsa ImportPublicKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
35+
internal static partial SlhDsaImplementation ImportPublicKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
3636
throw new PlatformNotSupportedException();
3737

38-
internal static partial SlhDsa ImportPkcs8PrivateKeyValue(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
38+
internal static partial SlhDsaImplementation ImportPkcs8PrivateKeyValue(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
3939
throw new PlatformNotSupportedException();
4040

41-
internal static partial SlhDsa ImportSecretKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
41+
internal static partial SlhDsaImplementation ImportSecretKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
4242
throw new PlatformNotSupportedException();
4343
}
4444
}

src/libraries/Common/src/System/Security/Cryptography/SlhDsaImplementation.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,30 @@ internal sealed partial class SlhDsaImplementation : SlhDsa
1111
{
1212
internal static partial bool SupportsAny();
1313

14-
internal static partial SlhDsa GenerateKeyCore(SlhDsaAlgorithm algorithm);
15-
internal static partial SlhDsa ImportPublicKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source);
16-
internal static partial SlhDsa ImportPkcs8PrivateKeyValue(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source);
17-
internal static partial SlhDsa ImportSecretKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source);
14+
internal static partial SlhDsaImplementation GenerateKeyCore(SlhDsaAlgorithm algorithm);
15+
internal static partial SlhDsaImplementation ImportPublicKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source);
16+
internal static partial SlhDsaImplementation ImportPkcs8PrivateKeyValue(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source);
17+
internal static partial SlhDsaImplementation ImportSecretKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source);
18+
19+
/// <summary>
20+
/// Duplicates an SLH-DSA private key by export/import.
21+
/// Only intended to be used when the key type is unknown.
22+
/// </summary>
23+
internal static SlhDsaImplementation DuplicatePrivateKey(SlhDsa key)
24+
{
25+
Debug.Assert(key is not SlhDsaImplementation);
26+
Debug.Assert(key.Algorithm.SecretKeySizeInBytes <= 128);
27+
28+
Span<byte> secretKey = (stackalloc byte[128])[..key.Algorithm.SecretKeySizeInBytes];
29+
key.ExportSlhDsaSecretKey(secretKey);
30+
try
31+
{
32+
return ImportSecretKey(key.Algorithm, secretKey);
33+
}
34+
finally
35+
{
36+
CryptographicOperations.ZeroMemory(secretKey);
37+
}
38+
}
1839
}
1940
}

0 commit comments

Comments
 (0)