-
-
Notifications
You must be signed in to change notification settings - Fork 970
Add support for AEAD AES 128/256 GCM Ciphers (.NET 6.0 onward only) #1369
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
Merged
Merged
Changes from 19 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
8b5c819
Init AeadCipher
scott-xu 4d160ca
Move AeadCipher to parent folder. Move EncryptBlock/DecryptBlock from…
scott-xu 7546a5a
simplify parameter name
scott-xu 270fb96
Implement AesGcmCipher
scott-xu 7a6b334
Merge branch 'develop' into aes-gcm-and-chacha20-poly1305
scott-xu 1059b68
Update README
scott-xu fe26b65
Remove protected IV from AeadCipher; Set offset to outbound sequence …
scott-xu 1b92a6a
Rename associatedData to packetLengthField
scott-xu 7377cbe
Use Span<byte> to avoid unnecessary allocations
scott-xu cdbe822
Use `Span` to improve performance when `IncrementCounter()`
scott-xu 65ad16a
Add `IsAead` property to `CipherInfo`. Include packet length field an…
scott-xu 3336d3d
Fix build
scott-xu 37a16bf
Fix UT
scott-xu 7debca4
Merge branch 'develop' into aesgcm
scott-xu e727959
Check `AesGcm.IsSupported` before add to the `Encryptions` collection.
scott-xu b0ccb0c
Merge branch 'aesgcm' of https://github.com/scott-xu/SSH.NET into aesgcm
scott-xu 6bdbfc0
Merge branch 'develop' into aesgcm
scott-xu e8ed4a2
Suppress CA1859 "Use concrete types when possible for improved perfor…
scott-xu 4228014
Merge branch 'develop' into aesgcm
scott-xu 7a24090
Merge branch 'develop' into aesgcm
scott-xu c532157
Update xml doc comments. Do not treat AesGcmCipher separately in Sess…
scott-xu a831f3d
Merge branch 'aesgcm' of https://github.com/scott-xu/SSH.NET into aesgcm
scott-xu 299a3f6
Fix build
scott-xu ebeb582
Fix build
scott-xu 46f9a6d
Update the comment as ChaCha20Poly1305 uses a separated key to encypt…
scott-xu 8aaabc4
Update src/Renci.SshNet/Security/Cryptography/Cipher.cs
scott-xu 0922306
Make `AesGcmCipher` internal.
scott-xu 71d5635
Merge branch 'aesgcm' of https://github.com/scott-xu/SSH.NET into aesgcm
scott-xu fb7884b
Fix nullable error in build
Rob-Hague 12b7c38
Merge branch 'fixbuild' into aesgcm
Rob-Hague ec34f71
typos
Rob-Hague File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,9 @@ public class ConnectionInfo : IConnectionInfoInternal | |
| /// <summary> | ||
| /// Gets supported encryptions for this connection. | ||
| /// </summary> | ||
| #pragma warning disable CA1859 // Use concrete types when possible for improved performance | ||
| public IDictionary<string, CipherInfo> Encryptions { get; private set; } | ||
| #pragma warning restore CA1859 // Use concrete types when possible for improved performance | ||
|
|
||
| /// <summary> | ||
| /// Gets supported hash algorithms for this connection. | ||
|
|
@@ -380,25 +382,30 @@ public ConnectionInfo(string host, int port, string username, ProxyTypes proxyTy | |
| { "diffie-hellman-group1-sha1", () => new KeyExchangeDiffieHellmanGroup1Sha1() }, | ||
| }; | ||
|
|
||
| Encryptions = new Dictionary<string, CipherInfo> | ||
| { | ||
| { "aes128-ctr", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)) }, | ||
| { "aes192-ctr", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)) }, | ||
| { "aes256-ctr", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)) }, | ||
| { "aes128-cbc", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)) }, | ||
| { "aes192-cbc", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)) }, | ||
| { "aes256-cbc", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)) }, | ||
| { "3des-cbc", new CipherInfo(192, (key, iv) => new TripleDesCipher(key, new CbcCipherMode(iv), padding: null)) }, | ||
| { "blowfish-cbc", new CipherInfo(128, (key, iv) => new BlowfishCipher(key, new CbcCipherMode(iv), padding: null)) }, | ||
| { "twofish-cbc", new CipherInfo(256, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), padding: null)) }, | ||
| { "twofish192-cbc", new CipherInfo(192, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), padding: null)) }, | ||
| { "twofish128-cbc", new CipherInfo(128, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), padding: null)) }, | ||
| { "twofish256-cbc", new CipherInfo(256, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), padding: null)) }, | ||
| { "arcfour", new CipherInfo(128, (key, iv) => new Arc4Cipher(key, dischargeFirstBytes: false)) }, | ||
| { "arcfour128", new CipherInfo(128, (key, iv) => new Arc4Cipher(key, dischargeFirstBytes: true)) }, | ||
| { "arcfour256", new CipherInfo(256, (key, iv) => new Arc4Cipher(key, dischargeFirstBytes: true)) }, | ||
| { "cast128-cbc", new CipherInfo(128, (key, iv) => new CastCipher(key, new CbcCipherMode(iv), padding: null)) }, | ||
| }; | ||
| Encryptions = new Dictionary<string, CipherInfo>(); | ||
| Encryptions.Add("aes128-ctr", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false))); | ||
| Encryptions.Add("aes192-ctr", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false))); | ||
| Encryptions.Add("aes256-ctr", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false))); | ||
| #if NET6_0_OR_GREATER | ||
| if (AesGcm.IsSupported) | ||
| { | ||
| Encryptions.Add("[email protected]", new CipherInfo(128, (key, iv) => new AesGcmCipher(key, iv), isAead: true)); | ||
| Encryptions.Add("[email protected]", new CipherInfo(256, (key, iv) => new AesGcmCipher(key, iv), isAead: true)); | ||
| } | ||
| #endif | ||
| Encryptions.Add("aes128-cbc", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false))); | ||
| Encryptions.Add("aes192-cbc", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false))); | ||
| Encryptions.Add("aes256-cbc", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false))); | ||
| Encryptions.Add("3des-cbc", new CipherInfo(192, (key, iv) => new TripleDesCipher(key, new CbcCipherMode(iv), padding: null))); | ||
| Encryptions.Add("blowfish-cbc", new CipherInfo(128, (key, iv) => new BlowfishCipher(key, new CbcCipherMode(iv), padding: null))); | ||
| Encryptions.Add("twofish-cbc", new CipherInfo(256, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), padding: null))); | ||
| Encryptions.Add("twofish192-cbc", new CipherInfo(192, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), padding: null))); | ||
| Encryptions.Add("twofish128-cbc", new CipherInfo(128, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), padding: null))); | ||
| Encryptions.Add("twofish256-cbc", new CipherInfo(256, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), padding: null))); | ||
| Encryptions.Add("arcfour", new CipherInfo(128, (key, iv) => new Arc4Cipher(key, dischargeFirstBytes: false))); | ||
| Encryptions.Add("arcfour128", new CipherInfo(128, (key, iv) => new Arc4Cipher(key, dischargeFirstBytes: true))); | ||
| Encryptions.Add("arcfour256", new CipherInfo(256, (key, iv) => new Arc4Cipher(key, dischargeFirstBytes: true))); | ||
| Encryptions.Add("cast128-cbc", new CipherInfo(128, (key, iv) => new CastCipher(key, new CbcCipherMode(iv), padding: null))); | ||
|
|
||
| #pragma warning disable IDE0200 // Remove unnecessary lambda expression; We want to prevent instantiating the HashAlgorithm objects. | ||
| HmacAlgorithms = new Dictionary<string, HashInfo> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/Renci.SshNet/Security/Cryptography/Ciphers/AesGcmCipher.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| #if NET6_0_OR_GREATER | ||
| using System; | ||
| using System.Buffers.Binary; | ||
| using System.Security.Cryptography; | ||
|
|
||
| using Renci.SshNet.Common; | ||
|
|
||
| namespace Renci.SshNet.Security.Cryptography.Ciphers | ||
| { | ||
| /// <summary> | ||
| /// AES GCM cipher implementation. | ||
| /// <see href="https://datatracker.ietf.org/doc/html/rfc5647"/>. | ||
| /// </summary> | ||
| public sealed class AesGcmCipher : SymmetricCipher, IDisposable | ||
| { | ||
| private readonly byte[] _nonce; | ||
| private readonly AesGcm _aesGcm; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override byte MinimumSize | ||
| { | ||
| get | ||
| { | ||
| return 16; | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override int TagSize | ||
| { | ||
| get | ||
| { | ||
| return 16; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AesGcmCipher"/> class. | ||
| /// </summary> | ||
| /// <param name="key">The key.</param> | ||
| /// <param name="iv">The IV.</param> | ||
| public AesGcmCipher(byte[] key, byte[] iv) | ||
| : base(key) | ||
| { | ||
| _nonce = iv.Take(12); | ||
| #if NET8_0_OR_GREATER | ||
| _aesGcm = new AesGcm(key, TagSize); | ||
| #else | ||
| _aesGcm = new AesGcm(key); | ||
| #endif | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Encrypts the specified input. | ||
| /// </summary> | ||
| /// <param name="input">The input.</param> | ||
| /// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin encrypting.</param> | ||
| /// <param name="length">The number of bytes to encrypt from <paramref name="input"/>.</param> | ||
| /// <returns> | ||
| /// The packet length field + cipher text + tag. | ||
| /// </returns> | ||
| public override byte[] Encrypt(byte[] input, int offset, int length) | ||
| { | ||
| // [outbound sequence field][packet length field][padding length field sz][payload][random paddings] | ||
| // [----4 bytes----(offset)][------4 bytes------][----------------Plain Text---------------(length)] | ||
| var packetLengthField = new ReadOnlySpan<byte>(input, offset, 4); | ||
scott-xu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var plainText = new ReadOnlySpan<byte>(input, offset + 4, length - 4); | ||
|
|
||
| var output = new byte[length + TagSize]; | ||
| packetLengthField.CopyTo(output); | ||
| var cipherText = new Span<byte>(output, 4, length - 4); | ||
| var tag = new Span<byte>(output, length, TagSize); | ||
|
|
||
| _aesGcm.Encrypt(_nonce, plainText, cipherText, tag, packetLengthField); | ||
|
|
||
| IncrementCounter(); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Decrypts the specified input. | ||
| /// </summary> | ||
| /// <param name="input">The input.</param> | ||
| /// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin decrypting and authenticating.</param> | ||
| /// <param name="length">The number of bytes to decrypt and authenticate from <paramref name="input"/>.</param> | ||
| /// <returns> | ||
| /// The packet length field + plain text. | ||
| /// </returns> | ||
| public override byte[] Decrypt(byte[] input, int offset, int length) | ||
| { | ||
| // [inbound sequence field][packet length field][padding length field sz][payload][random paddings][Authenticated TAG] | ||
| // [----4 bytes---(offset)][------4 bytes------][------------------Cipher Text--------------------][---TAG---(length)] | ||
| var packetLengthField = new ReadOnlySpan<byte>(input, offset, 4); | ||
scott-xu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var cipherText = new ReadOnlySpan<byte>(input, offset + 4, length - 4 - TagSize); | ||
| var tag = new ReadOnlySpan<byte>(input, offset + length - TagSize, TagSize); | ||
|
|
||
| var output = new byte[length - TagSize]; | ||
| packetLengthField.CopyTo(output); | ||
| var plainText = new Span<byte>(output, 4, length - 4 - TagSize); | ||
|
|
||
| _aesGcm.Decrypt(_nonce, cipherText, tag, plainText, packetLengthField); | ||
|
|
||
| IncrementCounter(); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| private void IncrementCounter() | ||
| { | ||
| var invocationCounter = new Span<byte>(_nonce, 4, 8); | ||
| var count = BinaryPrimitives.ReadUInt64BigEndian(invocationCounter); | ||
| BinaryPrimitives.WriteUInt64BigEndian(invocationCounter, count + 1); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Dispose the instance. | ||
| /// </summary> | ||
| /// <param name="disposing">Set to True to dispose of resouces.</param> | ||
| public void Dispose(bool disposing) | ||
| { | ||
| if (disposing) | ||
| { | ||
| _aesGcm.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void Dispose() | ||
| { | ||
| // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
| Dispose(disposing: true); | ||
| GC.SuppressFinalize(this); | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.