@@ -194,20 +194,18 @@ public byte[] Decrypt(ArraySegment<byte> protectedPayload, ArraySegment<byte> ad
194194                ciphertextOffset  =  ivOffset  +  _symmetricAlgorithmBlockSizeInBytes ; 
195195            } 
196196
197-             ArraySegment < byte >  keyModifier  =  new   ArraySegment < byte > ( protectedPayload . Array ! ,   keyModifierOffset ,  ivOffset  -  keyModifierOffset ) ; 
197+             ReadOnlySpan < byte >  keyModifier  =  protectedPayload . Array ! . AsSpan ( ) . Slice ( keyModifierOffset ,  ivOffset  -  keyModifierOffset ) ; 
198198
199199            // Step 2: Decrypt the KDK and use it to restore the original encryption and MAC keys. 
200200            // We pin all unencrypted keys to limit their exposure via GC relocation. 
201201
202202            var  decryptedKdk  =  new  byte [ _keyDerivationKey . Length ] ; 
203203            var  decryptionSubkey  =  new  byte [ _symmetricAlgorithmSubkeyLengthInBytes ] ; 
204204            var  validationSubkey  =  new  byte [ _validationAlgorithmSubkeyLengthInBytes ] ; 
205-             var  derivedKeysBuffer  =  new  byte [ checked ( decryptionSubkey . Length  +  validationSubkey . Length ) ] ; 
206205
207206            fixed ( byte *  __unused__1  =  decryptedKdk ) 
208207            fixed ( byte *  __unused__2  =  decryptionSubkey ) 
209208            fixed ( byte *  __unused__3  =  validationSubkey ) 
210-             fixed ( byte *  __unused__4  =  derivedKeysBuffer ) 
211209            { 
212210                try 
213211                { 
@@ -218,10 +216,8 @@ public byte[] Decrypt(ArraySegment<byte> protectedPayload, ArraySegment<byte> ad
218216                        contextHeader :  _contextHeader , 
219217                        contextData :  keyModifier , 
220218                        prfFactory :  _kdkPrfFactory , 
221-                         output :  new  ArraySegment < byte > ( derivedKeysBuffer ) ) ; 
222- 
223-                     derivedKeysBuffer . AsSpan ( ) . Slice ( start :  0 ,  length :  decryptionSubkey . Length ) . CopyTo ( decryptionSubkey ) ; 
224-                     derivedKeysBuffer . AsSpan ( ) . Slice ( start :  decryptionSubkey . Length ,  length :  validationSubkey . Length ) . CopyTo ( validationSubkey ) ; 
219+                         operationSubKey :  decryptionSubkey , 
220+                         validationSubKey :  validationSubkey ) ; 
225221
226222                    // Step 3: Calculate the correct MAC for this payload. 
227223                    // correctHash := MAC(IV || ciphertext) 
@@ -255,7 +251,7 @@ public byte[] Decrypt(ArraySegment<byte> protectedPayload, ArraySegment<byte> ad
255251                    return  symmetricAlgorithm . DecryptCbc ( ciphertext ,  iv ) ;  // symmetricAlgorithm is created with CBC mode 
256252#else
257253                    var  iv  =  new  byte [ _symmetricAlgorithmBlockSizeInBytes ] ; 
258-                     Buffer . BlockCopy ( protectedPayload . Array ! ,   ivOffset ,  iv ,   0 ,   iv . Length ) ; 
254+                     protectedPayload . Array . AsSpan ( ) . Slice ( ivOffset ,  iv . Length ) . CopyTo ( iv ) ; 
259255
260256                    using  var  symmetricAlgorithm  =  CreateSymmetricAlgorithm ( ) ; 
261257                    using  ( var  cryptoTransform  =  symmetricAlgorithm . CreateDecryptor ( decryptionSubkey ,  iv ) )     
@@ -273,7 +269,6 @@ public byte[] Decrypt(ArraySegment<byte> protectedPayload, ArraySegment<byte> ad
273269                    Array . Clear ( decryptedKdk ,  0 ,  decryptedKdk . Length ) ; 
274270                    Array . Clear ( decryptionSubkey ,  0 ,  decryptionSubkey . Length ) ; 
275271                    Array . Clear ( validationSubkey ,  0 ,  validationSubkey . Length ) ; 
276-                     Array . Clear ( derivedKeysBuffer ,  0 ,  derivedKeysBuffer . Length ) ; 
277272                } 
278273            } 
279274        } 
0 commit comments