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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

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

98.51
/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 <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

39
namespace Botan {
40

41
std::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

51
std::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
      auto commoncrypto_cipher = make_commoncrypto_cipher_mode(algo, direction);
57

58
      if(commoncrypto_cipher)
59
         return commoncrypto_cipher;
60

61
      if(!provider.empty())
62
         return std::unique_ptr<Cipher_Mode>();
63
   }
64
#endif
65

66
#if defined(BOTAN_HAS_STREAM_CIPHER)
67
   if(auto sc = StreamCipher::create(algo)) {
68
      return std::make_unique<Stream_Cipher_Mode>(std::move(sc));
69
   }
70
#endif
71

72
#if defined(BOTAN_HAS_AEAD_MODES)
73
   if(auto aead = AEAD_Mode::create(algo, direction)) {
74
      return aead;
75
   }
76
#endif
77

78
   if(algo.find('/') != std::string::npos) {
79
      const std::vector<std::string> algo_parts = split_on(algo, '/');
80
      std::string_view cipher_name = algo_parts[0];
81
      const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
82

83
      if(mode_info.empty()) {
84
         return std::unique_ptr<Cipher_Mode>();
85
      }
86

87
      std::ostringstream mode_name;
88

89
      mode_name << mode_info[0] << '(' << cipher_name;
90
      for(size_t i = 1; i < mode_info.size(); ++i) {
91
         mode_name << ',' << mode_info[i];
92
      }
93
      for(size_t i = 2; i < algo_parts.size(); ++i) {
94
         mode_name << ',' << algo_parts[i];
95
      }
96
      mode_name << ')';
97

98
      return Cipher_Mode::create(mode_name.str(), direction, provider);
99
   }
100

101
#if defined(BOTAN_HAS_BLOCK_CIPHER)
102

103
   SCAN_Name spec(algo);
104

105
   if(spec.arg_count() == 0) {
106
      return std::unique_ptr<Cipher_Mode>();
107
   }
108

109
   auto bc = BlockCipher::create(spec.arg(0), provider);
110

111
   if(!bc) {
112
      return std::unique_ptr<Cipher_Mode>();
113
   }
114

115
   #if defined(BOTAN_HAS_MODE_CBC)
116
   if(spec.algo_name() == "CBC") {
117
      const std::string padding = spec.arg(1, "PKCS7");
118

119
      if(padding == "CTS") {
120
         if(direction == Cipher_Dir::Encryption) {
121
            return std::make_unique<CTS_Encryption>(std::move(bc));
122
         } else {
123
            return std::make_unique<CTS_Decryption>(std::move(bc));
124
         }
125
      } else {
126
         auto pad = BlockCipherModePaddingMethod::create(padding);
127

128
         if(pad) {
129
            if(direction == Cipher_Dir::Encryption) {
130
               return std::make_unique<CBC_Encryption>(std::move(bc), std::move(pad));
131
            } else {
132
               return std::make_unique<CBC_Decryption>(std::move(bc), std::move(pad));
133
            }
134
         }
135
      }
136
   }
137
   #endif
138

139
   #if defined(BOTAN_HAS_MODE_XTS)
140
   if(spec.algo_name() == "XTS") {
141
      if(direction == Cipher_Dir::Encryption) {
142
         return std::make_unique<XTS_Encryption>(std::move(bc));
143
      } else {
144
         return std::make_unique<XTS_Decryption>(std::move(bc));
145
      }
146
   }
147
   #endif
148

149
   #if defined(BOTAN_HAS_MODE_CFB)
150
   if(spec.algo_name() == "CFB") {
151
      const size_t feedback_bits = spec.arg_as_integer(1, 8 * bc->block_size());
152
      if(direction == Cipher_Dir::Encryption) {
153
         return std::make_unique<CFB_Encryption>(std::move(bc), feedback_bits);
154
      } else {
155
         return std::make_unique<CFB_Decryption>(std::move(bc), feedback_bits);
156
      }
157
   }
158
   #endif
159

160
#endif
161

162
   return std::unique_ptr<Cipher_Mode>();
163
}
164

165
//static
166
std::vector<std::string> Cipher_Mode::providers(std::string_view algo_spec) {
167
   const std::vector<std::string>& possible = {"base", "commoncrypto"};
168
   std::vector<std::string> providers;
169
   for(auto&& prov : possible) {
170
      auto mode = Cipher_Mode::create(algo_spec, Cipher_Dir::Encryption, prov);
171
      if(mode) {
172
         providers.push_back(prov);  // available
173
      }
174
   }
175
   return providers;
176
}
177

178
}  // 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

© 2025 Coveralls, Inc