File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -9,14 +9,16 @@ namespace System.Security.Cryptography
99{
1010 public sealed partial class AesCcm
1111 {
12- private byte [ ] _key ;
12+ private byte [ ] ? _key ;
1313
1414 public static bool IsSupported => true ;
1515
1616 [ MemberNotNull ( nameof ( _key ) ) ]
1717 private void ImportKey ( ReadOnlySpan < byte > key )
1818 {
19- _key = key . ToArray ( ) ;
19+ // Pin the array on the POH so that the GC doesn't move it around to allow zeroing to be more effective.
20+ _key = GC . AllocateArray < byte > ( key . Length , pinned : true ) ;
21+ key . CopyTo ( _key ) ;
2022 }
2123
2224 private void EncryptCore (
@@ -26,6 +28,8 @@ private void EncryptCore(
2628 Span < byte > tag ,
2729 ReadOnlySpan < byte > associatedData = default )
2830 {
31+ CheckDisposed ( ) ;
32+
2933 // Convert key length to bits.
3034 using ( SafeEvpCipherCtxHandle ctx = Interop . Crypto . EvpCipherCreatePartial ( GetCipher ( _key . Length * 8 ) ) )
3135 {
@@ -109,6 +113,8 @@ private void DecryptCore(
109113 Span < byte > plaintext ,
110114 ReadOnlySpan < byte > associatedData )
111115 {
116+ CheckDisposed ( ) ;
117+
112118 using ( SafeEvpCipherCtxHandle ctx = Interop . Crypto . EvpCipherCreatePartial ( GetCipher ( _key . Length * 8 ) ) )
113119 {
114120 if ( ctx . IsInvalid )
@@ -180,9 +186,16 @@ private static IntPtr GetCipher(int keySizeInBits)
180186 } ;
181187 }
182188
189+ [ MemberNotNull ( nameof ( _key ) ) ]
190+ private void CheckDisposed ( )
191+ {
192+ ObjectDisposedException . ThrowIf ( _key is null , this ) ;
193+ }
194+
183195 public void Dispose ( )
184196 {
185197 CryptographicOperations . ZeroMemory ( _key ) ;
198+ _key = null ;
186199 }
187200 }
188201}
You can’t perform that action at this time.
0 commit comments