Skip to content

Commit 4ba9cc2

Browse files
committed
address PR comments
1 parent 49eab48 commit 4ba9cc2

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/DataProtection/DataProtection/src/KeyManagement/KeyManagementOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class KeyManagementOptions
2323
/// <summary>
2424
/// Initializes a new instance of <see cref="KeyManagementOptions"/>.
2525
/// </summary>
26-
public KeyManagementOptions()
26+
public KeyManagementOptions()
2727
{
2828
}
2929

@@ -111,7 +111,7 @@ internal static TimeSpan MaxServerClockSkew
111111
/// <remarks>
112112
/// Settable for testing.
113113
/// </remarks>
114-
internal TimeSpan DefaultKeyResolverRetryDelay { get; set; } = TimeSpan.FromMilliseconds(200.0);
114+
internal TimeSpan DefaultKeyResolverRetryDelay { get; set; } = TimeSpan.FromMilliseconds(200);
115115

116116
/// <summary>
117117
/// Controls the lifetime (number of days before expiration)

src/DataProtection/DataProtection/src/Managed/ManagedAuthenticatedEncryptor.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ public byte[] Decrypt(ArraySegment<byte> protectedPayload, ArraySegment<byte> ad
168168
protectedPayload.Validate();
169169
additionalAuthenticatedData.Validate();
170170

171-
var protectedPayloadSpan = protectedPayload.AsSpan();
172-
173171
// Argument checking - input must at the absolute minimum contain a key modifier, IV, and MAC
174172
if (protectedPayload.Count < checked(KEY_MODIFIER_SIZE_IN_BYTES + _symmetricAlgorithmBlockSizeInBytes + _validationAlgorithmDigestLengthInBytes))
175173
{
@@ -194,7 +192,7 @@ public byte[] Decrypt(ArraySegment<byte> protectedPayload, ArraySegment<byte> ad
194192
ciphertextOffset = ivOffset + _symmetricAlgorithmBlockSizeInBytes;
195193
}
196194

197-
ReadOnlySpan<byte> keyModifier = protectedPayload.Array!.AsSpan().Slice(keyModifierOffset, ivOffset - keyModifierOffset);
195+
ReadOnlySpan<byte> keyModifier = protectedPayload.Array!.AsSpan(keyModifierOffset, ivOffset - keyModifierOffset);
198196

199197
// Step 2: Decrypt the KDK and use it to restore the original encryption and MAC keys.
200198
// We pin all unencrypted keys to limit their exposure via GC relocation.
@@ -245,8 +243,8 @@ public byte[] Decrypt(ArraySegment<byte> protectedPayload, ArraySegment<byte> ad
245243
using var symmetricAlgorithm = CreateSymmetricAlgorithm(key: decryptionSubkey);
246244

247245
// note: here protectedPayload.Array is taken without an offset (cant use AsSpan() on ArraySegment)
248-
var ciphertext = protectedPayload.Array.AsSpan().Slice(ciphertextOffset, macOffset - ciphertextOffset);
249-
var iv = protectedPayload.Array.AsSpan().Slice(ivOffset, _symmetricAlgorithmBlockSizeInBytes);
246+
var ciphertext = protectedPayload.Array.AsSpan(ciphertextOffset, macOffset - ciphertextOffset);
247+
var iv = protectedPayload.Array.AsSpan(ivOffset, _symmetricAlgorithmBlockSizeInBytes);
250248

251249
return symmetricAlgorithm.DecryptCbc(ciphertext, iv); // symmetricAlgorithm is created with CBC mode
252250
#else

src/DataProtection/DataProtection/src/SP800_108/ManagedSP800_108_CTR_HMACSHA512.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ public static void DeriveKeys(
116116
if (operationSubKeyIndex < operationSubKey.Length) // meaning we need to write to operationSubKey
117117
{
118118
var destination = operationSubKey.Slice(operationSubKeyIndex, bytesToWrite);
119-
prfOutput.AsSpan().Slice(0, bytesToWrite).CopyTo(destination);
119+
prfOutput.AsSpan(0, bytesToWrite).CopyTo(destination);
120120
operationSubKeyIndex += bytesToWrite;
121121
}
122122
if (operationSubKeyIndex == operationSubKey.Length && leftOverBytes != 0) // we have filled the operationSubKey. It's time for the validationSubKey
123123
{
124124
var destination = validationSubKey.Slice(validationSubKeyIndex, leftOverBytes);
125-
prfOutput.AsSpan().Slice(bytesToWrite, leftOverBytes).CopyTo(destination);
125+
prfOutput.AsSpan(bytesToWrite, leftOverBytes).CopyTo(destination);
126126
validationSubKeyIndex += leftOverBytes;
127127
}
128128

@@ -136,6 +136,10 @@ public static void DeriveKeys(
136136
}
137137
}
138138

139+
/// <remarks>
140+
/// Probably, you would want to use similar method <see cref="DeriveKeys(byte[], ReadOnlySpan{byte}, ReadOnlySpan{byte}, ReadOnlySpan{byte}, Func{byte[], HashAlgorithm}, Span{byte}, Span{byte})"/>.
141+
/// It is more efficient allowing to skip an allocation of `combinedContext` and writing directly into passed Spans
142+
/// </remarks>
139143
public static void DeriveKeysWithContextHeader(byte[] kdk, ArraySegment<byte> label, byte[] contextHeader, ArraySegment<byte> context, Func<byte[], HashAlgorithm> prfFactory, ArraySegment<byte> output)
140144
{
141145
var combinedContext = new byte[checked(contextHeader.Length + context.Count)];

0 commit comments

Comments
 (0)