2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
2023-07-15 18:22:48 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2022-01-03 05:35:59 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Genius Sonority's logic for <see cref="GameVersion.XD"/> and <see cref="GameVersion.BATREV"/> encryption.
|
|
|
|
/// </summary>
|
|
|
|
public static class GeniusCrypto
|
2019-03-31 04:28:32 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void ReadKeys(ReadOnlySpan<byte> input, Span<ushort> keys)
|
2019-03-31 04:28:32 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int i = 0; i < keys.Length; i++)
|
|
|
|
keys[i] = ReadUInt16BigEndian(input[(i * 2)..]);
|
|
|
|
}
|
2019-03-31 04:28:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void Decrypt(ReadOnlySpan<byte> input, Span<byte> output, Span<ushort> keys)
|
|
|
|
{
|
2023-12-09 23:21:10 +00:00
|
|
|
ArgumentOutOfRangeException.ThrowIfNotEqual(keys.Length, 4);
|
2022-03-26 20:28:29 +00:00
|
|
|
|
2023-07-15 18:22:48 +00:00
|
|
|
var in16 = MemoryMarshal.Cast<byte, ushort>(input);
|
|
|
|
var out16 = MemoryMarshal.Cast<byte, ushort>(output);
|
2022-06-18 18:04:24 +00:00
|
|
|
int i = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
foreach (var key in keys)
|
2019-03-31 04:28:32 +00:00
|
|
|
{
|
2023-07-15 18:22:48 +00:00
|
|
|
var value = in16[i];
|
|
|
|
if (BitConverter.IsLittleEndian)
|
|
|
|
value = ReverseEndianness(value);
|
2022-06-18 18:04:24 +00:00
|
|
|
value -= key;
|
2023-07-15 18:22:48 +00:00
|
|
|
if (BitConverter.IsLittleEndian)
|
|
|
|
value = ReverseEndianness(value);
|
|
|
|
out16[i] = value;
|
|
|
|
i++;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
AdvanceKeys(keys);
|
2023-07-15 18:22:48 +00:00
|
|
|
} while (i != in16.Length);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-03-31 04:28:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void Encrypt(ReadOnlySpan<byte> input, Span<byte> output, Span<ushort> keys)
|
|
|
|
{
|
2023-12-09 23:21:10 +00:00
|
|
|
ArgumentOutOfRangeException.ThrowIfNotEqual(keys.Length, 4);
|
2019-03-31 04:28:32 +00:00
|
|
|
|
2023-07-15 18:22:48 +00:00
|
|
|
var in16 = MemoryMarshal.Cast<byte, ushort>(input);
|
|
|
|
var out16 = MemoryMarshal.Cast<byte, ushort>(output);
|
2022-06-18 18:04:24 +00:00
|
|
|
int i = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
foreach (var key in keys)
|
2019-03-31 04:28:32 +00:00
|
|
|
{
|
2023-07-15 18:22:48 +00:00
|
|
|
var value = in16[i];
|
|
|
|
if (BitConverter.IsLittleEndian)
|
|
|
|
value = ReverseEndianness(value);
|
2022-06-18 18:04:24 +00:00
|
|
|
value += key;
|
2023-07-15 18:22:48 +00:00
|
|
|
if (BitConverter.IsLittleEndian)
|
|
|
|
value = ReverseEndianness(value);
|
|
|
|
out16[i] = value;
|
|
|
|
i++;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
AdvanceKeys(keys);
|
2023-07-15 18:22:48 +00:00
|
|
|
} while (i != in16.Length);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-03-31 04:28:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void AdvanceKeys(Span<ushort> keys)
|
|
|
|
{
|
|
|
|
var k3 = keys[3] + 0x13;
|
|
|
|
var k2 = keys[2] + 0x17;
|
|
|
|
var k1 = keys[1] + 0x29;
|
|
|
|
var k0 = keys[0] + 0x43;
|
2019-03-31 04:28:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Rotate 4bit groups across the diagonal [ / ] after biasing each u16 (no overflow):
|
|
|
|
// 0123 FB73
|
|
|
|
// 4567 EA62
|
|
|
|
// 89AB becomes D951
|
|
|
|
// CDEF C840
|
|
|
|
// We can leave our intermediary types as int as the bit-masks remove any overflow.
|
2019-03-31 04:28:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
keys[3] = (ushort)(((k0 >> 12) & 0xf) | ((k1 >> 8) & 0xf0) | ((k2 >> 4) & 0xf00) | ( k3 & 0xf000));
|
|
|
|
keys[2] = (ushort)(((k0 >> 08) & 0xf) | ((k1 >> 4) & 0xf0) | ( k2 & 0xf00) | ((k3 << 04) & 0xf000));
|
|
|
|
keys[1] = (ushort)(((k0 >> 04) & 0xf) | ( k1 & 0xf0) | ((k2 << 4) & 0xf00) | ((k3 << 08) & 0xf000));
|
|
|
|
keys[0] = (ushort)(( k0 & 0xf) | ((k1 << 4) & 0xf0) | ((k2 << 8) & 0xf00) | ((k3 << 12) & 0xf000));
|
2019-03-31 04:28:32 +00:00
|
|
|
}
|
2021-01-08 00:01:34 +00:00
|
|
|
}
|