Botan  2.1.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 : public StreamCipher
22  {
23  public:
24  void clear() override { clear_mem(&m_rc4, 1); }
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_skip(" + 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 Exception("RC4 does not support seeking");
60  }
61  private:
62  void cipher(const uint8_t in[], uint8_t out[], size_t length) override
63  {
64  ::RC4(&m_rc4, length, in, out);
65  }
66 
67  void key_schedule(const uint8_t key[], size_t length) override
68  {
69  ::RC4_set_key(&m_rc4, length, key);
70  uint8_t d = 0;
71  for(size_t i = 0; i != m_skip; ++i)
72  ::RC4(&m_rc4, 1, &d, &d);
73  }
74 
75  size_t m_skip;
76  RC4_KEY m_rc4;
77  };
78 
79 }
80 
81 std::unique_ptr<StreamCipher>
82 make_openssl_rc4(size_t skip)
83  {
84  return std::unique_ptr<StreamCipher>(new OpenSSL_RC4(skip));
85  }
86 
87 
88 }
89 
90 #endif
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:57
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
Definition: alg_id.cpp:13