Skip to content

Commit b3ffd33

Browse files
committed
Throw PNSE for GCM/CCM on unsupported platforms
1 parent ac63e25 commit b3ffd33

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesCcm.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ public sealed partial class AesCcm : IDisposable
1414

1515
public AesCcm(ReadOnlySpan<byte> key)
1616
{
17+
ThrowIfNotSupported();
18+
1719
AesAEAD.CheckKeySize(key.Length);
1820
ImportKey(key);
1921
}
2022

2123
public AesCcm(byte[] key)
2224
{
25+
ThrowIfNotSupported();
26+
2327
if (key == null)
2428
throw new ArgumentNullException(nameof(key));
2529

@@ -76,5 +80,13 @@ private static void CheckParameters(
7680
if (!tag.Length.IsLegalSize(TagByteSizes))
7781
throw new ArgumentException(SR.Cryptography_InvalidTagLength, nameof(tag));
7882
}
83+
84+
private static void ThrowIfNotSupported()
85+
{
86+
if (!IsSupported)
87+
{
88+
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(AesCcm)));
89+
}
90+
}
7991
}
8092
}

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesGcm.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ public sealed partial class AesGcm : IDisposable
1515

1616
public AesGcm(ReadOnlySpan<byte> key)
1717
{
18+
ThrowIfNotSupported();
19+
1820
AesAEAD.CheckKeySize(key.Length);
1921
ImportKey(key);
2022
}
2123

2224
public AesGcm(byte[] key)
2325
{
26+
ThrowIfNotSupported();
27+
2428
if (key == null)
2529
throw new ArgumentNullException(nameof(key));
2630

@@ -77,5 +81,13 @@ private static void CheckParameters(
7781
if (!tag.Length.IsLegalSize(TagByteSizes))
7882
throw new ArgumentException(SR.Cryptography_InvalidTagLength, nameof(tag));
7983
}
84+
85+
private static void ThrowIfNotSupported()
86+
{
87+
if (!IsSupported)
88+
{
89+
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(AesGcm)));
90+
}
91+
}
8092
}
8193
}

src/libraries/System.Security.Cryptography.Algorithms/tests/AesCcmTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,17 @@ public static IEnumerable<object[]> GetNistCcmTestCasesWithNonEmptyPT()
678678

679679
public class AesCcmIsSupportedTests
680680
{
681+
public static bool RuntimeSaysIsNotSupported => !AesCcm.IsSupported;
682+
683+
[ConditionalFact(nameof(RuntimeSaysIsNotSupported))]
684+
public static void CtorThrowsPNSEIfNotSupported()
685+
{
686+
byte[] key = RandomNumberGenerator.GetBytes(256 / 8);
687+
688+
Assert.Throws<PlatformNotSupportedException>(() => new AesCcm(key));
689+
Assert.Throws<PlatformNotSupportedException>(() => new AesCcm(key.AsSpan()));
690+
}
691+
681692
[Fact]
682693
public static void CheckIsSupported()
683694
{

src/libraries/System.Security.Cryptography.Algorithms/tests/AesGcmTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,17 @@ private static IEnumerable<AEADTest> GetNistTests()
839839

840840
public class AesGcmIsSupportedTests
841841
{
842+
public static bool RuntimeSaysIsNotSupported => !AesGcm.IsSupported;
843+
844+
[ConditionalFact(nameof(RuntimeSaysIsNotSupported))]
845+
public static void CtorThrowsPNSEIfNotSupported()
846+
{
847+
byte[] key = RandomNumberGenerator.GetBytes(256 / 8);
848+
849+
Assert.Throws<PlatformNotSupportedException>(() => new AesGcm(key));
850+
Assert.Throws<PlatformNotSupportedException>(() => new AesGcm(key.AsSpan()));
851+
}
852+
842853
[Fact]
843854
public static void CheckIsSupported()
844855
{

0 commit comments

Comments
 (0)