-
-
Notifications
You must be signed in to change notification settings - Fork 952
Support AesGcm cipher #1364
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
Closed
Closed
Support AesGcm cipher #1364
Changes from all commits
Commits
Show all changes
14 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 fdc8f49
Merge branch 'develop' into aes-gcm-and-chacha20-poly1305
scott-xu 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
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 NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_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); | ||
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); | ||
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 |
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
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.