2019-11-20 03:05:33 +01:00
|
|
|
|
using System;
|
|
|
|
|
using LibHac.Crypto;
|
2019-11-09 08:32:13 +01:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace LibHac.Tests.CryptoTests
|
|
|
|
|
{
|
|
|
|
|
internal static class Common
|
|
|
|
|
{
|
2021-07-14 08:53:33 +02:00
|
|
|
|
internal delegate ICipher CipherCreator(byte[] key, byte[] iv);
|
|
|
|
|
|
2019-11-09 08:32:13 +01:00
|
|
|
|
internal static void CipherTestCore(byte[] inputData, byte[] expected, ICipher cipher)
|
|
|
|
|
{
|
2021-01-20 03:49:58 +01:00
|
|
|
|
byte[] transformBuffer = new byte[inputData.Length];
|
2019-11-20 03:05:33 +01:00
|
|
|
|
Buffer.BlockCopy(inputData, 0, transformBuffer, 0, inputData.Length);
|
2019-11-09 08:32:13 +01:00
|
|
|
|
|
2019-11-20 03:05:33 +01:00
|
|
|
|
cipher.Transform(transformBuffer, transformBuffer);
|
2019-11-09 08:32:13 +01:00
|
|
|
|
|
2019-11-20 03:05:33 +01:00
|
|
|
|
Assert.Equal(expected, transformBuffer);
|
2019-11-09 08:32:13 +01:00
|
|
|
|
}
|
2019-11-25 21:11:40 +01:00
|
|
|
|
|
2021-07-14 08:53:33 +02:00
|
|
|
|
internal static void EncryptCipherTest(EncryptionTestVector[] testVectors, CipherCreator cipherGenerator)
|
|
|
|
|
{
|
|
|
|
|
foreach (EncryptionTestVector tv in testVectors)
|
|
|
|
|
{
|
|
|
|
|
CipherTestCore(tv.PlainText, tv.CipherText, cipherGenerator(tv.Key, tv.Iv));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void DecryptCipherTest(EncryptionTestVector[] testVectors, CipherCreator cipherGenerator)
|
2019-11-25 21:11:40 +01:00
|
|
|
|
{
|
2021-07-14 08:53:33 +02:00
|
|
|
|
foreach (EncryptionTestVector tv in testVectors)
|
|
|
|
|
{
|
|
|
|
|
CipherTestCore(tv.CipherText, tv.PlainText, cipherGenerator(tv.Key, tv.Iv));
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-25 21:11:40 +01:00
|
|
|
|
|
2021-07-14 08:53:33 +02:00
|
|
|
|
internal static void HashTestCore(ReadOnlySpan<byte> message, byte[] expectedDigest, IHash hash)
|
|
|
|
|
{
|
2021-01-20 03:49:58 +01:00
|
|
|
|
byte[] digestBuffer = new byte[Sha256.DigestSize];
|
2019-11-25 21:11:40 +01:00
|
|
|
|
|
|
|
|
|
hash.Initialize();
|
|
|
|
|
hash.Update(message);
|
|
|
|
|
hash.GetHash(digestBuffer);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(expectedDigest, digestBuffer);
|
|
|
|
|
}
|
2019-11-09 08:32:13 +01:00
|
|
|
|
}
|
|
|
|
|
}
|