Skip to content
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

Use ReadOnlySpan for DefaultExponent #33820

Merged
merged 4 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ internal static partial class Crypto
internal static extern void BigNumDestroy(IntPtr a);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_BigNumFromBinary")]
private static extern IntPtr BigNumFromBinary(byte[] s, int len);
private static extern IntPtr BigNumFromBinary(ref byte s, int len);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_BigNumToBinary")]
private static extern unsafe int BigNumToBinary(SafeBignumHandle a, byte* to);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetBigNumBytes")]
private static extern int GetBigNumBytes(SafeBignumHandle a);

private static IntPtr CreateBignumPtr(byte[] bigEndianValue)
private static IntPtr CreateBignumPtr(ReadOnlySpan<byte> bigEndianValue)
{
if (bigEndianValue == null)
if (bigEndianValue.IsEmpty)
vcsjones marked this conversation as resolved.
Show resolved Hide resolved
{
return IntPtr.Zero;
}

IntPtr ret = BigNumFromBinary(bigEndianValue, bigEndianValue.Length);
IntPtr ret = BigNumFromBinary(ref MemoryMarshal.GetReference(bigEndianValue), bigEndianValue.Length);

if (ret == IntPtr.Zero)
{
Expand All @@ -41,7 +41,7 @@ private static IntPtr CreateBignumPtr(byte[] bigEndianValue)
return ret;
}

internal static SafeBignumHandle CreateBignum(byte[] bigEndianValue)
internal static SafeBignumHandle CreateBignum(ReadOnlySpan<byte> bigEndianValue)
{
IntPtr handle = CreateBignumPtr(bigEndianValue);
return new SafeBignumHandle(handle, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed partial class RSAOpenSsl : RSA
private const int BitsPerByte = 8;

// 65537 (0x10001) in big-endian form
private static readonly byte[] s_defaultExponent = { 0x01, 0x00, 0x01 };
private static ReadOnlySpan<byte> DefaultExponent => new byte[] { 0x01, 0x00, 0x01 };

private Lazy<SafeRsaHandle> _key;

Expand Down Expand Up @@ -593,7 +593,7 @@ private SafeRsaHandle GenerateKey()

try
{
using (SafeBignumHandle exponent = Interop.Crypto.CreateBignum(s_defaultExponent))
using (SafeBignumHandle exponent = Interop.Crypto.CreateBignum(DefaultExponent))
{
// The documentation for RSA_generate_key_ex does not say that it returns only
// 0 or 1, so the call marshals it back as a full Int32 and checks for a value
Expand Down