Skip to content

Commit 965f265

Browse files
authored
ML-KEM: Miscellaneous cleanup
Use an existing decoding helper for SubjectPublicKeyInfo, remove polyfill for ObjectDisposedException.ThrowIf, and use CryptoPool consistently.
1 parent 98b09ef commit 965f265

File tree

1 file changed

+14
-29
lines changed
  • src/libraries/Common/src/System/Security/Cryptography

1 file changed

+14
-29
lines changed

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

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
5-
using System.Buffers;
64
using System.Diagnostics;
75
using System.Diagnostics.CodeAnalysis;
86
using System.Formats.Asn1;
@@ -1173,25 +1171,20 @@ public static MLKem ImportSubjectPublicKeyInfo(ReadOnlySpan<byte> source)
11731171
ThrowIfTrailingData(source);
11741172
ThrowIfNotSupported();
11751173

1176-
unsafe
1177-
{
1178-
fixed (byte* pointer = source)
1179-
{
1180-
using (PointerMemoryManager<byte> manager = new(pointer, source.Length))
1181-
{
1182-
AsnValueReader reader = new(source, AsnEncodingRules.DER);
1183-
SubjectPublicKeyInfoAsn.Decode(ref reader, manager.Memory, out SubjectPublicKeyInfoAsn spki);
1184-
MLKemAlgorithm algorithm = GetAlgorithmIdentifier(ref spki.Algorithm);
1185-
ReadOnlySpan<byte> subjectPublicKey = spki.SubjectPublicKey.Span;
1174+
KeyFormatHelper.ReadSubjectPublicKeyInfo(s_knownOids, source, SubjectPublicKeyReader, out int read, out MLKem kem);
1175+
Debug.Assert(read == source.Length);
1176+
return kem;
11861177

1187-
if (subjectPublicKey.Length != algorithm.EncapsulationKeySizeInBytes)
1188-
{
1189-
throw new CryptographicException(SR.Argument_KemInvalidEncapsulationKeyLength);
1190-
}
1178+
static void SubjectPublicKeyReader(ReadOnlyMemory<byte> key, in AlgorithmIdentifierAsn identifier, out MLKem kem)
1179+
{
1180+
MLKemAlgorithm algorithm = GetAlgorithmIdentifier(in identifier);
11911181

1192-
return MLKemImplementation.ImportEncapsulationKeyImpl(algorithm, subjectPublicKey);
1193-
}
1182+
if (key.Length != algorithm.EncapsulationKeySizeInBytes)
1183+
{
1184+
throw new CryptographicException(SR.Argument_KemInvalidEncapsulationKeyLength);
11941185
}
1186+
1187+
kem = MLKemImplementation.ImportEncapsulationKeyImpl(algorithm, key.Span);
11951188
}
11961189
}
11971190

@@ -1669,9 +1662,8 @@ private protected static void ThrowIfNotSupported()
16691662

16701663
private static MLKemAlgorithm GetAlgorithmIdentifier(ref readonly AlgorithmIdentifierAsn identifier)
16711664
{
1672-
MLKemAlgorithm algorithm = MLKemAlgorithm.FromOid(identifier.Algorithm) ??
1673-
throw new CryptographicException(
1674-
SR.Format(SR.Cryptography_UnknownAlgorithmIdentifier, identifier.Algorithm));
1665+
MLKemAlgorithm? algorithm = MLKemAlgorithm.FromOid(identifier.Algorithm);
1666+
Debug.Assert(algorithm is not null, "Algorithm identifier should have been pre-validated by KeyFormatHelper.");
16751667

16761668
if (identifier.Parameters.HasValue)
16771669
{
@@ -1767,14 +1759,7 @@ private static void ThrowIfTrailingData(ReadOnlySpan<byte> data)
17671759

17681760
private protected void ThrowIfDisposed()
17691761
{
1770-
#if NET
17711762
ObjectDisposedException.ThrowIf(_disposed, typeof(MLKem));
1772-
#else
1773-
if (_disposed)
1774-
{
1775-
throw new ObjectDisposedException(typeof(MLKem).FullName);
1776-
}
1777-
#endif
17781763
}
17791764

17801765
private AsnWriter ExportEncryptedPkcs8PrivateKeyCore<TChar>(
@@ -1822,7 +1807,7 @@ private TResult ExportPkcs8PrivateKeyCallback<TResult>(ExportPkcs8PrivateKeyFunc
18221807
{
18231808
CryptoPool.Return(buffer);
18241809
size = checked(size * 2);
1825-
buffer = ArrayPool<byte>.Shared.Rent(size);
1810+
buffer = CryptoPool.Rent(size);
18261811
}
18271812

18281813
if (written < 0 || written > buffer.Length)

0 commit comments

Comments
 (0)