Skip to content

Commit

Permalink
reduce camellia allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-heinz committed Jan 10, 2022
1 parent 8667a5a commit c4ede5e
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 157 deletions.
7 changes: 3 additions & 4 deletions Arrowgene.Ddon.Cli/Command/BruteForceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,15 @@ private void ProcessMs(uint ms, int depth)
}

Camellia c = new Camellia();
byte[][] subKey = new byte[34][];
byte[] output = new byte[BlockSize];
for (int j = 0; j < keyBuffer.Length - KeyLength; j++)
{
c.KeySchedule(KeyLengthBits, keyBuffer.Slice(j, KeyLength), subKey);
c.KeySchedule( keyBuffer.Slice(j, KeyLength), out Memory<byte> subKey);
c.CryptBlock(
true,
KeyLength,
KeyLength * 8,
Data,
subKey,
subKey.Span,
output
);
for (int i = 0; i < BlockSize; i++)
Expand Down
32 changes: 27 additions & 5 deletions Arrowgene.Ddon.Server/Network/PacketFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Arrowgene.Ddon.Server.Network
public class PacketFactory
{
private static readonly DdonLogger Logger = LogProvider.Logger<DdonLogger>(typeof(PacketFactory));
private static readonly Camellia _camellia = new Camellia();
private static readonly Camellia Camellia = new Camellia();

private static readonly byte[] CamelliaKey = new byte[]
{
Expand All @@ -28,6 +28,7 @@ public class PacketFactory
private const int PacketLengthFieldSize = 2;
private const int PacketHeaderSize = 9;
private const int PacketMinimumDataSize = 16;
private const uint CamelliaKeyLength = 32 * 8;

private bool _readHeader;
private ushort _dataSize;
Expand All @@ -37,20 +38,26 @@ public class PacketFactory
private readonly ServerSetting _setting;
private byte[] _camelliaKey;
private IPacketIdResolver _packetIdResolver;
private Memory<byte> _t8Encrypt;
private Memory<byte> _t8Decrypt;
private Memory<byte> _camelliaSubKey;


public PacketFactory(ServerSetting setting, IPacketIdResolver packetIdResolver)
{
_setting = setting;
_camelliaKey = CamelliaKey;
_packetCount = 0;
_packetIdResolver = packetIdResolver;
_t8Encrypt = new Memory<byte>(new byte[8]);
_t8Decrypt = new Memory<byte>(new byte[8]);
SetCamelliaKey(CamelliaKey);
Reset();
}

public void SetCamelliaKey(byte[] camelliaKey)
{
_camelliaKey = Copy(camelliaKey);
Camellia.KeySchedule(_camelliaKey, out _camelliaSubKey);
}

public byte[] WriteWithoutHeader(Packet packet)
Expand Down Expand Up @@ -135,14 +142,15 @@ public List<Packet> Read(byte[] data)
Reset();
return packets;
}

_readHeader = true;
}

if (_readHeader && _buffer.Size - _buffer.Position >= _dataSize)
{
byte[] encryptedPacketData = _buffer.ReadBytes(_dataSize);
byte[] packetData = Decrypt(encryptedPacketData);

IBuffer packetBuffer = new StreamBuffer(packetData);
packetBuffer.SetPositionStart();
byte groupId = packetBuffer.ReadByte();
Expand Down Expand Up @@ -203,13 +211,27 @@ public List<Packet> Read(byte[] data)

public byte[] Encrypt(byte[] data)
{
_camellia.Encrypt(data, out Span<byte> encrypted, _camelliaKey, Copy(CamelliaIv));
Camellia.Encrypt(
data,
out Span<byte> encrypted,
CamelliaKeyLength,
_camelliaSubKey.Span,
Copy(CamelliaIv),
_t8Encrypt.Span
);
return encrypted.ToArray();
}

public byte[] Decrypt(byte[] encrypted)
{
_camellia.Decrypt(encrypted, out Span<byte> decrypted, _camelliaKey, Copy(CamelliaIv));
Camellia.Decrypt(
encrypted,
out Span<byte> decrypted,
CamelliaKeyLength,
_camelliaSubKey.Span,
Copy(CamelliaIv),
_t8Decrypt.Span
);
return decrypted.ToArray();
}

Expand Down
Loading

0 comments on commit c4ede5e

Please sign in to comment.