Skip to content

Commit f87f654

Browse files
committed
MLDSA+COSE
1 parent c8403e6 commit f87f654

33 files changed

+2489
-450
lines changed

src/libraries/Common/tests/System/Security/Cryptography/PlatformSupport.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ private static bool CheckIfVbsAvailable()
107107
}
108108
}
109109

110+
private static bool CheckIfRsaPssSupported()
111+
{
112+
if (PlatformDetection.IsBrowser)
113+
{
114+
// Browser doesn't support PSS or RSA at all.
115+
return false;
116+
}
117+
118+
using (RSA rsa = RSA.Create())
119+
{
120+
try
121+
{
122+
rsa.SignData(Array.Empty<byte>(), HashAlgorithmName.SHA256, RSASignaturePadding.Pss);
123+
}
124+
catch (CryptographicException)
125+
{
126+
return false;
127+
}
128+
}
129+
130+
return true;
131+
}
132+
110133
// Platforms that use Apple Cryptography
111134
internal const TestPlatforms AppleCrypto = TestPlatforms.OSX | TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst;
112135
internal const TestPlatforms MobileAppleCrypto = TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst;
@@ -129,5 +152,13 @@ private static bool CheckIfVbsAvailable()
129152

130153
private static bool? s_isVbsAvailable;
131154
internal static bool IsVbsAvailable => s_isVbsAvailable ??= CheckIfVbsAvailable();
155+
156+
private static bool? s_isRsaPssSupported;
157+
158+
/// <summary>
159+
/// Checks if the platform supports RSA-PSS signatures.
160+
/// This value is not suitable to check if RSA-PSS is supported in cert chains - see CertificateRequestChainTests.PlatformSupportsPss.
161+
/// </summary>
162+
internal static bool IsRsaPssSupported => s_isRsaPssSupported ??= CheckIfRsaPssSupported();
132163
}
133164
}

src/libraries/Microsoft.Bcl.Cryptography/src/Microsoft.Bcl.Cryptography.Forwards.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.X509Certificates.X509CertificateLoader))]
1313
#endif
1414
#if NET10_0_OR_GREATER
15+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.MLDsa))]
16+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.MLDsaAlgorithm))]
1517
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.MLKem))]
1618
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.MLKemAlgorithm))]
1719
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SlhDsa))]

src/libraries/Microsoft.Bcl.Cryptography/src/Microsoft.Bcl.Cryptography.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@
116116
<Link>Common\System\Security\Cryptography\Asn1\EncryptedPrivateKeyInfoAsn.xml.cs</Link>
117117
<DependentUpon>Common\System\Security\Cryptography\Asn1\EncryptedPrivateKeyInfoAsn.xml</DependentUpon>
118118
</Compile>
119+
<Compile Include="$(CommonPath)System\Security\Cryptography\Asn1\MLDsaPrivateKeyAsn.xml.cs">
120+
<Link>Common\System\Security\Cryptography\Asn1\MLDsaPrivateKeyAsn.xml.cs</Link>
121+
<DependentUpon>Common\System\Security\Cryptography\Asn1\MLDsaPrivateKeyAsn.xml</DependentUpon>
122+
</Compile>
123+
<Compile Include="$(CommonPath)System\Security\Cryptography\Asn1\MLDsaPrivateKeyBothAsn.xml.cs">
124+
<Link>Common\System\Security\Cryptography\Asn1\MLDsaPrivateKeyBothAsn.xml.cs</Link>
125+
<DependentUpon>Common\System\Security\Cryptography\Asn1\MLDsaPrivateKeyBothAsn.xml</DependentUpon>
126+
</Compile>
119127
<AsnXml Include="$(CommonPath)System\Security\Cryptography\Asn1\PBEParameter.xml">
120128
<Link>Common\System\Security\Cryptography\Asn1\PBEParameter.xml</Link>
121129
</AsnXml>
@@ -374,6 +382,14 @@
374382
Link="Common\System\Security\Cryptography\SlhDsaImplementation.cs" />
375383
<Compile Include="$(CommonPath)System\Security\Cryptography\SlhDsaImplementation.NotSupported.cs"
376384
Link="Common\System\Security\Cryptography\SlhDsaImplementation.NotSupported.cs" />
385+
<Compile Include="$(CommonPath)System\Security\Cryptography\MLDsa.cs"
386+
Link="Common\System\Security\Cryptography\MLDsa.cs" />
387+
<Compile Include="$(CommonPath)System\Security\Cryptography\MLDsaAlgorithm.cs"
388+
Link="Common\System\Security\Cryptography\MLDsaAlgorithm.cs" />
389+
<Compile Include="$(CommonPath)System\Security\Cryptography\MLDsaImplementation.cs"
390+
Link="Common\System\Security\Cryptography\MLDsaImplementation.cs" />
391+
<Compile Include="$(CommonPath)System\Security\Cryptography\MLDsaImplementation.NotSupported.cs"
392+
Link="Common\System\Security\Cryptography\MLDsaImplementation.NotSupported.cs" />
377393
</ItemGroup>
378394

379395
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">

src/libraries/Microsoft.Bcl.Cryptography/src/Resources/Strings.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@
156156
<data name="Cryptography_KemPkcs8KeyMismatch" xml:space="preserve">
157157
<value>The specified PKCS#8 key contains a seed that does not match the expanded key.</value>
158158
</data>
159+
<data name="Cryptography_KeyWrongSizeForAlgorithm" xml:space="preserve">
160+
<value>The specified key is not the correct size for the indicated algorithm.</value>
161+
</data>
162+
<data name="Cryptography_MLDsaPkcs8KeyMismatch" xml:space="preserve">
163+
<value>The specified PKCS#8 key contains a seed that does not match the expanded key.</value>
164+
</data>
159165
<data name="Cryptography_NotValidPublicOrPrivateKey" xml:space="preserve">
160166
<value>Key is not a valid public or private key.</value>
161167
</data>

src/libraries/Microsoft.Bcl.Cryptography/tests/Microsoft.Bcl.Cryptography.Tests.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@
128128
Link="CommonTest\System\Security\Cryptography\SP800108HmacCounterKdfTests.Helpers.cs" />
129129
<Compile Include="$(CommonTestPath)System\Security\Cryptography\SP800108HmacCounterKdfTests.ThreadSafety.cs"
130130
Link="CommonTest\System\Security\Cryptography\SP800108HmacCounterKdfTests.ThreadSafety.cs" />
131+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\MLDsa\MLDsaTestImplementation.cs"
132+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\MLDsa\MLDsaTestImplementation.cs" />
133+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\MLDsa\MLDsaTests.cs"
134+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\MLDsa\MLDsaTests.cs" />
135+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\MLDsa\MLDsaImplementationTests.cs"
136+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\MLDsa\MLDsaImplementationTests.cs" />
137+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\MLDsa\MLDsaTestsBase.cs"
138+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\MLDsa\MLDsaTestsBase.cs" />
139+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\MLDsa\MLDsaTestsData.cs"
140+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\MLDsa\MLDsaTestsData.cs" />
131141
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\SlhDsa\SlhDsaAlgorithmTests.cs"
132142
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\SlhDsa\SlhDsaAlgorithmTests.cs" />
133143
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\SlhDsa\SlhDsaContractTests.cs"

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ public void CopyTo(System.Collections.Generic.KeyValuePair<System.Security.Crypt
7373
public static bool operator ==(System.Security.Cryptography.Cose.CoseHeaderValue left, System.Security.Cryptography.Cose.CoseHeaderValue right) { throw null; }
7474
public static bool operator !=(System.Security.Cryptography.Cose.CoseHeaderValue left, System.Security.Cryptography.Cose.CoseHeaderValue right) { throw null; }
7575
}
76+
public sealed partial class CoseKey
77+
{
78+
internal CoseKey() { }
79+
public static System.Security.Cryptography.Cose.CoseKey FromKey(System.Security.Cryptography.ECDsa key, System.Security.Cryptography.HashAlgorithmName hashAlgorithm) { throw null; }
80+
[System.Diagnostics.CodeAnalysis.ExperimentalAttribute("SYSLIB5006")]
81+
public static System.Security.Cryptography.Cose.CoseKey FromKey(System.Security.Cryptography.MLDsa key) { throw null; }
82+
public static System.Security.Cryptography.Cose.CoseKey FromKey(System.Security.Cryptography.RSA key, System.Security.Cryptography.RSASignaturePadding signaturePadding, System.Security.Cryptography.HashAlgorithmName hashAlgorithm) { throw null; }
83+
}
7684
public abstract partial class CoseMessage
7785
{
7886
internal CoseMessage() { }
@@ -129,9 +137,13 @@ internal CoseSign1Message() { }
129137
public bool VerifyDetached(System.Security.Cryptography.AsymmetricAlgorithm key, byte[] detachedContent, byte[]? associatedData = null) { throw null; }
130138
public bool VerifyDetached(System.Security.Cryptography.AsymmetricAlgorithm key, System.IO.Stream detachedContent, System.ReadOnlySpan<byte> associatedData = default(System.ReadOnlySpan<byte>)) { throw null; }
131139
public bool VerifyDetached(System.Security.Cryptography.AsymmetricAlgorithm key, System.ReadOnlySpan<byte> detachedContent, System.ReadOnlySpan<byte> associatedData = default(System.ReadOnlySpan<byte>)) { throw null; }
140+
public bool VerifyDetached(System.Security.Cryptography.Cose.CoseKey key, System.IO.Stream detachedContent, System.ReadOnlySpan<byte> associatedData = default(System.ReadOnlySpan<byte>)) { throw null; }
141+
public bool VerifyDetached(System.Security.Cryptography.Cose.CoseKey key, System.ReadOnlySpan<byte> detachedContent, System.ReadOnlySpan<byte> associatedData = default(System.ReadOnlySpan<byte>)) { throw null; }
132142
public System.Threading.Tasks.Task<bool> VerifyDetachedAsync(System.Security.Cryptography.AsymmetricAlgorithm key, System.IO.Stream detachedContent, System.ReadOnlyMemory<byte> associatedData = default(System.ReadOnlyMemory<byte>), System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
143+
public System.Threading.Tasks.Task<bool> VerifyDetachedAsync(System.Security.Cryptography.Cose.CoseKey key, System.IO.Stream detachedContent, System.ReadOnlyMemory<byte> associatedData = default(System.ReadOnlyMemory<byte>), System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
133144
public bool VerifyEmbedded(System.Security.Cryptography.AsymmetricAlgorithm key, byte[]? associatedData = null) { throw null; }
134145
public bool VerifyEmbedded(System.Security.Cryptography.AsymmetricAlgorithm key, System.ReadOnlySpan<byte> associatedData) { throw null; }
146+
public bool VerifyEmbedded(System.Security.Cryptography.Cose.CoseKey key, System.ReadOnlySpan<byte> associatedData = default(System.ReadOnlySpan<byte>)) { throw null; }
135147
}
136148
public sealed partial class CoseSignature
137149
{
@@ -143,16 +155,22 @@ internal CoseSignature() { }
143155
public bool VerifyDetached(System.Security.Cryptography.AsymmetricAlgorithm key, byte[] detachedContent, byte[]? associatedData = null) { throw null; }
144156
public bool VerifyDetached(System.Security.Cryptography.AsymmetricAlgorithm key, System.IO.Stream detachedContent, System.ReadOnlySpan<byte> associatedData = default(System.ReadOnlySpan<byte>)) { throw null; }
145157
public bool VerifyDetached(System.Security.Cryptography.AsymmetricAlgorithm key, System.ReadOnlySpan<byte> detachedContent, System.ReadOnlySpan<byte> associatedData = default(System.ReadOnlySpan<byte>)) { throw null; }
158+
public bool VerifyDetached(System.Security.Cryptography.Cose.CoseKey key, System.IO.Stream detachedContent, System.ReadOnlySpan<byte> associatedData = default(System.ReadOnlySpan<byte>)) { throw null; }
159+
public bool VerifyDetached(System.Security.Cryptography.Cose.CoseKey key, System.ReadOnlySpan<byte> detachedContent, System.ReadOnlySpan<byte> associatedData = default(System.ReadOnlySpan<byte>)) { throw null; }
146160
public System.Threading.Tasks.Task<bool> VerifyDetachedAsync(System.Security.Cryptography.AsymmetricAlgorithm key, System.IO.Stream detachedContent, System.ReadOnlyMemory<byte> associatedData = default(System.ReadOnlyMemory<byte>), System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
161+
public System.Threading.Tasks.Task<bool> VerifyDetachedAsync(System.Security.Cryptography.Cose.CoseKey key, System.IO.Stream detachedContent, System.ReadOnlyMemory<byte> associatedData = default(System.ReadOnlyMemory<byte>), System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
147162
public bool VerifyEmbedded(System.Security.Cryptography.AsymmetricAlgorithm key, byte[]? associatedData = null) { throw null; }
148163
public bool VerifyEmbedded(System.Security.Cryptography.AsymmetricAlgorithm key, System.ReadOnlySpan<byte> associatedData) { throw null; }
164+
public bool VerifyEmbedded(System.Security.Cryptography.Cose.CoseKey key, System.ReadOnlySpan<byte> associatedData = default(System.ReadOnlySpan<byte>)) { throw null; }
149165
}
150166
public sealed partial class CoseSigner
151167
{
152168
public CoseSigner(System.Security.Cryptography.AsymmetricAlgorithm key, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, System.Security.Cryptography.Cose.CoseHeaderMap? protectedHeaders = null, System.Security.Cryptography.Cose.CoseHeaderMap? unprotectedHeaders = null) { }
169+
public CoseSigner(System.Security.Cryptography.Cose.CoseKey key, System.Security.Cryptography.Cose.CoseHeaderMap? protectedHeaders = null, System.Security.Cryptography.Cose.CoseHeaderMap? unprotectedHeaders = null) { }
153170
public CoseSigner(System.Security.Cryptography.RSA key, System.Security.Cryptography.RSASignaturePadding signaturePadding, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, System.Security.Cryptography.Cose.CoseHeaderMap? protectedHeaders = null, System.Security.Cryptography.Cose.CoseHeaderMap? unprotectedHeaders = null) { }
171+
public System.Security.Cryptography.Cose.CoseKey CoseKey { get { throw null; } }
154172
public System.Security.Cryptography.HashAlgorithmName HashAlgorithm { get { throw null; } }
155-
public System.Security.Cryptography.AsymmetricAlgorithm Key { get { throw null; } }
173+
public System.Security.Cryptography.AsymmetricAlgorithm? Key { get { throw null; } }
156174
public System.Security.Cryptography.Cose.CoseHeaderMap ProtectedHeaders { get { throw null; } }
157175
public System.Security.Cryptography.RSASignaturePadding? RSASignaturePadding { get { throw null; } }
158176
public System.Security.Cryptography.Cose.CoseHeaderMap UnprotectedHeaders { get { throw null; } }

src/libraries/System.Security.Cryptography.Cose/ref/System.Security.Cryptography.Cose.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@
77
<Compile Include="System.Security.Cryptography.Cose.cs" />
88
</ItemGroup>
99

10+
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
11+
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\ExperimentalAttribute.cs" />
12+
</ItemGroup>
13+
1014
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
1115
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
1216
</ItemGroup>
1317

1418
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
1519
<PackageReference Include="System.ValueTuple" Version="$(SystemValueTupleVersion)" />
1620
</ItemGroup>
21+
22+
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net10.0'))">
23+
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Bcl.Cryptography\src\Microsoft.Bcl.Cryptography.csproj" />
24+
</ItemGroup>
1725
</Project>
Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
@@ -9,31 +9,38 @@
99
<!-- TODO: Add package README file: https://github.com/dotnet/runtime/issues/99358 -->
1010
<EnableDefaultPackageReadmeFile>false</EnableDefaultPackageReadmeFile>
1111
</PropertyGroup>
12-
12+
1313
<ItemGroup>
14-
<Compile Include="$(CommonPath)System\HashCodeRandomization.cs"
15-
Link="Common\System\HashCodeRandomization.cs" />
16-
<Compile Include="$(CommonPath)System\Memory\PointerMemoryManager.cs"
17-
Link="Common\System\Memory\PointerMemoryManager.cs" />
18-
<Compile Include="$(CommonPath)System\Security\Cryptography\IncrementalHash.netfx.cs"
19-
Link="Common\System\Security\Cryptography\IncrementalHash.cs"
20-
Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'" />
21-
<Compile Include="$(LibrariesProjectRoot)System.Formats.Cbor\src\System\Formats\Cbor\CborInitialByte.cs"
22-
Link="System\Formats\Cbor\CborInitialByte.cs" />
14+
<Compile Include="$(CommonPath)System\Experimentals.cs" Link="Common\Experimentals.cs" />
15+
<Compile Include="$(CommonPath)System\HashCodeRandomization.cs" Link="Common\System\HashCodeRandomization.cs" />
16+
<Compile Include="$(CommonPath)System\Memory\PointerMemoryManager.cs" Link="Common\System\Memory\PointerMemoryManager.cs" />
17+
<Compile Include="$(LibrariesProjectRoot)System.Formats.Cbor\src\System\Formats\Cbor\CborInitialByte.cs" Link="System\Formats\Cbor\CborInitialByte.cs" />
2318

2419
<Compile Include="System\Security\Cryptography\Cose\CoseHeaderLabel.cs" />
2520
<Compile Include="System\Security\Cryptography\Cose\CoseHeaderMap.cs" />
2621
<Compile Include="System\Security\Cryptography\Cose\CoseHeaderValue.cs" />
2722
<Compile Include="System\Security\Cryptography\Cose\CoseHelpers.cs" />
23+
<Compile Include="System\Security\Cryptography\Cose\CoseAlgorithm.cs" />
24+
<Compile Include="System\Security\Cryptography\Cose\CoseKey.cs" />
2825
<Compile Include="System\Security\Cryptography\Cose\CoseMessage.cs" />
2926
<Compile Include="System\Security\Cryptography\Cose\CoseMultiSignMessage.cs" />
3027
<Compile Include="System\Security\Cryptography\Cose\CoseSign1Message.cs" />
3128
<Compile Include="System\Security\Cryptography\Cose\CoseSignature.cs" />
3229
<Compile Include="System\Security\Cryptography\Cose\CoseSigner.cs" />
30+
<Compile Include="System\Security\Cryptography\Cose\PureDataToBeSignedBuilder.cs" />
31+
<Compile Include="System\Security\Cryptography\Cose\HashToBeSignedBuilder.cs" />
3332
<Compile Include="System\Security\Cryptography\Cose\KeyType.cs" />
34-
<Compile Include="System\Security\Cryptography\Cose\KnownCoseAlgorithms.cs" />
3533
<Compile Include="System\Security\Cryptography\Cose\KnownHeaders.cs" />
3634
<Compile Include="System\Security\Cryptography\Cose\SigStructureContext.cs" />
35+
<Compile Include="System\Security\Cryptography\Cose\ToBeSignedBuilder.cs" />
36+
</ItemGroup>
37+
38+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
39+
<Compile Include="$(CommonPath)System\Security\Cryptography\IncrementalHash.netfx.cs" Link="Common\System\Security\Cryptography\IncrementalHash.cs" />
40+
</ItemGroup>
41+
42+
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
43+
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\ExperimentalAttribute.cs" />
3744
</ItemGroup>
3845

3946
<ItemGroup>
@@ -45,4 +52,8 @@
4552
<PackageReference Include="Microsoft.Bcl.HashCode" Version="$(MicrosoftBclHashCodeVersion)" />
4653
</ItemGroup>
4754

55+
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net10.0'))">
56+
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Bcl.Cryptography\src\Microsoft.Bcl.Cryptography.csproj" />
57+
</ItemGroup>
58+
4859
</Project>

0 commit comments

Comments
 (0)