Coverage Report

Created: 2020-05-23 13:54

/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/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
   verify_key_set(m_state.empty() == false);
19
0
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
StreamCipher* RC4::clone() const
33
0
   {
34
0
   return new 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*, 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
0
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
0
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
0
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
/*
77
* RC4 Key Schedule
78
*/
79
void RC4::key_schedule(const uint8_t key[], size_t length)
80
0
   {
81
0
   m_state.resize(256);
82
0
   m_buffer.resize(256);
83
0
84
0
   m_position = m_X = m_Y = 0;
85
0
86
0
   for(size_t i = 0; i != 256; ++i)
87
0
      m_state[i] = static_cast<uint8_t>(i);
88
0
89
0
   for(size_t i = 0, state_index = 0; i != 256; ++i)
90
0
      {
91
0
      state_index = (state_index + key[i % length] + m_state[i]) % 256;
92
0
      std::swap(m_state[i], m_state[state_index]);
93
0
      }
94
0
95
0
   for(size_t i = 0; i <= m_SKIP; i += m_buffer.size())
96
0
      generate();
97
0
98
0
   m_position += (m_SKIP % m_buffer.size());
99
0
   }
100
101
/*
102
* Return the name of this type
103
*/
104
std::string RC4::name() const
105
0
   {
106
0
   if(m_SKIP == 0)
107
0
      return "RC4";
108
0
   else if(m_SKIP == 256)
109
0
      return "MARK-4";
110
0
   else
111
0
      return "RC4(" + std::to_string(m_SKIP) + ")";
112
0
   }
113
114
/*
115
* Clear memory of sensitive data
116
*/
117
void RC4::clear()
118
0
   {
119
0
   zap(m_state);
120
0
   zap(m_buffer);
121
0
   m_position = m_X = m_Y = 0;
122
0
   }
123
124
/*
125
* RC4 Constructor
126
*/
127
0
RC4::RC4(size_t s) : m_SKIP(s) {}
128
129
void RC4::seek(uint64_t)
130
0
   {
131
0
   throw Not_Implemented("RC4 does not support seeking");
132
0
   }
133
}