Skip to content

Commit

Permalink
Rename PacketEncoder/Decoder to PacketEncryptor/Decryptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds committed Aug 28, 2024
1 parent 2e15ace commit 7eb8ad1
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Tmds.Ssh;

sealed class AesGcmPacketDecoder : IPacketDecoder
sealed class AesGcmPacketDecryptor : IPacketDecryptor
{
private const int AesBlockSize = 16;

Expand All @@ -16,7 +16,7 @@ sealed class AesGcmPacketDecoder : IPacketDecoder
private readonly SequencePool _sequencePool;
private readonly int _tagLength;

public AesGcmPacketDecoder(SequencePool sequencePool, byte[] key, byte[] iv, int tagLength)
public AesGcmPacketDecryptor(SequencePool sequencePool, byte[] key, byte[] iv, int tagLength)
{
_iv = iv;
_tagLength = tagLength;
Expand All @@ -29,7 +29,7 @@ public void Dispose()
_aesGcm.Dispose();
}

public bool TryDecodePacket(Sequence receiveBuffer, uint sequenceNumber, int maxLength, out Packet packet)
public bool TryDecrypt(Sequence receiveBuffer, uint sequenceNumber, int maxLength, out Packet packet)
{
packet = new Packet(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

namespace Tmds.Ssh;

sealed class AesGcmPacketEncoder : IPacketEncoder
sealed class AesGcmPacketEncryptor : IPacketEncryptor
{
private const int AesBlockSize = 16;

private readonly AesGcm _aesGcm;
private readonly byte[] _iv;
private readonly int _tagLength;

public AesGcmPacketEncoder(byte[] key, byte[] iv, int tagLength)
public AesGcmPacketEncryptor(byte[] key, byte[] iv, int tagLength)
{
_iv = iv;
_tagLength = tagLength;
Expand All @@ -27,7 +27,7 @@ public void Dispose()
_aesGcm.Dispose();
}

public void Encode(uint sequenceNumber, Packet packet, Sequence output)
public void Encrypt(uint sequenceNumber, Packet packet, Sequence output)
{
using var pkt = packet.Move(); // Dispose the packet.

Expand All @@ -37,7 +37,7 @@ public void Encode(uint sequenceNumber, Packet packet, Sequence output)
// byte padding_length; // 4 <= padding_length < 256
// byte[n1] payload; // n1 = packet_length-padding_length-1
// byte[n2] random_padding; // n2 = padding_length
byte padding_length = IPacketEncoder.DeterminePaddingLength(payload_length + 1, multipleOf: AesBlockSize);
byte padding_length = IPacketEncryptor.DeterminePaddingLength(payload_length + 1, multipleOf: AesBlockSize);
pkt.WriteHeaderAndPadding(padding_length);

var unencrypted_packet = pkt.AsReadOnlySequence();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace Tmds.Ssh;

sealed class ChaCha20Poly1305PacketDecoder : ChaCha20Poly1305PacketEncDecBase, IPacketDecoder
sealed class ChaCha20Poly1305PacketDecryptor : ChaCha20Poly1305PacketEncDecBase, IPacketDecryptor
{
private readonly SequencePool _sequencePool;
private int _currentPacketLength = -1;

public ChaCha20Poly1305PacketDecoder(SequencePool sequencePool, byte[] key) :
public ChaCha20Poly1305PacketDecryptor(SequencePool sequencePool, byte[] key) :
base(key)
{
_sequencePool = sequencePool;
Expand All @@ -21,7 +21,7 @@ public ChaCha20Poly1305PacketDecoder(SequencePool sequencePool, byte[] key) :
public void Dispose()
{ }

public bool TryDecodePacket(Sequence receiveBuffer, uint sequenceNumber, int maxLength, out Packet packet)
public bool TryDecrypt(Sequence receiveBuffer, uint sequenceNumber, int maxLength, out Packet packet)
{
packet = new Packet(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
namespace Tmds.Ssh;

// https://cvsweb.openbsd.org/src/usr.bin/ssh/PROTOCOL.chacha20poly1305?annotate=HEAD
sealed class ChaCha20Poly1305PacketEncoder : ChaCha20Poly1305PacketEncDecBase, IPacketEncoder
sealed class ChaCha20Poly1305PacketEncryptor : ChaCha20Poly1305PacketEncDecBase, IPacketEncryptor
{
public ChaCha20Poly1305PacketEncoder(byte[] key) :
public ChaCha20Poly1305PacketEncryptor(byte[] key) :
base(key)
{ }

public void Dispose()
{ }

public void Encode(uint sequenceNumber, Packet packet, Sequence output)
public void Encrypt(uint sequenceNumber, Packet packet, Sequence output)
{
using var pkt = packet.Move(); // Dispose the packet.

Expand All @@ -27,7 +27,7 @@ public void Encode(uint sequenceNumber, Packet packet, Sequence output)
// byte padding_length; // 4 <= padding_length < 256
// byte[n1] payload; // n1 = packet_length-padding_length-1
// byte[n2] random_padding; // n2 = padding_length
byte padding_length = IPacketEncoder.DeterminePaddingLength(payload_length + 1, multipleOf: PaddTo);
byte padding_length = IPacketEncryptor.DeterminePaddingLength(payload_length + 1, multipleOf: PaddTo);
pkt.WriteHeaderAndPadding(padding_length);

var unencrypted_packet = pkt.AsReadOnlySequence();
Expand Down
92 changes: 0 additions & 92 deletions src/Tmds.Ssh/EncryptionAlgorithm.cs

This file was deleted.

9 changes: 0 additions & 9 deletions src/Tmds.Ssh/IPacketDecoder.cs

This file was deleted.

9 changes: 9 additions & 0 deletions src/Tmds.Ssh/IPacketDecryptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is part of Tmds.Ssh which is released under MIT.
// See file LICENSE for full license details.

namespace Tmds.Ssh;

interface IPacketDecryptor : IDisposable
{
bool TryDecrypt(Sequence receiveBuffer, uint sequenceNumber, int maxLength, out Packet packet);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace Tmds.Ssh;

interface IPacketEncoder : IDisposable
interface IPacketEncryptor : IDisposable
{
public void Encode(uint sequenceNumber, Packet packet, Sequence buffer);
public void Encrypt(uint sequenceNumber, Packet packet, Sequence buffer);

protected static byte DeterminePaddingLength(uint length, uint multipleOf)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Tmds.Ssh/KeyExchangeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ private bool CheckPacketForReturn(MessageId expected, Packet packet)
public ValueTask SendPacketAsync(Packet packet, CancellationToken ct)
=> _connection.SendPacketAsync(packet, ct);

public void SetEncoderDecoder(IPacketEncoder packetEncoder, IPacketDecoder packetDecoder)
=> _connection.SetEncoderDecoder(packetEncoder, packetDecoder);
public void SetEncryptorDecryptor(IPacketEncryptor encryptor, IPacketDecryptor decryptor)
=> _connection.SetEncryptorDecryptor(encryptor, decryptor);

public required List<Name> KeyExchangeAlgorithms { get; init; }
public required List<Name> ServerHostKeyAlgorithms { get; init; }
Expand Down
92 changes: 92 additions & 0 deletions src/Tmds.Ssh/PacketEncryptionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// This file is part of Tmds.Ssh which is released under MIT.
// See file LICENSE for full license details.

namespace Tmds.Ssh;

sealed class PacketEncryptionAlgorithm
{
private readonly Func<PacketEncryptionAlgorithm, byte[], byte[], HMacAlgorithm?, byte[], IPacketEncryptor> _createPacketEncryptor;
private readonly Func<PacketEncryptionAlgorithm, SequencePool, byte[], byte[], HMacAlgorithm?, byte[], IPacketDecryptor> _createPacketDecryptor;

private PacketEncryptionAlgorithm(int keyLength, int ivLength,
Func<PacketEncryptionAlgorithm, byte[], byte[], HMacAlgorithm?, byte[], IPacketEncryptor> createPacketEncryptor,
Func<PacketEncryptionAlgorithm, SequencePool, byte[], byte[], HMacAlgorithm?, byte[], IPacketDecryptor> createPacketDecryptor,
bool isAuthenticated = false,
int tagLength = 0)
{
KeyLength = keyLength;
IVLength = ivLength;
IsAuthenticated = isAuthenticated;
TagLength = tagLength;
_createPacketEncryptor = createPacketEncryptor;
_createPacketDecryptor = createPacketDecryptor;
}

public int KeyLength { get; }
public int IVLength { get; }
public bool IsAuthenticated { get; }
public int TagLength { get; } // When IsAuthenticated == true

public IPacketEncryptor CreatePacketEncryptor(byte[] key, byte[] iv, HMacAlgorithm? hmacAlgorithm, byte[] hmacKey)
{
CheckArguments(this, key, iv, hmacAlgorithm, hmacKey);
return _createPacketEncryptor(this, key, iv, hmacAlgorithm, hmacKey);
}

public IPacketDecryptor CreatePacketDecryptor(SequencePool sequencePool, byte[] key, byte[] iv, HMacAlgorithm? hmacAlgorithm, byte[] hmacKey)
{
CheckArguments(this, key, iv, hmacAlgorithm, hmacKey);
return _createPacketDecryptor(this, sequencePool, key, iv, hmacAlgorithm, hmacKey);
}

private static void CheckArguments(PacketEncryptionAlgorithm algorithm, byte[] key, byte[] iv, HMacAlgorithm? hmacAlgorithm, byte[] hmacKey)
{
if (algorithm.IVLength != iv.Length)
{
throw new ArgumentException(nameof(iv));
}
if (algorithm.KeyLength != key.Length)
{
throw new ArgumentException(nameof(key));
}
if (algorithm.IsAuthenticated && hmacAlgorithm is not null)
{
throw new ArgumentException(nameof(hmacAlgorithm));
}
if (hmacAlgorithm is null && hmacKey.Length > 0)
{
throw new ArgumentException(nameof(hmacKey));
}
}

public static PacketEncryptionAlgorithm Find(Name name)
=> _algorithms[name];

private static Dictionary<Name, PacketEncryptionAlgorithm> _algorithms = new()
{
{ AlgorithmNames.Aes128Gcm,
new PacketEncryptionAlgorithm(keyLength: 128 / 8, ivLength: 12,
(PacketEncryptionAlgorithm algorithm, byte[] key, byte[] iv, HMacAlgorithm? hmac, byte[] hmacKey)
=> new AesGcmPacketEncryptor(key, iv, algorithm.TagLength),
(PacketEncryptionAlgorithm algorithm, SequencePool sequencePool, byte[] key, byte[] iv, HMacAlgorithm? hmac, byte[] hmacKey)
=> new AesGcmPacketDecryptor(sequencePool, key, iv, algorithm.TagLength),
isAuthenticated: true,
tagLength: 16) },
{ AlgorithmNames.Aes256Gcm,
new PacketEncryptionAlgorithm(keyLength: 256 / 8, ivLength: 12,
(PacketEncryptionAlgorithm algorithm, byte[] key, byte[] iv, HMacAlgorithm? hmac, byte[] hmacKey)
=> new AesGcmPacketEncryptor(key, iv, algorithm.TagLength),
(PacketEncryptionAlgorithm algorithm, SequencePool sequencePool, byte[] key, byte[] iv, HMacAlgorithm? hmac, byte[] hmacKey)
=> new AesGcmPacketDecryptor(sequencePool, key, iv, algorithm.TagLength),
isAuthenticated: true,
tagLength: 16) },
{ AlgorithmNames.ChaCha20Poly1305,
new PacketEncryptionAlgorithm(keyLength: 512 / 8, ivLength: 0,
(PacketEncryptionAlgorithm algorithm, byte[] key, byte[] iv, HMacAlgorithm? hmac, byte[] hmacKey)
=> new ChaCha20Poly1305PacketEncryptor(key),
(PacketEncryptionAlgorithm algorithm, SequencePool sequencePool, byte[] key, byte[] iv, HMacAlgorithm? hmac, byte[] hmacKey)
=> new ChaCha20Poly1305PacketDecryptor(sequencePool, key),
isAuthenticated: true,
tagLength: ChaCha20Poly1305PacketEncryptor.TagSize) },
};
}
Loading

0 comments on commit 7eb8ad1

Please sign in to comment.