Skip to content

Use System.Security.Cryptography in AesCipher - #1235

Merged
WojciechNagorski merged 5 commits into
sshnet:developfrom
Rob-Hague:aesssc
Nov 16, 2023
Merged

Use System.Security.Cryptography in AesCipher#1235
WojciechNagorski merged 5 commits into
sshnet:developfrom
Rob-Hague:aesssc

Conversation

@Rob-Hague

Copy link
Copy Markdown
Collaborator

System.Security.Cryptography.Aes is supported on all target platforms (it wasn't in previous versions of the library). So we can use it rather than the hand-written code.

The change comes with modest performance gains (#865 (comment)) and from a maintenance perspective, makes it easier to achieve the design proposed in the linked comment in order to get much greater performance gains.

Comment threadtest/Renci.SshNet.IntegrationTests/SshTests.cs
@WojciechNagorski

Copy link
Copy Markdown
Collaborator

I'm really impressed this is a great job!
Can you describe how this PRis related with #865 ?
Can you provide some Benchmark results?

@Rob-Hague

Copy link
Copy Markdown
CollaboratorAuthor

I would expect this to become the implementation of SshNetCipherModeImpl in the proposal in #865 (comment), which would be used when using the constructor taking Renci.SshNet.Security.Ciphers.CipherMode. #865 would provide a much faster implementation when using a new constructor taking a System.Security.Cryptography.CipherMode value.

Reposting the benchmarks here (they are the same as in the linked comment)

Results on develop branch 826222f:

MethodMeanErrorStdDevGen0Allocated
Encrypt_CBC320.4 μs0.47 μs0.36 μs15.625032.41 KB
Decrypt_CBC352.3 μs2.37 μs1.85 μs15.625032.41 KB
Encrypt_CFB335.6 μs0.43 μs0.34 μs15.625032.45 KB
Decrypt_CFB367.9 μs4.23 μs3.95 μs15.625032.45 KB
Encrypt_CTR310.4 μs0.38 μs0.31 μs15.625032.45 KB
Decrypt_CTR315.3 μs3.96 μs3.70 μs15.625032.45 KB

Results on these changes:

MethodMeanErrorStdDevGen0Allocated
Encrypt_CBC221.2 μs2.01 μs1.88 μs15.869132.7 KB
Decrypt_CBC220.0 μs2.73 μs2.56 μs15.869132.71 KB
Encrypt_CFB251.3 μs0.33 μs0.27 μs15.625032.75 KB
Decrypt_CFB255.3 μs0.67 μs0.60 μs15.625032.75 KB
Encrypt_CTR201.3 μs0.41 μs0.38 μs15.869132.75 KB
Decrypt_CTR209.3 μs0.22 μs0.19 μs15.869132.75 KB

Results on #865zybexXL@a9f68fb:

MethodMeanErrorStdDevGen0Allocated
Encrypt_CBC29.637 μs0.1278 μs0.1067 μs16.113333.14 KB
Decrypt_CBC6.579 μs0.0160 μs0.0142 μs16.128533.14 KB
Encrypt_CFB333.761 μs0.1710 μs0.1335 μs15.625032.52 KB
Decrypt_CFB333.899 μs0.2090 μs0.1955 μs15.625032.52 KB
Encrypt_CTR24.630 μs0.0327 μs0.0273 μs78.1250161.2 KB
Decrypt_CTR24.544 μs0.0817 μs0.0683 μs78.1250161.2 KB

The allocations in CTR mode can easily be removed, and I would expect it to extend to CFB as well. So given how much faster it is, it will be worth having as an implementation. The proposal is to keep the implementations separate so that we keep using a Renci.SshNet.Security.Ciphers.CipherMode if it is provided.

@zybexXL

zybexXL commented Nov 7, 2023

Copy link
Copy Markdown
Contributor

@WojciechNagorski I've restructured #865 to incorporate suggestions from Rob-Hague in the above linked comment. This makes both PRs functionally similar, but #865 is much faster as this one only accelerates blockwise encryption/decryption, while 865 also accelerates one-shot buffer operations.

#865 already used acceleration (2 years ago) but the code was injected into the CipherMode classes instead of AesCipher.cs. I've now refactored and improved it.

@WojciechNagorski

Copy link
Copy Markdown
Collaborator

@Rob-Hague, @zybexXL Which of these two PRs should be checked first? #1235#865 They are related and I don't really know where to start.

Comment threadsrc/Renci.SshNet/Security/Cryptography/Ciphers/AesCipher.cs
@WojciechNagorski

Copy link
Copy Markdown
Collaborator

I like this kind of PR
image

@WojciechNagorski
WojciechNagorski merged commit 5021f6d into sshnet:developNov 16, 2023
@Rob-Hague
Rob-Hague deleted the aesssc branch November 16, 2023 07:43
@WojciechNagorskiWojciechNagorski added this to the 2023.0.1 milestone Nov 16, 2023
@WojciechNagorskiWojciechNagorski mentioned this pull request Dec 29, 2023
@WojciechNagorski

Copy link
Copy Markdown
Collaborator

The 2023.0.1 version has been released to Nuget: https://www.nuget.org/packages/SSH.NET/2023.0.1

This was referenced Aug 12, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@Rob-Hague@WojciechNagorski@zybexXL