Botan 2.19.1
Crypto and TLS for C&
rc4.cpp
Go to the documentation of this file.
1/*
2* RC4
3* (C) 1999-2007 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/rc4.h>
9#include <botan/exceptn.h>
10
11namespace Botan {
12
13/*
14* Combine cipher stream with message
15*/
16void RC4::cipher(const uint8_t in[], uint8_t out[], size_t length)
17 {
18 verify_key_set(m_state.empty() == false);
19
20 while(length >= m_buffer.size() - m_position)
21 {
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);
26 generate();
27 }
28 xor_buf(out, in, &m_buffer[m_position], length);
29 m_position += length;
30 }
31
33 {
34 return new RC4(m_SKIP);
35 }
36
38 {
39 return Key_Length_Specification(1, 256);
40 }
41
42void RC4::set_iv(const uint8_t*, size_t length)
43 {
44 if(length > 0)
45 throw Invalid_IV_Length("RC4", length);
46 }
47
48/*
49* Generate cipher stream
50*/
51void RC4::generate()
52 {
53 uint8_t SX, SY;
54 for(size_t i = 0; i != m_buffer.size(); i += 4)
55 {
56 SX = m_state[m_X+1]; m_Y = (m_Y + SX) % 256; SY = m_state[m_Y];
57 m_state[m_X+1] = SY; m_state[m_Y] = SX;
58 m_buffer[i] = m_state[(SX + SY) % 256];
59
60 SX = m_state[m_X+2]; m_Y = (m_Y + SX) % 256; SY = m_state[m_Y];
61 m_state[m_X+2] = SY; m_state[m_Y] = SX;
62 m_buffer[i+1] = m_state[(SX + SY) % 256];
63
64 SX = m_state[m_X+3]; m_Y = (m_Y + SX) % 256; SY = m_state[m_Y];
65 m_state[m_X+3] = SY; m_state[m_Y] = SX;
66 m_buffer[i+2] = m_state[(SX + SY) % 256];
67
68 m_X = (m_X + 4) % 256;
69 SX = m_state[m_X]; m_Y = (m_Y + SX) % 256; SY = m_state[m_Y];
70 m_state[m_X] = SY; m_state[m_Y] = SX;
71 m_buffer[i+3] = m_state[(SX + SY) % 256];
72 }
73 m_position = 0;
74 }
75
76/*
77* RC4 Key Schedule
78*/
79void RC4::key_schedule(const uint8_t key[], size_t length)
80 {
81 m_state.resize(256);
82 m_buffer.resize(256);
83
84 m_position = m_X = m_Y = 0;
85
86 for(size_t i = 0; i != 256; ++i)
87 m_state[i] = static_cast<uint8_t>(i);
88
89 for(size_t i = 0, state_index = 0; i != 256; ++i)
90 {
91 state_index = (state_index + key[i % length] + m_state[i]) % 256;
92 std::swap(m_state[i], m_state[state_index]);
93 }
94
95 for(size_t i = 0; i <= m_SKIP; i += m_buffer.size())
96 generate();
97
98 m_position += (m_SKIP % m_buffer.size());
99 }
100
101/*
102* Return the name of this type
103*/
104std::string RC4::name() const
105 {
106 if(m_SKIP == 0)
107 return "RC4";
108 else if(m_SKIP == 256)
109 return "MARK-4";
110 else
111 return "RC4(" + std::to_string(m_SKIP) + ")";
112 }
113
114/*
115* Clear memory of sensitive data
116*/
118 {
119 zap(m_state);
120 zap(m_buffer);
121 m_position = m_X = m_Y = 0;
122 }
123
124/*
125* RC4 Constructor
126*/
127RC4::RC4(size_t s) : m_SKIP(s) {}
128
129void RC4::seek(uint64_t)
130 {
131 throw Not_Implemented("RC4 does not support seeking");
132 }
133}
Key_Length_Specification key_spec() const override
Definition: rc4.cpp:37
StreamCipher * clone() const override
Definition: rc4.cpp:32
std::string name() const override
Definition: rc4.cpp:104
void clear() override
Definition: rc4.cpp:117
RC4(size_t skip=0)
Definition: rc4.cpp:127
void cipher(const uint8_t in[], uint8_t out[], size_t length) override
Definition: rc4.cpp:16
void set_iv(const uint8_t iv[], size_t iv_len) override
Definition: rc4.cpp:42
void seek(uint64_t offset) override
Definition: rc4.cpp:129
void verify_key_set(bool cond) const
Definition: sym_algo.h:171
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:213
Definition: alg_id.cpp:13
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:127
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:262