feat: Upgrades networking to use io_uring. (#2315)

> [!IMPORTANT]
> **Breaking Changes**
> - DecodePacket and EncodePacket delegates replaced with IClientEncryption interface
> - NetState.Connection (Socket) replaced with internal RingSocket management
> - NetState.RecvPipe and NetState.SendPipe removed (buffers managed internally)

## Summary

Upgrades the networking stack from PollGroup-based I/O to io_uring, significantly improving I/O performance on Linux.
This also adds native client encryption support for encrypted UO clients.

## Major Changes

io_uring Networking Architecture
- Replaced PollGroup with IORingGroup for async socket I/O operations
- Removed Pipe.cs (mirrored ring buffer) and TcpServer.cs in favor of RingSocketManager
- Added NetState.Network.cs - centralized network infrastructure handling accept, recv, send, and disconnect
completions
- Added SocketHelper.cs - platform-specific socket utilities for raw socket handle operations (getpeername,
getsockname)
- Buffer management now handled by RingSocketManager with configurable slab allocation

### Client Encryption Support
- Added full encryption stack in Network/Encryption/:
  - EncryptionConfig.cs - configurable encryption modes (None, Unencrypted, Encrypted, Both)
  - EncryptionManager.cs - encryption detection and initialization for login/game packets
  - LoginEncryption.cs - handles login packet encryption with version-derived keys
  - GameEncryption.cs - handles game server encryption using Twofish
  - TwofishEngine.cs - optimized Twofish block cipher implementation
  - LoginKeys.cs - encryption key table for client versions
  - IClientEncryption.cs - interface for client encryption implementations

### NetState Improvements
- Replaced Socket Connection with RingSocket _socket for managed socket lifecycle
- Changed from GCHandle polling to event-based completion processing
- Disconnect handling now properly waits for pending sends to flush
- Simplified connecting socket management using lazy queue removal

### Configuration
- New settings: network.encryptionMode and network.encryptionDebug
- Encryption mode flags: Unencrypted, Encrypted, or Both

### Dependencies
- Replaced PollGroup NuGet package with IORingGroup
- Linux requires liburing-dev / liburing-devel package

### Test plan

- Verify server starts and accepts connections on Linux with io_uring
- Verify server starts and accepts connections on Windows (fallback to IOCP)
- Test unencrypted client connections (ClassicUO with encryption disabled)
- Test encrypted client connections if available
- Verify graceful disconnect flushes pending data
- Confirm CI builds pass on all target platforms
This commit is contained in:
Kamron Batman 2026-02-01 16:02:32 -08:00 committed by GitHub
parent 91a553b0bc
commit 3c0d6cb9d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
70 changed files with 2227 additions and 1468 deletions

View file

@ -0,0 +1,45 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: EncryptionConfig.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
namespace Server.Network;
/// <summary>
/// Specifies which encryption modes the server will accept.
/// </summary>
[Flags]
public enum EncryptionMode
{
/// <summary>
/// Encryption handling is disabled. Current behavior.
/// </summary>
None = 0x0,
/// <summary>
/// Accept unencrypted clients (e.g., ClassicUO with encryption disabled).
/// </summary>
Unencrypted = 0x1,
/// <summary>
/// Accept encrypted clients (original UO client, Enhanced Client).
/// </summary>
Encrypted = 0x2,
/// <summary>
/// Auto-detect and accept both encrypted and unencrypted clients.
/// </summary>
Both = Unencrypted | Encrypted
}

View file

@ -0,0 +1,207 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: EncryptionManager.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Buffers.Binary;
using Server.Logging;
namespace Server.Network;
/// <summary>
/// Manages encryption detection and configuration for client connections.
/// </summary>
public static class EncryptionManager
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(EncryptionManager));
private static EncryptionMode _mode = EncryptionMode.None;
private static bool _debug;
/// <summary>
/// Gets whether encryption handling is enabled.
/// </summary>
public static bool Enabled => _mode != EncryptionMode.None;
/// <summary>
/// Gets the current encryption mode.
/// </summary>
public static EncryptionMode Mode => _mode;
/// <summary>
/// Gets whether debug logging is enabled for encryption.
/// </summary>
public static bool Debug => _debug;
/// <summary>
/// Configures encryption settings from server configuration.
/// </summary>
public static void Configure()
{
_mode = ServerConfiguration.GetSetting("network.encryptionMode", EncryptionMode.Both);
_debug = ServerConfiguration.GetSetting("network.encryptionDebug", false);
if (_mode != EncryptionMode.None)
{
logger.Information("Encryption support enabled: {Mode}", _mode);
}
}
/// <param name="ns">The network state.</param>
extension(NetState ns)
{
/// <summary>
/// Detects and initializes encryption for a login packet (0x80).
/// </summary>
/// <param name="buffer">The 62-byte login packet buffer.</param>
/// <param name="encryption">The detected encryption, or null if unencrypted.</param>
/// <returns>True if detection succeeded (encrypted or unencrypted), false if rejected.</returns>
public bool DetectLoginEncryption(ReadOnlySpan<byte> buffer, out IClientEncryption encryption)
{
encryption = null;
if (buffer.Length < 62)
{
return false;
}
// Check if unencrypted:
// - Packet ID is 0x80, OR
// - Username and password null terminators are present
var isUnencrypted = buffer[0] == 0x80 || buffer[30] == 0x00 && buffer[60] == 0x00;
if (isUnencrypted)
{
if (!_mode.HasFlag(EncryptionMode.Unencrypted))
{
if (_debug)
{
logger.Debug("Client {Address}: Unencrypted login rejected (mode: {Mode})", ns.Address, _mode);
}
return false;
}
if (_debug)
{
logger.Debug("Client {Address}: Unencrypted login detected", ns.Address);
}
return true;
}
// Try encrypted
if (!_mode.HasFlag(EncryptionMode.Encrypted))
{
if (_debug)
{
logger.Debug("Client {Address}: Encrypted login rejected (mode: {Mode})", ns.Address, _mode);
}
return false;
}
// Attempt decryption with version-derived keys
if (LoginEncryption.TryDecrypt(ns.Version, (uint)ns.Seed, buffer, out var loginEncryption))
{
encryption = loginEncryption;
if (_debug)
{
logger.Debug("Client {Address}: Encrypted login detected (version: {Version})", ns.Address, ns.Version);
}
return true;
}
if (_debug)
{
logger.Debug("Client {Address}: Login encryption detection failed", ns.Address);
}
return false;
}
/// <summary>
/// Detects and initializes encryption for a game server login packet (0x91).
/// </summary>
/// <param name="buffer">The 65-byte game login packet buffer.</param>
/// <param name="encryption">The detected encryption, or null if unencrypted.</param>
/// <returns>True if detection succeeded (encrypted or unencrypted), false if rejected.</returns>
public bool DetectGameEncryption(ReadOnlySpan<byte> buffer, out IClientEncryption encryption)
{
encryption = null;
if (buffer.Length < 65)
{
return false;
}
// Extract auth ID from packet (bytes 1-4, big-endian)
var authId = BinaryPrimitives.ReadUInt32BigEndian(buffer[1..]);
// Check if unencrypted:
// - Packet ID is 0x91, OR
// - Auth ID equals seed (indicates no encryption applied)
var isUnencrypted = buffer[0] == 0x91 || authId == (uint)ns.Seed;
if (isUnencrypted)
{
if (!_mode.HasFlag(EncryptionMode.Unencrypted))
{
if (_debug)
{
logger.Debug("Client {Address}: Unencrypted game login rejected (mode: {Mode})", ns.Address, _mode);
}
return false;
}
if (_debug)
{
logger.Debug("Client {Address}: Unencrypted game login detected", ns.Address);
}
return true;
}
// Try encrypted
if (!_mode.HasFlag(EncryptionMode.Encrypted))
{
if (_debug)
{
logger.Debug("Client {Address}: Encrypted game login rejected (mode: {Mode})", ns.Address, _mode);
}
return false;
}
// Attempt decryption with seed-derived Twofish
if (GameEncryption.TryDecrypt((uint)ns.Seed, buffer, out var gameEncryption))
{
encryption = gameEncryption;
if (_debug)
{
logger.Debug("Client {Address}: Encrypted game login detected (seed: 0x{Seed:X8})", ns.Address, ns.Seed);
}
return true;
}
if (_debug)
{
logger.Debug("Client {Address}: Game encryption detection failed", ns.Address);
}
return false;
}
}
}

View file

@ -0,0 +1,204 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: GameEncryption.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Security.Cryptography;
using Server.Logging;
namespace Server.Network;
/// <summary>
/// Implements game packet encryption/decryption using Twofish + MD5.
/// Used for all game packets after login (0x91 and onwards).
/// </summary>
public sealed class GameEncryption : IClientEncryption
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(GameEncryption));
private const int CipherTableSize = 256;
private const int BlockSize = 16;
// Static identity table [0..255] for vectorized CopyTo initialization
private static readonly byte[] IdentityTable = CreateIdentityTable();
private static byte[] CreateIdentityTable()
{
var table = new byte[CipherTableSize];
for (var i = 0; i < CipherTableSize; i++)
{
table[i] = (byte)i;
}
return table;
}
private readonly TwofishEngine _twofish;
private readonly byte[] _cipherTable;
private readonly byte[] _xorKey;
private ushort _recvPos;
private byte _sendPos;
public GameEncryption(uint seed)
{
// Create 16-byte key from seed (repeated 4 times)
Span<byte> key = stackalloc byte[16];
key[0] = key[4] = key[8] = key[12] = (byte)((seed >> 24) & 0xFF);
key[1] = key[5] = key[9] = key[13] = (byte)((seed >> 16) & 0xFF);
key[2] = key[6] = key[10] = key[14] = (byte)((seed >> 8) & 0xFF);
key[3] = key[7] = key[11] = key[15] = (byte)(seed & 0xFF);
_twofish = new TwofishEngine(key);
// Initialize cipher table with identity [0..255] using vectorized copy
_cipherTable = GC.AllocateUninitializedArray<byte>(CipherTableSize);
IdentityTable.CopyTo(_cipherTable, 0);
// Encrypt cipher table with Twofish
RefreshCipherTable();
// Compute MD5 hash of cipher table for server->client XOR key
_xorKey = MD5.HashData(_cipherTable);
}
/// <summary>
/// Refreshes the cipher table by encrypting it with Twofish.
/// Called every 256 bytes of received data.
/// </summary>
private void RefreshCipherTable()
{
// Encrypt cipher table in 16-byte blocks
for (var i = 0; i < CipherTableSize; i += BlockSize)
{
_twofish.EncryptBlock(_cipherTable.AsSpan(i, BlockSize));
}
_recvPos = 0;
}
/// <summary>
/// Decrypts incoming data from the client (in-place).
/// XORs with cipher table, refreshing every 256 bytes.
/// </summary>
public void ClientDecrypt(Span<byte> buffer)
{
for (var i = 0; i < buffer.Length; i++)
{
if (_recvPos >= CipherTableSize)
{
RefreshCipherTable();
}
buffer[i] ^= _cipherTable[_recvPos++];
}
}
/// <summary>
/// Encrypts outgoing data to the client (in-place).
/// XORs with MD5 hash of cipher table (16-byte rotating key).
/// Uses SIMD optimization for larger buffers.
/// </summary>
public void ServerEncrypt(Span<byte> buffer)
{
var i = 0;
// SIMD path: process 16 bytes at a time when aligned with key
if (_sendPos == 0 && buffer.Length >= 16 && Vector128.IsHardwareAccelerated)
{
var keyVec = Vector128.Create(_xorKey);
for (; i + 16 <= buffer.Length; i += 16)
{
var chunk = Vector128.LoadUnsafe(ref buffer[i]);
var result = Vector128.Xor(chunk, keyVec);
result.StoreUnsafe(ref buffer[i]);
}
}
// Scalar path for remainder or when not aligned
for (; i < buffer.Length; i++)
{
buffer[i] ^= _xorKey[_sendPos++];
_sendPos &= 0x0F; // Wrap at 16
}
}
/// <summary>
/// Attempts to decrypt a game login packet and validate it.
/// Returns true if the packet appears to be validly encrypted.
/// </summary>
public static bool TryDecrypt(uint seed, ReadOnlySpan<byte> encryptedPacket, out GameEncryption encryption)
{
const int GameLoginPacketSize = 65;
encryption = null;
if (encryptedPacket.Length < GameLoginPacketSize)
{
if (EncryptionManager.Debug)
{
logger.Debug("GameEncryption.TryDecrypt: Invalid buffer length {Length}", encryptedPacket.Length);
}
return false;
}
if (EncryptionManager.Debug)
{
logger.Debug("GameEncryption.TryDecrypt: Seed=0x{Seed:X8}", seed);
logger.Debug("GameEncryption.TryDecrypt: Encrypted[0..16]: {Bytes}", Convert.ToHexString(encryptedPacket[..16]));
}
// Copy and decrypt
Span<byte> decrypted = stackalloc byte[GameLoginPacketSize];
encryptedPacket[..GameLoginPacketSize].CopyTo(decrypted);
var enc = new GameEncryption(seed);
if (EncryptionManager.Debug)
{
logger.Debug("GameEncryption.TryDecrypt: CipherTable[0..16]: {Bytes}",
Convert.ToHexString(enc._cipherTable.AsSpan(0, 16)));
logger.Debug("GameEncryption.TryDecrypt: XorKey: {Bytes}", Convert.ToHexString(enc._xorKey));
}
enc.ClientDecrypt(decrypted);
if (EncryptionManager.Debug)
{
logger.Debug("GameEncryption.TryDecrypt: Decrypted[0..16]: {Bytes}", Convert.ToHexString(decrypted[..16]));
logger.Debug("GameEncryption.TryDecrypt: First byte=0x{Byte:X2} (expected 0x91)", decrypted[0]);
}
// Validate: first byte must be 0x91 (game server login packet ID)
if (decrypted[0] != 0x91)
{
if (EncryptionManager.Debug)
{
logger.Debug("GameEncryption.TryDecrypt: Validation FAILED - first byte is not 0x91");
}
return false;
}
if (EncryptionManager.Debug)
{
logger.Debug("GameEncryption.TryDecrypt: Validation PASSED");
}
// Re-create encryption for actual use
encryption = new GameEncryption(seed);
return true;
}
}

View file

@ -0,0 +1,37 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: IClientEncryption.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
namespace Server.Network;
/// <summary>
/// Interface for client encryption implementations.
/// Uses Span-based API for zero-allocation in the hot path.
/// </summary>
public interface IClientEncryption
{
/// <summary>
/// Decrypts incoming data from the client (in-place).
/// </summary>
/// <param name="buffer">The buffer containing encrypted data. Modified in-place.</param>
void ClientDecrypt(Span<byte> buffer);
/// <summary>
/// Encrypts outgoing data to the client (in-place).
/// </summary>
/// <param name="buffer">The buffer containing plaintext data. Modified in-place.</param>
void ServerEncrypt(Span<byte> buffer);
}

View file

@ -0,0 +1,123 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: LoginEncryption.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
namespace Server.Network;
/// <summary>
/// Implements login packet encryption/decryption using XOR with version-derived keys.
/// Used for the initial account login packet (0x80).
/// </summary>
public sealed class LoginEncryption : IClientEncryption
{
private uint _table1;
private uint _table2;
private readonly uint _key1;
private readonly uint _key2;
public LoginEncryption(uint seed, LoginKeys keys)
{
_key1 = keys.Key1;
_key2 = keys.Key2;
// Initialize state tables from seed
_table1 = ((~seed ^ 0x00001357) << 16) | ((seed ^ 0xFFFFAAAA) & 0x0000FFFF);
_table2 = ((seed ^ 0x43210000) >> 16) | ((~seed ^ 0xABCDFFFF) & 0xFFFF0000);
}
/// <summary>
/// Attempts to initialize login encryption and validate the packet.
/// Returns true if the packet appears to be validly encrypted with this scheme.
/// </summary>
public static bool TryDecrypt(
ClientVersion version,
uint seed,
ReadOnlySpan<byte> encryptedPacket,
out LoginEncryption encryption)
{
const int LoginPacketSize = 62;
encryption = null;
var keys = LoginKeys.GetKeys(version);
if (keys is { Key1: 0, Key2: 0 })
{
return false;
}
if (encryptedPacket.Length < LoginPacketSize)
{
return false;
}
// Copy and decrypt
Span<byte> decrypted = stackalloc byte[LoginPacketSize];
encryptedPacket[..LoginPacketSize].CopyTo(decrypted);
var enc = new LoginEncryption(seed, keys);
enc.ClientDecrypt(decrypted);
// Validate decrypted packet structure:
// - Byte 0 must be 0x80 (account login packet ID)
// - Byte 30 must be 0x00 (null terminator for username)
// - Byte 60 must be 0x00 (null terminator for password)
if (decrypted[0] != 0x80 || decrypted[30] != 0x00 || decrypted[60] != 0x00)
{
return false;
}
// Re-initialize encryption state for actual use
encryption = new LoginEncryption(seed, keys);
return true;
}
/// <summary>
/// Decrypts incoming data from the client (in-place).
/// </summary>
public void ClientDecrypt(Span<byte> buffer)
{
for (var i = 0; i < buffer.Length; i++)
{
buffer[i] ^= (byte)(_table1 & 0xFF);
var edx = _table2;
var esi = _table1 << 31;
var eax = _table2 >> 1;
eax |= esi;
eax ^= _key1 - 1;
edx <<= 31;
eax >>= 1;
var ecx = _table1 >> 1;
eax |= esi;
ecx |= edx;
eax ^= _key1;
ecx ^= _key2;
_table1 = ecx;
_table2 = eax;
}
}
/// <summary>
/// Server does not encrypt login responses, so this is a no-op.
/// </summary>
public void ServerEncrypt(Span<byte> buffer)
{
// Login encryption is client-to-server only
}
}

View file

@ -0,0 +1,89 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: LoginKeys.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Server.Network;
/// <summary>
/// Represents encryption keys derived from a client version.
/// Used for login packet encryption/decryption.
/// </summary>
public readonly struct LoginKeys
{
public static readonly LoginKeys Empty = new(0, 0);
private static readonly Dictionary<ClientVersion, LoginKeys> _cache = [];
public uint Key1 { get; }
public uint Key2 { get; }
private LoginKeys(uint key1, uint key2)
{
Key1 = key1;
Key2 = key2;
}
/// <summary>
/// Gets or computes encryption keys for the specified client version.
/// Results are cached for performance.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static LoginKeys GetKeys(ClientVersion version)
{
if (version == null)
{
return Empty;
}
if (_cache.TryGetValue(version, out var keys))
{
return keys;
}
keys = ComputeKeys(version);
_cache[version] = keys;
return keys;
}
/// <summary>
/// Computes encryption keys from client version using the UO key derivation algorithm.
/// </summary>
private static LoginKeys ComputeKeys(ClientVersion version)
{
uint major = (uint)version.Major;
uint minor = (uint)version.Minor;
uint revision = (uint)version.Revision;
// Key1 derivation
uint key1 = (major << 23) | (minor << 14) | (revision << 4);
key1 ^= (revision * revision) << 9;
key1 ^= minor * minor;
key1 ^= (minor * 11) << 24;
key1 ^= (revision * 7) << 19;
key1 ^= 0x2C13A5FD;
// Key2 derivation
uint key2 = (major << 22) | (revision << 13) | (minor << 3);
key2 ^= (revision * revision * 3) << 10;
key2 ^= minor * minor;
key2 ^= (minor * 13) << 23;
key2 ^= (revision * 7) << 18;
key2 ^= 0xA31D527F;
return new LoginKeys(key1, key2);
}
}

View file

@ -0,0 +1,271 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: TwofishEngine.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Server.Network;
/// <summary>
/// Twofish block cipher implementation for UO encryption.
/// Implements 128-bit block encryption with 128-bit key in ECB mode.
/// Based on the public domain Twofish algorithm by Bruce Schneier et al.
/// </summary>
public sealed class TwofishEngine
{
private const int BlockSize = 16; // 128 bits
private const int Rounds = 16;
private const int InputWhiten = 0;
private const int OutputWhiten = 4;
private const int RoundSubkeys = 8;
private const int TotalSubkeys = RoundSubkeys + 2 * Rounds;
private const uint SkStep = 0x02020202u;
private const uint SkBump = 0x01010101u;
private const int SkRotl = 9;
private const uint RsGfFdbk = 0x14D;
private const int MdsGfFdbk = 0x169;
// P0 and P1 permutation tables
private static readonly byte[] P0 =
{
0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38,
0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C, 0x43, 0x75, 0x37, 0x26, 0x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0, 0xDE, 0xB3, 0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E,
0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 0x81, 0x88, 0xEE, 0x21, 0xC4, 0x1A, 0xEB, 0xD9,
0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01, 0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48,
0x4F, 0xF2, 0x65, 0x8E, 0x78, 0x5C, 0x58, 0x19, 0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64,
0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 0xCE, 0xE9, 0x68, 0x44, 0xE0, 0x4D, 0x43, 0x69,
0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E, 0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC,
0x22, 0xC9, 0xC0, 0x9B, 0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9,
0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 0x16, 0x25, 0x86, 0x56, 0x55, 0x09, 0xBE, 0x91
};
private readonly uint[] _sboxKeys = new uint[2]; // For 128-bit key
private readonly uint[] _subKeys = new uint[TotalSubkeys];
/// <summary>
/// Creates a new Twofish engine with the specified 128-bit key.
/// </summary>
public TwofishEngine(ReadOnlySpan<byte> key)
{
if (key.Length != 16)
{
throw new ArgumentException("Key must be 16 bytes (128 bits)", nameof(key));
}
GenerateSubkeys(MemoryMarshal.Cast<byte, uint>(key));
}
private void GenerateSubkeys(ReadOnlySpan<uint> keyWords)
{
// Split key into even and odd words
var k0 = keyWords[0];
var k1 = keyWords[1];
var k2 = keyWords[2];
var k3 = keyWords[3];
// Compute S-box keys using RS matrix
_sboxKeys[0] = RsMdsEncode(k2, k3);
_sboxKeys[1] = RsMdsEncode(k0, k1);
// Generate round subkeys
for (var i = 0; i < TotalSubkeys / 2; i++)
{
var a = F32((uint)(i * SkStep), k0, k2);
var b = F32((uint)(i * SkStep + SkBump), k1, k3);
b = BitOperations.RotateLeft(b, 8);
_subKeys[2 * i] = a + b;
_subKeys[2 * i + 1] = BitOperations.RotateLeft(a + 2 * b, SkRotl);
}
}
/// <summary>
/// Encrypts a 16-byte block in place.
/// </summary>
public void EncryptBlock(Span<byte> block)
{
if (block.Length < BlockSize)
{
throw new ArgumentException("Block must be at least 16 bytes", nameof(block));
}
var x = MemoryMarshal.Cast<byte, uint>(block);
// Input whitening
x[0] ^= _subKeys[InputWhiten];
x[1] ^= _subKeys[InputWhiten + 1];
x[2] ^= _subKeys[InputWhiten + 2];
x[3] ^= _subKeys[InputWhiten + 3];
// 16 rounds
for (var r = 0; r < Rounds; r++)
{
var t0 = F32Sbox(x[0]);
var t1 = F32Sbox(BitOperations.RotateLeft(x[1], 8));
x[3] = BitOperations.RotateLeft(x[3], 1);
x[2] ^= t0 + t1 + _subKeys[RoundSubkeys + 2 * r];
x[3] ^= t0 + 2 * t1 + _subKeys[RoundSubkeys + 2 * r + 1];
x[2] = BitOperations.RotateRight(x[2], 1);
if (r < Rounds - 1)
{
// Swap for next round
(x[0], x[2]) = (x[2], x[0]);
(x[1], x[3]) = (x[3], x[1]);
}
}
// Output whitening
x[0] ^= _subKeys[OutputWhiten];
x[1] ^= _subKeys[OutputWhiten + 1];
x[2] ^= _subKeys[OutputWhiten + 2];
x[3] ^= _subKeys[OutputWhiten + 3];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private uint F32Sbox(uint x)
{
// For 128-bit key, use 2 S-box keys
// Permutation sequence from Twofish spec (P_ij constants):
// b0: P0[P0[P0[x]^k1]^k0] then P1 final
// b1: P0[P0[P1[x]^k1]^k0] then P0 final
// b2: P1[P1[P0[x]^k1]^k0] then P1 final
// b3: P1[P1[P1[x]^k1]^k0] then P0 final
var b0 = (byte)x;
var b1 = (byte)(x >> 8);
var b2 = (byte)(x >> 16);
var b3 = (byte)(x >> 24);
var k0 = _sboxKeys[0];
var k1 = _sboxKeys[1];
// First layer: P_02=0(P0), P_12=1(P1), P_22=0(P0), P_32=1(P1)
b0 = (byte)(P0[b0] ^ (byte)k1);
b1 = (byte)(P1[b1] ^ (byte)(k1 >> 8));
b2 = (byte)(P0[b2] ^ (byte)(k1 >> 16));
b3 = (byte)(P1[b3] ^ (byte)(k1 >> 24));
// Second layer: P_01=0(P0), P_11=0(P0), P_21=1(P1), P_31=1(P1)
b0 = (byte)(P0[b0] ^ (byte)k0);
b1 = (byte)(P0[b1] ^ (byte)(k0 >> 8));
b2 = (byte)(P1[b2] ^ (byte)(k0 >> 16));
b3 = (byte)(P1[b3] ^ (byte)(k0 >> 24));
// Final layer: P_00=1(P1), P_10=0(P0), P_20=1(P1), P_30=0(P0)
// MDS matrix multiply
return MdsMultiply(P1[b0], P0[b1], P1[b2], P0[b3]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint F32(uint x, uint k0, uint k2)
{
var b0 = (byte)x;
var b1 = (byte)(x >> 8);
var b2 = (byte)(x >> 16);
var b3 = (byte)(x >> 24);
// First layer: P_02=0(P0), P_12=1(P1), P_22=0(P0), P_32=1(P1)
b0 = (byte)(P0[b0] ^ (byte)k2);
b1 = (byte)(P1[b1] ^ (byte)(k2 >> 8));
b2 = (byte)(P0[b2] ^ (byte)(k2 >> 16));
b3 = (byte)(P1[b3] ^ (byte)(k2 >> 24));
// Second layer: P_01=0(P0), P_11=0(P0), P_21=1(P1), P_31=1(P1)
b0 = (byte)(P0[b0] ^ (byte)k0);
b1 = (byte)(P0[b1] ^ (byte)(k0 >> 8));
b2 = (byte)(P1[b2] ^ (byte)(k0 >> 16));
b3 = (byte)(P1[b3] ^ (byte)(k0 >> 24));
// Final layer: P_00=1(P1), P_10=0(P0), P_20=1(P1), P_30=0(P0)
return MdsMultiply(P1[b0], P0[b1], P1[b2], P0[b3]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint MdsMultiply(byte b0, byte b1, byte b2, byte b3)
{
// MDS matrix multiplication (Galois Field 2^8)
var m0 = (uint)(b0 ^ Lfsr2(b1) ^ Lfsr1(b2) ^ Lfsr1(b3));
var m1 = (uint)(Lfsr1(b0) ^ Lfsr2(b1) ^ Lfsr2(b2) ^ b3);
var m2 = (uint)(Lfsr2(b0) ^ Lfsr1(b1) ^ b2 ^ Lfsr2(b3));
var m3 = (uint)(Lfsr2(b0) ^ b1 ^ Lfsr2(b2) ^ Lfsr1(b3));
return m0 | (m1 << 8) | (m2 << 16) | (m3 << 24);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Lfsr1(int val) => val ^ Lfsr4(val);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Lfsr2(int val) => val ^ Lfsr3(val) ^ Lfsr4(val);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Lfsr3(int val) => (val >> 1) ^ ((val & 0x01) == 0x01 ? MdsGfFdbk / 2 : 0);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Lfsr4(int val) =>
(val >> 2) ^ ((val & 0x02) == 0x02 ? MdsGfFdbk / 2 : 0) ^ ((val & 0x01) == 0x01 ? MdsGfFdbk / 4 : 0);
private static uint RsMdsEncode(uint k0, uint k1)
{
uint r = 0;
for (var i = 0; i < 2; i++)
{
r ^= i > 0 ? k0 : k1;
for (var j = 0; j < 4; j++)
{
var v1 = (byte)(r >> 24);
var v2 = (uint)(((v1 << 1) ^ ((v1 & 0x80) == 0x80 ? RsGfFdbk : 0)) & 0xFF);
var v3 = (uint)(((v1 >> 1) & 0x7F) ^ ((v1 & 1) == 1 ? RsGfFdbk >> 1 : 0) ^ v2);
r = (r << 8) ^ (v3 << 24) ^ (v2 << 16) ^ (v3 << 8) ^ v1;
}
}
return r;
}
}