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

Commit bd6672e

Browse files
committed
Add an RSAFactory to the RSA test suite.
Implementors will need to define an IRSAProvider and assign it via a field initializer (s_provider) for the RSAFactory class. This was chosen over RSA.Create() for two reasons: 1) RSA.Create() isn't part of the contract yet. 2) There will be 3 RSA implementations, but only two of them capable of being returned by RSA.Create(), so an alternate factory is required for long-term support of CAPI testing.
1 parent 4cc2a21 commit bd6672e

File tree

7 files changed

+123
-61
lines changed

7 files changed

+123
-61
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+
}

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

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.Collections;
54
using System.Collections.Generic;
65
using System.IO;
76
using Test.IO.Streams;
@@ -147,10 +146,10 @@ public static void ExpectSignature_SHA256_1024_Stream()
147146
byte[] signature;
148147

149148
using (Stream stream = new PositionValueStream(10))
150-
using (var rsa = new RSACryptoServiceProvider())
149+
using (RSA rsa = RSAFactory.Create())
151150
{
152151
rsa.ImportParameters(TestData.RSA1024Params);
153-
signature = rsa.SignData(stream, "SHA256");
152+
signature = rsa.SignData(stream, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
154153
}
155154

156155
Assert.Equal(expectedSignature, signature);
@@ -287,11 +286,11 @@ public static void SignAndVerify_SHA256_1024()
287286
[Fact]
288287
public static void NegativeVerify_WrongAlgorithm()
289288
{
290-
using (var rsa = new RSACryptoServiceProvider())
289+
using (RSA rsa = RSAFactory.Create())
291290
{
292291
rsa.ImportParameters(TestData.RSA2048Params);
293-
byte[] signature = rsa.SignData(TestData.HelloBytes, "SHA1");
294-
bool signatureMatched = rsa.VerifyData(TestData.HelloBytes, "SHA256", signature);
292+
byte[] signature = rsa.SignData(TestData.HelloBytes, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
293+
bool signatureMatched = rsa.VerifyData(TestData.HelloBytes, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
295294

296295
Assert.False(signatureMatched);
297296
}
@@ -300,27 +299,27 @@ public static void NegativeVerify_WrongAlgorithm()
300299
[Fact]
301300
public static void NegativeVerify_WrongSignature()
302301
{
303-
using (var rsa = new RSACryptoServiceProvider())
302+
using (RSA rsa = RSAFactory.Create())
304303
{
305304
rsa.ImportParameters(TestData.RSA2048Params);
306-
byte[] signature = rsa.SignData(TestData.HelloBytes, "SHA1");
305+
byte[] signature = rsa.SignData(TestData.HelloBytes, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
307306

308307
// Invalidate the signature.
309308
signature[0] = (byte)~signature[0];
310309

311-
bool signatureMatched = rsa.VerifyData(TestData.HelloBytes, "SHA1", signature);
310+
bool signatureMatched = rsa.VerifyData(TestData.HelloBytes, signature, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
312311
Assert.False(signatureMatched);
313312
}
314313
}
315314

316315
[Fact]
317316
public static void NegativeVerify_TamperedData()
318317
{
319-
using (var rsa = new RSACryptoServiceProvider())
318+
using (RSA rsa = RSAFactory.Create())
320319
{
321320
rsa.ImportParameters(TestData.RSA2048Params);
322-
byte[] signature = rsa.SignData(TestData.HelloBytes, "SHA1");
323-
bool signatureMatched = rsa.VerifyData(Array.Empty<byte>(), "SHA1", signature);
321+
byte[] signature = rsa.SignData(TestData.HelloBytes, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
322+
bool signatureMatched = rsa.VerifyData(Array.Empty<byte>(), signature, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
324323
Assert.False(signatureMatched);
325324
}
326325
}
@@ -330,16 +329,16 @@ public static void NegativeVerify_BadKeysize()
330329
{
331330
byte[] signature;
332331

333-
using (var rsa = new RSACryptoServiceProvider())
332+
using (RSA rsa = RSAFactory.Create())
334333
{
335334
rsa.ImportParameters(TestData.RSA2048Params);
336-
signature = rsa.SignData(TestData.HelloBytes, "SHA1");
335+
signature = rsa.SignData(TestData.HelloBytes, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
337336
}
338337

339-
using (var rsa = new RSACryptoServiceProvider())
338+
using (RSA rsa = RSAFactory.Create())
340339
{
341340
rsa.ImportParameters(TestData.RSA1024Params);
342-
bool signatureMatched = rsa.VerifyData(TestData.HelloBytes, "SHA1", signature);
341+
bool signatureMatched = rsa.VerifyData(TestData.HelloBytes, signature, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
343342

344343
Assert.False(signatureMatched);
345344
}
@@ -619,10 +618,10 @@ private static void ExpectSignature(
619618
// the signature is deterministic, so we can safely verify it here.
620619
byte[] signature;
621620

622-
using (var rsa = new RSACryptoServiceProvider())
621+
using (RSA rsa = RSAFactory.Create())
623622
{
624623
rsa.ImportParameters(rsaParameters);
625-
signature = rsa.SignData(data, hashAlgorithmName);
624+
signature = rsa.SignData(data, new HashAlgorithmName(hashAlgorithmName), RSASignaturePadding.Pkcs1);
626625
}
627626

628627
Assert.Equal(expectedSignature, signature);
@@ -640,10 +639,10 @@ private static void ExpectHashSignature(
640639
// the signature is deterministic, so we can safely verify it here.
641640
byte[] signature;
642641

643-
using (var rsa = new RSACryptoServiceProvider())
642+
using (RSA rsa = RSAFactory.Create())
644643
{
645644
rsa.ImportParameters(rsaParameters);
646-
signature = rsa.SignHash(dataHash, hashAlgorithmName);
645+
signature = rsa.SignHash(dataHash, new HashAlgorithmName(hashAlgorithmName), RSASignaturePadding.Pkcs1);
647646
}
648647

649648
Assert.Equal(expectedSignature, signature);
@@ -663,10 +662,10 @@ private static void VerifySignature(
663662

664663
bool signatureMatched;
665664

666-
using (var rsa = new RSACryptoServiceProvider())
665+
using (RSA rsa = RSAFactory.Create())
667666
{
668667
rsa.ImportParameters(publicOnly);
669-
signatureMatched = rsa.VerifyData(data, hashAlgorithmName, signature);
668+
signatureMatched = rsa.VerifyData(data, signature, new HashAlgorithmName(hashAlgorithmName), RSASignaturePadding.Pkcs1);
670669
}
671670

672671
Assert.True(signatureMatched);
@@ -686,22 +685,22 @@ private static void VerifyHashSignature(
686685

687686
bool signatureMatched;
688687

689-
using (var rsa = new RSACryptoServiceProvider())
688+
using (RSA rsa = RSAFactory.Create())
690689
{
691690
rsa.ImportParameters(publicOnly);
692-
signatureMatched = rsa.VerifyHash(dataHash, hashAlgorithmName, signature);
691+
signatureMatched = rsa.VerifyHash(dataHash, signature, new HashAlgorithmName(hashAlgorithmName), RSASignaturePadding.Pkcs1);
693692
}
694693

695694
Assert.True(signatureMatched);
696695
}
697696

698697
private static void SignAndVerify(byte[] data, string hashAlgorithmName, RSAParameters rsaParameters)
699698
{
700-
using (var rsa = new RSACryptoServiceProvider())
699+
using (RSA rsa = RSAFactory.Create())
701700
{
702701
rsa.ImportParameters(rsaParameters);
703-
byte[] signature = rsa.SignData(data, hashAlgorithmName);
704-
bool signatureMatched = rsa.VerifyData(data, hashAlgorithmName, signature);
702+
byte[] signature = rsa.SignData(data, new HashAlgorithmName(hashAlgorithmName), RSASignaturePadding.Pkcs1);
703+
bool signatureMatched = rsa.VerifyData(data, signature, new HashAlgorithmName(hashAlgorithmName), RSASignaturePadding.Pkcs1);
705704
Assert.True(signatureMatched);
706705
}
707706
}

0 commit comments

Comments
 (0)