Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve AesCmac KAT testing #154

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 124 additions & 1 deletion UnitTests/AesCmac_KAT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,134 @@ sealed class AesCmac_KAT
[TestMethod]
[TestCategory("NIST")]
[NistAesCmacSampleDataSource]
public void NistExample(NistAesCmacSampleTestVector testVector)
public void NistExample_ComputeHash(NistAesCmacSampleTestVector testVector)
{
using var aesCmac = AesCmac.Create();
aesCmac.Key = testVector.Key.ToArray();
var tag = aesCmac.ComputeHash(testVector.PT.ToArray());
CollectionAssert.AreEqual(testVector.Tag.ToArray(), tag);
}

[TestMethod]
[TestCategory("NIST")]
[NistAesCmacSampleDataSource]
public void NistExample_TryHashData(NistAesCmacSampleTestVector testVector)
{
var tag = new byte[testVector.Tag.Length];

var success = AesCmac.TryHashData(testVector.Key.Span, testVector.PT.Span, tag, out var bytesWritten);

Assert.IsTrue(success);
Assert.AreEqual(testVector.Tag.Length, bytesWritten);
CollectionAssert.AreEqual(testVector.Tag.ToArray(), tag);
}

[TestMethod]
[TestCategory("NIST")]
[NistAesCmacSampleDataSource]
public void NistExample_HashData_Array_Array(NistAesCmacSampleTestVector testVector)
{
var tag = AesCmac.HashData(testVector.Key.ToArray(), testVector.PT.ToArray());

CollectionAssert.AreEqual(testVector.Tag.ToArray(), tag);
}

[TestMethod]
[TestCategory("NIST")]
[NistAesCmacSampleDataSource]
public void NistExample_HashData_ReadOnlySpan_ReadOnlySpan(NistAesCmacSampleTestVector testVector)
{
var tag = AesCmac.HashData(testVector.Key.Span, testVector.PT.Span);

CollectionAssert.AreEqual(testVector.Tag.ToArray(), tag);
}

[TestMethod]
[TestCategory("NIST")]
[NistAesCmacSampleDataSource]
public void NistExample_HashData_ReadOnlySpan_ReadOnlySpan_Span(NistAesCmacSampleTestVector testVector)
{
var tag = new byte[testVector.Tag.Length];

var bytesWritten = AesCmac.HashData(testVector.Key.Span, testVector.PT.Span, tag);

Assert.AreEqual(testVector.Tag.Length, bytesWritten);
CollectionAssert.AreEqual(testVector.Tag.ToArray(), tag);
}

[TestMethod]
[TestCategory("NIST")]
[NistAesCmacSampleDataSource]
public void NistExample_HashData_Array_Stream(NistAesCmacSampleTestVector testVector)
{
using var stream = new MemoryStream(testVector.PT.ToArray());

var tag = AesCmac.HashData(testVector.Key.ToArray(), stream);

CollectionAssert.AreEqual(testVector.Tag.ToArray(), tag);
}

[TestMethod]
[TestCategory("NIST")]
[NistAesCmacSampleDataSource]
public void NistExample_HashData_ReadOnlySpan_Stream(NistAesCmacSampleTestVector testVector)
{
using var stream = new MemoryStream(testVector.PT.ToArray());

var tag = AesCmac.HashData(testVector.Key.Span, stream);

CollectionAssert.AreEqual(testVector.Tag.ToArray(), tag);
}

[TestMethod]
[TestCategory("NIST")]
[NistAesCmacSampleDataSource]
public void NistExample_HashData_ReadOnlySpan_Stream_Span(NistAesCmacSampleTestVector testVector)
{
using var stream = new MemoryStream(testVector.PT.ToArray());
var tag = new byte[testVector.Tag.Length];

var bytesWritten = AesCmac.HashData(testVector.Key.Span, stream, tag);

Assert.AreEqual(testVector.Tag.Length, bytesWritten);
CollectionAssert.AreEqual(testVector.Tag.ToArray(), tag);
}

[TestMethod]
[TestCategory("NIST")]
[NistAesCmacSampleDataSource]
public async Task NistExample_HashDataAsync_Array_Stream(NistAesCmacSampleTestVector testVector)
{
using var stream = new MemoryStream(testVector.PT.ToArray());

var tag = await AesCmac.HashDataAsync(testVector.Key.ToArray(), stream);

CollectionAssert.AreEqual(testVector.Tag.ToArray(), tag);
}

[TestMethod]
[TestCategory("NIST")]
[NistAesCmacSampleDataSource]
public async Task NistExample_HashDataAsync_ReadOnlyMemory_Stream(NistAesCmacSampleTestVector testVector)
{
using var stream = new MemoryStream(testVector.PT.ToArray());

var tag = await AesCmac.HashDataAsync(testVector.Key, stream);

CollectionAssert.AreEqual(testVector.Tag.ToArray(), tag);
}

[TestMethod]
[TestCategory("NIST")]
[NistAesCmacSampleDataSource]
public async Task NistExample_HashDataAsync_ReadOnlyMemory_Stream_Memory(NistAesCmacSampleTestVector testVector)
{
using var stream = new MemoryStream(testVector.PT.ToArray());
var tag = new byte[testVector.Tag.Length];

var bytesWritten = await AesCmac.HashDataAsync(testVector.Key, stream, tag);

Assert.AreEqual(testVector.Tag.Length, bytesWritten);
CollectionAssert.AreEqual(testVector.Tag.ToArray(), tag);
}
}
26 changes: 7 additions & 19 deletions UnitTests/AesCmac_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,14 @@ namespace UnitTests;
sealed class AesCmac_Tests
{
const int BLOCKSIZE = 16; // bytes
const int BitsPerByte = 8;

static readonly byte[] TestKey =
[
31, 32, 33, 34, 35, 36, 37, 38,
41, 42, 43, 44, 45, 46, 47, 48,
51, 52, 53, 54, 55, 56, 57, 58
];
static readonly NistAesCmacSampleTestVector TestVector = NistAesCmacSampleTestVector.All.First(
v => v.Key.Length == 192 / BitsPerByte && v.PT.Length > BLOCKSIZE);

static readonly byte[] TestMessage = [1, 2, 3, 4, 5];

static readonly byte[] TestTag = InitializeTestTag();

static byte[] InitializeTestTag()
{
// This is "known good", in the sense that ComputeHash is tested with the SIV vectors.
// This value is subsequently used to verify the one-shot functions.

using var cmac = new AesCmac(TestKey);
return cmac.ComputeHash(TestMessage);
}
static byte[] TestKey => TestVector.Key.ToArray();
static byte[] TestMessage => TestVector.PT.ToArray();
static byte[] TestTag => TestVector.Tag.ToArray();

[TestMethod]
public void Create()
Expand Down Expand Up @@ -218,7 +206,7 @@ public void HashData_Array_Array()
}

[TestMethod]
public void HashData_ReadOnlySpan_Span()
public void HashData_ReadOnlySpan_ReadOnlySpan()
{
using var stream = new MemoryStream(TestMessage);

Expand Down
Loading