Skip to content

Commit 9fd7c39

Browse files
authored
Fix Equals and GetHashCode for cryptographic wrappers
1 parent 1a8ccb6 commit 9fd7c39

File tree

12 files changed

+194
-16
lines changed

12 files changed

+194
-16
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Xunit;
5+
6+
namespace System.Security.Cryptography.Dsa.Tests
7+
{
8+
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst, "Not supported on Browser/iOS/tvOS/MacCatalyst")]
9+
public partial class DSAFactoryTests
10+
{
11+
[Fact]
12+
public static void DSACreateDefault_Equals_SameInstance()
13+
{
14+
using DSA dsa = DSAFactory.Create();
15+
dsa.ImportParameters(DSATestData.GetDSA1024Params());
16+
AssertExtensions.TrueExpression(dsa.Equals(dsa));
17+
}
18+
19+
[Fact]
20+
public static void DSACreateKeySize_Equals_SameInstance()
21+
{
22+
using DSA dsa = DSAFactory.Create(1024);
23+
AssertExtensions.TrueExpression(dsa.Equals(dsa));
24+
}
25+
26+
[Fact]
27+
public static void DsaCreate_Equals_DifferentInstance_FalseForSameKeyMaterial()
28+
{
29+
using DSA dsa1 = DSAFactory.Create();
30+
using DSA dsa2 = DSAFactory.Create();
31+
dsa1.ImportParameters(DSATestData.GetDSA1024Params());
32+
dsa2.ImportParameters(DSATestData.GetDSA1024Params());
33+
AssertExtensions.FalseExpression(dsa1.Equals(dsa2));
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Security.Cryptography.Tests;
5+
using Xunit;
6+
7+
namespace System.Security.Cryptography.EcDiffieHellman.Tests
8+
{
9+
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
10+
public static class ECDiffieHellmanFactoryTests
11+
{
12+
[Fact]
13+
public static void ECDiffieHellmanCreateDefault_Equals_SameInstance()
14+
{
15+
using ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create();
16+
AssertExtensions.TrueExpression(ecdh.Equals(ecdh));
17+
}
18+
19+
[Fact]
20+
public static void ECDiffieHellmanCreateKeySize_Equals_SameInstance()
21+
{
22+
using ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create(256);
23+
AssertExtensions.TrueExpression(ecdh.Equals(ecdh));
24+
}
25+
26+
[Fact]
27+
public static void ECDiffieHellmanCreateKeySize_Equals_DifferentInstance_FalseForSameKeyMaterial()
28+
{
29+
using ECDiffieHellman ecdh1 = ECDiffieHellmanFactory.Create();
30+
using ECDiffieHellman ecdh2 = ECDiffieHellmanFactory.Create();
31+
ecdh1.ImportParameters(EccTestData.GetNistP256ReferenceKey());
32+
ecdh2.ImportParameters(EccTestData.GetNistP256ReferenceKey());
33+
AssertExtensions.FalseExpression(ecdh1.Equals(ecdh2));
34+
}
35+
36+
#if NET
37+
[Fact]
38+
public static void ECDiffieHellmanCreateCurve_Equals_SameInstance()
39+
{
40+
using ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create(ECCurve.NamedCurves.nistP256);
41+
AssertExtensions.TrueExpression(ecdh.Equals(ecdh));
42+
}
43+
#endif
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Security.Cryptography.Tests;
5+
using Xunit;
6+
7+
namespace System.Security.Cryptography.EcDsa.Tests
8+
{
9+
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
10+
public static class ECDsaFactoryTests
11+
{
12+
[Fact]
13+
public static void ECDsaCreateDefault_Equals_SameInstance()
14+
{
15+
using ECDsa ecdsa = ECDsaFactory.Create();
16+
AssertExtensions.TrueExpression(ecdsa.Equals(ecdsa));
17+
}
18+
19+
[Fact]
20+
public static void ECDsaCreateKeySize_Equals_SameInstance()
21+
{
22+
using ECDsa ecdsa = ECDsaFactory.Create(256);
23+
AssertExtensions.TrueExpression(ecdsa.Equals(ecdsa));
24+
}
25+
26+
[Fact]
27+
public static void ECDsaCreateKeySize_Equals_DifferentInstance_FalseForSameKeyMaterial()
28+
{
29+
using ECDsa ecdsa1 = ECDsaFactory.Create();
30+
using ECDsa ecdsa2 = ECDsaFactory.Create();
31+
ecdsa1.ImportParameters(EccTestData.GetNistP256ReferenceKey());
32+
ecdsa2.ImportParameters(EccTestData.GetNistP256ReferenceKey());
33+
AssertExtensions.FalseExpression(ecdsa1.Equals(ecdsa2));
34+
}
35+
36+
#if NET
37+
[Fact]
38+
public static void ECDsaCreateCurve_Equals_SameInstance()
39+
{
40+
using ECDsa ecdsa = ECDsaFactory.Create(ECCurve.NamedCurves.nistP256);
41+
AssertExtensions.TrueExpression(ecdsa.Equals(ecdsa));
42+
}
43+
#endif
44+
}
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Xunit;
5+
6+
namespace System.Security.Cryptography.Rsa.Tests
7+
{
8+
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
9+
public static class RSAFactoryTests
10+
{
11+
[Fact]
12+
public static void RSACreateDefault_Equals_SameInstance()
13+
{
14+
using RSA rsa = RSAFactory.Create();
15+
AssertExtensions.TrueExpression(rsa.Equals(rsa));
16+
}
17+
18+
[Fact]
19+
public static void RSACreateKeySize_Equals_SameInstance()
20+
{
21+
using RSA rsa = RSAFactory.Create(2048);
22+
AssertExtensions.TrueExpression(rsa.Equals(rsa));
23+
}
24+
25+
[Fact]
26+
public static void RSACreateParameters_Equals_SameInstance()
27+
{
28+
using RSA rsa = RSAFactory.Create(TestData.RSA2048Params);
29+
AssertExtensions.TrueExpression(rsa.Equals(rsa));
30+
}
31+
32+
[Fact]
33+
public static void RSACreateParameters_Equals_DifferentInstance_FalseForSameKeyMaterial()
34+
{
35+
using RSA rsa1 = RSAFactory.Create(TestData.RSA2048Params);
36+
using RSA rsa2 = RSAFactory.Create(TestData.RSA2048Params);
37+
AssertExtensions.FalseExpression(rsa1.Equals(rsa2));
38+
}
39+
}
40+
}

src/libraries/System.Security.Cryptography.Cng/tests/System.Security.Cryptography.Cng.Tests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
<Compile Include="ECDiffieHellmanCngProvider.cs" />
3838
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDiffieHellman\ECDiffieHellmanFactory.cs"
3939
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\ECDiffieHellman\ECDiffieHellmanFactory.cs" />
40+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDiffieHellman\ECDiffieHellmanFactoryTests.cs"
41+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\ECDiffieHellman\ECDiffieHellmanFactoryTests.cs" />
4042
<Compile Include="$(CommonTestPath)System\Security\Cryptography\ByteUtils.cs"
4143
Link="CommonTest\System\Security\Cryptography\ByteUtils.cs" />
4244
<Compile Include="$(CommonTestPath)System\Security\Cryptography\CngKeyWrapper.cs"
@@ -54,6 +56,8 @@
5456
<Compile Include="ECDsaCngProvider.cs" />
5557
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaFactory.cs"
5658
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaFactory.cs" />
59+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaFactoryTests.cs"
60+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaFactoryTests.cs" />
5761
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaImportExport.cs"
5862
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaImportExport.cs" />
5963
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaStub.cs"
@@ -73,6 +77,8 @@
7377
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\KeyGeneration.cs" />
7478
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\RSA\RSAFactory.cs"
7579
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\RSAFactory.cs" />
80+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\RSA\RSAFactoryTests.cs"
81+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\RSAFactoryTests.cs" />
7682
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\RSA\RSATestHelpers.cs"
7783
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\RSATestHelpers.cs" />
7884
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\RSA\SignVerify.cs"
@@ -93,6 +99,8 @@
9399
Link="CommonTest\System\Security\Cryptography\AsymmetricSignatureFormatter.cs" />
94100
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\DSA\DSAFactory.cs"
95101
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\DSA\DSAFactory.cs" />
102+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\DSA\DSAFactoryTests.cs"
103+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\DSA\DSAFactoryTests.cs" />
96104
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\DSA\DsaFamilySignatureFormatTests.cs"
97105
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\DSA\DsaFamilySignatureFormatTests.cs" />
98106
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\DSA\DSAImportExport.cs"

src/libraries/System.Security.Cryptography.Csp/tests/System.Security.Cryptography.Csp.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\KeyGeneration.cs" />
3333
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\RSA\RSAFactory.cs"
3434
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\RSAFactory.cs" />
35+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\RSA\RSAFactoryTests.cs"
36+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\RSAFactoryTests.cs" />
3537
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\RSA\RSATestHelpers.cs"
3638
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\RSATestHelpers.cs" />
3739
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\RSA\SignVerify.cs"
@@ -62,6 +64,8 @@
6264
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\DES\DesTests.cs" />
6365
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\DSA\DSAFactory.cs"
6466
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\DSA\DSAFactory.cs" />
67+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\DSA\DSAFactoryTests.cs"
68+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\DSA\DSAFactoryTests.cs" />
6569
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\DSA\DsaFamilySignatureFormatTests.cs"
6670
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\DSA\DsaFamilySignatureFormatTests.cs" />
6771
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\DSA\DSAImportExport.cs"

src/libraries/System.Security.Cryptography.OpenSsl/tests/System.Security.Cryptography.OpenSsl.Tests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\EC\EccTestData.cs" />
2929
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDiffieHellman\ECDiffieHellmanFactory.cs"
3030
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\ECDiffieHellman\ECDiffieHellmanFactory.cs" />
31+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDiffieHellman\ECDiffieHellmanFactoryTests.cs"
32+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\ECDiffieHellman\ECDiffieHellmanFactoryTests.cs" />
3133
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDiffieHellman\ECDiffieHellmanTests.cs"
3234
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\ECDiffieHellman\ECDiffieHellmanTests.cs" />
3335
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDiffieHellman\ECDiffieHellmanTests.Hash.cs"
@@ -46,6 +48,8 @@
4648
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\ECDiffieHellman\ECDiffieHellmanTests.Xml.cs" />
4749
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaFactory.cs"
4850
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaFactory.cs" />
51+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaFactoryTests.cs"
52+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaFactoryTests.cs" />
4953
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaImportExport.cs"
5054
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaImportExport.cs" />
5155
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\ECDsa\ECDsaSignatureFormatTests.cs"
@@ -66,6 +70,8 @@
6670
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\KeyGeneration.cs" />
6771
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\RSA\RSAFactory.cs"
6872
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\RSAFactory.cs" />
73+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\RSA\RSAFactoryTests.cs"
74+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\RSAFactoryTests.cs" />
6975
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\RSA\RSATestHelpers.cs"
7076
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\RSATestHelpers.cs" />
7177
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\RSA\SignVerify.cs"
@@ -85,6 +91,8 @@
8591
Link="CommonTest\System\Security\Cryptography\AsymmetricSignatureFormatter.cs" />
8692
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\DSA\DSAFactory.cs"
8793
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\DSA\DSAFactory.cs" />
94+
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\DSA\DSAFactoryTests.cs"
95+
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\DSA\DSAFactoryTests.cs" />
8896
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\DSA\DsaFamilySignatureFormatTests.cs"
8997
Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\DSA\DsaFamilySignatureFormatTests.cs" />
9098
<Compile Include="$(CommonTestPath)System\Security\Cryptography\AlgorithmImplementations\DSA\DSAImportExport.cs"

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ public override byte[] ExportEncryptedPkcs8PrivateKey(
150150

151151
public override byte[] ExportSubjectPublicKeyInfo() => _wrapped.ExportSubjectPublicKeyInfo();
152152

153-
public override bool Equals(object? obj) => _wrapped.Equals(obj);
154-
155-
public override int GetHashCode() => _wrapped.GetHashCode();
156-
157153
public override string ToString() => _wrapped.ToString()!;
158154

159155
protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,6 @@ public override byte[] ExportEncryptedPkcs8PrivateKey(
152152

153153
public override byte[] ExportSubjectPublicKeyInfo() => _wrapped.ExportSubjectPublicKeyInfo();
154154

155-
public override bool Equals(object? obj) => _wrapped.Equals(obj);
156-
157-
public override int GetHashCode() => _wrapped.GetHashCode();
158-
159155
public override string ToString() => _wrapped.ToString()!;
160156

161157
private static ECDiffieHellmanPublicKey Unwrap(ECDiffieHellmanPublicKey otherPartyPublicKey)

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,6 @@ public override bool TrySignHash(ReadOnlySpan<byte> hash, Span<byte> destination
175175
public override bool VerifyHash(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> signature) =>
176176
_wrapped.VerifyHash(hash, signature);
177177

178-
public override bool Equals(object? obj) => _wrapped.Equals(obj);
179-
180-
public override int GetHashCode() => _wrapped.GetHashCode();
181-
182178
public override string ToString() => _wrapped.ToString()!;
183179

184180
protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>

0 commit comments

Comments
 (0)