Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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 @@ -184,6 +184,65 @@ internal static bool MLDsaVerifyPreEncoded(
}
}

[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_MLDsaSignExternalMu(
SafeEvpPKeyHandle pkey, IntPtr extraHandle,
ReadOnlySpan<byte> mu, int muLength,
Span<byte> destination, int destinationLength);

internal static void MLDsaSignExternalMu(
SafeEvpPKeyHandle pkey,
ReadOnlySpan<byte> mu,
Span<byte> destination)
{
const int Success = 1;
const int SigningFailure = 0;

int ret = CryptoNative_MLDsaSignExternalMu(
pkey, GetExtraHandle(pkey),
mu, mu.Length,
destination, destination.Length);

if (ret != Success)
{
Debug.Assert(ret == SigningFailure, $"Unexpected return value {ret} from {nameof(CryptoNative_MLDsaSignExternalMu)}.");
throw Interop.Crypto.CreateOpenSslCryptographicException();
}
}

[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_MLDsaVerifyExternalMu(
SafeEvpPKeyHandle pkey, IntPtr extraHandle,
ReadOnlySpan<byte> mu, int muLength,
ReadOnlySpan<byte> signature, int signatureLength);

internal static bool MLDsaVerifyExternalMu(
SafeEvpPKeyHandle pkey,
ReadOnlySpan<byte> mu,
ReadOnlySpan<byte> signature)
{
const int ValidSignature = 1;
const int InvalidSignature = 0;

int ret = CryptoNative_MLDsaVerifyExternalMu(
pkey, GetExtraHandle(pkey),
mu, mu.Length,
signature, signature.Length);

if (ret == ValidSignature)
{
return true;
}
else if (ret == InvalidSignature)
{
return false;
}
else
{
throw Interop.Crypto.CreateOpenSslCryptographicException();
}
}

[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_MLDsaExportSecretKey(SafeEvpPKeyHandle pkey, Span<byte> destination, int destinationLength);

Expand Down
24 changes: 24 additions & 0 deletions src/libraries/Common/src/System/Security/Cryptography/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,29 @@ internal static void ThrowIfAsnInvalidLength(ReadOnlySpan<byte> data)
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
}
}

#if !BUILDING_PKCS
internal static void ThrowIfWrongLength(
ReadOnlySpan<byte> source,
int expectedLength,
[System.Runtime.CompilerServices.CallerArgumentExpression(nameof(source))] string? paramName = null)
{
if (source.Length != expectedLength)
{
throw new ArgumentException(SR.Format(SR.Argument_DestinationImprecise, expectedLength), paramName);
}
}

internal static void ThrowIfWrongLength(
Span<byte> destination,
int expectedLength,
[System.Runtime.CompilerServices.CallerArgumentExpression(nameof(destination))] string? paramName = null)
{
if (destination.Length != expectedLength)
{
throw new ArgumentException(SR.Format(SR.Argument_DestinationImprecise, expectedLength), paramName);
}
}
#endif
}
}
Loading
Loading