-
Notifications
You must be signed in to change notification settings - Fork 5.5k
BCrypt Composite ML-DSA #129612
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 5 commits into
dotnet:main
from
PranavSenthilnathan:cmldsa-bcrypt
Jun 26, 2026
Merged
BCrypt Composite ML-DSA #129612
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f7063c5
BCrypt Composite ML-DSA
PranavSenthilnathan 30f5a41
add assert
PranavSenthilnathan b1c9b6a
M.BCL.Crypto doesn't need managed Composite ML-DSA
PranavSenthilnathan c62670f
address feeback
PranavSenthilnathan cde7fe2
PR feedback
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
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
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
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
232 changes: 201 additions & 31 deletions
232
...libraries/Common/src/System/Security/Cryptography/CompositeMLDsaImplementation.Windows.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 |
|---|---|---|
| @@ -1,81 +1,251 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Formats.Asn1; | ||
| using System.Security.Cryptography.Asn1; | ||
| using Internal.Cryptography; | ||
| using Internal.NativeCrypto; | ||
| using Microsoft.Win32.SafeHandles; | ||
|
|
||
| using NTSTATUS = Interop.BCrypt.NTSTATUS; | ||
|
|
||
| namespace System.Security.Cryptography | ||
| { | ||
| internal sealed partial class CompositeMLDsaImplementation : CompositeMLDsa | ||
| { | ||
| private CompositeMLDsaImplementation(CompositeMLDsaAlgorithm algorithm) | ||
| private static readonly SafeBCryptAlgorithmHandle? s_algHandle = OpenAlgorithmHandle(); | ||
|
|
||
| private readonly bool _hasPrivateKey; | ||
| private SafeBCryptKeyHandle _key; | ||
|
|
||
| private CompositeMLDsaImplementation( | ||
| CompositeMLDsaAlgorithm algorithm, | ||
| SafeBCryptKeyHandle key, | ||
| bool hasPrivateKey) | ||
| : base(algorithm) | ||
| { | ||
| throw new PlatformNotSupportedException(); | ||
| _key = key; | ||
| _hasPrivateKey = hasPrivateKey; | ||
| } | ||
|
|
||
| internal static partial bool SupportsAny() | ||
| [MemberNotNullWhen(true, nameof(s_algHandle))] | ||
| internal static partial bool SupportsAny() => s_algHandle is not null; | ||
|
|
||
| [MemberNotNullWhen(true, nameof(s_algHandle))] | ||
| internal static partial bool IsAlgorithmSupportedImpl(CompositeMLDsaAlgorithm algorithm) => | ||
| SupportsAny() && PqcBlobHelpers.TryGetCompositeMLDsaParameterSet(algorithm, out _); | ||
|
|
||
| protected override int SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, Span<byte> destination) | ||
| { | ||
| if (!Helpers.IsOSPlatformWindows) | ||
| if (!_hasPrivateKey) | ||
| { | ||
| return false; | ||
| throw new CryptographicException(SR.Cryptography_NoPrivateKeyAvailable); | ||
| } | ||
|
|
||
| return CompositeMLDsaManaged.SupportsAny(); | ||
| return Interop.BCrypt.BCryptSignHashPqcPure(_key, data, context, destination); | ||
| } | ||
|
|
||
| internal static partial bool IsAlgorithmSupportedImpl(CompositeMLDsaAlgorithm algorithm) | ||
| protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, ReadOnlySpan<byte> signature) => | ||
| Interop.BCrypt.BCryptVerifySignaturePqcPure(_key, data, context, signature); | ||
|
|
||
| internal static partial CompositeMLDsa GenerateKeyImpl(CompositeMLDsaAlgorithm algorithm) | ||
| { | ||
| if (!Helpers.IsOSPlatformWindows) | ||
| Debug.Assert(SupportsAny()); | ||
|
|
||
| if (!PqcBlobHelpers.TryGetCompositeMLDsaParameterSet(algorithm, out string? parameterSet)) | ||
| { | ||
| return false; | ||
| Debug.Fail("Base class should have validated algorithm support."); | ||
| throw new CryptographicException(); | ||
| } | ||
|
|
||
| return CompositeMLDsaManaged.IsAlgorithmSupportedImpl(algorithm); | ||
| } | ||
| SafeBCryptKeyHandle keyHandle = Interop.BCrypt.BCryptGenerateKeyPair(s_algHandle, keyLength: 0); | ||
|
|
||
| internal static partial CompositeMLDsa GenerateKeyImpl(CompositeMLDsaAlgorithm algorithm) | ||
| { | ||
| if (!Helpers.IsOSPlatformWindows) | ||
| try | ||
| { | ||
| Interop.BCrypt.BCryptSetSZProperty(keyHandle, Interop.BCrypt.BCryptPropertyStrings.BCRYPT_PARAMETER_SET_NAME, parameterSet); | ||
| Interop.BCrypt.BCryptFinalizeKeyPair(keyHandle); | ||
| } | ||
| catch | ||
| { | ||
| throw new PlatformNotSupportedException(); | ||
| keyHandle?.Dispose(); | ||
| throw; | ||
| } | ||
|
|
||
| return CompositeMLDsaManaged.GenerateKeyImpl(algorithm); | ||
| return new CompositeMLDsaImplementation(algorithm, keyHandle, hasPrivateKey: true); | ||
| } | ||
|
|
||
| internal static partial CompositeMLDsa ImportCompositeMLDsaPublicKeyImpl(CompositeMLDsaAlgorithm algorithm, ReadOnlySpan<byte> source) | ||
| { | ||
| if (!Helpers.IsOSPlatformWindows) | ||
| Debug.Assert(SupportsAny()); | ||
|
|
||
| if (!PqcBlobHelpers.TryGetCompositeMLDsaParameterSet(algorithm, out string? parameterSet)) | ||
| { | ||
| throw new PlatformNotSupportedException(); | ||
| Debug.Fail("Base class should have validated algorithm support."); | ||
| throw new CryptographicException(); | ||
| } | ||
|
|
||
| return CompositeMLDsaManaged.ImportCompositeMLDsaPublicKeyImpl(algorithm, source); | ||
| const string PublicBlobType = Interop.BCrypt.KeyBlobType.BCRYPT_PQDSA_PUBLIC_BLOB; | ||
|
|
||
| SafeBCryptKeyHandle key = | ||
| PqcBlobHelpers.EncodeCompositeMLDsaBlob( | ||
| parameterSet, | ||
| source, | ||
| PublicBlobType, | ||
| static blob => Interop.BCrypt.BCryptImportKeyPair(s_algHandle, PublicBlobType, blob)); | ||
|
|
||
| return new CompositeMLDsaImplementation(algorithm, key, hasPrivateKey: false); | ||
| } | ||
|
|
||
| internal static partial CompositeMLDsa ImportCompositeMLDsaPrivateKeyImpl(CompositeMLDsaAlgorithm algorithm, ReadOnlySpan<byte> source) | ||
| { | ||
| if (!Helpers.IsOSPlatformWindows) | ||
| Debug.Assert(SupportsAny()); | ||
|
|
||
| if (!PqcBlobHelpers.TryGetCompositeMLDsaParameterSet(algorithm, out string? parameterSet)) | ||
| { | ||
| throw new PlatformNotSupportedException(); | ||
| Debug.Fail("Base class should have validated algorithm support."); | ||
| throw new CryptographicException(); | ||
| } | ||
|
|
||
| return CompositeMLDsaManaged.ImportCompositeMLDsaPrivateKeyImpl(algorithm, source); | ||
| const string PrivateBlobType = Interop.BCrypt.KeyBlobType.BCRYPT_PQDSA_PRIVATE_BLOB; | ||
|
|
||
| SafeBCryptKeyHandle key = | ||
| PqcBlobHelpers.EncodeCompositeMLDsaBlob( | ||
| parameterSet, | ||
| source, | ||
| PrivateBlobType, | ||
| static blob => Interop.BCrypt.BCryptImportKeyPair(s_algHandle, PrivateBlobType, blob)); | ||
|
|
||
| return new CompositeMLDsaImplementation(algorithm, key, hasPrivateKey: true); | ||
| } | ||
|
|
||
| protected override int SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, Span<byte> destination) => | ||
| throw new PlatformNotSupportedException(); | ||
| protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) | ||
| { | ||
| if (!_hasPrivateKey) | ||
| { | ||
| throw new CryptographicException(SR.Cryptography_NoPrivateKeyAvailable); | ||
| } | ||
|
|
||
| ValuePrivateKeyInfoAsn privateKeyInfo = new() | ||
| { | ||
| PrivateKeyAlgorithm = new ValueAlgorithmIdentifierAsn | ||
| { | ||
| Algorithm = Algorithm.Oid, | ||
| }, | ||
| }; | ||
|
|
||
| protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, ReadOnlySpan<byte> signature) => | ||
| throw new PlatformNotSupportedException(); | ||
| int written = 0; | ||
| byte[] rented = CryptoPool.Rent(Algorithm.MaxPrivateKeySizeInBytes); | ||
|
|
||
| try | ||
| { | ||
| written = ExportKey(Interop.BCrypt.KeyBlobType.BCRYPT_PQDSA_PRIVATE_BLOB, rented); | ||
|
|
||
| Debug.Assert(Algorithm.IsValidPrivateKeySize(written)); | ||
|
|
||
| privateKeyInfo.PrivateKey = rented.AsSpan(0, written); | ||
|
|
||
| AsnWriter pkcs8Writer = new(AsnEncodingRules.DER); | ||
| privateKeyInfo.Encode(pkcs8Writer); | ||
|
|
||
| protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) => | ||
| throw new PlatformNotSupportedException(); | ||
| bool result = pkcs8Writer.TryEncode(destination, out bytesWritten); | ||
| pkcs8Writer.Reset(); | ||
|
|
||
| return result; | ||
| } | ||
| finally | ||
| { | ||
| CryptoPool.Return(rented, clearSize: written); | ||
| } | ||
| } | ||
|
|
||
| protected override int ExportCompositeMLDsaPublicKeyCore(Span<byte> destination) => | ||
| throw new PlatformNotSupportedException(); | ||
| ExportKey(Interop.BCrypt.KeyBlobType.BCRYPT_PQDSA_PUBLIC_BLOB, destination); | ||
|
|
||
| protected override int ExportCompositeMLDsaPrivateKeyCore(Span<byte> destination) | ||
| { | ||
| if (!_hasPrivateKey) | ||
| { | ||
| throw new CryptographicException(SR.Cryptography_NoPrivateKeyAvailable); | ||
| } | ||
|
|
||
| return ExportKey( | ||
| Interop.BCrypt.KeyBlobType.BCRYPT_PQDSA_PRIVATE_BLOB, | ||
| destination); | ||
|
PranavSenthilnathan marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| if (disposing) | ||
| { | ||
| _key?.Dispose(); | ||
| _key = null!; | ||
| } | ||
|
|
||
| base.Dispose(disposing); | ||
| } | ||
|
|
||
| private int ExportKey(string keyBlobType, Span<byte> destination) | ||
| { | ||
| ArraySegment<byte> keyBlob = Interop.BCrypt.BCryptExportKey(_key, keyBlobType); | ||
|
|
||
| try | ||
| { | ||
| ReadOnlySpan<byte> keyBytes = PqcBlobHelpers.DecodeCompositeMLDsaBlob( | ||
| keyBlob, | ||
| out ReadOnlySpan<char> parameterSet, | ||
| out string blobType); | ||
|
|
||
| if (!PqcBlobHelpers.TryGetCompositeMLDsaParameterSet(Algorithm, out string? expectedParameterSet)) | ||
| { | ||
| Debug.Fail("Unsupported algorithm."); | ||
| throw new CryptographicException(); | ||
| } | ||
|
|
||
| if (blobType != keyBlobType || | ||
| keyBytes.Length > destination.Length || | ||
| !parameterSet.SequenceEqual(expectedParameterSet)) | ||
| { | ||
|
PranavSenthilnathan marked this conversation as resolved.
|
||
| Debug.Fail( | ||
| $"{nameof(blobType)}: {blobType}, " + | ||
| $"{nameof(parameterSet)}: {parameterSet.ToString()}, " + | ||
| $"{nameof(keyBytes)}.Length: {keyBytes.Length} / {destination.Length}"); | ||
|
|
||
| throw new CryptographicException(); | ||
| } | ||
|
|
||
| keyBytes.CopyTo(destination); | ||
| return keyBytes.Length; | ||
| } | ||
| finally | ||
| { | ||
| CryptoPool.Return(keyBlob); | ||
| } | ||
| } | ||
|
|
||
| private static SafeBCryptAlgorithmHandle? OpenAlgorithmHandle() | ||
| { | ||
| if (!Helpers.IsOSPlatformWindows) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| protected override int ExportCompositeMLDsaPrivateKeyCore(Span<byte> destination) => | ||
| throw new PlatformNotSupportedException(); | ||
| NTSTATUS status = Interop.BCrypt.BCryptOpenAlgorithmProvider( | ||
| out SafeBCryptAlgorithmHandle hAlgorithm, | ||
| BCryptNative.AlgorithmName.CompositeMLDsa, | ||
| pszImplementation: null, | ||
| Interop.BCrypt.BCryptOpenAlgorithmProviderFlags.None); | ||
|
|
||
| if (status != NTSTATUS.STATUS_SUCCESS) | ||
| { | ||
| hAlgorithm.Dispose(); | ||
| return null; | ||
| } | ||
| else | ||
| { | ||
| return hAlgorithm; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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.