-
Notifications
You must be signed in to change notification settings - Fork 10.5k
feat: "Spanify" DataProtector with IBufferWriter<byte> #64262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DeagleGross
wants to merge
72
commits into
main
Choose a base branch
from
dmkorolev/dataprotection-ibufferwriter-spans
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,912
−1,015
Open
Changes from all commits
Commits
Show all changes
72 commits
Select commit
Hold shift + click to select a range
b1a4b40
implement encrypt size calculation
DeagleGross aecf589
implement geteencyrptedsize
DeagleGross 198501e
try encrypt
DeagleGross a4e3ca4
simplify net10 impl
DeagleGross 8d49973
simplify tests and cbc
DeagleGross ae6fb76
cnggcm
DeagleGross 9caa72e
aes
DeagleGross b9b45b2
something
DeagleGross b3e5b42
key ring based part
DeagleGross 4eb96e3
Merge branch 'main' into dmkorolev/dataprotector-spans
DeagleGross afd344c
refactor
DeagleGross fd7f929
finally passed!
DeagleGross afbad47
generate test for plain text
DeagleGross 34d52e7
correct empty plain text scenario
DeagleGross 18eec02
minor improvements
DeagleGross 9e4a634
hide in internal + docs
DeagleGross 8829566
to public api - its a nightmare to implement otherwise
DeagleGross 5f240c2
fix build
DeagleGross 8d275ac
wip
DeagleGross 8f5b762
re-review
DeagleGross efd4f79
implement timelimitedDataProtector
DeagleGross 6142309
introduce `IOptimizedDataProtector`
DeagleGross ae5bb82
introduce optimized IAuthenticatedEncryptor
DeagleGross 0646d1a
fix dataprotector usage
DeagleGross 3d37955
fix build?
DeagleGross bbfce53
move to a separate ISpanAuthenticatedEncryptor
DeagleGross b0fc525
refactor to ISpan interfaces
DeagleGross d4b1fc9
correct span allocation
DeagleGross 146af98
use TryEncrypt from Encrypt()
DeagleGross 630e451
fix
DeagleGross 983b69f
Merge branch 'main' into dmkorolev/dataprotector-spans
DeagleGross 948052c
init
DeagleGross 37a3277
inheritance!
DeagleGross 0e1dfd6
separate impl
DeagleGross 35f7522
distinguish impl
DeagleGross 05fdcc3
other impls
DeagleGross f223b45
tests & api
DeagleGross c029655
init decrypt
DeagleGross fe478d2
cnggcm
DeagleGross 72aecba
init cbc
DeagleGross 29e7622
cbc
DeagleGross 516729c
mockable
DeagleGross 21b7d87
AesGcm
DeagleGross 5a1ea5c
impl managed
DeagleGross f561796
fix slices everywhere + rollback managedauth to a proper impl
DeagleGross c1b203f
ispanauth: decryption ready
DeagleGross 614b569
intro ispandataprotector.unprotect \ fix warnings \ dont change timel…
DeagleGross 9f24867
span data protector unprotect
DeagleGross 27f7609
"final" review
DeagleGross e3726ec
avoid blank lines!
DeagleGross bc44688
push project for microbenchmarks
DeagleGross 57e9f85
Merge branch 'main' into dmkorolev/dataprotector-spans
DeagleGross 4eadb9b
PR review comments + check length upfront
DeagleGross e6d0d0a
fix warnings
DeagleGross 41c2163
fix warnings in benchmarks
DeagleGross bf9537a
fix build?
DeagleGross 77dd694
rollback benchmark changes
DeagleGross 97db08b
init + cbc + mock
DeagleGross edada0c
cng gcm
DeagleGross 1c16e20
aes gcm
DeagleGross cef12f0
encryptors
DeagleGross 96b8d93
change
DeagleGross 400ec6d
fixes / tests / etc
DeagleGross d309725
nit
DeagleGross 05e3dde
reimplement with "ref"
DeagleGross ccb4bc8
diff!
DeagleGross 72418b6
fix AES-GCM encrypt with pre-post buffers
DeagleGross 6b4823b
fix reading spans
DeagleGross d300807
rewrite refPooledArrayBufferWriter
DeagleGross 4d2a8b4
setup numbers using stackalloc
DeagleGross 11a249f
improve CBC authenticated encryptor
DeagleGross 7601537
Revert "improve CBC authenticated encryptor"
DeagleGross File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Buffers; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.AspNetCore.DataProtection; | ||
|
|
||
| /// <summary> | ||
| /// An interface that can provide data protection services. | ||
| /// Is an optimized version of <see cref="IDataProtector"/>. | ||
| /// </summary> | ||
| public interface ISpanDataProtector : IDataProtector | ||
| { | ||
| /// <summary> | ||
| /// Cryptographically protects a piece of plaintext data and writes the result to a buffer writer. | ||
| /// </summary> | ||
| /// <typeparam name="TWriter">The type of buffer writer to write the protected data to.</typeparam> | ||
| /// <param name="plaintext">The plaintext data to protect.</param> | ||
| /// <param name="destination">The buffer writer to which the protected data will be written.</param> | ||
| /// <remarks> | ||
| /// This method provides an optimized, streaming alternative to <see cref="IDataProtector.Protect(byte[])"/>. | ||
| /// Rather than allocating an intermediate buffer, the protected data is written directly to the provided | ||
| /// buffer writer, which can improve performance and reduce memory allocation pressure. | ||
| /// The buffer writer is advanced by the total number of bytes written to it. | ||
| /// </remarks> | ||
| void Protect<TWriter>(ReadOnlySpan<byte> plaintext, ref TWriter destination) | ||
| where TWriter : IBufferWriter<byte> | ||
| #if NET | ||
| , allows ref struct | ||
| #endif | ||
| ; | ||
|
|
||
| /// <summary> | ||
| /// Cryptographically unprotects a piece of protected data and writes the result to a buffer writer. | ||
| /// </summary> | ||
| /// <typeparam name="TWriter">The type of buffer writer to write the unprotected data to.</typeparam> | ||
| /// <param name="protectedData">The protected data to unprotect.</param> | ||
| /// <param name="destination">The buffer writer to which the unprotected plaintext will be written.</param> | ||
| /// <remarks> | ||
| /// This method provides an optimized, streaming alternative to <see cref="IDataProtector.Unprotect(byte[])"/>. | ||
| /// Rather than allocating an intermediate buffer, the unprotected plaintext is written directly to the provided | ||
| /// buffer writer, which can improve performance and reduce memory allocation pressure. | ||
| /// The buffer writer is advanced by the total number of bytes written to it. | ||
| /// </remarks> | ||
| void Unprotect<TWriter>(ReadOnlySpan<byte> protectedData, ref TWriter destination) | ||
| where TWriter : IBufferWriter<byte> | ||
| #if NET | ||
| , allows ref struct | ||
| #endif | ||
| ; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| #nullable enable | ||
| Microsoft.AspNetCore.DataProtection.ISpanDataProtector | ||
| Microsoft.AspNetCore.DataProtection.ISpanDataProtector.Protect<TWriter>(System.ReadOnlySpan<byte> plaintext, ref TWriter destination) -> void | ||
| Microsoft.AspNetCore.DataProtection.ISpanDataProtector.Unprotect<TWriter>(System.ReadOnlySpan<byte> protectedData, ref TWriter destination) -> void |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/DataProtection/DataProtection/src/AuthenticatedEncryption/ISpanAuthenticatedEncryptor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Buffers; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption; | ||
|
|
||
| /// <summary> | ||
| /// Provides an authenticated encryption and decryption routine via a span-based API. | ||
| /// </summary> | ||
| public interface ISpanAuthenticatedEncryptor : IAuthenticatedEncryptor | ||
| { | ||
| /// <summary> | ||
| /// Encrypts and authenticates a piece of plaintext data and writes the result to a buffer writer. | ||
| /// </summary> | ||
| /// <typeparam name="TWriter">The type of buffer writer to write the ciphertext to.</typeparam> | ||
| /// <param name="plaintext">The plaintext to encrypt. This input may be zero bytes in length.</param> | ||
| /// <param name="additionalAuthenticatedData"> | ||
| /// A piece of data which will not be included in the returned ciphertext | ||
| /// but which will still be covered by the authentication tag. This input may be zero bytes in length. | ||
| /// The same AAD must be specified in the corresponding call to <see cref="Decrypt{TWriter}"/>. | ||
| /// </param> | ||
| /// <param name="destination">The buffer writer to which the ciphertext (including authentication tag) will be written.</param> | ||
| /// <remarks> | ||
| /// This method provides an optimized, streaming alternative to <see cref="IAuthenticatedEncryptor.Encrypt(System.ArraySegment{byte}, System.ArraySegment{byte})"/>. | ||
| /// Rather than allocating an intermediate buffer, the ciphertext is written directly to the provided buffer writer, | ||
| /// which can improve performance and reduce memory allocation pressure. | ||
| /// The buffer writer is advanced by the total number of bytes written to it. | ||
| /// </remarks> | ||
| void Encrypt<TWriter>(ReadOnlySpan<byte> plaintext, ReadOnlySpan<byte> additionalAuthenticatedData, ref TWriter destination) | ||
| where TWriter : IBufferWriter<byte> | ||
| #if NET | ||
| , allows ref struct | ||
| #endif | ||
| ; | ||
|
|
||
| /// <summary> | ||
| /// Validates the authentication tag of and decrypts a blob of encrypted data, writing the result to a buffer writer. | ||
| /// </summary> | ||
| /// <typeparam name="TWriter">The type of buffer writer to write the plaintext to.</typeparam> | ||
| /// <param name="ciphertext">The ciphertext (including authentication tag) to decrypt.</param> | ||
| /// <param name="additionalAuthenticatedData"> | ||
| /// Any ancillary data which was used during computation of the authentication tag. | ||
| /// The same AAD must have been specified in the corresponding call to <see cref="Encrypt{TWriter}"/>. | ||
| /// </param> | ||
| /// <param name="destination">The buffer writer to which the decrypted plaintext will be written.</param> | ||
| /// <remarks> | ||
| /// This method provides an optimized, streaming alternative to <see cref="IAuthenticatedEncryptor.Decrypt(System.ArraySegment{byte}, System.ArraySegment{byte})"/>. | ||
| /// Rather than allocating an intermediate buffer, the plaintext is written directly to the provided buffer writer, | ||
| /// which can improve performance and reduce memory allocation pressure. | ||
| /// The buffer writer is advanced by the total number of bytes written to it. | ||
| /// </remarks> | ||
| void Decrypt<TWriter>(ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> additionalAuthenticatedData, ref TWriter destination) | ||
| where TWriter : IBufferWriter<byte> | ||
| #if NET | ||
| , allows ref struct | ||
| #endif | ||
| ; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to drop the netstandard support for this high-performance (perhaps unwieldy) API limit
ISpanDataProtectorandISpanAuthenticatedEncryptorto .NET 11 and above?