Skip to content

Commit ef71bb7

Browse files
authored
Use output size helpers for cryptographic primitives (#82800)
1 parent 945c01f commit ef71bb7

File tree

3 files changed

+3
-11
lines changed

3 files changed

+3
-11
lines changed

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,7 @@ public override byte[] SignHash(byte[] hash)
2020
{
2121
ArgumentNullException.ThrowIfNull(hash);
2222

23-
int estimatedSize = KeySize switch
24-
{
25-
256 => 64,
26-
384 => 96,
27-
521 => 132,
28-
// If we got here, the range of legal key sizes for ECDsaCng was expanded and someone didn't update this switch.
29-
// Since it isn't a fatal error to miscalculate the estimatedSize, don't throw an exception. Just truck along.
30-
_ => KeySize / 4,
31-
};
23+
int estimatedSize = GetMaxSignatureSize(DSASignatureFormat.IeeeP1363FixedFieldConcatenation);
3224

3325
unsafe
3426
{

src/libraries/Common/src/System/Security/Cryptography/RSACng.EncryptDecrypt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private unsafe bool TryEncryptOrDecrypt(ReadOnlySpan<byte> data, Span<byte> dest
198198
// Now that the padding mode and information have been marshaled to their native counterparts, perform the encryption or decryption.
199199
private unsafe byte[] EncryptOrDecrypt(SafeNCryptKeyHandle key, ReadOnlySpan<byte> input, AsymmetricPaddingMode paddingMode, void* paddingInfo, bool encrypt)
200200
{
201-
int estimatedSize = KeySize / 8;
201+
int estimatedSize = GetMaxOutputSize();
202202
#if DEBUG
203203
estimatedSize = 2; // Make sure the NTE_BUFFER_TOO_SMALL scenario gets exercised.
204204
#endif

src/libraries/Common/src/System/Security/Cryptography/RSACng.SignVerify.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RS
5959
{
6060
unsafe
6161
{
62-
int estimatedSize = KeySize / 8;
62+
int estimatedSize = GetMaxOutputSize();
6363
switch (padding.Mode)
6464
{
6565
case RSASignaturePaddingMode.Pkcs1:

0 commit comments

Comments
 (0)