This commit is contained in:
Mino 2020-03-14 01:52:24 +09:00
parent 4a60a5f8a2
commit 33145ea0f2
5 changed files with 106 additions and 62 deletions

View file

@ -19,6 +19,8 @@ namespace Dalamud.Bootstrap
public void Launch(string exePath, string? commandLine) public void Launch(string exePath, string? commandLine)
{ {
commandLine = commandLine ?? "";
throw new NotImplementedException("TODO"); throw new NotImplementedException("TODO");
} }
@ -46,7 +48,7 @@ namespace Dalamud.Bootstrap
using var process = Process.Open(pid); using var process = Process.Open(pid);
var commandLine = process.ReadCommandLine(); var commandLine = process.ReadCommandLine();
if (!ArgumentContainer.Parse(commandLine[1], out var container)) if (!EncodedArgument.Parse(commandLine[1], out var container))
{ {
} }

View file

@ -4,7 +4,7 @@ using System.Runtime.CompilerServices;
namespace Dalamud.Bootstrap.Crypto namespace Dalamud.Bootstrap.Crypto
{ {
internal unsafe partial struct BlowfishState internal unsafe struct BlowfishState
{ {
// References: // References:
// https://www.schneier.com/academic/archives/1994/09/description_of_a_new.html // https://www.schneier.com/academic/archives/1994/09/description_of_a_new.html
@ -171,14 +171,9 @@ namespace Dalamud.Bootstrap.Crypto
private fixed uint m_s1[SSize]; private fixed uint m_s1[SSize];
private fixed uint m_s2[SSize]; private fixed uint m_s2[SSize];
private fixed uint m_s3[SSize]; private fixed uint m_s3[SSize];
}
internal unsafe partial struct BlowfishState
{
public BlowfishState(ReadOnlySpan<byte> key) public BlowfishState(ReadOnlySpan<byte> key)
{ {
CheckKeyLength(key);
// initializes P-array and S-boxes to initial values. // initializes P-array and S-boxes to initial values.
fixed (uint* pSrc = PInit) fixed (uint* pSrc = PInit)
fixed (uint* pDst = m_p) fixed (uint* pDst = m_p)
@ -213,15 +208,15 @@ namespace Dalamud.Bootstrap.Crypto
InitKey(key); InitKey(key);
} }
private void CheckKeyLength(ReadOnlySpan<byte> key) // private void CheckKeyLength(ReadOnlySpan<byte> key)
{ // {
// Supported key sizes: 32448 bits // // Supported key sizes: 32448 bits
// https://en.wikipedia.org/wiki/Blowfish_(cipher)#The_algorithm // // https://en.wikipedia.org/wiki/Blowfish_(cipher)#The_algorithm
if (key.Length < 4 || key.Length > 56) // if (key.Length < 4 || key.Length > 56)
{ // {
throw new ArgumentException("Key length must be between from 32 to 448 bits.", nameof(key)); // throw new ArgumentException("Key length must be between from 32 to 448 bits.", nameof(key));
} // }
} // }
/// <summary> /// <summary>
/// Encrypts a block. /// Encrypts a block.
@ -363,13 +358,10 @@ namespace Dalamud.Bootstrap.Crypto
} }
} }
internal sealed partial class Blowfish internal sealed class Blowfish
{ {
private BlowfishState m_state; private BlowfishState m_state;
}
internal sealed partial class Blowfish
{
/// <summary> /// <summary>
/// Initializes a new instance of the Blowfish class. /// Initializes a new instance of the Blowfish class.
/// </summary> /// </summary>

View file

@ -1,40 +0,0 @@
using System;
namespace Dalamud.Bootstrap.SqexArg
{
internal sealed class ArgumentContainer
{
/// <summary>
///
/// </summary>
/// <param name="argument"></param>
/// <param name="container"></param>
/// <returns></returns>
public static bool Parse(string argument, out ArgumentContainer? container)
{
if (argument.Length < 17)
{
// does not contain: //**sqex003 + payload + checksum + **//
container = null;
return false;
}
if (!argument.StartsWith("//**sqex003") || !argument.EndsWith("**//"))
{
container = null;
return false;
}
var checksum = argument[^5];
var payload = argument[11..^5];
// decode
//Convert.FromBase64String();
//container = new ArgumentContainer(payload, checksum);
container = null;
return true;
}
}
}

View file

@ -0,0 +1,87 @@
using System;
using System.Buffers;
using System.Buffers.Text;
using System.Runtime.InteropServices;
using System.Text;
using Dalamud.Bootstrap.Crypto;
namespace Dalamud.Bootstrap.SqexArg
{
internal sealed class EncodedArgument
{
private static char[] ChecksumTable = new char[]
{
'f', 'X', '1', 'p', 'G', 't', 'd', 'S',
'5', 'C', 'A', 'P', '4', '_', 'V', 'L'
};
private readonly ReadOnlyMemory<byte> m_data;
private readonly uint m_key;
//private readonly Blowfish m_cachedBlowfish;
public EncodedArgument(ReadOnlyMemory<byte> data, uint key)
{
m_data = data;
m_key = key;
}
public static bool TryParse(string argument, out EncodedArgument? value)
{
if (argument.Length <= 17)
{
// does not contain: //**sqex0003 + payload + checksum + **//
value = null;
return false;
}
if (!argument.StartsWith("//**sqex0003") || !argument.EndsWith("**//"))
{
value = null;
return false;
}
var checksum = argument[^5];
var payload = DecodeUrlSafeBase64(argument.Substring(12, argument.Length - 1 - 12 - 4)); // //**sqex0003, checksum, **//
if (!FindPartialKey(checksum, out var partialKey))
{
value = null;
return false;
}
for (var i = 0u; i <= 0xFFF; i++)
{
var key = (i << 20) | partialKey;
}
}
private static bool FindPartialKey(char checksum, out uint recoveredKey)
{
var index = MemoryExtensions.IndexOf(ChecksumTable, checksum);
if (index < 0)
{
recoveredKey = 0;
return false;
}
recoveredKey = (uint) (index << 16);
return true;
}
/// <summary>
/// Converts url safe variant of base64 string to bytes.
/// </summary>
/// <param name="payload">A url-safe variant of base64 string.</param>
private static byte[] DecodeUrlSafeBase64(string payload)
{
var base64Str = payload
.Replace('-', '+')
.Replace('_', '/');
return Convert.FromBase64String(base64Str);
}
}
}

View file

@ -35,6 +35,8 @@ class DalamudBuild : NukeBuild
AbsolutePath OutputDirectory => RootDirectory / "output"; AbsolutePath OutputDirectory => RootDirectory / "output";
//AbsolutePath DefaultDalamudRoot =>
Target Clean => _ => _ Target Clean => _ => _
.Before(Restore) .Before(Restore)
.Executes(() => .Executes(() =>
@ -59,7 +61,7 @@ class DalamudBuild : NukeBuild
.SetAssemblyVersion(GitVersion.AssemblySemVer) .SetAssemblyVersion(GitVersion.AssemblySemVer)
.SetFileVersion(GitVersion.AssemblySemFileVer) .SetFileVersion(GitVersion.AssemblySemFileVer)
.SetInformationalVersion(GitVersion.InformationalVersion) .SetInformationalVersion(GitVersion.InformationalVersion)
/*.EnableNoRestore()*/); .EnableNoRestore());
}); });
Target Install => _ => _ Target Install => _ => _
@ -67,5 +69,6 @@ class DalamudBuild : NukeBuild
.Executes(() => .Executes(() =>
{ {
// TODO // TODO
Direct
}); });
} }