File tree Expand file tree Collapse file tree 4 files changed +46
-0
lines changed
src/libraries/System.Security.Cryptography.Algorithms
src/System/Security/Cryptography Expand file tree Collapse file tree 4 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -14,12 +14,16 @@ public sealed partial class AesCcm : IDisposable
14
14
15
15
public AesCcm ( ReadOnlySpan < byte > key )
16
16
{
17
+ ThrowIfNotSupported ( ) ;
18
+
17
19
AesAEAD . CheckKeySize ( key . Length ) ;
18
20
ImportKey ( key ) ;
19
21
}
20
22
21
23
public AesCcm ( byte [ ] key )
22
24
{
25
+ ThrowIfNotSupported ( ) ;
26
+
23
27
if ( key == null )
24
28
throw new ArgumentNullException ( nameof ( key ) ) ;
25
29
@@ -76,5 +80,13 @@ private static void CheckParameters(
76
80
if ( ! tag . Length . IsLegalSize ( TagByteSizes ) )
77
81
throw new ArgumentException ( SR . Cryptography_InvalidTagLength , nameof ( tag ) ) ;
78
82
}
83
+
84
+ private static void ThrowIfNotSupported ( )
85
+ {
86
+ if ( ! IsSupported )
87
+ {
88
+ throw new PlatformNotSupportedException ( SR . Format ( SR . Cryptography_AlgorithmNotSupported , nameof ( AesCcm ) ) ) ;
89
+ }
90
+ }
79
91
}
80
92
}
Original file line number Diff line number Diff line change @@ -15,12 +15,16 @@ public sealed partial class AesGcm : IDisposable
15
15
16
16
public AesGcm ( ReadOnlySpan < byte > key )
17
17
{
18
+ ThrowIfNotSupported ( ) ;
19
+
18
20
AesAEAD . CheckKeySize ( key . Length ) ;
19
21
ImportKey ( key ) ;
20
22
}
21
23
22
24
public AesGcm ( byte [ ] key )
23
25
{
26
+ ThrowIfNotSupported ( ) ;
27
+
24
28
if ( key == null )
25
29
throw new ArgumentNullException ( nameof ( key ) ) ;
26
30
@@ -77,5 +81,13 @@ private static void CheckParameters(
77
81
if ( ! tag . Length . IsLegalSize ( TagByteSizes ) )
78
82
throw new ArgumentException ( SR . Cryptography_InvalidTagLength , nameof ( tag ) ) ;
79
83
}
84
+
85
+ private static void ThrowIfNotSupported ( )
86
+ {
87
+ if ( ! IsSupported )
88
+ {
89
+ throw new PlatformNotSupportedException ( SR . Format ( SR . Cryptography_AlgorithmNotSupported , nameof ( AesGcm ) ) ) ;
90
+ }
91
+ }
80
92
}
81
93
}
Original file line number Diff line number Diff line change @@ -678,6 +678,17 @@ public static IEnumerable<object[]> GetNistCcmTestCasesWithNonEmptyPT()
678
678
679
679
public class AesCcmIsSupportedTests
680
680
{
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
+
681
692
[ Fact ]
682
693
public static void CheckIsSupported ( )
683
694
{
Original file line number Diff line number Diff line change @@ -839,6 +839,17 @@ private static IEnumerable<AEADTest> GetNistTests()
839
839
840
840
public class AesGcmIsSupportedTests
841
841
{
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
+
842
853
[ Fact ]
843
854
public static void CheckIsSupported ( )
844
855
{
You can’t perform that action at this time.
0 commit comments