Botan 2.19.5 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
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 StreamCipher* RC4::clone() const
33 {
34 return new RC4(m_SKIP);
35 }
36
37 Key_Length_Specification RC4::key_spec() const
38 {
39 return Key_Length_Specification(1, 256);
40 }
41
42 void 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*/
51 void 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*/
79 void 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*/
117 void RC4::clear()
118 {
119 zap(m_state);
120 zap(m_buffer);
121 m_position = m_X = m_Y = 0;
122 }
123
124/*
125* RC4 Constructor
126*/
127 RC4::RC4(size_t s) : m_SKIP(s) {}
128
129 void RC4::seek(uint64_t)
130 {
131 throw Not_Implemented("RC4 does not support seeking");
132 }
133}
Botan::Invalid_IV_Length
Definition: exceptn.h:162
Botan::Key_Length_Specification
Definition: sym_algo.h:20
Botan::Not_Implemented
Definition: exceptn.h:356
Botan::RC4::key_spec
Key_Length_Specification key_spec() const override
Definition: rc4.cpp:37
Botan::RC4::clone
StreamCipher * clone() const override
Definition: rc4.cpp:32
Botan::RC4::name
std::string name() const override
Definition: rc4.cpp:104
Botan::RC4::clear
void clear() override
Definition: rc4.cpp:117
Botan::RC4::RC4
RC4(size_t skip=0)
Definition: rc4.cpp:127
Botan::RC4::cipher
void cipher(const uint8_t in[], uint8_t out[], size_t length) override
Definition: rc4.cpp:16
Botan::RC4::set_iv
void set_iv(const uint8_t iv[], size_t iv_len) override
Definition: rc4.cpp:42
Botan::RC4::seek
void seek(uint64_t offset) override
Definition: rc4.cpp:129
Botan::StreamCipher
Definition: stream_cipher.h:22
Botan::SymmetricAlgorithm::verify_key_set
void verify_key_set(bool cond) const
Definition: sym_algo.h:171
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:213
Botan
Definition: alg_id.cpp:13
Botan::zap
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:124
Botan::xor_buf
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:262