76 lines
2.7 KiB
C#
Raw Normal View History

2019-11-15 18:38:55 -06:00
using System;
using System.Collections.Generic;
using System.Linq;
2019-11-18 18:31:06 -07:00
using LibHac.Crypto;
2019-11-15 18:38:55 -06:00
using Xunit;
namespace LibHac.Tests.CryptoTests
{
public class AesXtsTests
{
public static TheoryData<EncryptionTestVector> EncryptTestVectors =
2019-11-25 14:11:40 -06:00
RemovePartialByteTests(RspReader.ReadEncryptionTestVectors(true, "XTSGenAES128.rsp"));
2019-11-15 18:38:55 -06:00
public static TheoryData<EncryptionTestVector> DecryptTestVectors =
2019-11-25 14:11:40 -06:00
RemovePartialByteTests(RspReader.ReadEncryptionTestVectors(false, "XTSGenAES128.rsp"));
2019-11-15 18:38:55 -06:00
// 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-18 18:20:21 -07:00
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateXtsEncryptor(key1, key2, tv.Iv, true));
2019-11-15 18:38:55 -06: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-18 18:20:21 -07:00
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateXtsDecryptor(key1, key2, tv.Iv, true));
2019-11-15 18:38:55 -06: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-18 18:20:21 -07:00
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateXtsEncryptor(key1, key2, tv.Iv));
2019-11-15 18:38:55 -06: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-18 18:20:21 -07:00
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateXtsDecryptor(key1, key2, tv.Iv));
2019-11-15 18:38:55 -06:00
}
}
}