aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/stream/chacha
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-08-08 17:08:22 -0400
committerJack Lloyd <[email protected]>2018-08-08 17:08:22 -0400
commit25326f304dc5783940c92996e0e4853c38576ce9 (patch)
treed4b5b10804aa4851b36fbfc0fc27e139d90c47d1 /src/lib/stream/chacha
parenta048766b33e88f3ffe5ca71a65105c9f58d55ecf (diff)
Add StreamCipher::write_keystream
Avoids the XOR operation. Only implemented for ChaCha20 currently, everything else defaults to memset-to-zero + xor-cipher
Diffstat (limited to 'src/lib/stream/chacha')
-rw-r--r--src/lib/stream/chacha/chacha.cpp18
-rw-r--r--src/lib/stream/chacha/chacha.h2
2 files changed, 20 insertions, 0 deletions
diff --git a/src/lib/stream/chacha/chacha.cpp b/src/lib/stream/chacha/chacha.cpp
index d2d31a12e..8edb685da 100644
--- a/src/lib/stream/chacha/chacha.cpp
+++ b/src/lib/stream/chacha/chacha.cpp
@@ -173,6 +173,24 @@ void ChaCha::cipher(const uint8_t in[], uint8_t out[], size_t length)
m_position += length;
}
+void ChaCha::write_keystream(uint8_t out[], size_t length)
+ {
+ verify_key_set(m_state.empty() == false);
+
+ 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);
+ chacha_x4(m_buffer.data(), m_state.data(), m_rounds);
+ m_position = 0;
+ }
+
+ copy_mem(out, &m_buffer[m_position], length);
+
+ m_position += length;
+ }
+
void ChaCha::initialize_state()
{
static const uint32_t TAU[] =
diff --git a/src/lib/stream/chacha/chacha.h b/src/lib/stream/chacha/chacha.h
index e41fd927f..346e25c28 100644
--- a/src/lib/stream/chacha/chacha.h
+++ b/src/lib/stream/chacha/chacha.h
@@ -29,6 +29,8 @@ class BOTAN_PUBLIC_API(2,0) ChaCha final : public StreamCipher
void cipher(const uint8_t in[], uint8_t out[], size_t length) override;
+ void write_keystream(uint8_t out[], size_t len) override;
+
void set_iv(const uint8_t iv[], size_t iv_len) override;
/*