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

Commit eb32fdd

Browse files
committed
Merge pull request #1992 from stephentoub/aes_overflow
Fix overflowing key size in AesImplementation
2 parents 568b7d7 + e560040 commit eb32fdd

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

src/System.Security.Cryptography.Encryption.Aes/src/Internal/Cryptography/AesImplementation.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ private ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encry
4141
{
4242
if (rgbKey == null)
4343
throw new ArgumentNullException("key");
44-
int keySize = rgbKey.Length * BitsPerByte;
45-
if (!keySize.IsLegalSize(this.LegalKeySizes))
46-
throw new ArgumentException(SR.Format(SR.Cryptography_InvalidKeySize, "key"));
47-
if (rgbIV != null && rgbIV.Length * BitsPerByte != BlockSize)
48-
throw new ArgumentException(SR.Format(SR.Cryptography_InvalidIVSize, "iv"));
44+
45+
long keySize = rgbKey.Length * (long)BitsPerByte;
46+
if (keySize > int.MaxValue || !((int)keySize).IsLegalSize(this.LegalKeySizes))
47+
throw new ArgumentException(SR.Cryptography_InvalidKeySize, "key");
48+
49+
if (rgbIV != null)
50+
{
51+
long ivSize = rgbIV.Length * (long)BitsPerByte;
52+
if (ivSize != BlockSize)
53+
throw new ArgumentException(SR.Cryptography_InvalidIVSize, "iv");
54+
}
4955

5056
if (encrypting)
5157
return CreateEncryptor(Mode, Padding, rgbKey, rgbIV, BlockSize / BitsPerByte);

src/System.Security.Cryptography.Encryption.Aes/tests/AesContractTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,59 @@ public static void LegalKeySizes()
5757
}
5858
}
5959

60+
[Theory]
61+
[InlineData(64)] // too small
62+
[InlineData(129)] // in valid range but not valid increment
63+
[InlineData(384)] // too large
64+
[InlineData(536870928)] // number of bits overflows and wraps around to a valid size
65+
public static void InvalidKeySizes(int invalidKeySize)
66+
{
67+
using (Aes aes = Aes.Create())
68+
{
69+
// Test KeySize property
70+
Assert.Throws<CryptographicException>(() => aes.KeySize = invalidKeySize);
71+
72+
// Test passing a key to CreateEncryptor and CreateDecryptor
73+
aes.GenerateIV();
74+
byte[] iv = aes.IV;
75+
byte[] key;
76+
try
77+
{
78+
key = new byte[invalidKeySize];
79+
}
80+
catch (OutOfMemoryException) // in case there isn't enough memory at test-time to allocate the large array
81+
{
82+
return;
83+
}
84+
Assert.Throws<ArgumentException>("key", () => aes.CreateEncryptor(key, iv));
85+
Assert.Throws<ArgumentException>("key", () => aes.CreateDecryptor(key, iv));
86+
}
87+
}
88+
89+
[Theory]
90+
[InlineData(64)] // smaller than default BlockSize
91+
[InlineData(129)] // larger than default BlockSize
92+
[InlineData(536870928)] // number of bits overflows and wraps around to default BlockSize
93+
public static void InvalidIVSizes(int invalidIvSize)
94+
{
95+
using (Aes aes = Aes.Create())
96+
{
97+
aes.GenerateKey();
98+
byte[] key = aes.Key;
99+
byte[] iv;
100+
try
101+
{
102+
iv = new byte[invalidIvSize];
103+
}
104+
catch (OutOfMemoryException) // in case there isn't enough memory at test-time to allocate the large array
105+
{
106+
return;
107+
}
108+
Assert.Throws<ArgumentException>("iv", () => aes.CreateEncryptor(key, iv));
109+
Assert.Throws<ArgumentException>("iv", () => aes.CreateDecryptor(key, iv));
110+
}
111+
}
112+
60113
[Fact]
61114
public static void VerifyKeyGeneration_Default()
62115
{

0 commit comments

Comments
 (0)