Botan 3.14.1
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/algorithm_spec.h>
12#include <botan/internal/stream_mode.h>
13#include <memory>
14#include <utility>
15
16#if defined(BOTAN_HAS_BLOCK_CIPHER)
17 #include <botan/block_cipher.h>
18#endif
19
20#if defined(BOTAN_HAS_AEAD_MODES)
21 #include <botan/aead.h>
22#endif
23
24#if defined(BOTAN_HAS_MODE_CBC)
25 #include <botan/internal/cbc.h>
26#endif
27
28#if defined(BOTAN_HAS_MODE_CFB)
29 #include <botan/internal/cfb.h>
30#endif
31
32#if defined(BOTAN_HAS_MODE_XTS)
33 #include <botan/internal/xts.h>
34#endif
35
36#if defined(BOTAN_HAS_COMMONCRYPTO)
37 #include <botan/internal/commoncrypto.h>
38#endif
39
40namespace Botan {
41
42std::unique_ptr<Cipher_Mode> Cipher_Mode::create_or_throw(std::string_view algo,
43 Cipher_Dir direction,
44 std::string_view provider) {
45 if(auto mode = Cipher_Mode::create(algo, direction, provider)) {
46 return mode;
47 }
48
49 throw Lookup_Error("Cipher mode", algo, provider);
50}
51
52std::unique_ptr<Cipher_Mode> Cipher_Mode::create(std::string_view algo,
53 Cipher_Dir direction,
54 std::string_view provider) {
55#if defined(BOTAN_HAS_COMMONCRYPTO)
56 if(provider.empty() || provider == "commoncrypto") {
57 if(auto cm = make_commoncrypto_cipher_mode(algo, direction))
58 return cm;
59
60 if(!provider.empty())
61 return nullptr;
62 }
63#endif
64
65 if(provider != "base" && !provider.empty()) {
66 return nullptr;
67 }
68
70
71#if defined(BOTAN_HAS_STREAM_CIPHER)
72 // Stream ciphers such as CTR and OFB are also accepted in mode form, eg "AES-128/CTR-BE"
73 if(auto sc = StreamCipher::create(spec.canonical_form())) {
74 return std::make_unique<Stream_Cipher_Mode>(std::move(sc));
75 }
76#endif
77
78#if defined(BOTAN_HAS_AEAD_MODES)
79 if(auto aead = AEAD_Mode::create(algo, direction)) {
80 return aead;
81 }
82#endif
83
84#if defined(BOTAN_HAS_BLOCK_CIPHER)
85 #if defined(BOTAN_HAS_MODE_CBC)
86 if(auto m = spec.match("CBC({cipher},{padding:str=PKCS7})")) {
87 if(auto bc = BlockCipher::create(m->str("cipher"), provider)) {
88 const auto padding = m->str("padding");
89
90 if(padding == "CTS") {
91 if(direction == Cipher_Dir::Encryption) {
92 return std::make_unique<CTS_Encryption>(std::move(bc));
93 } else {
94 return std::make_unique<CTS_Decryption>(std::move(bc));
95 }
96 } else if(auto pad = BlockCipherModePaddingMethod::create(padding)) {
97 if(direction == Cipher_Dir::Encryption) {
98 return std::make_unique<CBC_Encryption>(std::move(bc), std::move(pad));
99 } else {
100 return std::make_unique<CBC_Decryption>(std::move(bc), std::move(pad));
101 }
102 }
103 }
104 }
105 #endif
106
107 #if defined(BOTAN_HAS_MODE_XTS)
108 if(auto m = spec.match("XTS({cipher})")) {
109 if(auto bc = BlockCipher::create(m->str("cipher"), provider)) {
110 if(direction == Cipher_Dir::Encryption) {
111 return std::make_unique<XTS_Encryption>(std::move(bc));
112 } else {
113 return std::make_unique<XTS_Decryption>(std::move(bc));
114 }
115 }
116 }
117 #endif
118
119 #if defined(BOTAN_HAS_MODE_CFB)
120 if(auto m = spec.match("CFB({cipher},{feedback_bits:int?})")) {
121 if(auto bc = BlockCipher::create(m->str("cipher"), provider)) {
122 const size_t feedback_bits = m->has("feedback_bits") ? m->integer("feedback_bits") : 8 * bc->block_size();
123 if(direction == Cipher_Dir::Encryption) {
124 return std::make_unique<CFB_Encryption>(std::move(bc), feedback_bits);
125 } else {
126 return std::make_unique<CFB_Decryption>(std::move(bc), feedback_bits);
127 }
128 }
129 }
130 #endif
131
132#endif
133
134 BOTAN_UNUSED(spec);
135
136 return std::unique_ptr<Cipher_Mode>();
137}
138
139//static
140std::vector<std::string> Cipher_Mode::providers(std::string_view algo_spec) {
141 const std::vector<std::string>& possible = {"base", "commoncrypto"};
142 std::vector<std::string> providers;
143 for(auto&& prov : possible) {
144 const auto mode = Cipher_Mode::create(algo_spec, Cipher_Dir::Encryption, prov);
145 if(mode) {
146 providers.push_back(prov); // available
147 }
148 }
149 return providers;
150}
151
152} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
static std::unique_ptr< AEAD_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
Definition aead.cpp:61
std::string canonical_form() const
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
static std::unique_ptr< BlockCipherModePaddingMethod > create(std::string_view algo_spec)
Definition mode_pad.cpp:26
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)
static std::unique_ptr< StreamCipher > create(std::string_view algo_spec, std::string_view provider="")
std::unique_ptr< Cipher_Mode > make_commoncrypto_cipher_mode(std::string_view name, Cipher_Dir direction)