Skip to content

Commit 6ec76d3

Browse files
committed
use same overload in decrypt()
1 parent d090882 commit 6ec76d3

File tree

2 files changed

+4
-58
lines changed

2 files changed

+4
-58
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

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

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -55,55 +55,6 @@ public static void DeriveKeys(byte[] kdk, ArraySegment<byte> label, ArraySegment
5555
}
5656
}
5757

58-
public static void DeriveKeys(byte[] kdk, ReadOnlySpan<byte> label, ReadOnlySpan<byte> contextHeader, ReadOnlySpan<byte> contextData, Func<byte[], HashAlgorithm> prfFactory, ArraySegment<byte> output)
59-
{
60-
// make copies so we can mutate these local vars
61-
var outputOffset = output.Offset;
62-
var outputCount = output.Count;
63-
64-
var contextSharedLength = contextHeader.Length + contextData.Length;
65-
66-
using (var prf = prfFactory(kdk))
67-
{
68-
// See SP800-108, Sec. 5.1 for the format of the input to the PRF routine.
69-
var prfInput = new byte[checked(sizeof(uint) /* [i]_2 */ + label.Length + 1 /* 0x00 */ + contextSharedLength + sizeof(uint) /* [K]_2 */)];
70-
71-
// Copy [L]_2 to prfInput since it's stable over all iterations
72-
uint outputSizeInBits = (uint)checked((int)outputCount * 8);
73-
prfInput[prfInput.Length - 4] = (byte)(outputSizeInBits >> 24);
74-
prfInput[prfInput.Length - 3] = (byte)(outputSizeInBits >> 16);
75-
prfInput[prfInput.Length - 2] = (byte)(outputSizeInBits >> 8);
76-
prfInput[prfInput.Length - 1] = (byte)(outputSizeInBits);
77-
78-
// Copy label and context to prfInput since they're stable over all iterations
79-
label.CopyTo(prfInput.AsSpan(sizeof(uint)));
80-
contextHeader.CopyTo(prfInput.AsSpan(sizeof(uint) + label.Length + 1));
81-
contextData.CopyTo(prfInput.AsSpan(sizeof(uint) + label.Length + 1 + contextHeader.Length));
82-
83-
var prfOutputSizeInBytes = prf.GetDigestSizeInBytes();
84-
for (uint i = 1; outputCount > 0; i++)
85-
{
86-
// Copy [i]_2 to prfInput since it mutates with each iteration
87-
prfInput[0] = (byte)(i >> 24);
88-
prfInput[1] = (byte)(i >> 16);
89-
prfInput[2] = (byte)(i >> 8);
90-
prfInput[3] = (byte)(i);
91-
92-
// Run the PRF and copy the results to the output buffer
93-
var prfOutput = prf.ComputeHash(prfInput);
94-
CryptoUtil.Assert(prfOutputSizeInBytes == prfOutput.Length, "prfOutputSizeInBytes == prfOutput.Length");
95-
var numBytesToCopyThisIteration = Math.Min(prfOutputSizeInBytes, outputCount);
96-
97-
prfOutput.AsSpan().Slice(0, numBytesToCopyThisIteration).CopyTo(output.Array!.AsSpan().Slice(start: outputOffset));
98-
Array.Clear(prfOutput, 0, prfOutput.Length); // contains key material, so delete it
99-
100-
// adjust offsets
101-
outputOffset += numBytesToCopyThisIteration;
102-
outputCount -= numBytesToCopyThisIteration;
103-
}
104-
}
105-
}
106-
10758
public static void DeriveKeys(
10859
byte[] kdk,
10960
ReadOnlySpan<byte> label,

0 commit comments

Comments
 (0)