• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.1
/src/lib/modes/cipher_mode.cpp
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 <memory>
14
#include <sstream>
15
#include <utility>
16

17
#if defined(BOTAN_HAS_BLOCK_CIPHER)
18
   #include <botan/block_cipher.h>
19
#endif
20

21
#if defined(BOTAN_HAS_AEAD_MODES)
22
   #include <botan/aead.h>
23
#endif
24

25
#if defined(BOTAN_HAS_MODE_CBC)
26
   #include <botan/internal/cbc.h>
27
#endif
28

29
#if defined(BOTAN_HAS_MODE_CFB)
30
   #include <botan/internal/cfb.h>
31
#endif
32

33
#if defined(BOTAN_HAS_MODE_XTS)
34
   #include <botan/internal/xts.h>
35
#endif
36

37
#if defined(BOTAN_HAS_COMMONCRYPTO)
38
   #include <botan/internal/commoncrypto.h>
39
#endif
40

41
namespace Botan {
42

43
std::unique_ptr<Cipher_Mode> Cipher_Mode::create_or_throw(std::string_view algo,
163✔
44
                                                          Cipher_Dir direction,
45
                                                          std::string_view provider) {
46
   if(auto mode = Cipher_Mode::create(algo, direction, provider)) {
163✔
47
      return mode;
162✔
48
   }
162✔
49

50
   throw Lookup_Error("Cipher mode", algo, provider);
1✔
51
}
52

53
std::unique_ptr<Cipher_Mode> Cipher_Mode::create(std::string_view algo,
20,418✔
54
                                                 Cipher_Dir direction,
55
                                                 std::string_view provider) {
56
#if defined(BOTAN_HAS_COMMONCRYPTO)
57
   if(provider.empty() || provider == "commoncrypto") {
58
      if(auto cm = make_commoncrypto_cipher_mode(algo, direction))
59
         return cm;
60

61
      if(!provider.empty())
62
         return nullptr;
63
   }
64
#endif
65

66
   if(provider != "base" && !provider.empty()) {
27,888✔
67
      return nullptr;
1,249✔
68
   }
69

70
#if defined(BOTAN_HAS_STREAM_CIPHER)
71
   if(auto sc = StreamCipher::create(algo)) {
19,169✔
72
      return std::make_unique<Stream_Cipher_Mode>(std::move(sc));
51✔
73
   }
51✔
74
#endif
75

76
#if defined(BOTAN_HAS_AEAD_MODES)
77
   if(auto aead = AEAD_Mode::create(algo, direction)) {
19,118✔
78
      return aead;
3,863✔
79
   }
3,863✔
80
#endif
81

82
   if(algo.find('/') != std::string::npos) {
15,255✔
83
      const std::vector<std::string> algo_parts = split_on(algo, '/');
7,494✔
84
      std::string_view cipher_name = algo_parts[0];
7,494✔
85
      const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
7,494✔
86

87
      if(mode_info.empty()) {
7,494✔
88
         return std::unique_ptr<Cipher_Mode>();
×
89
      }
90

91
      std::ostringstream mode_name;
7,494✔
92

93
      mode_name << mode_info[0] << '(' << cipher_name;
7,494✔
94
      for(size_t i = 1; i < mode_info.size(); ++i) {
7,726✔
95
         mode_name << ',' << mode_info[i];
232✔
96
      }
97
      for(size_t i = 2; i < algo_parts.size(); ++i) {
9,158✔
98
         mode_name << ',' << algo_parts[i];
1,664✔
99
      }
100
      mode_name << ')';
7,494✔
101

102
      return Cipher_Mode::create(mode_name.str(), direction, provider);
7,494✔
103
   }
7,494✔
104

105
#if defined(BOTAN_HAS_BLOCK_CIPHER)
106

107
   SCAN_Name spec(algo);
7,761✔
108

109
   if(spec.arg_count() == 0) {
7,761✔
110
      return std::unique_ptr<Cipher_Mode>();
1✔
111
   }
112

113
   auto bc = BlockCipher::create(spec.arg(0), provider);
7,760✔
114

115
   if(!bc) {
7,760✔
116
      return std::unique_ptr<Cipher_Mode>();
2✔
117
   }
118

119
   #if defined(BOTAN_HAS_MODE_CBC)
120
   if(spec.algo_name() == "CBC") {
7,758✔
121
      const std::string padding = spec.arg(1, "PKCS7");
2,878✔
122

123
      if(padding == "CTS") {
2,878✔
124
         if(direction == Cipher_Dir::Encryption) {
240✔
125
            return std::make_unique<CTS_Encryption>(std::move(bc));
144✔
126
         } else {
127
            return std::make_unique<CTS_Decryption>(std::move(bc));
96✔
128
         }
129
      } else {
130
         auto pad = BlockCipherModePaddingMethod::create(padding);
2,638✔
131

132
         if(pad) {
2,638✔
133
            if(direction == Cipher_Dir::Encryption) {
2,638✔
134
               return std::make_unique<CBC_Encryption>(std::move(bc), std::move(pad));
1,484✔
135
            } else {
136
               return std::make_unique<CBC_Decryption>(std::move(bc), std::move(pad));
1,154✔
137
            }
138
         }
139
      }
2,638✔
140
   }
2,878✔
141
   #endif
142

143
   #if defined(BOTAN_HAS_MODE_XTS)
144
   if(spec.algo_name() == "XTS") {
4,880✔
145
      if(direction == Cipher_Dir::Encryption) {
4,175✔
146
         return std::make_unique<XTS_Encryption>(std::move(bc));
2,505✔
147
      } else {
148
         return std::make_unique<XTS_Decryption>(std::move(bc));
1,670✔
149
      }
150
   }
151
   #endif
152

153
   #if defined(BOTAN_HAS_MODE_CFB)
154
   if(spec.algo_name() == "CFB") {
705✔
155
      const size_t feedback_bits = spec.arg_as_integer(1, 8 * bc->block_size());
705✔
156
      if(direction == Cipher_Dir::Encryption) {
705✔
157
         return std::make_unique<CFB_Encryption>(std::move(bc), feedback_bits);
340✔
158
      } else {
159
         return std::make_unique<CFB_Decryption>(std::move(bc), feedback_bits);
365✔
160
      }
161
   }
162
   #endif
163

164
#endif
165

166
   return std::unique_ptr<Cipher_Mode>();
×
167
}
7,761✔
168

169
//static
170
std::vector<std::string> Cipher_Mode::providers(std::string_view algo_spec) {
1,249✔
171
   const std::vector<std::string>& possible = {"base", "commoncrypto"};
1,249✔
172
   std::vector<std::string> providers;
1,249✔
173
   for(auto&& prov : possible) {
3,747✔
174
      auto mode = Cipher_Mode::create(algo_spec, Cipher_Dir::Encryption, prov);
2,498✔
175
      if(mode) {
2,498✔
176
         providers.push_back(prov);  // available
1,247✔
177
      }
178
   }
2,498✔
179
   return providers;
1,249✔
180
}
1,249✔
181

182
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc