-
Notifications
You must be signed in to change notification settings - Fork 105
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
Reduce memory usage when AEAD en/decrypting large messages #259
Merged
Conversation
This file contains 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
lubux
reviewed
Dec 12, 2024
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.
Well done, I went over most of the logic.
The only thing I did not get is the ar.peekedBytes
in the first chunk, but I might have missed something.
Since the chunk size is capped at 4MB now, we can safely preallocate it so that we don't have to buffer each chunk.
172524b
to
1fd5ec8
Compare
lubux
approved these changes
Dec 16, 2024
DennisRasey
pushed a commit
to DennisRasey/forgejo
that referenced
this pull request
Jan 8, 2025
…499) This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [github.com/ProtonMail/go-crypto](https://github.com/ProtonMail/go-crypto) | require | patch | `v1.1.3` -> `v1.1.4` | --- ### Release Notes <details> <summary>ProtonMail/go-crypto (github.com/ProtonMail/go-crypto)</summary> ### [`v1.1.4`](https://github.com/ProtonMail/go-crypto/releases/tag/v1.1.4) [Compare Source](ProtonMail/go-crypto@v1.1.3...v1.1.4) #### What's Changed - Emit armor headers in sorted order by [@​andrewgdotcom](https://github.com/andrewgdotcom) in ProtonMail/go-crypto#255 - Reduce memory usage when AEAD en/decrypting large messages by [@​twiss](https://github.com/twiss) in ProtonMail/go-crypto#259 - Update artifact actions to v4 by [@​twiss](https://github.com/twiss) in ProtonMail/go-crypto#260 **Full Changelog**: ProtonMail/go-crypto@v1.1.3...v1.1.4 </details> --- ### Configuration 📅 **Schedule**: Branch creation - "* 0-3 * * *" (UTC), Automerge - "* 0-3 * * *" (UTC). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS45Mi4wIiwidXBkYXRlZEluVmVyIjoiMzkuOTIuMCIsInRhcmdldEJyYW5jaCI6ImZvcmdlam8iLCJsYWJlbHMiOlsiZGVwZW5kZW5jeS11cGdyYWRlIiwidGVzdC9ub3QtbmVlZGVkIl19--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6499 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Renovate Bot <forgejo-renovate-action@forgejo.org> Co-committed-by: Renovate Bot <forgejo-renovate-action@forgejo.org>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The AEAD code buffered the data to be en/decrypted at multiple points. Firstly, when decrypting, the ciphertext was buffered in order to not pre-allocate the chunk size. However, given RFC9580 caps the chunk size at 4MiB, and most (all?) OSes reuse empty pages, the overhead of allocating the the entire chunk size (256KiB by default in our implementation) is not that large. This leads to a large reduction in memory usage when decrypting large messages, at the cost of somewhat increased memory usage when decrypting small messages. For an added reduction, the ciphertext buffer can be reused for the plaintext, and also for the buffered plaintext when it's read across multiple calls to
Read()
.Similarly, we can reduce the memory usage when encrypting large messages (though not by as much) by manually buffering the plaintext to be encrypted carefully to reserve space for the tag. That way, the plaintext buffer can be reused for the ciphertext.
Finally, fix OCB en/decryption when reusing the buffer.
Before (chunk size = 256KiB):
After (chunk size = 256KiB):
After (chunk size = 4MiB):
After (chunk size = message size):
Because of the above results, it may be worth setting the chunk size to the message size when known (e.g. in gopenpgp).