Coverage Report

Created: 2021-06-10 10:30

/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/internal/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.31k
   {
54
1.31k
   if(auto aead = AEAD_Mode::create(algo, dir, provider))
55
1.31k
      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
2.57k
   {
64
2.57k
   BOTAN_UNUSED(provider);
65
2.57k
#if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
66
2.57k
   if(algo == "ChaCha20Poly1305")
67
57
      {
68
57
      if(dir == ENCRYPTION)
69
14
         return std::make_unique<ChaCha20Poly1305_Encryption>();
70
43
      else
71
43
         return std::make_unique<ChaCha20Poly1305_Decryption>();
72
73
2.52k
      }
74
2.52k
#endif
75
76
2.52k
   if(algo.find('/') != std::string::npos)
77
1.26k
      {
78
1.26k
      const std::vector<std::string> algo_parts = split_on(algo, '/');
79
1.26k
      const std::string cipher_name = algo_parts[0];
80
1.26k
      const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
81
82
1.26k
      if(mode_info.empty())
83
0
         return std::unique_ptr<AEAD_Mode>();
84
85
1.26k
      std::ostringstream mode_name;
86
87
1.26k
      mode_name << mode_info[0] << '(' << cipher_name;
88
1.89k
      for(size_t i = 1; i < mode_info.size(); ++i)
89
634
         mode_name << ',' << mode_info[i];
90
1.26k
      for(size_t i = 2; i < algo_parts.size(); ++i)
91
0
         mode_name << ',' << algo_parts[i];
92
1.26k
      mode_name << ')';
93
94
1.26k
      return AEAD_Mode::create(mode_name.str(), dir);
95
1.26k
      }
96
97
1.26k
#if defined(BOTAN_HAS_BLOCK_CIPHER)
98
99
1.26k
   SCAN_Name req(algo);
100
101
1.26k
   if(req.arg_count() == 0)
102
0
      {
103
0
      return std::unique_ptr<AEAD_Mode>();
104
0
      }
105
106
1.26k
   std::unique_ptr<BlockCipher> bc(BlockCipher::create(req.arg(0), provider));
107
108
1.26k
   if(!bc)
109
0
      {
110
0
      return std::unique_ptr<AEAD_Mode>();
111
0
      }
112
113
1.26k
#if defined(BOTAN_HAS_AEAD_CCM)
114
1.26k
   if(req.algo_name() == "CCM")
115
356
      {
116
356
      size_t tag_len = req.arg_as_integer(1, 16);
117
356
      size_t L_len = req.arg_as_integer(2, 3);
118
356
      if(dir == ENCRYPTION)
119
157
         return std::make_unique<CCM_Encryption>(std::move(bc), tag_len, L_len);
120
199
      else
121
199
         return std::make_unique<CCM_Decryption>(std::move(bc), tag_len, L_len);
122
905
      }
123
905
#endif
124
125
905
#if defined(BOTAN_HAS_AEAD_GCM)
126
905
   if(req.algo_name() == "GCM")
127
620
      {
128
620
      size_t tag_len = req.arg_as_integer(1, 16);
129
620
      if(dir == ENCRYPTION)
130
500
         return std::make_unique<GCM_Encryption>(std::move(bc), tag_len);
131
120
      else
132
120
         return std::make_unique<GCM_Decryption>(std::move(bc), tag_len);
133
285
      }
134
285
#endif
135
136
285
#if defined(BOTAN_HAS_AEAD_OCB)
137
285
   if(req.algo_name() == "OCB")
138
285
      {
139
285
      size_t tag_len = req.arg_as_integer(1, 16);
140
285
      if(dir == ENCRYPTION)
141
129
         return std::make_unique<OCB_Encryption>(std::move(bc), tag_len);
142
156
      else
143
156
         return std::make_unique<OCB_Decryption>(std::move(bc), tag_len);
144
0
      }
145
0
#endif
146
147
0
#if defined(BOTAN_HAS_AEAD_EAX)
148
0
   if(req.algo_name() == "EAX")
149
0
      {
150
0
      size_t tag_len = req.arg_as_integer(1, bc->block_size());
151
0
      if(dir == ENCRYPTION)
152
0
         return std::make_unique<EAX_Encryption>(std::move(bc), tag_len);
153
0
      else
154
0
         return std::make_unique<EAX_Decryption>(std::move(bc), tag_len);
155
0
      }
156
0
#endif
157
158
0
#if defined(BOTAN_HAS_AEAD_SIV)
159
0
   if(req.algo_name() == "SIV")
160
0
      {
161
0
      if(dir == ENCRYPTION)
162
0
         return std::make_unique<SIV_Encryption>(std::move(bc));
163
0
      else
164
0
         return std::make_unique<SIV_Decryption>(std::move(bc));
165
0
      }
166
0
#endif
167
168
0
#endif
169
170
0
   return std::unique_ptr<AEAD_Mode>();
171
0
   }
172
173
}