Botan  1.10.17
ossl_arc4.cpp
Go to the documentation of this file.
1 /*
2 * OpenSSL ARC4
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/internal/openssl_engine.h>
9 #include <botan/parsing.h>
10 #include <openssl/opensslconf.h>
11 #if !defined(OPENSSL_NO_RC4)
12 #include <openssl/rc4.h>
13 #endif
14 
15 namespace Botan {
16 
17 #if !defined(OPENSSL_NO_RC4)
18 namespace {
19 
20 /**
21 * ARC4 as implemented by OpenSSL
22 */
23 class ARC4_OpenSSL : public StreamCipher
24  {
25  public:
26  void clear() { std::memset(&state, 0, sizeof(state)); }
27  std::string name() const;
28  StreamCipher* clone() const { return new ARC4_OpenSSL(SKIP); }
29 
30  Key_Length_Specification key_spec() const
31  {
32  return Key_Length_Specification(1, 32);
33  }
34 
35 
36  ARC4_OpenSSL(size_t s = 0) : SKIP(s) { clear(); }
37  ~ARC4_OpenSSL() { clear(); }
38  private:
39  void cipher(const byte[], byte[], size_t);
40  void key_schedule(const byte[], size_t);
41 
42  const size_t SKIP;
43  RC4_KEY state;
44  };
45 
46 /*
47 * Return the name of this type
48 */
49 std::string ARC4_OpenSSL::name() const
50  {
51  if(SKIP == 0) return "ARC4";
52  if(SKIP == 256) return "MARK-4";
53  else return "RC4_skip(" + to_string(SKIP) + ")";
54  }
55 
56 /*
57 * ARC4 Key Schedule
58 */
59 void ARC4_OpenSSL::key_schedule(const byte key[], size_t length)
60  {
61  RC4_set_key(&state, length, key);
62  byte dummy = 0;
63  for(size_t i = 0; i != SKIP; ++i)
64  RC4(&state, 1, &dummy, &dummy);
65  }
66 
67 /*
68 * ARC4 Encryption
69 */
70 void ARC4_OpenSSL::cipher(const byte in[], byte out[], size_t length)
71  {
72  RC4(&state, length, in, out);
73  }
74 
75 }
76 #endif
77 
78 /**
79 * Look for an OpenSSL-supported stream cipher (ARC4)
80 */
81 StreamCipher*
83  Algorithm_Factory&) const
84  {
85 #if !defined(OPENSSL_NO_RC4)
86  if(request.algo_name() == "ARC4")
87  return new ARC4_OpenSSL(request.arg_as_integer(0, 0));
88  if(request.algo_name() == "RC4_drop")
89  return new ARC4_OpenSSL(768);
90 #endif
91 
92  return 0;
93  }
94 
95 }
size_t arg_as_integer(size_t i, size_t def_value) const
Definition: scan_name.cpp:167
StreamCipher * find_stream_cipher(const SCAN_Name &, Algorithm_Factory &) const
Definition: ossl_arc4.cpp:82
unsigned char byte
Definition: types.h:22
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
std::string algo_name() const
Definition: scan_name.h:37