Skip to content
Draft
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace System.Security.Cryptography.Rsa.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public sealed class EncryptDecrypt_Span : EncryptDecrypt
public abstract class EncryptDecrypt_Span<TProvider> : EncryptDecrypt<TProvider> where TProvider : IRSAProvider, new()
{
protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) =>
WithOutputArray(dest => rsa.Encrypt(data, dest, padding));
Expand Down Expand Up @@ -36,7 +36,7 @@ private static byte[] WithOutputArray(Func<byte[], int> func)
}

[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public sealed class EncryptDecrypt_AllocatingSpan : EncryptDecrypt
public abstract class EncryptDecrypt_AllocatingSpan<TProvider> : EncryptDecrypt<TProvider> where TProvider : IRSAProvider, new()
{
protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) =>
rsa.Encrypt(new ReadOnlySpan<byte>(data), padding);
Expand All @@ -46,7 +46,7 @@ protected override byte[] Decrypt(RSA rsa, byte[] data, RSAEncryptionPadding pad
}

[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public sealed class EncryptDecrypt_TrySpan : EncryptDecrypt
public abstract class EncryptDecrypt_TrySpan<TProvider> : EncryptDecrypt<TProvider> where TProvider : IRSAProvider, new()
{
protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) =>
TryWithOutputArray(dest => rsa.TryEncrypt(data, dest, padding, out int bytesWritten) ? (true, bytesWritten) : (false, 0));
Expand All @@ -71,7 +71,7 @@ private static byte[] TryWithOutputArray(Func<byte[], (bool, int)> func)
[Fact]
public void Decrypt_VariousSizeSpans_Success()
{
using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(TestData.RSA1024Params);
byte[] cipherBytes = Encrypt(rsa, TestData.HelloBytes, RSAEncryptionPadding.OaepSHA1);
Expand Down Expand Up @@ -103,7 +103,7 @@ public void Decrypt_VariousSizeSpans_Success()
[Fact]
public void Encrypt_VariousSizeSpans_Success()
{
using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(TestData.RSA1024Params);
byte[] cipherBytes = Encrypt(rsa, TestData.HelloBytes, RSAEncryptionPadding.OaepSHA1);
Expand Down Expand Up @@ -148,7 +148,7 @@ public void Decrypt_WrongKey_OAEP_SHA256()
[Fact]
public static void EncryptDefaultSpan()
{
using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
byte[] dest = new byte[rsa.KeySize / 8];

Expand All @@ -166,8 +166,8 @@ public static void EncryptDefaultSpan()

private static void Decrypt_WrongKey(RSAEncryptionPadding padding)
{
using (RSA rsa1 = RSAFactory.Create())
using (RSA rsa2 = RSAFactory.Create())
using (RSA rsa1 = s_provider.Create())
using (RSA rsa2 = s_provider.Create())
{
byte[] input = TestData.HelloBytes;
byte[] encrypted = rsa1.Encrypt(input, padding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
namespace System.Security.Cryptography.Rsa.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public partial class ImportExport
public abstract partial class ImportExport<TProvider> where TProvider : IRSAProvider, new()
{
private static readonly TProvider s_provider = new TProvider();

public static bool Supports16384 { get; } = TestRsa16384();

[Fact]
Expand All @@ -19,7 +21,7 @@ public static void ExportAutoKey()
RSAParameters publicParams;
int keySize;

using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
keySize = rsa.KeySize;

Expand Down Expand Up @@ -58,7 +60,7 @@ public static void PaddedExport()
RSAParameters diminishedDPParameters = TestData.DiminishedDPParameters;
RSAParameters exported;

using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(diminishedDPParameters);
exported = rsa.ExportParameters(true);
Expand All @@ -74,7 +76,7 @@ public static void LargeKeyImportExport()
{
RSAParameters imported = TestData.RSA16384Params;

using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
try
{
Expand Down Expand Up @@ -111,7 +113,7 @@ public static void UnusualExponentImportExport()
RSAParameters unusualExponentParameters = TestData.UnusualExponentParameters;
RSAParameters exported;

using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(unusualExponentParameters);
exported = rsa.ExportParameters(true);
Expand All @@ -129,7 +131,7 @@ public static void ImportExport1032()
RSAParameters exported;
RSAParameters exportedPublic;

using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(imported);
exported = rsa.ExportParameters(true);
Expand All @@ -146,7 +148,7 @@ public static void ImportExport1032()
[Fact]
public static void ImportReset()
{
using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
RSAParameters exported = rsa.ExportParameters(true);
RSAParameters imported;
Expand Down Expand Up @@ -178,7 +180,7 @@ public static void ImportPrivateExportPublic()
{
RSAParameters imported = TestData.RSA1024Params;

using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(imported);

Expand All @@ -196,7 +198,7 @@ public static void MultiExport()
{
RSAParameters imported = TestData.RSA1024Params;

using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(imported);

Expand Down Expand Up @@ -231,7 +233,7 @@ public static void PublicOnlyPrivateExport()
Exponent = TestData.RSA1024Params.Exponent,
};

using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(imported);
Assert.ThrowsAny<CryptographicException>(() => rsa.ExportParameters(true));
Expand All @@ -246,7 +248,7 @@ public static void ImportNoExponent()
Modulus = TestData.RSA1024Params.Modulus,
};

using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
if (rsa is RSACng && PlatformDetection.IsNetFramework)
AssertExtensions.Throws<ArgumentException>(null, () => rsa.ImportParameters(imported));
Expand All @@ -263,7 +265,7 @@ public static void ImportNoModulus()
Exponent = TestData.RSA1024Params.Exponent,
};

using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
if (rsa is RSACng && PlatformDetection.IsNetFramework)
AssertExtensions.Throws<ArgumentException>(null, () => rsa.ImportParameters(imported));
Expand All @@ -283,7 +285,7 @@ public static void ImportNoDP()
RSAParameters imported = TestData.RSA1024Params;
imported.DP = null;

using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
Assert.ThrowsAny<CryptographicException>(() => rsa.ImportParameters(imported));
}
Expand All @@ -294,7 +296,7 @@ public static void ImportNoDP()
[InlineData(false)]
public static void ExportAfterDispose(bool importKey)
{
RSA rsa = importKey ? RSAFactory.Create(TestData.RSA2048Params) : RSAFactory.Create(1024);
RSA rsa = importKey ? CreateRSA(TestData.RSA2048Params) : s_provider.Create(1024);

// Ensure that the key got created, and then Dispose it.
using (rsa)
Expand Down Expand Up @@ -324,7 +326,7 @@ public static void ImportZeroModulus(bool includePrivateParameters)
zeroModulus = MakePublic(zeroModulus);
}

Assert.ThrowsAny<CryptographicException>(() => RSAFactory.Create(zeroModulus));
Assert.ThrowsAny<CryptographicException>(() => CreateRSA(zeroModulus));
}

internal static void ValidateParameters(ref RSAParameters rsaParams)
Expand Down Expand Up @@ -365,11 +367,18 @@ internal static RSAParameters MakePublic(in RSAParameters rsaParams)
};
}

private static RSA CreateRSA(RSAParameters rsaParameters)
{
RSA rsa = s_provider.Create();
rsa.ImportParameters(rsaParameters);
return rsa;
}

private static bool TestRsa16384()
{
try
{
using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(TestData.RSA16384Params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
namespace System.Security.Cryptography.Rsa.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public class KeyGeneration
public abstract class KeyGeneration<TProvider> where TProvider : IRSAProvider, new()
{
private static readonly TProvider s_provider = new TProvider();
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotSymCryptOpenSsl))]
public static void GenerateMinKey()
{
Expand Down Expand Up @@ -47,19 +48,19 @@ private static void GenerateKey(Func<RSA, int> getSize)
{
int keySize;

using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
keySize = getSize(rsa);
}

using (RSA rsa = RSAFactory.Create(keySize))
using (RSA rsa = s_provider.Create(keySize))
{
Assert.Equal(keySize, rsa.KeySize);

// Some providers may generate the key in the constructor, but
// all of them should have generated it before answering ExportParameters.
RSAParameters keyParameters = rsa.ExportParameters(false);
ImportExport.ValidateParameters(ref keyParameters);
ImportExport<TProvider>.ValidateParameters(ref keyParameters);

// KeySize should still be what we set it to originally.
Assert.Equal(keySize, rsa.KeySize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,4 @@ public interface IRSAProvider
bool SupportsMd5Signatures { get; }
bool SupportsSha3 { get; }
}

public static partial class RSAFactory
{
public static RSA Create()
{
return s_provider.Create();
}

public static RSA Create(int keySize)
{
return s_provider.Create(keySize);
}

public static RSA Create(RSAParameters rsaParameters)
{
RSA rsa = Create();
rsa.ImportParameters(rsaParameters);
return rsa;
}

public static bool Supports384PrivateKey => s_provider.Supports384PrivateKey;

public static bool SupportsLargeExponent => s_provider.SupportsLargeExponent;

public static bool SupportsSha2Oaep => s_provider.SupportsSha2Oaep;

public static bool SupportsPss => s_provider.SupportsPss;

public static bool SupportsSha1Signatures => s_provider.SupportsSha1Signatures;
public static bool SupportsMd5Signatures => s_provider.SupportsMd5Signatures;

public static bool SupportsSha3 => s_provider.SupportsSha3;
public static bool NoSupportsSha3 => !SupportsSha3;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,43 @@
namespace System.Security.Cryptography.Rsa.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public static class RSAFactoryTests
public abstract class RSAFactoryTests<TProvider> where TProvider : IRSAProvider, new()
{
private static readonly TProvider s_provider = new TProvider();

private static RSA CreateRSA(RSAParameters rsaParameters)
{
RSA rsa = s_provider.Create();
rsa.ImportParameters(rsaParameters);
return rsa;
}

[Fact]
public static void RSACreateDefault_Equals_SameInstance()
{
using RSA rsa = RSAFactory.Create();
using RSA rsa = s_provider.Create();
AssertExtensions.TrueExpression(rsa.Equals(rsa));
}

[Fact]
public static void RSACreateKeySize_Equals_SameInstance()
{
using RSA rsa = RSAFactory.Create(2048);
using RSA rsa = s_provider.Create(2048);
AssertExtensions.TrueExpression(rsa.Equals(rsa));
}

[Fact]
public static void RSACreateParameters_Equals_SameInstance()
{
using RSA rsa = RSAFactory.Create(TestData.RSA2048Params);
using RSA rsa = CreateRSA(TestData.RSA2048Params);
AssertExtensions.TrueExpression(rsa.Equals(rsa));
}

[Fact]
public static void RSACreateParameters_Equals_DifferentInstance_FalseForSameKeyMaterial()
{
using RSA rsa1 = RSAFactory.Create(TestData.RSA2048Params);
using RSA rsa2 = RSAFactory.Create(TestData.RSA2048Params);
using RSA rsa1 = CreateRSA(TestData.RSA2048Params);
using RSA rsa2 = CreateRSA(TestData.RSA2048Params);
AssertExtensions.FalseExpression(rsa1.Equals(rsa2));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@
namespace System.Security.Cryptography.Rsa.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public partial class RSAKeyExchangeFormatterTests
public abstract partial class RSAKeyExchangeFormatterTests<TProvider> where TProvider : IRSAProvider, new()
{
private static readonly TProvider s_provider = new TProvider();

private static RSA CreateRSA(RSAParameters rsaParameters)
{
RSA rsa = s_provider.Create();
rsa.ImportParameters(rsaParameters);
return rsa;
}
[Fact]
public static void VerifyDecryptKeyExchangeOaep()
{
using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(TestData.RSA2048Params);

Expand All @@ -26,7 +34,7 @@ public static void VerifyDecryptKeyExchangeOaep()
[Fact]
public static void VerifyDecryptKeyExchangePkcs1()
{
using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(TestData.RSA2048Params);

Expand All @@ -39,7 +47,7 @@ public static void VerifyDecryptKeyExchangePkcs1()
[Fact]
public static void TestKnownValueOaep()
{
using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(TestData.RSA1024Params);
byte[] encrypted =
Expand All @@ -56,7 +64,7 @@ public static void TestKnownValueOaep()
[Fact]
public static void TestKnownValuePkcs1()
{
using (RSA rsa = RSAFactory.Create())
using (RSA rsa = s_provider.Create())
{
rsa.ImportParameters(TestData.RSA1024Params);
byte[] encrypted =
Expand Down
Loading
Loading