This commit is contained in:
Mino 2020-03-26 21:55:33 +09:00
parent 16d806a2bb
commit 1775122d03
3 changed files with 41 additions and 16 deletions

View file

@ -15,6 +15,8 @@ namespace Dalamud.Bootstrap.Crypto
/// </remarks> /// </remarks>
internal sealed class Blowfish internal sealed class Blowfish
{ {
public static int BlockSize => 8;
private BlowfishState m_state; private BlowfishState m_state;
/// <summary> /// <summary>
@ -50,7 +52,7 @@ namespace Dalamud.Bootstrap.Crypto
private static bool CheckBufferLength(int length) => length switch private static bool CheckBufferLength(int length) => length switch
{ {
_ when length % 8 == 0 => true, _ when length % BlockSize == 0 => true,
_ => false, _ => false,
}; };

View file

@ -26,6 +26,19 @@ namespace Dalamud.Bootstrap.SqexArg
return this; return this;
} }
public bool ContainsKey(string key) => m_dict.ContainsKey(key);
public bool ContainsValue(string value) => m_dict.ContainsValue(value);
public ArgumentBuilder Remove(string key)
{
m_dict.Remove(key);
return this;
}
public bool TryRemove(string key) => m_dict.Remove(key);
private static void Write(StringBuilder buffer, string key, string value) private static void Write(StringBuilder buffer, string key, string value)
{ {
var escapedKey = EscapeValue(key); var escapedKey = EscapeValue(key);

View file

@ -53,7 +53,7 @@ namespace Dalamud.Bootstrap.SqexArg
} }
/// <summary> /// <summary>
/// /// Extracts the payload and checksum from the encrypted argument.
/// </summary> /// </summary>
/// <param name="argument"></param> /// <param name="argument"></param>
/// <param name="payload">An encrypted payload encoded in url-safe base64 string extracted. The value is undefined if the function fails.</param> /// <param name="payload">An encrypted payload encoded in url-safe base64 string extracted. The value is undefined if the function fails.</param>
@ -83,6 +83,13 @@ namespace Dalamud.Bootstrap.SqexArg
Span<byte> keyBytes = stackalloc byte[8]; Span<byte> keyBytes = stackalloc byte[8];
CreateKey(key, keyBytes); CreateKey(key, keyBytes);
if (!Extract(argument, out var encryptedStr, out var _))
{
throw new SqexArgException($"Could not extract the argument and checksum from {argument}");
}
var encryptedData = DecodeUrlSafeBase64(encryptedStr);
// Allocate the buffer to store decrypted data // Allocate the buffer to store decrypted data
var decryptedData = CreateAlignedBuffer(encryptedData.Length); var decryptedData = CreateAlignedBuffer(encryptedData.Length);
@ -101,25 +108,24 @@ namespace Dalamud.Bootstrap.SqexArg
return new EncryptedArgument(decryptedData); return new EncryptedArgument(decryptedData);
} }
private static IMemoryOwner<byte> CreateAlignedBuffer(int minimumSize)
{
// align (by padding) to block size if needed
throw new NotImplementedException("TODO");
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="decryptedData"></param> /// <param name="minimumSize">Indicates that returned buffer must be larger than `minimumSize` bytes.</param>
/// <returns></returns> /// <returns>
private static bool CheckDecryptedData(ReadOnlySpan<byte> decryptedData) /// A buffer aligned to next multiple of block size.
/// Dispose() must be called when it's not used anymore.
/// </returns>
private static IMemoryOwner<byte> CreateAlignedBuffer(int minimumSize)
{ {
// TODO // align to next multiple of block size.
return false; var alignedSize = (minimumSize + Blowfish.BlockSize - 1) & (~(-Blowfish.BlockSize));
return MemoryPool<byte>.Shared.Rent(alignedSize);
} }
/// <summary> /// <summary>
/// Formats the key. /// Formats a key.
/// </summary> /// </summary>
/// <param name="key">A secret key.</param> /// <param name="key">A secret key.</param>
/// <param name="destination">A buffer where formatted key will be stored. This must be larger than 8 bytes.</param> /// <param name="destination">A buffer where formatted key will be stored. This must be larger than 8 bytes.</param>
@ -127,8 +133,7 @@ namespace Dalamud.Bootstrap.SqexArg
{ {
if (!Utf8Formatter.TryFormat(key, destination, out var _, new StandardFormat('x', 8))) if (!Utf8Formatter.TryFormat(key, destination, out var _, new StandardFormat('x', 8)))
{ {
var message = $"BUG: Could not create a key"; // This should not fail but.. throw new InvalidOperationException("BUG: Could not create a key");
throw new InvalidOperationException(message);
} }
} }
@ -144,5 +149,10 @@ namespace Dalamud.Bootstrap.SqexArg
return Convert.FromBase64String(base64Str); return Convert.FromBase64String(base64Str);
} }
public string Encrypt(uint key)
{
}
} }
} }