-
-
Notifications
You must be signed in to change notification settings - Fork 706
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow external consumers to specify AES implementation (#4311)
Allow external consumers to specify AES/MD5 implementation HOME: Replace direct usage of transforms with built-in wrapper methods for easier API replacement. BDSP: Replace incremental hash with one-liner for easier API replacement. Handle dirtying manually.
- Loading branch information
1 parent
298c83b
commit 6de68ac
Showing
6 changed files
with
117 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
PKHeX.Core/Saves/Encryption/Providers/IAesCryptographyProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Security.Cryptography; | ||
|
||
namespace PKHeX.Core; | ||
|
||
/// <summary> | ||
/// Provide an implementation of the Aes algorithm | ||
/// </summary> | ||
/// <remarks> | ||
/// <p>The <see cref="Default"/> property will use the .NET implementation that will return an implementation that is specific for each platform except browser (web assembly).</p> | ||
/// <p>This interface is intended to allow any runtime that's not supported to provide its own implementation.</p> | ||
/// <p>See more at https://learn.microsoft.com/en-us/dotnet/core/compatibility/cryptography/5.0/cryptography-apis-not-supported-on-blazor-webassembly</p> | ||
/// </remarks> | ||
public interface IAesCryptographyProvider | ||
{ | ||
IAes Create(byte[] key, CipherMode mode, PaddingMode padding, byte[]? iv = null); | ||
|
||
internal static readonly IAesCryptographyProvider Default = new DefaultAes(); | ||
|
||
public interface IAes : IDisposable | ||
{ | ||
void EncryptEcb(ReadOnlySpan<byte> plaintext, Span<byte> destination); | ||
void DecryptEcb(ReadOnlySpan<byte> ciphertext, Span<byte> destination); | ||
void EncryptCbc(ReadOnlySpan<byte> plaintext, Span<byte> destination); | ||
void DecryptCbc(ReadOnlySpan<byte> ciphertext, Span<byte> destination); | ||
} | ||
|
||
private sealed class DefaultAes : IAesCryptographyProvider | ||
{ | ||
public IAes Create(byte[] key, CipherMode mode, PaddingMode padding, byte[]? iv = null) => new AesSession(key, mode, padding, iv); | ||
|
||
private class AesSession : IAes | ||
{ | ||
private readonly Aes _aes = Aes.Create(); | ||
|
||
public AesSession(byte[] key, CipherMode mode, PaddingMode padding, byte[]? iv) | ||
{ | ||
_aes.Mode = mode; | ||
_aes.Padding = padding; | ||
_aes.Key = key; | ||
if (iv != null) | ||
_aes.IV = iv; | ||
} | ||
|
||
public void Dispose() => _aes.Dispose(); | ||
public void EncryptEcb(ReadOnlySpan<byte> plaintext, Span<byte> destination) => _aes.EncryptEcb(plaintext, destination, _aes.Padding); | ||
public void DecryptEcb(ReadOnlySpan<byte> ciphertext, Span<byte> destination) => _aes.DecryptEcb(ciphertext, destination, _aes.Padding); | ||
public void EncryptCbc(ReadOnlySpan<byte> plaintext, Span<byte> destination) => _aes.EncryptCbc(plaintext, _aes.IV, destination, _aes.Padding); | ||
public void DecryptCbc(ReadOnlySpan<byte> ciphertext, Span<byte> destination) => _aes.DecryptCbc(ciphertext, _aes.IV, destination, _aes.Padding); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Security.Cryptography; | ||
|
||
namespace PKHeX.Core; | ||
|
||
public interface IMd5Provider | ||
{ | ||
void HashData(ReadOnlySpan<byte> source, Span<byte> destination); | ||
|
||
internal static readonly IMd5Provider Default = new DefaultMd5(); | ||
|
||
private sealed class DefaultMd5 : IMd5Provider | ||
{ | ||
public void HashData(ReadOnlySpan<byte> source, Span<byte> destination) => MD5.HashData(source, destination); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
PKHeX.Core/Saves/Encryption/Providers/RuntimeCryptographyProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace PKHeX.Core; | ||
|
||
/// <summary> | ||
/// Holds the singleton instance that provides the AES implementation to the app running this library | ||
/// </summary> | ||
public static class RuntimeCryptographyProvider | ||
{ | ||
public static IAesCryptographyProvider Aes { get; set; } = IAesCryptographyProvider.Default; | ||
public static IMd5Provider Md5 { get; set; } = IMd5Provider.Default; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters