Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
11 namespace Botan {
12
13 /*
14 * Combine cipher stream with message
15 */
16 void RC4::cipher(const uint8_t in[], uint8_t out[], size_t length)
17 {
18 while(length >= m_buffer.size() - m_position)
19 {
20 xor_buf(out, in, &m_buffer[m_position], m_buffer.size() - m_position);
21 length -= (m_buffer.size() - m_position);
22 in += (m_buffer.size() - m_position);
23 out += (m_buffer.size() - m_position);
24 generate();
25 }
26 xor_buf(out, in, &m_buffer[m_position], length);
27 m_position += length;
28 }
29
30 void RC4::set_iv(const uint8_t*, size_t length)
31 {
32 if(length > 0)
33 throw Exception("RC4 does not support an IV");
34 }
35
36 /*
37 * Generate cipher stream
38 */
39 void RC4::generate()
40 {
41 uint8_t SX, SY;
42 for(size_t i = 0; i != m_buffer.size(); i += 4)
43 {
44 SX = m_state[m_X+1]; m_Y = (m_Y + SX) % 256; SY = m_state[m_Y];
45 m_state[m_X+1] = SY; m_state[m_Y] = SX;
46 m_buffer[i] = m_state[(SX + SY) % 256];
47
48 SX = m_state[m_X+2]; m_Y = (m_Y + SX) % 256; SY = m_state[m_Y];
49 m_state[m_X+2] = SY; m_state[m_Y] = SX;
50 m_buffer[i+1] = m_state[(SX + SY) % 256];
51
52 SX = m_state[m_X+3]; m_Y = (m_Y + SX) % 256; SY = m_state[m_Y];
53 m_state[m_X+3] = SY; m_state[m_Y] = SX;
54 m_buffer[i+2] = m_state[(SX + SY) % 256];
55
56 m_X = (m_X + 4) % 256;
57 SX = m_state[m_X]; m_Y = (m_Y + SX) % 256; SY = m_state[m_Y];
58 m_state[m_X] = SY; m_state[m_Y] = SX;
59 m_buffer[i+3] = m_state[(SX + SY) % 256];
60 }
61 m_position = 0;
62 }
63
64 /*
65 * RC4 Key Schedule
66 */
67 void RC4::key_schedule(const uint8_t key[], size_t length)
68 {
69 m_state.resize(256);
70 m_buffer.resize(256);
71
72 m_position = m_X = m_Y = 0;
73
74 for(size_t i = 0; i != 256; ++i)
75 m_state[i] = static_cast<uint8_t>(i);
76
77 for(size_t i = 0, state_index = 0; i != 256; ++i)
78 {
79 state_index = (state_index + key[i % length] + m_state[i]) % 256;
80 std::swap(m_state[i], m_state[state_index]);
81 }
82
83 for(size_t i = 0; i <= m_SKIP; i += m_buffer.size())
84 generate();
85
86 m_position += (m_SKIP % m_buffer.size());
87 }
88
89 /*
90 * Return the name of this type
91 */
92 std::string RC4::name() const
93 {
94 if(m_SKIP == 0) return "RC4";
95 if(m_SKIP == 256) return "MARK-4";
96 else return "RC4_skip(" + std::to_string(m_SKIP) + ")";
97 }
98
99 /*
100 * Clear memory of sensitive data
101 */
102 void RC4::clear()
103 {
104 zap(m_state);
105 zap(m_buffer);
106 m_position = m_X = m_Y = 0;
107 }
108
109 /*
110 * RC4 Constructor
111 */
112 RC4::RC4(size_t s) : m_SKIP(s) {}
113
114 void RC4::seek(uint64_t)
115 {
116 throw Exception("RC4 does not support seeking");
117 }
118 }
Botan::xor_buf
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:115
Botan::RC4::clear
void clear() override
Definition: rc4.cpp:102
Botan::Exception
Definition: exceptn.h:21
Botan::zap
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:221
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
Botan::RC4::seek
void seek(uint64_t offset) override
Definition: rc4.cpp:114
Botan::RC4::cipher
void cipher(const uint8_t in[], uint8_t out[], size_t length) override
Definition: rc4.cpp:16
Botan
Definition: alg_id.cpp:13
Botan::RC4::name
std::string name() const override
Definition: rc4.cpp:92
Botan::RC4::set_iv
void set_iv(const uint8_t iv[], size_t iv_len) override
Definition: rc4.cpp:30
Botan::RC4::RC4
RC4(size_t skip=0)
Definition: rc4.cpp:112