-
Notifications
You must be signed in to change notification settings - Fork 5.2k
SLH-DSA implementation for OpenSSL #114060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PranavSenthilnathan
merged 13 commits into
dotnet:main
from
PranavSenthilnathan:slh-dsa-impl
Apr 18, 2025
Merged
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1d5593e
SLH-DSA implementation for OpenSSL
PranavSenthilnathan ced5261
PR comments
PranavSenthilnathan cfcd249
merge
PranavSenthilnathan 5499e18
merge cleanup
PranavSenthilnathan 83fabda
Add nist tests and copy over ml-dsa tests
PranavSenthilnathan 5a8ccc0
add platform check condition to tests
PranavSenthilnathan 5947ef6
merge
PranavSenthilnathan 5a5f38c
fix merge
PranavSenthilnathan 927d8a5
PR feedback, incorporate changes from ml-dsa
PranavSenthilnathan 82c847d
PR feedback
PranavSenthilnathan e547a15
Update src/libraries/Common/tests/System/Security/Cryptography/Algori…
PranavSenthilnathan b880aa6
Make sure empty spans work as expected
PranavSenthilnathan 45f02b7
merge
PranavSenthilnathan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
...ommon/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EVP.Kem.ExtraHandle.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ommon/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EvpPkey.ExtraHandle.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Security.Cryptography; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class Crypto | ||
| { | ||
| /// <summary> | ||
| /// Gets the extra handle associated with the EVP_PKEY. Some tests need to access | ||
| /// the interop layer and achieve this by adding the relevant classes to the test | ||
| /// project as links. However, accesses to internal members like <see cref="SafeEvpPKeyHandle.ExtraHandle"/> | ||
| /// in the product project will not work in the test project. In this particular case, | ||
| /// the test project does not need the value of the handle, so it can implement this | ||
| /// method to return a null pointer. | ||
| /// </summary> | ||
| /// <param name="handle"> | ||
| /// The extra handle associated with the EVP_PKEY.</param> | ||
| /// <returns> | ||
| /// The extra handle associated with the EVP_PKEY. | ||
| /// </returns> | ||
| private static IntPtr GetExtraHandle(SafeEvpPKeyHandle handle) => handle.ExtraHandle; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
146 changes: 146 additions & 0 deletions
146
...ies/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EvpPkey.SlhDsa.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Runtime.InteropServices; | ||
| using System.Security.Cryptography; | ||
| using Microsoft.Win32.SafeHandles; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class Crypto | ||
| { | ||
| [LibraryImport(Libraries.CryptoNative, StringMarshalling = StringMarshalling.Utf8)] | ||
| private static partial SafeEvpPKeyHandle CryptoNative_SlhDsaGenerateKey(string keyType); | ||
|
|
||
| internal static SafeEvpPKeyHandle SlhDsaGenerateKey(string algorithmName) | ||
| { | ||
| SafeEvpPKeyHandle handle = CryptoNative_SlhDsaGenerateKey(algorithmName); | ||
| Debug.Assert(handle != null, "handle != null"); | ||
|
|
||
| if (handle.IsInvalid) | ||
| { | ||
| Exception ex = Interop.Crypto.CreateOpenSslCryptographicException(); | ||
| handle.Dispose(); | ||
| throw ex; | ||
| } | ||
|
|
||
| return handle; | ||
| } | ||
|
|
||
| // Must be kept in sync with PalSlhDsaId in native shim. | ||
| internal enum PalSlhDsaAlgorithmId | ||
| { | ||
| Unknown = 0, | ||
| SlhDsaSha2_128s = 1, | ||
| SlhDsaShake128s = 2, | ||
| SlhDsaSha2_128f = 3, | ||
| SlhDsaShake128f = 4, | ||
| SlhDsaSha2_192s = 5, | ||
| SlhDsaShake192s = 6, | ||
| SlhDsaSha2_192f = 7, | ||
| SlhDsaShake192f = 8, | ||
| SlhDsaSha2_256s = 9, | ||
| SlhDsaShake256s = 10, | ||
| SlhDsaSha2_256f = 11, | ||
| SlhDsaShake256f = 12, | ||
| } | ||
|
|
||
| [LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_SlhDsaGetPalId")] | ||
| private static partial int CryptoNative_SlhDsaGetPalId( | ||
| SafeEvpPKeyHandle slhDsa, | ||
| out PalSlhDsaAlgorithmId slhDsaId); | ||
|
|
||
| internal static PalSlhDsaAlgorithmId GetSlhDsaAlgorithmId(SafeEvpPKeyHandle key) | ||
| { | ||
| const int Success = 1; | ||
| const int Fail = 0; | ||
| int result = CryptoNative_SlhDsaGetPalId(key, out PalSlhDsaAlgorithmId slhDsaId); | ||
|
|
||
| return result switch | ||
| { | ||
| Success => slhDsaId, | ||
| Fail => throw CreateOpenSslCryptographicException(), | ||
| int other => throw FailThrow(other), | ||
| }; | ||
|
|
||
| static Exception FailThrow(int result) | ||
| { | ||
| Debug.Fail($"Unexpected return value {result} from {nameof(CryptoNative_SlhDsaGetPalId)}."); | ||
| return new CryptographicException(); | ||
| } | ||
| } | ||
|
|
||
| [LibraryImport(Libraries.CryptoNative)] | ||
| private static partial int CryptoNative_SlhDsaSignPure( | ||
| SafeEvpPKeyHandle pkey, IntPtr extraHandle, | ||
| ReadOnlySpan<byte> msg, int msgLength, | ||
| ReadOnlySpan<byte> context, int contextLength, | ||
| Span<byte> destination, int destinationLength); | ||
|
|
||
| internal static void SlhDsaSignPure( | ||
| SafeEvpPKeyHandle pkey, | ||
| ReadOnlySpan<byte> msg, | ||
| ReadOnlySpan<byte> context, | ||
| Span<byte> destination) | ||
| { | ||
| int ret = CryptoNative_SlhDsaSignPure( | ||
| pkey, GetExtraHandle(pkey), | ||
| msg, msg.Length, | ||
| context, context.Length, | ||
| destination, destination.Length); | ||
|
|
||
| if (ret != 1) | ||
| { | ||
| throw Interop.Crypto.CreateOpenSslCryptographicException(); | ||
| } | ||
| } | ||
|
|
||
| [LibraryImport(Libraries.CryptoNative)] | ||
| private static partial int CryptoNative_SlhDsaVerifyPure( | ||
| SafeEvpPKeyHandle pkey, IntPtr extraHandle, | ||
| ReadOnlySpan<byte> msg, int msgLength, | ||
| ReadOnlySpan<byte> context, int contextLength, | ||
| ReadOnlySpan<byte> signature, int signatureLength); | ||
|
|
||
| internal static bool SlhDsaVerifyPure( | ||
| SafeEvpPKeyHandle pkey, | ||
| ReadOnlySpan<byte> msg, | ||
| ReadOnlySpan<byte> context, | ||
| ReadOnlySpan<byte> signature) | ||
| { | ||
| int ret = CryptoNative_SlhDsaVerifyPure( | ||
| pkey, GetExtraHandle(pkey), | ||
| msg, msg.Length, | ||
| context, context.Length, | ||
| signature, signature.Length); | ||
|
|
||
| if (ret == 1) | ||
| { | ||
| return true; | ||
| } | ||
| else if (ret == 0) | ||
| { | ||
| return false; | ||
| } | ||
| else | ||
| { | ||
| throw Interop.Crypto.CreateOpenSslCryptographicException(); | ||
| } | ||
| } | ||
|
|
||
| [LibraryImport(Libraries.CryptoNative)] | ||
| private static partial int CryptoNative_SlhDsaExportSecretKey(SafeEvpPKeyHandle pkey, Span<byte> destination, int destinationLength); | ||
|
|
||
| [LibraryImport(Libraries.CryptoNative)] | ||
| private static partial int CryptoNative_SlhDsaExportPublicKey(SafeEvpPKeyHandle pkey, Span<byte> destination, int destinationLength); | ||
|
|
||
| internal static void SlhDsaExportSecretKey(SafeEvpPKeyHandle key, Span<byte> destination) => | ||
| Interop.Crypto.ExportKeyContents(key, destination, CryptoNative_SlhDsaExportSecretKey); | ||
|
|
||
| internal static void SlhDsaExportPublicKey(SafeEvpPKeyHandle key, Span<byte> destination) => | ||
| Interop.Crypto.ExportKeyContents(key, destination, CryptoNative_SlhDsaExportPublicKey); | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
...Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EvpPkey.SlhDsaAlgs.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| 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 EvpPKeySlhDsaAlgs | ||
| { | ||
| internal static string? SlhDsaSha2_128s { get; } | ||
| internal static string? SlhDsaShake128s { get; } | ||
| internal static string? SlhDsaSha2_128f { get; } | ||
| internal static string? SlhDsaShake128f { get; } | ||
| internal static string? SlhDsaSha2_192s { get; } | ||
| internal static string? SlhDsaShake192s { get; } | ||
| internal static string? SlhDsaSha2_192f { get; } | ||
| internal static string? SlhDsaShake192f { get; } | ||
| internal static string? SlhDsaSha2_256s { get; } | ||
| internal static string? SlhDsaShake256s { get; } | ||
| internal static string? SlhDsaSha2_256f { get; } | ||
| internal static string? SlhDsaShake256f { get; } | ||
|
|
||
| static EvpPKeySlhDsaAlgs() | ||
| { | ||
| 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. | ||
| SlhDsaSha2_128s = IsSignatureAlgorithmAvailable(SlhDsaAlgorithm.SlhDsaSha2_128s.Name); | ||
| SlhDsaShake128s = IsSignatureAlgorithmAvailable(SlhDsaAlgorithm.SlhDsaShake128s.Name); | ||
| SlhDsaSha2_128f = IsSignatureAlgorithmAvailable(SlhDsaAlgorithm.SlhDsaSha2_128f.Name); | ||
| SlhDsaShake128f = IsSignatureAlgorithmAvailable(SlhDsaAlgorithm.SlhDsaShake128f.Name); | ||
| SlhDsaSha2_192s = IsSignatureAlgorithmAvailable(SlhDsaAlgorithm.SlhDsaSha2_192s.Name); | ||
| SlhDsaShake192s = IsSignatureAlgorithmAvailable(SlhDsaAlgorithm.SlhDsaShake192s.Name); | ||
| SlhDsaSha2_192f = IsSignatureAlgorithmAvailable(SlhDsaAlgorithm.SlhDsaSha2_192f.Name); | ||
| SlhDsaShake192f = IsSignatureAlgorithmAvailable(SlhDsaAlgorithm.SlhDsaShake192f.Name); | ||
| SlhDsaSha2_256s = IsSignatureAlgorithmAvailable(SlhDsaAlgorithm.SlhDsaSha2_256s.Name); | ||
| SlhDsaShake256s = IsSignatureAlgorithmAvailable(SlhDsaAlgorithm.SlhDsaShake256s.Name); | ||
| SlhDsaSha2_256f = IsSignatureAlgorithmAvailable(SlhDsaAlgorithm.SlhDsaSha2_256f.Name); | ||
| SlhDsaShake256f = IsSignatureAlgorithmAvailable(SlhDsaAlgorithm.SlhDsaShake256f.Name); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.