aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/lib/stream/chacha/chacha.cpp20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/lib/stream/chacha/chacha.cpp b/src/lib/stream/chacha/chacha.cpp
index a1355cbe6..327454d79 100644
--- a/src/lib/stream/chacha/chacha.cpp
+++ b/src/lib/stream/chacha/chacha.cpp
@@ -176,11 +176,14 @@ void ChaCha::cipher(const uint8_t in[], uint8_t out[], size_t length)
while(length >= m_buffer.size() - m_position)
{
- xor_buf(out, in, &m_buffer[m_position], m_buffer.size() - m_position);
- length -= (m_buffer.size() - m_position);
- in += (m_buffer.size() - m_position);
- out += (m_buffer.size() - m_position);
+ const size_t available = m_buffer.size() - m_position;
+
+ xor_buf(out, in, &m_buffer[m_position], available);
chacha_x8(m_buffer.data(), m_state.data(), m_rounds);
+
+ length -= available;
+ in += available;
+ out += available;
m_position = 0;
}
@@ -195,10 +198,13 @@ void ChaCha::write_keystream(uint8_t out[], size_t length)
while(length >= m_buffer.size() - m_position)
{
- copy_mem(out, &m_buffer[m_position], m_buffer.size() - m_position);
- length -= (m_buffer.size() - m_position);
- out += (m_buffer.size() - m_position);
+ const size_t available = m_buffer.size() - m_position;
+
+ copy_mem(out, &m_buffer[m_position], available);
chacha_x8(m_buffer.data(), m_state.data(), m_rounds);
+
+ length -= available;
+ out += available;
m_position = 0;
}