Skip to content

Add MLDsaCng into Microsoft.Bcl.Cryptography #116678

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

Expand Down Expand Up @@ -36,10 +37,78 @@ internal static ArraySegment<byte> BCryptExportKey(SafeBCryptKeyHandle key, stri

if (ntStatus != NTSTATUS.STATUS_SUCCESS)
{
CryptoPool.Return(rented);
throw CreateCryptographicException(ntStatus);
}

return new ArraySegment<byte>(rented, 0, numBytesNeeded);
}

internal static T BCryptExportKey<T>(SafeBCryptKeyHandle key, string blobType, Func<byte[], T> callback)
{
int numBytesNeeded;
NTSTATUS ntStatus = BCryptExportKey(key, IntPtr.Zero, blobType, null, 0, out numBytesNeeded, 0);

if (ntStatus != NTSTATUS.STATUS_SUCCESS)
{
throw CreateCryptographicException(ntStatus);
}

// Array must be precisely-sized, so no renting.
byte[] destination = new byte[numBytesNeeded];

using (PinAndClear.Track(destination))
{
ntStatus = BCryptExportKey(key, IntPtr.Zero, blobType, destination, numBytesNeeded, out numBytesNeeded, 0);

if (ntStatus != NTSTATUS.STATUS_SUCCESS)
{
throw CreateCryptographicException(ntStatus);
}

if (numBytesNeeded != destination.Length)
{
Debug.Fail("Written byte count does not match required byte count.");
throw new CryptographicException();
}

return callback(destination);
}
}

internal delegate T ExportKeyCallback<T>(ReadOnlySpan<byte> keyBytes);

internal static T BCryptExportKey<T>(SafeBCryptKeyHandle key, string blobType, ExportKeyCallback<T> callback)
{
int numBytesNeeded;
NTSTATUS ntStatus = BCryptExportKey(key, IntPtr.Zero, blobType, null, 0, out numBytesNeeded, 0);

if (ntStatus != NTSTATUS.STATUS_SUCCESS)
{
throw CreateCryptographicException(ntStatus);
}

byte[] rented = CryptoPool.Rent(numBytesNeeded);

try
{
using (PinAndClear.Track(rented))
{
ntStatus = BCryptExportKey(key, IntPtr.Zero, blobType, rented, numBytesNeeded, out numBytesNeeded, 0);

if (ntStatus != NTSTATUS.STATUS_SUCCESS)
{
throw CreateCryptographicException(ntStatus);
}

return callback(rented.AsSpan(0, numBytesNeeded));
}
}
finally
{
// PinAndClear will clear
CryptoPool.Return(rented, clearSize: 0);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ internal static unsafe ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void*
}

[LibraryImport(Libraries.NCrypt, StringMarshalling = StringMarshalling.Utf16)]
internal static unsafe partial ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ReadOnlySpan<byte> pbHashValue, int cbHashValue, ReadOnlySpan<byte> pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags);
private static unsafe partial ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void* pPaddingInfo, byte* pbHashValue, int cbHashValue, byte* pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags);

internal static unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ReadOnlySpan<byte> pbHashValue, int cbHashValue, ReadOnlySpan<byte> pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags)
{
fixed (byte* pHash = &Helpers.GetNonNullPinnableReference(pbHashValue))
fixed (byte* pSignature = &Helpers.GetNonNullPinnableReference(pbSignature))
{
return NCryptVerifySignature(hKey, pPaddingInfo, pHash, cbHashValue, pSignature, cbSignature, dwFlags);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// 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 Internal.Cryptography;
using Microsoft.Win32.SafeHandles;
using AsymmetricPaddingMode = Interop.NCrypt.AsymmetricPaddingMode;
using ErrorCode = Interop.NCrypt.ErrorCode;

namespace System.Security.Cryptography
{
internal static partial class CngCommon
internal static partial class CngHelpers
{
private const int StatusUnsuccessfulRetryCount = 1;

Expand Down
Loading
Loading