Skip to content

Commit 18eec02

Browse files
committed
minor improvements
1 parent 34d52e7 commit 18eec02

File tree

4 files changed

+7
-9
lines changed

4 files changed

+7
-9
lines changed

src/DataProtection/DataProtection/src/AuthenticatedEncryption/AuthenticatedEncryptorExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public static bool TryEncrypt(
4343
out int bytesWritten)
4444
{
4545
var destinationBufferOffsets = destination.Slice(preBufferSize, destination.Length - (preBufferSize + postBufferSize));
46-
// var plaintextWithOffsets = plaintext.Slice((int)preBufferSize, plaintext.Length - (int)(preBufferSize + postBufferSize));
4746
return encryptor.TryEncrypt(plaintext, additionalAuthenticatedData, destinationBufferOffsets, out bytesWritten);
4847
}
4948
#endif

src/DataProtection/DataProtection/src/Cng/CbcAuthenticatedEncryptor.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Runtime.CompilerServices;
56
using Microsoft.AspNetCore.Cryptography;
67
using Microsoft.AspNetCore.Cryptography.Cng;
78
using Microsoft.AspNetCore.Cryptography.SafeHandles;
@@ -363,7 +364,7 @@ public override bool TryEncrypt(ReadOnlySpan<byte> plaintext, ReadOnlySpan<byte>
363364
byte* pbOutputHmac = &pbOutputCiphertext[cbOutputCiphertext];
364365

365366
// Copy key modifier and IV to destination
366-
UnsafeBufferUtil.BlockCopy(from: pbKeyModifierAndIV, to: pbOutputKeyModifier, byteCount: cbKeyModifierAndIV);
367+
Unsafe.CopyBlock(pbOutputKeyModifier, pbKeyModifierAndIV, cbKeyModifierAndIV);
367368
bytesWritten += checked((int)cbKeyModifierAndIV);
368369

369370
// Perform CBC encryption directly into destination
@@ -512,7 +513,6 @@ private uint GetCbcEncryptedOutputSizeWithPadding(uint cbInput)
512513
byte* pbDummyIV = stackalloc byte[checked((int)_symmetricAlgorithmBlockSizeInBytes)];
513514
byte* pbDummyInput = stackalloc byte[checked((int)cbInput)];
514515

515-
uint dwResult;
516516
var ntstatus = UnsafeNativeMethods.BCryptEncrypt(
517517
hKey: tempKeyHandle,
518518
pbInput: pbDummyInput,
@@ -522,7 +522,7 @@ private uint GetCbcEncryptedOutputSizeWithPadding(uint cbInput)
522522
cbIV: _symmetricAlgorithmBlockSizeInBytes,
523523
pbOutput: null, // NULL output = size query only
524524
cbOutput: 0,
525-
pcbResult: out dwResult,
525+
pcbResult: out var dwResult,
526526
dwFlags: BCryptEncryptFlags.BCRYPT_BLOCK_PADDING);
527527
UnsafeNativeMethods.ThrowExceptionForBCryptStatus(ntstatus);
528528

@@ -536,7 +536,6 @@ private uint GetCbcEncryptedOutputSizeWithPadding(BCryptKeyHandle symmetricKeyHa
536536

537537
// Calling BCryptEncrypt with a null output pointer will cause it to return the total number
538538
// of bytes required for the output buffer.
539-
uint dwResult;
540539
var ntstatus = UnsafeNativeMethods.BCryptEncrypt(
541540
hKey: symmetricKeyHandle,
542541
pbInput: pbInput,
@@ -546,7 +545,7 @@ private uint GetCbcEncryptedOutputSizeWithPadding(BCryptKeyHandle symmetricKeyHa
546545
cbIV: _symmetricAlgorithmBlockSizeInBytes,
547546
pbOutput: null,
548547
cbOutput: 0,
549-
pcbResult: out dwResult,
548+
pcbResult: out var dwResult,
550549
dwFlags: BCryptEncryptFlags.BCRYPT_BLOCK_PADDING);
551550
UnsafeNativeMethods.ThrowExceptionForBCryptStatus(ntstatus);
552551

src/DataProtection/DataProtection/src/KeyManagement/KeyRingBasedDataProtector.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ public bool TryProtect(ReadOnlySpan<byte> plaintext, Span<byte> destination, out
124124
// If the default key id has been updated since the last call to Protect, also write back the updated template.
125125
var aad = _aadTemplate.GetAadForKey(defaultKeyId, isProtecting: true);
126126

127-
// We allocate a 20-byte pre-buffer so that we can inject the magic header and key id into the return value.
128127
var success = defaultEncryptorInstance.TryEncrypt(
129128
plaintext: plaintext,
130129
additionalAuthenticatedData: aad,

src/DataProtection/DataProtection/src/SP800_108/SP800_108_CTR_HMACSHA512Extensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Runtime.CompilerServices;
45
using Microsoft.AspNetCore.Cryptography;
56

67
namespace Microsoft.AspNetCore.DataProtection.SP800_108;
@@ -24,9 +25,9 @@ public static void DeriveKeyWithContextHeader(this ISP800_108_CTR_HMACSHA512Prov
2425

2526
fixed (byte* pbContextHeader = contextHeader)
2627
{
27-
UnsafeBufferUtil.BlockCopy(from: pbContextHeader, to: pbCombinedContext, byteCount: contextHeader.Length);
28+
Unsafe.CopyBlock(pbCombinedContext, pbContextHeader, (uint)contextHeader.Length);
2829
}
29-
UnsafeBufferUtil.BlockCopy(from: pbContext, to: &pbCombinedContext[contextHeader.Length], byteCount: cbContext);
30+
Unsafe.CopyBlock(&pbCombinedContext[contextHeader.Length], pbContext, cbContext);
3031

3132
// At this point, combinedContext := { contextHeader || context }
3233
provider.DeriveKey(pbLabel, cbLabel, pbCombinedContext, cbCombinedContext, pbDerivedKey, cbDerivedKey);

0 commit comments

Comments
 (0)