Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -4,13 +4,14 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Security.Cryptography.Tests;
using Test.Cryptography;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;

namespace System.Security.Cryptography.Rsa.Tests
{
public sealed class EncryptDecrypt_Array : EncryptDecrypt
public abstract class EncryptDecrypt_Array : EncryptDecrypt
{
protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) =>
rsa.Encrypt(data, padding);
Expand All @@ -31,7 +32,9 @@ public void NullArray_Throws()
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public abstract class EncryptDecrypt
{
public static bool SupportsSha2Oaep => RSAFactory.SupportsSha2Oaep;
public bool SupportsSha2Oaep => RSAFactory.SupportsSha2Oaep;

protected abstract RSAProvider RSAFactory { get; }

protected abstract byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding);
protected abstract byte[] Decrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding);
Expand Down Expand Up @@ -473,9 +476,11 @@ public void RsaDecryptOaep_ExpectFailure()
}
}

[ConditionalFact(typeof(EncryptDecrypt), nameof(SupportsSha2Oaep))]
[ConditionalFact]
public void RsaDecryptOaepWrongAlgorithm()
{
SkipTestException.ThrowUnless(SupportsSha2Oaep);

using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params))
{
byte[] data = TestData.HelloBytes;
Expand Down Expand Up @@ -644,9 +649,11 @@ public void RsaDecryptAfterExport()
Assert.Equal(TestData.HelloBytes, output);
}

[ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))]
[ConditionalFact]
public void LargeKeyCryptRoundtrip()
{
SkipTestException.ThrowUnless(RSAFactory.Supports16384);

byte[] output;

using (RSA rsa = RSAFactory.Create())
Expand Down Expand Up @@ -691,8 +698,17 @@ public void UnusualExponentCryptRoundtrip()

[Theory]
[MemberData(nameof(OaepPaddingModes))]
public void NonPowerOfTwoKeySizeOaepRoundtrip(RSAEncryptionPadding oaepPaddingMode)
public void NonPowerOfTwoKeySizeOaepRoundtrip(
RSAEncryptionPadding oaepPaddingMode,
bool requiresSha2Oaep,
bool requiresSha3)
{
if ((requiresSha2Oaep && !RSAFactory.SupportsSha2Oaep) ||
(requiresSha3 && !RSAFactory.SupportsSha3))
{
return;
}
Comment thread
PranavSenthilnathan marked this conversation as resolved.

// Key generation can transiently fail on some platforms due to resource contention.
// Retry a few times before failing the test.
RetryHelper.Execute(() =>
Expand Down Expand Up @@ -840,21 +856,15 @@ public static IEnumerable<object[]> OaepPaddingModes
{
get
{
yield return new object[] { RSAEncryptionPadding.OaepSHA1 };
yield return new object[] { RSAEncryptionPadding.OaepSHA1, false, false };

if (RSAFactory.SupportsSha2Oaep)
{
yield return new object[] { RSAEncryptionPadding.OaepSHA256 };
yield return new object[] { RSAEncryptionPadding.OaepSHA384 };
yield return new object[] { RSAEncryptionPadding.OaepSHA512 };
}

if (RSAFactory.SupportsSha3)
{
yield return new object[] { RSAEncryptionPadding.OaepSHA3_256 };
yield return new object[] { RSAEncryptionPadding.OaepSHA3_384 };
yield return new object[] { RSAEncryptionPadding.OaepSHA3_512 };
}
yield return new object[] { RSAEncryptionPadding.OaepSHA256, true, false };
yield return new object[] { RSAEncryptionPadding.OaepSHA384, true, false };
yield return new object[] { RSAEncryptionPadding.OaepSHA512, true, false };

Comment thread
PranavSenthilnathan marked this conversation as resolved.
Outdated
yield return new object[] { RSAEncryptionPadding.OaepSHA3_256, false, true };
Comment thread
PranavSenthilnathan marked this conversation as resolved.
yield return new object[] { RSAEncryptionPadding.OaepSHA3_384, false, true };
yield return new object[] { RSAEncryptionPadding.OaepSHA3_512, false, true };
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Linq;
using System.Security.Cryptography.Tests;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;

namespace System.Security.Cryptography.Rsa.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public sealed class EncryptDecrypt_Span : EncryptDecrypt
public abstract class EncryptDecrypt_Span : EncryptDecrypt
{
protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) =>
WithOutputArray(dest => rsa.Encrypt(data, dest, padding));
Expand Down Expand Up @@ -36,7 +38,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 : EncryptDecrypt
{
protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) =>
rsa.Encrypt(new ReadOnlySpan<byte>(data), padding);
Expand All @@ -46,7 +48,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 : EncryptDecrypt
{
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 Down Expand Up @@ -139,14 +141,16 @@ public void Decrypt_WrongKey_OAEP_SHA1()
Decrypt_WrongKey(RSAEncryptionPadding.OaepSHA1);
}

[ConditionalFact(typeof(EncryptDecrypt_TrySpan), nameof(SupportsSha2Oaep))]
[ConditionalFact]
public void Decrypt_WrongKey_OAEP_SHA256()
{
SkipTestException.ThrowUnless(RSAFactory.SupportsSha2Oaep);

Decrypt_WrongKey(RSAEncryptionPadding.OaepSHA256);
}

[Fact]
public static void EncryptDefaultSpan()
public void EncryptDefaultSpan()
{
using (RSA rsa = RSAFactory.Create())
{
Expand All @@ -164,7 +168,7 @@ public static void EncryptDefaultSpan()
}
}

private static void Decrypt_WrongKey(RSAEncryptionPadding padding)
private void Decrypt_WrongKey(RSAEncryptionPadding padding)
{
using (RSA rsa1 = RSAFactory.Create())
using (RSA rsa2 = RSAFactory.Create())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@

using System.Linq;
using System.Numerics;
using System.Security.Cryptography.Tests;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;

namespace System.Security.Cryptography.Rsa.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public partial class ImportExport
public abstract class ImportExport
{
public static bool Supports16384 { get; } = TestRsa16384();
protected abstract RSAProvider RSAFactory { get; }

[Fact]
public static void ExportAutoKey()
public void ExportAutoKey()
{
RSAParameters privateParams;
RSAParameters publicParams;
Expand Down Expand Up @@ -43,7 +45,7 @@ public static void ExportAutoKey()
}

[Fact]
public static void PaddedExport()
public void PaddedExport()
{
// OpenSSL's numeric type for the storage of RSA key parts disregards zero-valued
// prefix bytes.
Expand All @@ -69,9 +71,11 @@ public static void PaddedExport()
RSATestHelpers.AssertKeyEquals(diminishedDPParameters, exported);
}

[ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))]
public static void LargeKeyImportExport()
[ConditionalFact]
public void LargeKeyImportExport()
{
SkipTestException.ThrowUnless(RSAFactory.Supports16384);

RSAParameters imported = TestData.RSA16384Params;

using (RSA rsa = RSAFactory.Create())
Expand Down Expand Up @@ -99,7 +103,7 @@ public static void LargeKeyImportExport()
}

[Fact]
public static void UnusualExponentImportExport()
public void UnusualExponentImportExport()
{
// Most choices for the Exponent value in an RSA key use a Fermat prime.
// Since a Fermat prime is 2^(2^m) + 1, it always only has two bits set, and
Expand All @@ -123,7 +127,7 @@ public static void UnusualExponentImportExport()
}

[Fact]
public static void ImportExport1032()
public void ImportExport1032()
{
RSAParameters imported = TestData.RSA1032Parameters;
RSAParameters exported;
Expand All @@ -144,7 +148,7 @@ public static void ImportExport1032()
}

[Fact]
public static void ImportReset()
public void ImportReset()
{
using (RSA rsa = RSAFactory.Create())
{
Expand Down Expand Up @@ -174,7 +178,7 @@ public static void ImportReset()
}

[Fact]
public static void ImportPrivateExportPublic()
public void ImportPrivateExportPublic()
{
RSAParameters imported = TestData.RSA1024Params;

Expand All @@ -192,7 +196,7 @@ public static void ImportPrivateExportPublic()
}

[Fact]
public static void MultiExport()
public void MultiExport()
{
RSAParameters imported = TestData.RSA1024Params;

Expand Down Expand Up @@ -223,7 +227,7 @@ public static void MultiExport()
}

[Fact]
public static void PublicOnlyPrivateExport()
public void PublicOnlyPrivateExport()
{
RSAParameters imported = new RSAParameters
{
Expand All @@ -239,7 +243,7 @@ public static void PublicOnlyPrivateExport()
}

[Fact]
public static void ImportNoExponent()
public void ImportNoExponent()
{
RSAParameters imported = new RSAParameters
{
Expand All @@ -256,7 +260,7 @@ public static void ImportNoExponent()
}

[Fact]
public static void ImportNoModulus()
public void ImportNoModulus()
{
RSAParameters imported = new RSAParameters
{
Expand All @@ -276,7 +280,7 @@ public static void ImportNoModulus()
#if TESTING_CNG_IMPLEMENTATION
[ActiveIssue("https://github.com/dotnet/runtime/issues/21341", TargetFrameworkMonikers.NetFramework)]
#endif
public static void ImportNoDP()
public void ImportNoDP()
{
// Because RSAParameters is a struct, this is a copy,
// so assigning DP is not destructive to other tests.
Expand All @@ -292,7 +296,7 @@ public static void ImportNoDP()
[Theory]
[InlineData(true)]
[InlineData(false)]
public static void ExportAfterDispose(bool importKey)
public void ExportAfterDispose(bool importKey)
{
RSA rsa = importKey ? RSAFactory.Create(TestData.RSA2048Params) : RSAFactory.Create(1024);

Expand All @@ -314,7 +318,7 @@ public static void ExportAfterDispose(bool importKey)
[Theory]
[InlineData(true)]
[InlineData(false)]
public static void ImportZeroModulus(bool includePrivateParameters)
public void ImportZeroModulus(bool includePrivateParameters)
{
RSAParameters zeroModulus = CopyRSAParameters(TestData.RSA2048Params);
zeroModulus.Modulus.AsSpan().Clear();
Expand Down Expand Up @@ -365,31 +369,6 @@ internal static RSAParameters MakePublic(in RSAParameters rsaParams)
};
}

private static bool TestRsa16384()
{
if (PlatformDetection.IsAndroid)
{
// We cannot detect this on Android at the moment. Even attempting to generate or import a 16K RSA key
// may leave the error queue in the incorrect state. See https://github.com/google/conscrypt/issues/1507
return false;
}

try
{
using (RSA rsa = RSAFactory.Create())
{
rsa.ImportParameters(TestData.RSA16384Params);
}

return true;
}
catch (Exception e) when (e is CryptographicException or PlatformNotSupportedException)
{
// The key is too big for this platform or the platform is not supported.
return false;
}
}

private static RSAParameters CopyRSAParameters(in RSAParameters rsaParams)
{
static byte[] CopyBytes(byte[] data) => data is null ? null : data.AsSpan().ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,46 @@
namespace System.Security.Cryptography.Rsa.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public class KeyGeneration
public abstract class KeyGeneration
{
protected abstract RSAProvider RSAFactory { get; }

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotSymCryptOpenSsl))]
public static void GenerateMinKey()
public void GenerateMinKey()
{
GenerateKey(rsa => GetMin(rsa.LegalKeySizes));
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotSymCryptOpenSsl))]
public static void GenerateSecondMinKey()
public void GenerateSecondMinKey()
{
GenerateKey(rsa => GetSecondMin(rsa.LegalKeySizes));
}

[ConditionalFact(typeof(TestEnvironment), nameof(TestEnvironment.IsStressModeEnabled))]
public static void GenerateMaxKey()
public void GenerateMaxKey()
{
GenerateKey(rsa => GetMax(rsa.LegalKeySizes));
}

[Fact]
public static void GenerateKey_2048()
public void GenerateKey_2048()
{
GenerateKey(2048);
}

[Fact]
public static void GenerateKey_4096()
public void GenerateKey_4096()
{
GenerateKey(4096);
}

private static void GenerateKey(int size)
private void GenerateKey(int size)
{
GenerateKey(rsa => size);
}

private static void GenerateKey(Func<RSA, int> getSize)
private void GenerateKey(Func<RSA, int> getSize)
{
int keySize;

Expand Down
Loading
Loading