Skip to content

Commit 614b569

Browse files
committed
intro ispandataprotector.unprotect \ fix warnings \ dont change timelimiteddataprotectors
1 parent c1b203f commit 614b569

File tree

8 files changed

+37
-88
lines changed

8 files changed

+37
-88
lines changed

src/DataProtection/Abstractions/src/ISpanDataProtector.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ public interface ISpanDataProtector : IDataProtector
2222
/// <returns>The size of the protected data.</returns>
2323
int GetProtectedSize(ReadOnlySpan<byte> plainText);
2424

25+
/// <summary>
26+
/// Returns the size of the decrypted data for a given ciphertext length.
27+
/// </summary>
28+
/// <param name="cipherTextLength">Length of the cipher text that will be decrypted later.</param>
29+
/// <returns>The length of the decrypted data.</returns>
30+
int GetUnprotectedSize(int cipherTextLength);
31+
2532
/// <summary>
2633
/// Attempts to encrypt and tamper-proof a piece of data.
2734
/// </summary>
@@ -30,4 +37,17 @@ public interface ISpanDataProtector : IDataProtector
3037
/// <param name="bytesWritten">When this method returns, the total number of bytes written into destination</param>
3138
/// <returns>true if destination is long enough to receive the encrypted data; otherwise, false.</returns>
3239
bool TryProtect(ReadOnlySpan<byte> plainText, Span<byte> destination, out int bytesWritten);
40+
41+
/// <summary>
42+
/// Attempts to validate the authentication tag of and decrypt a blob of encrypted data.
43+
/// </summary>
44+
/// <param name="cipherText">The encrypted data to decrypt.</param>
45+
/// <param name="additionalAuthenticatedData">
46+
/// A piece of data which was included in the authentication tag during encryption.
47+
/// This input may be zero bytes in length. The same AAD must be specified in the corresponding encryption call.
48+
/// </param>
49+
/// <param name="destination">The decrypted output.</param>
50+
/// <param name="bytesWritten">When this method returns, the total number of bytes written into destination</param>
51+
/// <returns>true if decryption was successful; otherwise, false.</returns>
52+
bool TryUnprotect(ReadOnlySpan<byte> cipherText, ReadOnlySpan<byte> additionalAuthenticatedData, Span<byte> destination, out int bytesWritten);
3353
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#nullable enable
22
Microsoft.AspNetCore.DataProtection.ISpanDataProtector
33
Microsoft.AspNetCore.DataProtection.ISpanDataProtector.GetProtectedSize(System.ReadOnlySpan<byte> plainText) -> int
4+
Microsoft.AspNetCore.DataProtection.ISpanDataProtector.GetUnprotectedSize(int cipherTextLength) -> int
45
Microsoft.AspNetCore.DataProtection.ISpanDataProtector.TryProtect(System.ReadOnlySpan<byte> plainText, System.Span<byte> destination, out int bytesWritten) -> bool
6+
Microsoft.AspNetCore.DataProtection.ISpanDataProtector.TryUnprotect(System.ReadOnlySpan<byte> cipherText, System.ReadOnlySpan<byte> additionalAuthenticatedData, System.Span<byte> destination, out int bytesWritten) -> bool

src/DataProtection/DataProtection/src/AuthenticatedEncryption/ISpanAuthenticatedEncryptor.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public interface ISpanAuthenticatedEncryptor : IAuthenticatedEncryptor
2424
/// <summary>
2525
/// Returns the size of the decrypted data for a given ciphertext length.
2626
/// </summary>
27-
/// <param name="cipherTextLength">Length of the cipher text that will be decrypted later</param>
28-
/// <returns>The length of the decrypted data</returns>
27+
/// <param name="cipherTextLength">Length of the cipher text that will be decrypted later.</param>
28+
/// <returns>The length of the decrypted data.</returns>
2929
int GetDecryptedSize(int cipherTextLength);
3030

3131
/// <summary>
@@ -42,5 +42,16 @@ public interface ISpanAuthenticatedEncryptor : IAuthenticatedEncryptor
4242
/// <returns>true if destination is long enough to receive the encrypted data; otherwise, false.</returns>
4343
bool TryEncrypt(ReadOnlySpan<byte> plaintext, ReadOnlySpan<byte> additionalAuthenticatedData, Span<byte> destination, out int bytesWritten);
4444

45+
/// <summary>
46+
/// Attempts to validate the authentication tag of and decrypt a blob of encrypted data.
47+
/// </summary>
48+
/// <param name="cipherText">The encrypted data to decrypt.</param>
49+
/// <param name="additionalAuthenticatedData">
50+
/// A piece of data which was included in the authentication tag during encryption.
51+
/// This input may be zero bytes in length. The same AAD must be specified in the corresponding encryption call.
52+
/// </param>
53+
/// <param name="destination">The decrypted output.</param>
54+
/// <param name="bytesWritten">When this method returns, the total number of bytes written into destination</param>
55+
/// <returns>true if decryption was successful; otherwise, false.</returns>
4556
bool TryDecrypt(ReadOnlySpan<byte> cipherText, ReadOnlySpan<byte> additionalAuthenticatedData, Span<byte> destination, out int bytesWritten);
4657
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,6 @@ private uint GetCbcEncryptedOutputSizeWithPadding(uint cbInput)
431431
byte* pbDummyIV = stackalloc byte[checked((int)_symmetricAlgorithmBlockSizeInBytes)];
432432
byte* pbDummyInput = stackalloc byte[checked((int)cbInput)];
433433

434-
435434
var ntstatus = UnsafeNativeMethods.BCryptEncrypt(
436435
hKey: tempKeyHandle,
437436
pbInput: pbDummyInput,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#nullable enable
22
Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ISpanAuthenticatedEncryptor
3+
Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ISpanAuthenticatedEncryptor.GetDecryptedSize(int cipherTextLength) -> int
34
Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ISpanAuthenticatedEncryptor.GetEncryptedSize(int plainTextLength) -> int
5+
Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ISpanAuthenticatedEncryptor.TryDecrypt(System.ReadOnlySpan<byte> cipherText, System.ReadOnlySpan<byte> additionalAuthenticatedData, System.Span<byte> destination, out int bytesWritten) -> bool
46
Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ISpanAuthenticatedEncryptor.TryEncrypt(System.ReadOnlySpan<byte> plaintext, System.ReadOnlySpan<byte> additionalAuthenticatedData, System.Span<byte> destination, out int bytesWritten) -> bool

src/DataProtection/Extensions/src/DataProtectionAdvancedExtensions.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,4 @@ public byte[] Unprotect(byte[] protectedData)
127127
return _innerProtector.Unprotect(protectedData, out Expiration);
128128
}
129129
}
130-
131-
private class TimeLimitedWrappingSpanProtector : TimeLimitedWrappingProtector, ISpanDataProtector
132-
{
133-
public TimeLimitedWrappingSpanProtector(ITimeLimitedDataProtector innerProtector) : base(innerProtector)
134-
{
135-
}
136-
137-
public int GetProtectedSize(ReadOnlySpan<byte> plainText)
138-
{
139-
var inner = (ISpanDataProtector)_innerProtector;
140-
return inner.GetProtectedSize(plainText);
141-
}
142-
143-
public bool TryProtect(ReadOnlySpan<byte> plainText, Span<byte> destination, out int bytesWritten)
144-
{
145-
var inner = (ISpanDataProtector)_innerProtector;
146-
return inner.TryProtect(plainText, destination, out bytesWritten);
147-
}
148-
}
149130
}

src/DataProtection/Extensions/src/TimeLimitedDataProtector.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ public ITimeLimitedDataProtector CreateProtector(string purpose)
3535
ArgumentNullThrowHelper.ThrowIfNull(purpose);
3636

3737
var protector = _innerProtector.CreateProtector(purpose);
38-
if (protector is ISpanDataProtector spanDataProtector)
39-
{
40-
return new TimeLimitedSpanDataProtector(spanDataProtector);
41-
}
42-
4338
return new TimeLimitedDataProtector(protector);
4439
}
4540

src/DataProtection/Extensions/src/TimeLimitedSpanDataProtector.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)