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