Botan 3.11.0
Crypto and TLS for C&
cipher_mode.cpp
Go to the documentation of this file.
1/*
2* Cipher Modes
3* (C) 2015 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/cipher_mode.h>
9
10#include <botan/exceptn.h>
11#include <botan/internal/parsing.h>
12#include <botan/internal/scan_name.h>
13#include <botan/internal/stream_mode.h>
14#include <memory>
15#include <sstream>
16#include <utility>
17
18#if defined(BOTAN_HAS_BLOCK_CIPHER)
19 #include <botan/block_cipher.h>
20#endif
21
22#if defined(BOTAN_HAS_AEAD_MODES)
23 #include <botan/aead.h>
24#endif
25
26#if defined(BOTAN_HAS_MODE_CBC)
27 #include <botan/internal/cbc.h>
28#endif
29
30#if defined(BOTAN_HAS_MODE_CFB)
31 #include <botan/internal/cfb.h>
32#endif
33
34#if defined(BOTAN_HAS_MODE_XTS)
35 #include <botan/internal/xts.h>
36#endif
37
38#if defined(BOTAN_HAS_COMMONCRYPTO)
39 #include <botan/internal/commoncrypto.h>
40#endif
41
42namespace Botan {
43
44std::unique_ptr<Cipher_Mode> Cipher_Mode::create_or_throw(std::string_view algo,
45 Cipher_Dir direction,
46 std::string_view provider) {
47 if(auto mode = Cipher_Mode::create(algo, direction, provider)) {
48 return mode;
49 }
50
51 throw Lookup_Error("Cipher mode", algo, provider);
52}
53
54std::unique_ptr<Cipher_Mode> Cipher_Mode::create(std::string_view algo,
55 Cipher_Dir direction,
56 std::string_view provider) {
57#if defined(BOTAN_HAS_COMMONCRYPTO)
58 if(provider.empty() || provider == "commoncrypto") {
59 if(auto cm = make_commoncrypto_cipher_mode(algo, direction))
60 return cm;
61
62 if(!provider.empty())
63 return nullptr;
64 }
65#endif
66
67 if(provider != "base" && !provider.empty()) {
68 return nullptr;
69 }
70
71#if defined(BOTAN_HAS_STREAM_CIPHER)
72 if(auto sc = StreamCipher::create(algo)) {
73 return std::make_unique<Stream_Cipher_Mode>(std::move(sc));
74 }
75#endif
76
77#if defined(BOTAN_HAS_AEAD_MODES)
78 if(auto aead = AEAD_Mode::create(algo, direction)) {
79 return aead;
80 }
81#endif
82
83 if(algo.find('/') != std::string::npos) {
84 const std::vector<std::string> algo_parts = split_on(algo, '/');
85 const std::string_view cipher_name = algo_parts[0];
86 const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
87
88 if(mode_info.empty()) {
89 return std::unique_ptr<Cipher_Mode>();
90 }
91
92 std::ostringstream mode_name;
93
94 mode_name << mode_info[0] << '(' << cipher_name;
95 for(size_t i = 1; i < mode_info.size(); ++i) {
96 mode_name << ',' << mode_info[i];
97 }
98 for(size_t i = 2; i < algo_parts.size(); ++i) {
99 mode_name << ',' << algo_parts[i];
100 }
101 mode_name << ')';
102
103 return Cipher_Mode::create(mode_name.str(), direction, provider);
104 }
105
106#if defined(BOTAN_HAS_BLOCK_CIPHER)
107
108 const SCAN_Name spec(algo);
109
110 if(spec.arg_count() == 0) {
111 return std::unique_ptr<Cipher_Mode>();
112 }
113
114 auto bc = BlockCipher::create(spec.arg(0), provider);
115
116 if(!bc) {
117 return std::unique_ptr<Cipher_Mode>();
118 }
119
120 #if defined(BOTAN_HAS_MODE_CBC)
121 if(spec.algo_name() == "CBC") {
122 const std::string padding = spec.arg(1, "PKCS7");
123
124 if(padding == "CTS") {
125 if(direction == Cipher_Dir::Encryption) {
126 return std::make_unique<CTS_Encryption>(std::move(bc));
127 } else {
128 return std::make_unique<CTS_Decryption>(std::move(bc));
129 }
130 } else {
131 auto pad = BlockCipherModePaddingMethod::create(padding);
132
133 if(pad) {
134 if(direction == Cipher_Dir::Encryption) {
135 return std::make_unique<CBC_Encryption>(std::move(bc), std::move(pad));
136 } else {
137 return std::make_unique<CBC_Decryption>(std::move(bc), std::move(pad));
138 }
139 }
140 }
141 }
142 #endif
143
144 #if defined(BOTAN_HAS_MODE_XTS)
145 if(spec.algo_name() == "XTS") {
146 if(direction == Cipher_Dir::Encryption) {
147 return std::make_unique<XTS_Encryption>(std::move(bc));
148 } else {
149 return std::make_unique<XTS_Decryption>(std::move(bc));
150 }
151 }
152 #endif
153
154 #if defined(BOTAN_HAS_MODE_CFB)
155 if(spec.algo_name() == "CFB") {
156 const size_t feedback_bits = spec.arg_as_integer(1, 8 * bc->block_size());
157 if(direction == Cipher_Dir::Encryption) {
158 return std::make_unique<CFB_Encryption>(std::move(bc), feedback_bits);
159 } else {
160 return std::make_unique<CFB_Decryption>(std::move(bc), feedback_bits);
161 }
162 }
163 #endif
164
165#endif
166
167 return std::unique_ptr<Cipher_Mode>();
168}
169
170//static
171std::vector<std::string> Cipher_Mode::providers(std::string_view algo_spec) {
172 const std::vector<std::string>& possible = {"base", "commoncrypto"};
173 std::vector<std::string> providers;
174 for(auto&& prov : possible) {
175 auto mode = Cipher_Mode::create(algo_spec, Cipher_Dir::Encryption, prov);
176 if(mode) {
177 providers.push_back(prov); // available
178 }
179 }
180 return providers;
181}
182
183} // namespace Botan
static std::unique_ptr< AEAD_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
Definition aead.cpp:59
static std::unique_ptr< BlockCipherModePaddingMethod > create(std::string_view algo_spec)
Definition mode_pad.cpp:19
static std::unique_ptr< BlockCipher > create(std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< Cipher_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
virtual std::string provider() const
static std::unique_ptr< Cipher_Mode > create_or_throw(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
static std::vector< std::string > providers(std::string_view algo_spec)
std::string arg(size_t i) const
size_t arg_count() const
Definition scan_name.h:49
const std::string & algo_name() const
Definition scan_name.h:44
size_t arg_as_integer(size_t i, size_t def_value) const
static std::unique_ptr< StreamCipher > create(std::string_view algo_spec, std::string_view provider="")
std::vector< std::string > split_on(std::string_view str, char delim)
Definition parsing.cpp:111
std::unique_ptr< Cipher_Mode > make_commoncrypto_cipher_mode(std::string_view name, Cipher_Dir direction)
std::vector< std::string > parse_algorithm_name(std::string_view scan_name)
Definition parsing.cpp:57