Coverage Report

Created: 2021-05-04 09:02

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