Coverage Report

Created: 2023-02-22 06:14

/src/botan/src/lib/stream/rc4/rc4.cpp
Line
Count
Source (jump to first uncovered line)
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/internal/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
0
   {
18
0
   assert_key_material_set();
19
20
0
   while(length >= m_buffer.size() - m_position)
21
0
      {
22
0
      xor_buf(out, in, &m_buffer[m_position], m_buffer.size() - m_position);
23
0
      length -= (m_buffer.size() - m_position);
24
0
      in += (m_buffer.size() - m_position);
25
0
      out += (m_buffer.size() - m_position);
26
0
      generate();
27
0
      }
28
0
   xor_buf(out, in, &m_buffer[m_position], length);
29
0
   m_position += length;
30
0
   }
31
32
std::unique_ptr<StreamCipher> RC4::new_object() const
33
0
   {
34
0
   return std::make_unique<RC4>(m_SKIP);
35
0
   }
36
37
Key_Length_Specification RC4::key_spec() const
38
0
   {
39
0
   return Key_Length_Specification(1, 256);
40
0
   }
41
42
void RC4::set_iv(const uint8_t* /*iv*/, size_t length)
43
0
   {
44
0
   if(length > 0)
45
0
      throw Invalid_IV_Length("RC4", length);
46
0
   }
47
48
/*
49
* Generate cipher stream
50
*/
51
void RC4::generate()
52
0
   {
53
0
   uint8_t SX, SY;
54
0
   for(size_t i = 0; i != m_buffer.size(); i += 4)
55
0
      {
56
0
      SX = m_state[m_X+1]; m_Y = (m_Y + SX) % 256; SY = m_state[m_Y];
57
0
      m_state[m_X+1] = SY; m_state[m_Y] = SX;
58
0
      m_buffer[i] = m_state[(SX + SY) % 256];
59
60
0
      SX = m_state[m_X+2]; m_Y = (m_Y + SX) % 256; SY = m_state[m_Y];
61
0
      m_state[m_X+2] = SY; m_state[m_Y] = SX;
62
0
      m_buffer[i+1] = m_state[(SX + SY) % 256];
63
64
0
      SX = m_state[m_X+3]; m_Y = (m_Y + SX) % 256; SY = m_state[m_Y];
65
0
      m_state[m_X+3] = SY; m_state[m_Y] = SX;
66
0
      m_buffer[i+2] = m_state[(SX + SY) % 256];
67
68
0
      m_X = (m_X + 4) % 256;
69
0
      SX = m_state[m_X]; m_Y = (m_Y + SX) % 256; SY = m_state[m_Y];
70
0
      m_state[m_X] = SY; m_state[m_Y] = SX;
71
0
      m_buffer[i+3] = m_state[(SX + SY) % 256];
72
0
      }
73
0
   m_position = 0;
74
0
   }
75
76
bool RC4::has_keying_material() const
77
0
   {
78
0
   return !m_state.empty();
79
0
   }
80
81
/*
82
* RC4 Key Schedule
83
*/
84
void RC4::key_schedule(const uint8_t key[], size_t length)
85
0
   {
86
0
   m_state.resize(256);
87
0
   m_buffer.resize(256);
88
89
0
   m_position = m_X = m_Y = 0;
90
91
0
   for(size_t i = 0; i != 256; ++i)
92
0
      m_state[i] = static_cast<uint8_t>(i);
93
94
0
   for(size_t i = 0, state_index = 0; i != 256; ++i)
95
0
      {
96
0
      state_index = (state_index + key[i % length] + m_state[i]) % 256;
97
0
      std::swap(m_state[i], m_state[state_index]);
98
0
      }
99
100
0
   for(size_t i = 0; i <= m_SKIP; i += m_buffer.size())
101
0
      generate();
102
103
0
   m_position += (m_SKIP % m_buffer.size());
104
0
   }
105
106
/*
107
* Return the name of this type
108
*/
109
std::string RC4::name() const
110
0
   {
111
0
   if(m_SKIP == 0)
112
0
      return "RC4";
113
0
   else if(m_SKIP == 256)
114
0
      return "MARK-4";
115
0
   else
116
0
      return "RC4(" + std::to_string(m_SKIP) + ")";
117
0
   }
118
119
/*
120
* Clear memory of sensitive data
121
*/
122
void RC4::clear()
123
0
   {
124
0
   zap(m_state);
125
0
   zap(m_buffer);
126
0
   m_position = m_X = m_Y = 0;
127
0
   }
128
129
/*
130
* RC4 Constructor
131
*/
132
0
RC4::RC4(size_t s) : m_SKIP(s) {}
133
134
void RC4::seek(uint64_t /*offset*/)
135
0
   {
136
0
   throw Not_Implemented("RC4 does not support seeking");
137
0
   }
138
}