Skip to content

Commit

Permalink
Use EVP_PKEY for RSA Decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
bartonjs committed Mar 24, 2021
1 parent d3acf41 commit 6aa4d59
Show file tree
Hide file tree
Showing 20 changed files with 444 additions and 174 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

internal static partial class Interop
{
internal static partial class Crypto
{
private static volatile IntPtr s_evpMd5;
private static volatile IntPtr s_evpSha1;
private static volatile IntPtr s_evpSha256;
private static volatile IntPtr s_evpSha384;
private static volatile IntPtr s_evpSha512;

[DllImport(Libraries.CryptoNative)]
private static extern IntPtr CryptoNative_EvpMd5();

internal static IntPtr EvpMd5() =>
s_evpMd5 != IntPtr.Zero ? s_evpMd5 : (s_evpMd5 = CryptoNative_EvpMd5());

[DllImport(Libraries.CryptoNative)]
internal static extern IntPtr CryptoNative_EvpSha1();

internal static IntPtr EvpSha1() =>
s_evpSha1 != IntPtr.Zero ? s_evpSha1 : (s_evpSha1 = CryptoNative_EvpSha1());

[DllImport(Libraries.CryptoNative)]
internal static extern IntPtr CryptoNative_EvpSha256();

internal static IntPtr EvpSha256() =>
s_evpSha256 != IntPtr.Zero ? s_evpSha256 : (s_evpSha256 = CryptoNative_EvpSha256());

[DllImport(Libraries.CryptoNative)]
internal static extern IntPtr CryptoNative_EvpSha384();

internal static IntPtr EvpSha384() =>
s_evpSha384 != IntPtr.Zero ? s_evpSha384 : (s_evpSha384 = CryptoNative_EvpSha384());

[DllImport(Libraries.CryptoNative)]
internal static extern IntPtr CryptoNative_EvpSha512();

internal static IntPtr EvpSha512() =>
s_evpSha512 != IntPtr.Zero ? s_evpSha512 : (s_evpSha512 = CryptoNative_EvpSha512());

internal static IntPtr HashAlgorithmToEvp(string hashAlgorithmId) => hashAlgorithmId switch
{
nameof(HashAlgorithmName.SHA1) => EvpSha1(),
nameof(HashAlgorithmName.SHA256) => EvpSha256(),
nameof(HashAlgorithmName.SHA384) => EvpSha384(),
nameof(HashAlgorithmName.SHA512) => EvpSha512(),
nameof(HashAlgorithmName.MD5) => EvpMd5(),
_ => throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmId))
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
Expand Down Expand Up @@ -36,21 +37,6 @@ internal static partial class Crypto
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpMdSize")]
internal static extern int EvpMdSize(IntPtr md);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpMd5")]
internal static extern IntPtr EvpMd5();

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpSha1")]
internal static extern IntPtr EvpSha1();

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpSha256")]
internal static extern IntPtr EvpSha256();

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpSha384")]
internal static extern IntPtr EvpSha384();

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpSha512")]
internal static extern IntPtr EvpSha512();

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetMaxMdSize")]
private static extern int GetMaxMdSize();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Microsoft.Win32.SafeHandles;
Expand All @@ -25,6 +27,41 @@ internal static SafeEvpPKeyHandle RsaGenerateKey(int keySize)
return pkey;
}

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

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

if (written < 0)
{
Debug.Assert(written == -1);
throw CreateOpenSslCryptographicException();
}

return written;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ internal static partial class Crypto
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeyDestroy")]
internal static extern void EvpPkeyDestroy(IntPtr pkey);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeySize")]
internal static extern int EvpPKeySize(SafeEvpPKeyHandle pkey);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_UpRefEvpPkey")]
internal static extern int UpRefEvpPkey(SafeEvpPKeyHandle handle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,6 @@ internal static partial class Crypto
SafeRsaHandle rsa,
RsaPadding padding);

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

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

internal static int RsaSignPrimitive(
ReadOnlySpan<byte> from,
Span<byte> to,
Expand Down

0 comments on commit 6aa4d59

Please sign in to comment.