Botan  2.1.0
Crypto and TLS for C++11
stream_cipher.cpp
Go to the documentation of this file.
1 /*
2 * Stream Ciphers
3 * (C) 2015,2016 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/stream_cipher.h>
9 #include <botan/scan_name.h>
10 
11 #if defined(BOTAN_HAS_CHACHA)
12  #include <botan/chacha.h>
13 #endif
14 
15 #if defined(BOTAN_HAS_SALSA20)
16  #include <botan/salsa20.h>
17 #endif
18 
19 #if defined(BOTAN_HAS_SHAKE_CIPHER)
20  #include <botan/shake_cipher.h>
21 #endif
22 
23 #if defined(BOTAN_HAS_CTR_BE)
24  #include <botan/ctr.h>
25 #endif
26 
27 #if defined(BOTAN_HAS_OFB)
28  #include <botan/ofb.h>
29 #endif
30 
31 #if defined(BOTAN_HAS_RC4)
32  #include <botan/rc4.h>
33 #endif
34 
35 #if defined(BOTAN_HAS_OPENSSL)
36  #include <botan/internal/openssl.h>
37 #endif
38 
39 namespace Botan {
40 
41 std::unique_ptr<StreamCipher> StreamCipher::create(const std::string& algo_spec,
42  const std::string& provider)
43  {
44  const SCAN_Name req(algo_spec);
45 
46 #if defined(BOTAN_HAS_CTR_BE)
47  if(req.algo_name() == "CTR-BE" && req.arg_count() == 1)
48  {
49  if(provider.empty() || provider == "base")
50  {
51  if(auto c = BlockCipher::create(req.arg(0)))
52  return std::unique_ptr<StreamCipher>(new CTR_BE(c.release()));
53  }
54  }
55 #endif
56 
57 #if defined(BOTAN_HAS_CHACHA)
58  if(req.algo_name() == "ChaCha")
59  {
60  if(provider.empty() || provider == "base")
61  return std::unique_ptr<StreamCipher>(new ChaCha(req.arg_as_integer(0, 20)));
62  }
63 #endif
64 
65 #if defined(BOTAN_HAS_SALSA20)
66  if(req.algo_name() == "Salsa20")
67  {
68  if(provider.empty() || provider == "base")
69  return std::unique_ptr<StreamCipher>(new Salsa20);
70  }
71 #endif
72 
73 #if defined(BOTAN_HAS_SHAKE_CIPHER)
74  if(req.algo_name() == "SHAKE-128")
75  {
76  if(provider.empty() || provider == "base")
77  return std::unique_ptr<StreamCipher>(new SHAKE_128_Cipher);
78  }
79 #endif
80 
81 #if defined(BOTAN_HAS_OFB)
82  if(req.algo_name() == "OFB" && req.arg_count() == 1)
83  {
84  if(provider.empty() || provider == "base")
85  {
86  if(auto c = BlockCipher::create(req.arg(0)))
87  return std::unique_ptr<StreamCipher>(new OFB(c.release()));
88  }
89  }
90 #endif
91 
92 #if defined(BOTAN_HAS_RC4)
93 
94  if(req.algo_name() == "RC4" ||
95  req.algo_name() == "ARC4" ||
96  req.algo_name() == "MARK-4")
97  {
98  const size_t skip = (req.algo_name() == "MARK-4") ? 256 : req.arg_as_integer(0, 0);
99 
100 #if defined(BOTAN_HAS_OPENSSL)
101  if(provider.empty() || provider == "openssl")
102  {
103  return std::unique_ptr<StreamCipher>(make_openssl_rc4(skip));
104  }
105 #endif
106 
107  if(provider.empty() || provider == "base")
108  {
109  return std::unique_ptr<StreamCipher>(new RC4(skip));
110  }
111  }
112 
113 #endif
114 
115  BOTAN_UNUSED(req);
116  BOTAN_UNUSED(provider);
117 
118  return nullptr;
119  }
120 
121 //static
122 std::unique_ptr<StreamCipher>
123 StreamCipher::create_or_throw(const std::string& algo,
124  const std::string& provider)
125  {
126  if(auto sc = StreamCipher::create(algo, provider))
127  {
128  return sc;
129  }
130  throw Lookup_Error("Stream cipher", algo, provider);
131  }
132 
133 std::vector<std::string> StreamCipher::providers(const std::string& algo_spec)
134  {
135  return probe_providers_of<StreamCipher>(algo_spec, {"base", "openssl"});
136  }
137 
138 }
std::string arg(size_t i) const
Definition: scan_name.cpp:122
static std::unique_ptr< StreamCipher > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: ofb.h:19
size_t arg_count() const
Definition: scan_name.h:50
#define BOTAN_UNUSED(v)
Definition: assert.h:92
size_t arg_as_integer(size_t i, size_t def_value) const
Definition: scan_name.cpp:137
Definition: rc4.h:19
Definition: alg_id.cpp:13
static std::vector< std::string > providers(const std::string &algo_spec)
static std::unique_ptr< BlockCipher > create(const std::string &algo_spec, const std::string &provider="")
const std::string & algo_name() const
Definition: scan_name.h:45
static std::unique_ptr< StreamCipher > create(const std::string &algo_spec, const std::string &provider="")