Skip to content

Commit fbe0f8c

Browse files
committed
Handle minimum key size for OAEP (prevent overflow error with 512bit keys and OaepSHA256), add testing for more key sizes
1 parent a79121f commit fbe0f8c

5 files changed

Lines changed: 34 additions & 6 deletions

File tree

RSA.commons/ICommonRSAUnitTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace RSA.commons;
22

33
public interface ICommonRSAUnitTest
44
{
5-
void GenerateKeys_ShouldNotErrorAndReturnWorkingKeys();
5+
void GenerateKeys_ShouldNotErrorAndReturnWorkingKeysInAllSizes();
66
void EncryptString_ShouldRunWithProvenKeysOfMultipleFormats();
77
void DecryptString_ShouldOutputCorrectTextWithProvenKeysOfMultipleFormats();
88
void EncryptString_ShouldThrowExceptionWithInvalidPadding();

RSA/RSA.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ private static byte[] ApplyPkcs1Padding(byte[] dataBytes, int keySize)
268268

269269
private static byte[] ApplyOaepPadding(byte[] dataBytes, int keySize, bool useSHA256)
270270
{
271+
// Check if the key size is large enough for OAEP
272+
int maxKeySize = useSHA256 ? 1024 : 512;
273+
if (keySize < maxKeySize)
274+
{
275+
throw new ArgumentException("Key size too small for OAEP padding (minimum 1024 bits for SHA256, 512 bits for SHA1)");
276+
}
277+
271278
int chunkSize = keySize / 8; // Chunk size is the key's size in bytes
272279
int hashLength = useSHA256 ? 32 : 20;
273280
int dbLength = chunkSize - hashLength - 1;

cs-RSA.sln.DotSettings.user

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@
77
<s:String x:Key="/Default/Environment/UnitTesting/CreateUnitTestDialog/TestProjectMapping/=69E3EC28_002D9C44_002D4220_002DACEE_002D5FE1CF7A4C42/@EntryIndexedValue">E56C5D00-9269-48D3-AC8A-C0CF7FD60592</s:String>
88
<s:String x:Key="/Default/Environment/UnitTesting/CreateUnitTestDialog/TestProjectMapping/=6CD39657_002D12AF_002D456D_002DA7E7_002D65106279F02E/@EntryIndexedValue">ReferenceRSA.Tests</s:String>
99
<s:String x:Key="/Default/Environment/UnitTesting/CreateUnitTestDialog/TestTemplateMapping/=MSTest/@EntryIndexedValue">d6790ab7-33c2-4425-b2c9-51480cd1a852</s:String>
10-
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=d027b926_002D8256_002D4fc4_002D8f60_002D601fb40bbfd3/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="GenerateKeys_ShouldNotErrorAndReturnWorkingKeys" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
10+
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=10f3e468_002D8ebc_002D4d90_002D980c_002D37791e2f0eeb/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="GenerateKeys_ShouldNotErrorAndReturnWorkingKeysInAllSizes #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
11+
&lt;TestAncestor&gt;&#xD;
12+
&lt;TestId&gt;MSTest::E56C5D00-9269-48D3-AC8A-C0CF7FD60592::net9.0::csRSA.Tests.RSATest.GenerateKeys_ShouldNotErrorAndReturnWorkingKeysInAllSizes&lt;/TestId&gt;&#xD;
13+
&lt;/TestAncestor&gt;&#xD;
14+
&lt;/SessionState&gt;</s:String>
15+
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=3364e7df_002Df20f_002D487b_002D82ad_002Dfad6c0538bf0/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="GenerateKeys_ShouldNotErrorAndReturnWorkingKeys" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
16+
&lt;TestAncestor&gt;&#xD;
17+
&lt;TestId&gt;MSTest::E56C5D00-9269-48D3-AC8A-C0CF7FD60592::net9.0::csRSA.Tests.RSATest&lt;/TestId&gt;&#xD;
18+
&lt;TestId&gt;MSTest::E56C5D00-9269-48D3-AC8A-C0CF7FD60592::net9.0::csRSA.Tests.ReferenceRSATest.GenerateKeys_ShouldNotErrorAndReturnWorkingKeysInAllSizes&lt;/TestId&gt;&#xD;
19+
&lt;/TestAncestor&gt;&#xD;
20+
&lt;/SessionState&gt;</s:String>
21+
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=b2bbe27c_002Da36a_002D49c3_002D95b5_002Df24073568e90/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="GenerateKeys_ShouldNotErrorAndReturnWorkingKeysInAllSizes" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
22+
&lt;TestAncestor&gt;&#xD;
23+
&lt;TestId&gt;MSTest::E56C5D00-9269-48D3-AC8A-C0CF7FD60592::net9.0::csRSA.Tests.ReferenceRSATest.GenerateKeys_ShouldNotErrorAndReturnWorkingKeysInAllSizes&lt;/TestId&gt;&#xD;
24+
&lt;TestId&gt;MSTest::E56C5D00-9269-48D3-AC8A-C0CF7FD60592::net9.0::csRSA.Tests.RSATest.GenerateKeys_ShouldNotErrorAndReturnWorkingKeysInAllSizes&lt;/TestId&gt;&#xD;
25+
&lt;/TestAncestor&gt;&#xD;
26+
&lt;/SessionState&gt;</s:String>
27+
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=d027b926_002D8256_002D4fc4_002D8f60_002D601fb40bbfd3/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="GenerateKeys_ShouldNotErrorAndReturnWorkingKeys" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
1128
&lt;Solution /&gt;&#xD;
1229
&lt;/SessionState&gt;</s:String>
1330
<s:Boolean x:Key="/Default/UserDictionary/Words/=bitperbyte/@EntryIndexedValue">True</s:Boolean>

csRSA.Tests/RSATest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public class RSATest : ICommonRSAUnitTest
1414

1515
[TestMethod]
1616
// Pass if generating keys and then using them doesn't throw an error and the decrypted text is the same as the original text
17-
public void GenerateKeys_ShouldNotErrorAndReturnWorkingKeys()
17+
public void GenerateKeys_ShouldNotErrorAndReturnWorkingKeysInAllSizes()
1818
{
19-
int[] keySizes = [512, 1024, 2048, 4096];
19+
int[] keySizes = [512, 1024, 2048, 4096, 8192];
2020
string text = "Less than 256 bits";
2121

2222
foreach (int keySize in keySizes)
@@ -25,6 +25,7 @@ public void GenerateKeys_ShouldNotErrorAndReturnWorkingKeys()
2525

2626
string encryptedText = _selfImplementedRSA.EncryptString(publicKey, "pkcs1", text);
2727
string decryptedText = _selfImplementedRSA.DecryptString(privateKey, "pkcs1", encryptedText);
28+
2829
Assert.AreEqual(text, decryptedText);
2930
}
3031
}
@@ -44,6 +45,7 @@ public void EncryptString_ShouldRunWithProvenKeysOfMultipleFormats()
4445
{
4546
string cipherText = _selfImplementedRSA.EncryptString(publicKey, "pkcs1", plainText);
4647
byte[] encryptedBytes = Convert.FromBase64String(cipherText);
48+
4749
Assert.AreEqual(256, encryptedBytes.Length);
4850
}
4951
}

csRSA.Tests/ReferenceRSATest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public class ReferenceRSATest : ICommonRSAUnitTest
1414

1515
[TestMethod]
1616
// Pass if generating keys and then using them doesn't throw an error and the decrypted text is the same as the original text
17-
public void GenerateKeys_ShouldNotErrorAndReturnWorkingKeys()
17+
public void GenerateKeys_ShouldNotErrorAndReturnWorkingKeysInAllSizes()
1818
{
19-
int[] keySizes = [512, 1024, 2048, 4096];
19+
int[] keySizes = [512, 1024, 2048, 4096, 8192];
2020
string text = "Less than 256 bits";
2121

2222
foreach (int keySize in keySizes)
@@ -25,6 +25,7 @@ public void GenerateKeys_ShouldNotErrorAndReturnWorkingKeys()
2525

2626
string encryptedText = _referenceRSA.EncryptString(publicKey, "pkcs1", text);
2727
string decryptedText = _referenceRSA.DecryptString(privateKey, "pkcs1", encryptedText);
28+
2829
Assert.AreEqual(text, decryptedText);
2930
}
3031
}
@@ -44,6 +45,7 @@ public void EncryptString_ShouldRunWithProvenKeysOfMultipleFormats()
4445
{
4546
string cipherText = _referenceRSA.EncryptString(publicKey, "pkcs1", plainText);
4647
byte[] encryptedBytes = Convert.FromBase64String(cipherText);
48+
4749
Assert.AreEqual(256, encryptedBytes.Length);
4850
}
4951
}

0 commit comments

Comments
 (0)