modes: starting to replace raw pointer arithmetic with std::span view.#5265
Open
modes: starting to replace raw pointer arithmetic with std::span view.#5265
Conversation
reneme
reviewed
Feb 3, 2026
Collaborator
reneme
left a comment
There was a problem hiding this comment.
Some general suggestions. Note that this clashes with #4880 which has been open for a while. Perhaps let's revisit this once 3.11.0 is out the door, @randombit?
Comment on lines
181
to
192
| const secure_vector<uint8_t>& ad = ad_buf(); | ||
| BOTAN_ARG_CHECK(ad.size() % CCM_BS == 0, "AD is block size multiple"); | ||
|
|
||
| const BlockCipher& E = cipher(); | ||
|
|
||
| secure_vector<uint8_t> T(CCM_BS); | ||
| E.encrypt(format_b0(sz), T); | ||
|
|
||
| for(size_t i = 0; i != ad.size(); i += CCM_BS) { | ||
| xor_buf(T.data(), &ad[i], CCM_BS); | ||
| xor_buf(std::span{T}.first(CCM_BS), std::span(&ad[i], CCM_BS)); | ||
| E.encrypt(T); | ||
| } |
Collaborator
There was a problem hiding this comment.
For the record: This could be simplified even further, along those lines:
Suggested change
| } | |
| BufferSlicer ad(ad_buf()); | |
| BOTAN_ARG_CHECK(ad.remaining() % CCM_BS == 0, "AD is block size multiple"); | |
| const BlockCipher& E = cipher(); | |
| secure_vector<uint8_t> T(CCM_BS); | |
| E.encrypt(format_b0(sz), T); | |
| while(!ad.empty()) { | |
| xor_buf(T, ad.take<CCM_BS>()); | |
| E.encrypt(T); | |
| } | |
| BOTAN_DEBUG_ASSERT(ad.empty()); |
... no need for the .first for T because T is allocated with exactly CCM_BS bytes anyway. Also, you might have to include stl_util.h for the BufferSlicer.
Comment on lines
+113
to
115
| copy_mem(std::span{out}.first(k1.length()), k1.bits_of()); | ||
| xor_buf(std::span{out}.first(k2.length()), k2.bits_of()); | ||
| return OctetString(out); |
Collaborator
There was a problem hiding this comment.
While you're on it:
Suggested change
| copy_mem(std::span{out}.first(k1.length()), k1.bits_of()); | |
| xor_buf(std::span{out}.first(k2.length()), k2.bits_of()); | |
| return OctetString(out); | |
| copy_mem(std::span{out}.first(k1.length()), k1.bits_of()); | |
| xor_buf(std::span{out}.first(k2.length()), k2.bits_of()); | |
| return OctetString(std::move(out)); |
Comment on lines
234
to
245
| const secure_vector<uint8_t>& ad = ad_buf(); | ||
| BOTAN_ARG_CHECK(ad.size() % CCM_BS == 0, "AD is block size multiple"); | ||
|
|
||
| const BlockCipher& E = cipher(); | ||
|
|
||
| secure_vector<uint8_t> T(CCM_BS); | ||
| E.encrypt(format_b0(sz - tag_size()), T); | ||
|
|
||
| for(size_t i = 0; i != ad.size(); i += CCM_BS) { | ||
| xor_buf(T.data(), &ad[i], CCM_BS); | ||
| xor_buf(std::span{T}.first(CCM_BS), std::span(&ad[i], CCM_BS)); | ||
| E.encrypt(T); | ||
| } |
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
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.
No description provided.