Skip to content

Commit

Permalink
remember to log exact failure reason in blte
Browse files Browse the repository at this point in the history
  • Loading branch information
ZingBallyhoo committed Dec 2, 2023
1 parent 9c2513a commit 6497f65
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions TACTLib/Core/BLTEDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public static unsafe byte[] Decode(ClientHandler client, Span<byte> src) {

private static void HandleDataBlock(ClientHandler client, Span<byte> inputData, Span<byte> outputData, int blockIndex)
{
switch ((char)SpanHelper.ReadByte(ref inputData)) {
var blockType = (char)SpanHelper.ReadByte(ref inputData);
switch (blockType) {
case 'E': // E (encrypted)
var decrypted = Decrypt(client, inputData, blockIndex);
HandleDataBlock(client, decrypted, outputData, blockIndex);
Expand All @@ -107,15 +108,15 @@ private static void HandleDataBlock(ClientHandler client, Span<byte> inputData,
case 'F': // F (frame, recursive)
throw new BLTEDecoderException(null, "DecoderFrame not implemented");
default:
throw new BLTEDecoderException(null, "unknown BLTE block type {0} (0x{1:X2})!", (char) inputData[0], inputData[0]);
throw new BLTEDecoderException(null, $"unknown BLTE block type {blockType} (0x{(byte)blockType:X2})!");
}
}

private static Span<byte> Decrypt(ClientHandler client, Span<byte> data, int blockIndex)
{
var keyNameSize = SpanHelper.ReadByte(ref data);
if (keyNameSize == 0 || keyNameSize != 8)
throw new BLTEDecoderException(null, "keyNameSize == 0 || keyNameSize != 8");
if (keyNameSize != 8)
throw new BLTEDecoderException(null, $"keyNameSize != 8. got {keyNameSize}");

var keyNameData = SpanHelper.Advance(ref data, keyNameSize);
var keyName = BinaryPrimitives.ReadUInt64LittleEndian(keyNameData);
Expand All @@ -131,7 +132,7 @@ private static Span<byte> Decrypt(ClientHandler client, Span<byte> data, int blo

var encType = SpanHelper.ReadByte(ref data);
if (encType != EncryptionSalsa20 && encType != EncryptionArc4) // 'S' or 'A'
throw new BLTEDecoderException(null, "encType != ENCRYPTION_SALSA20 && encType != ENCRYPTION_ARC4");
throw new BLTEDecoderException(null, $"encType != ENCRYPTION_SALSA20 && encType != ENCRYPTION_ARC4. got {(char)encType}");

// expand to 8 bytes
Span<byte> iv = stackalloc byte[8];
Expand All @@ -149,7 +150,7 @@ private static Span<byte> Decrypt(ClientHandler client, Span<byte> data, int blo
return data;
} else
{
throw new BLTEDecoderException(null, $"encType {encType} not implemented");
throw new BLTEDecoderException(null, $"encType {(char)encType} not implemented");
}
}

Expand Down
4 changes: 2 additions & 2 deletions TACTLib/Core/Salsa20.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using CommunityToolkit.HighPerformance;

namespace TACTLib.Core
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public Salsa20(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv) {

public void Transform(ReadOnlySpan<byte> input, Span<byte> output) {
var hashOutput = new UIntArray();
var hashOutputBytes = MemoryMarshal.Cast<uint, byte>(hashOutput);
var hashOutputBytes = ((ReadOnlySpan<uint>)hashOutput).AsBytes();

while (input.Length > 0) {
Hash(ref hashOutput, ref m_state);
Expand Down

0 comments on commit 6497f65

Please sign in to comment.