PKHeX/PKHeX.Core/Saves/Encryption/SwishCrypto/SwishCrypto.cs
Kurt 67fc0d0885 Misc tweaks
Add Cap Pikachu const value updating for USUM, also set Magearna const for USUM (not only SM)
Remove `NotDistributed` property from WC3 as only CXD events used it, no longer WC3 type.
Delete ArrayUtil; move remaining methods into the StorageUtil class that used them for compressing/inserting bulk into boxes.
Add shiny wormhole flag
Extract byte array strings into spans, faster tests and less overhead.
2024-06-09 08:52:32 -05:00

160 lines
6.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
namespace PKHeX.Core;
/// <summary>
/// MemeCrypto V2 - The Next Generation
/// </summary>
/// <remarks>
/// A variant of <see cref="SaveFile"/> encryption and obfuscation used in <see cref="GameVersion.SWSH"/> and future in-house titles.
/// <br> Individual save blocks are stored in a hash map, with some object-type details prefixing the block's raw data. </br>
/// <br> Once the raw save file data is dumped, the binary is hashed with SHA256 using a static Intro salt and static Outro salt. </br>
/// <br> With the hash computed, the data is encrypted with a repeating irregular-sized static xor cipher. </br>
/// </remarks>
public static class SwishCrypto
{
private const int SIZE_HASH = SHA256.HashSizeInBytes; // 0x20
private static ReadOnlySpan<byte> IntroHashBytes =>
[
0x9E, 0xC9, 0x9C, 0xD7, 0x0E, 0xD3, 0x3C, 0x44, 0xFB, 0x93, 0x03, 0xDC, 0xEB, 0x39, 0xB4, 0x2A,
0x19, 0x47, 0xE9, 0x63, 0x4B, 0xA2, 0x33, 0x44, 0x16, 0xBF, 0x82, 0xA2, 0xBA, 0x63, 0x55, 0xB6,
0x3D, 0x9D, 0xF2, 0x4B, 0x5F, 0x7B, 0x6A, 0xB2, 0x62, 0x1D, 0xC2, 0x1B, 0x68, 0xE5, 0xC8, 0xB5,
0x3A, 0x05, 0x90, 0x00, 0xE8, 0xA8, 0x10, 0x3D, 0xE2, 0xEC, 0xF0, 0x0C, 0xB2, 0xED, 0x4F, 0x6D,
];
private static ReadOnlySpan<byte> Outro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0xA4, 0x48, 0xB3, 0x50, 0x9E, 0x14, 0xA0, 0x52, 0xDE, 0x7E, 0x10, 0x2B, 0x1B, 0x77, 0x6E, 0, // aligned to 0x80
];
public static void CryptStaticXorpadBytes(Span<byte> data)
{
// Apply the xorpad over each chunk of xorpad-sized spans.
// This is 30x as fast as a single loop with a modulus operation (benchmarked; modulo is slower).
// Marshal as a vectorized operation to speed up the process.
// Due to the xorpad being extended 0x7F->0x80, if len%7F==0, we miss the last vectored xor.
// Subtract 1 from the data size in the event that the length is an even multiple, to get one less iteration.
var xp = StaticXorpad;
var xp64 = MemoryMarshal.Cast<byte, Vector<ulong>>(xp);
var size = xp.Length - 1;
int iterations = (data.Length - 1) / size;
do
{
var slice = MemoryMarshal.Cast<byte, Vector<ulong>>(data[..xp.Length]);
for (int i = slice.Length - 1; i >= 0; i--)
slice[i] ^= xp64[i];
data = data[size..];
} while (--iterations != 0);
// Xor the remainder.
for (int i = data.Length - 1; i >= 0; i--)
data[i] ^= xp[i];
}
private static void ComputeHash(ReadOnlySpan<byte> data, Span<byte> hash)
{
using var h = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
h.AppendData(IntroHashBytes);
h.AppendData(data);
h.AppendData(OutroHashBytes);
h.GetCurrentHash(hash);
}
/// <summary>
/// Checks if the file is a rough example of a save file.
/// </summary>
/// <param name="data">Encrypted save data</param>
/// <returns>True if hash matches</returns>
public static bool GetIsHashValid(ReadOnlySpan<byte> data)
{
Span<byte> computed = stackalloc byte[SIZE_HASH];
ComputeHash(data[..^SIZE_HASH], computed);
var stored = data[^computed.Length..];
return computed.SequenceEqual(stored);
}
/// <summary>
/// Decrypts the save data in-place, then unpacks the blocks.
/// </summary>
/// <param name="data">Encrypted save data</param>
/// <returns>Decrypted blocks.</returns>
/// <remarks>
/// Hash is assumed to be valid before calling this method.
/// </remarks>
public static IReadOnlyList<SCBlock> Decrypt(Span<byte> data)
{
// ignore hash
var payload = data[..^SIZE_HASH];
CryptStaticXorpadBytes(payload);
return ReadBlocks(payload);
}
private const int BlockDataRatioEstimate1 = 777; // bytes per block, on average (generous)
private const int BlockDataRatioEstimate2 = 555; // bytes per block, on average (stingy)
private static List<SCBlock> ReadBlocks(ReadOnlySpan<byte> data)
{
var result = new List<SCBlock>(data.Length / BlockDataRatioEstimate2);
int offset = 0;
while (offset < data.Length)
{
var block = SCBlock.ReadFromOffset(data, ref offset);
result.Add(block);
}
return result;
}
/// <summary>
/// Tries to encrypt the save data.
/// </summary>
/// <param name="blocks">Decrypted save data</param>
/// <returns>Encrypted save data.</returns>
public static byte[] Encrypt(IReadOnlyList<SCBlock> blocks)
{
var result = GetDecryptedRawData(blocks);
var span = result.AsSpan();
var payload = span[..^SIZE_HASH];
CryptStaticXorpadBytes(payload);
ComputeHash(payload, span[^SIZE_HASH..]);
return result;
}
/// <summary>
/// Tries to encrypt the save data.
/// </summary>
/// <returns>Raw save data without the final xorpad layer.</returns>
public static byte[] GetDecryptedRawData(IReadOnlyList<SCBlock> blocks)
{
using var ms = new MemoryStream(blocks.Count * BlockDataRatioEstimate1);
using var bw = new BinaryWriter(ms);
foreach (var block in blocks)
block.WriteBlock(bw);
var result = new byte[ms.Position + SIZE_HASH];
var payload = result.AsSpan()[..^SIZE_HASH];
ms.Position = 0;
ms.ReadExactly(payload);
return result;
}
}