2020-09-06 06:10:39 +02:00
|
|
|
|
using System;
|
|
|
|
|
using LibHac.Crypto;
|
2020-08-20 16:30:47 +02:00
|
|
|
|
using Xunit;
|
2018-12-21 00:09:30 +01:00
|
|
|
|
|
|
|
|
|
namespace LibHac.Tests
|
|
|
|
|
{
|
|
|
|
|
public class AesCmac
|
|
|
|
|
{
|
2018-12-28 18:36:02 +01:00
|
|
|
|
public static readonly TheoryData<TestData> TestVectors = new TheoryData<TestData>
|
2018-12-21 00:09:30 +01:00
|
|
|
|
{
|
|
|
|
|
new TestData
|
|
|
|
|
{
|
2018-12-28 18:36:02 +01:00
|
|
|
|
Key = "2B7E151628AED2A6ABF7158809CF4F3C".ToBytes(),
|
2018-12-21 00:09:30 +01:00
|
|
|
|
Message = "".ToBytes(),
|
2018-12-28 18:36:02 +01:00
|
|
|
|
Expected = "BB1D6929E95937287FA37D129B756746".ToBytes()
|
|
|
|
|
},
|
|
|
|
|
new TestData
|
|
|
|
|
{
|
|
|
|
|
Key = "2B7E151628AED2A6ABF7158809CF4F3C".ToBytes(),
|
|
|
|
|
Message = "6BC1BEE22E409F96E93D7E117393172A".ToBytes(),
|
|
|
|
|
Expected = "070A16B46B4D4144F79BDD9DD04A287C".ToBytes(),
|
|
|
|
|
Length = 0x10
|
|
|
|
|
},
|
|
|
|
|
new TestData
|
|
|
|
|
{
|
|
|
|
|
Key = "2B7E151628AED2A6ABF7158809CF4F3C".ToBytes(),
|
|
|
|
|
Message = "6BC1BEE22E409F96E93D7E117393172AAE2D8A571E03AC9C9EB76FAC45AF8E5130C81C46A35CE411".ToBytes(),
|
|
|
|
|
Expected = "DFA66747DE9AE63030CA32611497C827".ToBytes(),
|
|
|
|
|
Length = 0x28
|
2018-12-21 00:09:30 +01:00
|
|
|
|
},
|
|
|
|
|
new TestData
|
|
|
|
|
{
|
2018-12-28 18:36:02 +01:00
|
|
|
|
Key = "2B7E151628AED2A6ABF7158809CF4F3C".ToBytes(),
|
|
|
|
|
Message = "6BC1BEE22E409F96E93D7E117393172AAE2D8A571E03AC9C9EB76FAC45AF8E5130C81C46A35CE411E5FBC1191A0A52EFF69F2445DF4F9B17AD2B417BE66C3710".ToBytes(),
|
|
|
|
|
Expected = "51F0BEBF7E3B9D92FC49741779363CFE".ToBytes(),
|
|
|
|
|
Length = 0x40
|
2018-12-21 00:09:30 +01:00
|
|
|
|
},
|
|
|
|
|
new TestData
|
|
|
|
|
{
|
2018-12-28 18:36:02 +01:00
|
|
|
|
Key = "2B7E151628AED2A6ABF7158809CF4F3C".ToBytes(),
|
|
|
|
|
Message = "00000000006BC1BEE22E409F96E93D7E117393172A0000000000".ToBytes(),
|
|
|
|
|
Expected = "070A16B46B4D4144F79BDD9DD04A287C".ToBytes(),
|
|
|
|
|
Start = 5,
|
|
|
|
|
Length = 0x10
|
2018-12-21 00:09:30 +01:00
|
|
|
|
},
|
|
|
|
|
new TestData
|
|
|
|
|
{
|
2018-12-28 18:36:02 +01:00
|
|
|
|
Key = "2B7E151628AED2A6ABF7158809CF4F3C".ToBytes(),
|
|
|
|
|
Message = "00000000006BC1BEE22E409F96E93D7E117393172A0000000000000000000000".ToBytes(),
|
|
|
|
|
Expected = "070A16B46B4D4144F79BDD9DD04A287C".ToBytes(),
|
|
|
|
|
Start = 5,
|
|
|
|
|
Length = 0x10
|
2018-12-21 00:09:30 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
[Theory]
|
2018-12-28 18:36:02 +01:00
|
|
|
|
[MemberData(nameof(TestVectors))]
|
|
|
|
|
public static void TestCmacTestVectors(TestData data)
|
2018-12-21 00:09:30 +01:00
|
|
|
|
{
|
|
|
|
|
var actual = new byte[0x10];
|
|
|
|
|
|
2020-09-06 06:10:39 +02:00
|
|
|
|
Aes.CalculateCmac(actual, data.Message.AsSpan(data.Start, data.Length), data.Key);
|
2018-12-21 00:09:30 +01:00
|
|
|
|
|
|
|
|
|
Assert.Equal(data.Expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-28 18:36:02 +01:00
|
|
|
|
public struct TestData
|
2018-12-21 00:09:30 +01:00
|
|
|
|
{
|
|
|
|
|
public byte[] Key;
|
|
|
|
|
public byte[] Message;
|
|
|
|
|
public byte[] Expected;
|
2018-12-28 18:36:02 +01:00
|
|
|
|
public int Start;
|
|
|
|
|
public int Length;
|
2018-12-21 00:09:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|