Botan  2.6.0
Crypto and TLS for C++11
openssl_rc4.cpp
Go to the documentation of this file.
1 /*
2 * OpenSSL RC4
3 * (C) 1999-2007,2015 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/stream_cipher.h>
9 
10 #if defined(BOTAN_HAS_OPENSSL) && defined(BOTAN_HAS_RC4)
11 
12 #include <botan/internal/openssl.h>
13 #include <botan/parsing.h>
14 #include <botan/exceptn.h>
15 #include <openssl/rc4.h>
16 
17 namespace Botan {
18 
19 namespace {
20 
21 class OpenSSL_RC4 final : public StreamCipher
22  {
23  public:
24  void clear() override { clear_mem(&m_rc4, 1); m_key_set = false; }
25 
26  std::string provider() const override { return "openssl"; }
27 
28  std::string name() const override
29  {
30  switch(m_skip)
31  {
32  case 0:
33  return "RC4";
34  case 256:
35  return "MARK-4";
36  default:
37  return "RC4(" + std::to_string(m_skip) + ")";
38  }
39  }
40 
41  StreamCipher* clone() const override { return new OpenSSL_RC4(m_skip); }
42 
43  Key_Length_Specification key_spec() const override
44  {
45  return Key_Length_Specification(1, 32);
46  }
47 
48  explicit OpenSSL_RC4(size_t skip = 0) : m_skip(skip) { clear(); }
49  ~OpenSSL_RC4() { clear(); }
50 
51  void set_iv(const uint8_t*, size_t len) override
52  {
53  if(len > 0)
54  throw Exception("RC4 does not support an IV");
55  }
56 
57  void seek(uint64_t) override
58  {
59  throw Not_Implemented("RC4 does not support seeking");
60  }
61  private:
62  void cipher(const uint8_t in[], uint8_t out[], size_t length) override
63  {
64  verify_key_set(m_key_set);
65  ::RC4(&m_rc4, length, in, out);
66  }
67 
68  void key_schedule(const uint8_t key[], size_t length) override
69  {
70  ::RC4_set_key(&m_rc4, length, key);
71  uint8_t d = 0;
72  for(size_t i = 0; i != m_skip; ++i)
73  ::RC4(&m_rc4, 1, &d, &d);
74  m_key_set = true;
75  }
76 
77  size_t m_skip;
78  RC4_KEY m_rc4;
79  bool m_key_set;
80  };
81 
82 }
83 
84 std::unique_ptr<StreamCipher>
85 make_openssl_rc4(size_t skip)
86  {
87  return std::unique_ptr<StreamCipher>(new OpenSSL_RC4(skip));
88  }
89 
90 
91 }
92 
93 #endif
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:145
Definition: alg_id.cpp:13
bool m_key_set