8 #include <botan/cipher_mode.h>
9 #include <botan/stream_mode.h>
10 #include <botan/scan_name.h>
11 #include <botan/parsing.h>
14 #if defined(BOTAN_HAS_BLOCK_CIPHER)
15 #include <botan/block_cipher.h>
18 #if defined(BOTAN_HAS_AEAD_MODES)
19 #include <botan/aead.h>
22 #if defined(BOTAN_HAS_MODE_CBC)
23 #include <botan/cbc.h>
26 #if defined(BOTAN_HAS_MODE_CFB)
27 #include <botan/cfb.h>
30 #if defined(BOTAN_HAS_MODE_XTS)
31 #include <botan/xts.h>
34 #if defined(BOTAN_HAS_OPENSSL)
35 #include <botan/internal/openssl.h>
38 #if defined(BOTAN_HAS_COMMONCRYPTO)
39 #include <botan/internal/commoncrypto.h>
46 const std::string& provider)
56 const std::string& provider)
58 #if defined(BOTAN_HAS_COMMONCRYPTO)
59 if(provider.empty() || provider ==
"commoncrypto")
63 if(commoncrypto_cipher)
64 return commoncrypto_cipher;
67 return std::unique_ptr<Cipher_Mode>();
71 #if defined(BOTAN_HAS_OPENSSL)
72 if(provider.empty() || provider ==
"openssl")
77 return openssl_cipher;
80 return std::unique_ptr<Cipher_Mode>();
84 #if defined(BOTAN_HAS_STREAM_CIPHER)
87 return std::unique_ptr<Cipher_Mode>(
new Stream_Cipher_Mode(sc.release()));
91 #if defined(BOTAN_HAS_AEAD_MODES)
94 return std::unique_ptr<Cipher_Mode>(aead.release());
98 if(algo.find(
'/') != std::string::npos)
100 const std::vector<std::string> algo_parts =
split_on(algo,
'/');
101 const std::string cipher_name = algo_parts[0];
104 if(mode_info.empty())
105 return std::unique_ptr<Cipher_Mode>();
107 std::ostringstream alg_args;
109 alg_args <<
'(' << cipher_name;
110 for(
size_t i = 1; i < mode_info.size(); ++i)
111 alg_args <<
',' << mode_info[i];
112 for(
size_t i = 2; i < algo_parts.size(); ++i)
113 alg_args <<
',' << algo_parts[i];
116 const std::string mode_name = mode_info[0] + alg_args.str();
120 #if defined(BOTAN_HAS_BLOCK_CIPHER)
126 return std::unique_ptr<Cipher_Mode>();
133 return std::unique_ptr<Cipher_Mode>();
136 #if defined(BOTAN_HAS_MODE_CBC)
139 const std::string padding = spec.
arg(1,
"PKCS7");
144 return std::unique_ptr<Cipher_Mode>(
new CTS_Encryption(bc.release()));
146 return std::unique_ptr<Cipher_Mode>(
new CTS_Decryption(bc.release()));
150 std::unique_ptr<BlockCipherModePaddingMethod> pad(
get_bc_pad(padding));
155 return std::unique_ptr<Cipher_Mode>(
new CBC_Encryption(bc.release(), pad.release()));
157 return std::unique_ptr<Cipher_Mode>(
new CBC_Decryption(bc.release(), pad.release()));
163 #if defined(BOTAN_HAS_MODE_XTS)
167 return std::unique_ptr<Cipher_Mode>(
new XTS_Encryption(bc.release()));
169 return std::unique_ptr<Cipher_Mode>(
new XTS_Decryption(bc.release()));
173 #if defined(BOTAN_HAS_MODE_CFB)
176 const size_t feedback_bits = spec.
arg_as_integer(1, 8*bc->block_size());
178 return std::unique_ptr<Cipher_Mode>(
new CFB_Encryption(bc.release(), feedback_bits));
180 return std::unique_ptr<Cipher_Mode>(
new CFB_Decryption(bc.release(), feedback_bits));
186 return std::unique_ptr<Cipher_Mode>();
192 const std::vector<std::string>& possible = {
"base",
"openssl",
"commoncrypto" };
194 for(
auto&& prov : possible)
199 providers.push_back(prov);
std::string arg(size_t i) const
std::vector< std::string > split_on(const std::string &str, char delim)
Cipher_Mode * make_openssl_cipher_mode(const std::string &name, Cipher_Dir direction)
virtual std::string provider() const
static std::unique_ptr< AEAD_Mode > create(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
size_t arg_as_integer(size_t i, size_t def_value) const
std::vector< std::string > parse_algorithm_name(const std::string &namex)
static std::unique_ptr< BlockCipher > create(const std::string &algo_spec, const std::string &provider="")
static std::unique_ptr< Cipher_Mode > create(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
const std::string & algo_name() const
static std::vector< std::string > providers(const std::string &algo_spec)
static std::unique_ptr< StreamCipher > create(const std::string &algo_spec, const std::string &provider="")
static std::unique_ptr< Cipher_Mode > create_or_throw(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
Cipher_Mode * make_commoncrypto_cipher_mode(const std::string &name, Cipher_Dir direction)
BlockCipherModePaddingMethod * get_bc_pad(const std::string &algo_spec)