Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public static void ArgumentValidation_MalformedAsnEncoding()

static void AssertThrows(byte[] encodedBytes)
{
CompositeMLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
CompositeMLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => Assert.Throws<CryptographicException>(() => import(encodedBytes)),
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(encodedBytes))));

Expand All @@ -457,7 +457,7 @@ public static void ImportSpki_BerEncoding()
byte[] spki = CompositeMLDsaTestData.GetIetfTestVector(CompositeMLDsaAlgorithm.MLDsa65WithECDsaP384).Spki;
byte[] berSpki = AsnUtils.ConvertDerToNonDerBer(spki);

CompositeMLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(import =>
CompositeMLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(import =>
AssertThrowIfNotSupported(() =>
Assert.Throws<CryptographicException>(() => import(berSpki))));
}
Expand All @@ -474,7 +474,7 @@ public static void Import_WrongAsnType()
algorithmIdentifier.Encode(writer);
byte[] wrongAsnType = writer.Encode();

CompositeMLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
CompositeMLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(wrongAsnType))));

CompositeMLDsaTestHelpers.AssertImportPkcs8PrivateKey(
Expand All @@ -485,15 +485,15 @@ public static void Import_WrongAsnType()
}

[Fact]
public static void ImportSubjectKeyPublicInfo_AlgorithmErrorsInAsn()
public static void ImportSubjectPublicKeyInfo_AlgorithmErrorsInAsn()
{
#if !NETFRAMEWORK // Does not support exporting RSA SPKI
if (!OperatingSystem.IsBrowser())
{
// RSA key
using RSA rsa = RSA.Create();
byte[] rsaSpkiBytes = rsa.ExportSubjectPublicKeyInfo();
CompositeMLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
CompositeMLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(rsaSpkiBytes))));
}
#endif
Expand All @@ -509,17 +509,17 @@ public static void ImportSubjectKeyPublicInfo_AlgorithmErrorsInAsn()
SubjectPublicKey = CompositeMLDsaTestData.GetIetfTestVector(CompositeMLDsaAlgorithm.MLDsa65WithECDsaP384).PublicKey,
};

CompositeMLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
CompositeMLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(spki.Encode()))));

spki.Algorithm.Parameters = AsnUtils.DerNull;

CompositeMLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
CompositeMLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(spki.Encode()))));

// Sanity check
spki.Algorithm.Parameters = null;
CompositeMLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(import => AssertThrowIfNotSupported(() => import(spki.Encode())));
CompositeMLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(import => AssertThrowIfNotSupported(() => import(spki.Encode())));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,17 @@ public void RoundTrip_Export_Import_SPKI(CompositeMLDsaAlgorithm algorithm)
// Generate new key
using CompositeMLDsa dsa = GenerateKey(algorithm);
byte[] publicKey = dsa.ExportCompositeMLDsaPublicKey();
byte[] privateKey = dsa.ExportCompositeMLDsaPrivateKey();

CompositeMLDsaTestHelpers.AssertExportPkcs8PrivateKey(export =>
CompositeMLDsaTestHelpers.AssertImportPkcs8PrivateKey(import =>
CompositeMLDsaTestHelpers.AssertExportSubjectPublicKeyInfo(export =>
CompositeMLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(import =>
{
// Roundtrip it using SPKI
using CompositeMLDsa roundTrippedDsa = import(export(dsa));

// The keys should be the same
Assert.Equal(algorithm, roundTrippedDsa.Algorithm);
AssertExtensions.SequenceEqual(publicKey, roundTrippedDsa.ExportCompositeMLDsaPublicKey());
AssertExtensions.SequenceEqual(privateKey, roundTrippedDsa.ExportCompositeMLDsaPrivateKey());
Assert.Throws<CryptographicException>(() => roundTrippedDsa.ExportCompositeMLDsaPrivateKey());
}));
}

Expand Down Expand Up @@ -282,7 +281,7 @@ public void RoundTrip_Import_Export_PrivateKey(CompositeMLDsaTestData.CompositeM
[MemberData(nameof(CompositeMLDsaTestData.SupportedAlgorithmIetfVectorsTestData), MemberType = typeof(CompositeMLDsaTestData))]
public void RoundTrip_Import_Export_SpkiPublicKey(CompositeMLDsaTestData.CompositeMLDsaTestVector info)
{
CompositeMLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(import =>
CompositeMLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(import =>
CompositeMLDsaTestHelpers.AssertExportSubjectPublicKeyInfo(export =>
CompositeMLDsaTestHelpers.WithDispose(import(info.Spki), dsa =>
AssertExtensions.SequenceEqual(info.Spki, export(dsa)))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ internal static void AssertImportPublicKey(Action<Func<CompositeMLDsa>> testDire
SubjectPublicKey = publicKey,
};

AssertImportSubjectKeyPublicInfo(import => testEmbeddedCall(() => import(spki.Encode())));
AssertImportSubjectPublicKeyInfo(import => testEmbeddedCall(() => import(spki.Encode())));
}

internal delegate CompositeMLDsa ImportSubjectKeyPublicInfoCallback(byte[] spki);
internal static void AssertImportSubjectKeyPublicInfo(Action<ImportSubjectKeyPublicInfoCallback> test) =>
AssertImportSubjectKeyPublicInfo(test, test);
internal delegate CompositeMLDsa ImportSubjectPublicKeyInfoCallback(byte[] spki);
internal static void AssertImportSubjectPublicKeyInfo(Action<ImportSubjectPublicKeyInfoCallback> test) =>
AssertImportSubjectPublicKeyInfo(test, test);

internal static void AssertImportSubjectKeyPublicInfo(
Action<ImportSubjectKeyPublicInfoCallback> testDirectCall,
Action<ImportSubjectKeyPublicInfoCallback> testEmbeddedCall)
internal static void AssertImportSubjectPublicKeyInfo(
Action<ImportSubjectPublicKeyInfoCallback> testDirectCall,
Action<ImportSubjectPublicKeyInfoCallback> testEmbeddedCall)
{
testDirectCall(spki => CompositeMLDsa.ImportSubjectPublicKeyInfo(spki));
testDirectCall(spki => CompositeMLDsa.ImportSubjectPublicKeyInfo(spki.AsSpan()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static void ImportMLDsaPublicKey_WrongSize(MLDsaAlgorithm algorithm)
}

[Fact]
public static void ImportSubjectKeyPublicInfo_NullSource()
public static void ImportSubjectPublicKeyInfo_NullSource()
{
AssertExtensions.Throws<ArgumentNullException>("source", () => MLDsa.ImportSubjectPublicKeyInfo(null));
}
Expand Down Expand Up @@ -153,7 +153,7 @@ public static void ArgumentValidation_MalformedAsnEncoding()

static void AssertThrows(byte[] encodedBytes)
{
MLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
MLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => Assert.Throws<CryptographicException>(() => import(encodedBytes)),
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(encodedBytes))));

Expand All @@ -172,7 +172,7 @@ public static void ImportSpki_BerEncoding()
{
// Valid BER but invalid DER - uses indefinite length encoding
byte[] indefiniteLengthOctet = [0x04, 0x80, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00];
MLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(import =>
MLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(import =>
AssertThrowIfNotSupported(() =>
Assert.Throws<CryptographicException>(() => import(indefiniteLengthOctet))));
}
Expand Down Expand Up @@ -206,7 +206,7 @@ public static void ImportPkcs8_WrongTypeInAsn()
algorithmIdentifier.Encode(writer);
byte[] wrongAsnType = writer.Encode();

MLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
MLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(wrongAsnType))));

MLDsaTestHelpers.AssertImportPkcs8PrivateKey(
Expand All @@ -217,15 +217,15 @@ public static void ImportPkcs8_WrongTypeInAsn()
}

[Fact]
public static void ImportSubjectKeyPublicInfo_AlgorithmErrorsInAsn()
public static void ImportSubjectPublicKeyInfo_AlgorithmErrorsInAsn()
{
#if !NETFRAMEWORK // Does not support exporting RSA SPKI
if (!OperatingSystem.IsBrowser())
{
// RSA key
using RSA rsa = RSA.Create();
byte[] rsaSpkiBytes = rsa.ExportSubjectPublicKeyInfo();
MLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
MLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(rsaSpkiBytes))));
}
#endif
Expand All @@ -241,17 +241,17 @@ public static void ImportSubjectKeyPublicInfo_AlgorithmErrorsInAsn()
SubjectPublicKey = new byte[MLDsaAlgorithm.MLDsa44.PublicKeySizeInBytes]
};

MLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
MLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(spki.Encode()))));

spki.Algorithm.Parameters = AsnUtils.DerNull;

MLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
MLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(spki.Encode()))));

// Sanity check
spki.Algorithm.Parameters = null;
MLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(import => AssertThrowIfNotSupported(() => import(spki.Encode())));
MLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(import => AssertThrowIfNotSupported(() => import(spki.Encode())));
}

[Fact]
Expand Down Expand Up @@ -486,7 +486,7 @@ public void RoundTrip_Import_Export_PrivateSeed(MLDsaKeyInfo info)
[MemberData(nameof(MLDsaTestsData.IetfMLDsaAlgorithms), MemberType = typeof(MLDsaTestsData))]
public void RoundTrip_Import_Export_SPKI(MLDsaKeyInfo info)
{
MLDsaTestHelpers.AssertImportSubjectKeyPublicInfo(import =>
MLDsaTestHelpers.AssertImportSubjectPublicKeyInfo(import =>
MLDsaTestHelpers.AssertExportSubjectPublicKeyInfo(export =>
WithDispose(import(info.Pkcs8PublicKey), mldsa =>
AssertExtensions.SequenceEqual(info.Pkcs8PublicKey, export(mldsa)))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ internal static void AssertImportPublicKey(Action<Func<MLDsa>> testDirectCall, A
SubjectPublicKey = publicKey,
};

AssertImportSubjectKeyPublicInfo(import => testEmbeddedCall(() => import(spki.Encode())));
AssertImportSubjectPublicKeyInfo(import => testEmbeddedCall(() => import(spki.Encode())));
}

internal delegate MLDsa ImportSubjectKeyPublicInfoCallback(byte[] spki);
internal static void AssertImportSubjectKeyPublicInfo(Action<ImportSubjectKeyPublicInfoCallback> test) =>
AssertImportSubjectKeyPublicInfo(test, test);
internal delegate MLDsa ImportSubjectPublicKeyInfoCallback(byte[] spki);
internal static void AssertImportSubjectPublicKeyInfo(Action<ImportSubjectPublicKeyInfoCallback> test) =>
AssertImportSubjectPublicKeyInfo(test, test);

internal static void AssertImportSubjectKeyPublicInfo(
Action<ImportSubjectKeyPublicInfoCallback> testDirectCall,
Action<ImportSubjectKeyPublicInfoCallback> testEmbeddedCall)
internal static void AssertImportSubjectPublicKeyInfo(
Action<ImportSubjectPublicKeyInfoCallback> testDirectCall,
Action<ImportSubjectPublicKeyInfoCallback> testEmbeddedCall)
{
testDirectCall(spki => MLDsa.ImportSubjectPublicKeyInfo(spki));
testDirectCall(spki => MLDsa.ImportSubjectPublicKeyInfo(spki.AsSpan()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void ArgumentValidation_MalformedAsnEncoding()

static void AssertThrows(byte[] encodedBytes)
{
SlhDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
SlhDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => Assert.Throws<CryptographicException>(() => import(encodedBytes)),
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(encodedBytes))));

Expand All @@ -92,7 +92,7 @@ public static void ImportSpki_BerEncoding()
{
// Valid BER but invalid DER - uses indefinite length encoding
byte[] indefiniteLengthOctet = [0x04, 0x80, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00];
SlhDsaTestHelpers.AssertImportSubjectKeyPublicInfo(import =>
SlhDsaTestHelpers.AssertImportSubjectPublicKeyInfo(import =>
AssertThrowIfNotSupported(() =>
Assert.Throws<CryptographicException>(() => import(indefiniteLengthOctet))));
}
Expand All @@ -109,7 +109,7 @@ public static void ImportPkcs8_WrongTypeInAsn()
algorithmIdentifier.Encode(writer);
byte[] wrongAsnType = writer.Encode();

SlhDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
SlhDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(wrongAsnType))));

SlhDsaTestHelpers.AssertImportPkcs8PrivateKey(
Expand All @@ -120,15 +120,15 @@ public static void ImportPkcs8_WrongTypeInAsn()
}

[Fact]
public static void ImportSubjectKeyPublicInfo_AlgorithmErrorsInAsn()
public static void ImportSubjectPublicKeyInfo_AlgorithmErrorsInAsn()
{
#if !NETFRAMEWORK // Does not support exporting RSA SPKI
if (!OperatingSystem.IsBrowser())
{
// RSA key
using RSA rsa = RSA.Create();
byte[] rsaSpkiBytes = rsa.ExportSubjectPublicKeyInfo();
SlhDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
SlhDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(rsaSpkiBytes))));
}
#endif
Expand All @@ -144,17 +144,17 @@ public static void ImportSubjectKeyPublicInfo_AlgorithmErrorsInAsn()
SubjectPublicKey = new byte[SlhDsaAlgorithm.SlhDsaSha2_128s.PublicKeySizeInBytes]
};

SlhDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
SlhDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(spki.Encode()))));

spki.Algorithm.Parameters = AsnUtils.DerNull;

SlhDsaTestHelpers.AssertImportSubjectKeyPublicInfo(
SlhDsaTestHelpers.AssertImportSubjectPublicKeyInfo(
import => AssertThrowIfNotSupported(() => Assert.Throws<CryptographicException>(() => import(spki.Encode()))));

// Sanity check
spki.Algorithm.Parameters = null;
SlhDsaTestHelpers.AssertImportSubjectKeyPublicInfo(import => AssertThrowIfNotSupported(() => import(spki.Encode())));
SlhDsaTestHelpers.AssertImportSubjectPublicKeyInfo(import => AssertThrowIfNotSupported(() => import(spki.Encode())));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,17 @@ public void RoundTrip_Export_Import_SPKI(SlhDsaAlgorithm algorithm)
// Generate new key
using SlhDsa slhDsa = GenerateKey(algorithm);
byte[] publicKey = slhDsa.ExportSlhDsaPublicKey();
byte[] privateKey = slhDsa.ExportSlhDsaPrivateKey();

SlhDsaTestHelpers.AssertExportPkcs8PrivateKey(export =>
SlhDsaTestHelpers.AssertImportPkcs8PrivateKey(import =>
SlhDsaTestHelpers.AssertExportSubjectPublicKeyInfo(export =>
SlhDsaTestHelpers.AssertImportSubjectPublicKeyInfo(import =>
{
// Roundtrip it using SPKI
using SlhDsa roundTrippedSlhDsa = import(export(slhDsa));

// The keys should be the same
Assert.Equal(algorithm, roundTrippedSlhDsa.Algorithm);
AssertExtensions.SequenceEqual(publicKey, roundTrippedSlhDsa.ExportSlhDsaPublicKey());
AssertExtensions.SequenceEqual(privateKey, roundTrippedSlhDsa.ExportSlhDsaPrivateKey());
Assert.Throws<CryptographicException>(() => roundTrippedSlhDsa.ExportSlhDsaPrivateKey());
}));
}

Expand Down Expand Up @@ -268,7 +267,7 @@ public void RoundTrip_Import_Export_PrivateKey(SlhDsaTestData.SlhDsaGeneratedKey
[MemberData(nameof(SlhDsaTestData.GeneratedKeyInfosData), MemberType = typeof(SlhDsaTestData))]
public void RoundTrip_Import_Export_SPKI(SlhDsaTestData.SlhDsaGeneratedKeyInfo info)
{
SlhDsaTestHelpers.AssertImportSubjectKeyPublicInfo(import =>
SlhDsaTestHelpers.AssertImportSubjectPublicKeyInfo(import =>
SlhDsaTestHelpers.AssertExportSubjectPublicKeyInfo(export =>
SlhDsaTestHelpers.WithDispose(import(info.Pkcs8PublicKey), slhDsa =>
AssertExtensions.SequenceEqual(info.Pkcs8PublicKey, export(slhDsa)))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ internal static void AssertImportPublicKey(Action<Func<SlhDsa>> testDirectCall,
SubjectPublicKey = publicKey,
};

AssertImportSubjectKeyPublicInfo(import => testEmbeddedCall(() => import(spki.Encode())));
AssertImportSubjectPublicKeyInfo(import => testEmbeddedCall(() => import(spki.Encode())));
}

internal delegate SlhDsa ImportSubjectKeyPublicInfoCallback(byte[] spki);
internal static void AssertImportSubjectKeyPublicInfo(Action<ImportSubjectKeyPublicInfoCallback> test) =>
AssertImportSubjectKeyPublicInfo(test, test);
internal delegate SlhDsa ImportSubjectPublicKeyInfoCallback(byte[] spki);
internal static void AssertImportSubjectPublicKeyInfo(Action<ImportSubjectPublicKeyInfoCallback> test) =>
AssertImportSubjectPublicKeyInfo(test, test);

internal static void AssertImportSubjectKeyPublicInfo(
Action<ImportSubjectKeyPublicInfoCallback> testDirectCall,
Action<ImportSubjectKeyPublicInfoCallback> testEmbeddedCall)
internal static void AssertImportSubjectPublicKeyInfo(
Action<ImportSubjectPublicKeyInfoCallback> testDirectCall,
Action<ImportSubjectPublicKeyInfoCallback> testEmbeddedCall)
{
testDirectCall(spki => SlhDsa.ImportSubjectPublicKeyInfo(spki));
testDirectCall(spki => SlhDsa.ImportSubjectPublicKeyInfo(spki.AsSpan()));
Expand Down
Loading