New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Additional span overloads for asymmetric signing and encryption #68767
Comments
Tagging subscribers to this area: @dotnet/area-system-security, @vcsjones Issue DetailsBackground and motivationFor asymmetric signing (ECDsa, RSA) we have There is not:
I propose we fill those API gaps. API Proposalnamespace System.Security.Cryptography {
public abstract partial class RSA {
public byte[] Encrypt(
ReadOnlySpan<byte> data,
RSAEncryptionPadding padding);
public int Encrypt(
ReadOnlySpan<byte> data,
Span<byte> destination,
RSAEncryptionPadding padding);
public byte[] Decrypt(
ReadOnlySpan<byte> data,
RSAEncryptionPadding padding);
public int Decrypt(
ReadOnlySpan<byte> data,
Span<byte> destination,
RSAEncryptionPadding padding);
public byte[] SignData(
ReadOnlySpan<byte> data,
HashAlgorithmName hashAlgorithm,
RSASignaturePadding padding);
public int SignData(
ReadOnlySpan<byte> data,
Span<byte> destination,
HashAlgorithmName hashAlgorithm,
RSASignaturePadding padding);
public byte[] SignHash(
ReadOnlySpan<byte> hash,
HashAlgorithmName hashAlgorithm,
RSASignaturePadding padding);
public int SignHash(
ReadOnlySpan<byte> hash,
Span<byte> destination,
HashAlgorithmName hashAlgorithm,
RSASignaturePadding padding);
}
public abstract partial class ECDsa {
public byte[] SignData(
ReadOnlySpan<byte> data,
HashAlgorithmName hashAlgorithm,
DSASignatureFormat signatureFormat);
public byte[] SignData(
ReadOnlySpan<byte> data,
HashAlgorithmName hashAlgorithm);
public int SignData(
ReadOnlySpan<byte> data,
Span<byte> destination,
HashAlgorithmName hashAlgorithm,
DSASignatureFormat signatureFormat);
public int SignData(
ReadOnlySpan<byte> data,
Span<byte> destination,
HashAlgorithmName hashAlgorithm);
public byte[] SignHash(
ReadOnlySpan<byte> hash,
DSASignatureFormat signatureFormat);
public byte[] SignHash(
ReadOnlySpan<byte> hash);
public int SignHash(
ReadOnlySpan<byte> hash,
Span<byte> destination,
DSASignatureFormat signatureFormat);
public int SignHash(
ReadOnlySpan<byte> hash,
Span<byte> destination);
}
} API UsageUsage should be fairly straightforward. Span<byte> hash = stackalloc byte[SHA256.HashSizeInBytes];
// Populate hash
byte[] signature = rsa.SignHash(hash, HashAlgorithmName.SHA256, RSASignaturePadding.Pss); Alternative DesignsDo nothing. There are "Try*" APIs that can perform the operations on span, so these APIs don't enable new scenarios, but rather make typical scenarios less cumbersome. RisksNo response
|
Looks good to me. There are a good number of |
namespace System.Security.Cryptography;
public abstract partial class RSA
{
public byte[] Encrypt(
ReadOnlySpan<byte> data,
RSAEncryptionPadding padding);
public int Encrypt(
ReadOnlySpan<byte> data,
Span<byte> destination,
RSAEncryptionPadding padding);
public byte[] Decrypt(
ReadOnlySpan<byte> data,
RSAEncryptionPadding padding);
public int Decrypt(
ReadOnlySpan<byte> data,
Span<byte> destination,
RSAEncryptionPadding padding);
public byte[] SignData(
ReadOnlySpan<byte> data,
HashAlgorithmName hashAlgorithm,
RSASignaturePadding padding);
public int SignData(
ReadOnlySpan<byte> data,
Span<byte> destination,
HashAlgorithmName hashAlgorithm,
RSASignaturePadding padding);
public byte[] SignHash(
ReadOnlySpan<byte> hash,
HashAlgorithmName hashAlgorithm,
RSASignaturePadding padding);
public int SignHash(
ReadOnlySpan<byte> hash,
Span<byte> destination,
HashAlgorithmName hashAlgorithm,
RSASignaturePadding padding);
}
public abstract partial class ECDsa
{
public byte[] SignData(
ReadOnlySpan<byte> data,
HashAlgorithmName hashAlgorithm,
DSASignatureFormat signatureFormat);
public byte[] SignData(
ReadOnlySpan<byte> data,
HashAlgorithmName hashAlgorithm);
public int SignData(
ReadOnlySpan<byte> data,
Span<byte> destination,
HashAlgorithmName hashAlgorithm,
DSASignatureFormat signatureFormat);
public int SignData(
ReadOnlySpan<byte> data,
Span<byte> destination,
HashAlgorithmName hashAlgorithm);
public byte[] SignHash(
ReadOnlySpan<byte> hash,
DSASignatureFormat signatureFormat);
public byte[] SignHash(
ReadOnlySpan<byte> hash);
public int SignHash(
ReadOnlySpan<byte> hash,
Span<byte> destination,
DSASignatureFormat signatureFormat);
public int SignHash(
ReadOnlySpan<byte> hash,
Span<byte> destination);
} |
Background and motivation
For asymmetric signing (ECDsa, RSA) we have
Sign
APIs that accept abyte[]
andTrySign
APIs that accept aReadOnlySpan<byte>
.There is not:
ReadOnlySpan<byte>
ReadOnlySpan<byte>
and throws if the destination is too small.I propose we fill those API gaps.
API Proposal
API Usage
Usage should be fairly straightforward.
Alternative Designs
Do nothing. There are "Try*" APIs that can perform the operations on span, so these APIs don't enable new scenarios, but rather make typical scenarios less cumbersome. This also makes the available overloads more consistence with "the typical four".
Risks
No response
The text was updated successfully, but these errors were encountered: