Skip to content

Commit abcf594

Browse files
authored
Change Key on MLDsaCng to GetKey method
1 parent cc564ef commit abcf594

File tree

6 files changed

+49
-25
lines changed

6 files changed

+49
-25
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,21 @@ static Exception DebugFailAndGetException(string? parameterSet)
9595
}
9696
}
9797

98-
public partial CngKey Key
98+
public partial CngKey GetKey()
9999
{
100-
get
101-
{
102-
ThrowIfDisposed();
100+
ThrowIfDisposed();
103101

104-
return _key;
105-
}
102+
#if SYSTEM_SECURITY_CRYPTOGRAPHY
103+
return CngHelpers.Duplicate(_key.HandleNoDuplicate, _key.IsEphemeral);
104+
#else
105+
#pragma warning disable CA1416 // only supported on: 'windows'
106+
return _key.Duplicate();
107+
#pragma warning restore CA1416 // only supported on: 'windows'
108+
#endif
106109
}
107110

111+
internal CngKey KeyNoDuplicate => _key;
112+
108113
/// <inheritdoc/>
109114
protected override void ExportMLDsaPublicKeyCore(Span<byte> destination) =>
110115
ExportKey(CngKeyBlobFormat.PQDsaPublicBlob, Algorithm.PublicKeySizeInBytes, destination);

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,15 @@ private static MLDsaAlgorithm AlgorithmFromHandleWithPlatformCheck(CngKey key, o
6363
private static partial MLDsaAlgorithm AlgorithmFromHandle(CngKey key, out CngKey duplicateKey);
6464

6565
/// <summary>
66-
/// Gets the key that will be used by the <see cref="MLDsaCng"/> object for any cryptographic operation that it performs.
66+
/// Gets a new <see cref="CngKey" /> representing the key used by the current instance.
6767
/// </summary>
68-
/// <value>
69-
/// The key that will be used by the <see cref="MLDsaCng"/> object for any cryptographic operation that it performs.
70-
/// </value>
7168
/// <exception cref="ObjectDisposedException">
7269
/// This instance has been disposed.
7370
/// </exception>
7471
/// <remarks>
75-
/// This <see cref="CngKey"/> object is not the same as the one passed to the <see cref="MLDsaCng"/> constructor,
72+
/// This <see cref="CngKey"/> object is not the same as the one passed to <see cref="MLDsaCng(CngKey)"/>,
7673
/// if that constructor was used. However, it will point to the same CNG key.
7774
/// </remarks>
78-
public partial CngKey Key { get; }
75+
public partial CngKey GetKey();
7976
}
8077
}

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/MLDsa/MLDsaCngTests.Windows.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,27 @@ public void MLDsaCng_DuplicateHandle(string? name)
234234
key.Delete();
235235
}
236236
}
237+
238+
[Fact]
239+
public static void MLDsaCng_GetKey()
240+
{
241+
CngProperty parameterSet = MLDsaTestHelpers.GetCngProperty(MLDsaAlgorithm.MLDsa65);
242+
CngKeyCreationParameters creationParams = new();
243+
creationParams.Parameters.Add(parameterSet);
244+
245+
using CngKey key = CngKey.Create(CngAlgorithm.MLDsa, keyName: null, creationParams);
246+
247+
using (MLDsaCng mlDsaKey = new(key))
248+
using (CngKey getKey1 = mlDsaKey.GetKey())
249+
{
250+
using (CngKey getKey2 = mlDsaKey.GetKey())
251+
{
252+
Assert.NotSame(key, getKey1);
253+
Assert.NotSame(getKey1, getKey2);
254+
}
255+
256+
Assert.Equal(key.Algorithm, getKey1.Algorithm); // Assert.NoThrow on getKey1.Algorithm
257+
}
258+
}
237259
}
238260
}

src/libraries/System.Security.Cryptography/ref/System.Security.Cryptography.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2002,11 +2002,11 @@ public sealed partial class MLDsaCng : System.Security.Cryptography.MLDsa
20022002
{
20032003
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
20042004
public MLDsaCng(System.Security.Cryptography.CngKey key) : base (default(System.Security.Cryptography.MLDsaAlgorithm)) { }
2005-
public System.Security.Cryptography.CngKey Key { get { throw null; } }
20062005
protected override void Dispose(bool disposing) { }
20072006
protected override void ExportMLDsaPrivateSeedCore(System.Span<byte> destination) { }
20082007
protected override void ExportMLDsaPublicKeyCore(System.Span<byte> destination) { }
20092008
protected override void ExportMLDsaSecretKeyCore(System.Span<byte> destination) { }
2009+
public System.Security.Cryptography.CngKey GetKey() { throw null; }
20102010
protected override void SignDataCore(System.ReadOnlySpan<byte> data, System.ReadOnlySpan<byte> context, System.Span<byte> destination) { }
20112011
protected override void SignPreHashCore(System.ReadOnlySpan<byte> hash, System.ReadOnlySpan<byte> context, string hashAlgorithmOid, System.Span<byte> destination) { }
20122012
protected override bool TryExportPkcs8PrivateKeyCore(System.Span<byte> destination, out int bytesWritten) { throw null; }

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Cng.NotSupported.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -396,32 +396,32 @@ public sealed partial class MLDsaCng : MLDsa
396396
private static partial MLDsaAlgorithm AlgorithmFromHandle(CngKey key, out CngKey duplicateKey) =>
397397
throw new PlatformNotSupportedException();
398398

399-
public partial CngKey Key =>
400-
throw new PlatformNotSupportedException();
399+
public partial CngKey GetKey() =>
400+
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CryptographyCng);
401401

402402
protected override void ExportMLDsaPrivateSeedCore(Span<byte> destination) =>
403-
throw new PlatformNotSupportedException();
403+
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CryptographyCng);
404404

405405
protected override void ExportMLDsaPublicKeyCore(Span<byte> destination) =>
406-
throw new PlatformNotSupportedException();
406+
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CryptographyCng);
407407

408408
protected override void ExportMLDsaSecretKeyCore(Span<byte> destination) =>
409-
throw new PlatformNotSupportedException();
409+
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CryptographyCng);
410410

411411
protected override void SignDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, Span<byte> destination) =>
412-
throw new PlatformNotSupportedException();
412+
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CryptographyCng);
413413

414414
protected override void SignPreHashCore(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> context, string hashAlgorithmOid, Span<byte> destination) =>
415-
throw new PlatformNotSupportedException();
415+
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CryptographyCng);
416416

417417
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
418-
throw new PlatformNotSupportedException();
418+
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CryptographyCng);
419419

420420
protected override bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> context, ReadOnlySpan<byte> signature) =>
421-
throw new PlatformNotSupportedException();
421+
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CryptographyCng);
422422

423423
protected override bool VerifyPreHashCore(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> context, string hashAlgorithmOid, ReadOnlySpan<byte> signature) =>
424-
throw new PlatformNotSupportedException();
424+
throw new PlatformNotSupportedException(SR.PlatformNotSupported_CryptographyCng);
425425
}
426426

427427
public sealed partial class MLKemCng : MLKem

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/CertificatePal.Windows.PrivateKey.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public ICertificatePal CopyWithPrivateKey(MLDsa privateKey)
196196
{
197197
if (privateKey is MLDsaCng mldsaCng)
198198
{
199-
CngKey key = mldsaCng.Key;
199+
CngKey key = mldsaCng.KeyNoDuplicate;
200200

201201
ICertificatePal? clone = CopyWithPersistedCngKey(key);
202202

@@ -223,7 +223,7 @@ public ICertificatePal CopyWithPrivateKey(MLDsa privateKey)
223223
using (PinAndClear.Track(exportedPkcs8))
224224
using (MLDsaCng clonedKey = MLDsaCng.ImportPkcs8PrivateKey(exportedPkcs8, out _))
225225
{
226-
CngKey clonedCngKey = clonedKey.Key;
226+
CngKey clonedCngKey = clonedKey.KeyNoDuplicate;
227227

228228
if (clonedCngKey.AlgorithmGroup != CngAlgorithmGroup.MLDsa)
229229
{

0 commit comments

Comments
 (0)