Skip to content

Commit 5697987

Browse files
authored
Implement {Try}HashData on asymmetric algorithms
Historically, the asymmetric algorithm base classes have had the hashing routines as abstract (or virtual+throw). Now the base classes provide an implementation for these methods, reducing the amount of redundant effort on the part of derived types.
1 parent 5705c98 commit 5697987

31 files changed

+355
-223
lines changed

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace System.Security.Cryptography
1111
{
12-
public sealed partial class DSACng : DSA
12+
public sealed partial class DSACng : DSA, IRuntimeAlgorithm
1313
{
1414
/// <summary>
1515
/// Create a DSACng algorithm with a random 2048 bit key pair.
@@ -45,16 +45,6 @@ public override KeySizes[] LegalKeySizes
4545
public override string SignatureAlgorithm => "DSA";
4646
public override string? KeyExchangeAlgorithm => null;
4747

48-
// Need to override since base methods throw a "override me" exception: makes SignData/VerifyData function.
49-
protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>
50-
HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan<byte>(data, offset, count));
51-
52-
protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
53-
HashOneShotHelpers.HashData(hashAlgorithm, data);
54-
55-
protected override bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
56-
HashOneShotHelpers.TryHashData(hashAlgorithm, source, destination, out bytesWritten);
57-
5848
private void ForceSetKeySize(int newKeySize)
5949
{
6050
// Our LegalKeySizes value stores the values that we encoded as being the correct

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace System.Security.Cryptography
1111
{
12-
public sealed partial class DSAOpenSsl : DSA
12+
public sealed partial class DSAOpenSsl : DSA, IRuntimeAlgorithm
1313
{
1414
// The biggest key allowed by FIPS 186-4 has N=256 (bit), which
1515
// maximally produces a 72-byte DER signature.
@@ -193,23 +193,6 @@ private SafeDsaHandle GenerateKey()
193193
return key;
194194
}
195195

196-
protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
197-
{
198-
// we're sealed and the base should have checked this already
199-
Debug.Assert(data != null);
200-
Debug.Assert(offset >= 0 && offset <= data.Length);
201-
Debug.Assert(count >= 0 && count <= data.Length);
202-
Debug.Assert(!string.IsNullOrEmpty(hashAlgorithm.Name));
203-
204-
return HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan<byte>(data, offset, count));
205-
}
206-
207-
protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
208-
HashOneShotHelpers.HashData(hashAlgorithm, data);
209-
210-
protected override bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
211-
HashOneShotHelpers.TryHashData(hashAlgorithm, data, destination, out bytesWritten);
212-
213196
public override byte[] CreateSignature(byte[] rgbHash!!)
214197
{
215198
SafeDsaHandle key = GetKey();

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace System.Security.Cryptography
99
{
1010
internal static partial class DSAImplementation
1111
{
12-
public sealed partial class DSASecurityTransforms : DSA
12+
public sealed partial class DSASecurityTransforms : DSA, IRuntimeAlgorithm
1313
{
1414
private SecKeyPair? _keys;
1515
private bool _disposed;
@@ -118,12 +118,6 @@ protected override byte[] HashData(byte[] data, int offset, int count, HashAlgor
118118
return HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan<byte>(data, offset, count));
119119
}
120120

121-
protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
122-
HashOneShotHelpers.HashData(hashAlgorithm, data);
123-
124-
protected override bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
125-
HashOneShotHelpers.TryHashData(hashAlgorithm, data, destination, out bytesWritten);
126-
127121
protected override void Dispose(bool disposing)
128122
{
129123
if (disposing)

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Security.Cryptography
1010
{
1111
internal static partial class ECDsaImplementation
1212
{
13-
public sealed partial class ECDsaAndroid : ECDsa
13+
public sealed partial class ECDsaAndroid : ECDsa, IRuntimeAlgorithm
1414
{
1515
// secp521r1 maxes out at 139 bytes, so 256 should always be enough
1616
private const int SignatureStackBufSize = 256;
@@ -240,15 +240,6 @@ protected override bool VerifyHashCore(
240240
return verifyResult == 1;
241241
}
242242

243-
protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>
244-
HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan<byte>(data, offset, count));
245-
246-
protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
247-
HashOneShotHelpers.HashData(hashAlgorithm, data);
248-
249-
protected override bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
250-
HashOneShotHelpers.TryHashData(hashAlgorithm, data, destination, out bytesWritten);
251-
252243
protected override void Dispose(bool disposing)
253244
{
254245
if (disposing)

src/libraries/Common/src/System/Security/Cryptography/ECDsaCng.HashData.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace System.Security.Cryptography
77
{
8-
public sealed partial class ECDsaCng : ECDsa
8+
public sealed partial class ECDsaCng : ECDsa, IRuntimeAlgorithm
99
{
1010
/// <summary>
1111
/// Create an ECDsaCng algorithm with a named curve.

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace System.Security.Cryptography
1111
{
12-
public sealed partial class ECDsaOpenSsl : ECDsa
12+
public sealed partial class ECDsaOpenSsl : ECDsa, IRuntimeAlgorithm
1313
{
1414
// secp521r1 maxes out at 139 bytes, so 256 should always be enough
1515
private const int SignatureStackBufSize = 256;
@@ -250,15 +250,6 @@ protected override bool VerifyHashCore(
250250
return verifyResult == 1;
251251
}
252252

253-
protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>
254-
HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan<byte>(data, offset, count));
255-
256-
protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
257-
HashOneShotHelpers.HashData(hashAlgorithm, data);
258-
259-
protected override bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
260-
HashOneShotHelpers.TryHashData(hashAlgorithm, data, destination, out bytesWritten);
261-
262253
protected override void Dispose(bool disposing)
263254
{
264255
if (disposing)

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace System.Security.Cryptography
99
{
1010
internal static partial class ECDsaImplementation
1111
{
12-
public sealed partial class ECDsaSecurityTransforms : ECDsa
12+
public sealed partial class ECDsaSecurityTransforms : ECDsa, IRuntimeAlgorithm
1313
{
1414
private readonly EccSecurityTransforms _ecc = new EccSecurityTransforms(nameof(ECDsa));
1515

@@ -135,15 +135,6 @@ public override bool VerifyHash(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> sign
135135
Interop.AppleCrypto.PAL_SignatureAlgorithm.EC);
136136
}
137137

138-
protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>
139-
HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan<byte>(data, offset, count));
140-
141-
protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
142-
HashOneShotHelpers.HashData(hashAlgorithm, data);
143-
144-
protected override bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
145-
HashOneShotHelpers.TryHashData(hashAlgorithm, source, destination, out bytesWritten);
146-
147138
private void ThrowIfDisposed()
148139
{
149140
_ecc.ThrowIfDisposed();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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.Diagnostics;
5+
using System.Formats.Asn1;
6+
using System.Numerics;
7+
8+
namespace System.Security.Cryptography
9+
{
10+
// Marker interface that goes on runtime-provided algorithms.
11+
internal interface IRuntimeAlgorithm
12+
{
13+
}
14+
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace System.Security.Cryptography
1313
{
1414
internal static partial class RSAImplementation
1515
{
16-
public sealed partial class RSAAndroid : RSA
16+
public sealed partial class RSAAndroid : RSA, IRuntimeAlgorithm
1717
{
1818
private const int BitsPerByte = 8;
1919

@@ -647,15 +647,6 @@ private SafeRsaHandle GenerateKey()
647647
return key;
648648
}
649649

650-
protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>
651-
HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan<byte>(data, offset, count));
652-
653-
protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
654-
HashOneShotHelpers.HashData(hashAlgorithm, data);
655-
656-
protected override bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
657-
HashOneShotHelpers.TryHashData(hashAlgorithm, data, destination, out bytesWritten);
658-
659650
public override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
660651
{
661652
ArgumentNullException.ThrowIfNull(hash);

0 commit comments

Comments
 (0)