Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 00a304f

Browse files
committed
Merge pull request #2550 from bartonjs/restore_crypto_tests
Restore testing of RSAOpenSsl
2 parents 6ef2364 + dc473a1 commit 00a304f

35 files changed

+1171
-253
lines changed

src/Common/tests/Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public static void DecryptSavedAnswer()
3232

3333
byte[] output;
3434

35-
using (var rsa = new RSACryptoServiceProvider())
35+
using (RSA rsa = RSAFactory.Create())
3636
{
3737
rsa.ImportParameters(TestData.RSA1024Params);
38-
output = rsa.Decrypt(cipherBytes, true);
38+
output = rsa.Decrypt(cipherBytes, RSAEncryptionPadding.OaepSHA1);
3939
}
4040

4141
Assert.Equal(TestData.HelloBytes, output);
@@ -66,10 +66,10 @@ public static void DecryptSavedAnswerUnusualExponent()
6666

6767
byte[] output;
6868

69-
using (var rsa = new RSACryptoServiceProvider())
69+
using (RSA rsa = RSAFactory.Create())
7070
{
7171
rsa.ImportParameters(TestData.UnusualExponentParameters);
72-
output = rsa.Decrypt(cipherBytes, true);
72+
output = rsa.Decrypt(cipherBytes, RSAEncryptionPadding.OaepSHA1);
7373
}
7474

7575
Assert.Equal(TestData.HelloBytes, output);
@@ -81,10 +81,10 @@ public static void RsaCryptRoundtrip()
8181
byte[] crypt;
8282
byte[] output;
8383

84-
using (var rsa = new RSACryptoServiceProvider())
84+
using (RSA rsa = RSAFactory.Create())
8585
{
86-
crypt = rsa.Encrypt(TestData.HelloBytes, true);
87-
output = rsa.Decrypt(crypt, true);
86+
crypt = rsa.Encrypt(TestData.HelloBytes, RSAEncryptionPadding.OaepSHA1);
87+
output = rsa.Decrypt(crypt, RSAEncryptionPadding.OaepSHA1);
8888
}
8989

9090
Assert.NotEqual(crypt, output);
@@ -96,13 +96,13 @@ public static void RsaDecryptAfterExport()
9696
{
9797
byte[] output;
9898

99-
using (var rsa = new RSACryptoServiceProvider())
99+
using (RSA rsa = RSAFactory.Create())
100100
{
101-
byte[] crypt = rsa.Encrypt(TestData.HelloBytes, true);
101+
byte[] crypt = rsa.Encrypt(TestData.HelloBytes, RSAEncryptionPadding.OaepSHA1);
102102

103103
// Export the key, this should not clear/destroy the key.
104104
RSAParameters ignored = rsa.ExportParameters(true);
105-
output = rsa.Decrypt(crypt, true);
105+
output = rsa.Decrypt(crypt, RSAEncryptionPadding.OaepSHA1);
106106
}
107107

108108
Assert.Equal(TestData.HelloBytes, output);
@@ -113,7 +113,7 @@ public static void LargeKeyCryptRoundtrip()
113113
{
114114
byte[] output;
115115

116-
using (var rsa = new RSACryptoServiceProvider())
116+
using (RSA rsa = RSAFactory.Create())
117117
{
118118
try
119119
{
@@ -125,11 +125,11 @@ public static void LargeKeyCryptRoundtrip()
125125
return;
126126
}
127127

128-
byte[] crypt = rsa.Encrypt(TestData.HelloBytes, true);
128+
byte[] crypt = rsa.Encrypt(TestData.HelloBytes, RSAEncryptionPadding.OaepSHA1);
129129

130130
Assert.Equal(rsa.KeySize, crypt.Length * 8);
131131

132-
output = rsa.Decrypt(crypt, true);
132+
output = rsa.Decrypt(crypt, RSAEncryptionPadding.OaepSHA1);
133133
}
134134

135135
Assert.Equal(TestData.HelloBytes, output);
@@ -141,12 +141,12 @@ public static void UnusualExponentCryptRoundtrip()
141141
byte[] crypt;
142142
byte[] output;
143143

144-
using (var rsa = new RSACryptoServiceProvider())
144+
using (RSA rsa = RSAFactory.Create())
145145
{
146146
rsa.ImportParameters(TestData.UnusualExponentParameters);
147147

148-
crypt = rsa.Encrypt(TestData.HelloBytes, true);
149-
output = rsa.Decrypt(crypt, true);
148+
crypt = rsa.Encrypt(TestData.HelloBytes, RSAEncryptionPadding.OaepSHA1);
149+
output = rsa.Decrypt(crypt, RSAEncryptionPadding.OaepSHA1);
150150
}
151151

152152
Assert.NotEqual(crypt, output);

src/Common/tests/Cryptography/AlgorithmImplementations/RSA/ImportExport.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static void ExportAutoKey()
1414
RSAParameters publicParams;
1515
int keySize;
1616

17-
using (RSA rsa = new RSACryptoServiceProvider())
17+
using (RSA rsa = RSAFactory.Create())
1818
{
1919
keySize = rsa.KeySize;
2020

@@ -53,7 +53,7 @@ public static void PaddedExport()
5353
RSAParameters diminishedDPParamaters = TestData.DiminishedDPParamaters;
5454
RSAParameters exported;
5555

56-
using (RSA rsa = new RSACryptoServiceProvider())
56+
using (RSA rsa = RSAFactory.Create())
5757
{
5858
rsa.ImportParameters(diminishedDPParamaters);
5959
exported = rsa.ExportParameters(true);
@@ -69,7 +69,7 @@ public static void LargeKeyImportExport()
6969
{
7070
RSAParameters imported = TestData.RSA16384Params;
7171

72-
using (RSA rsa = new RSACryptoServiceProvider())
72+
using (RSA rsa = RSAFactory.Create())
7373
{
7474
try
7575
{
@@ -106,7 +106,7 @@ public static void UnusualExponentImportExport()
106106
RSAParameters unusualExponentParameters = TestData.UnusualExponentParameters;
107107
RSAParameters exported;
108108

109-
using (RSA rsa = new RSACryptoServiceProvider())
109+
using (RSA rsa = RSAFactory.Create())
110110
{
111111
rsa.ImportParameters(unusualExponentParameters);
112112
exported = rsa.ExportParameters(true);
@@ -120,7 +120,7 @@ public static void UnusualExponentImportExport()
120120
[Fact]
121121
public static void ImportReset()
122122
{
123-
using (RSA rsa = new RSACryptoServiceProvider())
123+
using (RSA rsa = RSAFactory.Create())
124124
{
125125
RSAParameters exported = rsa.ExportParameters(true);
126126
RSAParameters imported;
@@ -152,7 +152,7 @@ public static void MultiExport()
152152
{
153153
RSAParameters imported = TestData.RSA1024Params;
154154

155-
using (RSA rsa = new RSACryptoServiceProvider())
155+
using (RSA rsa = RSAFactory.Create())
156156
{
157157
rsa.ImportParameters(imported);
158158

@@ -187,7 +187,7 @@ public static void PublicOnlyPrivateExport()
187187
Exponent = TestData.RSA1024Params.Exponent,
188188
};
189189

190-
using (RSA rsa = new RSACryptoServiceProvider())
190+
using (RSA rsa = RSAFactory.Create())
191191
{
192192
rsa.ImportParameters(imported);
193193
Assert.Throws<CryptographicException>(() => rsa.ExportParameters(true));
@@ -202,7 +202,7 @@ public static void ImportNoExponent()
202202
Modulus = TestData.RSA1024Params.Modulus,
203203
};
204204

205-
using (RSA rsa = new RSACryptoServiceProvider())
205+
using (RSA rsa = RSAFactory.Create())
206206
{
207207
Assert.Throws<CryptographicException>(() => rsa.ImportParameters(imported));
208208
}
@@ -216,7 +216,7 @@ public static void ImportNoModulus()
216216
Exponent = TestData.RSA1024Params.Exponent,
217217
};
218218

219-
using (RSA rsa = new RSACryptoServiceProvider())
219+
using (RSA rsa = RSAFactory.Create())
220220
{
221221
Assert.Throws<CryptographicException>(() => rsa.ImportParameters(imported));
222222
}
@@ -230,7 +230,7 @@ public static void ImportNoDP()
230230
RSAParameters imported = TestData.RSA1024Params;
231231
imported.DP = null;
232232

233-
using (RSA rsa = new RSACryptoServiceProvider())
233+
using (RSA rsa = RSAFactory.Create())
234234
{
235235
Assert.Throws<CryptographicException>(() => rsa.ImportParameters(imported));
236236
}

src/Common/tests/Cryptography/AlgorithmImplementations/RSA/KeyGeneration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ private static void GenerateKey(Func<RSA, int> getSize)
4646
{
4747
int keySize;
4848

49-
using (var rsa = new RSACryptoServiceProvider())
49+
using (RSA rsa = RSAFactory.Create())
5050
{
5151
keySize = getSize(rsa);
5252
}
5353

54-
using (var rsa = new RSACryptoServiceProvider(keySize))
54+
using (RSA rsa = RSAFactory.Create(keySize))
5555
{
5656
Assert.Equal(keySize, rsa.KeySize);
5757

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace System.Security.Cryptography.Rsa.Tests
5+
{
6+
public interface IRSAProvider
7+
{
8+
RSA Create();
9+
RSA Create(int keySize);
10+
}
11+
12+
public static partial class RSAFactory
13+
{
14+
public static RSA Create()
15+
{
16+
return s_provider.Create();
17+
}
18+
19+
public static RSA Create(int keySize)
20+
{
21+
return s_provider.Create(keySize);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)