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_COMMONCRYPTO)
35 #include <botan/internal/commoncrypto.h>
42 const std::string& provider)
52 const std::string& provider)
54#if defined(BOTAN_HAS_COMMONCRYPTO)
59 if(commoncrypto_cipher)
60 return commoncrypto_cipher;
63 return std::unique_ptr<Cipher_Mode>();
67#if defined(BOTAN_HAS_STREAM_CIPHER)
70 return std::unique_ptr<Cipher_Mode>(
new Stream_Cipher_Mode(sc.release()));
74#if defined(BOTAN_HAS_AEAD_MODES)
77 return std::unique_ptr<Cipher_Mode>(aead.release());
81 if(algo.find(
'/') != std::string::npos)
83 const std::vector<std::string> algo_parts =
split_on(algo,
'/');
84 const std::string cipher_name = algo_parts[0];
88 return std::unique_ptr<Cipher_Mode>();
90 std::ostringstream alg_args;
92 alg_args <<
'(' << cipher_name;
93 for(
size_t i = 1; i < mode_info.size(); ++i)
94 alg_args <<
',' << mode_info[i];
95 for(
size_t i = 2; i < algo_parts.size(); ++i)
96 alg_args <<
',' << algo_parts[i];
99 const std::string mode_name = mode_info[0] + alg_args.str();
103#if defined(BOTAN_HAS_BLOCK_CIPHER)
109 return std::unique_ptr<Cipher_Mode>();
116 return std::unique_ptr<Cipher_Mode>();
119#if defined(BOTAN_HAS_MODE_CBC)
122 const std::string padding = spec.
arg(1,
"PKCS7");
127 return std::unique_ptr<Cipher_Mode>(
new CTS_Encryption(bc.release()));
129 return std::unique_ptr<Cipher_Mode>(
new CTS_Decryption(bc.release()));
133 std::unique_ptr<BlockCipherModePaddingMethod> pad(
get_bc_pad(padding));
138 return std::unique_ptr<Cipher_Mode>(
new CBC_Encryption(bc.release(), pad.release()));
140 return std::unique_ptr<Cipher_Mode>(
new CBC_Decryption(bc.release(), pad.release()));
146#if defined(BOTAN_HAS_MODE_XTS)
150 return std::unique_ptr<Cipher_Mode>(
new XTS_Encryption(bc.release()));
152 return std::unique_ptr<Cipher_Mode>(
new XTS_Decryption(bc.release()));
156#if defined(BOTAN_HAS_MODE_CFB)
159 const size_t feedback_bits = spec.
arg_as_integer(1, 8*bc->block_size());
161 return std::unique_ptr<Cipher_Mode>(
new CFB_Encryption(bc.release(), feedback_bits));
163 return std::unique_ptr<Cipher_Mode>(
new CFB_Decryption(bc.release(), feedback_bits));
169 return std::unique_ptr<Cipher_Mode>();
175 const std::vector<std::string>& possible = {
"base",
"openssl",
"commoncrypto" };
177 for(
auto&& prov : possible)
static std::unique_ptr< AEAD_Mode > create(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
static std::unique_ptr< BlockCipher > create(const std::string &algo_spec, const std::string &provider="")
static std::vector< std::string > providers(const std::string &algo_spec)
virtual std::string provider() const
static std::unique_ptr< Cipher_Mode > create(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
static std::unique_ptr< Cipher_Mode > create_or_throw(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
std::string arg(size_t i) const
const std::string & algo_name() const
size_t arg_as_integer(size_t i, size_t def_value) const
static std::unique_ptr< StreamCipher > create(const std::string &algo_spec, const std::string &provider="")
std::vector< std::string > split_on(const std::string &str, char delim)
BlockCipherModePaddingMethod * get_bc_pad(const std::string &algo_spec)
Cipher_Mode * make_commoncrypto_cipher_mode(const std::string &name, Cipher_Dir direction)
std::vector< std::string > parse_algorithm_name(const std::string &namex)