Skip to content

Commit 6bc502f

Browse files
Update Composite ML-DSA import/export APIs (#118600)
1 parent 7bc40a5 commit 6bc502f

File tree

14 files changed

+1019
-565
lines changed

14 files changed

+1019
-565
lines changed

src/libraries/Common/src/System/Security/Cryptography/CompositeMLDsa.cs

Lines changed: 176 additions & 114 deletions
Large diffs are not rendered by default.

src/libraries/Common/src/System/Security/Cryptography/CompositeMLDsaAlgorithm.cs

Lines changed: 178 additions & 121 deletions
Large diffs are not rendered by default.

src/libraries/Common/src/System/Security/Cryptography/CompositeMLDsaCng.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ protected override int SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte>
1313
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(CompositeMLDsa)));
1414

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

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

2323
/// <inheritdoc/>

src/libraries/Common/src/System/Security/Cryptography/CompositeMLDsaImplementation.NotSupported.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byt
3737
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
3838
throw new PlatformNotSupportedException();
3939

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

43-
protected override bool TryExportCompositeMLDsaPrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
43+
protected override int ExportCompositeMLDsaPrivateKeyCore(Span<byte> destination) =>
4444
throw new PlatformNotSupportedException();
4545
}
4646
}

src/libraries/Common/src/System/Security/Cryptography/CompositeMLDsaImplementation.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byt
7272
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
7373
throw new PlatformNotSupportedException();
7474

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

78-
protected override bool TryExportCompositeMLDsaPrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
78+
protected override int ExportCompositeMLDsaPrivateKeyCore(Span<byte> destination) =>
7979
throw new PlatformNotSupportedException();
8080
}
8181
}

src/libraries/Common/src/System/Security/Cryptography/CompositeMLDsaManaged.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -370,26 +370,29 @@ protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byt
370370
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
371371
throw new PlatformNotSupportedException();
372372

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

380+
int bytesWritten = 0;
381+
380382
_mldsa.ExportMLDsaPublicKey(destination.Slice(0, AlgorithmDetails.MLDsaAlgorithm.PublicKeySizeInBytes));
383+
bytesWritten += AlgorithmDetails.MLDsaAlgorithm.PublicKeySizeInBytes;
381384

382-
if (_componentAlgorithm.TryExportPublicKey(destination.Slice(AlgorithmDetails.MLDsaAlgorithm.PublicKeySizeInBytes), out int componentBytesWritten))
385+
if (!_componentAlgorithm.TryExportPublicKey(destination.Slice(AlgorithmDetails.MLDsaAlgorithm.PublicKeySizeInBytes), out int componentBytesWritten))
383386
{
384-
bytesWritten = AlgorithmDetails.MLDsaAlgorithm.PublicKeySizeInBytes + componentBytesWritten;
385-
return true;
387+
throw new CryptographicException();
386388
}
387389

388-
bytesWritten = 0;
389-
return false;
390+
bytesWritten += componentBytesWritten;
391+
392+
return bytesWritten;
390393
}
391394

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

399402
try
400403
{
404+
int bytesWritten = 0;
405+
401406
_mldsa.ExportMLDsaPrivateSeed(destination.Slice(0, AlgorithmDetails.MLDsaAlgorithm.PrivateSeedSizeInBytes));
407+
bytesWritten += AlgorithmDetails.MLDsaAlgorithm.PrivateSeedSizeInBytes;
402408

403-
if (_componentAlgorithm.TryExportPrivateKey(destination.Slice(AlgorithmDetails.MLDsaAlgorithm.PrivateSeedSizeInBytes), out int componentBytesWritten))
409+
if (!_componentAlgorithm.TryExportPrivateKey(destination.Slice(AlgorithmDetails.MLDsaAlgorithm.PrivateSeedSizeInBytes), out int componentBytesWritten))
404410
{
405-
bytesWritten = AlgorithmDetails.MLDsaAlgorithm.PrivateSeedSizeInBytes + componentBytesWritten;
406-
return true;
411+
throw new CryptographicException();
407412
}
408413

409-
bytesWritten = 0;
410-
return false;
414+
bytesWritten += componentBytesWritten;
415+
416+
return bytesWritten;
411417
}
412418
catch (CryptographicException)
413419
{

src/libraries/Common/src/System/Security/Cryptography/CryptoPool.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ internal static void Return(byte[] array, int clearSize = ClearAll)
4545

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

48+
internal readonly bool IsRented => _rented is not null;
49+
4850
public void Dispose()
4951
{
5052
Return();

0 commit comments

Comments
 (0)