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