LibHac/tests/LibHac.Tests/CryptoTests/AesXtsTests.cs

76 lines
2.6 KiB
C#
Raw Normal View History

2019-11-16 01:38:55 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
2019-11-19 02:31:06 +01:00
using LibHac.Crypto;
2019-11-16 01:38:55 +01:00
using Xunit;
namespace LibHac.Tests.CryptoTests
{
public class AesXtsTests
{
public static TheoryData<EncryptionTestVector> EncryptTestVectors =
RemovePartialByteTests(RspReader.ReadTestVectors(true, "XTSGenAES128.rsp"));
public static TheoryData<EncryptionTestVector> DecryptTestVectors =
RemovePartialByteTests(RspReader.ReadTestVectors(false, "XTSGenAES128.rsp"));
// The XTS implementation only supports multiples of whole bytes
private static TheoryData<EncryptionTestVector> RemovePartialByteTests(TheoryData<EncryptionTestVector> input)
{
IEnumerable<EncryptionTestVector> filteredTestVectors = input
.Select(x => x[0])
.Cast<EncryptionTestVector>()
.Where(x => x.DataUnitLength % 8 == 0);
var output = new TheoryData<EncryptionTestVector>();
foreach (EncryptionTestVector item in filteredTestVectors)
{
output.Add(item);
}
return output;
}
[Theory]
[MemberData(nameof(EncryptTestVectors))]
public static void Encrypt(EncryptionTestVector tv)
{
Span<byte> key1 = tv.Key.AsSpan(0, 0x10);
Span<byte> key2 = tv.Key.AsSpan(0x10, 0x10);
2019-11-19 02:20:21 +01:00
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateXtsEncryptor(key1, key2, tv.Iv, true));
2019-11-16 01:38:55 +01:00
}
[Theory]
[MemberData(nameof(DecryptTestVectors))]
public static void Decrypt(EncryptionTestVector tv)
{
Span<byte> key1 = tv.Key.AsSpan(0, 0x10);
Span<byte> key2 = tv.Key.AsSpan(0x10, 0x10);
2019-11-19 02:20:21 +01:00
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateXtsDecryptor(key1, key2, tv.Iv, true));
2019-11-16 01:38:55 +01:00
}
[AesIntrinsicsRequiredTheory]
[MemberData(nameof(EncryptTestVectors))]
public static void EncryptIntrinsics(EncryptionTestVector tv)
{
Span<byte> key1 = tv.Key.AsSpan(0, 0x10);
Span<byte> key2 = tv.Key.AsSpan(0x10, 0x10);
2019-11-19 02:20:21 +01:00
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateXtsEncryptor(key1, key2, tv.Iv));
2019-11-16 01:38:55 +01:00
}
[AesIntrinsicsRequiredTheory]
[MemberData(nameof(DecryptTestVectors))]
public static void DecryptIntrinsics(EncryptionTestVector tv)
{
Span<byte> key1 = tv.Key.AsSpan(0, 0x10);
Span<byte> key2 = tv.Key.AsSpan(0x10, 0x10);
2019-11-19 02:20:21 +01:00
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateXtsDecryptor(key1, key2, tv.Iv));
2019-11-16 01:38:55 +01:00
}
}
}