Coverage Report

Created: 2022-05-14 06:06

/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
740
   {
54
740
   if(auto aead = AEAD_Mode::create(algo, dir, provider))
55
740
      return aead;
56
57
0
   throw Lookup_Error("AEAD", algo, provider);
58
740
   }
59
60
std::unique_ptr<AEAD_Mode> AEAD_Mode::create(const std::string& algo,
61
                                             Cipher_Dir dir,
62
                                             const std::string& provider)
63
1.41k
   {
64
1.41k
   BOTAN_UNUSED(provider);
65
1.41k
#if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
66
1.41k
   if(algo == "ChaCha20Poly1305")
67
69
      {
68
69
      if(dir == ENCRYPTION)
69
15
         return std::make_unique<ChaCha20Poly1305_Encryption>();
70
54
      else
71
54
         return std::make_unique<ChaCha20Poly1305_Decryption>();
72
73
69
      }
74
1.34k
#endif
75
76
1.34k
   if(algo.find('/') != std::string::npos)
77
671
      {
78
671
      const std::vector<std::string> algo_parts = split_on(algo, '/');
79
671
      const std::string cipher_name = algo_parts[0];
80
671
      const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
81
82
671
      if(mode_info.empty())
83
0
         return std::unique_ptr<AEAD_Mode>();
84
85
671
      std::ostringstream mode_name;
86
87
671
      mode_name << mode_info[0] << '(' << cipher_name;
88
955
      for(size_t i = 1; i < mode_info.size(); ++i)
89
284
         mode_name << ',' << mode_info[i];
90
671
      for(size_t i = 2; i < algo_parts.size(); ++i)
91
0
         mode_name << ',' << algo_parts[i];
92
671
      mode_name << ')';
93
94
671
      return AEAD_Mode::create(mode_name.str(), dir);
95
671
      }
96
97
671
#if defined(BOTAN_HAS_BLOCK_CIPHER)
98
99
671
   SCAN_Name req(algo);
100
101
671
   if(req.arg_count() == 0)
102
0
      {
103
0
      return std::unique_ptr<AEAD_Mode>();
104
0
      }
105
106
671
   std::unique_ptr<BlockCipher> bc(BlockCipher::create(req.arg(0), provider));
107
108
671
   if(!bc)
109
0
      {
110
0
      return std::unique_ptr<AEAD_Mode>();
111
0
      }
112
113
671
#if defined(BOTAN_HAS_AEAD_CCM)
114
671
   if(req.algo_name() == "CCM")
115
55
      {
116
55
      size_t tag_len = req.arg_as_integer(1, 16);
117
55
      size_t L_len = req.arg_as_integer(2, 3);
118
55
      if(dir == ENCRYPTION)
119
12
         return std::make_unique<CCM_Encryption>(std::move(bc), tag_len, L_len);
120
43
      else
121
43
         return std::make_unique<CCM_Decryption>(std::move(bc), tag_len, L_len);
122
55
      }
123
616
#endif
124
125
616
#if defined(BOTAN_HAS_AEAD_GCM)
126
616
   if(req.algo_name() == "GCM")
127
379
      {
128
379
      size_t tag_len = req.arg_as_integer(1, 16);
129
379
      if(dir == ENCRYPTION)
130
260
         return std::make_unique<GCM_Encryption>(std::move(bc), tag_len);
131
119
      else
132
119
         return std::make_unique<GCM_Decryption>(std::move(bc), tag_len);
133
379
      }
134
237
#endif
135
136
237
#if defined(BOTAN_HAS_AEAD_OCB)
137
237
   if(req.algo_name() == "OCB")
138
237
      {
139
237
      size_t tag_len = req.arg_as_integer(1, 16);
140
237
      if(dir == ENCRYPTION)
141
99
         return std::make_unique<OCB_Encryption>(std::move(bc), tag_len);
142
138
      else
143
138
         return std::make_unique<OCB_Decryption>(std::move(bc), tag_len);
144
237
      }
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
}