Skip to content
Merged
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
290 changes: 176 additions & 114 deletions src/libraries/Common/src/System/Security/Cryptography/CompositeMLDsa.cs

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ protected override int SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte>
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(CompositeMLDsa)));

/// <inheritdoc/>
protected override bool TryExportCompositeMLDsaPrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
protected override int ExportCompositeMLDsaPrivateKeyCore(Span<byte> destination) =>
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(CompositeMLDsa)));

/// <inheritdoc/>
protected override bool TryExportCompositeMLDsaPublicKeyCore(Span<byte> destination, out int bytesWritten) =>
protected override int ExportCompositeMLDsaPublicKeyCore(Span<byte> destination) =>
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(CompositeMLDsa)));

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byt
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
throw new PlatformNotSupportedException();

protected override bool TryExportCompositeMLDsaPublicKeyCore(Span<byte> destination, out int bytesWritten) =>
protected override int ExportCompositeMLDsaPublicKeyCore(Span<byte> destination) =>
throw new PlatformNotSupportedException();

protected override bool TryExportCompositeMLDsaPrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
protected override int ExportCompositeMLDsaPrivateKeyCore(Span<byte> destination) =>
throw new PlatformNotSupportedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byt
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
throw new PlatformNotSupportedException();

protected override bool TryExportCompositeMLDsaPublicKeyCore(Span<byte> destination, out int bytesWritten) =>
protected override int ExportCompositeMLDsaPublicKeyCore(Span<byte> destination) =>
throw new PlatformNotSupportedException();

protected override bool TryExportCompositeMLDsaPrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
protected override int ExportCompositeMLDsaPrivateKeyCore(Span<byte> destination) =>
throw new PlatformNotSupportedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,26 +370,29 @@ protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byt
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
throw new PlatformNotSupportedException();

protected override bool TryExportCompositeMLDsaPublicKeyCore(Span<byte> destination, out int bytesWritten)
protected override int ExportCompositeMLDsaPublicKeyCore(Span<byte> destination)
{
// draft-ietf-lamps-pq-composite-sigs-latest (June 20, 2025), 5.1
// 1. Combine and output the encoded public key
//
// output mldsaPK || tradPK

int bytesWritten = 0;

_mldsa.ExportMLDsaPublicKey(destination.Slice(0, AlgorithmDetails.MLDsaAlgorithm.PublicKeySizeInBytes));
bytesWritten += AlgorithmDetails.MLDsaAlgorithm.PublicKeySizeInBytes;

if (_componentAlgorithm.TryExportPublicKey(destination.Slice(AlgorithmDetails.MLDsaAlgorithm.PublicKeySizeInBytes), out int componentBytesWritten))
if (!_componentAlgorithm.TryExportPublicKey(destination.Slice(AlgorithmDetails.MLDsaAlgorithm.PublicKeySizeInBytes), out int componentBytesWritten))
{
bytesWritten = AlgorithmDetails.MLDsaAlgorithm.PublicKeySizeInBytes + componentBytesWritten;
return true;
throw new CryptographicException();
}

bytesWritten = 0;
return false;
bytesWritten += componentBytesWritten;

return bytesWritten;
}

protected override bool TryExportCompositeMLDsaPrivateKeyCore(Span<byte> destination, out int bytesWritten)
protected override int ExportCompositeMLDsaPrivateKeyCore(Span<byte> destination)
{
// draft-ietf-lamps-pq-composite-sigs-latest (June 20, 2025), 5.2
// 1. Combine and output the encoded private key
Expand All @@ -398,16 +401,19 @@ protected override bool TryExportCompositeMLDsaPrivateKeyCore(Span<byte> destina

try
{
int bytesWritten = 0;

_mldsa.ExportMLDsaPrivateSeed(destination.Slice(0, AlgorithmDetails.MLDsaAlgorithm.PrivateSeedSizeInBytes));
bytesWritten += AlgorithmDetails.MLDsaAlgorithm.PrivateSeedSizeInBytes;

if (_componentAlgorithm.TryExportPrivateKey(destination.Slice(AlgorithmDetails.MLDsaAlgorithm.PrivateSeedSizeInBytes), out int componentBytesWritten))
if (!_componentAlgorithm.TryExportPrivateKey(destination.Slice(AlgorithmDetails.MLDsaAlgorithm.PrivateSeedSizeInBytes), out int componentBytesWritten))
{
bytesWritten = AlgorithmDetails.MLDsaAlgorithm.PrivateSeedSizeInBytes + componentBytesWritten;
return true;
throw new CryptographicException();
}

bytesWritten = 0;
return false;
bytesWritten += componentBytesWritten;

return bytesWritten;
}
catch (CryptographicException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ internal static void Return(byte[] array, int clearSize = ClearAll)

internal Span<byte> Span { get; private set; }

internal readonly bool IsRented => _rented is not null;

public void Dispose()
{
Return();
Expand Down
Loading
Loading