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