|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 |
| -using System; |
5 |
| -using System.Buffers; |
6 | 4 | using System.Diagnostics;
|
7 | 5 | using System.Diagnostics.CodeAnalysis;
|
8 | 6 | using System.Formats.Asn1;
|
@@ -1173,25 +1171,20 @@ public static MLKem ImportSubjectPublicKeyInfo(ReadOnlySpan<byte> source)
|
1173 | 1171 | ThrowIfTrailingData(source);
|
1174 | 1172 | ThrowIfNotSupported();
|
1175 | 1173 |
|
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; |
1186 | 1177 |
|
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); |
1191 | 1181 |
|
1192 |
| - return MLKemImplementation.ImportEncapsulationKeyImpl(algorithm, subjectPublicKey); |
1193 |
| - } |
| 1182 | + if (key.Length != algorithm.EncapsulationKeySizeInBytes) |
| 1183 | + { |
| 1184 | + throw new CryptographicException(SR.Argument_KemInvalidEncapsulationKeyLength); |
1194 | 1185 | }
|
| 1186 | + |
| 1187 | + kem = MLKemImplementation.ImportEncapsulationKeyImpl(algorithm, key.Span); |
1195 | 1188 | }
|
1196 | 1189 | }
|
1197 | 1190 |
|
@@ -1669,9 +1662,8 @@ private protected static void ThrowIfNotSupported()
|
1669 | 1662 |
|
1670 | 1663 | private static MLKemAlgorithm GetAlgorithmIdentifier(ref readonly AlgorithmIdentifierAsn identifier)
|
1671 | 1664 | {
|
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."); |
1675 | 1667 |
|
1676 | 1668 | if (identifier.Parameters.HasValue)
|
1677 | 1669 | {
|
@@ -1767,14 +1759,7 @@ private static void ThrowIfTrailingData(ReadOnlySpan<byte> data)
|
1767 | 1759 |
|
1768 | 1760 | private protected void ThrowIfDisposed()
|
1769 | 1761 | {
|
1770 |
| -#if NET |
1771 | 1762 | ObjectDisposedException.ThrowIf(_disposed, typeof(MLKem));
|
1772 |
| -#else |
1773 |
| - if (_disposed) |
1774 |
| - { |
1775 |
| - throw new ObjectDisposedException(typeof(MLKem).FullName); |
1776 |
| - } |
1777 |
| -#endif |
1778 | 1763 | }
|
1779 | 1764 |
|
1780 | 1765 | private AsnWriter ExportEncryptedPkcs8PrivateKeyCore<TChar>(
|
@@ -1822,7 +1807,7 @@ private TResult ExportPkcs8PrivateKeyCallback<TResult>(ExportPkcs8PrivateKeyFunc
|
1822 | 1807 | {
|
1823 | 1808 | CryptoPool.Return(buffer);
|
1824 | 1809 | size = checked(size * 2);
|
1825 |
| - buffer = ArrayPool<byte>.Shared.Rent(size); |
| 1810 | + buffer = CryptoPool.Rent(size); |
1826 | 1811 | }
|
1827 | 1812 |
|
1828 | 1813 | if (written < 0 || written > buffer.Length)
|
|
0 commit comments