Skip to content
Open
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 @@ -14,16 +14,15 @@ public CurveDef() { }
public bool RequiredOnPlatform;
public string DisplayName;

public bool IsCurveValidOnPlatform
{
get
{
// Assume curve is valid if required; tests will fail if not present
return RequiredOnPlatform ||
(Curve.IsNamed && (EcDsa.Tests.ECDsaFactory.IsCurveValid(Curve.Oid) || EcDiffieHellman.Tests.ECDiffieHellmanFactory.IsCurveValid(Curve.Oid))) ||
(Curve.IsExplicit && (EcDsa.Tests.ECDsaFactory.ExplicitCurvesSupported || EcDiffieHellman.Tests.ECDiffieHellmanFactory.ExplicitCurvesSupported));
}
}
public bool IsCurveValidOnPlatform(EcDsa.Tests.ECDsaProvider provider) =>
RequiredOnPlatform ||
(Curve.IsNamed && provider.IsCurveValid(Curve.Oid)) ||
(Curve.IsExplicit && provider.ExplicitCurvesSupported);

public bool IsCurveValidOnPlatform(EcDiffieHellman.Tests.ECDiffieHellmanProvider provider) =>
RequiredOnPlatform ||
(Curve.IsNamed && provider.IsCurveValid(Curve.Oid)) ||
(Curve.IsExplicit && provider.ExplicitCurvesSupported);

public bool IsCurveTypeEqual(ECCurve.ECCurveType actual)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,20 @@ public abstract partial class ECKeyFileTests<T> where T : ECAlgorithm
protected virtual Func<T, byte[]> PublicKeyWriteArrayFunc { get; } = null;
protected virtual WriteKeyToSpanFunc PublicKeyWriteSpanFunc { get; } = null;

// This would need to be virtualized if there was ever a platform that
// allowed explicit in ECDH or ECDSA but not the other.
public static bool SupportsExplicitCurves { get; } =
EcDiffieHellman.Tests.ECDiffieHellmanFactory.ExplicitCurvesSupported ||
EcDiffieHellman.Tests.ECDiffieHellmanFactory.ExplicitCurvesSupportFailOnUseOnly;
protected abstract bool SupportsExplicitCurves { get; }
protected abstract bool CanDeriveNewPublicKey { get; }

public static bool CanDeriveNewPublicKey { get; } = EcDiffieHellman.Tests.ECDiffieHellmanFactory.CanDeriveNewPublicKey;

public static bool SupportsBrainpool { get; } = IsCurveSupported(ECCurve.NamedCurves.brainpoolP160r1.Oid);
public static bool SupportsSect163k1 { get; } = IsCurveSupported(EccTestData.Sect163k1Key1.Curve.Oid);
public static bool SupportsSect283k1 { get; } = IsCurveSupported(EccTestData.Sect283k1Key1.Curve.Oid);
public static bool SupportsC2pnb163v1 { get; } = IsCurveSupported(EccTestData.C2pnb163v1Key1.Curve.Oid);
public bool SupportsBrainpool => IsCurveSupported(ECCurve.NamedCurves.brainpoolP160r1.Oid);
public bool SupportsSect163k1 => IsCurveSupported(EccTestData.Sect163k1Key1.Curve.Oid);
public bool SupportsSect283k1 => IsCurveSupported(EccTestData.Sect283k1Key1.Curve.Oid);
public bool SupportsC2pnb163v1 => IsCurveSupported(EccTestData.C2pnb163v1Key1.Curve.Oid);

// Some platforms support explicitly specifying these curves, but do not support specifying them by name.
public static bool ExplicitNamedSameSupport { get; } = !PlatformDetection.IsAndroid;
public static bool SupportsSect163k1Explicit { get; } = SupportsSect163k1 || (!ExplicitNamedSameSupport && SupportsExplicitCurves);
public static bool SupportsC2pnb163v1Explicit { get; } = SupportsC2pnb163v1 || (!ExplicitNamedSameSupport && SupportsExplicitCurves);
public bool ExplicitNamedSameSupport => !PlatformDetection.IsAndroid;
public bool SupportsSect163k1Explicit => SupportsSect163k1 || (!ExplicitNamedSameSupport && SupportsExplicitCurves);
public bool SupportsC2pnb163v1Explicit => SupportsC2pnb163v1 || (!ExplicitNamedSameSupport && SupportsExplicitCurves);

private static bool IsCurveSupported(Oid oid)
{
return EcDiffieHellman.Tests.ECDiffieHellmanFactory.IsCurveValid(oid);
}
protected abstract bool IsCurveSupported(Oid oid);

[Theory]
[InlineData(false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,72 +22,32 @@ public abstract class EccTestBase
internal const string ECDSA_P521_OID_VALUE = "1.3.132.0.35"; // Also called nistP521 or secP521r1
internal const string ECDSA_Sect193r1_OID_VALUE = "1.3.132.0.24"; //Char-2 curve

public static IEnumerable<object[]> TestCurvesFull
public static IEnumerable<object[]> AllTestCurves
{
get
{
var curveDefs =
from curveDef in TestCurvesRaw
where curveDef.IsCurveValidOnPlatform == true
select curveDef;

foreach (CurveDef cd in curveDefs)
foreach (CurveDef cd in PublicTestCurvesRaw)
yield return new[] { cd };

// return again with IncludePrivate = true
foreach (CurveDef cd in curveDefs)
foreach (CurveDef cd in PublicTestCurvesRaw)
{
cd.IncludePrivate = true;
yield return new[] { cd };
}
}
}

public static IEnumerable<object[]> TestCurves
public static IEnumerable<object[]> PublicTestCurves
{
get
{
var curveDefs =
from curveDef in TestCurvesRaw
where curveDef.IsCurveValidOnPlatform == true
select curveDef;

foreach (CurveDef curveDef in curveDefs)
yield return new[] { curveDef };
}
}

public static IEnumerable<object[]> TestInvalidCurves
{
get
{
var curveDefs =
from curveDef in TestCurvesRaw
where curveDef.IsCurveValidOnPlatform == false
select curveDef;

foreach (CurveDef curveDef in curveDefs)
yield return new[] { curveDef };
}
}

public static IEnumerable<object[]> TestNewCurves
{
get
{
var curveDefs =
from curveDef in TestCurvesRaw
where
curveDef.IsCurveValidOnPlatform == true &&
curveDef.RequiredOnPlatform == false
select curveDef;

foreach (CurveDef curveDef in curveDefs)
yield return new[] { curveDef };
foreach (CurveDef cd in PublicTestCurvesRaw)
yield return new[] { cd };
}
}

private static IEnumerable<CurveDef> TestCurvesRaw
protected static IEnumerable<CurveDef> PublicTestCurvesRaw
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ namespace System.Security.Cryptography.EcDiffieHellman.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
[ActiveIssue("https://github.com/dotnet/runtime/issues/64389", TestPlatforms.Windows)]
public class ECDhKeyFileTests : ECKeyFileTests<ECDiffieHellman>
public abstract class ECDhKeyFileTests : ECKeyFileTests<ECDiffieHellman>
{
protected abstract ECDiffieHellmanProvider ECDiffieHellmanFactory { get; }

protected override ECDiffieHellman CreateKey() => ECDiffieHellmanFactory.Create();
protected override void Exercise(ECDiffieHellman key) => key.Exercise();
protected override bool CanDeriveNewPublicKey => ECDiffieHellmanFactory.CanDeriveNewPublicKey;
protected override bool SupportsExplicitCurves =>
ECDiffieHellmanFactory.ExplicitCurvesSupported || ECDiffieHellmanFactory.ExplicitCurvesSupportFailOnUseOnly;
protected override bool IsCurveSupported(Oid oid) => ECDiffieHellmanFactory.IsCurveValid(oid);

protected override Func<ECDiffieHellman, byte[]> PublicKeyWriteArrayFunc { get; } =
key =>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@
namespace System.Security.Cryptography.EcDiffieHellman.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public static class ECDiffieHellmanFactoryTests
public abstract class ECDiffieHellmanFactoryTests
{
protected abstract ECDiffieHellmanProvider ECDiffieHellmanFactory { get; }

[Fact]
public static void ECDiffieHellmanCreateDefault_Equals_SameInstance()
public void ECDiffieHellmanCreateDefault_Equals_SameInstance()
{
using ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create();
AssertExtensions.TrueExpression(ecdh.Equals(ecdh));
}

[Fact]
public static void ECDiffieHellmanCreateKeySize_Equals_SameInstance()
public void ECDiffieHellmanCreateKeySize_Equals_SameInstance()
{
using ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create(256);
AssertExtensions.TrueExpression(ecdh.Equals(ecdh));
}

[Fact]
public static void ECDiffieHellmanCreateKeySize_Equals_DifferentInstance_FalseForSameKeyMaterial()
public void ECDiffieHellmanCreateKeySize_Equals_DifferentInstance_FalseForSameKeyMaterial()
{
using ECDiffieHellman ecdh1 = ECDiffieHellmanFactory.Create();
using ECDiffieHellman ecdh2 = ECDiffieHellmanFactory.Create();
Expand All @@ -35,7 +37,7 @@ public static void ECDiffieHellmanCreateKeySize_Equals_DifferentInstance_FalseFo

#if NET
[Fact]
public static void ECDiffieHellmanCreateCurve_Equals_SameInstance()
public void ECDiffieHellmanCreateCurve_Equals_SameInstance()
{
using ECDiffieHellman ecdh = ECDiffieHellmanFactory.Create(ECCurve.NamedCurves.nistP256);
AssertExtensions.TrueExpression(ecdh.Equals(ecdh));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
using System.Security.Cryptography.Tests;
using Xunit;

namespace System.Security.Cryptography.EcDsa.Tests
namespace System.Security.Cryptography.EcDiffieHellman.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public sealed class ECDiffieHellmanKeyPemTests : ECKeyPemTests<ECDiffieHellman>
public abstract class ECDiffieHellmanKeyPemTests : ECKeyPemTests<ECDiffieHellman>
{
protected override ECDiffieHellman CreateKey() => ECDiffieHellman.Create();
protected abstract ECDiffieHellmanProvider ECDiffieHellmanFactory { get; }

protected override ECDiffieHellman CreateKey() => ECDiffieHellmanFactory.Create();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Win32.SafeHandles;

namespace System.Security.Cryptography.EcDiffieHellman.Tests
{
public abstract class ECDiffieHellmanProvider
{
public abstract ECDiffieHellman Create();
public abstract ECDiffieHellman Create(int keySize);
public abstract ECDiffieHellman Create(ECCurve curve);
public abstract bool IsCurveValid(Oid oid);
public abstract bool ExplicitCurvesSupported { get; }

// In OSSL 3+ we use EVP_PKEY APIs instead of EC_KEY APIs so import and export of explicit curves also fails for SymCrypt.
public bool ExplicitCurvesSupportFailOnUseOnly => PlatformDetection.IsSymCryptOpenSsl && SafeEvpPKeyHandle.OpenSslVersion < 0x3_00_00_00_0;

public abstract bool CanDeriveNewPublicKey { get; }
public abstract bool SupportsRawDerivation { get; }
public abstract bool SupportsSha3 { get; }
}
}
Loading
Loading