Skip to content

Use native AesGcm for .NET Framework from nuget #1619

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

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageVersion Include="Meziantou.Analyzer" Version="2.0.201" />

<!-- Should stay on LTS .NET releases. -->
<PackageVersion Include="Microsoft.Bcl.Cryptography" Version="10.0.0-preview.5.25277.114" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.3" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.5" />
<PackageVersion Include="MSTest" Version="3.9.1" />
Expand Down
6 changes: 5 additions & 1 deletion src/Renci.SshNet/Renci.SshNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
</PackageReference>
</ItemGroup>

<ItemGroup Condition=" !$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0')) ">
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<PackageReference Include="Microsoft.Bcl.Cryptography" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Formats.Asn1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET
#if !NETSTANDARD
using System;
using System.Security.Cryptography;

Expand Down
14 changes: 11 additions & 3 deletions src/Renci.SshNet/Security/Cryptography/Ciphers/AesGcmCipher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal sealed partial class AesGcmCipher : SymmetricCipher, IDisposable
private const int TagSizeInBytes = 16;
private readonly byte[] _iv;
private readonly int _aadLength;
#if NET
#if !NETSTANDARD
private readonly Impl _impl;
#else
private readonly BouncyCastleImpl _impl;
Expand Down Expand Up @@ -62,10 +62,18 @@ public AesGcmCipher(byte[] key, byte[] iv, int aadLength)
// SSH AES-GCM requires a 12-octet Initial IV
_iv = iv.Take(12);
_aadLength = aadLength;
#if NET
#if !NETSTANDARD
if (System.Security.Cryptography.AesGcm.IsSupported)
{
_impl = new BclImpl(key, _iv);
try
{
_impl = new BclImpl(key, _iv);
}
catch (DllNotFoundException)
{
// Mono doesn't have BCrypt.dll
_impl = new BouncyCastleImpl(key, _iv);
}
}
else
#endif
Expand Down