8#include <botan/internal/rc4.h>
10#include <botan/exceptn.h>
11#include <botan/mem_ops.h>
18void RC4::cipher_bytes(
const uint8_t in[], uint8_t out[],
size_t length) {
21 while(length >= m_buffer.size() - m_position) {
22 xor_buf(out, in, &m_buffer[m_position], m_buffer.size() - m_position);
23 length -= (m_buffer.size() - m_position);
24 in += (m_buffer.size() - m_position);
25 out += (m_buffer.size() - m_position);
28 xor_buf(out, in, &m_buffer[m_position], length);
33 return std::make_unique<RC4>(m_SKIP);
44void RC4::set_iv_bytes(
const uint8_t* ,
size_t length) {
55 for(
size_t i = 0; i != m_buffer.size(); i += 4) {
56 SX = m_state[m_X + 1];
57 m_Y = (m_Y + SX) % 256;
59 m_state[m_X + 1] = SY;
61 m_buffer[i] = m_state[(SX + SY) % 256];
63 SX = m_state[m_X + 2];
64 m_Y = (m_Y + SX) % 256;
66 m_state[m_X + 2] = SY;
68 m_buffer[i + 1] = m_state[(SX + SY) % 256];
70 SX = m_state[m_X + 3];
71 m_Y = (m_Y + SX) % 256;
73 m_state[m_X + 3] = SY;
75 m_buffer[i + 2] = m_state[(SX + SY) % 256];
77 m_X = (m_X + 4) % 256;
79 m_Y = (m_Y + SX) % 256;
83 m_buffer[i + 3] = m_state[(SX + SY) % 256];
89 return !m_state.empty();
95void RC4::key_schedule(std::span<const uint8_t> key) {
99 m_position = m_X = m_Y = 0;
101 for(
size_t i = 0; i != 256; ++i) {
102 m_state[i] =
static_cast<uint8_t
>(i);
105 for(
size_t i = 0, state_index = 0; i != 256; ++i) {
106 state_index = (state_index + key[i % key.size()] + m_state[i]) % 256;
107 std::swap(m_state[i], m_state[state_index]);
110 for(
size_t i = 0; i <= m_SKIP; i += m_buffer.size()) {
114 m_position += (m_SKIP % m_buffer.size());
123 }
else if(m_SKIP == 256) {
126 return "RC4(" + std::to_string(m_SKIP) +
")";
136 m_position = m_X = m_Y = 0;
Key_Length_Specification key_spec() const override
std::string name() const override
std::unique_ptr< StreamCipher > new_object() const override
size_t buffer_size() const override
void seek(uint64_t offset) override
bool has_keying_material() const override
void assert_key_material_set() const
void zap(std::vector< T, Alloc > &vec)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)