Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit c360ba2

Browse files
authored
Normalize names of span inputs in crypto API
The asymmetric types operate on either hashed input, or unprocessed input. The existing API tends to call hashed input "hash" (or "rgbHash" for older API), and unprocessed input "data" (or "rgbData"). This change modifies the new (ReadOnly)Span-based methods to use "data" and "hash" (as appropriate) instead of "source". Particularly because the hash-based methods in DSA do not contain the word Hash, making "source" for CreateSignature ambiguous. In the cases where the existing parameter was named "rgbHash" (et al) the "rgb" was dropped in the (ReadOnly)Span variant, including in the cases where the (ReadOnly)Span variant is a proper overload.
1 parent cac1f05 commit c360ba2

File tree

15 files changed

+117
-117
lines changed

15 files changed

+117
-117
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,21 @@ public override byte[] CreateSignature(byte[] rgbHash)
4545
}
4646
}
4747

48-
public override unsafe bool TryCreateSignature(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
48+
public override unsafe bool TryCreateSignature(ReadOnlySpan<byte> hash, Span<byte> destination, out int bytesWritten)
4949
{
50-
byte[] arrayToReturnToArrayPool = AdjustHashSizeIfNecessaryWithArrayPool(ref source);
50+
byte[] arrayToReturnToArrayPool = AdjustHashSizeIfNecessaryWithArrayPool(ref hash);
5151
try
5252
{
5353
using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle())
5454
{
55-
return CngCommon.TrySignHash(keyHandle, source, destination, AsymmetricPaddingMode.None, null, out bytesWritten);
55+
return CngCommon.TrySignHash(keyHandle, hash, destination, AsymmetricPaddingMode.None, null, out bytesWritten);
5656
}
5757
}
5858
finally
5959
{
6060
if (arrayToReturnToArrayPool != null)
6161
{
62-
Array.Clear(arrayToReturnToArrayPool, 0, source.Length);
62+
Array.Clear(arrayToReturnToArrayPool, 0, hash.Length);
6363
ArrayPool<byte>.Shared.Return(arrayToReturnToArrayPool);
6464
}
6565
}
@@ -79,24 +79,24 @@ public override bool VerifySignature(byte[] rgbHash, byte[] rgbSignature)
7979
return VerifySignature((ReadOnlySpan<byte>)rgbHash, (ReadOnlySpan<byte>)rgbSignature);
8080
}
8181

82-
public override bool VerifySignature(ReadOnlySpan<byte> rgbHash, ReadOnlySpan<byte> rgbSignature)
82+
public override bool VerifySignature(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> signature)
8383
{
84-
byte[] arrayToReturnToArrayPool = AdjustHashSizeIfNecessaryWithArrayPool(ref rgbHash);
84+
byte[] arrayToReturnToArrayPool = AdjustHashSizeIfNecessaryWithArrayPool(ref hash);
8585
try
8686
{
8787
using (SafeNCryptKeyHandle keyHandle = GetDuplicatedKeyHandle())
8888
{
8989
unsafe
9090
{
91-
return CngCommon.VerifyHash(keyHandle, rgbHash, rgbSignature, AsymmetricPaddingMode.None, null);
91+
return CngCommon.VerifyHash(keyHandle, hash, signature, AsymmetricPaddingMode.None, null);
9292
}
9393
}
9494
}
9595
finally
9696
{
9797
if (arrayToReturnToArrayPool != null)
9898
{
99-
Array.Clear(arrayToReturnToArrayPool, 0, rgbHash.Length);
99+
Array.Clear(arrayToReturnToArrayPool, 0, hash.Length);
100100
ArrayPool<byte>.Shared.Return(arrayToReturnToArrayPool);
101101
}
102102
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ protected override byte[] HashData(byte[] data, int offset, int count, HashAlgor
180180
protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
181181
AsymmetricAlgorithmHelpers.HashData(data, hashAlgorithm);
182182

183-
protected override bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
184-
AsymmetricAlgorithmHelpers.TryHashData(source, destination, hashAlgorithm, out bytesWritten);
183+
protected override bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
184+
AsymmetricAlgorithmHelpers.TryHashData(data, destination, hashAlgorithm, out bytesWritten);
185185

186186
public override byte[] CreateSignature(byte[] rgbHash)
187187
{
@@ -216,15 +216,15 @@ public override byte[] CreateSignature(byte[] rgbHash)
216216
}
217217
}
218218

219-
public override bool TryCreateSignature(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
219+
public override bool TryCreateSignature(ReadOnlySpan<byte> hash, Span<byte> destination, out int bytesWritten)
220220
{
221221
byte[] converted;
222222
SafeDsaHandle key = _key.Value;
223223
int signatureSize = Interop.Crypto.DsaEncodedSignatureSize(key);
224224
byte[] signature = ArrayPool<byte>.Shared.Rent(signatureSize);
225225
try
226226
{
227-
bool success = Interop.Crypto.DsaSign(key, source, source.Length, new Span<byte>(signature, 0, signatureSize), out signatureSize);
227+
bool success = Interop.Crypto.DsaSign(key, hash, hash.Length, new Span<byte>(signature, 0, signatureSize), out signatureSize);
228228
if (!success)
229229
{
230230
throw Interop.Crypto.CreateOpenSslCryptographicException();
@@ -269,20 +269,20 @@ public override bool VerifySignature(byte[] rgbHash, byte[] rgbSignature)
269269
return VerifySignature((ReadOnlySpan<byte>)rgbHash, (ReadOnlySpan<byte>)rgbSignature);
270270
}
271271

272-
public override bool VerifySignature(ReadOnlySpan<byte> rgbHash, ReadOnlySpan<byte> rgbSignature)
272+
public override bool VerifySignature(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> signature)
273273
{
274274
SafeDsaHandle key = _key.Value;
275275

276276
int expectedSignatureBytes = Interop.Crypto.DsaSignatureFieldSize(key) * 2;
277-
if (rgbSignature.Length != expectedSignatureBytes)
277+
if (signature.Length != expectedSignatureBytes)
278278
{
279279
// The input isn't of the right length (assuming no DER), so we can't sensibly re-encode it with DER.
280280
return false;
281281
}
282282

283-
byte[] openSslFormat = AsymmetricAlgorithmHelpers.ConvertIeee1363ToDer(rgbSignature);
283+
byte[] openSslFormat = AsymmetricAlgorithmHelpers.ConvertIeee1363ToDer(signature);
284284

285-
return Interop.Crypto.DsaVerify(key, rgbHash, rgbHash.Length, openSslFormat, openSslFormat.Length);
285+
return Interop.Crypto.DsaVerify(key, hash, hash.Length, openSslFormat, openSslFormat.Length);
286286
}
287287

288288
private void SetKey(SafeDsaHandle newKey)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ protected override byte[] HashData(byte[] data, int offset, int count, HashAlgor
235235
protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
236236
AsymmetricAlgorithmHelpers.HashData(data, hashAlgorithm);
237237

238-
protected override bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
239-
AsymmetricAlgorithmHelpers.TryHashData(source, destination, hashAlgorithm, out bytesWritten);
238+
protected override bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
239+
AsymmetricAlgorithmHelpers.TryHashData(data, destination, hashAlgorithm, out bytesWritten);
240240

241241
protected override void Dispose(bool disposing)
242242
{

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public override byte[] SignHash(byte[] hash)
8989
return converted;
9090
}
9191

92-
public override bool TrySignHash(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
92+
public override bool TrySignHash(ReadOnlySpan<byte> hash, Span<byte> destination, out int bytesWritten)
9393
{
9494
SafeEcKeyHandle key = _key.Value;
9595

@@ -98,7 +98,7 @@ public override bool TrySignHash(ReadOnlySpan<byte> source, Span<byte> destinati
9898
byte[] signature = ArrayPool<byte>.Shared.Rent(signatureLength);
9999
try
100100
{
101-
if (!Interop.Crypto.EcDsaSign(source, source.Length, new Span<byte>(signature, 0, signatureLength), ref signatureLength, key))
101+
if (!Interop.Crypto.EcDsaSign(hash, hash.Length, new Span<byte>(signature, 0, signatureLength), ref signatureLength, key))
102102
{
103103
throw Interop.Crypto.CreateOpenSslCryptographicException();
104104
}
@@ -159,8 +159,8 @@ protected override byte[] HashData(byte[] data, int offset, int count, HashAlgor
159159
protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
160160
AsymmetricAlgorithmHelpers.HashData(data, hashAlgorithm);
161161

162-
protected override bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
163-
AsymmetricAlgorithmHelpers.TryHashData(source, destination, hashAlgorithm, out bytesWritten);
162+
protected override bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
163+
AsymmetricAlgorithmHelpers.TryHashData(data, destination, hashAlgorithm, out bytesWritten);
164164

165165
protected override void Dispose(bool disposing)
166166
{

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public override unsafe byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
2727
EncryptOrDecrypt(data, padding, encrypt: false);
2828

2929
/// <summary>Encrypts data using the public key.</summary>
30-
public override bool TryEncrypt(ReadOnlySpan<byte> source, Span<byte> destination, RSAEncryptionPadding padding, out int bytesWritten) =>
31-
TryEncryptOrDecrypt(source, destination, padding, encrypt: true, bytesWritten: out bytesWritten);
30+
public override bool TryEncrypt(ReadOnlySpan<byte> data, Span<byte> destination, RSAEncryptionPadding padding, out int bytesWritten) =>
31+
TryEncryptOrDecrypt(data, destination, padding, encrypt: true, bytesWritten: out bytesWritten);
3232

3333
/// <summary>Decrypts data using the private key.</summary>
34-
public override bool TryDecrypt(ReadOnlySpan<byte> source, Span<byte> destination, RSAEncryptionPadding padding, out int bytesWritten) =>
35-
TryEncryptOrDecrypt(source, destination, padding, encrypt: false, bytesWritten: out bytesWritten);
34+
public override bool TryDecrypt(ReadOnlySpan<byte> data, Span<byte> destination, RSAEncryptionPadding padding, out int bytesWritten) =>
35+
TryEncryptOrDecrypt(data, destination, padding, encrypt: false, bytesWritten: out bytesWritten);
3636

3737
// Conveniently, Encrypt() and Decrypt() are identical save for the actual P/Invoke call to CNG. Thus, both
3838
// array-based APIs invoke this common helper with the "encrypt" parameter determining whether encryption or decryption is done.
@@ -81,7 +81,7 @@ private unsafe byte[] EncryptOrDecrypt(byte[] data, RSAEncryptionPadding padding
8181

8282
// Conveniently, Encrypt() and Decrypt() are identical save for the actual P/Invoke call to CNG. Thus, both
8383
// span-based APIs invoke this common helper with the "encrypt" parameter determining whether encryption or decryption is done.
84-
private unsafe bool TryEncryptOrDecrypt(ReadOnlySpan<byte> source, Span<byte> destination, RSAEncryptionPadding padding, bool encrypt, out int bytesWritten)
84+
private unsafe bool TryEncryptOrDecrypt(ReadOnlySpan<byte> data, Span<byte> destination, RSAEncryptionPadding padding, bool encrypt, out int bytesWritten)
8585
{
8686
if (padding == null)
8787
{
@@ -93,7 +93,7 @@ private unsafe bool TryEncryptOrDecrypt(ReadOnlySpan<byte> source, Span<byte> de
9393
switch (padding.Mode)
9494
{
9595
case RSAEncryptionPaddingMode.Pkcs1:
96-
return TryEncryptOrDecrypt(keyHandle, source, destination, AsymmetricPaddingMode.NCRYPT_PAD_PKCS1_FLAG, null, encrypt, out bytesWritten);
96+
return TryEncryptOrDecrypt(keyHandle, data, destination, AsymmetricPaddingMode.NCRYPT_PAD_PKCS1_FLAG, null, encrypt, out bytesWritten);
9797

9898
case RSAEncryptionPaddingMode.Oaep:
9999
IntPtr namePtr = Marshal.StringToHGlobalUni(padding.OaepHashAlgorithm.Name);
@@ -105,7 +105,7 @@ private unsafe bool TryEncryptOrDecrypt(ReadOnlySpan<byte> source, Span<byte> de
105105
pbLabel = IntPtr.Zero, // It would nice to put randomized data here but RSAEncryptionPadding does not at this point provide support for this.
106106
cbLabel = 0,
107107
};
108-
return TryEncryptOrDecrypt(keyHandle, source, destination, AsymmetricPaddingMode.NCRYPT_PAD_OAEP_FLAG, &paddingInfo, encrypt, out bytesWritten);
108+
return TryEncryptOrDecrypt(keyHandle, data, destination, AsymmetricPaddingMode.NCRYPT_PAD_OAEP_FLAG, &paddingInfo, encrypt, out bytesWritten);
109109
}
110110
finally
111111
{

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RS
6868
}
6969
}
7070

71-
public override unsafe bool TrySignHash(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding, out int bytesWritten)
71+
public override unsafe bool TrySignHash(ReadOnlySpan<byte> hash, Span<byte> destination, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding, out int bytesWritten)
7272
{
7373
string hashAlgorithmName = hashAlgorithm.Name;
7474
if (string.IsNullOrEmpty(hashAlgorithmName))
@@ -89,11 +89,11 @@ public override unsafe bool TrySignHash(ReadOnlySpan<byte> source, Span<byte> de
8989
{
9090
case RSASignaturePaddingMode.Pkcs1:
9191
var pkcs1PaddingInfo = new BCRYPT_PKCS1_PADDING_INFO() { pszAlgId = namePtr };
92-
return keyHandle.TrySignHash(source, destination, AsymmetricPaddingMode.NCRYPT_PAD_PKCS1_FLAG, &pkcs1PaddingInfo, out bytesWritten);
92+
return keyHandle.TrySignHash(hash, destination, AsymmetricPaddingMode.NCRYPT_PAD_PKCS1_FLAG, &pkcs1PaddingInfo, out bytesWritten);
9393

9494
case RSASignaturePaddingMode.Pss:
95-
var pssPaddingInfo = new BCRYPT_PSS_PADDING_INFO() { pszAlgId = namePtr, cbSalt = source.Length };
96-
return keyHandle.TrySignHash(source, destination, AsymmetricPaddingMode.NCRYPT_PAD_PSS_FLAG, &pssPaddingInfo, out bytesWritten);
95+
var pssPaddingInfo = new BCRYPT_PSS_PADDING_INFO() { pszAlgId = namePtr, cbSalt = hash.Length };
96+
return keyHandle.TrySignHash(hash, destination, AsymmetricPaddingMode.NCRYPT_PAD_PSS_FLAG, &pssPaddingInfo, out bytesWritten);
9797

9898
default:
9999
throw new CryptographicException(SR.Cryptography_UnsupportedPaddingMode);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public override KeySizes[] LegalKeySizes
5050
protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>
5151
CngCommon.HashData(data, offset, count, hashAlgorithm);
5252

53-
protected override bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
54-
CngCommon.TryHashData(source, destination, hashAlgorithm, out bytesWritten);
53+
protected override bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
54+
CngCommon.TryHashData(data, destination, hashAlgorithm, out bytesWritten);
5555

5656
protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
5757
CngCommon.HashData(data, hashAlgorithm);

src/Common/src/System/Security/Cryptography/RSAOpenSsl.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
120120
return plainBytes;
121121
}
122122

123-
public override bool TryDecrypt(ReadOnlySpan<byte> source, Span<byte> destination, RSAEncryptionPadding padding, out int bytesWritten)
123+
public override bool TryDecrypt(ReadOnlySpan<byte> data, Span<byte> destination, RSAEncryptionPadding padding, out int bytesWritten)
124124
{
125125
if (padding == null)
126126
{
@@ -137,7 +137,7 @@ public override bool TryDecrypt(ReadOnlySpan<byte> source, Span<byte> destinatio
137137
return false;
138138
}
139139

140-
int returnValue = Interop.Crypto.RsaPrivateDecrypt(source.Length, source, destination, key, rsaPadding);
140+
int returnValue = Interop.Crypto.RsaPrivateDecrypt(data.Length, data, destination, key, rsaPadding);
141141
CheckReturn(returnValue);
142142

143143
// If the padding mode is RSA_NO_PADDING then the size of the decrypted block
@@ -174,7 +174,7 @@ public override byte[] Encrypt(byte[] data, RSAEncryptionPadding padding)
174174
return buf;
175175
}
176176

177-
public override bool TryEncrypt(ReadOnlySpan<byte> source, Span<byte> destination, RSAEncryptionPadding padding, out int bytesWritten)
177+
public override bool TryEncrypt(ReadOnlySpan<byte> data, Span<byte> destination, RSAEncryptionPadding padding, out int bytesWritten)
178178
{
179179
if (padding == null)
180180
{
@@ -191,7 +191,7 @@ public override bool TryEncrypt(ReadOnlySpan<byte> source, Span<byte> destinatio
191191
return false;
192192
}
193193

194-
int returnValue = Interop.Crypto.RsaPublicEncrypt(source.Length, source, destination, key, rsaPadding);
194+
int returnValue = Interop.Crypto.RsaPublicEncrypt(data.Length, data, destination, key, rsaPadding);
195195
CheckReturn(returnValue);
196196

197197
bytesWritten = returnValue;
@@ -389,8 +389,8 @@ protected override byte[] HashData(byte[] data, int offset, int count, HashAlgor
389389
protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
390390
AsymmetricAlgorithmHelpers.HashData(data, hashAlgorithm);
391391

392-
protected override bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
393-
AsymmetricAlgorithmHelpers.TryHashData(source, destination, hashAlgorithm, out bytesWritten);
392+
protected override bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
393+
AsymmetricAlgorithmHelpers.TryHashData(data, destination, hashAlgorithm, out bytesWritten);
394394

395395
public override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
396396
{
@@ -436,7 +436,7 @@ private byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithmName)
436436
return signature;
437437
}
438438

439-
public override bool TrySignHash(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding, out int bytesWritten)
439+
public override bool TrySignHash(ReadOnlySpan<byte> hash, Span<byte> destination, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding, out int bytesWritten)
440440
{
441441
if (string.IsNullOrEmpty(hashAlgorithm.Name))
442442
{
@@ -462,7 +462,7 @@ public override bool TrySignHash(ReadOnlySpan<byte> source, Span<byte> destinati
462462
}
463463

464464
int signatureSize;
465-
if (!Interop.Crypto.RsaSign(algorithmNid, source, source.Length, destination, out signatureSize, rsa))
465+
if (!Interop.Crypto.RsaSign(algorithmNid, hash, hash.Length, destination, out signatureSize, rsa))
466466
{
467467
throw Interop.Crypto.CreateOpenSslCryptographicException();
468468
}

0 commit comments

Comments
 (0)