Skip to content

Commit

Permalink
Remove use of buffer_insert from filters, MACs, stream ciphers
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Oct 13, 2023
1 parent 2423e91 commit e7419bc
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/lib/filters/b64_filt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ void Base64_Encoder::do_output(const uint8_t input[], size_t length) {
* Convert some data into Base64
*/
void Base64_Encoder::write(const uint8_t input[], size_t length) {
buffer_insert(m_in, m_position, input, length);
const size_t initial_fill = std::min(m_in.size() - m_position, length);
copy_mem(&m_in[m_position], input, initial_fill);

if(m_position + length >= m_in.size()) {
encode_and_send(m_in.data(), m_in.size());
input += (m_in.size() - m_position);
Expand Down
4 changes: 3 additions & 1 deletion src/lib/filters/hex_filt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ void Hex_Encoder::encode_and_send(const uint8_t block[], size_t length) {
* Convert some data into hex format
*/
void Hex_Encoder::write(const uint8_t input[], size_t length) {
buffer_insert(m_in, m_position, input, length);
const size_t initial_fill = std::min(m_in.size() - m_position, length);
copy_mem(&m_in[m_position], input, initial_fill);

if(m_position + length >= m_in.size()) {
encode_and_send(m_in.data(), m_in.size());
input += (m_in.size() - m_position);
Expand Down
4 changes: 3 additions & 1 deletion src/lib/mac/cmac/cmac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ namespace Botan {
void CMAC::add_data(std::span<const uint8_t> input) {
const size_t bs = output_length();

buffer_insert(m_buffer, m_position, input.data(), input.size());
const size_t initial_fill = std::min(m_buffer.size() - m_position, input.size());
copy_mem(m_buffer.data() + m_position, input.data(), initial_fill);

if(m_position + input.size() > bs) {
xor_buf(m_state, m_buffer, bs);
m_cipher->encrypt(m_state);
Expand Down
6 changes: 4 additions & 2 deletions src/lib/mac/poly1305/poly1305.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ void Poly1305::add_data(std::span<const uint8_t> input) {
assert_key_material_set();

if(m_buf_pos) {
buffer_insert(m_buf, m_buf_pos, input.data(), input.size());
const size_t initial_fill = std::min(m_buf.size() - m_buf_pos, input.size());
copy_mem(m_buf.data() + m_buf_pos, input.data(), initial_fill);

if(m_buf_pos + input.size() >= m_buf.size()) {
poly1305_blocks(m_poly, m_buf.data(), 1);
Expand All @@ -201,7 +202,8 @@ void Poly1305::add_data(std::span<const uint8_t> input) {
}

const auto remaining = in.take(in.remaining());
buffer_insert(m_buf, m_buf_pos, remaining.data(), remaining.size());
BOTAN_ASSERT_NOMSG(m_buf_pos + remaining.size() < m_buf.size());
copy_mem(m_buf.data() + m_buf_pos, remaining.data(), remaining.size());
m_buf_pos += remaining.size();
}

Expand Down
7 changes: 4 additions & 3 deletions src/lib/stream/ctr/ctr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void CTR_BE::set_iv_bytes(const uint8_t iv[], size_t iv_len) {

m_iv.resize(m_block_size);
zeroise(m_iv);
buffer_insert(m_iv, 0, iv, iv_len);
copy_mem(&m_iv[0], iv, iv_len);

seek(0);
}
Expand Down Expand Up @@ -211,7 +211,8 @@ void CTR_BE::seek(uint64_t offset) {
const uint64_t base_counter = m_ctr_blocks * (offset / m_counter.size());

zeroise(m_counter);
buffer_insert(m_counter, 0, m_iv);
BOTAN_ASSERT_NOMSG(m_counter.size() >= m_iv.size());
copy_mem(&m_counter[0], &m_iv[0], m_iv.size());

const size_t BS = m_block_size;

Expand Down Expand Up @@ -239,7 +240,7 @@ void CTR_BE::seek(uint64_t offset) {
} else {
// do everything sequentially:
for(size_t i = 1; i != m_ctr_blocks; ++i) {
buffer_insert(m_counter, i * BS, &m_counter[(i - 1) * BS], BS);
copy_mem(&m_counter[i * BS], &m_counter[(i - 1) * BS], BS);

for(size_t j = 0; j != m_ctr_size; ++j) {
if(++m_counter[i * BS + (BS - 1 - j)]) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/stream/ofb/ofb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ void OFB::set_iv_bytes(const uint8_t iv[], size_t iv_len) {
}

zeroise(m_buffer);
buffer_insert(m_buffer, 0, iv, iv_len);
BOTAN_ASSERT_NOMSG(m_buffer.size() >= iv_len);
copy_mem(&m_buffer[0], iv, iv_len);

m_cipher->encrypt(m_buffer);
m_buf_pos = 0;
Expand Down

0 comments on commit e7419bc

Please sign in to comment.