Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -62,6 +62,41 @@ ref MemoryMarshal.GetReference(destination),
return written;
}

[DllImport(Libraries.CryptoNative)]
private static extern int CryptoNative_RsaEncrypt(
SafeEvpPKeyHandle pkey,
ref byte source,
int sourceLength,
RSAEncryptionPaddingMode paddingMode,
IntPtr digestAlgorithm,
ref byte destination,
int destinationLength);

internal static int RsaEncrypt(
SafeEvpPKeyHandle pkey,
ReadOnlySpan<byte> source,
RSAEncryptionPaddingMode paddingMode,
IntPtr digestAlgorithm,
Span<byte> destination)
{
int written = CryptoNative_RsaEncrypt(
pkey,
ref MemoryMarshal.GetReference(source),
source.Length,
paddingMode,
digestAlgorithm,
ref MemoryMarshal.GetReference(destination),
destination.Length);

if (written < 0)
{
Debug.Assert(written == -1);
Comment thread
bartonjs marked this conversation as resolved.
throw CreateOpenSslCryptographicException();
}

return written;
}

[DllImport(Libraries.CryptoNative)]
private static extern int CryptoNative_RsaSignHash(
SafeEvpPKeyHandle pkey,
Expand Down Expand Up @@ -97,11 +132,55 @@ ref MemoryMarshal.GetReference(destination),
return written;
}

[DllImport(Libraries.CryptoNative)]
private static extern int CryptoNative_RsaVerifyHash(
SafeEvpPKeyHandle pkey,
RSASignaturePaddingMode paddingMode,
IntPtr digestAlgorithm,
ref byte hash,
int hashLength,
ref byte signature,
int signatureLength);

internal static bool RsaVerifyHash(
SafeEvpPKeyHandle pkey,
RSASignaturePaddingMode paddingMode,
IntPtr digestAlgorithm,
ReadOnlySpan<byte> hash,
ReadOnlySpan<byte> signature)
{
int ret = CryptoNative_RsaVerifyHash(
pkey,
paddingMode,
digestAlgorithm,
ref MemoryMarshal.GetReference(hash),
hash.Length,
ref MemoryMarshal.GetReference(signature),
signature.Length);

if (ret == 1)
{
return true;
}

if (ret == 0)
{
return false;
}

Debug.Assert(ret == -1);
throw CreateOpenSslCryptographicException();
}

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeyGetRsa")]
internal static extern SafeRsaHandle EvpPkeyGetRsa(SafeEvpPKeyHandle pkey);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeySetRsa")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool EvpPkeySetRsa(SafeEvpPKeyHandle pkey, SafeRsaHandle rsa);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeySetRsa")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool EvpPkeySetRsa(SafeEvpPKeyHandle pkey, IntPtr rsa);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,61 +27,17 @@ internal static SafeRsaHandle DecodeRsaPublicKey(ReadOnlySpan<byte> buf) =>
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_DecodeRsaPublicKey")]
private static extern SafeRsaHandle DecodeRsaPublicKey(ref byte buf, int len);

internal static int RsaPublicEncrypt(
int flen,
ReadOnlySpan<byte> from,
Span<byte> to,
SafeRsaHandle rsa,
RsaPadding padding) =>
RsaPublicEncrypt(flen, ref MemoryMarshal.GetReference(from), ref MemoryMarshal.GetReference(to), rsa, padding);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaPublicEncrypt")]
private static extern int RsaPublicEncrypt(
int flen,
ref byte from,
ref byte to,
SafeRsaHandle rsa,
RsaPadding padding);

internal static int RsaVerificationPrimitive(
ReadOnlySpan<byte> from,
Span<byte> to,
SafeRsaHandle rsa) =>
RsaVerificationPrimitive(from.Length, ref MemoryMarshal.GetReference(from), ref MemoryMarshal.GetReference(to), rsa);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaVerificationPrimitive")]
private static extern int RsaVerificationPrimitive(
int flen,
ref byte from,
ref byte to,
SafeRsaHandle rsa);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaSize")]
internal static extern int RsaSize(SafeRsaHandle rsa);

internal static bool RsaVerify(int type, ReadOnlySpan<byte> m, ReadOnlySpan<byte> sigbuf, SafeRsaHandle rsa)
internal static RSAParameters ExportRsaParameters(SafeEvpPKeyHandle key, bool includePrivateParameters)
{
bool ret = RsaVerify(
type,
ref MemoryMarshal.GetReference(m),
m.Length,
ref MemoryMarshal.GetReference(sigbuf),
sigbuf.Length,
rsa);

if (!ret)
using (SafeRsaHandle rsa = EvpPkeyGetRsa(key))
Comment thread
krwq marked this conversation as resolved.
{
ErrClearError();
return ExportRsaParameters(rsa, includePrivateParameters);
}

return ret;
}


[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaVerify")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool RsaVerify(int type, ref byte m, int m_len, ref byte sigbuf, int siglen, SafeRsaHandle rsa);

internal static RSAParameters ExportRsaParameters(SafeRsaHandle key, bool includePrivateParameters)
{
Debug.Assert(
Expand Down
Loading