Coverage Report

Created: 2020-11-21 08:34

/src/botan/src/lib/modes/aead/aead.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2013,2015 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#include <botan/aead.h>
8
#include <botan/scan_name.h>
9
#include <botan/internal/parsing.h>
10
#include <sstream>
11
12
#if defined(BOTAN_HAS_BLOCK_CIPHER)
13
  #include <botan/block_cipher.h>
14
#endif
15
16
#if defined(BOTAN_HAS_AEAD_CCM)
17
  #include <botan/internal/ccm.h>
18
#endif
19
20
#if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
21
  #include <botan/internal/chacha20poly1305.h>
22
#endif
23
24
#if defined(BOTAN_HAS_AEAD_EAX)
25
  #include <botan/internal/eax.h>
26
#endif
27
28
#if defined(BOTAN_HAS_AEAD_GCM)
29
  #include <botan/internal/gcm.h>
30
#endif
31
32
#if defined(BOTAN_HAS_AEAD_OCB)
33
  #include <botan/internal/ocb.h>
34
#endif
35
36
#if defined(BOTAN_HAS_AEAD_SIV)
37
  #include <botan/internal/siv.h>
38
#endif
39
40
namespace Botan {
41
42
void AEAD_Mode::set_associated_data_n(size_t i, const uint8_t ad[], size_t ad_len)
43
0
   {
44
0
   if(i == 0)
45
0
      this->set_associated_data(ad, ad_len);
46
0
   else
47
0
      throw Invalid_Argument("AEAD '" + name() + "' does not support multiple associated data");
48
0
   }
49
50
std::unique_ptr<AEAD_Mode> AEAD_Mode::create_or_throw(const std::string& algo,
51
                                                      Cipher_Dir dir,
52
                                                      const std::string& provider)
53
1.90k
   {
54
1.90k
   if(auto aead = AEAD_Mode::create(algo, dir, provider))
55
1.90k
      return aead;
56
57
0
   throw Lookup_Error("AEAD", algo, provider);
58
0
   }
59
60
std::unique_ptr<AEAD_Mode> AEAD_Mode::create(const std::string& algo,
61
                                             Cipher_Dir dir,
62
                                             const std::string& provider)
63
3.68k
   {
64
3.68k
   BOTAN_UNUSED(provider);
65
3.68k
#if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
66
3.68k
   if(algo == "ChaCha20Poly1305")
67
115
      {
68
115
      if(dir == ENCRYPTION)
69
60
         return std::unique_ptr<AEAD_Mode>(new ChaCha20Poly1305_Encryption);
70
55
      else
71
55
         return std::unique_ptr<AEAD_Mode>(new ChaCha20Poly1305_Decryption);
72
73
3.57k
      }
74
3.57k
#endif
75
76
3.57k
   if(algo.find('/') != std::string::npos)
77
1.78k
      {
78
1.78k
      const std::vector<std::string> algo_parts = split_on(algo, '/');
79
1.78k
      const std::string cipher_name = algo_parts[0];
80
1.78k
      const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
81
82
1.78k
      if(mode_info.empty())
83
0
         return std::unique_ptr<AEAD_Mode>();
84
85
1.78k
      std::ostringstream alg_args;
86
87
1.78k
      alg_args << '(' << cipher_name;
88
2.57k
      for(size_t i = 1; i < mode_info.size(); ++i)
89
793
         alg_args << ',' << mode_info[i];
90
1.78k
      for(size_t i = 2; i < algo_parts.size(); ++i)
91
0
         alg_args << ',' << algo_parts[i];
92
1.78k
      alg_args << ')';
93
94
1.78k
      const std::string mode_name = mode_info[0] + alg_args.str();
95
1.78k
      return AEAD_Mode::create(mode_name, dir);
96
1.78k
      }
97
98
1.78k
#if defined(BOTAN_HAS_BLOCK_CIPHER)
99
100
1.78k
   SCAN_Name req(algo);
101
102
1.78k
   if(req.arg_count() == 0)
103
0
      {
104
0
      return std::unique_ptr<AEAD_Mode>();
105
0
      }
106
107
1.78k
   std::unique_ptr<BlockCipher> bc(BlockCipher::create(req.arg(0), provider));
108
109
1.78k
   if(!bc)
110
0
      {
111
0
      return std::unique_ptr<AEAD_Mode>();
112
0
      }
113
114
1.78k
#if defined(BOTAN_HAS_AEAD_CCM)
115
1.78k
   if(req.algo_name() == "CCM")
116
261
      {
117
261
      size_t tag_len = req.arg_as_integer(1, 16);
118
261
      size_t L_len = req.arg_as_integer(2, 3);
119
261
      if(dir == ENCRYPTION)
120
127
         return std::unique_ptr<AEAD_Mode>(new CCM_Encryption(bc.release(), tag_len, L_len));
121
134
      else
122
134
         return std::unique_ptr<AEAD_Mode>(new CCM_Decryption(bc.release(), tag_len, L_len));
123
1.52k
      }
124
1.52k
#endif
125
126
1.52k
#if defined(BOTAN_HAS_AEAD_GCM)
127
1.52k
   if(req.algo_name() == "GCM")
128
929
      {
129
929
      size_t tag_len = req.arg_as_integer(1, 16);
130
929
      if(dir == ENCRYPTION)
131
626
         return std::unique_ptr<AEAD_Mode>(new GCM_Encryption(bc.release(), tag_len));
132
303
      else
133
303
         return std::unique_ptr<AEAD_Mode>(new GCM_Decryption(bc.release(), tag_len));
134
596
      }
135
596
#endif
136
137
596
#if defined(BOTAN_HAS_AEAD_OCB)
138
596
   if(req.algo_name() == "OCB")
139
596
      {
140
596
      size_t tag_len = req.arg_as_integer(1, 16);
141
596
      if(dir == ENCRYPTION)
142
280
         return std::unique_ptr<AEAD_Mode>(new OCB_Encryption(bc.release(), tag_len));
143
316
      else
144
316
         return std::unique_ptr<AEAD_Mode>(new OCB_Decryption(bc.release(), tag_len));
145
0
      }
146
0
#endif
147
148
0
#if defined(BOTAN_HAS_AEAD_EAX)
149
0
   if(req.algo_name() == "EAX")
150
0
      {
151
0
      size_t tag_len = req.arg_as_integer(1, bc->block_size());
152
0
      if(dir == ENCRYPTION)
153
0
         return std::unique_ptr<AEAD_Mode>(new EAX_Encryption(bc.release(), tag_len));
154
0
      else
155
0
         return std::unique_ptr<AEAD_Mode>(new EAX_Decryption(bc.release(), tag_len));
156
0
      }
157
0
#endif
158
159
0
#if defined(BOTAN_HAS_AEAD_SIV)
160
0
   if(req.algo_name() == "SIV")
161
0
      {
162
0
      if(dir == ENCRYPTION)
163
0
         return std::unique_ptr<AEAD_Mode>(new SIV_Encryption(bc.release()));
164
0
      else
165
0
         return std::unique_ptr<AEAD_Mode>(new SIV_Decryption(bc.release()));
166
0
      }
167
0
#endif
168
169
0
#endif
170
171
0
   return std::unique_ptr<AEAD_Mode>();
172
0
   }
173
174
}